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