scsi.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. /** TEST UNIT READY has been issued */
  200. SCSIDEV_UNIT_TESTED = 0x0001,
  201. /** TEST UNIT READY has completed successfully */
  202. SCSIDEV_UNIT_READY = 0x0002,
  203. };
  204. /** A SCSI command */
  205. struct scsi_command {
  206. /** Reference count */
  207. struct refcnt refcnt;
  208. /** SCSI device */
  209. struct scsi_device *scsidev;
  210. /** List of SCSI commands */
  211. struct list_head list;
  212. /** Block data interface */
  213. struct interface block;
  214. /** SCSI data interface */
  215. struct interface scsi;
  216. /** Command type */
  217. struct scsi_command_type *type;
  218. /** Starting logical block address */
  219. uint64_t lba;
  220. /** Number of blocks */
  221. unsigned int count;
  222. /** Data buffer */
  223. userptr_t buffer;
  224. /** Length of data buffer */
  225. size_t len;
  226. /** Command tag */
  227. uint32_t tag;
  228. /** Retry count */
  229. unsigned int retries;
  230. /** Private data */
  231. uint8_t priv[0];
  232. };
  233. /** A SCSI command type */
  234. struct scsi_command_type {
  235. /** Name */
  236. const char *name;
  237. /** Additional working space */
  238. size_t priv_len;
  239. /**
  240. * Construct SCSI command IU
  241. *
  242. * @v scsicmd SCSI command
  243. * @v command SCSI command IU
  244. */
  245. void ( * cmd ) ( struct scsi_command *scsicmd,
  246. struct scsi_cmd *command );
  247. /**
  248. * Handle SCSI command completion
  249. *
  250. * @v scsicmd SCSI command
  251. * @v rc Reason for completion
  252. */
  253. void ( * done ) ( struct scsi_command *scsicmd, int rc );
  254. };
  255. /**
  256. * Get reference to SCSI device
  257. *
  258. * @v scsidev SCSI device
  259. * @ret scsidev SCSI device
  260. */
  261. static inline __attribute__ (( always_inline )) struct scsi_device *
  262. scsidev_get ( struct scsi_device *scsidev ) {
  263. ref_get ( &scsidev->refcnt );
  264. return scsidev;
  265. }
  266. /**
  267. * Drop reference to SCSI device
  268. *
  269. * @v scsidev SCSI device
  270. */
  271. static inline __attribute__ (( always_inline )) void
  272. scsidev_put ( struct scsi_device *scsidev ) {
  273. ref_put ( &scsidev->refcnt );
  274. }
  275. /**
  276. * Get reference to SCSI command
  277. *
  278. * @v scsicmd SCSI command
  279. * @ret scsicmd SCSI command
  280. */
  281. static inline __attribute__ (( always_inline )) struct scsi_command *
  282. scsicmd_get ( struct scsi_command *scsicmd ) {
  283. ref_get ( &scsicmd->refcnt );
  284. return scsicmd;
  285. }
  286. /**
  287. * Drop reference to SCSI command
  288. *
  289. * @v scsicmd SCSI command
  290. */
  291. static inline __attribute__ (( always_inline )) void
  292. scsicmd_put ( struct scsi_command *scsicmd ) {
  293. ref_put ( &scsicmd->refcnt );
  294. }
  295. /**
  296. * Get SCSI command private data
  297. *
  298. * @v scsicmd SCSI command
  299. * @ret priv Private data
  300. */
  301. static inline __attribute__ (( always_inline )) void *
  302. scsicmd_priv ( struct scsi_command *scsicmd ) {
  303. return scsicmd->priv;
  304. }
  305. /**
  306. * Free SCSI command
  307. *
  308. * @v refcnt Reference count
  309. */
  310. static void scsicmd_free ( struct refcnt *refcnt ) {
  311. struct scsi_command *scsicmd =
  312. container_of ( refcnt, struct scsi_command, refcnt );
  313. /* Remove from list of commands */
  314. list_del ( &scsicmd->list );
  315. scsidev_put ( scsicmd->scsidev );
  316. /* Free command */
  317. free ( scsicmd );
  318. }
  319. /**
  320. * Close SCSI command
  321. *
  322. * @v scsicmd SCSI command
  323. * @v rc Reason for close
  324. */
  325. static void scsicmd_close ( struct scsi_command *scsicmd, int rc ) {
  326. struct scsi_device *scsidev = scsicmd->scsidev;
  327. if ( rc != 0 ) {
  328. DBGC ( scsidev, "SCSI %p tag %08x closed: %s\n",
  329. scsidev, scsicmd->tag, strerror ( rc ) );
  330. }
  331. /* Shut down interfaces */
  332. intf_shutdown ( &scsicmd->scsi, rc );
  333. intf_shutdown ( &scsicmd->block, rc );
  334. }
  335. /**
  336. * Construct and issue SCSI command
  337. *
  338. * @ret rc Return status code
  339. */
  340. static int scsicmd_command ( struct scsi_command *scsicmd ) {
  341. struct scsi_device *scsidev = scsicmd->scsidev;
  342. struct scsi_cmd command;
  343. int tag;
  344. int rc;
  345. /* Construct command */
  346. memset ( &command, 0, sizeof ( command ) );
  347. memcpy ( &command.lun, &scsidev->lun, sizeof ( command.lun ) );
  348. scsicmd->type->cmd ( scsicmd, &command );
  349. /* Issue command */
  350. if ( ( tag = scsi_command ( &scsidev->scsi, &scsicmd->scsi,
  351. &command ) ) < 0 ) {
  352. rc = tag;
  353. DBGC ( scsidev, "SCSI %p could not issue command: %s\n",
  354. scsidev, strerror ( rc ) );
  355. return rc;
  356. }
  357. /* Record tag */
  358. if ( scsicmd->tag ) {
  359. DBGC ( scsidev, "SCSI %p tag %08x is now tag %08x\n",
  360. scsidev, scsicmd->tag, tag );
  361. }
  362. scsicmd->tag = tag;
  363. DBGC2 ( scsidev, "SCSI %p tag %08x %s " SCSI_CDB_FORMAT "\n",
  364. scsidev, scsicmd->tag, scsicmd->type->name,
  365. SCSI_CDB_DATA ( command.cdb ) );
  366. return 0;
  367. }
  368. /**
  369. * Handle SCSI command completion
  370. *
  371. * @v scsicmd SCSI command
  372. * @v rc Reason for close
  373. */
  374. static void scsicmd_done ( struct scsi_command *scsicmd, int rc ) {
  375. struct scsi_device *scsidev = scsicmd->scsidev;
  376. /* Restart SCSI interface */
  377. intf_restart ( &scsicmd->scsi, rc );
  378. /* SCSI targets have an annoying habit of returning occasional
  379. * pointless "error" messages such as "power-on occurred", so
  380. * we have to be prepared to retry commands.
  381. */
  382. if ( ( rc != 0 ) && ( scsicmd->retries++ < SCSICMD_MAX_RETRIES ) ) {
  383. /* Retry command */
  384. DBGC ( scsidev, "SCSI %p tag %08x failed: %s\n",
  385. scsidev, scsicmd->tag, strerror ( rc ) );
  386. DBGC ( scsidev, "SCSI %p tag %08x retrying (retry %d)\n",
  387. scsidev, scsicmd->tag, scsicmd->retries );
  388. if ( ( rc = scsicmd_command ( scsicmd ) ) == 0 )
  389. return;
  390. }
  391. /* If we didn't (successfully) reissue the command, hand over
  392. * to the command completion handler.
  393. */
  394. scsicmd->type->done ( scsicmd, rc );
  395. }
  396. /**
  397. * Handle SCSI response
  398. *
  399. * @v scsicmd SCSI command
  400. * @v response SCSI response
  401. */
  402. static void scsicmd_response ( struct scsi_command *scsicmd,
  403. struct scsi_rsp *response ) {
  404. struct scsi_device *scsidev = scsicmd->scsidev;
  405. size_t overrun;
  406. size_t underrun;
  407. int rc;
  408. if ( response->status == 0 ) {
  409. scsicmd_done ( scsicmd, 0 );
  410. } else {
  411. DBGC ( scsidev, "SCSI %p tag %08x status %02x",
  412. scsidev, scsicmd->tag, response->status );
  413. if ( response->overrun > 0 ) {
  414. overrun = response->overrun;
  415. DBGC ( scsidev, " overrun +%zd", overrun );
  416. } else if ( response->overrun < 0 ) {
  417. underrun = -(response->overrun);
  418. DBGC ( scsidev, " underrun -%zd", underrun );
  419. }
  420. DBGC ( scsidev, " sense %02x:%02x:%08x\n",
  421. response->sense.code, response->sense.key,
  422. ntohl ( response->sense.info ) );
  423. /* Construct error number from sense data */
  424. rc = -EIO_SENSE ( response->sense.key & SCSI_SENSE_KEY_MASK );
  425. scsicmd_done ( scsicmd, rc );
  426. }
  427. }
  428. /**
  429. * Construct SCSI READ command
  430. *
  431. * @v scsicmd SCSI command
  432. * @v command SCSI command IU
  433. */
  434. static void scsicmd_read_cmd ( struct scsi_command *scsicmd,
  435. struct scsi_cmd *command ) {
  436. if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
  437. /* Use READ (16) */
  438. command->cdb.read16.opcode = SCSI_OPCODE_READ_16;
  439. command->cdb.read16.lba = cpu_to_be64 ( scsicmd->lba );
  440. command->cdb.read16.len = cpu_to_be32 ( scsicmd->count );
  441. } else {
  442. /* Use READ (10) */
  443. command->cdb.read10.opcode = SCSI_OPCODE_READ_10;
  444. command->cdb.read10.lba = cpu_to_be32 ( scsicmd->lba );
  445. command->cdb.read10.len = cpu_to_be16 ( scsicmd->count );
  446. }
  447. command->data_in = scsicmd->buffer;
  448. command->data_in_len = scsicmd->len;
  449. }
  450. /** SCSI READ command type */
  451. static struct scsi_command_type scsicmd_read = {
  452. .name = "READ",
  453. .cmd = scsicmd_read_cmd,
  454. .done = scsicmd_close,
  455. };
  456. /**
  457. * Construct SCSI WRITE command
  458. *
  459. * @v scsicmd SCSI command
  460. * @v command SCSI command IU
  461. */
  462. static void scsicmd_write_cmd ( struct scsi_command *scsicmd,
  463. struct scsi_cmd *command ) {
  464. if ( ( scsicmd->lba + scsicmd->count ) > SCSI_MAX_BLOCK_10 ) {
  465. /* Use WRITE (16) */
  466. command->cdb.write16.opcode = SCSI_OPCODE_WRITE_16;
  467. command->cdb.write16.lba = cpu_to_be64 ( scsicmd->lba );
  468. command->cdb.write16.len = cpu_to_be32 ( scsicmd->count );
  469. } else {
  470. /* Use WRITE (10) */
  471. command->cdb.write10.opcode = SCSI_OPCODE_WRITE_10;
  472. command->cdb.write10.lba = cpu_to_be32 ( scsicmd->lba );
  473. command->cdb.write10.len = cpu_to_be16 ( scsicmd->count );
  474. }
  475. command->data_out = scsicmd->buffer;
  476. command->data_out_len = scsicmd->len;
  477. }
  478. /** SCSI WRITE command type */
  479. static struct scsi_command_type scsicmd_write = {
  480. .name = "WRITE",
  481. .cmd = scsicmd_write_cmd,
  482. .done = scsicmd_close,
  483. };
  484. /** SCSI READ CAPACITY private data */
  485. struct scsi_read_capacity_private {
  486. /** Use READ CAPACITY (16) */
  487. int use16;
  488. /** Data buffer for READ CAPACITY commands */
  489. union {
  490. /** Data buffer for READ CAPACITY (10) */
  491. struct scsi_capacity_10 capacity10;
  492. /** Data buffer for READ CAPACITY (16) */
  493. struct scsi_capacity_16 capacity16;
  494. } capacity;
  495. };
  496. /**
  497. * Construct SCSI READ CAPACITY command
  498. *
  499. * @v scsicmd SCSI command
  500. * @v command SCSI command IU
  501. */
  502. static void scsicmd_read_capacity_cmd ( struct scsi_command *scsicmd,
  503. struct scsi_cmd *command ) {
  504. struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
  505. struct scsi_cdb_read_capacity_16 *readcap16 = &command->cdb.readcap16;
  506. struct scsi_cdb_read_capacity_10 *readcap10 = &command->cdb.readcap10;
  507. struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
  508. struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
  509. if ( priv->use16 ) {
  510. /* Use READ CAPACITY (16) */
  511. readcap16->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
  512. readcap16->service_action =
  513. SCSI_SERVICE_ACTION_READ_CAPACITY_16;
  514. readcap16->len = cpu_to_be32 ( sizeof ( *capacity16 ) );
  515. command->data_in = virt_to_user ( capacity16 );
  516. command->data_in_len = sizeof ( *capacity16 );
  517. } else {
  518. /* Use READ CAPACITY (10) */
  519. readcap10->opcode = SCSI_OPCODE_READ_CAPACITY_10;
  520. command->data_in = virt_to_user ( capacity10 );
  521. command->data_in_len = sizeof ( *capacity10 );
  522. }
  523. }
  524. /**
  525. * Handle SCSI READ CAPACITY command completion
  526. *
  527. * @v scsicmd SCSI command
  528. * @v rc Reason for completion
  529. */
  530. static void scsicmd_read_capacity_done ( struct scsi_command *scsicmd,
  531. int rc ) {
  532. struct scsi_read_capacity_private *priv = scsicmd_priv ( scsicmd );
  533. struct scsi_capacity_16 *capacity16 = &priv->capacity.capacity16;
  534. struct scsi_capacity_10 *capacity10 = &priv->capacity.capacity10;
  535. struct block_device_capacity capacity;
  536. /* Close if command failed */
  537. if ( rc != 0 ) {
  538. scsicmd_close ( scsicmd, rc );
  539. return;
  540. }
  541. /* Extract capacity */
  542. if ( priv->use16 ) {
  543. capacity.blocks = ( be64_to_cpu ( capacity16->lba ) + 1 );
  544. capacity.blksize = be32_to_cpu ( capacity16->blksize );
  545. } else {
  546. capacity.blocks = ( be32_to_cpu ( capacity10->lba ) + 1 );
  547. capacity.blksize = be32_to_cpu ( capacity10->blksize );
  548. /* If capacity range was exceeded (i.e. capacity.lba
  549. * was 0xffffffff, meaning that blockdev->blocks is
  550. * now zero), use READ CAPACITY (16) instead. READ
  551. * CAPACITY (16) is not mandatory, so we can't just
  552. * use it straight off.
  553. */
  554. if ( capacity.blocks == 0 ) {
  555. priv->use16 = 1;
  556. if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 ) {
  557. scsicmd_close ( scsicmd, rc );
  558. return;
  559. }
  560. return;
  561. }
  562. }
  563. capacity.max_count = -1U;
  564. /* Return capacity to caller */
  565. block_capacity ( &scsicmd->block, &capacity );
  566. /* Close command */
  567. scsicmd_close ( scsicmd, 0 );
  568. }
  569. /** SCSI READ CAPACITY command type */
  570. static struct scsi_command_type scsicmd_read_capacity = {
  571. .name = "READ CAPACITY",
  572. .priv_len = sizeof ( struct scsi_read_capacity_private ),
  573. .cmd = scsicmd_read_capacity_cmd,
  574. .done = scsicmd_read_capacity_done,
  575. };
  576. /**
  577. * Construct SCSI TEST UNIT READY command
  578. *
  579. * @v scsicmd SCSI command
  580. * @v command SCSI command IU
  581. */
  582. static void scsicmd_test_unit_ready_cmd ( struct scsi_command *scsicmd __unused,
  583. struct scsi_cmd *command ) {
  584. struct scsi_cdb_test_unit_ready *testready = &command->cdb.testready;
  585. testready->opcode = SCSI_OPCODE_TEST_UNIT_READY;
  586. }
  587. /** SCSI TEST UNIT READY command type */
  588. static struct scsi_command_type scsicmd_test_unit_ready = {
  589. .name = "TEST UNIT READY",
  590. .cmd = scsicmd_test_unit_ready_cmd,
  591. .done = scsicmd_close,
  592. };
  593. /** SCSI command block interface operations */
  594. static struct interface_operation scsicmd_block_op[] = {
  595. INTF_OP ( intf_close, struct scsi_command *, scsicmd_close ),
  596. };
  597. /** SCSI command block interface descriptor */
  598. static struct interface_descriptor scsicmd_block_desc =
  599. INTF_DESC_PASSTHRU ( struct scsi_command, block,
  600. scsicmd_block_op, scsi );
  601. /** SCSI command SCSI interface operations */
  602. static struct interface_operation scsicmd_scsi_op[] = {
  603. INTF_OP ( intf_close, struct scsi_command *, scsicmd_done ),
  604. INTF_OP ( scsi_response, struct scsi_command *, scsicmd_response ),
  605. };
  606. /** SCSI command SCSI interface descriptor */
  607. static struct interface_descriptor scsicmd_scsi_desc =
  608. INTF_DESC_PASSTHRU ( struct scsi_command, scsi,
  609. scsicmd_scsi_op, block );
  610. /**
  611. * Create SCSI command
  612. *
  613. * @v scsidev SCSI device
  614. * @v block Block data interface
  615. * @v type SCSI command type
  616. * @v lba Starting logical block address
  617. * @v count Number of blocks to transfer
  618. * @v buffer Data buffer
  619. * @v len Length of data buffer
  620. * @ret rc Return status code
  621. */
  622. static int scsidev_command ( struct scsi_device *scsidev,
  623. struct interface *block,
  624. struct scsi_command_type *type,
  625. uint64_t lba, unsigned int count,
  626. userptr_t buffer, size_t len ) {
  627. struct scsi_command *scsicmd;
  628. int rc;
  629. /* Allocate and initialise structure */
  630. scsicmd = zalloc ( sizeof ( *scsicmd ) + type->priv_len );
  631. if ( ! scsicmd ) {
  632. rc = -ENOMEM;
  633. goto err_zalloc;
  634. }
  635. ref_init ( &scsicmd->refcnt, scsicmd_free );
  636. intf_init ( &scsicmd->block, &scsicmd_block_desc, &scsicmd->refcnt );
  637. intf_init ( &scsicmd->scsi, &scsicmd_scsi_desc,
  638. &scsicmd->refcnt );
  639. scsicmd->scsidev = scsidev_get ( scsidev );
  640. list_add ( &scsicmd->list, &scsidev->cmds );
  641. scsicmd->type = type;
  642. scsicmd->lba = lba;
  643. scsicmd->count = count;
  644. scsicmd->buffer = buffer;
  645. scsicmd->len = len;
  646. /* Issue SCSI command */
  647. if ( ( rc = scsicmd_command ( scsicmd ) ) != 0 )
  648. goto err_command;
  649. /* Attach to parent interface, mortalise self, and return */
  650. intf_plug_plug ( &scsicmd->block, block );
  651. ref_put ( &scsicmd->refcnt );
  652. return 0;
  653. err_command:
  654. scsicmd_close ( scsicmd, rc );
  655. ref_put ( &scsicmd->refcnt );
  656. err_zalloc:
  657. return rc;
  658. }
  659. /**
  660. * Issue SCSI block read
  661. *
  662. * @v scsidev SCSI device
  663. * @v block Block data interface
  664. * @v lba Starting logical block address
  665. * @v count Number of blocks to transfer
  666. * @v buffer Data buffer
  667. * @v len Length of data buffer
  668. * @ret rc Return status code
  669. */
  670. static int scsidev_read ( struct scsi_device *scsidev,
  671. struct interface *block,
  672. uint64_t lba, unsigned int count,
  673. userptr_t buffer, size_t len ) {
  674. return scsidev_command ( scsidev, block, &scsicmd_read,
  675. lba, count, buffer, len );
  676. }
  677. /**
  678. * Issue SCSI block write
  679. *
  680. * @v scsidev SCSI device
  681. * @v block Block data interface
  682. * @v lba Starting logical block address
  683. * @v count Number of blocks to transfer
  684. * @v buffer Data buffer
  685. * @v len Length of data buffer
  686. * @ret rc Return status code
  687. */
  688. static int scsidev_write ( struct scsi_device *scsidev,
  689. struct interface *block,
  690. uint64_t lba, unsigned int count,
  691. userptr_t buffer, size_t len ) {
  692. return scsidev_command ( scsidev, block, &scsicmd_write,
  693. lba, count, buffer, len );
  694. }
  695. /**
  696. * Read SCSI device capacity
  697. *
  698. * @v scsidev SCSI device
  699. * @v block Block data interface
  700. * @ret rc Return status code
  701. */
  702. static int scsidev_read_capacity ( struct scsi_device *scsidev,
  703. struct interface *block ) {
  704. return scsidev_command ( scsidev, block, &scsicmd_read_capacity,
  705. 0, 0, UNULL, 0 );
  706. }
  707. /**
  708. * Test to see if SCSI device is ready
  709. *
  710. * @v scsidev SCSI device
  711. * @v block Block data interface
  712. * @ret rc Return status code
  713. */
  714. static int scsidev_test_unit_ready ( struct scsi_device *scsidev,
  715. struct interface *block ) {
  716. return scsidev_command ( scsidev, block, &scsicmd_test_unit_ready,
  717. 0, 0, UNULL, 0 );
  718. }
  719. /**
  720. * Check SCSI device flow-control window
  721. *
  722. * @v scsidev SCSI device
  723. * @ret len Length of window
  724. */
  725. static size_t scsidev_window ( struct scsi_device *scsidev ) {
  726. /* Refuse commands until unit is confirmed ready */
  727. if ( ! ( scsidev->flags & SCSIDEV_UNIT_READY ) )
  728. return 0;
  729. return xfer_window ( &scsidev->scsi );
  730. }
  731. /**
  732. * Close SCSI device
  733. *
  734. * @v scsidev SCSI device
  735. * @v rc Reason for close
  736. */
  737. static void scsidev_close ( struct scsi_device *scsidev, int rc ) {
  738. struct scsi_command *scsicmd;
  739. struct scsi_command *tmp;
  740. /* Stop process */
  741. process_del ( &scsidev->process );
  742. /* Shut down interfaces */
  743. intf_shutdown ( &scsidev->block, rc );
  744. intf_shutdown ( &scsidev->scsi, rc );
  745. intf_shutdown ( &scsidev->ready, rc );
  746. /* Shut down any remaining commands */
  747. list_for_each_entry_safe ( scsicmd, tmp, &scsidev->cmds, list ) {
  748. scsicmd_get ( scsicmd );
  749. scsicmd_close ( scsicmd, rc );
  750. scsicmd_put ( scsicmd );
  751. }
  752. }
  753. /** SCSI device block interface operations */
  754. static struct interface_operation scsidev_block_op[] = {
  755. INTF_OP ( xfer_window, struct scsi_device *, scsidev_window ),
  756. INTF_OP ( block_read, struct scsi_device *, scsidev_read ),
  757. INTF_OP ( block_write, struct scsi_device *, scsidev_write ),
  758. INTF_OP ( block_read_capacity, struct scsi_device *,
  759. scsidev_read_capacity ),
  760. INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
  761. };
  762. /** SCSI device block interface descriptor */
  763. static struct interface_descriptor scsidev_block_desc =
  764. INTF_DESC_PASSTHRU ( struct scsi_device, block,
  765. scsidev_block_op, scsi );
  766. /**
  767. * Handle SCSI TEST UNIT READY response
  768. *
  769. * @v scsidev SCSI device
  770. * @v rc Reason for close
  771. */
  772. static void scsidev_ready ( struct scsi_device *scsidev, int rc ) {
  773. /* Shut down interface */
  774. intf_shutdown ( &scsidev->ready, rc );
  775. /* Close device on failure */
  776. if ( rc != 0 ) {
  777. DBGC ( scsidev, "SCSI %p not ready: %s\n",
  778. scsidev, strerror ( rc ) );
  779. scsidev_close ( scsidev, rc );
  780. return;
  781. }
  782. /* Mark device as ready */
  783. scsidev->flags |= SCSIDEV_UNIT_READY;
  784. xfer_window_changed ( &scsidev->block );
  785. DBGC ( scsidev, "SCSI %p unit is ready\n", scsidev );
  786. }
  787. /** SCSI device TEST UNIT READY interface operations */
  788. static struct interface_operation scsidev_ready_op[] = {
  789. INTF_OP ( intf_close, struct scsi_device *, scsidev_ready ),
  790. };
  791. /** SCSI device TEST UNIT READY interface descriptor */
  792. static struct interface_descriptor scsidev_ready_desc =
  793. INTF_DESC ( struct scsi_device, ready, scsidev_ready_op );
  794. /**
  795. * SCSI TEST UNIT READY process
  796. *
  797. * @v scsidev SCSI device
  798. */
  799. static void scsidev_step ( struct scsi_device *scsidev ) {
  800. int rc;
  801. /* Do nothing if we have already issued TEST UNIT READY */
  802. if ( scsidev->flags & SCSIDEV_UNIT_TESTED )
  803. return;
  804. /* Wait until underlying SCSI device is ready */
  805. if ( xfer_window ( &scsidev->scsi ) == 0 )
  806. return;
  807. DBGC ( scsidev, "SCSI %p waiting for unit to become ready\n",
  808. scsidev );
  809. /* Mark TEST UNIT READY as sent */
  810. scsidev->flags |= SCSIDEV_UNIT_TESTED;
  811. /* Issue TEST UNIT READY command */
  812. if ( ( rc = scsidev_test_unit_ready ( scsidev, &scsidev->ready )) !=0){
  813. scsidev_close ( scsidev, rc );
  814. return;
  815. }
  816. }
  817. /** SCSI device SCSI interface operations */
  818. static struct interface_operation scsidev_scsi_op[] = {
  819. INTF_OP ( xfer_window_changed, struct scsi_device *, scsidev_step ),
  820. INTF_OP ( intf_close, struct scsi_device *, scsidev_close ),
  821. };
  822. /** SCSI device SCSI interface descriptor */
  823. static struct interface_descriptor scsidev_scsi_desc =
  824. INTF_DESC_PASSTHRU ( struct scsi_device, scsi,
  825. scsidev_scsi_op, block );
  826. /** SCSI device process descriptor */
  827. static struct process_descriptor scsidev_process_desc =
  828. PROC_DESC_ONCE ( struct scsi_device, process, scsidev_step );
  829. /**
  830. * Open SCSI device
  831. *
  832. * @v block Block control interface
  833. * @v scsi SCSI control interface
  834. * @v lun SCSI LUN
  835. * @ret rc Return status code
  836. */
  837. int scsi_open ( struct interface *block, struct interface *scsi,
  838. struct scsi_lun *lun ) {
  839. struct scsi_device *scsidev;
  840. /* Allocate and initialise structure */
  841. scsidev = zalloc ( sizeof ( *scsidev ) );
  842. if ( ! scsidev )
  843. return -ENOMEM;
  844. ref_init ( &scsidev->refcnt, NULL );
  845. intf_init ( &scsidev->block, &scsidev_block_desc, &scsidev->refcnt );
  846. intf_init ( &scsidev->scsi, &scsidev_scsi_desc, &scsidev->refcnt );
  847. intf_init ( &scsidev->ready, &scsidev_ready_desc, &scsidev->refcnt );
  848. process_init ( &scsidev->process, &scsidev_process_desc,
  849. &scsidev->refcnt );
  850. INIT_LIST_HEAD ( &scsidev->cmds );
  851. memcpy ( &scsidev->lun, lun, sizeof ( scsidev->lun ) );
  852. DBGC ( scsidev, "SCSI %p created for LUN " SCSI_LUN_FORMAT "\n",
  853. scsidev, SCSI_LUN_DATA ( scsidev->lun ) );
  854. /* Attach to SCSI and parent interfaces, mortalise self, and return */
  855. intf_plug_plug ( &scsidev->scsi, scsi );
  856. intf_plug_plug ( &scsidev->block, block );
  857. ref_put ( &scsidev->refcnt );
  858. return 0;
  859. }