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