2 #include "qemu-error.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
);
15 static Property scsi_props
[] = {
16 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
17 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
18 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
19 DEFINE_PROP_END_OF_LIST(),
22 static void scsi_bus_class_init(ObjectClass
*klass
, void *data
)
24 BusClass
*k
= BUS_CLASS(klass
);
26 k
->get_dev_path
= scsibus_get_dev_path
;
27 k
->get_fw_dev_path
= scsibus_get_fw_dev_path
;
30 static const TypeInfo scsi_bus_info
= {
31 .name
= TYPE_SCSI_BUS
,
33 .instance_size
= sizeof(SCSIBus
),
34 .class_init
= scsi_bus_class_init
,
36 static int next_scsi_bus
;
38 static int scsi_device_init(SCSIDevice
*s
)
40 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
47 static void scsi_device_destroy(SCSIDevice
*s
)
49 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
55 static SCSIRequest
*scsi_device_alloc_req(SCSIDevice
*s
, uint32_t tag
, uint32_t lun
,
56 uint8_t *buf
, void *hba_private
)
58 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
60 return sc
->alloc_req(s
, tag
, lun
, buf
, hba_private
);
66 static void scsi_device_unit_attention_reported(SCSIDevice
*s
)
68 SCSIDeviceClass
*sc
= SCSI_DEVICE_GET_CLASS(s
);
69 if (sc
->unit_attention_reported
) {
70 sc
->unit_attention_reported(s
);
74 /* Create a scsi bus, and attach devices to it. */
75 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
77 qbus_create_inplace(&bus
->qbus
, TYPE_SCSI_BUS
, host
, NULL
);
78 bus
->busnr
= next_scsi_bus
++;
80 bus
->qbus
.allow_hotplug
= 1;
83 static void scsi_dma_restart_bh(void *opaque
)
85 SCSIDevice
*s
= opaque
;
86 SCSIRequest
*req
, *next
;
88 qemu_bh_delete(s
->bh
);
91 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
95 switch (req
->cmd
.mode
) {
96 case SCSI_XFER_FROM_DEV
:
97 case SCSI_XFER_TO_DEV
:
98 scsi_req_continue(req
);
102 scsi_req_dequeue(req
);
103 scsi_req_enqueue(req
);
111 void scsi_req_retry(SCSIRequest
*req
)
113 /* No need to save a reference, because scsi_dma_restart_bh just
114 * looks at the request list. */
118 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
120 SCSIDevice
*s
= opaque
;
126 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
127 qemu_bh_schedule(s
->bh
);
131 static int scsi_qdev_init(DeviceState
*qdev
)
133 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
134 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
138 if (dev
->channel
> bus
->info
->max_channel
) {
139 error_report("bad scsi channel id: %d", dev
->channel
);
142 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
143 error_report("bad scsi device id: %d", dev
->id
);
146 if (dev
->lun
!= -1 && dev
->lun
> bus
->info
->max_lun
) {
147 error_report("bad scsi device lun: %d", dev
->lun
);
153 if (dev
->lun
== -1) {
157 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
158 } while (d
&& d
->lun
== dev
->lun
&& id
< bus
->info
->max_target
);
159 if (d
&& d
->lun
== dev
->lun
) {
160 error_report("no free target");
164 } else if (dev
->lun
== -1) {
167 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
168 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
169 if (d
&& d
->lun
== lun
) {
170 error_report("no free lun");
175 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
177 if (d
->lun
== dev
->lun
&& dev
!= d
) {
182 QTAILQ_INIT(&dev
->requests
);
183 rc
= scsi_device_init(dev
);
185 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
189 if (bus
->info
->hotplug
) {
190 bus
->info
->hotplug(bus
, dev
);
197 static int scsi_qdev_exit(DeviceState
*qdev
)
199 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
202 qemu_del_vm_change_state_handler(dev
->vmsentry
);
204 scsi_device_destroy(dev
);
208 /* handle legacy '-drive if=scsi,...' cmd line args */
209 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
210 int unit
, bool removable
, int bootindex
)
215 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
216 dev
= qdev_create(&bus
->qbus
, driver
);
217 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
218 if (bootindex
>= 0) {
219 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
221 if (object_property_find(OBJECT(dev
), "removable", NULL
)) {
222 qdev_prop_set_bit(dev
, "removable", removable
);
224 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
228 if (qdev_init(dev
) < 0)
230 return SCSI_DEVICE(dev
);
233 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
240 for (unit
= 0; unit
<= bus
->info
->max_target
; unit
++) {
241 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
245 qemu_opts_loc_restore(dinfo
->opts
);
246 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
255 static int32_t scsi_invalid_field(SCSIRequest
*req
, uint8_t *buf
)
257 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
258 scsi_req_complete(req
, CHECK_CONDITION
);
262 static const struct SCSIReqOps reqops_invalid_field
= {
263 .size
= sizeof(SCSIRequest
),
264 .send_command
= scsi_invalid_field
267 /* SCSIReqOps implementation for invalid commands. */
269 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
271 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
272 scsi_req_complete(req
, CHECK_CONDITION
);
276 static const struct SCSIReqOps reqops_invalid_opcode
= {
277 .size
= sizeof(SCSIRequest
),
278 .send_command
= scsi_invalid_command
281 /* SCSIReqOps implementation for unit attention conditions. */
283 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
285 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
286 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
287 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
288 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
290 scsi_req_complete(req
, CHECK_CONDITION
);
294 static const struct SCSIReqOps reqops_unit_attention
= {
295 .size
= sizeof(SCSIRequest
),
296 .send_command
= scsi_unit_attention
299 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
302 typedef struct SCSITargetReq SCSITargetReq
;
304 struct SCSITargetReq
{
310 static void store_lun(uint8_t *outbuf
, int lun
)
316 outbuf
[1] = (lun
& 255);
317 outbuf
[0] = (lun
>> 8) | 0x40;
320 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
327 if (r
->req
.cmd
.xfer
< 16) {
330 if (r
->req
.cmd
.buf
[2] > 2) {
333 channel
= r
->req
.dev
->channel
;
337 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
338 DeviceState
*qdev
= kid
->child
;
339 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
341 if (dev
->channel
== channel
&& dev
->id
== id
) {
351 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
352 if (len
> sizeof(r
->buf
)) {
353 /* TODO: > 256 LUNs? */
357 memset(r
->buf
, 0, len
);
358 stl_be_p(&r
->buf
, n
);
359 i
= found_lun0
? 8 : 16;
360 QTAILQ_FOREACH(kid
, &r
->req
.bus
->qbus
.children
, sibling
) {
361 DeviceState
*qdev
= kid
->child
;
362 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
364 if (dev
->channel
== channel
&& dev
->id
== id
) {
365 store_lun(&r
->buf
[i
], dev
->lun
);
374 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
376 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
377 if (r
->req
.cmd
.buf
[1] & 0x2) {
378 /* Command support data - optional, not implemented */
382 if (r
->req
.cmd
.buf
[1] & 0x1) {
383 /* Vital product data */
384 uint8_t page_code
= r
->req
.cmd
.buf
[2];
385 r
->buf
[r
->len
++] = page_code
; /* this page */
386 r
->buf
[r
->len
++] = 0x00;
389 case 0x00: /* Supported page codes, mandatory */
393 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
394 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
401 assert(r
->len
< sizeof(r
->buf
));
402 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
406 /* Standard INQUIRY data */
407 if (r
->req
.cmd
.buf
[2] != 0) {
412 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
413 memset(r
->buf
, 0, r
->len
);
414 if (r
->req
.lun
!= 0) {
415 r
->buf
[0] = TYPE_NO_LUN
;
417 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
418 r
->buf
[2] = 5; /* Version */
419 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
420 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
421 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
422 memcpy(&r
->buf
[8], "QEMU ", 8);
423 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
424 pstrcpy((char *) &r
->buf
[32], 4, qemu_get_version());
429 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
431 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
435 if (!scsi_target_emulate_report_luns(r
)) {
436 goto illegal_request
;
440 if (!scsi_target_emulate_inquiry(r
)) {
441 goto illegal_request
;
445 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
446 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
447 (req
->cmd
.buf
[1] & 1) == 0);
448 if (r
->req
.dev
->sense_is_ua
) {
449 scsi_device_unit_attention_reported(req
->dev
);
450 r
->req
.dev
->sense_len
= 0;
451 r
->req
.dev
->sense_is_ua
= false;
455 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
456 scsi_req_complete(req
, CHECK_CONDITION
);
459 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
460 scsi_req_complete(req
, CHECK_CONDITION
);
465 scsi_req_complete(req
, GOOD
);
470 static void scsi_target_read_data(SCSIRequest
*req
)
472 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
478 scsi_req_data(&r
->req
, n
);
480 scsi_req_complete(&r
->req
, GOOD
);
484 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
486 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
491 static const struct SCSIReqOps reqops_target_command
= {
492 .size
= sizeof(SCSITargetReq
),
493 .send_command
= scsi_target_send_command
,
494 .read_data
= scsi_target_read_data
,
495 .get_buf
= scsi_target_get_buf
,
499 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
500 uint32_t tag
, uint32_t lun
, void *hba_private
)
504 req
= g_malloc0(reqops
->size
);
506 req
->bus
= scsi_bus_from_device(d
);
510 req
->hba_private
= hba_private
;
514 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
518 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
519 uint8_t *buf
, void *hba_private
)
521 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
525 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
526 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
527 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
529 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
532 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
536 if (cmd
.xfer
> INT32_MAX
) {
537 req
= scsi_req_alloc(&reqops_invalid_field
, d
, tag
, lun
, hba_private
);
538 } else if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
539 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
540 (buf
[0] != INQUIRY
&&
541 buf
[0] != REPORT_LUNS
&&
542 buf
[0] != GET_CONFIGURATION
&&
543 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
546 * If we already have a pending unit attention condition,
547 * report this one before triggering another one.
549 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
550 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
552 } else if (lun
!= d
->lun
||
553 buf
[0] == REPORT_LUNS
||
554 (buf
[0] == REQUEST_SENSE
&& d
->sense_len
)) {
555 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
558 req
= scsi_device_alloc_req(d
, tag
, lun
, buf
, hba_private
);
563 req
->resid
= req
->cmd
.xfer
;
567 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
569 case TEST_UNIT_READY
:
570 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
573 trace_scsi_report_luns(d
->id
, lun
, tag
);
576 trace_scsi_request_sense(d
->id
, lun
, tag
);
585 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
587 return req
->ops
->get_buf(req
);
590 static void scsi_clear_unit_attention(SCSIRequest
*req
)
593 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
594 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
599 * If an INQUIRY command enters the enabled command state,
600 * the device server shall [not] clear any unit attention condition;
601 * See also MMC-6, paragraphs 6.5 and 6.6.2.
603 if (req
->cmd
.buf
[0] == INQUIRY
||
604 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
605 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
609 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
610 ua
= &req
->dev
->unit_attention
;
612 ua
= &req
->bus
->unit_attention
;
616 * If a REPORT LUNS command enters the enabled command state, [...]
617 * the device server shall clear any pending unit attention condition
618 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
620 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
621 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
622 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
626 *ua
= SENSE_CODE(NO_SENSE
);
629 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
634 if (!req
->sense_len
) {
638 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
641 * FIXME: clearing unit attention conditions upon autosense should be done
642 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
645 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
646 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
647 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
649 if (req
->dev
->sense_is_ua
) {
650 scsi_device_unit_attention_reported(req
->dev
);
651 req
->dev
->sense_len
= 0;
652 req
->dev
->sense_is_ua
= false;
657 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
659 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
662 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
664 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
665 sense
.key
, sense
.asc
, sense
.ascq
);
666 memset(req
->sense
, 0, 18);
667 req
->sense
[0] = 0x70;
668 req
->sense
[2] = sense
.key
;
670 req
->sense
[12] = sense
.asc
;
671 req
->sense
[13] = sense
.ascq
;
675 static void scsi_req_enqueue_internal(SCSIRequest
*req
)
677 assert(!req
->enqueued
);
679 if (req
->bus
->info
->get_sg_list
) {
680 req
->sg
= req
->bus
->info
->get_sg_list(req
);
684 req
->enqueued
= true;
685 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
688 int32_t scsi_req_enqueue(SCSIRequest
*req
)
693 scsi_req_enqueue_internal(req
);
695 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
700 static void scsi_req_dequeue(SCSIRequest
*req
)
702 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
705 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
706 req
->enqueued
= false;
711 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
713 /* MMC-6, paragraph 6.7. */
716 if ((data_type
& 3) == 0) {
717 /* Each descriptor is as in Table 295 - Nominal performance. */
718 return 16 * num_desc
+ 8;
720 /* Each descriptor is as in Table 296 - Exceptions. */
721 return 6 * num_desc
+ 8;
726 return 8 * num_desc
+ 8;
728 return 2048 * num_desc
+ 8;
730 return 16 * num_desc
+ 8;
736 static int ata_passthrough_xfer_unit(SCSIDevice
*dev
, uint8_t *buf
)
738 int byte_block
= (buf
[2] >> 2) & 0x1;
739 int type
= (buf
[2] >> 4) & 0x1;
744 xfer_unit
= dev
->blocksize
;
755 static int ata_passthrough_12_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
757 int length
= buf
[2] & 0x3;
759 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
763 case 3: /* USB-specific. */
778 static int ata_passthrough_16_xfer_size(SCSIDevice
*dev
, uint8_t *buf
)
780 int extend
= buf
[1] & 0x1;
781 int length
= buf
[2] & 0x3;
783 int unit
= ata_passthrough_xfer_unit(dev
, buf
);
787 case 3: /* USB-specific. */
793 xfer
|= (extend
? buf
[3] << 8 : 0);
797 xfer
|= (extend
? buf
[5] << 8 : 0);
804 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
806 switch (buf
[0] >> 5) {
812 cmd
->xfer
= lduw_be_p(&buf
[7]);
815 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
818 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
825 case TEST_UNIT_READY
:
829 case WRITE_FILEMARKS
:
830 case WRITE_FILEMARKS_16
:
835 case ALLOW_MEDIUM_REMOVAL
:
838 case SYNCHRONIZE_CACHE
:
839 case SYNCHRONIZE_CACHE_16
:
841 case LOCK_UNLOCK_CACHE
:
850 case ALLOW_OVERWRITE
:
857 cmd
->xfer
= dev
->blocksize
;
859 case READ_CAPACITY_10
:
862 case READ_BLOCK_LIMITS
:
865 case SEND_VOLUME_TAG
:
866 /* GPCMD_SET_STREAMING from multimedia commands. */
867 if (dev
->type
== TYPE_ROM
) {
868 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
870 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
874 /* length 0 means 256 blocks */
875 if (cmd
->xfer
== 0) {
879 case WRITE_VERIFY_10
:
881 case WRITE_VERIFY_12
:
883 case WRITE_VERIFY_16
:
884 cmd
->xfer
*= dev
->blocksize
;
888 /* length 0 means 256 blocks */
889 if (cmd
->xfer
== 0) {
893 case RECOVER_BUFFERED_DATA
:
896 cmd
->xfer
*= dev
->blocksize
;
899 /* MMC mandates the parameter list to be 12-bytes long. Parameters
900 * for block devices are restricted to the header right now. */
901 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
904 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
908 case RECEIVE_DIAGNOSTIC
:
909 case SEND_DIAGNOSTIC
:
910 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
916 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
918 case PERSISTENT_RESERVE_OUT
:
919 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
922 if (dev
->type
== TYPE_ROM
) {
923 /* MMC command GET PERFORMANCE. */
924 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
925 buf
[10], buf
[1] & 0x1f);
928 case MECHANISM_STATUS
:
929 case READ_DVD_STRUCTURE
:
930 case SEND_DVD_STRUCTURE
:
931 case MAINTENANCE_OUT
:
933 if (dev
->type
== TYPE_ROM
) {
934 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
935 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
938 case ATA_PASSTHROUGH_12
:
939 if (dev
->type
== TYPE_ROM
) {
940 /* BLANK command of MMC */
943 cmd
->xfer
= ata_passthrough_12_xfer_size(dev
, buf
);
946 case ATA_PASSTHROUGH_16
:
947 cmd
->xfer
= ata_passthrough_16_xfer_size(dev
, buf
);
953 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
956 /* stream commands */
963 case RECOVER_BUFFERED_DATA
:
965 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
966 if (buf
[1] & 0x01) { /* fixed */
967 cmd
->xfer
*= dev
->blocksize
;
971 case READ_REVERSE_16
:
974 cmd
->xfer
= buf
[14] | (buf
[13] << 8) | (buf
[12] << 16);
975 if (buf
[1] & 0x01) { /* fixed */
976 cmd
->xfer
*= dev
->blocksize
;
984 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
987 switch (buf
[1] & 0x1f) /* operation code */ {
988 case SHORT_FORM_BLOCK_ID
:
989 case SHORT_FORM_VENDOR_SPECIFIC
:
996 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
1004 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
1006 /* generic commands */
1008 return scsi_req_length(cmd
, dev
, buf
);
1013 static int scsi_req_medium_changer_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1016 /* medium changer commands */
1017 case EXCHANGE_MEDIUM
:
1018 case INITIALIZE_ELEMENT_STATUS
:
1019 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE
:
1021 case POSITION_TO_ELEMENT
:
1024 case READ_ELEMENT_STATUS
:
1025 cmd
->xfer
= buf
[9] | (buf
[8] << 8) | (buf
[7] << 16);
1028 /* generic commands */
1030 return scsi_req_length(cmd
, dev
, buf
);
1036 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
1039 cmd
->mode
= SCSI_XFER_NONE
;
1042 switch (cmd
->buf
[0]) {
1045 case WRITE_VERIFY_10
:
1047 case WRITE_VERIFY_12
:
1049 case WRITE_VERIFY_16
:
1053 case CHANGE_DEFINITION
:
1056 case MODE_SELECT_10
:
1057 case SEND_DIAGNOSTIC
:
1060 case REASSIGN_BLOCKS
:
1069 case SEARCH_HIGH_12
:
1070 case SEARCH_EQUAL_12
:
1073 case SEND_VOLUME_TAG
:
1074 case SEND_CUE_SHEET
:
1075 case SEND_DVD_STRUCTURE
:
1076 case PERSISTENT_RESERVE_OUT
:
1077 case MAINTENANCE_OUT
:
1078 cmd
->mode
= SCSI_XFER_TO_DEV
;
1080 case ATA_PASSTHROUGH_12
:
1081 case ATA_PASSTHROUGH_16
:
1083 cmd
->mode
= (cmd
->buf
[2] & 0x8) ?
1084 SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
;
1087 cmd
->mode
= SCSI_XFER_FROM_DEV
;
1092 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
1094 uint8_t *buf
= cmd
->buf
;
1097 switch (buf
[0] >> 5) {
1099 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
1104 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
1107 lba
= ldq_be_p(&buf
[2]);
1116 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
1120 switch (buf
[0] >> 5) {
1138 switch (dev
->type
) {
1140 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
1142 case TYPE_MEDIUM_CHANGER
:
1143 rc
= scsi_req_medium_changer_length(cmd
, dev
, buf
);
1146 rc
= scsi_req_length(cmd
, dev
, buf
);
1153 memcpy(cmd
->buf
, buf
, cmd
->len
);
1154 scsi_cmd_xfer_mode(cmd
);
1155 cmd
->lba
= scsi_cmd_lba(cmd
);
1159 void scsi_device_report_change(SCSIDevice
*dev
, SCSISense sense
)
1161 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1163 scsi_device_set_ua(dev
, sense
);
1164 if (bus
->info
->change
) {
1165 bus
->info
->change(bus
, dev
, sense
);
1170 * Predefined sense codes
1173 /* No sense data available */
1174 const struct SCSISense sense_code_NO_SENSE
= {
1175 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
1178 /* LUN not ready, Manual intervention required */
1179 const struct SCSISense sense_code_LUN_NOT_READY
= {
1180 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
1183 /* LUN not ready, Medium not present */
1184 const struct SCSISense sense_code_NO_MEDIUM
= {
1185 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
1188 /* LUN not ready, medium removal prevented */
1189 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
1190 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x02
1193 /* Hardware error, internal target failure */
1194 const struct SCSISense sense_code_TARGET_FAILURE
= {
1195 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
1198 /* Illegal request, invalid command operation code */
1199 const struct SCSISense sense_code_INVALID_OPCODE
= {
1200 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
1203 /* Illegal request, LBA out of range */
1204 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
1205 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
1208 /* Illegal request, Invalid field in CDB */
1209 const struct SCSISense sense_code_INVALID_FIELD
= {
1210 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
1213 /* Illegal request, Invalid field in parameter list */
1214 const struct SCSISense sense_code_INVALID_PARAM
= {
1215 .key
= ILLEGAL_REQUEST
, .asc
= 0x26, .ascq
= 0x00
1218 /* Illegal request, Parameter list length error */
1219 const struct SCSISense sense_code_INVALID_PARAM_LEN
= {
1220 .key
= ILLEGAL_REQUEST
, .asc
= 0x1a, .ascq
= 0x00
1223 /* Illegal request, LUN not supported */
1224 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
1225 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
1228 /* Illegal request, Saving parameters not supported */
1229 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
1230 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1233 /* Illegal request, Incompatible medium installed */
1234 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1235 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1238 /* Illegal request, medium removal prevented */
1239 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1240 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x02
1243 /* Command aborted, I/O process terminated */
1244 const struct SCSISense sense_code_IO_ERROR
= {
1245 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1248 /* Command aborted, I_T Nexus loss occurred */
1249 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1250 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1253 /* Command aborted, Logical Unit failure */
1254 const struct SCSISense sense_code_LUN_FAILURE
= {
1255 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1258 /* Unit attention, Capacity data has changed */
1259 const struct SCSISense sense_code_CAPACITY_CHANGED
= {
1260 .key
= UNIT_ATTENTION
, .asc
= 0x2a, .ascq
= 0x09
1263 /* Unit attention, Power on, reset or bus device reset occurred */
1264 const struct SCSISense sense_code_RESET
= {
1265 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1268 /* Unit attention, No medium */
1269 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1270 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1273 /* Unit attention, Medium may have changed */
1274 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1275 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1278 /* Unit attention, Reported LUNs data has changed */
1279 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1280 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1283 /* Unit attention, Device internal reset */
1284 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1285 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1288 /* Data Protection, Write Protected */
1289 const struct SCSISense sense_code_WRITE_PROTECTED
= {
1290 .key
= DATA_PROTECT
, .asc
= 0x27, .ascq
= 0x00
1296 * Convert between fixed and descriptor sense buffers
1298 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1299 uint8_t *buf
, int len
, bool fixed
)
1303 if (!fixed
&& len
< 8) {
1308 sense
.key
= NO_SENSE
;
1312 fixed_in
= (in_buf
[0] & 2) == 0;
1314 if (fixed
== fixed_in
) {
1315 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1316 return MIN(len
, in_len
);
1320 sense
.key
= in_buf
[2];
1321 sense
.asc
= in_buf
[12];
1322 sense
.ascq
= in_buf
[13];
1324 sense
.key
= in_buf
[1];
1325 sense
.asc
= in_buf
[2];
1326 sense
.ascq
= in_buf
[3];
1330 memset(buf
, 0, len
);
1332 /* Return fixed format sense buffer */
1336 buf
[12] = sense
.asc
;
1337 buf
[13] = sense
.ascq
;
1338 return MIN(len
, 18);
1340 /* Return descriptor format sense buffer */
1344 buf
[3] = sense
.ascq
;
1349 static const char *scsi_command_name(uint8_t cmd
)
1351 static const char *names
[] = {
1352 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1353 [ REWIND
] = "REWIND",
1354 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1355 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1356 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1357 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1358 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1359 [ READ_6
] = "READ_6",
1360 [ WRITE_6
] = "WRITE_6",
1361 [ SET_CAPACITY
] = "SET_CAPACITY",
1362 [ READ_REVERSE
] = "READ_REVERSE",
1363 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1364 [ SPACE
] = "SPACE",
1365 [ INQUIRY
] = "INQUIRY",
1366 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1367 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1368 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1369 [ MODE_SELECT
] = "MODE_SELECT",
1370 [ RESERVE
] = "RESERVE",
1371 [ RELEASE
] = "RELEASE",
1373 [ ERASE
] = "ERASE",
1374 [ MODE_SENSE
] = "MODE_SENSE",
1375 [ START_STOP
] = "START_STOP/LOAD_UNLOAD",
1376 /* LOAD_UNLOAD and START_STOP use the same operation code */
1377 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1378 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1379 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1380 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1381 [ READ_10
] = "READ_10",
1382 [ WRITE_10
] = "WRITE_10",
1383 [ SEEK_10
] = "SEEK_10/POSITION_TO_ELEMENT",
1384 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1385 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1386 [ VERIFY_10
] = "VERIFY_10",
1387 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1388 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1389 [ SEARCH_LOW
] = "SEARCH_LOW",
1390 [ SET_LIMITS
] = "SET_LIMITS",
1391 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1392 /* READ_POSITION and PRE_FETCH use the same operation code */
1393 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1394 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1395 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1396 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1397 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1398 [ COMPARE
] = "COMPARE",
1399 [ COPY_VERIFY
] = "COPY_VERIFY",
1400 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1401 [ READ_BUFFER
] = "READ_BUFFER",
1402 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1403 [ READ_LONG_10
] = "READ_LONG_10",
1404 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1405 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1406 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1407 [ UNMAP
] = "UNMAP",
1408 [ READ_TOC
] = "READ_TOC",
1409 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1410 [ SANITIZE
] = "SANITIZE",
1411 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1412 [ LOG_SELECT
] = "LOG_SELECT",
1413 [ LOG_SENSE
] = "LOG_SENSE",
1414 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1415 [ RESERVE_10
] = "RESERVE_10",
1416 [ RELEASE_10
] = "RELEASE_10",
1417 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1418 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1419 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1420 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1421 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1422 [ ATA_PASSTHROUGH_16
] = "ATA_PASSTHROUGH_16",
1423 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1424 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1425 [ READ_16
] = "READ_16",
1426 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1427 [ WRITE_16
] = "WRITE_16",
1428 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1429 [ VERIFY_16
] = "VERIFY_16",
1430 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1431 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1432 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1433 [ LOCATE_16
] = "LOCATE_16",
1434 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1435 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1436 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1437 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1438 [ REPORT_LUNS
] = "REPORT_LUNS",
1439 [ ATA_PASSTHROUGH_12
] = "BLANK/ATA_PASSTHROUGH_12",
1440 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1441 [ EXCHANGE_MEDIUM
] = "EXCHANGE MEDIUM",
1442 [ READ_12
] = "READ_12",
1443 [ WRITE_12
] = "WRITE_12",
1444 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1445 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1446 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1447 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1448 [ VERIFY_12
] = "VERIFY_12",
1449 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1450 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1451 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1452 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1453 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1454 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1455 [ READ_CD
] = "READ_CD",
1456 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1457 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1458 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1459 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1460 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1461 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1462 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1463 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1464 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1467 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1472 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1474 assert(req
->refcount
> 0);
1479 void scsi_req_unref(SCSIRequest
*req
)
1481 assert(req
->refcount
> 0);
1482 if (--req
->refcount
== 0) {
1483 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, req
->dev
->qdev
.parent_bus
);
1484 if (bus
->info
->free_request
&& req
->hba_private
) {
1485 bus
->info
->free_request(bus
, req
->hba_private
);
1487 if (req
->ops
->free_req
) {
1488 req
->ops
->free_req(req
);
1494 /* Tell the device that we finished processing this chunk of I/O. It
1495 will start the next chunk or complete the command. */
1496 void scsi_req_continue(SCSIRequest
*req
)
1498 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1499 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1500 req
->ops
->write_data(req
);
1502 req
->ops
->read_data(req
);
1506 /* Called by the devices when data is ready for the HBA. The HBA should
1507 start a DMA operation to read or fill the device's data buffer.
1508 Once it completes, calling scsi_req_continue will restart I/O. */
1509 void scsi_req_data(SCSIRequest
*req
, int len
)
1512 if (req
->io_canceled
) {
1513 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1516 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1517 assert(req
->cmd
.mode
!= SCSI_XFER_NONE
);
1520 req
->bus
->info
->transfer_data(req
, len
);
1524 /* If the device calls scsi_req_data and the HBA specified a
1525 * scatter/gather list, the transfer has to happen in a single
1527 assert(!req
->dma_started
);
1528 req
->dma_started
= true;
1530 buf
= scsi_req_get_buf(req
);
1531 if (req
->cmd
.mode
== SCSI_XFER_FROM_DEV
) {
1532 req
->resid
= dma_buf_read(buf
, len
, req
->sg
);
1534 req
->resid
= dma_buf_write(buf
, len
, req
->sg
);
1536 scsi_req_continue(req
);
1539 void scsi_req_print(SCSIRequest
*req
)
1544 fprintf(fp
, "[%s id=%d] %s",
1545 req
->dev
->qdev
.parent_bus
->name
,
1547 scsi_command_name(req
->cmd
.buf
[0]));
1548 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1549 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1551 switch (req
->cmd
.mode
) {
1552 case SCSI_XFER_NONE
:
1553 fprintf(fp
, " - none\n");
1555 case SCSI_XFER_FROM_DEV
:
1556 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1558 case SCSI_XFER_TO_DEV
:
1559 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1562 fprintf(fp
, " - Oops\n");
1567 void scsi_req_complete(SCSIRequest
*req
, int status
)
1569 assert(req
->status
== -1);
1570 req
->status
= status
;
1572 assert(req
->sense_len
<= sizeof(req
->sense
));
1573 if (status
== GOOD
) {
1577 if (req
->sense_len
) {
1578 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1579 req
->dev
->sense_len
= req
->sense_len
;
1580 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1582 req
->dev
->sense_len
= 0;
1583 req
->dev
->sense_is_ua
= false;
1587 * Unit attention state is now stored in the device's sense buffer
1588 * if the HBA didn't do autosense. Clear the pending unit attention
1591 scsi_clear_unit_attention(req
);
1594 scsi_req_dequeue(req
);
1595 req
->bus
->info
->complete(req
, req
->status
, req
->resid
);
1596 scsi_req_unref(req
);
1599 void scsi_req_cancel(SCSIRequest
*req
)
1601 trace_scsi_req_cancel(req
->dev
->id
, req
->lun
, req
->tag
);
1602 if (!req
->enqueued
) {
1606 scsi_req_dequeue(req
);
1607 req
->io_canceled
= true;
1608 if (req
->ops
->cancel_io
) {
1609 req
->ops
->cancel_io(req
);
1611 if (req
->bus
->info
->cancel
) {
1612 req
->bus
->info
->cancel(req
);
1614 scsi_req_unref(req
);
1617 void scsi_req_abort(SCSIRequest
*req
, int status
)
1619 if (!req
->enqueued
) {
1623 scsi_req_dequeue(req
);
1624 req
->io_canceled
= true;
1625 if (req
->ops
->cancel_io
) {
1626 req
->ops
->cancel_io(req
);
1628 scsi_req_complete(req
, status
);
1629 scsi_req_unref(req
);
1632 static int scsi_ua_precedence(SCSISense sense
)
1634 if (sense
.key
!= UNIT_ATTENTION
) {
1637 if (sense
.asc
== 0x29 && sense
.ascq
== 0x04) {
1638 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1640 } else if (sense
.asc
== 0x3F && sense
.ascq
== 0x01) {
1641 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1643 } else if (sense
.asc
== 0x29 && (sense
.ascq
== 0x05 || sense
.ascq
== 0x06)) {
1644 /* These two go with "all others". */
1646 } else if (sense
.asc
== 0x29 && sense
.ascq
<= 0x07) {
1647 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1648 * POWER ON OCCURRED = 1
1649 * SCSI BUS RESET OCCURRED = 2
1650 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1651 * I_T NEXUS LOSS OCCURRED = 7
1654 } else if (sense
.asc
== 0x2F && sense
.ascq
== 0x01) {
1655 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1658 return (sense
.asc
<< 8) | sense
.ascq
;
1661 void scsi_device_set_ua(SCSIDevice
*sdev
, SCSISense sense
)
1664 if (sense
.key
!= UNIT_ATTENTION
) {
1667 trace_scsi_device_set_ua(sdev
->id
, sdev
->lun
, sense
.key
,
1668 sense
.asc
, sense
.ascq
);
1671 * Override a pre-existing unit attention condition, except for a more
1672 * important reset condition.
1674 prec1
= scsi_ua_precedence(sdev
->unit_attention
);
1675 prec2
= scsi_ua_precedence(sense
);
1676 if (prec2
< prec1
) {
1677 sdev
->unit_attention
= sense
;
1681 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1685 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1686 req
= QTAILQ_FIRST(&sdev
->requests
);
1687 scsi_req_cancel(req
);
1690 scsi_device_set_ua(sdev
, sense
);
1693 static char *scsibus_get_dev_path(DeviceState
*dev
)
1695 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1696 DeviceState
*hba
= dev
->parent_bus
->parent
;
1700 id
= qdev_get_dev_path(hba
);
1702 path
= g_strdup_printf("%s/%d:%d:%d", id
, d
->channel
, d
->id
, d
->lun
);
1704 path
= g_strdup_printf("%d:%d:%d", d
->channel
, d
->id
, d
->lun
);
1710 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1712 SCSIDevice
*d
= SCSI_DEVICE(dev
);
1715 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1716 qdev_fw_name(dev
), d
->id
, d
->lun
);
1718 return strdup(path
);
1721 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1724 SCSIDevice
*target_dev
= NULL
;
1726 QTAILQ_FOREACH_REVERSE(kid
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1727 DeviceState
*qdev
= kid
->child
;
1728 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1730 if (dev
->channel
== channel
&& dev
->id
== id
) {
1731 if (dev
->lun
== lun
) {
1740 /* SCSI request list. For simplicity, pv points to the whole device */
1742 static void put_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1745 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1748 QTAILQ_FOREACH(req
, &s
->requests
, next
) {
1749 assert(!req
->io_canceled
);
1750 assert(req
->status
== -1);
1751 assert(req
->enqueued
);
1753 qemu_put_sbyte(f
, req
->retry
? 1 : 2);
1754 qemu_put_buffer(f
, req
->cmd
.buf
, sizeof(req
->cmd
.buf
));
1755 qemu_put_be32s(f
, &req
->tag
);
1756 qemu_put_be32s(f
, &req
->lun
);
1757 if (bus
->info
->save_request
) {
1758 bus
->info
->save_request(f
, req
);
1760 if (req
->ops
->save_request
) {
1761 req
->ops
->save_request(f
, req
);
1764 qemu_put_sbyte(f
, 0);
1767 static int get_scsi_requests(QEMUFile
*f
, void *pv
, size_t size
)
1770 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, s
->qdev
.parent_bus
);
1773 while ((sbyte
= qemu_get_sbyte(f
)) > 0) {
1774 uint8_t buf
[SCSI_CMD_BUF_SIZE
];
1779 qemu_get_buffer(f
, buf
, sizeof(buf
));
1780 qemu_get_be32s(f
, &tag
);
1781 qemu_get_be32s(f
, &lun
);
1782 req
= scsi_req_new(s
, tag
, lun
, buf
, NULL
);
1783 req
->retry
= (sbyte
== 1);
1784 if (bus
->info
->load_request
) {
1785 req
->hba_private
= bus
->info
->load_request(f
, req
);
1787 if (req
->ops
->load_request
) {
1788 req
->ops
->load_request(f
, req
);
1791 /* Just restart it later. */
1792 scsi_req_enqueue_internal(req
);
1794 /* At this point, the request will be kept alive by the reference
1795 * added by scsi_req_enqueue_internal, so we can release our reference.
1796 * The HBA of course will add its own reference in the load_request
1797 * callback if it needs to hold on the SCSIRequest.
1799 scsi_req_unref(req
);
1805 static int scsi_qdev_unplug(DeviceState
*qdev
)
1807 SCSIDevice
*dev
= SCSI_DEVICE(qdev
);
1808 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
1810 if (bus
->info
->hot_unplug
) {
1811 bus
->info
->hot_unplug(bus
, dev
);
1813 return qdev_simple_unplug_cb(qdev
);
1816 static const VMStateInfo vmstate_info_scsi_requests
= {
1817 .name
= "scsi-requests",
1818 .get
= get_scsi_requests
,
1819 .put
= put_scsi_requests
,
1822 const VMStateDescription vmstate_scsi_device
= {
1823 .name
= "SCSIDevice",
1825 .minimum_version_id
= 1,
1826 .minimum_version_id_old
= 1,
1827 .fields
= (VMStateField
[]) {
1828 VMSTATE_UINT8(unit_attention
.key
, SCSIDevice
),
1829 VMSTATE_UINT8(unit_attention
.asc
, SCSIDevice
),
1830 VMSTATE_UINT8(unit_attention
.ascq
, SCSIDevice
),
1831 VMSTATE_BOOL(sense_is_ua
, SCSIDevice
),
1832 VMSTATE_UINT8_ARRAY(sense
, SCSIDevice
, SCSI_SENSE_BUF_SIZE
),
1833 VMSTATE_UINT32(sense_len
, SCSIDevice
),
1837 .field_exists
= NULL
,
1838 .size
= 0, /* ouch */
1839 .info
= &vmstate_info_scsi_requests
,
1840 .flags
= VMS_SINGLE
,
1843 VMSTATE_END_OF_LIST()
1847 static void scsi_device_class_init(ObjectClass
*klass
, void *data
)
1849 DeviceClass
*k
= DEVICE_CLASS(klass
);
1850 k
->bus_type
= TYPE_SCSI_BUS
;
1851 k
->init
= scsi_qdev_init
;
1852 k
->unplug
= scsi_qdev_unplug
;
1853 k
->exit
= scsi_qdev_exit
;
1854 k
->props
= scsi_props
;
1857 static TypeInfo scsi_device_type_info
= {
1858 .name
= TYPE_SCSI_DEVICE
,
1859 .parent
= TYPE_DEVICE
,
1860 .instance_size
= sizeof(SCSIDevice
),
1862 .class_size
= sizeof(SCSIDeviceClass
),
1863 .class_init
= scsi_device_class_init
,
1866 static void scsi_register_types(void)
1868 type_register_static(&scsi_bus_info
);
1869 type_register_static(&scsi_device_type_info
);
1872 type_init(scsi_register_types
)