Add access control support to qemu bridge helper
[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 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
11 static void scsi_req_dequeue(SCSIRequest *req);
12
13 static struct BusInfo scsi_bus_info = {
14 .name = "SCSI",
15 .size = sizeof(SCSIBus),
16 .get_fw_dev_path = scsibus_get_fw_dev_path,
17 .props = (Property[]) {
18 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
19 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
20 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
21 DEFINE_PROP_END_OF_LIST(),
22 },
23 };
24 static int next_scsi_bus;
25
26 static int scsi_device_init(SCSIDevice *s)
27 {
28 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
29 if (sc->init) {
30 return sc->init(s);
31 }
32 return 0;
33 }
34
35 static void scsi_device_destroy(SCSIDevice *s)
36 {
37 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
38 if (sc->destroy) {
39 sc->destroy(s);
40 }
41 }
42
43 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
44 uint8_t *buf, void *hba_private)
45 {
46 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
47 if (sc->alloc_req) {
48 return sc->alloc_req(s, tag, lun, buf, hba_private);
49 }
50
51 return NULL;
52 }
53
54 static void scsi_device_unit_attention_reported(SCSIDevice *s)
55 {
56 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
57 if (sc->unit_attention_reported) {
58 sc->unit_attention_reported(s);
59 }
60 }
61
62 /* Create a scsi bus, and attach devices to it. */
63 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info)
64 {
65 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
66 bus->busnr = next_scsi_bus++;
67 bus->info = info;
68 bus->qbus.allow_hotplug = 1;
69 }
70
71 static void scsi_dma_restart_bh(void *opaque)
72 {
73 SCSIDevice *s = opaque;
74 SCSIRequest *req, *next;
75
76 qemu_bh_delete(s->bh);
77 s->bh = NULL;
78
79 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
80 scsi_req_ref(req);
81 if (req->retry) {
82 req->retry = false;
83 switch (req->cmd.mode) {
84 case SCSI_XFER_FROM_DEV:
85 case SCSI_XFER_TO_DEV:
86 scsi_req_continue(req);
87 break;
88 case SCSI_XFER_NONE:
89 scsi_req_dequeue(req);
90 scsi_req_enqueue(req);
91 break;
92 }
93 }
94 scsi_req_unref(req);
95 }
96 }
97
98 void scsi_req_retry(SCSIRequest *req)
99 {
100 /* No need to save a reference, because scsi_dma_restart_bh just
101 * looks at the request list. */
102 req->retry = true;
103 }
104
105 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
106 {
107 SCSIDevice *s = opaque;
108
109 if (!running) {
110 return;
111 }
112 if (!s->bh) {
113 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
114 qemu_bh_schedule(s->bh);
115 }
116 }
117
118 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
119 {
120 SCSIDevice *dev = SCSI_DEVICE(qdev);
121 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
122 SCSIDevice *d;
123 int rc = -1;
124
125 if (dev->channel > bus->info->max_channel) {
126 error_report("bad scsi channel id: %d", dev->channel);
127 goto err;
128 }
129 if (dev->id != -1 && dev->id > bus->info->max_target) {
130 error_report("bad scsi device id: %d", dev->id);
131 goto err;
132 }
133
134 if (dev->id == -1) {
135 int id = -1;
136 if (dev->lun == -1) {
137 dev->lun = 0;
138 }
139 do {
140 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
141 } while (d && d->lun == dev->lun && id <= bus->info->max_target);
142 if (id > bus->info->max_target) {
143 error_report("no free target");
144 goto err;
145 }
146 dev->id = id;
147 } else if (dev->lun == -1) {
148 int lun = -1;
149 do {
150 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
151 } while (d && d->lun == lun && lun < bus->info->max_lun);
152 if (lun > bus->info->max_lun) {
153 error_report("no free lun");
154 goto err;
155 }
156 dev->lun = lun;
157 } else {
158 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
159 if (dev->lun == d->lun && dev != d) {
160 qdev_free(&d->qdev);
161 }
162 }
163
164 QTAILQ_INIT(&dev->requests);
165 rc = scsi_device_init(dev);
166 if (rc == 0) {
167 dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
168 dev);
169 }
170
171 err:
172 return rc;
173 }
174
175 static int scsi_qdev_exit(DeviceState *qdev)
176 {
177 SCSIDevice *dev = SCSI_DEVICE(qdev);
178
179 if (dev->vmsentry) {
180 qemu_del_vm_change_state_handler(dev->vmsentry);
181 }
182 scsi_device_destroy(dev);
183 return 0;
184 }
185
186 void scsi_qdev_register(DeviceInfo *info)
187 {
188 info->bus_info = &scsi_bus_info;
189 info->init = scsi_qdev_init;
190 info->unplug = qdev_simple_unplug_cb;
191 info->exit = scsi_qdev_exit;
192 qdev_register_subclass(info, TYPE_SCSI_DEVICE);
193 }
194
195 /* handle legacy '-drive if=scsi,...' cmd line args */
196 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
197 int unit, bool removable, int bootindex)
198 {
199 const char *driver;
200 DeviceState *dev;
201
202 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
203 dev = qdev_create(&bus->qbus, driver);
204 qdev_prop_set_uint32(dev, "scsi-id", unit);
205 if (bootindex >= 0) {
206 qdev_prop_set_int32(dev, "bootindex", bootindex);
207 }
208 if (qdev_prop_exists(dev, "removable")) {
209 qdev_prop_set_bit(dev, "removable", removable);
210 }
211 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
212 qdev_free(dev);
213 return NULL;
214 }
215 if (qdev_init(dev) < 0)
216 return NULL;
217 return SCSI_DEVICE(dev);
218 }
219
220 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
221 {
222 Location loc;
223 DriveInfo *dinfo;
224 int res = 0, unit;
225
226 loc_push_none(&loc);
227 for (unit = 0; unit < bus->info->max_target; unit++) {
228 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
229 if (dinfo == NULL) {
230 continue;
231 }
232 qemu_opts_loc_restore(dinfo->opts);
233 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) {
234 res = -1;
235 break;
236 }
237 }
238 loc_pop(&loc);
239 return res;
240 }
241
242 /* SCSIReqOps implementation for invalid commands. */
243
244 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
245 {
246 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
247 scsi_req_complete(req, CHECK_CONDITION);
248 return 0;
249 }
250
251 static const struct SCSIReqOps reqops_invalid_opcode = {
252 .size = sizeof(SCSIRequest),
253 .send_command = scsi_invalid_command
254 };
255
256 /* SCSIReqOps implementation for unit attention conditions. */
257
258 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
259 {
260 if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
261 scsi_req_build_sense(req, req->dev->unit_attention);
262 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
263 scsi_req_build_sense(req, req->bus->unit_attention);
264 }
265 scsi_req_complete(req, CHECK_CONDITION);
266 return 0;
267 }
268
269 static const struct SCSIReqOps reqops_unit_attention = {
270 .size = sizeof(SCSIRequest),
271 .send_command = scsi_unit_attention
272 };
273
274 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
275 an invalid LUN. */
276
277 typedef struct SCSITargetReq SCSITargetReq;
278
279 struct SCSITargetReq {
280 SCSIRequest req;
281 int len;
282 uint8_t buf[2056];
283 };
284
285 static void store_lun(uint8_t *outbuf, int lun)
286 {
287 if (lun < 256) {
288 outbuf[1] = lun;
289 return;
290 }
291 outbuf[1] = (lun & 255);
292 outbuf[0] = (lun >> 8) | 0x40;
293 }
294
295 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
296 {
297 DeviceState *qdev;
298 int i, len, n;
299 int channel, id;
300 bool found_lun0;
301
302 if (r->req.cmd.xfer < 16) {
303 return false;
304 }
305 if (r->req.cmd.buf[2] > 2) {
306 return false;
307 }
308 channel = r->req.dev->channel;
309 id = r->req.dev->id;
310 found_lun0 = false;
311 n = 0;
312 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
313 SCSIDevice *dev = SCSI_DEVICE(qdev);
314
315 if (dev->channel == channel && dev->id == id) {
316 if (dev->lun == 0) {
317 found_lun0 = true;
318 }
319 n += 8;
320 }
321 }
322 if (!found_lun0) {
323 n += 8;
324 }
325 len = MIN(n + 8, r->req.cmd.xfer & ~7);
326 if (len > sizeof(r->buf)) {
327 /* TODO: > 256 LUNs? */
328 return false;
329 }
330
331 memset(r->buf, 0, len);
332 stl_be_p(&r->buf, n);
333 i = found_lun0 ? 8 : 16;
334 QTAILQ_FOREACH(qdev, &r->req.bus->qbus.children, sibling) {
335 SCSIDevice *dev = SCSI_DEVICE(qdev);
336
337 if (dev->channel == channel && dev->id == id) {
338 store_lun(&r->buf[i], dev->lun);
339 i += 8;
340 }
341 }
342 assert(i == n + 8);
343 r->len = len;
344 return true;
345 }
346
347 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
348 {
349 assert(r->req.dev->lun != r->req.lun);
350 if (r->req.cmd.buf[1] & 0x2) {
351 /* Command support data - optional, not implemented */
352 return false;
353 }
354
355 if (r->req.cmd.buf[1] & 0x1) {
356 /* Vital product data */
357 uint8_t page_code = r->req.cmd.buf[2];
358 if (r->req.cmd.xfer < 4) {
359 return false;
360 }
361
362 r->buf[r->len++] = page_code ; /* this page */
363 r->buf[r->len++] = 0x00;
364
365 switch (page_code) {
366 case 0x00: /* Supported page codes, mandatory */
367 {
368 int pages;
369 pages = r->len++;
370 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
371 r->buf[pages] = r->len - pages - 1; /* number of pages */
372 break;
373 }
374 default:
375 return false;
376 }
377 /* done with EVPD */
378 assert(r->len < sizeof(r->buf));
379 r->len = MIN(r->req.cmd.xfer, r->len);
380 return true;
381 }
382
383 /* Standard INQUIRY data */
384 if (r->req.cmd.buf[2] != 0) {
385 return false;
386 }
387
388 /* PAGE CODE == 0 */
389 if (r->req.cmd.xfer < 5) {
390 return -1;
391 }
392
393 r->len = MIN(r->req.cmd.xfer, 36);
394 memset(r->buf, 0, r->len);
395 if (r->req.lun != 0) {
396 r->buf[0] = TYPE_NO_LUN;
397 } else {
398 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
399 r->buf[2] = 5; /* Version */
400 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
401 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
402 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
403 memcpy(&r->buf[8], "QEMU ", 8);
404 memcpy(&r->buf[16], "QEMU TARGET ", 16);
405 strncpy((char *) &r->buf[32], QEMU_VERSION, 4);
406 }
407 return true;
408 }
409
410 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
411 {
412 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
413
414 switch (buf[0]) {
415 case REPORT_LUNS:
416 if (!scsi_target_emulate_report_luns(r)) {
417 goto illegal_request;
418 }
419 break;
420 case INQUIRY:
421 if (!scsi_target_emulate_inquiry(r)) {
422 goto illegal_request;
423 }
424 break;
425 case REQUEST_SENSE:
426 if (req->cmd.xfer < 4) {
427 goto illegal_request;
428 }
429 r->len = scsi_device_get_sense(r->req.dev, r->buf,
430 MIN(req->cmd.xfer, sizeof r->buf),
431 (req->cmd.buf[1] & 1) == 0);
432 if (r->req.dev->sense_is_ua) {
433 scsi_device_unit_attention_reported(req->dev);
434 r->req.dev->sense_len = 0;
435 r->req.dev->sense_is_ua = false;
436 }
437 break;
438 default:
439 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
440 scsi_req_complete(req, CHECK_CONDITION);
441 return 0;
442 illegal_request:
443 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
444 scsi_req_complete(req, CHECK_CONDITION);
445 return 0;
446 }
447
448 if (!r->len) {
449 scsi_req_complete(req, GOOD);
450 }
451 return r->len;
452 }
453
454 static void scsi_target_read_data(SCSIRequest *req)
455 {
456 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
457 uint32_t n;
458
459 n = r->len;
460 if (n > 0) {
461 r->len = 0;
462 scsi_req_data(&r->req, n);
463 } else {
464 scsi_req_complete(&r->req, GOOD);
465 }
466 }
467
468 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
469 {
470 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
471
472 return r->buf;
473 }
474
475 static const struct SCSIReqOps reqops_target_command = {
476 .size = sizeof(SCSITargetReq),
477 .send_command = scsi_target_send_command,
478 .read_data = scsi_target_read_data,
479 .get_buf = scsi_target_get_buf,
480 };
481
482
483 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
484 uint32_t tag, uint32_t lun, void *hba_private)
485 {
486 SCSIRequest *req;
487
488 req = g_malloc0(reqops->size);
489 req->refcount = 1;
490 req->bus = scsi_bus_from_device(d);
491 req->dev = d;
492 req->tag = tag;
493 req->lun = lun;
494 req->hba_private = hba_private;
495 req->status = -1;
496 req->sense_len = 0;
497 req->ops = reqops;
498 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
499 return req;
500 }
501
502 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
503 uint8_t *buf, void *hba_private)
504 {
505 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
506 SCSIRequest *req;
507 SCSICommand cmd;
508
509 if (scsi_req_parse(&cmd, d, buf) != 0) {
510 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
511 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
512 } else {
513 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
514 cmd.mode, cmd.xfer);
515 if (cmd.lba != -1) {
516 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
517 cmd.lba);
518 }
519
520 if ((d->unit_attention.key == UNIT_ATTENTION ||
521 bus->unit_attention.key == UNIT_ATTENTION) &&
522 (buf[0] != INQUIRY &&
523 buf[0] != REPORT_LUNS &&
524 buf[0] != GET_CONFIGURATION &&
525 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
526
527 /*
528 * If we already have a pending unit attention condition,
529 * report this one before triggering another one.
530 */
531 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
532 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
533 hba_private);
534 } else if (lun != d->lun ||
535 buf[0] == REPORT_LUNS ||
536 (buf[0] == REQUEST_SENSE && (d->sense_len || cmd.xfer < 4))) {
537 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
538 hba_private);
539 } else {
540 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
541 }
542 }
543
544 req->cmd = cmd;
545 switch (buf[0]) {
546 case INQUIRY:
547 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
548 break;
549 case TEST_UNIT_READY:
550 trace_scsi_test_unit_ready(d->id, lun, tag);
551 break;
552 case REPORT_LUNS:
553 trace_scsi_report_luns(d->id, lun, tag);
554 break;
555 case REQUEST_SENSE:
556 trace_scsi_request_sense(d->id, lun, tag);
557 break;
558 default:
559 break;
560 }
561
562 return req;
563 }
564
565 uint8_t *scsi_req_get_buf(SCSIRequest *req)
566 {
567 return req->ops->get_buf(req);
568 }
569
570 static void scsi_clear_unit_attention(SCSIRequest *req)
571 {
572 SCSISense *ua;
573 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
574 req->bus->unit_attention.key != UNIT_ATTENTION) {
575 return;
576 }
577
578 /*
579 * If an INQUIRY command enters the enabled command state,
580 * the device server shall [not] clear any unit attention condition;
581 * See also MMC-6, paragraphs 6.5 and 6.6.2.
582 */
583 if (req->cmd.buf[0] == INQUIRY ||
584 req->cmd.buf[0] == GET_CONFIGURATION ||
585 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
586 return;
587 }
588
589 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
590 ua = &req->dev->unit_attention;
591 } else {
592 ua = &req->bus->unit_attention;
593 }
594
595 /*
596 * If a REPORT LUNS command enters the enabled command state, [...]
597 * the device server shall clear any pending unit attention condition
598 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
599 */
600 if (req->cmd.buf[0] == REPORT_LUNS &&
601 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
602 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
603 return;
604 }
605
606 *ua = SENSE_CODE(NO_SENSE);
607 }
608
609 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
610 {
611 int ret;
612
613 assert(len >= 14);
614 if (!req->sense_len) {
615 return 0;
616 }
617
618 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
619
620 /*
621 * FIXME: clearing unit attention conditions upon autosense should be done
622 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
623 * (SAM-5, 5.14).
624 *
625 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
626 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
627 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
628 */
629 if (req->dev->sense_is_ua) {
630 scsi_device_unit_attention_reported(req->dev);
631 req->dev->sense_len = 0;
632 req->dev->sense_is_ua = false;
633 }
634 return ret;
635 }
636
637 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
638 {
639 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
640 }
641
642 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
643 {
644 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
645 sense.key, sense.asc, sense.ascq);
646 memset(req->sense, 0, 18);
647 req->sense[0] = 0xf0;
648 req->sense[2] = sense.key;
649 req->sense[7] = 10;
650 req->sense[12] = sense.asc;
651 req->sense[13] = sense.ascq;
652 req->sense_len = 18;
653 }
654
655 int32_t scsi_req_enqueue(SCSIRequest *req)
656 {
657 int32_t rc;
658
659 assert(!req->enqueued);
660 scsi_req_ref(req);
661 req->enqueued = true;
662 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
663
664 scsi_req_ref(req);
665 rc = req->ops->send_command(req, req->cmd.buf);
666 scsi_req_unref(req);
667 return rc;
668 }
669
670 static void scsi_req_dequeue(SCSIRequest *req)
671 {
672 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
673 req->retry = false;
674 if (req->enqueued) {
675 QTAILQ_REMOVE(&req->dev->requests, req, next);
676 req->enqueued = false;
677 scsi_req_unref(req);
678 }
679 }
680
681 static int scsi_get_performance_length(int num_desc, int type, int data_type)
682 {
683 /* MMC-6, paragraph 6.7. */
684 switch (type) {
685 case 0:
686 if ((data_type & 3) == 0) {
687 /* Each descriptor is as in Table 295 - Nominal performance. */
688 return 16 * num_desc + 8;
689 } else {
690 /* Each descriptor is as in Table 296 - Exceptions. */
691 return 6 * num_desc + 8;
692 }
693 case 1:
694 case 4:
695 case 5:
696 return 8 * num_desc + 8;
697 case 2:
698 return 2048 * num_desc + 8;
699 case 3:
700 return 16 * num_desc + 8;
701 default:
702 return 8;
703 }
704 }
705
706 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
707 {
708 switch (buf[0] >> 5) {
709 case 0:
710 cmd->xfer = buf[4];
711 cmd->len = 6;
712 /* length 0 means 256 blocks */
713 if (cmd->xfer == 0) {
714 cmd->xfer = 256;
715 }
716 break;
717 case 1:
718 case 2:
719 cmd->xfer = lduw_be_p(&buf[7]);
720 cmd->len = 10;
721 break;
722 case 4:
723 cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL;
724 cmd->len = 16;
725 break;
726 case 5:
727 cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL;
728 cmd->len = 12;
729 break;
730 default:
731 return -1;
732 }
733
734 switch (buf[0]) {
735 case TEST_UNIT_READY:
736 case REWIND:
737 case START_STOP:
738 case SET_CAPACITY:
739 case WRITE_FILEMARKS:
740 case WRITE_FILEMARKS_16:
741 case SPACE:
742 case RESERVE:
743 case RELEASE:
744 case ERASE:
745 case ALLOW_MEDIUM_REMOVAL:
746 case VERIFY_10:
747 case SEEK_10:
748 case SYNCHRONIZE_CACHE:
749 case SYNCHRONIZE_CACHE_16:
750 case LOCATE_16:
751 case LOCK_UNLOCK_CACHE:
752 case LOAD_UNLOAD:
753 case SET_CD_SPEED:
754 case SET_LIMITS:
755 case WRITE_LONG_10:
756 case MOVE_MEDIUM:
757 case UPDATE_BLOCK:
758 case RESERVE_TRACK:
759 case SET_READ_AHEAD:
760 case PRE_FETCH:
761 case PRE_FETCH_16:
762 case ALLOW_OVERWRITE:
763 cmd->xfer = 0;
764 break;
765 case MODE_SENSE:
766 break;
767 case WRITE_SAME_10:
768 cmd->xfer = 1;
769 break;
770 case READ_CAPACITY_10:
771 cmd->xfer = 8;
772 break;
773 case READ_BLOCK_LIMITS:
774 cmd->xfer = 6;
775 break;
776 case SEND_VOLUME_TAG:
777 /* GPCMD_SET_STREAMING from multimedia commands. */
778 if (dev->type == TYPE_ROM) {
779 cmd->xfer = buf[10] | (buf[9] << 8);
780 } else {
781 cmd->xfer = buf[9] | (buf[8] << 8);
782 }
783 break;
784 case WRITE_10:
785 case WRITE_VERIFY_10:
786 case WRITE_6:
787 case WRITE_12:
788 case WRITE_VERIFY_12:
789 case WRITE_16:
790 case WRITE_VERIFY_16:
791 cmd->xfer *= dev->blocksize;
792 break;
793 case READ_10:
794 case READ_6:
795 case READ_REVERSE:
796 case RECOVER_BUFFERED_DATA:
797 case READ_12:
798 case READ_16:
799 cmd->xfer *= dev->blocksize;
800 break;
801 case FORMAT_UNIT:
802 /* MMC mandates the parameter list to be 12-bytes long. Parameters
803 * for block devices are restricted to the header right now. */
804 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
805 cmd->xfer = 12;
806 } else {
807 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
808 }
809 break;
810 case INQUIRY:
811 case RECEIVE_DIAGNOSTIC:
812 case SEND_DIAGNOSTIC:
813 cmd->xfer = buf[4] | (buf[3] << 8);
814 break;
815 case READ_CD:
816 case READ_BUFFER:
817 case WRITE_BUFFER:
818 case SEND_CUE_SHEET:
819 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
820 break;
821 case PERSISTENT_RESERVE_OUT:
822 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
823 break;
824 case ERASE_12:
825 if (dev->type == TYPE_ROM) {
826 /* MMC command GET PERFORMANCE. */
827 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
828 buf[10], buf[1] & 0x1f);
829 }
830 break;
831 case MECHANISM_STATUS:
832 case READ_DVD_STRUCTURE:
833 case SEND_DVD_STRUCTURE:
834 case MAINTENANCE_OUT:
835 case MAINTENANCE_IN:
836 if (dev->type == TYPE_ROM) {
837 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
838 cmd->xfer = buf[9] | (buf[8] << 8);
839 }
840 break;
841 }
842 return 0;
843 }
844
845 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
846 {
847 switch (buf[0]) {
848 /* stream commands */
849 case ERASE_12:
850 case ERASE_16:
851 cmd->xfer = 0;
852 break;
853 case READ_6:
854 case READ_REVERSE:
855 case RECOVER_BUFFERED_DATA:
856 case WRITE_6:
857 cmd->len = 6;
858 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
859 if (buf[1] & 0x01) { /* fixed */
860 cmd->xfer *= dev->blocksize;
861 }
862 break;
863 case REWIND:
864 case START_STOP:
865 cmd->len = 6;
866 cmd->xfer = 0;
867 break;
868 case SPACE_16:
869 cmd->xfer = buf[13] | (buf[12] << 8);
870 break;
871 case READ_POSITION:
872 cmd->xfer = buf[8] | (buf[7] << 8);
873 break;
874 case FORMAT_UNIT:
875 cmd->xfer = buf[4] | (buf[3] << 8);
876 break;
877 /* generic commands */
878 default:
879 return scsi_req_length(cmd, dev, buf);
880 }
881 return 0;
882 }
883
884 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
885 {
886 switch (cmd->buf[0]) {
887 case WRITE_6:
888 case WRITE_10:
889 case WRITE_VERIFY_10:
890 case WRITE_12:
891 case WRITE_VERIFY_12:
892 case WRITE_16:
893 case WRITE_VERIFY_16:
894 case COPY:
895 case COPY_VERIFY:
896 case COMPARE:
897 case CHANGE_DEFINITION:
898 case LOG_SELECT:
899 case MODE_SELECT:
900 case MODE_SELECT_10:
901 case SEND_DIAGNOSTIC:
902 case WRITE_BUFFER:
903 case FORMAT_UNIT:
904 case REASSIGN_BLOCKS:
905 case SEARCH_EQUAL:
906 case SEARCH_HIGH:
907 case SEARCH_LOW:
908 case UPDATE_BLOCK:
909 case WRITE_LONG_10:
910 case WRITE_SAME_10:
911 case SEARCH_HIGH_12:
912 case SEARCH_EQUAL_12:
913 case SEARCH_LOW_12:
914 case MEDIUM_SCAN:
915 case SEND_VOLUME_TAG:
916 case SEND_CUE_SHEET:
917 case SEND_DVD_STRUCTURE:
918 case PERSISTENT_RESERVE_OUT:
919 case MAINTENANCE_OUT:
920 cmd->mode = SCSI_XFER_TO_DEV;
921 break;
922 default:
923 if (cmd->xfer)
924 cmd->mode = SCSI_XFER_FROM_DEV;
925 else {
926 cmd->mode = SCSI_XFER_NONE;
927 }
928 break;
929 }
930 }
931
932 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
933 {
934 uint8_t *buf = cmd->buf;
935 uint64_t lba;
936
937 switch (buf[0] >> 5) {
938 case 0:
939 lba = ldl_be_p(&buf[0]) & 0x1fffff;
940 break;
941 case 1:
942 case 2:
943 case 5:
944 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
945 break;
946 case 4:
947 lba = ldq_be_p(&buf[2]);
948 break;
949 default:
950 lba = -1;
951
952 }
953 return lba;
954 }
955
956 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
957 {
958 int rc;
959
960 if (dev->type == TYPE_TAPE) {
961 rc = scsi_req_stream_length(cmd, dev, buf);
962 } else {
963 rc = scsi_req_length(cmd, dev, buf);
964 }
965 if (rc != 0)
966 return rc;
967
968 memcpy(cmd->buf, buf, cmd->len);
969 scsi_cmd_xfer_mode(cmd);
970 cmd->lba = scsi_cmd_lba(cmd);
971 return 0;
972 }
973
974 /*
975 * Predefined sense codes
976 */
977
978 /* No sense data available */
979 const struct SCSISense sense_code_NO_SENSE = {
980 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
981 };
982
983 /* LUN not ready, Manual intervention required */
984 const struct SCSISense sense_code_LUN_NOT_READY = {
985 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
986 };
987
988 /* LUN not ready, Medium not present */
989 const struct SCSISense sense_code_NO_MEDIUM = {
990 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
991 };
992
993 /* LUN not ready, medium removal prevented */
994 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
995 .key = NOT_READY, .asc = 0x53, .ascq = 0x00
996 };
997
998 /* Hardware error, internal target failure */
999 const struct SCSISense sense_code_TARGET_FAILURE = {
1000 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
1001 };
1002
1003 /* Illegal request, invalid command operation code */
1004 const struct SCSISense sense_code_INVALID_OPCODE = {
1005 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
1006 };
1007
1008 /* Illegal request, LBA out of range */
1009 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
1010 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
1011 };
1012
1013 /* Illegal request, Invalid field in CDB */
1014 const struct SCSISense sense_code_INVALID_FIELD = {
1015 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
1016 };
1017
1018 /* Illegal request, LUN not supported */
1019 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
1020 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
1021 };
1022
1023 /* Illegal request, Saving parameters not supported */
1024 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
1025 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1026 };
1027
1028 /* Illegal request, Incompatible medium installed */
1029 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1030 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1031 };
1032
1033 /* Illegal request, medium removal prevented */
1034 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1035 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x00
1036 };
1037
1038 /* Command aborted, I/O process terminated */
1039 const struct SCSISense sense_code_IO_ERROR = {
1040 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1041 };
1042
1043 /* Command aborted, I_T Nexus loss occurred */
1044 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1045 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1046 };
1047
1048 /* Command aborted, Logical Unit failure */
1049 const struct SCSISense sense_code_LUN_FAILURE = {
1050 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1051 };
1052
1053 /* Unit attention, Power on, reset or bus device reset occurred */
1054 const struct SCSISense sense_code_RESET = {
1055 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1056 };
1057
1058 /* Unit attention, No medium */
1059 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1060 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1061 };
1062
1063 /* Unit attention, Medium may have changed */
1064 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1065 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1066 };
1067
1068 /* Unit attention, Reported LUNs data has changed */
1069 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1070 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1071 };
1072
1073 /* Unit attention, Device internal reset */
1074 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1075 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1076 };
1077
1078 /*
1079 * scsi_build_sense
1080 *
1081 * Convert between fixed and descriptor sense buffers
1082 */
1083 int scsi_build_sense(uint8_t *in_buf, int in_len,
1084 uint8_t *buf, int len, bool fixed)
1085 {
1086 bool fixed_in;
1087 SCSISense sense;
1088 if (!fixed && len < 8) {
1089 return 0;
1090 }
1091
1092 if (in_len == 0) {
1093 sense.key = NO_SENSE;
1094 sense.asc = 0;
1095 sense.ascq = 0;
1096 } else {
1097 fixed_in = (in_buf[0] & 2) == 0;
1098
1099 if (fixed == fixed_in) {
1100 memcpy(buf, in_buf, MIN(len, in_len));
1101 return MIN(len, in_len);
1102 }
1103
1104 if (fixed_in) {
1105 sense.key = in_buf[2];
1106 sense.asc = in_buf[12];
1107 sense.ascq = in_buf[13];
1108 } else {
1109 sense.key = in_buf[1];
1110 sense.asc = in_buf[2];
1111 sense.ascq = in_buf[3];
1112 }
1113 }
1114
1115 memset(buf, 0, len);
1116 if (fixed) {
1117 /* Return fixed format sense buffer */
1118 buf[0] = 0xf0;
1119 buf[2] = sense.key;
1120 buf[7] = 10;
1121 buf[12] = sense.asc;
1122 buf[13] = sense.ascq;
1123 return MIN(len, 18);
1124 } else {
1125 /* Return descriptor format sense buffer */
1126 buf[0] = 0x72;
1127 buf[1] = sense.key;
1128 buf[2] = sense.asc;
1129 buf[3] = sense.ascq;
1130 return 8;
1131 }
1132 }
1133
1134 static const char *scsi_command_name(uint8_t cmd)
1135 {
1136 static const char *names[] = {
1137 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
1138 [ REWIND ] = "REWIND",
1139 [ REQUEST_SENSE ] = "REQUEST_SENSE",
1140 [ FORMAT_UNIT ] = "FORMAT_UNIT",
1141 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
1142 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
1143 [ READ_6 ] = "READ_6",
1144 [ WRITE_6 ] = "WRITE_6",
1145 [ SET_CAPACITY ] = "SET_CAPACITY",
1146 [ READ_REVERSE ] = "READ_REVERSE",
1147 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
1148 [ SPACE ] = "SPACE",
1149 [ INQUIRY ] = "INQUIRY",
1150 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
1151 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
1152 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
1153 [ MODE_SELECT ] = "MODE_SELECT",
1154 [ RESERVE ] = "RESERVE",
1155 [ RELEASE ] = "RELEASE",
1156 [ COPY ] = "COPY",
1157 [ ERASE ] = "ERASE",
1158 [ MODE_SENSE ] = "MODE_SENSE",
1159 [ START_STOP ] = "START_STOP",
1160 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
1161 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
1162 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
1163 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
1164 [ READ_10 ] = "READ_10",
1165 [ WRITE_10 ] = "WRITE_10",
1166 [ SEEK_10 ] = "SEEK_10",
1167 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
1168 [ VERIFY_10 ] = "VERIFY_10",
1169 [ SEARCH_HIGH ] = "SEARCH_HIGH",
1170 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
1171 [ SEARCH_LOW ] = "SEARCH_LOW",
1172 [ SET_LIMITS ] = "SET_LIMITS",
1173 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
1174 /* READ_POSITION and PRE_FETCH use the same operation code */
1175 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
1176 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
1177 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
1178 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
1179 [ COMPARE ] = "COMPARE",
1180 [ COPY_VERIFY ] = "COPY_VERIFY",
1181 [ WRITE_BUFFER ] = "WRITE_BUFFER",
1182 [ READ_BUFFER ] = "READ_BUFFER",
1183 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
1184 [ READ_LONG_10 ] = "READ_LONG_10",
1185 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
1186 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
1187 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
1188 [ UNMAP ] = "UNMAP",
1189 [ READ_TOC ] = "READ_TOC",
1190 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
1191 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
1192 [ LOG_SELECT ] = "LOG_SELECT",
1193 [ LOG_SENSE ] = "LOG_SENSE",
1194 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
1195 [ RESERVE_10 ] = "RESERVE_10",
1196 [ RELEASE_10 ] = "RELEASE_10",
1197 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
1198 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
1199 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
1200 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
1201 [ EXTENDED_COPY ] = "EXTENDED_COPY",
1202 [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
1203 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
1204 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
1205 [ READ_16 ] = "READ_16",
1206 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
1207 [ WRITE_16 ] = "WRITE_16",
1208 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
1209 [ VERIFY_16 ] = "VERIFY_16",
1210 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
1211 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1212 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1213 [ LOCATE_16 ] = "LOCATE_16",
1214 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
1215 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1216 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
1217 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
1218 [ REPORT_LUNS ] = "REPORT_LUNS",
1219 [ BLANK ] = "BLANK",
1220 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
1221 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
1222 [ READ_12 ] = "READ_12",
1223 [ WRITE_12 ] = "WRITE_12",
1224 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
1225 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1226 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
1227 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
1228 [ VERIFY_12 ] = "VERIFY_12",
1229 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
1230 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
1231 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
1232 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
1233 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
1234 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1235 [ READ_CD ] = "READ_CD",
1236 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
1237 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
1238 [ RESERVE_TRACK ] = "RESERVE_TRACK",
1239 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
1240 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
1241 [ SET_CD_SPEED ] = "SET_CD_SPEED",
1242 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
1243 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
1244 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
1245 };
1246
1247 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1248 return "*UNKNOWN*";
1249 return names[cmd];
1250 }
1251
1252 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1253 {
1254 req->refcount++;
1255 return req;
1256 }
1257
1258 void scsi_req_unref(SCSIRequest *req)
1259 {
1260 if (--req->refcount == 0) {
1261 if (req->ops->free_req) {
1262 req->ops->free_req(req);
1263 }
1264 g_free(req);
1265 }
1266 }
1267
1268 /* Tell the device that we finished processing this chunk of I/O. It
1269 will start the next chunk or complete the command. */
1270 void scsi_req_continue(SCSIRequest *req)
1271 {
1272 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1273 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1274 req->ops->write_data(req);
1275 } else {
1276 req->ops->read_data(req);
1277 }
1278 }
1279
1280 /* Called by the devices when data is ready for the HBA. The HBA should
1281 start a DMA operation to read or fill the device's data buffer.
1282 Once it completes, calling scsi_req_continue will restart I/O. */
1283 void scsi_req_data(SCSIRequest *req, int len)
1284 {
1285 if (req->io_canceled) {
1286 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1287 } else {
1288 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1289 req->bus->info->transfer_data(req, len);
1290 }
1291 }
1292
1293 void scsi_req_print(SCSIRequest *req)
1294 {
1295 FILE *fp = stderr;
1296 int i;
1297
1298 fprintf(fp, "[%s id=%d] %s",
1299 req->dev->qdev.parent_bus->name,
1300 req->dev->id,
1301 scsi_command_name(req->cmd.buf[0]));
1302 for (i = 1; i < req->cmd.len; i++) {
1303 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1304 }
1305 switch (req->cmd.mode) {
1306 case SCSI_XFER_NONE:
1307 fprintf(fp, " - none\n");
1308 break;
1309 case SCSI_XFER_FROM_DEV:
1310 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1311 break;
1312 case SCSI_XFER_TO_DEV:
1313 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1314 break;
1315 default:
1316 fprintf(fp, " - Oops\n");
1317 break;
1318 }
1319 }
1320
1321 void scsi_req_complete(SCSIRequest *req, int status)
1322 {
1323 assert(req->status == -1);
1324 req->status = status;
1325
1326 assert(req->sense_len < sizeof(req->sense));
1327 if (status == GOOD) {
1328 req->sense_len = 0;
1329 }
1330
1331 if (req->sense_len) {
1332 memcpy(req->dev->sense, req->sense, req->sense_len);
1333 req->dev->sense_len = req->sense_len;
1334 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1335 } else {
1336 req->dev->sense_len = 0;
1337 req->dev->sense_is_ua = false;
1338 }
1339
1340 /*
1341 * Unit attention state is now stored in the device's sense buffer
1342 * if the HBA didn't do autosense. Clear the pending unit attention
1343 * flags.
1344 */
1345 scsi_clear_unit_attention(req);
1346
1347 scsi_req_ref(req);
1348 scsi_req_dequeue(req);
1349 req->bus->info->complete(req, req->status);
1350 scsi_req_unref(req);
1351 }
1352
1353 void scsi_req_cancel(SCSIRequest *req)
1354 {
1355 if (!req->enqueued) {
1356 return;
1357 }
1358 scsi_req_ref(req);
1359 scsi_req_dequeue(req);
1360 req->io_canceled = true;
1361 if (req->ops->cancel_io) {
1362 req->ops->cancel_io(req);
1363 }
1364 if (req->bus->info->cancel) {
1365 req->bus->info->cancel(req);
1366 }
1367 scsi_req_unref(req);
1368 }
1369
1370 void scsi_req_abort(SCSIRequest *req, int status)
1371 {
1372 if (!req->enqueued) {
1373 return;
1374 }
1375 scsi_req_ref(req);
1376 scsi_req_dequeue(req);
1377 req->io_canceled = true;
1378 if (req->ops->cancel_io) {
1379 req->ops->cancel_io(req);
1380 }
1381 scsi_req_complete(req, status);
1382 scsi_req_unref(req);
1383 }
1384
1385 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1386 {
1387 SCSIRequest *req;
1388
1389 while (!QTAILQ_EMPTY(&sdev->requests)) {
1390 req = QTAILQ_FIRST(&sdev->requests);
1391 scsi_req_cancel(req);
1392 }
1393 sdev->unit_attention = sense;
1394 }
1395
1396 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1397 {
1398 SCSIDevice *d = SCSI_DEVICE(dev);
1399 char path[100];
1400
1401 snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel,
1402 qdev_fw_name(dev), d->id, d->lun);
1403
1404 return strdup(path);
1405 }
1406
1407 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1408 {
1409 DeviceState *qdev;
1410 SCSIDevice *target_dev = NULL;
1411
1412 QTAILQ_FOREACH_REVERSE(qdev, &bus->qbus.children, ChildrenHead, sibling) {
1413 SCSIDevice *dev = SCSI_DEVICE(qdev);
1414
1415 if (dev->channel == channel && dev->id == id) {
1416 if (dev->lun == lun) {
1417 return dev;
1418 }
1419 target_dev = dev;
1420 }
1421 }
1422 return target_dev;
1423 }
1424
1425 static TypeInfo scsi_device_type_info = {
1426 .name = TYPE_SCSI_DEVICE,
1427 .parent = TYPE_DEVICE,
1428 .instance_size = sizeof(SCSIDevice),
1429 .abstract = true,
1430 .class_size = sizeof(SCSIDeviceClass),
1431 };
1432
1433 static void scsi_register_devices(void)
1434 {
1435 type_register_static(&scsi_device_type_info);
1436 }
1437
1438 device_init(scsi_register_devices);