2 * SCSI definitions for s390 machine loader for qemu
4 * Copyright 2015 IBM Corp.
5 * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or (at
8 * your option) any later version. See the COPYING file in the top-level
17 #define SCSI_DEFAULT_CDB_SIZE 32
18 #define SCSI_DEFAULT_SENSE_SIZE 96
20 #define CDB_STATUS_GOOD 0
21 #define CDB_STATUS_CHECK_CONDITION 0x02U
22 #define CDB_STATUS_VALID(status) (((status) & ~0x3eU) == 0)
24 #define SCSI_SENSE_CODE_MASK 0x7fU
25 #define SCSI_SENSE_KEY_MASK 0x0fU
26 #define SCSI_SENSE_KEY_NO_SENSE 0
27 #define SCSI_SENSE_KEY_UNIT_ATTENTION 6
29 /* SCSI Inquiry Types */
30 #define SCSI_INQUIRY_STANDARD 0x00U
32 /* SCSI Inquiry Pages */
33 #define SCSI_INQUIRY_STANDARD_NONE 0x00U
36 uint64_t v64
; /* numeric shortcut */
37 uint8_t v8
[8]; /* generic 8 bytes representation */
38 uint16_t v16
[4]; /* 4-level big-endian LUN as specified by SAM-2 */
40 typedef union ScsiLun ScsiLun
;
43 uint8_t b0
; /* b0 & 7f = resp code (0x70 or 0x71) */
44 uint8_t b1
, b2
; /* b2 & 0f = sense key */
45 uint8_t u1
[1 * 4 + 1 + 1 * 4]; /* b7 = N - 7 */
46 uint8_t additional_sense_code
; /* b12 */
47 uint8_t additional_sense_code_qualifier
; /* b13 */
48 uint8_t u2
[1 + 3 + 0]; /* up to N (<=252) bytes */
49 } __attribute__((packed
));
50 typedef struct ScsiSense70 ScsiSense70
;
52 /* don't confuse with virtio-scsi response/status fields! */
54 static inline uint8_t scsi_sense_response(const void *p
)
56 return ((const ScsiSense70
*)p
)->b0
& SCSI_SENSE_CODE_MASK
;
59 static inline uint8_t scsi_sense_key(const void *p
)
61 return ((const ScsiSense70
*)p
)->b2
& SCSI_SENSE_KEY_MASK
;
64 #define SCSI_INQ_RDT_CDROM 0x05
66 struct ScsiInquiryStd
{
67 uint8_t peripheral_qdt
; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT */
68 uint8_t b1
; /* Removable Media Bit = b1 & 0x80 */
69 uint8_t spc_version
; /* b2 */
70 uint8_t b3
; /* b3 & 0x0f == resp_data_fmt == 2, must! */
71 uint8_t u1
[1 + 1 + 1 + 1 + 8]; /* b4..b15 unused, b4 = (N - 1) */
72 char prod_id
[16]; /* "QEMU CD-ROM" is here */
73 uint8_t u2
[4 /* b32..b35 unused, mandatory */
74 + 8 + 12 + 1 + 1 + 8 * 2 + 22 /* b36..95 unused, optional*/
75 + 0]; /* b96..bN unused, vendor specific */
77 } __attribute__((packed
));
78 typedef struct ScsiInquiryStd ScsiInquiryStd
;
80 struct ScsiCdbInquiry
{
81 uint8_t command
; /* b0, == 0x12 */
82 uint8_t b1
; /* b1, |= 0x01 (evpd) */
83 uint8_t b2
; /* b2; if evpd==1 */
84 uint16_t alloc_len
; /* b3, b4 */
85 uint8_t control
; /* b5 */
86 } __attribute__((packed
));
87 typedef struct ScsiCdbInquiry ScsiCdbInquiry
;
89 struct ScsiCdbRead10
{
90 uint8_t command
; /* =0x28 */
96 } __attribute__((packed
));
97 typedef struct ScsiCdbRead10 ScsiCdbRead10
;
99 struct ScsiCdbTestUnitReady
{
100 uint8_t command
; /* =0x00 */
103 } __attribute__((packed
));
104 typedef struct ScsiCdbTestUnitReady ScsiCdbTestUnitReady
;
106 struct ScsiCdbReportLuns
{
107 uint8_t command
; /* =0xa0 */
109 uint8_t select_report
; /* =0x02, "all" */
114 } __attribute__((packed
));
115 typedef struct ScsiCdbReportLuns ScsiCdbReportLuns
;
117 struct ScsiLunReport
{
118 uint32_t lun_list_len
;
120 ScsiLun lun
[1]; /* space for at least 1 lun must be allocated */
121 } __attribute__((packed
));
122 typedef struct ScsiLunReport ScsiLunReport
;
124 struct ScsiCdbReadCapacity16
{
125 uint8_t command
; /* =0x9e = "service action in 16" */
126 uint8_t service_action
; /* 5 bits, =0x10 = "read capacity 16" */
131 } __attribute__((packed
));
132 typedef struct ScsiCdbReadCapacity16 ScsiCdbReadCapacity16
;
134 struct ScsiReadCapacity16Data
{
135 uint64_t ret_lba
; /* get it, 0..7 */
136 uint32_t lb_len
; /* bytes, 8..11 */
137 uint8_t u1
[2 + 1 * 2 + 16]; /* b12..b31, unused */
138 } __attribute__((packed
));
139 typedef struct ScsiReadCapacity16Data ScsiReadCapacity16Data
;
141 static inline ScsiLun
make_lun(uint16_t channel
, uint16_t target
, uint32_t lun
)
143 ScsiLun r
= { .v64
= 0 };
145 /* See QEMU code to choose the way to handle LUNs.
147 * So, a valid LUN must have (always channel #0):
149 * lun[1] - target, any value
150 * lun[2] == 0 or (LUN, MSB, 0x40 set, 0x80 clear)
151 * lun[3] - LUN, LSB, any value
154 r
.v8
[1] = target
& 0xffU
;
155 r
.v8
[2] = (lun
>> 8) & 0x3fU
;
159 r
.v8
[3] = lun
& 0xffU
;
164 static inline const char *scsi_cdb_status_msg(uint8_t status
)
166 static char err_msg
[] = "STATUS=XX";
167 uint8_t v
= status
& 0x3eU
;
169 fill_hex_val(err_msg
+ 7, &v
, 1);
173 static inline const char *scsi_cdb_asc_msg(const void *s
)
175 static char err_msg
[] = "RSPN=XX KEY=XX CODE=XX QLFR=XX";
176 const ScsiSense70
*p
= s
;
177 uint8_t sr
= scsi_sense_response(s
);
178 uint8_t sk
= scsi_sense_key(s
);
179 uint8_t ac
= p
->additional_sense_code
;
180 uint8_t cq
= p
->additional_sense_code_qualifier
;
182 fill_hex_val(err_msg
+ 5, &sr
, 1);
183 fill_hex_val(err_msg
+ 12, &sk
, 1);
184 fill_hex_val(err_msg
+ 20, &ac
, 1);
185 fill_hex_val(err_msg
+ 28, &cq
, 1);