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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 in an ACPI table
  758. *
  759. * @v fcpdev FCP device
  760. * @v acpi ACPI table
  761. * @v len Length of ACPI table
  762. * @ret rc Return status code
  763. */
  764. static int fcpdev_acpi_describe ( struct fcp_device *fcpdev,
  765. struct acpi_description_header *acpi,
  766. size_t len ) {
  767. DBGC ( fcpdev, "FCP %p cannot yet describe device in an ACPI table\n",
  768. fcpdev );
  769. ( void ) acpi;
  770. ( void ) len;
  771. return 0;
  772. }
  773. /**
  774. * Describe FCP device using EDD
  775. *
  776. * @v fcpdev FCP device
  777. * @v type EDD interface type
  778. * @v path EDD device path
  779. * @ret rc Return status code
  780. */
  781. static int fcpdev_edd_describe ( struct fcp_device *fcpdev,
  782. struct edd_interface_type *type,
  783. union edd_device_path *path ) {
  784. union {
  785. struct fc_name fc;
  786. uint64_t u64;
  787. } wwn;
  788. union {
  789. struct scsi_lun scsi;
  790. uint64_t u64;
  791. } lun;
  792. type->type = cpu_to_le64 ( EDD_INTF_TYPE_FIBRE );
  793. memcpy ( &wwn.fc, &fcpdev->wwn, sizeof ( wwn.fc ) );
  794. path->fibre.wwn = be64_to_cpu ( wwn.u64 );
  795. memcpy ( &lun.scsi, &fcpdev->lun, sizeof ( lun.scsi ) );
  796. path->fibre.lun = be64_to_cpu ( lun.u64 );
  797. return 0;
  798. }
  799. /**
  800. * Identify device underlying FCP device
  801. *
  802. * @v fcpdev FCP device
  803. * @ret device Underlying device
  804. */
  805. static struct device * fcpdev_identify_device ( struct fcp_device *fcpdev ) {
  806. /* We know the underlying device only if the link is up;
  807. * otherwise we don't have a port to examine.
  808. */
  809. if ( ! fc_link_ok ( &fcpdev->user.ulp->link ) ) {
  810. DBGC ( fcpdev, "FCP %p doesn't know underlying device "
  811. "until link is up\n", fcpdev );
  812. return NULL;
  813. }
  814. /* Hand off to port's transport interface */
  815. assert ( fcpdev->user.ulp->peer->port != NULL );
  816. return identify_device ( &fcpdev->user.ulp->peer->port->transport );
  817. }
  818. /** FCP device SCSI interface operations */
  819. static struct interface_operation fcpdev_scsi_op[] = {
  820. INTF_OP ( scsi_command, struct fcp_device *, fcpdev_scsi_command ),
  821. INTF_OP ( xfer_window, struct fcp_device *, fcpdev_window ),
  822. INTF_OP ( intf_close, struct fcp_device *, fcpdev_close ),
  823. INTF_OP ( acpi_describe, struct fcp_device *, fcpdev_acpi_describe ),
  824. INTF_OP ( edd_describe, struct fcp_device *, fcpdev_edd_describe ),
  825. INTF_OP ( identify_device, struct fcp_device *,
  826. fcpdev_identify_device ),
  827. };
  828. /** FCP device SCSI interface descriptor */
  829. static struct interface_descriptor fcpdev_scsi_desc =
  830. INTF_DESC ( struct fcp_device, scsi, fcpdev_scsi_op );
  831. /**
  832. * Examine FCP ULP link state
  833. *
  834. * @v user Fibre Channel upper-layer protocol user
  835. */
  836. static void fcpdev_examine ( struct fc_ulp_user *user ) {
  837. struct fcp_device *fcpdev =
  838. container_of ( user, struct fcp_device, user );
  839. if ( fc_link_ok ( &fcpdev->user.ulp->link ) ) {
  840. DBGC ( fcpdev, "FCP %p link is up\n", fcpdev );
  841. } else {
  842. DBGC ( fcpdev, "FCP %p link is down: %s\n",
  843. fcpdev, strerror ( fcpdev->user.ulp->link.rc ) );
  844. }
  845. /* Notify SCSI layer of window change */
  846. xfer_window_changed ( &fcpdev->scsi );
  847. }
  848. /**
  849. * Open FCP device
  850. *
  851. * @v parent Parent interface
  852. * @v wwn Fibre Channel WWN
  853. * @v lun SCSI LUN
  854. * @ret rc Return status code
  855. */
  856. static int fcpdev_open ( struct interface *parent, struct fc_name *wwn,
  857. struct scsi_lun *lun ) {
  858. struct fc_ulp *ulp;
  859. struct fcp_device *fcpdev;
  860. int rc;
  861. /* Get Fibre Channel ULP interface */
  862. ulp = fc_ulp_get_wwn_type ( wwn, FC_TYPE_FCP );
  863. if ( ! ulp ) {
  864. rc = -ENOMEM;
  865. goto err_ulp_get;
  866. }
  867. /* Allocate and initialise structure */
  868. fcpdev = zalloc ( sizeof ( *fcpdev ) );
  869. if ( ! fcpdev ) {
  870. rc = -ENOMEM;
  871. goto err_zalloc;
  872. }
  873. ref_init ( &fcpdev->refcnt, NULL );
  874. intf_init ( &fcpdev->scsi, &fcpdev_scsi_desc, &fcpdev->refcnt );
  875. INIT_LIST_HEAD ( &fcpdev->fcpcmds );
  876. fc_ulp_user_init ( &fcpdev->user, fcpdev_examine, &fcpdev->refcnt );
  877. DBGC ( fcpdev, "FCP %p opened for %s\n", fcpdev, fc_ntoa ( wwn ) );
  878. /* Attach to Fibre Channel ULP */
  879. fc_ulp_attach ( ulp, &fcpdev->user );
  880. /* Preserve parameters required for boot firmware table */
  881. memcpy ( &fcpdev->wwn, wwn, sizeof ( fcpdev->wwn ) );
  882. memcpy ( &fcpdev->lun, lun, sizeof ( fcpdev->lun ) );
  883. /* Attach SCSI device to parent interface */
  884. if ( ( rc = scsi_open ( parent, &fcpdev->scsi, lun ) ) != 0 ) {
  885. DBGC ( fcpdev, "FCP %p could not create SCSI device: %s\n",
  886. fcpdev, strerror ( rc ) );
  887. goto err_scsi_open;
  888. }
  889. /* Drop temporary reference to ULP */
  890. fc_ulp_put ( ulp );
  891. /* Mortalise self and return */
  892. ref_put ( &fcpdev->refcnt );
  893. return 0;
  894. err_scsi_open:
  895. fcpdev_close ( fcpdev, rc );
  896. ref_put ( &fcpdev->refcnt );
  897. err_zalloc:
  898. fc_ulp_put ( ulp );
  899. err_ulp_get:
  900. return rc;
  901. }
  902. /******************************************************************************
  903. *
  904. * FCP URIs
  905. *
  906. ******************************************************************************
  907. */
  908. /**
  909. * Parse FCP URI
  910. *
  911. * @v uri URI
  912. * @ret wwn Fibre Channel WWN
  913. * @ret lun SCSI LUN
  914. * @ret rc Return status code
  915. *
  916. * An FCP URI has the form "fcp:<wwn>:<lun>" or "fcp://<wwn>/<lun>"
  917. */
  918. static int fcp_parse_uri ( struct uri *uri, struct fc_name *wwn,
  919. struct scsi_lun *lun ) {
  920. char wwn_buf[ FC_NAME_STRLEN + 1 /* NUL */ ];
  921. const char *wwn_text;
  922. const char *lun_text;
  923. int rc;
  924. /* Extract WWN and LUN texts from URI */
  925. if ( uri->opaque ) {
  926. /* "fcp:<wwn>:<lun>" */
  927. if ( snprintf ( wwn_buf, sizeof ( wwn_buf ), "%s",
  928. uri->opaque ) < ( FC_NAME_STRLEN + 1 /* : */ ) )
  929. return -EINVAL;
  930. if ( uri->opaque[FC_NAME_STRLEN] != ':' )
  931. return -EINVAL;
  932. wwn_text = wwn_buf;
  933. lun_text = &uri->opaque[FC_NAME_STRLEN + 1];
  934. } else {
  935. /* If host exists, path must also exist */
  936. if ( ! ( uri->host && uri->path ) )
  937. return -EINVAL;
  938. if ( uri->path[0] != '/' )
  939. return -EINVAL;
  940. wwn_text = uri->host;
  941. lun_text = ( uri->path + 1 );
  942. }
  943. /* Parse WWN */
  944. if ( ( rc = fc_aton ( wwn_text, wwn ) ) != 0 )
  945. return rc;
  946. /* Parse LUN */
  947. if ( ( rc = scsi_parse_lun ( lun_text, lun ) ) != 0 )
  948. return rc;
  949. return 0;
  950. }
  951. /**
  952. * Open FCP URI
  953. *
  954. * @v parent Parent interface
  955. * @v uri URI
  956. * @ret rc Return status code
  957. */
  958. static int fcp_open ( struct interface *parent, struct uri *uri ) {
  959. struct fc_name wwn;
  960. struct scsi_lun lun;
  961. int rc;
  962. /* Parse URI */
  963. if ( ( rc = fcp_parse_uri ( uri, &wwn, &lun ) ) != 0 )
  964. return rc;
  965. /* Open FCP device */
  966. if ( ( rc = fcpdev_open ( parent, &wwn, &lun ) ) != 0 )
  967. return rc;
  968. return 0;
  969. }
  970. /** FCP URI opener */
  971. struct uri_opener fcp_uri_opener __uri_opener = {
  972. .scheme = "fcp",
  973. .open = fcp_open,
  974. };