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
29union ScsiLun {
30 uint64_t v64;
31 uint8_t v8[8];
32 uint16_t v16[4];
33};
34typedef union ScsiLun ScsiLun;
35
36struct ScsiSense70 {
37 uint8_t b0;
38 uint8_t b1, b2;
39 uint8_t u1[1 * 4 + 1 + 1 * 4];
40 uint8_t additional_sense_code;
41 uint8_t additional_sense_code_qualifier;
42 uint8_t u2[1 + 3 + 0];
43} __attribute__((packed));
44typedef struct ScsiSense70 ScsiSense70;
45
46
47
48static inline uint8_t scsi_sense_response(const void *p)
49{
50 return ((const ScsiSense70 *)p)->b0 & SCSI_SENSE_CODE_MASK;
51}
52
53static inline uint8_t scsi_sense_key(const void *p)
54{
55 return ((const ScsiSense70 *)p)->b2 & SCSI_SENSE_KEY_MASK;
56}
57
58#define SCSI_INQ_RDT_CDROM 0x05
59
60struct ScsiInquiryStd {
61 uint8_t peripheral_qdt;
62 uint8_t b1;
63 uint8_t spc_version;
64 uint8_t b3;
65 uint8_t u1[1 + 1 + 1 + 1 + 8];
66 char prod_id[16];
67 uint8_t u2[4
68 + 8 + 12 + 1 + 1 + 8 * 2 + 22
69 + 0];
70
71} __attribute__((packed));
72typedef struct ScsiInquiryStd ScsiInquiryStd;
73
74struct ScsiCdbInquiry {
75 uint8_t command;
76 uint8_t b1;
77 uint8_t b2;
78 uint16_t alloc_len;
79 uint8_t control;
80} __attribute__((packed));
81typedef struct ScsiCdbInquiry ScsiCdbInquiry;
82
83struct ScsiCdbRead10 {
84 uint8_t command;
85 uint8_t b1;
86 uint32_t lba;
87 uint8_t b6;
88 uint16_t xfer_length;
89 uint8_t control;
90} __attribute__((packed));
91typedef struct ScsiCdbRead10 ScsiCdbRead10;
92
93struct ScsiCdbTestUnitReady {
94 uint8_t command;
95 uint8_t b1_b4[4];
96 uint8_t control;
97} __attribute__((packed));
98typedef struct ScsiCdbTestUnitReady ScsiCdbTestUnitReady;
99
100struct ScsiCdbReportLuns {
101 uint8_t command;
102 uint8_t b1;
103 uint8_t select_report;
104 uint8_t b3_b5[3];
105 uint32_t alloc_len;
106 uint8_t b10;
107 uint8_t control;
108} __attribute__((packed));
109typedef struct ScsiCdbReportLuns ScsiCdbReportLuns;
110
111struct ScsiLunReport {
112 uint32_t lun_list_len;
113 uint32_t b4_b7;
114 ScsiLun lun[1];
115} __attribute__((packed));
116typedef struct ScsiLunReport ScsiLunReport;
117
118struct ScsiCdbReadCapacity16 {
119 uint8_t command;
120 uint8_t service_action;
121 uint64_t b2_b9;
122 uint32_t alloc_len;
123 uint8_t b14;
124 uint8_t control;
125} __attribute__((packed));
126typedef struct ScsiCdbReadCapacity16 ScsiCdbReadCapacity16;
127
128struct ScsiReadCapacity16Data {
129 uint64_t ret_lba;
130 uint32_t lb_len;
131 uint8_t u1[2 + 1 * 2 + 16];
132} __attribute__((packed));
133typedef struct ScsiReadCapacity16Data ScsiReadCapacity16Data;
134
135static inline ScsiLun make_lun(uint16_t channel, uint16_t target, uint32_t lun)
136{
137 ScsiLun r = { .v64 = 0 };
138
139
140
141
142
143
144
145
146
147 r.v8[0] = 1;
148 r.v8[1] = target & 0xffU;
149 r.v8[2] = (lun >> 8) & 0x3fU;
150 if (r.v8[2]) {
151 r.v8[2] |= 0x40;
152 }
153 r.v8[3] = lun & 0xffU;
154
155 return r;
156}
157
158static inline const char *scsi_cdb_status_msg(uint8_t status)
159{
160 static char err_msg[] = "STATUS=XX";
161 uint8_t v = status & 0x3eU;
162
163 fill_hex_val(err_msg + 7, &v, 1);
164 return err_msg;
165}
166
167static inline const char *scsi_cdb_asc_msg(const void *s)
168{
169 static char err_msg[] = "RSPN=XX KEY=XX CODE=XX QLFR=XX";
170 const ScsiSense70 *p = s;
171 uint8_t sr = scsi_sense_response(s);
172 uint8_t sk = scsi_sense_key(s);
173 uint8_t ac = p->additional_sense_code;
174 uint8_t cq = p->additional_sense_code_qualifier;
175
176 fill_hex_val(err_msg + 5, &sr, 1);
177 fill_hex_val(err_msg + 12, &sk, 1);
178 fill_hex_val(err_msg + 20, &ac, 1);
179 fill_hex_val(err_msg + 28, &cq, 1);
180
181 return err_msg;
182}
183
184#endif
185