Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

scsi.c 27KB

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