2 #include "qemu-error.h"
8 static char *scsibus_get_fw_dev_path(DeviceState
*dev
);
10 static struct BusInfo scsi_bus_info
= {
12 .size
= sizeof(SCSIBus
),
13 .get_fw_dev_path
= scsibus_get_fw_dev_path
,
14 .props
= (Property
[]) {
15 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
16 DEFINE_PROP_END_OF_LIST(),
19 static int next_scsi_bus
;
21 /* Create a scsi bus, and attach devices to it. */
22 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
23 scsi_completionfn complete
)
25 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
26 bus
->busnr
= next_scsi_bus
++;
29 bus
->complete
= complete
;
30 bus
->qbus
.allow_hotplug
= 1;
33 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
35 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
36 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
37 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
41 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
42 if (bus
->devs
[dev
->id
] == NULL
)
46 if (dev
->id
>= bus
->ndev
) {
47 error_report("bad scsi device id: %d", dev
->id
);
51 if (bus
->devs
[dev
->id
]) {
52 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
54 bus
->devs
[dev
->id
] = dev
;
57 QTAILQ_INIT(&dev
->requests
);
58 rc
= dev
->info
->init(dev
);
60 bus
->devs
[dev
->id
] = NULL
;
67 static int scsi_qdev_exit(DeviceState
*qdev
)
69 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
70 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
72 assert(bus
->devs
[dev
->id
] != NULL
);
73 if (bus
->devs
[dev
->id
]->info
->destroy
) {
74 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
76 bus
->devs
[dev
->id
] = NULL
;
80 void scsi_qdev_register(SCSIDeviceInfo
*info
)
82 info
->qdev
.bus_info
= &scsi_bus_info
;
83 info
->qdev
.init
= scsi_qdev_init
;
84 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
85 info
->qdev
.exit
= scsi_qdev_exit
;
86 qdev_register(&info
->qdev
);
89 /* handle legacy '-drive if=scsi,...' cmd line args */
90 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, BlockDriverState
*bdrv
, int unit
)
95 driver
= bdrv_is_sg(bdrv
) ? "scsi-generic" : "scsi-disk";
96 dev
= qdev_create(&bus
->qbus
, driver
);
97 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
98 if (qdev_prop_set_drive(dev
, "drive", bdrv
) < 0) {
102 if (qdev_init(dev
) < 0)
104 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
107 int scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
114 for (unit
= 0; unit
< bus
->ndev
; unit
++) {
115 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
119 qemu_opts_loc_restore(dinfo
->opts
);
120 if (!scsi_bus_legacy_add_drive(bus
, dinfo
->bdrv
, unit
)) {
129 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
133 req
= qemu_mallocz(size
);
134 req
->bus
= scsi_bus_from_device(d
);
139 req
->enqueued
= true;
140 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
144 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
148 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
149 if (req
->tag
== tag
) {
156 static void scsi_req_dequeue(SCSIRequest
*req
)
159 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
160 req
->enqueued
= false;
164 void scsi_req_free(SCSIRequest
*req
)
166 scsi_req_dequeue(req
);
170 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
172 switch (cmd
[0] >> 5) {
174 req
->cmd
.xfer
= cmd
[4];
176 /* length 0 means 256 blocks */
177 if (req
->cmd
.xfer
== 0)
182 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
186 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
190 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
198 case TEST_UNIT_READY
:
202 case WRITE_FILEMARKS
:
207 case ALLOW_MEDIUM_REMOVAL
:
210 case SYNCHRONIZE_CACHE
:
211 case LOCK_UNLOCK_CACHE
:
228 case READ_BLOCK_LIMITS
:
234 case SEND_VOLUME_TAG
:
244 case WRITE_VERIFY_12
:
246 case WRITE_VERIFY_16
:
247 req
->cmd
.xfer
*= req
->dev
->blocksize
;
252 case RECOVER_BUFFERED_DATA
:
255 req
->cmd
.xfer
*= req
->dev
->blocksize
;
258 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
260 case MAINTENANCE_OUT
:
262 if (req
->dev
->type
== TYPE_ROM
) {
263 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
264 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8);
271 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
274 /* stream commands */
277 case RECOVER_BUFFERED_DATA
:
280 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
281 if (cmd
[1] & 0x01) /* fixed */
282 req
->cmd
.xfer
*= req
->dev
->blocksize
;
289 /* generic commands */
291 return scsi_req_length(req
, cmd
);
296 static void scsi_req_xfer_mode(SCSIRequest
*req
)
298 switch (req
->cmd
.buf
[0]) {
303 case WRITE_VERIFY_12
:
305 case WRITE_VERIFY_16
:
309 case CHANGE_DEFINITION
:
313 case SEND_DIAGNOSTIC
:
316 case REASSIGN_BLOCKS
:
324 case SEARCH_EQUAL_12
:
328 case SEND_VOLUME_TAG
:
330 case PERSISTENT_RESERVE_OUT
:
331 case MAINTENANCE_OUT
:
332 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
336 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
338 req
->cmd
.mode
= SCSI_XFER_NONE
;
344 static uint64_t scsi_req_lba(SCSIRequest
*req
)
346 uint8_t *buf
= req
->cmd
.buf
;
349 switch (buf
[0] >> 5) {
351 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
352 (((uint64_t) buf
[1] & 0x1f) << 16);
356 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
357 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
360 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
361 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
362 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
363 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
366 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
367 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
376 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
380 if (req
->dev
->type
== TYPE_TAPE
) {
381 rc
= scsi_req_stream_length(req
, buf
);
383 rc
= scsi_req_length(req
, buf
);
388 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
389 scsi_req_xfer_mode(req
);
390 req
->cmd
.lba
= scsi_req_lba(req
);
394 static const char *scsi_command_name(uint8_t cmd
)
396 static const char *names
[] = {
397 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
398 [ REZERO_UNIT
] = "REZERO_UNIT",
399 /* REWIND and REZERO_UNIT use the same operation code */
400 [ REQUEST_SENSE
] = "REQUEST_SENSE",
401 [ FORMAT_UNIT
] = "FORMAT_UNIT",
402 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
403 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
404 [ READ_6
] = "READ_6",
405 [ WRITE_6
] = "WRITE_6",
406 [ SEEK_6
] = "SEEK_6",
407 [ READ_REVERSE
] = "READ_REVERSE",
408 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
410 [ INQUIRY
] = "INQUIRY",
411 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
412 [ MAINTENANCE_IN
] = "MAINTENANCE_IN",
413 [ MAINTENANCE_OUT
] = "MAINTENANCE_OUT",
414 [ MODE_SELECT
] = "MODE_SELECT",
415 [ RESERVE
] = "RESERVE",
416 [ RELEASE
] = "RELEASE",
419 [ MODE_SENSE
] = "MODE_SENSE",
420 [ START_STOP
] = "START_STOP",
421 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
422 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
423 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
425 [ SET_WINDOW
] = "SET_WINDOW",
426 [ READ_CAPACITY
] = "READ_CAPACITY",
427 [ READ_10
] = "READ_10",
428 [ WRITE_10
] = "WRITE_10",
429 [ SEEK_10
] = "SEEK_10",
430 [ WRITE_VERIFY
] = "WRITE_VERIFY",
431 [ VERIFY
] = "VERIFY",
432 [ SEARCH_HIGH
] = "SEARCH_HIGH",
433 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
434 [ SEARCH_LOW
] = "SEARCH_LOW",
435 [ SET_LIMITS
] = "SET_LIMITS",
436 [ PRE_FETCH
] = "PRE_FETCH",
437 /* READ_POSITION and PRE_FETCH use the same operation code */
438 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
439 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
440 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
441 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
442 [ COMPARE
] = "COMPARE",
443 [ COPY_VERIFY
] = "COPY_VERIFY",
444 [ WRITE_BUFFER
] = "WRITE_BUFFER",
445 [ READ_BUFFER
] = "READ_BUFFER",
446 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
447 [ READ_LONG
] = "READ_LONG",
448 [ WRITE_LONG
] = "WRITE_LONG",
449 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
450 [ WRITE_SAME
] = "WRITE_SAME",
451 [ READ_TOC
] = "READ_TOC",
452 [ LOG_SELECT
] = "LOG_SELECT",
453 [ LOG_SENSE
] = "LOG_SENSE",
454 [ MODE_SELECT_10
] = "MODE_SELECT_10",
455 [ RESERVE_10
] = "RESERVE_10",
456 [ RELEASE_10
] = "RELEASE_10",
457 [ MODE_SENSE_10
] = "MODE_SENSE_10",
458 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
459 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
460 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
461 [ READ_12
] = "READ_12",
462 [ WRITE_12
] = "WRITE_12",
463 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
464 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
465 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
466 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
467 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
468 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
469 [ WRITE_LONG_2
] = "WRITE_LONG_2",
471 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
472 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
473 [ READ_16
] = "READ_16",
474 [ WRITE_16
] = "WRITE_16",
475 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
476 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
477 [ REPORT_LUNS
] = "REPORT_LUNS",
478 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
479 [ SET_CD_SPEED
] = "SET_CD_SPEED",
483 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
488 void scsi_req_print(SCSIRequest
*req
)
493 fprintf(fp
, "[%s id=%d] %s",
494 req
->dev
->qdev
.parent_bus
->name
,
496 scsi_command_name(req
->cmd
.buf
[0]));
497 for (i
= 1; i
< req
->cmd
.len
; i
++) {
498 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
500 switch (req
->cmd
.mode
) {
502 fprintf(fp
, " - none\n");
504 case SCSI_XFER_FROM_DEV
:
505 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
507 case SCSI_XFER_TO_DEV
:
508 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
511 fprintf(fp
, " - Oops\n");
516 void scsi_req_complete(SCSIRequest
*req
)
518 assert(req
->status
!= -1);
519 scsi_req_dequeue(req
);
520 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,
525 static char *scsibus_get_fw_dev_path(DeviceState
*dev
)
527 SCSIDevice
*d
= (SCSIDevice
*)dev
;
528 SCSIBus
*bus
= scsi_bus_from_device(d
);
532 for (i
= 0; i
< bus
->ndev
; i
++) {
533 if (bus
->devs
[i
] == d
) {
538 assert(i
!= bus
->ndev
);
540 snprintf(path
, sizeof(path
), "%s@%x", qdev_fw_name(dev
), i
);