2 #include "qemu-error.h"
8 static struct BusInfo scsi_bus_info
= {
10 .size
= sizeof(SCSIBus
),
11 .props
= (Property
[]) {
12 DEFINE_PROP_UINT32("scsi-id", SCSIDevice
, id
, -1),
13 DEFINE_PROP_END_OF_LIST(),
16 static int next_scsi_bus
;
18 /* Create a scsi bus, and attach devices to it. */
19 void scsi_bus_new(SCSIBus
*bus
, DeviceState
*host
, int tcq
, int ndev
,
20 scsi_completionfn complete
)
22 qbus_create_inplace(&bus
->qbus
, &scsi_bus_info
, host
, NULL
);
23 bus
->busnr
= next_scsi_bus
++;
26 bus
->complete
= complete
;
27 bus
->qbus
.allow_hotplug
= 1;
30 static int scsi_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
32 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
33 SCSIDeviceInfo
*info
= DO_UPCAST(SCSIDeviceInfo
, qdev
, base
);
34 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
38 for (dev
->id
= 0; dev
->id
< bus
->ndev
; dev
->id
++) {
39 if (bus
->devs
[dev
->id
] == NULL
)
43 if (dev
->id
>= bus
->ndev
) {
44 error_report("bad scsi device id: %d", dev
->id
);
48 if (bus
->devs
[dev
->id
]) {
49 qdev_free(&bus
->devs
[dev
->id
]->qdev
);
51 bus
->devs
[dev
->id
] = dev
;
54 QTAILQ_INIT(&dev
->requests
);
55 rc
= dev
->info
->init(dev
);
57 bus
->devs
[dev
->id
] = NULL
;
64 static int scsi_qdev_exit(DeviceState
*qdev
)
66 SCSIDevice
*dev
= DO_UPCAST(SCSIDevice
, qdev
, qdev
);
67 SCSIBus
*bus
= DO_UPCAST(SCSIBus
, qbus
, dev
->qdev
.parent_bus
);
69 assert(bus
->devs
[dev
->id
] != NULL
);
70 if (bus
->devs
[dev
->id
]->info
->destroy
) {
71 bus
->devs
[dev
->id
]->info
->destroy(bus
->devs
[dev
->id
]);
73 bus
->devs
[dev
->id
] = NULL
;
77 void scsi_qdev_register(SCSIDeviceInfo
*info
)
79 info
->qdev
.bus_info
= &scsi_bus_info
;
80 info
->qdev
.init
= scsi_qdev_init
;
81 info
->qdev
.unplug
= qdev_simple_unplug_cb
;
82 info
->qdev
.exit
= scsi_qdev_exit
;
83 qdev_register(&info
->qdev
);
86 /* handle legacy '-drive if=scsi,...' cmd line args */
87 /* FIXME callers should check for failure, but don't */
88 SCSIDevice
*scsi_bus_legacy_add_drive(SCSIBus
*bus
, DriveInfo
*dinfo
, int unit
)
93 driver
= bdrv_is_sg(dinfo
->bdrv
) ? "scsi-generic" : "scsi-disk";
94 dev
= qdev_create(&bus
->qbus
, driver
);
95 qdev_prop_set_uint32(dev
, "scsi-id", unit
);
96 qdev_prop_set_drive(dev
, "drive", dinfo
);
97 if (qdev_init(dev
) < 0)
99 return DO_UPCAST(SCSIDevice
, qdev
, dev
);
102 void scsi_bus_legacy_handle_cmdline(SCSIBus
*bus
)
107 for (unit
= 0; unit
< MAX_SCSI_DEVS
; unit
++) {
108 dinfo
= drive_get(IF_SCSI
, bus
->busnr
, unit
);
112 scsi_bus_legacy_add_drive(bus
, dinfo
, unit
);
116 void scsi_dev_clear_sense(SCSIDevice
*dev
)
118 memset(&dev
->sense
, 0, sizeof(dev
->sense
));
121 void scsi_dev_set_sense(SCSIDevice
*dev
, uint8_t key
)
123 dev
->sense
.key
= key
;
126 SCSIRequest
*scsi_req_alloc(size_t size
, SCSIDevice
*d
, uint32_t tag
, uint32_t lun
)
130 req
= qemu_mallocz(size
);
131 req
->bus
= scsi_bus_from_device(d
);
136 QTAILQ_INSERT_TAIL(&d
->requests
, req
, next
);
140 SCSIRequest
*scsi_req_find(SCSIDevice
*d
, uint32_t tag
)
144 QTAILQ_FOREACH(req
, &d
->requests
, next
) {
145 if (req
->tag
== tag
) {
152 void scsi_req_free(SCSIRequest
*req
)
154 QTAILQ_REMOVE(&req
->dev
->requests
, req
, next
);
158 static int scsi_req_length(SCSIRequest
*req
, uint8_t *cmd
)
160 switch (cmd
[0] >> 5) {
162 req
->cmd
.xfer
= cmd
[4];
164 /* length 0 means 256 blocks */
165 if (req
->cmd
.xfer
== 0)
170 req
->cmd
.xfer
= cmd
[8] | (cmd
[7] << 8);
174 req
->cmd
.xfer
= cmd
[13] | (cmd
[12] << 8) | (cmd
[11] << 16) | (cmd
[10] << 24);
178 req
->cmd
.xfer
= cmd
[9] | (cmd
[8] << 8) | (cmd
[7] << 16) | (cmd
[6] << 24);
186 case TEST_UNIT_READY
:
190 case WRITE_FILEMARKS
:
193 case ALLOW_MEDIUM_REMOVAL
:
196 case SYNCHRONIZE_CACHE
:
197 case LOCK_UNLOCK_CACHE
:
214 case READ_BLOCK_LIMITS
:
220 case SEND_VOLUME_TAG
:
230 case WRITE_VERIFY_12
:
232 case WRITE_VERIFY_16
:
233 req
->cmd
.xfer
*= req
->dev
->blocksize
;
238 case RECOVER_BUFFERED_DATA
:
241 req
->cmd
.xfer
*= req
->dev
->blocksize
;
244 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8);
250 static int scsi_req_stream_length(SCSIRequest
*req
, uint8_t *cmd
)
253 /* stream commands */
256 case RECOVER_BUFFERED_DATA
:
259 req
->cmd
.xfer
= cmd
[4] | (cmd
[3] << 8) | (cmd
[2] << 16);
260 if (cmd
[1] & 0x01) /* fixed */
261 req
->cmd
.xfer
*= req
->dev
->blocksize
;
268 /* generic commands */
270 return scsi_req_length(req
, cmd
);
275 static void scsi_req_xfer_mode(SCSIRequest
*req
)
277 switch (req
->cmd
.buf
[0]) {
282 case WRITE_VERIFY_12
:
284 case WRITE_VERIFY_16
:
288 case CHANGE_DEFINITION
:
292 case SEND_DIAGNOSTIC
:
295 case REASSIGN_BLOCKS
:
304 case SEARCH_EQUAL_12
:
308 case SEND_VOLUME_TAG
:
310 req
->cmd
.mode
= SCSI_XFER_TO_DEV
;
314 req
->cmd
.mode
= SCSI_XFER_FROM_DEV
;
316 req
->cmd
.mode
= SCSI_XFER_NONE
;
322 static uint64_t scsi_req_lba(SCSIRequest
*req
)
324 uint8_t *buf
= req
->cmd
.buf
;
327 switch (buf
[0] >> 5) {
329 lba
= (uint64_t) buf
[3] | ((uint64_t) buf
[2] << 8) |
330 (((uint64_t) buf
[1] & 0x1f) << 16);
334 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
335 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
338 lba
= (uint64_t) buf
[9] | ((uint64_t) buf
[8] << 8) |
339 ((uint64_t) buf
[7] << 16) | ((uint64_t) buf
[6] << 24) |
340 ((uint64_t) buf
[5] << 32) | ((uint64_t) buf
[4] << 40) |
341 ((uint64_t) buf
[3] << 48) | ((uint64_t) buf
[2] << 56);
344 lba
= (uint64_t) buf
[5] | ((uint64_t) buf
[4] << 8) |
345 ((uint64_t) buf
[3] << 16) | ((uint64_t) buf
[2] << 24);
354 int scsi_req_parse(SCSIRequest
*req
, uint8_t *buf
)
358 if (req
->dev
->type
== TYPE_TAPE
) {
359 rc
= scsi_req_stream_length(req
, buf
);
361 rc
= scsi_req_length(req
, buf
);
366 memcpy(req
->cmd
.buf
, buf
, req
->cmd
.len
);
367 scsi_req_xfer_mode(req
);
368 req
->cmd
.lba
= scsi_req_lba(req
);
372 static const char *scsi_command_name(uint8_t cmd
)
374 static const char *names
[] = {
375 [ TEST_UNIT_READY
] = "TEST_UNIT_READY",
376 [ REZERO_UNIT
] = "REZERO_UNIT",
377 /* REWIND and REZERO_UNIT use the same operation code */
378 [ REQUEST_SENSE
] = "REQUEST_SENSE",
379 [ FORMAT_UNIT
] = "FORMAT_UNIT",
380 [ READ_BLOCK_LIMITS
] = "READ_BLOCK_LIMITS",
381 [ REASSIGN_BLOCKS
] = "REASSIGN_BLOCKS",
382 [ READ_6
] = "READ_6",
383 [ WRITE_6
] = "WRITE_6",
384 [ SEEK_6
] = "SEEK_6",
385 [ READ_REVERSE
] = "READ_REVERSE",
386 [ WRITE_FILEMARKS
] = "WRITE_FILEMARKS",
388 [ INQUIRY
] = "INQUIRY",
389 [ RECOVER_BUFFERED_DATA
] = "RECOVER_BUFFERED_DATA",
390 [ MODE_SELECT
] = "MODE_SELECT",
391 [ RESERVE
] = "RESERVE",
392 [ RELEASE
] = "RELEASE",
395 [ MODE_SENSE
] = "MODE_SENSE",
396 [ START_STOP
] = "START_STOP",
397 [ RECEIVE_DIAGNOSTIC
] = "RECEIVE_DIAGNOSTIC",
398 [ SEND_DIAGNOSTIC
] = "SEND_DIAGNOSTIC",
399 [ ALLOW_MEDIUM_REMOVAL
] = "ALLOW_MEDIUM_REMOVAL",
401 [ SET_WINDOW
] = "SET_WINDOW",
402 [ READ_CAPACITY
] = "READ_CAPACITY",
403 [ READ_10
] = "READ_10",
404 [ WRITE_10
] = "WRITE_10",
405 [ SEEK_10
] = "SEEK_10",
406 [ WRITE_VERIFY
] = "WRITE_VERIFY",
407 [ VERIFY
] = "VERIFY",
408 [ SEARCH_HIGH
] = "SEARCH_HIGH",
409 [ SEARCH_EQUAL
] = "SEARCH_EQUAL",
410 [ SEARCH_LOW
] = "SEARCH_LOW",
411 [ SET_LIMITS
] = "SET_LIMITS",
412 [ PRE_FETCH
] = "PRE_FETCH",
413 /* READ_POSITION and PRE_FETCH use the same operation code */
414 [ SYNCHRONIZE_CACHE
] = "SYNCHRONIZE_CACHE",
415 [ LOCK_UNLOCK_CACHE
] = "LOCK_UNLOCK_CACHE",
416 [ READ_DEFECT_DATA
] = "READ_DEFECT_DATA",
417 [ MEDIUM_SCAN
] = "MEDIUM_SCAN",
418 [ COMPARE
] = "COMPARE",
419 [ COPY_VERIFY
] = "COPY_VERIFY",
420 [ WRITE_BUFFER
] = "WRITE_BUFFER",
421 [ READ_BUFFER
] = "READ_BUFFER",
422 [ UPDATE_BLOCK
] = "UPDATE_BLOCK",
423 [ READ_LONG
] = "READ_LONG",
424 [ WRITE_LONG
] = "WRITE_LONG",
425 [ CHANGE_DEFINITION
] = "CHANGE_DEFINITION",
426 [ WRITE_SAME
] = "WRITE_SAME",
427 [ READ_TOC
] = "READ_TOC",
428 [ LOG_SELECT
] = "LOG_SELECT",
429 [ LOG_SENSE
] = "LOG_SENSE",
430 [ MODE_SELECT_10
] = "MODE_SELECT_10",
431 [ RESERVE_10
] = "RESERVE_10",
432 [ RELEASE_10
] = "RELEASE_10",
433 [ MODE_SENSE_10
] = "MODE_SENSE_10",
434 [ PERSISTENT_RESERVE_IN
] = "PERSISTENT_RESERVE_IN",
435 [ PERSISTENT_RESERVE_OUT
] = "PERSISTENT_RESERVE_OUT",
436 [ MOVE_MEDIUM
] = "MOVE_MEDIUM",
437 [ READ_12
] = "READ_12",
438 [ WRITE_12
] = "WRITE_12",
439 [ WRITE_VERIFY_12
] = "WRITE_VERIFY_12",
440 [ SEARCH_HIGH_12
] = "SEARCH_HIGH_12",
441 [ SEARCH_EQUAL_12
] = "SEARCH_EQUAL_12",
442 [ SEARCH_LOW_12
] = "SEARCH_LOW_12",
443 [ READ_ELEMENT_STATUS
] = "READ_ELEMENT_STATUS",
444 [ SEND_VOLUME_TAG
] = "SEND_VOLUME_TAG",
445 [ WRITE_LONG_2
] = "WRITE_LONG_2",
447 [ REPORT_DENSITY_SUPPORT
] = "REPORT_DENSITY_SUPPORT",
448 [ GET_CONFIGURATION
] = "GET_CONFIGURATION",
449 [ READ_16
] = "READ_16",
450 [ WRITE_16
] = "WRITE_16",
451 [ WRITE_VERIFY_16
] = "WRITE_VERIFY_16",
452 [ SERVICE_ACTION_IN
] = "SERVICE_ACTION_IN",
453 [ REPORT_LUNS
] = "REPORT_LUNS",
454 [ LOAD_UNLOAD
] = "LOAD_UNLOAD",
455 [ SET_CD_SPEED
] = "SET_CD_SPEED",
459 if (cmd
>= ARRAY_SIZE(names
) || names
[cmd
] == NULL
)
464 void scsi_req_print(SCSIRequest
*req
)
469 fprintf(fp
, "[%s id=%d] %s",
470 req
->dev
->qdev
.parent_bus
->name
,
472 scsi_command_name(req
->cmd
.buf
[0]));
473 for (i
= 1; i
< req
->cmd
.len
; i
++) {
474 fprintf(fp
, " 0x%02x", req
->cmd
.buf
[i
]);
476 switch (req
->cmd
.mode
) {
478 fprintf(fp
, " - none\n");
480 case SCSI_XFER_FROM_DEV
:
481 fprintf(fp
, " - from-dev len=%zd\n", req
->cmd
.xfer
);
483 case SCSI_XFER_TO_DEV
:
484 fprintf(fp
, " - to-dev len=%zd\n", req
->cmd
.xfer
);
487 fprintf(fp
, " - Oops\n");
492 void scsi_req_complete(SCSIRequest
*req
)
494 assert(req
->status
!= -1);
495 req
->bus
->complete(req
->bus
, SCSI_REASON_DONE
,