Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

scsi.c 9.7KB

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