savevm: Add DeviceState param
[qemu/kraxel.git] / hw / scsi-bus.c
blobb84b9b98b52b874e3ace9bfaeea7b3344d07ce66
1 #include "hw.h"
2 #include "qemu-error.h"
3 #include "scsi.h"
4 #include "scsi-defs.h"
5 #include "qdev.h"
7 static struct BusInfo scsi_bus_info = {
8 .name = "SCSI",
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++;
23 bus->tcq = tcq;
24 bus->ndev = ndev;
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);
34 int rc = -1;
36 if (dev->id == -1) {
37 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
38 if (bus->devs[dev->id] == NULL)
39 break;
42 if (dev->id >= bus->ndev) {
43 error_report("bad scsi device id: %d", dev->id);
44 goto err;
47 if (bus->devs[dev->id]) {
48 qdev_free(&bus->devs[dev->id]->qdev);
50 bus->devs[dev->id] = dev;
52 dev->info = info;
53 QTAILQ_INIT(&dev->requests);
54 rc = dev->info->init(dev);
55 if (rc != 0) {
56 bus->devs[dev->id] = NULL;
59 err:
60 return rc;
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;
73 return 0;
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 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv, int unit)
88 const char *driver;
89 DeviceState *dev;
91 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
92 dev = qdev_create(&bus->qbus, driver);
93 qdev_prop_set_uint32(dev, "scsi-id", unit);
94 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
95 qdev_free(dev);
96 return NULL;
98 if (qdev_init(dev) < 0)
99 return NULL;
100 return DO_UPCAST(SCSIDevice, qdev, dev);
103 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
105 DriveInfo *dinfo;
106 int res = 0, unit;
108 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
109 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
110 if (dinfo == NULL) {
111 continue;
113 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit)) {
114 res = -1;
115 break;
118 return res;
121 void scsi_dev_clear_sense(SCSIDevice *dev)
123 memset(&dev->sense, 0, sizeof(dev->sense));
126 void scsi_dev_set_sense(SCSIDevice *dev, uint8_t key)
128 dev->sense.key = key;
131 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag, uint32_t lun)
133 SCSIRequest *req;
135 req = qemu_mallocz(size);
136 req->bus = scsi_bus_from_device(d);
137 req->dev = d;
138 req->tag = tag;
139 req->lun = lun;
140 req->status = -1;
141 QTAILQ_INSERT_TAIL(&d->requests, req, next);
142 return req;
145 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
147 SCSIRequest *req;
149 QTAILQ_FOREACH(req, &d->requests, next) {
150 if (req->tag == tag) {
151 return req;
154 return NULL;
157 void scsi_req_free(SCSIRequest *req)
159 QTAILQ_REMOVE(&req->dev->requests, req, next);
160 qemu_free(req);
163 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
165 switch (cmd[0] >> 5) {
166 case 0:
167 req->cmd.xfer = cmd[4];
168 req->cmd.len = 6;
169 /* length 0 means 256 blocks */
170 if (req->cmd.xfer == 0)
171 req->cmd.xfer = 256;
172 break;
173 case 1:
174 case 2:
175 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
176 req->cmd.len = 10;
177 break;
178 case 4:
179 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
180 req->cmd.len = 16;
181 break;
182 case 5:
183 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
184 req->cmd.len = 12;
185 break;
186 default:
187 return -1;
190 switch(cmd[0]) {
191 case TEST_UNIT_READY:
192 case REZERO_UNIT:
193 case START_STOP:
194 case SEEK_6:
195 case WRITE_FILEMARKS:
196 case SPACE:
197 case ERASE:
198 case ALLOW_MEDIUM_REMOVAL:
199 case VERIFY:
200 case SEEK_10:
201 case SYNCHRONIZE_CACHE:
202 case LOCK_UNLOCK_CACHE:
203 case LOAD_UNLOAD:
204 case SET_CD_SPEED:
205 case SET_LIMITS:
206 case WRITE_LONG:
207 case MOVE_MEDIUM:
208 case UPDATE_BLOCK:
209 req->cmd.xfer = 0;
210 break;
211 case MODE_SENSE:
212 break;
213 case WRITE_SAME:
214 req->cmd.xfer = 1;
215 break;
216 case READ_CAPACITY:
217 req->cmd.xfer = 8;
218 break;
219 case READ_BLOCK_LIMITS:
220 req->cmd.xfer = 6;
221 break;
222 case READ_POSITION:
223 req->cmd.xfer = 20;
224 break;
225 case SEND_VOLUME_TAG:
226 req->cmd.xfer *= 40;
227 break;
228 case MEDIUM_SCAN:
229 req->cmd.xfer *= 8;
230 break;
231 case WRITE_10:
232 case WRITE_VERIFY:
233 case WRITE_6:
234 case WRITE_12:
235 case WRITE_VERIFY_12:
236 case WRITE_16:
237 case WRITE_VERIFY_16:
238 req->cmd.xfer *= req->dev->blocksize;
239 break;
240 case READ_10:
241 case READ_6:
242 case READ_REVERSE:
243 case RECOVER_BUFFERED_DATA:
244 case READ_12:
245 case READ_16:
246 req->cmd.xfer *= req->dev->blocksize;
247 break;
248 case INQUIRY:
249 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
250 break;
251 case MAINTENANCE_OUT:
252 case MAINTENANCE_IN:
253 if (req->dev->type == TYPE_ROM) {
254 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
255 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
257 break;
259 return 0;
262 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
264 switch(cmd[0]) {
265 /* stream commands */
266 case READ_6:
267 case READ_REVERSE:
268 case RECOVER_BUFFERED_DATA:
269 case WRITE_6:
270 req->cmd.len = 6;
271 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
272 if (cmd[1] & 0x01) /* fixed */
273 req->cmd.xfer *= req->dev->blocksize;
274 break;
275 case REWIND:
276 case START_STOP:
277 req->cmd.len = 6;
278 req->cmd.xfer = 0;
279 break;
280 /* generic commands */
281 default:
282 return scsi_req_length(req, cmd);
284 return 0;
287 static void scsi_req_xfer_mode(SCSIRequest *req)
289 switch (req->cmd.buf[0]) {
290 case WRITE_6:
291 case WRITE_10:
292 case WRITE_VERIFY:
293 case WRITE_12:
294 case WRITE_VERIFY_12:
295 case WRITE_16:
296 case WRITE_VERIFY_16:
297 case COPY:
298 case COPY_VERIFY:
299 case COMPARE:
300 case CHANGE_DEFINITION:
301 case LOG_SELECT:
302 case MODE_SELECT:
303 case MODE_SELECT_10:
304 case SEND_DIAGNOSTIC:
305 case WRITE_BUFFER:
306 case FORMAT_UNIT:
307 case REASSIGN_BLOCKS:
308 case RESERVE:
309 case SEARCH_EQUAL:
310 case SEARCH_HIGH:
311 case SEARCH_LOW:
312 case UPDATE_BLOCK:
313 case WRITE_LONG:
314 case WRITE_SAME:
315 case SEARCH_HIGH_12:
316 case SEARCH_EQUAL_12:
317 case SEARCH_LOW_12:
318 case SET_WINDOW:
319 case MEDIUM_SCAN:
320 case SEND_VOLUME_TAG:
321 case WRITE_LONG_2:
322 case PERSISTENT_RESERVE_OUT:
323 case MAINTENANCE_OUT:
324 req->cmd.mode = SCSI_XFER_TO_DEV;
325 break;
326 default:
327 if (req->cmd.xfer)
328 req->cmd.mode = SCSI_XFER_FROM_DEV;
329 else {
330 req->cmd.mode = SCSI_XFER_NONE;
332 break;
336 static uint64_t scsi_req_lba(SCSIRequest *req)
338 uint8_t *buf = req->cmd.buf;
339 uint64_t lba;
341 switch (buf[0] >> 5) {
342 case 0:
343 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
344 (((uint64_t) buf[1] & 0x1f) << 16);
345 break;
346 case 1:
347 case 2:
348 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
349 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
350 break;
351 case 4:
352 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
353 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
354 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
355 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
356 break;
357 case 5:
358 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
359 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
360 break;
361 default:
362 lba = -1;
365 return lba;
368 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
370 int rc;
372 if (req->dev->type == TYPE_TAPE) {
373 rc = scsi_req_stream_length(req, buf);
374 } else {
375 rc = scsi_req_length(req, buf);
377 if (rc != 0)
378 return rc;
380 memcpy(req->cmd.buf, buf, req->cmd.len);
381 scsi_req_xfer_mode(req);
382 req->cmd.lba = scsi_req_lba(req);
383 return 0;
386 static const char *scsi_command_name(uint8_t cmd)
388 static const char *names[] = {
389 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
390 [ REZERO_UNIT ] = "REZERO_UNIT",
391 /* REWIND and REZERO_UNIT use the same operation code */
392 [ REQUEST_SENSE ] = "REQUEST_SENSE",
393 [ FORMAT_UNIT ] = "FORMAT_UNIT",
394 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
395 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
396 [ READ_6 ] = "READ_6",
397 [ WRITE_6 ] = "WRITE_6",
398 [ SEEK_6 ] = "SEEK_6",
399 [ READ_REVERSE ] = "READ_REVERSE",
400 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
401 [ SPACE ] = "SPACE",
402 [ INQUIRY ] = "INQUIRY",
403 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
404 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
405 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
406 [ MODE_SELECT ] = "MODE_SELECT",
407 [ RESERVE ] = "RESERVE",
408 [ RELEASE ] = "RELEASE",
409 [ COPY ] = "COPY",
410 [ ERASE ] = "ERASE",
411 [ MODE_SENSE ] = "MODE_SENSE",
412 [ START_STOP ] = "START_STOP",
413 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
414 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
415 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
417 [ SET_WINDOW ] = "SET_WINDOW",
418 [ READ_CAPACITY ] = "READ_CAPACITY",
419 [ READ_10 ] = "READ_10",
420 [ WRITE_10 ] = "WRITE_10",
421 [ SEEK_10 ] = "SEEK_10",
422 [ WRITE_VERIFY ] = "WRITE_VERIFY",
423 [ VERIFY ] = "VERIFY",
424 [ SEARCH_HIGH ] = "SEARCH_HIGH",
425 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
426 [ SEARCH_LOW ] = "SEARCH_LOW",
427 [ SET_LIMITS ] = "SET_LIMITS",
428 [ PRE_FETCH ] = "PRE_FETCH",
429 /* READ_POSITION and PRE_FETCH use the same operation code */
430 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
431 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
432 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
433 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
434 [ COMPARE ] = "COMPARE",
435 [ COPY_VERIFY ] = "COPY_VERIFY",
436 [ WRITE_BUFFER ] = "WRITE_BUFFER",
437 [ READ_BUFFER ] = "READ_BUFFER",
438 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
439 [ READ_LONG ] = "READ_LONG",
440 [ WRITE_LONG ] = "WRITE_LONG",
441 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
442 [ WRITE_SAME ] = "WRITE_SAME",
443 [ READ_TOC ] = "READ_TOC",
444 [ LOG_SELECT ] = "LOG_SELECT",
445 [ LOG_SENSE ] = "LOG_SENSE",
446 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
447 [ RESERVE_10 ] = "RESERVE_10",
448 [ RELEASE_10 ] = "RELEASE_10",
449 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
450 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
451 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
452 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
453 [ READ_12 ] = "READ_12",
454 [ WRITE_12 ] = "WRITE_12",
455 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
456 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
457 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
458 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
459 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
460 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
461 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
463 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
464 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
465 [ READ_16 ] = "READ_16",
466 [ WRITE_16 ] = "WRITE_16",
467 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
468 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
469 [ REPORT_LUNS ] = "REPORT_LUNS",
470 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
471 [ SET_CD_SPEED ] = "SET_CD_SPEED",
472 [ BLANK ] = "BLANK",
475 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
476 return "*UNKNOWN*";
477 return names[cmd];
480 void scsi_req_print(SCSIRequest *req)
482 FILE *fp = stderr;
483 int i;
485 fprintf(fp, "[%s id=%d] %s",
486 req->dev->qdev.parent_bus->name,
487 req->dev->id,
488 scsi_command_name(req->cmd.buf[0]));
489 for (i = 1; i < req->cmd.len; i++) {
490 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
492 switch (req->cmd.mode) {
493 case SCSI_XFER_NONE:
494 fprintf(fp, " - none\n");
495 break;
496 case SCSI_XFER_FROM_DEV:
497 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
498 break;
499 case SCSI_XFER_TO_DEV:
500 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
501 break;
502 default:
503 fprintf(fp, " - Oops\n");
504 break;
508 void scsi_req_complete(SCSIRequest *req)
510 assert(req->status != -1);
511 req->bus->complete(req->bus, SCSI_REASON_DONE,
512 req->tag,
513 req->status);