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