virtio-scsi: work around bug in old BIOSes
[qemu.git] / hw / scsi / virtio-scsi.c
1 /*
2 * Virtio SCSI HBA
3 *
4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
6 *
7 * Authors:
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
16 #include "hw/virtio/virtio-scsi.h"
17 #include "qemu/error-report.h"
18 #include "qemu/iov.h"
19 #include "sysemu/block-backend.h"
20 #include <hw/scsi/scsi.h>
21 #include <block/scsi.h>
22 #include <hw/virtio/virtio-bus.h>
23 #include "hw/virtio/virtio-access.h"
24 #include "migration/migration.h"
25
26 static inline int virtio_scsi_get_lun(uint8_t *lun)
27 {
28 return ((lun[2] << 8) | lun[3]) & 0x3FFF;
29 }
30
31 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
32 {
33 if (lun[0] != 1) {
34 return NULL;
35 }
36 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
37 return NULL;
38 }
39 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
40 }
41
42 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq)
43 {
44 VirtIOSCSIReq *req;
45 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
46 const size_t zero_skip = offsetof(VirtIOSCSIReq, elem)
47 + sizeof(VirtQueueElement);
48
49 req = g_slice_alloc(sizeof(*req) + vs->cdb_size);
50 req->vq = vq;
51 req->dev = s;
52 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory);
53 qemu_iovec_init(&req->resp_iov, 1);
54 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip);
55 return req;
56 }
57
58 void virtio_scsi_free_req(VirtIOSCSIReq *req)
59 {
60 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)req->dev;
61
62 qemu_iovec_destroy(&req->resp_iov);
63 qemu_sglist_destroy(&req->qsgl);
64 g_slice_free1(sizeof(*req) + vs->cdb_size, req);
65 }
66
67 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
68 {
69 VirtIOSCSI *s = req->dev;
70 VirtQueue *vq = req->vq;
71 VirtIODevice *vdev = VIRTIO_DEVICE(s);
72
73 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
74 if (req->vring) {
75 assert(req->vq == NULL);
76 virtio_scsi_vring_push_notify(req);
77 } else {
78 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
79 virtio_notify(vdev, vq);
80 }
81
82 if (req->sreq) {
83 req->sreq->hba_private = NULL;
84 scsi_req_unref(req->sreq);
85 }
86 virtio_scsi_free_req(req);
87 }
88
89 static void virtio_scsi_bad_req(void)
90 {
91 error_report("wrong size for virtio-scsi headers");
92 exit(1);
93 }
94
95 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
96 hwaddr *addr, int num, size_t skip)
97 {
98 QEMUSGList *qsgl = &req->qsgl;
99 size_t copied = 0;
100
101 while (num) {
102 if (skip >= iov->iov_len) {
103 skip -= iov->iov_len;
104 } else {
105 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
106 copied += iov->iov_len - skip;
107 skip = 0;
108 }
109 iov++;
110 addr++;
111 num--;
112 }
113
114 assert(skip == 0);
115 return copied;
116 }
117
118 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
119 unsigned req_size, unsigned resp_size)
120 {
121 VirtIODevice *vdev = (VirtIODevice *) req->dev;
122 size_t in_size, out_size;
123
124 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
125 &req->req, req_size) < req_size) {
126 return -EINVAL;
127 }
128
129 if (qemu_iovec_concat_iov(&req->resp_iov,
130 req->elem.in_sg, req->elem.in_num, 0,
131 resp_size) < resp_size) {
132 return -EINVAL;
133 }
134
135 req->resp_size = resp_size;
136
137 /* Old BIOSes left some padding by mistake after the req_size/resp_size.
138 * As a workaround, always consider the first buffer as the virtio-scsi
139 * request/response, making the payload start at the second element
140 * of the iovec.
141 *
142 * The actual length of the response header, stored in req->resp_size,
143 * does not change.
144 *
145 * TODO: always disable this workaround for virtio 1.0 devices.
146 */
147 if ((vdev->guest_features & VIRTIO_F_ANY_LAYOUT) == 0) {
148 req_size = req->elem.out_sg[0].iov_len;
149 resp_size = req->elem.in_sg[0].iov_len;
150 }
151
152 out_size = qemu_sgl_concat(req, req->elem.out_sg,
153 &req->elem.out_addr[0], req->elem.out_num,
154 req_size);
155 in_size = qemu_sgl_concat(req, req->elem.in_sg,
156 &req->elem.in_addr[0], req->elem.in_num,
157 resp_size);
158
159 if (out_size && in_size) {
160 return -ENOTSUP;
161 }
162
163 if (out_size) {
164 req->mode = SCSI_XFER_TO_DEV;
165 } else if (in_size) {
166 req->mode = SCSI_XFER_FROM_DEV;
167 }
168
169 return 0;
170 }
171
172 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
173 {
174 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq);
175 if (!virtqueue_pop(vq, &req->elem)) {
176 virtio_scsi_free_req(req);
177 return NULL;
178 }
179 return req;
180 }
181
182 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
183 {
184 VirtIOSCSIReq *req = sreq->hba_private;
185 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev);
186 uint32_t n = virtio_queue_get_id(req->vq) - 2;
187
188 assert(n < vs->conf.num_queues);
189 qemu_put_be32s(f, &n);
190 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
191 }
192
193 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
194 {
195 SCSIBus *bus = sreq->bus;
196 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
197 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
198 VirtIOSCSIReq *req;
199 uint32_t n;
200
201 qemu_get_be32s(f, &n);
202 assert(n < vs->conf.num_queues);
203 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]);
204 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
205 /* TODO: add a way for SCSIBusInfo's load_request to fail,
206 * and fail migration instead of asserting here.
207 * When we do, we might be able to re-enable NDEBUG below.
208 */
209 #ifdef NDEBUG
210 #error building with NDEBUG is not supported
211 #endif
212 assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg));
213 assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg));
214
215 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
216 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
217 error_report("invalid SCSI request migration data");
218 exit(1);
219 }
220
221 scsi_req_ref(sreq);
222 req->sreq = sreq;
223 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
224 assert(req->sreq->cmd.mode == req->mode);
225 }
226 return req;
227 }
228
229 typedef struct {
230 Notifier notifier;
231 VirtIOSCSIReq *tmf_req;
232 } VirtIOSCSICancelNotifier;
233
234 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data)
235 {
236 VirtIOSCSICancelNotifier *n = container_of(notifier,
237 VirtIOSCSICancelNotifier,
238 notifier);
239
240 if (--n->tmf_req->remaining == 0) {
241 virtio_scsi_complete_req(n->tmf_req);
242 }
243 g_slice_free(VirtIOSCSICancelNotifier, n);
244 }
245
246 /* Return 0 if the request is ready to be completed and return to guest;
247 * -EINPROGRESS if the request is submitted and will be completed later, in the
248 * case of async cancellation. */
249 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
250 {
251 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun);
252 SCSIRequest *r, *next;
253 BusChild *kid;
254 int target;
255 int ret = 0;
256
257 if (s->dataplane_started && blk_get_aio_context(d->conf.blk) != s->ctx) {
258 aio_context_acquire(s->ctx);
259 blk_set_aio_context(d->conf.blk, s->ctx);
260 aio_context_release(s->ctx);
261 }
262 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
263 req->resp.tmf.response = VIRTIO_SCSI_S_OK;
264
265 virtio_tswap32s(VIRTIO_DEVICE(s), &req->req.tmf.subtype);
266 switch (req->req.tmf.subtype) {
267 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
268 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
269 if (!d) {
270 goto fail;
271 }
272 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
273 goto incorrect_lun;
274 }
275 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
276 VirtIOSCSIReq *cmd_req = r->hba_private;
277 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) {
278 break;
279 }
280 }
281 if (r) {
282 /*
283 * Assert that the request has not been completed yet, we
284 * check for it in the loop above.
285 */
286 assert(r->hba_private);
287 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
288 /* "If the specified command is present in the task set, then
289 * return a service response set to FUNCTION SUCCEEDED".
290 */
291 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
292 } else {
293 VirtIOSCSICancelNotifier *notifier;
294
295 req->remaining = 1;
296 notifier = g_slice_new(VirtIOSCSICancelNotifier);
297 notifier->tmf_req = req;
298 notifier->notifier.notify = virtio_scsi_cancel_notify;
299 scsi_req_cancel_async(r, &notifier->notifier);
300 ret = -EINPROGRESS;
301 }
302 }
303 break;
304
305 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
306 if (!d) {
307 goto fail;
308 }
309 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
310 goto incorrect_lun;
311 }
312 s->resetting++;
313 qdev_reset_all(&d->qdev);
314 s->resetting--;
315 break;
316
317 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
318 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
319 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
320 if (!d) {
321 goto fail;
322 }
323 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
324 goto incorrect_lun;
325 }
326
327 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns.
328 * This way, if the bus starts calling back to the notifiers
329 * even before we finish the loop, virtio_scsi_cancel_notify
330 * will not complete the TMF too early.
331 */
332 req->remaining = 1;
333 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
334 if (r->hba_private) {
335 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
336 /* "If there is any command present in the task set, then
337 * return a service response set to FUNCTION SUCCEEDED".
338 */
339 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
340 break;
341 } else {
342 VirtIOSCSICancelNotifier *notifier;
343
344 req->remaining++;
345 notifier = g_slice_new(VirtIOSCSICancelNotifier);
346 notifier->notifier.notify = virtio_scsi_cancel_notify;
347 notifier->tmf_req = req;
348 scsi_req_cancel_async(r, &notifier->notifier);
349 }
350 }
351 }
352 if (--req->remaining > 0) {
353 ret = -EINPROGRESS;
354 }
355 break;
356
357 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
358 target = req->req.tmf.lun[1];
359 s->resetting++;
360 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
361 d = DO_UPCAST(SCSIDevice, qdev, kid->child);
362 if (d->channel == 0 && d->id == target) {
363 qdev_reset_all(&d->qdev);
364 }
365 }
366 s->resetting--;
367 break;
368
369 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
370 default:
371 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
372 break;
373 }
374
375 return ret;
376
377 incorrect_lun:
378 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
379 return ret;
380
381 fail:
382 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
383 return ret;
384 }
385
386 void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
387 {
388 VirtIODevice *vdev = (VirtIODevice *)s;
389 uint32_t type;
390 int r = 0;
391
392 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
393 &type, sizeof(type)) < sizeof(type)) {
394 virtio_scsi_bad_req();
395 return;
396 }
397
398 virtio_tswap32s(vdev, &type);
399 if (type == VIRTIO_SCSI_T_TMF) {
400 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
401 sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
402 virtio_scsi_bad_req();
403 } else {
404 r = virtio_scsi_do_tmf(s, req);
405 }
406
407 } else if (type == VIRTIO_SCSI_T_AN_QUERY ||
408 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
409 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
410 sizeof(VirtIOSCSICtrlANResp)) < 0) {
411 virtio_scsi_bad_req();
412 } else {
413 req->resp.an.event_actual = 0;
414 req->resp.an.response = VIRTIO_SCSI_S_OK;
415 }
416 }
417 if (r == 0) {
418 virtio_scsi_complete_req(req);
419 } else {
420 assert(r == -EINPROGRESS);
421 }
422 }
423
424 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
425 {
426 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
427 VirtIOSCSIReq *req;
428
429 if (s->ctx && !s->dataplane_disabled) {
430 virtio_scsi_dataplane_start(s);
431 return;
432 }
433 while ((req = virtio_scsi_pop_req(s, vq))) {
434 virtio_scsi_handle_ctrl_req(s, req);
435 }
436 }
437
438 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
439 {
440 /* Sense data is not in req->resp and is copied separately
441 * in virtio_scsi_command_complete.
442 */
443 req->resp_size = sizeof(VirtIOSCSICmdResp);
444 virtio_scsi_complete_req(req);
445 }
446
447 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
448 size_t resid)
449 {
450 VirtIOSCSIReq *req = r->hba_private;
451 uint8_t sense[SCSI_SENSE_BUF_SIZE];
452 uint32_t sense_len;
453 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
454
455 if (r->io_canceled) {
456 return;
457 }
458
459 req->resp.cmd.response = VIRTIO_SCSI_S_OK;
460 req->resp.cmd.status = status;
461 if (req->resp.cmd.status == GOOD) {
462 req->resp.cmd.resid = virtio_tswap32(vdev, resid);
463 } else {
464 req->resp.cmd.resid = 0;
465 sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
466 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
467 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
468 sense, sense_len);
469 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
470 }
471 virtio_scsi_complete_cmd_req(req);
472 }
473
474 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
475 uint8_t *buf, void *hba_private)
476 {
477 VirtIOSCSIReq *req = hba_private;
478
479 if (cmd->len == 0) {
480 cmd->len = MIN(VIRTIO_SCSI_CDB_SIZE, SCSI_CMD_BUF_SIZE);
481 memcpy(cmd->buf, buf, cmd->len);
482 }
483
484 /* Extract the direction and mode directly from the request, for
485 * host device passthrough.
486 */
487 cmd->xfer = req->qsgl.size;
488 cmd->mode = req->mode;
489 return 0;
490 }
491
492 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
493 {
494 VirtIOSCSIReq *req = r->hba_private;
495
496 return &req->qsgl;
497 }
498
499 static void virtio_scsi_request_cancelled(SCSIRequest *r)
500 {
501 VirtIOSCSIReq *req = r->hba_private;
502
503 if (!req) {
504 return;
505 }
506 if (req->dev->resetting) {
507 req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
508 } else {
509 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
510 }
511 virtio_scsi_complete_cmd_req(req);
512 }
513
514 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
515 {
516 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
517 virtio_scsi_complete_cmd_req(req);
518 }
519
520 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
521 {
522 VirtIOSCSICommon *vs = &s->parent_obj;
523 SCSIDevice *d;
524 int rc;
525
526 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
527 sizeof(VirtIOSCSICmdResp) + vs->sense_size);
528 if (rc < 0) {
529 if (rc == -ENOTSUP) {
530 virtio_scsi_fail_cmd_req(req);
531 } else {
532 virtio_scsi_bad_req();
533 }
534 return false;
535 }
536
537 d = virtio_scsi_device_find(s, req->req.cmd.lun);
538 if (!d) {
539 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
540 virtio_scsi_complete_cmd_req(req);
541 return false;
542 }
543 if (s->dataplane_started && blk_get_aio_context(d->conf.blk) != s->ctx) {
544 aio_context_acquire(s->ctx);
545 blk_set_aio_context(d->conf.blk, s->ctx);
546 aio_context_release(s->ctx);
547 }
548 req->sreq = scsi_req_new(d, req->req.cmd.tag,
549 virtio_scsi_get_lun(req->req.cmd.lun),
550 req->req.cdb, req);
551
552 if (req->sreq->cmd.mode != SCSI_XFER_NONE
553 && (req->sreq->cmd.mode != req->mode ||
554 req->sreq->cmd.xfer > req->qsgl.size)) {
555 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
556 virtio_scsi_complete_cmd_req(req);
557 return false;
558 }
559 scsi_req_ref(req->sreq);
560 blk_io_plug(d->conf.blk);
561 return true;
562 }
563
564 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
565 {
566 SCSIRequest *sreq = req->sreq;
567 if (scsi_req_enqueue(sreq)) {
568 scsi_req_continue(sreq);
569 }
570 blk_io_unplug(sreq->dev->conf.blk);
571 scsi_req_unref(sreq);
572 }
573
574 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
575 {
576 /* use non-QOM casts in the data path */
577 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
578 VirtIOSCSIReq *req, *next;
579 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
580
581 if (s->ctx && !s->dataplane_disabled) {
582 virtio_scsi_dataplane_start(s);
583 return;
584 }
585 while ((req = virtio_scsi_pop_req(s, vq))) {
586 if (virtio_scsi_handle_cmd_req_prepare(s, req)) {
587 QTAILQ_INSERT_TAIL(&reqs, req, next);
588 }
589 }
590
591 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
592 virtio_scsi_handle_cmd_req_submit(s, req);
593 }
594 }
595
596 static void virtio_scsi_get_config(VirtIODevice *vdev,
597 uint8_t *config)
598 {
599 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
600 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
601
602 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
603 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2);
604 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
605 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
606 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
607 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
608 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
609 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
610 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
611 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
612 }
613
614 static void virtio_scsi_set_config(VirtIODevice *vdev,
615 const uint8_t *config)
616 {
617 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
618 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
619
620 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
621 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
622 error_report("bad data written to virtio-scsi configuration space");
623 exit(1);
624 }
625
626 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
627 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
628 }
629
630 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
631 uint32_t requested_features)
632 {
633 return requested_features;
634 }
635
636 static void virtio_scsi_reset(VirtIODevice *vdev)
637 {
638 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
639 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
640
641 if (s->ctx) {
642 virtio_scsi_dataplane_stop(s);
643 }
644 s->resetting++;
645 qbus_reset_all(&s->bus.qbus);
646 s->resetting--;
647
648 vs->sense_size = VIRTIO_SCSI_SENSE_SIZE;
649 vs->cdb_size = VIRTIO_SCSI_CDB_SIZE;
650 s->events_dropped = false;
651 }
652
653 /* The device does not have anything to save beyond the virtio data.
654 * Request data is saved with callbacks from SCSI devices.
655 */
656 static void virtio_scsi_save(QEMUFile *f, void *opaque)
657 {
658 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
659 virtio_save(vdev, f);
660 }
661
662 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
663 {
664 VirtIODevice *vdev = VIRTIO_DEVICE(opaque);
665 int ret;
666
667 ret = virtio_load(vdev, f, version_id);
668 if (ret) {
669 return ret;
670 }
671 return 0;
672 }
673
674 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
675 uint32_t event, uint32_t reason)
676 {
677 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
678 VirtIOSCSIReq *req;
679 VirtIOSCSIEvent *evt;
680 VirtIODevice *vdev = VIRTIO_DEVICE(s);
681
682 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
683 return;
684 }
685
686 if (s->dataplane_started) {
687 assert(s->ctx);
688 aio_context_acquire(s->ctx);
689 }
690
691 if (s->dataplane_started) {
692 req = virtio_scsi_pop_req_vring(s, s->event_vring);
693 } else {
694 req = virtio_scsi_pop_req(s, vs->event_vq);
695 }
696 if (!req) {
697 s->events_dropped = true;
698 goto out;
699 }
700
701 if (s->events_dropped) {
702 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
703 s->events_dropped = false;
704 }
705
706 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
707 virtio_scsi_bad_req();
708 }
709
710 evt = &req->resp.event;
711 memset(evt, 0, sizeof(VirtIOSCSIEvent));
712 evt->event = virtio_tswap32(vdev, event);
713 evt->reason = virtio_tswap32(vdev, reason);
714 if (!dev) {
715 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED);
716 } else {
717 evt->lun[0] = 1;
718 evt->lun[1] = dev->id;
719
720 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
721 if (dev->lun >= 256) {
722 evt->lun[2] = (dev->lun >> 8) | 0x40;
723 }
724 evt->lun[3] = dev->lun & 0xFF;
725 }
726 virtio_scsi_complete_req(req);
727 out:
728 if (s->dataplane_started) {
729 aio_context_release(s->ctx);
730 }
731 }
732
733 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
734 {
735 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
736
737 if (s->ctx && !s->dataplane_disabled) {
738 virtio_scsi_dataplane_start(s);
739 return;
740 }
741 if (s->events_dropped) {
742 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
743 }
744 }
745
746 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
747 {
748 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
749 VirtIODevice *vdev = VIRTIO_DEVICE(s);
750
751 if (((vdev->guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) &&
752 dev->type != TYPE_ROM) {
753 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
754 sense.asc | (sense.ascq << 8));
755 }
756 }
757
758 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
759 Error **errp)
760 {
761 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
762 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
763 SCSIDevice *sd = SCSI_DEVICE(dev);
764
765 if (s->ctx && !s->dataplane_disabled) {
766 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) {
767 return;
768 }
769 blk_op_block_all(sd->conf.blk, s->blocker);
770 }
771
772 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
773 virtio_scsi_push_event(s, sd,
774 VIRTIO_SCSI_T_TRANSPORT_RESET,
775 VIRTIO_SCSI_EVT_RESET_RESCAN);
776 }
777 }
778
779 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
780 Error **errp)
781 {
782 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
783 VirtIOSCSI *s = VIRTIO_SCSI(vdev);
784 SCSIDevice *sd = SCSI_DEVICE(dev);
785
786 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
787 virtio_scsi_push_event(s, sd,
788 VIRTIO_SCSI_T_TRANSPORT_RESET,
789 VIRTIO_SCSI_EVT_RESET_REMOVED);
790 }
791
792 if (s->ctx) {
793 blk_op_unblock_all(sd->conf.blk, s->blocker);
794 }
795 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
796 }
797
798 static struct SCSIBusInfo virtio_scsi_scsi_info = {
799 .tcq = true,
800 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
801 .max_target = VIRTIO_SCSI_MAX_TARGET,
802 .max_lun = VIRTIO_SCSI_MAX_LUN,
803
804 .complete = virtio_scsi_command_complete,
805 .cancel = virtio_scsi_request_cancelled,
806 .change = virtio_scsi_change,
807 .parse_cdb = virtio_scsi_parse_cdb,
808 .get_sg_list = virtio_scsi_get_sg_list,
809 .save_request = virtio_scsi_save_request,
810 .load_request = virtio_scsi_load_request,
811 };
812
813 void virtio_scsi_common_realize(DeviceState *dev, Error **errp,
814 HandleOutput ctrl, HandleOutput evt,
815 HandleOutput cmd)
816 {
817 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
818 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
819 int i;
820
821 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI,
822 sizeof(VirtIOSCSIConfig));
823
824 if (s->conf.num_queues == 0 ||
825 s->conf.num_queues > VIRTIO_PCI_QUEUE_MAX - 2) {
826 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
827 "must be a positive integer less than %d.",
828 s->conf.num_queues, VIRTIO_PCI_QUEUE_MAX - 2);
829 virtio_cleanup(vdev);
830 return;
831 }
832 s->cmd_vqs = g_malloc0(s->conf.num_queues * sizeof(VirtQueue *));
833 s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
834 s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
835
836 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
837 ctrl);
838 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
839 evt);
840 for (i = 0; i < s->conf.num_queues; i++) {
841 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE,
842 cmd);
843 }
844
845 if (s->conf.iothread) {
846 virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread);
847 }
848 }
849
850 /* Disable dataplane thread during live migration since it does not
851 * update the dirty memory bitmap yet.
852 */
853 static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data)
854 {
855 VirtIOSCSI *s = container_of(notifier, VirtIOSCSI,
856 migration_state_notifier);
857 MigrationState *mig = data;
858
859 if (migration_in_setup(mig)) {
860 if (!s->dataplane_started) {
861 return;
862 }
863 virtio_scsi_dataplane_stop(s);
864 s->dataplane_disabled = true;
865 } else if (migration_has_finished(mig) ||
866 migration_has_failed(mig)) {
867 if (s->dataplane_started) {
868 return;
869 }
870 blk_drain_all(); /* complete in-flight non-dataplane requests */
871 s->dataplane_disabled = false;
872 }
873 }
874
875 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
876 {
877 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
878 VirtIOSCSI *s = VIRTIO_SCSI(dev);
879 static int virtio_scsi_id;
880 Error *err = NULL;
881
882 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl,
883 virtio_scsi_handle_event,
884 virtio_scsi_handle_cmd);
885 if (err != NULL) {
886 error_propagate(errp, err);
887 return;
888 }
889
890 scsi_bus_new(&s->bus, sizeof(s->bus), dev,
891 &virtio_scsi_scsi_info, vdev->bus_name);
892 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
893 qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort);
894
895 if (!dev->hotplugged) {
896 scsi_bus_legacy_handle_cmdline(&s->bus, &err);
897 if (err != NULL) {
898 error_propagate(errp, err);
899 return;
900 }
901 }
902
903 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
904 virtio_scsi_save, virtio_scsi_load, s);
905 s->migration_state_notifier.notify = virtio_scsi_migration_state_changed;
906 add_migration_state_change_notifier(&s->migration_state_notifier);
907 }
908
909 static void virtio_scsi_instance_init(Object *obj)
910 {
911 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(obj);
912
913 object_property_add_link(obj, "iothread", TYPE_IOTHREAD,
914 (Object **)&vs->conf.iothread,
915 qdev_prop_allow_set_link_before_realize,
916 OBJ_PROP_LINK_UNREF_ON_RELEASE, &error_abort);
917 }
918
919 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp)
920 {
921 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
922 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
923
924 g_free(vs->cmd_vqs);
925 virtio_cleanup(vdev);
926 }
927
928 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp)
929 {
930 VirtIOSCSI *s = VIRTIO_SCSI(dev);
931
932 unregister_savevm(dev, "virtio-scsi", s);
933 remove_migration_state_change_notifier(&s->migration_state_notifier);
934
935 virtio_scsi_common_unrealize(dev, errp);
936 }
937
938 static Property virtio_scsi_properties[] = {
939 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf),
940 DEFINE_PROP_END_OF_LIST(),
941 };
942
943 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data)
944 {
945 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
946 DeviceClass *dc = DEVICE_CLASS(klass);
947
948 vdc->get_config = virtio_scsi_get_config;
949 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
950 }
951
952 static void virtio_scsi_class_init(ObjectClass *klass, void *data)
953 {
954 DeviceClass *dc = DEVICE_CLASS(klass);
955 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
956 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
957
958 dc->props = virtio_scsi_properties;
959 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
960 vdc->realize = virtio_scsi_device_realize;
961 vdc->unrealize = virtio_scsi_device_unrealize;
962 vdc->set_config = virtio_scsi_set_config;
963 vdc->get_features = virtio_scsi_get_features;
964 vdc->reset = virtio_scsi_reset;
965 hc->plug = virtio_scsi_hotplug;
966 hc->unplug = virtio_scsi_hotunplug;
967 }
968
969 static const TypeInfo virtio_scsi_common_info = {
970 .name = TYPE_VIRTIO_SCSI_COMMON,
971 .parent = TYPE_VIRTIO_DEVICE,
972 .instance_size = sizeof(VirtIOSCSICommon),
973 .abstract = true,
974 .class_init = virtio_scsi_common_class_init,
975 };
976
977 static const TypeInfo virtio_scsi_info = {
978 .name = TYPE_VIRTIO_SCSI,
979 .parent = TYPE_VIRTIO_SCSI_COMMON,
980 .instance_size = sizeof(VirtIOSCSI),
981 .instance_init = virtio_scsi_instance_init,
982 .class_init = virtio_scsi_class_init,
983 .interfaces = (InterfaceInfo[]) {
984 { TYPE_HOTPLUG_HANDLER },
985 { }
986 }
987 };
988
989 static void virtio_register_types(void)
990 {
991 type_register_static(&virtio_scsi_common_info);
992 type_register_static(&virtio_scsi_info);
993 }
994
995 type_init(virtio_register_types)