xref: /openbmc/qemu/hw/scsi/virtio-scsi.c (revision 072470dfae125f5622e2250ebd1daf626d4023b7)
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 "migration/qemu-file-types.h"
21 #include "qemu/defer-call.h"
22 #include "qemu/error-report.h"
23 #include "qemu/iov.h"
24 #include "qemu/module.h"
25 #include "system/block-backend.h"
26 #include "system/dma.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/scsi/scsi.h"
29 #include "scsi/constants.h"
30 #include "hw/virtio/iothread-vq-mapping.h"
31 #include "hw/virtio/virtio-bus.h"
32 #include "hw/virtio/virtio-access.h"
33 #include "trace.h"
34 
35 typedef struct VirtIOSCSIReq {
36     /*
37      * Note:
38      * - fields up to resp_iov are initialized by virtio_scsi_init_req;
39      * - fields starting at vring are zeroed by virtio_scsi_init_req.
40      */
41     VirtQueueElement elem;
42 
43     VirtIOSCSI *dev;
44     VirtQueue *vq;
45     QEMUSGList qsgl;
46     QEMUIOVector resp_iov;
47 
48     /* Used for two-stage request submission and TMFs deferred to BH */
49     QTAILQ_ENTRY(VirtIOSCSIReq) next;
50 
51     /* Used for cancellation of request during TMFs. Atomic. */
52     int remaining;
53 
54     SCSIRequest *sreq;
55     size_t resp_size;
56     enum SCSIXferMode mode;
57     union {
58         VirtIOSCSICmdResp     cmd;
59         VirtIOSCSICtrlTMFResp tmf;
60         VirtIOSCSICtrlANResp  an;
61         VirtIOSCSIEvent       event;
62     } resp;
63     union {
64         VirtIOSCSICmdReq      cmd;
65         VirtIOSCSICtrlTMFReq  tmf;
66         VirtIOSCSICtrlANReq   an;
67     } req;
68 } VirtIOSCSIReq;
69 
70 static inline int virtio_scsi_get_lun(uint8_t *lun)
71 {
72     return ((lun[2] << 8) | lun[3]) & 0x3FFF;
73 }
74 
75 static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *lun)
76 {
77     if (lun[0] != 1) {
78         return NULL;
79     }
80     if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
81         return NULL;
82     }
83     return scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
84 }
85 
86 static void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req)
87 {
88     VirtIODevice *vdev = VIRTIO_DEVICE(s);
89     const size_t zero_skip =
90         offsetof(VirtIOSCSIReq, resp_iov) + sizeof(req->resp_iov);
91 
92     req->vq = vq;
93     req->dev = s;
94     qemu_sglist_init(&req->qsgl, DEVICE(s), 8, vdev->dma_as);
95     qemu_iovec_init(&req->resp_iov, 1);
96     memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip);
97 }
98 
99 static void virtio_scsi_free_req(VirtIOSCSIReq *req)
100 {
101     qemu_iovec_destroy(&req->resp_iov);
102     qemu_sglist_destroy(&req->qsgl);
103     g_free(req);
104 }
105 
106 static void virtio_scsi_complete_req(VirtIOSCSIReq *req, QemuMutex *vq_lock)
107 {
108     VirtIOSCSI *s = req->dev;
109     VirtQueue *vq = req->vq;
110     VirtIODevice *vdev = VIRTIO_DEVICE(s);
111 
112     qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size);
113 
114     if (vq_lock) {
115         qemu_mutex_lock(vq_lock);
116     }
117 
118     virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size);
119     if (s->dataplane_started && !s->dataplane_fenced) {
120         virtio_notify_irqfd(vdev, vq);
121     } else {
122         virtio_notify(vdev, vq);
123     }
124 
125     if (vq_lock) {
126         qemu_mutex_unlock(vq_lock);
127     }
128 
129     if (req->sreq) {
130         req->sreq->hba_private = NULL;
131         scsi_req_unref(req->sreq);
132     }
133     virtio_scsi_free_req(req);
134 }
135 
136 static void virtio_scsi_bad_req(VirtIOSCSIReq *req, QemuMutex *vq_lock)
137 {
138     virtio_error(VIRTIO_DEVICE(req->dev), "wrong size for virtio-scsi headers");
139 
140     if (vq_lock) {
141         qemu_mutex_lock(vq_lock);
142     }
143 
144     virtqueue_detach_element(req->vq, &req->elem, 0);
145 
146     if (vq_lock) {
147         qemu_mutex_unlock(vq_lock);
148     }
149 
150     virtio_scsi_free_req(req);
151 }
152 
153 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov,
154                               hwaddr *addr, int num, size_t skip)
155 {
156     QEMUSGList *qsgl = &req->qsgl;
157     size_t copied = 0;
158 
159     while (num) {
160         if (skip >= iov->iov_len) {
161             skip -= iov->iov_len;
162         } else {
163             qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip);
164             copied += iov->iov_len - skip;
165             skip = 0;
166         }
167         iov++;
168         addr++;
169         num--;
170     }
171 
172     assert(skip == 0);
173     return copied;
174 }
175 
176 static int virtio_scsi_parse_req(VirtIOSCSIReq *req,
177                                  unsigned req_size, unsigned resp_size)
178 {
179     VirtIODevice *vdev = (VirtIODevice *) req->dev;
180     size_t in_size, out_size;
181 
182     if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
183                    &req->req, req_size) < req_size) {
184         return -EINVAL;
185     }
186 
187     if (qemu_iovec_concat_iov(&req->resp_iov,
188                               req->elem.in_sg, req->elem.in_num, 0,
189                               resp_size) < resp_size) {
190         return -EINVAL;
191     }
192 
193     req->resp_size = resp_size;
194 
195     /* Old BIOSes left some padding by mistake after the req_size/resp_size.
196      * As a workaround, always consider the first buffer as the virtio-scsi
197      * request/response, making the payload start at the second element
198      * of the iovec.
199      *
200      * The actual length of the response header, stored in req->resp_size,
201      * does not change.
202      *
203      * TODO: always disable this workaround for virtio 1.0 devices.
204      */
205     if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) {
206         if (req->elem.out_num) {
207             req_size = req->elem.out_sg[0].iov_len;
208         }
209         if (req->elem.in_num) {
210             resp_size = req->elem.in_sg[0].iov_len;
211         }
212     }
213 
214     out_size = qemu_sgl_concat(req, req->elem.out_sg,
215                                &req->elem.out_addr[0], req->elem.out_num,
216                                req_size);
217     in_size = qemu_sgl_concat(req, req->elem.in_sg,
218                               &req->elem.in_addr[0], req->elem.in_num,
219                               resp_size);
220 
221     if (out_size && in_size) {
222         return -ENOTSUP;
223     }
224 
225     if (out_size) {
226         req->mode = SCSI_XFER_TO_DEV;
227     } else if (in_size) {
228         req->mode = SCSI_XFER_FROM_DEV;
229     }
230 
231     return 0;
232 }
233 
234 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq, QemuMutex *vq_lock)
235 {
236     VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s;
237     VirtIOSCSIReq *req;
238 
239     if (vq_lock) {
240         qemu_mutex_lock(vq_lock);
241     }
242 
243     req = virtqueue_pop(vq, sizeof(VirtIOSCSIReq) + vs->cdb_size);
244 
245     if (vq_lock) {
246         qemu_mutex_unlock(vq_lock);
247     }
248 
249     if (!req) {
250         return NULL;
251     }
252     virtio_scsi_init_req(s, vq, req);
253     return req;
254 }
255 
256 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
257 {
258     VirtIOSCSIReq *req = sreq->hba_private;
259     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev);
260     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
261     uint32_t n = virtio_get_queue_index(req->vq) - VIRTIO_SCSI_VQ_NUM_FIXED;
262 
263     assert(n < vs->conf.num_queues);
264     qemu_put_be32s(f, &n);
265     qemu_put_virtqueue_element(vdev, f, &req->elem);
266 }
267 
268 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
269 {
270     SCSIBus *bus = sreq->bus;
271     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
272     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
273     VirtIODevice *vdev = VIRTIO_DEVICE(s);
274     VirtIOSCSIReq *req;
275     uint32_t n;
276 
277     qemu_get_be32s(f, &n);
278     assert(n < vs->conf.num_queues);
279     req = qemu_get_virtqueue_element(vdev, f,
280                                      sizeof(VirtIOSCSIReq) + vs->cdb_size);
281     virtio_scsi_init_req(s, vs->cmd_vqs[n], req);
282 
283     if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
284                               sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) {
285         error_report("invalid SCSI request migration data");
286         exit(1);
287     }
288 
289     scsi_req_ref(sreq);
290     req->sreq = sreq;
291     if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
292         assert(req->sreq->cmd.mode == req->mode);
293     }
294     return req;
295 }
296 
297 typedef struct {
298     Notifier        notifier;
299     VirtIOSCSIReq  *tmf_req;
300 } VirtIOSCSICancelNotifier;
301 
302 static void virtio_scsi_tmf_dec_remaining(VirtIOSCSIReq *tmf)
303 {
304     if (qatomic_fetch_dec(&tmf->remaining) == 1) {
305         trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(tmf->req.tmf.lun),
306                                    tmf->req.tmf.tag, tmf->resp.tmf.response);
307 
308         virtio_scsi_complete_req(tmf, &tmf->dev->ctrl_lock);
309     }
310 }
311 
312 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data)
313 {
314     VirtIOSCSICancelNotifier *n = container_of(notifier,
315                                                VirtIOSCSICancelNotifier,
316                                                notifier);
317 
318     virtio_scsi_tmf_dec_remaining(n->tmf_req);
319     g_free(n);
320 }
321 
322 static void virtio_scsi_tmf_cancel_req(VirtIOSCSIReq *tmf, SCSIRequest *r)
323 {
324     VirtIOSCSICancelNotifier *notifier;
325 
326     assert(r->ctx == qemu_get_current_aio_context());
327 
328     /* Decremented in virtio_scsi_cancel_notify() */
329     qatomic_inc(&tmf->remaining);
330 
331     notifier = g_new(VirtIOSCSICancelNotifier, 1);
332     notifier->notifier.notify = virtio_scsi_cancel_notify;
333     notifier->tmf_req = tmf;
334     scsi_req_cancel_async(r, &notifier->notifier);
335 }
336 
337 /* Execute a TMF on the requests in the current AioContext */
338 static void virtio_scsi_do_tmf_aio_context(void *opaque)
339 {
340     AioContext *ctx = qemu_get_current_aio_context();
341     VirtIOSCSIReq *tmf = opaque;
342     VirtIOSCSI *s = tmf->dev;
343     SCSIDevice *d = virtio_scsi_device_get(s, tmf->req.tmf.lun);
344     SCSIRequest *r;
345     bool match_tag;
346     g_autoptr(GList) reqs = NULL;
347 
348     if (!d) {
349         tmf->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
350         virtio_scsi_tmf_dec_remaining(tmf);
351         return;
352     }
353 
354     /*
355      * This function could handle other subtypes that need to be processed in
356      * the request's AioContext in the future, but for now only request
357      * cancelation subtypes are performed here.
358      */
359     switch (tmf->req.tmf.subtype) {
360     case VIRTIO_SCSI_T_TMF_ABORT_TASK:
361         match_tag = true;
362         break;
363     case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
364     case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
365         match_tag = false;
366         break;
367     default:
368         g_assert_not_reached();
369     }
370 
371     WITH_QEMU_LOCK_GUARD(&d->requests_lock) {
372         QTAILQ_FOREACH(r, &d->requests, next) {
373             VirtIOSCSIReq *cmd_req = r->hba_private;
374             assert(cmd_req); /* request has hba_private while enqueued */
375 
376             if (r->ctx != ctx) {
377                 continue;
378             }
379             if (match_tag && cmd_req->req.cmd.tag != tmf->req.tmf.tag) {
380                 continue;
381             }
382             /*
383              * Cannot cancel directly, because scsi_req_dequeue() would deadlock
384              * when attempting to acquire the request_lock a second time. Taking
385              * a reference here is paired with an unref after cancelling below.
386              */
387             scsi_req_ref(r);
388             reqs = g_list_prepend(reqs, r);
389         }
390     }
391 
392     for (GList *elem = g_list_first(reqs); elem; elem = g_list_next(elem)) {
393         virtio_scsi_tmf_cancel_req(tmf, elem->data);
394         scsi_req_unref(elem->data);
395     }
396 
397     /* Incremented by virtio_scsi_do_tmf() */
398     virtio_scsi_tmf_dec_remaining(tmf);
399 
400     object_unref(d);
401 }
402 
403 static void dummy_bh(void *opaque)
404 {
405     /* Do nothing */
406 }
407 
408 /*
409  * Wait for pending virtio_scsi_defer_tmf_to_aio_context() BHs.
410  */
411 static void virtio_scsi_flush_defer_tmf_to_aio_context(VirtIOSCSI *s)
412 {
413     GLOBAL_STATE_CODE();
414 
415     assert(!s->dataplane_started);
416 
417     for (uint32_t i = 0; i < s->parent_obj.conf.num_queues; i++) {
418         AioContext *ctx = s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i];
419 
420         /* Our BH only runs after previously scheduled BHs */
421         aio_wait_bh_oneshot(ctx, dummy_bh, NULL);
422     }
423 }
424 
425 /*
426  * Run the TMF in a specific AioContext, handling only requests in that
427  * AioContext. This is necessary because requests can run in different
428  * AioContext and it is only possible to cancel them from the AioContext where
429  * they are running.
430  */
431 static void virtio_scsi_defer_tmf_to_aio_context(VirtIOSCSIReq *tmf,
432                                                  AioContext *ctx)
433 {
434     /* Decremented in virtio_scsi_do_tmf_aio_context() */
435     qatomic_inc(&tmf->remaining);
436 
437     /* See virtio_scsi_flush_defer_tmf_to_aio_context() cleanup during reset */
438     aio_bh_schedule_oneshot(ctx, virtio_scsi_do_tmf_aio_context, tmf);
439 }
440 
441 /*
442  * Returns the AioContext for a given TMF's tag field or NULL. Note that the
443  * request identified by the tag may have completed by the time you can execute
444  * a BH in the AioContext, so don't assume the request still exists in your BH.
445  */
446 static AioContext *find_aio_context_for_tmf_tag(SCSIDevice *d,
447                                                 VirtIOSCSIReq *tmf)
448 {
449     WITH_QEMU_LOCK_GUARD(&d->requests_lock) {
450         SCSIRequest *r;
451         SCSIRequest *next;
452 
453         QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
454             VirtIOSCSIReq *cmd_req = r->hba_private;
455 
456             /* hba_private is non-NULL while the request is enqueued */
457             assert(cmd_req);
458 
459             if (cmd_req->req.cmd.tag == tmf->req.tmf.tag) {
460                 return r->ctx;
461             }
462         }
463     }
464     return NULL;
465 }
466 
467 /* Return 0 if the request is ready to be completed and return to guest;
468  * -EINPROGRESS if the request is submitted and will be completed later, in the
469  *  case of async cancellation. */
470 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
471 {
472     SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun);
473     SCSIRequest *r, *next;
474     AioContext *ctx;
475     int ret = 0;
476 
477     /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE".  */
478     req->resp.tmf.response = VIRTIO_SCSI_S_OK;
479 
480     /*
481      * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s()
482      * to avoid compiler errors.
483      */
484     req->req.tmf.subtype =
485         virtio_tswap32(VIRTIO_DEVICE(s), req->req.tmf.subtype);
486 
487     trace_virtio_scsi_tmf_req(virtio_scsi_get_lun(req->req.tmf.lun),
488                               req->req.tmf.tag, req->req.tmf.subtype);
489 
490     switch (req->req.tmf.subtype) {
491     case VIRTIO_SCSI_T_TMF_ABORT_TASK: {
492         if (!d) {
493             goto fail;
494         }
495         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
496             goto incorrect_lun;
497         }
498 
499         ctx = find_aio_context_for_tmf_tag(d, req);
500         if (ctx) {
501             virtio_scsi_defer_tmf_to_aio_context(req, ctx);
502             ret = -EINPROGRESS;
503         }
504         break;
505     }
506 
507     case VIRTIO_SCSI_T_TMF_QUERY_TASK:
508         if (!d) {
509             goto fail;
510         }
511         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
512             goto incorrect_lun;
513         }
514 
515         WITH_QEMU_LOCK_GUARD(&d->requests_lock) {
516             QTAILQ_FOREACH(r, &d->requests, next) {
517                 VirtIOSCSIReq *cmd_req = r->hba_private;
518                 assert(cmd_req); /* request has hba_private while enqueued */
519 
520                 if (cmd_req->req.cmd.tag == req->req.tmf.tag) {
521                     /*
522                      * "If the specified command is present in the task set,
523                      * then return a service response set to FUNCTION
524                      * SUCCEEDED".
525                      */
526                     req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
527                 }
528             }
529         }
530         break;
531 
532     case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
533         if (!d) {
534             goto fail;
535         }
536         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
537             goto incorrect_lun;
538         }
539         qatomic_inc(&s->resetting);
540         device_cold_reset(&d->qdev);
541         qatomic_dec(&s->resetting);
542         break;
543 
544     case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: {
545         BusChild *kid;
546         int target = req->req.tmf.lun[1];
547         qatomic_inc(&s->resetting);
548 
549         rcu_read_lock();
550         QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) {
551             SCSIDevice *d1 = SCSI_DEVICE(kid->child);
552             if (d1->channel == 0 && d1->id == target) {
553                 device_cold_reset(&d1->qdev);
554             }
555         }
556         rcu_read_unlock();
557 
558         qatomic_dec(&s->resetting);
559         break;
560     }
561 
562     case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
563     case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: {
564         g_autoptr(GHashTable) aio_contexts = g_hash_table_new(NULL, NULL);
565 
566         if (!d) {
567             goto fail;
568         }
569         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
570             goto incorrect_lun;
571         }
572 
573         qatomic_inc(&req->remaining);
574 
575         for (uint32_t i = 0; i < s->parent_obj.conf.num_queues; i++) {
576             ctx = s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED + i];
577 
578             if (!g_hash_table_add(aio_contexts, ctx)) {
579                 continue; /* skip previously added AioContext */
580             }
581 
582             virtio_scsi_defer_tmf_to_aio_context(req, ctx);
583         }
584 
585         virtio_scsi_tmf_dec_remaining(req);
586         ret = -EINPROGRESS;
587         break;
588     }
589 
590     case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
591         if (!d) {
592             goto fail;
593         }
594         if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) {
595             goto incorrect_lun;
596         }
597 
598         WITH_QEMU_LOCK_GUARD(&d->requests_lock) {
599             QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
600                 /* Request has hba_private while enqueued */
601                 assert(r->hba_private);
602 
603                 /*
604                  * "If there is any command present in the task set, then
605                  * return a service response set to FUNCTION SUCCEEDED".
606                  */
607                 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
608                 break;
609             }
610         }
611         break;
612 
613     case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
614     default:
615         req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
616         break;
617     }
618 
619     object_unref(OBJECT(d));
620     return ret;
621 
622 incorrect_lun:
623     req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN;
624     object_unref(OBJECT(d));
625     return ret;
626 
627 fail:
628     req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET;
629     object_unref(OBJECT(d));
630     return ret;
631 }
632 
633 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req)
634 {
635     VirtIODevice *vdev = (VirtIODevice *)s;
636     uint32_t type;
637     int r = 0;
638 
639     if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0,
640                 &type, sizeof(type)) < sizeof(type)) {
641         virtio_scsi_bad_req(req, &s->ctrl_lock);
642         return;
643     }
644 
645     virtio_tswap32s(vdev, &type);
646     if (type == VIRTIO_SCSI_T_TMF) {
647         if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq),
648                     sizeof(VirtIOSCSICtrlTMFResp)) < 0) {
649             virtio_scsi_bad_req(req, &s->ctrl_lock);
650             return;
651         } else {
652             r = virtio_scsi_do_tmf(s, req);
653         }
654 
655     } else if (type == VIRTIO_SCSI_T_AN_QUERY ||
656                type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
657         if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq),
658                     sizeof(VirtIOSCSICtrlANResp)) < 0) {
659             virtio_scsi_bad_req(req, &s->ctrl_lock);
660             return;
661         } else {
662             req->req.an.event_requested =
663                 virtio_tswap32(VIRTIO_DEVICE(s), req->req.an.event_requested);
664             trace_virtio_scsi_an_req(virtio_scsi_get_lun(req->req.an.lun),
665                                      req->req.an.event_requested);
666             req->resp.an.event_actual = 0;
667             req->resp.an.response = VIRTIO_SCSI_S_OK;
668         }
669     }
670     if (r == 0) {
671         if (type == VIRTIO_SCSI_T_TMF)
672             trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun),
673                                        req->req.tmf.tag,
674                                        req->resp.tmf.response);
675         else if (type == VIRTIO_SCSI_T_AN_QUERY ||
676                  type == VIRTIO_SCSI_T_AN_SUBSCRIBE)
677             trace_virtio_scsi_an_resp(virtio_scsi_get_lun(req->req.an.lun),
678                                       req->resp.an.response);
679         virtio_scsi_complete_req(req, &s->ctrl_lock);
680     } else {
681         assert(r == -EINPROGRESS);
682     }
683 }
684 
685 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq)
686 {
687     VirtIOSCSIReq *req;
688 
689     while ((req = virtio_scsi_pop_req(s, vq, &s->ctrl_lock))) {
690         virtio_scsi_handle_ctrl_req(s, req);
691     }
692 }
693 
694 /*
695  * If dataplane is configured but not yet started, do so now and return true on
696  * success.
697  *
698  * Dataplane is started by the core virtio code but virtqueue handler functions
699  * can also be invoked when a guest kicks before DRIVER_OK, so this helper
700  * function helps us deal with manually starting ioeventfd in that case.
701  */
702 static bool virtio_scsi_defer_to_dataplane(VirtIOSCSI *s)
703 {
704     if (s->dataplane_started) {
705         return false;
706     }
707     if (s->vq_aio_context[0] == qemu_get_aio_context()) {
708         return false; /* not using IOThreads */
709     }
710 
711     virtio_device_start_ioeventfd(&s->parent_obj.parent_obj);
712     return !s->dataplane_fenced;
713 }
714 
715 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
716 {
717     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
718 
719     if (virtio_scsi_defer_to_dataplane(s)) {
720         return;
721     }
722 
723     virtio_scsi_handle_ctrl_vq(s, vq);
724 }
725 
726 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req)
727 {
728     trace_virtio_scsi_cmd_resp(virtio_scsi_get_lun(req->req.cmd.lun),
729                                req->req.cmd.tag,
730                                req->resp.cmd.response,
731                                req->resp.cmd.status);
732     /* Sense data is not in req->resp and is copied separately
733      * in virtio_scsi_command_complete.
734      */
735     req->resp_size = sizeof(VirtIOSCSICmdResp);
736     virtio_scsi_complete_req(req, NULL);
737 }
738 
739 static void virtio_scsi_command_failed(SCSIRequest *r)
740 {
741     VirtIOSCSIReq *req = r->hba_private;
742 
743     if (r->io_canceled) {
744         return;
745     }
746 
747     req->resp.cmd.status = GOOD;
748     switch (r->host_status) {
749     case SCSI_HOST_NO_LUN:
750         req->resp.cmd.response = VIRTIO_SCSI_S_INCORRECT_LUN;
751         break;
752     case SCSI_HOST_BUSY:
753         req->resp.cmd.response = VIRTIO_SCSI_S_BUSY;
754         break;
755     case SCSI_HOST_TIME_OUT:
756     case SCSI_HOST_ABORTED:
757         req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
758         break;
759     case SCSI_HOST_BAD_RESPONSE:
760         req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
761         break;
762     case SCSI_HOST_RESET:
763         req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
764         break;
765     case SCSI_HOST_TRANSPORT_DISRUPTED:
766         req->resp.cmd.response = VIRTIO_SCSI_S_TRANSPORT_FAILURE;
767         break;
768     case SCSI_HOST_TARGET_FAILURE:
769         req->resp.cmd.response = VIRTIO_SCSI_S_TARGET_FAILURE;
770         break;
771     case SCSI_HOST_RESERVATION_ERROR:
772         req->resp.cmd.response = VIRTIO_SCSI_S_NEXUS_FAILURE;
773         break;
774     case SCSI_HOST_ALLOCATION_FAILURE:
775     case SCSI_HOST_MEDIUM_ERROR:
776     case SCSI_HOST_ERROR:
777     default:
778         req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
779         break;
780     }
781     virtio_scsi_complete_cmd_req(req);
782 }
783 
784 static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid)
785 {
786     VirtIOSCSIReq *req = r->hba_private;
787     uint8_t sense[SCSI_SENSE_BUF_SIZE];
788     uint32_t sense_len;
789     VirtIODevice *vdev = VIRTIO_DEVICE(req->dev);
790 
791     if (r->io_canceled) {
792         return;
793     }
794 
795     req->resp.cmd.response = VIRTIO_SCSI_S_OK;
796     req->resp.cmd.status = r->status;
797     if (req->resp.cmd.status == GOOD) {
798         req->resp.cmd.resid = virtio_tswap32(vdev, resid);
799     } else {
800         req->resp.cmd.resid = 0;
801         sense_len = scsi_req_get_sense(r, sense, sizeof(sense));
802         sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd));
803         qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd),
804                             sense, sense_len);
805         req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len);
806     }
807     virtio_scsi_complete_cmd_req(req);
808 }
809 
810 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd,
811                                  uint8_t *buf, size_t buf_len,
812                                  void *hba_private)
813 {
814     VirtIOSCSIReq *req = hba_private;
815 
816     if (cmd->len == 0) {
817         cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE);
818         memcpy(cmd->buf, buf, cmd->len);
819     }
820 
821     /* Extract the direction and mode directly from the request, for
822      * host device passthrough.
823      */
824     cmd->xfer = req->qsgl.size;
825     cmd->mode = req->mode;
826     return 0;
827 }
828 
829 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
830 {
831     VirtIOSCSIReq *req = r->hba_private;
832 
833     return &req->qsgl;
834 }
835 
836 static void virtio_scsi_request_cancelled(SCSIRequest *r)
837 {
838     VirtIOSCSIReq *req = r->hba_private;
839 
840     if (!req) {
841         return;
842     }
843     if (qatomic_read(&req->dev->resetting)) {
844         req->resp.cmd.response = VIRTIO_SCSI_S_RESET;
845     } else {
846         req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED;
847     }
848     virtio_scsi_complete_cmd_req(req);
849 }
850 
851 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
852 {
853     req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE;
854     virtio_scsi_complete_cmd_req(req);
855 }
856 
857 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req)
858 {
859     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
860     SCSIDevice *d;
861     int rc;
862 
863     rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size,
864                                sizeof(VirtIOSCSICmdResp) + vs->sense_size);
865     if (rc < 0) {
866         if (rc == -ENOTSUP) {
867             virtio_scsi_fail_cmd_req(req);
868             return -ENOTSUP;
869         } else {
870             virtio_scsi_bad_req(req, NULL);
871             return -EINVAL;
872         }
873     }
874     trace_virtio_scsi_cmd_req(virtio_scsi_get_lun(req->req.cmd.lun),
875                               req->req.cmd.tag, req->req.cmd.cdb[0]);
876 
877     d = virtio_scsi_device_get(s, req->req.cmd.lun);
878     if (!d) {
879         req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET;
880         virtio_scsi_complete_cmd_req(req);
881         return -ENOENT;
882     }
883     req->sreq = scsi_req_new(d, req->req.cmd.tag,
884                              virtio_scsi_get_lun(req->req.cmd.lun),
885                              req->req.cmd.cdb, vs->cdb_size, req);
886 
887     if (req->sreq->cmd.mode != SCSI_XFER_NONE
888         && (req->sreq->cmd.mode != req->mode ||
889             req->sreq->cmd.xfer > req->qsgl.size)) {
890         req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN;
891         virtio_scsi_complete_cmd_req(req);
892         object_unref(OBJECT(d));
893         return -ENOBUFS;
894     }
895     scsi_req_ref(req->sreq);
896     defer_call_begin();
897     object_unref(OBJECT(d));
898     return 0;
899 }
900 
901 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req)
902 {
903     SCSIRequest *sreq = req->sreq;
904     if (scsi_req_enqueue(sreq)) {
905         scsi_req_continue(sreq);
906     }
907     defer_call_end();
908     scsi_req_unref(sreq);
909 }
910 
911 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq)
912 {
913     VirtIOSCSIReq *req, *next;
914     int ret = 0;
915     bool suppress_notifications = virtio_queue_get_notification(vq);
916 
917     QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs);
918 
919     do {
920         if (suppress_notifications) {
921             virtio_queue_set_notification(vq, 0);
922         }
923 
924         while ((req = virtio_scsi_pop_req(s, vq, NULL))) {
925             ret = virtio_scsi_handle_cmd_req_prepare(s, req);
926             if (!ret) {
927                 QTAILQ_INSERT_TAIL(&reqs, req, next);
928             } else if (ret == -EINVAL) {
929                 /* The device is broken and shouldn't process any request */
930                 while (!QTAILQ_EMPTY(&reqs)) {
931                     req = QTAILQ_FIRST(&reqs);
932                     QTAILQ_REMOVE(&reqs, req, next);
933                     defer_call_end();
934                     scsi_req_unref(req->sreq);
935                     virtqueue_detach_element(req->vq, &req->elem, 0);
936                     virtio_scsi_free_req(req);
937                 }
938             }
939         }
940 
941         if (suppress_notifications) {
942             virtio_queue_set_notification(vq, 1);
943         }
944     } while (ret != -EINVAL && !virtio_queue_empty(vq));
945 
946     QTAILQ_FOREACH_SAFE(req, &reqs, next, next) {
947         virtio_scsi_handle_cmd_req_submit(s, req);
948     }
949 }
950 
951 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
952 {
953     /* use non-QOM casts in the data path */
954     VirtIOSCSI *s = (VirtIOSCSI *)vdev;
955 
956     if (virtio_scsi_defer_to_dataplane(s)) {
957         return;
958     }
959 
960     virtio_scsi_handle_cmd_vq(s, vq);
961 }
962 
963 static void virtio_scsi_get_config(VirtIODevice *vdev,
964                                    uint8_t *config)
965 {
966     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
967     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev);
968 
969     virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues);
970     virtio_stl_p(vdev, &scsiconf->seg_max,
971                  s->conf.seg_max_adjust ? s->conf.virtqueue_size - 2 : 128 - 2);
972     virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors);
973     virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun);
974     virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
975     virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size);
976     virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size);
977     virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
978     virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
979     virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
980 }
981 
982 static void virtio_scsi_set_config(VirtIODevice *vdev,
983                                    const uint8_t *config)
984 {
985     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
986     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
987 
988     if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 ||
989         (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) {
990         virtio_error(vdev,
991                      "bad data written to virtio-scsi configuration space");
992         return;
993     }
994 
995     vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size);
996     vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size);
997 }
998 
999 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev,
1000                                          uint64_t requested_features,
1001                                          Error **errp)
1002 {
1003     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1004 
1005     /* Firstly sync all virtio-scsi possible supported features */
1006     requested_features |= s->host_features;
1007     return requested_features;
1008 }
1009 
1010 static void virtio_scsi_reset(VirtIODevice *vdev)
1011 {
1012     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1013     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
1014 
1015     assert(!s->dataplane_started);
1016 
1017     virtio_scsi_flush_defer_tmf_to_aio_context(s);
1018 
1019     qatomic_inc(&s->resetting);
1020     bus_cold_reset(BUS(&s->bus));
1021     qatomic_dec(&s->resetting);
1022 
1023     vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
1024     vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
1025 
1026     WITH_QEMU_LOCK_GUARD(&s->event_lock) {
1027         s->events_dropped = false;
1028     }
1029 }
1030 
1031 typedef struct {
1032     uint32_t event;
1033     uint32_t reason;
1034     union {
1035         /* Used by messages specific to a device */
1036         struct {
1037             uint32_t id;
1038             uint32_t lun;
1039         } address;
1040     };
1041 } VirtIOSCSIEventInfo;
1042 
1043 static void virtio_scsi_push_event(VirtIOSCSI *s,
1044                                    const VirtIOSCSIEventInfo *info)
1045 {
1046     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
1047     VirtIOSCSIReq *req;
1048     VirtIOSCSIEvent *evt;
1049     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1050     uint32_t event = info->event;
1051     uint32_t reason = info->reason;
1052 
1053     if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1054         return;
1055     }
1056 
1057     req = virtio_scsi_pop_req(s, vs->event_vq, &s->event_lock);
1058     WITH_QEMU_LOCK_GUARD(&s->event_lock) {
1059         if (!req) {
1060             s->events_dropped = true;
1061             return;
1062         }
1063 
1064         if (s->events_dropped) {
1065             event |= VIRTIO_SCSI_T_EVENTS_MISSED;
1066             s->events_dropped = false;
1067         }
1068     }
1069 
1070     if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) {
1071         virtio_scsi_bad_req(req, &s->event_lock);
1072         return;
1073     }
1074 
1075     evt = &req->resp.event;
1076     memset(evt, 0, sizeof(VirtIOSCSIEvent));
1077     evt->event = virtio_tswap32(vdev, event);
1078     evt->reason = virtio_tswap32(vdev, reason);
1079     if (event != VIRTIO_SCSI_T_EVENTS_MISSED) {
1080         evt->lun[0] = 1;
1081         evt->lun[1] = info->address.id;
1082 
1083         /* Linux wants us to keep the same encoding we use for REPORT LUNS.  */
1084         if (info->address.lun >= 256) {
1085             evt->lun[2] = (info->address.lun >> 8) | 0x40;
1086         }
1087         evt->lun[3] = info->address.lun & 0xFF;
1088     }
1089     trace_virtio_scsi_event(virtio_scsi_get_lun(evt->lun), event, reason);
1090 
1091     virtio_scsi_complete_req(req, &s->event_lock);
1092 }
1093 
1094 static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq)
1095 {
1096     bool events_dropped;
1097 
1098     WITH_QEMU_LOCK_GUARD(&s->event_lock) {
1099         events_dropped = s->events_dropped;
1100     }
1101 
1102     if (events_dropped) {
1103         VirtIOSCSIEventInfo info = {
1104             .event = VIRTIO_SCSI_T_NO_EVENT,
1105         };
1106         virtio_scsi_push_event(s, &info);
1107     }
1108 }
1109 
1110 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
1111 {
1112     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1113 
1114     if (virtio_scsi_defer_to_dataplane(s)) {
1115         return;
1116     }
1117 
1118     virtio_scsi_handle_event_vq(s, vq);
1119 }
1120 
1121 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
1122 {
1123     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1124     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1125 
1126     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) &&
1127         dev->type != TYPE_ROM) {
1128         VirtIOSCSIEventInfo info = {
1129             .event   = VIRTIO_SCSI_T_PARAM_CHANGE,
1130             .reason  = sense.asc | (sense.ascq << 8),
1131             .address = {
1132                 .id  = dev->id,
1133                 .lun = dev->lun,
1134             },
1135         };
1136 
1137         virtio_scsi_push_event(s, &info);
1138     }
1139 }
1140 
1141 static void virtio_scsi_pre_hotplug(HotplugHandler *hotplug_dev,
1142                                     DeviceState *dev, Error **errp)
1143 {
1144     SCSIDevice *sd = SCSI_DEVICE(dev);
1145     sd->hba_supports_iothread = true;
1146 }
1147 
1148 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev,
1149                                 Error **errp)
1150 {
1151     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
1152     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1153     AioContext *ctx = s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED];
1154     SCSIDevice *sd = SCSI_DEVICE(dev);
1155 
1156     if (ctx != qemu_get_aio_context() && !s->dataplane_fenced) {
1157         /*
1158          * Try to make the BlockBackend's AioContext match ours. Ignore failure
1159          * because I/O will still work although block jobs and other users
1160          * might be slower when multiple AioContexts use a BlockBackend.
1161          */
1162         blk_set_aio_context(sd->conf.blk, ctx, NULL);
1163     }
1164 
1165     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
1166         VirtIOSCSIEventInfo info = {
1167             .event   = VIRTIO_SCSI_T_TRANSPORT_RESET,
1168             .reason  = VIRTIO_SCSI_EVT_RESET_RESCAN,
1169             .address = {
1170                 .id  = sd->id,
1171                 .lun = sd->lun,
1172             },
1173         };
1174 
1175         virtio_scsi_push_event(s, &info);
1176         scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
1177     }
1178 }
1179 
1180 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev,
1181                                   Error **errp)
1182 {
1183     VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev);
1184     VirtIOSCSI *s = VIRTIO_SCSI(vdev);
1185     SCSIDevice *sd = SCSI_DEVICE(dev);
1186     VirtIOSCSIEventInfo info = {
1187         .event   = VIRTIO_SCSI_T_TRANSPORT_RESET,
1188         .reason  = VIRTIO_SCSI_EVT_RESET_REMOVED,
1189         .address = {
1190             .id  = sd->id,
1191             .lun = sd->lun,
1192         },
1193     };
1194 
1195     qdev_simple_device_unplug_cb(hotplug_dev, dev, errp);
1196 
1197     if (s->vq_aio_context[VIRTIO_SCSI_VQ_NUM_FIXED] != qemu_get_aio_context()) {
1198         /* If other users keep the BlockBackend in the iothread, that's ok */
1199         blk_set_aio_context(sd->conf.blk, qemu_get_aio_context(), NULL);
1200     }
1201 
1202     if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) {
1203         virtio_scsi_push_event(s, &info);
1204         scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED));
1205     }
1206 }
1207 
1208 /* Suspend virtqueue ioeventfd processing during drain */
1209 static void virtio_scsi_drained_begin(SCSIBus *bus)
1210 {
1211     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1212     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1213     uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED +
1214                             s->parent_obj.conf.num_queues;
1215 
1216     /*
1217      * Drain is called when stopping dataplane but the host notifier has
1218      * already been detached. Detaching multiple times is a no-op if nothing
1219      * else is using the monitoring same file descriptor, but avoid it just in
1220      * case.
1221      *
1222      * Also, don't detach if dataplane has not even been started yet because
1223      * the host notifier isn't attached.
1224      */
1225     if (s->dataplane_stopping || !s->dataplane_started) {
1226         return;
1227     }
1228 
1229     for (uint32_t i = 0; i < total_queues; i++) {
1230         VirtQueue *vq = virtio_get_queue(vdev, i);
1231         virtio_queue_aio_detach_host_notifier(vq, s->vq_aio_context[i]);
1232     }
1233 }
1234 
1235 /* Resume virtqueue ioeventfd processing after drain */
1236 static void virtio_scsi_drained_end(SCSIBus *bus)
1237 {
1238     VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
1239     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
1240     VirtIODevice *vdev = VIRTIO_DEVICE(s);
1241     uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED +
1242                             s->parent_obj.conf.num_queues;
1243 
1244     /*
1245      * Drain is called when stopping dataplane. Keep the host notifier detached
1246      * so it's not left dangling after dataplane is stopped.
1247      *
1248      * Also, don't attach if dataplane has not even been started yet. We're not
1249      * ready.
1250      */
1251     if (s->dataplane_stopping || !s->dataplane_started) {
1252         return;
1253     }
1254 
1255     for (uint32_t i = 0; i < total_queues; i++) {
1256         VirtQueue *vq = virtio_get_queue(vdev, i);
1257         AioContext *ctx = s->vq_aio_context[i];
1258 
1259         if (vq == vs->event_vq) {
1260             virtio_queue_aio_attach_host_notifier_no_poll(vq, ctx);
1261         } else {
1262             virtio_queue_aio_attach_host_notifier(vq, ctx);
1263         }
1264     }
1265 }
1266 
1267 static struct SCSIBusInfo virtio_scsi_scsi_info = {
1268     .tcq = true,
1269     .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
1270     .max_target = VIRTIO_SCSI_MAX_TARGET,
1271     .max_lun = VIRTIO_SCSI_MAX_LUN,
1272 
1273     .complete = virtio_scsi_command_complete,
1274     .fail = virtio_scsi_command_failed,
1275     .cancel = virtio_scsi_request_cancelled,
1276     .change = virtio_scsi_change,
1277     .parse_cdb = virtio_scsi_parse_cdb,
1278     .get_sg_list = virtio_scsi_get_sg_list,
1279     .save_request = virtio_scsi_save_request,
1280     .load_request = virtio_scsi_load_request,
1281     .drained_begin = virtio_scsi_drained_begin,
1282     .drained_end = virtio_scsi_drained_end,
1283 };
1284 
1285 void virtio_scsi_common_realize(DeviceState *dev,
1286                                 VirtIOHandleOutput ctrl,
1287                                 VirtIOHandleOutput evt,
1288                                 VirtIOHandleOutput cmd,
1289                                 Error **errp)
1290 {
1291     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1292     VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev);
1293     int i;
1294 
1295     virtio_init(vdev, VIRTIO_ID_SCSI, sizeof(VirtIOSCSIConfig));
1296 
1297     if (s->conf.num_queues == VIRTIO_SCSI_AUTO_NUM_QUEUES) {
1298         s->conf.num_queues = 1;
1299     }
1300     if (s->conf.num_queues == 0 ||
1301             s->conf.num_queues > VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED) {
1302         error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1303                          "must be a positive integer less than %d.",
1304                    s->conf.num_queues,
1305                    VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED);
1306         virtio_cleanup(vdev);
1307         return;
1308     }
1309     if (s->conf.virtqueue_size <= 2) {
1310         error_setg(errp, "invalid virtqueue_size property (= %" PRIu32 "), "
1311                    "must be > 2", s->conf.virtqueue_size);
1312         return;
1313     }
1314     s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues);
1315     s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE;
1316     s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE;
1317 
1318     s->ctrl_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, ctrl);
1319     s->event_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, evt);
1320     for (i = 0; i < s->conf.num_queues; i++) {
1321         s->cmd_vqs[i] = virtio_add_queue(vdev, s->conf.virtqueue_size, cmd);
1322     }
1323 }
1324 
1325 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp)
1326 {
1327     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1328     VirtIOSCSI *s = VIRTIO_SCSI(dev);
1329     Error *err = NULL;
1330 
1331     qemu_mutex_init(&s->ctrl_lock);
1332     qemu_mutex_init(&s->event_lock);
1333 
1334     virtio_scsi_common_realize(dev,
1335                                virtio_scsi_handle_ctrl,
1336                                virtio_scsi_handle_event,
1337                                virtio_scsi_handle_cmd,
1338                                &err);
1339     if (err != NULL) {
1340         error_propagate(errp, err);
1341         return;
1342     }
1343 
1344     scsi_bus_init_named(&s->bus, sizeof(s->bus), dev,
1345                        &virtio_scsi_scsi_info, vdev->bus_name);
1346     /* override default SCSI bus hotplug-handler, with virtio-scsi's one */
1347     qbus_set_hotplug_handler(BUS(&s->bus), OBJECT(dev));
1348 
1349     virtio_scsi_dataplane_setup(s, errp);
1350 }
1351 
1352 void virtio_scsi_common_unrealize(DeviceState *dev)
1353 {
1354     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1355     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
1356     int i;
1357 
1358     virtio_delete_queue(vs->ctrl_vq);
1359     virtio_delete_queue(vs->event_vq);
1360     for (i = 0; i < vs->conf.num_queues; i++) {
1361         virtio_delete_queue(vs->cmd_vqs[i]);
1362     }
1363     g_free(vs->cmd_vqs);
1364     virtio_cleanup(vdev);
1365 }
1366 
1367 /* main loop */
1368 static void virtio_scsi_device_unrealize(DeviceState *dev)
1369 {
1370     VirtIOSCSI *s = VIRTIO_SCSI(dev);
1371 
1372     virtio_scsi_dataplane_cleanup(s);
1373     qbus_set_hotplug_handler(BUS(&s->bus), NULL);
1374     virtio_scsi_common_unrealize(dev);
1375     qemu_mutex_destroy(&s->event_lock);
1376     qemu_mutex_destroy(&s->ctrl_lock);
1377 }
1378 
1379 static const Property virtio_scsi_properties[] = {
1380     DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues,
1381                        VIRTIO_SCSI_AUTO_NUM_QUEUES),
1382     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI,
1383                                          parent_obj.conf.virtqueue_size, 256),
1384     DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI,
1385                       parent_obj.conf.seg_max_adjust, true),
1386     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors,
1387                                                   0xFFFF),
1388     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun,
1389                                                   128),
1390     DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features,
1391                                            VIRTIO_SCSI_F_HOTPLUG, true),
1392     DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features,
1393                                                 VIRTIO_SCSI_F_CHANGE, true),
1394     DEFINE_PROP_LINK("iothread", VirtIOSCSI, parent_obj.conf.iothread,
1395                      TYPE_IOTHREAD, IOThread *),
1396     DEFINE_PROP_IOTHREAD_VQ_MAPPING_LIST("iothread-vq-mapping", VirtIOSCSI,
1397             parent_obj.conf.iothread_vq_mapping_list),
1398 };
1399 
1400 static const VMStateDescription vmstate_virtio_scsi = {
1401     .name = "virtio-scsi",
1402     .minimum_version_id = 1,
1403     .version_id = 1,
1404     .fields = (const VMStateField[]) {
1405         VMSTATE_VIRTIO_DEVICE,
1406         VMSTATE_END_OF_LIST()
1407     },
1408 };
1409 
1410 static void virtio_scsi_common_class_init(ObjectClass *klass, const void *data)
1411 {
1412     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1413     DeviceClass *dc = DEVICE_CLASS(klass);
1414 
1415     vdc->get_config = virtio_scsi_get_config;
1416     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1417 }
1418 
1419 static void virtio_scsi_class_init(ObjectClass *klass, const void *data)
1420 {
1421     DeviceClass *dc = DEVICE_CLASS(klass);
1422     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1423     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1424 
1425     device_class_set_props(dc, virtio_scsi_properties);
1426     dc->vmsd = &vmstate_virtio_scsi;
1427     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
1428     vdc->realize = virtio_scsi_device_realize;
1429     vdc->unrealize = virtio_scsi_device_unrealize;
1430     vdc->set_config = virtio_scsi_set_config;
1431     vdc->get_features = virtio_scsi_get_features;
1432     vdc->reset = virtio_scsi_reset;
1433     vdc->start_ioeventfd = virtio_scsi_dataplane_start;
1434     vdc->stop_ioeventfd = virtio_scsi_dataplane_stop;
1435     hc->pre_plug = virtio_scsi_pre_hotplug;
1436     hc->plug = virtio_scsi_hotplug;
1437     hc->unplug = virtio_scsi_hotunplug;
1438 }
1439 
1440 static const TypeInfo virtio_scsi_common_info = {
1441     .name = TYPE_VIRTIO_SCSI_COMMON,
1442     .parent = TYPE_VIRTIO_DEVICE,
1443     .instance_size = sizeof(VirtIOSCSICommon),
1444     .abstract = true,
1445     .class_init = virtio_scsi_common_class_init,
1446 };
1447 
1448 static const TypeInfo virtio_scsi_info = {
1449     .name = TYPE_VIRTIO_SCSI,
1450     .parent = TYPE_VIRTIO_SCSI_COMMON,
1451     .instance_size = sizeof(VirtIOSCSI),
1452     .class_init = virtio_scsi_class_init,
1453     .interfaces = (const InterfaceInfo[]) {
1454         { TYPE_HOTPLUG_HANDLER },
1455         { }
1456     }
1457 };
1458 
1459 static void virtio_register_types(void)
1460 {
1461     type_register_static(&virtio_scsi_common_info);
1462     type_register_static(&virtio_scsi_info);
1463 }
1464 
1465 type_init(virtio_register_types)
1466