]> git.proxmox.com Git - mirror_qemu.git/blob - pc-bios/s390-ccw/virtio-scsi.c
pc-bios/s390-ccw: Move SCSI block factor to outer read
[mirror_qemu.git] / pc-bios / s390-ccw / virtio-scsi.c
1 /*
2 * Virtio-SCSI implementation for s390 machine loader for qemu
3 *
4 * Copyright 2015 IBM Corp.
5 * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com>
6 *
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
9 * directory.
10 */
11
12 #include "s390-ccw.h"
13 #include "virtio.h"
14 #include "scsi.h"
15 #include "virtio-scsi.h"
16
17 static ScsiDevice default_scsi_device;
18 static VirtioScsiCmdReq req;
19 static VirtioScsiCmdResp resp;
20
21 static uint8_t scsi_inquiry_std_response[256];
22
23 static inline void vs_assert(bool term, const char **msgs)
24 {
25 if (!term) {
26 int i = 0;
27
28 sclp_print("\n! ");
29 while (msgs[i]) {
30 sclp_print(msgs[i++]);
31 }
32 panic(" !\n");
33 }
34 }
35
36 static void virtio_scsi_verify_response(VirtioScsiCmdResp *resp,
37 const char *title)
38 {
39 const char *mr[] = {
40 title, ": response ", virtio_scsi_response_msg(resp), 0
41 };
42 const char *ms[] = {
43 title,
44 CDB_STATUS_VALID(resp->status) ? ": " : ": invalid ",
45 scsi_cdb_status_msg(resp->status),
46 resp->status == CDB_STATUS_CHECK_CONDITION ? " " : 0,
47 resp->sense_len ? scsi_cdb_asc_msg(resp->sense)
48 : "no sense data",
49 scsi_sense_response(resp->sense) == 0x70 ? ", sure" : "?",
50 0
51 };
52
53 vs_assert(resp->response == VIRTIO_SCSI_S_OK, mr);
54 vs_assert(resp->status == CDB_STATUS_GOOD, ms);
55 }
56
57 static void prepare_request(VDev *vdev, const void *cdb, int cdb_size,
58 void *data, uint32_t data_size)
59 {
60 const ScsiDevice *sdev = vdev->scsi_device;
61
62 memset(&req, 0, sizeof(req));
63 req.lun = make_lun(sdev->channel, sdev->target, sdev->lun);
64 memcpy(&req.cdb, cdb, cdb_size);
65
66 memset(&resp, 0, sizeof(resp));
67 resp.status = 0xff; /* set invalid */
68 resp.response = 0xff; /* */
69
70 if (data && data_size) {
71 memset(data, 0, data_size);
72 }
73 }
74
75 static inline void vs_io_assert(bool term, const char *msg)
76 {
77 if (!term) {
78 virtio_scsi_verify_response(&resp, msg);
79 }
80 }
81
82 static void vs_run(const char *title, VirtioCmd *cmd, VDev *vdev,
83 const void *cdb, int cdb_size,
84 void *data, uint32_t data_size)
85 {
86 prepare_request(vdev, cdb, cdb_size, data, data_size);
87 vs_io_assert(virtio_run(vdev, VR_REQUEST, cmd) == 0, title);
88 }
89
90 /* SCSI protocol implementation routines */
91
92 static bool scsi_inquiry(VDev *vdev, void *data, uint32_t data_size)
93 {
94 ScsiCdbInquiry cdb = {
95 .command = 0x12,
96 .alloc_len = data_size < 65535 ? data_size : 65535,
97 };
98 VirtioCmd inquiry[] = {
99 { &req, sizeof(req), VRING_DESC_F_NEXT },
100 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
101 { data, data_size, VRING_DESC_F_WRITE },
102 };
103
104 vs_run("inquiry", inquiry, vdev, &cdb, sizeof(cdb), data, data_size);
105
106 return virtio_scsi_response_ok(&resp);
107 }
108
109 static bool scsi_test_unit_ready(VDev *vdev)
110 {
111 ScsiCdbTestUnitReady cdb = {
112 .command = 0x00,
113 };
114 VirtioCmd test_unit_ready[] = {
115 { &req, sizeof(req), VRING_DESC_F_NEXT },
116 { &resp, sizeof(resp), VRING_DESC_F_WRITE },
117 };
118
119 prepare_request(vdev, &cdb, sizeof(cdb), 0, 0);
120 virtio_run(vdev, VR_REQUEST, test_unit_ready); /* ignore errors here */
121
122 return virtio_scsi_response_ok(&resp);
123 }
124
125 static bool scsi_report_luns(VDev *vdev, void *data, uint32_t data_size)
126 {
127 ScsiCdbReportLuns cdb = {
128 .command = 0xa0,
129 .select_report = 0x02, /* REPORT ALL */
130 .alloc_len = data_size,
131 };
132 VirtioCmd report_luns[] = {
133 { &req, sizeof(req), VRING_DESC_F_NEXT },
134 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
135 { data, data_size, VRING_DESC_F_WRITE },
136 };
137
138 vs_run("report luns", report_luns,
139 vdev, &cdb, sizeof(cdb), data, data_size);
140
141 return virtio_scsi_response_ok(&resp);
142 }
143
144 static bool scsi_read_10(VDev *vdev,
145 ulong sector, int sectors, void *data,
146 unsigned int data_size)
147 {
148 ScsiCdbRead10 cdb = {
149 .command = 0x28,
150 .lba = sector,
151 .xfer_length = sectors,
152 };
153 VirtioCmd read_10[] = {
154 { &req, sizeof(req), VRING_DESC_F_NEXT },
155 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
156 { data, data_size, VRING_DESC_F_WRITE },
157 };
158
159 debug_print_int("read_10 sector", sector);
160 debug_print_int("read_10 sectors", sectors);
161
162 vs_run("read(10)", read_10, vdev, &cdb, sizeof(cdb), data, data_size);
163
164 return virtio_scsi_response_ok(&resp);
165 }
166
167 static bool scsi_read_capacity(VDev *vdev,
168 void *data, uint32_t data_size)
169 {
170 ScsiCdbReadCapacity16 cdb = {
171 .command = 0x9e, /* SERVICE_ACTION_IN_16 */
172 .service_action = 0x10, /* SA_READ_CAPACITY */
173 .alloc_len = data_size,
174 };
175 VirtioCmd read_capacity_16[] = {
176 { &req, sizeof(req), VRING_DESC_F_NEXT },
177 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
178 { data, data_size, VRING_DESC_F_WRITE },
179 };
180
181 vs_run("read capacity", read_capacity_16,
182 vdev, &cdb, sizeof(cdb), data, data_size);
183
184 return virtio_scsi_response_ok(&resp);
185 }
186
187 /* virtio-scsi routines */
188
189 static void virtio_scsi_locate_device(VDev *vdev)
190 {
191 const uint16_t channel = 0; /* again, it's what QEMU does */
192 uint16_t target;
193 static uint8_t data[16 + 8 * 63];
194 ScsiLunReport *r = (void *) data;
195 ScsiDevice *sdev = vdev->scsi_device;
196 int i, luns;
197
198 /* QEMU has hardcoded channel #0 in many places.
199 * If this hardcoded value is ever changed, we'll need to add code for
200 * vdev->config.scsi.max_channel != 0 here.
201 */
202 debug_print_int("config.scsi.max_channel", vdev->config.scsi.max_channel);
203 debug_print_int("config.scsi.max_target ", vdev->config.scsi.max_target);
204 debug_print_int("config.scsi.max_lun ", vdev->config.scsi.max_lun);
205
206 if (vdev->scsi_device_selected) {
207 sdev->channel = vdev->selected_scsi_device.channel;
208 sdev->target = vdev->selected_scsi_device.target;
209 sdev->lun = vdev->selected_scsi_device.lun;
210
211 IPL_check(sdev->channel == 0, "non-zero channel requested");
212 IPL_check(sdev->target <= vdev->config.scsi.max_target, "target# high");
213 IPL_check(sdev->lun <= vdev->config.scsi.max_lun, "LUN# high");
214 return;
215 }
216
217 for (target = 0; target <= vdev->config.scsi.max_target; target++) {
218 sdev->channel = channel;
219 sdev->target = target; /* sdev->lun will be 0 here */
220 if (!scsi_report_luns(vdev, data, sizeof(data))) {
221 if (resp.response == VIRTIO_SCSI_S_BAD_TARGET) {
222 continue;
223 }
224 print_int("target", target);
225 virtio_scsi_verify_response(&resp, "SCSI cannot report LUNs");
226 }
227 if (r->lun_list_len == 0) {
228 print_int("no LUNs for target", target);
229 continue;
230 }
231 luns = r->lun_list_len / 8;
232 debug_print_int("LUNs reported", luns);
233 if (luns == 1) {
234 /* There is no ",lun=#" arg for -device or ",lun=0" given.
235 * Hence, the only LUN reported.
236 * Usually, it's 0.
237 */
238 sdev->lun = r->lun[0].v16[0]; /* it's returned this way */
239 debug_print_int("Have to use LUN", sdev->lun);
240 return; /* we have to use this device */
241 }
242 for (i = 0; i < luns; i++) {
243 if (r->lun[i].v64) {
244 /* Look for non-zero LUN - we have where to choose from */
245 sdev->lun = r->lun[i].v16[0];
246 debug_print_int("Will use LUN", sdev->lun);
247 return; /* we have found a device */
248 }
249 }
250 }
251 panic("\n! Cannot locate virtio-scsi device !\n");
252 }
253
254 int virtio_scsi_read_many(VDev *vdev,
255 ulong sector, void *load_addr, int sec_num)
256 {
257 int f = vdev->blk_factor;
258 unsigned int data_size = sec_num * virtio_get_block_size() * f;
259
260 if (!scsi_read_10(vdev, sector * f, sec_num * f, load_addr, data_size)) {
261 virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
262 }
263
264 return 0;
265 }
266
267 static bool virtio_scsi_inquiry_response_is_cdrom(void *data)
268 {
269 const ScsiInquiryStd *response = data;
270 const int resp_data_fmt = response->b3 & 0x0f;
271 int i;
272
273 IPL_check(resp_data_fmt == 2, "Wrong INQUIRY response format");
274 if (resp_data_fmt != 2) {
275 return false; /* cannot decode */
276 }
277
278 if ((response->peripheral_qdt & 0x1f) == SCSI_INQ_RDT_CDROM) {
279 return true;
280 }
281
282 for (i = 0; i < sizeof(response->prod_id); i++) {
283 if (response->prod_id[i] != QEMU_CDROM_SIGNATURE[i]) {
284 return false;
285 }
286 }
287 return true;
288 }
289
290 static void scsi_parse_capacity_report(void *data,
291 uint64_t *last_lba, uint32_t *lb_len)
292 {
293 ScsiReadCapacity16Data *p = data;
294
295 if (last_lba) {
296 *last_lba = p->ret_lba;
297 }
298
299 if (lb_len) {
300 *lb_len = p->lb_len;
301 }
302 }
303
304 void virtio_scsi_setup(VDev *vdev)
305 {
306 int retry_test_unit_ready = 3;
307 uint8_t data[256];
308 uint32_t data_size = sizeof(data);
309
310 vdev->scsi_device = &default_scsi_device;
311 virtio_scsi_locate_device(vdev);
312
313 /* We have to "ping" the device before it becomes readable */
314 while (!scsi_test_unit_ready(vdev)) {
315
316 if (!virtio_scsi_response_ok(&resp)) {
317 uint8_t code = resp.sense[0] & SCSI_SENSE_CODE_MASK;
318 uint8_t sense_key = resp.sense[2] & SCSI_SENSE_KEY_MASK;
319
320 IPL_assert(resp.sense_len != 0, "virtio-scsi:setup: no SENSE data");
321
322 IPL_assert(retry_test_unit_ready && code == 0x70 &&
323 sense_key == SCSI_SENSE_KEY_UNIT_ATTENTION,
324 "virtio-scsi:setup: cannot retry");
325
326 /* retry on CHECK_CONDITION/UNIT_ATTENTION as it
327 * may not designate a real error, but it may be
328 * a result of device reset, etc.
329 */
330 retry_test_unit_ready--;
331 sleep(1);
332 continue;
333 }
334
335 virtio_scsi_verify_response(&resp, "virtio-scsi:setup");
336 }
337
338 /* read and cache SCSI INQUIRY response */
339 if (!scsi_inquiry(vdev, scsi_inquiry_std_response,
340 sizeof(scsi_inquiry_std_response))) {
341 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:inquiry");
342 }
343
344 if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response)) {
345 sclp_print("SCSI CD-ROM detected.\n");
346 vdev->is_cdrom = true;
347 vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
348 }
349
350 if (!scsi_read_capacity(vdev, data, data_size)) {
351 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:read_capacity");
352 }
353 scsi_parse_capacity_report(data, &vdev->scsi_last_block,
354 (uint32_t *) &vdev->scsi_block_size);
355 }