Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Copyright (C) 2006 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 <stdlib.h>
  26. #include <string.h>
  27. #include <byteswap.h>
  28. #include <errno.h>
  29. #include <ipxe/list.h>
  30. #include <ipxe/process.h>
  31. #include <ipxe/xfer.h>
  32. #include <ipxe/blockdev.h>
  33. #include <ipxe/scsi.h>
  34. /** @file
  35. *
  36. * SCSI block device
  37. *
  38. */
  39. /** Maximum number of command retries */
  40. #define SCSICMD_MAX_RETRIES 10
  41. /* Error numbers generated by SCSI sense data */
  42. #define EIO_NO_SENSE __einfo_error ( EINFO_EIO_NO_SENSE )
  43. #define EINFO_EIO_NO_SENSE \
  44. __einfo_uniqify ( EINFO_EIO, 0x00, "No sense" )
  45. #define EIO_RECOVERED_ERROR __einfo_error ( EINFO_EIO_RECOVERED_ERROR )
  46. #define EINFO_EIO_RECOVERED_ERROR \
  47. __einfo_uniqify ( EINFO_EIO, 0x01, "Recovered error" )
  48. #define EIO_NOT_READY __einfo_error ( EINFO_EIO_NOT_READY )
  49. #define EINFO_EIO_NOT_READY \
  50. __einfo_uniqify ( EINFO_EIO, 0x02, "Not ready" )
  51. #define EIO_MEDIUM_ERROR __einfo_error ( EINFO_EIO_MEDIUM_ERROR )
  52. #define EINFO_EIO_MEDIUM_ERROR \
  53. __einfo_uniqify ( EINFO_EIO, 0x03, "Medium error" )
  54. #define EIO_HARDWARE_ERROR __einfo_error ( EINFO_EIO_HARDWARE_ERROR )
  55. #define EINFO_EIO_HARDWARE_ERROR \
  56. __einfo_uniqify ( EINFO_EIO, 0x04, "Hardware error" )
  57. #define EIO_ILLEGAL_REQUEST __einfo_error ( EINFO_EIO_ILLEGAL_REQUEST )
  58. #define EINFO_EIO_ILLEGAL_REQUEST \
  59. __einfo_uniqify ( EINFO_EIO, 0x05, "Illegal request" )
  60. #define EIO_UNIT_ATTENTION __einfo_error ( EINFO_EIO_UNIT_ATTENTION )
  61. #define EINFO_EIO_UNIT_ATTENTION \
  62. __einfo_uniqify ( EINFO_EIO, 0x06, "Unit attention" )
  63. #define EIO_DATA_PROTECT __einfo_error ( EINFO_EIO_DATA_PROTECT )
  64. #define EINFO_EIO_DATA_PROTECT \
  65. __einfo_uniqify ( EINFO_EIO, 0x07, "Data protect" )
  66. #define EIO_BLANK_CHECK __einfo_error ( EINFO_EIO_BLANK_CHECK )
  67. #define EINFO_EIO_BLANK_CHECK \
  68. __einfo_uniqify ( EINFO_EIO, 0x08, "Blank check" )
  69. #define EIO_VENDOR_SPECIFIC __einfo_error ( EINFO_EIO_VENDOR_SPECIFIC )
  70. #define EINFO_EIO_VENDOR_SPECIFIC \
  71. __einfo_uniqify ( EINFO_EIO, 0x09, "Vendor specific" )
  72. #define EIO_COPY_ABORTED __einfo_error ( EINFO_EIO_COPY_ABORTED )
  73. #define EINFO_EIO_COPY_ABORTED \
  74. __einfo_uniqify ( EINFO_EIO, 0x0a, "Copy aborted" )
  75. #define EIO_ABORTED_COMMAND __einfo_error ( EINFO_EIO_ABORTED_COMMAND )
  76. #define EINFO_EIO_ABORTED_COMMAND \
  77. __einfo_uniqify ( EINFO_EIO, 0x0b, "Aborted command" )
  78. #define EIO_RESERVED __einfo_error ( EINFO_EIO_RESERVED )
  79. #define EINFO_EIO_RESERVED \
  80. __einfo_uniqify ( EINFO_EIO, 0x0c, "Reserved" )
  81. #define EIO_VOLUME_OVERFLOW __einfo_error ( EINFO_EIO_VOLUME_OVERFLOW )
  82. #define EINFO_EIO_VOLUME_OVERFLOW \
  83. __einfo_uniqify ( EINFO_EIO, 0x0d, "Volume overflow" )
  84. #define EIO_MISCOMPARE __einfo_error ( EINFO_EIO_MISCOMPARE )
  85. #define EINFO_EIO_MISCOMPARE \
  86. __einfo_uniqify ( EINFO_EIO, 0x0e, "Miscompare" )
  87. #define EIO_COMPLETED __einfo_error ( EINFO_EIO_COMPLETED )
  88. #define EINFO_EIO_COMPLETED \
  89. __einfo_uniqify ( EINFO_EIO, 0x0f, "Completed" )
  90. #define EIO_SENSE( key ) \
  91. EUNIQ ( EINFO_EIO, (key), EIO_NO_SENSE, EIO_RECOVERED_ERROR, \
  92. EIO_NOT_READY, EIO_MEDIUM_ERROR, EIO_HARDWARE_ERROR, \
  93. EIO_ILLEGAL_REQUEST, EIO_UNIT_ATTENTION, \
  94. EIO_DATA_PROTECT, EIO_BLANK_CHECK, EIO_VENDOR_SPECIFIC, \
  95. EIO_COPY_ABORTED, EIO_ABORTED_COMMAND, EIO_RESERVED, \
  96. EIO_VOLUME_OVERFLOW, EIO_MISCOMPARE, EIO_COMPLETED )
  97. /******************************************************************************
  98. *
  99. * Utility functions
  100. *
  101. ******************************************************************************
  102. */
  103. /**
  104. * Parse SCSI LUN
  105. *
  106. * @v lun_string LUN string representation
  107. * @v lun LUN to fill in
  108. * @ret rc Return status code
  109. */
  110. int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun ) {
  111. char *p;
  112. int i;
  113. memset ( lun, 0, sizeof ( *lun ) );
  114. if ( lun_string ) {
  115. p = ( char * ) lun_string;
  116. for ( i = 0 ; i < 4 ; i++ ) {
  117. lun->u16[i] = htons ( strtoul ( p, &p, 16 ) );
  118. if ( *p == '\0' )
  119. break;
  120. if ( *p != '-' )
  121. return -EINVAL;
  122. p++;
  123. }
  124. if ( *p )
  125. return -EINVAL;
  126. }
  127. return 0;
  128. }
  129. /**
  130. * Parse SCSI sense data
  131. *
  132. * @v data Raw sense data
  133. * @v len Length of raw sense data
  134. * @v sense Descriptor-format sense data to fill in
  135. */
  136. void scsi_parse_sense ( const void *data, size_t len,
  137. struct scsi_sns_descriptor *sense ) {
  138. const union scsi_sns *sns = data;
  139. /* Avoid returning uninitialised data */
  140. memset ( sense, 0, sizeof ( *sense ) );
  141. /* Copy, assuming descriptor-format data */
  142. if ( len < sizeof ( sns->desc ) )
  143. return;
  144. memcpy ( sense, &sns->desc, sizeof ( *sense ) );
  145. /* Convert fixed-format to descriptor-format, if applicable */
  146. if ( len < sizeof ( sns->fixed ) )
  147. return;
  148. if ( ! SCSI_SENSE_FIXED ( sns->code ) )
  149. return;
  150. sense->additional = sns->fixed.additional;
  151. }
  152. /******************************************************************************
  153. *
  154. * Interface methods
  155. *
  156. ******************************************************************************
  157. */
  158. /**
  159. * Issue SCSI command
  160. *
  161. * @v control SCSI control interface
  162. * @v data SCSI data interface
  163. * @v command SCSI command
  164. * @ret tag Command tag, or negative error
  165. */
  166. int scsi_command ( struct interface *control, struct interface *data,
  167. struct scsi_cmd *command ) {
  168. struct interface *dest;
  169. scsi_command_TYPE ( void * ) *op =
  170. intf_get_dest_op ( control, scsi_command, &dest );
  171. void *object = intf_object ( dest );
  172. int tap;
  173. if ( op ) {
  174. tap = op ( object, data, command );
  175. } else {
  176. /* Default is to fail to issue the command */
  177. tap = -EOPNOTSUPP;
  178. }
  179. intf_put ( dest );
  180. return tap;
  181. }
  182. /**
  183. * Report SCSI response
  184. *
  185. * @v interface SCSI command interface
  186. * @v response SCSI response
  187. */
  188. void scsi_response ( struct interface *intf, struct scsi_rsp *response ) {
  189. struct interface *dest;
  190. scsi_response_TYPE ( void * ) *op =
  191. intf_get_dest_op ( intf, scsi_response, &dest );
  192. void *object = intf_object ( dest );
  193. if ( op ) {
  194. op ( object, response );
  195. } else {
  196. /* Default is to ignore the response */
  197. }
  198. intf_put ( dest );
  199. }
  200. /******************************************************************************
  201. *
  202. * SCSI devices and commands
  203. *
  204. ******************************************************************************
  205. */
  206. /** A SCSI device */
  207. struct scsi_device {
  208. /** Reference count */
  209. struct refcnt refcnt;
  210. /** Block control interface */
  211. struct interface block;
  212. /** SCSI control interface */
  213. struct interface scsi;
  214. /** SCSI LUN */
  215. struct scsi_lun lun;
  216. /** Flags */
  217. unsigned int flags;
  218. /** TEST UNIT READY interface */
  219. struct interface ready;
  220. /** TEST UNIT READY process */
  221. struct process process;
  222. /** List of commands */
  223. struct list_head cmds;
  224. };
  225. /** SCSI device flags */
  226. enum scsi_device_flags {
  227. /** TEST UNIT READY has been issued */
  228. SCSIDEV_UNIT_TESTED = 0x0001,
  229. /** TEST UNIT READY has completed successfully */
  230. SCSIDEV_UNIT_READY = 0x0002,
  231. };
  232. /** A SCSI command */
  233. struct scsi_command {
  234. /** Reference count */
  235. struct refcnt refcnt;
  236. /** SCSI device */
  237. struct scsi_device *scsidev;
  238. /** List of SCSI commands */
  239. struct list_head list;
  240. /** Block data interface */
  241. struct interface block;
  242. /** SCSI data interface */
  243. struct interface scsi;
  244. /** Command type */
  245. struct scsi_command_type *type;
  246. /** Starting logical block address */
  247. uint64_t lba;
  248. /** Number of blocks */
  249. unsigned int count;
  250. /** Data buffer */
  251. userptr_t buffer;
  252. /** Length of data buffer */
  253. size_t len;
  254. /** Command tag */
  255. uint32_t tag;
  256. /** Retry count */
  257. unsigned int retries;
  258. /** Private data */
  259. uint8_t priv[0];
  260. };
  261. /** A SCSI command type */
  262. struct scsi_command_type {
  263. /** Name */
  264. const char *name;
  265. /** Additional working space */
  266. size_t priv_len;
  267. /**
  268. * Construct SCSI command IU
  269. *
  270. * @v scsicmd SCSI command
  271. * @v command SCSI command IU
  272. */
  273. void ( * cmd ) ( struct scsi_command *scsicmd,
  274. struct scsi_cmd *command );
  275. /**
  276. * Handle SCSI command completion
  277. *
  278. * @v scsicmd SCSI command
  279. * @v rc Reason for completion
  280. */
  281. void ( * done ) ( struct scsi_command *scsicmd, int rc );
  282. };
  283. /**
  284. * Get reference to SCSI device
  285. *
  286. * @v scsidev SCSI device
  287. * @ret scsidev SCSI device
  288. */
  289. static inline __attribute__ (( always_inline )) struct scsi_device *
  290. scsidev_get ( struct scsi_device *scsidev ) {
  291. ref_get ( &scsidev->refcnt );
  292. return scsidev;
  293. }
  294. /**
  295. * Drop reference to SCSI device
  296. *
  297. * @v scsidev SCSI device
  298. */
  299. static inline __attribute__ (( always_inline )) void
  300. scsidev_put ( struct scsi_device *scsidev ) {
  301. ref_put ( &scsidev->refcnt );
  302. }
  303. /**
  304. * Get reference to SCSI command
  305. *
  306. * @v scsicmd SCSI command
  307. * @ret scsicmd SCSI command
  308. */
  309. static inline __attribute__ (( always_inline )) struct scsi_command *
  310. scsicmd_get ( struct scsi_command *scsicmd ) {
  311. ref_get ( &scsicmd->refcnt );
  312. return scsicmd;
  313. }
  314. /**
  315. * Drop reference to SCSI command
  316. *
  317. * @v scsicmd SCSI command
  318. */
  319. static inline __attribute__ (( always_inline )) void
  320. scsicmd_put ( struct scsi_command *scsicmd ) {
  321. ref_put ( &scsicmd->refcnt );
  322. }
  323. /**
  324. * Get SCSI command private data
  325. *
  326. * @v scsicmd SCSI command
  327. * @ret priv Private data
  328. */
  329. static inline __attribute__ (( always_inline )) void *
  330. scsicmd_priv ( struct scsi_command *scsicmd ) {
  331. return scsicmd->priv;
  332. }
  333. /**
  334. * Free SCSI command
  335. *
  336. * @v refcnt Reference count
  337. */
  338. static void scsicmd_free ( struct refcnt *refcnt ) {
  339. struct scsi_command *scsicmd =
  340. container_of ( refcnt, struct scsi_command, refcnt );
  341. /* Remove from list of commands */
  342. list_del ( &scsicmd->list );
  343. scsidev_put ( scsicmd->scsidev );
  344. /* Free command */
  345. free ( scsicmd );
  346. }
  347. /**
  348. * Close SCSI command
  349. *
  350. * @v scsicmd SCSI command
  351. * @v rc Reason for close
  352. */
  353. static void scsicmd_close ( struct scsi_command *scsicmd, int rc ) {
  354. struct scsi_device *scsidev = scsicmd->scsidev;
  355. if ( rc != 0 ) {
  356. DBGC ( scsidev, "SCSI %p tag %08x closed: %s\n",
  357. scsidev, scsicmd->tag, strerror ( rc ) );
  358. }
  359. /* Shut down interfaces */
  360. intf_shutdown ( &scsicmd->scsi, rc );
  361. intf_shutdown ( &scsicmd->block, rc );
  362. }
  363. /**
  364. * Construct and issue SCSI command
  365. *
  366. * @ret rc Return status code
  367. */
  368. static int scsicmd_command ( struct scsi_command *scsicmd ) {
  369. struct scsi_device *scsidev = scsicmd->scsidev;
  370. struct scsi_cmd command;
  371. int tag;
  372. int rc;
  373. /* Construct command */
  374. memset ( &command, 0, sizeof ( command ) );
  375. memcpy ( &command.lun, &scsidev->lun, sizeof ( command.lun ) );
  376. scsicmd->type->cmd ( scsicmd, &command );
  377. /* Issue command */
  378. if ( ( tag = scsi_command ( &scsidev->scsi, &scsicmd->scsi,
  379. &command ) ) < 0 ) {
  380. rc = tag;
  381. DBGC ( scsidev, "SCSI %p could not issue command: %s\n",
  382. scsidev, strerror ( rc ) );
  383. return rc;
  384. }
  385. /* Record tag */
  386. if ( scsicmd->tag ) {
  387. DBGC ( scsidev, "SCSI %p tag %08x is now tag %08x\n",
  388. scsidev, scsicmd->tag, tag );
  389. }
  390. scsicmd->tag = tag;
  391. DBGC2 ( scsidev, "SCSI %p tag %08x %s " SCSI_CDB_FORMAT "\n",
  392. scsidev, scsicmd->tag, scsicmd->type->name,
  393. SCSI_CDB_DATA ( command.cdb ) );
  394. return 0;
  395. }
  396. /**
  397. * Handle SCSI command completion
  398. *
  399. * @v scsicmd SCSI command
  400. * @v rc Reason for close
  401. */
  402. static void scsicmd_done ( struct scsi_command *scsicmd, int rc ) {
  403. struct scsi_device *scsidev = scsicmd->scsidev;
  404. /* Restart SCSI interface */
  405. intf_restart ( &scsicmd->scsi, rc );
  406. /* SCSI targets have an annoying habit of returning occasional
  407. * pointless "error" messages such as "power-on occurred", so
  408. * we have to be prepared to retry commands.
  409. */
  410. if ( ( rc != 0 ) && ( scsicmd->retries++ < SCSICMD_MAX_RETRIES ) ) {
  411. /* Retry command */
  412. DBGC ( scsidev, "SCSI %p tag %08x failed: %s\n",
  413. scsidev, scsicmd->tag, strerror ( rc ) );
  414. DBGC ( scsidev, "SCSI %p tag %08x retrying (retry %d)\n",
  415. scsidev, scsicmd->tag, scsicmd->retries );
  416. if ( ( rc = scsicmd_command ( scsicmd ) ) == 0 )
  417. return;
  418. }
  419. /* If we didn't (successfully) reissue the command, hand over
  420. * to the command completion handler.
  421. */
  422. scsicmd->type->done ( scsicmd, rc );
  423. }
  424. /**
  425. * Handle SCSI response
  426. *
  427. * @v scsicmd SCSI command
  428. * @v response SCSI response
  429. */
  430. static void scsicmd_response ( struct scsi_command *scsicmd,
  431. struct scsi_rsp *response ) {
  432. struct scsi_device *scsidev = scsicmd->scsidev;
  433. size_t overrun;
  434. size_t underrun;
  435. int rc;
  436. if ( response->status == 0 ) {
  437. scsicmd_done ( scsicmd, 0 );
  438. } else {
  439. DBGC ( scsidev, "SCSI %p tag %08x status %02x",
  440. scsidev, scsicmd->tag, response->status );
  441. if ( response->overrun > 0 ) {
  442. overrun = response->overrun;
  443. DBGC ( scsidev, " overrun +%zd", overrun );
  444. } else if ( response->overrun < 0 ) {
  445. underrun = -(response->overrun);
  446. DBGC ( scsidev, " underrun -%zd", underrun );
  447. }
  448. DBGC ( scsidev, " sense %02x key %02x additional %04x\n",
  449. ( response->sense.code & SCSI_SENSE_CODE_MASK ),
  450. ( response->sense.key & SCSI_SENSE_KEY_MASK ),
  451. ntohs ( response->sense.additional ) );
  452. /* Construct error number from sense data */
  453. rc = -EIO_SENSE ( response->sense.key & SCSI_SENSE_KEY_MASK );
  454. scsicmd_done ( scsicmd, rc );
  455. }
  456. }
  457. /**
  458. * Construct SCSI READ command
  459. *
  460. * @v scsicmd SCSI command
  461. * @v command SCSI command IU
  462. */
  463. static void scsicmd_read_cmd ( struct scsi_command *scsicmd,
  464. struct scsi_cmd *command ) {
  465. if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
  466. /* Use READ (16) */
  467. command->cdb.read16.opcode = SCSI_OPCODE_READ_16;
  468. command->cdb.read16.lba = cpu_to_be64 ( scsicmd->lba );
  469. command->cdb.read16.len = cpu_to_be32 ( scsicmd->count );
  470. } else {
  471. /* Use READ (10) */
  472. command->cdb.read10.opcode = SCSI_OPCODE_READ_10;
  473. command->cdb.read10.lba = cpu_to_be32 ( scsicmd->lba );
  474. command->cdb.read10.len = cpu_to_be16 ( scsicmd->count );
  475. }
  476. command->data_in = scsicmd->buffer;
  477. command->data_in_len = scsicmd->len;
  478. }
  479. /** SCSI READ command type */
  480. static struct scsi_command_type scsicmd_read = {
  481. .name = "READ",
  482. .cmd = scsicmd_read_cmd,
  483. .done = scsicmd_close,
  484. };
  485. /**
  486. * Construct SCSI WRITE command
  487. *
  488. * @v scsicmd SCSI command
  489. * @v command SCSI command IU
  490. */
  491. static void scsicmd_write_cmd ( struct scsi_command *scsicmd,
  492. struct scsi_cmd *command ) {
  493. if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
  494. /* Use WRITE (16) */
  495. command->cdb.write16.opcode = SCSI_OPCODE_WRITE_16;
  496. command->cdb.write16.lba = cpu_to_be64 ( scsicmd->lba );
  497. command->cdb.write16.len = cpu_to_be32 ( scsicmd->count );
  498. } else {
  499. /* Use WRITE (10) */
  500. command->cdb.write10.opcode = SCSI_OPCODE_WRITE_10;
  501. command->cdb.write10.lba = cpu_to_be32 ( scsicmd->lba );
  502. command->cdb.write10.len = cpu_to_be16 ( scsicmd->count );
  503. }
  504. command->data_out = scsicmd->buffer;
  505. command->data_out_len = scsicmd->len;
  506. }
  507. /** SCSI WRITE command type */
  508. static struct scsi_command_type scsicmd_write = {
  509. .name = "WRITE",
  510. .cmd = scsicmd_write_cmd,
  511. .done = scsicmd_close,
  512. };
  513. /** SCSI READ CAPACITY private data */
  514. struct scsi_read_capacity_private {
  515. /** Use READ CAPACITY (16) */
  516. int use16;
  517. /** Data buffer for READ CAPACITY commands */
  518. union {
  519. /** Data buffer for READ CAPACITY (10) */
  520. struct scsi_capacity_10 capacity10;
  521. /** Data buffer for READ CAPACITY (16) */
  522. struct scsi_capacity_16 capacity16;
  523. } capacity;
  524. };
  525. /**
  526. * Construct SCSI READ CAPACITY command
  527. *
  528. * @v scsicmd SCSI command
  529. * @v command SCSI command IU
  530. */
  531. static void scsicmd_read_capacity_cmd ( struct scsi_command *scsicmd,
  532. struct scsi_cmd *command ) {
  533. struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
  534. struct scsi_cdb_read_capacity_16 *readcap16 = &command->cdb.readcap16;
  535. struct scsi_cdb_read_capacity_10 *readcap10 = &command->cdb.readcap10;
  536. struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
  537. struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
  538. if ( priv->use16 ) {
  539. /* Use READ CAPACITY (16) */
  540. readcap16->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
  541. readcap16->service_action =
  542. SCSI_SERVICE_ACTION_READ_CAPACITY_16;
  543. readcap16->len = cpu_to_be32 ( sizeof ( *capacity16 ) );
  544. command->data_in = virt_to_user ( capacity16 );
  545. command->data_in_len = sizeof ( *capacity16 );
  546. } else {
  547. /* Use READ CAPACITY (10) */
  548. readcap10->opcode = SCSI_OPCODE_READ_CAPACITY_10;
  549. command->data_in = virt_to_user ( capacity10 );
  550. command->data_in_len = sizeof ( *capacity10 );
  551. }
  552. }
  553. /**
  554. * Handle SCSI READ CAPACITY command completion
  555. *
  556. * @v scsicmd SCSI command
  557. * @v rc Reason for completion
  558. */
  559. static void scsicmd_read_capacity_done ( struct scsi_command *scsicmd,
  560. int rc ) {
  561. struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
  562. struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
  563. struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
  564. struct block_device_capacity capacity;
  565. /* Close if command failed */
  566. if ( rc != 0 ) {
  567. scsicmd_close ( scsicmd, rc );
  568. return;
  569. }
  570. /* Extract capacity */
  571. if ( priv->use16 ) {
  572. capacity.blocks = ( be64_to_cpu ( capacity16->lba ) + 1 );
  573. capacity.blksize = be32_to_cpu ( capacity16->blksize );
  574. } else {
  575. capacity.blocks = ( be32_to_cpu ( capacity10->lba ) + 1 );
  576. capacity.blksize = be32_to_cpu ( capacity10->blksize );
  577. /* If capacity range was exceeded (i.e. capacity.lba
  578. * was 0xffffffff, meaning that blockdev->blocks is
  579. * now zero), use READ CAPACITY (16) instead. READ
  580. * CAPACITY (16) is not mandatory, so we can't just
  581. * use it straight off.
  582. */
  583. if ( capacity.blocks == 0 ) {
  584. priv->use16 = 1;
  585. if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 ) {
  586. scsicmd_close ( scsicmd, rc );
  587. return;
  588. }
  589. return;
  590. }
  591. }
  592. capacity.max_count = -1U;
  593. /* Return capacity to caller */
  594. block_capacity ( &scsicmd->block, &capacity );
  595. /* Close command */
  596. scsicmd_close ( scsicmd, 0 );
  597. }
  598. /** SCSI READ CAPACITY command type */
  599. static struct scsi_command_type scsicmd_read_capacity = {
  600. .name = "READ CAPACITY",
  601. .priv_len = sizeof ( struct scsi_read_capacity_private ),
  602. .cmd = scsicmd_read_capacity_cmd,
  603. .done = scsicmd_read_capacity_done,
  604. };
  605. /**
  606. * Construct SCSI TEST UNIT READY command
  607. *
  608. * @v scsicmd SCSI command
  609. * @v command SCSI command IU
  610. */
  611. static void scsicmd_test_unit_ready_cmd ( struct scsi_command *scsicmd __unused,
  612. struct scsi_cmd *command ) {
  613. struct scsi_cdb_test_unit_ready *testready = &command->cdb.testready;
  614. testready->opcode = SCSI_OPCODE_TEST_UNIT_READY;
  615. }
  616. /** SCSI TEST UNIT READY command type */
  617. static struct scsi_command_type scsicmd_test_unit_ready = {
  618. .name = "TEST UNIT READY",
  619. .cmd = scsicmd_test_unit_ready_cmd,
  620. .done = scsicmd_close,
  621. };
  622. /** SCSI command block interface operations */
  623. static struct interface_operation scsicmd_block_op[] = {
  624. INTF_OP ( intf_close, struct scsi_command *, scsicmd_close ),
  625. };
  626. /** SCSI command block interface descriptor */
  627. static struct interface_descriptor scsicmd_block_desc =
  628. INTF_DESC_PASSTHRU ( struct scsi_command, block,
  629. scsicmd_block_op, scsi );
  630. /** SCSI command SCSI interface operations */
  631. static struct interface_operation scsicmd_scsi_op[] = {
  632. INTF_OP ( intf_close, struct scsi_command *, scsicmd_done ),
  633. INTF_OP ( scsi_response, struct scsi_command *, scsicmd_response ),
  634. };
  635. /** SCSI command SCSI interface descriptor */
  636. static struct interface_descriptor scsicmd_scsi_desc =
  637. INTF_DESC_PASSTHRU ( struct scsi_command, scsi,
  638. scsicmd_scsi_op, block );
  639. /**
  640. * Create SCSI command
  641. *
  642. * @v scsidev SCSI device
  643. * @v block Block data interface
  644. * @v type SCSI command type
  645. * @v lba Starting logical block address
  646. * @v count Number of blocks to transfer
  647. * @v buffer Data buffer
  648. * @v len Length of data buffer
  649. * @ret rc Return status code
  650. */
  651. static int scsidev_command ( struct scsi_device *scsidev,
  652. struct interface *block,
  653. struct scsi_command_type *type,
  654. uint64_t lba, unsigned int count,
  655. userptr_t buffer, size_t len ) {
  656. struct scsi_command *scsicmd;
  657. int rc;
  658. /* Allocate and initialise structure */
  659. scsicmd = zalloc ( sizeof ( *scsicmd ) + type->priv_len );
  660. if ( ! scsicmd ) {
  661. rc = -ENOMEM;
  662. goto err_zalloc;
  663. }
  664. ref_init ( &scsicmd->refcnt, scsicmd_free );
  665. intf_init ( &scsicmd->block, &scsicmd_block_desc, &scsicmd->refcnt );
  666. intf_init ( &scsicmd->scsi, &scsicmd_scsi_desc,
  667. &scsicmd->refcnt );
  668. scsicmd->scsidev = scsidev_get ( scsidev );
  669. list_add ( &scsicmd->list, &scsidev->cmds );
  670. scsicmd->type = type;
  671. scsicmd->lba = lba;
  672. scsicmd->count = count;
  673. scsicmd->buffer = buffer;
  674. scsicmd->len = len;
  675. /* Issue SCSI command */
  676. if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 )
  677. goto err_command;
  678. /* Attach to parent interface, mortalise self, and return */
  679. intf_plug_plug ( &scsicmd->block, block );
  680. ref_put ( &scsicmd->refcnt );
  681. return 0;
  682. err_command:
  683. scsicmd_close ( scsicmd, rc );
  684. ref_put ( &scsicmd->refcnt );
  685. err_zalloc:
  686. return rc;
  687. }
  688. /**
  689. * Issue SCSI block read
  690. *
  691. * @v scsidev SCSI device
  692. * @v block Block data interface
  693. * @v lba Starting logical block address
  694. * @v count Number of blocks to transfer
  695. * @v buffer Data buffer
  696. * @v len Length of data buffer
  697. * @ret rc Return status code
  698. */
  699. static int scsidev_read ( struct scsi_device *scsidev,
  700. struct interface *block,
  701. uint64_t lba, unsigned int count,
  702. userptr_t buffer, size_t len ) {
  703. return scsidev_command ( scsidev, block, &scsicmd_read,
  704. lba, count, buffer, len );
  705. }
  706. /**
  707. * Issue SCSI block write
  708. *
  709. * @v scsidev SCSI device
  710. * @v block Block data interface
  711. * @v lba Starting logical block address
  712. * @v count Number of blocks to transfer
  713. * @v buffer Data buffer
  714. * @v len Length of data buffer
  715. * @ret rc Return status code
  716. */
  717. static int scsidev_write ( struct scsi_device *scsidev,
  718. struct interface *block,
  719. uint64_t lba, unsigned int count,
  720. userptr_t buffer, size_t len ) {
  721. return scsidev_command ( scsidev, block, &scsicmd_write,
  722. lba, count, buffer, len );
  723. }
  724. /**
  725. * Read SCSI device capacity
  726. *
  727. * @v scsidev SCSI device
  728. * @v block Block data interface
  729. * @ret rc Return status code
  730. */
  731. static int scsidev_read_capacity ( struct scsi_device *scsidev,
  732. struct interface *block ) {
  733. return scsidev_command ( scsidev, block, &scsicmd_read_capacity,
  734. 0, 0, UNULL, 0 );
  735. }
  736. /**
  737. * Test to see if SCSI device is ready
  738. *
  739. * @v scsidev SCSI device
  740. * @v block Block data interface
  741. * @ret rc Return status code
  742. */
  743. static int scsidev_test_unit_ready ( struct scsi_device *scsidev,
  744. struct interface *block ) {
  745. return scsidev_command ( scsidev, block, &scsicmd_test_unit_ready,
  746. 0, 0, UNULL, 0 );
  747. }
  748. /**
  749. * Check SCSI device flow-control window
  750. *
  751. * @v scsidev SCSI device
  752. * @ret len Length of window
  753. */
  754. static size_t scsidev_window ( struct scsi_device *scsidev ) {
  755. /* Refuse commands until unit is confirmed ready */
  756. if ( ! ( scsidev->flags & SCSIDEV_UNIT_READY ) )
  757. return 0;
  758. return xfer_window ( &scsidev->scsi );
  759. }
  760. /**
  761. * Close SCSI device
  762. *
  763. * @v scsidev SCSI device
  764. * @v rc Reason for close
  765. */
  766. static void scsidev_close ( struct scsi_device *scsidev, int rc ) {
  767. struct scsi_command *scsicmd;
  768. struct scsi_command *tmp;
  769. /* Stop process */
  770. process_del ( &scsidev->process );
  771. /* Shut down interfaces */
  772. intf_shutdown ( &scsidev->block, rc );
  773. intf_shutdown ( &scsidev->scsi, rc );
  774. intf_shutdown ( &scsidev->ready, rc );
  775. /* Shut down any remaining commands */
  776. list_for_each_entry_safe ( scsicmd, tmp, &scsidev->cmds, list ) {
  777. scsicmd_get ( scsicmd );
  778. scsicmd_close ( scsicmd, rc );
  779. scsicmd_put ( scsicmd );
  780. }
  781. }
  782. /** SCSI device block interface operations */
  783. static struct interface_operation scsidev_block_op[] = {
  784. INTF_OP ( xfer_window, struct scsi_device *, scsidev_window ),
  785. INTF_OP ( block_read, struct scsi_device *, scsidev_read ),
  786. INTF_OP ( block_write, struct scsi_device *, scsidev_write ),
  787. INTF_OP ( block_read_capacity, struct scsi_device *,
  788. scsidev_read_capacity ),
  789. INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
  790. };
  791. /** SCSI device block interface descriptor */
  792. static struct interface_descriptor scsidev_block_desc =
  793. INTF_DESC_PASSTHRU ( struct scsi_device, block,
  794. scsidev_block_op, scsi );
  795. /**
  796. * Handle SCSI TEST UNIT READY response
  797. *
  798. * @v scsidev SCSI device
  799. * @v rc Reason for close
  800. */
  801. static void scsidev_ready ( struct scsi_device *scsidev, int rc ) {
  802. /* Shut down interface */
  803. intf_shutdown ( &scsidev->ready, rc );
  804. /* Close device on failure */
  805. if ( rc != 0 ) {
  806. DBGC ( scsidev, "SCSI %p not ready: %s\n",
  807. scsidev, strerror ( rc ) );
  808. scsidev_close ( scsidev, rc );
  809. return;
  810. }
  811. /* Mark device as ready */
  812. scsidev->flags |= SCSIDEV_UNIT_READY;
  813. xfer_window_changed ( &scsidev->block );
  814. DBGC ( scsidev, "SCSI %p unit is ready\n", scsidev );
  815. }
  816. /** SCSI device TEST UNIT READY interface operations */
  817. static struct interface_operation scsidev_ready_op[] = {
  818. INTF_OP ( intf_close, struct scsi_device *, scsidev_ready ),
  819. };
  820. /** SCSI device TEST UNIT READY interface descriptor */
  821. static struct interface_descriptor scsidev_ready_desc =
  822. INTF_DESC ( struct scsi_device, ready, scsidev_ready_op );
  823. /**
  824. * SCSI TEST UNIT READY process
  825. *
  826. * @v scsidev SCSI device
  827. */
  828. static void scsidev_step ( struct scsi_device *scsidev ) {
  829. int rc;
  830. /* Do nothing if we have already issued TEST UNIT READY */
  831. if ( scsidev->flags & SCSIDEV_UNIT_TESTED )
  832. return;
  833. /* Wait until underlying SCSI device is ready */
  834. if ( xfer_window ( &scsidev->scsi ) == 0 )
  835. return;
  836. DBGC ( scsidev, "SCSI %p waiting for unit to become ready\n",
  837. scsidev );
  838. /* Mark TEST UNIT READY as sent */
  839. scsidev->flags |= SCSIDEV_UNIT_TESTED;
  840. /* Issue TEST UNIT READY command */
  841. if ( ( rc = scsidev_test_unit_ready ( scsidev, &scsidev->ready )) !=0){
  842. scsidev_close ( scsidev, rc );
  843. return;
  844. }
  845. }
  846. /** SCSI device SCSI interface operations */
  847. static struct interface_operation scsidev_scsi_op[] = {
  848. INTF_OP ( xfer_window_changed, struct scsi_device *, scsidev_step ),
  849. INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
  850. };
  851. /** SCSI device SCSI interface descriptor */
  852. static struct interface_descriptor scsidev_scsi_desc =
  853. INTF_DESC_PASSTHRU ( struct scsi_device, scsi,
  854. scsidev_scsi_op, block );
  855. /** SCSI device process descriptor */
  856. static struct process_descriptor scsidev_process_desc =
  857. PROC_DESC_ONCE ( struct scsi_device, process, scsidev_step );
  858. /**
  859. * Open SCSI device
  860. *
  861. * @v block Block control interface
  862. * @v scsi SCSI control interface
  863. * @v lun SCSI LUN
  864. * @ret rc Return status code
  865. */
  866. int scsi_open ( struct interface *block, struct interface *scsi,
  867. struct scsi_lun *lun ) {
  868. struct scsi_device *scsidev;
  869. /* Allocate and initialise structure */
  870. scsidev = zalloc ( sizeof ( *scsidev ) );
  871. if ( ! scsidev )
  872. return -ENOMEM;
  873. ref_init ( &scsidev->refcnt, NULL );
  874. intf_init ( &scsidev->block, &scsidev_block_desc, &scsidev->refcnt );
  875. intf_init ( &scsidev->scsi, &scsidev_scsi_desc, &scsidev->refcnt );
  876. intf_init ( &scsidev->ready, &scsidev_ready_desc, &scsidev->refcnt );
  877. process_init ( &scsidev->process, &scsidev_process_desc,
  878. &scsidev->refcnt );
  879. INIT_LIST_HEAD ( &scsidev->cmds );
  880. memcpy ( &scsidev->lun, lun, sizeof ( scsidev->lun ) );
  881. DBGC ( scsidev, "SCSI %p created for LUN " SCSI_LUN_FORMAT "\n",
  882. scsidev, SCSI_LUN_DATA ( scsidev->lun ) );
  883. /* Attach to SCSI and parent interfaces, mortalise self, and return */
  884. intf_plug_plug ( &scsidev->scsi, scsi );
  885. intf_plug_plug ( &scsidev->block, block );
  886. ref_put ( &scsidev->refcnt );
  887. return 0;
  888. }