1 #include "qemu/osdep.h"
3 #include "qemu/error-report.h"
4 #include "hw/scsi/scsi.h"
5 #include "block/scsi.h"
7 #include "sysemu/block-backend.h"
8 #include "sysemu/blockdev.h"
10 #include "sysemu/dma.h"
12 static char *scsibus_get_dev_path(DeviceState
*dev
);
13 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
14 static void scsi_req_dequeue(SCSIRequest
*req
);
15 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
16 static void scsi_target_free_buf(SCSIRequest
*req
);
18 static Property scsi_props
[] = {
19 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
20 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
21 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
22 DEFINE_PROP_END_OF_LIST(),
25 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
27 BusClass
*k
= BUS_CLASS(klass
);
28 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
30 k
->get_dev_path
= scsibus_get_dev_path
;
31 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
32 hc
->unplug
= qdev_simple_device_unplug_cb
;
35 static const TypeInfo scsi_bus_info
= {
36 .name
= TYPE_SCSI_BUS
,
38 .instance_size
= sizeof(SCSIBus
),
39 .class_init
= scsi_bus_class_init
,
40 .interfaces
= (InterfaceInfo
[]) {
41 { TYPE_HOTPLUG_HANDLER
},
45 static int next_scsi_bus
;
47 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
55 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
58 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
61 assert(cmd
->len
== 0);
62 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
63 if (bus
->info
->parse_cdb
) {
64 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
69 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
70 uint8_t *buf
, void *hba_private
)
72 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
74 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
80 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
82 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
83 if (sc
->unit_attention_reported
) {
84 sc
->unit_attention_reported(s
);
88 /* Create a scsi bus, and attach devices to it. */
89 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
90 const SCSIBusInfo
*info
, const char *bus_name
)
92 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
93 bus
->busnr
= next_scsi_bus
++;
95 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
98 static void scsi_dma_restart_bh(void *opaque
)
100 SCSIDevice
*s
= opaque
;
101 SCSIRequest
*req
, *next
;
103 qemu_bh_delete(s
->bh
);
106 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
110 switch (req
->cmd
.mode
) {
111 case SCSI_XFER_FROM_DEV
:
112 case SCSI_XFER_TO_DEV
:
113 scsi_req_continue(req
);
116 scsi_req_dequeue(req
);
117 scsi_req_enqueue(req
);
125 void scsi_req_retry(SCSIRequest
*req
)
127 /* No need to save a reference, because scsi_dma_restart_bh just
128 * looks at the request list. */
132 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
134 SCSIDevice
*s
= opaque
;
140 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
141 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
142 qemu_bh_schedule(s
->bh
);
146 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
148 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
149 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
151 Error
*local_err
= NULL
;
153 if (dev
->channel
> bus
->info
->max_channel
) {
154 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
157 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
158 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
161 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
162 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
168 if (dev
->lun
== -1) {
172 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
173 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
174 if (d
&& d
->lun
== dev
->lun
) {
175 error_setg(errp
, "no free target");
179 } else if (dev
->lun
== -1) {
182 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
183 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
184 if (d
&& d
->lun
== lun
) {
185 error_setg(errp
, "no free lun");
190 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
192 if (d
->lun
== dev
->lun
&& dev
!= d
) {
193 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
198 QTAILQ_INIT(&dev
->requests
);
199 scsi_device_realize(dev
, &local_err
);
201 error_propagate(errp
, local_err
);
204 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
208 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
210 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
213 qemu_del_vm_change_state_handler(dev
->vmsentry
);
216 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
217 blockdev_mark_auto_del(dev
->conf
.blk
);
220 /* handle legacy '-drive if=scsi,...' cmd line args */
221 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
222 int unit
, bool removable
, int bootindex
,
223 const char *serial
, Error
**errp
)
230 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
231 dev
= qdev_create(&bus
->qbus
, driver
);
232 name
= g_strdup_printf("legacy[%d]", unit
);
233 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
236 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
237 if (bootindex
>= 0) {
238 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
241 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
242 qdev_prop_set_bit(dev
, "removable", removable
);
244 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
245 qdev_prop_set_string(dev
, "serial", serial
);
247 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
249 error_propagate(errp
, err
);
250 object_unparent(OBJECT(dev
));
253 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
255 error_propagate(errp
, err
);
256 object_unparent(OBJECT(dev
));
259 return SCSI_DEVICE(dev
);
262 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
270 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
271 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
275 qemu_opts_loc_restore(dinfo
->opts
);
276 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
277 unit
, false, -1, NULL
, &err
);
279 error_propagate(errp
, err
);
286 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
288 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
289 scsi_req_complete(req
, CHECK_CONDITION
);
293 static const struct SCSIReqOps reqops_invalid_field
= {
294 .size
= sizeof(SCSIRequest
),
295 .send_command
= scsi_invalid_field
298 /* SCSIReqOps implementation for invalid commands. */
300 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
302 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
303 scsi_req_complete(req
, CHECK_CONDITION
);
307 static const struct SCSIReqOps reqops_invalid_opcode
= {
308 .size
= sizeof(SCSIRequest
),
309 .send_command
= scsi_invalid_command
312 /* SCSIReqOps implementation for unit attention conditions. */
314 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
316 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
317 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
318 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
319 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
321 scsi_req_complete(req
, CHECK_CONDITION
);
325 static const struct SCSIReqOps reqops_unit_attention
= {
326 .size
= sizeof(SCSIRequest
),
327 .send_command
= scsi_unit_attention
330 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
333 typedef struct SCSITargetReq SCSITargetReq
;
335 struct SCSITargetReq
{
342 static void store_lun(uint8_t *outbuf
, int lun
)
348 outbuf
[1] = (lun
& 255);
349 outbuf
[0] = (lun
>> 8) | 0x40;
352 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
359 if (r
->req
.cmd
.xfer
< 16) {
362 if (r
->req
.cmd
.buf
[2] > 2) {
365 channel
= r
->req
.dev
->channel
;
369 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
370 DeviceState
*qdev
= kid
->child
;
371 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
373 if (dev
->channel
== channel
&& dev
->id
== id
) {
384 scsi_target_alloc_buf(&r
->req
, n
+ 8);
386 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
387 memset(r
->buf
, 0, len
);
388 stl_be_p(&r
->buf
[0], n
);
389 i
= found_lun0
? 8 : 16;
390 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
391 DeviceState
*qdev
= kid
->child
;
392 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
394 if (dev
->channel
== channel
&& dev
->id
== id
) {
395 store_lun(&r
->buf
[i
], dev
->lun
);
404 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
406 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
408 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
410 if (r
->req
.cmd
.buf
[1] & 0x2) {
411 /* Command support data - optional, not implemented */
415 if (r
->req
.cmd
.buf
[1] & 0x1) {
416 /* Vital product data */
417 uint8_t page_code
= r
->req
.cmd
.buf
[2];
418 r
->buf
[r
->len
++] = page_code
; /* this page */
419 r
->buf
[r
->len
++] = 0x00;
422 case 0x00: /* Supported page codes, mandatory */
426 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
427 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
434 assert(r
->len
< r
->buf_len
);
435 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
439 /* Standard INQUIRY data */
440 if (r
->req
.cmd
.buf
[2] != 0) {
445 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
446 memset(r
->buf
, 0, r
->len
);
447 if (r
->req
.lun
!= 0) {
448 r
->buf
[0] = TYPE_NO_LUN
;
450 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
451 r
->buf
[2] = 5; /* Version */
452 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
453 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
454 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
455 memcpy(&r
->buf
[8], "QEMU ", 8);
456 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
457 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
462 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
464 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
468 if (!scsi_target_emulate_report_luns(r
)) {
469 goto illegal_request
;
473 if (!scsi_target_emulate_inquiry(r
)) {
474 goto illegal_request
;
478 scsi_target_alloc_buf(&r
->req
, SCSI_SENSE_LEN
);
479 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
480 MIN(req
->cmd
.xfer
, r
->buf_len
),
481 (req
->cmd
.buf
[1] & 1) == 0);
482 if (r
->req
.dev
->sense_is_ua
) {
483 scsi_device_unit_attention_reported(req
->dev
);
484 r
->req
.dev
->sense_len
= 0;
485 r
->req
.dev
->sense_is_ua
= false;
488 case TEST_UNIT_READY
:
491 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
492 scsi_req_complete(req
, CHECK_CONDITION
);
495 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
496 scsi_req_complete(req
, CHECK_CONDITION
);
501 scsi_req_complete(req
, GOOD
);
506 static void scsi_target_read_data(SCSIRequest
*req
)
508 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
514 scsi_req_data(&r
->req
, n
);
516 scsi_req_complete(&r
->req
, GOOD
);
520 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
522 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
527 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
529 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
531 r
->buf
= g_malloc(len
);
537 static void scsi_target_free_buf(SCSIRequest
*req
)
539 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
544 static const struct SCSIReqOps reqops_target_command
= {
545 .size
= sizeof(SCSITargetReq
),
546 .send_command
= scsi_target_send_command
,
547 .read_data
= scsi_target_read_data
,
548 .get_buf
= scsi_target_get_buf
,
549 .free_req
= scsi_target_free_buf
,
553 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
554 uint32_t tag
, uint32_t lun
, void *hba_private
)
557 SCSIBus
*bus
= scsi_bus_from_device(d
);
558 BusState
*qbus
= BUS(bus
);
559 const int memset_off
= offsetof(SCSIRequest
, sense
)
560 + sizeof(req
->sense
);
562 req
= g_malloc(reqops
->size
);
563 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
569 req
->hba_private
= hba_private
;
572 object_ref(OBJECT(d
));
573 object_ref(OBJECT(qbus
->parent
));
574 notifier_list_init(&req
->cancel_notifiers
);
575 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
579 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
580 uint8_t *buf
, void *hba_private
)
582 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
583 const SCSIReqOps
*ops
;
584 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
586 SCSICommand cmd
= { .len
= 0 };
589 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
590 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
591 (buf
[0] != INQUIRY
&&
592 buf
[0] != REPORT_LUNS
&&
593 buf
[0] != GET_CONFIGURATION
&&
594 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
597 * If we already have a pending unit attention condition,
598 * report this one before triggering another one.
600 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
601 ops
= &reqops_unit_attention
;
602 } else if (lun
!= d
->lun
||
603 buf
[0] == REPORT_LUNS
||
604 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
605 ops
= &reqops_target_command
;
610 if (ops
!= NULL
|| !sc
->parse_cdb
) {
611 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
613 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
617 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
618 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
620 assert(cmd
.len
!= 0);
621 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
624 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
628 if (cmd
.xfer
> INT32_MAX
) {
629 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
631 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
633 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
638 req
->resid
= req
->cmd
.xfer
;
642 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
644 case TEST_UNIT_READY
:
645 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
648 trace_scsi_report_luns(d
->id
, lun
, tag
);
651 trace_scsi_request_sense(d
->id
, lun
, tag
);
660 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
662 return req
->ops
->get_buf(req
);
665 static void scsi_clear_unit_attention(SCSIRequest
*req
)
668 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
669 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
674 * If an INQUIRY command enters the enabled command state,
675 * the device server shall [not] clear any unit attention condition;
676 * See also MMC-6, paragraphs 6.5 and 6.6.2.
678 if (req
->cmd
.buf
[0] == INQUIRY
||
679 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
680 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
684 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
685 ua
= &req
->dev
->unit_attention
;
687 ua
= &req
->bus
->unit_attention
;
691 * If a REPORT LUNS command enters the enabled command state, [...]
692 * the device server shall clear any pending unit attention condition
693 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
695 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
696 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
697 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
701 *ua
= SENSE_CODE(NO_SENSE
);
704 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
709 if (!req
->sense_len
) {
713 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
716 * FIXME: clearing unit attention conditions upon autosense should be done
717 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
720 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
721 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
722 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
724 if (req
->dev
->sense_is_ua
) {
725 scsi_device_unit_attention_reported(req
->dev
);
726 req
->dev
->sense_len
= 0;
727 req
->dev
->sense_is_ua
= false;
732 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
734 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
737 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
739 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
740 sense
.key
, sense
.asc
, sense
.ascq
);
741 memset(req
->sense
, 0, 18);
742 req
->sense
[0] = 0x70;
743 req
->sense
[2] = sense
.key
;
745 req
->sense
[12] = sense
.asc
;
746 req
->sense
[13] = sense
.ascq
;
750 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
752 assert(!req
->enqueued
);
754 if (req
->bus
->info
->get_sg_list
) {
755 req
->sg
= req
->bus
->info
->get_sg_list(req
);
759 req
->enqueued
= true;
760 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
763 int32_t scsi_req_enqueue(SCSIRequest
*req
)
768 scsi_req_enqueue_internal(req
);
770 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
775 static void scsi_req_dequeue(SCSIRequest
*req
)
777 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
780 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
781 req
->enqueued
= false;
786 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
788 /* MMC-6, paragraph 6.7. */
791 if ((data_type
& 3) == 0) {
792 /* Each descriptor is as in Table 295 - Nominal performance. */
793 return 16 * num_desc
+ 8;
795 /* Each descriptor is as in Table 296 - Exceptions. */
796 return 6 * num_desc
+ 8;
801 return 8 * num_desc
+ 8;
803 return 2048 * num_desc
+ 8;
805 return 16 * num_desc
+ 8;
811 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
813 int byte_block
= (buf
[2] >> 2) & 0x1;
814 int type
= (buf
[2] >> 4) & 0x1;
819 xfer_unit
= dev
->blocksize
;
830 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
832 int length
= buf
[2] & 0x3;
834 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
838 case 3: /* USB-specific. */
853 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
855 int extend
= buf
[1] & 0x1;
856 int length
= buf
[2] & 0x3;
858 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
862 case 3: /* USB-specific. */
868 xfer
|= (extend
? buf
[3] << 8 : 0);
872 xfer
|= (extend
? buf
[5] << 8 : 0);
879 uint32_t scsi_data_cdb_xfer(uint8_t *buf
)
881 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
884 return scsi_cdb_xfer(buf
);
888 uint32_t scsi_cdb_xfer(uint8_t *buf
)
890 switch (buf
[0] >> 5) {
896 return lduw_be_p(&buf
[7]);
899 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
902 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
909 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
911 cmd
->xfer
= scsi_cdb_xfer(buf
);
913 case TEST_UNIT_READY
:
917 case WRITE_FILEMARKS
:
918 case WRITE_FILEMARKS_16
:
923 case ALLOW_MEDIUM_REMOVAL
:
925 case SYNCHRONIZE_CACHE
:
926 case SYNCHRONIZE_CACHE_16
:
928 case LOCK_UNLOCK_CACHE
:
937 case ALLOW_OVERWRITE
:
943 if ((buf
[1] & 2) == 0) {
945 } else if ((buf
[1] & 4) != 0) {
948 cmd
->xfer
*= dev
->blocksize
;
954 cmd
->xfer
= dev
->blocksize
;
956 case READ_CAPACITY_10
:
959 case READ_BLOCK_LIMITS
:
962 case SEND_VOLUME_TAG
:
963 /* GPCMD_SET_STREAMING from multimedia commands. */
964 if (dev
->type
== TYPE_ROM
) {
965 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
967 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
971 /* length 0 means 256 blocks */
972 if (cmd
->xfer
== 0) {
977 case WRITE_VERIFY_10
:
979 case WRITE_VERIFY_12
:
981 case WRITE_VERIFY_16
:
982 cmd
->xfer
*= dev
->blocksize
;
986 /* length 0 means 256 blocks */
987 if (cmd
->xfer
== 0) {
994 cmd
->xfer
*= dev
->blocksize
;
997 /* MMC mandates the parameter list to be 12-bytes long. Parameters
998 * for block devices are restricted to the header right now. */
999 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1002 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1006 case RECEIVE_DIAGNOSTIC
:
1007 case SEND_DIAGNOSTIC
:
1008 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1013 case SEND_CUE_SHEET
:
1014 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1016 case PERSISTENT_RESERVE_OUT
:
1017 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1020 if (dev
->type
== TYPE_ROM
) {
1021 /* MMC command GET PERFORMANCE. */
1022 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1023 buf
[10], buf
[1] & 0x1f);
1026 case MECHANISM_STATUS
:
1027 case READ_DVD_STRUCTURE
:
1028 case SEND_DVD_STRUCTURE
:
1029 case MAINTENANCE_OUT
:
1030 case MAINTENANCE_IN
:
1031 if (dev
->type
== TYPE_ROM
) {
1032 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1033 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1036 case ATA_PASSTHROUGH_12
:
1037 if (dev
->type
== TYPE_ROM
) {
1038 /* BLANK command of MMC */
1041 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1044 case ATA_PASSTHROUGH_16
:
1045 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1051 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1054 /* stream commands */
1061 case RECOVER_BUFFERED_DATA
:
1063 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1064 if (buf
[1] & 0x01) { /* fixed */
1065 cmd
->xfer
*= dev
->blocksize
;
1069 case READ_REVERSE_16
:
1072 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1073 if (buf
[1] & 0x01) { /* fixed */
1074 cmd
->xfer
*= dev
->blocksize
;
1082 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1085 switch (buf
[1] & 0x1f) /* operation code */ {
1086 case SHORT_FORM_BLOCK_ID
:
1087 case SHORT_FORM_VENDOR_SPECIFIC
:
1094 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1102 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1104 /* generic commands */
1106 return scsi_req_xfer(cmd
, dev
, buf
);
1111 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1114 /* medium changer commands */
1115 case EXCHANGE_MEDIUM
:
1116 case INITIALIZE_ELEMENT_STATUS
:
1117 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1119 case POSITION_TO_ELEMENT
:
1122 case READ_ELEMENT_STATUS
:
1123 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1126 /* generic commands */
1128 return scsi_req_xfer(cmd
, dev
, buf
);
1134 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1137 cmd
->mode
= SCSI_XFER_NONE
;
1140 switch (cmd
->buf
[0]) {
1143 case WRITE_VERIFY_10
:
1145 case WRITE_VERIFY_12
:
1147 case WRITE_VERIFY_16
:
1154 case CHANGE_DEFINITION
:
1157 case MODE_SELECT_10
:
1158 case SEND_DIAGNOSTIC
:
1161 case REASSIGN_BLOCKS
:
1170 case SEARCH_HIGH_12
:
1171 case SEARCH_EQUAL_12
:
1174 case SEND_VOLUME_TAG
:
1175 case SEND_CUE_SHEET
:
1176 case SEND_DVD_STRUCTURE
:
1177 case PERSISTENT_RESERVE_OUT
:
1178 case MAINTENANCE_OUT
:
1179 cmd
->mode
= SCSI_XFER_TO_DEV
;
1181 case ATA_PASSTHROUGH_12
:
1182 case ATA_PASSTHROUGH_16
:
1184 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1185 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1188 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1193 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1195 uint8_t *buf
= cmd
->buf
;
1198 switch (buf
[0] >> 5) {
1200 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1205 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1208 lba
= ldq_be_p(&buf
[2]);
1217 int scsi_cdb_length(uint8_t *buf
) {
1220 switch (buf
[0] >> 5) {
1240 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1246 len
= scsi_cdb_length(buf
);
1252 switch (dev
->type
) {
1254 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1256 case TYPE_MEDIUM_CHANGER
:
1257 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1260 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1267 memcpy(cmd
->buf
, buf
, cmd
->len
);
1268 scsi_cmd_xfer_mode(cmd
);
1269 cmd
->lba
= scsi_cmd_lba(cmd
);
1273 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1275 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1277 scsi_device_set_ua(dev
, sense
);
1278 if (bus
->info
->change
) {
1279 bus
->info
->change(bus
, dev
, sense
);
1284 * Predefined sense codes
1287 /* No sense data available */
1288 const struct SCSISense sense_code_NO_SENSE
= {
1289 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1292 /* LUN not ready, Manual intervention required */
1293 const struct SCSISense sense_code_LUN_NOT_READY
= {
1294 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1297 /* LUN not ready, Medium not present */
1298 const struct SCSISense sense_code_NO_MEDIUM
= {
1299 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1302 /* LUN not ready, medium removal prevented */
1303 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1304 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1307 /* Hardware error, internal target failure */
1308 const struct SCSISense sense_code_TARGET_FAILURE
= {
1309 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1312 /* Illegal request, invalid command operation code */
1313 const struct SCSISense sense_code_INVALID_OPCODE
= {
1314 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1317 /* Illegal request, LBA out of range */
1318 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1319 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1322 /* Illegal request, Invalid field in CDB */
1323 const struct SCSISense sense_code_INVALID_FIELD
= {
1324 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1327 /* Illegal request, Invalid field in parameter list */
1328 const struct SCSISense sense_code_INVALID_PARAM
= {
1329 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1332 /* Illegal request, Parameter list length error */
1333 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1334 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1337 /* Illegal request, LUN not supported */
1338 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1339 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1342 /* Illegal request, Saving parameters not supported */
1343 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1344 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1347 /* Illegal request, Incompatible medium installed */
1348 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1349 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1352 /* Illegal request, medium removal prevented */
1353 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1354 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1357 /* Illegal request, Invalid Transfer Tag */
1358 const struct SCSISense sense_code_INVALID_TAG
= {
1359 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1362 /* Command aborted, I/O process terminated */
1363 const struct SCSISense sense_code_IO_ERROR
= {
1364 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1367 /* Command aborted, I_T Nexus loss occurred */
1368 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1369 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1372 /* Command aborted, Logical Unit failure */
1373 const struct SCSISense sense_code_LUN_FAILURE
= {
1374 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1377 /* Command aborted, Overlapped Commands Attempted */
1378 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1379 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1382 /* Unit attention, Capacity data has changed */
1383 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1384 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1387 /* Unit attention, Power on, reset or bus device reset occurred */
1388 const struct SCSISense sense_code_RESET
= {
1389 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1392 /* Unit attention, No medium */
1393 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1394 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1397 /* Unit attention, Medium may have changed */
1398 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1399 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1402 /* Unit attention, Reported LUNs data has changed */
1403 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1404 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1407 /* Unit attention, Device internal reset */
1408 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1409 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1412 /* Data Protection, Write Protected */
1413 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1414 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1417 /* Data Protection, Space Allocation Failed Write Protect */
1418 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1419 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1425 * Convert between fixed and descriptor sense buffers
1427 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1428 uint8_t *buf
, int len
, bool fixed
)
1432 if (!fixed
&& len
< 8) {
1437 sense
.key
= NO_SENSE
;
1441 fixed_in
= (in_buf
[0] & 2) == 0;
1443 if (fixed
== fixed_in
) {
1444 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1445 return MIN(len
, in_len
);
1449 sense
.key
= in_buf
[2];
1450 sense
.asc
= in_buf
[12];
1451 sense
.ascq
= in_buf
[13];
1453 sense
.key
= in_buf
[1];
1454 sense
.asc
= in_buf
[2];
1455 sense
.ascq
= in_buf
[3];
1459 memset(buf
, 0, len
);
1461 /* Return fixed format sense buffer */
1465 buf
[12] = sense
.asc
;
1466 buf
[13] = sense
.ascq
;
1467 return MIN(len
, SCSI_SENSE_LEN
);
1469 /* Return descriptor format sense buffer */
1473 buf
[3] = sense
.ascq
;
1478 const char *scsi_command_name(uint8_t cmd
)
1480 static const char *names
[] = {
1481 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1482 [ REWIND
] = "REWIND",
1483 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1484 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1485 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1486 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1487 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1488 [ READ_6
] = "READ_6",
1489 [ WRITE_6
] = "WRITE_6",
1490 [ SET_CAPACITY
] = "SET_CAPACITY",
1491 [ READ_REVERSE
] = "READ_REVERSE",
1492 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1493 [ SPACE
] = "SPACE",
1494 [ INQUIRY
] = "INQUIRY",
1495 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1496 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1497 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1498 [ MODE_SELECT
] = "MODE_SELECT",
1499 [ RESERVE
] = "RESERVE",
1500 [ RELEASE
] = "RELEASE",
1502 [ ERASE
] = "ERASE",
1503 [ MODE_SENSE
] = "MODE_SENSE",
1504 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1505 /* LOAD_UNLOAD and START_STOP use the same operation code */
1506 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1507 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1508 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1509 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1510 [ READ_10
] = "READ_10",
1511 [ WRITE_10
] = "WRITE_10",
1512 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1513 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1514 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1515 [ VERIFY_10
] = "VERIFY_10",
1516 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1517 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1518 [ SEARCH_LOW
] = "SEARCH_LOW",
1519 [ SET_LIMITS
] = "SET_LIMITS",
1520 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1521 /* READ_POSITION and PRE_FETCH use the same operation code */
1522 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1523 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1524 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1525 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1526 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1527 [ COMPARE
] = "COMPARE",
1528 [ COPY_VERIFY
] = "COPY_VERIFY",
1529 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1530 [ READ_BUFFER
] = "READ_BUFFER",
1531 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1532 [ READ_LONG_10
] = "READ_LONG_10",
1533 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1534 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1535 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1536 [ UNMAP
] = "UNMAP",
1537 [ READ_TOC
] = "READ_TOC",
1538 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1539 [ SANITIZE
] = "SANITIZE",
1540 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1541 [ LOG_SELECT
] = "LOG_SELECT",
1542 [ LOG_SENSE
] = "LOG_SENSE",
1543 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1544 [ RESERVE_10
] = "RESERVE_10",
1545 [ RELEASE_10
] = "RELEASE_10",
1546 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1547 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1548 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1549 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1550 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1551 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1552 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1553 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1554 [ READ_16
] = "READ_16",
1555 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1556 [ WRITE_16
] = "WRITE_16",
1557 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1558 [ VERIFY_16
] = "VERIFY_16",
1559 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1560 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1561 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1562 [ LOCATE_16
] = "LOCATE_16",
1563 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1564 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1565 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1566 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1567 [ REPORT_LUNS
] = "REPORT_LUNS",
1568 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1569 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1570 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1571 [ READ_12
] = "READ_12",
1572 [ WRITE_12
] = "WRITE_12",
1573 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1574 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1575 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1576 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1577 [ VERIFY_12
] = "VERIFY_12",
1578 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1579 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1580 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1581 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1582 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1583 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1584 [ READ_CD
] = "READ_CD",
1585 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1586 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1587 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1588 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1589 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1590 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1591 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1592 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1593 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1594 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1595 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1598 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1603 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1605 assert(req
->refcount
> 0);
1610 void scsi_req_unref(SCSIRequest
*req
)
1612 assert(req
->refcount
> 0);
1613 if (--req
->refcount
== 0) {
1614 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1615 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1617 if (bus
->info
->free_request
&& req
->hba_private
) {
1618 bus
->info
->free_request(bus
, req
->hba_private
);
1620 if (req
->ops
->free_req
) {
1621 req
->ops
->free_req(req
);
1623 object_unref(OBJECT(req
->dev
));
1624 object_unref(OBJECT(qbus
->parent
));
1629 /* Tell the device that we finished processing this chunk of I/O. It
1630 will start the next chunk or complete the command. */
1631 void scsi_req_continue(SCSIRequest
*req
)
1633 if (req
->io_canceled
) {
1634 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1637 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1638 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1639 req
->ops
->write_data(req
);
1641 req
->ops
->read_data(req
);
1645 /* Called by the devices when data is ready for the HBA. The HBA should
1646 start a DMA operation to read or fill the device's data buffer.
1647 Once it completes, calling scsi_req_continue will restart I/O. */
1648 void scsi_req_data(SCSIRequest
*req
, int len
)
1651 if (req
->io_canceled
) {
1652 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1655 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1656 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1659 req
->bus
->info
->transfer_data(req
, len
);
1663 /* If the device calls scsi_req_data and the HBA specified a
1664 * scatter/gather list, the transfer has to happen in a single
1666 assert(!req
->dma_started
);
1667 req
->dma_started
= true;
1669 buf
= scsi_req_get_buf(req
);
1670 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1671 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1673 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1675 scsi_req_continue(req
);
1678 void scsi_req_print(SCSIRequest
*req
)
1683 fprintf(fp
, "[%s id=%d] %s",
1684 req
->dev
->qdev
.parent_bus
->name
,
1686 scsi_command_name(req
->cmd
.buf
[0]));
1687 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1688 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1690 switch (req
->cmd
.mode
) {
1691 case SCSI_XFER_NONE
:
1692 fprintf(fp
, " - none\n");
1694 case SCSI_XFER_FROM_DEV
:
1695 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1697 case SCSI_XFER_TO_DEV
:
1698 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1701 fprintf(fp
, " - Oops\n");
1706 void scsi_req_complete(SCSIRequest
*req
, int status
)
1708 assert(req
->status
== -1);
1709 req
->status
= status
;
1711 assert(req
->sense_len
<= sizeof(req
->sense
));
1712 if (status
== GOOD
) {
1716 if (req
->sense_len
) {
1717 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1718 req
->dev
->sense_len
= req
->sense_len
;
1719 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1721 req
->dev
->sense_len
= 0;
1722 req
->dev
->sense_is_ua
= false;
1726 * Unit attention state is now stored in the device's sense buffer
1727 * if the HBA didn't do autosense. Clear the pending unit attention
1730 scsi_clear_unit_attention(req
);
1733 scsi_req_dequeue(req
);
1734 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1736 /* Cancelled requests might end up being completed instead of cancelled */
1737 notifier_list_notify(&req
->cancel_notifiers
, req
);
1738 scsi_req_unref(req
);
1741 /* Called by the devices when the request is canceled. */
1742 void scsi_req_cancel_complete(SCSIRequest
*req
)
1744 assert(req
->io_canceled
);
1745 if (req
->bus
->info
->cancel
) {
1746 req
->bus
->info
->cancel(req
);
1748 notifier_list_notify(&req
->cancel_notifiers
, req
);
1749 scsi_req_unref(req
);
1752 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1753 * notifier list, the bus will be notified the requests cancellation is
1756 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1758 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1760 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1762 if (req
->io_canceled
) {
1763 /* A blk_aio_cancel_async is pending; when it finishes,
1764 * scsi_req_cancel_complete will be called and will
1765 * call the notifier we just added. Just wait for that.
1770 /* Dropped in scsi_req_cancel_complete. */
1772 scsi_req_dequeue(req
);
1773 req
->io_canceled
= true;
1775 blk_aio_cancel_async(req
->aiocb
);
1777 scsi_req_cancel_complete(req
);
1781 void scsi_req_cancel(SCSIRequest
*req
)
1783 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1784 if (!req
->enqueued
) {
1787 assert(!req
->io_canceled
);
1788 /* Dropped in scsi_req_cancel_complete. */
1790 scsi_req_dequeue(req
);
1791 req
->io_canceled
= true;
1793 blk_aio_cancel(req
->aiocb
);
1795 scsi_req_cancel_complete(req
);
1799 static int scsi_ua_precedence(SCSISense sense
)
1801 if (sense
.key
!= UNIT_ATTENTION
) {
1804 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1805 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1807 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1808 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1810 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1811 /* These two go with "all others". */
1813 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1814 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1815 * POWER ON OCCURRED = 1
1816 * SCSI BUS RESET OCCURRED = 2
1817 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1818 * I_T NEXUS LOSS OCCURRED = 7
1821 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1822 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1825 return (sense
.asc
<< 8) | sense
.ascq
;
1828 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1831 if (sense
.key
!= UNIT_ATTENTION
) {
1834 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1835 sense
.asc
, sense
.ascq
);
1838 * Override a pre-existing unit attention condition, except for a more
1839 * important reset condition.
1841 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1842 prec2
= scsi_ua_precedence(sense
);
1843 if (prec2
< prec1
) {
1844 sdev
->unit_attention
= sense
;
1848 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1852 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1853 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1854 req
= QTAILQ_FIRST(&sdev
->requests
);
1855 scsi_req_cancel_async(req
, NULL
);
1857 blk_drain(sdev
->conf
.blk
);
1858 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1859 scsi_device_set_ua(sdev
, sense
);
1862 static char *scsibus_get_dev_path(DeviceState
*dev
)
1864 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1865 DeviceState
*hba
= dev
->parent_bus
->parent
;
1869 id
= qdev_get_dev_path(hba
);
1871 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1873 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1879 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1881 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1882 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1883 qdev_fw_name(dev
), d
->id
, d
->lun
);
1886 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1889 SCSIDevice
*target_dev
= NULL
;
1891 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1892 DeviceState
*qdev
= kid
->child
;
1893 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1895 if (dev
->channel
== channel
&& dev
->id
== id
) {
1896 if (dev
->lun
== lun
) {
1905 /* SCSI request list. For simplicity, pv points to the whole device */
1907 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1910 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1913 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1914 assert(!req
->io_canceled
);
1915 assert(req
->status
== -1);
1916 assert(req
->enqueued
);
1918 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1919 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1920 qemu_put_be32s(f
, &req
->tag
);
1921 qemu_put_be32s(f
, &req
->lun
);
1922 if (bus
->info
->save_request
) {
1923 bus
->info
->save_request(f
, req
);
1925 if (req
->ops
->save_request
) {
1926 req
->ops
->save_request(f
, req
);
1929 qemu_put_sbyte(f
, 0);
1932 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1935 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1938 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1939 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1944 qemu_get_buffer(f
, buf
, sizeof(buf
));
1945 qemu_get_be32s(f
, &tag
);
1946 qemu_get_be32s(f
, &lun
);
1947 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1948 req
->retry
= (sbyte
== 1);
1949 if (bus
->info
->load_request
) {
1950 req
->hba_private
= bus
->info
->load_request(f
, req
);
1952 if (req
->ops
->load_request
) {
1953 req
->ops
->load_request(f
, req
);
1956 /* Just restart it later. */
1957 scsi_req_enqueue_internal(req
);
1959 /* At this point, the request will be kept alive by the reference
1960 * added by scsi_req_enqueue_internal, so we can release our reference.
1961 * The HBA of course will add its own reference in the load_request
1962 * callback if it needs to hold on the SCSIRequest.
1964 scsi_req_unref(req
);
1970 static const VMStateInfo vmstate_info_scsi_requests
= {
1971 .name
= "scsi-requests",
1972 .get
= get_scsi_requests
,
1973 .put
= put_scsi_requests
,
1976 static bool scsi_sense_state_needed(void *opaque
)
1978 SCSIDevice
*s
= opaque
;
1980 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1983 static const VMStateDescription vmstate_scsi_sense_state
= {
1984 .name
= "SCSIDevice/sense",
1986 .minimum_version_id
= 1,
1987 .needed
= scsi_sense_state_needed
,
1988 .fields
= (VMStateField
[]) {
1989 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1990 SCSI_SENSE_BUF_SIZE_OLD
,
1991 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1992 VMSTATE_END_OF_LIST()
1996 const VMStateDescription vmstate_scsi_device
= {
1997 .name
= "SCSIDevice",
1999 .minimum_version_id
= 1,
2000 .fields
= (VMStateField
[]) {
2001 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
2002 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
2003 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
2004 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
2005 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
2006 VMSTATE_UINT32(sense_len
, SCSIDevice
),
2010 .field_exists
= NULL
,
2011 .size
= 0, /* ouch */
2012 .info
= &vmstate_info_scsi_requests
,
2013 .flags
= VMS_SINGLE
,
2016 VMSTATE_END_OF_LIST()
2018 .subsections
= (const VMStateDescription
*[]) {
2019 &vmstate_scsi_sense_state
,
2024 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
2026 DeviceClass
*k
= DEVICE_CLASS(klass
);
2027 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
2028 k
->bus_type
= TYPE_SCSI_BUS
;
2029 k
->realize
= scsi_qdev_realize
;
2030 k
->unrealize
= scsi_qdev_unrealize
;
2031 k
->props
= scsi_props
;
2034 static void scsi_dev_instance_init(Object
*obj
)
2036 DeviceState
*dev
= DEVICE(obj
);
2037 SCSIDevice
*s
= SCSI_DEVICE(dev
);
2039 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
2044 static const TypeInfo scsi_device_type_info
= {
2045 .name
= TYPE_SCSI_DEVICE
,
2046 .parent
= TYPE_DEVICE
,
2047 .instance_size
= sizeof(SCSIDevice
),
2049 .class_size
= sizeof(SCSIDeviceClass
),
2050 .class_init
= scsi_device_class_init
,
2051 .instance_init
= scsi_dev_instance_init
,
2054 static void scsi_register_types(void)
2056 type_register_static(&scsi_bus_info
);
2057 type_register_static(&scsi_device_type_info
);
2060 type_init(scsi_register_types
)