]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi-bus.c
Merge remote-tracking branch 'mst/for_anthony' into staging
[mirror_qemu.git] / hw / scsi-bus.c
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"
7 #include "trace.h"
8
9 static char *scsibus_get_fw_dev_path(DeviceState *dev);
10
11 static struct BusInfo scsi_bus_info = {
12 .name = "SCSI",
13 .size = sizeof(SCSIBus),
14 .get_fw_dev_path = scsibus_get_fw_dev_path,
15 .props = (Property[]) {
16 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
17 DEFINE_PROP_END_OF_LIST(),
18 },
19 };
20 static int next_scsi_bus;
21
22 /* Create a scsi bus, and attach devices to it. */
23 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
24 const SCSIBusOps *ops)
25 {
26 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
27 bus->busnr = next_scsi_bus++;
28 bus->tcq = tcq;
29 bus->ndev = ndev;
30 bus->ops = ops;
31 bus->qbus.allow_hotplug = 1;
32 }
33
34 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
35 {
36 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
37 SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
38 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
39 int rc = -1;
40
41 if (dev->id == -1) {
42 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
43 if (bus->devs[dev->id] == NULL)
44 break;
45 }
46 }
47 if (dev->id >= bus->ndev) {
48 error_report("bad scsi device id: %d", dev->id);
49 goto err;
50 }
51
52 if (bus->devs[dev->id]) {
53 qdev_free(&bus->devs[dev->id]->qdev);
54 }
55 bus->devs[dev->id] = dev;
56
57 dev->info = info;
58 QTAILQ_INIT(&dev->requests);
59 rc = dev->info->init(dev);
60 if (rc != 0) {
61 bus->devs[dev->id] = NULL;
62 }
63
64 err:
65 return rc;
66 }
67
68 static int scsi_qdev_exit(DeviceState *qdev)
69 {
70 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
71 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
72
73 assert(bus->devs[dev->id] != NULL);
74 if (bus->devs[dev->id]->info->destroy) {
75 bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
76 }
77 bus->devs[dev->id] = NULL;
78 return 0;
79 }
80
81 void scsi_qdev_register(SCSIDeviceInfo *info)
82 {
83 info->qdev.bus_info = &scsi_bus_info;
84 info->qdev.init = scsi_qdev_init;
85 info->qdev.unplug = qdev_simple_unplug_cb;
86 info->qdev.exit = scsi_qdev_exit;
87 qdev_register(&info->qdev);
88 }
89
90 /* handle legacy '-drive if=scsi,...' cmd line args */
91 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
92 int unit, bool removable)
93 {
94 const char *driver;
95 DeviceState *dev;
96
97 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
98 dev = qdev_create(&bus->qbus, driver);
99 qdev_prop_set_uint32(dev, "scsi-id", unit);
100 if (qdev_prop_exists(dev, "removable")) {
101 qdev_prop_set_bit(dev, "removable", removable);
102 }
103 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
104 qdev_free(dev);
105 return NULL;
106 }
107 if (qdev_init(dev) < 0)
108 return NULL;
109 return DO_UPCAST(SCSIDevice, qdev, dev);
110 }
111
112 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
113 {
114 Location loc;
115 DriveInfo *dinfo;
116 int res = 0, unit;
117
118 loc_push_none(&loc);
119 for (unit = 0; unit < bus->ndev; unit++) {
120 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
121 if (dinfo == NULL) {
122 continue;
123 }
124 qemu_opts_loc_restore(dinfo->opts);
125 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
126 res = -1;
127 break;
128 }
129 }
130 loc_pop(&loc);
131 return res;
132 }
133
134 SCSIRequest *scsi_req_alloc(size_t size, SCSIDevice *d, uint32_t tag,
135 uint32_t lun, void *hba_private)
136 {
137 SCSIRequest *req;
138
139 req = qemu_mallocz(size);
140 req->refcount = 1;
141 req->bus = scsi_bus_from_device(d);
142 req->dev = d;
143 req->tag = tag;
144 req->lun = lun;
145 req->hba_private = hba_private;
146 req->status = -1;
147 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
148 return req;
149 }
150
151 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
152 void *hba_private)
153 {
154 return d->info->alloc_req(d, tag, lun, hba_private);
155 }
156
157 uint8_t *scsi_req_get_buf(SCSIRequest *req)
158 {
159 return req->dev->info->get_buf(req);
160 }
161
162 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
163 {
164 if (req->dev->info->get_sense) {
165 return req->dev->info->get_sense(req, buf, len);
166 } else {
167 return 0;
168 }
169 }
170
171 int32_t scsi_req_enqueue(SCSIRequest *req, uint8_t *buf)
172 {
173 int32_t rc;
174
175 assert(!req->enqueued);
176 scsi_req_ref(req);
177 req->enqueued = true;
178 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
179
180 scsi_req_ref(req);
181 rc = req->dev->info->send_command(req, buf);
182 scsi_req_unref(req);
183 return rc;
184 }
185
186 static void scsi_req_dequeue(SCSIRequest *req)
187 {
188 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
189 if (req->enqueued) {
190 QTAILQ_REMOVE(&req->dev->requests, req, next);
191 req->enqueued = false;
192 scsi_req_unref(req);
193 }
194 }
195
196 static int scsi_req_length(SCSIRequest *req, uint8_t *cmd)
197 {
198 switch (cmd[0] >> 5) {
199 case 0:
200 req->cmd.xfer = cmd[4];
201 req->cmd.len = 6;
202 /* length 0 means 256 blocks */
203 if (req->cmd.xfer == 0)
204 req->cmd.xfer = 256;
205 break;
206 case 1:
207 case 2:
208 req->cmd.xfer = cmd[8] | (cmd[7] << 8);
209 req->cmd.len = 10;
210 break;
211 case 4:
212 req->cmd.xfer = cmd[13] | (cmd[12] << 8) | (cmd[11] << 16) | (cmd[10] << 24);
213 req->cmd.len = 16;
214 break;
215 case 5:
216 req->cmd.xfer = cmd[9] | (cmd[8] << 8) | (cmd[7] << 16) | (cmd[6] << 24);
217 req->cmd.len = 12;
218 break;
219 default:
220 trace_scsi_req_parse_bad(req->dev->id, req->lun, req->tag, cmd[0]);
221 return -1;
222 }
223
224 switch(cmd[0]) {
225 case TEST_UNIT_READY:
226 case REWIND:
227 case START_STOP:
228 case SEEK_6:
229 case WRITE_FILEMARKS:
230 case SPACE:
231 case RESERVE:
232 case RELEASE:
233 case ERASE:
234 case ALLOW_MEDIUM_REMOVAL:
235 case VERIFY_10:
236 case SEEK_10:
237 case SYNCHRONIZE_CACHE:
238 case LOCK_UNLOCK_CACHE:
239 case LOAD_UNLOAD:
240 case SET_CD_SPEED:
241 case SET_LIMITS:
242 case WRITE_LONG_10:
243 case MOVE_MEDIUM:
244 case UPDATE_BLOCK:
245 req->cmd.xfer = 0;
246 break;
247 case MODE_SENSE:
248 break;
249 case WRITE_SAME_10:
250 req->cmd.xfer = 1;
251 break;
252 case READ_CAPACITY_10:
253 req->cmd.xfer = 8;
254 break;
255 case READ_BLOCK_LIMITS:
256 req->cmd.xfer = 6;
257 break;
258 case READ_POSITION:
259 req->cmd.xfer = 20;
260 break;
261 case SEND_VOLUME_TAG:
262 req->cmd.xfer *= 40;
263 break;
264 case MEDIUM_SCAN:
265 req->cmd.xfer *= 8;
266 break;
267 case WRITE_10:
268 case WRITE_VERIFY_10:
269 case WRITE_6:
270 case WRITE_12:
271 case WRITE_VERIFY_12:
272 case WRITE_16:
273 case WRITE_VERIFY_16:
274 req->cmd.xfer *= req->dev->blocksize;
275 break;
276 case READ_10:
277 case READ_6:
278 case READ_REVERSE:
279 case RECOVER_BUFFERED_DATA:
280 case READ_12:
281 case READ_16:
282 req->cmd.xfer *= req->dev->blocksize;
283 break;
284 case INQUIRY:
285 req->cmd.xfer = cmd[4] | (cmd[3] << 8);
286 break;
287 case MAINTENANCE_OUT:
288 case MAINTENANCE_IN:
289 if (req->dev->type == TYPE_ROM) {
290 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
291 req->cmd.xfer = cmd[9] | (cmd[8] << 8);
292 }
293 break;
294 }
295 return 0;
296 }
297
298 static int scsi_req_stream_length(SCSIRequest *req, uint8_t *cmd)
299 {
300 switch(cmd[0]) {
301 /* stream commands */
302 case READ_6:
303 case READ_REVERSE:
304 case RECOVER_BUFFERED_DATA:
305 case WRITE_6:
306 req->cmd.len = 6;
307 req->cmd.xfer = cmd[4] | (cmd[3] << 8) | (cmd[2] << 16);
308 if (cmd[1] & 0x01) /* fixed */
309 req->cmd.xfer *= req->dev->blocksize;
310 break;
311 case REWIND:
312 case START_STOP:
313 req->cmd.len = 6;
314 req->cmd.xfer = 0;
315 break;
316 /* generic commands */
317 default:
318 return scsi_req_length(req, cmd);
319 }
320 return 0;
321 }
322
323 static void scsi_req_xfer_mode(SCSIRequest *req)
324 {
325 switch (req->cmd.buf[0]) {
326 case WRITE_6:
327 case WRITE_10:
328 case WRITE_VERIFY_10:
329 case WRITE_12:
330 case WRITE_VERIFY_12:
331 case WRITE_16:
332 case WRITE_VERIFY_16:
333 case COPY:
334 case COPY_VERIFY:
335 case COMPARE:
336 case CHANGE_DEFINITION:
337 case LOG_SELECT:
338 case MODE_SELECT:
339 case MODE_SELECT_10:
340 case SEND_DIAGNOSTIC:
341 case WRITE_BUFFER:
342 case FORMAT_UNIT:
343 case REASSIGN_BLOCKS:
344 case SEARCH_EQUAL:
345 case SEARCH_HIGH:
346 case SEARCH_LOW:
347 case UPDATE_BLOCK:
348 case WRITE_LONG_10:
349 case WRITE_SAME_10:
350 case SEARCH_HIGH_12:
351 case SEARCH_EQUAL_12:
352 case SEARCH_LOW_12:
353 case MEDIUM_SCAN:
354 case SEND_VOLUME_TAG:
355 case PERSISTENT_RESERVE_OUT:
356 case MAINTENANCE_OUT:
357 req->cmd.mode = SCSI_XFER_TO_DEV;
358 break;
359 default:
360 if (req->cmd.xfer)
361 req->cmd.mode = SCSI_XFER_FROM_DEV;
362 else {
363 req->cmd.mode = SCSI_XFER_NONE;
364 }
365 break;
366 }
367 }
368
369 static uint64_t scsi_req_lba(SCSIRequest *req)
370 {
371 uint8_t *buf = req->cmd.buf;
372 uint64_t lba;
373
374 switch (buf[0] >> 5) {
375 case 0:
376 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
377 (((uint64_t) buf[1] & 0x1f) << 16);
378 break;
379 case 1:
380 case 2:
381 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
382 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
383 break;
384 case 4:
385 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
386 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
387 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
388 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
389 break;
390 case 5:
391 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
392 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
393 break;
394 default:
395 lba = -1;
396
397 }
398 return lba;
399 }
400
401 int scsi_req_parse(SCSIRequest *req, uint8_t *buf)
402 {
403 int rc;
404
405 if (req->dev->type == TYPE_TAPE) {
406 rc = scsi_req_stream_length(req, buf);
407 } else {
408 rc = scsi_req_length(req, buf);
409 }
410 if (rc != 0)
411 return rc;
412
413 memcpy(req->cmd.buf, buf, req->cmd.len);
414 scsi_req_xfer_mode(req);
415 req->cmd.lba = scsi_req_lba(req);
416 trace_scsi_req_parsed(req->dev->id, req->lun, req->tag, buf[0],
417 req->cmd.mode, req->cmd.xfer);
418 if (req->cmd.lba != -1) {
419 trace_scsi_req_parsed_lba(req->dev->id, req->lun, req->tag, buf[0],
420 req->cmd.lba);
421 }
422 return 0;
423 }
424
425 /*
426 * Predefined sense codes
427 */
428
429 /* No sense data available */
430 const struct SCSISense sense_code_NO_SENSE = {
431 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
432 };
433
434 /* LUN not ready, Manual intervention required */
435 const struct SCSISense sense_code_LUN_NOT_READY = {
436 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
437 };
438
439 /* LUN not ready, Medium not present */
440 const struct SCSISense sense_code_NO_MEDIUM = {
441 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
442 };
443
444 /* Hardware error, internal target failure */
445 const struct SCSISense sense_code_TARGET_FAILURE = {
446 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
447 };
448
449 /* Illegal request, invalid command operation code */
450 const struct SCSISense sense_code_INVALID_OPCODE = {
451 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
452 };
453
454 /* Illegal request, LBA out of range */
455 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
456 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
457 };
458
459 /* Illegal request, Invalid field in CDB */
460 const struct SCSISense sense_code_INVALID_FIELD = {
461 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
462 };
463
464 /* Illegal request, LUN not supported */
465 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
466 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
467 };
468
469 /* Command aborted, I/O process terminated */
470 const struct SCSISense sense_code_IO_ERROR = {
471 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
472 };
473
474 /* Command aborted, I_T Nexus loss occurred */
475 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
476 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
477 };
478
479 /* Command aborted, Logical Unit failure */
480 const struct SCSISense sense_code_LUN_FAILURE = {
481 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
482 };
483
484 /*
485 * scsi_build_sense
486 *
487 * Build a sense buffer
488 */
489 int scsi_build_sense(SCSISense sense, uint8_t *buf, int len, int fixed)
490 {
491 if (!fixed && len < 8) {
492 return 0;
493 }
494
495 memset(buf, 0, len);
496 if (fixed) {
497 /* Return fixed format sense buffer */
498 buf[0] = 0xf0;
499 buf[2] = sense.key;
500 buf[7] = 7;
501 buf[12] = sense.asc;
502 buf[13] = sense.ascq;
503 return MIN(len, 18);
504 } else {
505 /* Return descriptor format sense buffer */
506 buf[0] = 0x72;
507 buf[1] = sense.key;
508 buf[2] = sense.asc;
509 buf[3] = sense.ascq;
510 return 8;
511 }
512 }
513
514 static const char *scsi_command_name(uint8_t cmd)
515 {
516 static const char *names[] = {
517 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
518 [ REWIND ] = "REWIND",
519 [ REQUEST_SENSE ] = "REQUEST_SENSE",
520 [ FORMAT_UNIT ] = "FORMAT_UNIT",
521 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
522 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
523 [ READ_6 ] = "READ_6",
524 [ WRITE_6 ] = "WRITE_6",
525 [ SEEK_6 ] = "SEEK_6",
526 [ READ_REVERSE ] = "READ_REVERSE",
527 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
528 [ SPACE ] = "SPACE",
529 [ INQUIRY ] = "INQUIRY",
530 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
531 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
532 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
533 [ MODE_SELECT ] = "MODE_SELECT",
534 [ RESERVE ] = "RESERVE",
535 [ RELEASE ] = "RELEASE",
536 [ COPY ] = "COPY",
537 [ ERASE ] = "ERASE",
538 [ MODE_SENSE ] = "MODE_SENSE",
539 [ START_STOP ] = "START_STOP",
540 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
541 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
542 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
543 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
544 [ READ_10 ] = "READ_10",
545 [ WRITE_10 ] = "WRITE_10",
546 [ SEEK_10 ] = "SEEK_10",
547 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
548 [ VERIFY_10 ] = "VERIFY_10",
549 [ SEARCH_HIGH ] = "SEARCH_HIGH",
550 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
551 [ SEARCH_LOW ] = "SEARCH_LOW",
552 [ SET_LIMITS ] = "SET_LIMITS",
553 [ PRE_FETCH ] = "PRE_FETCH",
554 /* READ_POSITION and PRE_FETCH use the same operation code */
555 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
556 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
557 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
558 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
559 [ COMPARE ] = "COMPARE",
560 [ COPY_VERIFY ] = "COPY_VERIFY",
561 [ WRITE_BUFFER ] = "WRITE_BUFFER",
562 [ READ_BUFFER ] = "READ_BUFFER",
563 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
564 [ READ_LONG_10 ] = "READ_LONG_10",
565 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
566 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
567 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
568 [ UNMAP ] = "UNMAP",
569 [ READ_TOC ] = "READ_TOC",
570 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
571 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
572 [ LOG_SELECT ] = "LOG_SELECT",
573 [ LOG_SENSE ] = "LOG_SENSE",
574 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
575 [ RESERVE_10 ] = "RESERVE_10",
576 [ RELEASE_10 ] = "RELEASE_10",
577 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
578 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
579 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
580 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
581 [ EXTENDED_COPY ] = "EXTENDED_COPY",
582 [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
583 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
584 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
585 [ READ_16 ] = "READ_16",
586 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
587 [ WRITE_16 ] = "WRITE_16",
588 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
589 [ VERIFY_16 ] = "VERIFY_16",
590 [ SYNCHRONIZE_CACHE_16 ] = "SYNCHRONIZE_CACHE_16",
591 [ LOCATE_16 ] = "LOCATE_16",
592 [ WRITE_SAME_16 ] = "WRITE_SAME_16",
593 [ ERASE_16 ] = "ERASE_16",
594 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
595 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
596 [ REPORT_LUNS ] = "REPORT_LUNS",
597 [ BLANK ] = "BLANK",
598 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
599 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
600 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
601 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
602 [ READ_12 ] = "READ_12",
603 [ WRITE_12 ] = "WRITE_12",
604 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
605 [ VERIFY_12 ] = "VERIFY_12",
606 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
607 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
608 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
609 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
610 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
611 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
612 [ SET_CD_SPEED ] = "SET_CD_SPEED",
613 };
614
615 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
616 return "*UNKNOWN*";
617 return names[cmd];
618 }
619
620 SCSIRequest *scsi_req_ref(SCSIRequest *req)
621 {
622 req->refcount++;
623 return req;
624 }
625
626 void scsi_req_unref(SCSIRequest *req)
627 {
628 if (--req->refcount == 0) {
629 if (req->dev->info->free_req) {
630 req->dev->info->free_req(req);
631 }
632 qemu_free(req);
633 }
634 }
635
636 /* Tell the device that we finished processing this chunk of I/O. It
637 will start the next chunk or complete the command. */
638 void scsi_req_continue(SCSIRequest *req)
639 {
640 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
641 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
642 req->dev->info->write_data(req);
643 } else {
644 req->dev->info->read_data(req);
645 }
646 }
647
648 /* Called by the devices when data is ready for the HBA. The HBA should
649 start a DMA operation to read or fill the device's data buffer.
650 Once it completes, calling scsi_req_continue will restart I/O. */
651 void scsi_req_data(SCSIRequest *req, int len)
652 {
653 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
654 req->bus->ops->transfer_data(req, len);
655 }
656
657 void scsi_req_print(SCSIRequest *req)
658 {
659 FILE *fp = stderr;
660 int i;
661
662 fprintf(fp, "[%s id=%d] %s",
663 req->dev->qdev.parent_bus->name,
664 req->dev->id,
665 scsi_command_name(req->cmd.buf[0]));
666 for (i = 1; i < req->cmd.len; i++) {
667 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
668 }
669 switch (req->cmd.mode) {
670 case SCSI_XFER_NONE:
671 fprintf(fp, " - none\n");
672 break;
673 case SCSI_XFER_FROM_DEV:
674 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
675 break;
676 case SCSI_XFER_TO_DEV:
677 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
678 break;
679 default:
680 fprintf(fp, " - Oops\n");
681 break;
682 }
683 }
684
685 void scsi_req_complete(SCSIRequest *req)
686 {
687 assert(req->status != -1);
688 scsi_req_ref(req);
689 scsi_req_dequeue(req);
690 req->bus->ops->complete(req, req->status);
691 scsi_req_unref(req);
692 }
693
694 void scsi_req_cancel(SCSIRequest *req)
695 {
696 if (req->dev && req->dev->info->cancel_io) {
697 req->dev->info->cancel_io(req);
698 }
699 scsi_req_ref(req);
700 scsi_req_dequeue(req);
701 if (req->bus->ops->cancel) {
702 req->bus->ops->cancel(req);
703 }
704 scsi_req_unref(req);
705 }
706
707 void scsi_req_abort(SCSIRequest *req, int status)
708 {
709 req->status = status;
710 if (req->dev && req->dev->info->cancel_io) {
711 req->dev->info->cancel_io(req);
712 }
713 scsi_req_complete(req);
714 }
715
716 void scsi_device_purge_requests(SCSIDevice *sdev)
717 {
718 SCSIRequest *req;
719
720 while (!QTAILQ_EMPTY(&sdev->requests)) {
721 req = QTAILQ_FIRST(&sdev->requests);
722 scsi_req_cancel(req);
723 }
724 }
725
726 static char *scsibus_get_fw_dev_path(DeviceState *dev)
727 {
728 SCSIDevice *d = (SCSIDevice*)dev;
729 SCSIBus *bus = scsi_bus_from_device(d);
730 char path[100];
731 int i;
732
733 for (i = 0; i < bus->ndev; i++) {
734 if (bus->devs[i] == d) {
735 break;
736 }
737 }
738
739 assert(i != bus->ndev);
740
741 snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
742
743 return strdup(path);
744 }