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

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