qdev: register all types natively through QEMU Object Model
[qemu.git] / hw / scsi-bus.c
blobb75044f93be1ed06fd8e29ba5de548c6c65f2d6d
1 #include "hw.h"
2 #include "qemu-error.h"
3 #include "scsi.h"
4 #include "scsi-defs.h"
5 #include "qdev.h"
6 #include "blockdev.h"
7 #include "trace.h"
9 static char *scsibus_get_fw_dev_path(DeviceState *dev);
10 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
11 static void scsi_req_dequeue(SCSIRequest *req);
13 static struct BusInfo scsi_bus_info = {
14 .name = "SCSI",
15 .size = sizeof(SCSIBus),
16 .get_fw_dev_path = scsibus_get_fw_dev_path,
17 .props = (Property[]) {
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 int next_scsi_bus;
26 static int scsi_device_init(SCSIDevice *s)
28 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
29 if (sc->init) {
30 return sc->init(s);
32 return 0;
35 static void scsi_device_destroy(SCSIDevice *s)
37 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
38 if (sc->destroy) {
39 sc->destroy(s);
43 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
44 uint8_t *buf, void *hba_private)
46 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
47 if (sc->alloc_req) {
48 return sc->alloc_req(s, tag, lun, buf, hba_private);
51 return NULL;
54 static void scsi_device_unit_attention_reported(SCSIDevice *s)
56 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
57 if (sc->unit_attention_reported) {
58 sc->unit_attention_reported(s);
62 /* Create a scsi bus, and attach devices to it. */
63 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info)
65 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
66 bus->busnr = next_scsi_bus++;
67 bus->info = info;
68 bus->qbus.allow_hotplug = 1;
71 static void scsi_dma_restart_bh(void *opaque)
73 SCSIDevice *s = opaque;
74 SCSIRequest *req, *next;
76 qemu_bh_delete(s->bh);
77 s->bh = NULL;
79 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
80 scsi_req_ref(req);
81 if (req->retry) {
82 req->retry = false;
83 switch (req->cmd.mode) {
84 case SCSI_XFER_FROM_DEV:
85 case SCSI_XFER_TO_DEV:
86 scsi_req_continue(req);
87 break;
88 case SCSI_XFER_NONE:
89 scsi_req_dequeue(req);
90 scsi_req_enqueue(req);
91 break;
94 scsi_req_unref(req);
98 void scsi_req_retry(SCSIRequest *req)
100 /* No need to save a reference, because scsi_dma_restart_bh just
101 * looks at the request list. */
102 req->retry = true;
105 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
107 SCSIDevice *s = opaque;
109 if (!running) {
110 return;
112 if (!s->bh) {
113 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
114 qemu_bh_schedule(s->bh);
118 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
120 SCSIDevice *dev = SCSI_DEVICE(qdev);
121 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
122 SCSIDevice *d;
123 int rc = -1;
125 if (dev->channel > bus->info->max_channel) {
126 error_report("bad scsi channel id: %d", dev->channel);
127 goto err;
129 if (dev->id != -1 && dev->id > bus->info->max_target) {
130 error_report("bad scsi device id: %d", dev->id);
131 goto err;
134 if (dev->id == -1) {
135 int id = -1;
136 if (dev->lun == -1) {
137 dev->lun = 0;
139 do {
140 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
141 } while (d && d->lun == dev->lun && id <= bus->info->max_target);
142 if (id > bus->info->max_target) {
143 error_report("no free target");
144 goto err;
146 dev->id = id;
147 } else if (dev->lun == -1) {
148 int lun = -1;
149 do {
150 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
151 } while (d && d->lun == lun && lun < bus->info->max_lun);
152 if (lun > bus->info->max_lun) {
153 error_report("no free lun");
154 goto err;
156 dev->lun = lun;
157 } else {
158 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
159 if (dev->lun == d->lun && dev != d) {
160 qdev_free(&d->qdev);
164 QTAILQ_INIT(&dev->requests);
165 rc = scsi_device_init(dev);
166 if (rc == 0) {
167 dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
168 dev);
171 err:
172 return rc;
175 static int scsi_qdev_exit(DeviceState *qdev)
177 SCSIDevice *dev = SCSI_DEVICE(qdev);
179 if (dev->vmsentry) {
180 qemu_del_vm_change_state_handler(dev->vmsentry);
182 scsi_device_destroy(dev);
183 return 0;
186 /* handle legacy '-drive if=scsi,...' cmd line args */
187 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
188 int unit, bool removable, int bootindex)
190 const char *driver;
191 DeviceState *dev;
193 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
194 dev = qdev_create(&bus->qbus, driver);
195 qdev_prop_set_uint32(dev, "scsi-id", unit);
196 if (bootindex >= 0) {
197 qdev_prop_set_int32(dev, "bootindex", bootindex);
199 if (qdev_prop_exists(dev, "removable")) {
200 qdev_prop_set_bit(dev, "removable", removable);
202 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
203 qdev_free(dev);
204 return NULL;
206 if (qdev_init(dev) < 0)
207 return NULL;
208 return SCSI_DEVICE(dev);
211 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
213 Location loc;
214 DriveInfo *dinfo;
215 int res = 0, unit;
217 loc_push_none(&loc);
218 for (unit = 0; unit < bus->info->max_target; unit++) {
219 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
220 if (dinfo == NULL) {
221 continue;
223 qemu_opts_loc_restore(dinfo->opts);
224 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) {
225 res = -1;
226 break;
229 loc_pop(&loc);
230 return res;
233 /* SCSIReqOps implementation for invalid commands. */
235 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
237 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
238 scsi_req_complete(req, CHECK_CONDITION);
239 return 0;
242 static const struct SCSIReqOps reqops_invalid_opcode = {
243 .size = sizeof(SCSIRequest),
244 .send_command = scsi_invalid_command
247 /* SCSIReqOps implementation for unit attention conditions. */
249 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
251 if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
252 scsi_req_build_sense(req, req->dev->unit_attention);
253 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
254 scsi_req_build_sense(req, req->bus->unit_attention);
256 scsi_req_complete(req, CHECK_CONDITION);
257 return 0;
260 static const struct SCSIReqOps reqops_unit_attention = {
261 .size = sizeof(SCSIRequest),
262 .send_command = scsi_unit_attention
265 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
266 an invalid LUN. */
268 typedef struct SCSITargetReq SCSITargetReq;
270 struct SCSITargetReq {
271 SCSIRequest req;
272 int len;
273 uint8_t buf[2056];
276 static void store_lun(uint8_t *outbuf, int lun)
278 if (lun < 256) {
279 outbuf[1] = lun;
280 return;
282 outbuf[1] = (lun & 255);
283 outbuf[0] = (lun >> 8) | 0x40;
286 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
288 DeviceState *qdev;
289 int i, len, n;
290 int channel, id;
291 bool found_lun0;
293 if (r->req.cmd.xfer < 16) {
294 return false;
296 if (r->req.cmd.buf[2] > 2) {
297 return false;
299 channel = r->req.dev->channel;
300 id = r->req.dev->id;
301 found_lun0 = false;
302 n = 0;
303 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
304 SCSIDevice *dev = SCSI_DEVICE(qdev);
306 if (dev->channel == channel && dev->id == id) {
307 if (dev->lun == 0) {
308 found_lun0 = true;
310 n += 8;
313 if (!found_lun0) {
314 n += 8;
316 len = MIN(n + 8, r->req.cmd.xfer & ~7);
317 if (len > sizeof(r->buf)) {
318 /* TODO: > 256 LUNs? */
319 return false;
322 memset(r->buf, 0, len);
323 stl_be_p(&r->buf, n);
324 i = found_lun0 ? 8 : 16;
325 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
326 SCSIDevice *dev = SCSI_DEVICE(qdev);
328 if (dev->channel == channel && dev->id == id) {
329 store_lun(&r->buf[i], dev->lun);
330 i += 8;
333 assert(i == n + 8);
334 r->len = len;
335 return true;
338 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
340 assert(r->req.dev->lun != r->req.lun);
341 if (r->req.cmd.buf[1] & 0x2) {
342 /* Command support data - optional, not implemented */
343 return false;
346 if (r->req.cmd.buf[1] & 0x1) {
347 /* Vital product data */
348 uint8_t page_code = r->req.cmd.buf[2];
349 if (r->req.cmd.xfer < 4) {
350 return false;
353 r->buf[r->len++] = page_code ; /* this page */
354 r->buf[r->len++] = 0x00;
356 switch (page_code) {
357 case 0x00: /* Supported page codes, mandatory */
359 int pages;
360 pages = r->len++;
361 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
362 r->buf[pages] = r->len - pages - 1; /* number of pages */
363 break;
365 default:
366 return false;
368 /* done with EVPD */
369 assert(r->len < sizeof(r->buf));
370 r->len = MIN(r->req.cmd.xfer, r->len);
371 return true;
374 /* Standard INQUIRY data */
375 if (r->req.cmd.buf[2] != 0) {
376 return false;
379 /* PAGE CODE == 0 */
380 if (r->req.cmd.xfer < 5) {
381 return -1;
384 r->len = MIN(r->req.cmd.xfer, 36);
385 memset(r->buf, 0, r->len);
386 if (r->req.lun != 0) {
387 r->buf[0] = TYPE_NO_LUN;
388 } else {
389 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
390 r->buf[2] = 5; /* Version */
391 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
392 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
393 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
394 memcpy(&r->buf[8], "QEMU ", 8);
395 memcpy(&r->buf[16], "QEMU TARGET ", 16);
396 strncpy((char *) &r->buf[32], QEMU_VERSION, 4);
398 return true;
401 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
403 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
405 switch (buf[0]) {
406 case REPORT_LUNS:
407 if (!scsi_target_emulate_report_luns(r)) {
408 goto illegal_request;
410 break;
411 case INQUIRY:
412 if (!scsi_target_emulate_inquiry(r)) {
413 goto illegal_request;
415 break;
416 case REQUEST_SENSE:
417 if (req->cmd.xfer < 4) {
418 goto illegal_request;
420 r->len = scsi_device_get_sense(r->req.dev, r->buf,
421 MIN(req->cmd.xfer, sizeof r->buf),
422 (req->cmd.buf[1] & 1) == 0);
423 if (r->req.dev->sense_is_ua) {
424 scsi_device_unit_attention_reported(req->dev);
425 r->req.dev->sense_len = 0;
426 r->req.dev->sense_is_ua = false;
428 break;
429 default:
430 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
431 scsi_req_complete(req, CHECK_CONDITION);
432 return 0;
433 illegal_request:
434 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
435 scsi_req_complete(req, CHECK_CONDITION);
436 return 0;
439 if (!r->len) {
440 scsi_req_complete(req, GOOD);
442 return r->len;
445 static void scsi_target_read_data(SCSIRequest *req)
447 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
448 uint32_t n;
450 n = r->len;
451 if (n > 0) {
452 r->len = 0;
453 scsi_req_data(&r->req, n);
454 } else {
455 scsi_req_complete(&r->req, GOOD);
459 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
461 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
463 return r->buf;
466 static const struct SCSIReqOps reqops_target_command = {
467 .size = sizeof(SCSITargetReq),
468 .send_command = scsi_target_send_command,
469 .read_data = scsi_target_read_data,
470 .get_buf = scsi_target_get_buf,
474 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
475 uint32_t tag, uint32_t lun, void *hba_private)
477 SCSIRequest *req;
479 req = g_malloc0(reqops->size);
480 req->refcount = 1;
481 req->bus = scsi_bus_from_device(d);
482 req->dev = d;
483 req->tag = tag;
484 req->lun = lun;
485 req->hba_private = hba_private;
486 req->status = -1;
487 req->sense_len = 0;
488 req->ops = reqops;
489 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
490 return req;
493 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
494 uint8_t *buf, void *hba_private)
496 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
497 SCSIRequest *req;
498 SCSICommand cmd;
500 if (scsi_req_parse(&cmd, d, buf) != 0) {
501 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
502 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
503 } else {
504 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
505 cmd.mode, cmd.xfer);
506 if (cmd.lba != -1) {
507 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
508 cmd.lba);
511 if ((d->unit_attention.key == UNIT_ATTENTION ||
512 bus->unit_attention.key == UNIT_ATTENTION) &&
513 (buf[0] != INQUIRY &&
514 buf[0] != REPORT_LUNS &&
515 buf[0] != GET_CONFIGURATION &&
516 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
519 * If we already have a pending unit attention condition,
520 * report this one before triggering another one.
522 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
523 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
524 hba_private);
525 } else if (lun != d->lun ||
526 buf[0] == REPORT_LUNS ||
527 (buf[0] == REQUEST_SENSE && (d->sense_len || cmd.xfer < 4))) {
528 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
529 hba_private);
530 } else {
531 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
535 req->cmd = cmd;
536 switch (buf[0]) {
537 case INQUIRY:
538 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
539 break;
540 case TEST_UNIT_READY:
541 trace_scsi_test_unit_ready(d->id, lun, tag);
542 break;
543 case REPORT_LUNS:
544 trace_scsi_report_luns(d->id, lun, tag);
545 break;
546 case REQUEST_SENSE:
547 trace_scsi_request_sense(d->id, lun, tag);
548 break;
549 default:
550 break;
553 return req;
556 uint8_t *scsi_req_get_buf(SCSIRequest *req)
558 return req->ops->get_buf(req);
561 static void scsi_clear_unit_attention(SCSIRequest *req)
563 SCSISense *ua;
564 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
565 req->bus->unit_attention.key != UNIT_ATTENTION) {
566 return;
570 * If an INQUIRY command enters the enabled command state,
571 * the device server shall [not] clear any unit attention condition;
572 * See also MMC-6, paragraphs 6.5 and 6.6.2.
574 if (req->cmd.buf[0] == INQUIRY ||
575 req->cmd.buf[0] == GET_CONFIGURATION ||
576 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
577 return;
580 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
581 ua = &req->dev->unit_attention;
582 } else {
583 ua = &req->bus->unit_attention;
587 * If a REPORT LUNS command enters the enabled command state, [...]
588 * the device server shall clear any pending unit attention condition
589 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
591 if (req->cmd.buf[0] == REPORT_LUNS &&
592 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
593 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
594 return;
597 *ua = SENSE_CODE(NO_SENSE);
600 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
602 int ret;
604 assert(len >= 14);
605 if (!req->sense_len) {
606 return 0;
609 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
612 * FIXME: clearing unit attention conditions upon autosense should be done
613 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
614 * (SAM-5, 5.14).
616 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
617 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
618 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
620 if (req->dev->sense_is_ua) {
621 scsi_device_unit_attention_reported(req->dev);
622 req->dev->sense_len = 0;
623 req->dev->sense_is_ua = false;
625 return ret;
628 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
630 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
633 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
635 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
636 sense.key, sense.asc, sense.ascq);
637 memset(req->sense, 0, 18);
638 req->sense[0] = 0xf0;
639 req->sense[2] = sense.key;
640 req->sense[7] = 10;
641 req->sense[12] = sense.asc;
642 req->sense[13] = sense.ascq;
643 req->sense_len = 18;
646 int32_t scsi_req_enqueue(SCSIRequest *req)
648 int32_t rc;
650 assert(!req->enqueued);
651 scsi_req_ref(req);
652 req->enqueued = true;
653 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
655 scsi_req_ref(req);
656 rc = req->ops->send_command(req, req->cmd.buf);
657 scsi_req_unref(req);
658 return rc;
661 static void scsi_req_dequeue(SCSIRequest *req)
663 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
664 req->retry = false;
665 if (req->enqueued) {
666 QTAILQ_REMOVE(&req->dev->requests, req, next);
667 req->enqueued = false;
668 scsi_req_unref(req);
672 static int scsi_get_performance_length(int num_desc, int type, int data_type)
674 /* MMC-6, paragraph 6.7. */
675 switch (type) {
676 case 0:
677 if ((data_type & 3) == 0) {
678 /* Each descriptor is as in Table 295 - Nominal performance. */
679 return 16 * num_desc + 8;
680 } else {
681 /* Each descriptor is as in Table 296 - Exceptions. */
682 return 6 * num_desc + 8;
684 case 1:
685 case 4:
686 case 5:
687 return 8 * num_desc + 8;
688 case 2:
689 return 2048 * num_desc + 8;
690 case 3:
691 return 16 * num_desc + 8;
692 default:
693 return 8;
697 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
699 switch (buf[0] >> 5) {
700 case 0:
701 cmd->xfer = buf[4];
702 cmd->len = 6;
703 /* length 0 means 256 blocks */
704 if (cmd->xfer == 0) {
705 cmd->xfer = 256;
707 break;
708 case 1:
709 case 2:
710 cmd->xfer = lduw_be_p(&buf[7]);
711 cmd->len = 10;
712 break;
713 case 4:
714 cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL;
715 cmd->len = 16;
716 break;
717 case 5:
718 cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL;
719 cmd->len = 12;
720 break;
721 default:
722 return -1;
725 switch (buf[0]) {
726 case TEST_UNIT_READY:
727 case REWIND:
728 case START_STOP:
729 case SET_CAPACITY:
730 case WRITE_FILEMARKS:
731 case WRITE_FILEMARKS_16:
732 case SPACE:
733 case RESERVE:
734 case RELEASE:
735 case ERASE:
736 case ALLOW_MEDIUM_REMOVAL:
737 case VERIFY_10:
738 case SEEK_10:
739 case SYNCHRONIZE_CACHE:
740 case SYNCHRONIZE_CACHE_16:
741 case LOCATE_16:
742 case LOCK_UNLOCK_CACHE:
743 case LOAD_UNLOAD:
744 case SET_CD_SPEED:
745 case SET_LIMITS:
746 case WRITE_LONG_10:
747 case MOVE_MEDIUM:
748 case UPDATE_BLOCK:
749 case RESERVE_TRACK:
750 case SET_READ_AHEAD:
751 case PRE_FETCH:
752 case PRE_FETCH_16:
753 case ALLOW_OVERWRITE:
754 cmd->xfer = 0;
755 break;
756 case MODE_SENSE:
757 break;
758 case WRITE_SAME_10:
759 cmd->xfer = 1;
760 break;
761 case READ_CAPACITY_10:
762 cmd->xfer = 8;
763 break;
764 case READ_BLOCK_LIMITS:
765 cmd->xfer = 6;
766 break;
767 case SEND_VOLUME_TAG:
768 /* GPCMD_SET_STREAMING from multimedia commands. */
769 if (dev->type == TYPE_ROM) {
770 cmd->xfer = buf[10] | (buf[9] << 8);
771 } else {
772 cmd->xfer = buf[9] | (buf[8] << 8);
774 break;
775 case WRITE_10:
776 case WRITE_VERIFY_10:
777 case WRITE_6:
778 case WRITE_12:
779 case WRITE_VERIFY_12:
780 case WRITE_16:
781 case WRITE_VERIFY_16:
782 cmd->xfer *= dev->blocksize;
783 break;
784 case READ_10:
785 case READ_6:
786 case READ_REVERSE:
787 case RECOVER_BUFFERED_DATA:
788 case READ_12:
789 case READ_16:
790 cmd->xfer *= dev->blocksize;
791 break;
792 case FORMAT_UNIT:
793 /* MMC mandates the parameter list to be 12-bytes long. Parameters
794 * for block devices are restricted to the header right now. */
795 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
796 cmd->xfer = 12;
797 } else {
798 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
800 break;
801 case INQUIRY:
802 case RECEIVE_DIAGNOSTIC:
803 case SEND_DIAGNOSTIC:
804 cmd->xfer = buf[4] | (buf[3] << 8);
805 break;
806 case READ_CD:
807 case READ_BUFFER:
808 case WRITE_BUFFER:
809 case SEND_CUE_SHEET:
810 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
811 break;
812 case PERSISTENT_RESERVE_OUT:
813 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
814 break;
815 case ERASE_12:
816 if (dev->type == TYPE_ROM) {
817 /* MMC command GET PERFORMANCE. */
818 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
819 buf[10], buf[1] & 0x1f);
821 break;
822 case MECHANISM_STATUS:
823 case READ_DVD_STRUCTURE:
824 case SEND_DVD_STRUCTURE:
825 case MAINTENANCE_OUT:
826 case MAINTENANCE_IN:
827 if (dev->type == TYPE_ROM) {
828 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
829 cmd->xfer = buf[9] | (buf[8] << 8);
831 break;
833 return 0;
836 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
838 switch (buf[0]) {
839 /* stream commands */
840 case ERASE_12:
841 case ERASE_16:
842 cmd->xfer = 0;
843 break;
844 case READ_6:
845 case READ_REVERSE:
846 case RECOVER_BUFFERED_DATA:
847 case WRITE_6:
848 cmd->len = 6;
849 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
850 if (buf[1] & 0x01) { /* fixed */
851 cmd->xfer *= dev->blocksize;
853 break;
854 case REWIND:
855 case START_STOP:
856 cmd->len = 6;
857 cmd->xfer = 0;
858 break;
859 case SPACE_16:
860 cmd->xfer = buf[13] | (buf[12] << 8);
861 break;
862 case READ_POSITION:
863 cmd->xfer = buf[8] | (buf[7] << 8);
864 break;
865 case FORMAT_UNIT:
866 cmd->xfer = buf[4] | (buf[3] << 8);
867 break;
868 /* generic commands */
869 default:
870 return scsi_req_length(cmd, dev, buf);
872 return 0;
875 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
877 switch (cmd->buf[0]) {
878 case WRITE_6:
879 case WRITE_10:
880 case WRITE_VERIFY_10:
881 case WRITE_12:
882 case WRITE_VERIFY_12:
883 case WRITE_16:
884 case WRITE_VERIFY_16:
885 case COPY:
886 case COPY_VERIFY:
887 case COMPARE:
888 case CHANGE_DEFINITION:
889 case LOG_SELECT:
890 case MODE_SELECT:
891 case MODE_SELECT_10:
892 case SEND_DIAGNOSTIC:
893 case WRITE_BUFFER:
894 case FORMAT_UNIT:
895 case REASSIGN_BLOCKS:
896 case SEARCH_EQUAL:
897 case SEARCH_HIGH:
898 case SEARCH_LOW:
899 case UPDATE_BLOCK:
900 case WRITE_LONG_10:
901 case WRITE_SAME_10:
902 case SEARCH_HIGH_12:
903 case SEARCH_EQUAL_12:
904 case SEARCH_LOW_12:
905 case MEDIUM_SCAN:
906 case SEND_VOLUME_TAG:
907 case SEND_CUE_SHEET:
908 case SEND_DVD_STRUCTURE:
909 case PERSISTENT_RESERVE_OUT:
910 case MAINTENANCE_OUT:
911 cmd->mode = SCSI_XFER_TO_DEV;
912 break;
913 default:
914 if (cmd->xfer)
915 cmd->mode = SCSI_XFER_FROM_DEV;
916 else {
917 cmd->mode = SCSI_XFER_NONE;
919 break;
923 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
925 uint8_t *buf = cmd->buf;
926 uint64_t lba;
928 switch (buf[0] >> 5) {
929 case 0:
930 lba = ldl_be_p(&buf[0]) & 0x1fffff;
931 break;
932 case 1:
933 case 2:
934 case 5:
935 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
936 break;
937 case 4:
938 lba = ldq_be_p(&buf[2]);
939 break;
940 default:
941 lba = -1;
944 return lba;
947 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
949 int rc;
951 if (dev->type == TYPE_TAPE) {
952 rc = scsi_req_stream_length(cmd, dev, buf);
953 } else {
954 rc = scsi_req_length(cmd, dev, buf);
956 if (rc != 0)
957 return rc;
959 memcpy(cmd->buf, buf, cmd->len);
960 scsi_cmd_xfer_mode(cmd);
961 cmd->lba = scsi_cmd_lba(cmd);
962 return 0;
966 * Predefined sense codes
969 /* No sense data available */
970 const struct SCSISense sense_code_NO_SENSE = {
971 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
974 /* LUN not ready, Manual intervention required */
975 const struct SCSISense sense_code_LUN_NOT_READY = {
976 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
979 /* LUN not ready, Medium not present */
980 const struct SCSISense sense_code_NO_MEDIUM = {
981 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
984 /* LUN not ready, medium removal prevented */
985 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
986 .key = NOT_READY, .asc = 0x53, .ascq = 0x00
989 /* Hardware error, internal target failure */
990 const struct SCSISense sense_code_TARGET_FAILURE = {
991 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
994 /* Illegal request, invalid command operation code */
995 const struct SCSISense sense_code_INVALID_OPCODE = {
996 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
999 /* Illegal request, LBA out of range */
1000 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
1001 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
1004 /* Illegal request, Invalid field in CDB */
1005 const struct SCSISense sense_code_INVALID_FIELD = {
1006 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
1009 /* Illegal request, LUN not supported */
1010 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
1011 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
1014 /* Illegal request, Saving parameters not supported */
1015 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
1016 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1019 /* Illegal request, Incompatible medium installed */
1020 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1021 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1024 /* Illegal request, medium removal prevented */
1025 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1026 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00
1029 /* Command aborted, I/O process terminated */
1030 const struct SCSISense sense_code_IO_ERROR = {
1031 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1034 /* Command aborted, I_T Nexus loss occurred */
1035 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1036 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1039 /* Command aborted, Logical Unit failure */
1040 const struct SCSISense sense_code_LUN_FAILURE = {
1041 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1044 /* Unit attention, Power on, reset or bus device reset occurred */
1045 const struct SCSISense sense_code_RESET = {
1046 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1049 /* Unit attention, No medium */
1050 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1051 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1054 /* Unit attention, Medium may have changed */
1055 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1056 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1059 /* Unit attention, Reported LUNs data has changed */
1060 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1061 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1064 /* Unit attention, Device internal reset */
1065 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1066 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1070 * scsi_build_sense
1072 * Convert between fixed and descriptor sense buffers
1074 int scsi_build_sense(uint8_t *in_buf, int in_len,
1075 uint8_t *buf, int len, bool fixed)
1077 bool fixed_in;
1078 SCSISense sense;
1079 if (!fixed && len < 8) {
1080 return 0;
1083 if (in_len == 0) {
1084 sense.key = NO_SENSE;
1085 sense.asc = 0;
1086 sense.ascq = 0;
1087 } else {
1088 fixed_in = (in_buf[0] & 2) == 0;
1090 if (fixed == fixed_in) {
1091 memcpy(buf, in_buf, MIN(len, in_len));
1092 return MIN(len, in_len);
1095 if (fixed_in) {
1096 sense.key = in_buf[2];
1097 sense.asc = in_buf[12];
1098 sense.ascq = in_buf[13];
1099 } else {
1100 sense.key = in_buf[1];
1101 sense.asc = in_buf[2];
1102 sense.ascq = in_buf[3];
1106 memset(buf, 0, len);
1107 if (fixed) {
1108 /* Return fixed format sense buffer */
1109 buf[0] = 0xf0;
1110 buf[2] = sense.key;
1111 buf[7] = 10;
1112 buf[12] = sense.asc;
1113 buf[13] = sense.ascq;
1114 return MIN(len, 18);
1115 } else {
1116 /* Return descriptor format sense buffer */
1117 buf[0] = 0x72;
1118 buf[1] = sense.key;
1119 buf[2] = sense.asc;
1120 buf[3] = sense.ascq;
1121 return 8;
1125 static const char *scsi_command_name(uint8_t cmd)
1127 static const char *names[] = {
1128 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
1129 [ REWIND ] = "REWIND",
1130 [ REQUEST_SENSE ] = "REQUEST_SENSE",
1131 [ FORMAT_UNIT ] = "FORMAT_UNIT",
1132 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
1133 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
1134 [ READ_6 ] = "READ_6",
1135 [ WRITE_6 ] = "WRITE_6",
1136 [ SET_CAPACITY ] = "SET_CAPACITY",
1137 [ READ_REVERSE ] = "READ_REVERSE",
1138 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
1139 [ SPACE ] = "SPACE",
1140 [ INQUIRY ] = "INQUIRY",
1141 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
1142 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
1143 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
1144 [ MODE_SELECT ] = "MODE_SELECT",
1145 [ RESERVE ] = "RESERVE",
1146 [ RELEASE ] = "RELEASE",
1147 [ COPY ] = "COPY",
1148 [ ERASE ] = "ERASE",
1149 [ MODE_SENSE ] = "MODE_SENSE",
1150 [ START_STOP ] = "START_STOP",
1151 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
1152 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
1153 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
1154 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
1155 [ READ_10 ] = "READ_10",
1156 [ WRITE_10 ] = "WRITE_10",
1157 [ SEEK_10 ] = "SEEK_10",
1158 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
1159 [ VERIFY_10 ] = "VERIFY_10",
1160 [ SEARCH_HIGH ] = "SEARCH_HIGH",
1161 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
1162 [ SEARCH_LOW ] = "SEARCH_LOW",
1163 [ SET_LIMITS ] = "SET_LIMITS",
1164 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
1165 /* READ_POSITION and PRE_FETCH use the same operation code */
1166 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
1167 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
1168 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
1169 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
1170 [ COMPARE ] = "COMPARE",
1171 [ COPY_VERIFY ] = "COPY_VERIFY",
1172 [ WRITE_BUFFER ] = "WRITE_BUFFER",
1173 [ READ_BUFFER ] = "READ_BUFFER",
1174 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
1175 [ READ_LONG_10 ] = "READ_LONG_10",
1176 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
1177 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
1178 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
1179 [ UNMAP ] = "UNMAP",
1180 [ READ_TOC ] = "READ_TOC",
1181 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
1182 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
1183 [ LOG_SELECT ] = "LOG_SELECT",
1184 [ LOG_SENSE ] = "LOG_SENSE",
1185 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
1186 [ RESERVE_10 ] = "RESERVE_10",
1187 [ RELEASE_10 ] = "RELEASE_10",
1188 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
1189 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
1190 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
1191 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
1192 [ EXTENDED_COPY ] = "EXTENDED_COPY",
1193 [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
1194 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
1195 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
1196 [ READ_16 ] = "READ_16",
1197 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
1198 [ WRITE_16 ] = "WRITE_16",
1199 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
1200 [ VERIFY_16 ] = "VERIFY_16",
1201 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
1202 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1203 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1204 [ LOCATE_16 ] = "LOCATE_16",
1205 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
1206 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1207 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
1208 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
1209 [ REPORT_LUNS ] = "REPORT_LUNS",
1210 [ BLANK ] = "BLANK",
1211 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
1212 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
1213 [ READ_12 ] = "READ_12",
1214 [ WRITE_12 ] = "WRITE_12",
1215 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
1216 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1217 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
1218 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
1219 [ VERIFY_12 ] = "VERIFY_12",
1220 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
1221 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
1222 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
1223 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
1224 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
1225 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1226 [ READ_CD ] = "READ_CD",
1227 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
1228 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
1229 [ RESERVE_TRACK ] = "RESERVE_TRACK",
1230 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
1231 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
1232 [ SET_CD_SPEED ] = "SET_CD_SPEED",
1233 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
1234 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
1235 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
1238 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1239 return "*UNKNOWN*";
1240 return names[cmd];
1243 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1245 req->refcount++;
1246 return req;
1249 void scsi_req_unref(SCSIRequest *req)
1251 if (--req->refcount == 0) {
1252 if (req->ops->free_req) {
1253 req->ops->free_req(req);
1255 g_free(req);
1259 /* Tell the device that we finished processing this chunk of I/O. It
1260 will start the next chunk or complete the command. */
1261 void scsi_req_continue(SCSIRequest *req)
1263 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1264 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1265 req->ops->write_data(req);
1266 } else {
1267 req->ops->read_data(req);
1271 /* Called by the devices when data is ready for the HBA. The HBA should
1272 start a DMA operation to read or fill the device's data buffer.
1273 Once it completes, calling scsi_req_continue will restart I/O. */
1274 void scsi_req_data(SCSIRequest *req, int len)
1276 if (req->io_canceled) {
1277 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1278 } else {
1279 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1280 req->bus->info->transfer_data(req, len);
1284 void scsi_req_print(SCSIRequest *req)
1286 FILE *fp = stderr;
1287 int i;
1289 fprintf(fp, "[%s id=%d] %s",
1290 req->dev->qdev.parent_bus->name,
1291 req->dev->id,
1292 scsi_command_name(req->cmd.buf[0]));
1293 for (i = 1; i < req->cmd.len; i++) {
1294 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1296 switch (req->cmd.mode) {
1297 case SCSI_XFER_NONE:
1298 fprintf(fp, " - none\n");
1299 break;
1300 case SCSI_XFER_FROM_DEV:
1301 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1302 break;
1303 case SCSI_XFER_TO_DEV:
1304 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1305 break;
1306 default:
1307 fprintf(fp, " - Oops\n");
1308 break;
1312 void scsi_req_complete(SCSIRequest *req, int status)
1314 assert(req->status == -1);
1315 req->status = status;
1317 assert(req->sense_len < sizeof(req->sense));
1318 if (status == GOOD) {
1319 req->sense_len = 0;
1322 if (req->sense_len) {
1323 memcpy(req->dev->sense, req->sense, req->sense_len);
1324 req->dev->sense_len = req->sense_len;
1325 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1326 } else {
1327 req->dev->sense_len = 0;
1328 req->dev->sense_is_ua = false;
1332 * Unit attention state is now stored in the device's sense buffer
1333 * if the HBA didn't do autosense. Clear the pending unit attention
1334 * flags.
1336 scsi_clear_unit_attention(req);
1338 scsi_req_ref(req);
1339 scsi_req_dequeue(req);
1340 req->bus->info->complete(req, req->status);
1341 scsi_req_unref(req);
1344 void scsi_req_cancel(SCSIRequest *req)
1346 if (!req->enqueued) {
1347 return;
1349 scsi_req_ref(req);
1350 scsi_req_dequeue(req);
1351 req->io_canceled = true;
1352 if (req->ops->cancel_io) {
1353 req->ops->cancel_io(req);
1355 if (req->bus->info->cancel) {
1356 req->bus->info->cancel(req);
1358 scsi_req_unref(req);
1361 void scsi_req_abort(SCSIRequest *req, int status)
1363 if (!req->enqueued) {
1364 return;
1366 scsi_req_ref(req);
1367 scsi_req_dequeue(req);
1368 req->io_canceled = true;
1369 if (req->ops->cancel_io) {
1370 req->ops->cancel_io(req);
1372 scsi_req_complete(req, status);
1373 scsi_req_unref(req);
1376 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1378 SCSIRequest *req;
1380 while (!QTAILQ_EMPTY(&sdev->requests)) {
1381 req = QTAILQ_FIRST(&sdev->requests);
1382 scsi_req_cancel(req);
1384 sdev->unit_attention = sense;
1387 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1389 SCSIDevice *d = SCSI_DEVICE(dev);
1390 char path[100];
1392 snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel,
1393 qdev_fw_name(dev), d->id, d->lun);
1395 return strdup(path);
1398 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1400 DeviceState *qdev;
1401 SCSIDevice *target_dev = NULL;
1403 QTAILQ_FOREACH_REVERSE(qdev, &bus->qbus.children, ChildrenHead, sibling) {
1404 SCSIDevice *dev = SCSI_DEVICE(qdev);
1406 if (dev->channel == channel && dev->id == id) {
1407 if (dev->lun == lun) {
1408 return dev;
1410 target_dev = dev;
1413 return target_dev;
1416 static void scsi_device_class_init(ObjectClass *klass, void *data)
1418 DeviceClass *k = DEVICE_CLASS(klass);
1419 k->bus_info = &scsi_bus_info;
1420 k->init = scsi_qdev_init;
1421 k->unplug = qdev_simple_unplug_cb;
1422 k->exit = scsi_qdev_exit;
1425 static TypeInfo scsi_device_type_info = {
1426 .name = TYPE_SCSI_DEVICE,
1427 .parent = TYPE_DEVICE,
1428 .instance_size = sizeof(SCSIDevice),
1429 .abstract = true,
1430 .class_size = sizeof(SCSIDeviceClass),
1431 .class_init = scsi_device_class_init,
1434 static void scsi_register_devices(void)
1436 type_register_static(&scsi_device_type_info);
1439 device_init(scsi_register_devices);