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];
23 static inline void vs_assert(bool term
, const char **msgs
)
30 sclp_print(msgs
[i
++]);
36 static void virtio_scsi_verify_response(VirtioScsiCmdResp
*resp
,
40 title
, ": response ", virtio_scsi_response_msg(resp
), 0
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
)
49 scsi_sense_response(resp
->sense
) == 0x70 ? ", sure" : "?",
53 vs_assert(resp
->response
== VIRTIO_SCSI_S_OK
, mr
);
54 vs_assert(resp
->status
== CDB_STATUS_GOOD
, ms
);
57 static void prepare_request(VDev
*vdev
, const void *cdb
, int cdb_size
,
58 void *data
, uint32_t data_size
)
60 const ScsiDevice
*sdev
= vdev
->scsi_device
;
62 memset(&req
, 0, sizeof(req
));
63 req
.lun
= make_lun(sdev
->channel
, sdev
->target
, sdev
->lun
);
64 memcpy(&req
.cdb
, cdb
, cdb_size
);
66 memset(&resp
, 0, sizeof(resp
));
67 resp
.status
= 0xff; /* set invalid */
68 resp
.response
= 0xff; /* */
70 if (data
&& data_size
) {
71 memset(data
, 0, data_size
);
75 static inline void vs_io_assert(bool term
, const char *msg
)
78 virtio_scsi_verify_response(&resp
, msg
);
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
)
86 prepare_request(vdev
, cdb
, cdb_size
, data
, data_size
);
87 vs_io_assert(virtio_run(vdev
, VR_REQUEST
, cmd
) == 0, title
);
90 /* SCSI protocol implementation routines */
92 static bool scsi_inquiry(VDev
*vdev
, void *data
, uint32_t data_size
)
94 ScsiCdbInquiry cdb
= {
96 .alloc_len
= data_size
< 65535 ? data_size
: 65535,
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
},
104 vs_run("inquiry", inquiry
, vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
106 return virtio_scsi_response_ok(&resp
);
109 static bool scsi_test_unit_ready(VDev
*vdev
)
111 ScsiCdbTestUnitReady cdb
= {
114 VirtioCmd test_unit_ready
[] = {
115 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
116 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
},
119 prepare_request(vdev
, &cdb
, sizeof(cdb
), 0, 0);
120 virtio_run(vdev
, VR_REQUEST
, test_unit_ready
); /* ignore errors here */
122 return virtio_scsi_response_ok(&resp
);
125 static bool scsi_report_luns(VDev
*vdev
, void *data
, uint32_t data_size
)
127 ScsiCdbReportLuns cdb
= {
129 .select_report
= 0x02, /* REPORT ALL */
130 .alloc_len
= data_size
,
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
},
138 vs_run("report luns", report_luns
,
139 vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
141 return virtio_scsi_response_ok(&resp
);
144 static bool scsi_read_10(VDev
*vdev
,
145 ulong sector
, int sectors
, void *data
)
147 int f
= vdev
->blk_factor
;
148 unsigned int data_size
= sectors
* virtio_get_block_size() * f
;
149 ScsiCdbRead10 cdb
= {
152 .xfer_length
= sectors
* f
,
154 VirtioCmd read_10
[] = {
155 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
156 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
157 { data
, data_size
* f
, VRING_DESC_F_WRITE
},
160 debug_print_int("read_10 sector", sector
);
161 debug_print_int("read_10 sectors", sectors
);
163 vs_run("read(10)", read_10
, vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
165 return virtio_scsi_response_ok(&resp
);
168 static bool scsi_read_capacity(VDev
*vdev
,
169 void *data
, uint32_t data_size
)
171 ScsiCdbReadCapacity16 cdb
= {
172 .command
= 0x9e, /* SERVICE_ACTION_IN_16 */
173 .service_action
= 0x10, /* SA_READ_CAPACITY */
174 .alloc_len
= data_size
,
176 VirtioCmd read_capacity_16
[] = {
177 { &req
, sizeof(req
), VRING_DESC_F_NEXT
},
178 { &resp
, sizeof(resp
), VRING_DESC_F_WRITE
| VRING_DESC_F_NEXT
},
179 { data
, data_size
, VRING_DESC_F_WRITE
},
182 vs_run("read capacity", read_capacity_16
,
183 vdev
, &cdb
, sizeof(cdb
), data
, data_size
);
185 return virtio_scsi_response_ok(&resp
);
188 /* virtio-scsi routines */
190 static void virtio_scsi_locate_device(VDev
*vdev
)
192 const uint16_t channel
= 0; /* again, it's what QEMU does */
194 static uint8_t data
[16 + 8 * 63];
195 ScsiLunReport
*r
= (void *) data
;
196 ScsiDevice
*sdev
= vdev
->scsi_device
;
199 /* QEMU has hardcoded channel #0 in many places.
200 * If this hardcoded value is ever changed, we'll need to add code for
201 * vdev->config.scsi.max_channel != 0 here.
203 debug_print_int("config.scsi.max_channel", vdev
->config
.scsi
.max_channel
);
204 debug_print_int("config.scsi.max_target ", vdev
->config
.scsi
.max_target
);
205 debug_print_int("config.scsi.max_lun ", vdev
->config
.scsi
.max_lun
);
207 for (target
= 0; target
<= vdev
->config
.scsi
.max_target
; target
++) {
208 sdev
->channel
= channel
;
209 sdev
->target
= target
; /* sdev->lun will be 0 here */
210 if (!scsi_report_luns(vdev
, data
, sizeof(data
))) {
211 if (resp
.response
== VIRTIO_SCSI_S_BAD_TARGET
) {
214 print_int("target", target
);
215 virtio_scsi_verify_response(&resp
, "SCSI cannot report LUNs");
217 if (r
->lun_list_len
== 0) {
218 print_int("no LUNs for target", target
);
221 luns
= r
->lun_list_len
/ 8;
222 debug_print_int("LUNs reported", luns
);
224 /* There is no ",lun=#" arg for -device or ",lun=0" given.
225 * Hence, the only LUN reported.
228 sdev
->lun
= r
->lun
[0].v16
[0]; /* it's returned this way */
229 debug_print_int("Have to use LUN", sdev
->lun
);
230 return; /* we have to use this device */
232 for (i
= 0; i
< luns
; i
++) {
234 /* Look for non-zero LUN - we have where to choose from */
235 sdev
->lun
= r
->lun
[i
].v16
[0];
236 debug_print_int("Will use LUN", sdev
->lun
);
237 return; /* we have found a device */
241 panic("\n! Cannot locate virtio-scsi device !\n");
244 int virtio_scsi_read_many(VDev
*vdev
,
245 ulong sector
, void *load_addr
, int sec_num
)
247 if (!scsi_read_10(vdev
, sector
, sec_num
, load_addr
)) {
248 virtio_scsi_verify_response(&resp
, "virtio-scsi:read_many");
254 static bool virtio_scsi_inquiry_response_is_cdrom(void *data
)
256 const ScsiInquiryStd
*response
= data
;
257 const int resp_data_fmt
= response
->b3
& 0x0f;
260 IPL_check(resp_data_fmt
== 2, "Wrong INQUIRY response format");
261 if (resp_data_fmt
!= 2) {
262 return false; /* cannot decode */
265 if ((response
->peripheral_qdt
& 0x1f) == SCSI_INQ_RDT_CDROM
) {
269 for (i
= 0; i
< sizeof(response
->prod_id
); i
++) {
270 if (response
->prod_id
[i
] != QEMU_CDROM_SIGNATURE
[i
]) {
277 static void scsi_parse_capacity_report(void *data
,
278 uint64_t *last_lba
, uint32_t *lb_len
)
280 ScsiReadCapacity16Data
*p
= data
;
283 *last_lba
= p
->ret_lba
;
291 void virtio_scsi_setup(VDev
*vdev
)
293 int retry_test_unit_ready
= 3;
295 uint32_t data_size
= sizeof(data
);
297 vdev
->scsi_device
= &default_scsi_device
;
298 virtio_scsi_locate_device(vdev
);
300 /* We have to "ping" the device before it becomes readable */
301 while (!scsi_test_unit_ready(vdev
)) {
303 if (!virtio_scsi_response_ok(&resp
)) {
304 uint8_t code
= resp
.sense
[0] & SCSI_SENSE_CODE_MASK
;
305 uint8_t sense_key
= resp
.sense
[2] & SCSI_SENSE_KEY_MASK
;
307 IPL_assert(resp
.sense_len
!= 0, "virtio-scsi:setup: no SENSE data");
309 IPL_assert(retry_test_unit_ready
&& code
== 0x70 &&
310 sense_key
== SCSI_SENSE_KEY_UNIT_ATTENTION
,
311 "virtio-scsi:setup: cannot retry");
313 /* retry on CHECK_CONDITION/UNIT_ATTENTION as it
314 * may not designate a real error, but it may be
315 * a result of device reset, etc.
317 retry_test_unit_ready
--;
322 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup");
325 /* read and cache SCSI INQUIRY response */
326 if (!scsi_inquiry(vdev
, scsi_inquiry_std_response
,
327 sizeof(scsi_inquiry_std_response
))) {
328 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:inquiry");
331 if (virtio_scsi_inquiry_response_is_cdrom(scsi_inquiry_std_response
)) {
332 sclp_print("SCSI CD-ROM detected.\n");
333 vdev
->is_cdrom
= true;
334 vdev
->scsi_block_size
= VIRTIO_ISO_BLOCK_SIZE
;
337 if (!scsi_read_capacity(vdev
, data
, data_size
)) {
338 virtio_scsi_verify_response(&resp
, "virtio-scsi:setup:read_capacity");
340 scsi_parse_capacity_report(data
, &vdev
->scsi_last_block
,
341 (uint32_t *) &vdev
->scsi_block_size
);