Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <byteswap.h>
  28. #include <ipxe/refcnt.h>
  29. #include <ipxe/list.h>
  30. #include <ipxe/interface.h>
  31. #include <ipxe/xfer.h>
  32. #include <ipxe/iobuf.h>
  33. #include <ipxe/open.h>
  34. #include <ipxe/process.h>
  35. #include <ipxe/uri.h>
  36. #include <ipxe/acpi.h>
  37. #include <ipxe/scsi.h>
  38. #include <ipxe/device.h>
  39. #include <ipxe/edd.h>
  40. #include <ipxe/fc.h>
  41. #include <ipxe/fcels.h>
  42. #include <ipxe/fcp.h>
  43. /** @file
  44. *
  45. * Fibre Channel Protocol
  46. *
  47. */
  48. /* Disambiguate the various error causes */
  49. #define ERANGE_READ_DATA_ORDERING \
  50. __einfo_error ( EINFO_ERANGE_READ_DATA_ORDERING )
  51. #define EINFO_ERANGE_READ_DATA_ORDERING \
  52. __einfo_uniqify ( EINFO_ERANGE, 0x01, "Read data out of order" )
  53. #define ERANGE_READ_DATA_OVERRUN \
  54. __einfo_error ( EINFO_ERANGE_READ_DATA_OVERRUN )
  55. #define EINFO_ERANGE_READ_DATA_OVERRUN \
  56. __einfo_uniqify ( EINFO_ERANGE, 0x02, "Read data overrun" )
  57. #define ERANGE_WRITE_DATA_STUCK \
  58. __einfo_error ( EINFO_ERANGE_WRITE_DATA_STUCK )
  59. #define EINFO_ERANGE_WRITE_DATA_STUCK \
  60. __einfo_uniqify ( EINFO_ERANGE, 0x03, "Write data stuck" )
  61. #define ERANGE_WRITE_DATA_OVERRUN \
  62. __einfo_error ( EINFO_ERANGE_WRITE_DATA_OVERRUN )
  63. #define EINFO_ERANGE_WRITE_DATA_OVERRUN \
  64. __einfo_uniqify ( EINFO_ERANGE, 0x04, "Write data overrun" )
  65. #define ERANGE_DATA_UNDERRUN \
  66. __einfo_error ( EINFO_ERANGE_DATA_UNDERRUN )
  67. #define EINFO_ERANGE_DATA_UNDERRUN \
  68. __einfo_uniqify ( EINFO_ERANGE, 0x05, "Data underrun" )
  69. /******************************************************************************
  70. *
  71. * PRLI
  72. *
  73. ******************************************************************************
  74. */
  75. struct fc_els_prli_descriptor fcp_prli_descriptor __fc_els_prli_descriptor;
  76. /**
  77. * Transmit FCP PRLI
  78. *
  79. * @v els Fibre Channel ELS transaction
  80. * @ret rc Return status code
  81. */
  82. static int fcp_prli_tx ( struct fc_els *els ) {
  83. struct fcp_prli_service_parameters param;
  84. /* Build service parameter page */
  85. memset ( &param, 0, sizeof ( param ) );
  86. param.flags = htonl ( FCP_PRLI_NO_READ_RDY | FCP_PRLI_INITIATOR );
  87. return fc_els_prli_tx ( els, &fcp_prli_descriptor, &param );
  88. }
  89. /**
  90. * Receive FCP PRLI
  91. *
  92. * @v els Fibre Channel ELS transaction
  93. * @v frame ELS frame
  94. * @v len Length of ELS frame
  95. * @ret rc Return status code
  96. */
  97. static int fcp_prli_rx ( struct fc_els *els, void *data, size_t len ) {
  98. return fc_els_prli_rx ( els, &fcp_prli_descriptor, data, len );
  99. }
  100. /**
  101. * Detect FCP PRLI
  102. *
  103. * @v els Fibre Channel ELS transaction
  104. * @v data ELS frame
  105. * @v len Length of ELS frame
  106. * @ret rc Return status code
  107. */
  108. static int fcp_prli_detect ( struct fc_els *els, const void *data,
  109. size_t len ) {
  110. return fc_els_prli_detect ( els, &fcp_prli_descriptor, data, len );
  111. }
  112. /** FCP PRLI ELS handler */
  113. struct fc_els_handler fcp_prli_handler __fc_els_handler = {
  114. .name = "PRLI-FCP",
  115. .tx = fcp_prli_tx,
  116. .rx = fcp_prli_rx,
  117. .detect = fcp_prli_detect,
  118. };
  119. /** FCP PRLI descriptor */
  120. struct fc_els_prli_descriptor fcp_prli_descriptor __fc_els_prli_descriptor = {
  121. .type = FC_TYPE_FCP,
  122. .param_len = sizeof ( struct fcp_prli_service_parameters ),
  123. .handler = &fcp_prli_handler,
  124. };
  125. /******************************************************************************
  126. *
  127. * FCP devices and commands
  128. *
  129. ******************************************************************************
  130. */
  131. /** An FCP device */
  132. struct fcp_device {
  133. /** Reference count */
  134. struct refcnt refcnt;
  135. /** Fibre Channel upper-layer protocol user */
  136. struct fc_ulp_user user;
  137. /** SCSI command issuing interface */
  138. struct interface scsi;
  139. /** List of active commands */
  140. struct list_head fcpcmds;
  141. /** Fibre Channel WWN (for boot firmware table) */
  142. struct fc_name wwn;
  143. /** SCSI LUN (for boot firmware table) */
  144. struct scsi_lun lun;
  145. };
  146. /** An FCP command */
  147. struct fcp_command {
  148. /** Reference count */
  149. struct refcnt refcnt;
  150. /** FCP SCSI device */
  151. struct fcp_device *fcpdev;
  152. /** List of active commands */
  153. struct list_head list;
  154. /** SCSI command interface */
  155. struct interface scsi;
  156. /** Fibre Channel exchange interface */
  157. struct interface xchg;
  158. /** Send process */
  159. struct process process;
  160. /** Send current IU
  161. *
  162. * @v fcpcmd FCP command
  163. * @ret rc Return status code
  164. */
  165. int ( * send ) ( struct fcp_command *fcpcmd );
  166. /** SCSI command */
  167. struct scsi_cmd command;
  168. /** Data offset within command */
  169. size_t offset;
  170. /** Length of data remaining to be sent within this IU */
  171. size_t remaining;
  172. /** Exchange ID */
  173. uint16_t xchg_id;
  174. };
  175. /**
  176. * Get reference to FCP device
  177. *
  178. * @v fcpdev FCP device
  179. * @ret fcpdev FCP device
  180. */
  181. static inline __attribute__ (( always_inline )) struct fcp_device *
  182. fcpdev_get ( struct fcp_device *fcpdev ) {
  183. ref_get ( &fcpdev->refcnt );
  184. return fcpdev;
  185. }
  186. /**
  187. * Drop reference to FCP device
  188. *
  189. * @v fcpdev FCP device
  190. */
  191. static inline __attribute__ (( always_inline )) void
  192. fcpdev_put ( struct fcp_device *fcpdev ) {
  193. ref_put ( &fcpdev->refcnt );
  194. }
  195. /**
  196. * Get reference to FCP command
  197. *
  198. * @v fcpcmd FCP command
  199. * @ret fcpcmd FCP command
  200. */
  201. static inline __attribute__ (( always_inline )) struct fcp_command *
  202. fcpcmd_get ( struct fcp_command *fcpcmd ) {
  203. ref_get ( &fcpcmd->refcnt );
  204. return fcpcmd;
  205. }
  206. /**
  207. * Drop reference to FCP command
  208. *
  209. * @v fcpcmd FCP command
  210. */
  211. static inline __attribute__ (( always_inline )) void
  212. fcpcmd_put ( struct fcp_command *fcpcmd ) {
  213. ref_put ( &fcpcmd->refcnt );
  214. }
  215. /**
  216. * Start FCP command sending
  217. *
  218. * @v fcpcmd FCP command
  219. * @v send Send method
  220. */
  221. static inline __attribute__ (( always_inline )) void
  222. fcpcmd_start_send ( struct fcp_command *fcpcmd,
  223. int ( * send ) ( struct fcp_command *fcpcmd ) ) {
  224. fcpcmd->send = send;
  225. process_add ( &fcpcmd->process );
  226. }
  227. /**
  228. * Stop FCP command sending
  229. *
  230. * @v fcpcmd FCP command
  231. */
  232. static inline __attribute__ (( always_inline )) void
  233. fcpcmd_stop_send ( struct fcp_command *fcpcmd ) {
  234. process_del ( &fcpcmd->process );
  235. }
  236. /**
  237. * Free FCP command
  238. *
  239. * @v refcnt Reference count
  240. */
  241. static void fcpcmd_free ( struct refcnt *refcnt ) {
  242. struct fcp_command *fcpcmd =
  243. container_of ( refcnt, struct fcp_command, refcnt );
  244. /* Remove from list of commands */
  245. list_del ( &fcpcmd->list );
  246. fcpdev_put ( fcpcmd->fcpdev );
  247. /* Free command */
  248. free ( fcpcmd );
  249. }
  250. /**
  251. * Close FCP command
  252. *
  253. * @v fcpcmd FCP command
  254. * @v rc Reason for close
  255. */
  256. static void fcpcmd_close ( struct fcp_command *fcpcmd, int rc ) {
  257. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  258. if ( rc != 0 ) {
  259. DBGC ( fcpdev, "FCP %p xchg %04x closed: %s\n",
  260. fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  261. }
  262. /* Stop sending */
  263. fcpcmd_stop_send ( fcpcmd );
  264. /* Shut down interfaces */
  265. intf_shutdown ( &fcpcmd->scsi, rc );
  266. intf_shutdown ( &fcpcmd->xchg, rc );
  267. }
  268. /**
  269. * Close FCP command in error
  270. *
  271. * @v fcpcmd FCP command
  272. * @v rc Reason for close
  273. */
  274. static void fcpcmd_close_err ( struct fcp_command *fcpcmd, int rc ) {
  275. if ( rc == 0 )
  276. rc = -EPIPE;
  277. fcpcmd_close ( fcpcmd, rc );
  278. }
  279. /**
  280. * Send FCP command IU
  281. *
  282. * @v fcpcmd FCP command
  283. * @ret rc Return status code
  284. */
  285. static int fcpcmd_send_cmnd ( struct fcp_command *fcpcmd ) {
  286. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  287. struct scsi_cmd *command = &fcpcmd->command;
  288. struct io_buffer *iobuf;
  289. struct fcp_cmnd *cmnd;
  290. struct xfer_metadata meta;
  291. int rc;
  292. /* Sanity check */
  293. if ( command->data_in_len && command->data_out_len ) {
  294. DBGC ( fcpdev, "FCP %p xchg %04x cannot handle bidirectional "
  295. "command\n", fcpdev, fcpcmd->xchg_id );
  296. return -ENOTSUP;
  297. }
  298. /* Allocate I/O buffer */
  299. iobuf = xfer_alloc_iob ( &fcpcmd->xchg, sizeof ( *cmnd ) );
  300. if ( ! iobuf ) {
  301. DBGC ( fcpdev, "FCP %p xchg %04x cannot allocate command IU\n",
  302. fcpdev, fcpcmd->xchg_id );
  303. return -ENOMEM;
  304. }
  305. /* Construct command IU frame */
  306. cmnd = iob_put ( iobuf, sizeof ( *cmnd ) );
  307. memset ( cmnd, 0, sizeof ( *cmnd ) );
  308. memcpy ( &cmnd->lun, &command->lun, sizeof ( cmnd->lun ) );
  309. assert ( ! ( command->data_in_len && command->data_out_len ) );
  310. if ( command->data_in_len )
  311. cmnd->dirn |= FCP_CMND_RDDATA;
  312. if ( command->data_out_len )
  313. cmnd->dirn |= FCP_CMND_WRDATA;
  314. memcpy ( &cmnd->cdb, &fcpcmd->command.cdb, sizeof ( cmnd->cdb ) );
  315. cmnd->len = htonl ( command->data_in_len + command->data_out_len );
  316. memset ( &meta, 0, sizeof ( meta ) );
  317. meta.flags = ( XFER_FL_CMD_STAT | XFER_FL_OVER );
  318. DBGC2 ( fcpdev, "FCP %p xchg %04x CMND " SCSI_CDB_FORMAT " %04x\n",
  319. fcpdev, fcpcmd->xchg_id, SCSI_CDB_DATA ( cmnd->cdb ),
  320. ntohl ( cmnd->len ) );
  321. /* No further data to send within this IU */
  322. fcpcmd_stop_send ( fcpcmd );
  323. /* Send command IU frame */
  324. if ( ( rc = xfer_deliver ( &fcpcmd->xchg, iob_disown ( iobuf ),
  325. &meta ) ) != 0 ) {
  326. DBGC ( fcpdev, "FCP %p xchg %04x cannot deliver command IU: "
  327. "%s\n", fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  328. return rc;
  329. }
  330. return 0;
  331. }
  332. /**
  333. * Handle FCP read data IU
  334. *
  335. * @v fcpcmd FCP command
  336. * @v iobuf I/O buffer
  337. * @v meta Data transfer metadata
  338. * @ret rc Return status code
  339. */
  340. static int fcpcmd_recv_rddata ( struct fcp_command *fcpcmd,
  341. struct io_buffer *iobuf,
  342. struct xfer_metadata *meta ) {
  343. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  344. struct scsi_cmd *command = &fcpcmd->command;
  345. size_t offset = meta->offset;
  346. size_t len = iob_len ( iobuf );
  347. int rc;
  348. /* Sanity checks */
  349. if ( ! ( meta->flags & XFER_FL_ABS_OFFSET ) ) {
  350. DBGC ( fcpdev, "FCP %p xchg %04x read data missing offset\n",
  351. fcpdev, fcpcmd->xchg_id );
  352. rc = -ERANGE_READ_DATA_ORDERING;
  353. goto done;
  354. }
  355. if ( offset != fcpcmd->offset ) {
  356. DBGC ( fcpdev, "FCP %p xchg %04x read data out of order "
  357. "(expected %zd, received %zd)\n",
  358. fcpdev, fcpcmd->xchg_id, fcpcmd->offset, offset );
  359. rc = -ERANGE_READ_DATA_ORDERING;
  360. goto done;
  361. }
  362. if ( ( offset + len ) > command->data_in_len ) {
  363. DBGC ( fcpdev, "FCP %p xchg %04x read data overrun (max %zd, "
  364. "received %zd)\n", fcpdev, fcpcmd->xchg_id,
  365. command->data_in_len, ( offset + len ) );
  366. rc = -ERANGE_READ_DATA_OVERRUN;
  367. goto done;
  368. }
  369. DBGC2 ( fcpdev, "FCP %p xchg %04x RDDATA [%08zx,%08zx)\n",
  370. fcpdev, fcpcmd->xchg_id, offset, ( offset + len ) );
  371. /* Copy to user buffer */
  372. copy_to_user ( command->data_in, offset, iobuf->data, len );
  373. fcpcmd->offset += len;
  374. assert ( fcpcmd->offset <= command->data_in_len );
  375. rc = 0;
  376. done:
  377. free_iob ( iobuf );
  378. return rc;
  379. }
  380. /**
  381. * Send FCP write data IU
  382. *
  383. * @v fcpcmd FCP command
  384. * @ret rc Return status code
  385. */
  386. static int fcpcmd_send_wrdata ( struct fcp_command *fcpcmd ) {
  387. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  388. struct scsi_cmd *command = &fcpcmd->command;
  389. struct io_buffer *iobuf;
  390. struct xfer_metadata meta;
  391. size_t len;
  392. int rc;
  393. /* Calculate length to be sent */
  394. len = xfer_window ( &fcpcmd->xchg );
  395. if ( len > fcpcmd->remaining )
  396. len = fcpcmd->remaining;
  397. /* Sanity checks */
  398. if ( len == 0 ) {
  399. DBGC ( fcpdev, "FCP %p xchg %04x write data stuck\n",
  400. fcpdev, fcpcmd->xchg_id );
  401. return -ERANGE_WRITE_DATA_STUCK;
  402. }
  403. if ( ( fcpcmd->offset + len ) > command->data_out_len ) {
  404. DBGC ( fcpdev, "FCP %p xchg %04x write data overrun (max %zd, "
  405. "requested %zd)\n", fcpdev, fcpcmd->xchg_id,
  406. command->data_out_len, ( fcpcmd->offset + len ) );
  407. return -ERANGE_WRITE_DATA_OVERRUN;
  408. }
  409. /* Allocate I/O buffer */
  410. iobuf = xfer_alloc_iob ( &fcpcmd->xchg, len );
  411. if ( ! iobuf ) {
  412. DBGC ( fcpdev, "FCP %p xchg %04x cannot allocate write data "
  413. "IU for %zd bytes\n", fcpdev, fcpcmd->xchg_id, len );
  414. return -ENOMEM;
  415. }
  416. /* Construct data IU frame */
  417. copy_from_user ( iob_put ( iobuf, len ), command->data_out,
  418. fcpcmd->offset, len );
  419. memset ( &meta, 0, sizeof ( meta ) );
  420. meta.flags = ( XFER_FL_RESPONSE | XFER_FL_ABS_OFFSET );
  421. meta.offset = fcpcmd->offset;
  422. DBGC2 ( fcpdev, "FCP %p xchg %04x WRDATA [%08zx,%04zx)\n",
  423. fcpdev, fcpcmd->xchg_id, fcpcmd->offset,
  424. ( fcpcmd->offset + iob_len ( iobuf ) ) );
  425. /* Calculate amount of data remaining to be sent within this IU */
  426. assert ( len <= fcpcmd->remaining );
  427. fcpcmd->offset += len;
  428. fcpcmd->remaining -= len;
  429. assert ( fcpcmd->offset <= command->data_out_len );
  430. if ( fcpcmd->remaining == 0 ) {
  431. fcpcmd_stop_send ( fcpcmd );
  432. meta.flags |= XFER_FL_OVER;
  433. }
  434. /* Send data IU frame */
  435. if ( ( rc = xfer_deliver ( &fcpcmd->xchg, iob_disown ( iobuf ),
  436. &meta ) ) != 0 ) {
  437. DBGC ( fcpdev, "FCP %p xchg %04x cannot deliver write data "
  438. "IU: %s\n", fcpdev, fcpcmd->xchg_id, strerror ( rc ) );
  439. return rc;
  440. }
  441. return 0;
  442. }
  443. /**
  444. * Handle FCP transfer ready IU
  445. *
  446. * @v fcpcmd FCP command
  447. * @v iobuf I/O buffer
  448. * @v meta Data transfer metadata
  449. * @ret rc Return status code
  450. */
  451. static int fcpcmd_recv_xfer_rdy ( struct fcp_command *fcpcmd,
  452. struct io_buffer *iobuf,
  453. struct xfer_metadata *meta __unused ) {
  454. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  455. struct fcp_xfer_rdy *xfer_rdy = iobuf->data;
  456. int rc;
  457. /* Sanity checks */
  458. if ( iob_len ( iobuf ) != sizeof ( *xfer_rdy ) ) {
  459. DBGC ( fcpdev, "FCP %p xchg %04x received invalid transfer "
  460. "ready IU:\n", fcpdev, fcpcmd->xchg_id );
  461. DBGC_HDA ( fcpdev, 0, iobuf->data, iob_len ( iobuf ) );
  462. rc = -EPROTO;
  463. goto done;
  464. }
  465. if ( ntohl ( xfer_rdy->offset ) != fcpcmd->offset ) {
  466. /* We do not advertise out-of-order delivery */
  467. DBGC ( fcpdev, "FCP %p xchg %04x cannot support out-of-order "
  468. "delivery (expected %zd, requested %d)\n",
  469. fcpdev, fcpcmd->xchg_id, fcpcmd->offset,
  470. ntohl ( xfer_rdy->offset ) );
  471. rc = -EPROTO;
  472. goto done;
  473. }
  474. DBGC2 ( fcpdev, "FCP %p xchg %04x XFER_RDY [%08x,%08x)\n",
  475. fcpdev, fcpcmd->xchg_id, ntohl ( xfer_rdy->offset ),
  476. ( ntohl ( xfer_rdy->offset ) + ntohl ( xfer_rdy->len ) ) );
  477. /* Start sending requested data */
  478. fcpcmd->remaining = ntohl ( xfer_rdy->len );
  479. fcpcmd_start_send ( fcpcmd, fcpcmd_send_wrdata );
  480. rc = 0;
  481. done:
  482. free_iob ( iobuf );
  483. return rc;
  484. }
  485. /**
  486. * Handle FCP response IU
  487. *
  488. * @v fcpcmd FCP command
  489. * @v iobuf I/O buffer
  490. * @v meta Data transfer metadata
  491. * @ret rc Return status code
  492. */
  493. static int fcpcmd_recv_rsp ( struct fcp_command *fcpcmd,
  494. struct io_buffer *iobuf,
  495. struct xfer_metadata *meta __unused ) {
  496. struct fcp_device *fcpdev = fcpcmd->fcpdev;
  497. struct scsi_cmd *command = &fcpcmd->command;
  498. struct fcp_rsp *rsp = iobuf->data;
  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. scsi_parse_sense ( fcp_rsp_sense_data ( rsp ),
  552. fcp_rsp_sense_data_len ( rsp ), &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. };