fix vnc regression
[qemu/ar7.git] / hw / scsi-bus.c
blobceeb4ecb9182d8bd0cdad5ce75203f303da67268
1 #include "hw.h"
2 #include "qemu-error.h"
3 #include "scsi.h"
4 #include "scsi-defs.h"
5 #include "qdev.h"
6 #include "blockdev.h"
8 static char *scsibus_get_fw_dev_path(DeviceState *dev);
10 static struct BusInfo scsi_bus_info = {
11 .name = "SCSI",
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++;
27 bus->tcq = tcq;
28 bus->ndev = ndev;
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);
38 int rc = -1;
40 if (dev->id == -1) {
41 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
42 if (bus->devs[dev->id] == NULL)
43 break;
46 if (dev->id >= bus->ndev) {
47 error_report("bad scsi device id: %d", dev->id);
48 goto err;
51 if (bus->devs[dev->id]) {
52 qdev_free(&bus->devs[dev->id]->qdev);
54 bus->devs[dev->id] = dev;
56 dev->info = info;
57 QTAILQ_INIT(&dev->requests);
58 rc = dev->info->init(dev);
59 if (rc != 0) {
60 bus->devs[dev->id] = NULL;
63 err:
64 return rc;
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;
77 return 0;
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,
91 int unit, bool removable)
93 const char *driver;
94 DeviceState *dev;
96 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
97 dev = qdev_create(&bus->qbus, driver);
98 qdev_prop_set_uint32(dev, "scsi-id", unit);
99 if (qdev_prop_exists(dev, "removable")) {
100 qdev_prop_set_bit(dev, "removable", removable);
102 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
103 qdev_free(dev);
104 return NULL;
106 if (qdev_init(dev) < 0)
107 return NULL;
108 return DO_UPCAST(SCSIDevice, qdev, dev);
111 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
113 Location loc;
114 DriveInfo *dinfo;
115 int res = 0, unit;
117 loc_push_none(&loc);
118 for (unit = 0; unit < bus->ndev; unit++) {
119 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
120 if (dinfo == NULL) {
121 continue;
123 qemu_opts_loc_restore(dinfo->opts);
124 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
125 res = -1;
126 break;
129 loc_pop(&loc);
130 return res;
133 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
135 SCSIRequest *req;
137 req = qemu_mallocz(size);
138 req->bus = scsi_bus_from_device(d);
139 req->dev = d;
140 req->tag = tag;
141 req->lun = lun;
142 req->status = -1;
143 req->enqueued = true;
144 QTAILQ_INSERT_TAIL(&d->requests, req, next);
145 return req;
148 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
150 SCSIRequest *req;
152 QTAILQ_FOREACH(req, &d->requests, next) {
153 if (req->tag == tag) {
154 return req;
157 return NULL;
160 static void scsi_req_dequeue(SCSIRequest *req)
162 if (req->enqueued) {
163 QTAILQ_REMOVE(&req->dev->requests, req, next);
164 req->enqueued = false;
168 void scsi_req_free(SCSIRequest *req)
170 scsi_req_dequeue(req);
171 qemu_free(req);
174 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
176 switch (cmd[0] >> 5) {
177 case 0:
178 req->cmd.xfer = cmd[4];
179 req->cmd.len = 6;
180 /* length 0 means 256 blocks */
181 if (req->cmd.xfer == 0)
182 req->cmd.xfer = 256;
183 break;
184 case 1:
185 case 2:
186 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
187 req->cmd.len = 10;
188 break;
189 case 4:
190 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
191 req->cmd.len = 16;
192 break;
193 case 5:
194 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
195 req->cmd.len = 12;
196 break;
197 default:
198 return -1;
201 switch(cmd[0]) {
202 case TEST_UNIT_READY:
203 case REZERO_UNIT:
204 case START_STOP:
205 case SEEK_6:
206 case WRITE_FILEMARKS:
207 case SPACE:
208 case RESERVE:
209 case RELEASE:
210 case ERASE:
211 case ALLOW_MEDIUM_REMOVAL:
212 case VERIFY:
213 case SEEK_10:
214 case SYNCHRONIZE_CACHE:
215 case LOCK_UNLOCK_CACHE:
216 case LOAD_UNLOAD:
217 case SET_CD_SPEED:
218 case SET_LIMITS:
219 case WRITE_LONG:
220 case MOVE_MEDIUM:
221 case UPDATE_BLOCK:
222 req->cmd.xfer = 0;
223 break;
224 case MODE_SENSE:
225 break;
226 case WRITE_SAME:
227 req->cmd.xfer = 1;
228 break;
229 case READ_CAPACITY:
230 req->cmd.xfer = 8;
231 break;
232 case READ_BLOCK_LIMITS:
233 req->cmd.xfer = 6;
234 break;
235 case READ_POSITION:
236 req->cmd.xfer = 20;
237 break;
238 case SEND_VOLUME_TAG:
239 req->cmd.xfer *= 40;
240 break;
241 case MEDIUM_SCAN:
242 req->cmd.xfer *= 8;
243 break;
244 case WRITE_10:
245 case WRITE_VERIFY:
246 case WRITE_6:
247 case WRITE_12:
248 case WRITE_VERIFY_12:
249 case WRITE_16:
250 case WRITE_VERIFY_16:
251 req->cmd.xfer *= req->dev->blocksize;
252 break;
253 case READ_10:
254 case READ_6:
255 case READ_REVERSE:
256 case RECOVER_BUFFERED_DATA:
257 case READ_12:
258 case READ_16:
259 req->cmd.xfer *= req->dev->blocksize;
260 break;
261 case INQUIRY:
262 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
263 break;
264 case MAINTENANCE_OUT:
265 case MAINTENANCE_IN:
266 if (req->dev->type == TYPE_ROM) {
267 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
268 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
270 break;
272 return 0;
275 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
277 switch(cmd[0]) {
278 /* stream commands */
279 case READ_6:
280 case READ_REVERSE:
281 case RECOVER_BUFFERED_DATA:
282 case WRITE_6:
283 req->cmd.len = 6;
284 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
285 if (cmd[1] & 0x01) /* fixed */
286 req->cmd.xfer *= req->dev->blocksize;
287 break;
288 case REWIND:
289 case START_STOP:
290 req->cmd.len = 6;
291 req->cmd.xfer = 0;
292 break;
293 /* generic commands */
294 default:
295 return scsi_req_length(req, cmd);
297 return 0;
300 static void scsi_req_xfer_mode(SCSIRequest *req)
302 switch (req->cmd.buf[0]) {
303 case WRITE_6:
304 case WRITE_10:
305 case WRITE_VERIFY:
306 case WRITE_12:
307 case WRITE_VERIFY_12:
308 case WRITE_16:
309 case WRITE_VERIFY_16:
310 case COPY:
311 case COPY_VERIFY:
312 case COMPARE:
313 case CHANGE_DEFINITION:
314 case LOG_SELECT:
315 case MODE_SELECT:
316 case MODE_SELECT_10:
317 case SEND_DIAGNOSTIC:
318 case WRITE_BUFFER:
319 case FORMAT_UNIT:
320 case REASSIGN_BLOCKS:
321 case SEARCH_EQUAL:
322 case SEARCH_HIGH:
323 case SEARCH_LOW:
324 case UPDATE_BLOCK:
325 case WRITE_LONG:
326 case WRITE_SAME:
327 case SEARCH_HIGH_12:
328 case SEARCH_EQUAL_12:
329 case SEARCH_LOW_12:
330 case SET_WINDOW:
331 case MEDIUM_SCAN:
332 case SEND_VOLUME_TAG:
333 case WRITE_LONG_2:
334 case PERSISTENT_RESERVE_OUT:
335 case MAINTENANCE_OUT:
336 req->cmd.mode = SCSI_XFER_TO_DEV;
337 break;
338 default:
339 if (req->cmd.xfer)
340 req->cmd.mode = SCSI_XFER_FROM_DEV;
341 else {
342 req->cmd.mode = SCSI_XFER_NONE;
344 break;
348 static uint64_t scsi_req_lba(SCSIRequest *req)
350 uint8_t *buf = req->cmd.buf;
351 uint64_t lba;
353 switch (buf[0] >> 5) {
354 case 0:
355 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
356 (((uint64_t) buf[1] & 0x1f) << 16);
357 break;
358 case 1:
359 case 2:
360 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
361 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
362 break;
363 case 4:
364 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
365 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
366 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
367 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
368 break;
369 case 5:
370 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
371 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
372 break;
373 default:
374 lba = -1;
377 return lba;
380 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
382 int rc;
384 if (req->dev->type == TYPE_TAPE) {
385 rc = scsi_req_stream_length(req, buf);
386 } else {
387 rc = scsi_req_length(req, buf);
389 if (rc != 0)
390 return rc;
392 memcpy(req->cmd.buf, buf, req->cmd.len);
393 scsi_req_xfer_mode(req);
394 req->cmd.lba = scsi_req_lba(req);
395 return 0;
398 static const char *scsi_command_name(uint8_t cmd)
400 static const char *names[] = {
401 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
402 [ REZERO_UNIT ] = "REZERO_UNIT",
403 /* REWIND and REZERO_UNIT use the same operation code */
404 [ REQUEST_SENSE ] = "REQUEST_SENSE",
405 [ FORMAT_UNIT ] = "FORMAT_UNIT",
406 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
407 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
408 [ READ_6 ] = "READ_6",
409 [ WRITE_6 ] = "WRITE_6",
410 [ SEEK_6 ] = "SEEK_6",
411 [ READ_REVERSE ] = "READ_REVERSE",
412 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
413 [ SPACE ] = "SPACE",
414 [ INQUIRY ] = "INQUIRY",
415 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
416 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
417 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
418 [ MODE_SELECT ] = "MODE_SELECT",
419 [ RESERVE ] = "RESERVE",
420 [ RELEASE ] = "RELEASE",
421 [ COPY ] = "COPY",
422 [ ERASE ] = "ERASE",
423 [ MODE_SENSE ] = "MODE_SENSE",
424 [ START_STOP ] = "START_STOP",
425 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
426 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
427 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
429 [ SET_WINDOW ] = "SET_WINDOW",
430 [ READ_CAPACITY ] = "READ_CAPACITY",
431 [ READ_10 ] = "READ_10",
432 [ WRITE_10 ] = "WRITE_10",
433 [ SEEK_10 ] = "SEEK_10",
434 [ WRITE_VERIFY ] = "WRITE_VERIFY",
435 [ VERIFY ] = "VERIFY",
436 [ SEARCH_HIGH ] = "SEARCH_HIGH",
437 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
438 [ SEARCH_LOW ] = "SEARCH_LOW",
439 [ SET_LIMITS ] = "SET_LIMITS",
440 [ PRE_FETCH ] = "PRE_FETCH",
441 /* READ_POSITION and PRE_FETCH use the same operation code */
442 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
443 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
444 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
445 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
446 [ COMPARE ] = "COMPARE",
447 [ COPY_VERIFY ] = "COPY_VERIFY",
448 [ WRITE_BUFFER ] = "WRITE_BUFFER",
449 [ READ_BUFFER ] = "READ_BUFFER",
450 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
451 [ READ_LONG ] = "READ_LONG",
452 [ WRITE_LONG ] = "WRITE_LONG",
453 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
454 [ WRITE_SAME ] = "WRITE_SAME",
455 [ READ_TOC ] = "READ_TOC",
456 [ LOG_SELECT ] = "LOG_SELECT",
457 [ LOG_SENSE ] = "LOG_SENSE",
458 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
459 [ RESERVE_10 ] = "RESERVE_10",
460 [ RELEASE_10 ] = "RELEASE_10",
461 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
462 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
463 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
464 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
465 [ READ_12 ] = "READ_12",
466 [ WRITE_12 ] = "WRITE_12",
467 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
468 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
469 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
470 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
471 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
472 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
473 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
475 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
476 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
477 [ READ_16 ] = "READ_16",
478 [ WRITE_16 ] = "WRITE_16",
479 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
480 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
481 [ REPORT_LUNS ] = "REPORT_LUNS",
482 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
483 [ SET_CD_SPEED ] = "SET_CD_SPEED",
484 [ BLANK ] = "BLANK",
487 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
488 return "*UNKNOWN*";
489 return names[cmd];
492 void scsi_req_print(SCSIRequest *req)
494 FILE *fp = stderr;
495 int i;
497 fprintf(fp, "[%s id=%d] %s",
498 req->dev->qdev.parent_bus->name,
499 req->dev->id,
500 scsi_command_name(req->cmd.buf[0]));
501 for (i = 1; i < req->cmd.len; i++) {
502 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
504 switch (req->cmd.mode) {
505 case SCSI_XFER_NONE:
506 fprintf(fp, " - none\n");
507 break;
508 case SCSI_XFER_FROM_DEV:
509 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
510 break;
511 case SCSI_XFER_TO_DEV:
512 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
513 break;
514 default:
515 fprintf(fp, " - Oops\n");
516 break;
520 void scsi_req_complete(SCSIRequest *req)
522 assert(req->status != -1);
523 scsi_req_dequeue(req);
524 req->bus->complete(req->bus, SCSI_REASON_DONE,
525 req->tag,
526 req->status);
529 static char *scsibus_get_fw_dev_path(DeviceState *dev)
531 SCSIDevice *d = (SCSIDevice*)dev;
532 SCSIBus *bus = scsi_bus_from_device(d);
533 char path[100];
534 int i;
536 for (i = 0; i < bus->ndev; i++) {
537 if (bus->devs[i] == d) {
538 break;
542 assert(i != bus->ndev);
544 snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
546 return strdup(path);