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.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #ifndef _GPXE_SCSI_H
  2. #define _GPXE_SCSI_H
  3. #include <stdint.h>
  4. #include <gpxe/blockdev.h>
  5. #include <gpxe/uaccess.h>
  6. #include <gpxe/refcnt.h>
  7. /** @file
  8. *
  9. * SCSI devices
  10. *
  11. */
  12. FILE_LICENCE ( GPL2_OR_LATER );
  13. /**
  14. * @defgroup scsiops SCSI operation codes
  15. * @{
  16. */
  17. #define SCSI_OPCODE_READ_10 0x28 /**< READ (10) */
  18. #define SCSI_OPCODE_READ_16 0x88 /**< READ (16) */
  19. #define SCSI_OPCODE_WRITE_10 0x2a /**< WRITE (10) */
  20. #define SCSI_OPCODE_WRITE_16 0x8a /**< WRITE (16) */
  21. #define SCSI_OPCODE_READ_CAPACITY_10 0x25 /**< READ CAPACITY (10) */
  22. #define SCSI_OPCODE_SERVICE_ACTION_IN 0x9e /**< SERVICE ACTION IN */
  23. #define SCSI_SERVICE_ACTION_READ_CAPACITY_16 0x10 /**< READ CAPACITY (16) */
  24. /** @} */
  25. /**
  26. * @defgroup scsiflags SCSI flags
  27. * @{
  28. */
  29. #define SCSI_FL_FUA_NV 0x02 /**< Force unit access to NVS */
  30. #define SCSI_FL_FUA 0x08 /**< Force unit access */
  31. #define SCSI_FL_DPO 0x10 /**< Disable cache page out */
  32. /** @} */
  33. /**
  34. * @defgroup scsicdbs SCSI command data blocks
  35. * @{
  36. */
  37. /** A SCSI "READ (10)" CDB */
  38. struct scsi_cdb_read_10 {
  39. /** Opcode (0x28) */
  40. uint8_t opcode;
  41. /** Flags */
  42. uint8_t flags;
  43. /** Start address
  44. *
  45. * This is a logical block number, in big-endian order.
  46. */
  47. uint32_t lba;
  48. /** Group number */
  49. uint8_t group;
  50. /** Transfer length
  51. *
  52. * This is a logical block count, in big-endian order.
  53. */
  54. uint16_t len;
  55. /** Control byte */
  56. uint8_t control;
  57. } __attribute__ (( packed ));
  58. /** A SCSI "READ (16)" CDB */
  59. struct scsi_cdb_read_16 {
  60. /** Opcode (0x88) */
  61. uint8_t opcode;
  62. /** Flags */
  63. uint8_t flags;
  64. /** Start address
  65. *
  66. * This is a logical block number, in big-endian order.
  67. */
  68. uint64_t lba;
  69. /** Transfer length
  70. *
  71. * This is a logical block count, in big-endian order.
  72. */
  73. uint32_t len;
  74. /** Group number */
  75. uint8_t group;
  76. /** Control byte */
  77. uint8_t control;
  78. } __attribute__ (( packed ));
  79. /** A SCSI "WRITE (10)" CDB */
  80. struct scsi_cdb_write_10 {
  81. /** Opcode (0x2a) */
  82. uint8_t opcode;
  83. /** Flags */
  84. uint8_t flags;
  85. /** Start address
  86. *
  87. * This is a logical block number, in big-endian order.
  88. */
  89. uint32_t lba;
  90. /** Group number */
  91. uint8_t group;
  92. /** Transfer length
  93. *
  94. * This is a logical block count, in big-endian order.
  95. */
  96. uint16_t len;
  97. /** Control byte */
  98. uint8_t control;
  99. } __attribute__ (( packed ));
  100. /** A SCSI "WRITE (16)" CDB */
  101. struct scsi_cdb_write_16 {
  102. /** Opcode (0x8a) */
  103. uint8_t opcode;
  104. /** Flags */
  105. uint8_t flags;
  106. /** Start address
  107. *
  108. * This is a logical block number, in big-endian order.
  109. */
  110. uint64_t lba;
  111. /** Transfer length
  112. *
  113. * This is a logical block count, in big-endian order.
  114. */
  115. uint32_t len;
  116. /** Group number */
  117. uint8_t group;
  118. /** Control byte */
  119. uint8_t control;
  120. } __attribute__ (( packed ));
  121. /** A SCSI "READ CAPACITY (10)" CDB */
  122. struct scsi_cdb_read_capacity_10 {
  123. /** Opcode (0x25) */
  124. uint8_t opcode;
  125. /** Reserved */
  126. uint8_t reserved_a;
  127. /** Logical block address
  128. *
  129. * Applicable only if the PMI bit is set.
  130. */
  131. uint32_t lba;
  132. /** Reserved */
  133. uint8_t reserved_b[3];
  134. /** Control byte */
  135. uint8_t control;
  136. } __attribute__ (( packed ));
  137. /** SCSI "READ CAPACITY (10)" parameter data */
  138. struct scsi_capacity_10 {
  139. /** Maximum logical block number */
  140. uint32_t lba;
  141. /** Block length in bytes */
  142. uint32_t blksize;
  143. } __attribute__ (( packed ));
  144. /** A SCSI "READ CAPACITY (16)" CDB */
  145. struct scsi_cdb_read_capacity_16 {
  146. /** Opcode (0x9e) */
  147. uint8_t opcode;
  148. /** Service action */
  149. uint8_t service_action;
  150. /** Logical block address
  151. *
  152. * Applicable only if the PMI bit is set.
  153. */
  154. uint64_t lba;
  155. /** Transfer length
  156. *
  157. * This is the size of the data-in buffer, in bytes.
  158. */
  159. uint32_t len;
  160. /** Reserved */
  161. uint8_t reserved;
  162. /** Control byte */
  163. uint8_t control;
  164. } __attribute__ (( packed ));
  165. /** SCSI "READ CAPACITY (16)" parameter data */
  166. struct scsi_capacity_16 {
  167. /** Maximum logical block number */
  168. uint64_t lba;
  169. /** Block length in bytes */
  170. uint32_t blksize;
  171. /** Reserved */
  172. uint8_t reserved[20];
  173. } __attribute__ (( packed ));
  174. /** A SCSI Command Data Block */
  175. union scsi_cdb {
  176. struct scsi_cdb_read_10 read10;
  177. struct scsi_cdb_read_16 read16;
  178. struct scsi_cdb_write_10 write10;
  179. struct scsi_cdb_write_16 write16;
  180. struct scsi_cdb_read_capacity_10 readcap10;
  181. struct scsi_cdb_read_capacity_16 readcap16;
  182. unsigned char bytes[16];
  183. };
  184. /** printf() format for dumping a scsi_cdb */
  185. #define SCSI_CDB_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
  186. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
  187. /** printf() parameters for dumping a scsi_cdb */
  188. #define SCSI_CDB_DATA(cdb) \
  189. (cdb).bytes[0], (cdb).bytes[1], (cdb).bytes[2], (cdb).bytes[3], \
  190. (cdb).bytes[4], (cdb).bytes[5], (cdb).bytes[6], (cdb).bytes[7], \
  191. (cdb).bytes[8], (cdb).bytes[9], (cdb).bytes[10], (cdb).bytes[11], \
  192. (cdb).bytes[12], (cdb).bytes[13], (cdb).bytes[14], (cdb).bytes[15]
  193. /** @} */
  194. /** A SCSI command */
  195. struct scsi_command {
  196. /** CDB for this command */
  197. union scsi_cdb cdb;
  198. /** Data-out buffer (may be NULL) */
  199. userptr_t data_out;
  200. /** Data-out buffer length
  201. *
  202. * Must be zero if @c data_out is NULL
  203. */
  204. size_t data_out_len;
  205. /** Data-in buffer (may be NULL) */
  206. userptr_t data_in;
  207. /** Data-in buffer length
  208. *
  209. * Must be zero if @c data_in is NULL
  210. */
  211. size_t data_in_len;
  212. /** SCSI status code */
  213. uint8_t status;
  214. /** SCSI sense response code */
  215. uint8_t sense_response;
  216. /** Command status code */
  217. int rc;
  218. };
  219. /** A SCSI device */
  220. struct scsi_device {
  221. /** Block device interface */
  222. struct block_device blockdev;
  223. /** Logical unit number (LUN)
  224. *
  225. * This is a four-level LUN as specified by SAM-2, in
  226. * big-endian order.
  227. */
  228. uint64_t lun;
  229. /**
  230. * Issue SCSI command
  231. *
  232. * @v scsi SCSI device
  233. * @v command SCSI command
  234. * @ret rc Return status code
  235. *
  236. * Note that a successful return status code indicates only
  237. * that the SCSI command was issued. The caller must check
  238. * the status field in the command structure to see when the
  239. * command completes and whether, for example, the device
  240. * returned CHECK CONDITION or some other non-success status
  241. * code.
  242. */
  243. int ( * command ) ( struct scsi_device *scsi,
  244. struct scsi_command *command );
  245. /** Backing device */
  246. struct refcnt *backend;
  247. };
  248. extern int init_scsidev ( struct scsi_device *scsi );
  249. #endif /* _GPXE_SCSI_H */