1 #include "qemu/osdep.h"
3 #include "qapi/error.h"
4 #include "qemu/error-report.h"
5 #include "hw/scsi/scsi.h"
6 #include "block/scsi.h"
8 #include "sysemu/block-backend.h"
9 #include "sysemu/blockdev.h"
11 #include "sysemu/dma.h"
12 #include "qemu/cutils.h"
14 static char *scsibus_get_dev_path(DeviceState
*dev
);
15 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
16 static void scsi_req_dequeue(SCSIRequest
*req
);
17 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
18 static void scsi_target_free_buf(SCSIRequest
*req
);
20 static Property scsi_props
[] = {
21 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
22 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
23 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
24 DEFINE_PROP_END_OF_LIST(),
27 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
29 BusClass
*k
= BUS_CLASS(klass
);
30 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
32 k
->get_dev_path
= scsibus_get_dev_path
;
33 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
34 hc
->unplug
= qdev_simple_device_unplug_cb
;
37 static const TypeInfo scsi_bus_info
= {
38 .name
= TYPE_SCSI_BUS
,
40 .instance_size
= sizeof(SCSIBus
),
41 .class_init
= scsi_bus_class_init
,
42 .interfaces
= (InterfaceInfo
[]) {
43 { TYPE_HOTPLUG_HANDLER
},
47 static int next_scsi_bus
;
49 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
51 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
57 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
60 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
63 assert(cmd
->len
== 0);
64 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
);
65 if (bus
->info
->parse_cdb
) {
66 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, hba_private
);
71 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
72 uint8_t *buf
, void *hba_private
)
74 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
76 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
82 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
84 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
85 if (sc
->unit_attention_reported
) {
86 sc
->unit_attention_reported(s
);
90 /* Create a scsi bus, and attach devices to it. */
91 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
92 const SCSIBusInfo
*info
, const char *bus_name
)
94 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
95 bus
->busnr
= next_scsi_bus
++;
97 qbus_set_bus_hotplug_handler(BUS(bus
), &error_abort
);
100 static void scsi_dma_restart_bh(void *opaque
)
102 SCSIDevice
*s
= opaque
;
103 SCSIRequest
*req
, *next
;
105 qemu_bh_delete(s
->bh
);
108 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
109 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
113 switch (req
->cmd
.mode
) {
114 case SCSI_XFER_FROM_DEV
:
115 case SCSI_XFER_TO_DEV
:
116 scsi_req_continue(req
);
119 scsi_req_dequeue(req
);
120 scsi_req_enqueue(req
);
126 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
129 void scsi_req_retry(SCSIRequest
*req
)
131 /* No need to save a reference, because scsi_dma_restart_bh just
132 * looks at the request list. */
136 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
138 SCSIDevice
*s
= opaque
;
144 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
145 s
->bh
= aio_bh_new(ctx
, scsi_dma_restart_bh
, s
);
146 qemu_bh_schedule(s
->bh
);
150 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
152 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
153 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
155 Error
*local_err
= NULL
;
157 if (dev
->channel
> bus
->info
->max_channel
) {
158 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
161 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
162 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
165 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
166 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
172 if (dev
->lun
== -1) {
176 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
177 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
178 if (d
&& d
->lun
== dev
->lun
) {
179 error_setg(errp
, "no free target");
183 } else if (dev
->lun
== -1) {
186 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
187 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
188 if (d
&& d
->lun
== lun
) {
189 error_setg(errp
, "no free lun");
194 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
196 if (d
->lun
== dev
->lun
&& dev
!= d
) {
197 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
202 QTAILQ_INIT(&dev
->requests
);
203 scsi_device_realize(dev
, &local_err
);
205 error_propagate(errp
, local_err
);
208 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
212 static void scsi_qdev_unrealize(DeviceState
*qdev
, Error
**errp
)
214 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
217 qemu_del_vm_change_state_handler(dev
->vmsentry
);
220 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
221 blockdev_mark_auto_del(dev
->conf
.blk
);
224 /* handle legacy '-drive if=scsi,...' cmd line args */
225 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
226 int unit
, bool removable
, int bootindex
,
227 const char *serial
, Error
**errp
)
234 driver
= blk_is_sg(blk
) ? "scsi-generic" : "scsi-disk";
235 dev
= qdev_create(&bus
->qbus
, driver
);
236 name
= g_strdup_printf("legacy[%d]", unit
);
237 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
), NULL
);
240 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
241 if (bootindex
>= 0) {
242 object_property_set_int(OBJECT(dev
), bootindex
, "bootindex",
245 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
246 qdev_prop_set_bit(dev
, "removable", removable
);
248 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
249 qdev_prop_set_string(dev
, "serial", serial
);
251 qdev_prop_set_drive(dev
, "drive", blk
, &err
);
253 error_propagate(errp
, err
);
254 object_unparent(OBJECT(dev
));
257 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
259 error_propagate(errp
, err
);
260 object_unparent(OBJECT(dev
));
263 return SCSI_DEVICE(dev
);
266 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, bool deprecated
)
273 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
274 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
278 qemu_opts_loc_restore(dinfo
->opts
);
280 /* Handling -drive not claimed by machine initialization */
281 if (blk_get_attached_dev(blk_by_legacy_dinfo(dinfo
))) {
282 continue; /* claimed */
284 if (!dinfo
->is_default
) {
285 error_report("warning: bus=%d,unit=%d is deprecated with this"
290 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
291 unit
, false, -1, NULL
, &error_fatal
);
296 static bool is_scsi_hba_with_legacy_magic(Object
*obj
)
298 static const char *magic
[] = {
299 "am53c974", "dc390", "esp", "lsi53c810", "lsi53c895a",
300 "megasas", "megasas-gen2", "mptsas1068", "spapr-vscsi",
301 "virtio-scsi-device",
304 const char *typename
= object_get_typename(obj
);
307 for (i
= 0; magic
[i
]; i
++)
308 if (!strcmp(typename
, magic
[i
])) {
315 static int scsi_legacy_handle_cmdline_cb(Object
*obj
, void *opaque
)
317 SCSIBus
*bus
= (SCSIBus
*)object_dynamic_cast(obj
, TYPE_SCSI_BUS
);
319 if (bus
&& is_scsi_hba_with_legacy_magic(OBJECT(bus
->qbus
.parent
))) {
320 scsi_bus_legacy_handle_cmdline(bus
, true);
326 void scsi_legacy_handle_cmdline(void)
328 object_child_foreach_recursive(object_get_root(),
329 scsi_legacy_handle_cmdline_cb
, NULL
);
332 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
334 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
335 scsi_req_complete(req
, CHECK_CONDITION
);
339 static const struct SCSIReqOps reqops_invalid_field
= {
340 .size
= sizeof(SCSIRequest
),
341 .send_command
= scsi_invalid_field
344 /* SCSIReqOps implementation for invalid commands. */
346 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
348 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
349 scsi_req_complete(req
, CHECK_CONDITION
);
353 static const struct SCSIReqOps reqops_invalid_opcode
= {
354 .size
= sizeof(SCSIRequest
),
355 .send_command
= scsi_invalid_command
358 /* SCSIReqOps implementation for unit attention conditions. */
360 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
362 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
363 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
364 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
365 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
367 scsi_req_complete(req
, CHECK_CONDITION
);
371 static const struct SCSIReqOps reqops_unit_attention
= {
372 .size
= sizeof(SCSIRequest
),
373 .send_command
= scsi_unit_attention
376 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
379 typedef struct SCSITargetReq SCSITargetReq
;
381 struct SCSITargetReq
{
388 static void store_lun(uint8_t *outbuf
, int lun
)
394 outbuf
[1] = (lun
& 255);
395 outbuf
[0] = (lun
>> 8) | 0x40;
398 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
405 if (r
->req
.cmd
.xfer
< 16) {
408 if (r
->req
.cmd
.buf
[2] > 2) {
411 channel
= r
->req
.dev
->channel
;
415 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
416 DeviceState
*qdev
= kid
->child
;
417 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
419 if (dev
->channel
== channel
&& dev
->id
== id
) {
430 scsi_target_alloc_buf(&r
->req
, n
+ 8);
432 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
433 memset(r
->buf
, 0, len
);
434 stl_be_p(&r
->buf
[0], n
);
435 i
= found_lun0
? 8 : 16;
436 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
437 DeviceState
*qdev
= kid
->child
;
438 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
440 if (dev
->channel
== channel
&& dev
->id
== id
) {
441 store_lun(&r
->buf
[i
], dev
->lun
);
450 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
452 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
454 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
456 if (r
->req
.cmd
.buf
[1] & 0x2) {
457 /* Command support data - optional, not implemented */
461 if (r
->req
.cmd
.buf
[1] & 0x1) {
462 /* Vital product data */
463 uint8_t page_code
= r
->req
.cmd
.buf
[2];
464 r
->buf
[r
->len
++] = page_code
; /* this page */
465 r
->buf
[r
->len
++] = 0x00;
468 case 0x00: /* Supported page codes, mandatory */
472 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
473 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
480 assert(r
->len
< r
->buf_len
);
481 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
485 /* Standard INQUIRY data */
486 if (r
->req
.cmd
.buf
[2] != 0) {
491 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
492 memset(r
->buf
, 0, r
->len
);
493 if (r
->req
.lun
!= 0) {
494 r
->buf
[0] = TYPE_NO_LUN
;
496 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
497 r
->buf
[2] = 5; /* Version */
498 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
499 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
500 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
501 memcpy(&r
->buf
[8], "QEMU ", 8);
502 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
503 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
508 static size_t scsi_sense_len(SCSIRequest
*req
)
510 if (req
->dev
->type
== TYPE_SCANNER
)
511 return SCSI_SENSE_LEN_SCANNER
;
513 return SCSI_SENSE_LEN
;
516 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
518 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
522 if (!scsi_target_emulate_report_luns(r
)) {
523 goto illegal_request
;
527 if (!scsi_target_emulate_inquiry(r
)) {
528 goto illegal_request
;
532 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
533 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
534 MIN(req
->cmd
.xfer
, r
->buf_len
),
535 (req
->cmd
.buf
[1] & 1) == 0);
536 if (r
->req
.dev
->sense_is_ua
) {
537 scsi_device_unit_attention_reported(req
->dev
);
538 r
->req
.dev
->sense_len
= 0;
539 r
->req
.dev
->sense_is_ua
= false;
542 case TEST_UNIT_READY
:
545 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
546 scsi_req_complete(req
, CHECK_CONDITION
);
549 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
550 scsi_req_complete(req
, CHECK_CONDITION
);
555 scsi_req_complete(req
, GOOD
);
560 static void scsi_target_read_data(SCSIRequest
*req
)
562 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
568 scsi_req_data(&r
->req
, n
);
570 scsi_req_complete(&r
->req
, GOOD
);
574 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
576 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
581 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
583 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
585 r
->buf
= g_malloc(len
);
591 static void scsi_target_free_buf(SCSIRequest
*req
)
593 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
598 static const struct SCSIReqOps reqops_target_command
= {
599 .size
= sizeof(SCSITargetReq
),
600 .send_command
= scsi_target_send_command
,
601 .read_data
= scsi_target_read_data
,
602 .get_buf
= scsi_target_get_buf
,
603 .free_req
= scsi_target_free_buf
,
607 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
608 uint32_t tag
, uint32_t lun
, void *hba_private
)
611 SCSIBus
*bus
= scsi_bus_from_device(d
);
612 BusState
*qbus
= BUS(bus
);
613 const int memset_off
= offsetof(SCSIRequest
, sense
)
614 + sizeof(req
->sense
);
616 req
= g_malloc(reqops
->size
);
617 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
623 req
->hba_private
= hba_private
;
626 object_ref(OBJECT(d
));
627 object_ref(OBJECT(qbus
->parent
));
628 notifier_list_init(&req
->cancel_notifiers
);
629 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
633 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
634 uint8_t *buf
, void *hba_private
)
636 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
637 const SCSIReqOps
*ops
;
638 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
640 SCSICommand cmd
= { .len
= 0 };
643 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
644 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
645 (buf
[0] != INQUIRY
&&
646 buf
[0] != REPORT_LUNS
&&
647 buf
[0] != GET_CONFIGURATION
&&
648 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
651 * If we already have a pending unit attention condition,
652 * report this one before triggering another one.
654 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
655 ops
= &reqops_unit_attention
;
656 } else if (lun
!= d
->lun
||
657 buf
[0] == REPORT_LUNS
||
658 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
659 ops
= &reqops_target_command
;
664 if (ops
!= NULL
|| !sc
->parse_cdb
) {
665 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
);
667 ret
= sc
->parse_cdb(d
, &cmd
, buf
, hba_private
);
671 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
672 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
674 assert(cmd
.len
!= 0);
675 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
678 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
682 if (cmd
.xfer
> INT32_MAX
) {
683 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
685 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
687 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
692 req
->resid
= req
->cmd
.xfer
;
696 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
698 case TEST_UNIT_READY
:
699 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
702 trace_scsi_report_luns(d
->id
, lun
, tag
);
705 trace_scsi_request_sense(d
->id
, lun
, tag
);
714 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
716 return req
->ops
->get_buf(req
);
719 static void scsi_clear_unit_attention(SCSIRequest
*req
)
722 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
723 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
728 * If an INQUIRY command enters the enabled command state,
729 * the device server shall [not] clear any unit attention condition;
730 * See also MMC-6, paragraphs 6.5 and 6.6.2.
732 if (req
->cmd
.buf
[0] == INQUIRY
||
733 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
734 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
738 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
739 ua
= &req
->dev
->unit_attention
;
741 ua
= &req
->bus
->unit_attention
;
745 * If a REPORT LUNS command enters the enabled command state, [...]
746 * the device server shall clear any pending unit attention condition
747 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
749 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
750 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
751 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
755 *ua
= SENSE_CODE(NO_SENSE
);
758 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
763 if (!req
->sense_len
) {
767 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
770 * FIXME: clearing unit attention conditions upon autosense should be done
771 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
774 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
775 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
776 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
778 if (req
->dev
->sense_is_ua
) {
779 scsi_device_unit_attention_reported(req
->dev
);
780 req
->dev
->sense_len
= 0;
781 req
->dev
->sense_is_ua
= false;
786 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
788 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
791 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
793 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
794 sense
.key
, sense
.asc
, sense
.ascq
);
795 memset(req
->sense
, 0, 18);
796 req
->sense
[0] = 0x70;
797 req
->sense
[2] = sense
.key
;
799 req
->sense
[12] = sense
.asc
;
800 req
->sense
[13] = sense
.ascq
;
804 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
806 assert(!req
->enqueued
);
808 if (req
->bus
->info
->get_sg_list
) {
809 req
->sg
= req
->bus
->info
->get_sg_list(req
);
813 req
->enqueued
= true;
814 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
817 int32_t scsi_req_enqueue(SCSIRequest
*req
)
822 scsi_req_enqueue_internal(req
);
824 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
829 static void scsi_req_dequeue(SCSIRequest
*req
)
831 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
834 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
835 req
->enqueued
= false;
840 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
842 /* MMC-6, paragraph 6.7. */
845 if ((data_type
& 3) == 0) {
846 /* Each descriptor is as in Table 295 - Nominal performance. */
847 return 16 * num_desc
+ 8;
849 /* Each descriptor is as in Table 296 - Exceptions. */
850 return 6 * num_desc
+ 8;
855 return 8 * num_desc
+ 8;
857 return 2048 * num_desc
+ 8;
859 return 16 * num_desc
+ 8;
865 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
867 int byte_block
= (buf
[2] >> 2) & 0x1;
868 int type
= (buf
[2] >> 4) & 0x1;
873 xfer_unit
= dev
->blocksize
;
884 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
886 int length
= buf
[2] & 0x3;
888 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
892 case 3: /* USB-specific. */
907 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
909 int extend
= buf
[1] & 0x1;
910 int length
= buf
[2] & 0x3;
912 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
916 case 3: /* USB-specific. */
922 xfer
|= (extend
? buf
[3] << 8 : 0);
926 xfer
|= (extend
? buf
[5] << 8 : 0);
933 uint32_t scsi_data_cdb_xfer(uint8_t *buf
)
935 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
938 return scsi_cdb_xfer(buf
);
942 uint32_t scsi_cdb_xfer(uint8_t *buf
)
944 switch (buf
[0] >> 5) {
950 return lduw_be_p(&buf
[7]);
953 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
956 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
963 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
965 cmd
->xfer
= scsi_cdb_xfer(buf
);
967 case TEST_UNIT_READY
:
971 case WRITE_FILEMARKS
:
972 case WRITE_FILEMARKS_16
:
977 case ALLOW_MEDIUM_REMOVAL
:
979 case SYNCHRONIZE_CACHE
:
980 case SYNCHRONIZE_CACHE_16
:
982 case LOCK_UNLOCK_CACHE
:
991 case ALLOW_OVERWRITE
:
997 if ((buf
[1] & 2) == 0) {
999 } else if ((buf
[1] & 4) != 0) {
1002 cmd
->xfer
*= dev
->blocksize
;
1008 cmd
->xfer
= dev
->blocksize
;
1010 case READ_CAPACITY_10
:
1013 case READ_BLOCK_LIMITS
:
1016 case SEND_VOLUME_TAG
:
1017 /* GPCMD_SET_STREAMING from multimedia commands. */
1018 if (dev
->type
== TYPE_ROM
) {
1019 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1021 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1025 /* length 0 means 256 blocks */
1026 if (cmd
->xfer
== 0) {
1031 case WRITE_VERIFY_10
:
1033 case WRITE_VERIFY_12
:
1035 case WRITE_VERIFY_16
:
1036 cmd
->xfer
*= dev
->blocksize
;
1040 /* length 0 means 256 blocks */
1041 if (cmd
->xfer
== 0) {
1048 cmd
->xfer
*= dev
->blocksize
;
1051 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1052 * for block devices are restricted to the header right now. */
1053 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1056 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1060 case RECEIVE_DIAGNOSTIC
:
1061 case SEND_DIAGNOSTIC
:
1062 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1067 case SEND_CUE_SHEET
:
1068 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1070 case PERSISTENT_RESERVE_OUT
:
1071 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1074 if (dev
->type
== TYPE_ROM
) {
1075 /* MMC command GET PERFORMANCE. */
1076 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1077 buf
[10], buf
[1] & 0x1f);
1080 case MECHANISM_STATUS
:
1081 case READ_DVD_STRUCTURE
:
1082 case SEND_DVD_STRUCTURE
:
1083 case MAINTENANCE_OUT
:
1084 case MAINTENANCE_IN
:
1085 if (dev
->type
== TYPE_ROM
) {
1086 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1087 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1090 case ATA_PASSTHROUGH_12
:
1091 if (dev
->type
== TYPE_ROM
) {
1092 /* BLANK command of MMC */
1095 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1098 case ATA_PASSTHROUGH_16
:
1099 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1105 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1108 /* stream commands */
1115 case RECOVER_BUFFERED_DATA
:
1117 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1118 if (buf
[1] & 0x01) { /* fixed */
1119 cmd
->xfer
*= dev
->blocksize
;
1123 case READ_REVERSE_16
:
1126 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1127 if (buf
[1] & 0x01) { /* fixed */
1128 cmd
->xfer
*= dev
->blocksize
;
1136 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1139 switch (buf
[1] & 0x1f) /* operation code */ {
1140 case SHORT_FORM_BLOCK_ID
:
1141 case SHORT_FORM_VENDOR_SPECIFIC
:
1148 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1156 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1158 /* generic commands */
1160 return scsi_req_xfer(cmd
, dev
, buf
);
1165 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1168 /* medium changer commands */
1169 case EXCHANGE_MEDIUM
:
1170 case INITIALIZE_ELEMENT_STATUS
:
1171 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1173 case POSITION_TO_ELEMENT
:
1176 case READ_ELEMENT_STATUS
:
1177 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1180 /* generic commands */
1182 return scsi_req_xfer(cmd
, dev
, buf
);
1187 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1190 /* Scanner commands */
1191 case OBJECT_POSITION
:
1201 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1204 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1205 return scsi_req_xfer(cmd
, dev
, buf
);
1211 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1214 cmd
->mode
= SCSI_XFER_NONE
;
1217 switch (cmd
->buf
[0]) {
1220 case WRITE_VERIFY_10
:
1222 case WRITE_VERIFY_12
:
1224 case WRITE_VERIFY_16
:
1231 case CHANGE_DEFINITION
:
1234 case MODE_SELECT_10
:
1235 case SEND_DIAGNOSTIC
:
1238 case REASSIGN_BLOCKS
:
1247 case SEARCH_HIGH_12
:
1248 case SEARCH_EQUAL_12
:
1251 case SEND_VOLUME_TAG
:
1252 case SEND_CUE_SHEET
:
1253 case SEND_DVD_STRUCTURE
:
1254 case PERSISTENT_RESERVE_OUT
:
1255 case MAINTENANCE_OUT
:
1258 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1259 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1261 cmd
->mode
= SCSI_XFER_TO_DEV
;
1263 case ATA_PASSTHROUGH_12
:
1264 case ATA_PASSTHROUGH_16
:
1266 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1267 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1270 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1275 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1277 uint8_t *buf
= cmd
->buf
;
1280 switch (buf
[0] >> 5) {
1282 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1287 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1290 lba
= ldq_be_p(&buf
[2]);
1299 int scsi_cdb_length(uint8_t *buf
) {
1302 switch (buf
[0] >> 5) {
1322 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
)
1328 len
= scsi_cdb_length(buf
);
1334 switch (dev
->type
) {
1336 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1338 case TYPE_MEDIUM_CHANGER
:
1339 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1342 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1345 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1352 memcpy(cmd
->buf
, buf
, cmd
->len
);
1353 scsi_cmd_xfer_mode(cmd
);
1354 cmd
->lba
= scsi_cmd_lba(cmd
);
1358 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1360 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1362 scsi_device_set_ua(dev
, sense
);
1363 if (bus
->info
->change
) {
1364 bus
->info
->change(bus
, dev
, sense
);
1369 * Predefined sense codes
1372 /* No sense data available */
1373 const struct SCSISense sense_code_NO_SENSE
= {
1374 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1377 /* LUN not ready, Manual intervention required */
1378 const struct SCSISense sense_code_LUN_NOT_READY
= {
1379 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1382 /* LUN not ready, Medium not present */
1383 const struct SCSISense sense_code_NO_MEDIUM
= {
1384 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1387 /* LUN not ready, medium removal prevented */
1388 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1389 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1392 /* Hardware error, internal target failure */
1393 const struct SCSISense sense_code_TARGET_FAILURE
= {
1394 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1397 /* Illegal request, invalid command operation code */
1398 const struct SCSISense sense_code_INVALID_OPCODE
= {
1399 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1402 /* Illegal request, LBA out of range */
1403 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1404 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1407 /* Illegal request, Invalid field in CDB */
1408 const struct SCSISense sense_code_INVALID_FIELD
= {
1409 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1412 /* Illegal request, Invalid field in parameter list */
1413 const struct SCSISense sense_code_INVALID_PARAM
= {
1414 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1417 /* Illegal request, Parameter list length error */
1418 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1419 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1422 /* Illegal request, LUN not supported */
1423 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1424 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1427 /* Illegal request, Saving parameters not supported */
1428 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1429 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1432 /* Illegal request, Incompatible medium installed */
1433 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1434 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1437 /* Illegal request, medium removal prevented */
1438 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1439 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1442 /* Illegal request, Invalid Transfer Tag */
1443 const struct SCSISense sense_code_INVALID_TAG
= {
1444 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1447 /* Command aborted, I/O process terminated */
1448 const struct SCSISense sense_code_IO_ERROR
= {
1449 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1452 /* Command aborted, I_T Nexus loss occurred */
1453 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1454 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1457 /* Command aborted, Logical Unit failure */
1458 const struct SCSISense sense_code_LUN_FAILURE
= {
1459 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1462 /* Command aborted, Overlapped Commands Attempted */
1463 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1464 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1467 /* Unit attention, Capacity data has changed */
1468 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1469 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1472 /* Unit attention, Power on, reset or bus device reset occurred */
1473 const struct SCSISense sense_code_RESET
= {
1474 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1477 /* Unit attention, No medium */
1478 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1479 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1482 /* Unit attention, Medium may have changed */
1483 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1484 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1487 /* Unit attention, Reported LUNs data has changed */
1488 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1489 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1492 /* Unit attention, Device internal reset */
1493 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1494 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1497 /* Data Protection, Write Protected */
1498 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1499 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1502 /* Data Protection, Space Allocation Failed Write Protect */
1503 const struct SCSISense sense_code_SPACE_ALLOC_FAILED
= {
1504 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x07
1510 * Convert between fixed and descriptor sense buffers
1512 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1513 uint8_t *buf
, int len
, bool fixed
)
1517 if (!fixed
&& len
< 8) {
1522 sense
.key
= NO_SENSE
;
1526 fixed_in
= (in_buf
[0] & 2) == 0;
1528 if (fixed
== fixed_in
) {
1529 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1530 return MIN(len
, in_len
);
1534 sense
.key
= in_buf
[2];
1535 sense
.asc
= in_buf
[12];
1536 sense
.ascq
= in_buf
[13];
1538 sense
.key
= in_buf
[1];
1539 sense
.asc
= in_buf
[2];
1540 sense
.ascq
= in_buf
[3];
1544 memset(buf
, 0, len
);
1546 /* Return fixed format sense buffer */
1550 buf
[12] = sense
.asc
;
1551 buf
[13] = sense
.ascq
;
1552 return MIN(len
, SCSI_SENSE_LEN
);
1554 /* Return descriptor format sense buffer */
1558 buf
[3] = sense
.ascq
;
1563 const char *scsi_command_name(uint8_t cmd
)
1565 static const char *names
[] = {
1566 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1567 [ REWIND
] = "REWIND",
1568 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1569 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1570 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1571 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1572 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1573 [ READ_6
] = "READ_6",
1574 [ WRITE_6
] = "WRITE_6",
1575 [ SET_CAPACITY
] = "SET_CAPACITY",
1576 [ READ_REVERSE
] = "READ_REVERSE",
1577 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1578 [ SPACE
] = "SPACE",
1579 [ INQUIRY
] = "INQUIRY",
1580 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1581 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1582 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1583 [ MODE_SELECT
] = "MODE_SELECT",
1584 [ RESERVE
] = "RESERVE",
1585 [ RELEASE
] = "RELEASE",
1587 [ ERASE
] = "ERASE",
1588 [ MODE_SENSE
] = "MODE_SENSE",
1589 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1590 /* LOAD_UNLOAD and START_STOP use the same operation code */
1591 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1592 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1593 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1594 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1595 [ READ_10
] = "READ_10",
1596 [ WRITE_10
] = "WRITE_10",
1597 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1598 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1599 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1600 [ VERIFY_10
] = "VERIFY_10",
1601 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1602 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1603 [ SEARCH_LOW
] = "SEARCH_LOW",
1604 [ SET_LIMITS
] = "SET_LIMITS",
1605 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1606 /* READ_POSITION and PRE_FETCH use the same operation code */
1607 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1608 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1609 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1610 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1611 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1612 [ COMPARE
] = "COMPARE",
1613 [ COPY_VERIFY
] = "COPY_VERIFY",
1614 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1615 [ READ_BUFFER
] = "READ_BUFFER",
1616 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1617 [ READ_LONG_10
] = "READ_LONG_10",
1618 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1619 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1620 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1621 [ UNMAP
] = "UNMAP",
1622 [ READ_TOC
] = "READ_TOC",
1623 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1624 [ SANITIZE
] = "SANITIZE",
1625 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1626 [ LOG_SELECT
] = "LOG_SELECT",
1627 [ LOG_SENSE
] = "LOG_SENSE",
1628 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1629 [ RESERVE_10
] = "RESERVE_10",
1630 [ RELEASE_10
] = "RELEASE_10",
1631 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1632 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1633 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1634 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1635 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1636 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1637 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1638 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1639 [ READ_16
] = "READ_16",
1640 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1641 [ WRITE_16
] = "WRITE_16",
1642 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1643 [ VERIFY_16
] = "VERIFY_16",
1644 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1645 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1646 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1647 [ LOCATE_16
] = "LOCATE_16",
1648 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1649 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1650 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1651 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1652 [ REPORT_LUNS
] = "REPORT_LUNS",
1653 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1654 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1655 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1656 [ READ_12
] = "READ_12",
1657 [ WRITE_12
] = "WRITE_12",
1658 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1659 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1660 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1661 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1662 [ VERIFY_12
] = "VERIFY_12",
1663 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1664 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1665 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1666 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1667 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1668 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1669 [ READ_CD
] = "READ_CD",
1670 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1671 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1672 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1673 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1674 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1675 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1676 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1677 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1678 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1679 [ GET_EVENT_STATUS_NOTIFICATION
] = "GET_EVENT_STATUS_NOTIFICATION",
1680 [ READ_DISC_INFORMATION
] = "READ_DISC_INFORMATION",
1683 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1688 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1690 assert(req
->refcount
> 0);
1695 void scsi_req_unref(SCSIRequest
*req
)
1697 assert(req
->refcount
> 0);
1698 if (--req
->refcount
== 0) {
1699 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1700 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1702 if (bus
->info
->free_request
&& req
->hba_private
) {
1703 bus
->info
->free_request(bus
, req
->hba_private
);
1705 if (req
->ops
->free_req
) {
1706 req
->ops
->free_req(req
);
1708 object_unref(OBJECT(req
->dev
));
1709 object_unref(OBJECT(qbus
->parent
));
1714 /* Tell the device that we finished processing this chunk of I/O. It
1715 will start the next chunk or complete the command. */
1716 void scsi_req_continue(SCSIRequest
*req
)
1718 if (req
->io_canceled
) {
1719 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1722 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1723 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1724 req
->ops
->write_data(req
);
1726 req
->ops
->read_data(req
);
1730 /* Called by the devices when data is ready for the HBA. The HBA should
1731 start a DMA operation to read or fill the device's data buffer.
1732 Once it completes, calling scsi_req_continue will restart I/O. */
1733 void scsi_req_data(SCSIRequest
*req
, int len
)
1736 if (req
->io_canceled
) {
1737 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1740 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1741 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1744 req
->bus
->info
->transfer_data(req
, len
);
1748 /* If the device calls scsi_req_data and the HBA specified a
1749 * scatter/gather list, the transfer has to happen in a single
1751 assert(!req
->dma_started
);
1752 req
->dma_started
= true;
1754 buf
= scsi_req_get_buf(req
);
1755 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1756 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1758 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1760 scsi_req_continue(req
);
1763 void scsi_req_print(SCSIRequest
*req
)
1768 fprintf(fp
, "[%s id=%d] %s",
1769 req
->dev
->qdev
.parent_bus
->name
,
1771 scsi_command_name(req
->cmd
.buf
[0]));
1772 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1773 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1775 switch (req
->cmd
.mode
) {
1776 case SCSI_XFER_NONE
:
1777 fprintf(fp
, " - none\n");
1779 case SCSI_XFER_FROM_DEV
:
1780 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1782 case SCSI_XFER_TO_DEV
:
1783 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1786 fprintf(fp
, " - Oops\n");
1791 void scsi_req_complete(SCSIRequest
*req
, int status
)
1793 assert(req
->status
== -1);
1794 req
->status
= status
;
1796 assert(req
->sense_len
<= sizeof(req
->sense
));
1797 if (status
== GOOD
) {
1801 if (req
->sense_len
) {
1802 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1803 req
->dev
->sense_len
= req
->sense_len
;
1804 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1806 req
->dev
->sense_len
= 0;
1807 req
->dev
->sense_is_ua
= false;
1811 * Unit attention state is now stored in the device's sense buffer
1812 * if the HBA didn't do autosense. Clear the pending unit attention
1815 scsi_clear_unit_attention(req
);
1818 scsi_req_dequeue(req
);
1819 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1821 /* Cancelled requests might end up being completed instead of cancelled */
1822 notifier_list_notify(&req
->cancel_notifiers
, req
);
1823 scsi_req_unref(req
);
1826 /* Called by the devices when the request is canceled. */
1827 void scsi_req_cancel_complete(SCSIRequest
*req
)
1829 assert(req
->io_canceled
);
1830 if (req
->bus
->info
->cancel
) {
1831 req
->bus
->info
->cancel(req
);
1833 notifier_list_notify(&req
->cancel_notifiers
, req
);
1834 scsi_req_unref(req
);
1837 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1838 * notifier list, the bus will be notified the requests cancellation is
1841 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1843 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1845 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1847 if (req
->io_canceled
) {
1848 /* A blk_aio_cancel_async is pending; when it finishes,
1849 * scsi_req_cancel_complete will be called and will
1850 * call the notifier we just added. Just wait for that.
1855 /* Dropped in scsi_req_cancel_complete. */
1857 scsi_req_dequeue(req
);
1858 req
->io_canceled
= true;
1860 blk_aio_cancel_async(req
->aiocb
);
1862 scsi_req_cancel_complete(req
);
1866 void scsi_req_cancel(SCSIRequest
*req
)
1868 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1869 if (!req
->enqueued
) {
1872 assert(!req
->io_canceled
);
1873 /* Dropped in scsi_req_cancel_complete. */
1875 scsi_req_dequeue(req
);
1876 req
->io_canceled
= true;
1878 blk_aio_cancel(req
->aiocb
);
1880 scsi_req_cancel_complete(req
);
1884 static int scsi_ua_precedence(SCSISense sense
)
1886 if (sense
.key
!= UNIT_ATTENTION
) {
1889 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1890 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1892 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1893 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1895 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1896 /* These two go with "all others". */
1898 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1899 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1900 * POWER ON OCCURRED = 1
1901 * SCSI BUS RESET OCCURRED = 2
1902 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1903 * I_T NEXUS LOSS OCCURRED = 7
1906 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1907 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1910 return (sense
.asc
<< 8) | sense
.ascq
;
1913 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1916 if (sense
.key
!= UNIT_ATTENTION
) {
1919 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1920 sense
.asc
, sense
.ascq
);
1923 * Override a pre-existing unit attention condition, except for a more
1924 * important reset condition.
1926 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1927 prec2
= scsi_ua_precedence(sense
);
1928 if (prec2
< prec1
) {
1929 sdev
->unit_attention
= sense
;
1933 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1937 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1938 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1939 req
= QTAILQ_FIRST(&sdev
->requests
);
1940 scsi_req_cancel_async(req
, NULL
);
1942 blk_drain(sdev
->conf
.blk
);
1943 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1944 scsi_device_set_ua(sdev
, sense
);
1947 static char *scsibus_get_dev_path(DeviceState
*dev
)
1949 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1950 DeviceState
*hba
= dev
->parent_bus
->parent
;
1954 id
= qdev_get_dev_path(hba
);
1956 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1958 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1964 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1966 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1967 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1968 qdev_fw_name(dev
), d
->id
, d
->lun
);
1971 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1974 SCSIDevice
*target_dev
= NULL
;
1976 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1977 DeviceState
*qdev
= kid
->child
;
1978 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1980 if (dev
->channel
== channel
&& dev
->id
== id
) {
1981 if (dev
->lun
== lun
) {
1990 /* SCSI request list. For simplicity, pv points to the whole device */
1992 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1993 VMStateField
*field
, QJSON
*vmdesc
)
1996 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1999 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
2000 assert(!req
->io_canceled
);
2001 assert(req
->status
== -1);
2002 assert(req
->enqueued
);
2004 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
2005 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
2006 qemu_put_be32s(f
, &req
->tag
);
2007 qemu_put_be32s(f
, &req
->lun
);
2008 if (bus
->info
->save_request
) {
2009 bus
->info
->save_request(f
, req
);
2011 if (req
->ops
->save_request
) {
2012 req
->ops
->save_request(f
, req
);
2015 qemu_put_sbyte(f
, 0);
2020 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
2021 VMStateField
*field
)
2024 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
2027 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
2028 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
2033 qemu_get_buffer(f
, buf
, sizeof(buf
));
2034 qemu_get_be32s(f
, &tag
);
2035 qemu_get_be32s(f
, &lun
);
2036 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
2037 req
->retry
= (sbyte
== 1);
2038 if (bus
->info
->load_request
) {
2039 req
->hba_private
= bus
->info
->load_request(f
, req
);
2041 if (req
->ops
->load_request
) {
2042 req
->ops
->load_request(f
, req
);
2045 /* Just restart it later. */
2046 scsi_req_enqueue_internal(req
);
2048 /* At this point, the request will be kept alive by the reference
2049 * added by scsi_req_enqueue_internal, so we can release our reference.
2050 * The HBA of course will add its own reference in the load_request
2051 * callback if it needs to hold on the SCSIRequest.
2053 scsi_req_unref(req
);
2059 static const VMStateInfo vmstate_info_scsi_requests
= {
2060 .name
= "scsi-requests",
2061 .get
= get_scsi_requests
,
2062 .put
= put_scsi_requests
,
2065 static bool scsi_sense_state_needed(void *opaque
)
2067 SCSIDevice
*s
= opaque
;
2069 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
2072 static const VMStateDescription vmstate_scsi_sense_state
= {
2073 .name
= "SCSIDevice/sense",
2075 .minimum_version_id
= 1,
2076 .needed
= scsi_sense_state_needed
,
2077 .fields
= (VMStateField
[]) {
2078 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
2079 SCSI_SENSE_BUF_SIZE_OLD
,
2080 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
2081 VMSTATE_END_OF_LIST()
2085 const VMStateDescription vmstate_scsi_device
= {
2086 .name
= "SCSIDevice",
2088 .minimum_version_id
= 1,
2089 .fields
= (VMStateField
[]) {
2090 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
2091 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
2092 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
2093 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
2094 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
2095 VMSTATE_UINT32(sense_len
, SCSIDevice
),
2099 .field_exists
= NULL
,
2100 .size
= 0, /* ouch */
2101 .info
= &vmstate_info_scsi_requests
,
2102 .flags
= VMS_SINGLE
,
2105 VMSTATE_END_OF_LIST()
2107 .subsections
= (const VMStateDescription
*[]) {
2108 &vmstate_scsi_sense_state
,
2113 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
2115 DeviceClass
*k
= DEVICE_CLASS(klass
);
2116 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
2117 k
->bus_type
= TYPE_SCSI_BUS
;
2118 k
->realize
= scsi_qdev_realize
;
2119 k
->unrealize
= scsi_qdev_unrealize
;
2120 k
->props
= scsi_props
;
2123 static void scsi_dev_instance_init(Object
*obj
)
2125 DeviceState
*dev
= DEVICE(obj
);
2126 SCSIDevice
*s
= SCSI_DEVICE(dev
);
2128 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
2133 static const TypeInfo scsi_device_type_info
= {
2134 .name
= TYPE_SCSI_DEVICE
,
2135 .parent
= TYPE_DEVICE
,
2136 .instance_size
= sizeof(SCSIDevice
),
2138 .class_size
= sizeof(SCSIDeviceClass
),
2139 .class_init
= scsi_device_class_init
,
2140 .instance_init
= scsi_dev_instance_init
,
2143 static void scsi_register_types(void)
2145 type_register_static(&scsi_bus_info
);
2146 type_register_static(&scsi_device_type_info
);
2149 type_init(scsi_register_types
)