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

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