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 /* FIXME callers should check for failure, but don't */
87 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, DriveInfo *dinfo, int unit)
92 driver = bdrv_is_sg(dinfo->bdrv) ? "scsi-generic" : "scsi-disk";
93 dev = qdev_create(&bus->qbus, driver);
94 qdev_prop_set_uint32(dev, "scsi-id", unit);
95 qdev_prop_set_drive(dev, "drive", dinfo);
96 if (qdev_init(dev) < 0)
98 return DO_UPCAST(SCSIDevice, qdev, dev);
101 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
106 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
107 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
111 scsi_bus_legacy_add_drive(bus, dinfo, unit);
115 void scsi_dev_clear_sense(SCSIDevice *dev)
117 memset(&dev->sense, 0, sizeof(dev->sense));
120 void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
122 dev->sense.key = key;
125 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
129 req = qemu_mallocz(size);
130 req->bus = scsi_bus_from_device(d);
135 QTAILQ_INSERT_TAIL(&d->requests, req, next);
139 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
143 QTAILQ_FOREACH(req, &d->requests, next) {
144 if (req->tag == tag) {
151 void scsi_req_free(SCSIRequest *req)
153 QTAILQ_REMOVE(&req->dev->requests, req, next);
157 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
159 switch (cmd[0] >> 5) {
161 req->cmd.xfer = cmd[4];
163 /* length 0 means 256 blocks */
164 if (req->cmd.xfer == 0)
169 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
173 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
177 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
185 case TEST_UNIT_READY:
189 case WRITE_FILEMARKS:
192 case ALLOW_MEDIUM_REMOVAL:
195 case SYNCHRONIZE_CACHE:
196 case LOCK_UNLOCK_CACHE:
213 case READ_BLOCK_LIMITS:
219 case SEND_VOLUME_TAG:
229 case WRITE_VERIFY_12:
231 case WRITE_VERIFY_16:
232 req->cmd.xfer *= req->dev->blocksize;
237 case RECOVER_BUFFERED_DATA:
240 req->cmd.xfer *= req->dev->blocksize;
243 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
249 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
252 /* stream commands */
255 case RECOVER_BUFFERED_DATA:
258 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
259 if (cmd[1] & 0x01) /* fixed */
260 req->cmd.xfer *= req->dev->blocksize;
267 /* generic commands */
269 return scsi_req_length(req, cmd);
274 static void scsi_req_xfer_mode(SCSIRequest *req)
276 switch (req->cmd.buf[0]) {
281 case WRITE_VERIFY_12:
283 case WRITE_VERIFY_16:
287 case CHANGE_DEFINITION:
291 case SEND_DIAGNOSTIC:
294 case REASSIGN_BLOCKS:
303 case SEARCH_EQUAL_12:
307 case SEND_VOLUME_TAG:
309 req->cmd.mode = SCSI_XFER_TO_DEV;
313 req->cmd.mode = SCSI_XFER_FROM_DEV;
315 req->cmd.mode = SCSI_XFER_NONE;
321 static uint64_t scsi_req_lba(SCSIRequest *req)
323 uint8_t *buf = req->cmd.buf;
326 switch (buf[0] >> 5) {
328 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
329 (((uint64_t) buf[1] & 0x1f) << 16);
333 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
334 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
337 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
338 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
339 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
340 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
343 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
344 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
353 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
357 if (req->dev->type == TYPE_TAPE) {
358 rc = scsi_req_stream_length(req, buf);
360 rc = scsi_req_length(req, buf);
365 memcpy(req->cmd.buf, buf, req->cmd.len);
366 scsi_req_xfer_mode(req);
367 req->cmd.lba = scsi_req_lba(req);
371 static const char *scsi_command_name(uint8_t cmd)
373 static const char *names[] = {
374 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
375 [ REZERO_UNIT ] = "REZERO_UNIT",
376 /* REWIND and REZERO_UNIT use the same operation code */
377 [ REQUEST_SENSE ] = "REQUEST_SENSE",
378 [ FORMAT_UNIT ] = "FORMAT_UNIT",
379 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
380 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
381 [ READ_6 ] = "READ_6",
382 [ WRITE_6 ] = "WRITE_6",
383 [ SEEK_6 ] = "SEEK_6",
384 [ READ_REVERSE ] = "READ_REVERSE",
385 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
387 [ INQUIRY ] = "INQUIRY",
388 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
389 [ MODE_SELECT ] = "MODE_SELECT",
390 [ RESERVE ] = "RESERVE",
391 [ RELEASE ] = "RELEASE",
394 [ MODE_SENSE ] = "MODE_SENSE",
395 [ START_STOP ] = "START_STOP",
396 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
397 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
398 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
400 [ SET_WINDOW ] = "SET_WINDOW",
401 [ READ_CAPACITY ] = "READ_CAPACITY",
402 [ READ_10 ] = "READ_10",
403 [ WRITE_10 ] = "WRITE_10",
404 [ SEEK_10 ] = "SEEK_10",
405 [ WRITE_VERIFY ] = "WRITE_VERIFY",
406 [ VERIFY ] = "VERIFY",
407 [ SEARCH_HIGH ] = "SEARCH_HIGH",
408 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
409 [ SEARCH_LOW ] = "SEARCH_LOW",
410 [ SET_LIMITS ] = "SET_LIMITS",
411 [ PRE_FETCH ] = "PRE_FETCH",
412 /* READ_POSITION and PRE_FETCH use the same operation code */
413 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
414 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
415 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
416 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
417 [ COMPARE ] = "COMPARE",
418 [ COPY_VERIFY ] = "COPY_VERIFY",
419 [ WRITE_BUFFER ] = "WRITE_BUFFER",
420 [ READ_BUFFER ] = "READ_BUFFER",
421 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
422 [ READ_LONG ] = "READ_LONG",
423 [ WRITE_LONG ] = "WRITE_LONG",
424 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
425 [ WRITE_SAME ] = "WRITE_SAME",
426 [ READ_TOC ] = "READ_TOC",
427 [ LOG_SELECT ] = "LOG_SELECT",
428 [ LOG_SENSE ] = "LOG_SENSE",
429 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
430 [ RESERVE_10 ] = "RESERVE_10",
431 [ RELEASE_10 ] = "RELEASE_10",
432 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
433 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
434 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
435 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
436 [ READ_12 ] = "READ_12",
437 [ WRITE_12 ] = "WRITE_12",
438 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
439 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
440 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
441 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
442 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
443 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
444 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
446 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
447 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
448 [ READ_16 ] = "READ_16",
449 [ WRITE_16 ] = "WRITE_16",
450 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
451 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
452 [ REPORT_LUNS ] = "REPORT_LUNS",
453 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
454 [ SET_CD_SPEED ] = "SET_CD_SPEED",
458 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
463 void scsi_req_print(SCSIRequest *req)
468 fprintf(fp, "[%s id=%d] %s",
469 req->dev->qdev.parent_bus->name,
471 scsi_command_name(req->cmd.buf[0]));
472 for (i = 1; i < req->cmd.len; i++) {
473 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
475 switch (req->cmd.mode) {
477 fprintf(fp, " - none\n");
479 case SCSI_XFER_FROM_DEV:
480 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
482 case SCSI_XFER_TO_DEV:
483 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
486 fprintf(fp, " - Oops\n");
491 void scsi_req_complete(SCSIRequest *req)
493 assert(req->status != -1);
494 req->bus->complete(req->bus, SCSI_REASON_DONE,