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