1
2
3
4
5
6
7
8
9
10
11
12#ifndef SCSI_H
13#define SCSI_H
14
15#include "s390-ccw.h"
16
17#define SCSI_DEFAULT_CDB_SIZE 32
18#define SCSI_DEFAULT_SENSE_SIZE 96
19
20#define CDB_STATUS_GOOD 0
21#define CDB_STATUS_CHECK_CONDITION 0x02U
22#define CDB_STATUS_VALID(status) (((status) & ~0x3eU) == 0)
23
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
28
29
30#define SCSI_INQUIRY_STANDARD 0x00U
31#define SCSI_INQUIRY_EVPD 0x01U
32
33
34#define SCSI_INQUIRY_STANDARD_NONE 0x00U
35#define SCSI_INQUIRY_EVPD_SUPPORTED_PAGES 0x00U
36#define SCSI_INQUIRY_EVPD_BLOCK_LIMITS 0xb0U
37
38union ScsiLun {
39 uint64_t v64;
40 uint8_t v8[8];
41 uint16_t v16[4];
42};
43typedef union ScsiLun ScsiLun;
44
45struct ScsiSense70 {
46 uint8_t b0;
47 uint8_t b1, b2;
48 uint8_t u1[1 * 4 + 1 + 1 * 4];
49 uint8_t additional_sense_code;
50 uint8_t additional_sense_code_qualifier;
51 uint8_t u2[1 + 3 + 0];
52} __attribute__((packed));
53typedef struct ScsiSense70 ScsiSense70;
54
55
56
57static inline uint8_t scsi_sense_response(const void *p)
58{
59 return ((const ScsiSense70 *)p)->b0 & SCSI_SENSE_CODE_MASK;
60}
61
62static inline uint8_t scsi_sense_key(const void *p)
63{
64 return ((const ScsiSense70 *)p)->b2 & SCSI_SENSE_KEY_MASK;
65}
66
67#define SCSI_INQ_RDT_CDROM 0x05
68
69struct ScsiInquiryStd {
70 uint8_t peripheral_qdt;
71 uint8_t b1;
72 uint8_t spc_version;
73 uint8_t b3;
74 uint8_t u1[1 + 1 + 1 + 1 + 8];
75 char prod_id[16];
76 uint8_t u2[4
77 + 8 + 12 + 1 + 1 + 8 * 2 + 22
78 + 0];
79
80} __attribute__((packed));
81typedef struct ScsiInquiryStd ScsiInquiryStd;
82
83struct ScsiInquiryEvpdPages {
84 uint8_t peripheral_qdt;
85 uint8_t page_code;
86 uint16_t page_length;
87 uint8_t byte[28];
88} __attribute__((packed));
89typedef struct ScsiInquiryEvpdPages ScsiInquiryEvpdPages;
90
91struct ScsiInquiryEvpdBl {
92 uint8_t peripheral_qdt;
93 uint8_t page_code;
94 uint16_t page_length;
95 uint8_t b4;
96 uint8_t b5;
97 uint16_t b6;
98 uint32_t max_transfer;
99 uint32_t b12[7];
100 uint32_t b44[5];
101} __attribute__((packed));
102typedef struct ScsiInquiryEvpdBl ScsiInquiryEvpdBl;
103
104struct ScsiCdbInquiry {
105 uint8_t command;
106 uint8_t b1;
107 uint8_t b2;
108 uint16_t alloc_len;
109 uint8_t control;
110} __attribute__((packed));
111typedef struct ScsiCdbInquiry ScsiCdbInquiry;
112
113struct ScsiCdbRead10 {
114 uint8_t command;
115 uint8_t b1;
116 uint32_t lba;
117 uint8_t b6;
118 uint16_t xfer_length;
119 uint8_t control;
120} __attribute__((packed));
121typedef struct ScsiCdbRead10 ScsiCdbRead10;
122
123struct ScsiCdbTestUnitReady {
124 uint8_t command;
125 uint8_t b1_b4[4];
126 uint8_t control;
127} __attribute__((packed));
128typedef struct ScsiCdbTestUnitReady ScsiCdbTestUnitReady;
129
130struct ScsiCdbReportLuns {
131 uint8_t command;
132 uint8_t b1;
133 uint8_t select_report;
134 uint8_t b3_b5[3];
135 uint32_t alloc_len;
136 uint8_t b10;
137 uint8_t control;
138} __attribute__((packed));
139typedef struct ScsiCdbReportLuns ScsiCdbReportLuns;
140
141struct ScsiLunReport {
142 uint32_t lun_list_len;
143 uint32_t b4_b7;
144 ScsiLun lun[1];
145} __attribute__((packed));
146typedef struct ScsiLunReport ScsiLunReport;
147
148struct ScsiCdbReadCapacity16 {
149 uint8_t command;
150 uint8_t service_action;
151 uint64_t b2_b9;
152 uint32_t alloc_len;
153 uint8_t b14;
154 uint8_t control;
155} __attribute__((packed));
156typedef struct ScsiCdbReadCapacity16 ScsiCdbReadCapacity16;
157
158struct ScsiReadCapacity16Data {
159 uint64_t ret_lba;
160 uint32_t lb_len;
161 uint8_t u1[2 + 1 * 2 + 16];
162} __attribute__((packed));
163typedef struct ScsiReadCapacity16Data ScsiReadCapacity16Data;
164
165static inline ScsiLun make_lun(uint16_t channel, uint16_t target, uint32_t lun)
166{
167 ScsiLun r = { .v64 = 0 };
168
169
170
171
172
173
174
175
176
177 r.v8[0] = 1;
178 r.v8[1] = target & 0xffU;
179 r.v8[2] = (lun >> 8) & 0x3fU;
180 if (r.v8[2]) {
181 r.v8[2] |= 0x40;
182 }
183 r.v8[3] = lun & 0xffU;
184
185 return r;
186}
187
188static inline const char *scsi_cdb_status_msg(uint8_t status)
189{
190 static char err_msg[] = "STATUS=XX";
191 uint8_t v = status & 0x3eU;
192
193 fill_hex_val(err_msg + 7, &v, 1);
194 return err_msg;
195}
196
197static inline const char *scsi_cdb_asc_msg(const void *s)
198{
199 static char err_msg[] = "RSPN=XX KEY=XX CODE=XX QLFR=XX";
200 const ScsiSense70 *p = s;
201 uint8_t sr = scsi_sense_response(s);
202 uint8_t sk = scsi_sense_key(s);
203 uint8_t ac = p->additional_sense_code;
204 uint8_t cq = p->additional_sense_code_qualifier;
205
206 fill_hex_val(err_msg + 5, &sr, 1);
207 fill_hex_val(err_msg + 12, &sk, 1);
208 fill_hex_val(err_msg + 20, &ac, 1);
209 fill_hex_val(err_msg + 28, &cq, 1);
210
211 return err_msg;
212}
213
214#endif
215