5 #include <ipxe/uaccess.h>
6 #include <ipxe/interface.h>
14 FILE_LICENCE ( GPL2_OR_LATER );
16 /** Maximum block for READ/WRITE (10) commands */
17 #define SCSI_MAX_BLOCK_10 0xffffffffULL
20 * @defgroup scsiops SCSI operation codes
24 #define SCSI_OPCODE_READ_10 0x28 /**< READ (10) */
25 #define SCSI_OPCODE_READ_16 0x88 /**< READ (16) */
26 #define SCSI_OPCODE_WRITE_10 0x2a /**< WRITE (10) */
27 #define SCSI_OPCODE_WRITE_16 0x8a /**< WRITE (16) */
28 #define SCSI_OPCODE_READ_CAPACITY_10 0x25 /**< READ CAPACITY (10) */
29 #define SCSI_OPCODE_SERVICE_ACTION_IN 0x9e /**< SERVICE ACTION IN */
30 #define SCSI_SERVICE_ACTION_READ_CAPACITY_16 0x10 /**< READ CAPACITY (16) */
31 #define SCSI_OPCODE_TEST_UNIT_READY 0x00 /**< TEST UNIT READY */
36 * @defgroup scsiflags SCSI flags
40 #define SCSI_FL_FUA_NV 0x02 /**< Force unit access to NVS */
41 #define SCSI_FL_FUA 0x08 /**< Force unit access */
42 #define SCSI_FL_DPO 0x10 /**< Disable cache page out */
47 * @defgroup scsicdbs SCSI command data blocks
51 /** A SCSI "READ (10)" CDB */
52 struct scsi_cdb_read_10 {
59 * This is a logical block number, in big-endian order.
66 * This is a logical block count, in big-endian order.
71 } __attribute__ (( packed ));
73 /** A SCSI "READ (16)" CDB */
74 struct scsi_cdb_read_16 {
81 * This is a logical block number, in big-endian order.
86 * This is a logical block count, in big-endian order.
93 } __attribute__ (( packed ));
95 /** A SCSI "WRITE (10)" CDB */
96 struct scsi_cdb_write_10 {
103 * This is a logical block number, in big-endian order.
110 * This is a logical block count, in big-endian order.
115 } __attribute__ (( packed ));
117 /** A SCSI "WRITE (16)" CDB */
118 struct scsi_cdb_write_16 {
125 * This is a logical block number, in big-endian order.
130 * This is a logical block count, in big-endian order.
137 } __attribute__ (( packed ));
139 /** A SCSI "READ CAPACITY (10)" CDB */
140 struct scsi_cdb_read_capacity_10 {
145 /** Logical block address
147 * Applicable only if the PMI bit is set.
151 uint8_t reserved_b[3];
154 } __attribute__ (( packed ));
156 /** SCSI "READ CAPACITY (10)" parameter data */
157 struct scsi_capacity_10 {
158 /** Maximum logical block number */
160 /** Block length in bytes */
162 } __attribute__ (( packed ));
164 /** A SCSI "READ CAPACITY (16)" CDB */
165 struct scsi_cdb_read_capacity_16 {
168 /** Service action */
169 uint8_t service_action;
170 /** Logical block address
172 * Applicable only if the PMI bit is set.
177 * This is the size of the data-in buffer, in bytes.
184 } __attribute__ (( packed ));
186 /** SCSI "READ CAPACITY (16)" parameter data */
187 struct scsi_capacity_16 {
188 /** Maximum logical block number */
190 /** Block length in bytes */
193 uint8_t reserved[20];
194 } __attribute__ (( packed ));
196 /** A SCSI "TEST UNIT READY" CDB */
197 struct scsi_cdb_test_unit_ready {
204 } __attribute__ (( packed ));
206 /** A SCSI Command Data Block */
208 struct scsi_cdb_read_10 read10;
209 struct scsi_cdb_read_16 read16;
210 struct scsi_cdb_write_10 write10;
211 struct scsi_cdb_write_16 write16;
212 struct scsi_cdb_read_capacity_10 readcap10;
213 struct scsi_cdb_read_capacity_16 readcap16;
214 struct scsi_cdb_test_unit_ready testready;
215 unsigned char bytes[16];
218 /** printf() format for dumping a scsi_cdb */
219 #define SCSI_CDB_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
220 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
222 /** printf() parameters for dumping a scsi_cdb */
223 #define SCSI_CDB_DATA(cdb) \
224 (cdb).bytes[0], (cdb).bytes[1], (cdb).bytes[2], (cdb).bytes[3], \
225 (cdb).bytes[4], (cdb).bytes[5], (cdb).bytes[6], (cdb).bytes[7], \
226 (cdb).bytes[8], (cdb).bytes[9], (cdb).bytes[10], (cdb).bytes[11], \
227 (cdb).bytes[12], (cdb).bytes[13], (cdb).bytes[14], (cdb).bytes[15]
233 * This is a four-level LUN as specified by SAM-2, in big-endian
238 } __attribute__ (( packed ));
240 /** printf() format for dumping a scsi_lun */
241 #define SCSI_LUN_FORMAT "%04x-%04x-%04x-%04x"
243 /** printf() parameters for dumping a scsi_lun */
244 #define SCSI_LUN_DATA(lun) \
245 ntohs ( (lun).u16[0] ), ntohs ( (lun).u16[1] ), \
246 ntohs ( (lun).u16[2] ), ntohs ( (lun).u16[3] )
248 /** A SCSI command information unit */
252 /** CDB for this command */
254 /** Data-out buffer (may be NULL) */
256 /** Data-out buffer length
258 * Must be zero if @c data_out is NULL
261 /** Data-in buffer (may be NULL) */
263 /** Data-in buffer length
265 * Must be zero if @c data_in is NULL
270 /** SCSI fixed-format sense data */
271 struct scsi_sns_fixed {
280 /** Additional sense length */
282 /** Command-specific information */
284 /** Additional sense code and qualifier */
286 } __attribute__ (( packed ));
288 /** SCSI descriptor-format sense data */
289 struct scsi_sns_descriptor {
294 /** Additional sense code and qualifier */
296 } __attribute__ (( packed ));
298 /** SCSI sense data */
302 /** Fixed-format sense data */
303 struct scsi_sns_fixed fixed;
304 /** Descriptor-format sense data */
305 struct scsi_sns_descriptor desc;
308 /** SCSI sense response code mask */
309 #define SCSI_SENSE_CODE_MASK 0x7f
311 /** Test if SCSI sense data is in fixed format
313 * @v code Response code
314 * @ret is_fixed Sense data is in fixed format
316 #define SCSI_SENSE_FIXED( code ) ( ( (code) & 0x7e ) == 0x70 )
318 /** SCSI sense key mask */
319 #define SCSI_SENSE_KEY_MASK 0x0f
321 /** A SCSI response information unit */
323 /** SCSI status code */
325 /** Data overrun (or negative underrun) */
327 /** Autosense data (if any)
329 * To minimise code size, this is stored as the first four
330 * bytes of a descriptor-format sense data block (even if the
331 * response code indicates fixed-format sense data).
333 struct scsi_sns_descriptor sense;
336 extern int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun );
337 extern void scsi_parse_sense ( const void *data, size_t len,
338 struct scsi_sns_descriptor *sense );
340 extern int scsi_command ( struct interface *control, struct interface *data,
341 struct scsi_cmd *command );
342 #define scsi_command_TYPE( object_type ) \
343 typeof ( int ( object_type, struct interface *data, \
344 struct scsi_cmd *command ) )
346 extern void scsi_response ( struct interface *intf, struct scsi_rsp *response );
347 #define scsi_response_TYPE( object_type ) \
348 typeof ( void ( object_type, struct scsi_rsp *response ) )
350 extern int scsi_open ( struct interface *block, struct interface *scsi,
351 struct scsi_lun *lun );
353 #endif /* _IPXE_SCSI_H */