virtio-blk: fix the list operation in virtio_blk_load().
[qemu.git] / hw / scsi-bus.c
blob24bd0602efbc535cf6557e7d0e3ad99fd41069aa
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 /* FIXME callers should check for failure, but don't */
87 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, DriveInfo *dinfo, int unit)
89 const char *driver;
90 DeviceState *dev;
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)
97 return NULL;
98 return DO_UPCAST(SCSIDevice, qdev, dev);
101 void scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
103 DriveInfo *dinfo;
104 int unit;
106 for (unit = 0; unit < MAX_SCSI_DEVS; unit++) {
107 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
108 if (dinfo == NULL) {
109 continue;
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)
127 SCSIRequest *req;
129 req = qemu_mallocz(size);
130 req->bus = scsi_bus_from_device(d);
131 req->dev = d;
132 req->tag = tag;
133 req->lun = lun;
134 req->status = -1;
135 QTAILQ_INSERT_TAIL(&d->requests, req, next);
136 return req;
139 SCSIRequest *scsi_req_find(SCSIDevice *d, uint32_t tag)
141 SCSIRequest *req;
143 QTAILQ_FOREACH(req, &d->requests, next) {
144 if (req->tag == tag) {
145 return req;
148 return NULL;
151 void scsi_req_free(SCSIRequest *req)
153 QTAILQ_REMOVE(&req->dev->requests, req, next);
154 qemu_free(req);
157 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
159 switch (cmd[0] >> 5) {
160 case 0:
161 req->cmd.xfer = cmd[4];
162 req->cmd.len = 6;
163 /* length 0 means 256 blocks */
164 if (req->cmd.xfer == 0)
165 req->cmd.xfer = 256;
166 break;
167 case 1:
168 case 2:
169 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
170 req->cmd.len = 10;
171 break;
172 case 4:
173 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
174 req->cmd.len = 16;
175 break;
176 case 5:
177 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
178 req->cmd.len = 12;
179 break;
180 default:
181 return -1;
184 switch(cmd[0]) {
185 case TEST_UNIT_READY:
186 case REZERO_UNIT:
187 case START_STOP:
188 case SEEK_6:
189 case WRITE_FILEMARKS:
190 case SPACE:
191 case ERASE:
192 case ALLOW_MEDIUM_REMOVAL:
193 case VERIFY:
194 case SEEK_10:
195 case SYNCHRONIZE_CACHE:
196 case LOCK_UNLOCK_CACHE:
197 case LOAD_UNLOAD:
198 case SET_CD_SPEED:
199 case SET_LIMITS:
200 case WRITE_LONG:
201 case MOVE_MEDIUM:
202 case UPDATE_BLOCK:
203 req->cmd.xfer = 0;
204 break;
205 case MODE_SENSE:
206 break;
207 case WRITE_SAME:
208 req->cmd.xfer = 1;
209 break;
210 case READ_CAPACITY:
211 req->cmd.xfer = 8;
212 break;
213 case READ_BLOCK_LIMITS:
214 req->cmd.xfer = 6;
215 break;
216 case READ_POSITION:
217 req->cmd.xfer = 20;
218 break;
219 case SEND_VOLUME_TAG:
220 req->cmd.xfer *= 40;
221 break;
222 case MEDIUM_SCAN:
223 req->cmd.xfer *= 8;
224 break;
225 case WRITE_10:
226 case WRITE_VERIFY:
227 case WRITE_6:
228 case WRITE_12:
229 case WRITE_VERIFY_12:
230 case WRITE_16:
231 case WRITE_VERIFY_16:
232 req->cmd.xfer *= req->dev->blocksize;
233 break;
234 case READ_10:
235 case READ_6:
236 case READ_REVERSE:
237 case RECOVER_BUFFERED_DATA:
238 case READ_12:
239 case READ_16:
240 req->cmd.xfer *= req->dev->blocksize;
241 break;
242 case INQUIRY:
243 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
244 break;
245 case MAINTENANCE_OUT:
246 case MAINTENANCE_IN:
247 if (req->dev->type == TYPE_ROM) {
248 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
249 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
251 break;
253 return 0;
256 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
258 switch(cmd[0]) {
259 /* stream commands */
260 case READ_6:
261 case READ_REVERSE:
262 case RECOVER_BUFFERED_DATA:
263 case WRITE_6:
264 req->cmd.len = 6;
265 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
266 if (cmd[1] & 0x01) /* fixed */
267 req->cmd.xfer *= req->dev->blocksize;
268 break;
269 case REWIND:
270 case START_STOP:
271 req->cmd.len = 6;
272 req->cmd.xfer = 0;
273 break;
274 /* generic commands */
275 default:
276 return scsi_req_length(req, cmd);
278 return 0;
281 static void scsi_req_xfer_mode(SCSIRequest *req)
283 switch (req->cmd.buf[0]) {
284 case WRITE_6:
285 case WRITE_10:
286 case WRITE_VERIFY:
287 case WRITE_12:
288 case WRITE_VERIFY_12:
289 case WRITE_16:
290 case WRITE_VERIFY_16:
291 case COPY:
292 case COPY_VERIFY:
293 case COMPARE:
294 case CHANGE_DEFINITION:
295 case LOG_SELECT:
296 case MODE_SELECT:
297 case MODE_SELECT_10:
298 case SEND_DIAGNOSTIC:
299 case WRITE_BUFFER:
300 case FORMAT_UNIT:
301 case REASSIGN_BLOCKS:
302 case RESERVE:
303 case SEARCH_EQUAL:
304 case SEARCH_HIGH:
305 case SEARCH_LOW:
306 case UPDATE_BLOCK:
307 case WRITE_LONG:
308 case WRITE_SAME:
309 case SEARCH_HIGH_12:
310 case SEARCH_EQUAL_12:
311 case SEARCH_LOW_12:
312 case SET_WINDOW:
313 case MEDIUM_SCAN:
314 case SEND_VOLUME_TAG:
315 case WRITE_LONG_2:
316 case PERSISTENT_RESERVE_OUT:
317 case MAINTENANCE_OUT:
318 req->cmd.mode = SCSI_XFER_TO_DEV;
319 break;
320 default:
321 if (req->cmd.xfer)
322 req->cmd.mode = SCSI_XFER_FROM_DEV;
323 else {
324 req->cmd.mode = SCSI_XFER_NONE;
326 break;
330 static uint64_t scsi_req_lba(SCSIRequest *req)
332 uint8_t *buf = req->cmd.buf;
333 uint64_t lba;
335 switch (buf[0] >> 5) {
336 case 0:
337 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
338 (((uint64_t) buf[1] & 0x1f) << 16);
339 break;
340 case 1:
341 case 2:
342 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
343 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
344 break;
345 case 4:
346 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
347 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
348 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
349 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
350 break;
351 case 5:
352 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
353 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
354 break;
355 default:
356 lba = -1;
359 return lba;
362 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
364 int rc;
366 if (req->dev->type == TYPE_TAPE) {
367 rc = scsi_req_stream_length(req, buf);
368 } else {
369 rc = scsi_req_length(req, buf);
371 if (rc != 0)
372 return rc;
374 memcpy(req->cmd.buf, buf, req->cmd.len);
375 scsi_req_xfer_mode(req);
376 req->cmd.lba = scsi_req_lba(req);
377 return 0;
380 static const char *scsi_command_name(uint8_t cmd)
382 static const char *names[] = {
383 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
384 [ REZERO_UNIT ] = "REZERO_UNIT",
385 /* REWIND and REZERO_UNIT use the same operation code */
386 [ REQUEST_SENSE ] = "REQUEST_SENSE",
387 [ FORMAT_UNIT ] = "FORMAT_UNIT",
388 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
389 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
390 [ READ_6 ] = "READ_6",
391 [ WRITE_6 ] = "WRITE_6",
392 [ SEEK_6 ] = "SEEK_6",
393 [ READ_REVERSE ] = "READ_REVERSE",
394 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
395 [ SPACE ] = "SPACE",
396 [ INQUIRY ] = "INQUIRY",
397 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
398 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
399 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
400 [ MODE_SELECT ] = "MODE_SELECT",
401 [ RESERVE ] = "RESERVE",
402 [ RELEASE ] = "RELEASE",
403 [ COPY ] = "COPY",
404 [ ERASE ] = "ERASE",
405 [ MODE_SENSE ] = "MODE_SENSE",
406 [ START_STOP ] = "START_STOP",
407 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
408 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
409 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
411 [ SET_WINDOW ] = "SET_WINDOW",
412 [ READ_CAPACITY ] = "READ_CAPACITY",
413 [ READ_10 ] = "READ_10",
414 [ WRITE_10 ] = "WRITE_10",
415 [ SEEK_10 ] = "SEEK_10",
416 [ WRITE_VERIFY ] = "WRITE_VERIFY",
417 [ VERIFY ] = "VERIFY",
418 [ SEARCH_HIGH ] = "SEARCH_HIGH",
419 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
420 [ SEARCH_LOW ] = "SEARCH_LOW",
421 [ SET_LIMITS ] = "SET_LIMITS",
422 [ PRE_FETCH ] = "PRE_FETCH",
423 /* READ_POSITION and PRE_FETCH use the same operation code */
424 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
425 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
426 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
427 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
428 [ COMPARE ] = "COMPARE",
429 [ COPY_VERIFY ] = "COPY_VERIFY",
430 [ WRITE_BUFFER ] = "WRITE_BUFFER",
431 [ READ_BUFFER ] = "READ_BUFFER",
432 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
433 [ READ_LONG ] = "READ_LONG",
434 [ WRITE_LONG ] = "WRITE_LONG",
435 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
436 [ WRITE_SAME ] = "WRITE_SAME",
437 [ READ_TOC ] = "READ_TOC",
438 [ LOG_SELECT ] = "LOG_SELECT",
439 [ LOG_SENSE ] = "LOG_SENSE",
440 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
441 [ RESERVE_10 ] = "RESERVE_10",
442 [ RELEASE_10 ] = "RELEASE_10",
443 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
444 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
445 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
446 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
447 [ READ_12 ] = "READ_12",
448 [ WRITE_12 ] = "WRITE_12",
449 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
450 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
451 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
452 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
453 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
454 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
455 [ WRITE_LONG_2 ] = "WRITE_LONG_2",
457 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
458 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
459 [ READ_16 ] = "READ_16",
460 [ WRITE_16 ] = "WRITE_16",
461 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
462 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
463 [ REPORT_LUNS ] = "REPORT_LUNS",
464 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
465 [ SET_CD_SPEED ] = "SET_CD_SPEED",
466 [ BLANK ] = "BLANK",
469 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
470 return "*UNKNOWN*";
471 return names[cmd];
474 void scsi_req_print(SCSIRequest *req)
476 FILE *fp = stderr;
477 int i;
479 fprintf(fp, "[%s id=%d] %s",
480 req->dev->qdev.parent_bus->name,
481 req->dev->id,
482 scsi_command_name(req->cmd.buf[0]));
483 for (i = 1; i < req->cmd.len; i++) {
484 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
486 switch (req->cmd.mode) {
487 case SCSI_XFER_NONE:
488 fprintf(fp, " - none\n");
489 break;
490 case SCSI_XFER_FROM_DEV:
491 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
492 break;
493 case SCSI_XFER_TO_DEV:
494 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
495 break;
496 default:
497 fprintf(fp, " - Oops\n");
498 break;
502 void scsi_req_complete(SCSIRequest *req)
504 assert(req->status != -1);
505 req->bus->complete(req->bus, SCSI_REASON_DONE,
506 req->tag,
507 req->status);