2 #include "qemu/error-report.h"
3 #include "hw/scsi/scsi.h"
4 #include "block/scsi.h"
6 #include "sysemu/blockdev.h"
8 #include "sysemu/dma.h"
10 static char *scsibus_get_dev_path(DeviceState
*dev
);
11 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
12 static void scsi_req_dequeue(SCSIRequest
*req
);
13 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
14 static void scsi_target_free_buf(SCSIRequest
*req
);
16 static Property scsi_props
[] = {
17 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
18 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
19 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
20 DEFINE_PROP_END_OF_LIST(),
23 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
25 BusClass
*k
= BUS_CLASS(klass
);
27 k
->get_dev_path
= scsibus_get_dev_path
;
28 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
31 static const TypeInfo scsi_bus_info
= {
32 .name
= TYPE_SCSI_BUS
,
34 .instance_size
= sizeof(SCSIBus
),
35 .class_init
= scsi_bus_class_init
,
37 static int next_scsi_bus
;
39 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
41 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
47 static void scsi_device_unrealize(SCSIDevice
*s
, Error
**errp
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
51 sc
->unrealize(s
, errp
);
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 static 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 bus
->qbus
.allow_hotplug
= 1;
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 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
141 qemu_bh_schedule(s
->bh
);
145 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
147 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
148 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
150 Error
*local_err
= NULL
;
152 if (dev
->channel
> bus
->info
->max_channel
) {
153 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
156 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
157 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
160 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
161 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
167 if (dev
->lun
== -1) {
171 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
172 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
173 if (d
&& d
->lun
== dev
->lun
) {
174 error_setg(errp
, "no free target");
178 } else if (dev
->lun
== -1) {
181 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
182 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
183 if (d
&& d
->lun
== lun
) {
184 error_setg(errp
, "no free lun");
189 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
191 if (d
->lun
== dev
->lun
&& dev
!= d
) {
192 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
197 QTAILQ_INIT(&dev
->requests
);
198 scsi_device_realize(dev
, &local_err
);
200 error_propagate(errp
, local_err
);
203 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
206 if (bus
->info
->hotplug
) {
207 bus
->info
->hotplug(bus
, dev
);
211 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
213 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
216 qemu_del_vm_change_state_handler(dev
->vmsentry
);
218 scsi_device_unrealize(dev
, errp
);
221 /* handle legacy '-drive if=scsi,...' cmd line args */
222 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
223 int unit
, bool removable
, int bootindex
,
224 const char *serial
, Error
**errp
)
230 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
231 dev
= qdev_create(&bus
->qbus
, driver
);
232 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
233 if (bootindex
>= 0) {
234 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
236 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
237 qdev_prop_set_bit(dev
, "removable", removable
);
239 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
240 qdev_prop_set_string(dev
, "serial", serial
);
242 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
243 error_setg(errp
, "Setting drive property failed");
244 object_unparent(OBJECT(dev
));
247 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
249 error_propagate(errp
, err
);
250 object_unparent(OBJECT(dev
));
253 return SCSI_DEVICE(dev
);
256 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
264 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
265 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
269 qemu_opts_loc_restore(dinfo
->opts
);
270 scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1, NULL
,
273 error_report("%s", error_get_pretty(err
));
274 error_propagate(errp
, err
);
281 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
283 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
284 scsi_req_complete(req
, CHECK_CONDITION
);
288 static const struct SCSIReqOps reqops_invalid_field
= {
289 .size
= sizeof(SCSIRequest
),
290 .send_command
= scsi_invalid_field
293 /* SCSIReqOps implementation for invalid commands. */
295 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
297 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
298 scsi_req_complete(req
, CHECK_CONDITION
);
302 static const struct SCSIReqOps reqops_invalid_opcode
= {
303 .size
= sizeof(SCSIRequest
),
304 .send_command
= scsi_invalid_command
307 /* SCSIReqOps implementation for unit attention conditions. */
309 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
311 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
312 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
313 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
314 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
316 scsi_req_complete(req
, CHECK_CONDITION
);
320 static const struct SCSIReqOps reqops_unit_attention
= {
321 .size
= sizeof(SCSIRequest
),
322 .send_command
= scsi_unit_attention
325 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
328 typedef struct SCSITargetReq SCSITargetReq
;
330 struct SCSITargetReq
{
337 static void store_lun(uint8_t *outbuf
, int lun
)
343 outbuf
[1] = (lun
& 255);
344 outbuf
[0] = (lun
>> 8) | 0x40;
347 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
354 if (r
->req
.cmd
.xfer
< 16) {
357 if (r
->req
.cmd
.buf
[2] > 2) {
360 channel
= r
->req
.dev
->channel
;
364 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
365 DeviceState
*qdev
= kid
->child
;
366 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
368 if (dev
->channel
== channel
&& dev
->id
== id
) {
379 scsi_target_alloc_buf(&r
->req
, n
+ 8);
381 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
382 memset(r
->buf
, 0, len
);
383 stl_be_p(&r
->buf
[0], n
);
384 i
= found_lun0
? 8 : 16;
385 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
386 DeviceState
*qdev
= kid
->child
;
387 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
389 if (dev
->channel
== channel
&& dev
->id
== id
) {
390 store_lun(&r
->buf
[i
], dev
->lun
);
399 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
401 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
403 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
405 if (r
->req
.cmd
.buf
[1] & 0x2) {
406 /* Command support data - optional, not implemented */
410 if (r
->req
.cmd
.buf
[1] & 0x1) {
411 /* Vital product data */
412 uint8_t page_code
= r
->req
.cmd
.buf
[2];
413 r
->buf
[r
->len
++] = page_code
; /* this page */
414 r
->buf
[r
->len
++] = 0x00;
417 case 0x00: /* Supported page codes, mandatory */
421 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
422 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
429 assert(r
->len
< r
->buf_len
);
430 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
434 /* Standard INQUIRY data */
435 if (r
->req
.cmd
.buf
[2] != 0) {
440 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
441 memset(r
->buf
, 0, r
->len
);
442 if (r
->req
.lun
!= 0) {
443 r
->buf
[0] = TYPE_NO_LUN
;
445 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
446 r
->buf
[2] = 5; /* Version */
447 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
448 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
449 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
450 memcpy(&r
->buf
[8], "QEMU ", 8);
451 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
452 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
457 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
459 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
463 if (!scsi_target_emulate_report_luns(r
)) {
464 goto illegal_request
;
468 if (!scsi_target_emulate_inquiry(r
)) {
469 goto illegal_request
;
473 scsi_target_alloc_buf(&r
->req
, SCSI_SENSE_LEN
);
474 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
475 MIN(req
->cmd
.xfer
, r
->buf_len
),
476 (req
->cmd
.buf
[1] & 1) == 0);
477 if (r
->req
.dev
->sense_is_ua
) {
478 scsi_device_unit_attention_reported(req
->dev
);
479 r
->req
.dev
->sense_len
= 0;
480 r
->req
.dev
->sense_is_ua
= false;
483 case TEST_UNIT_READY
:
486 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
487 scsi_req_complete(req
, CHECK_CONDITION
);
490 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
491 scsi_req_complete(req
, CHECK_CONDITION
);
496 scsi_req_complete(req
, GOOD
);
501 static void scsi_target_read_data(SCSIRequest
*req
)
503 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
509 scsi_req_data(&r
->req
, n
);
511 scsi_req_complete(&r
->req
, GOOD
);
515 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
517 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
522 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
524 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
526 r
->buf
= g_malloc(len
);
532 static void scsi_target_free_buf(SCSIRequest
*req
)
534 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
539 static const struct SCSIReqOps reqops_target_command
= {
540 .size
= sizeof(SCSITargetReq
),
541 .send_command
= scsi_target_send_command
,
542 .read_data
= scsi_target_read_data
,
543 .get_buf
= scsi_target_get_buf
,
544 .free_req
= scsi_target_free_buf
,
548 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
549 uint32_t tag
, uint32_t lun
, void *hba_private
)
552 SCSIBus
*bus
= scsi_bus_from_device(d
);
553 BusState
*qbus
= BUS(bus
);
555 req
= g_malloc0(reqops
->size
);
561 req
->hba_private
= hba_private
;
565 object_ref(OBJECT(d
));
566 object_ref(OBJECT(qbus
->parent
));
567 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
571 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
572 uint8_t *buf
, void *hba_private
)
574 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
575 const SCSIReqOps
*ops
;
576 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
578 SCSICommand cmd
= { .len
= 0 };
581 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
582 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
583 (buf
[0] != INQUIRY
&&
584 buf
[0] != REPORT_LUNS
&&
585 buf
[0] != GET_CONFIGURATION
&&
586 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
589 * If we already have a pending unit attention condition,
590 * report this one before triggering another one.
592 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
593 ops
= &reqops_unit_attention
;
594 } else if (lun
!= d
->lun
||
595 buf
[0] == REPORT_LUNS
||
596 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
597 ops
= &reqops_target_command
;
602 if (ops
!= NULL
|| !sc
->parse_cdb
) {
603 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
605 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
609 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
610 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
612 assert(cmd
.len
!= 0);
613 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
616 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
620 if (cmd
.xfer
> INT32_MAX
) {
621 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
623 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
625 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
630 req
->resid
= req
->cmd
.xfer
;
634 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
636 case TEST_UNIT_READY
:
637 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
640 trace_scsi_report_luns(d
->id
, lun
, tag
);
643 trace_scsi_request_sense(d
->id
, lun
, tag
);
652 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
654 return req
->ops
->get_buf(req
);
657 static void scsi_clear_unit_attention(SCSIRequest
*req
)
660 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
661 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
666 * If an INQUIRY command enters the enabled command state,
667 * the device server shall [not] clear any unit attention condition;
668 * See also MMC-6, paragraphs 6.5 and 6.6.2.
670 if (req
->cmd
.buf
[0] == INQUIRY
||
671 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
672 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
676 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
677 ua
= &req
->dev
->unit_attention
;
679 ua
= &req
->bus
->unit_attention
;
683 * If a REPORT LUNS command enters the enabled command state, [...]
684 * the device server shall clear any pending unit attention condition
685 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
687 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
688 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
689 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
693 *ua
= SENSE_CODE(NO_SENSE
);
696 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
701 if (!req
->sense_len
) {
705 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
708 * FIXME: clearing unit attention conditions upon autosense should be done
709 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
712 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
713 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
714 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
716 if (req
->dev
->sense_is_ua
) {
717 scsi_device_unit_attention_reported(req
->dev
);
718 req
->dev
->sense_len
= 0;
719 req
->dev
->sense_is_ua
= false;
724 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
726 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
729 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
731 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
732 sense
.key
, sense
.asc
, sense
.ascq
);
733 memset(req
->sense
, 0, 18);
734 req
->sense
[0] = 0x70;
735 req
->sense
[2] = sense
.key
;
737 req
->sense
[12] = sense
.asc
;
738 req
->sense
[13] = sense
.ascq
;
742 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
744 assert(!req
->enqueued
);
746 if (req
->bus
->info
->get_sg_list
) {
747 req
->sg
= req
->bus
->info
->get_sg_list(req
);
751 req
->enqueued
= true;
752 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
755 int32_t scsi_req_enqueue(SCSIRequest
*req
)
760 scsi_req_enqueue_internal(req
);
762 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
767 static void scsi_req_dequeue(SCSIRequest
*req
)
769 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
772 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
773 req
->enqueued
= false;
778 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
780 /* MMC-6, paragraph 6.7. */
783 if ((data_type
& 3) == 0) {
784 /* Each descriptor is as in Table 295 - Nominal performance. */
785 return 16 * num_desc
+ 8;
787 /* Each descriptor is as in Table 296 - Exceptions. */
788 return 6 * num_desc
+ 8;
793 return 8 * num_desc
+ 8;
795 return 2048 * num_desc
+ 8;
797 return 16 * num_desc
+ 8;
803 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
805 int byte_block
= (buf
[2] >> 2) & 0x1;
806 int type
= (buf
[2] >> 4) & 0x1;
811 xfer_unit
= dev
->blocksize
;
822 static int ata_passthrough_12_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
824 int length
= buf
[2] & 0x3;
826 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
830 case 3: /* USB-specific. */
845 static int ata_passthrough_16_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
847 int extend
= buf
[1] & 0x1;
848 int length
= buf
[2] & 0x3;
850 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
854 case 3: /* USB-specific. */
860 xfer
|= (extend
? buf
[3] << 8 : 0);
864 xfer
|= (extend
? buf
[5] << 8 : 0);
871 uint32_t scsi_data_cdb_length(uint8_t *buf
)
873 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
876 return scsi_cdb_length(buf
);
880 uint32_t scsi_cdb_length(uint8_t *buf
)
882 switch (buf
[0] >> 5) {
888 return lduw_be_p(&buf
[7]);
891 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
894 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
901 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
903 cmd
->xfer
= scsi_cdb_length(buf
);
905 case TEST_UNIT_READY
:
909 case WRITE_FILEMARKS
:
910 case WRITE_FILEMARKS_16
:
915 case ALLOW_MEDIUM_REMOVAL
:
917 case SYNCHRONIZE_CACHE
:
918 case SYNCHRONIZE_CACHE_16
:
920 case LOCK_UNLOCK_CACHE
:
929 case ALLOW_OVERWRITE
:
935 if ((buf
[1] & 2) == 0) {
937 } else if ((buf
[1] & 4) != 0) {
940 cmd
->xfer
*= dev
->blocksize
;
946 cmd
->xfer
= dev
->blocksize
;
948 case READ_CAPACITY_10
:
951 case READ_BLOCK_LIMITS
:
954 case SEND_VOLUME_TAG
:
955 /* GPCMD_SET_STREAMING from multimedia commands. */
956 if (dev
->type
== TYPE_ROM
) {
957 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
959 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
963 /* length 0 means 256 blocks */
964 if (cmd
->xfer
== 0) {
969 case WRITE_VERIFY_10
:
971 case WRITE_VERIFY_12
:
973 case WRITE_VERIFY_16
:
974 cmd
->xfer
*= dev
->blocksize
;
978 /* length 0 means 256 blocks */
979 if (cmd
->xfer
== 0) {
984 case RECOVER_BUFFERED_DATA
:
987 cmd
->xfer
*= dev
->blocksize
;
990 /* MMC mandates the parameter list to be 12-bytes long. Parameters
991 * for block devices are restricted to the header right now. */
992 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
995 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
999 case RECEIVE_DIAGNOSTIC
:
1000 case SEND_DIAGNOSTIC
:
1001 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1006 case SEND_CUE_SHEET
:
1007 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1009 case PERSISTENT_RESERVE_OUT
:
1010 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1013 if (dev
->type
== TYPE_ROM
) {
1014 /* MMC command GET PERFORMANCE. */
1015 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1016 buf
[10], buf
[1] & 0x1f);
1019 case MECHANISM_STATUS
:
1020 case READ_DVD_STRUCTURE
:
1021 case SEND_DVD_STRUCTURE
:
1022 case MAINTENANCE_OUT
:
1023 case MAINTENANCE_IN
:
1024 if (dev
->type
== TYPE_ROM
) {
1025 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1026 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1029 case ATA_PASSTHROUGH_12
:
1030 if (dev
->type
== TYPE_ROM
) {
1031 /* BLANK command of MMC */
1034 cmd
->xfer
= ata_passthrough_12_xfer_size(dev
, buf
);
1037 case ATA_PASSTHROUGH_16
:
1038 cmd
->xfer
= ata_passthrough_16_xfer_size(dev
, buf
);
1044 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1047 /* stream commands */
1054 case RECOVER_BUFFERED_DATA
:
1056 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1057 if (buf
[1] & 0x01) { /* fixed */
1058 cmd
->xfer
*= dev
->blocksize
;
1062 case READ_REVERSE_16
:
1065 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1066 if (buf
[1] & 0x01) { /* fixed */
1067 cmd
->xfer
*= dev
->blocksize
;
1075 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1078 switch (buf
[1] & 0x1f) /* operation code */ {
1079 case SHORT_FORM_BLOCK_ID
:
1080 case SHORT_FORM_VENDOR_SPECIFIC
:
1087 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1095 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1097 /* generic commands */
1099 return scsi_req_length(cmd
, dev
, buf
);
1104 static int scsi_req_medium_changer_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1107 /* medium changer commands */
1108 case EXCHANGE_MEDIUM
:
1109 case INITIALIZE_ELEMENT_STATUS
:
1110 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1112 case POSITION_TO_ELEMENT
:
1115 case READ_ELEMENT_STATUS
:
1116 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1119 /* generic commands */
1121 return scsi_req_length(cmd
, dev
, buf
);
1127 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1130 cmd
->mode
= SCSI_XFER_NONE
;
1133 switch (cmd
->buf
[0]) {
1136 case WRITE_VERIFY_10
:
1138 case WRITE_VERIFY_12
:
1140 case WRITE_VERIFY_16
:
1147 case CHANGE_DEFINITION
:
1150 case MODE_SELECT_10
:
1151 case SEND_DIAGNOSTIC
:
1154 case REASSIGN_BLOCKS
:
1163 case SEARCH_HIGH_12
:
1164 case SEARCH_EQUAL_12
:
1167 case SEND_VOLUME_TAG
:
1168 case SEND_CUE_SHEET
:
1169 case SEND_DVD_STRUCTURE
:
1170 case PERSISTENT_RESERVE_OUT
:
1171 case MAINTENANCE_OUT
:
1172 cmd
->mode
= SCSI_XFER_TO_DEV
;
1174 case ATA_PASSTHROUGH_12
:
1175 case ATA_PASSTHROUGH_16
:
1177 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1178 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1181 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1186 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1188 uint8_t *buf
= cmd
->buf
;
1191 switch (buf
[0] >> 5) {
1193 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1198 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1201 lba
= ldq_be_p(&buf
[2]);
1210 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1215 switch (buf
[0] >> 5) {
1233 switch (dev
->type
) {
1235 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1237 case TYPE_MEDIUM_CHANGER
:
1238 rc
= scsi_req_medium_changer_length(cmd
, dev
, buf
);
1241 rc
= scsi_req_length(cmd
, dev
, buf
);
1248 memcpy(cmd
->buf
, buf
, cmd
->len
);
1249 scsi_cmd_xfer_mode(cmd
);
1250 cmd
->lba
= scsi_cmd_lba(cmd
);
1254 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1256 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1258 scsi_device_set_ua(dev
, sense
);
1259 if (bus
->info
->change
) {
1260 bus
->info
->change(bus
, dev
, sense
);
1265 * Predefined sense codes
1268 /* No sense data available */
1269 const struct SCSISense sense_code_NO_SENSE
= {
1270 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1273 /* LUN not ready, Manual intervention required */
1274 const struct SCSISense sense_code_LUN_NOT_READY
= {
1275 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1278 /* LUN not ready, Medium not present */
1279 const struct SCSISense sense_code_NO_MEDIUM
= {
1280 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1283 /* LUN not ready, medium removal prevented */
1284 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1285 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1288 /* Hardware error, internal target failure */
1289 const struct SCSISense sense_code_TARGET_FAILURE
= {
1290 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1293 /* Illegal request, invalid command operation code */
1294 const struct SCSISense sense_code_INVALID_OPCODE
= {
1295 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1298 /* Illegal request, LBA out of range */
1299 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1300 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1303 /* Illegal request, Invalid field in CDB */
1304 const struct SCSISense sense_code_INVALID_FIELD
= {
1305 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1308 /* Illegal request, Invalid field in parameter list */
1309 const struct SCSISense sense_code_INVALID_PARAM
= {
1310 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1313 /* Illegal request, Parameter list length error */
1314 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1315 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1318 /* Illegal request, LUN not supported */
1319 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1320 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1323 /* Illegal request, Saving parameters not supported */
1324 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1325 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1328 /* Illegal request, Incompatible medium installed */
1329 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1330 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1333 /* Illegal request, medium removal prevented */
1334 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1335 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1338 /* Illegal request, Invalid Transfer Tag */
1339 const struct SCSISense sense_code_INVALID_TAG
= {
1340 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1343 /* Command aborted, I/O process terminated */
1344 const struct SCSISense sense_code_IO_ERROR
= {
1345 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1348 /* Command aborted, I_T Nexus loss occurred */
1349 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1350 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1353 /* Command aborted, Logical Unit failure */
1354 const struct SCSISense sense_code_LUN_FAILURE
= {
1355 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1358 /* Command aborted, Overlapped Commands Attempted */
1359 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1360 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1363 /* Unit attention, Capacity data has changed */
1364 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1365 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1368 /* Unit attention, Power on, reset or bus device reset occurred */
1369 const struct SCSISense sense_code_RESET
= {
1370 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1373 /* Unit attention, No medium */
1374 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1375 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1378 /* Unit attention, Medium may have changed */
1379 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1380 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1383 /* Unit attention, Reported LUNs data has changed */
1384 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1385 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1388 /* Unit attention, Device internal reset */
1389 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1390 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1393 /* Data Protection, Write Protected */
1394 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1395 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1398 /* Data Protection, Space Allocation Failed Write Protect */
1399 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1400 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1406 * Convert between fixed and descriptor sense buffers
1408 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1409 uint8_t *buf
, int len
, bool fixed
)
1413 if (!fixed
&& len
< 8) {
1418 sense
.key
= NO_SENSE
;
1422 fixed_in
= (in_buf
[0] & 2) == 0;
1424 if (fixed
== fixed_in
) {
1425 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1426 return MIN(len
, in_len
);
1430 sense
.key
= in_buf
[2];
1431 sense
.asc
= in_buf
[12];
1432 sense
.ascq
= in_buf
[13];
1434 sense
.key
= in_buf
[1];
1435 sense
.asc
= in_buf
[2];
1436 sense
.ascq
= in_buf
[3];
1440 memset(buf
, 0, len
);
1442 /* Return fixed format sense buffer */
1446 buf
[12] = sense
.asc
;
1447 buf
[13] = sense
.ascq
;
1448 return MIN(len
, SCSI_SENSE_LEN
);
1450 /* Return descriptor format sense buffer */
1454 buf
[3] = sense
.ascq
;
1459 const char *scsi_command_name(uint8_t cmd
)
1461 static const char *names
[] = {
1462 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1463 [ REWIND
] = "REWIND",
1464 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1465 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1466 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1467 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1468 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1469 [ READ_6
] = "READ_6",
1470 [ WRITE_6
] = "WRITE_6",
1471 [ SET_CAPACITY
] = "SET_CAPACITY",
1472 [ READ_REVERSE
] = "READ_REVERSE",
1473 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1474 [ SPACE
] = "SPACE",
1475 [ INQUIRY
] = "INQUIRY",
1476 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1477 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1478 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1479 [ MODE_SELECT
] = "MODE_SELECT",
1480 [ RESERVE
] = "RESERVE",
1481 [ RELEASE
] = "RELEASE",
1483 [ ERASE
] = "ERASE",
1484 [ MODE_SENSE
] = "MODE_SENSE",
1485 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1486 /* LOAD_UNLOAD and START_STOP use the same operation code */
1487 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1488 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1489 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1490 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1491 [ READ_10
] = "READ_10",
1492 [ WRITE_10
] = "WRITE_10",
1493 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1494 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1495 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1496 [ VERIFY_10
] = "VERIFY_10",
1497 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1498 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1499 [ SEARCH_LOW
] = "SEARCH_LOW",
1500 [ SET_LIMITS
] = "SET_LIMITS",
1501 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1502 /* READ_POSITION and PRE_FETCH use the same operation code */
1503 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1504 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1505 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1506 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1507 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1508 [ COMPARE
] = "COMPARE",
1509 [ COPY_VERIFY
] = "COPY_VERIFY",
1510 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1511 [ READ_BUFFER
] = "READ_BUFFER",
1512 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1513 [ READ_LONG_10
] = "READ_LONG_10",
1514 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1515 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1516 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1517 [ UNMAP
] = "UNMAP",
1518 [ READ_TOC
] = "READ_TOC",
1519 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1520 [ SANITIZE
] = "SANITIZE",
1521 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1522 [ LOG_SELECT
] = "LOG_SELECT",
1523 [ LOG_SENSE
] = "LOG_SENSE",
1524 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1525 [ RESERVE_10
] = "RESERVE_10",
1526 [ RELEASE_10
] = "RELEASE_10",
1527 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1528 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1529 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1530 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1531 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1532 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1533 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1534 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1535 [ READ_16
] = "READ_16",
1536 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1537 [ WRITE_16
] = "WRITE_16",
1538 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1539 [ VERIFY_16
] = "VERIFY_16",
1540 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1541 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1542 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1543 [ LOCATE_16
] = "LOCATE_16",
1544 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1545 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1546 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1547 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1548 [ REPORT_LUNS
] = "REPORT_LUNS",
1549 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1550 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1551 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1552 [ READ_12
] = "READ_12",
1553 [ WRITE_12
] = "WRITE_12",
1554 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1555 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1556 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1557 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1558 [ VERIFY_12
] = "VERIFY_12",
1559 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1560 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1561 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1562 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1563 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1564 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1565 [ READ_CD
] = "READ_CD",
1566 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1567 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1568 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1569 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1570 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1571 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1572 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1573 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1574 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1575 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1576 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1579 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1584 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1586 assert(req
->refcount
> 0);
1591 void scsi_req_unref(SCSIRequest
*req
)
1593 assert(req
->refcount
> 0);
1594 if (--req
->refcount
== 0) {
1595 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1596 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1598 if (bus
->info
->free_request
&& req
->hba_private
) {
1599 bus
->info
->free_request(bus
, req
->hba_private
);
1601 if (req
->ops
->free_req
) {
1602 req
->ops
->free_req(req
);
1604 object_unref(OBJECT(req
->dev
));
1605 object_unref(OBJECT(qbus
->parent
));
1610 /* Tell the device that we finished processing this chunk of I/O. It
1611 will start the next chunk or complete the command. */
1612 void scsi_req_continue(SCSIRequest
*req
)
1614 if (req
->io_canceled
) {
1615 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1618 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1619 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1620 req
->ops
->write_data(req
);
1622 req
->ops
->read_data(req
);
1626 /* Called by the devices when data is ready for the HBA. The HBA should
1627 start a DMA operation to read or fill the device's data buffer.
1628 Once it completes, calling scsi_req_continue will restart I/O. */
1629 void scsi_req_data(SCSIRequest
*req
, int len
)
1632 if (req
->io_canceled
) {
1633 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1636 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1637 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1640 req
->bus
->info
->transfer_data(req
, len
);
1644 /* If the device calls scsi_req_data and the HBA specified a
1645 * scatter/gather list, the transfer has to happen in a single
1647 assert(!req
->dma_started
);
1648 req
->dma_started
= true;
1650 buf
= scsi_req_get_buf(req
);
1651 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1652 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1654 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1656 scsi_req_continue(req
);
1659 void scsi_req_print(SCSIRequest
*req
)
1664 fprintf(fp
, "[%s id=%d] %s",
1665 req
->dev
->qdev
.parent_bus
->name
,
1667 scsi_command_name(req
->cmd
.buf
[0]));
1668 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1669 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1671 switch (req
->cmd
.mode
) {
1672 case SCSI_XFER_NONE
:
1673 fprintf(fp
, " - none\n");
1675 case SCSI_XFER_FROM_DEV
:
1676 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1678 case SCSI_XFER_TO_DEV
:
1679 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1682 fprintf(fp
, " - Oops\n");
1687 void scsi_req_complete(SCSIRequest
*req
, int status
)
1689 assert(req
->status
== -1);
1690 req
->status
= status
;
1692 assert(req
->sense_len
<= sizeof(req
->sense
));
1693 if (status
== GOOD
) {
1697 if (req
->sense_len
) {
1698 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1699 req
->dev
->sense_len
= req
->sense_len
;
1700 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1702 req
->dev
->sense_len
= 0;
1703 req
->dev
->sense_is_ua
= false;
1707 * Unit attention state is now stored in the device's sense buffer
1708 * if the HBA didn't do autosense. Clear the pending unit attention
1711 scsi_clear_unit_attention(req
);
1714 scsi_req_dequeue(req
);
1715 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1716 scsi_req_unref(req
);
1719 void scsi_req_cancel(SCSIRequest
*req
)
1721 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1722 if (!req
->enqueued
) {
1726 scsi_req_dequeue(req
);
1727 req
->io_canceled
= true;
1728 if (req
->ops
->cancel_io
) {
1729 req
->ops
->cancel_io(req
);
1731 if (req
->bus
->info
->cancel
) {
1732 req
->bus
->info
->cancel(req
);
1734 scsi_req_unref(req
);
1737 void scsi_req_abort(SCSIRequest
*req
, int status
)
1739 if (!req
->enqueued
) {
1743 scsi_req_dequeue(req
);
1744 req
->io_canceled
= true;
1745 if (req
->ops
->cancel_io
) {
1746 req
->ops
->cancel_io(req
);
1748 scsi_req_complete(req
, status
);
1749 scsi_req_unref(req
);
1752 static int scsi_ua_precedence(SCSISense sense
)
1754 if (sense
.key
!= UNIT_ATTENTION
) {
1757 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1758 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1760 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1761 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1763 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1764 /* These two go with "all others". */
1766 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1767 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1768 * POWER ON OCCURRED = 1
1769 * SCSI BUS RESET OCCURRED = 2
1770 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1771 * I_T NEXUS LOSS OCCURRED = 7
1774 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1775 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1778 return (sense
.asc
<< 8) | sense
.ascq
;
1781 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1784 if (sense
.key
!= UNIT_ATTENTION
) {
1787 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1788 sense
.asc
, sense
.ascq
);
1791 * Override a pre-existing unit attention condition, except for a more
1792 * important reset condition.
1794 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1795 prec2
= scsi_ua_precedence(sense
);
1796 if (prec2
< prec1
) {
1797 sdev
->unit_attention
= sense
;
1801 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1805 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1806 req
= QTAILQ_FIRST(&sdev
->requests
);
1807 scsi_req_cancel(req
);
1810 scsi_device_set_ua(sdev
, sense
);
1813 static char *scsibus_get_dev_path(DeviceState
*dev
)
1815 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1816 DeviceState
*hba
= dev
->parent_bus
->parent
;
1820 id
= qdev_get_dev_path(hba
);
1822 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1824 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1830 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1832 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1833 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1834 qdev_fw_name(dev
), d
->id
, d
->lun
);
1837 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1840 SCSIDevice
*target_dev
= NULL
;
1842 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1843 DeviceState
*qdev
= kid
->child
;
1844 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1846 if (dev
->channel
== channel
&& dev
->id
== id
) {
1847 if (dev
->lun
== lun
) {
1856 /* SCSI request list. For simplicity, pv points to the whole device */
1858 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1861 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1864 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1865 assert(!req
->io_canceled
);
1866 assert(req
->status
== -1);
1867 assert(req
->enqueued
);
1869 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1870 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1871 qemu_put_be32s(f
, &req
->tag
);
1872 qemu_put_be32s(f
, &req
->lun
);
1873 if (bus
->info
->save_request
) {
1874 bus
->info
->save_request(f
, req
);
1876 if (req
->ops
->save_request
) {
1877 req
->ops
->save_request(f
, req
);
1880 qemu_put_sbyte(f
, 0);
1883 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1886 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1889 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1890 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1895 qemu_get_buffer(f
, buf
, sizeof(buf
));
1896 qemu_get_be32s(f
, &tag
);
1897 qemu_get_be32s(f
, &lun
);
1898 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1899 req
->retry
= (sbyte
== 1);
1900 if (bus
->info
->load_request
) {
1901 req
->hba_private
= bus
->info
->load_request(f
, req
);
1903 if (req
->ops
->load_request
) {
1904 req
->ops
->load_request(f
, req
);
1907 /* Just restart it later. */
1908 scsi_req_enqueue_internal(req
);
1910 /* At this point, the request will be kept alive by the reference
1911 * added by scsi_req_enqueue_internal, so we can release our reference.
1912 * The HBA of course will add its own reference in the load_request
1913 * callback if it needs to hold on the SCSIRequest.
1915 scsi_req_unref(req
);
1921 static int scsi_qdev_unplug(DeviceState
*qdev
)
1923 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1924 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1926 if (bus
->info
->hot_unplug
) {
1927 bus
->info
->hot_unplug(bus
, dev
);
1929 return qdev_simple_unplug_cb(qdev
);
1932 static const VMStateInfo vmstate_info_scsi_requests
= {
1933 .name
= "scsi-requests",
1934 .get
= get_scsi_requests
,
1935 .put
= put_scsi_requests
,
1938 static bool scsi_sense_state_needed(void *opaque
)
1940 SCSIDevice
*s
= opaque
;
1942 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1945 static const VMStateDescription vmstate_scsi_sense_state
= {
1946 .name
= "SCSIDevice/sense",
1948 .minimum_version_id
= 1,
1949 .fields
= (VMStateField
[]) {
1950 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1951 SCSI_SENSE_BUF_SIZE_OLD
,
1952 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1953 VMSTATE_END_OF_LIST()
1957 const VMStateDescription vmstate_scsi_device
= {
1958 .name
= "SCSIDevice",
1960 .minimum_version_id
= 1,
1961 .fields
= (VMStateField
[]) {
1962 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1963 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1964 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1965 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1966 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1967 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1971 .field_exists
= NULL
,
1972 .size
= 0, /* ouch */
1973 .info
= &vmstate_info_scsi_requests
,
1974 .flags
= VMS_SINGLE
,
1977 VMSTATE_END_OF_LIST()
1979 .subsections
= (VMStateSubsection
[]) {
1981 .vmsd
= &vmstate_scsi_sense_state
,
1982 .needed
= scsi_sense_state_needed
,
1989 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1991 DeviceClass
*k
= DEVICE_CLASS(klass
);
1992 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1993 k
->bus_type
= TYPE_SCSI_BUS
;
1994 k
->realize
= scsi_qdev_realize
;
1995 k
->unplug
= scsi_qdev_unplug
;
1996 k
->unrealize
= scsi_qdev_unrealize
;
1997 k
->props
= scsi_props
;
2000 static const TypeInfo scsi_device_type_info
= {
2001 .name
= TYPE_SCSI_DEVICE
,
2002 .parent
= TYPE_DEVICE
,
2003 .instance_size
= sizeof(SCSIDevice
),
2005 .class_size
= sizeof(SCSIDeviceClass
),
2006 .class_init
= scsi_device_class_init
,
2009 static void scsi_register_types(void)
2011 type_register_static(&scsi_bus_info
);
2012 type_register_static(&scsi_device_type_info
);
2015 type_init(scsi_register_types
)