scsi/scsi_bus: Add scsi_device_get
[qemu/ar7.git] / hw / scsi / scsi-bus.c
blobeda8cb7e70f5d0d4e807cbff0551caf4a7a0af95
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 int next_scsi_bus;
27 static SCSIDevice *do_scsi_device_find(SCSIBus *bus,
28 int channel, int id, int lun,
29 bool include_unrealized)
31 BusChild *kid;
32 SCSIDevice *retval = NULL;
34 QTAILQ_FOREACH_RCU(kid, &bus->qbus.children, sibling) {
35 DeviceState *qdev = kid->child;
36 SCSIDevice *dev = SCSI_DEVICE(qdev);
38 if (dev->channel == channel && dev->id == id) {
39 if (dev->lun == lun) {
40 retval = dev;
41 break;
45 * If we don't find exact match (channel/bus/lun),
46 * we will return the first device which matches channel/bus
49 if (!retval) {
50 retval = dev;
56 * This function might run on the IO thread and we might race against
57 * main thread hot-plugging the device.
58 * We assume that as soon as .realized is set to true we can let
59 * the user access the device.
62 if (retval && !include_unrealized &&
63 !qatomic_load_acquire(&retval->qdev.realized)) {
64 retval = NULL;
67 return retval;
70 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
72 RCU_READ_LOCK_GUARD();
73 return do_scsi_device_find(bus, channel, id, lun, false);
76 SCSIDevice *scsi_device_get(SCSIBus *bus, int channel, int id, int lun)
78 SCSIDevice *d;
79 RCU_READ_LOCK_GUARD();
80 d = do_scsi_device_find(bus, channel, id, lun, false);
81 if (d) {
82 object_ref(d);
84 return d;
87 static void scsi_device_realize(SCSIDevice *s, Error **errp)
89 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
90 if (sc->realize) {
91 sc->realize(s, errp);
95 static void scsi_device_unrealize(SCSIDevice *s)
97 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
98 if (sc->unrealize) {
99 sc->unrealize(s);
103 int scsi_bus_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf,
104 void *hba_private)
106 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
107 int rc;
109 assert(cmd->len == 0);
110 rc = scsi_req_parse_cdb(dev, cmd, buf);
111 if (bus->info->parse_cdb) {
112 rc = bus->info->parse_cdb(dev, cmd, buf, hba_private);
114 return rc;
117 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
118 uint8_t *buf, void *hba_private)
120 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
121 if (sc->alloc_req) {
122 return sc->alloc_req(s, tag, lun, buf, hba_private);
125 return NULL;
128 void scsi_device_unit_attention_reported(SCSIDevice *s)
130 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
131 if (sc->unit_attention_reported) {
132 sc->unit_attention_reported(s);
136 /* Create a scsi bus, and attach devices to it. */
137 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
138 const SCSIBusInfo *info, const char *bus_name)
140 qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
141 bus->busnr = next_scsi_bus++;
142 bus->info = info;
143 qbus_set_bus_hotplug_handler(BUS(bus));
146 static void scsi_dma_restart_bh(void *opaque)
148 SCSIDevice *s = opaque;
149 SCSIRequest *req, *next;
151 qemu_bh_delete(s->bh);
152 s->bh = NULL;
154 aio_context_acquire(blk_get_aio_context(s->conf.blk));
155 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
156 scsi_req_ref(req);
157 if (req->retry) {
158 req->retry = false;
159 switch (req->cmd.mode) {
160 case SCSI_XFER_FROM_DEV:
161 case SCSI_XFER_TO_DEV:
162 scsi_req_continue(req);
163 break;
164 case SCSI_XFER_NONE:
165 scsi_req_dequeue(req);
166 scsi_req_enqueue(req);
167 break;
170 scsi_req_unref(req);
172 aio_context_release(blk_get_aio_context(s->conf.blk));
175 void scsi_req_retry(SCSIRequest *req)
177 /* No need to save a reference, because scsi_dma_restart_bh just
178 * looks at the request list. */
179 req->retry = true;
182 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
184 SCSIDevice *s = opaque;
186 if (!running) {
187 return;
189 if (!s->bh) {
190 AioContext *ctx = blk_get_aio_context(s->conf.blk);
191 s->bh = aio_bh_new(ctx, scsi_dma_restart_bh, s);
192 qemu_bh_schedule(s->bh);
196 static bool scsi_bus_is_address_free(SCSIBus *bus,
197 int channel, int target, int lun,
198 SCSIDevice **p_dev)
200 SCSIDevice *d;
202 RCU_READ_LOCK_GUARD();
203 d = do_scsi_device_find(bus, channel, target, lun, true);
204 if (d && d->lun == lun) {
205 if (p_dev) {
206 *p_dev = d;
208 return false;
210 if (p_dev) {
211 *p_dev = NULL;
213 return true;
216 static bool scsi_bus_check_address(BusState *qbus, DeviceState *qdev, Error **errp)
218 SCSIDevice *dev = SCSI_DEVICE(qdev);
219 SCSIBus *bus = SCSI_BUS(qbus);
221 if (dev->channel > bus->info->max_channel) {
222 error_setg(errp, "bad scsi channel id: %d", dev->channel);
223 return false;
225 if (dev->id != -1 && dev->id > bus->info->max_target) {
226 error_setg(errp, "bad scsi device id: %d", dev->id);
227 return false;
229 if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
230 error_setg(errp, "bad scsi device lun: %d", dev->lun);
231 return false;
234 if (dev->id != -1 && dev->lun != -1) {
235 SCSIDevice *d;
236 if (!scsi_bus_is_address_free(bus, dev->channel, dev->id, dev->lun, &d)) {
237 error_setg(errp, "lun already used by '%s'", d->qdev.id);
238 return false;
242 return true;
245 static void scsi_qdev_realize(DeviceState *qdev, Error **errp)
247 SCSIDevice *dev = SCSI_DEVICE(qdev);
248 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
249 bool is_free;
250 Error *local_err = NULL;
252 if (dev->id == -1) {
253 int id = -1;
254 if (dev->lun == -1) {
255 dev->lun = 0;
257 do {
258 is_free = scsi_bus_is_address_free(bus, dev->channel, ++id, dev->lun, NULL);
259 } while (!is_free && id < bus->info->max_target);
260 if (!is_free) {
261 error_setg(errp, "no free target");
262 return;
264 dev->id = id;
265 } else if (dev->lun == -1) {
266 int lun = -1;
267 do {
268 is_free = scsi_bus_is_address_free(bus, dev->channel, dev->id, ++lun, NULL);
269 } while (!is_free && lun < bus->info->max_lun);
270 if (!is_free) {
271 error_setg(errp, "no free lun");
272 return;
274 dev->lun = lun;
277 QTAILQ_INIT(&dev->requests);
278 scsi_device_realize(dev, &local_err);
279 if (local_err) {
280 error_propagate(errp, local_err);
281 return;
283 dev->vmsentry = qdev_add_vm_change_state_handler(DEVICE(dev),
284 scsi_dma_restart_cb, dev);
287 static void scsi_qdev_unrealize(DeviceState *qdev)
289 SCSIDevice *dev = SCSI_DEVICE(qdev);
291 if (dev->vmsentry) {
292 qemu_del_vm_change_state_handler(dev->vmsentry);
295 scsi_device_purge_requests(dev, SENSE_CODE(NO_SENSE));
297 scsi_device_unrealize(dev);
299 blockdev_mark_auto_del(dev->conf.blk);
302 /* handle legacy '-drive if=scsi,...' cmd line args */
303 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockBackend *blk,
304 int unit, bool removable, int bootindex,
305 bool share_rw,
306 BlockdevOnError rerror,
307 BlockdevOnError werror,
308 const char *serial, Error **errp)
310 const char *driver;
311 char *name;
312 DeviceState *dev;
313 DriveInfo *dinfo;
315 if (blk_is_sg(blk)) {
316 driver = "scsi-generic";
317 } else {
318 dinfo = blk_legacy_dinfo(blk);
319 if (dinfo && dinfo->media_cd) {
320 driver = "scsi-cd";
321 } else {
322 driver = "scsi-hd";
325 dev = qdev_new(driver);
326 name = g_strdup_printf("legacy[%d]", unit);
327 object_property_add_child(OBJECT(bus), name, OBJECT(dev));
328 g_free(name);
330 qdev_prop_set_uint32(dev, "scsi-id", unit);
331 if (bootindex >= 0) {
332 object_property_set_int(OBJECT(dev), "bootindex", bootindex,
333 &error_abort);
335 if (object_property_find(OBJECT(dev), "removable")) {
336 qdev_prop_set_bit(dev, "removable", removable);
338 if (serial && object_property_find(OBJECT(dev), "serial")) {
339 qdev_prop_set_string(dev, "serial", serial);
341 if (!qdev_prop_set_drive_err(dev, "drive", blk, errp)) {
342 object_unparent(OBJECT(dev));
343 return NULL;
345 if (!object_property_set_bool(OBJECT(dev), "share-rw", share_rw, errp)) {
346 object_unparent(OBJECT(dev));
347 return NULL;
350 qdev_prop_set_enum(dev, "rerror", rerror);
351 qdev_prop_set_enum(dev, "werror", werror);
353 if (!qdev_realize_and_unref(dev, &bus->qbus, errp)) {
354 object_unparent(OBJECT(dev));
355 return NULL;
357 return SCSI_DEVICE(dev);
360 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
362 Location loc;
363 DriveInfo *dinfo;
364 int unit;
366 loc_push_none(&loc);
367 for (unit = 0; unit <= bus->info->max_target; unit++) {
368 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
369 if (dinfo == NULL) {
370 continue;
372 qemu_opts_loc_restore(dinfo->opts);
373 scsi_bus_legacy_add_drive(bus, blk_by_legacy_dinfo(dinfo),
374 unit, false, -1, false,
375 BLOCKDEV_ON_ERROR_AUTO,
376 BLOCKDEV_ON_ERROR_AUTO,
377 NULL, &error_fatal);
379 loc_pop(&loc);
382 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
384 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
385 scsi_req_complete(req, CHECK_CONDITION);
386 return 0;
389 static const struct SCSIReqOps reqops_invalid_field = {
390 .size = sizeof(SCSIRequest),
391 .send_command = scsi_invalid_field
394 /* SCSIReqOps implementation for invalid commands. */
396 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
398 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
399 scsi_req_complete(req, CHECK_CONDITION);
400 return 0;
403 static const struct SCSIReqOps reqops_invalid_opcode = {
404 .size = sizeof(SCSIRequest),
405 .send_command = scsi_invalid_command
408 /* SCSIReqOps implementation for unit attention conditions. */
410 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
412 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
413 scsi_req_build_sense(req, req->dev->unit_attention);
414 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
415 scsi_req_build_sense(req, req->bus->unit_attention);
417 scsi_req_complete(req, CHECK_CONDITION);
418 return 0;
421 static const struct SCSIReqOps reqops_unit_attention = {
422 .size = sizeof(SCSIRequest),
423 .send_command = scsi_unit_attention
426 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
427 an invalid LUN. */
429 typedef struct SCSITargetReq SCSITargetReq;
431 struct SCSITargetReq {
432 SCSIRequest req;
433 int len;
434 uint8_t *buf;
435 int buf_len;
438 static void store_lun(uint8_t *outbuf, int lun)
440 if (lun < 256) {
441 outbuf[1] = lun;
442 return;
444 outbuf[1] = (lun & 255);
445 outbuf[0] = (lun >> 8) | 0x40;
448 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
450 BusChild *kid;
451 int i, len, n;
452 int channel, id;
453 bool found_lun0;
455 if (r->req.cmd.xfer < 16) {
456 return false;
458 if (r->req.cmd.buf[2] > 2) {
459 return false;
461 channel = r->req.dev->channel;
462 id = r->req.dev->id;
463 found_lun0 = false;
464 n = 0;
466 RCU_READ_LOCK_GUARD();
468 QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
469 DeviceState *qdev = kid->child;
470 SCSIDevice *dev = SCSI_DEVICE(qdev);
472 if (dev->channel == channel && dev->id == id) {
473 if (dev->lun == 0) {
474 found_lun0 = true;
476 n += 8;
479 if (!found_lun0) {
480 n += 8;
483 scsi_target_alloc_buf(&r->req, n + 8);
485 len = MIN(n + 8, r->req.cmd.xfer & ~7);
486 memset(r->buf, 0, len);
487 stl_be_p(&r->buf[0], n);
488 i = found_lun0 ? 8 : 16;
489 QTAILQ_FOREACH_RCU(kid, &r->req.bus->qbus.children, sibling) {
490 DeviceState *qdev = kid->child;
491 SCSIDevice *dev = SCSI_DEVICE(qdev);
493 if (dev->channel == channel && dev->id == id) {
494 store_lun(&r->buf[i], dev->lun);
495 i += 8;
499 assert(i == n + 8);
500 r->len = len;
501 return true;
504 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
506 assert(r->req.dev->lun != r->req.lun);
508 scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
510 if (r->req.cmd.buf[1] & 0x2) {
511 /* Command support data - optional, not implemented */
512 return false;
515 if (r->req.cmd.buf[1] & 0x1) {
516 /* Vital product data */
517 uint8_t page_code = r->req.cmd.buf[2];
518 r->buf[r->len++] = page_code ; /* this page */
519 r->buf[r->len++] = 0x00;
521 switch (page_code) {
522 case 0x00: /* Supported page codes, mandatory */
524 int pages;
525 pages = r->len++;
526 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
527 r->buf[pages] = r->len - pages - 1; /* number of pages */
528 break;
530 default:
531 return false;
533 /* done with EVPD */
534 assert(r->len < r->buf_len);
535 r->len = MIN(r->req.cmd.xfer, r->len);
536 return true;
539 /* Standard INQUIRY data */
540 if (r->req.cmd.buf[2] != 0) {
541 return false;
544 /* PAGE CODE == 0 */
545 r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
546 memset(r->buf, 0, r->len);
547 if (r->req.lun != 0) {
548 r->buf[0] = TYPE_NO_LUN;
549 } else {
550 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
551 r->buf[2] = 5; /* Version */
552 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
553 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
554 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
555 memcpy(&r->buf[8], "QEMU ", 8);
556 memcpy(&r->buf[16], "QEMU TARGET ", 16);
557 pstrcpy((char *) &r->buf[32], 4, qemu_hw_version());
559 return true;
562 static size_t scsi_sense_len(SCSIRequest *req)
564 if (req->dev->type == TYPE_SCANNER)
565 return SCSI_SENSE_LEN_SCANNER;
566 else
567 return SCSI_SENSE_LEN;
570 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
572 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
573 int fixed_sense = (req->cmd.buf[1] & 1) == 0;
575 if (req->lun != 0 &&
576 buf[0] != INQUIRY && buf[0] != REQUEST_SENSE) {
577 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
578 scsi_req_complete(req, CHECK_CONDITION);
579 return 0;
581 switch (buf[0]) {
582 case REPORT_LUNS:
583 if (!scsi_target_emulate_report_luns(r)) {
584 goto illegal_request;
586 break;
587 case INQUIRY:
588 if (!scsi_target_emulate_inquiry(r)) {
589 goto illegal_request;
591 break;
592 case REQUEST_SENSE:
593 scsi_target_alloc_buf(&r->req, scsi_sense_len(req));
594 if (req->lun != 0) {
595 const struct SCSISense sense = SENSE_CODE(LUN_NOT_SUPPORTED);
597 r->len = scsi_build_sense_buf(r->buf, req->cmd.xfer,
598 sense, fixed_sense);
599 } else {
600 r->len = scsi_device_get_sense(r->req.dev, r->buf,
601 MIN(req->cmd.xfer, r->buf_len),
602 fixed_sense);
604 if (r->req.dev->sense_is_ua) {
605 scsi_device_unit_attention_reported(req->dev);
606 r->req.dev->sense_len = 0;
607 r->req.dev->sense_is_ua = false;
609 break;
610 case TEST_UNIT_READY:
611 break;
612 default:
613 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
614 scsi_req_complete(req, CHECK_CONDITION);
615 return 0;
616 illegal_request:
617 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
618 scsi_req_complete(req, CHECK_CONDITION);
619 return 0;
622 if (!r->len) {
623 scsi_req_complete(req, GOOD);
625 return r->len;
628 static void scsi_target_read_data(SCSIRequest *req)
630 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
631 uint32_t n;
633 n = r->len;
634 if (n > 0) {
635 r->len = 0;
636 scsi_req_data(&r->req, n);
637 } else {
638 scsi_req_complete(&r->req, GOOD);
642 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
644 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
646 return r->buf;
649 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
651 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
653 r->buf = g_malloc(len);
654 r->buf_len = len;
656 return r->buf;
659 static void scsi_target_free_buf(SCSIRequest *req)
661 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
663 g_free(r->buf);
666 static const struct SCSIReqOps reqops_target_command = {
667 .size = sizeof(SCSITargetReq),
668 .send_command = scsi_target_send_command,
669 .read_data = scsi_target_read_data,
670 .get_buf = scsi_target_get_buf,
671 .free_req = scsi_target_free_buf,
675 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
676 uint32_t tag, uint32_t lun, void *hba_private)
678 SCSIRequest *req;
679 SCSIBus *bus = scsi_bus_from_device(d);
680 BusState *qbus = BUS(bus);
681 const int memset_off = offsetof(SCSIRequest, sense)
682 + sizeof(req->sense);
684 req = g_malloc(reqops->size);
685 memset((uint8_t *)req + memset_off, 0, reqops->size - memset_off);
686 req->refcount = 1;
687 req->bus = bus;
688 req->dev = d;
689 req->tag = tag;
690 req->lun = lun;
691 req->hba_private = hba_private;
692 req->status = -1;
693 req->ops = reqops;
694 object_ref(OBJECT(d));
695 object_ref(OBJECT(qbus->parent));
696 notifier_list_init(&req->cancel_notifiers);
697 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
698 return req;
701 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
702 uint8_t *buf, void *hba_private)
704 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
705 const SCSIReqOps *ops;
706 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(d);
707 SCSIRequest *req;
708 SCSICommand cmd = { .len = 0 };
709 int ret;
711 if ((d->unit_attention.key == UNIT_ATTENTION ||
712 bus->unit_attention.key == UNIT_ATTENTION) &&
713 (buf[0] != INQUIRY &&
714 buf[0] != REPORT_LUNS &&
715 buf[0] != GET_CONFIGURATION &&
716 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
719 * If we already have a pending unit attention condition,
720 * report this one before triggering another one.
722 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
723 ops = &reqops_unit_attention;
724 } else if (lun != d->lun ||
725 buf[0] == REPORT_LUNS ||
726 (buf[0] == REQUEST_SENSE && d->sense_len)) {
727 ops = &reqops_target_command;
728 } else {
729 ops = NULL;
732 if (ops != NULL || !sc->parse_cdb) {
733 ret = scsi_req_parse_cdb(d, &cmd, buf);
734 } else {
735 ret = sc->parse_cdb(d, &cmd, buf, hba_private);
738 if (ret != 0) {
739 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
740 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
741 } else {
742 assert(cmd.len != 0);
743 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
744 cmd.mode, cmd.xfer);
745 if (cmd.lba != -1) {
746 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
747 cmd.lba);
750 if (cmd.xfer > INT32_MAX) {
751 req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
752 } else if (ops) {
753 req = scsi_req_alloc(ops, d, tag, lun, hba_private);
754 } else {
755 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
759 req->cmd = cmd;
760 req->resid = req->cmd.xfer;
762 switch (buf[0]) {
763 case INQUIRY:
764 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
765 break;
766 case TEST_UNIT_READY:
767 trace_scsi_test_unit_ready(d->id, lun, tag);
768 break;
769 case REPORT_LUNS:
770 trace_scsi_report_luns(d->id, lun, tag);
771 break;
772 case REQUEST_SENSE:
773 trace_scsi_request_sense(d->id, lun, tag);
774 break;
775 default:
776 break;
779 return req;
782 uint8_t *scsi_req_get_buf(SCSIRequest *req)
784 return req->ops->get_buf(req);
787 static void scsi_clear_unit_attention(SCSIRequest *req)
789 SCSISense *ua;
790 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
791 req->bus->unit_attention.key != UNIT_ATTENTION) {
792 return;
796 * If an INQUIRY command enters the enabled command state,
797 * the device server shall [not] clear any unit attention condition;
798 * See also MMC-6, paragraphs 6.5 and 6.6.2.
800 if (req->cmd.buf[0] == INQUIRY ||
801 req->cmd.buf[0] == GET_CONFIGURATION ||
802 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
803 return;
806 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
807 ua = &req->dev->unit_attention;
808 } else {
809 ua = &req->bus->unit_attention;
813 * If a REPORT LUNS command enters the enabled command state, [...]
814 * the device server shall clear any pending unit attention condition
815 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
817 if (req->cmd.buf[0] == REPORT_LUNS &&
818 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
819 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
820 return;
823 *ua = SENSE_CODE(NO_SENSE);
826 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
828 int ret;
830 assert(len >= 14);
831 if (!req->sense_len) {
832 return 0;
835 ret = scsi_convert_sense(req->sense, req->sense_len, buf, len, true);
838 * FIXME: clearing unit attention conditions upon autosense should be done
839 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
840 * (SAM-5, 5.14).
842 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
843 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
844 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
846 if (req->dev->sense_is_ua) {
847 scsi_device_unit_attention_reported(req->dev);
848 req->dev->sense_len = 0;
849 req->dev->sense_is_ua = false;
851 return ret;
854 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
856 return scsi_convert_sense(dev->sense, dev->sense_len, buf, len, fixed);
859 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
861 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
862 sense.key, sense.asc, sense.ascq);
863 req->sense_len = scsi_build_sense(req->sense, sense);
866 static void scsi_req_enqueue_internal(SCSIRequest *req)
868 assert(!req->enqueued);
869 scsi_req_ref(req);
870 if (req->bus->info->get_sg_list) {
871 req->sg = req->bus->info->get_sg_list(req);
872 } else {
873 req->sg = NULL;
875 req->enqueued = true;
876 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
879 int32_t scsi_req_enqueue(SCSIRequest *req)
881 int32_t rc;
883 assert(!req->retry);
884 scsi_req_enqueue_internal(req);
885 scsi_req_ref(req);
886 rc = req->ops->send_command(req, req->cmd.buf);
887 scsi_req_unref(req);
888 return rc;
891 static void scsi_req_dequeue(SCSIRequest *req)
893 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
894 req->retry = false;
895 if (req->enqueued) {
896 QTAILQ_REMOVE(&req->dev->requests, req, next);
897 req->enqueued = false;
898 scsi_req_unref(req);
902 static int scsi_get_performance_length(int num_desc, int type, int data_type)
904 /* MMC-6, paragraph 6.7. */
905 switch (type) {
906 case 0:
907 if ((data_type & 3) == 0) {
908 /* Each descriptor is as in Table 295 - Nominal performance. */
909 return 16 * num_desc + 8;
910 } else {
911 /* Each descriptor is as in Table 296 - Exceptions. */
912 return 6 * num_desc + 8;
914 case 1:
915 case 4:
916 case 5:
917 return 8 * num_desc + 8;
918 case 2:
919 return 2048 * num_desc + 8;
920 case 3:
921 return 16 * num_desc + 8;
922 default:
923 return 8;
927 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
929 int byte_block = (buf[2] >> 2) & 0x1;
930 int type = (buf[2] >> 4) & 0x1;
931 int xfer_unit;
933 if (byte_block) {
934 if (type) {
935 xfer_unit = dev->blocksize;
936 } else {
937 xfer_unit = 512;
939 } else {
940 xfer_unit = 1;
943 return xfer_unit;
946 static int ata_passthrough_12_xfer(SCSIDevice *dev, uint8_t *buf)
948 int length = buf[2] & 0x3;
949 int xfer;
950 int unit = ata_passthrough_xfer_unit(dev, buf);
952 switch (length) {
953 case 0:
954 case 3: /* USB-specific. */
955 default:
956 xfer = 0;
957 break;
958 case 1:
959 xfer = buf[3];
960 break;
961 case 2:
962 xfer = buf[4];
963 break;
966 return xfer * unit;
969 static int ata_passthrough_16_xfer(SCSIDevice *dev, uint8_t *buf)
971 int extend = buf[1] & 0x1;
972 int length = buf[2] & 0x3;
973 int xfer;
974 int unit = ata_passthrough_xfer_unit(dev, buf);
976 switch (length) {
977 case 0:
978 case 3: /* USB-specific. */
979 default:
980 xfer = 0;
981 break;
982 case 1:
983 xfer = buf[4];
984 xfer |= (extend ? buf[3] << 8 : 0);
985 break;
986 case 2:
987 xfer = buf[6];
988 xfer |= (extend ? buf[5] << 8 : 0);
989 break;
992 return xfer * unit;
995 static int scsi_req_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
997 cmd->xfer = scsi_cdb_xfer(buf);
998 switch (buf[0]) {
999 case TEST_UNIT_READY:
1000 case REWIND:
1001 case START_STOP:
1002 case SET_CAPACITY:
1003 case WRITE_FILEMARKS:
1004 case WRITE_FILEMARKS_16:
1005 case SPACE:
1006 case RESERVE:
1007 case RELEASE:
1008 case ERASE:
1009 case ALLOW_MEDIUM_REMOVAL:
1010 case SEEK_10:
1011 case SYNCHRONIZE_CACHE:
1012 case SYNCHRONIZE_CACHE_16:
1013 case LOCATE_16:
1014 case LOCK_UNLOCK_CACHE:
1015 case SET_CD_SPEED:
1016 case SET_LIMITS:
1017 case WRITE_LONG_10:
1018 case UPDATE_BLOCK:
1019 case RESERVE_TRACK:
1020 case SET_READ_AHEAD:
1021 case PRE_FETCH:
1022 case PRE_FETCH_16:
1023 case ALLOW_OVERWRITE:
1024 cmd->xfer = 0;
1025 break;
1026 case VERIFY_10:
1027 case VERIFY_12:
1028 case VERIFY_16:
1029 if ((buf[1] & 2) == 0) {
1030 cmd->xfer = 0;
1031 } else if ((buf[1] & 4) != 0) {
1032 cmd->xfer = 1;
1034 cmd->xfer *= dev->blocksize;
1035 break;
1036 case MODE_SENSE:
1037 break;
1038 case WRITE_SAME_10:
1039 case WRITE_SAME_16:
1040 cmd->xfer = buf[1] & 1 ? 0 : dev->blocksize;
1041 break;
1042 case READ_CAPACITY_10:
1043 cmd->xfer = 8;
1044 break;
1045 case READ_BLOCK_LIMITS:
1046 cmd->xfer = 6;
1047 break;
1048 case SEND_VOLUME_TAG:
1049 /* GPCMD_SET_STREAMING from multimedia commands. */
1050 if (dev->type == TYPE_ROM) {
1051 cmd->xfer = buf[10] | (buf[9] << 8);
1052 } else {
1053 cmd->xfer = buf[9] | (buf[8] << 8);
1055 break;
1056 case WRITE_6:
1057 /* length 0 means 256 blocks */
1058 if (cmd->xfer == 0) {
1059 cmd->xfer = 256;
1061 /* fall through */
1062 case WRITE_10:
1063 case WRITE_VERIFY_10:
1064 case WRITE_12:
1065 case WRITE_VERIFY_12:
1066 case WRITE_16:
1067 case WRITE_VERIFY_16:
1068 cmd->xfer *= dev->blocksize;
1069 break;
1070 case READ_6:
1071 case READ_REVERSE:
1072 /* length 0 means 256 blocks */
1073 if (cmd->xfer == 0) {
1074 cmd->xfer = 256;
1076 /* fall through */
1077 case READ_10:
1078 case READ_12:
1079 case READ_16:
1080 cmd->xfer *= dev->blocksize;
1081 break;
1082 case FORMAT_UNIT:
1083 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1084 * for block devices are restricted to the header right now. */
1085 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
1086 cmd->xfer = 12;
1087 } else {
1088 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
1090 break;
1091 case INQUIRY:
1092 case RECEIVE_DIAGNOSTIC:
1093 case SEND_DIAGNOSTIC:
1094 cmd->xfer = buf[4] | (buf[3] << 8);
1095 break;
1096 case READ_CD:
1097 case READ_BUFFER:
1098 case WRITE_BUFFER:
1099 case SEND_CUE_SHEET:
1100 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1101 break;
1102 case PERSISTENT_RESERVE_OUT:
1103 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
1104 break;
1105 case ERASE_12:
1106 if (dev->type == TYPE_ROM) {
1107 /* MMC command GET PERFORMANCE. */
1108 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
1109 buf[10], buf[1] & 0x1f);
1111 break;
1112 case MECHANISM_STATUS:
1113 case READ_DVD_STRUCTURE:
1114 case SEND_DVD_STRUCTURE:
1115 case MAINTENANCE_OUT:
1116 case MAINTENANCE_IN:
1117 if (dev->type == TYPE_ROM) {
1118 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1119 cmd->xfer = buf[9] | (buf[8] << 8);
1121 break;
1122 case ATA_PASSTHROUGH_12:
1123 if (dev->type == TYPE_ROM) {
1124 /* BLANK command of MMC */
1125 cmd->xfer = 0;
1126 } else {
1127 cmd->xfer = ata_passthrough_12_xfer(dev, buf);
1129 break;
1130 case ATA_PASSTHROUGH_16:
1131 cmd->xfer = ata_passthrough_16_xfer(dev, buf);
1132 break;
1134 return 0;
1137 static int scsi_req_stream_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1139 switch (buf[0]) {
1140 /* stream commands */
1141 case ERASE_12:
1142 case ERASE_16:
1143 cmd->xfer = 0;
1144 break;
1145 case READ_6:
1146 case READ_REVERSE:
1147 case RECOVER_BUFFERED_DATA:
1148 case WRITE_6:
1149 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1150 if (buf[1] & 0x01) { /* fixed */
1151 cmd->xfer *= dev->blocksize;
1153 break;
1154 case READ_16:
1155 case READ_REVERSE_16:
1156 case VERIFY_16:
1157 case WRITE_16:
1158 cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1159 if (buf[1] & 0x01) { /* fixed */
1160 cmd->xfer *= dev->blocksize;
1162 break;
1163 case REWIND:
1164 case LOAD_UNLOAD:
1165 cmd->xfer = 0;
1166 break;
1167 case SPACE_16:
1168 cmd->xfer = buf[13] | (buf[12] << 8);
1169 break;
1170 case READ_POSITION:
1171 switch (buf[1] & 0x1f) /* operation code */ {
1172 case SHORT_FORM_BLOCK_ID:
1173 case SHORT_FORM_VENDOR_SPECIFIC:
1174 cmd->xfer = 20;
1175 break;
1176 case LONG_FORM:
1177 cmd->xfer = 32;
1178 break;
1179 case EXTENDED_FORM:
1180 cmd->xfer = buf[8] | (buf[7] << 8);
1181 break;
1182 default:
1183 return -1;
1186 break;
1187 case FORMAT_UNIT:
1188 cmd->xfer = buf[4] | (buf[3] << 8);
1189 break;
1190 /* generic commands */
1191 default:
1192 return scsi_req_xfer(cmd, dev, buf);
1194 return 0;
1197 static int scsi_req_medium_changer_xfer(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1199 switch (buf[0]) {
1200 /* medium changer commands */
1201 case EXCHANGE_MEDIUM:
1202 case INITIALIZE_ELEMENT_STATUS:
1203 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1204 case MOVE_MEDIUM:
1205 case POSITION_TO_ELEMENT:
1206 cmd->xfer = 0;
1207 break;
1208 case READ_ELEMENT_STATUS:
1209 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1210 break;
1212 /* generic commands */
1213 default:
1214 return scsi_req_xfer(cmd, dev, buf);
1216 return 0;
1219 static int scsi_req_scanner_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1221 switch (buf[0]) {
1222 /* Scanner commands */
1223 case OBJECT_POSITION:
1224 cmd->xfer = 0;
1225 break;
1226 case SCAN:
1227 cmd->xfer = buf[4];
1228 break;
1229 case READ_10:
1230 case SEND:
1231 case GET_WINDOW:
1232 case SET_WINDOW:
1233 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
1234 break;
1235 default:
1236 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1237 return scsi_req_xfer(cmd, dev, buf);
1240 return 0;
1243 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1245 if (!cmd->xfer) {
1246 cmd->mode = SCSI_XFER_NONE;
1247 return;
1249 switch (cmd->buf[0]) {
1250 case WRITE_6:
1251 case WRITE_10:
1252 case WRITE_VERIFY_10:
1253 case WRITE_12:
1254 case WRITE_VERIFY_12:
1255 case WRITE_16:
1256 case WRITE_VERIFY_16:
1257 case VERIFY_10:
1258 case VERIFY_12:
1259 case VERIFY_16:
1260 case COPY:
1261 case COPY_VERIFY:
1262 case COMPARE:
1263 case CHANGE_DEFINITION:
1264 case LOG_SELECT:
1265 case MODE_SELECT:
1266 case MODE_SELECT_10:
1267 case SEND_DIAGNOSTIC:
1268 case WRITE_BUFFER:
1269 case FORMAT_UNIT:
1270 case REASSIGN_BLOCKS:
1271 case SEARCH_EQUAL:
1272 case SEARCH_HIGH:
1273 case SEARCH_LOW:
1274 case UPDATE_BLOCK:
1275 case WRITE_LONG_10:
1276 case WRITE_SAME_10:
1277 case WRITE_SAME_16:
1278 case UNMAP:
1279 case SEARCH_HIGH_12:
1280 case SEARCH_EQUAL_12:
1281 case SEARCH_LOW_12:
1282 case MEDIUM_SCAN:
1283 case SEND_VOLUME_TAG:
1284 case SEND_CUE_SHEET:
1285 case SEND_DVD_STRUCTURE:
1286 case PERSISTENT_RESERVE_OUT:
1287 case MAINTENANCE_OUT:
1288 case SET_WINDOW:
1289 case SCAN:
1290 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1291 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1293 cmd->mode = SCSI_XFER_TO_DEV;
1294 break;
1295 case ATA_PASSTHROUGH_12:
1296 case ATA_PASSTHROUGH_16:
1297 /* T_DIR */
1298 cmd->mode = (cmd->buf[2] & 0x8) ?
1299 SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1300 break;
1301 default:
1302 cmd->mode = SCSI_XFER_FROM_DEV;
1303 break;
1307 int scsi_req_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, uint8_t *buf)
1309 int rc;
1310 int len;
1312 cmd->lba = -1;
1313 len = scsi_cdb_length(buf);
1314 if (len < 0) {
1315 return -1;
1318 cmd->len = len;
1319 switch (dev->type) {
1320 case TYPE_TAPE:
1321 rc = scsi_req_stream_xfer(cmd, dev, buf);
1322 break;
1323 case TYPE_MEDIUM_CHANGER:
1324 rc = scsi_req_medium_changer_xfer(cmd, dev, buf);
1325 break;
1326 case TYPE_SCANNER:
1327 rc = scsi_req_scanner_length(cmd, dev, buf);
1328 break;
1329 default:
1330 rc = scsi_req_xfer(cmd, dev, buf);
1331 break;
1334 if (rc != 0)
1335 return rc;
1337 memcpy(cmd->buf, buf, cmd->len);
1338 scsi_cmd_xfer_mode(cmd);
1339 cmd->lba = scsi_cmd_lba(cmd);
1340 return 0;
1343 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1345 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1347 scsi_device_set_ua(dev, sense);
1348 if (bus->info->change) {
1349 bus->info->change(bus, dev, sense);
1353 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1355 assert(req->refcount > 0);
1356 req->refcount++;
1357 return req;
1360 void scsi_req_unref(SCSIRequest *req)
1362 assert(req->refcount > 0);
1363 if (--req->refcount == 0) {
1364 BusState *qbus = req->dev->qdev.parent_bus;
1365 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1367 if (bus->info->free_request && req->hba_private) {
1368 bus->info->free_request(bus, req->hba_private);
1370 if (req->ops->free_req) {
1371 req->ops->free_req(req);
1373 object_unref(OBJECT(req->dev));
1374 object_unref(OBJECT(qbus->parent));
1375 g_free(req);
1379 /* Tell the device that we finished processing this chunk of I/O. It
1380 will start the next chunk or complete the command. */
1381 void scsi_req_continue(SCSIRequest *req)
1383 if (req->io_canceled) {
1384 trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1385 return;
1387 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1388 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1389 req->ops->write_data(req);
1390 } else {
1391 req->ops->read_data(req);
1395 /* Called by the devices when data is ready for the HBA. The HBA should
1396 start a DMA operation to read or fill the device's data buffer.
1397 Once it completes, calling scsi_req_continue will restart I/O. */
1398 void scsi_req_data(SCSIRequest *req, int len)
1400 uint8_t *buf;
1401 if (req->io_canceled) {
1402 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1403 return;
1405 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1406 assert(req->cmd.mode != SCSI_XFER_NONE);
1407 if (!req->sg) {
1408 req->resid -= len;
1409 req->bus->info->transfer_data(req, len);
1410 return;
1413 /* If the device calls scsi_req_data and the HBA specified a
1414 * scatter/gather list, the transfer has to happen in a single
1415 * step. */
1416 assert(!req->dma_started);
1417 req->dma_started = true;
1419 buf = scsi_req_get_buf(req);
1420 if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1421 req->resid = dma_buf_read(buf, len, req->sg);
1422 } else {
1423 req->resid = dma_buf_write(buf, len, req->sg);
1425 scsi_req_continue(req);
1428 void scsi_req_print(SCSIRequest *req)
1430 FILE *fp = stderr;
1431 int i;
1433 fprintf(fp, "[%s id=%d] %s",
1434 req->dev->qdev.parent_bus->name,
1435 req->dev->id,
1436 scsi_command_name(req->cmd.buf[0]));
1437 for (i = 1; i < req->cmd.len; i++) {
1438 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1440 switch (req->cmd.mode) {
1441 case SCSI_XFER_NONE:
1442 fprintf(fp, " - none\n");
1443 break;
1444 case SCSI_XFER_FROM_DEV:
1445 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1446 break;
1447 case SCSI_XFER_TO_DEV:
1448 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1449 break;
1450 default:
1451 fprintf(fp, " - Oops\n");
1452 break;
1456 void scsi_req_complete(SCSIRequest *req, int status)
1458 assert(req->status == -1);
1459 req->status = status;
1461 assert(req->sense_len <= sizeof(req->sense));
1462 if (status == GOOD) {
1463 req->sense_len = 0;
1466 if (req->sense_len) {
1467 memcpy(req->dev->sense, req->sense, req->sense_len);
1468 req->dev->sense_len = req->sense_len;
1469 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1470 } else {
1471 req->dev->sense_len = 0;
1472 req->dev->sense_is_ua = false;
1476 * Unit attention state is now stored in the device's sense buffer
1477 * if the HBA didn't do autosense. Clear the pending unit attention
1478 * flags.
1480 scsi_clear_unit_attention(req);
1482 scsi_req_ref(req);
1483 scsi_req_dequeue(req);
1484 req->bus->info->complete(req, req->status, req->resid);
1486 /* Cancelled requests might end up being completed instead of cancelled */
1487 notifier_list_notify(&req->cancel_notifiers, req);
1488 scsi_req_unref(req);
1491 /* Called by the devices when the request is canceled. */
1492 void scsi_req_cancel_complete(SCSIRequest *req)
1494 assert(req->io_canceled);
1495 if (req->bus->info->cancel) {
1496 req->bus->info->cancel(req);
1498 notifier_list_notify(&req->cancel_notifiers, req);
1499 scsi_req_unref(req);
1502 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1503 * notifier list, the bus will be notified the requests cancellation is
1504 * completed.
1505 * */
1506 void scsi_req_cancel_async(SCSIRequest *req, Notifier *notifier)
1508 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1509 if (notifier) {
1510 notifier_list_add(&req->cancel_notifiers, notifier);
1512 if (req->io_canceled) {
1513 /* A blk_aio_cancel_async is pending; when it finishes,
1514 * scsi_req_cancel_complete will be called and will
1515 * call the notifier we just added. Just wait for that.
1517 assert(req->aiocb);
1518 return;
1520 /* Dropped in scsi_req_cancel_complete. */
1521 scsi_req_ref(req);
1522 scsi_req_dequeue(req);
1523 req->io_canceled = true;
1524 if (req->aiocb) {
1525 blk_aio_cancel_async(req->aiocb);
1526 } else {
1527 scsi_req_cancel_complete(req);
1531 void scsi_req_cancel(SCSIRequest *req)
1533 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1534 if (!req->enqueued) {
1535 return;
1537 assert(!req->io_canceled);
1538 /* Dropped in scsi_req_cancel_complete. */
1539 scsi_req_ref(req);
1540 scsi_req_dequeue(req);
1541 req->io_canceled = true;
1542 if (req->aiocb) {
1543 blk_aio_cancel(req->aiocb);
1544 } else {
1545 scsi_req_cancel_complete(req);
1549 static int scsi_ua_precedence(SCSISense sense)
1551 if (sense.key != UNIT_ATTENTION) {
1552 return INT_MAX;
1554 if (sense.asc == 0x29 && sense.ascq == 0x04) {
1555 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1556 return 1;
1557 } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1558 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1559 return 2;
1560 } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1561 /* These two go with "all others". */
1563 } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1564 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1565 * POWER ON OCCURRED = 1
1566 * SCSI BUS RESET OCCURRED = 2
1567 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1568 * I_T NEXUS LOSS OCCURRED = 7
1570 return sense.ascq;
1571 } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1572 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1573 return 8;
1575 return (sense.asc << 8) | sense.ascq;
1578 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1580 int prec1, prec2;
1581 if (sense.key != UNIT_ATTENTION) {
1582 return;
1584 trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1585 sense.asc, sense.ascq);
1588 * Override a pre-existing unit attention condition, except for a more
1589 * important reset condition.
1591 prec1 = scsi_ua_precedence(sdev->unit_attention);
1592 prec2 = scsi_ua_precedence(sense);
1593 if (prec2 < prec1) {
1594 sdev->unit_attention = sense;
1598 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1600 SCSIRequest *req;
1602 aio_context_acquire(blk_get_aio_context(sdev->conf.blk));
1603 while (!QTAILQ_EMPTY(&sdev->requests)) {
1604 req = QTAILQ_FIRST(&sdev->requests);
1605 scsi_req_cancel_async(req, NULL);
1607 blk_drain(sdev->conf.blk);
1608 aio_context_release(blk_get_aio_context(sdev->conf.blk));
1609 scsi_device_set_ua(sdev, sense);
1612 static char *scsibus_get_dev_path(DeviceState *dev)
1614 SCSIDevice *d = SCSI_DEVICE(dev);
1615 DeviceState *hba = dev->parent_bus->parent;
1616 char *id;
1617 char *path;
1619 id = qdev_get_dev_path(hba);
1620 if (id) {
1621 path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1622 } else {
1623 path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1625 g_free(id);
1626 return path;
1629 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1631 SCSIDevice *d = SCSI_DEVICE(dev);
1632 return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1633 qdev_fw_name(dev), d->id, d->lun);
1636 /* SCSI request list. For simplicity, pv points to the whole device */
1638 static int put_scsi_requests(QEMUFile *f, void *pv, size_t size,
1639 const VMStateField *field, QJSON *vmdesc)
1641 SCSIDevice *s = pv;
1642 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1643 SCSIRequest *req;
1645 QTAILQ_FOREACH(req, &s->requests, next) {
1646 assert(!req->io_canceled);
1647 assert(req->status == -1);
1648 assert(req->enqueued);
1650 qemu_put_sbyte(f, req->retry ? 1 : 2);
1651 qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1652 qemu_put_be32s(f, &req->tag);
1653 qemu_put_be32s(f, &req->lun);
1654 if (bus->info->save_request) {
1655 bus->info->save_request(f, req);
1657 if (req->ops->save_request) {
1658 req->ops->save_request(f, req);
1661 qemu_put_sbyte(f, 0);
1663 return 0;
1666 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size,
1667 const VMStateField *field)
1669 SCSIDevice *s = pv;
1670 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1671 int8_t sbyte;
1673 while ((sbyte = qemu_get_sbyte(f)) > 0) {
1674 uint8_t buf[SCSI_CMD_BUF_SIZE];
1675 uint32_t tag;
1676 uint32_t lun;
1677 SCSIRequest *req;
1679 qemu_get_buffer(f, buf, sizeof(buf));
1680 qemu_get_be32s(f, &tag);
1681 qemu_get_be32s(f, &lun);
1682 req = scsi_req_new(s, tag, lun, buf, NULL);
1683 req->retry = (sbyte == 1);
1684 if (bus->info->load_request) {
1685 req->hba_private = bus->info->load_request(f, req);
1687 if (req->ops->load_request) {
1688 req->ops->load_request(f, req);
1691 /* Just restart it later. */
1692 scsi_req_enqueue_internal(req);
1694 /* At this point, the request will be kept alive by the reference
1695 * added by scsi_req_enqueue_internal, so we can release our reference.
1696 * The HBA of course will add its own reference in the load_request
1697 * callback if it needs to hold on the SCSIRequest.
1699 scsi_req_unref(req);
1702 return 0;
1705 static const VMStateInfo vmstate_info_scsi_requests = {
1706 .name = "scsi-requests",
1707 .get = get_scsi_requests,
1708 .put = put_scsi_requests,
1711 static bool scsi_sense_state_needed(void *opaque)
1713 SCSIDevice *s = opaque;
1715 return s->sense_len > SCSI_SENSE_BUF_SIZE_OLD;
1718 static const VMStateDescription vmstate_scsi_sense_state = {
1719 .name = "SCSIDevice/sense",
1720 .version_id = 1,
1721 .minimum_version_id = 1,
1722 .needed = scsi_sense_state_needed,
1723 .fields = (VMStateField[]) {
1724 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice,
1725 SCSI_SENSE_BUF_SIZE_OLD,
1726 SCSI_SENSE_BUF_SIZE - SCSI_SENSE_BUF_SIZE_OLD),
1727 VMSTATE_END_OF_LIST()
1731 const VMStateDescription vmstate_scsi_device = {
1732 .name = "SCSIDevice",
1733 .version_id = 1,
1734 .minimum_version_id = 1,
1735 .fields = (VMStateField[]) {
1736 VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1737 VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1738 VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1739 VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1740 VMSTATE_UINT8_SUB_ARRAY(sense, SCSIDevice, 0, SCSI_SENSE_BUF_SIZE_OLD),
1741 VMSTATE_UINT32(sense_len, SCSIDevice),
1743 .name = "requests",
1744 .version_id = 0,
1745 .field_exists = NULL,
1746 .size = 0, /* ouch */
1747 .info = &vmstate_info_scsi_requests,
1748 .flags = VMS_SINGLE,
1749 .offset = 0,
1751 VMSTATE_END_OF_LIST()
1753 .subsections = (const VMStateDescription*[]) {
1754 &vmstate_scsi_sense_state,
1755 NULL
1759 static Property scsi_props[] = {
1760 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
1761 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
1762 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
1763 DEFINE_PROP_END_OF_LIST(),
1766 static void scsi_device_class_init(ObjectClass *klass, void *data)
1768 DeviceClass *k = DEVICE_CLASS(klass);
1769 set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1770 k->bus_type = TYPE_SCSI_BUS;
1771 k->realize = scsi_qdev_realize;
1772 k->unrealize = scsi_qdev_unrealize;
1773 device_class_set_props(k, scsi_props);
1776 static void scsi_dev_instance_init(Object *obj)
1778 DeviceState *dev = DEVICE(obj);
1779 SCSIDevice *s = SCSI_DEVICE(dev);
1781 device_add_bootindex_property(obj, &s->conf.bootindex,
1782 "bootindex", NULL,
1783 &s->qdev);
1786 static const TypeInfo scsi_device_type_info = {
1787 .name = TYPE_SCSI_DEVICE,
1788 .parent = TYPE_DEVICE,
1789 .instance_size = sizeof(SCSIDevice),
1790 .abstract = true,
1791 .class_size = sizeof(SCSIDeviceClass),
1792 .class_init = scsi_device_class_init,
1793 .instance_init = scsi_dev_instance_init,
1796 static void scsi_bus_class_init(ObjectClass *klass, void *data)
1798 BusClass *k = BUS_CLASS(klass);
1799 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
1801 k->get_dev_path = scsibus_get_dev_path;
1802 k->get_fw_dev_path = scsibus_get_fw_dev_path;
1803 k->check_address = scsi_bus_check_address;
1804 hc->unplug = qdev_simple_device_unplug_cb;
1807 static const TypeInfo scsi_bus_info = {
1808 .name = TYPE_SCSI_BUS,
1809 .parent = TYPE_BUS,
1810 .instance_size = sizeof(SCSIBus),
1811 .class_init = scsi_bus_class_init,
1812 .interfaces = (InterfaceInfo[]) {
1813 { TYPE_HOTPLUG_HANDLER },
1818 static void scsi_register_types(void)
1820 type_register_static(&scsi_bus_info);
1821 type_register_static(&scsi_device_type_info);
1824 type_init(scsi_register_types)