iPXE
scsi.h
Go to the documentation of this file.
1 #ifndef _IPXE_SCSI_H
2 #define _IPXE_SCSI_H
3 
4 #include <stdint.h>
5 #include <ipxe/interface.h>
6 
7 /** @file
8  *
9  * SCSI devices
10  *
11  */
12 
13 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
14 
15 /** Maximum block for READ/WRITE (10) commands */
16 #define SCSI_MAX_BLOCK_10 0xffffffffULL
17 
18 /**
19  * @defgroup scsiops SCSI operation codes
20  * @{
21  */
22 
23 #define SCSI_OPCODE_READ_10 0x28 /**< READ (10) */
24 #define SCSI_OPCODE_READ_16 0x88 /**< READ (16) */
25 #define SCSI_OPCODE_WRITE_10 0x2a /**< WRITE (10) */
26 #define SCSI_OPCODE_WRITE_16 0x8a /**< WRITE (16) */
27 #define SCSI_OPCODE_READ_CAPACITY_10 0x25 /**< READ CAPACITY (10) */
28 #define SCSI_OPCODE_SERVICE_ACTION_IN 0x9e /**< SERVICE ACTION IN */
29 #define SCSI_SERVICE_ACTION_READ_CAPACITY_16 0x10 /**< READ CAPACITY (16) */
30 #define SCSI_OPCODE_TEST_UNIT_READY 0x00 /**< TEST UNIT READY */
31 
32 /** @} */
33 
34 /**
35  * @defgroup scsiflags SCSI flags
36  * @{
37  */
38 
39 #define SCSI_FL_FUA_NV 0x02 /**< Force unit access to NVS */
40 #define SCSI_FL_FUA 0x08 /**< Force unit access */
41 #define SCSI_FL_DPO 0x10 /**< Disable cache page out */
42 
43 /** @} */
44 
45 /**
46  * @defgroup scsicdbs SCSI command data blocks
47  * @{
48  */
49 
50 /** A SCSI "READ (10)" CDB */
52  /** Opcode (0x28) */
54  /** Flags */
56  /** Start address
57  *
58  * This is a logical block number, in big-endian order.
59  */
61  /** Group number */
63  /** Transfer length
64  *
65  * This is a logical block count, in big-endian order.
66  */
68  /** Control byte */
70 } __attribute__ (( packed ));
71 
72 /** A SCSI "READ (16)" CDB */
74  /** Opcode (0x88) */
76  /** Flags */
78  /** Start address
79  *
80  * This is a logical block number, in big-endian order.
81  */
83  /** Transfer length
84  *
85  * This is a logical block count, in big-endian order.
86  */
88  /** Group number */
90  /** Control byte */
92 } __attribute__ (( packed ));
93 
94 /** A SCSI "WRITE (10)" CDB */
96  /** Opcode (0x2a) */
98  /** Flags */
100  /** Start address
101  *
102  * This is a logical block number, in big-endian order.
103  */
105  /** Group number */
107  /** Transfer length
108  *
109  * This is a logical block count, in big-endian order.
110  */
112  /** Control byte */
114 } __attribute__ (( packed ));
115 
116 /** A SCSI "WRITE (16)" CDB */
118  /** Opcode (0x8a) */
120  /** Flags */
122  /** Start address
123  *
124  * This is a logical block number, in big-endian order.
125  */
127  /** Transfer length
128  *
129  * This is a logical block count, in big-endian order.
130  */
132  /** Group number */
134  /** Control byte */
136 } __attribute__ (( packed ));
137 
138 /** A SCSI "READ CAPACITY (10)" CDB */
140  /** Opcode (0x25) */
142  /** Reserved */
144  /** Logical block address
145  *
146  * Applicable only if the PMI bit is set.
147  */
149  /** Reserved */
151  /** Control byte */
153 } __attribute__ (( packed ));
154 
155 /** SCSI "READ CAPACITY (10)" parameter data */
157  /** Maximum logical block number */
159  /** Block length in bytes */
161 } __attribute__ (( packed ));
162 
163 /** A SCSI "READ CAPACITY (16)" CDB */
165  /** Opcode (0x9e) */
167  /** Service action */
169  /** Logical block address
170  *
171  * Applicable only if the PMI bit is set.
172  */
174  /** Transfer length
175  *
176  * This is the size of the data-in buffer, in bytes.
177  */
179  /** Reserved */
181  /** Control byte */
183 } __attribute__ (( packed ));
184 
185 /** SCSI "READ CAPACITY (16)" parameter data */
187  /** Maximum logical block number */
189  /** Block length in bytes */
191  /** Reserved */
193 } __attribute__ (( packed ));
194 
195 /** A SCSI "TEST UNIT READY" CDB */
197  /** Opcode (0x00) */
199  /** Reserved */
201  /** Control byte */
203 } __attribute__ (( packed ));
204 
205 /** A SCSI Command Data Block */
206 union scsi_cdb {
214  unsigned char bytes[16];
215 };
216 
217 /** printf() format for dumping a scsi_cdb */
218 #define SCSI_CDB_FORMAT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" \
219  "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
220 
221 /** printf() parameters for dumping a scsi_cdb */
222 #define SCSI_CDB_DATA(cdb) \
223  (cdb).bytes[0], (cdb).bytes[1], (cdb).bytes[2], (cdb).bytes[3], \
224  (cdb).bytes[4], (cdb).bytes[5], (cdb).bytes[6], (cdb).bytes[7], \
225  (cdb).bytes[8], (cdb).bytes[9], (cdb).bytes[10], (cdb).bytes[11], \
226  (cdb).bytes[12], (cdb).bytes[13], (cdb).bytes[14], (cdb).bytes[15]
227 
228 /** @} */
229 
230 /** A SCSI LUN
231  *
232  * This is a four-level LUN as specified by SAM-2, in big-endian
233  * order.
234  */
235 struct scsi_lun {
237 } __attribute__ (( packed ));
238 
239 /** printf() format for dumping a scsi_lun */
240 #define SCSI_LUN_FORMAT "%04x-%04x-%04x-%04x"
241 
242 /** printf() parameters for dumping a scsi_lun */
243 #define SCSI_LUN_DATA(lun) \
244  ntohs ( (lun).u16[0] ), ntohs ( (lun).u16[1] ), \
245  ntohs ( (lun).u16[2] ), ntohs ( (lun).u16[3] )
246 
247 /** A SCSI command information unit */
248 struct scsi_cmd {
249  /** LUN */
250  struct scsi_lun lun;
251  /** CDB for this command */
252  union scsi_cdb cdb;
253  /** Data-out buffer (may be NULL) */
254  void *data_out;
255  /** Data-out buffer length
256  *
257  * Must be zero if @c data_out is NULL
258  */
259  size_t data_out_len;
260  /** Data-in buffer (may be NULL) */
261  void *data_in;
262  /** Data-in buffer length
263  *
264  * Must be zero if @c data_in is NULL
265  */
266  size_t data_in_len;
267 };
268 
269 /** SCSI fixed-format sense data */
271  /** Response code */
273  /** Reserved */
275  /** Sense key */
277  /** Information */
279  /** Additional sense length */
281  /** Command-specific information */
283  /** Additional sense code and qualifier */
285 } __attribute__ (( packed ));
286 
287 /** SCSI descriptor-format sense data */
289  /** Response code */
291  /** Sense key */
293  /** Additional sense code and qualifier */
295 } __attribute__ (( packed ));
296 
297 /** SCSI sense data */
298 union scsi_sns {
299  /** Response code */
301  /** Fixed-format sense data */
303  /** Descriptor-format sense data */
305 };
306 
307 /** SCSI sense response code mask */
308 #define SCSI_SENSE_CODE_MASK 0x7f
309 
310 /** Test if SCSI sense data is in fixed format
311  *
312  * @v code Response code
313  * @ret is_fixed Sense data is in fixed format
314  */
315 #define SCSI_SENSE_FIXED( code ) ( ( (code) & 0x7e ) == 0x70 )
316 
317 /** SCSI sense key mask */
318 #define SCSI_SENSE_KEY_MASK 0x0f
319 
320 /** A SCSI response information unit */
321 struct scsi_rsp {
322  /** SCSI status code */
324  /** Data overrun (or negative underrun) */
326  /** Autosense data (if any)
327  *
328  * To minimise code size, this is stored as the first four
329  * bytes of a descriptor-format sense data block (even if the
330  * response code indicates fixed-format sense data).
331  */
333 };
334 
335 extern int scsi_parse_lun ( const char *lun_string, struct scsi_lun *lun );
336 extern void scsi_parse_sense ( const void *data, size_t len,
337  struct scsi_sns_descriptor *sense );
338 
339 extern int scsi_command ( struct interface *control, struct interface *data,
340  struct scsi_cmd *command );
341 #define scsi_command_TYPE( object_type ) \
342  typeof ( int ( object_type, struct interface *data, \
343  struct scsi_cmd *command ) )
344 
345 extern void scsi_response ( struct interface *intf, struct scsi_rsp *response );
346 #define scsi_response_TYPE( object_type ) \
347  typeof ( void ( object_type, struct scsi_rsp *response ) )
348 
349 extern int scsi_open ( struct interface *block, struct interface *scsi,
350  struct scsi_lun *lun );
351 
352 #endif /* _IPXE_SCSI_H */
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint16_t u16
Definition: stdint.h:21
A SCSI "READ (10)" CDB.
Definition: scsi.h:51
unsigned short uint16_t
Definition: stdint.h:11
uint8_t reserved[20]
Reserved.
Definition: scsi.h:192
uint16_t len
Transfer length.
Definition: scsi.h:29
size_t data_out_len
Data-out buffer length.
Definition: scsi.h:259
void * data_in
Data-in buffer (may be NULL)
Definition: scsi.h:261
void scsi_response(struct interface *intf, struct scsi_rsp *response)
Report SCSI response.
Definition: scsi.c:206
uint8_t control
Control byte.
Definition: scsi.h:69
uint8_t control
Control byte.
Definition: scsi.h:91
int scsi_open(struct interface *block, struct interface *scsi, struct scsi_lun *lun)
Open SCSI device.
Definition: scsi.c:984
uint8_t flags
Flags.
Definition: scsi.h:55
uint8_t control
Control byte.
Definition: scsi.h:152
A command-line command.
Definition: command.h:9
uint32_t info
Information.
Definition: scsi.h:278
uint32_t len
Transfer length.
Definition: scsi.h:178
unsigned char bytes[16]
Definition: scsi.h:214
SCSI "READ CAPACITY (10)" parameter data.
Definition: scsi.h:156
size_t data_in_len
Data-in buffer length.
Definition: scsi.h:266
uint32_t lba
Start address.
Definition: scsi.h:60
unsigned long long uint64_t
Definition: stdint.h:13
uint8_t reserved
Reserved.
Definition: scsi.h:274
void scsi_parse_sense(const void *data, size_t len, struct scsi_sns_descriptor *sense)
Parse SCSI sense data.
Definition: scsi.c:146
SCSI sense data.
Definition: scsi.h:298
union scsi_cdb cdb
CDB for this command.
Definition: scsi.h:252
uint8_t group
Group number.
Definition: scsi.h:133
uint8_t opcode
Opcode (0x8a)
Definition: scsi.h:119
SCSI fixed-format sense data.
Definition: scsi.h:270
uint32_t blksize
Block length in bytes.
Definition: scsi.h:160
SCSI "READ CAPACITY (16)" parameter data.
Definition: scsi.h:186
SCSI descriptor-format sense data.
Definition: scsi.h:288
uint32_t blksize
Block length in bytes.
Definition: scsi.h:190
uint8_t key
Sense key.
Definition: scsi.h:276
uint8_t code
Response code.
Definition: scsi.h:290
A SCSI "TEST UNIT READY" CDB.
Definition: scsi.h:196
uint8_t reserved_b[3]
Reserved.
Definition: scsi.h:150
uint16_t additional
Additional sense code and qualifier.
Definition: scsi.h:284
uint32_t len
Transfer length.
Definition: scsi.h:131
An object interface.
Definition: interface.h:124
uint8_t flags
Flags.
Definition: scsi.h:77
A SCSI response information unit.
Definition: scsi.h:321
Object interfaces.
A SCSI "READ CAPACITY (10)" CDB.
Definition: scsi.h:139
struct scsi_cdb_test_unit_ready testready
Definition: scsi.h:213
struct scsi_lun lun
LUN.
Definition: scsi.h:250
uint8_t control
Control byte.
Definition: scsi.h:113
uint8_t lun
Logical Unit Number.
Definition: edd.h:32
uint8_t opcode
Opcode (0x25)
Definition: scsi.h:141
struct scsi_cdb_read_10 read10
Definition: scsi.h:207
uint8_t opcode
Opcode (0x2a)
Definition: scsi.h:97
uint8_t opcode
Opcode (0x88)
Definition: scsi.h:75
int scsi_parse_lun(const char *lun_string, struct scsi_lun *lun)
Parse SCSI LUN.
Definition: scsi.c:117
struct scsi_cdb_write_16 write16
Definition: scsi.h:210
uint8_t status
SCSI status code.
Definition: scsi.h:323
uint8_t reserved_a
Reserved.
Definition: scsi.h:143
struct scsi_sns_fixed fixed
Fixed-format sense data.
Definition: scsi.h:302
uint8_t code
Response code.
Definition: scsi.h:300
union scsi_cdb __attribute__
uint8_t control
Control byte.
Definition: scsi.h:182
uint32_t lba
Maximum logical block number.
Definition: scsi.h:158
unsigned char uint8_t
Definition: stdint.h:10
struct scsi_cdb_read_16 read16
Definition: scsi.h:208
struct scsi_cdb_read_capacity_16 readcap16
Definition: scsi.h:212
A SCSI "WRITE (10)" CDB.
Definition: scsi.h:95
uint32_t lba
Logical block address.
Definition: scsi.h:148
unsigned int uint32_t
Definition: stdint.h:12
A SCSI "WRITE (16)" CDB.
Definition: scsi.h:117
A SCSI Command Data Block.
Definition: scsi.h:206
struct scsi_cdb_read_capacity_10 readcap10
Definition: scsi.h:211
uint8_t control
Control byte.
Definition: scsi.h:202
uint64_t lba
Maximum logical block number.
Definition: scsi.h:188
uint64_t lba
Logical block address.
Definition: scsi.h:173
A SCSI command information unit.
Definition: scsi.h:248
A SCSI "READ (16)" CDB.
Definition: scsi.h:73
uint64_t lba
Start address.
Definition: scsi.h:82
struct scsi_sns_descriptor desc
Descriptor-format sense data.
Definition: scsi.h:304
uint8_t opcode
Opcode (0x28)
Definition: scsi.h:53
uint8_t block[3][8]
DES-encrypted blocks.
Definition: mschapv2.h:12
uint32_t len
Transfer length.
Definition: scsi.h:87
uint8_t reserved
Reserved.
Definition: scsi.h:180
uint64_t lba
Start address.
Definition: scsi.h:126
uint8_t group
Group number.
Definition: scsi.h:89
uint32_t lba
Start address.
Definition: scsi.h:104
uint8_t flags
Flags.
Definition: scsi.h:99
struct scsi_sns_descriptor sense
Autosense data (if any)
Definition: scsi.h:332
uint16_t len
Transfer length.
Definition: scsi.h:67
uint8_t opcode
Opcode (0x00)
Definition: scsi.h:198
uint8_t len
Additional sense length.
Definition: scsi.h:280
uint8_t control
Control byte.
Definition: scsi.h:135
uint8_t opcode
Opcode (0x9e)
Definition: scsi.h:166
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t cs_info
Command-specific information.
Definition: scsi.h:282
A SCSI LUN.
Definition: scsi.h:235
int scsi_command(struct interface *control, struct interface *data, struct scsi_cmd *command)
Issue SCSI command.
Definition: scsi.c:181
uint8_t group
Group number.
Definition: scsi.h:106
uint8_t reserved[4]
Reserved.
Definition: scsi.h:200
signed long ssize_t
Definition: stdint.h:7
uint8_t key
Sense key.
Definition: scsi.h:292
A SCSI "READ CAPACITY (16)" CDB.
Definition: scsi.h:164
uint8_t code
Response code.
Definition: scsi.h:272
uint8_t group
Group number.
Definition: scsi.h:62
uint8_t control
Control byte.
Definition: scsi.h:31
uint8_t service_action
Service action.
Definition: scsi.h:168
uint8_t flags
Flags.
Definition: scsi.h:121
void * data_out
Data-out buffer (may be NULL)
Definition: scsi.h:254
ssize_t overrun
Data overrun (or negative underrun)
Definition: scsi.h:325
uint16_t additional
Additional sense code and qualifier.
Definition: scsi.h:294
struct scsi_cdb_write_10 write10
Definition: scsi.h:209
uint16_t len
Transfer length.
Definition: scsi.h:111