You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fcp.c 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <assert.h>
  31. #include <byteswap.h>
  32. #include <ipxe/refcnt.h>
  33. #include <ipxe/list.h>
  34. #include <ipxe/interface.h>
  35. #include <ipxe/xfer.h>
  36. #include <ipxe/iobuf.h>
  37. #include <ipxe/open.h>
  38. #include <ipxe/process.h>
  39. #include <ipxe/uri.h>
  40. #include <ipxe/acpi.h>
  41. #include <ipxe/scsi.h>
  42. #include <ipxe/device.h>
  43. #include <ipxe/edd.h>
  44. #include <ipxe/fc.h>
  45. #include <ipxe/fcels.h>
  46. #include <ipxe/fcp.h>
  47. /** @file
  48. *
  49. * Fibre Channel Protocol
  50. *
  51. */
  52. /* Disambiguate the various error causes */
  53. #define ERANGE_READ_DATA_ORDERING \
  54. __einfo_error ( EINFO_ERANGE_READ_DATA_ORDERING )
  55. #define EINFO_ERANGE_READ_DATA_ORDERING \
  56. __einfo_uniqify ( EINFO_ERANGE, 0x01, "Read data out of order" )
  57. #define ERANGE_READ_DATA_OVERRUN \
  58. __einfo_error ( EINFO_ERANGE_READ_DATA_OVERRUN )
  59. #define EINFO_ERANGE_READ_DATA_OVERRUN \
  60. __einfo_uniqify ( EINFO_ERANGE, 0x02, "Read data overrun" )
  61. #define ERANGE_WRITE_DATA_STUCK \
  62. __einfo_error ( EINFO_ERANGE_WRITE_DATA_STUCK )
  63. #define EINFO_ERANGE_WRITE_DATA_STUCK \
  64. __einfo_uniqify ( EINFO_ERANGE, 0x03, "Write data stuck" )
  65. #define ERANGE_WRITE_DATA_OVERRUN \
  66. __einfo_error ( EINFO_ERANGE_WRITE_DATA_OVERRUN )
  67. #define EINFO_ERANGE_WRITE_DATA_OVERRUN \
  68. __einfo_uniqify ( EINFO_ERANGE, 0x04, "Write data overrun" )
  69. #define ERANGE_DATA_UNDERRUN \
  70. __einfo_error ( EINFO_ERANGE_DATA_UNDERRUN )
  71. #define EINFO_ERANGE_DATA_UNDERRUN \
  72. __einfo_uniqify ( EINFO_ERANGE, 0x05, "Data underrun" )
  73. /******************************************************************************
  74. *
  75. * PRLI
  76. *
  77. ******************************************************************************
  78. */
  79. struct fc_els_prli_descriptor fcp_prli_descriptor __fc_els_prli_descriptor;
  80. /**
  81. * Transmit FCP PRLI
  82. *
  83. * @v els Fibre Channel ELS transaction
  84. * @ret rc Return status code
  85. */
  86. static int fcp_prli_tx ( struct fc_els *els ) {
  87. struct fcp_prli_service_parameters param;
  88. /* Build service parameter page */
  89. memset ( &param, 0, sizeof ( param ) );
  90. param.flags = htonl ( FCP_PRLI_NO_READ_RDY | FCP_PRLI_INITIATOR );
  91. return fc_els_prli_tx ( els, &fcp_prli_descriptor, &param );
  92. }
  93. /**
  94. * Receive FCP PRLI
  95. *
  96. * @v els Fibre Channel ELS transaction
  97. * @v frame ELS frame
  98. * @v len Length of ELS frame
  99. * @ret rc Return status code
  100. */
  101. static int fcp_prli_rx ( struct fc_els *els, void *data, size_t len ) {
  102. return fc_els_prli_rx ( els, &fcp_prli_descriptor, data, len );
  103. }
  104. /**
  105. * Detect FCP PRLI
  106. *
  107. * @v els Fibre Channel ELS transaction
  108. * @v data ELS frame
  109. * @v len Length of ELS frame
  110. * @ret rc Return status code
  111. */
  112. static int fcp_prli_detect ( struct fc_els *els, const void *data,
  113. size_t len ) {
  114. return fc_els_prli_detect ( els, &fcp_prli_descriptor, data, len );
  115. }
  116. /** FCP PRLI ELS handler */
  117. struct fc_els_handler fcp_prli_handler __fc_els_handler = {
  118. .name = "PRLI-FCP",
  119. .tx = fcp_prli_tx,
  120. .rx = fcp_prli_rx,
  121. .detect = fcp_prli_detect,
  122. };
  123. /** FCP PRLI descriptor */
  124. struct fc_els_prli_descriptor fcp_prli_descriptor __fc_els_prli_descriptor = {
  125. .type = FC_TYPE_FCP,
  126. .param_len = sizeof ( struct fcp_prli_service_parameters ),
  127. .handler = &fcp_prli_handler,
  128. };
  129. /******************************************************************************
  130. *
  131. * FCP devices and commands
  132. *
  133. ******************************************************************************
  134. */
  135. /** An FCP device */
  136. struct fcp_device {
  137. /** Reference count */
  138. struct refcnt refcnt;
  139. /** Fibre Channel upper-layer protocol user */
  140. struct fc_ulp_user user;
  141. /** SCSI command issuing interface */
  142. struct interface scsi;
  143. /** List of active commands */
  144. struct list_head fcpcmds;
  145. /** Fibre Channel WWN (for boot firmware table) */
  146. struct fc_name wwn;
  147. /** SCSI LUN (for boot firmware table) */
  148. struct scsi_lun lun;
  149. };
  150. /** An FCP command */
  151. struct fcp_command {
  152. /** Reference count */
  153. struct refcnt refcnt;
  154. /** FCP SCSI device */
  155. struct fcp_device *fcpdev;
  156. /** List of active commands */
  157. struct list_head list;
  158. /** SCSI command interface */
  159. struct interface scsi;
  160. /** Fibre Channel exchange interface */
  161. struct interface xchg;
  162. /** Send process */
  163. struct process process;
  164. /** Send current IU
  165. *
  166. * @v fcpcmd FCP command
  167. * @ret rc Return status code
  168. */
  169. int ( * send ) ( struct fcp_command *fcpcmd );
  170. /** SCSI command */
  171. struct scsi_cmd command;
  172. /** Data offset within command */
  173. size_t offset;
  174. /** Length of data remaining to be sent within this IU */
  175. size_t remaining;
  176. /** Exchange ID */
  177. uint16_t xchg_id;
  178. };
  179. /**
  180. * Get reference to FCP device
  181. *
  182. * @v fcpdev FCP device
  183. * @ret fcpdev FCP device
  184. */
  185. static inline __attribute__ (( always_inline )) struct fcp_device *
  186. fcpdev_get ( struct fcp_device *fcpdev ) {
  187. ref_get ( &fcpdev->refcnt );
  188. return fcpdev;
  189. }
  190. /**
  191. * Drop reference to FCP device
  192. *
  193. * @v fcpdev FCP device
  194. */
  195. static inline __attribute__ (( always_inline )) void
  196. fcpdev_put ( struct fcp_device *fcpdev ) {
  197. ref_put ( &fcpdev->refcnt );
  198. }
  199. /**
  200. * Get reference to FCP command
  201. *
  202. * @v fcpcmd FCP command
  203. * @ret fcpcmd FCP command
  204. */
  205. static inline __attribute__ (( always_inline )) struct fcp_command *
  206. fcpcmd_get ( struct fcp_command *fcpcmd ) {
  207. ref_get ( &fcpcmd->refcnt );
  208. return fcpcmd;
  209. }
  210. /**
  211. * Drop reference to FCP command
  212. *
  213. * @v fcpcmd FCP command
  214. */
  215. static inline __attribute__ (( always_inline )) void
  216. fcpcmd_put ( struct fcp_command *fcpcmd ) {
  217. ref_put ( &fcpcmd->refcnt );
  218. }
  219. /**
  220. * Start FCP command sending
  221. *
  222. * @v fcpcmd FCP command
  223. * @v send Send method
  224. */
  225. static inline __attribute__ (( always_inline )) void
  226. fcpcmd_start_send ( struct fcp_command *fcpcmd,
  227. int ( * send ) ( struct fcp_command *fcpcmd ) ) {
  228. fcpcmd->send = send;
  229. process_add ( &fcpcmd->process );
  230. }
  231. /**
  232. * Stop FCP command sending
  233. *
  234. * @v fcpcmd FCP command
  235. */
  236. static inline __attribute__ (( always_inline )) void
  237. fcpcmd_stop_send ( struct fcp_command *fcpcmd ) {
  238. process_del ( &fcpcmd->process );
  239. }
  240. /**
  241. * Free FCP command
  242. *
  243. * @v refcnt Reference count
  244. */
  245. static void fcpcmd_free ( struct refcnt *refcnt ) {
  246. struct fcp_command *fcpcmd =
  247. container_of ( refcnt, struct fcp_command, refcnt );
  248. /* Remove from list of commands */
  249. list_del ( &fcpcmd->list );
  250. fcpdev_put ( fcpcmd->fcpdev );
  251. /* Free command */
  252. free ( fcpcmd );
  253. }
  254. /**
  255. * Close FCP command
  256. *
  257. * @v fcpcmd FCP command
  258. * @v rc Reason for close
  259. */
  260. static void fcpcmd_close ( struct fcp_command *fcpcmd, int rc ) {
  261. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  262. if ( rc != 0 ) {
  263. DBGC ( fcpdev, "FCP %p xchg %04x closed: %s\n",
  264. fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  265. }
  266. /* Stop sending */
  267. fcpcmd_stop_send ( fcpcmd );
  268. /* Shut down interfaces */
  269. intf_shutdown ( &fcpcmd->scsi, rc );
  270. intf_shutdown ( &fcpcmd->xchg, rc );
  271. }
  272. /**
  273. * Close FCP command in error
  274. *
  275. * @v fcpcmd FCP command
  276. * @v rc Reason for close
  277. */
  278. static void fcpcmd_close_err ( struct fcp_command *fcpcmd, int rc ) {
  279. if ( rc == 0 )
  280. rc = -EPIPE;
  281. fcpcmd_close ( fcpcmd, rc );
  282. }
  283. /**
  284. * Send FCP command IU
  285. *
  286. * @v fcpcmd FCP command
  287. * @ret rc Return status code
  288. */
  289. static int fcpcmd_send_cmnd ( struct fcp_command *fcpcmd ) {
  290. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  291. struct scsi_cmd *command = &fcpcmd->command;
  292. struct io_buffer *iobuf;
  293. struct fcp_cmnd *cmnd;
  294. struct xfer_metadata meta;
  295. int rc;
  296. /* Sanity check */
  297. if ( command->data_in_len && command->data_out_len ) {
  298. DBGC ( fcpdev, "FCP %p xchg %04x cannot handle bidirectional "
  299. "command\n", fcpdev, fcpcmd->xchg_id );
  300. return -ENOTSUP;
  301. }
  302. /* Allocate I/O buffer */
  303. iobuf = xfer_alloc_iob ( &fcpcmd->xchg, sizeof ( *cmnd ) );
  304. if ( ! iobuf ) {
  305. DBGC ( fcpdev, "FCP %p xchg %04x cannot allocate command IU\n",
  306. fcpdev, fcpcmd->xchg_id );
  307. return -ENOMEM;
  308. }
  309. /* Construct command IU frame */
  310. cmnd = iob_put ( iobuf, sizeof ( *cmnd ) );
  311. memset ( cmnd, 0, sizeof ( *cmnd ) );
  312. memcpy ( &cmnd->lun, &command->lun, sizeof ( cmnd->lun ) );
  313. assert ( ! ( command->data_in_len && command->data_out_len ) );
  314. if ( command->data_in_len )
  315. cmnd->dirn |= FCP_CMND_RDDATA;
  316. if ( command->data_out_len )
  317. cmnd->dirn |= FCP_CMND_WRDATA;
  318. memcpy ( &cmnd->cdb, &fcpcmd->command.cdb, sizeof ( cmnd->cdb ) );
  319. cmnd->len = htonl ( command->data_in_len + command->data_out_len );
  320. memset ( &meta, 0, sizeof ( meta ) );
  321. meta.flags = ( XFER_FL_CMD_STAT | XFER_FL_OVER );
  322. DBGC2 ( fcpdev, "FCP %p xchg %04x CMND " SCSI_CDB_FORMAT " %04x\n",
  323. fcpdev, fcpcmd->xchg_id, SCSI_CDB_DATA ( cmnd->cdb ),
  324. ntohl ( cmnd->len ) );
  325. /* No further data to send within this IU */
  326. fcpcmd_stop_send ( fcpcmd );
  327. /* Send command IU frame */
  328. if ( ( rc = xfer_deliver ( &fcpcmd->xchg, iob_disown ( iobuf ),
  329. &meta ) ) != 0 ) {
  330. DBGC ( fcpdev, "FCP %p xchg %04x cannot deliver command IU: "
  331. "%s\n", fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  332. return rc;
  333. }
  334. return 0;
  335. }
  336. /**
  337. * Handle FCP read data IU
  338. *
  339. * @v fcpcmd FCP command
  340. * @v iobuf I/O buffer
  341. * @v meta Data transfer metadata
  342. * @ret rc Return status code
  343. */
  344. static int fcpcmd_recv_rddata ( struct fcp_command *fcpcmd,
  345. struct io_buffer *iobuf,
  346. struct xfer_metadata *meta ) {
  347. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  348. struct scsi_cmd *command = &fcpcmd->command;
  349. size_t offset = meta->offset;
  350. size_t len = iob_len ( iobuf );
  351. int rc;
  352. /* Sanity checks */
  353. if ( ! ( meta->flags & XFER_FL_ABS_OFFSET ) ) {
  354. DBGC ( fcpdev, "FCP %p xchg %04x read data missing offset\n",
  355. fcpdev, fcpcmd->xchg_id );
  356. rc = -ERANGE_READ_DATA_ORDERING;
  357. goto done;
  358. }
  359. if ( offset != fcpcmd->offset ) {
  360. DBGC ( fcpdev, "FCP %p xchg %04x read data out of order "
  361. "(expected %zd, received %zd)\n",
  362. fcpdev, fcpcmd->xchg_id, fcpcmd->offset, offset );
  363. rc = -ERANGE_READ_DATA_ORDERING;
  364. goto done;
  365. }
  366. if ( ( offset + len ) > command->data_in_len ) {
  367. DBGC ( fcpdev, "FCP %p xchg %04x read data overrun (max %zd, "
  368. "received %zd)\n", fcpdev, fcpcmd->xchg_id,
  369. command->data_in_len, ( offset + len ) );
  370. rc = -ERANGE_READ_DATA_OVERRUN;
  371. goto done;
  372. }
  373. DBGC2 ( fcpdev, "FCP %p xchg %04x RDDATA [%08zx,%08zx)\n",
  374. fcpdev, fcpcmd->xchg_id, offset, ( offset + len ) );
  375. /* Copy to user buffer */
  376. copy_to_user ( command->data_in, offset, iobuf->data, len );
  377. fcpcmd->offset += len;
  378. assert ( fcpcmd->offset <= command->data_in_len );
  379. rc = 0;
  380. done:
  381. free_iob ( iobuf );
  382. return rc;
  383. }
  384. /**
  385. * Send FCP write data IU
  386. *
  387. * @v fcpcmd FCP command
  388. * @ret rc Return status code
  389. */
  390. static int fcpcmd_send_wrdata ( struct fcp_command *fcpcmd ) {
  391. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  392. struct scsi_cmd *command = &fcpcmd->command;
  393. struct io_buffer *iobuf;
  394. struct xfer_metadata meta;
  395. size_t len;
  396. int rc;
  397. /* Calculate length to be sent */
  398. len = xfer_window ( &fcpcmd->xchg );
  399. if ( len > fcpcmd->remaining )
  400. len = fcpcmd->remaining;
  401. /* Sanity checks */
  402. if ( len == 0 ) {
  403. DBGC ( fcpdev, "FCP %p xchg %04x write data stuck\n",
  404. fcpdev, fcpcmd->xchg_id );
  405. return -ERANGE_WRITE_DATA_STUCK;
  406. }
  407. if ( ( fcpcmd->offset + len ) > command->data_out_len ) {
  408. DBGC ( fcpdev, "FCP %p xchg %04x write data overrun (max %zd, "
  409. "requested %zd)\n", fcpdev, fcpcmd->xchg_id,
  410. command->data_out_len, ( fcpcmd->offset + len ) );
  411. return -ERANGE_WRITE_DATA_OVERRUN;
  412. }
  413. /* Allocate I/O buffer */
  414. iobuf = xfer_alloc_iob ( &fcpcmd->xchg, len );
  415. if ( ! iobuf ) {
  416. DBGC ( fcpdev, "FCP %p xchg %04x cannot allocate write data "
  417. "IU for %zd bytes\n", fcpdev, fcpcmd->xchg_id, len );
  418. return -ENOMEM;
  419. }
  420. /* Construct data IU frame */
  421. copy_from_user ( iob_put ( iobuf, len ), command->data_out,
  422. fcpcmd->offset, len );
  423. memset ( &meta, 0, sizeof ( meta ) );
  424. meta.flags = ( XFER_FL_RESPONSE | XFER_FL_ABS_OFFSET );
  425. meta.offset = fcpcmd->offset;
  426. DBGC2 ( fcpdev, "FCP %p xchg %04x WRDATA [%08zx,%04zx)\n",
  427. fcpdev, fcpcmd->xchg_id, fcpcmd->offset,
  428. ( fcpcmd->offset + iob_len ( iobuf ) ) );
  429. /* Calculate amount of data remaining to be sent within this IU */
  430. assert ( len <= fcpcmd->remaining );
  431. fcpcmd->offset += len;
  432. fcpcmd->remaining -= len;
  433. assert ( fcpcmd->offset <= command->data_out_len );
  434. if ( fcpcmd->remaining == 0 ) {
  435. fcpcmd_stop_send ( fcpcmd );
  436. meta.flags |= XFER_FL_OVER;
  437. }
  438. /* Send data IU frame */
  439. if ( ( rc = xfer_deliver ( &fcpcmd->xchg, iob_disown ( iobuf ),
  440. &meta ) ) != 0 ) {
  441. DBGC ( fcpdev, "FCP %p xchg %04x cannot deliver write data "
  442. "IU: %s\n", fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  443. return rc;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * Handle FCP transfer ready IU
  449. *
  450. * @v fcpcmd FCP command
  451. * @v iobuf I/O buffer
  452. * @v meta Data transfer metadata
  453. * @ret rc Return status code
  454. */
  455. static int fcpcmd_recv_xfer_rdy ( struct fcp_command *fcpcmd,
  456. struct io_buffer *iobuf,
  457. struct xfer_metadata *meta __unused ) {
  458. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  459. struct fcp_xfer_rdy *xfer_rdy = iobuf->data;
  460. int rc;
  461. /* Sanity checks */
  462. if ( iob_len ( iobuf ) != sizeof ( *xfer_rdy ) ) {
  463. DBGC ( fcpdev, "FCP %p xchg %04x received invalid transfer "
  464. "ready IU:\n", fcpdev, fcpcmd->xchg_id );
  465. DBGC_HDA ( fcpdev, 0, iobuf->data, iob_len ( iobuf ) );
  466. rc = -EPROTO;
  467. goto done;
  468. }
  469. if ( ntohl ( xfer_rdy->offset ) != fcpcmd->offset ) {
  470. /* We do not advertise out-of-order delivery */
  471. DBGC ( fcpdev, "FCP %p xchg %04x cannot support out-of-order "
  472. "delivery (expected %zd, requested %d)\n",
  473. fcpdev, fcpcmd->xchg_id, fcpcmd->offset,
  474. ntohl ( xfer_rdy->offset ) );
  475. rc = -EPROTO;
  476. goto done;
  477. }
  478. DBGC2 ( fcpdev, "FCP %p xchg %04x XFER_RDY [%08x,%08x)\n",
  479. fcpdev, fcpcmd->xchg_id, ntohl ( xfer_rdy->offset ),
  480. ( ntohl ( xfer_rdy->offset ) + ntohl ( xfer_rdy->len ) ) );
  481. /* Start sending requested data */
  482. fcpcmd->remaining = ntohl ( xfer_rdy->len );
  483. fcpcmd_start_send ( fcpcmd, fcpcmd_send_wrdata );
  484. rc = 0;
  485. done:
  486. free_iob ( iobuf );
  487. return rc;
  488. }
  489. /**
  490. * Handle FCP response IU
  491. *
  492. * @v fcpcmd FCP command
  493. * @v iobuf I/O buffer
  494. * @v meta Data transfer metadata
  495. * @ret rc Return status code
  496. */
  497. static int fcpcmd_recv_rsp ( struct fcp_command *fcpcmd,
  498. struct io_buffer *iobuf,
  499. struct xfer_metadata *meta __unused ) {
  500. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  501. struct scsi_cmd *command = &fcpcmd->command;
  502. struct fcp_rsp *rsp = iobuf->data;
  503. struct scsi_rsp response;
  504. int rc;
  505. /* Sanity check */
  506. if ( ( iob_len ( iobuf ) < sizeof ( *rsp ) ) ||
  507. ( iob_len ( iobuf ) < ( sizeof ( *rsp ) +
  508. fcp_rsp_response_data_len ( rsp ) +
  509. fcp_rsp_sense_data_len ( rsp ) ) ) ) {
  510. DBGC ( fcpdev, "FCP %p xchg %04x received invalid response "
  511. "IU:\n", fcpdev, fcpcmd->xchg_id );
  512. DBGC_HDA ( fcpdev, 0, iobuf->data, iob_len ( iobuf ) );
  513. rc = -EPROTO;
  514. goto done;
  515. }
  516. DBGC2 ( fcpdev, "FCP %p xchg %04x RSP stat %02x resid %08x flags %02x"
  517. "%s%s%s%s\n", fcpdev, fcpcmd->xchg_id, rsp->status,
  518. ntohl ( rsp->residual ), rsp->flags,
  519. ( ( rsp->flags & FCP_RSP_RESPONSE_LEN_VALID ) ? " resp" : "" ),
  520. ( ( rsp->flags & FCP_RSP_SENSE_LEN_VALID ) ? " sense" : "" ),
  521. ( ( rsp->flags & FCP_RSP_RESIDUAL_OVERRUN ) ? " over" : "" ),
  522. ( ( rsp->flags & FCP_RSP_RESIDUAL_UNDERRUN ) ? " under" : "" ));
  523. if ( fcp_rsp_response_data ( rsp ) ) {
  524. DBGC2 ( fcpdev, "FCP %p xchg %04x response data:\n",
  525. fcpdev, fcpcmd->xchg_id );
  526. DBGC2_HDA ( fcpdev, 0, fcp_rsp_response_data ( rsp ),
  527. fcp_rsp_response_data_len ( rsp ) );
  528. }
  529. if ( fcp_rsp_sense_data ( rsp ) ) {
  530. DBGC2 ( fcpdev, "FCP %p xchg %04x sense data:\n",
  531. fcpdev, fcpcmd->xchg_id );
  532. DBGC2_HDA ( fcpdev, 0, fcp_rsp_sense_data ( rsp ),
  533. fcp_rsp_sense_data_len ( rsp ) );
  534. }
  535. /* Check for locally-detected command underrun */
  536. if ( ( rsp->status == 0 ) &&
  537. ( fcpcmd->offset != ( command->data_in_len +
  538. command->data_out_len ) ) ) {
  539. DBGC ( fcpdev, "FCP %p xchg %04x data underrun (expected %zd, "
  540. "got %zd)\n", fcpdev, fcpcmd->xchg_id,
  541. ( command->data_in_len + command->data_out_len ),
  542. fcpcmd->offset );
  543. rc = -ERANGE_DATA_UNDERRUN;
  544. goto done;
  545. }
  546. /* Build SCSI response */
  547. memset ( &response, 0, sizeof ( response ) );
  548. response.status = rsp->status;
  549. if ( rsp->flags & ( FCP_RSP_RESIDUAL_OVERRUN |
  550. FCP_RSP_RESIDUAL_UNDERRUN ) ) {
  551. response.overrun = ntohl ( rsp->residual );
  552. if ( rsp->flags & FCP_RSP_RESIDUAL_UNDERRUN )
  553. response.overrun = -response.overrun;
  554. }
  555. scsi_parse_sense ( fcp_rsp_sense_data ( rsp ),
  556. fcp_rsp_sense_data_len ( rsp ), &response.sense );
  557. /* Free buffer before sending response, to minimise
  558. * out-of-memory errors.
  559. */
  560. free_iob ( iob_disown ( iobuf ) );
  561. /* Send SCSI response */
  562. scsi_response ( &fcpcmd->scsi, &response );
  563. /* Terminate command */
  564. fcpcmd_close ( fcpcmd, 0 );
  565. rc = 0;
  566. done:
  567. free_iob ( iobuf );
  568. return rc;
  569. }
  570. /**
  571. * Handle unknown FCP IU
  572. *
  573. * @v fcpcmd FCP command
  574. * @v iobuf I/O buffer
  575. * @v meta Data transfer metadata
  576. * @ret rc Return status code
  577. */
  578. static int fcpcmd_recv_unknown ( struct fcp_command *fcpcmd,
  579. struct io_buffer *iobuf,
  580. struct xfer_metadata *meta __unused ) {
  581. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  582. DBGC ( fcpdev, "FCP %p xchg %04x received unknown IU:\n",
  583. fcpdev, fcpcmd->xchg_id );
  584. DBGC_HDA ( fcpdev, 0, iobuf->data, iob_len ( iobuf ) );
  585. free_iob ( iobuf );
  586. return -EPROTO;
  587. }
  588. /**
  589. * Transmit FCP frame
  590. *
  591. * @v fcpcmd FCP command
  592. */
  593. static void fcpcmd_step ( struct fcp_command *fcpcmd ) {
  594. int rc;
  595. /* Send the current IU */
  596. if ( ( rc = fcpcmd->send ( fcpcmd ) ) != 0 ) {
  597. /* Treat failure as a fatal error */
  598. fcpcmd_close ( fcpcmd, rc );
  599. }
  600. }
  601. /**
  602. * Receive FCP frame
  603. *
  604. * @v fcpcmd FCP command
  605. * @v iobuf I/O buffer
  606. * @v meta Data transfer metadata
  607. * @ret rc Return status code
  608. */
  609. static int fcpcmd_deliver ( struct fcp_command *fcpcmd,
  610. struct io_buffer *iobuf,
  611. struct xfer_metadata *meta ) {
  612. int ( * fcpcmd_recv ) ( struct fcp_command *fcpcmd,
  613. struct io_buffer *iobuf,
  614. struct xfer_metadata *meta );
  615. int rc;
  616. /* Determine handler */
  617. switch ( meta->flags & ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ) ) {
  618. case ( XFER_FL_RESPONSE ) :
  619. fcpcmd_recv = fcpcmd_recv_rddata;
  620. break;
  621. case ( XFER_FL_CMD_STAT ) :
  622. fcpcmd_recv = fcpcmd_recv_xfer_rdy;
  623. break;
  624. case ( XFER_FL_CMD_STAT | XFER_FL_RESPONSE ) :
  625. fcpcmd_recv = fcpcmd_recv_rsp;
  626. break;
  627. default:
  628. fcpcmd_recv = fcpcmd_recv_unknown;
  629. break;
  630. }
  631. /* Handle IU */
  632. if ( ( rc = fcpcmd_recv ( fcpcmd, iob_disown ( iobuf ), meta ) ) != 0 ){
  633. /* Treat any error as fatal to the command */
  634. fcpcmd_close ( fcpcmd, rc );
  635. }
  636. return rc;
  637. }
  638. /** FCP command SCSI interface operations */
  639. static struct interface_operation fcpcmd_scsi_op[] = {
  640. INTF_OP ( intf_close, struct fcp_command *, fcpcmd_close ),
  641. };
  642. /** FCP command SCSI interface descriptor */
  643. static struct interface_descriptor fcpcmd_scsi_desc =
  644. INTF_DESC_PASSTHRU ( struct fcp_command, scsi, fcpcmd_scsi_op, xchg );
  645. /** FCP command Fibre Channel exchange interface operations */
  646. static struct interface_operation fcpcmd_xchg_op[] = {
  647. INTF_OP ( xfer_deliver, struct fcp_command *, fcpcmd_deliver ),
  648. INTF_OP ( intf_close, struct fcp_command *, fcpcmd_close_err ),
  649. };
  650. /** FCP command Fibre Channel exchange interface descriptor */
  651. static struct interface_descriptor fcpcmd_xchg_desc =
  652. INTF_DESC_PASSTHRU ( struct fcp_command, xchg, fcpcmd_xchg_op, scsi );
  653. /** FCP command process descriptor */
  654. static struct process_descriptor fcpcmd_process_desc =
  655. PROC_DESC ( struct fcp_command, process, fcpcmd_step );
  656. /**
  657. * Issue FCP SCSI command
  658. *
  659. * @v fcpdev FCP device
  660. * @v parent Parent interface
  661. * @v command SCSI command
  662. * @ret tag Command tag, or negative error
  663. */
  664. static int fcpdev_scsi_command ( struct fcp_device *fcpdev,
  665. struct interface *parent,
  666. struct scsi_cmd *command ) {
  667. struct fcp_prli_service_parameters *param = fcpdev->user.ulp->param;
  668. struct fcp_command *fcpcmd;
  669. int xchg_id;
  670. int rc;
  671. /* Check link */
  672. if ( ( rc = fcpdev->user.ulp->link.rc ) != 0 ) {
  673. DBGC ( fcpdev, "FCP %p could not issue command while link is "
  674. "down: %s\n", fcpdev, strerror ( rc ) );
  675. goto err_link;
  676. }
  677. /* Check target capability */
  678. assert ( param != NULL );
  679. assert ( fcpdev->user.ulp->param_len >= sizeof ( *param ) );
  680. if ( ! ( param->flags & htonl ( FCP_PRLI_TARGET ) ) ) {
  681. DBGC ( fcpdev, "FCP %p could not issue command: not a target\n",
  682. fcpdev );
  683. rc = -ENOTTY;
  684. goto err_target;
  685. }
  686. /* Allocate and initialise structure */
  687. fcpcmd = zalloc ( sizeof ( *fcpcmd ) );
  688. if ( ! fcpcmd ) {
  689. rc = -ENOMEM;
  690. goto err_zalloc;
  691. }
  692. ref_init ( &fcpcmd->refcnt, fcpcmd_free );
  693. intf_init ( &fcpcmd->scsi, &fcpcmd_scsi_desc, &fcpcmd->refcnt );
  694. intf_init ( &fcpcmd->xchg, &fcpcmd_xchg_desc, &fcpcmd->refcnt );
  695. process_init_stopped ( &fcpcmd->process, &fcpcmd_process_desc,
  696. &fcpcmd->refcnt );
  697. fcpcmd->fcpdev = fcpdev_get ( fcpdev );
  698. list_add ( &fcpcmd->list, &fcpdev->fcpcmds );
  699. memcpy ( &fcpcmd->command, command, sizeof ( fcpcmd->command ) );
  700. /* Create new exchange */
  701. if ( ( xchg_id = fc_xchg_originate ( &fcpcmd->xchg,
  702. fcpdev->user.ulp->peer->port,
  703. &fcpdev->user.ulp->peer->port_id,
  704. FC_TYPE_FCP ) ) < 0 ) {
  705. rc = xchg_id;
  706. DBGC ( fcpdev, "FCP %p could not create exchange: %s\n",
  707. fcpdev, strerror ( rc ) );
  708. goto err_xchg_originate;
  709. }
  710. fcpcmd->xchg_id = xchg_id;
  711. /* Start sending command IU */
  712. fcpcmd_start_send ( fcpcmd, fcpcmd_send_cmnd );
  713. /* Attach to parent interface, mortalise self, and return */
  714. intf_plug_plug ( &fcpcmd->scsi, parent );
  715. ref_put ( &fcpcmd->refcnt );
  716. return ( FCP_TAG_MAGIC | fcpcmd->xchg_id );
  717. err_xchg_originate:
  718. fcpcmd_close ( fcpcmd, rc );
  719. ref_put ( &fcpcmd->refcnt );
  720. err_zalloc:
  721. err_target:
  722. err_link:
  723. return rc;
  724. }
  725. /**
  726. * Close FCP device
  727. *
  728. * @v fcpdev FCP device
  729. * @v rc Reason for close
  730. */
  731. static void fcpdev_close ( struct fcp_device *fcpdev, int rc ) {
  732. struct fcp_command *fcpcmd;
  733. struct fcp_command *tmp;
  734. DBGC ( fcpdev, "FCP %p closed: %s\n", fcpdev, strerror ( rc ) );
  735. /* Shut down interfaces */
  736. intf_shutdown ( &fcpdev->scsi, rc );
  737. /* Shut down any active commands */
  738. list_for_each_entry_safe ( fcpcmd, tmp, &fcpdev->fcpcmds, list ) {
  739. fcpcmd_get ( fcpcmd );
  740. fcpcmd_close ( fcpcmd, rc );
  741. fcpcmd_put ( fcpcmd );
  742. }
  743. /* Drop reference to ULP */
  744. fc_ulp_detach ( &fcpdev->user );
  745. }
  746. /**
  747. * Check FCP device flow-control window
  748. *
  749. * @v fcpdev FCP device
  750. * @ret len Length of window
  751. */
  752. static size_t fcpdev_window ( struct fcp_device *fcpdev ) {
  753. return ( fc_link_ok ( &fcpdev->user.ulp->link ) ?
  754. ~( ( size_t ) 0 ) : 0 );
  755. }
  756. /**
  757. * Describe FCP device using EDD
  758. *
  759. * @v fcpdev FCP device
  760. * @v type EDD interface type
  761. * @v path EDD device path
  762. * @ret rc Return status code
  763. */
  764. static int fcpdev_edd_describe ( struct fcp_device *fcpdev,
  765. struct edd_interface_type *type,
  766. union edd_device_path *path ) {
  767. union {
  768. struct fc_name fc;
  769. uint64_t u64;
  770. } wwn;
  771. union {
  772. struct scsi_lun scsi;
  773. uint64_t u64;
  774. } lun;
  775. type->type = cpu_to_le64 ( EDD_INTF_TYPE_FIBRE );
  776. memcpy ( &wwn.fc, &fcpdev->wwn, sizeof ( wwn.fc ) );
  777. path->fibre.wwn = be64_to_cpu ( wwn.u64 );
  778. memcpy ( &lun.scsi, &fcpdev->lun, sizeof ( lun.scsi ) );
  779. path->fibre.lun = be64_to_cpu ( lun.u64 );
  780. return 0;
  781. }
  782. /**
  783. * Identify device underlying FCP device
  784. *
  785. * @v fcpdev FCP device
  786. * @ret device Underlying device
  787. */
  788. static struct device * fcpdev_identify_device ( struct fcp_device *fcpdev ) {
  789. /* We know the underlying device only if the link is up;
  790. * otherwise we don't have a port to examine.
  791. */
  792. if ( ! fc_link_ok ( &fcpdev->user.ulp->link ) ) {
  793. DBGC ( fcpdev, "FCP %p doesn't know underlying device "
  794. "until link is up\n", fcpdev );
  795. return NULL;
  796. }
  797. /* Hand off to port's transport interface */
  798. assert ( fcpdev->user.ulp->peer->port != NULL );
  799. return identify_device ( &fcpdev->user.ulp->peer->port->transport );
  800. }
  801. /** FCP device SCSI interface operations */
  802. static struct interface_operation fcpdev_scsi_op[] = {
  803. INTF_OP ( scsi_command, struct fcp_device *, fcpdev_scsi_command ),
  804. INTF_OP ( xfer_window, struct fcp_device *, fcpdev_window ),
  805. INTF_OP ( intf_close, struct fcp_device *, fcpdev_close ),
  806. INTF_OP ( edd_describe, struct fcp_device *, fcpdev_edd_describe ),
  807. INTF_OP ( identify_device, struct fcp_device *,
  808. fcpdev_identify_device ),
  809. };
  810. /** FCP device SCSI interface descriptor */
  811. static struct interface_descriptor fcpdev_scsi_desc =
  812. INTF_DESC ( struct fcp_device, scsi, fcpdev_scsi_op );
  813. /**
  814. * Examine FCP ULP link state
  815. *
  816. * @v user Fibre Channel upper-layer protocol user
  817. */
  818. static void fcpdev_examine ( struct fc_ulp_user *user ) {
  819. struct fcp_device *fcpdev =
  820. container_of ( user, struct fcp_device, user );
  821. if ( fc_link_ok ( &fcpdev->user.ulp->link ) ) {
  822. DBGC ( fcpdev, "FCP %p link is up\n", fcpdev );
  823. } else {
  824. DBGC ( fcpdev, "FCP %p link is down: %s\n",
  825. fcpdev, strerror ( fcpdev->user.ulp->link.rc ) );
  826. }
  827. /* Notify SCSI layer of window change */
  828. xfer_window_changed ( &fcpdev->scsi );
  829. }
  830. /**
  831. * Open FCP device
  832. *
  833. * @v parent Parent interface
  834. * @v wwn Fibre Channel WWN
  835. * @v lun SCSI LUN
  836. * @ret rc Return status code
  837. */
  838. static int fcpdev_open ( struct interface *parent, struct fc_name *wwn,
  839. struct scsi_lun *lun ) {
  840. struct fc_ulp *ulp;
  841. struct fcp_device *fcpdev;
  842. int rc;
  843. /* Get Fibre Channel ULP interface */
  844. ulp = fc_ulp_get_wwn_type ( wwn, FC_TYPE_FCP );
  845. if ( ! ulp ) {
  846. rc = -ENOMEM;
  847. goto err_ulp_get;
  848. }
  849. /* Allocate and initialise structure */
  850. fcpdev = zalloc ( sizeof ( *fcpdev ) );
  851. if ( ! fcpdev ) {
  852. rc = -ENOMEM;
  853. goto err_zalloc;
  854. }
  855. ref_init ( &fcpdev->refcnt, NULL );
  856. intf_init ( &fcpdev->scsi, &fcpdev_scsi_desc, &fcpdev->refcnt );
  857. INIT_LIST_HEAD ( &fcpdev->fcpcmds );
  858. fc_ulp_user_init ( &fcpdev->user, fcpdev_examine, &fcpdev->refcnt );
  859. DBGC ( fcpdev, "FCP %p opened for %s\n", fcpdev, fc_ntoa ( wwn ) );
  860. /* Attach to Fibre Channel ULP */
  861. fc_ulp_attach ( ulp, &fcpdev->user );
  862. /* Preserve parameters required for boot firmware table */
  863. memcpy ( &fcpdev->wwn, wwn, sizeof ( fcpdev->wwn ) );
  864. memcpy ( &fcpdev->lun, lun, sizeof ( fcpdev->lun ) );
  865. /* Attach SCSI device to parent interface */
  866. if ( ( rc = scsi_open ( parent, &fcpdev->scsi, lun ) ) != 0 ) {
  867. DBGC ( fcpdev, "FCP %p could not create SCSI device: %s\n",
  868. fcpdev, strerror ( rc ) );
  869. goto err_scsi_open;
  870. }
  871. /* Drop temporary reference to ULP */
  872. fc_ulp_put ( ulp );
  873. /* Mortalise self and return */
  874. ref_put ( &fcpdev->refcnt );
  875. return 0;
  876. err_scsi_open:
  877. fcpdev_close ( fcpdev, rc );
  878. ref_put ( &fcpdev->refcnt );
  879. err_zalloc:
  880. fc_ulp_put ( ulp );
  881. err_ulp_get:
  882. return rc;
  883. }
  884. /******************************************************************************
  885. *
  886. * FCP URIs
  887. *
  888. ******************************************************************************
  889. */
  890. /**
  891. * Parse FCP URI
  892. *
  893. * @v uri URI
  894. * @ret wwn Fibre Channel WWN
  895. * @ret lun SCSI LUN
  896. * @ret rc Return status code
  897. *
  898. * An FCP URI has the form "fcp:<wwn>:<lun>" or "fcp://<wwn>/<lun>"
  899. */
  900. static int fcp_parse_uri ( struct uri *uri, struct fc_name *wwn,
  901. struct scsi_lun *lun ) {
  902. char wwn_buf[ FC_NAME_STRLEN + 1 /* NUL */ ];
  903. const char *wwn_text;
  904. const char *lun_text;
  905. int rc;
  906. /* Extract WWN and LUN texts from URI */
  907. if ( uri->opaque ) {
  908. /* "fcp:<wwn>:<lun>" */
  909. if ( snprintf ( wwn_buf, sizeof ( wwn_buf ), "%s",
  910. uri->opaque ) < ( FC_NAME_STRLEN + 1 /* : */ ) )
  911. return -EINVAL;
  912. if ( uri->opaque[FC_NAME_STRLEN] != ':' )
  913. return -EINVAL;
  914. wwn_text = wwn_buf;
  915. lun_text = &uri->opaque[FC_NAME_STRLEN + 1];
  916. } else {
  917. /* If host exists, path must also exist */
  918. if ( ! ( uri->host && uri->path ) )
  919. return -EINVAL;
  920. if ( uri->path[0] != '/' )
  921. return -EINVAL;
  922. wwn_text = uri->host;
  923. lun_text = ( uri->path + 1 );
  924. }
  925. /* Parse WWN */
  926. if ( ( rc = fc_aton ( wwn_text, wwn ) ) != 0 )
  927. return rc;
  928. /* Parse LUN */
  929. if ( ( rc = scsi_parse_lun ( lun_text, lun ) ) != 0 )
  930. return rc;
  931. return 0;
  932. }
  933. /**
  934. * Open FCP URI
  935. *
  936. * @v parent Parent interface
  937. * @v uri URI
  938. * @ret rc Return status code
  939. */
  940. static int fcp_open ( struct interface *parent, struct uri *uri ) {
  941. struct fc_name wwn;
  942. struct scsi_lun lun;
  943. int rc;
  944. /* Parse URI */
  945. if ( ( rc = fcp_parse_uri ( uri, &wwn, &lun ) ) != 0 )
  946. return rc;
  947. /* Open FCP device */
  948. if ( ( rc = fcpdev_open ( parent, &wwn, &lun ) ) != 0 )
  949. return rc;
  950. return 0;
  951. }
  952. /** FCP URI opener */
  953. struct uri_opener fcp_uri_opener __uri_opener = {
  954. .scheme = "fcp",
  955. .open = fcp_open,
  956. };