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
15 #include "virtio-scsi.h"
17 static ScsiDevice default_scsi_device
;
18 static VirtioScsiCmdReq req
;
19 static VirtioScsiCmdResp resp
;
21 static uint8_t scsi_inquiry_std_response
[256];
22 static ScsiInquiryEvpdPages scsi_inquiry_evpd_pages_response
;
23 static ScsiInquiryEvpdBl scsi_inquiry_evpd_bl_response
;
25 static inline void vs_assert(bool term
, const char **msgs
)
32 sclp_print(msgs
[i
++]);
38 static void virtio_scsi_verify_response(VirtioScsiCmdResp
*resp
,
42 title
, ": response ", virtio_scsi_response_msg(resp
), 0
46 CDB_STATUS_VALID(resp
->status
) ? ": " : ": invalid ",
47 scsi_cdb_status_msg(resp
->status
),
48 resp
->status
== CDB_STATUS_CHECK_CONDITION
? " " : 0,
49 resp
->sense_len
? scsi_cdb_asc_msg(resp
->sense
)
51 scsi_sense_response(resp
->sense
) == 0x70 ? ", sure" : "?",
55 vs_assert(resp
->response
== VIRTIO_SCSI_S_OK
, mr
);
56 vs_assert(resp
->status
== CDB_STATUS_GOOD
, ms
);
59 static void prepare_request(VDev
*vdev
, const void *cdb
, int cdb_size
,
60 void *data
, uint32_t data_size
)
62 const ScsiDevice
*sdev
= vdev
->scsi_device
;
64 memset(&req
, 0, sizeof(req
));
65 req
.lun
= make_lun(sdev
->channel
, sdev
->target
, sdev
->lun
);
66 memcpy(&req
.cdb
, cdb
, cdb_size
);
68 memset(&resp
, 0, sizeof(resp
));
69 resp
.status
= 0xff; /* set invalid */
70 resp
.response
= 0xff; /* */
72 if (data
&& data_size
) {
73 memset(data
, 0, data_size
);
77 static inline void vs_io_assert(bool term
, const char *msg
)
80 virtio_scsi_verify_response(&resp
, msg
);
84 static void vs_run(const char *title
, VirtioCmd
*cmd
, VDev
*vdev
,
85 const void *cdb
, int cdb_size
,
86 void *data
, uint32_t data_size
)
88 prepare_request(vdev
, cdb
, cdb_size
, data
, data_size
);
89 vs_io_assert(virtio_run(vdev
, VR_REQUEST
, cmd
) == 0, title
);
92 /* SCSI protocol implementation routines */
94 static bool scsi_inquiry(VDev
*vdev
, uint8_t evpd
, uint8_t page
,
95 void *data
, uint32_t data_size
)
97 ScsiCdbInquiry cdb
= {
101 .alloc_len
= data_size
< 65535 ? data_size
: 65535,
103 VirtioCmd inquiry
[] = {
104 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
105 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
106 { data
, data_size
, VRING_DESC_F_WRITE
},
109 vs_run("inquiry", inquiry
, vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
111 return virtio_scsi_response_ok(&resp
);
114 static bool scsi_test_unit_ready(VDev
*vdev
)
116 ScsiCdbTestUnitReady cdb
= {
119 VirtioCmd test_unit_ready
[] = {
120 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
121 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
},
124 prepare_request(vdev
, &cdb
, sizeof(cdb
), 0, 0);
125 virtio_run(vdev
, VR_REQUEST
, test_unit_ready
); /* ignore errors here */
127 return virtio_scsi_response_ok(&resp
);
130 static bool scsi_report_luns(VDev
*vdev
, void *data
, uint32_t data_size
)
132 ScsiCdbReportLuns cdb
= {
134 .select_report
= 0x02, /* REPORT ALL */
135 .alloc_len
= data_size
,
137 VirtioCmd report_luns
[] = {
138 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
139 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
140 { data
, data_size
, VRING_DESC_F_WRITE
},
143 vs_run("report luns", report_luns
,
144 vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
146 return virtio_scsi_response_ok(&resp
);
149 static bool scsi_read_10(VDev
*vdev
,
150 ulong sector
, int sectors
, void *data
,
151 unsigned int data_size
)
153 ScsiCdbRead10 cdb
= {
156 .xfer_length
= sectors
,
158 VirtioCmd read_10
[] = {
159 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
160 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
161 { data
, data_size
, VRING_DESC_F_WRITE
},
164 debug_print_int("read_10 sector", sector
);
165 debug_print_int("read_10 sectors", sectors
);
167 vs_run("read(10)", read_10
, vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
169 return virtio_scsi_response_ok(&resp
);
172 static bool scsi_read_capacity(VDev
*vdev
,
173 void *data
, uint32_t data_size
)
175 ScsiCdbReadCapacity16 cdb
= {
176 .command
= 0x9e, /* SERVICE_ACTION_IN_16 */
177 .service_action
= 0x10, /* SA_READ_CAPACITY */
178 .alloc_len
= data_size
,
180 VirtioCmd read_capacity_16
[] = {
181 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
182 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
183 { data
, data_size
, VRING_DESC_F_WRITE
},
186 vs_run("read capacity", read_capacity_16
,
187 vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
189 return virtio_scsi_response_ok(&resp
);
192 /* virtio-scsi routines */
194 static void virtio_scsi_locate_device(VDev
*vdev
)
196 const uint16_t channel
= 0; /* again, it's what QEMU does */
198 static uint8_t data
[16 + 8 * 63];
199 ScsiLunReport
*r
= (void *) data
;
200 ScsiDevice
*sdev
= vdev
->scsi_device
;
203 /* QEMU has hardcoded channel #0 in many places.
204 * If this hardcoded value is ever changed, we'll need to add code for
205 * vdev->config.scsi.max_channel != 0 here.
207 debug_print_int("config.scsi.max_channel", vdev
->config
.scsi
.max_channel
);
208 debug_print_int("config.scsi.max_target ", vdev
->config
.scsi
.max_target
);
209 debug_print_int("config.scsi.max_lun ", vdev
->config
.scsi
.max_lun
);
210 debug_print_int("config.scsi.max_sectors", vdev
->config
.scsi
.max_sectors
);
212 if (vdev
->scsi_device_selected
) {
213 sdev
->channel
= vdev
->selected_scsi_device
.channel
;
214 sdev
->target
= vdev
->selected_scsi_device
.target
;
215 sdev
->lun
= vdev
->selected_scsi_device
.lun
;
217 IPL_check(sdev
->channel
== 0, "non-zero channel requested");
218 IPL_check(sdev
->target
<= vdev
->config
.scsi
.max_target
, "target# high");
219 IPL_check(sdev
->lun
<= vdev
->config
.scsi
.max_lun
, "LUN# high");
223 for (target
= 0; target
<= vdev
->config
.scsi
.max_target
; target
++) {
224 sdev
->channel
= channel
;
225 sdev
->target
= target
; /* sdev->lun will be 0 here */
226 if (!scsi_report_luns(vdev
, data
, sizeof(data
))) {
227 if (resp
.response
== VIRTIO_SCSI_S_BAD_TARGET
) {
230 print_int("target", target
);
231 virtio_scsi_verify_response(&resp
, "SCSI cannot report LUNs");
233 if (r
->lun_list_len
== 0) {
234 print_int("no LUNs for target", target
);
237 luns
= r
->lun_list_len
/ 8;
238 debug_print_int("LUNs reported", luns
);
240 /* There is no ",lun=#" arg for -device or ",lun=0" given.
241 * Hence, the only LUN reported.
244 sdev
->lun
= r
->lun
[0].v16
[0]; /* it's returned this way */
245 debug_print_int("Have to use LUN", sdev
->lun
);
246 return; /* we have to use this device */
248 for (i
= 0; i
< luns
; i
++) {
250 /* Look for non-zero LUN - we have where to choose from */
251 sdev
->lun
= r
->lun
[i
].v16
[0];
252 debug_print_int("Will use LUN", sdev
->lun
);
253 return; /* we have found a device */
257 panic("\n! Cannot locate virtio-scsi device !\n");
260 int virtio_scsi_read_many(VDev
*vdev
,
261 ulong sector
, void *load_addr
, int sec_num
)
264 int f
= vdev
->blk_factor
;
265 unsigned int data_size
;
266 unsigned int max_transfer
= MIN_NON_ZERO(vdev
->config
.scsi
.max_sectors
,
270 sector_count
= MIN_NON_ZERO(sec_num
, max_transfer
);
271 data_size
= sector_count
* virtio_get_block_size() * f
;
272 if (!scsi_read_10(vdev
, sector
* f
, sector_count
* f
, load_addr
,
274 virtio_scsi_verify_response(&resp
, "virtio-scsi:read_many");
276 load_addr
+= data_size
;
277 sector
+= sector_count
;
278 sec_num
-= sector_count
;
279 } while (sec_num
> 0);
284 static bool virtio_scsi_inquiry_response_is_cdrom(void *data
)
286 const ScsiInquiryStd
*response
= data
;
287 const int resp_data_fmt
= response
->b3
& 0x0f;
290 IPL_check(resp_data_fmt
== 2, "Wrong INQUIRY response format");
291 if (resp_data_fmt
!= 2) {
292 return false; /* cannot decode */
295 if ((response
->peripheral_qdt
& 0x1f) == SCSI_INQ_RDT_CDROM
) {
299 for (i
= 0; i
< sizeof(response
->prod_id
); i
++) {
300 if (response
->prod_id
[i
] != QEMU_CDROM_SIGNATURE
[i
]) {
307 static void scsi_parse_capacity_report(void *data
,
308 uint64_t *last_lba
, uint32_t *lb_len
)
310 ScsiReadCapacity16Data
*p
= data
;
313 *last_lba
= p
->ret_lba
;
321 void virtio_scsi_setup(VDev
*vdev
)
323 int retry_test_unit_ready
= 3;
325 uint32_t data_size
= sizeof(data
);
326 ScsiInquiryEvpdPages
*evpd
= &scsi_inquiry_evpd_pages_response
;
327 ScsiInquiryEvpdBl
*evpd_bl
= &scsi_inquiry_evpd_bl_response
;
330 vdev
->scsi_device
= &default_scsi_device
;
331 virtio_scsi_locate_device(vdev
);
333 /* We have to "ping" the device before it becomes readable */
334 while (!scsi_test_unit_ready(vdev
)) {
336 if (!virtio_scsi_response_ok(&resp
)) {
337 uint8_t code
= resp
.sense
[0] & SCSI_SENSE_CODE_MASK
;
338 uint8_t sense_key
= resp
.sense
[2] & SCSI_SENSE_KEY_MASK
;
340 IPL_assert(resp
.sense_len
!= 0, "virtio-scsi:setup: no SENSE data");
342 IPL_assert(retry_test_unit_ready
&& code
== 0x70 &&
343 sense_key
== SCSI_SENSE_KEY_UNIT_ATTENTION
,
344 "virtio-scsi:setup: cannot retry");
346 /* retry on CHECK_CONDITION/UNIT_ATTENTION as it
347 * may not designate a real error, but it may be
348 * a result of device reset, etc.
350 retry_test_unit_ready
--;
355 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup");
358 /* read and cache SCSI INQUIRY response */
359 if (!scsi_inquiry(vdev
,
360 SCSI_INQUIRY_STANDARD
,
361 SCSI_INQUIRY_STANDARD_NONE
,
362 scsi_inquiry_std_response
,
363 sizeof(scsi_inquiry_std_response
))) {
364 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:inquiry");
367 if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response
)) {
368 sclp_print("SCSI CD-ROM detected.\n");
369 vdev
->is_cdrom
= true;
370 vdev
->scsi_block_size
= VIRTIO_ISO_BLOCK_SIZE
;
373 if (!scsi_inquiry(vdev
,
375 SCSI_INQUIRY_EVPD_SUPPORTED_PAGES
,
378 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:supported_pages");
381 debug_print_int("EVPD length", evpd
->page_length
);
383 for (i
= 0; i
<= evpd
->page_length
; i
++) {
384 debug_print_int("supported EVPD page", evpd
->byte
[i
]);
386 if (evpd
->byte
[i
] != SCSI_INQUIRY_EVPD_BLOCK_LIMITS
) {
390 if (!scsi_inquiry(vdev
,
392 SCSI_INQUIRY_EVPD_BLOCK_LIMITS
,
395 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:blocklimits");
398 debug_print_int("max transfer", evpd_bl
->max_transfer
);
399 vdev
->max_transfer
= evpd_bl
->max_transfer
;
403 * The host sg driver will often be unhappy with particularly large
404 * I/Os that exceed the block iovec limits. Let's enforce something
405 * reasonable, despite what the device configuration tells us.
408 vdev
->max_transfer
= MIN_NON_ZERO(VIRTIO_SCSI_MAX_SECTORS
,
411 if (!scsi_read_capacity(vdev
, data
, data_size
)) {
412 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:read_capacity");
414 scsi_parse_capacity_report(data
, &vdev
->scsi_last_block
,
415 (uint32_t *) &vdev
->scsi_block_size
);