.travis.yml: Add aarch64-* targets
[qemu/cris-port.git] / hw / scsi / scsi-bus.c
blob3496c0bbd8f93fc78150a42f7f6cb15d42552ff5
1 #include "hw/hw.h"
2 #include "qemu/error-report.h"
3 #include "hw/scsi/scsi.h"
4 #include "block/scsi.h"
5 #include "hw/qdev.h"
6 #include "sysemu/blockdev.h"
7 #include "trace.h"
8 #include "sysemu/dma.h"
10 static char *scsibus_get_dev_path(DeviceState *dev);
11 static char *scsibus_get_fw_dev_path(DeviceState *dev);
12 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
13 static void scsi_req_dequeue(SCSIRequest *req);
14 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len);
15 static void scsi_target_free_buf(SCSIRequest *req);
17 static Property scsi_props[] = {
18 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
19 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
20 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
21 DEFINE_PROP_END_OF_LIST(),
24 static void scsi_bus_class_init(ObjectClass *klass, void *data)
26 BusClass *k = BUS_CLASS(klass);
28 k->get_dev_path = scsibus_get_dev_path;
29 k->get_fw_dev_path = scsibus_get_fw_dev_path;
32 static const TypeInfo scsi_bus_info = {
33 .name = TYPE_SCSI_BUS,
34 .parent = TYPE_BUS,
35 .instance_size = sizeof(SCSIBus),
36 .class_init = scsi_bus_class_init,
38 static int next_scsi_bus;
40 static int scsi_device_init(SCSIDevice *s)
42 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
43 if (sc->init) {
44 return sc->init(s);
46 return 0;
49 static void scsi_device_destroy(SCSIDevice *s)
51 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
52 if (sc->destroy) {
53 sc->destroy(s);
57 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
58 uint8_t *buf, void *hba_private)
60 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
61 if (sc->alloc_req) {
62 return sc->alloc_req(s, tag, lun, buf, hba_private);
65 return NULL;
68 static void scsi_device_unit_attention_reported(SCSIDevice *s)
70 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
71 if (sc->unit_attention_reported) {
72 sc->unit_attention_reported(s);
76 /* Create a scsi bus, and attach devices to it. */
77 void scsi_bus_new(SCSIBus *bus, size_t bus_size, DeviceState *host,
78 const SCSIBusInfo *info, const char *bus_name)
80 qbus_create_inplace(bus, bus_size, TYPE_SCSI_BUS, host, bus_name);
81 bus->busnr = next_scsi_bus++;
82 bus->info = info;
83 bus->qbus.allow_hotplug = 1;
86 static void scsi_dma_restart_bh(void *opaque)
88 SCSIDevice *s = opaque;
89 SCSIRequest *req, *next;
91 qemu_bh_delete(s->bh);
92 s->bh = NULL;
94 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
95 scsi_req_ref(req);
96 if (req->retry) {
97 req->retry = false;
98 switch (req->cmd.mode) {
99 case SCSI_XFER_FROM_DEV:
100 case SCSI_XFER_TO_DEV:
101 scsi_req_continue(req);
102 break;
103 case SCSI_XFER_NONE:
104 assert(!req->sg);
105 scsi_req_dequeue(req);
106 scsi_req_enqueue(req);
107 break;
110 scsi_req_unref(req);
114 void scsi_req_retry(SCSIRequest *req)
116 /* No need to save a reference, because scsi_dma_restart_bh just
117 * looks at the request list. */
118 req->retry = true;
121 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
123 SCSIDevice *s = opaque;
125 if (!running) {
126 return;
128 if (!s->bh) {
129 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
130 qemu_bh_schedule(s->bh);
134 static int scsi_qdev_init(DeviceState *qdev)
136 SCSIDevice *dev = SCSI_DEVICE(qdev);
137 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
138 SCSIDevice *d;
139 int rc = -1;
141 if (dev->channel > bus->info->max_channel) {
142 error_report("bad scsi channel id: %d", dev->channel);
143 goto err;
145 if (dev->id != -1 && dev->id > bus->info->max_target) {
146 error_report("bad scsi device id: %d", dev->id);
147 goto err;
149 if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
150 error_report("bad scsi device lun: %d", dev->lun);
151 goto err;
154 if (dev->id == -1) {
155 int id = -1;
156 if (dev->lun == -1) {
157 dev->lun = 0;
159 do {
160 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
161 } while (d && d->lun == dev->lun && id < bus->info->max_target);
162 if (d && d->lun == dev->lun) {
163 error_report("no free target");
164 goto err;
166 dev->id = id;
167 } else if (dev->lun == -1) {
168 int lun = -1;
169 do {
170 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
171 } while (d && d->lun == lun && lun < bus->info->max_lun);
172 if (d && d->lun == lun) {
173 error_report("no free lun");
174 goto err;
176 dev->lun = lun;
177 } else {
178 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
179 assert(d);
180 if (d->lun == dev->lun && dev != d) {
181 object_unparent(OBJECT(d));
185 QTAILQ_INIT(&dev->requests);
186 rc = scsi_device_init(dev);
187 if (rc == 0) {
188 dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
189 dev);
192 if (bus->info->hotplug) {
193 bus->info->hotplug(bus, dev);
196 err:
197 return rc;
200 static int scsi_qdev_exit(DeviceState *qdev)
202 SCSIDevice *dev = SCSI_DEVICE(qdev);
204 if (dev->vmsentry) {
205 qemu_del_vm_change_state_handler(dev->vmsentry);
207 scsi_device_destroy(dev);
208 return 0;
211 /* handle legacy '-drive if=scsi,...' cmd line args */
212 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
213 int unit, bool removable, int bootindex,
214 const char *serial, Error **errp)
216 const char *driver;
217 DeviceState *dev;
218 Error *err = NULL;
220 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
221 dev = qdev_create(&bus->qbus, driver);
222 qdev_prop_set_uint32(dev, "scsi-id", unit);
223 if (bootindex >= 0) {
224 qdev_prop_set_int32(dev, "bootindex", bootindex);
226 if (object_property_find(OBJECT(dev), "removable", NULL)) {
227 qdev_prop_set_bit(dev, "removable", removable);
229 if (serial && object_property_find(OBJECT(dev), "serial", NULL)) {
230 qdev_prop_set_string(dev, "serial", serial);
232 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
233 error_setg(errp, "Setting drive property failed");
234 object_unparent(OBJECT(dev));
235 return NULL;
237 object_property_set_bool(OBJECT(dev), true, "realized", &err);
238 if (err != NULL) {
239 error_propagate(errp, err);
240 object_unparent(OBJECT(dev));
241 return NULL;
243 return SCSI_DEVICE(dev);
246 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus, Error **errp)
248 Location loc;
249 DriveInfo *dinfo;
250 int unit;
251 Error *err = NULL;
253 loc_push_none(&loc);
254 for (unit = 0; unit <= bus->info->max_target; unit++) {
255 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
256 if (dinfo == NULL) {
257 continue;
259 qemu_opts_loc_restore(dinfo->opts);
260 scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1, NULL,
261 &err);
262 if (err != NULL) {
263 error_propagate(errp, err);
264 break;
267 loc_pop(&loc);
270 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
272 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
273 scsi_req_complete(req, CHECK_CONDITION);
274 return 0;
277 static const struct SCSIReqOps reqops_invalid_field = {
278 .size = sizeof(SCSIRequest),
279 .send_command = scsi_invalid_field
282 /* SCSIReqOps implementation for invalid commands. */
284 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
286 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
287 scsi_req_complete(req, CHECK_CONDITION);
288 return 0;
291 static const struct SCSIReqOps reqops_invalid_opcode = {
292 .size = sizeof(SCSIRequest),
293 .send_command = scsi_invalid_command
296 /* SCSIReqOps implementation for unit attention conditions. */
298 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
300 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
301 scsi_req_build_sense(req, req->dev->unit_attention);
302 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
303 scsi_req_build_sense(req, req->bus->unit_attention);
305 scsi_req_complete(req, CHECK_CONDITION);
306 return 0;
309 static const struct SCSIReqOps reqops_unit_attention = {
310 .size = sizeof(SCSIRequest),
311 .send_command = scsi_unit_attention
314 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
315 an invalid LUN. */
317 typedef struct SCSITargetReq SCSITargetReq;
319 struct SCSITargetReq {
320 SCSIRequest req;
321 int len;
322 uint8_t *buf;
323 int buf_len;
326 static void store_lun(uint8_t *outbuf, int lun)
328 if (lun < 256) {
329 outbuf[1] = lun;
330 return;
332 outbuf[1] = (lun & 255);
333 outbuf[0] = (lun >> 8) | 0x40;
336 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
338 BusChild *kid;
339 int i, len, n;
340 int channel, id;
341 bool found_lun0;
343 if (r->req.cmd.xfer < 16) {
344 return false;
346 if (r->req.cmd.buf[2] > 2) {
347 return false;
349 channel = r->req.dev->channel;
350 id = r->req.dev->id;
351 found_lun0 = false;
352 n = 0;
353 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
354 DeviceState *qdev = kid->child;
355 SCSIDevice *dev = SCSI_DEVICE(qdev);
357 if (dev->channel == channel && dev->id == id) {
358 if (dev->lun == 0) {
359 found_lun0 = true;
361 n += 8;
364 if (!found_lun0) {
365 n += 8;
368 scsi_target_alloc_buf(&r->req, n + 8);
370 len = MIN(n + 8, r->req.cmd.xfer & ~7);
371 memset(r->buf, 0, len);
372 stl_be_p(&r->buf[0], n);
373 i = found_lun0 ? 8 : 16;
374 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
375 DeviceState *qdev = kid->child;
376 SCSIDevice *dev = SCSI_DEVICE(qdev);
378 if (dev->channel == channel && dev->id == id) {
379 store_lun(&r->buf[i], dev->lun);
380 i += 8;
383 assert(i == n + 8);
384 r->len = len;
385 return true;
388 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
390 assert(r->req.dev->lun != r->req.lun);
392 scsi_target_alloc_buf(&r->req, SCSI_INQUIRY_LEN);
394 if (r->req.cmd.buf[1] & 0x2) {
395 /* Command support data - optional, not implemented */
396 return false;
399 if (r->req.cmd.buf[1] & 0x1) {
400 /* Vital product data */
401 uint8_t page_code = r->req.cmd.buf[2];
402 r->buf[r->len++] = page_code ; /* this page */
403 r->buf[r->len++] = 0x00;
405 switch (page_code) {
406 case 0x00: /* Supported page codes, mandatory */
408 int pages;
409 pages = r->len++;
410 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
411 r->buf[pages] = r->len - pages - 1; /* number of pages */
412 break;
414 default:
415 return false;
417 /* done with EVPD */
418 assert(r->len < r->buf_len);
419 r->len = MIN(r->req.cmd.xfer, r->len);
420 return true;
423 /* Standard INQUIRY data */
424 if (r->req.cmd.buf[2] != 0) {
425 return false;
428 /* PAGE CODE == 0 */
429 r->len = MIN(r->req.cmd.xfer, SCSI_INQUIRY_LEN);
430 memset(r->buf, 0, r->len);
431 if (r->req.lun != 0) {
432 r->buf[0] = TYPE_NO_LUN;
433 } else {
434 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
435 r->buf[2] = 5; /* Version */
436 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
437 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
438 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
439 memcpy(&r->buf[8], "QEMU ", 8);
440 memcpy(&r->buf[16], "QEMU TARGET ", 16);
441 pstrcpy((char *) &r->buf[32], 4, qemu_get_version());
443 return true;
446 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
448 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
450 switch (buf[0]) {
451 case REPORT_LUNS:
452 if (!scsi_target_emulate_report_luns(r)) {
453 goto illegal_request;
455 break;
456 case INQUIRY:
457 if (!scsi_target_emulate_inquiry(r)) {
458 goto illegal_request;
460 break;
461 case REQUEST_SENSE:
462 scsi_target_alloc_buf(&r->req, SCSI_SENSE_LEN);
463 r->len = scsi_device_get_sense(r->req.dev, r->buf,
464 MIN(req->cmd.xfer, r->buf_len),
465 (req->cmd.buf[1] & 1) == 0);
466 if (r->req.dev->sense_is_ua) {
467 scsi_device_unit_attention_reported(req->dev);
468 r->req.dev->sense_len = 0;
469 r->req.dev->sense_is_ua = false;
471 break;
472 default:
473 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
474 scsi_req_complete(req, CHECK_CONDITION);
475 return 0;
476 illegal_request:
477 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
478 scsi_req_complete(req, CHECK_CONDITION);
479 return 0;
482 if (!r->len) {
483 scsi_req_complete(req, GOOD);
485 return r->len;
488 static void scsi_target_read_data(SCSIRequest *req)
490 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
491 uint32_t n;
493 n = r->len;
494 if (n > 0) {
495 r->len = 0;
496 scsi_req_data(&r->req, n);
497 } else {
498 scsi_req_complete(&r->req, GOOD);
502 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
504 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
506 return r->buf;
509 static uint8_t *scsi_target_alloc_buf(SCSIRequest *req, size_t len)
511 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
513 r->buf = g_malloc(len);
514 r->buf_len = len;
516 return r->buf;
519 static void scsi_target_free_buf(SCSIRequest *req)
521 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
523 g_free(r->buf);
526 static const struct SCSIReqOps reqops_target_command = {
527 .size = sizeof(SCSITargetReq),
528 .send_command = scsi_target_send_command,
529 .read_data = scsi_target_read_data,
530 .get_buf = scsi_target_get_buf,
531 .free_req = scsi_target_free_buf,
535 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
536 uint32_t tag, uint32_t lun, void *hba_private)
538 SCSIRequest *req;
539 SCSIBus *bus = scsi_bus_from_device(d);
540 BusState *qbus = BUS(bus);
542 req = g_malloc0(reqops->size);
543 req->refcount = 1;
544 req->bus = bus;
545 req->dev = d;
546 req->tag = tag;
547 req->lun = lun;
548 req->hba_private = hba_private;
549 req->status = -1;
550 req->sense_len = 0;
551 req->ops = reqops;
552 object_ref(OBJECT(d));
553 object_ref(OBJECT(qbus->parent));
554 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
555 return req;
558 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
559 uint8_t *buf, void *hba_private)
561 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
562 SCSIRequest *req;
563 SCSICommand cmd;
565 if (scsi_req_parse(&cmd, d, buf) != 0) {
566 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
567 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
568 } else {
569 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
570 cmd.mode, cmd.xfer);
571 if (cmd.lba != -1) {
572 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
573 cmd.lba);
576 if (cmd.xfer > INT32_MAX) {
577 req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
578 } else if ((d->unit_attention.key == UNIT_ATTENTION ||
579 bus->unit_attention.key == UNIT_ATTENTION) &&
580 (buf[0] != INQUIRY &&
581 buf[0] != REPORT_LUNS &&
582 buf[0] != GET_CONFIGURATION &&
583 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
586 * If we already have a pending unit attention condition,
587 * report this one before triggering another one.
589 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
590 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
591 hba_private);
592 } else if (lun != d->lun ||
593 buf[0] == REPORT_LUNS ||
594 (buf[0] == REQUEST_SENSE && d->sense_len)) {
595 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
596 hba_private);
597 } else {
598 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
602 req->cmd = cmd;
603 req->resid = req->cmd.xfer;
605 switch (buf[0]) {
606 case INQUIRY:
607 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
608 break;
609 case TEST_UNIT_READY:
610 trace_scsi_test_unit_ready(d->id, lun, tag);
611 break;
612 case REPORT_LUNS:
613 trace_scsi_report_luns(d->id, lun, tag);
614 break;
615 case REQUEST_SENSE:
616 trace_scsi_request_sense(d->id, lun, tag);
617 break;
618 default:
619 break;
622 return req;
625 uint8_t *scsi_req_get_buf(SCSIRequest *req)
627 return req->ops->get_buf(req);
630 static void scsi_clear_unit_attention(SCSIRequest *req)
632 SCSISense *ua;
633 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
634 req->bus->unit_attention.key != UNIT_ATTENTION) {
635 return;
639 * If an INQUIRY command enters the enabled command state,
640 * the device server shall [not] clear any unit attention condition;
641 * See also MMC-6, paragraphs 6.5 and 6.6.2.
643 if (req->cmd.buf[0] == INQUIRY ||
644 req->cmd.buf[0] == GET_CONFIGURATION ||
645 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
646 return;
649 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
650 ua = &req->dev->unit_attention;
651 } else {
652 ua = &req->bus->unit_attention;
656 * If a REPORT LUNS command enters the enabled command state, [...]
657 * the device server shall clear any pending unit attention condition
658 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
660 if (req->cmd.buf[0] == REPORT_LUNS &&
661 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
662 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
663 return;
666 *ua = SENSE_CODE(NO_SENSE);
669 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
671 int ret;
673 assert(len >= 14);
674 if (!req->sense_len) {
675 return 0;
678 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
681 * FIXME: clearing unit attention conditions upon autosense should be done
682 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
683 * (SAM-5, 5.14).
685 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
686 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
687 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
689 if (req->dev->sense_is_ua) {
690 scsi_device_unit_attention_reported(req->dev);
691 req->dev->sense_len = 0;
692 req->dev->sense_is_ua = false;
694 return ret;
697 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
699 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
702 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
704 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
705 sense.key, sense.asc, sense.ascq);
706 memset(req->sense, 0, 18);
707 req->sense[0] = 0x70;
708 req->sense[2] = sense.key;
709 req->sense[7] = 10;
710 req->sense[12] = sense.asc;
711 req->sense[13] = sense.ascq;
712 req->sense_len = 18;
715 static void scsi_req_enqueue_internal(SCSIRequest *req)
717 assert(!req->enqueued);
718 scsi_req_ref(req);
719 if (req->bus->info->get_sg_list) {
720 req->sg = req->bus->info->get_sg_list(req);
721 } else {
722 req->sg = NULL;
724 req->enqueued = true;
725 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
728 int32_t scsi_req_enqueue(SCSIRequest *req)
730 int32_t rc;
732 assert(!req->retry);
733 scsi_req_enqueue_internal(req);
734 scsi_req_ref(req);
735 rc = req->ops->send_command(req, req->cmd.buf);
736 scsi_req_unref(req);
737 return rc;
740 static void scsi_req_dequeue(SCSIRequest *req)
742 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
743 req->retry = false;
744 if (req->enqueued) {
745 QTAILQ_REMOVE(&req->dev->requests, req, next);
746 req->enqueued = false;
747 scsi_req_unref(req);
751 static int scsi_get_performance_length(int num_desc, int type, int data_type)
753 /* MMC-6, paragraph 6.7. */
754 switch (type) {
755 case 0:
756 if ((data_type & 3) == 0) {
757 /* Each descriptor is as in Table 295 - Nominal performance. */
758 return 16 * num_desc + 8;
759 } else {
760 /* Each descriptor is as in Table 296 - Exceptions. */
761 return 6 * num_desc + 8;
763 case 1:
764 case 4:
765 case 5:
766 return 8 * num_desc + 8;
767 case 2:
768 return 2048 * num_desc + 8;
769 case 3:
770 return 16 * num_desc + 8;
771 default:
772 return 8;
776 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
778 int byte_block = (buf[2] >> 2) & 0x1;
779 int type = (buf[2] >> 4) & 0x1;
780 int xfer_unit;
782 if (byte_block) {
783 if (type) {
784 xfer_unit = dev->blocksize;
785 } else {
786 xfer_unit = 512;
788 } else {
789 xfer_unit = 1;
792 return xfer_unit;
795 static int ata_passthrough_12_xfer_size(SCSIDevice *dev, uint8_t *buf)
797 int length = buf[2] & 0x3;
798 int xfer;
799 int unit = ata_passthrough_xfer_unit(dev, buf);
801 switch (length) {
802 case 0:
803 case 3: /* USB-specific. */
804 default:
805 xfer = 0;
806 break;
807 case 1:
808 xfer = buf[3];
809 break;
810 case 2:
811 xfer = buf[4];
812 break;
815 return xfer * unit;
818 static int ata_passthrough_16_xfer_size(SCSIDevice *dev, uint8_t *buf)
820 int extend = buf[1] & 0x1;
821 int length = buf[2] & 0x3;
822 int xfer;
823 int unit = ata_passthrough_xfer_unit(dev, buf);
825 switch (length) {
826 case 0:
827 case 3: /* USB-specific. */
828 default:
829 xfer = 0;
830 break;
831 case 1:
832 xfer = buf[4];
833 xfer |= (extend ? buf[3] << 8 : 0);
834 break;
835 case 2:
836 xfer = buf[6];
837 xfer |= (extend ? buf[5] << 8 : 0);
838 break;
841 return xfer * unit;
844 uint32_t scsi_data_cdb_length(uint8_t *buf)
846 if ((buf[0] >> 5) == 0 && buf[4] == 0) {
847 return 256;
848 } else {
849 return scsi_cdb_length(buf);
853 uint32_t scsi_cdb_length(uint8_t *buf)
855 switch (buf[0] >> 5) {
856 case 0:
857 return buf[4];
858 break;
859 case 1:
860 case 2:
861 return lduw_be_p(&buf[7]);
862 break;
863 case 4:
864 return ldl_be_p(&buf[10]) & 0xffffffffULL;
865 break;
866 case 5:
867 return ldl_be_p(&buf[6]) & 0xffffffffULL;
868 break;
869 default:
870 return -1;
874 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
876 cmd->xfer = scsi_cdb_length(buf);
877 switch (buf[0]) {
878 case TEST_UNIT_READY:
879 case REWIND:
880 case START_STOP:
881 case SET_CAPACITY:
882 case WRITE_FILEMARKS:
883 case WRITE_FILEMARKS_16:
884 case SPACE:
885 case RESERVE:
886 case RELEASE:
887 case ERASE:
888 case ALLOW_MEDIUM_REMOVAL:
889 case SEEK_10:
890 case SYNCHRONIZE_CACHE:
891 case SYNCHRONIZE_CACHE_16:
892 case LOCATE_16:
893 case LOCK_UNLOCK_CACHE:
894 case SET_CD_SPEED:
895 case SET_LIMITS:
896 case WRITE_LONG_10:
897 case UPDATE_BLOCK:
898 case RESERVE_TRACK:
899 case SET_READ_AHEAD:
900 case PRE_FETCH:
901 case PRE_FETCH_16:
902 case ALLOW_OVERWRITE:
903 cmd->xfer = 0;
904 break;
905 case VERIFY_10:
906 case VERIFY_12:
907 case VERIFY_16:
908 if ((buf[1] & 2) == 0) {
909 cmd->xfer = 0;
910 } else if ((buf[1] & 4) == 1) {
911 cmd->xfer = 1;
913 cmd->xfer *= dev->blocksize;
914 break;
915 case MODE_SENSE:
916 break;
917 case WRITE_SAME_10:
918 case WRITE_SAME_16:
919 cmd->xfer = dev->blocksize;
920 break;
921 case READ_CAPACITY_10:
922 cmd->xfer = 8;
923 break;
924 case READ_BLOCK_LIMITS:
925 cmd->xfer = 6;
926 break;
927 case SEND_VOLUME_TAG:
928 /* GPCMD_SET_STREAMING from multimedia commands. */
929 if (dev->type == TYPE_ROM) {
930 cmd->xfer = buf[10] | (buf[9] << 8);
931 } else {
932 cmd->xfer = buf[9] | (buf[8] << 8);
934 break;
935 case WRITE_6:
936 /* length 0 means 256 blocks */
937 if (cmd->xfer == 0) {
938 cmd->xfer = 256;
940 case WRITE_10:
941 case WRITE_VERIFY_10:
942 case WRITE_12:
943 case WRITE_VERIFY_12:
944 case WRITE_16:
945 case WRITE_VERIFY_16:
946 cmd->xfer *= dev->blocksize;
947 break;
948 case READ_6:
949 case READ_REVERSE:
950 /* length 0 means 256 blocks */
951 if (cmd->xfer == 0) {
952 cmd->xfer = 256;
954 case READ_10:
955 case RECOVER_BUFFERED_DATA:
956 case READ_12:
957 case READ_16:
958 cmd->xfer *= dev->blocksize;
959 break;
960 case FORMAT_UNIT:
961 /* MMC mandates the parameter list to be 12-bytes long. Parameters
962 * for block devices are restricted to the header right now. */
963 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
964 cmd->xfer = 12;
965 } else {
966 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
968 break;
969 case INQUIRY:
970 case RECEIVE_DIAGNOSTIC:
971 case SEND_DIAGNOSTIC:
972 cmd->xfer = buf[4] | (buf[3] << 8);
973 break;
974 case READ_CD:
975 case READ_BUFFER:
976 case WRITE_BUFFER:
977 case SEND_CUE_SHEET:
978 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
979 break;
980 case PERSISTENT_RESERVE_OUT:
981 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
982 break;
983 case ERASE_12:
984 if (dev->type == TYPE_ROM) {
985 /* MMC command GET PERFORMANCE. */
986 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
987 buf[10], buf[1] & 0x1f);
989 break;
990 case MECHANISM_STATUS:
991 case READ_DVD_STRUCTURE:
992 case SEND_DVD_STRUCTURE:
993 case MAINTENANCE_OUT:
994 case MAINTENANCE_IN:
995 if (dev->type == TYPE_ROM) {
996 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
997 cmd->xfer = buf[9] | (buf[8] << 8);
999 break;
1000 case ATA_PASSTHROUGH_12:
1001 if (dev->type == TYPE_ROM) {
1002 /* BLANK command of MMC */
1003 cmd->xfer = 0;
1004 } else {
1005 cmd->xfer = ata_passthrough_12_xfer_size(dev, buf);
1007 break;
1008 case ATA_PASSTHROUGH_16:
1009 cmd->xfer = ata_passthrough_16_xfer_size(dev, buf);
1010 break;
1012 return 0;
1015 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1017 switch (buf[0]) {
1018 /* stream commands */
1019 case ERASE_12:
1020 case ERASE_16:
1021 cmd->xfer = 0;
1022 break;
1023 case READ_6:
1024 case READ_REVERSE:
1025 case RECOVER_BUFFERED_DATA:
1026 case WRITE_6:
1027 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
1028 if (buf[1] & 0x01) { /* fixed */
1029 cmd->xfer *= dev->blocksize;
1031 break;
1032 case READ_16:
1033 case READ_REVERSE_16:
1034 case VERIFY_16:
1035 case WRITE_16:
1036 cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
1037 if (buf[1] & 0x01) { /* fixed */
1038 cmd->xfer *= dev->blocksize;
1040 break;
1041 case REWIND:
1042 case LOAD_UNLOAD:
1043 cmd->xfer = 0;
1044 break;
1045 case SPACE_16:
1046 cmd->xfer = buf[13] | (buf[12] << 8);
1047 break;
1048 case READ_POSITION:
1049 switch (buf[1] & 0x1f) /* operation code */ {
1050 case SHORT_FORM_BLOCK_ID:
1051 case SHORT_FORM_VENDOR_SPECIFIC:
1052 cmd->xfer = 20;
1053 break;
1054 case LONG_FORM:
1055 cmd->xfer = 32;
1056 break;
1057 case EXTENDED_FORM:
1058 cmd->xfer = buf[8] | (buf[7] << 8);
1059 break;
1060 default:
1061 return -1;
1064 break;
1065 case FORMAT_UNIT:
1066 cmd->xfer = buf[4] | (buf[3] << 8);
1067 break;
1068 /* generic commands */
1069 default:
1070 return scsi_req_length(cmd, dev, buf);
1072 return 0;
1075 static int scsi_req_medium_changer_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1077 switch (buf[0]) {
1078 /* medium changer commands */
1079 case EXCHANGE_MEDIUM:
1080 case INITIALIZE_ELEMENT_STATUS:
1081 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1082 case MOVE_MEDIUM:
1083 case POSITION_TO_ELEMENT:
1084 cmd->xfer = 0;
1085 break;
1086 case READ_ELEMENT_STATUS:
1087 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1088 break;
1090 /* generic commands */
1091 default:
1092 return scsi_req_length(cmd, dev, buf);
1094 return 0;
1098 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1100 if (!cmd->xfer) {
1101 cmd->mode = SCSI_XFER_NONE;
1102 return;
1104 switch (cmd->buf[0]) {
1105 case WRITE_6:
1106 case WRITE_10:
1107 case WRITE_VERIFY_10:
1108 case WRITE_12:
1109 case WRITE_VERIFY_12:
1110 case WRITE_16:
1111 case WRITE_VERIFY_16:
1112 case VERIFY_10:
1113 case VERIFY_12:
1114 case VERIFY_16:
1115 case COPY:
1116 case COPY_VERIFY:
1117 case COMPARE:
1118 case CHANGE_DEFINITION:
1119 case LOG_SELECT:
1120 case MODE_SELECT:
1121 case MODE_SELECT_10:
1122 case SEND_DIAGNOSTIC:
1123 case WRITE_BUFFER:
1124 case FORMAT_UNIT:
1125 case REASSIGN_BLOCKS:
1126 case SEARCH_EQUAL:
1127 case SEARCH_HIGH:
1128 case SEARCH_LOW:
1129 case UPDATE_BLOCK:
1130 case WRITE_LONG_10:
1131 case WRITE_SAME_10:
1132 case WRITE_SAME_16:
1133 case UNMAP:
1134 case SEARCH_HIGH_12:
1135 case SEARCH_EQUAL_12:
1136 case SEARCH_LOW_12:
1137 case MEDIUM_SCAN:
1138 case SEND_VOLUME_TAG:
1139 case SEND_CUE_SHEET:
1140 case SEND_DVD_STRUCTURE:
1141 case PERSISTENT_RESERVE_OUT:
1142 case MAINTENANCE_OUT:
1143 cmd->mode = SCSI_XFER_TO_DEV;
1144 break;
1145 case ATA_PASSTHROUGH_12:
1146 case ATA_PASSTHROUGH_16:
1147 /* T_DIR */
1148 cmd->mode = (cmd->buf[2] & 0x8) ?
1149 SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1150 break;
1151 default:
1152 cmd->mode = SCSI_XFER_FROM_DEV;
1153 break;
1157 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
1159 uint8_t *buf = cmd->buf;
1160 uint64_t lba;
1162 switch (buf[0] >> 5) {
1163 case 0:
1164 lba = ldl_be_p(&buf[0]) & 0x1fffff;
1165 break;
1166 case 1:
1167 case 2:
1168 case 5:
1169 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
1170 break;
1171 case 4:
1172 lba = ldq_be_p(&buf[2]);
1173 break;
1174 default:
1175 lba = -1;
1178 return lba;
1181 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1183 int rc;
1185 switch (buf[0] >> 5) {
1186 case 0:
1187 cmd->len = 6;
1188 break;
1189 case 1:
1190 case 2:
1191 cmd->len = 10;
1192 break;
1193 case 4:
1194 cmd->len = 16;
1195 break;
1196 case 5:
1197 cmd->len = 12;
1198 break;
1199 default:
1200 return -1;
1203 switch (dev->type) {
1204 case TYPE_TAPE:
1205 rc = scsi_req_stream_length(cmd, dev, buf);
1206 break;
1207 case TYPE_MEDIUM_CHANGER:
1208 rc = scsi_req_medium_changer_length(cmd, dev, buf);
1209 break;
1210 default:
1211 rc = scsi_req_length(cmd, dev, buf);
1212 break;
1215 if (rc != 0)
1216 return rc;
1218 memcpy(cmd->buf, buf, cmd->len);
1219 scsi_cmd_xfer_mode(cmd);
1220 cmd->lba = scsi_cmd_lba(cmd);
1221 return 0;
1224 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1226 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1228 scsi_device_set_ua(dev, sense);
1229 if (bus->info->change) {
1230 bus->info->change(bus, dev, sense);
1235 * Predefined sense codes
1238 /* No sense data available */
1239 const struct SCSISense sense_code_NO_SENSE = {
1240 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
1243 /* LUN not ready, Manual intervention required */
1244 const struct SCSISense sense_code_LUN_NOT_READY = {
1245 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
1248 /* LUN not ready, Medium not present */
1249 const struct SCSISense sense_code_NO_MEDIUM = {
1250 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
1253 /* LUN not ready, medium removal prevented */
1254 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
1255 .key = NOT_READY, .asc = 0x53, .ascq = 0x02
1258 /* Hardware error, internal target failure */
1259 const struct SCSISense sense_code_TARGET_FAILURE = {
1260 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
1263 /* Illegal request, invalid command operation code */
1264 const struct SCSISense sense_code_INVALID_OPCODE = {
1265 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
1268 /* Illegal request, LBA out of range */
1269 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
1270 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
1273 /* Illegal request, Invalid field in CDB */
1274 const struct SCSISense sense_code_INVALID_FIELD = {
1275 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
1278 /* Illegal request, Invalid field in parameter list */
1279 const struct SCSISense sense_code_INVALID_PARAM = {
1280 .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
1283 /* Illegal request, Parameter list length error */
1284 const struct SCSISense sense_code_INVALID_PARAM_LEN = {
1285 .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
1288 /* Illegal request, LUN not supported */
1289 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
1290 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
1293 /* Illegal request, Saving parameters not supported */
1294 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
1295 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1298 /* Illegal request, Incompatible medium installed */
1299 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1300 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1303 /* Illegal request, medium removal prevented */
1304 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1305 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
1308 /* Illegal request, Invalid Transfer Tag */
1309 const struct SCSISense sense_code_INVALID_TAG = {
1310 .key = ILLEGAL_REQUEST, .asc = 0x4b, .ascq = 0x01
1313 /* Command aborted, I/O process terminated */
1314 const struct SCSISense sense_code_IO_ERROR = {
1315 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1318 /* Command aborted, I_T Nexus loss occurred */
1319 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1320 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1323 /* Command aborted, Logical Unit failure */
1324 const struct SCSISense sense_code_LUN_FAILURE = {
1325 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1328 /* Command aborted, Overlapped Commands Attempted */
1329 const struct SCSISense sense_code_OVERLAPPED_COMMANDS = {
1330 .key = ABORTED_COMMAND, .asc = 0x4e, .ascq = 0x00
1333 /* Unit attention, Capacity data has changed */
1334 const struct SCSISense sense_code_CAPACITY_CHANGED = {
1335 .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
1338 /* Unit attention, Power on, reset or bus device reset occurred */
1339 const struct SCSISense sense_code_RESET = {
1340 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1343 /* Unit attention, No medium */
1344 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1345 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1348 /* Unit attention, Medium may have changed */
1349 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1350 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1353 /* Unit attention, Reported LUNs data has changed */
1354 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1355 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1358 /* Unit attention, Device internal reset */
1359 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1360 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1363 /* Data Protection, Write Protected */
1364 const struct SCSISense sense_code_WRITE_PROTECTED = {
1365 .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
1369 * scsi_build_sense
1371 * Convert between fixed and descriptor sense buffers
1373 int scsi_build_sense(uint8_t *in_buf, int in_len,
1374 uint8_t *buf, int len, bool fixed)
1376 bool fixed_in;
1377 SCSISense sense;
1378 if (!fixed && len < 8) {
1379 return 0;
1382 if (in_len == 0) {
1383 sense.key = NO_SENSE;
1384 sense.asc = 0;
1385 sense.ascq = 0;
1386 } else {
1387 fixed_in = (in_buf[0] & 2) == 0;
1389 if (fixed == fixed_in) {
1390 memcpy(buf, in_buf, MIN(len, in_len));
1391 return MIN(len, in_len);
1394 if (fixed_in) {
1395 sense.key = in_buf[2];
1396 sense.asc = in_buf[12];
1397 sense.ascq = in_buf[13];
1398 } else {
1399 sense.key = in_buf[1];
1400 sense.asc = in_buf[2];
1401 sense.ascq = in_buf[3];
1405 memset(buf, 0, len);
1406 if (fixed) {
1407 /* Return fixed format sense buffer */
1408 buf[0] = 0x70;
1409 buf[2] = sense.key;
1410 buf[7] = 10;
1411 buf[12] = sense.asc;
1412 buf[13] = sense.ascq;
1413 return MIN(len, SCSI_SENSE_LEN);
1414 } else {
1415 /* Return descriptor format sense buffer */
1416 buf[0] = 0x72;
1417 buf[1] = sense.key;
1418 buf[2] = sense.asc;
1419 buf[3] = sense.ascq;
1420 return 8;
1424 static const char *scsi_command_name(uint8_t cmd)
1426 static const char *names[] = {
1427 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
1428 [ REWIND ] = "REWIND",
1429 [ REQUEST_SENSE ] = "REQUEST_SENSE",
1430 [ FORMAT_UNIT ] = "FORMAT_UNIT",
1431 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
1432 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1433 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1434 [ READ_6 ] = "READ_6",
1435 [ WRITE_6 ] = "WRITE_6",
1436 [ SET_CAPACITY ] = "SET_CAPACITY",
1437 [ READ_REVERSE ] = "READ_REVERSE",
1438 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
1439 [ SPACE ] = "SPACE",
1440 [ INQUIRY ] = "INQUIRY",
1441 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
1442 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
1443 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
1444 [ MODE_SELECT ] = "MODE_SELECT",
1445 [ RESERVE ] = "RESERVE",
1446 [ RELEASE ] = "RELEASE",
1447 [ COPY ] = "COPY",
1448 [ ERASE ] = "ERASE",
1449 [ MODE_SENSE ] = "MODE_SENSE",
1450 [ START_STOP ] = "START_STOP/LOAD_UNLOAD",
1451 /* LOAD_UNLOAD and START_STOP use the same operation code */
1452 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
1453 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
1454 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
1455 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
1456 [ READ_10 ] = "READ_10",
1457 [ WRITE_10 ] = "WRITE_10",
1458 [ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT",
1459 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1460 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
1461 [ VERIFY_10 ] = "VERIFY_10",
1462 [ SEARCH_HIGH ] = "SEARCH_HIGH",
1463 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
1464 [ SEARCH_LOW ] = "SEARCH_LOW",
1465 [ SET_LIMITS ] = "SET_LIMITS",
1466 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
1467 /* READ_POSITION and PRE_FETCH use the same operation code */
1468 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
1469 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
1470 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1471 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1472 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
1473 [ COMPARE ] = "COMPARE",
1474 [ COPY_VERIFY ] = "COPY_VERIFY",
1475 [ WRITE_BUFFER ] = "WRITE_BUFFER",
1476 [ READ_BUFFER ] = "READ_BUFFER",
1477 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
1478 [ READ_LONG_10 ] = "READ_LONG_10",
1479 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
1480 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
1481 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
1482 [ UNMAP ] = "UNMAP",
1483 [ READ_TOC ] = "READ_TOC",
1484 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
1485 [ SANITIZE ] = "SANITIZE",
1486 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
1487 [ LOG_SELECT ] = "LOG_SELECT",
1488 [ LOG_SENSE ] = "LOG_SENSE",
1489 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
1490 [ RESERVE_10 ] = "RESERVE_10",
1491 [ RELEASE_10 ] = "RELEASE_10",
1492 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
1493 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
1494 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
1495 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
1496 [ EXTENDED_COPY ] = "EXTENDED_COPY",
1497 [ ATA_PASSTHROUGH_16 ] = "ATA_PASSTHROUGH_16",
1498 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
1499 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
1500 [ READ_16 ] = "READ_16",
1501 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
1502 [ WRITE_16 ] = "WRITE_16",
1503 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
1504 [ VERIFY_16 ] = "VERIFY_16",
1505 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
1506 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1507 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1508 [ LOCATE_16 ] = "LOCATE_16",
1509 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
1510 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1511 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
1512 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
1513 [ REPORT_LUNS ] = "REPORT_LUNS",
1514 [ ATA_PASSTHROUGH_12 ] = "BLANK/ATA_PASSTHROUGH_12",
1515 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
1516 [ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM",
1517 [ READ_12 ] = "READ_12",
1518 [ WRITE_12 ] = "WRITE_12",
1519 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
1520 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1521 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
1522 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
1523 [ VERIFY_12 ] = "VERIFY_12",
1524 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
1525 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
1526 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
1527 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
1528 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
1529 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1530 [ READ_CD ] = "READ_CD",
1531 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
1532 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
1533 [ RESERVE_TRACK ] = "RESERVE_TRACK",
1534 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
1535 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
1536 [ SET_CD_SPEED ] = "SET_CD_SPEED",
1537 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
1538 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
1539 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
1542 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1543 return "*UNKNOWN*";
1544 return names[cmd];
1547 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1549 assert(req->refcount > 0);
1550 req->refcount++;
1551 return req;
1554 void scsi_req_unref(SCSIRequest *req)
1556 assert(req->refcount > 0);
1557 if (--req->refcount == 0) {
1558 BusState *qbus = req->dev->qdev.parent_bus;
1559 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, qbus);
1561 if (bus->info->free_request && req->hba_private) {
1562 bus->info->free_request(bus, req->hba_private);
1564 if (req->ops->free_req) {
1565 req->ops->free_req(req);
1567 object_unref(OBJECT(req->dev));
1568 object_unref(OBJECT(qbus->parent));
1569 g_free(req);
1573 /* Tell the device that we finished processing this chunk of I/O. It
1574 will start the next chunk or complete the command. */
1575 void scsi_req_continue(SCSIRequest *req)
1577 if (req->io_canceled) {
1578 trace_scsi_req_continue_canceled(req->dev->id, req->lun, req->tag);
1579 return;
1581 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1582 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1583 req->ops->write_data(req);
1584 } else {
1585 req->ops->read_data(req);
1589 /* Called by the devices when data is ready for the HBA. The HBA should
1590 start a DMA operation to read or fill the device's data buffer.
1591 Once it completes, calling scsi_req_continue will restart I/O. */
1592 void scsi_req_data(SCSIRequest *req, int len)
1594 uint8_t *buf;
1595 if (req->io_canceled) {
1596 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1597 return;
1599 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1600 assert(req->cmd.mode != SCSI_XFER_NONE);
1601 if (!req->sg) {
1602 req->resid -= len;
1603 req->bus->info->transfer_data(req, len);
1604 return;
1607 /* If the device calls scsi_req_data and the HBA specified a
1608 * scatter/gather list, the transfer has to happen in a single
1609 * step. */
1610 assert(!req->dma_started);
1611 req->dma_started = true;
1613 buf = scsi_req_get_buf(req);
1614 if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1615 req->resid = dma_buf_read(buf, len, req->sg);
1616 } else {
1617 req->resid = dma_buf_write(buf, len, req->sg);
1619 scsi_req_continue(req);
1622 void scsi_req_print(SCSIRequest *req)
1624 FILE *fp = stderr;
1625 int i;
1627 fprintf(fp, "[%s id=%d] %s",
1628 req->dev->qdev.parent_bus->name,
1629 req->dev->id,
1630 scsi_command_name(req->cmd.buf[0]));
1631 for (i = 1; i < req->cmd.len; i++) {
1632 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1634 switch (req->cmd.mode) {
1635 case SCSI_XFER_NONE:
1636 fprintf(fp, " - none\n");
1637 break;
1638 case SCSI_XFER_FROM_DEV:
1639 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1640 break;
1641 case SCSI_XFER_TO_DEV:
1642 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1643 break;
1644 default:
1645 fprintf(fp, " - Oops\n");
1646 break;
1650 void scsi_req_complete(SCSIRequest *req, int status)
1652 assert(req->status == -1);
1653 req->status = status;
1655 assert(req->sense_len <= sizeof(req->sense));
1656 if (status == GOOD) {
1657 req->sense_len = 0;
1660 if (req->sense_len) {
1661 memcpy(req->dev->sense, req->sense, req->sense_len);
1662 req->dev->sense_len = req->sense_len;
1663 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1664 } else {
1665 req->dev->sense_len = 0;
1666 req->dev->sense_is_ua = false;
1670 * Unit attention state is now stored in the device's sense buffer
1671 * if the HBA didn't do autosense. Clear the pending unit attention
1672 * flags.
1674 scsi_clear_unit_attention(req);
1676 scsi_req_ref(req);
1677 scsi_req_dequeue(req);
1678 req->bus->info->complete(req, req->status, req->resid);
1679 scsi_req_unref(req);
1682 void scsi_req_cancel(SCSIRequest *req)
1684 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1685 if (!req->enqueued) {
1686 return;
1688 scsi_req_ref(req);
1689 scsi_req_dequeue(req);
1690 req->io_canceled = true;
1691 if (req->ops->cancel_io) {
1692 req->ops->cancel_io(req);
1694 if (req->bus->info->cancel) {
1695 req->bus->info->cancel(req);
1697 scsi_req_unref(req);
1700 void scsi_req_abort(SCSIRequest *req, int status)
1702 if (!req->enqueued) {
1703 return;
1705 scsi_req_ref(req);
1706 scsi_req_dequeue(req);
1707 req->io_canceled = true;
1708 if (req->ops->cancel_io) {
1709 req->ops->cancel_io(req);
1711 scsi_req_complete(req, status);
1712 scsi_req_unref(req);
1715 static int scsi_ua_precedence(SCSISense sense)
1717 if (sense.key != UNIT_ATTENTION) {
1718 return INT_MAX;
1720 if (sense.asc == 0x29 && sense.ascq == 0x04) {
1721 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1722 return 1;
1723 } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1724 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1725 return 2;
1726 } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1727 /* These two go with "all others". */
1729 } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1730 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1731 * POWER ON OCCURRED = 1
1732 * SCSI BUS RESET OCCURRED = 2
1733 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1734 * I_T NEXUS LOSS OCCURRED = 7
1736 return sense.ascq;
1737 } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1738 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1739 return 8;
1741 return (sense.asc << 8) | sense.ascq;
1744 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1746 int prec1, prec2;
1747 if (sense.key != UNIT_ATTENTION) {
1748 return;
1750 trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1751 sense.asc, sense.ascq);
1754 * Override a pre-existing unit attention condition, except for a more
1755 * important reset condition.
1757 prec1 = scsi_ua_precedence(sdev->unit_attention);
1758 prec2 = scsi_ua_precedence(sense);
1759 if (prec2 < prec1) {
1760 sdev->unit_attention = sense;
1764 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1766 SCSIRequest *req;
1768 while (!QTAILQ_EMPTY(&sdev->requests)) {
1769 req = QTAILQ_FIRST(&sdev->requests);
1770 scsi_req_cancel(req);
1773 scsi_device_set_ua(sdev, sense);
1776 static char *scsibus_get_dev_path(DeviceState *dev)
1778 SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1779 DeviceState *hba = dev->parent_bus->parent;
1780 char *id;
1781 char *path;
1783 id = qdev_get_dev_path(hba);
1784 if (id) {
1785 path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1786 } else {
1787 path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1789 g_free(id);
1790 return path;
1793 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1795 SCSIDevice *d = SCSI_DEVICE(dev);
1796 return g_strdup_printf("channel@%x/%s@%x,%x", d->channel,
1797 qdev_fw_name(dev), d->id, d->lun);
1800 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1802 BusChild *kid;
1803 SCSIDevice *target_dev = NULL;
1805 QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) {
1806 DeviceState *qdev = kid->child;
1807 SCSIDevice *dev = SCSI_DEVICE(qdev);
1809 if (dev->channel == channel && dev->id == id) {
1810 if (dev->lun == lun) {
1811 return dev;
1813 target_dev = dev;
1816 return target_dev;
1819 /* SCSI request list. For simplicity, pv points to the whole device */
1821 static void put_scsi_requests(QEMUFile *f, void *pv, size_t size)
1823 SCSIDevice *s = pv;
1824 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1825 SCSIRequest *req;
1827 QTAILQ_FOREACH(req, &s->requests, next) {
1828 assert(!req->io_canceled);
1829 assert(req->status == -1);
1830 assert(req->enqueued);
1832 qemu_put_sbyte(f, req->retry ? 1 : 2);
1833 qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1834 qemu_put_be32s(f, &req->tag);
1835 qemu_put_be32s(f, &req->lun);
1836 if (bus->info->save_request) {
1837 bus->info->save_request(f, req);
1839 if (req->ops->save_request) {
1840 req->ops->save_request(f, req);
1843 qemu_put_sbyte(f, 0);
1846 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size)
1848 SCSIDevice *s = pv;
1849 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1850 int8_t sbyte;
1852 while ((sbyte = qemu_get_sbyte(f)) > 0) {
1853 uint8_t buf[SCSI_CMD_BUF_SIZE];
1854 uint32_t tag;
1855 uint32_t lun;
1856 SCSIRequest *req;
1858 qemu_get_buffer(f, buf, sizeof(buf));
1859 qemu_get_be32s(f, &tag);
1860 qemu_get_be32s(f, &lun);
1861 req = scsi_req_new(s, tag, lun, buf, NULL);
1862 req->retry = (sbyte == 1);
1863 if (bus->info->load_request) {
1864 req->hba_private = bus->info->load_request(f, req);
1866 if (req->ops->load_request) {
1867 req->ops->load_request(f, req);
1870 /* Just restart it later. */
1871 scsi_req_enqueue_internal(req);
1873 /* At this point, the request will be kept alive by the reference
1874 * added by scsi_req_enqueue_internal, so we can release our reference.
1875 * The HBA of course will add its own reference in the load_request
1876 * callback if it needs to hold on the SCSIRequest.
1878 scsi_req_unref(req);
1881 return 0;
1884 static int scsi_qdev_unplug(DeviceState *qdev)
1886 SCSIDevice *dev = SCSI_DEVICE(qdev);
1887 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1889 if (bus->info->hot_unplug) {
1890 bus->info->hot_unplug(bus, dev);
1892 return qdev_simple_unplug_cb(qdev);
1895 static const VMStateInfo vmstate_info_scsi_requests = {
1896 .name = "scsi-requests",
1897 .get = get_scsi_requests,
1898 .put = put_scsi_requests,
1901 const VMStateDescription vmstate_scsi_device = {
1902 .name = "SCSIDevice",
1903 .version_id = 1,
1904 .minimum_version_id = 1,
1905 .minimum_version_id_old = 1,
1906 .fields = (VMStateField[]) {
1907 VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1908 VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1909 VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1910 VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1911 VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE),
1912 VMSTATE_UINT32(sense_len, SCSIDevice),
1914 .name = "requests",
1915 .version_id = 0,
1916 .field_exists = NULL,
1917 .size = 0, /* ouch */
1918 .info = &vmstate_info_scsi_requests,
1919 .flags = VMS_SINGLE,
1920 .offset = 0,
1922 VMSTATE_END_OF_LIST()
1926 static void scsi_device_class_init(ObjectClass *klass, void *data)
1928 DeviceClass *k = DEVICE_CLASS(klass);
1929 set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
1930 k->bus_type = TYPE_SCSI_BUS;
1931 k->init = scsi_qdev_init;
1932 k->unplug = scsi_qdev_unplug;
1933 k->exit = scsi_qdev_exit;
1934 k->props = scsi_props;
1937 static const TypeInfo scsi_device_type_info = {
1938 .name = TYPE_SCSI_DEVICE,
1939 .parent = TYPE_DEVICE,
1940 .instance_size = sizeof(SCSIDevice),
1941 .abstract = true,
1942 .class_size = sizeof(SCSIDeviceClass),
1943 .class_init = scsi_device_class_init,
1946 static void scsi_register_types(void)
1948 type_register_static(&scsi_bus_info);
1949 type_register_static(&scsi_device_type_info);
1952 type_init(scsi_register_types)