tcg: Use fprintf_function (format checking)
[qemu/stefanha.git] / hw / scsi-bus.c
blob5a3fd4b7acd615895fede26b447bef5a5365be81
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 struct BusInfo scsi_bus_info = {
9 .name = "SCSI",
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++;
24 bus->tcq = tcq;
25 bus->ndev = ndev;
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);
35 int rc = -1;
37 if (dev->id == -1) {
38 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
39 if (bus->devs[dev->id] == NULL)
40 break;
43 if (dev->id >= bus->ndev) {
44 error_report("bad scsi device id: %d", dev->id);
45 goto err;
48 if (bus->devs[dev->id]) {
49 qdev_free(&bus->devs[dev->id]->qdev);
51 bus->devs[dev->id] = dev;
53 dev->info = info;
54 QTAILQ_INIT(&dev->requests);
55 rc = dev->info->init(dev);
56 if (rc != 0) {
57 bus->devs[dev->id] = NULL;
60 err:
61 return rc;
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;
74 return 0;
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 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, int unit)
89 const char *driver;
90 DeviceState *dev;
92 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
93 dev = qdev_create(&bus->qbus, driver);
94 qdev_prop_set_uint32(dev, "scsi-id", unit);
95 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
96 qdev_free(dev);
97 return NULL;
99 if (qdev_init(dev) < 0)
100 return NULL;
101 return DO_UPCAST(SCSIDevice, qdev, dev);
104 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
106 Location loc;
107 DriveInfo *dinfo;
108 int res = 0, unit;
110 loc_push_none(&loc);
111 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
112 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
113 if (dinfo == NULL) {
114 continue;
116 qemu_opts_loc_restore(dinfo->opts);
117 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit)) {
118 res = -1;
119 break;
122 loc_pop(&loc);
123 return res;
126 void scsi_dev_clear_sense(SCSIDevice *dev)
128 memset(&dev->sense, 0, sizeof(dev->sense));
131 void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
133 dev->sense.key = key;
136 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
138 SCSIRequest *req;
140 req = qemu_mallocz(size);
141 req->bus = scsi_bus_from_device(d);
142 req->dev = d;
143 req->tag = tag;
144 req->lun = lun;
145 req->status = -1;
146 req->enqueued = true;
147 QTAILQ_INSERT_TAIL(&d->requests, req, next);
148 return req;
151 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
153 SCSIRequest *req;
155 QTAILQ_FOREACH(req, &d->requests, next) {
156 if (req->tag == tag) {
157 return req;
160 return NULL;
163 static void scsi_req_dequeue(SCSIRequest *req)
165 if (req->enqueued) {
166 QTAILQ_REMOVE(&req->dev->requests, req, next);
167 req->enqueued = false;
171 void scsi_req_free(SCSIRequest *req)
173 scsi_req_dequeue(req);
174 qemu_free(req);
177 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
179 switch (cmd[0] >> 5) {
180 case 0:
181 req->cmd.xfer = cmd[4];
182 req->cmd.len = 6;
183 /* length 0 means 256 blocks */
184 if (req->cmd.xfer == 0)
185 req->cmd.xfer = 256;
186 break;
187 case 1:
188 case 2:
189 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
190 req->cmd.len = 10;
191 break;
192 case 4:
193 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
194 req->cmd.len = 16;
195 break;
196 case 5:
197 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
198 req->cmd.len = 12;
199 break;
200 default:
201 return -1;
204 switch(cmd[0]) {
205 case TEST_UNIT_READY:
206 case REZERO_UNIT:
207 case START_STOP:
208 case SEEK_6:
209 case WRITE_FILEMARKS:
210 case SPACE:
211 case RESERVE:
212 case RELEASE:
213 case ERASE:
214 case ALLOW_MEDIUM_REMOVAL:
215 case VERIFY:
216 case SEEK_10:
217 case SYNCHRONIZE_CACHE:
218 case LOCK_UNLOCK_CACHE:
219 case LOAD_UNLOAD:
220 case SET_CD_SPEED:
221 case SET_LIMITS:
222 case WRITE_LONG:
223 case MOVE_MEDIUM:
224 case UPDATE_BLOCK:
225 req->cmd.xfer = 0;
226 break;
227 case MODE_SENSE:
228 break;
229 case WRITE_SAME:
230 req->cmd.xfer = 1;
231 break;
232 case READ_CAPACITY:
233 req->cmd.xfer = 8;
234 break;
235 case READ_BLOCK_LIMITS:
236 req->cmd.xfer = 6;
237 break;
238 case READ_POSITION:
239 req->cmd.xfer = 20;
240 break;
241 case SEND_VOLUME_TAG:
242 req->cmd.xfer *= 40;
243 break;
244 case MEDIUM_SCAN:
245 req->cmd.xfer *= 8;
246 break;
247 case WRITE_10:
248 case WRITE_VERIFY:
249 case WRITE_6:
250 case WRITE_12:
251 case WRITE_VERIFY_12:
252 case WRITE_16:
253 case WRITE_VERIFY_16:
254 req->cmd.xfer *= req->dev->blocksize;
255 break;
256 case READ_10:
257 case READ_6:
258 case READ_REVERSE:
259 case RECOVER_BUFFERED_DATA:
260 case READ_12:
261 case READ_16:
262 req->cmd.xfer *= req->dev->blocksize;
263 break;
264 case INQUIRY:
265 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
266 break;
267 case MAINTENANCE_OUT:
268 case MAINTENANCE_IN:
269 if (req->dev->type == TYPE_ROM) {
270 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
271 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
273 break;
275 return 0;
278 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
280 switch(cmd[0]) {
281 /* stream commands */
282 case READ_6:
283 case READ_REVERSE:
284 case RECOVER_BUFFERED_DATA:
285 case WRITE_6:
286 req->cmd.len = 6;
287 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
288 if (cmd[1] & 0x01) /* fixed */
289 req->cmd.xfer *= req->dev->blocksize;
290 break;
291 case REWIND:
292 case START_STOP:
293 req->cmd.len = 6;
294 req->cmd.xfer = 0;
295 break;
296 /* generic commands */
297 default:
298 return scsi_req_length(req, cmd);
300 return 0;
303 static void scsi_req_xfer_mode(SCSIRequest *req)
305 switch (req->cmd.buf[0]) {
306 case WRITE_6:
307 case WRITE_10:
308 case WRITE_VERIFY:
309 case WRITE_12:
310 case WRITE_VERIFY_12:
311 case WRITE_16:
312 case WRITE_VERIFY_16:
313 case COPY:
314 case COPY_VERIFY:
315 case COMPARE:
316 case CHANGE_DEFINITION:
317 case LOG_SELECT:
318 case MODE_SELECT:
319 case MODE_SELECT_10:
320 case SEND_DIAGNOSTIC:
321 case WRITE_BUFFER:
322 case FORMAT_UNIT:
323 case REASSIGN_BLOCKS:
324 case SEARCH_EQUAL:
325 case SEARCH_HIGH:
326 case SEARCH_LOW:
327 case UPDATE_BLOCK:
328 case WRITE_LONG:
329 case WRITE_SAME:
330 case SEARCH_HIGH_12:
331 case SEARCH_EQUAL_12:
332 case SEARCH_LOW_12:
333 case SET_WINDOW:
334 case MEDIUM_SCAN:
335 case SEND_VOLUME_TAG:
336 case WRITE_LONG_2:
337 case PERSISTENT_RESERVE_OUT:
338 case MAINTENANCE_OUT:
339 req->cmd.mode = SCSI_XFER_TO_DEV;
340 break;
341 default:
342 if (req->cmd.xfer)
343 req->cmd.mode = SCSI_XFER_FROM_DEV;
344 else {
345 req->cmd.mode = SCSI_XFER_NONE;
347 break;
351 static uint64_t scsi_req_lba(SCSIRequest *req)
353 uint8_t *buf = req->cmd.buf;
354 uint64_t lba;
356 switch (buf[0] >> 5) {
357 case 0:
358 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
359 (((uint64_t) buf[1] & 0x1f) << 16);
360 break;
361 case 1:
362 case 2:
363 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
364 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
365 break;
366 case 4:
367 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
368 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
369 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
370 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
371 break;
372 case 5:
373 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
374 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
375 break;
376 default:
377 lba = -1;
380 return lba;
383 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
385 int rc;
387 if (req->dev->type == TYPE_TAPE) {
388 rc = scsi_req_stream_length(req, buf);
389 } else {
390 rc = scsi_req_length(req, buf);
392 if (rc != 0)
393 return rc;
395 memcpy(req->cmd.buf, buf, req->cmd.len);
396 scsi_req_xfer_mode(req);
397 req->cmd.lba = scsi_req_lba(req);
398 return 0;
401 static const char *scsi_command_name(uint8_t cmd)
403 static const char *names[] = {
404 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
405 [ REZERO_UNIT ] = "REZERO_UNIT",
406 /* REWIND and REZERO_UNIT use the same operation code */
407 [ REQUEST_SENSE ] = "REQUEST_SENSE",
408 [ FORMAT_UNIT ] = "FORMAT_UNIT",
409 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
410 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
411 [ READ_6 ] = "READ_6",
412 [ WRITE_6 ] = "WRITE_6",
413 [ SEEK_6 ] = "SEEK_6",
414 [ READ_REVERSE ] = "READ_REVERSE",
415 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
416 [ SPACE ] = "SPACE",
417 [ INQUIRY ] = "INQUIRY",
418 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
419 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
420 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
421 [ MODE_SELECT ] = "MODE_SELECT",
422 [ RESERVE ] = "RESERVE",
423 [ RELEASE ] = "RELEASE",
424 [ COPY ] = "COPY",
425 [ ERASE ] = "ERASE",
426 [ MODE_SENSE ] = "MODE_SENSE",
427 [ START_STOP ] = "START_STOP",
428 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
429 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
430 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
432 [ SET_WINDOW ] = "SET_WINDOW",
433 [ READ_CAPACITY ] = "READ_CAPACITY",
434 [ READ_10 ] = "READ_10",
435 [ WRITE_10 ] = "WRITE_10",
436 [ SEEK_10 ] = "SEEK_10",
437 [ WRITE_VERIFY ] = "WRITE_VERIFY",
438 [ VERIFY ] = "VERIFY",
439 [ SEARCH_HIGH ] = "SEARCH_HIGH",
440 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
441 [ SEARCH_LOW ] = "SEARCH_LOW",
442 [ SET_LIMITS ] = "SET_LIMITS",
443 [ PRE_FETCH ] = "PRE_FETCH",
444 /* READ_POSITION and PRE_FETCH use the same operation code */
445 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
446 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
447 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
448 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
449 [ COMPARE ] = "COMPARE",
450 [ COPY_VERIFY ] = "COPY_VERIFY",
451 [ WRITE_BUFFER ] = "WRITE_BUFFER",
452 [ READ_BUFFER ] = "READ_BUFFER",
453 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
454 [ READ_LONG ] = "READ_LONG",
455 [ WRITE_LONG ] = "WRITE_LONG",
456 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
457 [ WRITE_SAME ] = "WRITE_SAME",
458 [ READ_TOC ] = "READ_TOC",
459 [ LOG_SELECT ] = "LOG_SELECT",
460 [ LOG_SENSE ] = "LOG_SENSE",
461 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
462 [ RESERVE_10 ] = "RESERVE_10",
463 [ RELEASE_10 ] = "RELEASE_10",
464 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
465 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
466 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
467 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
468 [ READ_12 ] = "READ_12",
469 [ WRITE_12 ] = "WRITE_12",
470 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
471 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
472 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
473 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
474 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
475 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
476 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
478 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
479 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
480 [ READ_16 ] = "READ_16",
481 [ WRITE_16 ] = "WRITE_16",
482 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
483 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
484 [ REPORT_LUNS ] = "REPORT_LUNS",
485 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
486 [ SET_CD_SPEED ] = "SET_CD_SPEED",
487 [ BLANK ] = "BLANK",
490 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
491 return "*UNKNOWN*";
492 return names[cmd];
495 void scsi_req_print(SCSIRequest *req)
497 FILE *fp = stderr;
498 int i;
500 fprintf(fp, "[%s id=%d] %s",
501 req->dev->qdev.parent_bus->name,
502 req->dev->id,
503 scsi_command_name(req->cmd.buf[0]));
504 for (i = 1; i < req->cmd.len; i++) {
505 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
507 switch (req->cmd.mode) {
508 case SCSI_XFER_NONE:
509 fprintf(fp, " - none\n");
510 break;
511 case SCSI_XFER_FROM_DEV:
512 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
513 break;
514 case SCSI_XFER_TO_DEV:
515 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
516 break;
517 default:
518 fprintf(fp, " - Oops\n");
519 break;
523 void scsi_req_complete(SCSIRequest *req)
525 assert(req->status != -1);
526 scsi_req_dequeue(req);
527 req->bus->complete(req->bus, SCSI_REASON_DONE,
528 req->tag,
529 req->status);