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

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