1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qemu/error-report.h"
4 #include "qemu/module.h"
5 #include "qemu/option.h"
6 #include "qemu/hw-version.h"
7 #include "hw/qdev-properties.h"
8 #include "hw/scsi/scsi.h"
9 #include "migration/qemu-file-types.h"
10 #include "migration/vmstate.h"
11 #include "scsi/constants.h"
12 #include "sysemu/block-backend.h"
13 #include "sysemu/blockdev.h"
14 #include "sysemu/sysemu.h"
15 #include "sysemu/runstate.h"
17 #include "sysemu/dma.h"
18 #include "qemu/cutils.h"
20 static char *scsibus_get_dev_path(DeviceState
*dev
);
21 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
22 static void scsi_req_dequeue(SCSIRequest
*req
);
23 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
24 static void scsi_target_free_buf(SCSIRequest
*req
);
25 static void scsi_clear_reported_luns_changed(SCSIRequest
*req
);
27 static int next_scsi_bus
;
29 static SCSIDevice
*do_scsi_device_find(SCSIBus
*bus
,
30 int channel
, int id
, int lun
,
31 bool include_unrealized
)
34 SCSIDevice
*retval
= NULL
;
36 QTAILQ_FOREACH_RCU(kid
, &bus
->qbus
.children
, sibling
) {
37 DeviceState
*qdev
= kid
->child
;
38 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
40 if (dev
->channel
== channel
&& dev
->id
== id
) {
41 if (dev
->lun
== lun
) {
47 * If we don't find exact match (channel/bus/lun),
48 * we will return the first device which matches channel/bus
58 * This function might run on the IO thread and we might race against
59 * main thread hot-plugging the device.
60 * We assume that as soon as .realized is set to true we can let
61 * the user access the device.
64 if (retval
&& !include_unrealized
&& !qdev_is_realized(&retval
->qdev
)) {
71 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
73 RCU_READ_LOCK_GUARD();
74 return do_scsi_device_find(bus
, channel
, id
, lun
, false);
77 SCSIDevice
*scsi_device_get(SCSIBus
*bus
, int channel
, int id
, int lun
)
80 RCU_READ_LOCK_GUARD();
81 d
= do_scsi_device_find(bus
, channel
, id
, lun
, false);
88 static void scsi_device_realize(SCSIDevice
*s
, Error
**errp
)
90 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
96 static void scsi_device_unrealize(SCSIDevice
*s
)
98 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
104 int scsi_bus_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
105 size_t buf_len
, void *hba_private
)
107 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
110 assert(cmd
->len
== 0);
111 rc
= scsi_req_parse_cdb(dev
, cmd
, buf
, buf_len
);
112 if (bus
->info
->parse_cdb
) {
113 rc
= bus
->info
->parse_cdb(dev
, cmd
, buf
, buf_len
, hba_private
);
118 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
119 uint8_t *buf
, void *hba_private
)
121 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
123 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
129 void scsi_device_unit_attention_reported(SCSIDevice
*s
)
131 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
132 if (sc
->unit_attention_reported
) {
133 sc
->unit_attention_reported(s
);
137 /* Create a scsi bus, and attach devices to it. */
138 void scsi_bus_init_named(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
139 const SCSIBusInfo
*info
, const char *bus_name
)
141 qbus_init(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
142 bus
->busnr
= next_scsi_bus
++;
144 qbus_set_bus_hotplug_handler(BUS(bus
));
147 static void scsi_dma_restart_bh(void *opaque
)
149 SCSIDevice
*s
= opaque
;
150 SCSIRequest
*req
, *next
;
152 qemu_bh_delete(s
->bh
);
155 aio_context_acquire(blk_get_aio_context(s
->conf
.blk
));
156 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
160 switch (req
->cmd
.mode
) {
161 case SCSI_XFER_FROM_DEV
:
162 case SCSI_XFER_TO_DEV
:
163 scsi_req_continue(req
);
166 scsi_req_dequeue(req
);
167 scsi_req_enqueue(req
);
173 aio_context_release(blk_get_aio_context(s
->conf
.blk
));
174 /* Drop the reference that was acquired in scsi_dma_restart_cb */
175 object_unref(OBJECT(s
));
178 void scsi_req_retry(SCSIRequest
*req
)
180 /* No need to save a reference, because scsi_dma_restart_bh just
181 * looks at the request list. */
185 static void scsi_dma_restart_cb(void *opaque
, bool running
, RunState state
)
187 SCSIDevice
*s
= opaque
;
193 AioContext
*ctx
= blk_get_aio_context(s
->conf
.blk
);
194 /* The reference is dropped in scsi_dma_restart_bh.*/
195 object_ref(OBJECT(s
));
196 s
->bh
= aio_bh_new_guarded(ctx
, scsi_dma_restart_bh
, s
,
197 &DEVICE(s
)->mem_reentrancy_guard
);
198 qemu_bh_schedule(s
->bh
);
202 static bool scsi_bus_is_address_free(SCSIBus
*bus
,
203 int channel
, int target
, int lun
,
208 RCU_READ_LOCK_GUARD();
209 d
= do_scsi_device_find(bus
, channel
, target
, lun
, true);
210 if (d
&& d
->lun
== lun
) {
222 static bool scsi_bus_check_address(BusState
*qbus
, DeviceState
*qdev
, Error
**errp
)
224 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
225 SCSIBus
*bus
= SCSI_BUS(qbus
);
227 if (dev
->channel
> bus
->info
->max_channel
) {
228 error_setg(errp
, "bad scsi channel id: %d", dev
->channel
);
231 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
232 error_setg(errp
, "bad scsi device id: %d", dev
->id
);
235 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
236 error_setg(errp
, "bad scsi device lun: %d", dev
->lun
);
240 if (dev
->id
!= -1 && dev
->lun
!= -1) {
242 if (!scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, dev
->lun
, &d
)) {
243 error_setg(errp
, "lun already used by '%s'", d
->qdev
.id
);
251 static void scsi_qdev_realize(DeviceState
*qdev
, Error
**errp
)
253 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
254 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
256 Error
*local_err
= NULL
;
260 if (dev
->lun
== -1) {
264 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, ++id
, dev
->lun
, NULL
);
265 } while (!is_free
&& id
< bus
->info
->max_target
);
267 error_setg(errp
, "no free target");
271 } else if (dev
->lun
== -1) {
274 is_free
= scsi_bus_is_address_free(bus
, dev
->channel
, dev
->id
, ++lun
, NULL
);
275 } while (!is_free
&& lun
< bus
->info
->max_lun
);
277 error_setg(errp
, "no free lun");
283 QTAILQ_INIT(&dev
->requests
);
284 scsi_device_realize(dev
, &local_err
);
286 error_propagate(errp
, local_err
);
289 dev
->vmsentry
= qdev_add_vm_change_state_handler(DEVICE(dev
),
290 scsi_dma_restart_cb
, dev
);
293 static void scsi_qdev_unrealize(DeviceState
*qdev
)
295 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
298 qemu_del_vm_change_state_handler(dev
->vmsentry
);
301 scsi_device_purge_requests(dev
, SENSE_CODE(NO_SENSE
));
303 scsi_device_unrealize(dev
);
305 blockdev_mark_auto_del(dev
->conf
.blk
);
308 /* handle legacy '-drive if=scsi,...' cmd line args */
309 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockBackend
*blk
,
310 int unit
, bool removable
, int bootindex
,
312 BlockdevOnError rerror
,
313 BlockdevOnError werror
,
314 const char *serial
, Error
**errp
)
321 if (blk_is_sg(blk
)) {
322 driver
= "scsi-generic";
324 dinfo
= blk_legacy_dinfo(blk
);
325 if (dinfo
&& dinfo
->media_cd
) {
331 dev
= qdev_new(driver
);
332 name
= g_strdup_printf("legacy[%d]", unit
);
333 object_property_add_child(OBJECT(bus
), name
, OBJECT(dev
));
336 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
337 if (bootindex
>= 0) {
338 object_property_set_int(OBJECT(dev
), "bootindex", bootindex
,
341 if (object_property_find(OBJECT(dev
), "removable")) {
342 qdev_prop_set_bit(dev
, "removable", removable
);
344 if (serial
&& object_property_find(OBJECT(dev
), "serial")) {
345 qdev_prop_set_string(dev
, "serial", serial
);
347 if (!qdev_prop_set_drive_err(dev
, "drive", blk
, errp
)) {
348 object_unparent(OBJECT(dev
));
351 if (!object_property_set_bool(OBJECT(dev
), "share-rw", share_rw
, errp
)) {
352 object_unparent(OBJECT(dev
));
356 qdev_prop_set_enum(dev
, "rerror", rerror
);
357 qdev_prop_set_enum(dev
, "werror", werror
);
359 if (!qdev_realize_and_unref(dev
, &bus
->qbus
, errp
)) {
360 object_unparent(OBJECT(dev
));
363 return SCSI_DEVICE(dev
);
366 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
373 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
374 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
378 qemu_opts_loc_restore(dinfo
->opts
);
379 scsi_bus_legacy_add_drive(bus
, blk_by_legacy_dinfo(dinfo
),
380 unit
, false, -1, false,
381 BLOCKDEV_ON_ERROR_AUTO
,
382 BLOCKDEV_ON_ERROR_AUTO
,
388 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
390 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
391 scsi_req_complete(req
, CHECK_CONDITION
);
395 static const struct SCSIReqOps reqops_invalid_field
= {
396 .size
= sizeof(SCSIRequest
),
397 .send_command
= scsi_invalid_field
400 /* SCSIReqOps implementation for invalid commands. */
402 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
404 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
405 scsi_req_complete(req
, CHECK_CONDITION
);
409 static const struct SCSIReqOps reqops_invalid_opcode
= {
410 .size
= sizeof(SCSIRequest
),
411 .send_command
= scsi_invalid_command
414 /* SCSIReqOps implementation for unit attention conditions. */
416 static void scsi_fetch_unit_attention_sense(SCSIRequest
*req
)
418 SCSISense
*ua
= NULL
;
420 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
421 ua
= &req
->dev
->unit_attention
;
422 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
423 ua
= &req
->bus
->unit_attention
;
427 * Fetch the unit attention sense immediately so that another
428 * scsi_req_new does not use reqops_unit_attention.
431 scsi_req_build_sense(req
, *ua
);
432 *ua
= SENSE_CODE(NO_SENSE
);
436 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
438 scsi_req_complete(req
, CHECK_CONDITION
);
442 static const struct SCSIReqOps reqops_unit_attention
= {
443 .size
= sizeof(SCSIRequest
),
444 .init_req
= scsi_fetch_unit_attention_sense
,
445 .send_command
= scsi_unit_attention
448 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
451 typedef struct SCSITargetReq SCSITargetReq
;
453 struct SCSITargetReq
{
460 static void store_lun(uint8_t *outbuf
, int lun
)
463 /* Simple logical unit addressing method*/
467 /* Flat space addressing method */
468 outbuf
[0] = 0x40 | (lun
>> 8);
469 outbuf
[1] = (lun
& 255);
473 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
477 uint8_t tmp
[8] = {0};
481 if (r
->req
.cmd
.xfer
< 16) {
484 if (r
->req
.cmd
.buf
[2] > 2) {
488 /* reserve space for 63 LUNs*/
489 buf
= g_byte_array_sized_new(512);
491 channel
= r
->req
.dev
->channel
;
494 /* add size (will be updated later to correct value */
495 g_byte_array_append(buf
, tmp
, 8);
499 g_byte_array_append(buf
, tmp
, 8);
502 WITH_RCU_READ_LOCK_GUARD() {
503 QTAILQ_FOREACH_RCU(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
504 DeviceState
*qdev
= kid
->child
;
505 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
507 if (dev
->channel
== channel
&& dev
->id
== id
&& dev
->lun
!= 0 &&
508 qdev_is_realized(&dev
->qdev
)) {
509 store_lun(tmp
, dev
->lun
);
510 g_byte_array_append(buf
, tmp
, 8);
517 r
->buf
= g_byte_array_free(buf
, FALSE
);
518 r
->len
= MIN(len
, r
->req
.cmd
.xfer
& ~7);
520 /* store the LUN list length */
521 stl_be_p(&r
->buf
[0], len
- 8);
524 * If a REPORT LUNS command enters the enabled command state, [...]
525 * the device server shall clear any pending unit attention condition
526 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
528 scsi_clear_reported_luns_changed(&r
->req
);
533 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
535 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
537 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
539 if (r
->req
.cmd
.buf
[1] & 0x2) {
540 /* Command support data - optional, not implemented */
544 if (r
->req
.cmd
.buf
[1] & 0x1) {
545 /* Vital product data */
546 uint8_t page_code
= r
->req
.cmd
.buf
[2];
547 r
->buf
[r
->len
++] = page_code
; /* this page */
548 r
->buf
[r
->len
++] = 0x00;
551 case 0x00: /* Supported page codes, mandatory */
555 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
556 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
563 assert(r
->len
< r
->buf_len
);
564 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
568 /* Standard INQUIRY data */
569 if (r
->req
.cmd
.buf
[2] != 0) {
574 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
575 memset(r
->buf
, 0, r
->len
);
576 if (r
->req
.lun
!= 0) {
577 r
->buf
[0] = TYPE_NO_LUN
;
579 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
580 r
->buf
[2] = 5; /* Version */
581 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
582 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
583 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
584 memcpy(&r
->buf
[8], "QEMU ", 8);
585 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
586 pstrcpy((char *) &r
->buf
[32], 4, qemu_hw_version());
591 static size_t scsi_sense_len(SCSIRequest
*req
)
593 if (req
->dev
->type
== TYPE_SCANNER
)
594 return SCSI_SENSE_LEN_SCANNER
;
596 return SCSI_SENSE_LEN
;
599 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
601 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
602 int fixed_sense
= (req
->cmd
.buf
[1] & 1) == 0;
605 buf
[0] != INQUIRY
&& buf
[0] != REQUEST_SENSE
) {
606 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
607 scsi_req_complete(req
, CHECK_CONDITION
);
612 if (!scsi_target_emulate_report_luns(r
)) {
613 goto illegal_request
;
617 if (!scsi_target_emulate_inquiry(r
)) {
618 goto illegal_request
;
622 scsi_target_alloc_buf(&r
->req
, scsi_sense_len(req
));
624 const struct SCSISense sense
= SENSE_CODE(LUN_NOT_SUPPORTED
);
626 r
->len
= scsi_build_sense_buf(r
->buf
, req
->cmd
.xfer
,
629 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
630 MIN(req
->cmd
.xfer
, r
->buf_len
),
633 if (r
->req
.dev
->sense_is_ua
) {
634 scsi_device_unit_attention_reported(req
->dev
);
635 r
->req
.dev
->sense_len
= 0;
636 r
->req
.dev
->sense_is_ua
= false;
639 case TEST_UNIT_READY
:
642 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
643 scsi_req_complete(req
, CHECK_CONDITION
);
646 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
647 scsi_req_complete(req
, CHECK_CONDITION
);
652 scsi_req_complete(req
, GOOD
);
657 static void scsi_target_read_data(SCSIRequest
*req
)
659 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
665 scsi_req_data(&r
->req
, n
);
667 scsi_req_complete(&r
->req
, GOOD
);
671 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
673 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
678 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
680 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
682 r
->buf
= g_malloc(len
);
688 static void scsi_target_free_buf(SCSIRequest
*req
)
690 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
695 static const struct SCSIReqOps reqops_target_command
= {
696 .size
= sizeof(SCSITargetReq
),
697 .send_command
= scsi_target_send_command
,
698 .read_data
= scsi_target_read_data
,
699 .get_buf
= scsi_target_get_buf
,
700 .free_req
= scsi_target_free_buf
,
704 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
705 uint32_t tag
, uint32_t lun
, void *hba_private
)
708 SCSIBus
*bus
= scsi_bus_from_device(d
);
709 BusState
*qbus
= BUS(bus
);
710 const int memset_off
= offsetof(SCSIRequest
, sense
)
711 + sizeof(req
->sense
);
713 req
= g_malloc(reqops
->size
);
714 memset((uint8_t *)req
+ memset_off
, 0, reqops
->size
- memset_off
);
720 req
->hba_private
= hba_private
;
722 req
->host_status
= -1;
724 object_ref(OBJECT(d
));
725 object_ref(OBJECT(qbus
->parent
));
726 notifier_list_init(&req
->cancel_notifiers
);
728 if (reqops
->init_req
) {
729 reqops
->init_req(req
);
732 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
736 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
737 uint8_t *buf
, size_t buf_len
, void *hba_private
)
739 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
740 const SCSIReqOps
*ops
;
741 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(d
);
743 SCSICommand cmd
= { .len
= 0 };
747 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, 0);
751 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
752 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
753 (buf
[0] != INQUIRY
&&
754 buf
[0] != REPORT_LUNS
&&
755 buf
[0] != GET_CONFIGURATION
&&
756 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
759 * If we already have a pending unit attention condition,
760 * report this one before triggering another one.
762 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
763 ops
= &reqops_unit_attention
;
764 } else if (lun
!= d
->lun
||
765 buf
[0] == REPORT_LUNS
||
766 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
767 ops
= &reqops_target_command
;
772 if (ops
!= NULL
|| !sc
->parse_cdb
) {
773 ret
= scsi_req_parse_cdb(d
, &cmd
, buf
, buf_len
);
775 ret
= sc
->parse_cdb(d
, &cmd
, buf
, buf_len
, hba_private
);
779 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
781 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
783 assert(cmd
.len
!= 0);
784 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
787 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
791 if (cmd
.xfer
> INT32_MAX
) {
792 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
794 req
= scsi_req_alloc(ops
, d
, tag
, lun
, hba_private
);
796 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
801 req
->residual
= req
->cmd
.xfer
;
805 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
807 case TEST_UNIT_READY
:
808 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
811 trace_scsi_report_luns(d
->id
, lun
, tag
);
814 trace_scsi_request_sense(d
->id
, lun
, tag
);
823 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
825 return req
->ops
->get_buf(req
);
828 static void scsi_clear_reported_luns_changed(SCSIRequest
*req
)
832 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
833 ua
= &req
->dev
->unit_attention
;
834 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
835 ua
= &req
->bus
->unit_attention
;
840 if (ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
841 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
) {
842 *ua
= SENSE_CODE(NO_SENSE
);
846 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
851 if (!req
->sense_len
) {
855 ret
= scsi_convert_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
858 * FIXME: clearing unit attention conditions upon autosense should be done
859 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
862 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
863 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
864 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
866 if (req
->dev
->sense_is_ua
) {
867 scsi_device_unit_attention_reported(req
->dev
);
868 req
->dev
->sense_len
= 0;
869 req
->dev
->sense_is_ua
= false;
874 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
876 return scsi_convert_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
879 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
881 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
882 sense
.key
, sense
.asc
, sense
.ascq
);
883 req
->sense_len
= scsi_build_sense(req
->sense
, sense
);
886 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
888 assert(!req
->enqueued
);
890 if (req
->bus
->info
->get_sg_list
) {
891 req
->sg
= req
->bus
->info
->get_sg_list(req
);
895 req
->enqueued
= true;
896 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
899 int32_t scsi_req_enqueue(SCSIRequest
*req
)
904 scsi_req_enqueue_internal(req
);
906 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
911 static void scsi_req_dequeue(SCSIRequest
*req
)
913 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
916 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
917 req
->enqueued
= false;
922 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
924 /* MMC-6, paragraph 6.7. */
927 if ((data_type
& 3) == 0) {
928 /* Each descriptor is as in Table 295 - Nominal performance. */
929 return 16 * num_desc
+ 8;
931 /* Each descriptor is as in Table 296 - Exceptions. */
932 return 6 * num_desc
+ 8;
937 return 8 * num_desc
+ 8;
939 return 2048 * num_desc
+ 8;
941 return 16 * num_desc
+ 8;
947 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
949 int byte_block
= (buf
[2] >> 2) & 0x1;
950 int type
= (buf
[2] >> 4) & 0x1;
955 xfer_unit
= dev
->blocksize
;
966 static int ata_passthrough_12_xfer(SCSIDevice
*dev
, uint8_t *buf
)
968 int length
= buf
[2] & 0x3;
970 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
974 case 3: /* USB-specific. */
989 static int ata_passthrough_16_xfer(SCSIDevice
*dev
, uint8_t *buf
)
991 int extend
= buf
[1] & 0x1;
992 int length
= buf
[2] & 0x3;
994 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
998 case 3: /* USB-specific. */
1004 xfer
|= (extend
? buf
[3] << 8 : 0);
1008 xfer
|= (extend
? buf
[5] << 8 : 0);
1015 static int scsi_req_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1017 cmd
->xfer
= scsi_cdb_xfer(buf
);
1019 case TEST_UNIT_READY
:
1023 case WRITE_FILEMARKS
:
1024 case WRITE_FILEMARKS_16
:
1029 case ALLOW_MEDIUM_REMOVAL
:
1031 case SYNCHRONIZE_CACHE
:
1032 case SYNCHRONIZE_CACHE_16
:
1034 case LOCK_UNLOCK_CACHE
:
1040 case SET_READ_AHEAD
:
1043 case ALLOW_OVERWRITE
:
1049 if ((buf
[1] & 2) == 0) {
1051 } else if ((buf
[1] & 4) != 0) {
1054 cmd
->xfer
*= dev
->blocksize
;
1060 cmd
->xfer
= buf
[1] & 1 ? 0 : dev
->blocksize
;
1062 case READ_CAPACITY_10
:
1065 case READ_BLOCK_LIMITS
:
1068 case SEND_VOLUME_TAG
:
1069 /* GPCMD_SET_STREAMING from multimedia commands. */
1070 if (dev
->type
== TYPE_ROM
) {
1071 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
1073 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1077 /* length 0 means 256 blocks */
1078 if (cmd
->xfer
== 0) {
1083 case WRITE_VERIFY_10
:
1085 case WRITE_VERIFY_12
:
1087 case WRITE_VERIFY_16
:
1088 cmd
->xfer
*= dev
->blocksize
;
1092 /* length 0 means 256 blocks */
1093 if (cmd
->xfer
== 0) {
1100 cmd
->xfer
*= dev
->blocksize
;
1103 /* MMC mandates the parameter list to be 12-bytes long. Parameters
1104 * for block devices are restricted to the header right now. */
1105 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
1108 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
1112 case RECEIVE_DIAGNOSTIC
:
1113 case SEND_DIAGNOSTIC
:
1114 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1119 case SEND_CUE_SHEET
:
1120 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1122 case PERSISTENT_RESERVE_OUT
:
1123 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
1126 if (dev
->type
== TYPE_ROM
) {
1127 /* MMC command GET PERFORMANCE. */
1128 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
1129 buf
[10], buf
[1] & 0x1f);
1132 case MECHANISM_STATUS
:
1133 case READ_DVD_STRUCTURE
:
1134 case SEND_DVD_STRUCTURE
:
1135 case MAINTENANCE_OUT
:
1136 case MAINTENANCE_IN
:
1137 if (dev
->type
== TYPE_ROM
) {
1138 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
1139 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1142 case ATA_PASSTHROUGH_12
:
1143 if (dev
->type
== TYPE_ROM
) {
1144 /* BLANK command of MMC */
1147 cmd
->xfer
= ata_passthrough_12_xfer(dev
, buf
);
1150 case ATA_PASSTHROUGH_16
:
1151 cmd
->xfer
= ata_passthrough_16_xfer(dev
, buf
);
1157 static int scsi_req_stream_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1160 /* stream commands */
1167 case RECOVER_BUFFERED_DATA
:
1169 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1170 if (buf
[1] & 0x01) { /* fixed */
1171 cmd
->xfer
*= dev
->blocksize
;
1175 case READ_REVERSE_16
:
1178 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1179 if (buf
[1] & 0x01) { /* fixed */
1180 cmd
->xfer
*= dev
->blocksize
;
1188 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1191 switch (buf
[1] & 0x1f) /* operation code */ {
1192 case SHORT_FORM_BLOCK_ID
:
1193 case SHORT_FORM_VENDOR_SPECIFIC
:
1200 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1208 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1210 /* generic commands */
1212 return scsi_req_xfer(cmd
, dev
, buf
);
1217 static int scsi_req_medium_changer_xfer(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1220 /* medium changer commands */
1221 case EXCHANGE_MEDIUM
:
1222 case INITIALIZE_ELEMENT_STATUS
:
1223 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1225 case POSITION_TO_ELEMENT
:
1228 case READ_ELEMENT_STATUS
:
1229 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1232 /* generic commands */
1234 return scsi_req_xfer(cmd
, dev
, buf
);
1239 static int scsi_req_scanner_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1242 /* Scanner commands */
1243 case OBJECT_POSITION
:
1253 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
1256 /* GET_DATA_BUFFER_STATUS xfer handled by scsi_req_xfer */
1257 return scsi_req_xfer(cmd
, dev
, buf
);
1263 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1266 cmd
->mode
= SCSI_XFER_NONE
;
1269 switch (cmd
->buf
[0]) {
1272 case WRITE_VERIFY_10
:
1274 case WRITE_VERIFY_12
:
1276 case WRITE_VERIFY_16
:
1283 case CHANGE_DEFINITION
:
1286 case MODE_SELECT_10
:
1287 case SEND_DIAGNOSTIC
:
1290 case REASSIGN_BLOCKS
:
1299 case SEARCH_HIGH_12
:
1300 case SEARCH_EQUAL_12
:
1303 case SEND_VOLUME_TAG
:
1304 case SEND_CUE_SHEET
:
1305 case SEND_DVD_STRUCTURE
:
1306 case PERSISTENT_RESERVE_OUT
:
1307 case MAINTENANCE_OUT
:
1310 /* SCAN conflicts with START_STOP. START_STOP has cmd->xfer set to 0 for
1311 * non-scanner devices, so we only get here for SCAN and not for START_STOP.
1313 cmd
->mode
= SCSI_XFER_TO_DEV
;
1315 case ATA_PASSTHROUGH_12
:
1316 case ATA_PASSTHROUGH_16
:
1318 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1319 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1322 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1327 int scsi_req_parse_cdb(SCSIDevice
*dev
, SCSICommand
*cmd
, uint8_t *buf
,
1334 len
= scsi_cdb_length(buf
);
1335 if (len
< 0 || len
> buf_len
) {
1340 switch (dev
->type
) {
1342 rc
= scsi_req_stream_xfer(cmd
, dev
, buf
);
1344 case TYPE_MEDIUM_CHANGER
:
1345 rc
= scsi_req_medium_changer_xfer(cmd
, dev
, buf
);
1348 rc
= scsi_req_scanner_length(cmd
, dev
, buf
);
1351 rc
= scsi_req_xfer(cmd
, dev
, buf
);
1358 memcpy(cmd
->buf
, buf
, cmd
->len
);
1359 scsi_cmd_xfer_mode(cmd
);
1360 cmd
->lba
= scsi_cmd_lba(cmd
);
1364 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1366 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1368 scsi_device_set_ua(dev
, sense
);
1369 if (bus
->info
->change
) {
1370 bus
->info
->change(bus
, dev
, sense
);
1374 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1376 assert(req
->refcount
> 0);
1381 void scsi_req_unref(SCSIRequest
*req
)
1383 assert(req
->refcount
> 0);
1384 if (--req
->refcount
== 0) {
1385 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1386 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1388 if (bus
->info
->free_request
&& req
->hba_private
) {
1389 bus
->info
->free_request(bus
, req
->hba_private
);
1391 if (req
->ops
->free_req
) {
1392 req
->ops
->free_req(req
);
1394 object_unref(OBJECT(req
->dev
));
1395 object_unref(OBJECT(qbus
->parent
));
1400 /* Tell the device that we finished processing this chunk of I/O. It
1401 will start the next chunk or complete the command. */
1402 void scsi_req_continue(SCSIRequest
*req
)
1404 if (req
->io_canceled
) {
1405 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1408 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1409 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1410 req
->ops
->write_data(req
);
1412 req
->ops
->read_data(req
);
1416 /* Called by the devices when data is ready for the HBA. The HBA should
1417 start a DMA operation to read or fill the device's data buffer.
1418 Once it completes, calling scsi_req_continue will restart I/O. */
1419 void scsi_req_data(SCSIRequest
*req
, int len
)
1422 if (req
->io_canceled
) {
1423 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1426 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1427 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1429 req
->residual
-= len
;
1430 req
->bus
->info
->transfer_data(req
, len
);
1434 /* If the device calls scsi_req_data and the HBA specified a
1435 * scatter/gather list, the transfer has to happen in a single
1437 assert(!req
->dma_started
);
1438 req
->dma_started
= true;
1440 buf
= scsi_req_get_buf(req
);
1441 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1442 dma_buf_read(buf
, len
, &req
->residual
, req
->sg
,
1443 MEMTXATTRS_UNSPECIFIED
);
1445 dma_buf_write(buf
, len
, &req
->residual
, req
->sg
,
1446 MEMTXATTRS_UNSPECIFIED
);
1448 scsi_req_continue(req
);
1451 void scsi_req_print(SCSIRequest
*req
)
1456 fprintf(fp
, "[%s id=%d] %s",
1457 req
->dev
->qdev
.parent_bus
->name
,
1459 scsi_command_name(req
->cmd
.buf
[0]));
1460 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1461 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1463 switch (req
->cmd
.mode
) {
1464 case SCSI_XFER_NONE
:
1465 fprintf(fp
, " - none\n");
1467 case SCSI_XFER_FROM_DEV
:
1468 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1470 case SCSI_XFER_TO_DEV
:
1471 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1474 fprintf(fp
, " - Oops\n");
1479 void scsi_req_complete_failed(SCSIRequest
*req
, int host_status
)
1484 assert(req
->status
== -1 && req
->host_status
== -1);
1485 assert(req
->ops
!= &reqops_unit_attention
);
1487 if (!req
->bus
->info
->fail
) {
1488 status
= scsi_sense_from_host_status(req
->host_status
, &sense
);
1489 if (status
== CHECK_CONDITION
) {
1490 scsi_req_build_sense(req
, sense
);
1492 scsi_req_complete(req
, status
);
1496 req
->host_status
= host_status
;
1498 scsi_req_dequeue(req
);
1499 req
->bus
->info
->fail(req
);
1501 /* Cancelled requests might end up being completed instead of cancelled */
1502 notifier_list_notify(&req
->cancel_notifiers
, req
);
1503 scsi_req_unref(req
);
1506 void scsi_req_complete(SCSIRequest
*req
, int status
)
1508 assert(req
->status
== -1 && req
->host_status
== -1);
1509 req
->status
= status
;
1510 req
->host_status
= SCSI_HOST_OK
;
1512 assert(req
->sense_len
<= sizeof(req
->sense
));
1513 if (status
== GOOD
) {
1517 if (req
->sense_len
) {
1518 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1519 req
->dev
->sense_len
= req
->sense_len
;
1520 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1522 req
->dev
->sense_len
= 0;
1523 req
->dev
->sense_is_ua
= false;
1527 scsi_req_dequeue(req
);
1528 req
->bus
->info
->complete(req
, req
->residual
);
1530 /* Cancelled requests might end up being completed instead of cancelled */
1531 notifier_list_notify(&req
->cancel_notifiers
, req
);
1532 scsi_req_unref(req
);
1535 /* Called by the devices when the request is canceled. */
1536 void scsi_req_cancel_complete(SCSIRequest
*req
)
1538 assert(req
->io_canceled
);
1539 if (req
->bus
->info
->cancel
) {
1540 req
->bus
->info
->cancel(req
);
1542 notifier_list_notify(&req
->cancel_notifiers
, req
);
1543 scsi_req_unref(req
);
1546 /* Cancel @req asynchronously. @notifier is added to @req's cancellation
1547 * notifier list, the bus will be notified the requests cancellation is
1550 void scsi_req_cancel_async(SCSIRequest
*req
, Notifier
*notifier
)
1552 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1554 notifier_list_add(&req
->cancel_notifiers
, notifier
);
1556 if (req
->io_canceled
) {
1557 /* A blk_aio_cancel_async is pending; when it finishes,
1558 * scsi_req_cancel_complete will be called and will
1559 * call the notifier we just added. Just wait for that.
1564 /* Dropped in scsi_req_cancel_complete. */
1566 scsi_req_dequeue(req
);
1567 req
->io_canceled
= true;
1569 blk_aio_cancel_async(req
->aiocb
);
1571 scsi_req_cancel_complete(req
);
1575 void scsi_req_cancel(SCSIRequest
*req
)
1577 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1578 if (!req
->enqueued
) {
1581 assert(!req
->io_canceled
);
1582 /* Dropped in scsi_req_cancel_complete. */
1584 scsi_req_dequeue(req
);
1585 req
->io_canceled
= true;
1587 blk_aio_cancel(req
->aiocb
);
1589 scsi_req_cancel_complete(req
);
1593 static int scsi_ua_precedence(SCSISense sense
)
1595 if (sense
.key
!= UNIT_ATTENTION
) {
1598 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1599 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1601 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1602 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1604 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1605 /* These two go with "all others". */
1607 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1608 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1609 * POWER ON OCCURRED = 1
1610 * SCSI BUS RESET OCCURRED = 2
1611 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1612 * I_T NEXUS LOSS OCCURRED = 7
1615 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1616 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1619 return (sense
.asc
<< 8) | sense
.ascq
;
1622 void scsi_bus_set_ua(SCSIBus
*bus
, SCSISense sense
)
1625 if (sense
.key
!= UNIT_ATTENTION
) {
1630 * Override a pre-existing unit attention condition, except for a more
1631 * important reset condition.
1633 prec1
= scsi_ua_precedence(bus
->unit_attention
);
1634 prec2
= scsi_ua_precedence(sense
);
1635 if (prec2
< prec1
) {
1636 bus
->unit_attention
= sense
;
1640 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1643 if (sense
.key
!= UNIT_ATTENTION
) {
1646 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1647 sense
.asc
, sense
.ascq
);
1650 * Override a pre-existing unit attention condition, except for a more
1651 * important reset condition.
1653 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1654 prec2
= scsi_ua_precedence(sense
);
1655 if (prec2
< prec1
) {
1656 sdev
->unit_attention
= sense
;
1660 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1664 aio_context_acquire(blk_get_aio_context(sdev
->conf
.blk
));
1665 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1666 req
= QTAILQ_FIRST(&sdev
->requests
);
1667 scsi_req_cancel_async(req
, NULL
);
1669 blk_drain(sdev
->conf
.blk
);
1670 aio_context_release(blk_get_aio_context(sdev
->conf
.blk
));
1671 scsi_device_set_ua(sdev
, sense
);
1674 void scsi_device_drained_begin(SCSIDevice
*sdev
)
1676 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, sdev
->qdev
.parent_bus
);
1681 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1682 assert(bus
->drain_count
< INT_MAX
);
1685 * Multiple BlockBackends can be on a SCSIBus and each may begin/end
1686 * draining at any time. Keep a counter so HBAs only see begin/end once.
1688 if (bus
->drain_count
++ == 0) {
1689 trace_scsi_bus_drained_begin(bus
, sdev
);
1690 if (bus
->info
->drained_begin
) {
1691 bus
->info
->drained_begin(bus
);
1696 void scsi_device_drained_end(SCSIDevice
*sdev
)
1698 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, sdev
->qdev
.parent_bus
);
1703 assert(qemu_get_current_aio_context() == qemu_get_aio_context());
1704 assert(bus
->drain_count
> 0);
1706 if (bus
->drain_count
-- == 1) {
1707 trace_scsi_bus_drained_end(bus
, sdev
);
1708 if (bus
->info
->drained_end
) {
1709 bus
->info
->drained_end(bus
);
1714 static char *scsibus_get_dev_path(DeviceState
*dev
)
1716 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1717 DeviceState
*hba
= dev
->parent_bus
->parent
;
1721 id
= qdev_get_dev_path(hba
);
1723 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1725 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1731 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1733 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1734 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1735 qdev_fw_name(dev
), d
->id
, d
->lun
);
1738 /* SCSI request list. For simplicity, pv points to the whole device */
1740 static int put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1741 const VMStateField
*field
, JSONWriter
*vmdesc
)
1744 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1747 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1748 assert(!req
->io_canceled
);
1749 assert(req
->status
== -1 && req
->host_status
== -1);
1750 assert(req
->enqueued
);
1752 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1753 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1754 qemu_put_be32s(f
, &req
->tag
);
1755 qemu_put_be32s(f
, &req
->lun
);
1756 if (bus
->info
->save_request
) {
1757 bus
->info
->save_request(f
, req
);
1759 if (req
->ops
->save_request
) {
1760 req
->ops
->save_request(f
, req
);
1763 qemu_put_sbyte(f
, 0);
1768 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
,
1769 const VMStateField
*field
)
1772 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1775 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1776 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1781 qemu_get_buffer(f
, buf
, sizeof(buf
));
1782 qemu_get_be32s(f
, &tag
);
1783 qemu_get_be32s(f
, &lun
);
1785 * A too-short CDB would have been rejected by scsi_req_new, so just use
1786 * SCSI_CMD_BUF_SIZE as the CDB length.
1788 req
= scsi_req_new(s
, tag
, lun
, buf
, sizeof(buf
), NULL
);
1789 req
->retry
= (sbyte
== 1);
1790 if (bus
->info
->load_request
) {
1791 req
->hba_private
= bus
->info
->load_request(f
, req
);
1793 if (req
->ops
->load_request
) {
1794 req
->ops
->load_request(f
, req
);
1797 /* Just restart it later. */
1798 scsi_req_enqueue_internal(req
);
1800 /* At this point, the request will be kept alive by the reference
1801 * added by scsi_req_enqueue_internal, so we can release our reference.
1802 * The HBA of course will add its own reference in the load_request
1803 * callback if it needs to hold on the SCSIRequest.
1805 scsi_req_unref(req
);
1811 static const VMStateInfo vmstate_info_scsi_requests
= {
1812 .name
= "scsi-requests",
1813 .get
= get_scsi_requests
,
1814 .put
= put_scsi_requests
,
1817 static bool scsi_sense_state_needed(void *opaque
)
1819 SCSIDevice
*s
= opaque
;
1821 return s
->sense_len
> SCSI_SENSE_BUF_SIZE_OLD
;
1824 static const VMStateDescription vmstate_scsi_sense_state
= {
1825 .name
= "SCSIDevice/sense",
1827 .minimum_version_id
= 1,
1828 .needed
= scsi_sense_state_needed
,
1829 .fields
= (VMStateField
[]) {
1830 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
,
1831 SCSI_SENSE_BUF_SIZE_OLD
,
1832 SCSI_SENSE_BUF_SIZE
- SCSI_SENSE_BUF_SIZE_OLD
),
1833 VMSTATE_END_OF_LIST()
1837 const VMStateDescription vmstate_scsi_device
= {
1838 .name
= "SCSIDevice",
1840 .minimum_version_id
= 1,
1841 .fields
= (VMStateField
[]) {
1842 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1843 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1844 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1845 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1846 VMSTATE_UINT8_SUB_ARRAY(sense
, SCSIDevice
, 0, SCSI_SENSE_BUF_SIZE_OLD
),
1847 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1851 .field_exists
= NULL
,
1852 .size
= 0, /* ouch */
1853 .info
= &vmstate_info_scsi_requests
,
1854 .flags
= VMS_SINGLE
,
1857 VMSTATE_END_OF_LIST()
1859 .subsections
= (const VMStateDescription
*[]) {
1860 &vmstate_scsi_sense_state
,
1865 static Property scsi_props
[] = {
1866 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
1867 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
1868 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
1869 DEFINE_PROP_END_OF_LIST(),
1872 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1874 DeviceClass
*k
= DEVICE_CLASS(klass
);
1875 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1876 k
->bus_type
= TYPE_SCSI_BUS
;
1877 k
->realize
= scsi_qdev_realize
;
1878 k
->unrealize
= scsi_qdev_unrealize
;
1879 device_class_set_props(k
, scsi_props
);
1882 static void scsi_dev_instance_init(Object
*obj
)
1884 DeviceState
*dev
= DEVICE(obj
);
1885 SCSIDevice
*s
= SCSI_DEVICE(dev
);
1887 device_add_bootindex_property(obj
, &s
->conf
.bootindex
,
1892 static const TypeInfo scsi_device_type_info
= {
1893 .name
= TYPE_SCSI_DEVICE
,
1894 .parent
= TYPE_DEVICE
,
1895 .instance_size
= sizeof(SCSIDevice
),
1897 .class_size
= sizeof(SCSIDeviceClass
),
1898 .class_init
= scsi_device_class_init
,
1899 .instance_init
= scsi_dev_instance_init
,
1902 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
1904 BusClass
*k
= BUS_CLASS(klass
);
1905 HotplugHandlerClass
*hc
= HOTPLUG_HANDLER_CLASS(klass
);
1907 k
->get_dev_path
= scsibus_get_dev_path
;
1908 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
1909 k
->check_address
= scsi_bus_check_address
;
1910 hc
->unplug
= qdev_simple_device_unplug_cb
;
1913 static const TypeInfo scsi_bus_info
= {
1914 .name
= TYPE_SCSI_BUS
,
1916 .instance_size
= sizeof(SCSIBus
),
1917 .class_init
= scsi_bus_class_init
,
1918 .interfaces
= (InterfaceInfo
[]) {
1919 { TYPE_HOTPLUG_HANDLER
},
1924 static void scsi_register_types(void)
1926 type_register_static(&scsi_bus_info
);
1927 type_register_static(&scsi_device_type_info
);
1930 type_init(scsi_register_types
)