2 #include "qemu-error.h"
7 static struct BusInfo scsi_bus_info
= {
9 .size
= sizeof(SCSIBus
),
10 .props
= (Property
[]) {
11 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
12 DEFINE_PROP_END_OF_LIST(),
15 static int next_scsi_bus
;
17 /* Create a scsi bus, and attach devices to it. */
18 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
19 scsi_completionfn complete
)
21 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
22 bus
->busnr
= next_scsi_bus
++;
25 bus
->complete
= complete
;
26 bus
->qbus
.allow_hotplug
= 1;
29 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
31 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
32 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
33 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
37 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
38 if (bus
->devs
[dev
->id
] == NULL
)
42 if (dev
->id
>= bus
->ndev
) {
43 error_report("bad scsi device id: %d", dev
->id
);
47 if (bus
->devs
[dev
->id
]) {
48 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
50 bus
->devs
[dev
->id
] = dev
;
53 QTAILQ_INIT(&dev
->requests
);
54 rc
= dev
->info
->init(dev
);
56 bus
->devs
[dev
->id
] = NULL
;
63 static int scsi_qdev_exit(DeviceState
*qdev
)
65 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
66 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
68 assert(bus
->devs
[dev
->id
] != NULL
);
69 if (bus
->devs
[dev
->id
]->info
->destroy
) {
70 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
72 bus
->devs
[dev
->id
] = NULL
;
76 void scsi_qdev_register(SCSIDeviceInfo
*info
)
78 info
->qdev
.bus_info
= &scsi_bus_info
;
79 info
->qdev
.init
= scsi_qdev_init
;
80 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
81 info
->qdev
.exit
= scsi_qdev_exit
;
82 qdev_register(&info
->qdev
);
85 /* handle legacy '-drive if=scsi,...' cmd line args */
86 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
, int unit
)
91 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
92 dev
= qdev_create(&bus
->qbus
, driver
);
93 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
94 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
98 if (qdev_init(dev
) < 0)
100 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
103 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
108 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
109 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
113 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
)) {
121 void scsi_dev_clear_sense(SCSIDevice
*dev
)
123 memset(&dev
->sense
, 0, sizeof(dev
->sense
));
126 void scsi_dev_set_sense(SCSIDevice
*dev
, uint8_t key
)
128 dev
->sense
.key
= key
;
131 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
135 req
= qemu_mallocz(size
);
136 req
->bus
= scsi_bus_from_device(d
);
141 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
145 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
149 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
150 if (req
->tag
== tag
) {
157 void scsi_req_free(SCSIRequest
*req
)
159 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
163 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
165 switch (cmd
[0] >> 5) {
167 req
->cmd
.xfer
= cmd
[4];
169 /* length 0 means 256 blocks */
170 if (req
->cmd
.xfer
== 0)
175 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
179 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
183 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
191 case TEST_UNIT_READY
:
195 case WRITE_FILEMARKS
:
198 case ALLOW_MEDIUM_REMOVAL
:
201 case SYNCHRONIZE_CACHE
:
202 case LOCK_UNLOCK_CACHE
:
219 case READ_BLOCK_LIMITS
:
225 case SEND_VOLUME_TAG
:
235 case WRITE_VERIFY_12
:
237 case WRITE_VERIFY_16
:
238 req
->cmd
.xfer
*= req
->dev
->blocksize
;
243 case RECOVER_BUFFERED_DATA
:
246 req
->cmd
.xfer
*= req
->dev
->blocksize
;
249 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
251 case MAINTENANCE_OUT
:
253 if (req
->dev
->type
== TYPE_ROM
) {
254 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
255 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
262 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
265 /* stream commands */
268 case RECOVER_BUFFERED_DATA
:
271 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
272 if (cmd
[1] & 0x01) /* fixed */
273 req
->cmd
.xfer
*= req
->dev
->blocksize
;
280 /* generic commands */
282 return scsi_req_length(req
, cmd
);
287 static void scsi_req_xfer_mode(SCSIRequest
*req
)
289 switch (req
->cmd
.buf
[0]) {
294 case WRITE_VERIFY_12
:
296 case WRITE_VERIFY_16
:
300 case CHANGE_DEFINITION
:
304 case SEND_DIAGNOSTIC
:
307 case REASSIGN_BLOCKS
:
316 case SEARCH_EQUAL_12
:
320 case SEND_VOLUME_TAG
:
322 case PERSISTENT_RESERVE_OUT
:
323 case MAINTENANCE_OUT
:
324 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
328 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
330 req
->cmd
.mode
= SCSI_XFER_NONE
;
336 static uint64_t scsi_req_lba(SCSIRequest
*req
)
338 uint8_t *buf
= req
->cmd
.buf
;
341 switch (buf
[0] >> 5) {
343 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
344 (((uint64_t) buf
[1] & 0x1f) << 16);
348 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
349 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
352 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
353 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
354 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
355 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
358 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
359 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
368 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
372 if (req
->dev
->type
== TYPE_TAPE
) {
373 rc
= scsi_req_stream_length(req
, buf
);
375 rc
= scsi_req_length(req
, buf
);
380 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
381 scsi_req_xfer_mode(req
);
382 req
->cmd
.lba
= scsi_req_lba(req
);
386 static const char *scsi_command_name(uint8_t cmd
)
388 static const char *names
[] = {
389 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
390 [ REZERO_UNIT
] = "REZERO_UNIT",
391 /* REWIND and REZERO_UNIT use the same operation code */
392 [ REQUEST_SENSE
] = "REQUEST_SENSE",
393 [ FORMAT_UNIT
] = "FORMAT_UNIT",
394 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
395 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
396 [ READ_6
] = "READ_6",
397 [ WRITE_6
] = "WRITE_6",
398 [ SEEK_6
] = "SEEK_6",
399 [ READ_REVERSE
] = "READ_REVERSE",
400 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
402 [ INQUIRY
] = "INQUIRY",
403 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
404 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
405 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
406 [ MODE_SELECT
] = "MODE_SELECT",
407 [ RESERVE
] = "RESERVE",
408 [ RELEASE
] = "RELEASE",
411 [ MODE_SENSE
] = "MODE_SENSE",
412 [ START_STOP
] = "START_STOP",
413 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
414 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
415 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
417 [ SET_WINDOW
] = "SET_WINDOW",
418 [ READ_CAPACITY
] = "READ_CAPACITY",
419 [ READ_10
] = "READ_10",
420 [ WRITE_10
] = "WRITE_10",
421 [ SEEK_10
] = "SEEK_10",
422 [ WRITE_VERIFY
] = "WRITE_VERIFY",
423 [ VERIFY
] = "VERIFY",
424 [ SEARCH_HIGH
] = "SEARCH_HIGH",
425 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
426 [ SEARCH_LOW
] = "SEARCH_LOW",
427 [ SET_LIMITS
] = "SET_LIMITS",
428 [ PRE_FETCH
] = "PRE_FETCH",
429 /* READ_POSITION and PRE_FETCH use the same operation code */
430 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
431 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
432 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
433 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
434 [ COMPARE
] = "COMPARE",
435 [ COPY_VERIFY
] = "COPY_VERIFY",
436 [ WRITE_BUFFER
] = "WRITE_BUFFER",
437 [ READ_BUFFER
] = "READ_BUFFER",
438 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
439 [ READ_LONG
] = "READ_LONG",
440 [ WRITE_LONG
] = "WRITE_LONG",
441 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
442 [ WRITE_SAME
] = "WRITE_SAME",
443 [ READ_TOC
] = "READ_TOC",
444 [ LOG_SELECT
] = "LOG_SELECT",
445 [ LOG_SENSE
] = "LOG_SENSE",
446 [ MODE_SELECT_10
] = "MODE_SELECT_10",
447 [ RESERVE_10
] = "RESERVE_10",
448 [ RELEASE_10
] = "RELEASE_10",
449 [ MODE_SENSE_10
] = "MODE_SENSE_10",
450 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
451 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
452 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
453 [ READ_12
] = "READ_12",
454 [ WRITE_12
] = "WRITE_12",
455 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
456 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
457 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
458 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
459 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
460 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
461 [ WRITE_LONG_2
] = "WRITE_LONG_2",
463 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
464 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
465 [ READ_16
] = "READ_16",
466 [ WRITE_16
] = "WRITE_16",
467 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
468 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
469 [ REPORT_LUNS
] = "REPORT_LUNS",
470 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
471 [ SET_CD_SPEED
] = "SET_CD_SPEED",
475 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
480 void scsi_req_print(SCSIRequest
*req
)
485 fprintf(fp
, "[%s id=%d] %s",
486 req
->dev
->qdev
.parent_bus
->name
,
488 scsi_command_name(req
->cmd
.buf
[0]));
489 for (i
= 1; i
< req
->cmd
.len
; i
++) {
490 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
492 switch (req
->cmd
.mode
) {
494 fprintf(fp
, " - none\n");
496 case SCSI_XFER_FROM_DEV
:
497 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
499 case SCSI_XFER_TO_DEV
:
500 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
503 fprintf(fp
, " - Oops\n");
508 void scsi_req_complete(SCSIRequest
*req
)
510 assert(req
->status
!= -1);
511 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,