2 #include "qemu-error.h"
9 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
11 static struct BusInfo scsi_bus_info
= {
13 .size
= sizeof(SCSIBus
),
14 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
15 .props
= (Property
[]) {
16 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
17 DEFINE_PROP_END_OF_LIST(),
20 static int next_scsi_bus
;
22 /* Create a scsi bus, and attach devices to it. */
23 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
24 const SCSIBusOps
*ops
)
26 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
27 bus
->busnr
= next_scsi_bus
++;
31 bus
->qbus
.allow_hotplug
= 1;
34 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
36 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
37 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
38 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
42 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
43 if (bus
->devs
[dev
->id
] == NULL
)
47 if (dev
->id
>= bus
->ndev
) {
48 error_report("bad scsi device id: %d", dev
->id
);
52 if (bus
->devs
[dev
->id
]) {
53 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
55 bus
->devs
[dev
->id
] = dev
;
58 QTAILQ_INIT(&dev
->requests
);
59 rc
= dev
->info
->init(dev
);
61 bus
->devs
[dev
->id
] = NULL
;
68 static int scsi_qdev_exit(DeviceState
*qdev
)
70 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
71 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
73 assert(bus
->devs
[dev
->id
] != NULL
);
74 if (bus
->devs
[dev
->id
]->info
->destroy
) {
75 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
77 bus
->devs
[dev
->id
] = NULL
;
81 void scsi_qdev_register(SCSIDeviceInfo
*info
)
83 info
->qdev
.bus_info
= &scsi_bus_info
;
84 info
->qdev
.init
= scsi_qdev_init
;
85 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
86 info
->qdev
.exit
= scsi_qdev_exit
;
87 qdev_register(&info
->qdev
);
90 /* handle legacy '-drive if=scsi,...' cmd line args */
91 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
,
92 int unit
, bool removable
)
97 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
98 dev
= qdev_create(&bus
->qbus
, driver
);
99 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
100 if (qdev_prop_exists(dev
, "removable")) {
101 qdev_prop_set_bit(dev
, "removable", removable
);
103 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
107 if (qdev_init(dev
) < 0)
109 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
112 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
119 for (unit
= 0; unit
< bus
->ndev
; unit
++) {
120 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
124 qemu_opts_loc_restore(dinfo
->opts
);
125 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
, false)) {
134 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
138 req
= qemu_mallocz(size
);
140 req
->bus
= scsi_bus_from_device(d
);
145 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
149 void scsi_req_enqueue(SCSIRequest
*req
)
151 assert(!req
->enqueued
);
153 req
->enqueued
= true;
154 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
157 static void scsi_req_dequeue(SCSIRequest
*req
)
159 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
161 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
162 req
->enqueued
= false;
167 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
169 switch (cmd
[0] >> 5) {
171 req
->cmd
.xfer
= cmd
[4];
173 /* length 0 means 256 blocks */
174 if (req
->cmd
.xfer
== 0)
179 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
183 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
187 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
191 trace_scsi_req_parse_bad(req
->dev
->id
, req
->lun
, req
->tag
, cmd
[0]);
196 case TEST_UNIT_READY
:
200 case WRITE_FILEMARKS
:
205 case ALLOW_MEDIUM_REMOVAL
:
208 case SYNCHRONIZE_CACHE
:
209 case LOCK_UNLOCK_CACHE
:
226 case READ_BLOCK_LIMITS
:
232 case SEND_VOLUME_TAG
:
242 case WRITE_VERIFY_12
:
244 case WRITE_VERIFY_16
:
245 req
->cmd
.xfer
*= req
->dev
->blocksize
;
250 case RECOVER_BUFFERED_DATA
:
253 req
->cmd
.xfer
*= req
->dev
->blocksize
;
256 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
258 case MAINTENANCE_OUT
:
260 if (req
->dev
->type
== TYPE_ROM
) {
261 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
262 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
269 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
272 /* stream commands */
275 case RECOVER_BUFFERED_DATA
:
278 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
279 if (cmd
[1] & 0x01) /* fixed */
280 req
->cmd
.xfer
*= req
->dev
->blocksize
;
287 /* generic commands */
289 return scsi_req_length(req
, cmd
);
294 static void scsi_req_xfer_mode(SCSIRequest
*req
)
296 switch (req
->cmd
.buf
[0]) {
301 case WRITE_VERIFY_12
:
303 case WRITE_VERIFY_16
:
307 case CHANGE_DEFINITION
:
311 case SEND_DIAGNOSTIC
:
314 case REASSIGN_BLOCKS
:
322 case SEARCH_EQUAL_12
:
326 case SEND_VOLUME_TAG
:
328 case PERSISTENT_RESERVE_OUT
:
329 case MAINTENANCE_OUT
:
330 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
334 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
336 req
->cmd
.mode
= SCSI_XFER_NONE
;
342 static uint64_t scsi_req_lba(SCSIRequest
*req
)
344 uint8_t *buf
= req
->cmd
.buf
;
347 switch (buf
[0] >> 5) {
349 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
350 (((uint64_t) buf
[1] & 0x1f) << 16);
354 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
355 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
358 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
359 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
360 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
361 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
364 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
365 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
374 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
378 if (req
->dev
->type
== TYPE_TAPE
) {
379 rc
= scsi_req_stream_length(req
, buf
);
381 rc
= scsi_req_length(req
, buf
);
386 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
387 scsi_req_xfer_mode(req
);
388 req
->cmd
.lba
= scsi_req_lba(req
);
389 trace_scsi_req_parsed(req
->dev
->id
, req
->lun
, req
->tag
, buf
[0],
390 req
->cmd
.mode
, req
->cmd
.xfer
, req
->cmd
.lba
);
395 * Predefined sense codes
398 /* No sense data available */
399 const struct SCSISense sense_code_NO_SENSE
= {
400 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
403 /* LUN not ready, Manual intervention required */
404 const struct SCSISense sense_code_LUN_NOT_READY
= {
405 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
408 /* LUN not ready, Medium not present */
409 const struct SCSISense sense_code_NO_MEDIUM
= {
410 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
413 /* Hardware error, internal target failure */
414 const struct SCSISense sense_code_TARGET_FAILURE
= {
415 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
418 /* Illegal request, invalid command operation code */
419 const struct SCSISense sense_code_INVALID_OPCODE
= {
420 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
423 /* Illegal request, LBA out of range */
424 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
425 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
428 /* Illegal request, Invalid field in CDB */
429 const struct SCSISense sense_code_INVALID_FIELD
= {
430 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
433 /* Illegal request, LUN not supported */
434 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
435 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
438 /* Command aborted, I/O process terminated */
439 const struct SCSISense sense_code_IO_ERROR
= {
440 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
443 /* Command aborted, I_T Nexus loss occurred */
444 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
445 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
448 /* Command aborted, Logical Unit failure */
449 const struct SCSISense sense_code_LUN_FAILURE
= {
450 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
456 * Build a sense buffer
458 int scsi_build_sense(SCSISense sense
, uint8_t *buf
, int len
, int fixed
)
460 if (!fixed
&& len
< 8) {
466 /* Return fixed format sense buffer */
471 buf
[13] = sense
.ascq
;
474 /* Return descriptor format sense buffer */
483 static const char *scsi_command_name(uint8_t cmd
)
485 static const char *names
[] = {
486 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
487 [ REZERO_UNIT
] = "REZERO_UNIT",
488 /* REWIND and REZERO_UNIT use the same operation code */
489 [ REQUEST_SENSE
] = "REQUEST_SENSE",
490 [ FORMAT_UNIT
] = "FORMAT_UNIT",
491 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
492 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
493 [ READ_6
] = "READ_6",
494 [ WRITE_6
] = "WRITE_6",
495 [ SEEK_6
] = "SEEK_6",
496 [ READ_REVERSE
] = "READ_REVERSE",
497 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
499 [ INQUIRY
] = "INQUIRY",
500 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
501 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
502 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
503 [ MODE_SELECT
] = "MODE_SELECT",
504 [ RESERVE
] = "RESERVE",
505 [ RELEASE
] = "RELEASE",
508 [ MODE_SENSE
] = "MODE_SENSE",
509 [ START_STOP
] = "START_STOP",
510 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
511 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
512 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
514 [ SET_WINDOW
] = "SET_WINDOW",
515 [ READ_CAPACITY
] = "READ_CAPACITY",
516 [ READ_10
] = "READ_10",
517 [ WRITE_10
] = "WRITE_10",
518 [ SEEK_10
] = "SEEK_10",
519 [ WRITE_VERIFY
] = "WRITE_VERIFY",
520 [ VERIFY
] = "VERIFY",
521 [ SEARCH_HIGH
] = "SEARCH_HIGH",
522 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
523 [ SEARCH_LOW
] = "SEARCH_LOW",
524 [ SET_LIMITS
] = "SET_LIMITS",
525 [ PRE_FETCH
] = "PRE_FETCH",
526 /* READ_POSITION and PRE_FETCH use the same operation code */
527 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
528 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
529 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
530 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
531 [ COMPARE
] = "COMPARE",
532 [ COPY_VERIFY
] = "COPY_VERIFY",
533 [ WRITE_BUFFER
] = "WRITE_BUFFER",
534 [ READ_BUFFER
] = "READ_BUFFER",
535 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
536 [ READ_LONG
] = "READ_LONG",
537 [ WRITE_LONG
] = "WRITE_LONG",
538 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
539 [ WRITE_SAME
] = "WRITE_SAME",
540 [ READ_TOC
] = "READ_TOC",
541 [ LOG_SELECT
] = "LOG_SELECT",
542 [ LOG_SENSE
] = "LOG_SENSE",
543 [ MODE_SELECT_10
] = "MODE_SELECT_10",
544 [ RESERVE_10
] = "RESERVE_10",
545 [ RELEASE_10
] = "RELEASE_10",
546 [ MODE_SENSE_10
] = "MODE_SENSE_10",
547 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
548 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
549 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
550 [ READ_12
] = "READ_12",
551 [ WRITE_12
] = "WRITE_12",
552 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
553 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
554 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
555 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
556 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
557 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
558 [ WRITE_LONG_2
] = "WRITE_LONG_2",
560 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
561 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
562 [ READ_16
] = "READ_16",
563 [ WRITE_16
] = "WRITE_16",
564 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
565 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
566 [ REPORT_LUNS
] = "REPORT_LUNS",
567 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
568 [ SET_CD_SPEED
] = "SET_CD_SPEED",
572 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
577 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
583 void scsi_req_unref(SCSIRequest
*req
)
585 if (--req
->refcount
== 0) {
586 if (req
->dev
->info
->free_req
) {
587 req
->dev
->info
->free_req(req
);
593 /* Called by the devices when data is ready for the HBA. The HBA should
594 start a DMA operation to read or fill the device's data buffer.
595 Once it completes, calling one of req->dev->info->read_data or
596 req->dev->info->write_data (depending on the direction of the
597 transfer) will restart I/O. */
598 void scsi_req_data(SCSIRequest
*req
, int len
)
600 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
601 req
->bus
->ops
->complete(req
, SCSI_REASON_DATA
, len
);
604 void scsi_req_print(SCSIRequest
*req
)
609 fprintf(fp
, "[%s id=%d] %s",
610 req
->dev
->qdev
.parent_bus
->name
,
612 scsi_command_name(req
->cmd
.buf
[0]));
613 for (i
= 1; i
< req
->cmd
.len
; i
++) {
614 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
616 switch (req
->cmd
.mode
) {
618 fprintf(fp
, " - none\n");
620 case SCSI_XFER_FROM_DEV
:
621 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
623 case SCSI_XFER_TO_DEV
:
624 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
627 fprintf(fp
, " - Oops\n");
632 void scsi_req_complete(SCSIRequest
*req
)
634 assert(req
->status
!= -1);
636 scsi_req_dequeue(req
);
637 req
->bus
->ops
->complete(req
, SCSI_REASON_DONE
, req
->status
);
641 void scsi_req_cancel(SCSIRequest
*req
)
643 if (req
->dev
&& req
->dev
->info
->cancel_io
) {
644 req
->dev
->info
->cancel_io(req
);
647 scsi_req_dequeue(req
);
648 if (req
->bus
->ops
->cancel
) {
649 req
->bus
->ops
->cancel(req
);
654 void scsi_req_abort(SCSIRequest
*req
, int status
)
656 req
->status
= status
;
657 if (req
->dev
&& req
->dev
->info
->cancel_io
) {
658 req
->dev
->info
->cancel_io(req
);
660 scsi_req_complete(req
);
663 void scsi_device_purge_requests(SCSIDevice
*sdev
)
667 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
668 req
= QTAILQ_FIRST(&sdev
->requests
);
669 scsi_req_cancel(req
);
673 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
675 SCSIDevice
*d
= (SCSIDevice
*)dev
;
676 SCSIBus
*bus
= scsi_bus_from_device(d
);
680 for (i
= 0; i
< bus
->ndev
; i
++) {
681 if (bus
->devs
[i
] == d
) {
686 assert(i
!= bus
->ndev
);
688 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);