summaryrefslogtreecommitdiffstats
path: root/pc-bios/s390-ccw/scsi.h
blob: fe3fd5ac053caf8beb0e76bee40cb2a925f13481 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * SCSI definitions for s390 machine loader for qemu
 *
 * Copyright 2015 IBM Corp.
 * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or (at
 * your option) any later version. See the COPYING file in the top-level
 * directory.
 */

#ifndef SCSI_H
#define SCSI_H

#include "s390-ccw.h"

#define SCSI_DEFAULT_CDB_SIZE                   32
#define SCSI_DEFAULT_SENSE_SIZE                 96

#define CDB_STATUS_GOOD                         0
#define CDB_STATUS_CHECK_CONDITION              0x02U
#define CDB_STATUS_VALID(status)    (((status) & ~0x3eU) == 0)

#define SCSI_SENSE_CODE_MASK                    0x7fU
#define SCSI_SENSE_KEY_MASK                     0x0fU
#define SCSI_SENSE_KEY_NO_SENSE                 0
#define SCSI_SENSE_KEY_UNIT_ATTENTION           6

/* SCSI Inquiry Types */
#define SCSI_INQUIRY_STANDARD                   0x00U
#define SCSI_INQUIRY_EVPD                       0x01U

/* SCSI Inquiry Pages */
#define SCSI_INQUIRY_STANDARD_NONE              0x00U
#define SCSI_INQUIRY_EVPD_SUPPORTED_PAGES       0x00U
#define SCSI_INQUIRY_EVPD_BLOCK_LIMITS          0xb0U

union ScsiLun {
    uint64_t v64;        /* numeric shortcut                             */
    uint8_t  v8[8];      /* generic 8 bytes representation               */
    uint16_t v16[4];     /* 4-level big-endian LUN as specified by SAM-2 */
};
typedef union ScsiLun ScsiLun;

struct ScsiSense70 {
    uint8_t b0;         /* b0 & 7f = resp code (0x70 or 0x71)   */
    uint8_t b1, b2;     /* b2 & 0f = sense key                  */
    uint8_t u1[1 * 4 + 1 + 1 * 4];   /* b7 = N - 7                  */
    uint8_t additional_sense_code;              /* b12          */
    uint8_t additional_sense_code_qualifier;    /* b13          */
    uint8_t u2[1 + 3 + 0];           /* up to N (<=252) bytes       */
} __attribute__((packed));
typedef struct ScsiSense70 ScsiSense70;

/* don't confuse with virtio-scsi response/status fields! */

static inline uint8_t scsi_sense_response(const void *p)
{
    return ((const ScsiSense70 *)p)->b0 & SCSI_SENSE_CODE_MASK;
}

static inline uint8_t scsi_sense_key(const void *p)
{
    return ((const ScsiSense70 *)p)->b2 & SCSI_SENSE_KEY_MASK;
}

#define SCSI_INQ_RDT_CDROM                      0x05

struct ScsiInquiryStd {
    uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT  */
    uint8_t b1;             /* Removable Media Bit = b1 & 0x80          */
    uint8_t spc_version;    /* b2                                       */
    uint8_t b3;             /* b3 & 0x0f == resp_data_fmt == 2, must!   */
    uint8_t u1[1 + 1 + 1 + 1 + 8];  /* b4..b15 unused, b4 = (N - 1)     */
    char prod_id[16];       /* "QEMU CD-ROM" is here                    */
    uint8_t u2[4            /* b32..b35 unused, mandatory               */
              + 8 + 12 + 1 + 1 + 8 * 2 + 22  /* b36..95 unused, optional*/
              + 0];          /* b96..bN unused, vendor specific          */
    /* byte N                                                           */
}  __attribute__((packed));
typedef struct ScsiInquiryStd ScsiInquiryStd;

struct ScsiInquiryEvpdPages {
    uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT  */
    uint8_t page_code;      /* b1                                       */
    uint16_t page_length;   /* b2..b3 length = N-3                      */
    uint8_t byte[28];       /* b4..bN Supported EVPD pages (N=31 here)  */
}  __attribute__((packed));
typedef struct ScsiInquiryEvpdPages ScsiInquiryEvpdPages;

struct ScsiInquiryEvpdBl {
    uint8_t peripheral_qdt; /* b0, use (b0 & 0x1f) to get SCSI_INQ_RDT  */
    uint8_t page_code;
    uint16_t page_length;
    uint8_t b4;
    uint8_t b5;
    uint16_t b6;
    uint32_t max_transfer;  /* b8                                       */
    uint32_t b12[7];        /* b12..b43 (defined fields)                */
    uint32_t b44[5];        /* b44..b63 (reserved fields)               */
}  __attribute__((packed));
typedef struct ScsiInquiryEvpdBl ScsiInquiryEvpdBl;

