xref: /openbmc/qemu/pc-bios/s390-ccw/virtio-scsi.c (revision 9f427883)
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 <string.h>
13 #include <stdio.h>
14 #include "s390-ccw.h"
15 #include "virtio.h"
16 #include "scsi.h"
17 #include "virtio-scsi.h"
18 #include "s390-time.h"
19 #include "helper.h"
20 
21 static ScsiDevice default_scsi_device;
22 static VirtioScsiCmdReq req;
23 static VirtioScsiCmdResp resp;
24 
25 static uint8_t scsi_inquiry_std_response[256];
26 static ScsiInquiryEvpdPages scsi_inquiry_evpd_pages_response;
27 static ScsiInquiryEvpdBl scsi_inquiry_evpd_bl_response;
28 
29 static inline void vs_assert(bool term, const char **msgs)
30 {
31     if (!term) {
32         int i = 0;
33 
34         printf("\n! ");
35         while (msgs[i]) {
36             printf("%s", msgs[i++]);
37         }
38         panic(" !\n");
39     }
40 }
41 
42 static void virtio_scsi_verify_response(VirtioScsiCmdResp *resp,
43                                         const char *title)
44 {
45     const char *mr[] = {
46         title, ": response ", virtio_scsi_response_msg(resp), 0
47     };
48     const char *ms[] = {
49         title,
50         CDB_STATUS_VALID(resp->status) ? ": " : ": invalid ",
51         scsi_cdb_status_msg(resp->status),
52         resp->status == CDB_STATUS_CHECK_CONDITION ? " " : 0,
53         resp->sense_len ? scsi_cdb_asc_msg(resp->sense)
54                         : "no sense data",
55         scsi_sense_response(resp->sense)  == 0x70 ? ", sure" : "?",
56         0
57     };
58 
59     vs_assert(resp->response == VIRTIO_SCSI_S_OK, mr);
60     vs_assert(resp->status == CDB_STATUS_GOOD, ms);
61 }
62 
63 static void prepare_request(VDev *vdev, const void *cdb, int cdb_size,
64                             void *data, uint32_t data_size)
65 {
66     const ScsiDevice *sdev = vdev->scsi_device;
67 
68     memset(&req, 0, sizeof(req));
69     req.lun = make_lun(sdev->channel, sdev->target, sdev->lun);
70     memcpy(&req.cdb, cdb, cdb_size);
71 
72     memset(&resp, 0, sizeof(resp));
73     resp.status = 0xff;     /* set invalid  */
74     resp.response = 0xff;   /*              */
75 
76     if (data && data_size) {
77         memset(data, 0, data_size);
78     }
79 }
80 
81 static inline void vs_io_assert(bool term, const char *msg)
82 {
83     if (!term) {
84         virtio_scsi_verify_response(&resp, msg);
85     }
86 }
87 
88 static void vs_run(const char *title, VirtioCmd *cmd, VDev *vdev,
89                    const void *cdb, int cdb_size,
90                    void *data, uint32_t data_size)
91 {
92     prepare_request(vdev, cdb, cdb_size, data, data_size);
93     vs_io_assert(virtio_run(vdev, VR_REQUEST, cmd) == 0, title);
94 }
95 
96 /* SCSI protocol implementation routines */
97 
98 static bool scsi_inquiry(VDev *vdev, uint8_t evpd, uint8_t page,
99                          void *data, uint32_t data_size)
100 {
101     ScsiCdbInquiry cdb = {
102         .command = 0x12,
103         .b1 = evpd,
104         .b2 = page,
105         .alloc_len = data_size < 65535 ? data_size : 65535,
106     };
107     VirtioCmd inquiry[] = {
108         { &req, sizeof(req), VRING_DESC_F_NEXT },
109         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
110         { data, data_size, VRING_DESC_F_WRITE },
111     };
112 
113     vs_run("inquiry", inquiry, vdev, &cdb, sizeof(cdb), data, data_size);
114 
115     return virtio_scsi_response_ok(&resp);
116 }
117 
118 static bool scsi_test_unit_ready(VDev *vdev)
119 {
120     ScsiCdbTestUnitReady cdb = {
121         .command = 0x00,
122     };
123     VirtioCmd test_unit_ready[] = {
124         { &req, sizeof(req), VRING_DESC_F_NEXT },
125         { &resp, sizeof(resp), VRING_DESC_F_WRITE },
126     };
127 
128     prepare_request(vdev, &cdb, sizeof(cdb), 0, 0);
129     virtio_run(vdev, VR_REQUEST, test_unit_ready); /* ignore errors here */
130 
131     return virtio_scsi_response_ok(&resp);
132 }
133 
134 static bool scsi_report_luns(VDev *vdev, void *data, uint32_t data_size)
135 {
136     ScsiCdbReportLuns cdb = {
137         .command = 0xa0,
138         .select_report = 0x02, /* REPORT ALL */
139         .alloc_len = data_size,
140     };
141     VirtioCmd report_luns[] = {
142         { &req, sizeof(req), VRING_DESC_F_NEXT },
143         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
144         { data, data_size, VRING_DESC_F_WRITE },
145     };
146 
147     vs_run("report luns", report_luns,
148            vdev, &cdb, sizeof(cdb), data, data_size);
149 
150     return virtio_scsi_response_ok(&resp);
151 }
152 
153 static bool scsi_read_10(VDev *vdev,
154                          unsigned long sector, int sectors, void *data,
155                          unsigned int data_size)
156 {
157     ScsiCdbRead10 cdb = {
158         .command = 0x28,
159         .lba = sector,
160         .xfer_length = sectors,
161     };
162     VirtioCmd read_10[] = {
163         { &req, sizeof(req), VRING_DESC_F_NEXT },
164         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
165         { data, data_size, VRING_DESC_F_WRITE },
166     };
167 
168     debug_print_int("read_10  sector", sector);
169     debug_print_int("read_10 sectors", sectors);
170 
171     vs_run("read(10)", read_10, vdev, &cdb, sizeof(cdb), data, data_size);
172 
173     return virtio_scsi_response_ok(&resp);
174 }
175 
176 static bool scsi_read_capacity(VDev *vdev,
177                                void *data, uint32_t data_size)
178 {
179     ScsiCdbReadCapacity16 cdb = {
180         .command = 0x9e, /* SERVICE_ACTION_IN_16 */
181         .service_action = 0x10, /* SA_READ_CAPACITY */
182         .alloc_len = data_size,
183     };
184     VirtioCmd read_capacity_16[] = {
185         { &req, sizeof(req), VRING_DESC_F_NEXT },
186         { &resp, sizeof(resp), VRING_DESC_F_WRITE | VRING_DESC_F_NEXT },
187         { data, data_size, VRING_DESC_F_WRITE },
188     };
189 
190     vs_run("read capacity", read_capacity_16,
191            vdev, &cdb, sizeof(cdb), data, data_size);
192 
193     return virtio_scsi_response_ok(&resp);
194 }
195 
196 /* virtio-scsi routines */
197 
198 /*
199  * Tries to locate a SCSI device and adds the information for the found
200  * device to the vdev->scsi_device structure.
201  * Returns 0 if SCSI device could be located, or a error code < 0 otherwise
202  */
203 static int virtio_scsi_locate_device(VDev *vdev)
204 {
205     const uint16_t channel = 0; /* again, it's what QEMU does */
206     uint16_t target;
207     static uint8_t data[16 + 8 * 63];
208     ScsiLunReport *r = (void *) data;
209     ScsiDevice *sdev = vdev->scsi_device;
210     int i, luns;
211 
212     /* QEMU has hardcoded channel #0 in many places.
213      * If this hardcoded value is ever changed, we'll need to add code for
214      * vdev->config.scsi.max_channel != 0 here.
215      */
216     debug_print_int("config.scsi.max_channel", vdev->config.scsi.max_channel);
217     debug_print_int("config.scsi.max_target ", vdev->config.scsi.max_target);
218     debug_print_int("config.scsi.max_lun    ", vdev->config.scsi.max_lun);
219     debug_print_int("config.scsi.max_sectors", vdev->config.scsi.max_sectors);
220 
221     if (vdev->scsi_device_selected) {
222         sdev->channel = vdev->selected_scsi_device.channel;
223         sdev->target = vdev->selected_scsi_device.target;
224         sdev->lun = vdev->selected_scsi_device.lun;
225 
226         IPL_check(sdev->channel == 0, "non-zero channel requested");
227         IPL_check(sdev->target <= vdev->config.scsi.max_target, "target# high");
228         IPL_check(sdev->lun <= vdev->config.scsi.max_lun, "LUN# high");
229         return 0;
230     }
231 
232     for (target = 0; target <= vdev->config.scsi.max_target; target++) {
233         sdev->channel = channel;
234         sdev->target = target;
235         sdev->lun = 0;          /* LUN has to be 0 for REPORT LUNS */
236         if (!scsi_report_luns(vdev, data, sizeof(data))) {
237             if (resp.response == VIRTIO_SCSI_S_BAD_TARGET) {
238                 continue;
239             }
240             printf("target 0x%X\n", target);
241             virtio_scsi_verify_response(&resp, "SCSI cannot report LUNs");
242         }
243         if (r->lun_list_len == 0) {
244             printf("no LUNs for target 0x%X\n", target);
245             continue;
246         }
247         luns = r->lun_list_len / 8;
248         debug_print_int("LUNs reported", luns);
249         if (luns == 1) {
250             /* There is no ",lun=#" arg for -device or ",lun=0" given.
251              * Hence, the only LUN reported.
252              * Usually, it's 0.
253              */
254             sdev->lun = r->lun[0].v16[0]; /* it's returned this way */
255             debug_print_int("Have to use LUN", sdev->lun);
256             return 0; /* we have to use this device */
257         }
258         for (i = 0; i < luns; i++) {
259             if (r->lun[i].v64) {
260                 /* Look for non-zero LUN - we have where to choose from */
261                 sdev->lun = r->lun[i].v16[0];
262                 debug_print_int("Will use LUN", sdev->lun);
263                 return 0; /* we have found a device */
264             }
265         }
266     }
267 
268     puts("Warning: Could not locate a usable virtio-scsi device");
269     return -ENODEV;
270 }
271 
272 int virtio_scsi_read_many(VDev *vdev,
273                           unsigned long sector, void *load_addr, int sec_num)
274 {
275     int sector_count;
276     int f = vdev->blk_factor;
277     unsigned int data_size;
278     unsigned int max_transfer = MIN_NON_ZERO(vdev->config.scsi.max_sectors,
279                                              vdev->max_transfer);
280 
281     do {
282         sector_count = MIN_NON_ZERO(sec_num, max_transfer);
283         data_size = sector_count * virtio_get_block_size() * f;
284         if (!scsi_read_10(vdev, sector * f, sector_count * f, load_addr,
285                           data_size)) {
286             virtio_scsi_verify_response(&resp, "virtio-scsi:read_many");
287         }
288         load_addr += data_size;
289         sector += sector_count;
290         sec_num -= sector_count;
291     } while (sec_num > 0);
292 
293     return 0;
294 }
295 
296 static bool virtio_scsi_inquiry_response_is_cdrom(void *data)
297 {
298     const ScsiInquiryStd *response = data;
299     const int resp_data_fmt = response->b3 & 0x0f;
300     int i;
301 
302     IPL_check(resp_data_fmt == 2, "Wrong INQUIRY response format");
303     if (resp_data_fmt != 2) {
304         return false; /* cannot decode */
305     }
306 
307     if ((response->peripheral_qdt & 0x1f) == SCSI_INQ_RDT_CDROM) {
308         return true;
309     }
310 
311     for (i = 0; i < sizeof(response->prod_id); i++) {
312         if (response->prod_id[i] != QEMU_CDROM_SIGNATURE[i]) {
313             return false;
314         }
315     }
316     return true;
317 }
318 
319 static void scsi_parse_capacity_report(void *data,
320                                        uint64_t *last_lba, uint32_t *lb_len)
321 {
322     ScsiReadCapacity16Data *p = data;
323 
324     if (last_lba) {
325         *last_lba = p->ret_lba;
326     }
327 
328     if (lb_len) {
329         *lb_len = p->lb_len;
330     }
331 }
332 
333 static int virtio_scsi_setup(VDev *vdev)
334 {
335     int retry_test_unit_ready = 3;
336     uint8_t data[256];
337     uint32_t data_size = sizeof(data);
338     ScsiInquiryEvpdPages *evpd = &scsi_inquiry_evpd_pages_response;
339     ScsiInquiryEvpdBl *evpd_bl = &scsi_inquiry_evpd_bl_response;
340     int i, ret;
341 
342     vdev->scsi_device = &default_scsi_device;
343     ret = virtio_scsi_locate_device(vdev);
344     if (ret < 0) {
345         return ret;
346     }
347 
348     /* We have to "ping" the device before it becomes readable */
349     while (!scsi_test_unit_ready(vdev)) {
350 
351         if (!virtio_scsi_response_ok(&resp)) {
352             uint8_t code = resp.sense[0] & SCSI_SENSE_CODE_MASK;
353             uint8_t sense_key = resp.sense[2] & SCSI_SENSE_KEY_MASK;
354 
355             IPL_assert(resp.sense_len != 0, "virtio-scsi:setup: no SENSE data");
356 
357             IPL_assert(retry_test_unit_ready && code == 0x70 &&
358                        sense_key == SCSI_SENSE_KEY_UNIT_ATTENTION,
359                        "virtio-scsi:setup: cannot retry");
360 
361             /* retry on CHECK_CONDITION/UNIT_ATTENTION as it
362              * may not designate a real error, but it may be
363              * a result of device reset, etc.
364              */
365             retry_test_unit_ready--;
366             sleep(1);
367             continue;
368         }
369 
370         virtio_scsi_verify_response(&resp, "virtio-scsi:setup");
371     }
372 
373     /* read and cache SCSI INQUIRY response */
374     if (!scsi_inquiry(vdev,
375                       SCSI_INQUIRY_STANDARD,
376                       SCSI_INQUIRY_STANDARD_NONE,
377                       scsi_inquiry_std_response,
378                       sizeof(scsi_inquiry_std_response))) {
379         virtio_scsi_verify_response(&resp, "virtio-scsi:setup:inquiry");
380     }
381 
382     if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response)) {
383         puts("SCSI CD-ROM detected.");
384         vdev->is_cdrom = true;
385         vdev->scsi_block_size = VIRTIO_ISO_BLOCK_SIZE;
386     }
387 
388     if (!scsi_inquiry(vdev,
389                       SCSI_INQUIRY_EVPD,
390                       SCSI_INQUIRY_EVPD_SUPPORTED_PAGES,
391                       evpd,
392                       sizeof(*evpd))) {
393         virtio_scsi_verify_response(&resp, "virtio-scsi:setup:supported_pages");
394     }
395 
396     debug_print_int("EVPD length", evpd->page_length);
397 
398     for (i = 0; i <= evpd->page_length; i++) {
399         debug_print_int("supported EVPD page", evpd->byte[i]);
400 
401         if (evpd->byte[i] != SCSI_INQUIRY_EVPD_BLOCK_LIMITS) {
402             continue;
403         }
404 
405         if (!scsi_inquiry(vdev,
406                           SCSI_INQUIRY_EVPD,
407                           SCSI_INQUIRY_EVPD_BLOCK_LIMITS,
408                           evpd_bl,
409                           sizeof(*evpd_bl))) {
410             virtio_scsi_verify_response(&resp, "virtio-scsi:setup:blocklimits");
411         }
412 
413         debug_print_int("max transfer", evpd_bl->max_transfer);
414         vdev->max_transfer = evpd_bl->max_transfer;
415     }
416 
417     /*
418      * The host sg driver will often be unhappy with particularly large
419      * I/Os that exceed the block iovec limits.  Let's enforce something
420      * reasonable, despite what the device configuration tells us.
421      */
422 
423     vdev->max_transfer = MIN_NON_ZERO(VIRTIO_SCSI_MAX_SECTORS,
424                                       vdev->max_transfer);
425 
426     if (!scsi_read_capacity(vdev, data, data_size)) {
427         virtio_scsi_verify_response(&resp, "virtio-scsi:setup:read_capacity");
428     }
429     scsi_parse_capacity_report(data, &vdev->scsi_last_block,
430                                (uint32_t *) &vdev->scsi_block_size);
431 
432     return 0;
433 }
434 
435 int virtio_scsi_setup_device(SubChannelId schid)
436 {
437     VDev *vdev = virtio_get_device();
438 
439     vdev->schid = schid;
440     virtio_setup_ccw(vdev);
441 
442     IPL_assert(vdev->config.scsi.sense_size == VIRTIO_SCSI_SENSE_SIZE,
443                "Config: sense size mismatch");
444     IPL_assert(vdev->config.scsi.cdb_size == VIRTIO_SCSI_CDB_SIZE,
445                "Config: CDB size mismatch");
446 
447     puts("Using virtio-scsi.");
448 
449     return virtio_scsi_setup(vdev);
450 }
451