xref: /openbmc/qemu/hw/scsi/scsi-bus.c (revision fe29141b)
1 #include "qemu/osdep.h"
2 #include "hw/hw.h"
3 #include "qapi/error.h"
4 #include "qemu/error-report.h"
5 #include "hw/scsi/scsi.h"
6 #include "scsi/constants.h"
7 #include "hw/qdev.h"
8 #include "sysemu/block-backend.h"
9 #include "sysemu/blockdev.h"
10 #include "trace.h"
11 #include "sysemu/dma.h"
12 #include "qemu/cutils.h"
13 
14 static char *scsibus_get_dev_path(DeviceState *dev);
15 static char *scsibus_get_fw_dev_path(DeviceState *dev);
16 static void scsi_req_dequeue(SCSIRequest *req);
17 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
18 static void scsi_target_free_buf(SCSIRequest *req);
19 
20 static Property scsi_props[] = {
21     DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
22     DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
23     DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
24     DEFINE_PROP_END_OF_LIST(),
25 };
26 
27 static void scsi_bus_class_init(ObjectClass *klass, void *data)
28 {
29     BusClass *k = BUS_CLASS(klass);
30     HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
31 
32     k->get_dev_path = scsibus_get_dev_path;
33     k->get_fw_dev_path = scsibus_get_fw_dev_path;
34     hc->unplug = qdev_simple_device_unplug_cb;
35 }
36 
37 static const TypeInfo scsi_bus_info = {
38     .name = TYPE_SCSI_BUS,
39     .parent = TYPE_BUS,
40     .instance_size = sizeof(SCSIBus),
41     .class_init = scsi_bus_class_init,
42     .interfaces = (InterfaceInfo[]) {
43         { TYPE_HOTPLUG_HANDLER },
44         { }
45     }
46 };
47 static int next_scsi_bus;
48 
49 static void scsi_device_realize(SCSIDevice *s, Error **errp)
50 {
51     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
52     if (sc->realize) {
53         sc->realize(s, errp);
54     }
55 }
56 
57 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
58                        void *hba_private)
59 {
60     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
61     int rc;
62 
63     assert(cmd->len == 0);
64     rc = scsi_req_parse_cdb(dev, cmd, buf);
65     if (bus->info->parse_cdb) {
66         rc = bus->info->parse_cdb(dev, cmd, buf, hba_private);
67     }
68     return rc;
69 }
70 
71 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
72                                           uint8_t *buf, void *hba_private)
73 {
74     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
75     if (sc->alloc_req) {
76         return sc->alloc_req(s, tag, lun, buf, hba_private);
77     }
78 
79     return NULL;
80 }
81 
82 void scsi_device_unit_attention_reported(SCSIDevice *s)
83 {
84     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
85     if (sc->unit_attention_reported) {
86         sc->unit_attention_reported(s);
87     }
88 }
89 
90 /* Create a scsi bus, and attach devices to it.  */
91 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
92                   const SCSIBusInfo *info, const char *bus_name)
93 {
94     qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
95     bus->busnr = next_scsi_bus++;
96     bus->info = info;
97     qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
98 }
99 
100 static void scsi_dma_restart_bh(void *opaque)
101 {
102     SCSIDevice *s = opaque;
103     SCSIRequest *req, *next;
104 
105     qemu_bh_delete(s->bh);
106     s->bh = NULL;
107 
108     aio_context_acquire(blk_get_aio_context(s->conf.blk));
109     QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
110         scsi_req_ref(req);
111         if (req->retry) {
112             req->retry = false;
113             switch (req->cmd.mode) {
114             case SCSI_XFER_FROM_DEV:
115             case SCSI_XFER_TO_DEV:
116                 scsi_req_continue(req);
117                 break;
118             case SCSI_XFER_NONE:
119                 scsi_req_dequeue(req);
120                 scsi_req_enqueue(req);
121                 break;
122             }
123         }
124         scsi_req_unref(req);
125     }
126     aio_context_release(blk_get_aio_context(s->conf.blk));
127 }
128 
129 void scsi_req_retry(SCSIRequest *req)
130 {
131     /* No need to save a reference, because scsi_dma_restart_bh just
132      * looks at the request list.  */
133     req->retry = true;
134 }
135 
136 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
137 {
138     SCSIDevice *s = opaque;
139 
140     if (!running) {
141         return;
142     }
143     if (!s->bh) {
144         AioContext *ctx = blk_get_aio_context(s->conf.blk);
145         s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s);
146         qemu_bh_schedule(s->bh);
147     }
148 }
149 
150 static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
151 {
152     SCSIDevice *dev = SCSI_DEVICE(qdev);
153     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
154     SCSIDevice *d;
155     Error *local_err = NULL;
156 
157     if (dev->channel > bus->info->max_channel) {
158         error_setg(errp, "bad scsi channel id: %d", dev->channel);
159         return;
160     }
161     if (dev->id != -1 && dev->id > bus->info->max_target) {
162         error_setg(errp, "bad scsi device id: %d", dev->id);
163         return;
164     }
165     if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
166         error_setg(errp, "bad scsi device lun: %d", dev->lun);
167         return;
168     }
169 
170     if (dev->id == -1) {
171         int id = -1;
172         if (dev->lun == -1) {
173             dev->lun = 0;
174         }
175         do {
176             d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
177         } while (d && d->lun == dev->lun && id < bus->info->max_target);
178         if (d && d->lun == dev->lun) {
179             error_setg(errp, "no free target");
180             return;
181         }
182         dev->id = id;
183     } else if (dev->lun == -1) {
184         int lun = -1;
185         do {
186             d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
187         } while (d && d->lun == lun && lun < bus->info->max_lun);
188         if (d && d->lun == lun) {
189             error_setg(errp, "no free lun");
190             return;
191         }
192         dev->lun = lun;
193     } else {
194         d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
195         assert(d);
196         if (d->lun == dev->lun && dev != d) {
197             error_setg(errp, "lun already used by '%s'", d->qdev.id);
198             return;
199         }
200     }
201 
202     QTAILQ_INIT(&dev->requests);
203     scsi_device_realize(dev, &local_err);
204     if (local_err) {
205         error_propagate(errp, local_err);
206         return;
207     }
208     dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
209                                                      dev);
210 }
211 
212 static void scsi_qdev_unrealize(DeviceState *qdev, Error **errp)
213 {
214     SCSIDevice *dev = SCSI_DEVICE(qdev);
215 
216     if (dev->vmsentry) {
217         qemu_del_vm_change_state_handler(dev->vmsentry);
218     }
219 
220     scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
221     blockdev_mark_auto_del(dev->conf.blk);
222 }
223 
224 /* handle legacy '-drive if=scsi,...' cmd line args */
225 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
226                                       int unit, bool removable, int bootindex,
227                                       bool share_rw,
228                                       const char *serial, Error **errp)
229 {
230     const char *driver;
231     char *name;
232     DeviceState *dev;
233     Error *err = NULL;
234 
235     driver = blk_is_sg(blk) ? "scsi-generic" : "scsi-disk";
236     dev = qdev_create(&bus->qbus, driver);
237     name = g_strdup_printf("legacy[%d]", unit);
238     object_property_add_child(OBJECT(bus), name, OBJECT(dev), NULL);
239     g_free(name);
240 
241     qdev_prop_set_uint32(dev, "scsi-id", unit);
242     if (bootindex >= 0) {
243         object_property_set_int(OBJECT(dev), bootindex, "bootindex",
244                                 &error_abort);
245     }
246     if (object_property_find(OBJECT(dev), "removable", NULL)) {
247         qdev_prop_set_bit(dev, "removable", removable);
248     }
249     if (serial && object_property_find(OBJECT(dev), "serial", NULL)) {
250         qdev_prop_set_string(dev, "serial", serial);
251     }
252     qdev_prop_set_drive(dev, "drive", blk, &err);
253     if (err) {
254         error_propagate(errp, err);
255         object_unparent(OBJECT(dev));
256         return NULL;
257     }
258     object_property_set_bool(OBJECT(dev), share_rw, "share-rw", &err);
259     if (err != NULL) {
260         error_propagate(errp, err);
261         object_unparent(OBJECT(dev));
262         return NULL;
263     }
264     object_property_set_bool(OBJECT(dev), true, "realized", &err);
265     if (err != NULL) {
266         error_propagate(errp, err);
267         object_unparent(OBJECT(dev));
268         return NULL;
269     }
270     return SCSI_DEVICE(dev);
271 }
272 
273 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, bool deprecated)
274 {
275     Location loc;
276     DriveInfo *dinfo;
277     int unit;
278 
279     loc_push_none(&loc);
280     for (unit = 0; unit <= bus->info->max_target; unit++) {
281         dinfo = drive_get(IF_SCSI, bus->busnr, unit);
282         if (dinfo == NULL) {
283             continue;
284         }
285         qemu_opts_loc_restore(dinfo->opts);
286         if (deprecated) {
287             /* Handling -drive not claimed by machine initialization */
288             if (blk_get_attached_dev(blk_by_legacy_dinfo(dinfo))) {
289                 continue;       /* claimed */
290             }
291             if (!dinfo->is_default) {
292                 warn_report("bus=%d,unit=%d is deprecated with this"
293                             " machine type",
294                             bus->busnr, unit);
295             }
296         }
297         scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
298                                   unit, false, -1, false, NULL, &error_fatal);
299     }
300     loc_pop(&loc);
301 }
302 
303 static bool is_scsi_hba_with_legacy_magic(Object *obj)
304 {
305     static const char *magic[] = {
306         "am53c974", "dc390", "esp", "lsi53c810", "lsi53c895a",
307         "megasas", "megasas-gen2", "mptsas1068", "spapr-vscsi",
308         "virtio-scsi-device",
309         NULL
310     };
311     const char *typename = object_get_typename(obj);
312     int i;
313 
314     for (i = 0; magic[i]; i++)
315         if (!strcmp(typename, magic[i])) {
316             return true;
317     }
318 
319     return false;
320 }
321 
322 static int scsi_legacy_handle_cmdline_cb(Object *obj, void *opaque)
323 {
324     SCSIBus *bus = (SCSIBus *)object_dynamic_cast(obj, TYPE_SCSI_BUS);
325 
326     if (bus && is_scsi_hba_with_legacy_magic(OBJECT(bus->qbus.parent))) {
327         scsi_bus_legacy_handle_cmdline(bus, true);
328     }
329 
330     return 0;
331 }
332 
333 void scsi_legacy_handle_cmdline(void)
334 {
335     object_child_foreach_recursive(object_get_root(),
336                                    scsi_legacy_handle_cmdline_cb, NULL);
337 }
338 
339 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
340 {
341     scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
342     scsi_req_complete(req, CHECK_CONDITION);
343     return 0;
344 }
345 
346 static const struct SCSIReqOps reqops_invalid_field = {
347     .size         = sizeof(SCSIRequest),
348     .send_command = scsi_invalid_field
349 };
350 
351 /* SCSIReqOps implementation for invalid commands.  */
352 
353 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
354 {
355     scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
356     scsi_req_complete(req, CHECK_CONDITION);
357     return 0;
358 }
359 
360 static const struct SCSIReqOps reqops_invalid_opcode = {
361     .size         = sizeof(SCSIRequest),
362     .send_command = scsi_invalid_command
363 };
364 
365 /* SCSIReqOps implementation for unit attention conditions.  */
366 
367 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
368 {
369     if (req->dev->unit_attention.key == UNIT_ATTENTION) {
370         scsi_req_build_sense(req, req->dev->unit_attention);
371     } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
372         scsi_req_build_sense(req, req->bus->unit_attention);
373     }
374     scsi_req_complete(req, CHECK_CONDITION);
375     return 0;
376 }
377 
378 static const struct SCSIReqOps reqops_unit_attention = {
379     .size         = sizeof(SCSIRequest),
380     .send_command = scsi_unit_attention
381 };
382 
383 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
384    an invalid LUN.  */
385 
386 typedef struct SCSITargetReq SCSITargetReq;
387 
388 struct SCSITargetReq {
389     SCSIRequest req;
390     int len;
391     uint8_t *buf;
392     int buf_len;
393 };
394 
395 static void store_lun(uint8_t *outbuf, int lun)
396 {
397     if (lun < 256) {
398         outbuf[1] = lun;
399         return;
400     }
401     outbuf[1] = (lun & 255);
402     outbuf[0] = (lun >> 8) | 0x40;
403 }
404 
405 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
406 {
407     BusChild *kid;
408     int i, len, n;
409     int channel, id;
410     bool found_lun0;
411 
412     if (r->req.cmd.xfer < 16) {
413         return false;
414     }
415     if (r->req.cmd.buf[2] > 2) {
416         return false;
417     }
418     channel = r->req.dev->channel;
419     id = r->req.dev->id;
420     found_lun0 = false;
421     n = 0;
422     QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
423         DeviceState *qdev = kid->child;
424         SCSIDevice *dev = SCSI_DEVICE(qdev);
425 
426         if (dev->channel == channel && dev->id == id) {
427             if (dev->lun == 0) {
428                 found_lun0 = true;
429             }
430             n += 8;
431         }
432     }
433     if (!found_lun0) {
434         n += 8;
435     }
436 
437     scsi_target_alloc_buf(&r->req, n + 8);
438 
439     len = MIN(n + 8, r->req.cmd.xfer & ~7);
440     memset(r->buf, 0, len);
441     stl_be_p(&r->buf[0], n);
442     i = found_lun0 ? 8 : 16;
443     QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
444         DeviceState *qdev = kid->child;
445         SCSIDevice *dev = SCSI_DEVICE(qdev);
446 
447         if (dev->channel == channel && dev->id == id) {
448             store_lun(&r->buf[i], dev->lun);
449             i += 8;
450         }
451     }
452     assert(i == n + 8);
453     r->len = len;
454     return true;
455 }
456 
457 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
458 {
459     assert(r->req.dev->lun != r->req.lun);
460 
461     scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
462 
463     if (r->req.cmd.buf[1] & 0x2) {
464         /* Command support data - optional, not implemented */
465         return false;
466     }
467 
468     if (r->req.cmd.buf[1] & 0x1) {
469         /* Vital product data */
470         uint8_t page_code = r->req.cmd.buf[2];
471         r->buf[r->len++] = page_code ; /* this page */
472         r->buf[r->len++] = 0x00;
473 
474         switch (page_code) {
475         case 0x00: /* Supported page codes, mandatory */
476         {
477             int pages;
478             pages = r->len++;
479             r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
480             r->buf[pages] = r->len - pages - 1; /* number of pages */
481             break;
482         }
483         default:
484             return false;
485         }
486         /* done with EVPD */
487         assert(r->len < r->buf_len);
488         r->len = MIN(r->req.cmd.xfer, r->len);
489         return true;
490     }
491 
492     /* Standard INQUIRY data */
493     if (r->req.cmd.buf[2] != 0) {
494         return false;
495     }
496 
497     /* PAGE CODE == 0 */
498     r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
499     memset(r->buf, 0, r->len);
500     if (r->req.lun != 0) {
501         r->buf[0] = TYPE_NO_LUN;
502     } else {
503         r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
504         r->buf[2] = 5; /* Version */
505         r->buf[3] = 2 | 0x10; /* HiSup, response data format */
506         r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
507         r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ.  */
508         memcpy(&r->buf[8], "QEMU    ", 8);
509         memcpy(&r->buf[16], "QEMU TARGET     ", 16);
510         pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
511     }
512     return true;
513 }
514 
515 static size_t scsi_sense_len(SCSIRequest *req)
516 {
517     if (req->dev->type == TYPE_SCANNER)
518         return SCSI_SENSE_LEN_SCANNER;
519     else
520         return SCSI_SENSE_LEN;
521 }
522 
523 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
524 {
525     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
526     int fixed_sense = (req->cmd.buf[1] & 1) == 0;
527 
528     if (req->lun != 0 &&
529         buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
530         scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
531         scsi_req_complete(req, CHECK_CONDITION);
532         return 0;
533     }
534     switch (buf[0]) {
535     case REPORT_LUNS:
536         if (!scsi_target_emulate_report_luns(r)) {
537             goto illegal_request;
538         }
539         break;
540     case INQUIRY:
541         if (!scsi_target_emulate_inquiry(r)) {
542             goto illegal_request;
543         }
544         break;
545     case REQUEST_SENSE:
546         scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
547         if (req->lun != 0) {
548             const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
549 
550             r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
551                                           sense, fixed_sense);
552         } else {
553             r->len = scsi_device_get_sense(r->req.dev, r->buf,
554                                            MIN(req->cmd.xfer, r->buf_len),
555                                            fixed_sense);
556         }
557         if (r->req.dev->sense_is_ua) {
558             scsi_device_unit_attention_reported(req->dev);
559             r->req.dev->sense_len = 0;
560             r->req.dev->sense_is_ua = false;
561         }
562         break;
563     case TEST_UNIT_READY:
564         break;
565     default:
566         scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
567         scsi_req_complete(req, CHECK_CONDITION);
568         return 0;
569     illegal_request:
570         scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
571         scsi_req_complete(req, CHECK_CONDITION);
572         return 0;
573     }
574 
575     if (!r->len) {
576         scsi_req_complete(req, GOOD);
577     }
578     return r->len;
579 }
580 
581 static void scsi_target_read_data(SCSIRequest *req)
582 {
583     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
584     uint32_t n;
585 
586     n = r->len;
587     if (n > 0) {
588         r->len = 0;
589         scsi_req_data(&r->req, n);
590     } else {
591         scsi_req_complete(&r->req, GOOD);
592     }
593 }
594 
595 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
596 {
597     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
598 
599     return r->buf;
600 }
601 
602 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
603 {
604     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
605 
606     r->buf = g_malloc(len);
607     r->buf_len = len;
608 
609     return r->buf;
610 }
611 
612 static void scsi_target_free_buf(SCSIRequest *req)
613 {
614     SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
615 
616     g_free(r->buf);
617 }
618 
619 static const struct SCSIReqOps reqops_target_command = {
620     .size         = sizeof(SCSITargetReq),
621     .send_command = scsi_target_send_command,
622     .read_data    = scsi_target_read_data,
623     .get_buf      = scsi_target_get_buf,
624     .free_req     = scsi_target_free_buf,
625 };
626 
627 
628 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
629                             uint32_t tag, uint32_t lun, void *hba_private)
630 {
631     SCSIRequest *req;
632     SCSIBus *bus = scsi_bus_from_device(d);
633     BusState *qbus = BUS(bus);
634     const int memset_off = offsetof(SCSIRequest, sense)
635                            + sizeof(req->sense);
636 
637     req = g_malloc(reqops->size);
638     memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
639     req->refcount = 1;
640     req->bus = bus;
641     req->dev = d;
642     req->tag = tag;
643     req->lun = lun;
644     req->hba_private = hba_private;
645     req->status = -1;
646     req->ops = reqops;
647     object_ref(OBJECT(d));
648     object_ref(OBJECT(qbus->parent));
649     notifier_list_init(&req->cancel_notifiers);
650     trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
651     return req;
652 }
653 
654 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
655                           uint8_t *buf, void *hba_private)
656 {
657     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
658     const SCSIReqOps *ops;
659     SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
660     SCSIRequest *req;
661     SCSICommand cmd = { .len = 0 };
662     int ret;
663 
664     if ((d->unit_attention.key == UNIT_ATTENTION ||
665          bus->unit_attention.key == UNIT_ATTENTION) &&
666         (buf[0] != INQUIRY &&
667          buf[0] != REPORT_LUNS &&
668          buf[0] != GET_CONFIGURATION &&
669          buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
670 
671          /*
672           * If we already have a pending unit attention condition,
673           * report this one before triggering another one.
674           */
675          !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
676         ops = &reqops_unit_attention;
677     } else if (lun != d->lun ||
678                buf[0] == REPORT_LUNS ||
679                (buf[0] == REQUEST_SENSE && d->sense_len)) {
680         ops = &reqops_target_command;
681     } else {
682         ops = NULL;
683     }
684 
685     if (ops != NULL || !sc->parse_cdb) {
686         ret = scsi_req_parse_cdb(d, &cmd, buf);
687     } else {
688         ret = sc->parse_cdb(d, &cmd, buf, hba_private);
689     }
690 
691     if (ret != 0) {
692         trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
693         req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
694     } else {
695         assert(cmd.len != 0);
696         trace_scsi_req_parsed(d->id, lun, tag, buf[0],
697                               cmd.mode, cmd.xfer);
698         if (cmd.lba != -1) {
699             trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
700                                       cmd.lba);
701         }
702 
703         if (cmd.xfer > INT32_MAX) {
704             req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
705         } else if (ops) {
706             req = scsi_req_alloc(ops, d, tag, lun, hba_private);
707         } else {
708             req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
709         }
710     }
711 
712     req->cmd = cmd;
713     req->resid = req->cmd.xfer;
714 
715     switch (buf[0]) {
716     case INQUIRY:
717         trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
718         break;
719     case TEST_UNIT_READY:
720         trace_scsi_test_unit_ready(d->id, lun, tag);
721         break;
722     case REPORT_LUNS:
723         trace_scsi_report_luns(d->id, lun, tag);
724         break;
725     case REQUEST_SENSE:
726         trace_scsi_request_sense(d->id, lun, tag);
727         break;
728     default:
729         break;
730     }
731 
732     return req;
733 }
734 
735 uint8_t *scsi_req_get_buf(SCSIRequest *req)
736 {
737     return req->ops->get_buf(req);
738 }
739 
740 static void scsi_clear_unit_attention(SCSIRequest *req)
741 {
742     SCSISense *ua;
743     if (req->dev->unit_attention.key != UNIT_ATTENTION &&
744         req->bus->unit_attention.key != UNIT_ATTENTION) {
745         return;
746     }
747 
748     /*
749      * If an INQUIRY command enters the enabled command state,
750      * the device server shall [not] clear any unit attention condition;
751      * See also MMC-6, paragraphs 6.5 and 6.6.2.
752      */
753     if (req->cmd.buf[0] == INQUIRY ||
754         req->cmd.buf[0] == GET_CONFIGURATION ||
755         req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
756         return;
757     }
758 
759     if (req->dev->unit_attention.key == UNIT_ATTENTION) {
760         ua = &req->dev->unit_attention;
761     } else {
762         ua = &req->bus->unit_attention;
763     }
764 
765     /*
766      * If a REPORT LUNS command enters the enabled command state, [...]
767      * the device server shall clear any pending unit attention condition
768      * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
769      */
770     if (req->cmd.buf[0] == REPORT_LUNS &&
771         !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
772           ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
773         return;
774     }
775 
776     *ua = SENSE_CODE(NO_SENSE);
777 }
778 
779 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
780 {
781     int ret;
782 
783     assert(len >= 14);
784     if (!req->sense_len) {
785         return 0;
786     }
787 
788     ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
789 
790     /*
791      * FIXME: clearing unit attention conditions upon autosense should be done
792      * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
793      * (SAM-5, 5.14).
794      *
795      * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
796      * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
797      * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
798      */
799     if (req->dev->sense_is_ua) {
800         scsi_device_unit_attention_reported(req->dev);
801         req->dev->sense_len = 0;
802         req->dev->sense_is_ua = false;
803     }
804     return ret;
805 }
806 
807 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
808 {
809     return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
810 }
811 
812 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
813 {
814     trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
815                                sense.key, sense.asc, sense.ascq);
816     req->sense_len = scsi_build_sense(req->sense, sense);
817 }
818 
819 static void scsi_req_enqueue_internal(SCSIRequest *req)
820 {
821     assert(!req->enqueued);
822     scsi_req_ref(req);
823     if (req->bus->info->get_sg_list) {
824         req->sg = req->bus->info->get_sg_list(req);
825     } else {
826         req->sg = NULL;
827     }
828     req->enqueued = true;
829     QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
830 }
831 
832 int32_t scsi_req_enqueue(SCSIRequest *req)
833 {
834     int32_t rc;
835 
836     assert(!req->retry);
837     scsi_req_enqueue_internal(req);
838     scsi_req_ref(req);
839     rc = req->ops->send_command(req, req->cmd.buf);
840     scsi_req_unref(req);
841     return rc;
842 }
843 
844 static void scsi_req_dequeue(SCSIRequest *req)
845 {
846     trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
847     req->retry = false;
848     if (req->enqueued) {
849         QTAILQ_REMOVE(&req->dev->requests, req, next);
850         req->enqueued = false;
851         scsi_req_unref(req);
852     }
853 }
854 
855 static int scsi_get_performance_length(int num_desc, int type, int data_type)
856 {
857     /* MMC-6, paragraph 6.7.  */
858     switch (type) {
859     case 0:
860         if ((data_type & 3) == 0) {
861             /* Each descriptor is as in Table 295 - Nominal performance.  */
862             return 16 * num_desc + 8;
863         } else {
864             /* Each descriptor is as in Table 296 - Exceptions.  */
865             return 6 * num_desc + 8;
866         }
867     case 1:
868     case 4:
869     case 5:
870         return 8 * num_desc + 8;
871     case 2:
872         return 2048 * num_desc + 8;
873     case 3:
874         return 16 * num_desc + 8;
875     default:
876         return 8;
877     }
878 }
879 
880 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
881 {
882     int byte_block = (buf[2] >> 2) & 0x1;
883     int type = (buf[2] >> 4) & 0x1;
884     int xfer_unit;
885 
886     if (byte_block) {
887         if (type) {
888             xfer_unit = dev->blocksize;
889         } else {
890             xfer_unit = 512;
891         }
892     } else {
893         xfer_unit = 1;
894     }
895 
896     return xfer_unit;
897 }
898 
899 static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
900 {
901     int length = buf[2] & 0x3;
902     int xfer;
903     int unit = ata_passthrough_xfer_unit(dev, buf);
904 
905     switch (length) {
906     case 0:
907     case 3: /* USB-specific.  */
908     default:
909         xfer = 0;
910         break;
911     case 1:
912         xfer = buf[3];
913         break;
914     case 2:
915         xfer = buf[4];
916         break;
917     }
918 
919     return xfer * unit;
920 }
921 
922 static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
923 {
924     int extend = buf[1] & 0x1;
925     int length = buf[2] & 0x3;
926     int xfer;
927     int unit = ata_passthrough_xfer_unit(dev, buf);
928 
929     switch (length) {
930     case 0:
931     case 3: /* USB-specific.  */
932     default:
933         xfer = 0;
934         break;
935     case 1:
936         xfer = buf[4];
937         xfer |= (extend ? buf[3] << 8 : 0);
938         break;
939     case 2:
940         xfer = buf[6];
941         xfer |= (extend ? buf[5] << 8 : 0);
942         break;
943     }
944 
945     return xfer * unit;
946 }
947 
948 static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
949 {
950     cmd->xfer = scsi_cdb_xfer(buf);
951     switch (buf[0]) {
952     case TEST_UNIT_READY:
953     case REWIND:
954     case START_STOP:
955     case SET_CAPACITY:
956     case WRITE_FILEMARKS:
957     case WRITE_FILEMARKS_16:
958     case SPACE:
959     case RESERVE:
960     case RELEASE:
961     case ERASE:
962     case ALLOW_MEDIUM_REMOVAL:
963     case SEEK_10:
964     case SYNCHRONIZE_CACHE:
965     case SYNCHRONIZE_CACHE_16:
966     case LOCATE_16:
967     case LOCK_UNLOCK_CACHE:
968     case SET_CD_SPEED:
969     case SET_LIMITS:
970     case WRITE_LONG_10:
971     case UPDATE_BLOCK:
972     case RESERVE_TRACK:
973     case SET_READ_AHEAD:
974     case PRE_FETCH:
975     case PRE_FETCH_16:
976     case ALLOW_OVERWRITE:
977         cmd->xfer = 0;
978         break;
979     case VERIFY_10:
980     case VERIFY_12:
981     case VERIFY_16:
982         if ((buf[1] & 2) == 0) {
983             cmd->xfer = 0;
984         } else if ((buf[1] & 4) != 0) {
985             cmd->xfer = 1;
986         }
987         cmd->xfer *= dev->blocksize;
988         break;
989     case MODE_SENSE:
990         break;
991     case WRITE_SAME_10:
992     case WRITE_SAME_16:
993         cmd->xfer = dev->blocksize;
994         break;
995     case READ_CAPACITY_10:
996         cmd->xfer = 8;
997         break;
998     case READ_BLOCK_LIMITS:
999         cmd->xfer = 6;
1000         break;
1001     case SEND_VOLUME_TAG:
1002         /* GPCMD_SET_STREAMING from multimedia commands.  */
1003         if (dev->type == TYPE_ROM) {
1004             cmd->xfer = buf[10] | (buf[9] << 8);
1005         } else {
1006             cmd->xfer = buf[9] | (buf[8] << 8);
1007         }
1008         break;
1009     case WRITE_6:
1010         /* length 0 means 256 blocks */
1011         if (cmd->xfer == 0) {
1012             cmd->xfer = 256;
1013         }
1014         /* fall through */
1015     case WRITE_10:
1016     case WRITE_VERIFY_10:
1017     case WRITE_12:
1018     case WRITE_VERIFY_12:
1019     case WRITE_16:
1020     case WRITE_VERIFY_16:
1021         cmd->xfer *= dev->blocksize;
1022         break;
1023     case READ_6:
1024     case READ_REVERSE:
1025         /* length 0 means 256 blocks */
1026         if (cmd->xfer == 0) {
1027             cmd->xfer = 256;
1028         }
1029         /* fall through */
1030     case READ_10:
1031     case READ_12:
1032     case READ_16:
1033         cmd->xfer *= dev->blocksize;
1034         break;
1035     case FORMAT_UNIT:
1036         /* MMC mandates the parameter list to be 12-bytes long.  Parameters
1037          * for block devices are restricted to the header right now.  */
1038         if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1039             cmd->xfer = 12;
1040         } else {
1041             cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1042         }
1043         break;
1044     case INQUIRY:
1045     case RECEIVE_DIAGNOSTIC:
1046     case SEND_DIAGNOSTIC:
1047         cmd->xfer = buf[4] | (buf[3] << 8);
1048         break;
1049     case READ_CD:
1050     case READ_BUFFER:
1051     case WRITE_BUFFER:
1052     case SEND_CUE_SHEET:
1053         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1054         break;
1055     case PERSISTENT_RESERVE_OUT:
1056         cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1057         break;
1058     case ERASE_12:
1059         if (dev->type == TYPE_ROM) {
1060             /* MMC command GET PERFORMANCE.  */
1061             cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1062                                                     buf[10], buf[1] & 0x1f);
1063         }
1064         break;
1065     case MECHANISM_STATUS:
1066     case READ_DVD_STRUCTURE:
1067     case SEND_DVD_STRUCTURE:
1068     case MAINTENANCE_OUT:
1069     case MAINTENANCE_IN:
1070         if (dev->type == TYPE_ROM) {
1071             /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1072             cmd->xfer = buf[9] | (buf[8] << 8);
1073         }
1074         break;
1075     case ATA_PASSTHROUGH_12:
1076         if (dev->type == TYPE_ROM) {
1077             /* BLANK command of MMC */
1078             cmd->xfer = 0;
1079         } else {
1080             cmd->xfer = ata_passthrough_12_xfer(dev, buf);
1081         }
1082         break;
1083     case ATA_PASSTHROUGH_16:
1084         cmd->xfer = ata_passthrough_16_xfer(dev, buf);
1085         break;
1086     }
1087     return 0;
1088 }
1089 
1090 static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1091 {
1092     switch (buf[0]) {
1093     /* stream commands */
1094     case ERASE_12:
1095     case ERASE_16:
1096         cmd->xfer = 0;
1097         break;
1098     case READ_6:
1099     case READ_REVERSE:
1100     case RECOVER_BUFFERED_DATA:
1101     case WRITE_6:
1102         cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1103         if (buf[1] & 0x01) { /* fixed */
1104             cmd->xfer *= dev->blocksize;
1105         }
1106         break;
1107     case READ_16:
1108     case READ_REVERSE_16:
1109     case VERIFY_16:
1110     case WRITE_16:
1111         cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1112         if (buf[1] & 0x01) { /* fixed */
1113             cmd->xfer *= dev->blocksize;
1114         }
1115         break;
1116     case REWIND:
1117     case LOAD_UNLOAD:
1118         cmd->xfer = 0;
1119         break;
1120     case SPACE_16:
1121         cmd->xfer = buf[13] | (buf[12] << 8);
1122         break;
1123     case READ_POSITION:
1124         switch (buf[1] & 0x1f) /* operation code */ {
1125         case SHORT_FORM_BLOCK_ID:
1126         case SHORT_FORM_VENDOR_SPECIFIC:
1127             cmd->xfer = 20;
1128             break;
1129         case LONG_FORM:
1130             cmd->xfer = 32;
1131             break;
1132         case EXTENDED_FORM:
1133             cmd->xfer = buf[8] | (buf[7] << 8);
1134             break;
1135         default:
1136             return -1;
1137         }
1138 
1139         break;
1140     case FORMAT_UNIT:
1141         cmd->xfer = buf[4] | (buf[3] << 8);
1142         break;
1143     /* generic commands */
1144     default:
1145         return scsi_req_xfer(cmd, dev, buf);
1146     }
1147     return 0;
1148 }
1149 
1150 static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1151 {
1152     switch (buf[0]) {
1153     /* medium changer commands */
1154     case EXCHANGE_MEDIUM:
1155     case INITIALIZE_ELEMENT_STATUS:
1156     case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1157     case MOVE_MEDIUM:
1158     case POSITION_TO_ELEMENT:
1159         cmd->xfer = 0;
1160         break;
1161     case READ_ELEMENT_STATUS:
1162         cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1163         break;
1164 
1165     /* generic commands */
1166     default:
1167         return scsi_req_xfer(cmd, dev, buf);
1168     }
1169     return 0;
1170 }
1171 
1172 static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1173 {
1174     switch (buf[0]) {
1175     /* Scanner commands */
1176     case OBJECT_POSITION:
1177         cmd->xfer = 0;
1178         break;
1179     case SCAN:
1180         cmd->xfer = buf[4];
1181         break;
1182     case READ_10:
1183     case SEND:
1184     case GET_WINDOW:
1185     case SET_WINDOW:
1186         cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1187         break;
1188     default:
1189         /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1190         return scsi_req_xfer(cmd, dev, buf);
1191     }
1192 
1193     return 0;
1194 }
1195 
1196 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1197 {
1198     if (!cmd->xfer) {
1199         cmd->mode = SCSI_XFER_NONE;
1200         return;
1201     }
1202     switch (cmd->buf[0]) {
1203     case WRITE_6:
1204     case WRITE_10:
1205     case WRITE_VERIFY_10:
1206     case WRITE_12:
1207     case WRITE_VERIFY_12:
1208     case WRITE_16:
1209     case WRITE_VERIFY_16:
1210     case VERIFY_10:
1211     case VERIFY_12:
1212     case VERIFY_16:
1213     case COPY:
1214     case COPY_VERIFY:
1215     case COMPARE:
1216     case CHANGE_DEFINITION:
1217     case LOG_SELECT:
1218     case MODE_SELECT:
1219     case MODE_SELECT_10:
1220     case SEND_DIAGNOSTIC:
1221     case WRITE_BUFFER:
1222     case FORMAT_UNIT:
1223     case REASSIGN_BLOCKS:
1224     case SEARCH_EQUAL:
1225     case SEARCH_HIGH:
1226     case SEARCH_LOW:
1227     case UPDATE_BLOCK:
1228     case WRITE_LONG_10:
1229     case WRITE_SAME_10:
1230     case WRITE_SAME_16:
1231     case UNMAP:
1232     case SEARCH_HIGH_12:
1233     case SEARCH_EQUAL_12:
1234     case SEARCH_LOW_12:
1235     case MEDIUM_SCAN:
1236     case SEND_VOLUME_TAG:
1237     case SEND_CUE_SHEET:
1238     case SEND_DVD_STRUCTURE:
1239     case PERSISTENT_RESERVE_OUT:
1240     case MAINTENANCE_OUT:
1241     case SET_WINDOW:
1242     case SCAN:
1243         /* SCAN conflicts with START_STOP.  START_STOP has cmd->xfer set to 0 for
1244          * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1245          */
1246         cmd->mode = SCSI_XFER_TO_DEV;
1247         break;
1248     case ATA_PASSTHROUGH_12:
1249     case ATA_PASSTHROUGH_16:
1250         /* T_DIR */
1251         cmd->mode = (cmd->buf[2] & 0x8) ?
1252                    SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1253         break;
1254     default:
1255         cmd->mode = SCSI_XFER_FROM_DEV;
1256         break;
1257     }
1258 }
1259 
1260 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
1261 {
1262     int rc;
1263     int len;
1264 
1265     cmd->lba = -1;
1266     len = scsi_cdb_length(buf);
1267     if (len < 0) {
1268         return -1;
1269     }
1270 
1271     cmd->len = len;
1272     switch (dev->type) {
1273     case TYPE_TAPE:
1274         rc = scsi_req_stream_xfer(cmd, dev, buf);
1275         break;
1276     case TYPE_MEDIUM_CHANGER:
1277         rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
1278         break;
1279     case TYPE_SCANNER:
1280         rc = scsi_req_scanner_length(cmd, dev, buf);
1281         break;
1282     default:
1283         rc = scsi_req_xfer(cmd, dev, buf);
1284         break;
1285     }
1286 
1287     if (rc != 0)
1288         return rc;
1289 
1290     memcpy(cmd->buf, buf, cmd->len);
1291     scsi_cmd_xfer_mode(cmd);
1292     cmd->lba = scsi_cmd_lba(cmd);
1293     return 0;
1294 }
1295 
1296 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1297 {
1298     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1299 
1300     scsi_device_set_ua(dev, sense);
1301     if (bus->info->change) {
1302         bus->info->change(bus, dev, sense);
1303     }
1304 }
1305 
1306 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1307 {
1308     assert(req->refcount > 0);
1309     req->refcount++;
1310     return req;
1311 }
1312 
1313 void scsi_req_unref(SCSIRequest *req)
1314 {
1315     assert(req->refcount > 0);
1316     if (--req->refcount == 0) {
1317         BusState *qbus = req->dev->qdev.parent_bus;
1318         SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1319 
1320         if (bus->info->free_request && req->hba_private) {
1321             bus->info->free_request(bus, req->hba_private);
1322         }
1323         if (req->ops->free_req) {
1324             req->ops->free_req(req);
1325         }
1326         object_unref(OBJECT(req->dev));
1327         object_unref(OBJECT(qbus->parent));
1328         g_free(req);
1329     }
1330 }
1331 
1332 /* Tell the device that we finished processing this chunk of I/O.  It
1333    will start the next chunk or complete the command.  */
1334 void scsi_req_continue(SCSIRequest *req)
1335 {
1336     if (req->io_canceled) {
1337         trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1338         return;
1339     }
1340     trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1341     if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1342         req->ops->write_data(req);
1343     } else {
1344         req->ops->read_data(req);
1345     }
1346 }
1347 
1348 /* Called by the devices when data is ready for the HBA.  The HBA should
1349    start a DMA operation to read or fill the device's data buffer.
1350    Once it completes, calling scsi_req_continue will restart I/O.  */
1351 void scsi_req_data(SCSIRequest *req, int len)
1352 {
1353     uint8_t *buf;
1354     if (req->io_canceled) {
1355         trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1356         return;
1357     }
1358     trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1359     assert(req->cmd.mode != SCSI_XFER_NONE);
1360     if (!req->sg) {
1361         req->resid -= len;
1362         req->bus->info->transfer_data(req, len);
1363         return;
1364     }
1365 
1366     /* If the device calls scsi_req_data and the HBA specified a
1367      * scatter/gather list, the transfer has to happen in a single
1368      * step.  */
1369     assert(!req->dma_started);
1370     req->dma_started = true;
1371 
1372     buf = scsi_req_get_buf(req);
1373     if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1374         req->resid = dma_buf_read(buf, len, req->sg);
1375     } else {
1376         req->resid = dma_buf_write(buf, len, req->sg);
1377     }
1378     scsi_req_continue(req);
1379 }
1380 
1381 void scsi_req_print(SCSIRequest *req)
1382 {
1383     FILE *fp = stderr;
1384     int i;
1385 
1386     fprintf(fp, "[%s id=%d] %s",
1387             req->dev->qdev.parent_bus->name,
1388             req->dev->id,
1389             scsi_command_name(req->cmd.buf[0]));
1390     for (i = 1; i < req->cmd.len; i++) {
1391         fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1392     }
1393     switch (req->cmd.mode) {
1394     case SCSI_XFER_NONE:
1395         fprintf(fp, " - none\n");
1396         break;
1397     case SCSI_XFER_FROM_DEV:
1398         fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1399         break;
1400     case SCSI_XFER_TO_DEV:
1401         fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1402         break;
1403     default:
1404         fprintf(fp, " - Oops\n");
1405         break;
1406     }
1407 }
1408 
1409 void scsi_req_complete(SCSIRequest *req, int status)
1410 {
1411     assert(req->status == -1);
1412     req->status = status;
1413 
1414     assert(req->sense_len <= sizeof(req->sense));
1415     if (status == GOOD) {
1416         req->sense_len = 0;
1417     }
1418 
1419     if (req->sense_len) {
1420         memcpy(req->dev->sense, req->sense, req->sense_len);
1421         req->dev->sense_len = req->sense_len;
1422         req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1423     } else {
1424         req->dev->sense_len = 0;
1425         req->dev->sense_is_ua = false;
1426     }
1427 
1428     /*
1429      * Unit attention state is now stored in the device's sense buffer
1430      * if the HBA didn't do autosense.  Clear the pending unit attention
1431      * flags.
1432      */
1433     scsi_clear_unit_attention(req);
1434 
1435     scsi_req_ref(req);
1436     scsi_req_dequeue(req);
1437     req->bus->info->complete(req, req->status, req->resid);
1438 
1439     /* Cancelled requests might end up being completed instead of cancelled */
1440     notifier_list_notify(&req->cancel_notifiers, req);
1441     scsi_req_unref(req);
1442 }
1443 
1444 /* Called by the devices when the request is canceled. */
1445 void scsi_req_cancel_complete(SCSIRequest *req)
1446 {
1447     assert(req->io_canceled);
1448     if (req->bus->info->cancel) {
1449         req->bus->info->cancel(req);
1450     }
1451     notifier_list_notify(&req->cancel_notifiers, req);
1452     scsi_req_unref(req);
1453 }
1454 
1455 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1456  * notifier list, the bus will be notified the requests cancellation is
1457  * completed.
1458  * */
1459 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1460 {
1461     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1462     if (notifier) {
1463         notifier_list_add(&req->cancel_notifiers, notifier);
1464     }
1465     if (req->io_canceled) {
1466         /* A blk_aio_cancel_async is pending; when it finishes,
1467          * scsi_req_cancel_complete will be called and will
1468          * call the notifier we just added.  Just wait for that.
1469          */
1470         assert(req->aiocb);
1471         return;
1472     }
1473     /* Dropped in scsi_req_cancel_complete.  */
1474     scsi_req_ref(req);
1475     scsi_req_dequeue(req);
1476     req->io_canceled = true;
1477     if (req->aiocb) {
1478         blk_aio_cancel_async(req->aiocb);
1479     } else {
1480         scsi_req_cancel_complete(req);
1481     }
1482 }
1483 
1484 void scsi_req_cancel(SCSIRequest *req)
1485 {
1486     trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1487     if (!req->enqueued) {
1488         return;
1489     }
1490     assert(!req->io_canceled);
1491     /* Dropped in scsi_req_cancel_complete.  */
1492     scsi_req_ref(req);
1493     scsi_req_dequeue(req);
1494     req->io_canceled = true;
1495     if (req->aiocb) {
1496         blk_aio_cancel(req->aiocb);
1497     } else {
1498         scsi_req_cancel_complete(req);
1499     }
1500 }
1501 
1502 static int scsi_ua_precedence(SCSISense sense)
1503 {
1504     if (sense.key != UNIT_ATTENTION) {
1505         return INT_MAX;
1506     }
1507     if (sense.asc == 0x29 && sense.ascq == 0x04) {
1508         /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1509         return 1;
1510     } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1511         /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1512         return 2;
1513     } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1514         /* These two go with "all others". */
1515         ;
1516     } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1517         /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1518          * POWER ON OCCURRED = 1
1519          * SCSI BUS RESET OCCURRED = 2
1520          * BUS DEVICE RESET FUNCTION OCCURRED = 3
1521          * I_T NEXUS LOSS OCCURRED = 7
1522          */
1523         return sense.ascq;
1524     } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1525         /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION  */
1526         return 8;
1527     }
1528     return (sense.asc << 8) | sense.ascq;
1529 }
1530 
1531 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1532 {
1533     int prec1, prec2;
1534     if (sense.key != UNIT_ATTENTION) {
1535         return;
1536     }
1537     trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1538                              sense.asc, sense.ascq);
1539 
1540     /*
1541      * Override a pre-existing unit attention condition, except for a more
1542      * important reset condition.
1543     */
1544     prec1 = scsi_ua_precedence(sdev->unit_attention);
1545     prec2 = scsi_ua_precedence(sense);
1546     if (prec2 < prec1) {
1547         sdev->unit_attention = sense;
1548     }
1549 }
1550 
1551 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1552 {
1553     SCSIRequest *req;
1554 
1555     aio_context_acquire(blk_get_aio_context(sdev->conf.blk));
1556     while (!QTAILQ_EMPTY(&sdev->requests)) {
1557         req = QTAILQ_FIRST(&sdev->requests);
1558         scsi_req_cancel_async(req, NULL);
1559     }
1560     blk_drain(sdev->conf.blk);
1561     aio_context_release(blk_get_aio_context(sdev->conf.blk));
1562     scsi_device_set_ua(sdev, sense);
1563 }
1564 
1565 static char *scsibus_get_dev_path(DeviceState *dev)
1566 {
1567     SCSIDevice *d = SCSI_DEVICE(dev);
1568     DeviceState *hba = dev->parent_bus->parent;
1569     char *id;
1570     char *path;
1571 
1572     id = qdev_get_dev_path(hba);
1573     if (id) {
1574         path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1575     } else {
1576         path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1577     }
1578     g_free(id);
1579     return path;
1580 }
1581 
1582 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1583 {
1584     SCSIDevice *d = SCSI_DEVICE(dev);
1585     return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1586                            qdev_fw_name(dev), d->id, d->lun);
1587 }
1588 
1589 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1590 {
1591     BusChild *kid;
1592     SCSIDevice *target_dev = NULL;
1593 
1594     QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) {
1595         DeviceState *qdev = kid->child;
1596         SCSIDevice *dev = SCSI_DEVICE(qdev);
1597 
1598         if (dev->channel == channel && dev->id == id) {
1599             if (dev->lun == lun) {
1600                 return dev;
1601             }
1602             target_dev = dev;
1603         }
1604     }
1605     return target_dev;
1606 }
1607 
1608 /* SCSI request list.  For simplicity, pv points to the whole device */
1609 
1610 static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
1611                              VMStateField *field, QJSON *vmdesc)
1612 {
1613     SCSIDevice *s = pv;
1614     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1615     SCSIRequest *req;
1616 
1617     QTAILQ_FOREACH(req, &s->requests, next) {
1618         assert(!req->io_canceled);
1619         assert(req->status == -1);
1620         assert(req->enqueued);
1621 
1622         qemu_put_sbyte(f, req->retry ? 1 : 2);
1623         qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1624         qemu_put_be32s(f, &req->tag);
1625         qemu_put_be32s(f, &req->lun);
1626         if (bus->info->save_request) {
1627             bus->info->save_request(f, req);
1628         }
1629         if (req->ops->save_request) {
1630             req->ops->save_request(f, req);
1631         }
1632     }
1633     qemu_put_sbyte(f, 0);
1634 
1635     return 0;
1636 }
1637 
1638 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
1639                              VMStateField *field)
1640 {
1641     SCSIDevice *s = pv;
1642     SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1643     int8_t sbyte;
1644 
1645     while ((sbyte = qemu_get_sbyte(f)) > 0) {
1646         uint8_t buf[SCSI_CMD_BUF_SIZE];
1647         uint32_t tag;
1648         uint32_t lun;
1649         SCSIRequest *req;
1650 
1651         qemu_get_buffer(f, buf, sizeof(buf));
1652         qemu_get_be32s(f, &tag);
1653         qemu_get_be32s(f, &lun);
1654         req = scsi_req_new(s, tag, lun, buf, NULL);
1655         req->retry = (sbyte == 1);
1656         if (bus->info->load_request) {
1657             req->hba_private = bus->info->load_request(f, req);
1658         }
1659         if (req->ops->load_request) {
1660             req->ops->load_request(f, req);
1661         }
1662 
1663         /* Just restart it later.  */
1664         scsi_req_enqueue_internal(req);
1665 
1666         /* At this point, the request will be kept alive by the reference
1667          * added by scsi_req_enqueue_internal, so we can release our reference.
1668          * The HBA of course will add its own reference in the load_request
1669          * callback if it needs to hold on the SCSIRequest.
1670          */
1671         scsi_req_unref(req);
1672     }
1673 
1674     return 0;
1675 }
1676 
1677 static const VMStateInfo vmstate_info_scsi_requests = {
1678     .name = "scsi-requests",
1679     .get  = get_scsi_requests,
1680     .put  = put_scsi_requests,
1681 };
1682 
1683 static bool scsi_sense_state_needed(void *opaque)
1684 {
1685     SCSIDevice *s = opaque;
1686 
1687     return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1688 }
1689 
1690 static const VMStateDescription vmstate_scsi_sense_state = {
1691     .name = "SCSIDevice/sense",
1692     .version_id = 1,
1693     .minimum_version_id = 1,
1694     .needed = scsi_sense_state_needed,
1695     .fields = (VMStateField[]) {
1696         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1697                                 SCSI_SENSE_BUF_SIZE_OLD,
1698                                 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1699         VMSTATE_END_OF_LIST()
1700     }
1701 };
1702 
1703 const VMStateDescription vmstate_scsi_device = {
1704     .name = "SCSIDevice",
1705     .version_id = 1,
1706     .minimum_version_id = 1,
1707     .fields = (VMStateField[]) {
1708         VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1709         VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1710         VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1711         VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1712         VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
1713         VMSTATE_UINT32(sense_len, SCSIDevice),
1714         {
1715             .name         = "requests",
1716             .version_id   = 0,
1717             .field_exists = NULL,
1718             .size         = 0,   /* ouch */
1719             .info         = &vmstate_info_scsi_requests,
1720             .flags        = VMS_SINGLE,
1721             .offset       = 0,
1722         },
1723         VMSTATE_END_OF_LIST()
1724     },
1725     .subsections = (const VMStateDescription*[]) {
1726         &vmstate_scsi_sense_state,
1727         NULL
1728     }
1729 };
1730 
1731 static void scsi_device_class_init(ObjectClass *klass, void *data)
1732 {
1733     DeviceClass *k = DEVICE_CLASS(klass);
1734     set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1735     k->bus_type  = TYPE_SCSI_BUS;
1736     k->realize   = scsi_qdev_realize;
1737     k->unrealize = scsi_qdev_unrealize;
1738     k->props     = scsi_props;
1739 }
1740 
1741 static void scsi_dev_instance_init(Object *obj)
1742 {
1743     DeviceState *dev = DEVICE(obj);
1744     SCSIDevice *s = SCSI_DEVICE(dev);
1745 
1746     device_add_bootindex_property(obj, &s->conf.bootindex,
1747                                   "bootindex", NULL,
1748                                   &s->qdev, NULL);
1749 }
1750 
1751 static const TypeInfo scsi_device_type_info = {
1752     .name = TYPE_SCSI_DEVICE,
1753     .parent = TYPE_DEVICE,
1754     .instance_size = sizeof(SCSIDevice),
1755     .abstract = true,
1756     .class_size = sizeof(SCSIDeviceClass),
1757     .class_init = scsi_device_class_init,
1758     .instance_init = scsi_dev_instance_init,
1759 };
1760 
1761 static void scsi_register_types(void)
1762 {
1763     type_register_static(&scsi_bus_info);
1764     type_register_static(&scsi_device_type_info);
1765 }
1766 
1767 type_init(scsi_register_types)
1768