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
);
13 static struct BusInfo scsi_bus_info
= {
15 .size
= sizeof(SCSIBus
),
16 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
17 .props
= (Property
[]) {
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 int next_scsi_bus
;
26 /* Create a scsi bus, and attach devices to it. */
27 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, const SCSIBusInfo
*info
)
29 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
30 bus
->busnr
= next_scsi_bus
++;
32 bus
->qbus
.allow_hotplug
= 1;
35 static void scsi_dma_restart_bh(void *opaque
)
37 SCSIDevice
*s
= opaque
;
38 SCSIRequest
*req
, *next
;
40 qemu_bh_delete(s
->bh
);
43 QTAILQ_FOREACH_SAFE(req
, &s
->requests
, next
, next
) {
47 switch (req
->cmd
.mode
) {
48 case SCSI_XFER_FROM_DEV
:
49 case SCSI_XFER_TO_DEV
:
50 scsi_req_continue(req
);
53 scsi_req_dequeue(req
);
54 scsi_req_enqueue(req
);
62 void scsi_req_retry(SCSIRequest
*req
)
64 /* No need to save a reference, because scsi_dma_restart_bh just
65 * looks at the request list. */
69 static void scsi_dma_restart_cb(void *opaque
, int running
, RunState state
)
71 SCSIDevice
*s
= opaque
;
77 s
->bh
= qemu_bh_new(scsi_dma_restart_bh
, s
);
78 qemu_bh_schedule(s
->bh
);
82 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
84 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
85 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
86 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
90 if (dev
->channel
> bus
->info
->max_channel
) {
91 error_report("bad scsi channel id: %d", dev
->channel
);
94 if (dev
->id
!= -1 && dev
->id
> bus
->info
->max_target
) {
95 error_report("bad scsi device id: %d", dev
->id
);
101 if (dev
->lun
== -1) {
105 d
= scsi_device_find(bus
, dev
->channel
, ++id
, dev
->lun
);
106 } while (d
&& d
->lun
== dev
->lun
&& id
<= bus
->info
->max_target
);
107 if (id
> bus
->info
->max_target
) {
108 error_report("no free target");
112 } else if (dev
->lun
== -1) {
115 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, ++lun
);
116 } while (d
&& d
->lun
== lun
&& lun
< bus
->info
->max_lun
);
117 if (lun
> bus
->info
->max_lun
) {
118 error_report("no free lun");
123 d
= scsi_device_find(bus
, dev
->channel
, dev
->id
, dev
->lun
);
124 if (dev
->lun
== d
->lun
&& dev
!= d
) {
130 QTAILQ_INIT(&dev
->requests
);
131 rc
= dev
->info
->init(dev
);
133 dev
->vmsentry
= qemu_add_vm_change_state_handler(scsi_dma_restart_cb
,
141 static int scsi_qdev_exit(DeviceState
*qdev
)
143 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
146 qemu_del_vm_change_state_handler(dev
->vmsentry
);
148 if (dev
->info
->destroy
) {
149 dev
->info
->destroy(dev
);
154 void scsi_qdev_register(SCSIDeviceInfo
*info
)
156 info
->qdev
.bus_info
= &scsi_bus_info
;
157 info
->qdev
.init
= scsi_qdev_init
;
158 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
159 info
->qdev
.exit
= scsi_qdev_exit
;
160 qdev_register(&info
->qdev
);
163 /* handle legacy '-drive if=scsi,...' cmd line args */
164 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
165 int unit
, bool removable
, int bootindex
)
170 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
171 dev
= qdev_create(&bus
->qbus
, driver
);
172 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
173 if (bootindex
>= 0) {
174 qdev_prop_set_int32(dev
, "bootindex", bootindex
);
176 if (qdev_prop_exists(dev
, "removable")) {
177 qdev_prop_set_bit(dev
, "removable", removable
);
179 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
183 if (qdev_init(dev
) < 0)
185 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
188 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
195 for (unit
= 0; unit
< bus
->info
->max_target
; unit
++) {
196 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
200 qemu_opts_loc_restore(dinfo
->opts
);
201 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false, -1)) {
210 /* SCSIReqOps implementation for invalid commands. */
212 static int32_t scsi_invalid_command(SCSIRequest
*req
, uint8_t *buf
)
214 scsi_req_build_sense(req
, SENSE_CODE(INVALID_OPCODE
));
215 scsi_req_complete(req
, CHECK_CONDITION
);
219 static const struct SCSIReqOps reqops_invalid_opcode
= {
220 .size
= sizeof(SCSIRequest
),
221 .send_command
= scsi_invalid_command
224 /* SCSIReqOps implementation for unit attention conditions. */
226 static int32_t scsi_unit_attention(SCSIRequest
*req
, uint8_t *buf
)
228 if (req
->dev
&& req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
229 scsi_req_build_sense(req
, req
->dev
->unit_attention
);
230 } else if (req
->bus
->unit_attention
.key
== UNIT_ATTENTION
) {
231 scsi_req_build_sense(req
, req
->bus
->unit_attention
);
233 scsi_req_complete(req
, CHECK_CONDITION
);
237 static const struct SCSIReqOps reqops_unit_attention
= {
238 .size
= sizeof(SCSIRequest
),
239 .send_command
= scsi_unit_attention
242 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
245 typedef struct SCSITargetReq SCSITargetReq
;
247 struct SCSITargetReq
{
253 static void store_lun(uint8_t *outbuf
, int lun
)
259 outbuf
[1] = (lun
& 255);
260 outbuf
[0] = (lun
>> 8) | 0x40;
263 static bool scsi_target_emulate_report_luns(SCSITargetReq
*r
)
270 if (r
->req
.cmd
.xfer
< 16) {
273 if (r
->req
.cmd
.buf
[2] > 2) {
276 channel
= r
->req
.dev
->channel
;
280 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
281 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
283 if (dev
->channel
== channel
&& dev
->id
== id
) {
293 len
= MIN(n
+ 8, r
->req
.cmd
.xfer
& ~7);
294 if (len
> sizeof(r
->buf
)) {
295 /* TODO: > 256 LUNs? */
299 memset(r
->buf
, 0, len
);
300 stl_be_p(&r
->buf
, n
);
301 i
= found_lun0
? 8 : 16;
302 QTAILQ_FOREACH(qdev
, &r
->req
.bus
->qbus
.children
, sibling
) {
303 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
305 if (dev
->channel
== channel
&& dev
->id
== id
) {
306 store_lun(&r
->buf
[i
], dev
->lun
);
315 static bool scsi_target_emulate_inquiry(SCSITargetReq
*r
)
317 assert(r
->req
.dev
->lun
!= r
->req
.lun
);
318 if (r
->req
.cmd
.buf
[1] & 0x2) {
319 /* Command support data - optional, not implemented */
323 if (r
->req
.cmd
.buf
[1] & 0x1) {
324 /* Vital product data */
325 uint8_t page_code
= r
->req
.cmd
.buf
[2];
326 if (r
->req
.cmd
.xfer
< 4) {
330 r
->buf
[r
->len
++] = page_code
; /* this page */
331 r
->buf
[r
->len
++] = 0x00;
334 case 0x00: /* Supported page codes, mandatory */
338 r
->buf
[r
->len
++] = 0x00; /* list of supported pages (this page) */
339 r
->buf
[pages
] = r
->len
- pages
- 1; /* number of pages */
346 assert(r
->len
< sizeof(r
->buf
));
347 r
->len
= MIN(r
->req
.cmd
.xfer
, r
->len
);
351 /* Standard INQUIRY data */
352 if (r
->req
.cmd
.buf
[2] != 0) {
357 if (r
->req
.cmd
.xfer
< 5) {
361 r
->len
= MIN(r
->req
.cmd
.xfer
, 36);
362 memset(r
->buf
, 0, r
->len
);
363 if (r
->req
.lun
!= 0) {
364 r
->buf
[0] = TYPE_NO_LUN
;
366 r
->buf
[0] = TYPE_NOT_PRESENT
| TYPE_INACTIVE
;
367 r
->buf
[2] = 5; /* Version */
368 r
->buf
[3] = 2 | 0x10; /* HiSup, response data format */
369 r
->buf
[4] = r
->len
- 5; /* Additional Length = (Len - 1) - 4 */
370 r
->buf
[7] = 0x10 | (r
->req
.bus
->info
->tcq
? 0x02 : 0); /* Sync, TCQ. */
371 memcpy(&r
->buf
[8], "QEMU ", 8);
372 memcpy(&r
->buf
[16], "QEMU TARGET ", 16);
373 strncpy((char *) &r
->buf
[32], QEMU_VERSION
, 4);
378 static int32_t scsi_target_send_command(SCSIRequest
*req
, uint8_t *buf
)
380 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
384 if (!scsi_target_emulate_report_luns(r
)) {
385 goto illegal_request
;
389 if (!scsi_target_emulate_inquiry(r
)) {
390 goto illegal_request
;
394 if (req
->cmd
.xfer
< 4) {
395 goto illegal_request
;
397 r
->len
= scsi_device_get_sense(r
->req
.dev
, r
->buf
,
398 MIN(req
->cmd
.xfer
, sizeof r
->buf
),
399 (req
->cmd
.buf
[1] & 1) == 0);
400 if (r
->req
.dev
->sense_is_ua
) {
401 if (r
->req
.dev
->info
->unit_attention_reported
) {
402 r
->req
.dev
->info
->unit_attention_reported(req
->dev
);
404 r
->req
.dev
->sense_len
= 0;
405 r
->req
.dev
->sense_is_ua
= false;
409 scsi_req_build_sense(req
, SENSE_CODE(LUN_NOT_SUPPORTED
));
410 scsi_req_complete(req
, CHECK_CONDITION
);
413 scsi_req_build_sense(req
, SENSE_CODE(INVALID_FIELD
));
414 scsi_req_complete(req
, CHECK_CONDITION
);
419 scsi_req_complete(req
, GOOD
);
424 static void scsi_target_read_data(SCSIRequest
*req
)
426 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
432 scsi_req_data(&r
->req
, n
);
434 scsi_req_complete(&r
->req
, GOOD
);
438 static uint8_t *scsi_target_get_buf(SCSIRequest
*req
)
440 SCSITargetReq
*r
= DO_UPCAST(SCSITargetReq
, req
, req
);
445 static const struct SCSIReqOps reqops_target_command
= {
446 .size
= sizeof(SCSITargetReq
),
447 .send_command
= scsi_target_send_command
,
448 .read_data
= scsi_target_read_data
,
449 .get_buf
= scsi_target_get_buf
,
453 SCSIRequest
*scsi_req_alloc(const SCSIReqOps
*reqops
, SCSIDevice
*d
,
454 uint32_t tag
, uint32_t lun
, void *hba_private
)
458 req
= g_malloc0(reqops
->size
);
460 req
->bus
= scsi_bus_from_device(d
);
464 req
->hba_private
= hba_private
;
468 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
472 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
473 uint8_t *buf
, void *hba_private
)
475 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, d
->qdev
.parent_bus
);
479 if (scsi_req_parse(&cmd
, d
, buf
) != 0) {
480 trace_scsi_req_parse_bad(d
->id
, lun
, tag
, buf
[0]);
481 req
= scsi_req_alloc(&reqops_invalid_opcode
, d
, tag
, lun
, hba_private
);
483 trace_scsi_req_parsed(d
->id
, lun
, tag
, buf
[0],
486 trace_scsi_req_parsed_lba(d
->id
, lun
, tag
, buf
[0],
490 if ((d
->unit_attention
.key
== UNIT_ATTENTION
||
491 bus
->unit_attention
.key
== UNIT_ATTENTION
) &&
492 (buf
[0] != INQUIRY
&&
493 buf
[0] != REPORT_LUNS
&&
494 buf
[0] != GET_CONFIGURATION
&&
495 buf
[0] != GET_EVENT_STATUS_NOTIFICATION
&&
498 * If we already have a pending unit attention condition,
499 * report this one before triggering another one.
501 !(buf
[0] == REQUEST_SENSE
&& d
->sense_is_ua
))) {
502 req
= scsi_req_alloc(&reqops_unit_attention
, d
, tag
, lun
,
504 } else if (lun
!= d
->lun
||
505 buf
[0] == REPORT_LUNS
||
506 (buf
[0] == REQUEST_SENSE
&& (d
->sense_len
|| cmd
.xfer
< 4))) {
507 req
= scsi_req_alloc(&reqops_target_command
, d
, tag
, lun
,
510 req
= d
->info
->alloc_req(d
, tag
, lun
, buf
, hba_private
);
517 trace_scsi_inquiry(d
->id
, lun
, tag
, cmd
.buf
[1], cmd
.buf
[2]);
519 case TEST_UNIT_READY
:
520 trace_scsi_test_unit_ready(d
->id
, lun
, tag
);
523 trace_scsi_report_luns(d
->id
, lun
, tag
);
526 trace_scsi_request_sense(d
->id
, lun
, tag
);
535 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
537 return req
->ops
->get_buf(req
);
540 static void scsi_clear_unit_attention(SCSIRequest
*req
)
543 if (req
->dev
->unit_attention
.key
!= UNIT_ATTENTION
&&
544 req
->bus
->unit_attention
.key
!= UNIT_ATTENTION
) {
549 * If an INQUIRY command enters the enabled command state,
550 * the device server shall [not] clear any unit attention condition;
551 * See also MMC-6, paragraphs 6.5 and 6.6.2.
553 if (req
->cmd
.buf
[0] == INQUIRY
||
554 req
->cmd
.buf
[0] == GET_CONFIGURATION
||
555 req
->cmd
.buf
[0] == GET_EVENT_STATUS_NOTIFICATION
) {
559 if (req
->dev
->unit_attention
.key
== UNIT_ATTENTION
) {
560 ua
= &req
->dev
->unit_attention
;
562 ua
= &req
->bus
->unit_attention
;
566 * If a REPORT LUNS command enters the enabled command state, [...]
567 * the device server shall clear any pending unit attention condition
568 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
570 if (req
->cmd
.buf
[0] == REPORT_LUNS
&&
571 !(ua
->asc
== SENSE_CODE(REPORTED_LUNS_CHANGED
).asc
&&
572 ua
->ascq
== SENSE_CODE(REPORTED_LUNS_CHANGED
).ascq
)) {
576 *ua
= SENSE_CODE(NO_SENSE
);
579 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
584 if (!req
->sense_len
) {
588 ret
= scsi_build_sense(req
->sense
, req
->sense_len
, buf
, len
, true);
591 * FIXME: clearing unit attention conditions upon autosense should be done
592 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
595 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
596 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
597 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
599 if (req
->dev
->sense_is_ua
) {
600 if (req
->dev
->info
->unit_attention_reported
) {
601 req
->dev
->info
->unit_attention_reported(req
->dev
);
603 req
->dev
->sense_len
= 0;
604 req
->dev
->sense_is_ua
= false;
609 int scsi_device_get_sense(SCSIDevice
*dev
, uint8_t *buf
, int len
, bool fixed
)
611 return scsi_build_sense(dev
->sense
, dev
->sense_len
, buf
, len
, fixed
);
614 void scsi_req_build_sense(SCSIRequest
*req
, SCSISense sense
)
616 trace_scsi_req_build_sense(req
->dev
->id
, req
->lun
, req
->tag
,
617 sense
.key
, sense
.asc
, sense
.ascq
);
618 memset(req
->sense
, 0, 18);
619 req
->sense
[0] = 0xf0;
620 req
->sense
[2] = sense
.key
;
622 req
->sense
[12] = sense
.asc
;
623 req
->sense
[13] = sense
.ascq
;
627 int32_t scsi_req_enqueue(SCSIRequest
*req
)
631 assert(!req
->enqueued
);
633 req
->enqueued
= true;
634 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
637 rc
= req
->ops
->send_command(req
, req
->cmd
.buf
);
642 static void scsi_req_dequeue(SCSIRequest
*req
)
644 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
647 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
648 req
->enqueued
= false;
653 static int scsi_get_performance_length(int num_desc
, int type
, int data_type
)
655 /* MMC-6, paragraph 6.7. */
658 if ((data_type
& 3) == 0) {
659 /* Each descriptor is as in Table 295 - Nominal performance. */
660 return 16 * num_desc
+ 8;
662 /* Each descriptor is as in Table 296 - Exceptions. */
663 return 6 * num_desc
+ 8;
668 return 8 * num_desc
+ 8;
670 return 2048 * num_desc
+ 8;
672 return 16 * num_desc
+ 8;
678 static int scsi_req_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
680 switch (buf
[0] >> 5) {
684 /* length 0 means 256 blocks */
685 if (cmd
->xfer
== 0) {
691 cmd
->xfer
= lduw_be_p(&buf
[7]);
695 cmd
->xfer
= ldl_be_p(&buf
[10]) & 0xffffffffULL
;
699 cmd
->xfer
= ldl_be_p(&buf
[6]) & 0xffffffffULL
;
707 case TEST_UNIT_READY
:
711 case WRITE_FILEMARKS
:
712 case WRITE_FILEMARKS_16
:
717 case ALLOW_MEDIUM_REMOVAL
:
720 case SYNCHRONIZE_CACHE
:
721 case SYNCHRONIZE_CACHE_16
:
723 case LOCK_UNLOCK_CACHE
:
734 case ALLOW_OVERWRITE
:
742 case READ_CAPACITY_10
:
745 case READ_BLOCK_LIMITS
:
748 case SEND_VOLUME_TAG
:
749 /* GPCMD_SET_STREAMING from multimedia commands. */
750 if (dev
->type
== TYPE_ROM
) {
751 cmd
->xfer
= buf
[10] | (buf
[9] << 8);
753 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
757 case WRITE_VERIFY_10
:
760 case WRITE_VERIFY_12
:
762 case WRITE_VERIFY_16
:
763 cmd
->xfer
*= dev
->blocksize
;
768 case RECOVER_BUFFERED_DATA
:
771 cmd
->xfer
*= dev
->blocksize
;
774 /* MMC mandates the parameter list to be 12-bytes long. Parameters
775 * for block devices are restricted to the header right now. */
776 if (dev
->type
== TYPE_ROM
&& (buf
[1] & 16)) {
779 cmd
->xfer
= (buf
[1] & 16) == 0 ? 0 : (buf
[1] & 32 ? 8 : 4);
783 case RECEIVE_DIAGNOSTIC
:
784 case SEND_DIAGNOSTIC
:
785 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
791 cmd
->xfer
= buf
[8] | (buf
[7] << 8) | (buf
[6] << 16);
793 case PERSISTENT_RESERVE_OUT
:
794 cmd
->xfer
= ldl_be_p(&buf
[5]) & 0xffffffffULL
;
797 if (dev
->type
== TYPE_ROM
) {
798 /* MMC command GET PERFORMANCE. */
799 cmd
->xfer
= scsi_get_performance_length(buf
[9] | (buf
[8] << 8),
800 buf
[10], buf
[1] & 0x1f);
803 case MECHANISM_STATUS
:
804 case READ_DVD_STRUCTURE
:
805 case SEND_DVD_STRUCTURE
:
806 case MAINTENANCE_OUT
:
808 if (dev
->type
== TYPE_ROM
) {
809 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
810 cmd
->xfer
= buf
[9] | (buf
[8] << 8);
817 static int scsi_req_stream_length(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
820 /* stream commands */
827 case RECOVER_BUFFERED_DATA
:
830 cmd
->xfer
= buf
[4] | (buf
[3] << 8) | (buf
[2] << 16);
831 if (buf
[1] & 0x01) { /* fixed */
832 cmd
->xfer
*= dev
->blocksize
;
841 cmd
->xfer
= buf
[13] | (buf
[12] << 8);
844 cmd
->xfer
= buf
[8] | (buf
[7] << 8);
847 cmd
->xfer
= buf
[4] | (buf
[3] << 8);
849 /* generic commands */
851 return scsi_req_length(cmd
, dev
, buf
);
856 static void scsi_cmd_xfer_mode(SCSICommand
*cmd
)
858 switch (cmd
->buf
[0]) {
861 case WRITE_VERIFY_10
:
863 case WRITE_VERIFY_12
:
865 case WRITE_VERIFY_16
:
869 case CHANGE_DEFINITION
:
873 case SEND_DIAGNOSTIC
:
876 case REASSIGN_BLOCKS
:
884 case SEARCH_EQUAL_12
:
887 case SEND_VOLUME_TAG
:
889 case SEND_DVD_STRUCTURE
:
890 case PERSISTENT_RESERVE_OUT
:
891 case MAINTENANCE_OUT
:
892 cmd
->mode
= SCSI_XFER_TO_DEV
;
896 cmd
->mode
= SCSI_XFER_FROM_DEV
;
898 cmd
->mode
= SCSI_XFER_NONE
;
904 static uint64_t scsi_cmd_lba(SCSICommand
*cmd
)
906 uint8_t *buf
= cmd
->buf
;
909 switch (buf
[0] >> 5) {
911 lba
= ldl_be_p(&buf
[0]) & 0x1fffff;
916 lba
= ldl_be_p(&buf
[2]) & 0xffffffffULL
;
919 lba
= ldq_be_p(&buf
[2]);
928 int scsi_req_parse(SCSICommand
*cmd
, SCSIDevice
*dev
, uint8_t *buf
)
932 if (dev
->type
== TYPE_TAPE
) {
933 rc
= scsi_req_stream_length(cmd
, dev
, buf
);
935 rc
= scsi_req_length(cmd
, dev
, buf
);
940 memcpy(cmd
->buf
, buf
, cmd
->len
);
941 scsi_cmd_xfer_mode(cmd
);
942 cmd
->lba
= scsi_cmd_lba(cmd
);
947 * Predefined sense codes
950 /* No sense data available */
951 const struct SCSISense sense_code_NO_SENSE
= {
952 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
955 /* LUN not ready, Manual intervention required */
956 const struct SCSISense sense_code_LUN_NOT_READY
= {
957 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
960 /* LUN not ready, Medium not present */
961 const struct SCSISense sense_code_NO_MEDIUM
= {
962 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
965 /* LUN not ready, medium removal prevented */
966 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED
= {
967 .key
= NOT_READY
, .asc
= 0x53, .ascq
= 0x00
970 /* Hardware error, internal target failure */
971 const struct SCSISense sense_code_TARGET_FAILURE
= {
972 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
975 /* Illegal request, invalid command operation code */
976 const struct SCSISense sense_code_INVALID_OPCODE
= {
977 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
980 /* Illegal request, LBA out of range */
981 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
982 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
985 /* Illegal request, Invalid field in CDB */
986 const struct SCSISense sense_code_INVALID_FIELD
= {
987 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
990 /* Illegal request, LUN not supported */
991 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
992 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
995 /* Illegal request, Saving parameters not supported */
996 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED
= {
997 .key
= ILLEGAL_REQUEST
, .asc
= 0x39, .ascq
= 0x00
1000 /* Illegal request, Incompatible medium installed */
1001 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT
= {
1002 .key
= ILLEGAL_REQUEST
, .asc
= 0x30, .ascq
= 0x00
1005 /* Illegal request, medium removal prevented */
1006 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED
= {
1007 .key
= ILLEGAL_REQUEST
, .asc
= 0x53, .ascq
= 0x00
1010 /* Command aborted, I/O process terminated */
1011 const struct SCSISense sense_code_IO_ERROR
= {
1012 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
1015 /* Command aborted, I_T Nexus loss occurred */
1016 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
1017 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
1020 /* Command aborted, Logical Unit failure */
1021 const struct SCSISense sense_code_LUN_FAILURE
= {
1022 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
1025 /* Unit attention, Power on, reset or bus device reset occurred */
1026 const struct SCSISense sense_code_RESET
= {
1027 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x00
1030 /* Unit attention, No medium */
1031 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM
= {
1032 .key
= UNIT_ATTENTION
, .asc
= 0x3a, .ascq
= 0x00
1035 /* Unit attention, Medium may have changed */
1036 const struct SCSISense sense_code_MEDIUM_CHANGED
= {
1037 .key
= UNIT_ATTENTION
, .asc
= 0x28, .ascq
= 0x00
1040 /* Unit attention, Reported LUNs data has changed */
1041 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED
= {
1042 .key
= UNIT_ATTENTION
, .asc
= 0x3f, .ascq
= 0x0e
1045 /* Unit attention, Device internal reset */
1046 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET
= {
1047 .key
= UNIT_ATTENTION
, .asc
= 0x29, .ascq
= 0x04
1053 * Convert between fixed and descriptor sense buffers
1055 int scsi_build_sense(uint8_t *in_buf
, int in_len
,
1056 uint8_t *buf
, int len
, bool fixed
)
1060 if (!fixed
&& len
< 8) {
1065 sense
.key
= NO_SENSE
;
1069 fixed_in
= (in_buf
[0] & 2) == 0;
1071 if (fixed
== fixed_in
) {
1072 memcpy(buf
, in_buf
, MIN(len
, in_len
));
1073 return MIN(len
, in_len
);
1077 sense
.key
= in_buf
[2];
1078 sense
.asc
= in_buf
[12];
1079 sense
.ascq
= in_buf
[13];
1081 sense
.key
= in_buf
[1];
1082 sense
.asc
= in_buf
[2];
1083 sense
.ascq
= in_buf
[3];
1087 memset(buf
, 0, len
);
1089 /* Return fixed format sense buffer */
1093 buf
[12] = sense
.asc
;
1094 buf
[13] = sense
.ascq
;
1095 return MIN(len
, 18);
1097 /* Return descriptor format sense buffer */
1101 buf
[3] = sense
.ascq
;
1106 static const char *scsi_command_name(uint8_t cmd
)
1108 static const char *names
[] = {
1109 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
1110 [ REWIND
] = "REWIND",
1111 [ REQUEST_SENSE
] = "REQUEST_SENSE",
1112 [ FORMAT_UNIT
] = "FORMAT_UNIT",
1113 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
1114 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
1115 [ READ_6
] = "READ_6",
1116 [ WRITE_6
] = "WRITE_6",
1117 [ SET_CAPACITY
] = "SET_CAPACITY",
1118 [ READ_REVERSE
] = "READ_REVERSE",
1119 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
1120 [ SPACE
] = "SPACE",
1121 [ INQUIRY
] = "INQUIRY",
1122 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
1123 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
1124 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
1125 [ MODE_SELECT
] = "MODE_SELECT",
1126 [ RESERVE
] = "RESERVE",
1127 [ RELEASE
] = "RELEASE",
1129 [ ERASE
] = "ERASE",
1130 [ MODE_SENSE
] = "MODE_SENSE",
1131 [ START_STOP
] = "START_STOP",
1132 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
1133 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
1134 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
1135 [ READ_CAPACITY_10
] = "READ_CAPACITY_10",
1136 [ READ_10
] = "READ_10",
1137 [ WRITE_10
] = "WRITE_10",
1138 [ SEEK_10
] = "SEEK_10",
1139 [ WRITE_VERIFY_10
] = "WRITE_VERIFY_10",
1140 [ VERIFY_10
] = "VERIFY_10",
1141 [ SEARCH_HIGH
] = "SEARCH_HIGH",
1142 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
1143 [ SEARCH_LOW
] = "SEARCH_LOW",
1144 [ SET_LIMITS
] = "SET_LIMITS",
1145 [ PRE_FETCH
] = "PRE_FETCH/READ_POSITION",
1146 /* READ_POSITION and PRE_FETCH use the same operation code */
1147 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
1148 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
1149 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
1150 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
1151 [ COMPARE
] = "COMPARE",
1152 [ COPY_VERIFY
] = "COPY_VERIFY",
1153 [ WRITE_BUFFER
] = "WRITE_BUFFER",
1154 [ READ_BUFFER
] = "READ_BUFFER",
1155 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
1156 [ READ_LONG_10
] = "READ_LONG_10",
1157 [ WRITE_LONG_10
] = "WRITE_LONG_10",
1158 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
1159 [ WRITE_SAME_10
] = "WRITE_SAME_10",
1160 [ UNMAP
] = "UNMAP",
1161 [ READ_TOC
] = "READ_TOC",
1162 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
1163 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
1164 [ LOG_SELECT
] = "LOG_SELECT",
1165 [ LOG_SENSE
] = "LOG_SENSE",
1166 [ MODE_SELECT_10
] = "MODE_SELECT_10",
1167 [ RESERVE_10
] = "RESERVE_10",
1168 [ RELEASE_10
] = "RELEASE_10",
1169 [ MODE_SENSE_10
] = "MODE_SENSE_10",
1170 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
1171 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
1172 [ WRITE_FILEMARKS_16
] = "WRITE_FILEMARKS_16",
1173 [ EXTENDED_COPY
] = "EXTENDED_COPY",
1174 [ ATA_PASSTHROUGH
] = "ATA_PASSTHROUGH",
1175 [ ACCESS_CONTROL_IN
] = "ACCESS_CONTROL_IN",
1176 [ ACCESS_CONTROL_OUT
] = "ACCESS_CONTROL_OUT",
1177 [ READ_16
] = "READ_16",
1178 [ COMPARE_AND_WRITE
] = "COMPARE_AND_WRITE",
1179 [ WRITE_16
] = "WRITE_16",
1180 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
1181 [ VERIFY_16
] = "VERIFY_16",
1182 [ PRE_FETCH_16
] = "PRE_FETCH_16",
1183 [ SYNCHRONIZE_CACHE_16
] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1184 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1185 [ LOCATE_16
] = "LOCATE_16",
1186 [ WRITE_SAME_16
] = "ERASE_16/WRITE_SAME_16",
1187 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1188 [ SERVICE_ACTION_IN_16
] = "SERVICE_ACTION_IN_16",
1189 [ WRITE_LONG_16
] = "WRITE_LONG_16",
1190 [ REPORT_LUNS
] = "REPORT_LUNS",
1191 [ BLANK
] = "BLANK",
1192 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
1193 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
1194 [ READ_12
] = "READ_12",
1195 [ WRITE_12
] = "WRITE_12",
1196 [ ERASE_12
] = "ERASE_12/GET_PERFORMANCE",
1197 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1198 [ SERVICE_ACTION_IN_12
] = "SERVICE_ACTION_IN_12",
1199 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
1200 [ VERIFY_12
] = "VERIFY_12",
1201 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
1202 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
1203 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
1204 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
1205 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG/SET_STREAMING",
1206 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1207 [ READ_CD
] = "READ_CD",
1208 [ READ_DEFECT_DATA_12
] = "READ_DEFECT_DATA_12",
1209 [ READ_DVD_STRUCTURE
] = "READ_DVD_STRUCTURE",
1210 [ RESERVE_TRACK
] = "RESERVE_TRACK",
1211 [ SEND_CUE_SHEET
] = "SEND_CUE_SHEET",
1212 [ SEND_DVD_STRUCTURE
] = "SEND_DVD_STRUCTURE",
1213 [ SET_CD_SPEED
] = "SET_CD_SPEED",
1214 [ SET_READ_AHEAD
] = "SET_READ_AHEAD",
1215 [ ALLOW_OVERWRITE
] = "ALLOW_OVERWRITE",
1216 [ MECHANISM_STATUS
] = "MECHANISM_STATUS",
1219 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
1224 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
1230 void scsi_req_unref(SCSIRequest
*req
)
1232 if (--req
->refcount
== 0) {
1233 if (req
->ops
->free_req
) {
1234 req
->ops
->free_req(req
);
1240 /* Tell the device that we finished processing this chunk of I/O. It
1241 will start the next chunk or complete the command. */
1242 void scsi_req_continue(SCSIRequest
*req
)
1244 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
1245 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
1246 req
->ops
->write_data(req
);
1248 req
->ops
->read_data(req
);
1252 /* Called by the devices when data is ready for the HBA. The HBA should
1253 start a DMA operation to read or fill the device's data buffer.
1254 Once it completes, calling scsi_req_continue will restart I/O. */
1255 void scsi_req_data(SCSIRequest
*req
, int len
)
1257 if (req
->io_canceled
) {
1258 trace_scsi_req_data_canceled(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1260 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
1261 req
->bus
->info
->transfer_data(req
, len
);
1265 void scsi_req_print(SCSIRequest
*req
)
1270 fprintf(fp
, "[%s id=%d] %s",
1271 req
->dev
->qdev
.parent_bus
->name
,
1273 scsi_command_name(req
->cmd
.buf
[0]));
1274 for (i
= 1; i
< req
->cmd
.len
; i
++) {
1275 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
1277 switch (req
->cmd
.mode
) {
1278 case SCSI_XFER_NONE
:
1279 fprintf(fp
, " - none\n");
1281 case SCSI_XFER_FROM_DEV
:
1282 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
1284 case SCSI_XFER_TO_DEV
:
1285 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
1288 fprintf(fp
, " - Oops\n");
1293 void scsi_req_complete(SCSIRequest
*req
, int status
)
1295 assert(req
->status
== -1);
1296 req
->status
= status
;
1298 assert(req
->sense_len
< sizeof(req
->sense
));
1299 if (status
== GOOD
) {
1303 if (req
->sense_len
) {
1304 memcpy(req
->dev
->sense
, req
->sense
, req
->sense_len
);
1305 req
->dev
->sense_len
= req
->sense_len
;
1306 req
->dev
->sense_is_ua
= (req
->ops
== &reqops_unit_attention
);
1308 req
->dev
->sense_len
= 0;
1309 req
->dev
->sense_is_ua
= false;
1313 * Unit attention state is now stored in the device's sense buffer
1314 * if the HBA didn't do autosense. Clear the pending unit attention
1317 scsi_clear_unit_attention(req
);
1320 scsi_req_dequeue(req
);
1321 req
->bus
->info
->complete(req
, req
->status
);
1322 scsi_req_unref(req
);
1325 void scsi_req_cancel(SCSIRequest
*req
)
1327 if (!req
->enqueued
) {
1331 scsi_req_dequeue(req
);
1332 req
->io_canceled
= true;
1333 if (req
->ops
->cancel_io
) {
1334 req
->ops
->cancel_io(req
);
1336 if (req
->bus
->info
->cancel
) {
1337 req
->bus
->info
->cancel(req
);
1339 scsi_req_unref(req
);
1342 void scsi_req_abort(SCSIRequest
*req
, int status
)
1344 if (!req
->enqueued
) {
1348 scsi_req_dequeue(req
);
1349 req
->io_canceled
= true;
1350 if (req
->ops
->cancel_io
) {
1351 req
->ops
->cancel_io(req
);
1353 scsi_req_complete(req
, status
);
1354 scsi_req_unref(req
);
1357 void scsi_device_purge_requests(SCSIDevice
*sdev
, SCSISense sense
)
1361 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
1362 req
= QTAILQ_FIRST(&sdev
->requests
);
1363 scsi_req_cancel(req
);
1365 sdev
->unit_attention
= sense
;
1368 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
1370 SCSIDevice
*d
= DO_UPCAST(SCSIDevice
, qdev
, dev
);
1373 snprintf(path
, sizeof(path
), "channel@%x/%s@%x,%x", d
->channel
,
1374 qdev_fw_name(dev
), d
->id
, d
->lun
);
1376 return strdup(path
);
1379 SCSIDevice
*scsi_device_find(SCSIBus
*bus
, int channel
, int id
, int lun
)
1382 SCSIDevice
*target_dev
= NULL
;
1384 QTAILQ_FOREACH_REVERSE(qdev
, &bus
->qbus
.children
, ChildrenHead
, sibling
) {
1385 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
1387 if (dev
->channel
== channel
&& dev
->id
== id
) {
1388 if (dev
->lun
== lun
) {