1f7915614SEugene (jno) Dvurechenski /*
2f7915614SEugene (jno) Dvurechenski * SCSI definitions for s390 machine loader for qemu
3f7915614SEugene (jno) Dvurechenski *
4f7915614SEugene (jno) Dvurechenski * Copyright 2015 IBM Corp.
5f7915614SEugene (jno) Dvurechenski * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com>
6f7915614SEugene (jno) Dvurechenski *
7f7915614SEugene (jno) Dvurechenski * This work is licensed under the terms of the GNU GPL, version 2 or (at
8f7915614SEugene (jno) Dvurechenski * your option) any later version. See the COPYING file in the top-level
9f7915614SEugene (jno) Dvurechenski * directory.
10f7915614SEugene (jno) Dvurechenski */
11f7915614SEugene (jno) Dvurechenski
12f7915614SEugene (jno) Dvurechenski #ifndef SCSI_H
13f7915614SEugene (jno) Dvurechenski #define SCSI_H
14f7915614SEugene (jno) Dvurechenski
15f7915614SEugene (jno) Dvurechenski #include "s390-ccw.h"
16f7915614SEugene (jno) Dvurechenski
17f7915614SEugene (jno) Dvurechenski #define SCSI_DEFAULT_CDB_SIZE 32
18f7915614SEugene (jno) Dvurechenski #define SCSI_DEFAULT_SENSE_SIZE 96
19f7915614SEugene (jno) Dvurechenski
20f7915614SEugene (jno) Dvurechenski #define CDB_STATUS_GOOD 0
21f7915614SEugene (jno) Dvurechenski #define CDB_STATUS_CHECK_CONDITION 0x02U
22f7915614SEugene (jno) Dvurechenski #define CDB_STATUS_VALID(status) (((status) & ~0x3eU) == 0)
23f7915614SEugene (jno) Dvurechenski
24f7915614SEugene (jno) Dvurechenski #define SCSI_SENSE_CODE_MASK 0x7fU
25f7915614SEugene (jno) Dvurechenski #define SCSI_SENSE_KEY_MASK 0x0fU
26f7915614SEugene (jno) Dvurechenski #define SCSI_SENSE_KEY_NO_SENSE 0
27f7915614SEugene (jno) Dvurechenski #define SCSI_SENSE_KEY_UNIT_ATTENTION 6
28f7915614SEugene (jno) Dvurechenski
299c12359cSEric Farman /* SCSI Inquiry Types */
309c12359cSEric Farman #define SCSI_INQUIRY_STANDARD 0x00U
318edfe85bSEric Farman #define SCSI_INQUIRY_EVPD 0x01U
329c12359cSEric Farman
339c12359cSEric Farman /* SCSI Inquiry Pages */
349c12359cSEric Farman #define SCSI_INQUIRY_STANDARD_NONE 0x00U
358edfe85bSEric Farman #define SCSI_INQUIRY_EVPD_SUPPORTED_PAGES 0x00U
36*fe921fc8SEric Farman #define SCSI_INQUIRY_EVPD_BLOCK_LIMITS 0xb0U
379c12359cSEric Farman
38f7915614SEugene (jno) Dvurechenski union ScsiLun {
39f7915614SEugene (jno) Dvurechenski uint64_t v64; /* numeric shortcut */
40f7915614SEugene (jno) Dvurechenski uint8_t v8[8]; /* generic 8 bytes representation */
41f7915614SEugene (jno) Dvurechenski uint16_t v16[4]; /* 4-level big-endian LUN as specified by SAM-2 */
42f7915614SEugene (jno) Dvurechenski };
43f7915614SEugene (jno) Dvurechenski typedef union ScsiLun ScsiLun;
44f7915614SEugene (jno) Dvurechenski
45f7915614SEugene (jno) Dvurechenski struct ScsiSense70 {
46f7915614SEugene (jno) Dvurechenski uint8_t b0; /* b0 & 7f = resp code (0x70 or 0x71) */
47f7915614SEugene (jno) Dvurechenski uint8_t b1, b2; /* b2 & 0f = sense key */
48f7915614SEugene (jno) Dvurechenski uint8_t u1[1 * 4 + 1 + 1 * 4]; /* b7 = N - 7 */
49f7915614SEugene (jno) Dvurechenski uint8_t additional_sense_code; /* b12 */
50f7915614SEugene (jno) Dvurechenski uint8_t additional_sense_code_qualifier; /* b13 */
51f7915614SEugene (jno) Dvurechenski uint8_t u2[1 + 3 + 0]; /* up to N (<=252) bytes */
52f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
53f7915614SEugene (jno) Dvurechenski typedef struct ScsiSense70 ScsiSense70;
54f7915614SEugene (jno) Dvurechenski
55f7915614SEugene (jno) Dvurechenski /* don't confuse with virtio-scsi response/status fields! */
56f7915614SEugene (jno) Dvurechenski
scsi_sense_response(const void * p)57f7915614SEugene (jno) Dvurechenski static inline uint8_t scsi_sense_response(const void *p)
58f7915614SEugene (jno) Dvurechenski {
59f7915614SEugene (jno) Dvurechenski return ((const ScsiSense70 *)p)->b0 & SCSI_SENSE_CODE_MASK;
60f7915614SEugene (jno) Dvurechenski }
61f7915614SEugene (jno) Dvurechenski
scsi_sense_key(const void * p)62f7915614SEugene (jno) Dvurechenski static inline uint8_t scsi_sense_key(const void *p)
63f7915614SEugene (jno) Dvurechenski {
64f7915614SEugene (jno) Dvurechenski return ((const ScsiSense70 *)p)->b2 & SCSI_SENSE_KEY_MASK;
65f7915614SEugene (jno) Dvurechenski }
66f7915614SEugene (jno) Dvurechenski
67f7915614SEugene (jno) Dvurechenski #define SCSI_INQ_RDT_CDROM 0x05
68f7915614SEugene (jno) Dvurechenski
69f7915614SEugene (jno) Dvurechenski struct ScsiInquiryStd {
70f7915614SEugene (jno) Dvurechenski uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT */
71f7915614SEugene (jno) Dvurechenski uint8_t b1; /* Removable Media Bit = b1 & 0x80 */
72f7915614SEugene (jno) Dvurechenski uint8_t spc_version; /* b2 */
73f7915614SEugene (jno) Dvurechenski uint8_t b3; /* b3 & 0x0f == resp_data_fmt == 2, must! */
74f7915614SEugene (jno) Dvurechenski uint8_t u1[1 + 1 + 1 + 1 + 8]; /* b4..b15 unused, b4 = (N - 1) */
75f7915614SEugene (jno) Dvurechenski char prod_id[16]; /* "QEMU CD-ROM" is here */
76f7915614SEugene (jno) Dvurechenski uint8_t u2[4 /* b32..b35 unused, mandatory */
77f7915614SEugene (jno) Dvurechenski + 8 + 12 + 1 + 1 + 8 * 2 + 22 /* b36..95 unused, optional*/
78f7915614SEugene (jno) Dvurechenski + 0]; /* b96..bN unused, vendor specific */
79f7915614SEugene (jno) Dvurechenski /* byte N */
80f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
81f7915614SEugene (jno) Dvurechenski typedef struct ScsiInquiryStd ScsiInquiryStd;
82f7915614SEugene (jno) Dvurechenski
838edfe85bSEric Farman struct ScsiInquiryEvpdPages {
848edfe85bSEric Farman uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT */
858edfe85bSEric Farman uint8_t page_code; /* b1 */
868edfe85bSEric Farman uint16_t page_length; /* b2..b3 length = N-3 */
878edfe85bSEric Farman uint8_t byte[28]; /* b4..bN Supported EVPD pages (N=31 here) */
888edfe85bSEric Farman } __attribute__((packed));
898edfe85bSEric Farman typedef struct ScsiInquiryEvpdPages ScsiInquiryEvpdPages;
908edfe85bSEric Farman
91*fe921fc8SEric Farman struct ScsiInquiryEvpdBl {
92*fe921fc8SEric Farman uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT */
93*fe921fc8SEric Farman uint8_t page_code;
94*fe921fc8SEric Farman uint16_t page_length;
95*fe921fc8SEric Farman uint8_t b4;
96*fe921fc8SEric Farman uint8_t b5;
97*fe921fc8SEric Farman uint16_t b6;
98*fe921fc8SEric Farman uint32_t max_transfer; /* b8 */
99*fe921fc8SEric Farman uint32_t b12[7]; /* b12..b43 (defined fields) */
100*fe921fc8SEric Farman uint32_t b44[5]; /* b44..b63 (reserved fields) */
101*fe921fc8SEric Farman } __attribute__((packed));
102*fe921fc8SEric Farman typedef struct ScsiInquiryEvpdBl ScsiInquiryEvpdBl;
103*fe921fc8SEric Farman
104f7915614SEugene (jno) Dvurechenski struct ScsiCdbInquiry {
105f7915614SEugene (jno) Dvurechenski uint8_t command; /* b0, == 0x12 */
106f7915614SEugene (jno) Dvurechenski uint8_t b1; /* b1, |= 0x01 (evpd) */
107f7915614SEugene (jno) Dvurechenski uint8_t b2; /* b2; if evpd==1 */
108f7915614SEugene (jno) Dvurechenski uint16_t alloc_len; /* b3, b4 */
109f7915614SEugene (jno) Dvurechenski uint8_t control; /* b5 */
110f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
111f7915614SEugene (jno) Dvurechenski typedef struct ScsiCdbInquiry ScsiCdbInquiry;
112f7915614SEugene (jno) Dvurechenski
113f7915614SEugene (jno) Dvurechenski struct ScsiCdbRead10 {
114f7915614SEugene (jno) Dvurechenski uint8_t command; /* =0x28 */
115f7915614SEugene (jno) Dvurechenski uint8_t b1;
116f7915614SEugene (jno) Dvurechenski uint32_t lba;
117f7915614SEugene (jno) Dvurechenski uint8_t b6;
118f7915614SEugene (jno) Dvurechenski uint16_t xfer_length;
119f7915614SEugene (jno) Dvurechenski uint8_t control;
120f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
121f7915614SEugene (jno) Dvurechenski typedef struct ScsiCdbRead10 ScsiCdbRead10;
122f7915614SEugene (jno) Dvurechenski
123f7915614SEugene (jno) Dvurechenski struct ScsiCdbTestUnitReady {
124f7915614SEugene (jno) Dvurechenski uint8_t command; /* =0x00 */
125f7915614SEugene (jno) Dvurechenski uint8_t b1_b4[4];
126f7915614SEugene (jno) Dvurechenski uint8_t control;
127f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
128f7915614SEugene (jno) Dvurechenski typedef struct ScsiCdbTestUnitReady ScsiCdbTestUnitReady;
129f7915614SEugene (jno) Dvurechenski
130f7915614SEugene (jno) Dvurechenski struct ScsiCdbReportLuns {
131f7915614SEugene (jno) Dvurechenski uint8_t command; /* =0xa0 */
132f7915614SEugene (jno) Dvurechenski uint8_t b1;
133f7915614SEugene (jno) Dvurechenski uint8_t select_report; /* =0x02, "all" */
134f7915614SEugene (jno) Dvurechenski uint8_t b3_b5[3];
135f7915614SEugene (jno) Dvurechenski uint32_t alloc_len;
136f7915614SEugene (jno) Dvurechenski uint8_t b10;
137f7915614SEugene (jno) Dvurechenski uint8_t control;
138f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
139f7915614SEugene (jno) Dvurechenski typedef struct ScsiCdbReportLuns ScsiCdbReportLuns;
140f7915614SEugene (jno) Dvurechenski
141f7915614SEugene (jno) Dvurechenski struct ScsiLunReport {
142f7915614SEugene (jno) Dvurechenski uint32_t lun_list_len;
143f7915614SEugene (jno) Dvurechenski uint32_t b4_b7;
144f7915614SEugene (jno) Dvurechenski ScsiLun lun[1]; /* space for at least 1 lun must be allocated */
145f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
146f7915614SEugene (jno) Dvurechenski typedef struct ScsiLunReport ScsiLunReport;
147f7915614SEugene (jno) Dvurechenski
148f7915614SEugene (jno) Dvurechenski struct ScsiCdbReadCapacity16 {
149f7915614SEugene (jno) Dvurechenski uint8_t command; /* =0x9e = "service action in 16" */
150f7915614SEugene (jno) Dvurechenski uint8_t service_action; /* 5 bits, =0x10 = "read capacity 16" */
151f7915614SEugene (jno) Dvurechenski uint64_t b2_b9;
152f7915614SEugene (jno) Dvurechenski uint32_t alloc_len;
153f7915614SEugene (jno) Dvurechenski uint8_t b14;
154f7915614SEugene (jno) Dvurechenski uint8_t control;
155f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
156f7915614SEugene (jno) Dvurechenski typedef struct ScsiCdbReadCapacity16 ScsiCdbReadCapacity16;
157f7915614SEugene (jno) Dvurechenski
158f7915614SEugene (jno) Dvurechenski struct ScsiReadCapacity16Data {
159f7915614SEugene (jno) Dvurechenski uint64_t ret_lba; /* get it, 0..7 */
160f7915614SEugene (jno) Dvurechenski uint32_t lb_len; /* bytes, 8..11 */
161f7915614SEugene (jno) Dvurechenski uint8_t u1[2 + 1 * 2 + 16]; /* b12..b31, unused */
162f7915614SEugene (jno) Dvurechenski } __attribute__((packed));
163f7915614SEugene (jno) Dvurechenski typedef struct ScsiReadCapacity16Data ScsiReadCapacity16Data;
164f7915614SEugene (jno) Dvurechenski
make_lun(uint16_t channel,uint16_t target,uint32_t lun)165f7915614SEugene (jno) Dvurechenski static inline ScsiLun make_lun(uint16_t channel, uint16_t target, uint32_t lun)
166f7915614SEugene (jno) Dvurechenski {
167f7915614SEugene (jno) Dvurechenski ScsiLun r = { .v64 = 0 };
168f7915614SEugene (jno) Dvurechenski
169f7915614SEugene (jno) Dvurechenski /* See QEMU code to choose the way to handle LUNs.
170f7915614SEugene (jno) Dvurechenski *
171f7915614SEugene (jno) Dvurechenski * So, a valid LUN must have (always channel #0):
172f7915614SEugene (jno) Dvurechenski * lun[0] == 1
173f7915614SEugene (jno) Dvurechenski * lun[1] - target, any value
174f7915614SEugene (jno) Dvurechenski * lun[2] == 0 or (LUN, MSB, 0x40 set, 0x80 clear)
175f7915614SEugene (jno) Dvurechenski * lun[3] - LUN, LSB, any value
176f7915614SEugene (jno) Dvurechenski */
177f7915614SEugene (jno) Dvurechenski r.v8[0] = 1;
178f7915614SEugene (jno) Dvurechenski r.v8[1] = target & 0xffU;
179f7915614SEugene (jno) Dvurechenski r.v8[2] = (lun >> 8) & 0x3fU;
180f7915614SEugene (jno) Dvurechenski if (r.v8[2]) {
181f7915614SEugene (jno) Dvurechenski r.v8[2] |= 0x40;
182f7915614SEugene (jno) Dvurechenski }
183f7915614SEugene (jno) Dvurechenski r.v8[3] = lun & 0xffU;
184f7915614SEugene (jno) Dvurechenski
185f7915614SEugene (jno) Dvurechenski return r;
186f7915614SEugene (jno) Dvurechenski }
187f7915614SEugene (jno) Dvurechenski
scsi_cdb_status_msg(uint8_t status)188f7915614SEugene (jno) Dvurechenski static inline const char *scsi_cdb_status_msg(uint8_t status)
189f7915614SEugene (jno) Dvurechenski {
190f7915614SEugene (jno) Dvurechenski static char err_msg[] = "STATUS=XX";
191f7915614SEugene (jno) Dvurechenski uint8_t v = status & 0x3eU;
192f7915614SEugene (jno) Dvurechenski
193f7915614SEugene (jno) Dvurechenski fill_hex_val(err_msg + 7, &v, 1);
194f7915614SEugene (jno) Dvurechenski return err_msg;
195f7915614SEugene (jno) Dvurechenski }
196f7915614SEugene (jno) Dvurechenski
scsi_cdb_asc_msg(const void * s)197f7915614SEugene (jno) Dvurechenski static inline const char *scsi_cdb_asc_msg(const void *s)
198f7915614SEugene (jno) Dvurechenski {
199f7915614SEugene (jno) Dvurechenski static char err_msg[] = "RSPN=XX KEY=XX CODE=XX QLFR=XX";
200f7915614SEugene (jno) Dvurechenski const ScsiSense70 *p = s;
201f7915614SEugene (jno) Dvurechenski uint8_t sr = scsi_sense_response(s);
202f7915614SEugene (jno) Dvurechenski uint8_t sk = scsi_sense_key(s);
203f7915614SEugene (jno) Dvurechenski uint8_t ac = p->additional_sense_code;
204f7915614SEugene (jno) Dvurechenski uint8_t cq = p->additional_sense_code_qualifier;
205f7915614SEugene (jno) Dvurechenski
206f7915614SEugene (jno) Dvurechenski fill_hex_val(err_msg + 5, &sr, 1);
207f7915614SEugene (jno) Dvurechenski fill_hex_val(err_msg + 12, &sk, 1);
208f7915614SEugene (jno) Dvurechenski fill_hex_val(err_msg + 20, &ac, 1);
209f7915614SEugene (jno) Dvurechenski fill_hex_val(err_msg + 28, &cq, 1);
210f7915614SEugene (jno) Dvurechenski
211f7915614SEugene (jno) Dvurechenski return err_msg;
212f7915614SEugene (jno) Dvurechenski }
213f7915614SEugene (jno) Dvurechenski
214f7915614SEugene (jno) Dvurechenski #endif /* SCSI_H */
215