Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2020-05-30' into...
[qemu/ar7.git] / hw / scsi / scsi-bus.c
blob2836f807a0476cad746a8bdfbf895caecd385827
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 "hw/qdev-properties.h"
7 #include "hw/scsi/scsi.h"
8 #include "migration/qemu-file-types.h"
9 #include "migration/vmstate.h"
10 #include "scsi/constants.h"
11 #include "sysemu/block-backend.h"
12 #include "sysemu/blockdev.h"
13 #include "sysemu/sysemu.h"
14 #include "sysemu/runstate.h"
15 #include "trace.h"
16 #include "sysemu/dma.h"
17 #include "qemu/cutils.h"
19 static char *scsibus_get_dev_path(DeviceState *dev);
20 static char *scsibus_get_fw_dev_path(DeviceState *dev);
21 static void scsi_req_dequeue(SCSIRequest *req);
22 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
23 static void scsi_target_free_buf(SCSIRequest *req);
25 static Property scsi_props[] = {
26 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
27 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
28 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
29 DEFINE_PROP_END_OF_LIST(),
32 static void scsi_bus_class_init(ObjectClass *klass, void *data)
34 BusClass *k = BUS_CLASS(klass);
35 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
37 k->get_dev_path = scsibus_get_dev_path;
38 k->get_fw_dev_path = scsibus_get_fw_dev_path;
39 hc->unplug = qdev_simple_device_unplug_cb;
42 static const TypeInfo scsi_bus_info = {
43 .name = TYPE_SCSI_BUS,
44 .parent = TYPE_BUS,
45 .instance_size = sizeof(SCSIBus),
46 .class_init = scsi_bus_class_init,
47 .interfaces = (InterfaceInfo[]) {
48 { TYPE_HOTPLUG_HANDLER },
49 { }
52 static int next_scsi_bus;
54 static void scsi_device_realize(SCSIDevice *s, Error **errp)
56 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
57 if (sc->realize) {
58 sc->realize(s, errp);
62 static void scsi_device_unrealize(SCSIDevice *s)
64 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
65 if (sc->unrealize) {
66 sc->unrealize(s);
70 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
71 void *hba_private)
73 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
74 int rc;
76 assert(cmd->len == 0);
77 rc = scsi_req_parse_cdb(dev, cmd, buf);
78 if (bus->info->parse_cdb) {
79 rc = bus->info->parse_cdb(dev, cmd, buf, hba_private);
81 return rc;
84 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
85 uint8_t *buf, void *hba_private)
87 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
88 if (sc->alloc_req) {
89 return sc->alloc_req(s, tag, lun, buf, hba_private);
92 return NULL;
95 void scsi_device_unit_attention_reported(SCSIDevice *s)
97 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
98 if (sc->unit_attention_reported) {
99 sc->unit_attention_reported(s);
103 /* Create a scsi bus, and attach devices to it. */
104 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
105 const SCSIBusInfo *info, const char *bus_name)
107 qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
108 bus->busnr = next_scsi_bus++;
109 bus->info = info;
110 qbus_set_bus_hotplug_handler(BUS(bus), &error_abort);
113 static void scsi_dma_restart_bh(void *opaque)
115 SCSIDevice *s = opaque;
116 SCSIRequest *req, *next;
118 qemu_bh_delete(s->bh);
119 s->bh = NULL;
121 aio_context_acquire(blk_get_aio_context(s->conf.blk));
122 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
123 scsi_req_ref(req);
124 if (req->retry) {
125 req->retry = false;
126 switch (req->cmd.mode) {
127 case SCSI_XFER_FROM_DEV:
128 case SCSI_XFER_TO_DEV:
129 scsi_req_continue(req);
130 break;
131 case SCSI_XFER_NONE:
132 scsi_req_dequeue(req);
133 scsi_req_enqueue(req);
134 break;
137 scsi_req_unref(req);
139 aio_context_release(blk_get_aio_context(s->conf.blk));
142 void scsi_req_retry(SCSIRequest *req)
144 /* No need to save a reference, because scsi_dma_restart_bh just
145 * looks at the request list. */
146 req->retry = true;
149 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
151 SCSIDevice *s = opaque;
153 if (!running) {
154 return;
156 if (!s->bh) {
157 AioContext *ctx = blk_get_aio_context(s->conf.blk);
158 s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s);
159 qemu_bh_schedule(s->bh);
163 static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
165 SCSIDevice *dev = SCSI_DEVICE(qdev);
166 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
167 SCSIDevice *d;
168 Error *local_err = NULL;
170 if (dev->channel > bus->info->max_channel) {
171 error_setg(errp, "bad scsi channel id: %d", dev->channel);
172 return;
174 if (dev->id != -1 && dev->id > bus->info->max_target) {
175 error_setg(errp, "bad scsi device id: %d", dev->id);
176 return;
178 if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
179 error_setg(errp, "bad scsi device lun: %d", dev->lun);
180 return;
183 if (dev->id == -1) {
184 int id = -1;
185 if (dev->lun == -1) {
186 dev->lun = 0;
188 do {
189 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
190 } while (d && d->lun == dev->lun && id < bus->info->max_target);
191 if (d && d->lun == dev->lun) {
192 error_setg(errp, "no free target");
193 return;
195 dev->id = id;
196 } else if (dev->lun == -1) {
197 int lun = -1;
198 do {
199 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
200 } while (d && d->lun == lun && lun < bus->info->max_lun);
201 if (d && d->lun == lun) {
202 error_setg(errp, "no free lun");
203 return;
205 dev->lun = lun;
206 } else {
207 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
208 assert(d);
209 if (d->lun == dev->lun && dev != d) {
210 error_setg(errp, "lun already used by '%s'", d->qdev.id);
211 return;
215 QTAILQ_INIT(&dev->requests);
216 scsi_device_realize(dev, &local_err);
217 if (local_err) {
218 error_propagate(errp, local_err);
219 return;
221 dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
222 scsi_dma_restart_cb, dev);
225 static void scsi_qdev_unrealize(DeviceState *qdev)
227 SCSIDevice *dev = SCSI_DEVICE(qdev);
229 if (dev->vmsentry) {
230 qemu_del_vm_change_state_handler(dev->vmsentry);
233 scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
235 scsi_device_unrealize(dev);
237 blockdev_mark_auto_del(dev->conf.blk);
240 /* handle legacy '-drive if=scsi,...' cmd line args */
241 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
242 int unit, bool removable, int bootindex,
243 bool share_rw,
244 BlockdevOnError rerror,
245 BlockdevOnError werror,
246 const char *serial, Error **errp)
248 const char *driver;
249 char *name;
250 DeviceState *dev;
251 Error *err = NULL;
252 DriveInfo *dinfo;
254 if (blk_is_sg(blk)) {
255 driver = "scsi-generic";
256 } else {
257 dinfo = blk_legacy_dinfo(blk);
258 if (dinfo && dinfo->media_cd) {
259 driver = "scsi-cd";
260 } else {
261 driver = "scsi-hd";
264 dev = qdev_create(&bus->qbus, driver);
265 name = g_strdup_printf("legacy[%d]", unit);
266 object_property_add_child(OBJECT(bus), name, OBJECT(dev));
267 g_free(name);
269 qdev_prop_set_uint32(dev, "scsi-id", unit);
270 if (bootindex >= 0) {
271 object_property_set_int(OBJECT(dev), bootindex, "bootindex",
272 &error_abort);
274 if (object_property_find(OBJECT(dev), "removable", NULL)) {
275 qdev_prop_set_bit(dev, "removable", removable);
277 if (serial && object_property_find(OBJECT(dev), "serial", NULL)) {
278 qdev_prop_set_string(dev, "serial", serial);
280 qdev_prop_set_drive(dev, "drive", blk, &err);
281 if (err) {
282 error_propagate(errp, err);
283 object_unparent(OBJECT(dev));
284 return NULL;
286 object_property_set_bool(OBJECT(dev), share_rw, "share-rw", &err);
287 if (err != NULL) {
288 error_propagate(errp, err);
289 object_unparent(OBJECT(dev));
290 return NULL;
293 qdev_prop_set_enum(dev, "rerror", rerror);
294 qdev_prop_set_enum(dev, "werror", werror);
296 object_property_set_bool(OBJECT(dev), true, "realized", &err);
297 if (err != NULL) {
298 error_propagate(errp, err);
299 object_unparent(OBJECT(dev));
300 return NULL;
302 return SCSI_DEVICE(dev);
305 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
307 Location loc;
308 DriveInfo *dinfo;
309 int unit;
311 loc_push_none(&loc);
312 for (unit = 0; unit <= bus->info->max_target; unit++) {
313 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
314 if (dinfo == NULL) {
315 continue;
317 qemu_opts_loc_restore(dinfo->opts);
318 scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
319 unit, false, -1, false,
320 BLOCKDEV_ON_ERROR_AUTO,
321 BLOCKDEV_ON_ERROR_AUTO,
322 NULL, &error_fatal);
324 loc_pop(&loc);
327 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
329 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
330 scsi_req_complete(req, CHECK_CONDITION);
331 return 0;
334 static const struct SCSIReqOps reqops_invalid_field = {
335 .size = sizeof(SCSIRequest),
336 .send_command = scsi_invalid_field
339 /* SCSIReqOps implementation for invalid commands. */
341 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
343 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
344 scsi_req_complete(req, CHECK_CONDITION);
345 return 0;
348 static const struct SCSIReqOps reqops_invalid_opcode = {
349 .size = sizeof(SCSIRequest),
350 .send_command = scsi_invalid_command
353 /* SCSIReqOps implementation for unit attention conditions. */
355 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
357 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
358 scsi_req_build_sense(req, req->dev->unit_attention);
359 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
360 scsi_req_build_sense(req, req->bus->unit_attention);
362 scsi_req_complete(req, CHECK_CONDITION);
363 return 0;
366 static const struct SCSIReqOps reqops_unit_attention = {
367 .size = sizeof(SCSIRequest),
368 .send_command = scsi_unit_attention
371 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
372 an invalid LUN. */
374 typedef struct SCSITargetReq SCSITargetReq;
376 struct SCSITargetReq {
377 SCSIRequest req;
378 int len;
379 uint8_t *buf;
380 int buf_len;
383 static void store_lun(uint8_t *outbuf, int lun)
385 if (lun < 256) {
386 outbuf[1] = lun;
387 return;
389 outbuf[1] = (lun & 255);
390 outbuf[0] = (lun >> 8) | 0x40;
393 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
395 BusChild *kid;
396 int i, len, n;
397 int channel, id;
398 bool found_lun0;
400 if (r->req.cmd.xfer < 16) {
401 return false;
403 if (r->req.cmd.buf[2] > 2) {
404 return false;
406 channel = r->req.dev->channel;
407 id = r->req.dev->id;
408 found_lun0 = false;
409 n = 0;
410 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
411 DeviceState *qdev = kid->child;
412 SCSIDevice *dev = SCSI_DEVICE(qdev);
414 if (dev->channel == channel && dev->id == id) {
415 if (dev->lun == 0) {
416 found_lun0 = true;
418 n += 8;
421 if (!found_lun0) {
422 n += 8;
425 scsi_target_alloc_buf(&r->req, n + 8);
427 len = MIN(n + 8, r->req.cmd.xfer & ~7);
428 memset(r->buf, 0, len);
429 stl_be_p(&r->buf[0], n);
430 i = found_lun0 ? 8 : 16;
431 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
432 DeviceState *qdev = kid->child;
433 SCSIDevice *dev = SCSI_DEVICE(qdev);
435 if (dev->channel == channel && dev->id == id) {
436 store_lun(&r->buf[i], dev->lun);
437 i += 8;
440 assert(i == n + 8);
441 r->len = len;
442 return true;
445 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
447 assert(r->req.dev->lun != r->req.lun);
449 scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
451 if (r->req.cmd.buf[1] & 0x2) {
452 /* Command support data - optional, not implemented */
453 return false;
456 if (r->req.cmd.buf[1] & 0x1) {
457 /* Vital product data */
458 uint8_t page_code = r->req.cmd.buf[2];
459 r->buf[r->len++] = page_code ; /* this page */
460 r->buf[r->len++] = 0x00;
462 switch (page_code) {
463 case 0x00: /* Supported page codes, mandatory */
465 int pages;
466 pages = r->len++;
467 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
468 r->buf[pages] = r->len - pages - 1; /* number of pages */
469 break;
471 default:
472 return false;
474 /* done with EVPD */
475 assert(r->len < r->buf_len);
476 r->len = MIN(r->req.cmd.xfer, r->len);
477 return true;
480 /* Standard INQUIRY data */
481 if (r->req.cmd.buf[2] != 0) {
482 return false;
485 /* PAGE CODE == 0 */
486 r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
487 memset(r->buf, 0, r->len);
488 if (r->req.lun != 0) {
489 r->buf[0] = TYPE_NO_LUN;
490 } else {
491 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
492 r->buf[2] = 5; /* Version */
493 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
494 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
495 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
496 memcpy(&r->buf[8], "QEMU ", 8);
497 memcpy(&r->buf[16], "QEMU TARGET ", 16);
498 pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
500 return true;
503 static size_t scsi_sense_len(SCSIRequest *req)
505 if (req->dev->type == TYPE_SCANNER)
506 return SCSI_SENSE_LEN_SCANNER;
507 else
508 return SCSI_SENSE_LEN;
511 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
513 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
514 int fixed_sense = (req->cmd.buf[1] & 1) == 0;
516 if (req->lun != 0 &&
517 buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
518 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
519 scsi_req_complete(req, CHECK_CONDITION);
520 return 0;
522 switch (buf[0]) {
523 case REPORT_LUNS:
524 if (!scsi_target_emulate_report_luns(r)) {
525 goto illegal_request;
527 break;
528 case INQUIRY:
529 if (!scsi_target_emulate_inquiry(r)) {
530 goto illegal_request;
532 break;
533 case REQUEST_SENSE:
534 scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
535 if (req->lun != 0) {
536 const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
538 r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
539 sense, fixed_sense);
540 } else {
541 r->len = scsi_device_get_sense(r->req.dev, r->buf,
542 MIN(req->cmd.xfer, r->buf_len),
543 fixed_sense);
545 if (r->req.dev->sense_is_ua) {
546 scsi_device_unit_attention_reported(req->dev);
547 r->req.dev->sense_len = 0;
548 r->req.dev->sense_is_ua = false;
550 break;
551 case TEST_UNIT_READY:
552 break;
553 default:
554 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
555 scsi_req_complete(req, CHECK_CONDITION);
556 return 0;
557 illegal_request:
558 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
559 scsi_req_complete(req, CHECK_CONDITION);
560 return 0;
563 if (!r->len) {
564 scsi_req_complete(req, GOOD);
566 return r->len;
569 static void scsi_target_read_data(SCSIRequest *req)
571 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
572 uint32_t n;
574 n = r->len;
575 if (n > 0) {
576 r->len = 0;
577 scsi_req_data(&r->req, n);
578 } else {
579 scsi_req_complete(&r->req, GOOD);
583 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
585 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
587 return r->buf;
590 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
592 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
594 r->buf = g_malloc(len);
595 r->buf_len = len;
597 return r->buf;
600 static void scsi_target_free_buf(SCSIRequest *req)
602 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
604 g_free(r->buf);
607 static const struct SCSIReqOps reqops_target_command = {
608 .size = sizeof(SCSITargetReq),
609 .send_command = scsi_target_send_command,
610 .read_data = scsi_target_read_data,
611 .get_buf = scsi_target_get_buf,
612 .free_req = scsi_target_free_buf,
616 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
617 uint32_t tag, uint32_t lun, void *hba_private)
619 SCSIRequest *req;
620 SCSIBus *bus = scsi_bus_from_device(d);
621 BusState *qbus = BUS(bus);
622 const int memset_off = offsetof(SCSIRequest, sense)
623 + sizeof(req->sense);
625 req = g_malloc(reqops->size);
626 memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
627 req->refcount = 1;
628 req->bus = bus;
629 req->dev = d;
630 req->tag = tag;
631 req->lun = lun;
632 req->hba_private = hba_private;
633 req->status = -1;
634 req->ops = reqops;
635 object_ref(OBJECT(d));
636 object_ref(OBJECT(qbus->parent));
637 notifier_list_init(&req->cancel_notifiers);
638 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
639 return req;
642 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
643 uint8_t *buf, void *hba_private)
645 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
646 const SCSIReqOps *ops;
647 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
648 SCSIRequest *req;
649 SCSICommand cmd = { .len = 0 };
650 int ret;
652 if ((d->unit_attention.key == UNIT_ATTENTION ||
653 bus->unit_attention.key == UNIT_ATTENTION) &&
654 (buf[0] != INQUIRY &&
655 buf[0] != REPORT_LUNS &&
656 buf[0] != GET_CONFIGURATION &&
657 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
660 * If we already have a pending unit attention condition,
661 * report this one before triggering another one.
663 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
664 ops = &reqops_unit_attention;
665 } else if (lun != d->lun ||
666 buf[0] == REPORT_LUNS ||
667 (buf[0] == REQUEST_SENSE && d->sense_len)) {
668 ops = &reqops_target_command;
669 } else {
670 ops = NULL;
673 if (ops != NULL || !sc->parse_cdb) {
674 ret = scsi_req_parse_cdb(d, &cmd, buf);
675 } else {
676 ret = sc->parse_cdb(d, &cmd, buf, hba_private);
679 if (ret != 0) {
680 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
681 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
682 } else {
683 assert(cmd.len != 0);
684 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
685 cmd.mode, cmd.xfer);
686 if (cmd.lba != -1) {
687 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
688 cmd.lba);
691 if (cmd.xfer > INT32_MAX) {
692 req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
693 } else if (ops) {
694 req = scsi_req_alloc(ops, d, tag, lun, hba_private);
695 } else {
696 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
700 req->cmd = cmd;
701 req->resid = req->cmd.xfer;
703 switch (buf[0]) {
704 case INQUIRY:
705 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
706 break;
707 case TEST_UNIT_READY:
708 trace_scsi_test_unit_ready(d->id, lun, tag);
709 break;
710 case REPORT_LUNS:
711 trace_scsi_report_luns(d->id, lun, tag);
712 break;
713 case REQUEST_SENSE:
714 trace_scsi_request_sense(d->id, lun, tag);
715 break;
716 default:
717 break;
720 return req;
723 uint8_t *scsi_req_get_buf(SCSIRequest *req)
725 return req->ops->get_buf(req);
728 static void scsi_clear_unit_attention(SCSIRequest *req)
730 SCSISense *ua;
731 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
732 req->bus->unit_attention.key != UNIT_ATTENTION) {
733 return;
737 * If an INQUIRY command enters the enabled command state,
738 * the device server shall [not] clear any unit attention condition;
739 * See also MMC-6, paragraphs 6.5 and 6.6.2.
741 if (req->cmd.buf[0] == INQUIRY ||
742 req->cmd.buf[0] == GET_CONFIGURATION ||
743 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
744 return;
747 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
748 ua = &req->dev->unit_attention;
749 } else {
750 ua = &req->bus->unit_attention;
754 * If a REPORT LUNS command enters the enabled command state, [...]
755 * the device server shall clear any pending unit attention condition
756 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
758 if (req->cmd.buf[0] == REPORT_LUNS &&
759 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
760 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
761 return;
764 *ua = SENSE_CODE(NO_SENSE);
767 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
769 int ret;
771 assert(len >= 14);
772 if (!req->sense_len) {
773 return 0;
776 ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
779 * FIXME: clearing unit attention conditions upon autosense should be done
780 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
781 * (SAM-5, 5.14).
783 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
784 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
785 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
787 if (req->dev->sense_is_ua) {
788 scsi_device_unit_attention_reported(req->dev);
789 req->dev->sense_len = 0;
790 req->dev->sense_is_ua = false;
792 return ret;
795 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
797 return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
800 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
802 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
803 sense.key, sense.asc, sense.ascq);
804 req->sense_len = scsi_build_sense(req->sense, sense);
807 static void scsi_req_enqueue_internal(SCSIRequest *req)
809 assert(!req->enqueued);
810 scsi_req_ref(req);
811 if (req->bus->info->get_sg_list) {
812 req->sg = req->bus->info->get_sg_list(req);
813 } else {
814 req->sg = NULL;
816 req->enqueued = true;
817 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
820 int32_t scsi_req_enqueue(SCSIRequest *req)
822 int32_t rc;
824 assert(!req->retry);
825 scsi_req_enqueue_internal(req);
826 scsi_req_ref(req);
827 rc = req->ops->send_command(req, req->cmd.buf);
828 scsi_req_unref(req);
829 return rc;
832 static void scsi_req_dequeue(SCSIRequest *req)
834 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
835 req->retry = false;
836 if (req->enqueued) {
837 QTAILQ_REMOVE(&req->dev->requests, req, next);
838 req->enqueued = false;
839 scsi_req_unref(req);
843 static int scsi_get_performance_length(int num_desc, int type, int data_type)
845 /* MMC-6, paragraph 6.7. */
846 switch (type) {
847 case 0:
848 if ((data_type & 3) == 0) {
849 /* Each descriptor is as in Table 295 - Nominal performance. */
850 return 16 * num_desc + 8;
851 } else {
852 /* Each descriptor is as in Table 296 - Exceptions. */
853 return 6 * num_desc + 8;
855 case 1:
856 case 4:
857 case 5:
858 return 8 * num_desc + 8;
859 case 2:
860 return 2048 * num_desc + 8;
861 case 3:
862 return 16 * num_desc + 8;
863 default:
864 return 8;
868 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
870 int byte_block = (buf[2] >> 2) & 0x1;
871 int type = (buf[2] >> 4) & 0x1;
872 int xfer_unit;
874 if (byte_block) {
875 if (type) {
876 xfer_unit = dev->blocksize;
877 } else {
878 xfer_unit = 512;
880 } else {
881 xfer_unit = 1;
884 return xfer_unit;
887 static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
889 int length = buf[2] & 0x3;
890 int xfer;
891 int unit = ata_passthrough_xfer_unit(dev, buf);
893 switch (length) {
894 case 0:
895 case 3: /* USB-specific. */
896 default:
897 xfer = 0;
898 break;
899 case 1:
900 xfer = buf[3];
901 break;
902 case 2:
903 xfer = buf[4];
904 break;
907 return xfer * unit;
910 static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
912 int extend = buf[1] & 0x1;
913 int length = buf[2] & 0x3;
914 int xfer;
915 int unit = ata_passthrough_xfer_unit(dev, buf);
917 switch (length) {
918 case 0:
919 case 3: /* USB-specific. */
920 default:
921 xfer = 0;
922 break;
923 case 1:
924 xfer = buf[4];
925 xfer |= (extend ? buf[3] << 8 : 0);
926 break;
927 case 2:
928 xfer = buf[6];
929 xfer |= (extend ? buf[5] << 8 : 0);
930 break;
933 return xfer * unit;
936 static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
938 cmd->xfer = scsi_cdb_xfer(buf);
939 switch (buf[0]) {
940 case TEST_UNIT_READY:
941 case REWIND:
942 case START_STOP:
943 case SET_CAPACITY:
944 case WRITE_FILEMARKS:
945 case WRITE_FILEMARKS_16:
946 case SPACE:
947 case RESERVE:
948 case RELEASE:
949 case ERASE:
950 case ALLOW_MEDIUM_REMOVAL:
951 case SEEK_10:
952 case SYNCHRONIZE_CACHE:
953 case SYNCHRONIZE_CACHE_16:
954 case LOCATE_16:
955 case LOCK_UNLOCK_CACHE:
956 case SET_CD_SPEED:
957 case SET_LIMITS:
958 case WRITE_LONG_10:
959 case UPDATE_BLOCK:
960 case RESERVE_TRACK:
961 case SET_READ_AHEAD:
962 case PRE_FETCH:
963 case PRE_FETCH_16:
964 case ALLOW_OVERWRITE:
965 cmd->xfer = 0;
966 break;
967 case VERIFY_10:
968 case VERIFY_12:
969 case VERIFY_16:
970 if ((buf[1] & 2) == 0) {
971 cmd->xfer = 0;
972 } else if ((buf[1] & 4) != 0) {
973 cmd->xfer = 1;
975 cmd->xfer *= dev->blocksize;
976 break;
977 case MODE_SENSE:
978 break;
979 case WRITE_SAME_10:
980 case WRITE_SAME_16:
981 cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
982 break;
983 case READ_CAPACITY_10:
984 cmd->xfer = 8;
985 break;
986 case READ_BLOCK_LIMITS:
987 cmd->xfer = 6;
988 break;
989 case SEND_VOLUME_TAG:
990 /* GPCMD_SET_STREAMING from multimedia commands. */
991 if (dev->type == TYPE_ROM) {
992 cmd->xfer = buf[10] | (buf[9] << 8);
993 } else {
994 cmd->xfer = buf[9] | (buf[8] << 8);
996 break;
997 case WRITE_6:
998 /* length 0 means 256 blocks */
999 if (cmd->xfer == 0) {
1000 cmd->xfer = 256;
1002 /* fall through */
1003 case WRITE_10:
1004 case WRITE_VERIFY_10:
1005 case WRITE_12:
1006 case WRITE_VERIFY_12:
1007 case WRITE_16:
1008 case WRITE_VERIFY_16:
1009 cmd->xfer *= dev->blocksize;
1010 break;
1011 case READ_6:
1012 case READ_REVERSE:
1013 /* length 0 means 256 blocks */
1014 if (cmd->xfer == 0) {
1015 cmd->xfer = 256;
1017 /* fall through */
1018 case READ_10:
1019 case READ_12:
1020 case READ_16:
1021 cmd->xfer *= dev->blocksize;
1022 break;
1023 case FORMAT_UNIT:
1024 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1025 * for block devices are restricted to the header right now. */
1026 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1027 cmd->xfer = 12;
1028 } else {
1029 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1031 break;
1032 case INQUIRY:
1033 case RECEIVE_DIAGNOSTIC:
1034 case SEND_DIAGNOSTIC:
1035 cmd->xfer = buf[4] | (buf[3] << 8);
1036 break;
1037 case READ_CD:
1038 case READ_BUFFER:
1039 case WRITE_BUFFER:
1040 case SEND_CUE_SHEET:
1041 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1042 break;
1043 case PERSISTENT_RESERVE_OUT:
1044 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1045 break;
1046 case ERASE_12:
1047 if (dev->type == TYPE_ROM) {
1048 /* MMC command GET PERFORMANCE. */
1049 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1050 buf[10], buf[1] & 0x1f);
1052 break;
1053 case MECHANISM_STATUS:
1054 case READ_DVD_STRUCTURE:
1055 case SEND_DVD_STRUCTURE:
1056 case MAINTENANCE_OUT:
1057 case MAINTENANCE_IN:
1058 if (dev->type == TYPE_ROM) {
1059 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1060 cmd->xfer = buf[9] | (buf[8] << 8);
1062 break;
1063 case ATA_PASSTHROUGH_12:
1064 if (dev->type == TYPE_ROM) {
1065 /* BLANK command of MMC */
1066 cmd->xfer = 0;
1067 } else {
1068 cmd->xfer = ata_passthrough_12_xfer(dev, buf);
1070 break;
1071 case ATA_PASSTHROUGH_16:
1072 cmd->xfer = ata_passthrough_16_xfer(dev, buf);
1073 break;
1075 return 0;
1078 static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1080 switch (buf[0]) {
1081 /* stream commands */
1082 case ERASE_12:
1083 case ERASE_16:
1084 cmd->xfer = 0;
1085 break;
1086 case READ_6:
1087 case READ_REVERSE:
1088 case RECOVER_BUFFERED_DATA:
1089 case WRITE_6:
1090 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1091 if (buf[1] & 0x01) { /* fixed */
1092 cmd->xfer *= dev->blocksize;
1094 break;
1095 case READ_16:
1096 case READ_REVERSE_16:
1097 case VERIFY_16:
1098 case WRITE_16:
1099 cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1100 if (buf[1] & 0x01) { /* fixed */
1101 cmd->xfer *= dev->blocksize;
1103 break;
1104 case REWIND:
1105 case LOAD_UNLOAD:
1106 cmd->xfer = 0;
1107 break;
1108 case SPACE_16:
1109 cmd->xfer = buf[13] | (buf[12] << 8);
1110 break;
1111 case READ_POSITION:
1112 switch (buf[1] & 0x1f) /* operation code */ {
1113 case SHORT_FORM_BLOCK_ID:
1114 case SHORT_FORM_VENDOR_SPECIFIC:
1115 cmd->xfer = 20;
1116 break;
1117 case LONG_FORM:
1118 cmd->xfer = 32;
1119 break;
1120 case EXTENDED_FORM:
1121 cmd->xfer = buf[8] | (buf[7] << 8);
1122 break;
1123 default:
1124 return -1;
1127 break;
1128 case FORMAT_UNIT:
1129 cmd->xfer = buf[4] | (buf[3] << 8);
1130 break;
1131 /* generic commands */
1132 default:
1133 return scsi_req_xfer(cmd, dev, buf);
1135 return 0;
1138 static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1140 switch (buf[0]) {
1141 /* medium changer commands */
1142 case EXCHANGE_MEDIUM:
1143 case INITIALIZE_ELEMENT_STATUS:
1144 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1145 case MOVE_MEDIUM:
1146 case POSITION_TO_ELEMENT:
1147 cmd->xfer = 0;
1148 break;
1149 case READ_ELEMENT_STATUS:
1150 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1151 break;
1153 /* generic commands */
1154 default:
1155 return scsi_req_xfer(cmd, dev, buf);
1157 return 0;
1160 static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1162 switch (buf[0]) {
1163 /* Scanner commands */
1164 case OBJECT_POSITION:
1165 cmd->xfer = 0;
1166 break;
1167 case SCAN:
1168 cmd->xfer = buf[4];
1169 break;
1170 case READ_10:
1171 case SEND:
1172 case GET_WINDOW:
1173 case SET_WINDOW:
1174 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1175 break;
1176 default:
1177 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1178 return scsi_req_xfer(cmd, dev, buf);
1181 return 0;
1184 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1186 if (!cmd->xfer) {
1187 cmd->mode = SCSI_XFER_NONE;
1188 return;
1190 switch (cmd->buf[0]) {
1191 case WRITE_6:
1192 case WRITE_10:
1193 case WRITE_VERIFY_10:
1194 case WRITE_12:
1195 case WRITE_VERIFY_12:
1196 case WRITE_16:
1197 case WRITE_VERIFY_16:
1198 case VERIFY_10:
1199 case VERIFY_12:
1200 case VERIFY_16:
1201 case COPY:
1202 case COPY_VERIFY:
1203 case COMPARE:
1204 case CHANGE_DEFINITION:
1205 case LOG_SELECT:
1206 case MODE_SELECT:
1207 case MODE_SELECT_10:
1208 case SEND_DIAGNOSTIC:
1209 case WRITE_BUFFER:
1210 case FORMAT_UNIT:
1211 case REASSIGN_BLOCKS:
1212 case SEARCH_EQUAL:
1213 case SEARCH_HIGH:
1214 case SEARCH_LOW:
1215 case UPDATE_BLOCK:
1216 case WRITE_LONG_10:
1217 case WRITE_SAME_10:
1218 case WRITE_SAME_16:
1219 case UNMAP:
1220 case SEARCH_HIGH_12:
1221 case SEARCH_EQUAL_12:
1222 case SEARCH_LOW_12:
1223 case MEDIUM_SCAN:
1224 case SEND_VOLUME_TAG:
1225 case SEND_CUE_SHEET:
1226 case SEND_DVD_STRUCTURE:
1227 case PERSISTENT_RESERVE_OUT:
1228 case MAINTENANCE_OUT:
1229 case SET_WINDOW:
1230 case SCAN:
1231 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1232 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1234 cmd->mode = SCSI_XFER_TO_DEV;
1235 break;
1236 case ATA_PASSTHROUGH_12:
1237 case ATA_PASSTHROUGH_16:
1238 /* T_DIR */
1239 cmd->mode = (cmd->buf[2] & 0x8) ?
1240 SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1241 break;
1242 default:
1243 cmd->mode = SCSI_XFER_FROM_DEV;
1244 break;
1248 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
1250 int rc;
1251 int len;
1253 cmd->lba = -1;
1254 len = scsi_cdb_length(buf);
1255 if (len < 0) {
1256 return -1;
1259 cmd->len = len;
1260 switch (dev->type) {
1261 case TYPE_TAPE:
1262 rc = scsi_req_stream_xfer(cmd, dev, buf);
1263 break;
1264 case TYPE_MEDIUM_CHANGER:
1265 rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
1266 break;
1267 case TYPE_SCANNER:
1268 rc = scsi_req_scanner_length(cmd, dev, buf);
1269 break;
1270 default:
1271 rc = scsi_req_xfer(cmd, dev, buf);
1272 break;
1275 if (rc != 0)
1276 return rc;
1278 memcpy(cmd->buf, buf, cmd->len);
1279 scsi_cmd_xfer_mode(cmd);
1280 cmd->lba = scsi_cmd_lba(cmd);
1281 return 0;
1284 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1286 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1288 scsi_device_set_ua(dev, sense);
1289 if (bus->info->change) {
1290 bus->info->change(bus, dev, sense);
1294 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1296 assert(req->refcount > 0);
1297 req->refcount++;
1298 return req;
1301 void scsi_req_unref(SCSIRequest *req)
1303 assert(req->refcount > 0);
1304 if (--req->refcount == 0) {
1305 BusState *qbus = req->dev->qdev.parent_bus;
1306 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1308 if (bus->info->free_request && req->hba_private) {
1309 bus->info->free_request(bus, req->hba_private);
1311 if (req->ops->free_req) {
1312 req->ops->free_req(req);
1314 object_unref(OBJECT(req->dev));
1315 object_unref(OBJECT(qbus->parent));
1316 g_free(req);
1320 /* Tell the device that we finished processing this chunk of I/O. It
1321 will start the next chunk or complete the command. */
1322 void scsi_req_continue(SCSIRequest *req)
1324 if (req->io_canceled) {
1325 trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1326 return;
1328 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1329 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1330 req->ops->write_data(req);
1331 } else {
1332 req->ops->read_data(req);
1336 /* Called by the devices when data is ready for the HBA. The HBA should
1337 start a DMA operation to read or fill the device's data buffer.
1338 Once it completes, calling scsi_req_continue will restart I/O. */
1339 void scsi_req_data(SCSIRequest *req, int len)
1341 uint8_t *buf;
1342 if (req->io_canceled) {
1343 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1344 return;
1346 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1347 assert(req->cmd.mode != SCSI_XFER_NONE);
1348 if (!req->sg) {
1349 req->resid -= len;
1350 req->bus->info->transfer_data(req, len);
1351 return;
1354 /* If the device calls scsi_req_data and the HBA specified a
1355 * scatter/gather list, the transfer has to happen in a single
1356 * step. */
1357 assert(!req->dma_started);
1358 req->dma_started = true;
1360 buf = scsi_req_get_buf(req);
1361 if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1362 req->resid = dma_buf_read(buf, len, req->sg);
1363 } else {
1364 req->resid = dma_buf_write(buf, len, req->sg);
1366 scsi_req_continue(req);
1369 void scsi_req_print(SCSIRequest *req)
1371 FILE *fp = stderr;
1372 int i;
1374 fprintf(fp, "[%s id=%d] %s",
1375 req->dev->qdev.parent_bus->name,
1376 req->dev->id,
1377 scsi_command_name(req->cmd.buf[0]));
1378 for (i = 1; i < req->cmd.len; i++) {
1379 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1381 switch (req->cmd.mode) {
1382 case SCSI_XFER_NONE:
1383 fprintf(fp, " - none\n");
1384 break;
1385 case SCSI_XFER_FROM_DEV:
1386 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1387 break;
1388 case SCSI_XFER_TO_DEV:
1389 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1390 break;
1391 default:
1392 fprintf(fp, " - Oops\n");
1393 break;
1397 void scsi_req_complete(SCSIRequest *req, int status)
1399 assert(req->status == -1);
1400 req->status = status;
1402 assert(req->sense_len <= sizeof(req->sense));
1403 if (status == GOOD) {
1404 req->sense_len = 0;
1407 if (req->sense_len) {
1408 memcpy(req->dev->sense, req->sense, req->sense_len);
1409 req->dev->sense_len = req->sense_len;
1410 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1411 } else {
1412 req->dev->sense_len = 0;
1413 req->dev->sense_is_ua = false;
1417 * Unit attention state is now stored in the device's sense buffer
1418 * if the HBA didn't do autosense. Clear the pending unit attention
1419 * flags.
1421 scsi_clear_unit_attention(req);
1423 scsi_req_ref(req);
1424 scsi_req_dequeue(req);
1425 req->bus->info->complete(req, req->status, req->resid);
1427 /* Cancelled requests might end up being completed instead of cancelled */
1428 notifier_list_notify(&req->cancel_notifiers, req);
1429 scsi_req_unref(req);
1432 /* Called by the devices when the request is canceled. */
1433 void scsi_req_cancel_complete(SCSIRequest *req)
1435 assert(req->io_canceled);
1436 if (req->bus->info->cancel) {
1437 req->bus->info->cancel(req);
1439 notifier_list_notify(&req->cancel_notifiers, req);
1440 scsi_req_unref(req);
1443 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1444 * notifier list, the bus will be notified the requests cancellation is
1445 * completed.
1446 * */
1447 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1449 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1450 if (notifier) {
1451 notifier_list_add(&req->cancel_notifiers, notifier);
1453 if (req->io_canceled) {
1454 /* A blk_aio_cancel_async is pending; when it finishes,
1455 * scsi_req_cancel_complete will be called and will
1456 * call the notifier we just added. Just wait for that.
1458 assert(req->aiocb);
1459 return;
1461 /* Dropped in scsi_req_cancel_complete. */
1462 scsi_req_ref(req);
1463 scsi_req_dequeue(req);
1464 req->io_canceled = true;
1465 if (req->aiocb) {
1466 blk_aio_cancel_async(req->aiocb);
1467 } else {
1468 scsi_req_cancel_complete(req);
1472 void scsi_req_cancel(SCSIRequest *req)
1474 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1475 if (!req->enqueued) {
1476 return;
1478 assert(!req->io_canceled);
1479 /* Dropped in scsi_req_cancel_complete. */
1480 scsi_req_ref(req);
1481 scsi_req_dequeue(req);
1482 req->io_canceled = true;
1483 if (req->aiocb) {
1484 blk_aio_cancel(req->aiocb);
1485 } else {
1486 scsi_req_cancel_complete(req);
1490 static int scsi_ua_precedence(SCSISense sense)
1492 if (sense.key != UNIT_ATTENTION) {
1493 return INT_MAX;
1495 if (sense.asc == 0x29 && sense.ascq == 0x04) {
1496 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1497 return 1;
1498 } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1499 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1500 return 2;
1501 } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1502 /* These two go with "all others". */
1504 } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1505 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1506 * POWER ON OCCURRED = 1
1507 * SCSI BUS RESET OCCURRED = 2
1508 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1509 * I_T NEXUS LOSS OCCURRED = 7
1511 return sense.ascq;
1512 } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1513 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1514 return 8;
1516 return (sense.asc << 8) | sense.ascq;
1519 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1521 int prec1, prec2;
1522 if (sense.key != UNIT_ATTENTION) {
1523 return;
1525 trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1526 sense.asc, sense.ascq);
1529 * Override a pre-existing unit attention condition, except for a more
1530 * important reset condition.
1532 prec1 = scsi_ua_precedence(sdev->unit_attention);
1533 prec2 = scsi_ua_precedence(sense);
1534 if (prec2 < prec1) {
1535 sdev->unit_attention = sense;
1539 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1541 SCSIRequest *req;
1543 aio_context_acquire(blk_get_aio_context(sdev->conf.blk));
1544 while (!QTAILQ_EMPTY(&sdev->requests)) {
1545 req = QTAILQ_FIRST(&sdev->requests);
1546 scsi_req_cancel_async(req, NULL);
1548 blk_drain(sdev->conf.blk);
1549 aio_context_release(blk_get_aio_context(sdev->conf.blk));
1550 scsi_device_set_ua(sdev, sense);
1553 static char *scsibus_get_dev_path(DeviceState *dev)
1555 SCSIDevice *d = SCSI_DEVICE(dev);
1556 DeviceState *hba = dev->parent_bus->parent;
1557 char *id;
1558 char *path;
1560 id = qdev_get_dev_path(hba);
1561 if (id) {
1562 path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1563 } else {
1564 path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1566 g_free(id);
1567 return path;
1570 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1572 SCSIDevice *d = SCSI_DEVICE(dev);
1573 return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1574 qdev_fw_name(dev), d->id, d->lun);
1577 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1579 BusChild *kid;
1580 SCSIDevice *target_dev = NULL;
1582 QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, sibling) {
1583 DeviceState *qdev = kid->child;
1584 SCSIDevice *dev = SCSI_DEVICE(qdev);
1586 if (dev->channel == channel && dev->id == id) {
1587 if (dev->lun == lun) {
1588 return dev;
1590 target_dev = dev;
1593 return target_dev;
1596 /* SCSI request list. For simplicity, pv points to the whole device */
1598 static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
1599 const VMStateField *field, QJSON *vmdesc)
1601 SCSIDevice *s = pv;
1602 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1603 SCSIRequest *req;
1605 QTAILQ_FOREACH(req, &s->requests, next) {
1606 assert(!req->io_canceled);
1607 assert(req->status == -1);
1608 assert(req->enqueued);
1610 qemu_put_sbyte(f, req->retry ? 1 : 2);
1611 qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1612 qemu_put_be32s(f, &req->tag);
1613 qemu_put_be32s(f, &req->lun);
1614 if (bus->info->save_request) {
1615 bus->info->save_request(f, req);
1617 if (req->ops->save_request) {
1618 req->ops->save_request(f, req);
1621 qemu_put_sbyte(f, 0);
1623 return 0;
1626 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
1627 const VMStateField *field)
1629 SCSIDevice *s = pv;
1630 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1631 int8_t sbyte;
1633 while ((sbyte = qemu_get_sbyte(f)) > 0) {
1634 uint8_t buf[SCSI_CMD_BUF_SIZE];
1635 uint32_t tag;
1636 uint32_t lun;
1637 SCSIRequest *req;
1639 qemu_get_buffer(f, buf, sizeof(buf));
1640 qemu_get_be32s(f, &tag);
1641 qemu_get_be32s(f, &lun);
1642 req = scsi_req_new(s, tag, lun, buf, NULL);
1643 req->retry = (sbyte == 1);
1644 if (bus->info->load_request) {
1645 req->hba_private = bus->info->load_request(f, req);
1647 if (req->ops->load_request) {
1648 req->ops->load_request(f, req);
1651 /* Just restart it later. */
1652 scsi_req_enqueue_internal(req);
1654 /* At this point, the request will be kept alive by the reference
1655 * added by scsi_req_enqueue_internal, so we can release our reference.
1656 * The HBA of course will add its own reference in the load_request
1657 * callback if it needs to hold on the SCSIRequest.
1659 scsi_req_unref(req);
1662 return 0;
1665 static const VMStateInfo vmstate_info_scsi_requests = {
1666 .name = "scsi-requests",
1667 .get = get_scsi_requests,
1668 .put = put_scsi_requests,
1671 static bool scsi_sense_state_needed(void *opaque)
1673 SCSIDevice *s = opaque;
1675 return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1678 static const VMStateDescription vmstate_scsi_sense_state = {
1679 .name = "SCSIDevice/sense",
1680 .version_id = 1,
1681 .minimum_version_id = 1,
1682 .needed = scsi_sense_state_needed,
1683 .fields = (VMStateField[]) {
1684 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1685 SCSI_SENSE_BUF_SIZE_OLD,
1686 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1687 VMSTATE_END_OF_LIST()
1691 const VMStateDescription vmstate_scsi_device = {
1692 .name = "SCSIDevice",
1693 .version_id = 1,
1694 .minimum_version_id = 1,
1695 .fields = (VMStateField[]) {
1696 VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1697 VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1698 VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1699 VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1700 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
1701 VMSTATE_UINT32(sense_len, SCSIDevice),
1703 .name = "requests",
1704 .version_id = 0,
1705 .field_exists = NULL,
1706 .size = 0, /* ouch */
1707 .info = &vmstate_info_scsi_requests,
1708 .flags = VMS_SINGLE,
1709 .offset = 0,
1711 VMSTATE_END_OF_LIST()
1713 .subsections = (const VMStateDescription*[]) {
1714 &vmstate_scsi_sense_state,
1715 NULL
1719 static void scsi_device_class_init(ObjectClass *klass, void *data)
1721 DeviceClass *k = DEVICE_CLASS(klass);
1722 set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1723 k->bus_type = TYPE_SCSI_BUS;
1724 k->realize = scsi_qdev_realize;
1725 k->unrealize = scsi_qdev_unrealize;
1726 device_class_set_props(k, scsi_props);
1729 static void scsi_dev_instance_init(Object *obj)
1731 DeviceState *dev = DEVICE(obj);
1732 SCSIDevice *s = SCSI_DEVICE(dev);
1734 device_add_bootindex_property(obj, &s->conf.bootindex,
1735 "bootindex", NULL,
1736 &s->qdev);
1739 static const TypeInfo scsi_device_type_info = {
1740 .name = TYPE_SCSI_DEVICE,
1741 .parent = TYPE_DEVICE,
1742 .instance_size = sizeof(SCSIDevice),
1743 .abstract = true,
1744 .class_size = sizeof(SCSIDeviceClass),
1745 .class_init = scsi_device_class_init,
1746 .instance_init = scsi_dev_instance_init,
1749 static void scsi_register_types(void)
1751 type_register_static(&scsi_bus_info);
1752 type_register_static(&scsi_device_type_info);
1755 type_init(scsi_register_types)