struct ScsiCdbInquiry {
    uint8_t command;     /* b0, == 0x12         */
    uint8_t b1;          /* b1, |= 0x01 (evpd)  */
    uint8_t b2;          /* b2; if evpd==1      */
    uint16_t alloc_len;  /* b3, b4              */
    uint8_t control;     /* b5                  */
}  __attribute__((packed));
typedef struct ScsiCdbInquiry ScsiCdbInquiry;

struct ScsiCdbRead10 {
    uint8_t command;    /* =0x28    */
    uint8_t b1;
    uint32_t lba;
    uint8_t b6;
    uint16_t xfer_length;
    uint8_t control;
}  __attribute__((packed));
typedef struct ScsiCdbRead10 ScsiCdbRead10;

struct ScsiCdbTestUnitReady {
    uint8_t command;    /* =0x00    */
    uint8_t b1_b4[4];
    uint8_t control;
} __attribute__((packed));
typedef struct ScsiCdbTestUnitReady ScsiCdbTestUnitReady;

struct ScsiCdbReportLuns {
    uint8_t command;        /* =0xa0        */
    uint8_t b1;
    uint8_t select_report;  /* =0x02, "all" */
    uint8_t b3_b5[3];
    uint32_t alloc_len;
    uint8_t b10;
    uint8_t control;
} __attribute__((packed));
typedef struct ScsiCdbReportLuns ScsiCdbReportLuns;

struct ScsiLunReport {
    uint32_t lun_list_len;
    uint32_t b4_b7;
    ScsiLun lun[1];   /* space for at least 1 lun must be allocated */
} __attribute__((packed));
typedef struct ScsiLunReport ScsiLunReport;

struct ScsiCdbReadCapacity16 {
    uint8_t command;        /* =0x9e = "service action in 16"       */
    uint8_t service_action; /* 5 bits, =0x10 = "read capacity 16"   */
    uint64_t b2_b9;
    uint32_t alloc_len;
    uint8_t b14;
    uint8_t control;
} __attribute__((packed));
typedef struct ScsiCdbReadCapacity16 ScsiCdbReadCapacity16;

struct ScsiReadCapacity16Data {
    uint64_t ret_lba;             /* get it, 0..7     */
    uint32_t lb_len;              /* bytes, 8..11     */
    uint8_t u1[2 + 1 * 2 + 16];   /* b12..b31, unused */
} __attribute__((packed));
typedef struct ScsiReadCapacity16Data ScsiReadCapacity16Data;

static inline ScsiLun make_lun(uint16_t channel, uint16_t target, uint32_t lun)
{
    ScsiLun r = { .v64 = 0 };

    /* See QEMU code to choose the way to handle LUNs.
     *
     * So, a valid LUN must have (always channel #0):
     *  lun[0] == 1
     *  lun[1] - target, any value
     *  lun[2] == 0 or (LUN, MSB, 0x40 set, 0x80 clear)
     *  lun[3] - LUN, LSB, any value
     */
    r.v8[0] = 1;
    r.v8[1] = target & 0xffU;
    r.v8[2] = (lun >> 8) & 0x3fU;
    if (r.v8[2]) {
        r.v8[2] |= 0x40;
    }
    r.v8[3] = lun & 0xffU;

    return r;
}

static inline const char *scsi_cdb_status_msg(uint8_t status)
{
    static char err_msg[] = "STATUS=XX";
    uint8_t v = status & 0x3eU;

    fill_hex_val(err_msg + 7, &v, 1);
    return err_msg;
}

static inline const char *scsi_cdb_asc_msg(const void *s)
{
    static char err_msg[] = "RSPN=XX KEY=XX CODE=XX QLFR=XX";
    const ScsiSense70 *p = s;
    uint8_t sr = scsi_sense_response(s);
    uint8_t sk = scsi_sense_key(s);
    uint8_t ac = p->additional_sense_code;
    uint8_t cq = p->additional_sense_code_qualifier;

    fill_hex_val(err_msg + 5, &sr, 1);
    fill_hex_val(err_msg + 12, &sk, 1);
    fill_hex_val(err_msg + 20, &ac, 1);
    fill_hex_val(err_msg + 28, &cq, 1);

    return err_msg;
}

#endif /* SCSI_H */