MAINTAINERS: Add PCI-PCI bridge to New World Mac machine
[qemu/opensuse.git] / hw / scsi-bus.c
blob64e709ee9fed70be92c60d916d6c364adaf48894
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 /* Create a scsi bus, and attach devices to it. */
27 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info)
29 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
30 bus->busnr = next_scsi_bus++;
31 bus->info = info;
32 bus->qbus.allow_hotplug = 1;
35 static void scsi_dma_restart_bh(void *opaque)
37 SCSIDevice *s = opaque;
38 SCSIRequest *req, *next;
40 qemu_bh_delete(s->bh);
41 s->bh = NULL;
43 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
44 scsi_req_ref(req);
45 if (req->retry) {
46 req->retry = false;
47 switch (req->cmd.mode) {
48 case SCSI_XFER_FROM_DEV:
49 case SCSI_XFER_TO_DEV:
50 scsi_req_continue(req);
51 break;
52 case SCSI_XFER_NONE:
53 scsi_req_dequeue(req);
54 scsi_req_enqueue(req);
55 break;
58 scsi_req_unref(req);
62 void scsi_req_retry(SCSIRequest *req)
64 /* No need to save a reference, because scsi_dma_restart_bh just
65 * looks at the request list. */
66 req->retry = true;
69 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
71 SCSIDevice *s = opaque;
73 if (!running) {
74 return;
76 if (!s->bh) {
77 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
78 qemu_bh_schedule(s->bh);
82 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
84 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
85 SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
86 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
87 SCSIDevice *d;
88 int rc = -1;
90 if (dev->channel > bus->info->max_channel) {
91 error_report("bad scsi channel id: %d", dev->channel);
92 goto err;
94 if (dev->id != -1 && dev->id > bus->info->max_target) {
95 error_report("bad scsi device id: %d", dev->id);
96 goto err;
99 if (dev->id == -1) {
100 int id = -1;
101 if (dev->lun == -1) {
102 dev->lun = 0;
104 do {
105 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
106 } while (d && d->lun == dev->lun && id <= bus->info->max_target);
107 if (id > bus->info->max_target) {
108 error_report("no free target");
109 goto err;
111 dev->id = id;
112 } else if (dev->lun == -1) {
113 int lun = -1;
114 do {
115 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
116 } while (d && d->lun == lun && lun < bus->info->max_lun);
117 if (lun > bus->info->max_lun) {
118 error_report("no free lun");
119 goto err;
121 dev->lun = lun;
122 } else {
123 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
124 if (dev->lun == d->lun && dev != d) {
125 qdev_free(&d->qdev);
129 dev->info = info;
130 QTAILQ_INIT(&dev->requests);
131 rc = dev->info->init(dev);
132 if (rc == 0) {
133 dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
134 dev);
137 err:
138 return rc;
141 static int scsi_qdev_exit(DeviceState *qdev)
143 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
145 if (dev->vmsentry) {
146 qemu_del_vm_change_state_handler(dev->vmsentry);
148 if (dev->info->destroy) {
149 dev->info->destroy(dev);
151 return 0;
154 void scsi_qdev_register(SCSIDeviceInfo *info)
156 info->qdev.bus_info = &scsi_bus_info;
157 info->qdev.init = scsi_qdev_init;
158 info->qdev.unplug = qdev_simple_unplug_cb;
159 info->qdev.exit = scsi_qdev_exit;
160 qdev_register(&info->qdev);
163 /* handle legacy '-drive if=scsi,...' cmd line args */
164 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
165 int unit, bool removable, int bootindex)
167 const char *driver;
168 DeviceState *dev;
170 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
171 dev = qdev_create(&bus->qbus, driver);
172 qdev_prop_set_uint32(dev, "scsi-id", unit);
173 if (bootindex >= 0) {
174 qdev_prop_set_int32(dev, "bootindex", bootindex);
176 if (qdev_prop_exists(dev, "removable")) {
177 qdev_prop_set_bit(dev, "removable", removable);
179 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
180 qdev_free(dev);
181 return NULL;
183 if (qdev_init(dev) < 0)
184 return NULL;
185 return DO_UPCAST(SCSIDevice, qdev, dev);
188 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
190 Location loc;
191 DriveInfo *dinfo;
192 int res = 0, unit;
194 loc_push_none(&loc);
195 for (unit = 0; unit < bus->info->max_target; unit++) {
196 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
197 if (dinfo == NULL) {
198 continue;
200 qemu_opts_loc_restore(dinfo->opts);
201 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) {
202 res = -1;
203 break;
206 loc_pop(&loc);
207 return res;
210 /* SCSIReqOps implementation for invalid commands. */
212 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
214 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
215 scsi_req_complete(req, CHECK_CONDITION);
216 return 0;
219 static const struct SCSIReqOps reqops_invalid_opcode = {
220 .size = sizeof(SCSIRequest),
221 .send_command = scsi_invalid_command
224 /* SCSIReqOps implementation for unit attention conditions. */
226 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
228 if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
229 scsi_req_build_sense(req, req->dev->unit_attention);
230 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
231 scsi_req_build_sense(req, req->bus->unit_attention);
233 scsi_req_complete(req, CHECK_CONDITION);
234 return 0;
237 static const struct SCSIReqOps reqops_unit_attention = {
238 .size = sizeof(SCSIRequest),
239 .send_command = scsi_unit_attention
242 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
243 an invalid LUN. */
245 typedef struct SCSITargetReq SCSITargetReq;
247 struct SCSITargetReq {
248 SCSIRequest req;
249 int len;
250 uint8_t buf[2056];
253 static void store_lun(uint8_t *outbuf, int lun)
255 if (lun < 256) {
256 outbuf[1] = lun;
257 return;
259 outbuf[1] = (lun & 255);
260 outbuf[0] = (lun >> 8) | 0x40;
263 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
265 DeviceState *qdev;
266 int i, len, n;
267 int channel, id;
268 bool found_lun0;
270 if (r->req.cmd.xfer < 16) {
271 return false;
273 if (r->req.cmd.buf[2] > 2) {
274 return false;
276 channel = r->req.dev->channel;
277 id = r->req.dev->id;
278 found_lun0 = false;
279 n = 0;
280 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
281 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
283 if (dev->channel == channel && dev->id == id) {
284 if (dev->lun == 0) {
285 found_lun0 = true;
287 n += 8;
290 if (!found_lun0) {
291 n += 8;
293 len = MIN(n + 8, r->req.cmd.xfer & ~7);
294 if (len > sizeof(r->buf)) {
295 /* TODO: > 256 LUNs? */
296 return false;
299 memset(r->buf, 0, len);
300 stl_be_p(&r->buf, n);
301 i = found_lun0 ? 8 : 16;
302 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
303 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
305 if (dev->channel == channel && dev->id == id) {
306 store_lun(&r->buf[i], dev->lun);
307 i += 8;
310 assert(i == n + 8);
311 r->len = len;
312 return true;
315 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
317 assert(r->req.dev->lun != r->req.lun);
318 if (r->req.cmd.buf[1] & 0x2) {
319 /* Command support data - optional, not implemented */
320 return false;
323 if (r->req.cmd.buf[1] & 0x1) {
324 /* Vital product data */
325 uint8_t page_code = r->req.cmd.buf[2];
326 if (r->req.cmd.xfer < 4) {
327 return false;
330 r->buf[r->len++] = page_code ; /* this page */
331 r->buf[r->len++] = 0x00;
333 switch (page_code) {
334 case 0x00: /* Supported page codes, mandatory */
336 int pages;
337 pages = r->len++;
338 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
339 r->buf[pages] = r->len - pages - 1; /* number of pages */
340 break;
342 default:
343 return false;
345 /* done with EVPD */
346 assert(r->len < sizeof(r->buf));
347 r->len = MIN(r->req.cmd.xfer, r->len);
348 return true;
351 /* Standard INQUIRY data */
352 if (r->req.cmd.buf[2] != 0) {
353 return false;
356 /* PAGE CODE == 0 */
357 if (r->req.cmd.xfer < 5) {
358 return -1;
361 r->len = MIN(r->req.cmd.xfer, 36);
362 memset(r->buf, 0, r->len);
363 if (r->req.lun != 0) {
364 r->buf[0] = TYPE_NO_LUN;
365 } else {
366 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
367 r->buf[2] = 5; /* Version */
368 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
369 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
370 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
371 memcpy(&r->buf[8], "QEMU ", 8);
372 memcpy(&r->buf[16], "QEMU TARGET ", 16);
373 strncpy((char *) &r->buf[32], QEMU_VERSION, 4);
375 return true;
378 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
380 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
382 switch (buf[0]) {
383 case REPORT_LUNS:
384 if (!scsi_target_emulate_report_luns(r)) {
385 goto illegal_request;
387 break;
388 case INQUIRY:
389 if (!scsi_target_emulate_inquiry(r)) {
390 goto illegal_request;
392 break;
393 case REQUEST_SENSE:
394 if (req->cmd.xfer < 4) {
395 goto illegal_request;
397 r->len = scsi_device_get_sense(r->req.dev, r->buf,
398 MIN(req->cmd.xfer, sizeof r->buf),
399 (req->cmd.buf[1] & 1) == 0);
400 if (r->req.dev->sense_is_ua) {
401 if (r->req.dev->info->unit_attention_reported) {
402 r->req.dev->info->unit_attention_reported(req->dev);
404 r->req.dev->sense_len = 0;
405 r->req.dev->sense_is_ua = false;
407 break;
408 default:
409 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
410 scsi_req_complete(req, CHECK_CONDITION);
411 return 0;
412 illegal_request:
413 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
414 scsi_req_complete(req, CHECK_CONDITION);
415 return 0;
418 if (!r->len) {
419 scsi_req_complete(req, GOOD);
421 return r->len;
424 static void scsi_target_read_data(SCSIRequest *req)
426 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
427 uint32_t n;
429 n = r->len;
430 if (n > 0) {
431 r->len = 0;
432 scsi_req_data(&r->req, n);
433 } else {
434 scsi_req_complete(&r->req, GOOD);
438 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
440 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
442 return r->buf;
445 static const struct SCSIReqOps reqops_target_command = {
446 .size = sizeof(SCSITargetReq),
447 .send_command = scsi_target_send_command,
448 .read_data = scsi_target_read_data,
449 .get_buf = scsi_target_get_buf,
453 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
454 uint32_t tag, uint32_t lun, void *hba_private)
456 SCSIRequest *req;
458 req = g_malloc0(reqops->size);
459 req->refcount = 1;
460 req->bus = scsi_bus_from_device(d);
461 req->dev = d;
462 req->tag = tag;
463 req->lun = lun;
464 req->hba_private = hba_private;
465 req->status = -1;
466 req->sense_len = 0;
467 req->ops = reqops;
468 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
469 return req;
472 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
473 uint8_t *buf, void *hba_private)
475 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
476 SCSIRequest *req;
477 SCSICommand cmd;
479 if (scsi_req_parse(&cmd, d, buf) != 0) {
480 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
481 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
482 } else {
483 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
484 cmd.mode, cmd.xfer);
485 if (cmd.lba != -1) {
486 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
487 cmd.lba);
490 if ((d->unit_attention.key == UNIT_ATTENTION ||
491 bus->unit_attention.key == UNIT_ATTENTION) &&
492 (buf[0] != INQUIRY &&
493 buf[0] != REPORT_LUNS &&
494 buf[0] != GET_CONFIGURATION &&
495 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
498 * If we already have a pending unit attention condition,
499 * report this one before triggering another one.
501 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
502 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
503 hba_private);
504 } else if (lun != d->lun ||
505 buf[0] == REPORT_LUNS ||
506 (buf[0] == REQUEST_SENSE && (d->sense_len || cmd.xfer < 4))) {
507 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
508 hba_private);
509 } else {
510 req = d->info->alloc_req(d, tag, lun, buf, hba_private);
514 req->cmd = cmd;
515 switch (buf[0]) {
516 case INQUIRY:
517 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
518 break;
519 case TEST_UNIT_READY:
520 trace_scsi_test_unit_ready(d->id, lun, tag);
521 break;
522 case REPORT_LUNS:
523 trace_scsi_report_luns(d->id, lun, tag);
524 break;
525 case REQUEST_SENSE:
526 trace_scsi_request_sense(d->id, lun, tag);
527 break;
528 default:
529 break;
532 return req;
535 uint8_t *scsi_req_get_buf(SCSIRequest *req)
537 return req->ops->get_buf(req);
540 static void scsi_clear_unit_attention(SCSIRequest *req)
542 SCSISense *ua;
543 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
544 req->bus->unit_attention.key != UNIT_ATTENTION) {
545 return;
549 * If an INQUIRY command enters the enabled command state,
550 * the device server shall [not] clear any unit attention condition;
551 * See also MMC-6, paragraphs 6.5 and 6.6.2.
553 if (req->cmd.buf[0] == INQUIRY ||
554 req->cmd.buf[0] == GET_CONFIGURATION ||
555 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
556 return;
559 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
560 ua = &req->dev->unit_attention;
561 } else {
562 ua = &req->bus->unit_attention;
566 * If a REPORT LUNS command enters the enabled command state, [...]
567 * the device server shall clear any pending unit attention condition
568 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
570 if (req->cmd.buf[0] == REPORT_LUNS &&
571 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
572 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
573 return;
576 *ua = SENSE_CODE(NO_SENSE);
579 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
581 int ret;
583 assert(len >= 14);
584 if (!req->sense_len) {
585 return 0;
588 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
591 * FIXME: clearing unit attention conditions upon autosense should be done
592 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
593 * (SAM-5, 5.14).
595 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
596 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
597 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
599 if (req->dev->sense_is_ua) {
600 if (req->dev->info->unit_attention_reported) {
601 req->dev->info->unit_attention_reported(req->dev);
603 req->dev->sense_len = 0;
604 req->dev->sense_is_ua = false;
606 return ret;
609 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
611 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
614 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
616 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
617 sense.key, sense.asc, sense.ascq);
618 memset(req->sense, 0, 18);
619 req->sense[0] = 0xf0;
620 req->sense[2] = sense.key;
621 req->sense[7] = 10;
622 req->sense[12] = sense.asc;
623 req->sense[13] = sense.ascq;
624 req->sense_len = 18;
627 int32_t scsi_req_enqueue(SCSIRequest *req)
629 int32_t rc;
631 assert(!req->enqueued);
632 scsi_req_ref(req);
633 req->enqueued = true;
634 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
636 scsi_req_ref(req);
637 rc = req->ops->send_command(req, req->cmd.buf);
638 scsi_req_unref(req);
639 return rc;
642 static void scsi_req_dequeue(SCSIRequest *req)
644 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
645 req->retry = false;
646 if (req->enqueued) {
647 QTAILQ_REMOVE(&req->dev->requests, req, next);
648 req->enqueued = false;
649 scsi_req_unref(req);
653 static int scsi_get_performance_length(int num_desc, int type, int data_type)
655 /* MMC-6, paragraph 6.7. */
656 switch (type) {
657 case 0:
658 if ((data_type & 3) == 0) {
659 /* Each descriptor is as in Table 295 - Nominal performance. */
660 return 16 * num_desc + 8;
661 } else {
662 /* Each descriptor is as in Table 296 - Exceptions. */
663 return 6 * num_desc + 8;
665 case 1:
666 case 4:
667 case 5:
668 return 8 * num_desc + 8;
669 case 2:
670 return 2048 * num_desc + 8;
671 case 3:
672 return 16 * num_desc + 8;
673 default:
674 return 8;
678 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
680 switch (buf[0] >> 5) {
681 case 0:
682 cmd->xfer = buf[4];
683 cmd->len = 6;
684 /* length 0 means 256 blocks */
685 if (cmd->xfer == 0) {
686 cmd->xfer = 256;
688 break;
689 case 1:
690 case 2:
691 cmd->xfer = lduw_be_p(&buf[7]);
692 cmd->len = 10;
693 break;
694 case 4:
695 cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL;
696 cmd->len = 16;
697 break;
698 case 5:
699 cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL;
700 cmd->len = 12;
701 break;
702 default:
703 return -1;
706 switch (buf[0]) {
707 case TEST_UNIT_READY:
708 case REWIND:
709 case START_STOP:
710 case SET_CAPACITY:
711 case WRITE_FILEMARKS:
712 case WRITE_FILEMARKS_16:
713 case SPACE:
714 case RESERVE:
715 case RELEASE:
716 case ERASE:
717 case ALLOW_MEDIUM_REMOVAL:
718 case VERIFY_10:
719 case SEEK_10:
720 case SYNCHRONIZE_CACHE:
721 case SYNCHRONIZE_CACHE_16:
722 case LOCATE_16:
723 case LOCK_UNLOCK_CACHE:
724 case LOAD_UNLOAD:
725 case SET_CD_SPEED:
726 case SET_LIMITS:
727 case WRITE_LONG_10:
728 case MOVE_MEDIUM:
729 case UPDATE_BLOCK:
730 case RESERVE_TRACK:
731 case SET_READ_AHEAD:
732 case PRE_FETCH:
733 case PRE_FETCH_16:
734 case ALLOW_OVERWRITE:
735 cmd->xfer = 0;
736 break;
737 case MODE_SENSE:
738 break;
739 case WRITE_SAME_10:
740 cmd->xfer = 1;
741 break;
742 case READ_CAPACITY_10:
743 cmd->xfer = 8;
744 break;
745 case READ_BLOCK_LIMITS:
746 cmd->xfer = 6;
747 break;
748 case SEND_VOLUME_TAG:
749 /* GPCMD_SET_STREAMING from multimedia commands. */
750 if (dev->type == TYPE_ROM) {
751 cmd->xfer = buf[10] | (buf[9] << 8);
752 } else {
753 cmd->xfer = buf[9] | (buf[8] << 8);
755 break;
756 case WRITE_10:
757 case WRITE_VERIFY_10:
758 case WRITE_6:
759 case WRITE_12:
760 case WRITE_VERIFY_12:
761 case WRITE_16:
762 case WRITE_VERIFY_16:
763 cmd->xfer *= dev->blocksize;
764 break;
765 case READ_10:
766 case READ_6:
767 case READ_REVERSE:
768 case RECOVER_BUFFERED_DATA:
769 case READ_12:
770 case READ_16:
771 cmd->xfer *= dev->blocksize;
772 break;
773 case FORMAT_UNIT:
774 /* MMC mandates the parameter list to be 12-bytes long. Parameters
775 * for block devices are restricted to the header right now. */
776 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
777 cmd->xfer = 12;
778 } else {
779 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
781 break;
782 case INQUIRY:
783 case RECEIVE_DIAGNOSTIC:
784 case SEND_DIAGNOSTIC:
785 cmd->xfer = buf[4] | (buf[3] << 8);
786 break;
787 case READ_CD:
788 case READ_BUFFER:
789 case WRITE_BUFFER:
790 case SEND_CUE_SHEET:
791 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
792 break;
793 case PERSISTENT_RESERVE_OUT:
794 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
795 break;
796 case ERASE_12:
797 if (dev->type == TYPE_ROM) {
798 /* MMC command GET PERFORMANCE. */
799 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
800 buf[10], buf[1] & 0x1f);
802 break;
803 case MECHANISM_STATUS:
804 case READ_DVD_STRUCTURE:
805 case SEND_DVD_STRUCTURE:
806 case MAINTENANCE_OUT:
807 case MAINTENANCE_IN:
808 if (dev->type == TYPE_ROM) {
809 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
810 cmd->xfer = buf[9] | (buf[8] << 8);
812 break;
814 return 0;
817 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
819 switch (buf[0]) {
820 /* stream commands */
821 case ERASE_12:
822 case ERASE_16:
823 cmd->xfer = 0;
824 break;
825 case READ_6:
826 case READ_REVERSE:
827 case RECOVER_BUFFERED_DATA:
828 case WRITE_6:
829 cmd->len = 6;
830 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
831 if (buf[1] & 0x01) { /* fixed */
832 cmd->xfer *= dev->blocksize;
834 break;
835 case REWIND:
836 case START_STOP:
837 cmd->len = 6;
838 cmd->xfer = 0;
839 break;
840 case SPACE_16:
841 cmd->xfer = buf[13] | (buf[12] << 8);
842 break;
843 case READ_POSITION:
844 cmd->xfer = buf[8] | (buf[7] << 8);
845 break;
846 case FORMAT_UNIT:
847 cmd->xfer = buf[4] | (buf[3] << 8);
848 break;
849 /* generic commands */
850 default:
851 return scsi_req_length(cmd, dev, buf);
853 return 0;
856 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
858 switch (cmd->buf[0]) {
859 case WRITE_6:
860 case WRITE_10:
861 case WRITE_VERIFY_10:
862 case WRITE_12:
863 case WRITE_VERIFY_12:
864 case WRITE_16:
865 case WRITE_VERIFY_16:
866 case COPY:
867 case COPY_VERIFY:
868 case COMPARE:
869 case CHANGE_DEFINITION:
870 case LOG_SELECT:
871 case MODE_SELECT:
872 case MODE_SELECT_10:
873 case SEND_DIAGNOSTIC:
874 case WRITE_BUFFER:
875 case FORMAT_UNIT:
876 case REASSIGN_BLOCKS:
877 case SEARCH_EQUAL:
878 case SEARCH_HIGH:
879 case SEARCH_LOW:
880 case UPDATE_BLOCK:
881 case WRITE_LONG_10:
882 case WRITE_SAME_10:
883 case SEARCH_HIGH_12:
884 case SEARCH_EQUAL_12:
885 case SEARCH_LOW_12:
886 case MEDIUM_SCAN:
887 case SEND_VOLUME_TAG:
888 case SEND_CUE_SHEET:
889 case SEND_DVD_STRUCTURE:
890 case PERSISTENT_RESERVE_OUT:
891 case MAINTENANCE_OUT:
892 cmd->mode = SCSI_XFER_TO_DEV;
893 break;
894 default:
895 if (cmd->xfer)
896 cmd->mode = SCSI_XFER_FROM_DEV;
897 else {
898 cmd->mode = SCSI_XFER_NONE;
900 break;
904 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
906 uint8_t *buf = cmd->buf;
907 uint64_t lba;
909 switch (buf[0] >> 5) {
910 case 0:
911 lba = ldl_be_p(&buf[0]) & 0x1fffff;
912 break;
913 case 1:
914 case 2:
915 case 5:
916 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
917 break;
918 case 4:
919 lba = ldq_be_p(&buf[2]);
920 break;
921 default:
922 lba = -1;
925 return lba;
928 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
930 int rc;
932 if (dev->type == TYPE_TAPE) {
933 rc = scsi_req_stream_length(cmd, dev, buf);
934 } else {
935 rc = scsi_req_length(cmd, dev, buf);
937 if (rc != 0)
938 return rc;
940 memcpy(cmd->buf, buf, cmd->len);
941 scsi_cmd_xfer_mode(cmd);
942 cmd->lba = scsi_cmd_lba(cmd);
943 return 0;
947 * Predefined sense codes
950 /* No sense data available */
951 const struct SCSISense sense_code_NO_SENSE = {
952 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
955 /* LUN not ready, Manual intervention required */
956 const struct SCSISense sense_code_LUN_NOT_READY = {
957 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
960 /* LUN not ready, Medium not present */
961 const struct SCSISense sense_code_NO_MEDIUM = {
962 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
965 /* LUN not ready, medium removal prevented */
966 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
967 .key = NOT_READY, .asc = 0x53, .ascq = 0x00
970 /* Hardware error, internal target failure */
971 const struct SCSISense sense_code_TARGET_FAILURE = {
972 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
975 /* Illegal request, invalid command operation code */
976 const struct SCSISense sense_code_INVALID_OPCODE = {
977 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
980 /* Illegal request, LBA out of range */
981 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
982 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
985 /* Illegal request, Invalid field in CDB */
986 const struct SCSISense sense_code_INVALID_FIELD = {
987 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
990 /* Illegal request, LUN not supported */
991 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
992 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
995 /* Illegal request, Saving parameters not supported */
996 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
997 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1000 /* Illegal request, Incompatible medium installed */
1001 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1002 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1005 /* Illegal request, medium removal prevented */
1006 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1007 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00
1010 /* Command aborted, I/O process terminated */
1011 const struct SCSISense sense_code_IO_ERROR = {
1012 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1015 /* Command aborted, I_T Nexus loss occurred */
1016 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1017 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1020 /* Command aborted, Logical Unit failure */
1021 const struct SCSISense sense_code_LUN_FAILURE = {
1022 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1025 /* Unit attention, Power on, reset or bus device reset occurred */
1026 const struct SCSISense sense_code_RESET = {
1027 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1030 /* Unit attention, No medium */
1031 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1032 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1035 /* Unit attention, Medium may have changed */
1036 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1037 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1040 /* Unit attention, Reported LUNs data has changed */
1041 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1042 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1045 /* Unit attention, Device internal reset */
1046 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1047 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1051 * scsi_build_sense
1053 * Convert between fixed and descriptor sense buffers
1055 int scsi_build_sense(uint8_t *in_buf, int in_len,
1056 uint8_t *buf, int len, bool fixed)
1058 bool fixed_in;
1059 SCSISense sense;
1060 if (!fixed && len < 8) {
1061 return 0;
1064 if (in_len == 0) {
1065 sense.key = NO_SENSE;
1066 sense.asc = 0;
1067 sense.ascq = 0;
1068 } else {
1069 fixed_in = (in_buf[0] & 2) == 0;
1071 if (fixed == fixed_in) {
1072 memcpy(buf, in_buf, MIN(len, in_len));
1073 return MIN(len, in_len);
1076 if (fixed_in) {
1077 sense.key = in_buf[2];
1078 sense.asc = in_buf[12];
1079 sense.ascq = in_buf[13];
1080 } else {
1081 sense.key = in_buf[1];
1082 sense.asc = in_buf[2];
1083 sense.ascq = in_buf[3];
1087 memset(buf, 0, len);
1088 if (fixed) {
1089 /* Return fixed format sense buffer */
1090 buf[0] = 0xf0;
1091 buf[2] = sense.key;
1092 buf[7] = 10;
1093 buf[12] = sense.asc;
1094 buf[13] = sense.ascq;
1095 return MIN(len, 18);
1096 } else {
1097 /* Return descriptor format sense buffer */
1098 buf[0] = 0x72;
1099 buf[1] = sense.key;
1100 buf[2] = sense.asc;
1101 buf[3] = sense.ascq;
1102 return 8;
1106 static const char *scsi_command_name(uint8_t cmd)
1108 static const char *names[] = {
1109 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
1110 [ REWIND ] = "REWIND",
1111 [ REQUEST_SENSE ] = "REQUEST_SENSE",
1112 [ FORMAT_UNIT ] = "FORMAT_UNIT",
1113 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
1114 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
1115 [ READ_6 ] = "READ_6",
1116 [ WRITE_6 ] = "WRITE_6",
1117 [ SET_CAPACITY ] = "SET_CAPACITY",
1118 [ READ_REVERSE ] = "READ_REVERSE",
1119 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
1120 [ SPACE ] = "SPACE",
1121 [ INQUIRY ] = "INQUIRY",
1122 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
1123 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
1124 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
1125 [ MODE_SELECT ] = "MODE_SELECT",
1126 [ RESERVE ] = "RESERVE",
1127 [ RELEASE ] = "RELEASE",
1128 [ COPY ] = "COPY",
1129 [ ERASE ] = "ERASE",
1130 [ MODE_SENSE ] = "MODE_SENSE",
1131 [ START_STOP ] = "START_STOP",
1132 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
1133 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
1134 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
1135 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
1136 [ READ_10 ] = "READ_10",
1137 [ WRITE_10 ] = "WRITE_10",
1138 [ SEEK_10 ] = "SEEK_10",
1139 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
1140 [ VERIFY_10 ] = "VERIFY_10",
1141 [ SEARCH_HIGH ] = "SEARCH_HIGH",
1142 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
1143 [ SEARCH_LOW ] = "SEARCH_LOW",
1144 [ SET_LIMITS ] = "SET_LIMITS",
1145 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
1146 /* READ_POSITION and PRE_FETCH use the same operation code */
1147 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
1148 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
1149 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
1150 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
1151 [ COMPARE ] = "COMPARE",
1152 [ COPY_VERIFY ] = "COPY_VERIFY",
1153 [ WRITE_BUFFER ] = "WRITE_BUFFER",
1154 [ READ_BUFFER ] = "READ_BUFFER",
1155 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
1156 [ READ_LONG_10 ] = "READ_LONG_10",
1157 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
1158 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
1159 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
1160 [ UNMAP ] = "UNMAP",
1161 [ READ_TOC ] = "READ_TOC",
1162 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
1163 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
1164 [ LOG_SELECT ] = "LOG_SELECT",
1165 [ LOG_SENSE ] = "LOG_SENSE",
1166 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
1167 [ RESERVE_10 ] = "RESERVE_10",
1168 [ RELEASE_10 ] = "RELEASE_10",
1169 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
1170 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
1171 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
1172 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
1173 [ EXTENDED_COPY ] = "EXTENDED_COPY",
1174 [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
1175 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
1176 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
1177 [ READ_16 ] = "READ_16",
1178 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
1179 [ WRITE_16 ] = "WRITE_16",
1180 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
1181 [ VERIFY_16 ] = "VERIFY_16",
1182 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
1183 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1184 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1185 [ LOCATE_16 ] = "LOCATE_16",
1186 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
1187 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1188 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
1189 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
1190 [ REPORT_LUNS ] = "REPORT_LUNS",
1191 [ BLANK ] = "BLANK",
1192 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
1193 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
1194 [ READ_12 ] = "READ_12",
1195 [ WRITE_12 ] = "WRITE_12",
1196 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
1197 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1198 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
1199 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
1200 [ VERIFY_12 ] = "VERIFY_12",
1201 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
1202 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
1203 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
1204 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
1205 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
1206 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1207 [ READ_CD ] = "READ_CD",
1208 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
1209 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
1210 [ RESERVE_TRACK ] = "RESERVE_TRACK",
1211 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
1212 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
1213 [ SET_CD_SPEED ] = "SET_CD_SPEED",
1214 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
1215 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
1216 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
1219 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1220 return "*UNKNOWN*";
1221 return names[cmd];
1224 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1226 req->refcount++;
1227 return req;
1230 void scsi_req_unref(SCSIRequest *req)
1232 if (--req->refcount == 0) {
1233 if (req->ops->free_req) {
1234 req->ops->free_req(req);
1236 g_free(req);
1240 /* Tell the device that we finished processing this chunk of I/O. It
1241 will start the next chunk or complete the command. */
1242 void scsi_req_continue(SCSIRequest *req)
1244 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1245 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1246 req->ops->write_data(req);
1247 } else {
1248 req->ops->read_data(req);
1252 /* Called by the devices when data is ready for the HBA. The HBA should
1253 start a DMA operation to read or fill the device's data buffer.
1254 Once it completes, calling scsi_req_continue will restart I/O. */
1255 void scsi_req_data(SCSIRequest *req, int len)
1257 if (req->io_canceled) {
1258 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1259 } else {
1260 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1261 req->bus->info->transfer_data(req, len);
1265 void scsi_req_print(SCSIRequest *req)
1267 FILE *fp = stderr;
1268 int i;
1270 fprintf(fp, "[%s id=%d] %s",
1271 req->dev->qdev.parent_bus->name,
1272 req->dev->id,
1273 scsi_command_name(req->cmd.buf[0]));
1274 for (i = 1; i < req->cmd.len; i++) {
1275 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1277 switch (req->cmd.mode) {
1278 case SCSI_XFER_NONE:
1279 fprintf(fp, " - none\n");
1280 break;
1281 case SCSI_XFER_FROM_DEV:
1282 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1283 break;
1284 case SCSI_XFER_TO_DEV:
1285 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1286 break;
1287 default:
1288 fprintf(fp, " - Oops\n");
1289 break;
1293 void scsi_req_complete(SCSIRequest *req, int status)
1295 assert(req->status == -1);
1296 req->status = status;
1298 assert(req->sense_len < sizeof(req->sense));
1299 if (status == GOOD) {
1300 req->sense_len = 0;
1303 if (req->sense_len) {
1304 memcpy(req->dev->sense, req->sense, req->sense_len);
1305 req->dev->sense_len = req->sense_len;
1306 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1307 } else {
1308 req->dev->sense_len = 0;
1309 req->dev->sense_is_ua = false;
1313 * Unit attention state is now stored in the device's sense buffer
1314 * if the HBA didn't do autosense. Clear the pending unit attention
1315 * flags.
1317 scsi_clear_unit_attention(req);
1319 scsi_req_ref(req);
1320 scsi_req_dequeue(req);
1321 req->bus->info->complete(req, req->status);
1322 scsi_req_unref(req);
1325 void scsi_req_cancel(SCSIRequest *req)
1327 if (!req->enqueued) {
1328 return;
1330 scsi_req_ref(req);
1331 scsi_req_dequeue(req);
1332 req->io_canceled = true;
1333 if (req->ops->cancel_io) {
1334 req->ops->cancel_io(req);
1336 if (req->bus->info->cancel) {
1337 req->bus->info->cancel(req);
1339 scsi_req_unref(req);
1342 void scsi_req_abort(SCSIRequest *req, int status)
1344 if (!req->enqueued) {
1345 return;
1347 scsi_req_ref(req);
1348 scsi_req_dequeue(req);
1349 req->io_canceled = true;
1350 if (req->ops->cancel_io) {
1351 req->ops->cancel_io(req);
1353 scsi_req_complete(req, status);
1354 scsi_req_unref(req);
1357 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1359 SCSIRequest *req;
1361 while (!QTAILQ_EMPTY(&sdev->requests)) {
1362 req = QTAILQ_FIRST(&sdev->requests);
1363 scsi_req_cancel(req);
1365 sdev->unit_attention = sense;
1368 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1370 SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1371 char path[100];
1373 snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel,
1374 qdev_fw_name(dev), d->id, d->lun);
1376 return strdup(path);
1379 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1381 DeviceState *qdev;
1382 SCSIDevice *target_dev = NULL;
1384 QTAILQ_FOREACH_REVERSE(qdev, &bus->qbus.children, ChildrenHead, sibling) {
1385 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
1387 if (dev->channel == channel && dev->id == id) {
1388 if (dev->lun == lun) {
1389 return dev;
1391 target_dev = dev;
1394 return target_dev;