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
);
26 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
28 k
->get_dev_path
= scsibus_get_dev_path
;
29 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
30 hc
->unplug
= qdev_simple_device_unplug_cb
;
33 static const TypeInfo scsi_bus_info
= {
34 .name
= TYPE_SCSI_BUS
,
36 .instance_size
= sizeof(SCSIBus
),
37 .class_init
= scsi_bus_class_init
,
38 .interfaces
= (InterfaceInfo
[]) {
39 { TYPE_HOTPLUG_HANDLER
},
43 static int next_scsi_bus
;
45 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
47 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
53 static void scsi_device_unrealize(SCSIDevice
*s
, Error
**errp
)
55 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
57 sc
->unrealize(s
, errp
);
61 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
64 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
67 assert(cmd
->len
== 0);
68 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
69 if (bus
->info
->parse_cdb
) {
70 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
75 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
76 uint8_t *buf
, void *hba_private
)
78 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
80 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
86 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
88 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
89 if (sc
->unit_attention_reported
) {
90 sc
->unit_attention_reported(s
);
94 /* Create a scsi bus, and attach devices to it. */
95 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
96 const SCSIBusInfo
*info
, const char *bus_name
)
98 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
99 bus
->busnr
= next_scsi_bus
++;
101 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
104 static void scsi_dma_restart_bh(void *opaque
)
106 SCSIDevice
*s
= opaque
;
107 SCSIRequest
*req
, *next
;
109 qemu_bh_delete(s
->bh
);
112 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
116 switch (req
->cmd
.mode
) {
117 case SCSI_XFER_FROM_DEV
:
118 case SCSI_XFER_TO_DEV
:
119 scsi_req_continue(req
);
122 scsi_req_dequeue(req
);
123 scsi_req_enqueue(req
);
131 void scsi_req_retry(SCSIRequest
*req
)
133 /* No need to save a reference, because scsi_dma_restart_bh just
134 * looks at the request list. */
138 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
140 SCSIDevice
*s
= opaque
;
146 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
147 qemu_bh_schedule(s
->bh
);
151 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
153 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
154 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
156 Error
*local_err
= NULL
;
158 if (dev
->channel
> bus
->info
->max_channel
) {
159 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
162 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
163 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
166 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
167 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
173 if (dev
->lun
== -1) {
177 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
178 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
179 if (d
&& d
->lun
== dev
->lun
) {
180 error_setg(errp
, "no free target");
184 } else if (dev
->lun
== -1) {
187 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
188 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
189 if (d
&& d
->lun
== lun
) {
190 error_setg(errp
, "no free lun");
195 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
197 if (d
->lun
== dev
->lun
&& dev
!= d
) {
198 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
203 QTAILQ_INIT(&dev
->requests
);
204 scsi_device_realize(dev
, &local_err
);
206 error_propagate(errp
, local_err
);
209 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
213 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
215 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
218 qemu_del_vm_change_state_handler(dev
->vmsentry
);
220 scsi_device_unrealize(dev
, errp
);
223 /* handle legacy '-drive if=scsi,...' cmd line args */
224 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
225 int unit
, bool removable
, int bootindex
,
226 const char *serial
, Error
**errp
)
232 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
233 dev
= qdev_create(&bus
->qbus
, driver
);
234 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
235 if (bootindex
>= 0) {
236 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
239 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
240 qdev_prop_set_bit(dev
, "removable", removable
);
242 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
243 qdev_prop_set_string(dev
, "serial", serial
);
245 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
246 error_setg(errp
, "Setting drive property failed");
247 object_unparent(OBJECT(dev
));
250 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
252 error_propagate(errp
, err
);
253 object_unparent(OBJECT(dev
));
256 return SCSI_DEVICE(dev
);
259 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
267 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
268 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
272 qemu_opts_loc_restore(dinfo
->opts
);
273 scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1, NULL
,
276 error_report("%s", error_get_pretty(err
));
277 error_propagate(errp
, err
);
284 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
286 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
287 scsi_req_complete(req
, CHECK_CONDITION
);
291 static const struct SCSIReqOps reqops_invalid_field
= {
292 .size
= sizeof(SCSIRequest
),
293 .send_command
= scsi_invalid_field
296 /* SCSIReqOps implementation for invalid commands. */
298 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
300 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
301 scsi_req_complete(req
, CHECK_CONDITION
);
305 static const struct SCSIReqOps reqops_invalid_opcode
= {
306 .size
= sizeof(SCSIRequest
),
307 .send_command
= scsi_invalid_command
310 /* SCSIReqOps implementation for unit attention conditions. */
312 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
314 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
315 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
316 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
317 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
319 scsi_req_complete(req
, CHECK_CONDITION
);
323 static const struct SCSIReqOps reqops_unit_attention
= {
324 .size
= sizeof(SCSIRequest
),
325 .send_command
= scsi_unit_attention
328 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
331 typedef struct SCSITargetReq SCSITargetReq
;
333 struct SCSITargetReq
{
340 static void store_lun(uint8_t *outbuf
, int lun
)
346 outbuf
[1] = (lun
& 255);
347 outbuf
[0] = (lun
>> 8) | 0x40;
350 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
357 if (r
->req
.cmd
.xfer
< 16) {
360 if (r
->req
.cmd
.buf
[2] > 2) {
363 channel
= r
->req
.dev
->channel
;
367 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
368 DeviceState
*qdev
= kid
->child
;
369 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
371 if (dev
->channel
== channel
&& dev
->id
== id
) {
382 scsi_target_alloc_buf(&r
->req
, n
+ 8);
384 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
385 memset(r
->buf
, 0, len
);
386 stl_be_p(&r
->buf
[0], n
);
387 i
= found_lun0
? 8 : 16;
388 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
389 DeviceState
*qdev
= kid
->child
;
390 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
392 if (dev
->channel
== channel
&& dev
->id
== id
) {
393 store_lun(&r
->buf
[i
], dev
->lun
);
402 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
404 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
406 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
408 if (r
->req
.cmd
.buf
[1] & 0x2) {
409 /* Command support data - optional, not implemented */
413 if (r
->req
.cmd
.buf
[1] & 0x1) {
414 /* Vital product data */
415 uint8_t page_code
= r
->req
.cmd
.buf
[2];
416 r
->buf
[r
->len
++] = page_code
; /* this page */
417 r
->buf
[r
->len
++] = 0x00;
420 case 0x00: /* Supported page codes, mandatory */
424 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
425 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
432 assert(r
->len
< r
->buf_len
);
433 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
437 /* Standard INQUIRY data */
438 if (r
->req
.cmd
.buf
[2] != 0) {
443 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
444 memset(r
->buf
, 0, r
->len
);
445 if (r
->req
.lun
!= 0) {
446 r
->buf
[0] = TYPE_NO_LUN
;
448 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
449 r
->buf
[2] = 5; /* Version */
450 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
451 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
452 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
453 memcpy(&r
->buf
[8], "QEMU ", 8);
454 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
455 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
460 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
462 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
466 if (!scsi_target_emulate_report_luns(r
)) {
467 goto illegal_request
;
471 if (!scsi_target_emulate_inquiry(r
)) {
472 goto illegal_request
;
476 scsi_target_alloc_buf(&r
->req
, SCSI_SENSE_LEN
);
477 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
478 MIN(req
->cmd
.xfer
, r
->buf_len
),
479 (req
->cmd
.buf
[1] & 1) == 0);
480 if (r
->req
.dev
->sense_is_ua
) {
481 scsi_device_unit_attention_reported(req
->dev
);
482 r
->req
.dev
->sense_len
= 0;
483 r
->req
.dev
->sense_is_ua
= false;
486 case TEST_UNIT_READY
:
489 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
490 scsi_req_complete(req
, CHECK_CONDITION
);
493 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
494 scsi_req_complete(req
, CHECK_CONDITION
);
499 scsi_req_complete(req
, GOOD
);
504 static void scsi_target_read_data(SCSIRequest
*req
)
506 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
512 scsi_req_data(&r
->req
, n
);
514 scsi_req_complete(&r
->req
, GOOD
);
518 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
520 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
525 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
527 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
529 r
->buf
= g_malloc(len
);
535 static void scsi_target_free_buf(SCSIRequest
*req
)
537 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
542 static const struct SCSIReqOps reqops_target_command
= {
543 .size
= sizeof(SCSITargetReq
),
544 .send_command
= scsi_target_send_command
,
545 .read_data
= scsi_target_read_data
,
546 .get_buf
= scsi_target_get_buf
,
547 .free_req
= scsi_target_free_buf
,
551 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
552 uint32_t tag
, uint32_t lun
, void *hba_private
)
555 SCSIBus
*bus
= scsi_bus_from_device(d
);
556 BusState
*qbus
= BUS(bus
);
557 const int memset_off
= offsetof(SCSIRequest
, sense
)
558 + sizeof(req
->sense
);
560 req
= g_slice_alloc(reqops
->size
);
561 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
567 req
->hba_private
= hba_private
;
570 object_ref(OBJECT(d
));
571 object_ref(OBJECT(qbus
->parent
));
572 notifier_list_init(&req
->cancel_notifiers
);
573 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
577 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
578 uint8_t *buf
, void *hba_private
)
580 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
581 const SCSIReqOps
*ops
;
582 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
584 SCSICommand cmd
= { .len
= 0 };
587 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
588 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
589 (buf
[0] != INQUIRY
&&
590 buf
[0] != REPORT_LUNS
&&
591 buf
[0] != GET_CONFIGURATION
&&
592 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
595 * If we already have a pending unit attention condition,
596 * report this one before triggering another one.
598 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
599 ops
= &reqops_unit_attention
;
600 } else if (lun
!= d
->lun
||
601 buf
[0] == REPORT_LUNS
||
602 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
603 ops
= &reqops_target_command
;
608 if (ops
!= NULL
|| !sc
->parse_cdb
) {
609 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
611 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
615 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
616 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
618 assert(cmd
.len
!= 0);
619 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
622 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
626 if (cmd
.xfer
> INT32_MAX
) {
627 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
629 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
631 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
636 req
->resid
= req
->cmd
.xfer
;
640 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
642 case TEST_UNIT_READY
:
643 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
646 trace_scsi_report_luns(d
->id
, lun
, tag
);
649 trace_scsi_request_sense(d
->id
, lun
, tag
);
658 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
660 return req
->ops
->get_buf(req
);
663 static void scsi_clear_unit_attention(SCSIRequest
*req
)
666 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
667 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
672 * If an INQUIRY command enters the enabled command state,
673 * the device server shall [not] clear any unit attention condition;
674 * See also MMC-6, paragraphs 6.5 and 6.6.2.
676 if (req
->cmd
.buf
[0] == INQUIRY
||
677 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
678 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
682 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
683 ua
= &req
->dev
->unit_attention
;
685 ua
= &req
->bus
->unit_attention
;
689 * If a REPORT LUNS command enters the enabled command state, [...]
690 * the device server shall clear any pending unit attention condition
691 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
693 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
694 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
695 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
699 *ua
= SENSE_CODE(NO_SENSE
);
702 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
707 if (!req
->sense_len
) {
711 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
714 * FIXME: clearing unit attention conditions upon autosense should be done
715 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
718 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
719 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
720 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
722 if (req
->dev
->sense_is_ua
) {
723 scsi_device_unit_attention_reported(req
->dev
);
724 req
->dev
->sense_len
= 0;
725 req
->dev
->sense_is_ua
= false;
730 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
732 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
735 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
737 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
738 sense
.key
, sense
.asc
, sense
.ascq
);
739 memset(req
->sense
, 0, 18);
740 req
->sense
[0] = 0x70;
741 req
->sense
[2] = sense
.key
;
743 req
->sense
[12] = sense
.asc
;
744 req
->sense
[13] = sense
.ascq
;
748 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
750 assert(!req
->enqueued
);
752 if (req
->bus
->info
->get_sg_list
) {
753 req
->sg
= req
->bus
->info
->get_sg_list(req
);
757 req
->enqueued
= true;
758 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
761 int32_t scsi_req_enqueue(SCSIRequest
*req
)
766 scsi_req_enqueue_internal(req
);
768 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
773 static void scsi_req_dequeue(SCSIRequest
*req
)
775 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
778 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
779 req
->enqueued
= false;
784 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
786 /* MMC-6, paragraph 6.7. */
789 if ((data_type
& 3) == 0) {
790 /* Each descriptor is as in Table 295 - Nominal performance. */
791 return 16 * num_desc
+ 8;
793 /* Each descriptor is as in Table 296 - Exceptions. */
794 return 6 * num_desc
+ 8;
799 return 8 * num_desc
+ 8;
801 return 2048 * num_desc
+ 8;
803 return 16 * num_desc
+ 8;
809 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
811 int byte_block
= (buf
[2] >> 2) & 0x1;
812 int type
= (buf
[2] >> 4) & 0x1;
817 xfer_unit
= dev
->blocksize
;
828 static int ata_passthrough_12_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
830 int length
= buf
[2] & 0x3;
832 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
836 case 3: /* USB-specific. */
851 static int ata_passthrough_16_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
853 int extend
= buf
[1] & 0x1;
854 int length
= buf
[2] & 0x3;
856 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
860 case 3: /* USB-specific. */
866 xfer
|= (extend
? buf
[3] << 8 : 0);
870 xfer
|= (extend
? buf
[5] << 8 : 0);
877 uint32_t scsi_data_cdb_length(uint8_t *buf
)
879 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
882 return scsi_cdb_length(buf
);
886 uint32_t scsi_cdb_length(uint8_t *buf
)
888 switch (buf
[0] >> 5) {
894 return lduw_be_p(&buf
[7]);
897 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
900 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
907 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
909 cmd
->xfer
= scsi_cdb_length(buf
);
911 case TEST_UNIT_READY
:
915 case WRITE_FILEMARKS
:
916 case WRITE_FILEMARKS_16
:
921 case ALLOW_MEDIUM_REMOVAL
:
923 case SYNCHRONIZE_CACHE
:
924 case SYNCHRONIZE_CACHE_16
:
926 case LOCK_UNLOCK_CACHE
:
935 case ALLOW_OVERWRITE
:
941 if ((buf
[1] & 2) == 0) {
943 } else if ((buf
[1] & 4) != 0) {
946 cmd
->xfer
*= dev
->blocksize
;
952 cmd
->xfer
= dev
->blocksize
;
954 case READ_CAPACITY_10
:
957 case READ_BLOCK_LIMITS
:
960 case SEND_VOLUME_TAG
:
961 /* GPCMD_SET_STREAMING from multimedia commands. */
962 if (dev
->type
== TYPE_ROM
) {
963 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
965 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
969 /* length 0 means 256 blocks */
970 if (cmd
->xfer
== 0) {
975 case WRITE_VERIFY_10
:
977 case WRITE_VERIFY_12
:
979 case WRITE_VERIFY_16
:
980 cmd
->xfer
*= dev
->blocksize
;
984 /* length 0 means 256 blocks */
985 if (cmd
->xfer
== 0) {
990 case RECOVER_BUFFERED_DATA
:
993 cmd
->xfer
*= dev
->blocksize
;
996 /* MMC mandates the parameter list to be 12-bytes long. Parameters
997 * for block devices are restricted to the header right now. */
998 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1001 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1005 case RECEIVE_DIAGNOSTIC
:
1006 case SEND_DIAGNOSTIC
:
1007 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1012 case SEND_CUE_SHEET
:
1013 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1015 case PERSISTENT_RESERVE_OUT
:
1016 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1019 if (dev
->type
== TYPE_ROM
) {
1020 /* MMC command GET PERFORMANCE. */
1021 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1022 buf
[10], buf
[1] & 0x1f);
1025 case MECHANISM_STATUS
:
1026 case READ_DVD_STRUCTURE
:
1027 case SEND_DVD_STRUCTURE
:
1028 case MAINTENANCE_OUT
:
1029 case MAINTENANCE_IN
:
1030 if (dev
->type
== TYPE_ROM
) {
1031 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1032 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1035 case ATA_PASSTHROUGH_12
:
1036 if (dev
->type
== TYPE_ROM
) {
1037 /* BLANK command of MMC */
1040 cmd
->xfer
= ata_passthrough_12_xfer_size(dev
, buf
);
1043 case ATA_PASSTHROUGH_16
:
1044 cmd
->xfer
= ata_passthrough_16_xfer_size(dev
, buf
);
1050 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1053 /* stream commands */
1060 case RECOVER_BUFFERED_DATA
:
1062 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1063 if (buf
[1] & 0x01) { /* fixed */
1064 cmd
->xfer
*= dev
->blocksize
;
1068 case READ_REVERSE_16
:
1071 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1072 if (buf
[1] & 0x01) { /* fixed */
1073 cmd
->xfer
*= dev
->blocksize
;
1081 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1084 switch (buf
[1] & 0x1f) /* operation code */ {
1085 case SHORT_FORM_BLOCK_ID
:
1086 case SHORT_FORM_VENDOR_SPECIFIC
:
1093 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1101 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1103 /* generic commands */
1105 return scsi_req_length(cmd
, dev
, buf
);
1110 static int scsi_req_medium_changer_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1113 /* medium changer commands */
1114 case EXCHANGE_MEDIUM
:
1115 case INITIALIZE_ELEMENT_STATUS
:
1116 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1118 case POSITION_TO_ELEMENT
:
1121 case READ_ELEMENT_STATUS
:
1122 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1125 /* generic commands */
1127 return scsi_req_length(cmd
, dev
, buf
);
1133 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1136 cmd
->mode
= SCSI_XFER_NONE
;
1139 switch (cmd
->buf
[0]) {
1142 case WRITE_VERIFY_10
:
1144 case WRITE_VERIFY_12
:
1146 case WRITE_VERIFY_16
:
1153 case CHANGE_DEFINITION
:
1156 case MODE_SELECT_10
:
1157 case SEND_DIAGNOSTIC
:
1160 case REASSIGN_BLOCKS
:
1169 case SEARCH_HIGH_12
:
1170 case SEARCH_EQUAL_12
:
1173 case SEND_VOLUME_TAG
:
1174 case SEND_CUE_SHEET
:
1175 case SEND_DVD_STRUCTURE
:
1176 case PERSISTENT_RESERVE_OUT
:
1177 case MAINTENANCE_OUT
:
1178 cmd
->mode
= SCSI_XFER_TO_DEV
;
1180 case ATA_PASSTHROUGH_12
:
1181 case ATA_PASSTHROUGH_16
:
1183 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1184 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1187 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1192 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1194 uint8_t *buf
= cmd
->buf
;
1197 switch (buf
[0] >> 5) {
1199 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1204 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1207 lba
= ldq_be_p(&buf
[2]);
1216 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1221 switch (buf
[0] >> 5) {
1239 switch (dev
->type
) {
1241 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1243 case TYPE_MEDIUM_CHANGER
:
1244 rc
= scsi_req_medium_changer_length(cmd
, dev
, buf
);
1247 rc
= scsi_req_length(cmd
, dev
, buf
);
1254 memcpy(cmd
->buf
, buf
, cmd
->len
);
1255 scsi_cmd_xfer_mode(cmd
);
1256 cmd
->lba
= scsi_cmd_lba(cmd
);
1260 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1262 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1264 scsi_device_set_ua(dev
, sense
);
1265 if (bus
->info
->change
) {
1266 bus
->info
->change(bus
, dev
, sense
);
1271 * Predefined sense codes
1274 /* No sense data available */
1275 const struct SCSISense sense_code_NO_SENSE
= {
1276 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1279 /* LUN not ready, Manual intervention required */
1280 const struct SCSISense sense_code_LUN_NOT_READY
= {
1281 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1284 /* LUN not ready, Medium not present */
1285 const struct SCSISense sense_code_NO_MEDIUM
= {
1286 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1289 /* LUN not ready, medium removal prevented */
1290 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1291 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1294 /* Hardware error, internal target failure */
1295 const struct SCSISense sense_code_TARGET_FAILURE
= {
1296 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1299 /* Illegal request, invalid command operation code */
1300 const struct SCSISense sense_code_INVALID_OPCODE
= {
1301 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1304 /* Illegal request, LBA out of range */
1305 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1306 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1309 /* Illegal request, Invalid field in CDB */
1310 const struct SCSISense sense_code_INVALID_FIELD
= {
1311 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1314 /* Illegal request, Invalid field in parameter list */
1315 const struct SCSISense sense_code_INVALID_PARAM
= {
1316 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1319 /* Illegal request, Parameter list length error */
1320 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1321 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1324 /* Illegal request, LUN not supported */
1325 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1326 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1329 /* Illegal request, Saving parameters not supported */
1330 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1331 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1334 /* Illegal request, Incompatible medium installed */
1335 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1336 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1339 /* Illegal request, medium removal prevented */
1340 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1341 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1344 /* Illegal request, Invalid Transfer Tag */
1345 const struct SCSISense sense_code_INVALID_TAG
= {
1346 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1349 /* Command aborted, I/O process terminated */
1350 const struct SCSISense sense_code_IO_ERROR
= {
1351 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1354 /* Command aborted, I_T Nexus loss occurred */
1355 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1356 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1359 /* Command aborted, Logical Unit failure */
1360 const struct SCSISense sense_code_LUN_FAILURE
= {
1361 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1364 /* Command aborted, Overlapped Commands Attempted */
1365 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1366 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1369 /* Unit attention, Capacity data has changed */
1370 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1371 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1374 /* Unit attention, Power on, reset or bus device reset occurred */
1375 const struct SCSISense sense_code_RESET
= {
1376 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1379 /* Unit attention, No medium */
1380 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1381 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1384 /* Unit attention, Medium may have changed */
1385 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1386 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1389 /* Unit attention, Reported LUNs data has changed */
1390 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1391 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1394 /* Unit attention, Device internal reset */
1395 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1396 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1399 /* Data Protection, Write Protected */
1400 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1401 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1404 /* Data Protection, Space Allocation Failed Write Protect */
1405 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1406 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1412 * Convert between fixed and descriptor sense buffers
1414 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1415 uint8_t *buf
, int len
, bool fixed
)
1419 if (!fixed
&& len
< 8) {
1424 sense
.key
= NO_SENSE
;
1428 fixed_in
= (in_buf
[0] & 2) == 0;
1430 if (fixed
== fixed_in
) {
1431 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1432 return MIN(len
, in_len
);
1436 sense
.key
= in_buf
[2];
1437 sense
.asc
= in_buf
[12];
1438 sense
.ascq
= in_buf
[13];
1440 sense
.key
= in_buf
[1];
1441 sense
.asc
= in_buf
[2];
1442 sense
.ascq
= in_buf
[3];
1446 memset(buf
, 0, len
);
1448 /* Return fixed format sense buffer */
1452 buf
[12] = sense
.asc
;
1453 buf
[13] = sense
.ascq
;
1454 return MIN(len
, SCSI_SENSE_LEN
);
1456 /* Return descriptor format sense buffer */
1460 buf
[3] = sense
.ascq
;
1465 const char *scsi_command_name(uint8_t cmd
)
1467 static const char *names
[] = {
1468 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1469 [ REWIND
] = "REWIND",
1470 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1471 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1472 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1473 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1474 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1475 [ READ_6
] = "READ_6",
1476 [ WRITE_6
] = "WRITE_6",
1477 [ SET_CAPACITY
] = "SET_CAPACITY",
1478 [ READ_REVERSE
] = "READ_REVERSE",
1479 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1480 [ SPACE
] = "SPACE",
1481 [ INQUIRY
] = "INQUIRY",
1482 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1483 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1484 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1485 [ MODE_SELECT
] = "MODE_SELECT",
1486 [ RESERVE
] = "RESERVE",
1487 [ RELEASE
] = "RELEASE",
1489 [ ERASE
] = "ERASE",
1490 [ MODE_SENSE
] = "MODE_SENSE",
1491 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1492 /* LOAD_UNLOAD and START_STOP use the same operation code */
1493 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1494 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1495 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1496 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1497 [ READ_10
] = "READ_10",
1498 [ WRITE_10
] = "WRITE_10",
1499 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1500 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1501 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1502 [ VERIFY_10
] = "VERIFY_10",
1503 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1504 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1505 [ SEARCH_LOW
] = "SEARCH_LOW",
1506 [ SET_LIMITS
] = "SET_LIMITS",
1507 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1508 /* READ_POSITION and PRE_FETCH use the same operation code */
1509 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1510 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1511 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1512 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1513 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1514 [ COMPARE
] = "COMPARE",
1515 [ COPY_VERIFY
] = "COPY_VERIFY",
1516 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1517 [ READ_BUFFER
] = "READ_BUFFER",
1518 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1519 [ READ_LONG_10
] = "READ_LONG_10",
1520 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1521 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1522 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1523 [ UNMAP
] = "UNMAP",
1524 [ READ_TOC
] = "READ_TOC",
1525 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1526 [ SANITIZE
] = "SANITIZE",
1527 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1528 [ LOG_SELECT
] = "LOG_SELECT",
1529 [ LOG_SENSE
] = "LOG_SENSE",
1530 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1531 [ RESERVE_10
] = "RESERVE_10",
1532 [ RELEASE_10
] = "RELEASE_10",
1533 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1534 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1535 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1536 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1537 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1538 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1539 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1540 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1541 [ READ_16
] = "READ_16",
1542 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1543 [ WRITE_16
] = "WRITE_16",
1544 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1545 [ VERIFY_16
] = "VERIFY_16",
1546 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1547 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1548 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1549 [ LOCATE_16
] = "LOCATE_16",
1550 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1551 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1552 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1553 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1554 [ REPORT_LUNS
] = "REPORT_LUNS",
1555 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1556 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1557 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1558 [ READ_12
] = "READ_12",
1559 [ WRITE_12
] = "WRITE_12",
1560 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1561 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1562 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1563 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1564 [ VERIFY_12
] = "VERIFY_12",
1565 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1566 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1567 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1568 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1569 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1570 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1571 [ READ_CD
] = "READ_CD",
1572 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1573 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1574 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1575 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1576 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1577 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1578 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1579 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1580 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1581 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1582 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1585 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1590 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1592 assert(req
->refcount
> 0);
1597 void scsi_req_unref(SCSIRequest
*req
)
1599 assert(req
->refcount
> 0);
1600 if (--req
->refcount
== 0) {
1601 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1602 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1604 if (bus
->info
->free_request
&& req
->hba_private
) {
1605 bus
->info
->free_request(bus
, req
->hba_private
);
1607 if (req
->ops
->free_req
) {
1608 req
->ops
->free_req(req
);
1610 object_unref(OBJECT(req
->dev
));
1611 object_unref(OBJECT(qbus
->parent
));
1612 g_slice_free1(req
->ops
->size
, req
);
1616 /* Tell the device that we finished processing this chunk of I/O. It
1617 will start the next chunk or complete the command. */
1618 void scsi_req_continue(SCSIRequest
*req
)
1620 if (req
->io_canceled
) {
1621 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1624 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1625 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1626 req
->ops
->write_data(req
);
1628 req
->ops
->read_data(req
);
1632 /* Called by the devices when data is ready for the HBA. The HBA should
1633 start a DMA operation to read or fill the device's data buffer.
1634 Once it completes, calling scsi_req_continue will restart I/O. */
1635 void scsi_req_data(SCSIRequest
*req
, int len
)
1638 if (req
->io_canceled
) {
1639 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1642 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1643 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1646 req
->bus
->info
->transfer_data(req
, len
);
1650 /* If the device calls scsi_req_data and the HBA specified a
1651 * scatter/gather list, the transfer has to happen in a single
1653 assert(!req
->dma_started
);
1654 req
->dma_started
= true;
1656 buf
= scsi_req_get_buf(req
);
1657 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1658 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1660 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1662 scsi_req_continue(req
);
1665 void scsi_req_print(SCSIRequest
*req
)
1670 fprintf(fp
, "[%s id=%d] %s",
1671 req
->dev
->qdev
.parent_bus
->name
,
1673 scsi_command_name(req
->cmd
.buf
[0]));
1674 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1675 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1677 switch (req
->cmd
.mode
) {
1678 case SCSI_XFER_NONE
:
1679 fprintf(fp
, " - none\n");
1681 case SCSI_XFER_FROM_DEV
:
1682 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1684 case SCSI_XFER_TO_DEV
:
1685 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1688 fprintf(fp
, " - Oops\n");
1693 void scsi_req_complete(SCSIRequest
*req
, int status
)
1695 assert(req
->status
== -1);
1696 req
->status
= status
;
1698 assert(req
->sense_len
<= sizeof(req
->sense
));
1699 if (status
== GOOD
) {
1703 if (req
->sense_len
) {
1704 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1705 req
->dev
->sense_len
= req
->sense_len
;
1706 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1708 req
->dev
->sense_len
= 0;
1709 req
->dev
->sense_is_ua
= false;
1713 * Unit attention state is now stored in the device's sense buffer
1714 * if the HBA didn't do autosense. Clear the pending unit attention
1717 scsi_clear_unit_attention(req
);
1720 scsi_req_dequeue(req
);
1721 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1723 /* Cancelled requests might end up being completed instead of cancelled */
1724 notifier_list_notify(&req
->cancel_notifiers
, req
);
1725 scsi_req_unref(req
);
1728 /* Called by the devices when the request is canceled. */
1729 void scsi_req_cancel_complete(SCSIRequest
*req
)
1731 assert(req
->io_canceled
);
1732 if (req
->bus
->info
->cancel
) {
1733 req
->bus
->info
->cancel(req
);
1735 notifier_list_notify(&req
->cancel_notifiers
, req
);
1736 scsi_req_unref(req
);
1739 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1740 * notifier list, the bus will be notified the requests cancellation is
1743 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1745 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1747 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1749 if (req
->io_canceled
) {
1753 scsi_req_dequeue(req
);
1754 req
->io_canceled
= true;
1756 bdrv_aio_cancel_async(req
->aiocb
);
1760 void scsi_req_cancel(SCSIRequest
*req
)
1762 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1763 if (!req
->enqueued
) {
1767 scsi_req_dequeue(req
);
1768 req
->io_canceled
= true;
1770 bdrv_aio_cancel(req
->aiocb
);
1774 static int scsi_ua_precedence(SCSISense sense
)
1776 if (sense
.key
!= UNIT_ATTENTION
) {
1779 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1780 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1782 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1783 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1785 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1786 /* These two go with "all others". */
1788 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1789 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1790 * POWER ON OCCURRED = 1
1791 * SCSI BUS RESET OCCURRED = 2
1792 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1793 * I_T NEXUS LOSS OCCURRED = 7
1796 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1797 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1800 return (sense
.asc
<< 8) | sense
.ascq
;
1803 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1806 if (sense
.key
!= UNIT_ATTENTION
) {
1809 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1810 sense
.asc
, sense
.ascq
);
1813 * Override a pre-existing unit attention condition, except for a more
1814 * important reset condition.
1816 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1817 prec2
= scsi_ua_precedence(sense
);
1818 if (prec2
< prec1
) {
1819 sdev
->unit_attention
= sense
;
1823 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1827 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1828 req
= QTAILQ_FIRST(&sdev
->requests
);
1829 scsi_req_cancel(req
);
1832 scsi_device_set_ua(sdev
, sense
);
1835 static char *scsibus_get_dev_path(DeviceState
*dev
)
1837 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1838 DeviceState
*hba
= dev
->parent_bus
->parent
;
1842 id
= qdev_get_dev_path(hba
);
1844 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1846 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1852 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1854 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1855 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1856 qdev_fw_name(dev
), d
->id
, d
->lun
);
1859 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1862 SCSIDevice
*target_dev
= NULL
;
1864 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1865 DeviceState
*qdev
= kid
->child
;
1866 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1868 if (dev
->channel
== channel
&& dev
->id
== id
) {
1869 if (dev
->lun
== lun
) {
1878 /* SCSI request list. For simplicity, pv points to the whole device */
1880 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1883 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1886 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1887 assert(!req
->io_canceled
);
1888 assert(req
->status
== -1);
1889 assert(req
->enqueued
);
1891 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1892 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1893 qemu_put_be32s(f
, &req
->tag
);
1894 qemu_put_be32s(f
, &req
->lun
);
1895 if (bus
->info
->save_request
) {
1896 bus
->info
->save_request(f
, req
);
1898 if (req
->ops
->save_request
) {
1899 req
->ops
->save_request(f
, req
);
1902 qemu_put_sbyte(f
, 0);
1905 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1908 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1911 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1912 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1917 qemu_get_buffer(f
, buf
, sizeof(buf
));
1918 qemu_get_be32s(f
, &tag
);
1919 qemu_get_be32s(f
, &lun
);
1920 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1921 req
->retry
= (sbyte
== 1);
1922 if (bus
->info
->load_request
) {
1923 req
->hba_private
= bus
->info
->load_request(f
, req
);
1925 if (req
->ops
->load_request
) {
1926 req
->ops
->load_request(f
, req
);
1929 /* Just restart it later. */
1930 scsi_req_enqueue_internal(req
);
1932 /* At this point, the request will be kept alive by the reference
1933 * added by scsi_req_enqueue_internal, so we can release our reference.
1934 * The HBA of course will add its own reference in the load_request
1935 * callback if it needs to hold on the SCSIRequest.
1937 scsi_req_unref(req
);
1943 static const VMStateInfo vmstate_info_scsi_requests
= {
1944 .name
= "scsi-requests",
1945 .get
= get_scsi_requests
,
1946 .put
= put_scsi_requests
,
1949 static bool scsi_sense_state_needed(void *opaque
)
1951 SCSIDevice
*s
= opaque
;
1953 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1956 static const VMStateDescription vmstate_scsi_sense_state
= {
1957 .name
= "SCSIDevice/sense",
1959 .minimum_version_id
= 1,
1960 .fields
= (VMStateField
[]) {
1961 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1962 SCSI_SENSE_BUF_SIZE_OLD
,
1963 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1964 VMSTATE_END_OF_LIST()
1968 const VMStateDescription vmstate_scsi_device
= {
1969 .name
= "SCSIDevice",
1971 .minimum_version_id
= 1,
1972 .fields
= (VMStateField
[]) {
1973 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1974 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1975 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1976 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1977 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1978 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1982 .field_exists
= NULL
,
1983 .size
= 0, /* ouch */
1984 .info
= &vmstate_info_scsi_requests
,
1985 .flags
= VMS_SINGLE
,
1988 VMSTATE_END_OF_LIST()
1990 .subsections
= (VMStateSubsection
[]) {
1992 .vmsd
= &vmstate_scsi_sense_state
,
1993 .needed
= scsi_sense_state_needed
,
2000 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
2002 DeviceClass
*k
= DEVICE_CLASS(klass
);
2003 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
2004 k
->bus_type
= TYPE_SCSI_BUS
;
2005 k
->realize
= scsi_qdev_realize
;
2006 k
->unrealize
= scsi_qdev_unrealize
;
2007 k
->props
= scsi_props
;
2010 static void scsi_dev_instance_init(Object
*obj
)
2012 DeviceState
*dev
= DEVICE(obj
);
2013 SCSIDevice
*s
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
2015 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
2020 static const TypeInfo scsi_device_type_info
= {
2021 .name
= TYPE_SCSI_DEVICE
,
2022 .parent
= TYPE_DEVICE
,
2023 .instance_size
= sizeof(SCSIDevice
),
2025 .class_size
= sizeof(SCSIDeviceClass
),
2026 .class_init
= scsi_device_class_init
,
2027 .instance_init
= scsi_dev_instance_init
,
2030 static void scsi_register_types(void)
2032 type_register_static(&scsi_bus_info
);
2033 type_register_static(&scsi_device_type_info
);
2036 type_init(scsi_register_types
)