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 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
);
13 static void scsi_req_dequeue(SCSIRequest
*req
);
14 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
);
15 static void scsi_target_free_buf(SCSIRequest
*req
);
17 static Property scsi_props
[] = {
18 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
19 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
20 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
21 DEFINE_PROP_END_OF_LIST(),
24 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
26 BusClass
*k
= BUS_CLASS(klass
);
28 k
->get_dev_path
= scsibus_get_dev_path
;
29 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
32 static const TypeInfo scsi_bus_info
= {
33 .name
= TYPE_SCSI_BUS
,
35 .instance_size
= sizeof(SCSIBus
),
36 .class_init
= scsi_bus_class_init
,
38 static int next_scsi_bus
;
40 static int scsi_device_init(SCSIDevice
*s
)
42 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
49 static void scsi_device_destroy(SCSIDevice
*s
)
51 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
57 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
58 uint8_t *buf
, void *hba_private
)
60 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
62 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
68 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
70 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
71 if (sc
->unit_attention_reported
) {
72 sc
->unit_attention_reported(s
);
76 /* Create a scsi bus, and attach devices to it. */
77 void scsi_bus_new(SCSIBus
*bus
, size_t bus_size
, DeviceState
*host
,
78 const SCSIBusInfo
*info
, const char *bus_name
)
80 qbus_create_inplace(bus
, bus_size
, TYPE_SCSI_BUS
, host
, bus_name
);
81 bus
->busnr
= next_scsi_bus
++;
83 bus
->qbus
.allow_hotplug
= 1;
86 static void scsi_dma_restart_bh(void *opaque
)
88 SCSIDevice
*s
= opaque
;
89 SCSIRequest
*req
, *next
;
91 qemu_bh_delete(s
->bh
);
94 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
98 switch (req
->cmd
.mode
) {
99 case SCSI_XFER_FROM_DEV
:
100 case SCSI_XFER_TO_DEV
:
101 scsi_req_continue(req
);
105 scsi_req_dequeue(req
);
106 scsi_req_enqueue(req
);
114 void scsi_req_retry(SCSIRequest
*req
)
116 /* No need to save a reference, because scsi_dma_restart_bh just
117 * looks at the request list. */
121 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
123 SCSIDevice
*s
= opaque
;
129 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
130 qemu_bh_schedule(s
->bh
);
134 static int scsi_qdev_init(DeviceState
*qdev
)
136 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
137 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
141 if (dev
->channel
> bus
->info
->max_channel
) {
142 error_report("bad scsi channel id: %d", dev
->channel
);
145 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
146 error_report("bad scsi device id: %d", dev
->id
);
149 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
150 error_report("bad scsi device lun: %d", dev
->lun
);
156 if (dev
->lun
== -1) {
160 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
161 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
162 if (d
&& d
->lun
== dev
->lun
) {
163 error_report("no free target");
167 } else if (dev
->lun
== -1) {
170 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
171 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
172 if (d
&& d
->lun
== lun
) {
173 error_report("no free lun");
178 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
180 if (d
->lun
== dev
->lun
&& dev
!= d
) {
181 object_unparent(OBJECT(d
));
185 QTAILQ_INIT(&dev
->requests
);
186 rc
= scsi_device_init(dev
);
188 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
192 if (bus
->info
->hotplug
) {
193 bus
->info
->hotplug(bus
, dev
);
200 static int scsi_qdev_exit(DeviceState
*qdev
)
202 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
205 qemu_del_vm_change_state_handler(dev
->vmsentry
);
207 scsi_device_destroy(dev
);
211 /* handle legacy '-drive if=scsi,...' cmd line args */
212 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
213 int unit
, bool removable
, int bootindex
,
214 const char *serial
, Error
**errp
)
220 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
221 dev
= qdev_create(&bus
->qbus
, driver
);
222 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
223 if (bootindex
>= 0) {
224 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
226 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
227 qdev_prop_set_bit(dev
, "removable", removable
);
229 if (serial
&& object_property_find(OBJECT(dev
), "serial", NULL
)) {
230 qdev_prop_set_string(dev
, "serial", serial
);
232 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
233 error_setg(errp
, "Setting drive property failed");
234 object_unparent(OBJECT(dev
));
237 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
239 error_propagate(errp
, err
);
240 object_unparent(OBJECT(dev
));
243 return SCSI_DEVICE(dev
);
246 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
, Error
**errp
)
254 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
255 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
259 qemu_opts_loc_restore(dinfo
->opts
);
260 scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1, NULL
,
263 error_propagate(errp
, err
);
270 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
272 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
273 scsi_req_complete(req
, CHECK_CONDITION
);
277 static const struct SCSIReqOps reqops_invalid_field
= {
278 .size
= sizeof(SCSIRequest
),
279 .send_command
= scsi_invalid_field
282 /* SCSIReqOps implementation for invalid commands. */
284 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
286 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
287 scsi_req_complete(req
, CHECK_CONDITION
);
291 static const struct SCSIReqOps reqops_invalid_opcode
= {
292 .size
= sizeof(SCSIRequest
),
293 .send_command
= scsi_invalid_command
296 /* SCSIReqOps implementation for unit attention conditions. */
298 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
300 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
301 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
302 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
303 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
305 scsi_req_complete(req
, CHECK_CONDITION
);
309 static const struct SCSIReqOps reqops_unit_attention
= {
310 .size
= sizeof(SCSIRequest
),
311 .send_command
= scsi_unit_attention
314 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
317 typedef struct SCSITargetReq SCSITargetReq
;
319 struct SCSITargetReq
{
326 static void store_lun(uint8_t *outbuf
, int lun
)
332 outbuf
[1] = (lun
& 255);
333 outbuf
[0] = (lun
>> 8) | 0x40;
336 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
343 if (r
->req
.cmd
.xfer
< 16) {
346 if (r
->req
.cmd
.buf
[2] > 2) {
349 channel
= r
->req
.dev
->channel
;
353 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
354 DeviceState
*qdev
= kid
->child
;
355 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
357 if (dev
->channel
== channel
&& dev
->id
== id
) {
368 scsi_target_alloc_buf(&r
->req
, n
+ 8);
370 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
371 memset(r
->buf
, 0, len
);
372 stl_be_p(&r
->buf
[0], n
);
373 i
= found_lun0
? 8 : 16;
374 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
375 DeviceState
*qdev
= kid
->child
;
376 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
378 if (dev
->channel
== channel
&& dev
->id
== id
) {
379 store_lun(&r
->buf
[i
], dev
->lun
);
388 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
390 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
392 scsi_target_alloc_buf(&r
->req
, SCSI_INQUIRY_LEN
);
394 if (r
->req
.cmd
.buf
[1] & 0x2) {
395 /* Command support data - optional, not implemented */
399 if (r
->req
.cmd
.buf
[1] & 0x1) {
400 /* Vital product data */
401 uint8_t page_code
= r
->req
.cmd
.buf
[2];
402 r
->buf
[r
->len
++] = page_code
; /* this page */
403 r
->buf
[r
->len
++] = 0x00;
406 case 0x00: /* Supported page codes, mandatory */
410 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
411 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
418 assert(r
->len
< r
->buf_len
);
419 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
423 /* Standard INQUIRY data */
424 if (r
->req
.cmd
.buf
[2] != 0) {
429 r
->len
= MIN(r
->req
.cmd
.xfer
, SCSI_INQUIRY_LEN
);
430 memset(r
->buf
, 0, r
->len
);
431 if (r
->req
.lun
!= 0) {
432 r
->buf
[0] = TYPE_NO_LUN
;
434 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
435 r
->buf
[2] = 5; /* Version */
436 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
437 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
438 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
439 memcpy(&r
->buf
[8], "QEMU ", 8);
440 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
441 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
446 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
448 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
452 if (!scsi_target_emulate_report_luns(r
)) {
453 goto illegal_request
;
457 if (!scsi_target_emulate_inquiry(r
)) {
458 goto illegal_request
;
462 scsi_target_alloc_buf(&r
->req
, SCSI_SENSE_LEN
);
463 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
464 MIN(req
->cmd
.xfer
, r
->buf_len
),
465 (req
->cmd
.buf
[1] & 1) == 0);
466 if (r
->req
.dev
->sense_is_ua
) {
467 scsi_device_unit_attention_reported(req
->dev
);
468 r
->req
.dev
->sense_len
= 0;
469 r
->req
.dev
->sense_is_ua
= false;
473 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
474 scsi_req_complete(req
, CHECK_CONDITION
);
477 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
478 scsi_req_complete(req
, CHECK_CONDITION
);
483 scsi_req_complete(req
, GOOD
);
488 static void scsi_target_read_data(SCSIRequest
*req
)
490 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
496 scsi_req_data(&r
->req
, n
);
498 scsi_req_complete(&r
->req
, GOOD
);
502 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
504 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
509 static uint8_t *scsi_target_alloc_buf(SCSIRequest
*req
, size_t len
)
511 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
513 r
->buf
= g_malloc(len
);
519 static void scsi_target_free_buf(SCSIRequest
*req
)
521 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
526 static const struct SCSIReqOps reqops_target_command
= {
527 .size
= sizeof(SCSITargetReq
),
528 .send_command
= scsi_target_send_command
,
529 .read_data
= scsi_target_read_data
,
530 .get_buf
= scsi_target_get_buf
,
531 .free_req
= scsi_target_free_buf
,
535 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
536 uint32_t tag
, uint32_t lun
, void *hba_private
)
539 SCSIBus
*bus
= scsi_bus_from_device(d
);
540 BusState
*qbus
= BUS(bus
);
542 req
= g_malloc0(reqops
->size
);
548 req
->hba_private
= hba_private
;
552 object_ref(OBJECT(d
));
553 object_ref(OBJECT(qbus
->parent
));
554 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
558 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
559 uint8_t *buf
, void *hba_private
)
561 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
565 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
566 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
567 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
569 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
572 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
576 if (cmd
.xfer
> INT32_MAX
) {
577 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
578 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
579 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
580 (buf
[0] != INQUIRY
&&
581 buf
[0] != REPORT_LUNS
&&
582 buf
[0] != GET_CONFIGURATION
&&
583 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
586 * If we already have a pending unit attention condition,
587 * report this one before triggering another one.
589 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
590 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
592 } else if (lun
!= d
->lun
||
593 buf
[0] == REPORT_LUNS
||
594 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
595 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
598 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
603 req
->resid
= req
->cmd
.xfer
;
607 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
609 case TEST_UNIT_READY
:
610 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
613 trace_scsi_report_luns(d
->id
, lun
, tag
);
616 trace_scsi_request_sense(d
->id
, lun
, tag
);
625 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
627 return req
->ops
->get_buf(req
);
630 static void scsi_clear_unit_attention(SCSIRequest
*req
)
633 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
634 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
639 * If an INQUIRY command enters the enabled command state,
640 * the device server shall [not] clear any unit attention condition;
641 * See also MMC-6, paragraphs 6.5 and 6.6.2.
643 if (req
->cmd
.buf
[0] == INQUIRY
||
644 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
645 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
649 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
650 ua
= &req
->dev
->unit_attention
;
652 ua
= &req
->bus
->unit_attention
;
656 * If a REPORT LUNS command enters the enabled command state, [...]
657 * the device server shall clear any pending unit attention condition
658 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
660 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
661 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
662 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
666 *ua
= SENSE_CODE(NO_SENSE
);
669 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
674 if (!req
->sense_len
) {
678 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
681 * FIXME: clearing unit attention conditions upon autosense should be done
682 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
685 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
686 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
687 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
689 if (req
->dev
->sense_is_ua
) {
690 scsi_device_unit_attention_reported(req
->dev
);
691 req
->dev
->sense_len
= 0;
692 req
->dev
->sense_is_ua
= false;
697 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
699 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
702 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
704 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
705 sense
.key
, sense
.asc
, sense
.ascq
);
706 memset(req
->sense
, 0, 18);
707 req
->sense
[0] = 0x70;
708 req
->sense
[2] = sense
.key
;
710 req
->sense
[12] = sense
.asc
;
711 req
->sense
[13] = sense
.ascq
;
715 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
717 assert(!req
->enqueued
);
719 if (req
->bus
->info
->get_sg_list
) {
720 req
->sg
= req
->bus
->info
->get_sg_list(req
);
724 req
->enqueued
= true;
725 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
728 int32_t scsi_req_enqueue(SCSIRequest
*req
)
733 scsi_req_enqueue_internal(req
);
735 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
740 static void scsi_req_dequeue(SCSIRequest
*req
)
742 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
745 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
746 req
->enqueued
= false;
751 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
753 /* MMC-6, paragraph 6.7. */
756 if ((data_type
& 3) == 0) {
757 /* Each descriptor is as in Table 295 - Nominal performance. */
758 return 16 * num_desc
+ 8;
760 /* Each descriptor is as in Table 296 - Exceptions. */
761 return 6 * num_desc
+ 8;
766 return 8 * num_desc
+ 8;
768 return 2048 * num_desc
+ 8;
770 return 16 * num_desc
+ 8;
776 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
778 int byte_block
= (buf
[2] >> 2) & 0x1;
779 int type
= (buf
[2] >> 4) & 0x1;
784 xfer_unit
= dev
->blocksize
;
795 static int ata_passthrough_12_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
797 int length
= buf
[2] & 0x3;
799 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
803 case 3: /* USB-specific. */
818 static int ata_passthrough_16_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
820 int extend
= buf
[1] & 0x1;
821 int length
= buf
[2] & 0x3;
823 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
827 case 3: /* USB-specific. */
833 xfer
|= (extend
? buf
[3] << 8 : 0);
837 xfer
|= (extend
? buf
[5] << 8 : 0);
844 uint32_t scsi_data_cdb_length(uint8_t *buf
)
846 if ((buf
[0] >> 5) == 0 && buf
[4] == 0) {
849 return scsi_cdb_length(buf
);
853 uint32_t scsi_cdb_length(uint8_t *buf
)
855 switch (buf
[0] >> 5) {
861 return lduw_be_p(&buf
[7]);
864 return ldl_be_p(&buf
[10]) & 0xffffffffULL
;
867 return ldl_be_p(&buf
[6]) & 0xffffffffULL
;
874 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
876 cmd
->xfer
= scsi_cdb_length(buf
);
878 case TEST_UNIT_READY
:
882 case WRITE_FILEMARKS
:
883 case WRITE_FILEMARKS_16
:
888 case ALLOW_MEDIUM_REMOVAL
:
890 case SYNCHRONIZE_CACHE
:
891 case SYNCHRONIZE_CACHE_16
:
893 case LOCK_UNLOCK_CACHE
:
902 case ALLOW_OVERWRITE
:
908 if ((buf
[1] & 2) == 0) {
910 } else if ((buf
[1] & 4) == 1) {
913 cmd
->xfer
*= dev
->blocksize
;
919 cmd
->xfer
= dev
->blocksize
;
921 case READ_CAPACITY_10
:
924 case READ_BLOCK_LIMITS
:
927 case SEND_VOLUME_TAG
:
928 /* GPCMD_SET_STREAMING from multimedia commands. */
929 if (dev
->type
== TYPE_ROM
) {
930 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
932 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
936 /* length 0 means 256 blocks */
937 if (cmd
->xfer
== 0) {
941 case WRITE_VERIFY_10
:
943 case WRITE_VERIFY_12
:
945 case WRITE_VERIFY_16
:
946 cmd
->xfer
*= dev
->blocksize
;
950 /* length 0 means 256 blocks */
951 if (cmd
->xfer
== 0) {
955 case RECOVER_BUFFERED_DATA
:
958 cmd
->xfer
*= dev
->blocksize
;
961 /* MMC mandates the parameter list to be 12-bytes long. Parameters
962 * for block devices are restricted to the header right now. */
963 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
966 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
970 case RECEIVE_DIAGNOSTIC
:
971 case SEND_DIAGNOSTIC
:
972 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
978 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
980 case PERSISTENT_RESERVE_OUT
:
981 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
984 if (dev
->type
== TYPE_ROM
) {
985 /* MMC command GET PERFORMANCE. */
986 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
987 buf
[10], buf
[1] & 0x1f);
990 case MECHANISM_STATUS
:
991 case READ_DVD_STRUCTURE
:
992 case SEND_DVD_STRUCTURE
:
993 case MAINTENANCE_OUT
:
995 if (dev
->type
== TYPE_ROM
) {
996 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
997 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
1000 case ATA_PASSTHROUGH_12
:
1001 if (dev
->type
== TYPE_ROM
) {
1002 /* BLANK command of MMC */
1005 cmd
->xfer
= ata_passthrough_12_xfer_size(dev
, buf
);
1008 case ATA_PASSTHROUGH_16
:
1009 cmd
->xfer
= ata_passthrough_16_xfer_size(dev
, buf
);
1015 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1018 /* stream commands */
1025 case RECOVER_BUFFERED_DATA
:
1027 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
1028 if (buf
[1] & 0x01) { /* fixed */
1029 cmd
->xfer
*= dev
->blocksize
;
1033 case READ_REVERSE_16
:
1036 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
1037 if (buf
[1] & 0x01) { /* fixed */
1038 cmd
->xfer
*= dev
->blocksize
;
1046 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
1049 switch (buf
[1] & 0x1f) /* operation code */ {
1050 case SHORT_FORM_BLOCK_ID
:
1051 case SHORT_FORM_VENDOR_SPECIFIC
:
1058 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1066 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1068 /* generic commands */
1070 return scsi_req_length(cmd
, dev
, buf
);
1075 static int scsi_req_medium_changer_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1078 /* medium changer commands */
1079 case EXCHANGE_MEDIUM
:
1080 case INITIALIZE_ELEMENT_STATUS
:
1081 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1083 case POSITION_TO_ELEMENT
:
1086 case READ_ELEMENT_STATUS
:
1087 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1090 /* generic commands */
1092 return scsi_req_length(cmd
, dev
, buf
);
1098 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1101 cmd
->mode
= SCSI_XFER_NONE
;
1104 switch (cmd
->buf
[0]) {
1107 case WRITE_VERIFY_10
:
1109 case WRITE_VERIFY_12
:
1111 case WRITE_VERIFY_16
:
1118 case CHANGE_DEFINITION
:
1121 case MODE_SELECT_10
:
1122 case SEND_DIAGNOSTIC
:
1125 case REASSIGN_BLOCKS
:
1134 case SEARCH_HIGH_12
:
1135 case SEARCH_EQUAL_12
:
1138 case SEND_VOLUME_TAG
:
1139 case SEND_CUE_SHEET
:
1140 case SEND_DVD_STRUCTURE
:
1141 case PERSISTENT_RESERVE_OUT
:
1142 case MAINTENANCE_OUT
:
1143 cmd
->mode
= SCSI_XFER_TO_DEV
;
1145 case ATA_PASSTHROUGH_12
:
1146 case ATA_PASSTHROUGH_16
:
1148 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1149 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1152 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1157 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1159 uint8_t *buf
= cmd
->buf
;
1162 switch (buf
[0] >> 5) {
1164 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1169 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1172 lba
= ldq_be_p(&buf
[2]);
1181 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1185 switch (buf
[0] >> 5) {
1203 switch (dev
->type
) {
1205 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1207 case TYPE_MEDIUM_CHANGER
:
1208 rc
= scsi_req_medium_changer_length(cmd
, dev
, buf
);
1211 rc
= scsi_req_length(cmd
, dev
, buf
);
1218 memcpy(cmd
->buf
, buf
, cmd
->len
);
1219 scsi_cmd_xfer_mode(cmd
);
1220 cmd
->lba
= scsi_cmd_lba(cmd
);
1224 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1226 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1228 scsi_device_set_ua(dev
, sense
);
1229 if (bus
->info
->change
) {
1230 bus
->info
->change(bus
, dev
, sense
);
1235 * Predefined sense codes
1238 /* No sense data available */
1239 const struct SCSISense sense_code_NO_SENSE
= {
1240 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1243 /* LUN not ready, Manual intervention required */
1244 const struct SCSISense sense_code_LUN_NOT_READY
= {
1245 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1248 /* LUN not ready, Medium not present */
1249 const struct SCSISense sense_code_NO_MEDIUM
= {
1250 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1253 /* LUN not ready, medium removal prevented */
1254 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1255 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1258 /* Hardware error, internal target failure */
1259 const struct SCSISense sense_code_TARGET_FAILURE
= {
1260 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1263 /* Illegal request, invalid command operation code */
1264 const struct SCSISense sense_code_INVALID_OPCODE
= {
1265 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1268 /* Illegal request, LBA out of range */
1269 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1270 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1273 /* Illegal request, Invalid field in CDB */
1274 const struct SCSISense sense_code_INVALID_FIELD
= {
1275 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1278 /* Illegal request, Invalid field in parameter list */
1279 const struct SCSISense sense_code_INVALID_PARAM
= {
1280 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1283 /* Illegal request, Parameter list length error */
1284 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1285 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1288 /* Illegal request, LUN not supported */
1289 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1290 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1293 /* Illegal request, Saving parameters not supported */
1294 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1295 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1298 /* Illegal request, Incompatible medium installed */
1299 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1300 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1303 /* Illegal request, medium removal prevented */
1304 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1305 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1308 /* Illegal request, Invalid Transfer Tag */
1309 const struct SCSISense sense_code_INVALID_TAG
= {
1310 .key
= ILLEGAL_REQUEST
, .asc
= 0x4b, .ascq
= 0x01
1313 /* Command aborted, I/O process terminated */
1314 const struct SCSISense sense_code_IO_ERROR
= {
1315 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1318 /* Command aborted, I_T Nexus loss occurred */
1319 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1320 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1323 /* Command aborted, Logical Unit failure */
1324 const struct SCSISense sense_code_LUN_FAILURE
= {
1325 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1328 /* Command aborted, Overlapped Commands Attempted */
1329 const struct SCSISense sense_code_OVERLAPPED_COMMANDS
= {
1330 .key
= ABORTED_COMMAND
, .asc
= 0x4e, .ascq
= 0x00
1333 /* Unit attention, Capacity data has changed */
1334 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1335 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1338 /* Unit attention, Power on, reset or bus device reset occurred */
1339 const struct SCSISense sense_code_RESET
= {
1340 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1343 /* Unit attention, No medium */
1344 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1345 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1348 /* Unit attention, Medium may have changed */
1349 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1350 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1353 /* Unit attention, Reported LUNs data has changed */
1354 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1355 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1358 /* Unit attention, Device internal reset */
1359 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1360 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1363 /* Data Protection, Write Protected */
1364 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1365 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1371 * Convert between fixed and descriptor sense buffers
1373 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1374 uint8_t *buf
, int len
, bool fixed
)
1378 if (!fixed
&& len
< 8) {
1383 sense
.key
= NO_SENSE
;
1387 fixed_in
= (in_buf
[0] & 2) == 0;
1389 if (fixed
== fixed_in
) {
1390 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1391 return MIN(len
, in_len
);
1395 sense
.key
= in_buf
[2];
1396 sense
.asc
= in_buf
[12];
1397 sense
.ascq
= in_buf
[13];
1399 sense
.key
= in_buf
[1];
1400 sense
.asc
= in_buf
[2];
1401 sense
.ascq
= in_buf
[3];
1405 memset(buf
, 0, len
);
1407 /* Return fixed format sense buffer */
1411 buf
[12] = sense
.asc
;
1412 buf
[13] = sense
.ascq
;
1413 return MIN(len
, SCSI_SENSE_LEN
);
1415 /* Return descriptor format sense buffer */
1419 buf
[3] = sense
.ascq
;
1424 static const char *scsi_command_name(uint8_t cmd
)
1426 static const char *names
[] = {
1427 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1428 [ REWIND
] = "REWIND",
1429 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1430 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1431 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1432 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1433 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1434 [ READ_6
] = "READ_6",
1435 [ WRITE_6
] = "WRITE_6",
1436 [ SET_CAPACITY
] = "SET_CAPACITY",
1437 [ READ_REVERSE
] = "READ_REVERSE",
1438 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1439 [ SPACE
] = "SPACE",
1440 [ INQUIRY
] = "INQUIRY",
1441 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1442 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1443 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1444 [ MODE_SELECT
] = "MODE_SELECT",
1445 [ RESERVE
] = "RESERVE",
1446 [ RELEASE
] = "RELEASE",
1448 [ ERASE
] = "ERASE",
1449 [ MODE_SENSE
] = "MODE_SENSE",
1450 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1451 /* LOAD_UNLOAD and START_STOP use the same operation code */
1452 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1453 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1454 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1455 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1456 [ READ_10
] = "READ_10",
1457 [ WRITE_10
] = "WRITE_10",
1458 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1459 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1460 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1461 [ VERIFY_10
] = "VERIFY_10",
1462 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1463 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1464 [ SEARCH_LOW
] = "SEARCH_LOW",
1465 [ SET_LIMITS
] = "SET_LIMITS",
1466 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1467 /* READ_POSITION and PRE_FETCH use the same operation code */
1468 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1469 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1470 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1471 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1472 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1473 [ COMPARE
] = "COMPARE",
1474 [ COPY_VERIFY
] = "COPY_VERIFY",
1475 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1476 [ READ_BUFFER
] = "READ_BUFFER",
1477 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1478 [ READ_LONG_10
] = "READ_LONG_10",
1479 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1480 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1481 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1482 [ UNMAP
] = "UNMAP",
1483 [ READ_TOC
] = "READ_TOC",
1484 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1485 [ SANITIZE
] = "SANITIZE",
1486 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1487 [ LOG_SELECT
] = "LOG_SELECT",
1488 [ LOG_SENSE
] = "LOG_SENSE",
1489 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1490 [ RESERVE_10
] = "RESERVE_10",
1491 [ RELEASE_10
] = "RELEASE_10",
1492 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1493 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1494 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1495 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1496 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1497 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1498 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1499 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1500 [ READ_16
] = "READ_16",
1501 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1502 [ WRITE_16
] = "WRITE_16",
1503 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1504 [ VERIFY_16
] = "VERIFY_16",
1505 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1506 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1507 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1508 [ LOCATE_16
] = "LOCATE_16",
1509 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1510 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1511 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1512 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1513 [ REPORT_LUNS
] = "REPORT_LUNS",
1514 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1515 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1516 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1517 [ READ_12
] = "READ_12",
1518 [ WRITE_12
] = "WRITE_12",
1519 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1520 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1521 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1522 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1523 [ VERIFY_12
] = "VERIFY_12",
1524 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1525 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1526 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1527 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1528 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1529 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1530 [ READ_CD
] = "READ_CD",
1531 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1532 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1533 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1534 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1535 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1536 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1537 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1538 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1539 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1542 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1547 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1549 assert(req
->refcount
> 0);
1554 void scsi_req_unref(SCSIRequest
*req
)
1556 assert(req
->refcount
> 0);
1557 if (--req
->refcount
== 0) {
1558 BusState
*qbus
= req
->dev
->qdev
.parent_bus
;
1559 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, qbus
);
1561 if (bus
->info
->free_request
&& req
->hba_private
) {
1562 bus
->info
->free_request(bus
, req
->hba_private
);
1564 if (req
->ops
->free_req
) {
1565 req
->ops
->free_req(req
);
1567 object_unref(OBJECT(req
->dev
));
1568 object_unref(OBJECT(qbus
->parent
));
1573 /* Tell the device that we finished processing this chunk of I/O. It
1574 will start the next chunk or complete the command. */
1575 void scsi_req_continue(SCSIRequest
*req
)
1577 if (req
->io_canceled
) {
1578 trace_scsi_req_continue_canceled(req
->dev
->id
, req
->lun
, req
->tag
);
1581 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1582 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1583 req
->ops
->write_data(req
);
1585 req
->ops
->read_data(req
);
1589 /* Called by the devices when data is ready for the HBA. The HBA should
1590 start a DMA operation to read or fill the device's data buffer.
1591 Once it completes, calling scsi_req_continue will restart I/O. */
1592 void scsi_req_data(SCSIRequest
*req
, int len
)
1595 if (req
->io_canceled
) {
1596 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1599 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1600 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1603 req
->bus
->info
->transfer_data(req
, len
);
1607 /* If the device calls scsi_req_data and the HBA specified a
1608 * scatter/gather list, the transfer has to happen in a single
1610 assert(!req
->dma_started
);
1611 req
->dma_started
= true;
1613 buf
= scsi_req_get_buf(req
);
1614 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1615 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1617 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1619 scsi_req_continue(req
);
1622 void scsi_req_print(SCSIRequest
*req
)
1627 fprintf(fp
, "[%s id=%d] %s",
1628 req
->dev
->qdev
.parent_bus
->name
,
1630 scsi_command_name(req
->cmd
.buf
[0]));
1631 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1632 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1634 switch (req
->cmd
.mode
) {
1635 case SCSI_XFER_NONE
:
1636 fprintf(fp
, " - none\n");
1638 case SCSI_XFER_FROM_DEV
:
1639 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1641 case SCSI_XFER_TO_DEV
:
1642 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1645 fprintf(fp
, " - Oops\n");
1650 void scsi_req_complete(SCSIRequest
*req
, int status
)
1652 assert(req
->status
== -1);
1653 req
->status
= status
;
1655 assert(req
->sense_len
<= sizeof(req
->sense
));
1656 if (status
== GOOD
) {
1660 if (req
->sense_len
) {
1661 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1662 req
->dev
->sense_len
= req
->sense_len
;
1663 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1665 req
->dev
->sense_len
= 0;
1666 req
->dev
->sense_is_ua
= false;
1670 * Unit attention state is now stored in the device's sense buffer
1671 * if the HBA didn't do autosense. Clear the pending unit attention
1674 scsi_clear_unit_attention(req
);
1677 scsi_req_dequeue(req
);
1678 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1679 scsi_req_unref(req
);
1682 void scsi_req_cancel(SCSIRequest
*req
)
1684 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1685 if (!req
->enqueued
) {
1689 scsi_req_dequeue(req
);
1690 req
->io_canceled
= true;
1691 if (req
->ops
->cancel_io
) {
1692 req
->ops
->cancel_io(req
);
1694 if (req
->bus
->info
->cancel
) {
1695 req
->bus
->info
->cancel(req
);
1697 scsi_req_unref(req
);
1700 void scsi_req_abort(SCSIRequest
*req
, int status
)
1702 if (!req
->enqueued
) {
1706 scsi_req_dequeue(req
);
1707 req
->io_canceled
= true;
1708 if (req
->ops
->cancel_io
) {
1709 req
->ops
->cancel_io(req
);
1711 scsi_req_complete(req
, status
);
1712 scsi_req_unref(req
);
1715 static int scsi_ua_precedence(SCSISense sense
)
1717 if (sense
.key
!= UNIT_ATTENTION
) {
1720 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1721 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1723 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1724 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1726 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1727 /* These two go with "all others". */
1729 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1730 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1731 * POWER ON OCCURRED = 1
1732 * SCSI BUS RESET OCCURRED = 2
1733 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1734 * I_T NEXUS LOSS OCCURRED = 7
1737 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1738 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1741 return (sense
.asc
<< 8) | sense
.ascq
;
1744 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1747 if (sense
.key
!= UNIT_ATTENTION
) {
1750 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1751 sense
.asc
, sense
.ascq
);
1754 * Override a pre-existing unit attention condition, except for a more
1755 * important reset condition.
1757 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1758 prec2
= scsi_ua_precedence(sense
);
1759 if (prec2
< prec1
) {
1760 sdev
->unit_attention
= sense
;
1764 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1768 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1769 req
= QTAILQ_FIRST(&sdev
->requests
);
1770 scsi_req_cancel(req
);
1773 scsi_device_set_ua(sdev
, sense
);
1776 static char *scsibus_get_dev_path(DeviceState
*dev
)
1778 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1779 DeviceState
*hba
= dev
->parent_bus
->parent
;
1783 id
= qdev_get_dev_path(hba
);
1785 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1787 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1793 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1795 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1796 return g_strdup_printf("channel@%x/%s@%x,%x", d
->channel
,
1797 qdev_fw_name(dev
), d
->id
, d
->lun
);
1800 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1803 SCSIDevice
*target_dev
= NULL
;
1805 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1806 DeviceState
*qdev
= kid
->child
;
1807 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1809 if (dev
->channel
== channel
&& dev
->id
== id
) {
1810 if (dev
->lun
== lun
) {
1819 /* SCSI request list. For simplicity, pv points to the whole device */
1821 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1824 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1827 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1828 assert(!req
->io_canceled
);
1829 assert(req
->status
== -1);
1830 assert(req
->enqueued
);
1832 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1833 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1834 qemu_put_be32s(f
, &req
->tag
);
1835 qemu_put_be32s(f
, &req
->lun
);
1836 if (bus
->info
->save_request
) {
1837 bus
->info
->save_request(f
, req
);
1839 if (req
->ops
->save_request
) {
1840 req
->ops
->save_request(f
, req
);
1843 qemu_put_sbyte(f
, 0);
1846 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1849 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1852 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1853 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1858 qemu_get_buffer(f
, buf
, sizeof(buf
));
1859 qemu_get_be32s(f
, &tag
);
1860 qemu_get_be32s(f
, &lun
);
1861 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1862 req
->retry
= (sbyte
== 1);
1863 if (bus
->info
->load_request
) {
1864 req
->hba_private
= bus
->info
->load_request(f
, req
);
1866 if (req
->ops
->load_request
) {
1867 req
->ops
->load_request(f
, req
);
1870 /* Just restart it later. */
1871 scsi_req_enqueue_internal(req
);
1873 /* At this point, the request will be kept alive by the reference
1874 * added by scsi_req_enqueue_internal, so we can release our reference.
1875 * The HBA of course will add its own reference in the load_request
1876 * callback if it needs to hold on the SCSIRequest.
1878 scsi_req_unref(req
);
1884 static int scsi_qdev_unplug(DeviceState
*qdev
)
1886 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1887 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1889 if (bus
->info
->hot_unplug
) {
1890 bus
->info
->hot_unplug(bus
, dev
);
1892 return qdev_simple_unplug_cb(qdev
);
1895 static const VMStateInfo vmstate_info_scsi_requests
= {
1896 .name
= "scsi-requests",
1897 .get
= get_scsi_requests
,
1898 .put
= put_scsi_requests
,
1901 const VMStateDescription vmstate_scsi_device
= {
1902 .name
= "SCSIDevice",
1904 .minimum_version_id
= 1,
1905 .minimum_version_id_old
= 1,
1906 .fields
= (VMStateField
[]) {
1907 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1908 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1909 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1910 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1911 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1912 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1916 .field_exists
= NULL
,
1917 .size
= 0, /* ouch */
1918 .info
= &vmstate_info_scsi_requests
,
1919 .flags
= VMS_SINGLE
,
1922 VMSTATE_END_OF_LIST()
1926 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1928 DeviceClass
*k
= DEVICE_CLASS(klass
);
1929 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
1930 k
->bus_type
= TYPE_SCSI_BUS
;
1931 k
->init
= scsi_qdev_init
;
1932 k
->unplug
= scsi_qdev_unplug
;
1933 k
->exit
= scsi_qdev_exit
;
1934 k
->props
= scsi_props
;
1937 static const TypeInfo scsi_device_type_info
= {
1938 .name
= TYPE_SCSI_DEVICE
,
1939 .parent
= TYPE_DEVICE
,
1940 .instance_size
= sizeof(SCSIDevice
),
1942 .class_size
= sizeof(SCSIDeviceClass
),
1943 .class_init
= scsi_device_class_init
,
1946 static void scsi_register_types(void)
1948 type_register_static(&scsi_bus_info
);
1949 type_register_static(&scsi_device_type_info
);
1952 type_init(scsi_register_types
)