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.0KB

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