target/arm: Use _ra versions of cpu_stl_data() in v7M helpers
[qemu/ar7.git] / pc-bios / s390-ccw / virtio-scsi.c
blob4fe4b9d261a6c88dada4df3bfde3093cdd2f7e07
1 /*
2 * Virtio-SCSI implementation for s390 machine loader for qemu
4 * Copyright 2015 IBM Corp.
5 * Author: Eugene "jno" Dvurechenski <jno@linux.vnet.ibm.com>
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.
12 #include "libc.h"
13 #include "s390-ccw.h"
14 #include "virtio.h"
15 #include "scsi.h"
16 #include "virtio-scsi.h"
18 static ScsiDevice default_scsi_device;
19 static VirtioScsiCmdReq req;
20 static VirtioScsiCmdResp resp;
22 static uint8_t scsi_inquiry_std_response[256];
23 static ScsiInquiryEvpdPages scsi_inquiry_evpd_pages_response;
24 static ScsiInquiryEvpdBl scsi_inquiry_evpd_bl_response;
26 static inline void vs_assert(bool term, const char **msgs)
28 if (!term) {
29 int i = 0;
31 sclp_print("\n! ");
32 while (msgs[i]) {
33 sclp_print(msgs[i++]);
35 panic(" !\n");
39 static void virtio_scsi_verify_response(VirtioScsiCmdResp *resp,
40 const char *title)
42 const char *mr[] = {
43 title, ": response ", virtio_scsi_response_msg(resp), 0
45 const char *ms[] = {
46 title,
47 CDB_STATUS_VALID(resp->status) ? ": " : ": invalid ",
48 scsi_cdb_status_msg(resp->status),
49 resp->status == CDB_STATUS_CHECK_CONDITION ? " " : 0,
50 resp->sense_len ? scsi_cdb_asc_msg(resp->sense)
51 : "no sense data",
52 scsi_sense_response(resp->sense) == 0x70 ? ", sure" : "?",
56 vs_assert(resp->response == VIRTIO_SCSI_S_OK, mr);
57 vs_assert(resp->status == CDB_STATUS_GOOD, ms);
60 static void prepare_request(VDev *vdev, const void *cdb, int cdb_size,
61 void *data, uint32_t data_size)
63 const ScsiDevice *sdev = vdev->scsi_device;
65 memset(&req, 0, sizeof(req));
66 req.lun = make_lun(sdev->channel, sdev->target, sdev->lun);
67 memcpy(&req.cdb, cdb, cdb_size);
69 memset(&resp, 0, sizeof(resp));
70 resp.status = 0xff; /* set invalid */
71 resp.response = 0xff; /* */
73 if (data && data_size) {
74 memset(data, 0, data_size);
78 static inline void vs_io_assert(bool term, const char *msg)
80 if (!term) {
81 virtio_scsi_verify_response(&resp, msg);
85 static void vs_run(const char *title, VirtioCmd *cmd, VDev *vdev,
86 const void *cdb, int cdb_size,
87 void *data, uint32_t data_size)
89 prepare_request(vdev, cdb, cdb_size, data, data_size);
90 vs_io_assert(virtio_run(vdev, VR_REQUEST, cmd) == 0, title);
93 /* SCSI protocol implementation routines */
95 static bool scsi_inquiry(VDev *vdev, uint8_t evpd, uint8_t page,
96 void *data, uint32_t data_size)
98 ScsiCdbInquiry cdb = {
99 .command = 0x12,
100 .b1 = evpd,
101 .b2 = page,
102 .alloc_len = data_size < 65535 ? data_size : 65535,
104 VirtioCmd inquiry[] = {
105 { &req, sizeof(req), VRING_DESC_F_NEXT },
106 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
107 { data, data_size, VRING_DESC_F_WRITE },
110 vs_run("inquiry", inquiry, vdev, &cdb, sizeof(cdb), data, data_size);
112 return virtio_scsi_response_ok(&resp);
115 static bool scsi_test_unit_ready(VDev *vdev)
117 ScsiCdbTestUnitReady cdb = {
118 .command = 0x00,
120 VirtioCmd test_unit_ready[] = {
121 { &req, sizeof(req), VRING_DESC_F_NEXT },
122 { &resp, sizeof(resp), VRING_DESC_F_WRITE },
125 prepare_request(vdev, &cdb, sizeof(cdb), 0, 0);
126 virtio_run(vdev, VR_REQUEST, test_unit_ready); /* ignore errors here */
128 return virtio_scsi_response_ok(&resp);
131 static bool scsi_report_luns(VDev *vdev, void *data, uint32_t data_size)
133 ScsiCdbReportLuns cdb = {
134 .command = 0xa0,
135 .select_report = 0x02, /* REPORT ALL */
136 .alloc_len = data_size,
138 VirtioCmd report_luns[] = {
139 { &req, sizeof(req), VRING_DESC_F_NEXT },
140 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
141 { data, data_size, VRING_DESC_F_WRITE },
144 vs_run("report luns", report_luns,
145 vdev, &cdb, sizeof(cdb), data, data_size);
147 return virtio_scsi_response_ok(&resp);
150 static bool scsi_read_10(VDev *vdev,
151 ulong sector, int sectors, void *data,
152 unsigned int data_size)
154 ScsiCdbRead10 cdb = {
155 .command = 0x28,
156 .lba = sector,
157 .xfer_length = sectors,
159 VirtioCmd read_10[] = {
160 { &req, sizeof(req), VRING_DESC_F_NEXT },
161 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
162 { data, data_size, VRING_DESC_F_WRITE },
165 debug_print_int("read_10 sector", sector);
166 debug_print_int("read_10 sectors", sectors);
168 vs_run("read(10)", read_10, vdev, &cdb, sizeof(cdb), data, data_size);
170 return virtio_scsi_response_ok(&resp);
173 static bool scsi_read_capacity(VDev *vdev,
174 void *data, uint32_t data_size)
176 ScsiCdbReadCapacity16 cdb = {
177 .command = 0x9e, /* SERVICE_ACTION_IN_16 */
178 .service_action = 0x10, /* SA_READ_CAPACITY */
179 .alloc_len = data_size,
181 VirtioCmd read_capacity_16[] = {
182 { &req, sizeof(req), VRING_DESC_F_NEXT },
183 { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
184 { data, data_size, VRING_DESC_F_WRITE },
187 vs_run("read capacity", read_capacity_16,
188 vdev, &cdb, sizeof(cdb), data, data_size);
190 return virtio_scsi_response_ok(&resp);
193 /* virtio-scsi routines */
195 static void virtio_scsi_locate_device(VDev *vdev)
197 const uint16_t channel = 0; /* again, it's what QEMU does */
198 uint16_t target;
199 static uint8_t data[16 + 8 * 63];
200 ScsiLunReport *r = (void *) data;
201 ScsiDevice *sdev = vdev->scsi_device;
202 int i, luns;
204 /* QEMU has hardcoded channel #0 in many places.
205 * If this hardcoded value is ever changed, we'll need to add code for
206 * vdev->config.scsi.max_channel != 0 here.
208 debug_print_int("config.scsi.max_channel", vdev->config.scsi.max_channel);
209 debug_print_int("config.scsi.max_target ", vdev->config.scsi.max_target);
210 debug_print_int("config.scsi.max_lun ", vdev->config.scsi.max_lun);
211 debug_print_int("config.scsi.max_sectors", vdev->config.scsi.max_sectors);
213 if (vdev->scsi_device_selected) {
214 sdev->channel = vdev->selected_scsi_device.channel;
215 sdev->target = vdev->selected_scsi_device.target;
216 sdev->lun = vdev->selected_scsi_device.lun;
218 IPL_check(sdev->channel == 0, "non-zero channel requested");
219 IPL_check(sdev->target <= vdev->config.scsi.max_target, "target# high");
220 IPL_check(sdev->lun <= vdev->config.scsi.max_lun, "LUN# high");
221 return;
224 for (target = 0; target <= vdev->config.scsi.max_target; target++) {
225 sdev->channel = channel;
226 sdev->target = target;
227 sdev->lun = 0; /* LUN has to be 0 for REPORT LUNS */
228 if (!scsi_report_luns(vdev, data, sizeof(data))) {
229 if (resp.response == VIRTIO_SCSI_S_BAD_TARGET) {
230 continue;
232 print_int("target", target);
233 virtio_scsi_verify_response(&resp, "SCSI cannot report LUNs");
235 if (r->lun_list_len == 0) {
236 print_int("no LUNs for target", target);
237 continue;
239 luns = r->lun_list_len / 8;
240 debug_print_int("LUNs reported", luns);
241 if (luns == 1) {
242 /* There is no ",lun=#" arg for -device or ",lun=0" given.
243 * Hence, the only LUN reported.
244 * Usually, it's 0.
246 sdev->lun = r->lun[0].v16[0]; /* it's returned this way */
247 debug_print_int("Have to use LUN", sdev->lun);
248 return; /* we have to use this device */
250 for (i = 0; i < luns; i++) {
251 if (r->lun[i].v64) {
252 /* Look for non-zero LUN - we have where to choose from */
253 sdev->lun = r->lun[i].v16[0];
254 debug_print_int("Will use LUN", sdev->lun);
255 return; /* we have found a device */
259 panic("\n! Cannot locate virtio-scsi device !\n");
262 int virtio_scsi_read_many(VDev *vdev,
263 ulong sector, void *load_addr, int sec_num)
265 int sector_count;
266 int f = vdev->blk_factor;
267 unsigned int data_size;
268 unsigned int max_transfer = MIN_NON_ZERO(vdev->config.scsi.max_sectors,
269 vdev->max_transfer);
271 do {
272 sector_count = MIN_NON_ZERO(sec_num, max_transfer);
273 data_size = sector_count * virtio_get_block_size() * f;
274 if (!scsi_read_10(vdev, sector * f, sector_count * f, load_addr,
275 data_size)) {
276 virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
278 load_addr += data_size;
279 sector += sector_count;
280 sec_num -= sector_count;
281 } while (sec_num > 0);
283 return 0;
286 static bool virtio_scsi_inquiry_response_is_cdrom(void *data)
288 const ScsiInquiryStd *response = data;
289 const int resp_data_fmt = response->b3 & 0x0f;
290 int i;
292 IPL_check(resp_data_fmt == 2, "Wrong INQUIRY response format");
293 if (resp_data_fmt != 2) {
294 return false; /* cannot decode */
297 if ((response->peripheral_qdt & 0x1f) == SCSI_INQ_RDT_CDROM) {
298 return true;
301 for (i = 0; i < sizeof(response->prod_id); i++) {
302 if (response->prod_id[i] != QEMU_CDROM_SIGNATURE[i]) {
303 return false;
306 return true;
309 static void scsi_parse_capacity_report(void *data,
310 uint64_t *last_lba, uint32_t *lb_len)
312 ScsiReadCapacity16Data *p = data;
314 if (last_lba) {
315 *last_lba = p->ret_lba;
318 if (lb_len) {
319 *lb_len = p->lb_len;
323 void virtio_scsi_setup(VDev *vdev)
325 int retry_test_unit_ready = 3;
326 uint8_t data[256];
327 uint32_t data_size = sizeof(data);
328 ScsiInquiryEvpdPages *evpd = &scsi_inquiry_evpd_pages_response;
329 ScsiInquiryEvpdBl *evpd_bl = &scsi_inquiry_evpd_bl_response;
330 int i;
332 vdev->scsi_device = &default_scsi_device;
333 virtio_scsi_locate_device(vdev);
335 /* We have to "ping" the device before it becomes readable */
336 while (!scsi_test_unit_ready(vdev)) {
338 if (!virtio_scsi_response_ok(&resp)) {
339 uint8_t code = resp.sense[0] & SCSI_SENSE_CODE_MASK;
340 uint8_t sense_key = resp.sense[2] & SCSI_SENSE_KEY_MASK;
342 IPL_assert(resp.sense_len != 0, "virtio-scsi:setup: no SENSE data");
344 IPL_assert(retry_test_unit_ready && code == 0x70 &&
345 sense_key == SCSI_SENSE_KEY_UNIT_ATTENTION,
346 "virtio-scsi:setup: cannot retry");
348 /* retry on CHECK_CONDITION/UNIT_ATTENTION as it
349 * may not designate a real error, but it may be
350 * a result of device reset, etc.
352 retry_test_unit_ready--;
353 sleep(1);
354 continue;
357 virtio_scsi_verify_response(&resp, "virtio-scsi:setup");
360 /* read and cache SCSI INQUIRY response */
361 if (!scsi_inquiry(vdev,
362 SCSI_INQUIRY_STANDARD,
363 SCSI_INQUIRY_STANDARD_NONE,
364 scsi_inquiry_std_response,
365 sizeof(scsi_inquiry_std_response))) {
366 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:inquiry");
369 if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response)) {
370 sclp_print("SCSI CD-ROM detected.\n");
371 vdev->is_cdrom = true;
372 vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
375 if (!scsi_inquiry(vdev,
376 SCSI_INQUIRY_EVPD,
377 SCSI_INQUIRY_EVPD_SUPPORTED_PAGES,
378 evpd,
379 sizeof(*evpd))) {
380 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:supported_pages");
383 debug_print_int("EVPD length", evpd->page_length);
385 for (i = 0; i <= evpd->page_length; i++) {
386 debug_print_int("supported EVPD page", evpd->byte[i]);
388 if (evpd->byte[i] != SCSI_INQUIRY_EVPD_BLOCK_LIMITS) {
389 continue;
392 if (!scsi_inquiry(vdev,
393 SCSI_INQUIRY_EVPD,
394 SCSI_INQUIRY_EVPD_BLOCK_LIMITS,
395 evpd_bl,
396 sizeof(*evpd_bl))) {
397 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:blocklimits");
400 debug_print_int("max transfer", evpd_bl->max_transfer);
401 vdev->max_transfer = evpd_bl->max_transfer;
405 * The host sg driver will often be unhappy with particularly large
406 * I/Os that exceed the block iovec limits. Let's enforce something
407 * reasonable, despite what the device configuration tells us.
410 vdev->max_transfer = MIN_NON_ZERO(VIRTIO_SCSI_MAX_SECTORS,
411 vdev->max_transfer);
413 if (!scsi_read_capacity(vdev, data, data_size)) {
414 virtio_scsi_verify_response(&resp, "virtio-scsi:setup:read_capacity");
416 scsi_parse_capacity_report(data, &vdev->scsi_last_block,
417 (uint32_t *) &vdev->scsi_block_size);