2 #include "qemu-error.h"
9 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
10 static int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
);
11 static void scsi_req_dequeue(SCSIRequest
*req
);
12 static int scsi_build_sense(uint8_t *in_buf
, int in_len
,
13 uint8_t *buf
, int len
, bool fixed
);
15 static struct BusInfo scsi_bus_info
= {
17 .size
= sizeof(SCSIBus
),
18 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
19 .props
= (Property
[]) {
20 DEFINE_PROP_UINT32("channel", SCSIDevice
, channel
, 0),
21 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
22 DEFINE_PROP_UINT32("lun", SCSIDevice
, lun
, -1),
23 DEFINE_PROP_END_OF_LIST(),
26 static int next_scsi_bus
;
28 /* Create a scsi bus, and attach devices to it. */
29 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
31 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
32 bus
->busnr
= next_scsi_bus
++;
34 bus
->qbus
.allow_hotplug
= 1;
37 static void scsi_dma_restart_bh(void *opaque
)
39 SCSIDevice
*s
= opaque
;
40 SCSIRequest
*req
, *next
;
42 qemu_bh_delete(s
->bh
);
45 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
49 switch (req
->cmd
.mode
) {
50 case SCSI_XFER_FROM_DEV
:
51 case SCSI_XFER_TO_DEV
:
52 scsi_req_continue(req
);
55 scsi_req_dequeue(req
);
56 scsi_req_enqueue(req
);
64 void scsi_req_retry(SCSIRequest
*req
)
66 /* No need to save a reference, because scsi_dma_restart_bh just
67 * looks at the request list. */
71 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
73 SCSIDevice
*s
= opaque
;
79 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
80 qemu_bh_schedule(s
->bh
);
84 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
86 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
87 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
88 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
92 if (dev
->channel
> bus
->info
->max_channel
) {
93 error_report("bad scsi channel id: %d", dev
->channel
);
96 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
97 error_report("bad scsi device id: %d", dev
->id
);
103 if (dev
->lun
== -1) {
107 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
108 } while (d
&& d
->lun
== dev
->lun
&& id
<= bus
->info
->max_target
);
109 if (id
> bus
->info
->max_target
) {
110 error_report("no free target");
114 } else if (dev
->lun
== -1) {
117 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
118 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
119 if (lun
> bus
->info
->max_lun
) {
120 error_report("no free lun");
125 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
126 if (dev
->lun
== d
->lun
&& dev
!= d
) {
132 QTAILQ_INIT(&dev
->requests
);
133 rc
= dev
->info
->init(dev
);
135 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
143 static int scsi_qdev_exit(DeviceState
*qdev
)
145 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
148 qemu_del_vm_change_state_handler(dev
->vmsentry
);
150 if (dev
->info
->destroy
) {
151 dev
->info
->destroy(dev
);
156 void scsi_qdev_register(SCSIDeviceInfo
*info
)
158 info
->qdev
.bus_info
= &scsi_bus_info
;
159 info
->qdev
.init
= scsi_qdev_init
;
160 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
161 info
->qdev
.exit
= scsi_qdev_exit
;
162 qdev_register(&info
->qdev
);
165 /* handle legacy '-drive if=scsi,...' cmd line args */
166 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
167 int unit
, bool removable
)
172 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
173 dev
= qdev_create(&bus
->qbus
, driver
);
174 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
175 if (qdev_prop_exists(dev
, "removable")) {
176 qdev_prop_set_bit(dev
, "removable", removable
);
178 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
182 if (qdev_init(dev
) < 0)
184 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
187 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
194 for (unit
= 0; unit
< bus
->info
->max_target
; unit
++) {
195 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
199 qemu_opts_loc_restore(dinfo
->opts
);
200 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false)) {
209 /* SCSIReqOps implementation for invalid commands. */
211 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
213 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
214 scsi_req_complete(req
, CHECK_CONDITION
);
218 static const struct SCSIReqOps reqops_invalid_opcode
= {
219 .size
= sizeof(SCSIRequest
),
220 .send_command
= scsi_invalid_command
223 /* SCSIReqOps implementation for unit attention conditions. */
225 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
227 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
228 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
229 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
230 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
232 scsi_req_complete(req
, CHECK_CONDITION
);
236 static const struct SCSIReqOps reqops_unit_attention
= {
237 .size
= sizeof(SCSIRequest
),
238 .send_command
= scsi_unit_attention
241 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
244 typedef struct SCSITargetReq SCSITargetReq
;
246 struct SCSITargetReq
{
252 static void store_lun(uint8_t *outbuf
, int lun
)
258 outbuf
[1] = (lun
& 255);
259 outbuf
[0] = (lun
>> 8) | 0x40;
262 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
269 if (r
->req
.cmd
.xfer
< 16) {
272 if (r
->req
.cmd
.buf
[2] > 2) {
275 channel
= r
->req
.dev
->channel
;
279 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
280 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
282 if (dev
->channel
== channel
&& dev
->id
== id
) {
292 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
293 if (len
> sizeof(r
->buf
)) {
294 /* TODO: > 256 LUNs? */
298 memset(r
->buf
, 0, len
);
299 stl_be_p(&r
->buf
, n
);
300 i
= found_lun0
? 8 : 16;
301 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
302 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
304 if (dev
->channel
== channel
&& dev
->id
== id
) {
305 store_lun(&r
->buf
[i
], dev
->lun
);
314 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
316 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
317 if (r
->req
.cmd
.buf
[1] & 0x2) {
318 /* Command support data - optional, not implemented */
322 if (r
->req
.cmd
.buf
[1] & 0x1) {
323 /* Vital product data */
324 uint8_t page_code
= r
->req
.cmd
.buf
[2];
325 if (r
->req
.cmd
.xfer
< 4) {
329 r
->buf
[r
->len
++] = page_code
; /* this page */
330 r
->buf
[r
->len
++] = 0x00;
333 case 0x00: /* Supported page codes, mandatory */
337 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
338 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
345 assert(r
->len
< sizeof(r
->buf
));
346 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
350 /* Standard INQUIRY data */
351 if (r
->req
.cmd
.buf
[2] != 0) {
356 if (r
->req
.cmd
.xfer
< 5) {
360 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
361 memset(r
->buf
, 0, r
->len
);
362 if (r
->req
.lun
!= 0) {
363 r
->buf
[0] = TYPE_NO_LUN
;
365 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
366 r
->buf
[2] = 5; /* Version */
367 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
368 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
369 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
370 memcpy(&r
->buf
[8], "QEMU ", 8);
371 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
372 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
377 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
379 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
383 if (!scsi_target_emulate_report_luns(r
)) {
384 goto illegal_request
;
388 if (!scsi_target_emulate_inquiry(r
)) {
389 goto illegal_request
;
393 if (req
->cmd
.xfer
< 4) {
394 goto illegal_request
;
396 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
397 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
398 (req
->cmd
.buf
[1] & 1) == 0);
399 if (r
->req
.dev
->sense_is_ua
) {
400 if (r
->req
.dev
->info
->unit_attention_reported
) {
401 r
->req
.dev
->info
->unit_attention_reported(req
->dev
);
403 r
->req
.dev
->sense_len
= 0;
404 r
->req
.dev
->sense_is_ua
= false;
408 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
409 scsi_req_complete(req
, CHECK_CONDITION
);
412 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
413 scsi_req_complete(req
, CHECK_CONDITION
);
418 scsi_req_complete(req
, GOOD
);
423 static void scsi_target_read_data(SCSIRequest
*req
)
425 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
431 scsi_req_data(&r
->req
, n
);
433 scsi_req_complete(&r
->req
, GOOD
);
437 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
439 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
444 static const struct SCSIReqOps reqops_target_command
= {
445 .size
= sizeof(SCSITargetReq
),
446 .send_command
= scsi_target_send_command
,
447 .read_data
= scsi_target_read_data
,
448 .get_buf
= scsi_target_get_buf
,
452 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
453 uint32_t tag
, uint32_t lun
, void *hba_private
)
457 req
= g_malloc0(reqops
->size
);
459 req
->bus
= scsi_bus_from_device(d
);
463 req
->hba_private
= hba_private
;
467 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
471 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
472 uint8_t *buf
, void *hba_private
)
474 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
478 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
479 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
480 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
482 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
485 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
489 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
490 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
491 (buf
[0] != INQUIRY
&&
492 buf
[0] != REPORT_LUNS
&&
493 buf
[0] != GET_CONFIGURATION
&&
494 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
497 * If we already have a pending unit attention condition,
498 * report this one before triggering another one.
500 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
501 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
503 } else if (lun
!= d
->lun
||
504 buf
[0] == REPORT_LUNS
||
505 buf
[0] == REQUEST_SENSE
) {
506 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
509 req
= d
->info
->alloc_req(d
, tag
, lun
, buf
, hba_private
);
516 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
518 case TEST_UNIT_READY
:
519 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
522 trace_scsi_report_luns(d
->id
, lun
, tag
);
525 trace_scsi_request_sense(d
->id
, lun
, tag
);
534 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
536 return req
->ops
->get_buf(req
);
539 static void scsi_clear_unit_attention(SCSIRequest
*req
)
542 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
543 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
548 * If an INQUIRY command enters the enabled command state,
549 * the device server shall [not] clear any unit attention condition;
550 * See also MMC-6, paragraphs 6.5 and 6.6.2.
552 if (req
->cmd
.buf
[0] == INQUIRY
||
553 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
554 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
558 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
559 ua
= &req
->dev
->unit_attention
;
561 ua
= &req
->bus
->unit_attention
;
565 * If a REPORT LUNS command enters the enabled command state, [...]
566 * the device server shall clear any pending unit attention condition
567 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
569 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
570 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
571 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
575 *ua
= SENSE_CODE(NO_SENSE
);
578 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
583 if (!req
->sense_len
) {
587 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
590 * FIXME: clearing unit attention conditions upon autosense should be done
591 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
594 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
595 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
596 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
598 if (req
->dev
->sense_is_ua
) {
599 if (req
->dev
->info
->unit_attention_reported
) {
600 req
->dev
->info
->unit_attention_reported(req
->dev
);
602 req
->dev
->sense_len
= 0;
603 req
->dev
->sense_is_ua
= false;
608 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
610 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
613 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
615 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
616 sense
.key
, sense
.asc
, sense
.ascq
);
617 memset(req
->sense
, 0, 18);
618 req
->sense
[0] = 0xf0;
619 req
->sense
[2] = sense
.key
;
621 req
->sense
[12] = sense
.asc
;
622 req
->sense
[13] = sense
.ascq
;
626 int32_t scsi_req_enqueue(SCSIRequest
*req
)
630 assert(!req
->enqueued
);
632 req
->enqueued
= true;
633 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
636 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
641 static void scsi_req_dequeue(SCSIRequest
*req
)
643 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
646 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
647 req
->enqueued
= false;
652 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
654 switch (buf
[0] >> 5) {
658 /* length 0 means 256 blocks */
659 if (cmd
->xfer
== 0) {
665 cmd
->xfer
= lduw_be_p(&buf
[7]);
669 cmd
->xfer
= ldl_be_p(&buf
[10]);
673 cmd
->xfer
= ldl_be_p(&buf
[6]);
681 case TEST_UNIT_READY
:
685 case WRITE_FILEMARKS
:
690 case ALLOW_MEDIUM_REMOVAL
:
693 case SYNCHRONIZE_CACHE
:
694 case LOCK_UNLOCK_CACHE
:
708 case READ_CAPACITY_10
:
711 case READ_BLOCK_LIMITS
:
717 case SEND_VOLUME_TAG
:
724 case WRITE_VERIFY_10
:
727 case WRITE_VERIFY_12
:
729 case WRITE_VERIFY_16
:
730 cmd
->xfer
*= dev
->blocksize
;
735 case RECOVER_BUFFERED_DATA
:
738 cmd
->xfer
*= dev
->blocksize
;
741 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
743 case MAINTENANCE_OUT
:
745 if (dev
->type
== TYPE_ROM
) {
746 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
747 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
754 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
757 /* stream commands */
760 case RECOVER_BUFFERED_DATA
:
763 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
764 if (buf
[1] & 0x01) { /* fixed */
765 cmd
->xfer
*= dev
->blocksize
;
773 /* generic commands */
775 return scsi_req_length(cmd
, dev
, buf
);
780 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
782 switch (cmd
->buf
[0]) {
785 case WRITE_VERIFY_10
:
787 case WRITE_VERIFY_12
:
789 case WRITE_VERIFY_16
:
793 case CHANGE_DEFINITION
:
797 case SEND_DIAGNOSTIC
:
800 case REASSIGN_BLOCKS
:
808 case SEARCH_EQUAL_12
:
811 case SEND_VOLUME_TAG
:
812 case PERSISTENT_RESERVE_OUT
:
813 case MAINTENANCE_OUT
:
814 cmd
->mode
= SCSI_XFER_TO_DEV
;
818 cmd
->mode
= SCSI_XFER_FROM_DEV
;
820 cmd
->mode
= SCSI_XFER_NONE
;
826 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
828 uint8_t *buf
= cmd
->buf
;
831 switch (buf
[0] >> 5) {
833 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
838 lba
= ldl_be_p(&buf
[2]);
841 lba
= ldq_be_p(&buf
[2]);
850 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
854 if (dev
->type
== TYPE_TAPE
) {
855 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
857 rc
= scsi_req_length(cmd
, dev
, buf
);
862 memcpy(cmd
->buf
, buf
, cmd
->len
);
863 scsi_cmd_xfer_mode(cmd
);
864 cmd
->lba
= scsi_cmd_lba(cmd
);
869 * Predefined sense codes
872 /* No sense data available */
873 const struct SCSISense sense_code_NO_SENSE
= {
874 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
877 /* LUN not ready, Manual intervention required */
878 const struct SCSISense sense_code_LUN_NOT_READY
= {
879 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
882 /* LUN not ready, Medium not present */
883 const struct SCSISense sense_code_NO_MEDIUM
= {
884 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
887 /* LUN not ready, medium removal prevented */
888 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
889 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
892 /* Hardware error, internal target failure */
893 const struct SCSISense sense_code_TARGET_FAILURE
= {
894 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
897 /* Illegal request, invalid command operation code */
898 const struct SCSISense sense_code_INVALID_OPCODE
= {
899 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
902 /* Illegal request, LBA out of range */
903 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
904 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
907 /* Illegal request, Invalid field in CDB */
908 const struct SCSISense sense_code_INVALID_FIELD
= {
909 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
912 /* Illegal request, LUN not supported */
913 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
914 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
917 /* Illegal request, Saving parameters not supported */
918 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
919 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
922 /* Illegal request, Incompatible medium installed */
923 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
924 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
927 /* Illegal request, medium removal prevented */
928 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
929 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
932 /* Command aborted, I/O process terminated */
933 const struct SCSISense sense_code_IO_ERROR
= {
934 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
937 /* Command aborted, I_T Nexus loss occurred */
938 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
939 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
942 /* Command aborted, Logical Unit failure */
943 const struct SCSISense sense_code_LUN_FAILURE
= {
944 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
947 /* Unit attention, Power on, reset or bus device reset occurred */
948 const struct SCSISense sense_code_RESET
= {
949 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
952 /* Unit attention, No medium */
953 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
954 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
957 /* Unit attention, Medium may have changed */
958 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
959 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
962 /* Unit attention, Reported LUNs data has changed */
963 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
964 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
967 /* Unit attention, Device internal reset */
968 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
969 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
975 * Convert between fixed and descriptor sense buffers
977 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
978 uint8_t *buf
, int len
, bool fixed
)
982 if (!fixed
&& len
< 8) {
987 sense
.key
= NO_SENSE
;
991 fixed_in
= (in_buf
[0] & 2) == 0;
993 if (fixed
== fixed_in
) {
994 memcpy(buf
, in_buf
, MIN(len
, in_len
));
995 return MIN(len
, in_len
);
999 sense
.key
= in_buf
[2];
1000 sense
.asc
= in_buf
[12];
1001 sense
.ascq
= in_buf
[13];
1003 sense
.key
= in_buf
[1];
1004 sense
.asc
= in_buf
[2];
1005 sense
.ascq
= in_buf
[3];
1009 memset(buf
, 0, len
);
1011 /* Return fixed format sense buffer */
1015 buf
[12] = sense
.asc
;
1016 buf
[13] = sense
.ascq
;
1017 return MIN(len
, 18);
1019 /* Return descriptor format sense buffer */
1023 buf
[3] = sense
.ascq
;
1028 static const char *scsi_command_name(uint8_t cmd
)
1030 static const char *names
[] = {
1031 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1032 [ REWIND
] = "REWIND",
1033 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1034 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1035 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1036 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1037 [ READ_6
] = "READ_6",
1038 [ WRITE_6
] = "WRITE_6",
1039 [ SEEK_6
] = "SEEK_6",
1040 [ READ_REVERSE
] = "READ_REVERSE",
1041 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1042 [ SPACE
] = "SPACE",
1043 [ INQUIRY
] = "INQUIRY",
1044 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1045 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1046 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1047 [ MODE_SELECT
] = "MODE_SELECT",
1048 [ RESERVE
] = "RESERVE",
1049 [ RELEASE
] = "RELEASE",
1051 [ ERASE
] = "ERASE",
1052 [ MODE_SENSE
] = "MODE_SENSE",
1053 [ START_STOP
] = "START_STOP",
1054 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1055 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1056 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1057 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1058 [ READ_10
] = "READ_10",
1059 [ WRITE_10
] = "WRITE_10",
1060 [ SEEK_10
] = "SEEK_10",
1061 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1062 [ VERIFY_10
] = "VERIFY_10",
1063 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1064 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1065 [ SEARCH_LOW
] = "SEARCH_LOW",
1066 [ SET_LIMITS
] = "SET_LIMITS",
1067 [ PRE_FETCH
] = "PRE_FETCH",
1068 /* READ_POSITION and PRE_FETCH use the same operation code */
1069 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1070 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1071 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1072 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1073 [ COMPARE
] = "COMPARE",
1074 [ COPY_VERIFY
] = "COPY_VERIFY",
1075 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1076 [ READ_BUFFER
] = "READ_BUFFER",
1077 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1078 [ READ_LONG_10
] = "READ_LONG_10",
1079 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1080 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1081 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1082 [ UNMAP
] = "UNMAP",
1083 [ READ_TOC
] = "READ_TOC",
1084 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1085 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1086 [ LOG_SELECT
] = "LOG_SELECT",
1087 [ LOG_SENSE
] = "LOG_SENSE",
1088 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1089 [ RESERVE_10
] = "RESERVE_10",
1090 [ RELEASE_10
] = "RELEASE_10",
1091 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1092 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1093 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1094 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1095 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1096 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1097 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1098 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1099 [ READ_16
] = "READ_16",
1100 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1101 [ WRITE_16
] = "WRITE_16",
1102 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1103 [ VERIFY_16
] = "VERIFY_16",
1104 [ SYNCHRONIZE_CACHE_16
] = "SYNCHRONIZE_CACHE_16",
1105 [ LOCATE_16
] = "LOCATE_16",
1106 [ WRITE_SAME_16
] = "WRITE_SAME_16",
1107 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1108 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1109 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1110 [ REPORT_LUNS
] = "REPORT_LUNS",
1111 [ BLANK
] = "BLANK",
1112 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1113 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1114 [ READ_12
] = "READ_12",
1115 [ WRITE_12
] = "WRITE_12",
1116 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1117 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1118 [ VERIFY_12
] = "VERIFY_12",
1119 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1120 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1121 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1122 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1123 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
1124 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1125 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1128 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1133 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1139 void scsi_req_unref(SCSIRequest
*req
)
1141 if (--req
->refcount
== 0) {
1142 if (req
->ops
->free_req
) {
1143 req
->ops
->free_req(req
);
1149 /* Tell the device that we finished processing this chunk of I/O. It
1150 will start the next chunk or complete the command. */
1151 void scsi_req_continue(SCSIRequest
*req
)
1153 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1154 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1155 req
->ops
->write_data(req
);
1157 req
->ops
->read_data(req
);
1161 /* Called by the devices when data is ready for the HBA. The HBA should
1162 start a DMA operation to read or fill the device's data buffer.
1163 Once it completes, calling scsi_req_continue will restart I/O. */
1164 void scsi_req_data(SCSIRequest
*req
, int len
)
1166 if (req
->io_canceled
) {
1167 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1169 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1170 req
->bus
->info
->transfer_data(req
, len
);
1174 void scsi_req_print(SCSIRequest
*req
)
1179 fprintf(fp
, "[%s id=%d] %s",
1180 req
->dev
->qdev
.parent_bus
->name
,
1182 scsi_command_name(req
->cmd
.buf
[0]));
1183 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1184 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1186 switch (req
->cmd
.mode
) {
1187 case SCSI_XFER_NONE
:
1188 fprintf(fp
, " - none\n");
1190 case SCSI_XFER_FROM_DEV
:
1191 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1193 case SCSI_XFER_TO_DEV
:
1194 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1197 fprintf(fp
, " - Oops\n");
1202 void scsi_req_complete(SCSIRequest
*req
, int status
)
1204 assert(req
->status
== -1);
1205 req
->status
= status
;
1207 assert(req
->sense_len
< sizeof(req
->sense
));
1208 if (status
== GOOD
) {
1212 if (req
->sense_len
) {
1213 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1214 req
->dev
->sense_len
= req
->sense_len
;
1215 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1217 req
->dev
->sense_len
= 0;
1218 req
->dev
->sense_is_ua
= false;
1222 * Unit attention state is now stored in the device's sense buffer
1223 * if the HBA didn't do autosense. Clear the pending unit attention
1226 scsi_clear_unit_attention(req
);
1229 scsi_req_dequeue(req
);
1230 req
->bus
->info
->complete(req
, req
->status
);
1231 scsi_req_unref(req
);
1234 void scsi_req_cancel(SCSIRequest
*req
)
1236 if (!req
->enqueued
) {
1240 scsi_req_dequeue(req
);
1241 req
->io_canceled
= true;
1242 if (req
->ops
->cancel_io
) {
1243 req
->ops
->cancel_io(req
);
1245 if (req
->bus
->info
->cancel
) {
1246 req
->bus
->info
->cancel(req
);
1248 scsi_req_unref(req
);
1251 void scsi_req_abort(SCSIRequest
*req
, int status
)
1253 if (!req
->enqueued
) {
1257 scsi_req_dequeue(req
);
1258 req
->io_canceled
= true;
1259 if (req
->ops
->cancel_io
) {
1260 req
->ops
->cancel_io(req
);
1262 scsi_req_complete(req
, status
);
1263 scsi_req_unref(req
);
1266 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1270 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1271 req
= QTAILQ_FIRST(&sdev
->requests
);
1272 scsi_req_cancel(req
);
1274 sdev
->unit_attention
= sense
;
1277 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1279 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1282 snprintf(path
, sizeof(path
), "%s@%d:%d:%d", qdev_fw_name(dev
),
1283 d
->channel
, d
->id
, d
->lun
);
1285 return strdup(path
);
1288 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1291 SCSIDevice
*target_dev
= NULL
;
1293 QTAILQ_FOREACH_REVERSE(qdev
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1294 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
1296 if (dev
->channel
== channel
&& dev
->id
== id
) {
1297 if (dev
->lun
== lun
) {