add migration state change notifiers
[qemu/stefanha.git] / hw / scsi-bus.c
blob7febb86e775b840b6f33ed7e069f21f39296a4e9
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, int unit)
92 const char *driver;
93 DeviceState *dev;
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) {
99 qdev_free(dev);
100 return NULL;
102 if (qdev_init(dev) < 0)
103 return NULL;
104 return DO_UPCAST(SCSIDevice, qdev, dev);
107 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
109 Location loc;
110 DriveInfo *dinfo;
111 int res = 0, unit;
113 loc_push_none(&loc);
114 for (unit = 0; unit < bus->ndev; unit++) {
115 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
116 if (dinfo == NULL) {
117 continue;
119 qemu_opts_loc_restore(dinfo->opts);
120 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit)) {
121 res = -1;
122 break;
125 loc_pop(&loc);
126 return res;
129 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
131 SCSIRequest *req;
133 req = qemu_mallocz(size);
134 req->bus = scsi_bus_from_device(d);
135 req->dev = d;
136 req->tag = tag;
137 req->lun = lun;
138 req->status = -1;
139 req->enqueued = true;
140 QTAILQ_INSERT_TAIL(&d->requests, req, next);
141 return req;
144 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
146 SCSIRequest *req;
148 QTAILQ_FOREACH(req, &d->requests, next) {
149 if (req->tag == tag) {
150 return req;
153 return NULL;
156 static void scsi_req_dequeue(SCSIRequest *req)
158 if (req->enqueued) {
159 QTAILQ_REMOVE(&req->dev->requests, req, next);
160 req->enqueued = false;
164 void scsi_req_free(SCSIRequest *req)
166 scsi_req_dequeue(req);
167 qemu_free(req);
170 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
172 switch (cmd[0] >> 5) {
173 case 0:
174 req->cmd.xfer = cmd[4];
175 req->cmd.len = 6;
176 /* length 0 means 256 blocks */
177 if (req->cmd.xfer == 0)
178 req->cmd.xfer = 256;
179 break;
180 case 1:
181 case 2:
182 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
183 req->cmd.len = 10;
184 break;
185 case 4:
186 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
187 req->cmd.len = 16;
188 break;
189 case 5:
190 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
191 req->cmd.len = 12;
192 break;
193 default:
194 return -1;
197 switch(cmd[0]) {
198 case TEST_UNIT_READY:
199 case REZERO_UNIT:
200 case START_STOP:
201 case SEEK_6:
202 case WRITE_FILEMARKS:
203 case SPACE:
204 case RESERVE:
205 case RELEASE:
206 case ERASE:
207 case ALLOW_MEDIUM_REMOVAL:
208 case VERIFY:
209 case SEEK_10:
210 case SYNCHRONIZE_CACHE:
211 case LOCK_UNLOCK_CACHE:
212 case LOAD_UNLOAD:
213 case SET_CD_SPEED:
214 case SET_LIMITS:
215 case WRITE_LONG:
216 case MOVE_MEDIUM:
217 case UPDATE_BLOCK:
218 req->cmd.xfer = 0;
219 break;
220 case MODE_SENSE:
221 break;
222 case WRITE_SAME:
223 req->cmd.xfer = 1;
224 break;
225 case READ_CAPACITY:
226 req->cmd.xfer = 8;
227 break;
228 case READ_BLOCK_LIMITS:
229 req->cmd.xfer = 6;
230 break;
231 case READ_POSITION:
232 req->cmd.xfer = 20;
233 break;
234 case SEND_VOLUME_TAG:
235 req->cmd.xfer *= 40;
236 break;
237 case MEDIUM_SCAN:
238 req->cmd.xfer *= 8;
239 break;
240 case WRITE_10:
241 case WRITE_VERIFY:
242 case WRITE_6:
243 case WRITE_12:
244 case WRITE_VERIFY_12:
245 case WRITE_16:
246 case WRITE_VERIFY_16:
247 req->cmd.xfer *= req->dev->blocksize;
248 break;
249 case READ_10:
250 case READ_6:
251 case READ_REVERSE:
252 case RECOVER_BUFFERED_DATA:
253 case READ_12:
254 case READ_16:
255 req->cmd.xfer *= req->dev->blocksize;
256 break;
257 case INQUIRY:
258 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
259 break;
260 case MAINTENANCE_OUT:
261 case MAINTENANCE_IN:
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);
266 break;
268 return 0;
271 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
273 switch(cmd[0]) {
274 /* stream commands */
275 case READ_6:
276 case READ_REVERSE:
277 case RECOVER_BUFFERED_DATA:
278 case WRITE_6:
279 req->cmd.len = 6;
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;
283 break;
284 case REWIND:
285 case START_STOP:
286 req->cmd.len = 6;
287 req->cmd.xfer = 0;
288 break;
289 /* generic commands */
290 default:
291 return scsi_req_length(req, cmd);
293 return 0;
296 static void scsi_req_xfer_mode(SCSIRequest *req)
298 switch (req->cmd.buf[0]) {
299 case WRITE_6:
300 case WRITE_10:
301 case WRITE_VERIFY:
302 case WRITE_12:
303 case WRITE_VERIFY_12:
304 case WRITE_16:
305 case WRITE_VERIFY_16:
306 case COPY:
307 case COPY_VERIFY:
308 case COMPARE:
309 case CHANGE_DEFINITION:
310 case LOG_SELECT:
311 case MODE_SELECT:
312 case MODE_SELECT_10:
313 case SEND_DIAGNOSTIC:
314 case WRITE_BUFFER:
315 case FORMAT_UNIT:
316 case REASSIGN_BLOCKS:
317 case SEARCH_EQUAL:
318 case SEARCH_HIGH:
319 case SEARCH_LOW:
320 case UPDATE_BLOCK:
321 case WRITE_LONG:
322 case WRITE_SAME:
323 case SEARCH_HIGH_12:
324 case SEARCH_EQUAL_12:
325 case SEARCH_LOW_12:
326 case SET_WINDOW:
327 case MEDIUM_SCAN:
328 case SEND_VOLUME_TAG:
329 case WRITE_LONG_2:
330 case PERSISTENT_RESERVE_OUT:
331 case MAINTENANCE_OUT:
332 req->cmd.mode = SCSI_XFER_TO_DEV;
333 break;
334 default:
335 if (req->cmd.xfer)
336 req->cmd.mode = SCSI_XFER_FROM_DEV;
337 else {
338 req->cmd.mode = SCSI_XFER_NONE;
340 break;
344 static uint64_t scsi_req_lba(SCSIRequest *req)
346 uint8_t *buf = req->cmd.buf;
347 uint64_t lba;
349 switch (buf[0] >> 5) {
350 case 0:
351 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
352 (((uint64_t) buf[1] & 0x1f) << 16);
353 break;
354 case 1:
355 case 2:
356 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
357 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
358 break;
359 case 4:
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);
364 break;
365 case 5:
366 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
367 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
368 break;
369 default:
370 lba = -1;
373 return lba;
376 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
378 int rc;
380 if (req->dev->type == TYPE_TAPE) {
381 rc = scsi_req_stream_length(req, buf);
382 } else {
383 rc = scsi_req_length(req, buf);
385 if (rc != 0)
386 return rc;
388 memcpy(req->cmd.buf, buf, req->cmd.len);
389 scsi_req_xfer_mode(req);
390 req->cmd.lba = scsi_req_lba(req);
391 return 0;
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",
409 [ SPACE ] = "SPACE",
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",
417 [ COPY ] = "COPY",
418 [ ERASE ] = "ERASE",
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",
480 [ BLANK ] = "BLANK",
483 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
484 return "*UNKNOWN*";
485 return names[cmd];
488 void scsi_req_print(SCSIRequest *req)
490 FILE *fp = stderr;
491 int i;
493 fprintf(fp, "[%s id=%d] %s",
494 req->dev->qdev.parent_bus->name,
495 req->dev->id,
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) {
501 case SCSI_XFER_NONE:
502 fprintf(fp, " - none\n");
503 break;
504 case SCSI_XFER_FROM_DEV:
505 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
506 break;
507 case SCSI_XFER_TO_DEV:
508 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
509 break;
510 default:
511 fprintf(fp, " - Oops\n");
512 break;
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,
521 req->tag,
522 req->status);
525 static char *scsibus_get_fw_dev_path(DeviceState *dev)
527 SCSIDevice *d = (SCSIDevice*)dev;
528 SCSIBus *bus = scsi_bus_from_device(d);
529 char path[100];
530 int i;
532 for (i = 0; i < bus->ndev; i++) {
533 if (bus->devs[i] == d) {
534 break;
538 assert(i != bus->ndev);
540 snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
542 return strdup(path);