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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 <gpxe/blockdev.h>
  25. #include <gpxe/process.h>
  26. #include <gpxe/scsi.h>
  27. /** @file
  28. *
  29. * SCSI block device
  30. *
  31. */
  32. /** Maximum number of dummy "read capacity (10)" operations
  33. *
  34. * These are issued at connection setup to draw out various useless
  35. * power-on messages.
  36. */
  37. #define SCSI_MAX_DUMMY_READ_CAP 10
  38. static inline __attribute__ (( always_inline )) struct scsi_device *
  39. block_to_scsi ( struct block_device *blockdev ) {
  40. return container_of ( blockdev, struct scsi_device, blockdev );
  41. }
  42. /**
  43. * Handle SCSI command with no backing device
  44. *
  45. * @v scsi SCSI device
  46. * @v command SCSI command
  47. * @ret rc Return status code
  48. */
  49. int scsi_detached_command ( struct scsi_device *scsi __unused,
  50. struct scsi_command *command __unused ) {
  51. return -ENODEV;
  52. }
  53. /**
  54. * Issue SCSI command
  55. *
  56. * @v scsi SCSI device
  57. * @v command SCSI command
  58. * @ret rc Return status code
  59. */
  60. static int scsi_command ( struct scsi_device *scsi,
  61. struct scsi_command *command ) {
  62. int rc;
  63. DBGC2 ( scsi, "SCSI %p " SCSI_CDB_FORMAT "\n",
  64. scsi, SCSI_CDB_DATA ( command->cdb ) );
  65. /* Clear sense response code before issuing command */
  66. command->sense_response = 0;
  67. /* Flag command as in-progress */
  68. command->rc = -EINPROGRESS;
  69. /* Issue SCSI command */
  70. if ( ( rc = scsi->command ( scsi, command ) ) != 0 ) {
  71. /* Something went wrong with the issuing mechanism */
  72. DBGC ( scsi, "SCSI %p " SCSI_CDB_FORMAT " err %s\n",
  73. scsi, SCSI_CDB_DATA ( command->cdb ), strerror ( rc ) );
  74. return rc;
  75. }
  76. /* Wait for command to complete */
  77. while ( command->rc == -EINPROGRESS )
  78. step();
  79. if ( ( rc = command->rc ) != 0 ) {
  80. /* Something went wrong with the command execution */
  81. DBGC ( scsi, "SCSI %p " SCSI_CDB_FORMAT " err %s\n",
  82. scsi, SCSI_CDB_DATA ( command->cdb ), strerror ( rc ) );
  83. return rc;
  84. }
  85. /* Check for SCSI errors */
  86. if ( command->status != 0 ) {
  87. DBGC ( scsi, "SCSI %p " SCSI_CDB_FORMAT " status %02x sense "
  88. "%02x\n", scsi, SCSI_CDB_DATA ( command->cdb ),
  89. command->status, command->sense_response );
  90. return -EIO;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * Read block from SCSI device using READ (10)
  96. *
  97. * @v blockdev Block device
  98. * @v block LBA block number
  99. * @v count Block count
  100. * @v buffer Data buffer
  101. * @ret rc Return status code
  102. */
  103. static int scsi_read_10 ( struct block_device *blockdev, uint64_t block,
  104. unsigned long count, userptr_t buffer ) {
  105. struct scsi_device *scsi = block_to_scsi ( blockdev );
  106. struct scsi_command command;
  107. struct scsi_cdb_read_10 *cdb = &command.cdb.read10;
  108. /* Issue READ (10) */
  109. memset ( &command, 0, sizeof ( command ) );
  110. cdb->opcode = SCSI_OPCODE_READ_10;
  111. cdb->lba = cpu_to_be32 ( block );
  112. cdb->len = cpu_to_be16 ( count );
  113. command.data_in = buffer;
  114. command.data_in_len = ( count * blockdev->blksize );
  115. return scsi_command ( scsi, &command );
  116. }
  117. /**
  118. * Read block from SCSI device using READ (16)
  119. *
  120. * @v blockdev Block device
  121. * @v block LBA block number
  122. * @v count Block count
  123. * @v buffer Data buffer
  124. * @ret rc Return status code
  125. */
  126. static int scsi_read_16 ( struct block_device *blockdev, uint64_t block,
  127. unsigned long count, userptr_t buffer ) {
  128. struct scsi_device *scsi = block_to_scsi ( blockdev );
  129. struct scsi_command command;
  130. struct scsi_cdb_read_16 *cdb = &command.cdb.read16;
  131. /* Issue READ (16) */
  132. memset ( &command, 0, sizeof ( command ) );
  133. cdb->opcode = SCSI_OPCODE_READ_16;
  134. cdb->lba = cpu_to_be64 ( block );
  135. cdb->len = cpu_to_be32 ( count );
  136. command.data_in = buffer;
  137. command.data_in_len = ( count * blockdev->blksize );
  138. return scsi_command ( scsi, &command );
  139. }
  140. /**
  141. * Write block to SCSI device using WRITE (10)
  142. *
  143. * @v blockdev Block device
  144. * @v block LBA block number
  145. * @v count Block count
  146. * @v buffer Data buffer
  147. * @ret rc Return status code
  148. */
  149. static int scsi_write_10 ( struct block_device *blockdev, uint64_t block,
  150. unsigned long count, userptr_t buffer ) {
  151. struct scsi_device *scsi = block_to_scsi ( blockdev );
  152. struct scsi_command command;
  153. struct scsi_cdb_write_10 *cdb = &command.cdb.write10;
  154. /* Issue WRITE (10) */
  155. memset ( &command, 0, sizeof ( command ) );
  156. cdb->opcode = SCSI_OPCODE_WRITE_10;
  157. cdb->lba = cpu_to_be32 ( block );
  158. cdb->len = cpu_to_be16 ( count );
  159. command.data_out = buffer;
  160. command.data_out_len = ( count * blockdev->blksize );
  161. return scsi_command ( scsi, &command );
  162. }
  163. /**
  164. * Write block to SCSI device using WRITE (16)
  165. *
  166. * @v blockdev Block device
  167. * @v block LBA block number
  168. * @v count Block count
  169. * @v buffer Data buffer
  170. * @ret rc Return status code
  171. */
  172. static int scsi_write_16 ( struct block_device *blockdev, uint64_t block,
  173. unsigned long count, userptr_t buffer ) {
  174. struct scsi_device *scsi = block_to_scsi ( blockdev );
  175. struct scsi_command command;
  176. struct scsi_cdb_write_16 *cdb = &command.cdb.write16;
  177. /* Issue WRITE (16) */
  178. memset ( &command, 0, sizeof ( command ) );
  179. cdb->opcode = SCSI_OPCODE_WRITE_16;
  180. cdb->lba = cpu_to_be64 ( block );
  181. cdb->len = cpu_to_be32 ( count );
  182. command.data_out = buffer;
  183. command.data_out_len = ( count * blockdev->blksize );
  184. return scsi_command ( scsi, &command );
  185. }
  186. /**
  187. * Read capacity of SCSI device via READ CAPACITY (10)
  188. *
  189. * @v blockdev Block device
  190. * @ret rc Return status code
  191. */
  192. static int scsi_read_capacity_10 ( struct block_device *blockdev ) {
  193. struct scsi_device *scsi = block_to_scsi ( blockdev );
  194. struct scsi_command command;
  195. struct scsi_cdb_read_capacity_10 *cdb = &command.cdb.readcap10;
  196. struct scsi_capacity_10 capacity;
  197. int rc;
  198. /* Issue READ CAPACITY (10) */
  199. memset ( &command, 0, sizeof ( command ) );
  200. cdb->opcode = SCSI_OPCODE_READ_CAPACITY_10;
  201. command.data_in = virt_to_user ( &capacity );
  202. command.data_in_len = sizeof ( capacity );
  203. if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
  204. return rc;
  205. /* Fill in block device fields */
  206. blockdev->blksize = be32_to_cpu ( capacity.blksize );
  207. blockdev->blocks = ( be32_to_cpu ( capacity.lba ) + 1 );
  208. return 0;
  209. }
  210. /**
  211. * Read capacity of SCSI device via READ CAPACITY (16)
  212. *
  213. * @v blockdev Block device
  214. * @ret rc Return status code
  215. */
  216. static int scsi_read_capacity_16 ( struct block_device *blockdev ) {
  217. struct scsi_device *scsi = block_to_scsi ( blockdev );
  218. struct scsi_command command;
  219. struct scsi_cdb_read_capacity_16 *cdb = &command.cdb.readcap16;
  220. struct scsi_capacity_16 capacity;
  221. int rc;
  222. /* Issue READ CAPACITY (16) */
  223. memset ( &command, 0, sizeof ( command ) );
  224. cdb->opcode = SCSI_OPCODE_SERVICE_ACTION_IN;
  225. cdb->service_action = SCSI_SERVICE_ACTION_READ_CAPACITY_16;
  226. cdb->len = cpu_to_be32 ( sizeof ( capacity ) );
  227. command.data_in = virt_to_user ( &capacity );
  228. command.data_in_len = sizeof ( capacity );
  229. if ( ( rc = scsi_command ( scsi, &command ) ) != 0 )
  230. return rc;
  231. /* Fill in block device fields */
  232. blockdev->blksize = be32_to_cpu ( capacity.blksize );
  233. blockdev->blocks = ( be64_to_cpu ( capacity.lba ) + 1 );
  234. return 0;
  235. }
  236. static struct block_device_operations scsi_operations_16 = {
  237. .read = scsi_read_16,
  238. .write = scsi_write_16,
  239. };
  240. static struct block_device_operations scsi_operations_10 = {
  241. .read = scsi_read_10,
  242. .write = scsi_write_10,
  243. };
  244. /**
  245. * Initialise SCSI device
  246. *
  247. * @v scsi SCSI device
  248. * @ret rc Return status code
  249. *
  250. * Initialises a SCSI device. The scsi_device::command and
  251. * scsi_device::lun fields must already be filled in. This function
  252. * will configure scsi_device::blockdev, including issuing a READ
  253. * CAPACITY call to determine the block size and total device size.
  254. */
  255. int init_scsidev ( struct scsi_device *scsi ) {
  256. unsigned int i;
  257. int rc;
  258. /* Issue some theoretically extraneous READ CAPACITY (10)
  259. * commands, solely in order to draw out the "CHECK CONDITION
  260. * (power-on occurred)", "CHECK CONDITION (reported LUNs data
  261. * has changed)" etc. that some dumb targets insist on sending
  262. * as an error at start of day. The precise command that we
  263. * use is unimportant; we just need to provide the target with
  264. * an opportunity to send its responses.
  265. */
  266. for ( i = 0 ; i < SCSI_MAX_DUMMY_READ_CAP ; i++ ) {
  267. if ( ( rc = scsi_read_capacity_10 ( &scsi->blockdev ) ) == 0 )
  268. break;
  269. DBGC ( scsi, "SCSI %p ignoring start-of-day error (#%d)\n",
  270. scsi, ( i + 1 ) );
  271. }
  272. /* Try READ CAPACITY (10), which is a mandatory command, first. */
  273. scsi->blockdev.op = &scsi_operations_10;
  274. if ( ( rc = scsi_read_capacity_10 ( &scsi->blockdev ) ) != 0 ) {
  275. DBGC ( scsi, "SCSI %p could not READ CAPACITY (10): %s\n",
  276. scsi, strerror ( rc ) );
  277. return rc;
  278. }
  279. /* If capacity range was exceeded (i.e. capacity.lba was
  280. * 0xffffffff, meaning that blockdev->blocks is now zero), use
  281. * READ CAPACITY (16) instead. READ CAPACITY (16) is not
  282. * mandatory, so we can't just use it straight off.
  283. */
  284. if ( scsi->blockdev.blocks == 0 ) {
  285. scsi->blockdev.op = &scsi_operations_16;
  286. if ( ( rc = scsi_read_capacity_16 ( &scsi->blockdev ) ) != 0 ){
  287. DBGC ( scsi, "SCSI %p could not READ CAPACITY (16): "
  288. "%s\n", scsi, strerror ( rc ) );
  289. return rc;
  290. }
  291. }
  292. DBGC ( scsi, "SCSI %p using READ/WRITE (%d) commands\n", scsi,
  293. ( ( scsi->blockdev.op == &scsi_operations_10 ) ? 10 : 16 ) );
  294. DBGC ( scsi, "SCSI %p capacity is %ld MB (%#llx blocks)\n", scsi,
  295. ( ( unsigned long ) ( scsi->blockdev.blocks >> 11 ) ),
  296. scsi->blockdev.blocks );
  297. return 0;
  298. }
  299. /**
  300. * Parse SCSI LUN
  301. *
  302. * @v lun_string LUN string representation
  303. * @v lun LUN to fill in
  304. * @ret rc Return status code
  305. */
  306. int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun ) {
  307. char *p;
  308. int i;
  309. memset ( lun, 0, sizeof ( *lun ) );
  310. if ( lun_string ) {
  311. p = ( char * ) lun_string;
  312. for ( i = 0 ; i < 4 ; i++ ) {
  313. lun->u16[i] = htons ( strtoul ( p, &p, 16 ) );
  314. if ( *p == '\0' )
  315. break;
  316. if ( *p != '-' )
  317. return -EINVAL;
  318. p++;
  319. }
  320. if ( *p )
  321. return -EINVAL;
  322. }
  323. return 0;
  324. }