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.

scsi.c 26KB

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