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
,
135 uint32_t lun
, void *hba_private
)
139 req
= qemu_mallocz(size
);
141 req
->bus
= scsi_bus_from_device(d
);
145 req
->hba_private
= hba_private
;
147 trace_scsi_req_alloc(req
->dev
->id
, req
->lun
, req
->tag
);
151 SCSIRequest
*scsi_req_new(SCSIDevice
*d
, uint32_t tag
, uint32_t lun
,
154 return d
->info
->alloc_req(d
, tag
, lun
, hba_private
);
157 uint8_t *scsi_req_get_buf(SCSIRequest
*req
)
159 return req
->dev
->info
->get_buf(req
);
162 int scsi_req_get_sense(SCSIRequest
*req
, uint8_t *buf
, int len
)
164 if (req
->dev
->info
->get_sense
) {
165 return req
->dev
->info
->get_sense(req
, buf
, len
);
171 int32_t scsi_req_enqueue(SCSIRequest
*req
, uint8_t *buf
)
175 assert(!req
->enqueued
);
177 req
->enqueued
= true;
178 QTAILQ_INSERT_TAIL(&req
->dev
->requests
, req
, next
);
181 rc
= req
->dev
->info
->send_command(req
, buf
);
186 static void scsi_req_dequeue(SCSIRequest
*req
)
188 trace_scsi_req_dequeue(req
->dev
->id
, req
->lun
, req
->tag
);
190 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
191 req
->enqueued
= false;
196 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
198 switch (cmd
[0] >> 5) {
200 req
->cmd
.xfer
= cmd
[4];
202 /* length 0 means 256 blocks */
203 if (req
->cmd
.xfer
== 0)
208 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
212 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
216 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
220 trace_scsi_req_parse_bad(req
->dev
->id
, req
->lun
, req
->tag
, cmd
[0]);
225 case TEST_UNIT_READY
:
229 case WRITE_FILEMARKS
:
234 case ALLOW_MEDIUM_REMOVAL
:
237 case SYNCHRONIZE_CACHE
:
238 case LOCK_UNLOCK_CACHE
:
255 case READ_BLOCK_LIMITS
:
261 case SEND_VOLUME_TAG
:
271 case WRITE_VERIFY_12
:
273 case WRITE_VERIFY_16
:
274 req
->cmd
.xfer
*= req
->dev
->blocksize
;
279 case RECOVER_BUFFERED_DATA
:
282 req
->cmd
.xfer
*= req
->dev
->blocksize
;
285 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
287 case MAINTENANCE_OUT
:
289 if (req
->dev
->type
== TYPE_ROM
) {
290 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
291 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
298 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
301 /* stream commands */
304 case RECOVER_BUFFERED_DATA
:
307 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
308 if (cmd
[1] & 0x01) /* fixed */
309 req
->cmd
.xfer
*= req
->dev
->blocksize
;
316 /* generic commands */
318 return scsi_req_length(req
, cmd
);
323 static void scsi_req_xfer_mode(SCSIRequest
*req
)
325 switch (req
->cmd
.buf
[0]) {
330 case WRITE_VERIFY_12
:
332 case WRITE_VERIFY_16
:
336 case CHANGE_DEFINITION
:
340 case SEND_DIAGNOSTIC
:
343 case REASSIGN_BLOCKS
:
351 case SEARCH_EQUAL_12
:
355 case SEND_VOLUME_TAG
:
357 case PERSISTENT_RESERVE_OUT
:
358 case MAINTENANCE_OUT
:
359 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
363 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
365 req
->cmd
.mode
= SCSI_XFER_NONE
;
371 static uint64_t scsi_req_lba(SCSIRequest
*req
)
373 uint8_t *buf
= req
->cmd
.buf
;
376 switch (buf
[0] >> 5) {
378 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
379 (((uint64_t) buf
[1] & 0x1f) << 16);
383 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
384 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
387 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
388 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
389 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
390 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
393 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
394 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
403 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
407 if (req
->dev
->type
== TYPE_TAPE
) {
408 rc
= scsi_req_stream_length(req
, buf
);
410 rc
= scsi_req_length(req
, buf
);
415 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
416 scsi_req_xfer_mode(req
);
417 req
->cmd
.lba
= scsi_req_lba(req
);
418 trace_scsi_req_parsed(req
->dev
->id
, req
->lun
, req
->tag
, buf
[0],
419 req
->cmd
.mode
, req
->cmd
.xfer
);
420 if (req
->cmd
.lba
!= -1) {
421 trace_scsi_req_parsed_lba(req
->dev
->id
, req
->lun
, req
->tag
, buf
[0],
428 * Predefined sense codes
431 /* No sense data available */
432 const struct SCSISense sense_code_NO_SENSE
= {
433 .key
= NO_SENSE
, .asc
= 0x00 , .ascq
= 0x00
436 /* LUN not ready, Manual intervention required */
437 const struct SCSISense sense_code_LUN_NOT_READY
= {
438 .key
= NOT_READY
, .asc
= 0x04, .ascq
= 0x03
441 /* LUN not ready, Medium not present */
442 const struct SCSISense sense_code_NO_MEDIUM
= {
443 .key
= NOT_READY
, .asc
= 0x3a, .ascq
= 0x00
446 /* Hardware error, internal target failure */
447 const struct SCSISense sense_code_TARGET_FAILURE
= {
448 .key
= HARDWARE_ERROR
, .asc
= 0x44, .ascq
= 0x00
451 /* Illegal request, invalid command operation code */
452 const struct SCSISense sense_code_INVALID_OPCODE
= {
453 .key
= ILLEGAL_REQUEST
, .asc
= 0x20, .ascq
= 0x00
456 /* Illegal request, LBA out of range */
457 const struct SCSISense sense_code_LBA_OUT_OF_RANGE
= {
458 .key
= ILLEGAL_REQUEST
, .asc
= 0x21, .ascq
= 0x00
461 /* Illegal request, Invalid field in CDB */
462 const struct SCSISense sense_code_INVALID_FIELD
= {
463 .key
= ILLEGAL_REQUEST
, .asc
= 0x24, .ascq
= 0x00
466 /* Illegal request, LUN not supported */
467 const struct SCSISense sense_code_LUN_NOT_SUPPORTED
= {
468 .key
= ILLEGAL_REQUEST
, .asc
= 0x25, .ascq
= 0x00
471 /* Command aborted, I/O process terminated */
472 const struct SCSISense sense_code_IO_ERROR
= {
473 .key
= ABORTED_COMMAND
, .asc
= 0x00, .ascq
= 0x06
476 /* Command aborted, I_T Nexus loss occurred */
477 const struct SCSISense sense_code_I_T_NEXUS_LOSS
= {
478 .key
= ABORTED_COMMAND
, .asc
= 0x29, .ascq
= 0x07
481 /* Command aborted, Logical Unit failure */
482 const struct SCSISense sense_code_LUN_FAILURE
= {
483 .key
= ABORTED_COMMAND
, .asc
= 0x3e, .ascq
= 0x01
489 * Build a sense buffer
491 int scsi_build_sense(SCSISense sense
, uint8_t *buf
, int len
, int fixed
)
493 if (!fixed
&& len
< 8) {
499 /* Return fixed format sense buffer */
504 buf
[13] = sense
.ascq
;
507 /* Return descriptor format sense buffer */
516 static const char *scsi_command_name(uint8_t cmd
)
518 static const char *names
[] = {
519 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
520 [ REZERO_UNIT
] = "REZERO_UNIT",
521 /* REWIND and REZERO_UNIT use the same operation code */
522 [ REQUEST_SENSE
] = "REQUEST_SENSE",
523 [ FORMAT_UNIT
] = "FORMAT_UNIT",
524 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
525 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
526 [ READ_6
] = "READ_6",
527 [ WRITE_6
] = "WRITE_6",
528 [ SEEK_6
] = "SEEK_6",
529 [ READ_REVERSE
] = "READ_REVERSE",
530 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
532 [ INQUIRY
] = "INQUIRY",
533 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
534 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
535 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
536 [ MODE_SELECT
] = "MODE_SELECT",
537 [ RESERVE
] = "RESERVE",
538 [ RELEASE
] = "RELEASE",
541 [ MODE_SENSE
] = "MODE_SENSE",
542 [ START_STOP
] = "START_STOP",
543 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
544 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
545 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
547 [ SET_WINDOW
] = "SET_WINDOW",
548 [ READ_CAPACITY
] = "READ_CAPACITY",
549 [ READ_10
] = "READ_10",
550 [ WRITE_10
] = "WRITE_10",
551 [ SEEK_10
] = "SEEK_10",
552 [ WRITE_VERIFY
] = "WRITE_VERIFY",
553 [ VERIFY
] = "VERIFY",
554 [ SEARCH_HIGH
] = "SEARCH_HIGH",
555 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
556 [ SEARCH_LOW
] = "SEARCH_LOW",
557 [ SET_LIMITS
] = "SET_LIMITS",
558 [ PRE_FETCH
] = "PRE_FETCH",
559 /* READ_POSITION and PRE_FETCH use the same operation code */
560 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
561 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
562 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
563 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
564 [ COMPARE
] = "COMPARE",
565 [ COPY_VERIFY
] = "COPY_VERIFY",
566 [ WRITE_BUFFER
] = "WRITE_BUFFER",
567 [ READ_BUFFER
] = "READ_BUFFER",
568 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
569 [ READ_LONG
] = "READ_LONG",
570 [ WRITE_LONG
] = "WRITE_LONG",
571 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
572 [ WRITE_SAME
] = "WRITE_SAME",
573 [ READ_TOC
] = "READ_TOC",
574 [ LOG_SELECT
] = "LOG_SELECT",
575 [ LOG_SENSE
] = "LOG_SENSE",
576 [ MODE_SELECT_10
] = "MODE_SELECT_10",
577 [ RESERVE_10
] = "RESERVE_10",
578 [ RELEASE_10
] = "RELEASE_10",
579 [ MODE_SENSE_10
] = "MODE_SENSE_10",
580 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
581 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
582 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
583 [ READ_12
] = "READ_12",
584 [ WRITE_12
] = "WRITE_12",
585 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
586 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
587 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
588 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
589 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
590 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
591 [ WRITE_LONG_2
] = "WRITE_LONG_2",
593 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
594 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
595 [ READ_16
] = "READ_16",
596 [ WRITE_16
] = "WRITE_16",
597 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
598 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
599 [ REPORT_LUNS
] = "REPORT_LUNS",
600 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
601 [ SET_CD_SPEED
] = "SET_CD_SPEED",
605 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
610 SCSIRequest
*scsi_req_ref(SCSIRequest
*req
)
616 void scsi_req_unref(SCSIRequest
*req
)
618 if (--req
->refcount
== 0) {
619 if (req
->dev
->info
->free_req
) {
620 req
->dev
->info
->free_req(req
);
626 /* Tell the device that we finished processing this chunk of I/O. It
627 will start the next chunk or complete the command. */
628 void scsi_req_continue(SCSIRequest
*req
)
630 trace_scsi_req_continue(req
->dev
->id
, req
->lun
, req
->tag
);
631 if (req
->cmd
.mode
== SCSI_XFER_TO_DEV
) {
632 req
->dev
->info
->write_data(req
);
634 req
->dev
->info
->read_data(req
);
638 /* Called by the devices when data is ready for the HBA. The HBA should
639 start a DMA operation to read or fill the device's data buffer.
640 Once it completes, calling scsi_req_continue will restart I/O. */
641 void scsi_req_data(SCSIRequest
*req
, int len
)
643 trace_scsi_req_data(req
->dev
->id
, req
->lun
, req
->tag
, len
);
644 req
->bus
->ops
->transfer_data(req
, len
);
647 void scsi_req_print(SCSIRequest
*req
)
652 fprintf(fp
, "[%s id=%d] %s",
653 req
->dev
->qdev
.parent_bus
->name
,
655 scsi_command_name(req
->cmd
.buf
[0]));
656 for (i
= 1; i
< req
->cmd
.len
; i
++) {
657 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
659 switch (req
->cmd
.mode
) {
661 fprintf(fp
, " - none\n");
663 case SCSI_XFER_FROM_DEV
:
664 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
666 case SCSI_XFER_TO_DEV
:
667 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
670 fprintf(fp
, " - Oops\n");
675 void scsi_req_complete(SCSIRequest
*req
)
677 assert(req
->status
!= -1);
679 scsi_req_dequeue(req
);
680 req
->bus
->ops
->complete(req
, req
->status
);
684 void scsi_req_cancel(SCSIRequest
*req
)
686 if (req
->dev
&& req
->dev
->info
->cancel_io
) {
687 req
->dev
->info
->cancel_io(req
);
690 scsi_req_dequeue(req
);
691 if (req
->bus
->ops
->cancel
) {
692 req
->bus
->ops
->cancel(req
);
697 void scsi_req_abort(SCSIRequest
*req
, int status
)
699 req
->status
= status
;
700 if (req
->dev
&& req
->dev
->info
->cancel_io
) {
701 req
->dev
->info
->cancel_io(req
);
703 scsi_req_complete(req
);
706 void scsi_device_purge_requests(SCSIDevice
*sdev
)
710 while (!QTAILQ_EMPTY(&sdev
->requests
)) {
711 req
= QTAILQ_FIRST(&sdev
->requests
);
712 scsi_req_cancel(req
);
716 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
718 SCSIDevice
*d
= (SCSIDevice
*)dev
;
719 SCSIBus
*bus
= scsi_bus_from_device(d
);
723 for (i
= 0; i
< bus
->ndev
; i
++) {
724 if (bus
->devs
[i
] == d
) {
729 assert(i
!= bus
->ndev
);
731 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);