]> git.proxmox.com Git - qemu.git/blob - hw/scsi-bus.c
scsi: add support for ATA_PASSTHROUGH_xx scsi command
[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 #include "dma.h"
9
10 static char *scsibus_get_dev_path(DeviceState *dev);
11 static char *scsibus_get_fw_dev_path(DeviceState *dev);
12 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
13 static void scsi_req_dequeue(SCSIRequest *req);
14
15 static Property scsi_props[] = {
16 DEFINE_PROP_UINT32("channel", SCSIDevice, channel, 0),
17 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
18 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, -1),
19 DEFINE_PROP_END_OF_LIST(),
20 };
21
22 static void scsi_bus_class_init(ObjectClass *klass, void *data)
23 {
24 BusClass *k = BUS_CLASS(klass);
25
26 k->get_dev_path = scsibus_get_dev_path;
27 k->get_fw_dev_path = scsibus_get_fw_dev_path;
28 }
29
30 static const TypeInfo scsi_bus_info = {
31 .name = TYPE_SCSI_BUS,
32 .parent = TYPE_BUS,
33 .instance_size = sizeof(SCSIBus),
34 .class_init = scsi_bus_class_init,
35 };
36 static int next_scsi_bus;
37
38 static int scsi_device_init(SCSIDevice *s)
39 {
40 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
41 if (sc->init) {
42 return sc->init(s);
43 }
44 return 0;
45 }
46
47 static void scsi_device_destroy(SCSIDevice *s)
48 {
49 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
50 if (sc->destroy) {
51 sc->destroy(s);
52 }
53 }
54
55 static SCSIRequest *scsi_device_alloc_req(SCSIDevice *s, uint32_t tag, uint32_t lun,
56 uint8_t *buf, void *hba_private)
57 {
58 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
59 if (sc->alloc_req) {
60 return sc->alloc_req(s, tag, lun, buf, hba_private);
61 }
62
63 return NULL;
64 }
65
66 static void scsi_device_unit_attention_reported(SCSIDevice *s)
67 {
68 SCSIDeviceClass *sc = SCSI_DEVICE_GET_CLASS(s);
69 if (sc->unit_attention_reported) {
70 sc->unit_attention_reported(s);
71 }
72 }
73
74 /* Create a scsi bus, and attach devices to it. */
75 void scsi_bus_new(SCSIBus *bus, DeviceState *host, const SCSIBusInfo *info)
76 {
77 qbus_create_inplace(&bus->qbus, TYPE_SCSI_BUS, host, NULL);
78 bus->busnr = next_scsi_bus++;
79 bus->info = info;
80 bus->qbus.allow_hotplug = 1;
81 }
82
83 static void scsi_dma_restart_bh(void *opaque)
84 {
85 SCSIDevice *s = opaque;
86 SCSIRequest *req, *next;
87
88 qemu_bh_delete(s->bh);
89 s->bh = NULL;
90
91 QTAILQ_FOREACH_SAFE(req, &s->requests, next, next) {
92 scsi_req_ref(req);
93 if (req->retry) {
94 req->retry = false;
95 switch (req->cmd.mode) {
96 case SCSI_XFER_FROM_DEV:
97 case SCSI_XFER_TO_DEV:
98 scsi_req_continue(req);
99 break;
100 case SCSI_XFER_NONE:
101 assert(!req->sg);
102 scsi_req_dequeue(req);
103 scsi_req_enqueue(req);
104 break;
105 }
106 }
107 scsi_req_unref(req);
108 }
109 }
110
111 void scsi_req_retry(SCSIRequest *req)
112 {
113 /* No need to save a reference, because scsi_dma_restart_bh just
114 * looks at the request list. */
115 req->retry = true;
116 }
117
118 static void scsi_dma_restart_cb(void *opaque, int running, RunState state)
119 {
120 SCSIDevice *s = opaque;
121
122 if (!running) {
123 return;
124 }
125 if (!s->bh) {
126 s->bh = qemu_bh_new(scsi_dma_restart_bh, s);
127 qemu_bh_schedule(s->bh);
128 }
129 }
130
131 static int scsi_qdev_init(DeviceState *qdev)
132 {
133 SCSIDevice *dev = SCSI_DEVICE(qdev);
134 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
135 SCSIDevice *d;
136 int rc = -1;
137
138 if (dev->channel > bus->info->max_channel) {
139 error_report("bad scsi channel id: %d", dev->channel);
140 goto err;
141 }
142 if (dev->id != -1 && dev->id > bus->info->max_target) {
143 error_report("bad scsi device id: %d", dev->id);
144 goto err;
145 }
146 if (dev->lun != -1 && dev->lun > bus->info->max_lun) {
147 error_report("bad scsi device lun: %d", dev->lun);
148 goto err;
149 }
150
151 if (dev->id == -1) {
152 int id = -1;
153 if (dev->lun == -1) {
154 dev->lun = 0;
155 }
156 do {
157 d = scsi_device_find(bus, dev->channel, ++id, dev->lun);
158 } while (d && d->lun == dev->lun && id < bus->info->max_target);
159 if (d && d->lun == dev->lun) {
160 error_report("no free target");
161 goto err;
162 }
163 dev->id = id;
164 } else if (dev->lun == -1) {
165 int lun = -1;
166 do {
167 d = scsi_device_find(bus, dev->channel, dev->id, ++lun);
168 } while (d && d->lun == lun && lun < bus->info->max_lun);
169 if (d && d->lun == lun) {
170 error_report("no free lun");
171 goto err;
172 }
173 dev->lun = lun;
174 } else {
175 d = scsi_device_find(bus, dev->channel, dev->id, dev->lun);
176 assert(d);
177 if (d->lun == dev->lun && dev != d) {
178 qdev_free(&d->qdev);
179 }
180 }
181
182 QTAILQ_INIT(&dev->requests);
183 rc = scsi_device_init(dev);
184 if (rc == 0) {
185 dev->vmsentry = qemu_add_vm_change_state_handler(scsi_dma_restart_cb,
186 dev);
187 }
188
189 if (bus->info->hotplug) {
190 bus->info->hotplug(bus, dev);
191 }
192
193 err:
194 return rc;
195 }
196
197 static int scsi_qdev_exit(DeviceState *qdev)
198 {
199 SCSIDevice *dev = SCSI_DEVICE(qdev);
200
201 if (dev->vmsentry) {
202 qemu_del_vm_change_state_handler(dev->vmsentry);
203 }
204 scsi_device_destroy(dev);
205 return 0;
206 }
207
208 /* handle legacy '-drive if=scsi,...' cmd line args */
209 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
210 int unit, bool removable, int bootindex)
211 {
212 const char *driver;
213 DeviceState *dev;
214
215 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
216 dev = qdev_create(&bus->qbus, driver);
217 qdev_prop_set_uint32(dev, "scsi-id", unit);
218 if (bootindex >= 0) {
219 qdev_prop_set_int32(dev, "bootindex", bootindex);
220 }
221 if (object_property_find(OBJECT(dev), "removable", NULL)) {
222 qdev_prop_set_bit(dev, "removable", removable);
223 }
224 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
225 qdev_free(dev);
226 return NULL;
227 }
228 if (qdev_init(dev) < 0)
229 return NULL;
230 return SCSI_DEVICE(dev);
231 }
232
233 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
234 {
235 Location loc;
236 DriveInfo *dinfo;
237 int res = 0, unit;
238
239 loc_push_none(&loc);
240 for (unit = 0; unit <= bus->info->max_target; unit++) {
241 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
242 if (dinfo == NULL) {
243 continue;
244 }
245 qemu_opts_loc_restore(dinfo->opts);
246 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false, -1)) {
247 res = -1;
248 break;
249 }
250 }
251 loc_pop(&loc);
252 return res;
253 }
254
255 static int32_t scsi_invalid_field(SCSIRequest *req, uint8_t *buf)
256 {
257 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
258 scsi_req_complete(req, CHECK_CONDITION);
259 return 0;
260 }
261
262 static const struct SCSIReqOps reqops_invalid_field = {
263 .size = sizeof(SCSIRequest),
264 .send_command = scsi_invalid_field
265 };
266
267 /* SCSIReqOps implementation for invalid commands. */
268
269 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
270 {
271 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
272 scsi_req_complete(req, CHECK_CONDITION);
273 return 0;
274 }
275
276 static const struct SCSIReqOps reqops_invalid_opcode = {
277 .size = sizeof(SCSIRequest),
278 .send_command = scsi_invalid_command
279 };
280
281 /* SCSIReqOps implementation for unit attention conditions. */
282
283 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
284 {
285 if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
286 scsi_req_build_sense(req, req->dev->unit_attention);
287 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
288 scsi_req_build_sense(req, req->bus->unit_attention);
289 }
290 scsi_req_complete(req, CHECK_CONDITION);
291 return 0;
292 }
293
294 static const struct SCSIReqOps reqops_unit_attention = {
295 .size = sizeof(SCSIRequest),
296 .send_command = scsi_unit_attention
297 };
298
299 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
300 an invalid LUN. */
301
302 typedef struct SCSITargetReq SCSITargetReq;
303
304 struct SCSITargetReq {
305 SCSIRequest req;
306 int len;
307 uint8_t buf[2056];
308 };
309
310 static void store_lun(uint8_t *outbuf, int lun)
311 {
312 if (lun < 256) {
313 outbuf[1] = lun;
314 return;
315 }
316 outbuf[1] = (lun & 255);
317 outbuf[0] = (lun >> 8) | 0x40;
318 }
319
320 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
321 {
322 BusChild *kid;
323 int i, len, n;
324 int channel, id;
325 bool found_lun0;
326
327 if (r->req.cmd.xfer < 16) {
328 return false;
329 }
330 if (r->req.cmd.buf[2] > 2) {
331 return false;
332 }
333 channel = r->req.dev->channel;
334 id = r->req.dev->id;
335 found_lun0 = false;
336 n = 0;
337 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
338 DeviceState *qdev = kid->child;
339 SCSIDevice *dev = SCSI_DEVICE(qdev);
340
341 if (dev->channel == channel && dev->id == id) {
342 if (dev->lun == 0) {
343 found_lun0 = true;
344 }
345 n += 8;
346 }
347 }
348 if (!found_lun0) {
349 n += 8;
350 }
351 len = MIN(n + 8, r->req.cmd.xfer & ~7);
352 if (len > sizeof(r->buf)) {
353 /* TODO: > 256 LUNs? */
354 return false;
355 }
356
357 memset(r->buf, 0, len);
358 stl_be_p(&r->buf, n);
359 i = found_lun0 ? 8 : 16;
360 QTAILQ_FOREACH(kid, &r->req.bus->qbus.children, sibling) {
361 DeviceState *qdev = kid->child;
362 SCSIDevice *dev = SCSI_DEVICE(qdev);
363
364 if (dev->channel == channel && dev->id == id) {
365 store_lun(&r->buf[i], dev->lun);
366 i += 8;
367 }
368 }
369 assert(i == n + 8);
370 r->len = len;
371 return true;
372 }
373
374 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
375 {
376 assert(r->req.dev->lun != r->req.lun);
377 if (r->req.cmd.buf[1] & 0x2) {
378 /* Command support data - optional, not implemented */
379 return false;
380 }
381
382 if (r->req.cmd.buf[1] & 0x1) {
383 /* Vital product data */
384 uint8_t page_code = r->req.cmd.buf[2];
385 r->buf[r->len++] = page_code ; /* this page */
386 r->buf[r->len++] = 0x00;
387
388 switch (page_code) {
389 case 0x00: /* Supported page codes, mandatory */
390 {
391 int pages;
392 pages = r->len++;
393 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
394 r->buf[pages] = r->len - pages - 1; /* number of pages */
395 break;
396 }
397 default:
398 return false;
399 }
400 /* done with EVPD */
401 assert(r->len < sizeof(r->buf));
402 r->len = MIN(r->req.cmd.xfer, r->len);
403 return true;
404 }
405
406 /* Standard INQUIRY data */
407 if (r->req.cmd.buf[2] != 0) {
408 return false;
409 }
410
411 /* PAGE CODE == 0 */
412 r->len = MIN(r->req.cmd.xfer, 36);
413 memset(r->buf, 0, r->len);
414 if (r->req.lun != 0) {
415 r->buf[0] = TYPE_NO_LUN;
416 } else {
417 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
418 r->buf[2] = 5; /* Version */
419 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
420 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
421 r->buf[7] = 0x10 | (r->req.bus->info->tcq ? 0x02 : 0); /* Sync, TCQ. */
422 memcpy(&r->buf[8], "QEMU ", 8);
423 memcpy(&r->buf[16], "QEMU TARGET ", 16);
424 pstrcpy((char *) &r->buf[32], 4, qemu_get_version());
425 }
426 return true;
427 }
428
429 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
430 {
431 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
432
433 switch (buf[0]) {
434 case REPORT_LUNS:
435 if (!scsi_target_emulate_report_luns(r)) {
436 goto illegal_request;
437 }
438 break;
439 case INQUIRY:
440 if (!scsi_target_emulate_inquiry(r)) {
441 goto illegal_request;
442 }
443 break;
444 case REQUEST_SENSE:
445 r->len = scsi_device_get_sense(r->req.dev, r->buf,
446 MIN(req->cmd.xfer, sizeof r->buf),
447 (req->cmd.buf[1] & 1) == 0);
448 if (r->req.dev->sense_is_ua) {
449 scsi_device_unit_attention_reported(req->dev);
450 r->req.dev->sense_len = 0;
451 r->req.dev->sense_is_ua = false;
452 }
453 break;
454 default:
455 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
456 scsi_req_complete(req, CHECK_CONDITION);
457 return 0;
458 illegal_request:
459 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
460 scsi_req_complete(req, CHECK_CONDITION);
461 return 0;
462 }
463
464 if (!r->len) {
465 scsi_req_complete(req, GOOD);
466 }
467 return r->len;
468 }
469
470 static void scsi_target_read_data(SCSIRequest *req)
471 {
472 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
473 uint32_t n;
474
475 n = r->len;
476 if (n > 0) {
477 r->len = 0;
478 scsi_req_data(&r->req, n);
479 } else {
480 scsi_req_complete(&r->req, GOOD);
481 }
482 }
483
484 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
485 {
486 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
487
488 return r->buf;
489 }
490
491 static const struct SCSIReqOps reqops_target_command = {
492 .size = sizeof(SCSITargetReq),
493 .send_command = scsi_target_send_command,
494 .read_data = scsi_target_read_data,
495 .get_buf = scsi_target_get_buf,
496 };
497
498
499 SCSIRequest *scsi_req_alloc(const SCSIReqOps *reqops, SCSIDevice *d,
500 uint32_t tag, uint32_t lun, void *hba_private)
501 {
502 SCSIRequest *req;
503
504 req = g_malloc0(reqops->size);
505 req->refcount = 1;
506 req->bus = scsi_bus_from_device(d);
507 req->dev = d;
508 req->tag = tag;
509 req->lun = lun;
510 req->hba_private = hba_private;
511 req->status = -1;
512 req->sense_len = 0;
513 req->ops = reqops;
514 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
515 return req;
516 }
517
518 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
519 uint8_t *buf, void *hba_private)
520 {
521 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
522 SCSIRequest *req;
523 SCSICommand cmd;
524
525 if (scsi_req_parse(&cmd, d, buf) != 0) {
526 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
527 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
528 } else {
529 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
530 cmd.mode, cmd.xfer);
531 if (cmd.lba != -1) {
532 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
533 cmd.lba);
534 }
535
536 if (cmd.xfer > INT32_MAX) {
537 req = scsi_req_alloc(&reqops_invalid_field, d, tag, lun, hba_private);
538 } else if ((d->unit_attention.key == UNIT_ATTENTION ||
539 bus->unit_attention.key == UNIT_ATTENTION) &&
540 (buf[0] != INQUIRY &&
541 buf[0] != REPORT_LUNS &&
542 buf[0] != GET_CONFIGURATION &&
543 buf[0] != GET_EVENT_STATUS_NOTIFICATION &&
544
545 /*
546 * If we already have a pending unit attention condition,
547 * report this one before triggering another one.
548 */
549 !(buf[0] == REQUEST_SENSE && d->sense_is_ua))) {
550 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
551 hba_private);
552 } else if (lun != d->lun ||
553 buf[0] == REPORT_LUNS ||
554 (buf[0] == REQUEST_SENSE && d->sense_len)) {
555 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
556 hba_private);
557 } else {
558 req = scsi_device_alloc_req(d, tag, lun, buf, hba_private);
559 }
560 }
561
562 req->cmd = cmd;
563 req->resid = req->cmd.xfer;
564
565 switch (buf[0]) {
566 case INQUIRY:
567 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
568 break;
569 case TEST_UNIT_READY:
570 trace_scsi_test_unit_ready(d->id, lun, tag);
571 break;
572 case REPORT_LUNS:
573 trace_scsi_report_luns(d->id, lun, tag);
574 break;
575 case REQUEST_SENSE:
576 trace_scsi_request_sense(d->id, lun, tag);
577 break;
578 default:
579 break;
580 }
581
582 return req;
583 }
584
585 uint8_t *scsi_req_get_buf(SCSIRequest *req)
586 {
587 return req->ops->get_buf(req);
588 }
589
590 static void scsi_clear_unit_attention(SCSIRequest *req)
591 {
592 SCSISense *ua;
593 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
594 req->bus->unit_attention.key != UNIT_ATTENTION) {
595 return;
596 }
597
598 /*
599 * If an INQUIRY command enters the enabled command state,
600 * the device server shall [not] clear any unit attention condition;
601 * See also MMC-6, paragraphs 6.5 and 6.6.2.
602 */
603 if (req->cmd.buf[0] == INQUIRY ||
604 req->cmd.buf[0] == GET_CONFIGURATION ||
605 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
606 return;
607 }
608
609 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
610 ua = &req->dev->unit_attention;
611 } else {
612 ua = &req->bus->unit_attention;
613 }
614
615 /*
616 * If a REPORT LUNS command enters the enabled command state, [...]
617 * the device server shall clear any pending unit attention condition
618 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
619 */
620 if (req->cmd.buf[0] == REPORT_LUNS &&
621 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
622 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
623 return;
624 }
625
626 *ua = SENSE_CODE(NO_SENSE);
627 }
628
629 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
630 {
631 int ret;
632
633 assert(len >= 14);
634 if (!req->sense_len) {
635 return 0;
636 }
637
638 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
639
640 /*
641 * FIXME: clearing unit attention conditions upon autosense should be done
642 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
643 * (SAM-5, 5.14).
644 *
645 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
646 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
647 * Here we handle unit attention clearing for UA_INTLCK_CTRL == 00b.
648 */
649 if (req->dev->sense_is_ua) {
650 scsi_device_unit_attention_reported(req->dev);
651 req->dev->sense_len = 0;
652 req->dev->sense_is_ua = false;
653 }
654 return ret;
655 }
656
657 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
658 {
659 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
660 }
661
662 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
663 {
664 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
665 sense.key, sense.asc, sense.ascq);
666 memset(req->sense, 0, 18);
667 req->sense[0] = 0x70;
668 req->sense[2] = sense.key;
669 req->sense[7] = 10;
670 req->sense[12] = sense.asc;
671 req->sense[13] = sense.ascq;
672 req->sense_len = 18;
673 }
674
675 static void scsi_req_enqueue_internal(SCSIRequest *req)
676 {
677 assert(!req->enqueued);
678 scsi_req_ref(req);
679 if (req->bus->info->get_sg_list) {
680 req->sg = req->bus->info->get_sg_list(req);
681 } else {
682 req->sg = NULL;
683 }
684 req->enqueued = true;
685 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
686 }
687
688 int32_t scsi_req_enqueue(SCSIRequest *req)
689 {
690 int32_t rc;
691
692 assert(!req->retry);
693 scsi_req_enqueue_internal(req);
694 scsi_req_ref(req);
695 rc = req->ops->send_command(req, req->cmd.buf);
696 scsi_req_unref(req);
697 return rc;
698 }
699
700 static void scsi_req_dequeue(SCSIRequest *req)
701 {
702 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
703 req->retry = false;
704 if (req->enqueued) {
705 QTAILQ_REMOVE(&req->dev->requests, req, next);
706 req->enqueued = false;
707 scsi_req_unref(req);
708 }
709 }
710
711 static int scsi_get_performance_length(int num_desc, int type, int data_type)
712 {
713 /* MMC-6, paragraph 6.7. */
714 switch (type) {
715 case 0:
716 if ((data_type & 3) == 0) {
717 /* Each descriptor is as in Table 295 - Nominal performance. */
718 return 16 * num_desc + 8;
719 } else {
720 /* Each descriptor is as in Table 296 - Exceptions. */
721 return 6 * num_desc + 8;
722 }
723 case 1:
724 case 4:
725 case 5:
726 return 8 * num_desc + 8;
727 case 2:
728 return 2048 * num_desc + 8;
729 case 3:
730 return 16 * num_desc + 8;
731 default:
732 return 8;
733 }
734 }
735
736 static int ata_passthrough_xfer_unit(SCSIDevice *dev, uint8_t *buf)
737 {
738 int byte_block = (buf[2] >> 2) & 0x1;
739 int type = (buf[2] >> 4) & 0x1;
740 int xfer_unit;
741
742 if (byte_block) {
743 if (type) {
744 xfer_unit = dev->blocksize;
745 } else {
746 xfer_unit = 512;
747 }
748 } else {
749 xfer_unit = 1;
750 }
751
752 return xfer_unit;
753 }
754
755 static int ata_passthrough_12_xfer_size(SCSIDevice *dev, uint8_t *buf)
756 {
757 int length = buf[2] & 0x3;
758 int xfer;
759 int unit = ata_passthrough_xfer_unit(dev, buf);
760
761 switch (length) {
762 case 0:
763 case 3: /* USB-specific. */
764 xfer = 0;
765 break;
766 case 1:
767 xfer = buf[3];
768 break;
769 case 2:
770 xfer = buf[4];
771 break;
772 }
773
774 return xfer * unit;
775 }
776
777 static int ata_passthrough_16_xfer_size(SCSIDevice *dev, uint8_t *buf)
778 {
779 int extend = buf[1] & 0x1;
780 int length = buf[2] & 0x3;
781 int xfer;
782 int unit = ata_passthrough_xfer_unit(dev, buf);
783
784 switch (length) {
785 case 0:
786 case 3: /* USB-specific. */
787 xfer = 0;
788 break;
789 case 1:
790 xfer = buf[4];
791 xfer |= (extend ? buf[3] << 8 : 0);
792 break;
793 case 2:
794 xfer = buf[6];
795 xfer |= (extend ? buf[5] << 8 : 0);
796 break;
797 }
798
799 return xfer * unit;
800 }
801
802 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
803 {
804 switch (buf[0] >> 5) {
805 case 0:
806 cmd->xfer = buf[4];
807 break;
808 case 1:
809 case 2:
810 cmd->xfer = lduw_be_p(&buf[7]);
811 break;
812 case 4:
813 cmd->xfer = ldl_be_p(&buf[10]) & 0xffffffffULL;
814 break;
815 case 5:
816 cmd->xfer = ldl_be_p(&buf[6]) & 0xffffffffULL;
817 break;
818 default:
819 return -1;
820 }
821
822 switch (buf[0]) {
823 case TEST_UNIT_READY:
824 case REWIND:
825 case START_STOP:
826 case SET_CAPACITY:
827 case WRITE_FILEMARKS:
828 case WRITE_FILEMARKS_16:
829 case SPACE:
830 case RESERVE:
831 case RELEASE:
832 case ERASE:
833 case ALLOW_MEDIUM_REMOVAL:
834 case VERIFY_10:
835 case SEEK_10:
836 case SYNCHRONIZE_CACHE:
837 case SYNCHRONIZE_CACHE_16:
838 case LOCATE_16:
839 case LOCK_UNLOCK_CACHE:
840 case SET_CD_SPEED:
841 case SET_LIMITS:
842 case WRITE_LONG_10:
843 case UPDATE_BLOCK:
844 case RESERVE_TRACK:
845 case SET_READ_AHEAD:
846 case PRE_FETCH:
847 case PRE_FETCH_16:
848 case ALLOW_OVERWRITE:
849 cmd->xfer = 0;
850 break;
851 case MODE_SENSE:
852 break;
853 case WRITE_SAME_10:
854 case WRITE_SAME_16:
855 cmd->xfer = dev->blocksize;
856 break;
857 case READ_CAPACITY_10:
858 cmd->xfer = 8;
859 break;
860 case READ_BLOCK_LIMITS:
861 cmd->xfer = 6;
862 break;
863 case SEND_VOLUME_TAG:
864 /* GPCMD_SET_STREAMING from multimedia commands. */
865 if (dev->type == TYPE_ROM) {
866 cmd->xfer = buf[10] | (buf[9] << 8);
867 } else {
868 cmd->xfer = buf[9] | (buf[8] << 8);
869 }
870 break;
871 case WRITE_6:
872 /* length 0 means 256 blocks */
873 if (cmd->xfer == 0) {
874 cmd->xfer = 256;
875 }
876 case WRITE_10:
877 case WRITE_VERIFY_10:
878 case WRITE_12:
879 case WRITE_VERIFY_12:
880 case WRITE_16:
881 case WRITE_VERIFY_16:
882 cmd->xfer *= dev->blocksize;
883 break;
884 case READ_6:
885 case READ_REVERSE:
886 /* length 0 means 256 blocks */
887 if (cmd->xfer == 0) {
888 cmd->xfer = 256;
889 }
890 case READ_10:
891 case RECOVER_BUFFERED_DATA:
892 case READ_12:
893 case READ_16:
894 cmd->xfer *= dev->blocksize;
895 break;
896 case FORMAT_UNIT:
897 /* MMC mandates the parameter list to be 12-bytes long. Parameters
898 * for block devices are restricted to the header right now. */
899 if (dev->type == TYPE_ROM && (buf[1] & 16)) {
900 cmd->xfer = 12;
901 } else {
902 cmd->xfer = (buf[1] & 16) == 0 ? 0 : (buf[1] & 32 ? 8 : 4);
903 }
904 break;
905 case INQUIRY:
906 case RECEIVE_DIAGNOSTIC:
907 case SEND_DIAGNOSTIC:
908 cmd->xfer = buf[4] | (buf[3] << 8);
909 break;
910 case READ_CD:
911 case READ_BUFFER:
912 case WRITE_BUFFER:
913 case SEND_CUE_SHEET:
914 cmd->xfer = buf[8] | (buf[7] << 8) | (buf[6] << 16);
915 break;
916 case PERSISTENT_RESERVE_OUT:
917 cmd->xfer = ldl_be_p(&buf[5]) & 0xffffffffULL;
918 break;
919 case ERASE_12:
920 if (dev->type == TYPE_ROM) {
921 /* MMC command GET PERFORMANCE. */
922 cmd->xfer = scsi_get_performance_length(buf[9] | (buf[8] << 8),
923 buf[10], buf[1] & 0x1f);
924 }
925 break;
926 case MECHANISM_STATUS:
927 case READ_DVD_STRUCTURE:
928 case SEND_DVD_STRUCTURE:
929 case MAINTENANCE_OUT:
930 case MAINTENANCE_IN:
931 if (dev->type == TYPE_ROM) {
932 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
933 cmd->xfer = buf[9] | (buf[8] << 8);
934 }
935 break;
936 case ATA_PASSTHROUGH_12:
937 if (dev->type == TYPE_ROM) {
938 /* BLANK command of MMC */
939 cmd->xfer = 0;
940 } else {
941 cmd->xfer = ata_passthrough_12_xfer_size(dev, buf);
942 }
943 break;
944 case ATA_PASSTHROUGH_16:
945 cmd->xfer = ata_passthrough_16_xfer_size(dev, buf);
946 break;
947 }
948 return 0;
949 }
950
951 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
952 {
953 switch (buf[0]) {
954 /* stream commands */
955 case ERASE_12:
956 case ERASE_16:
957 cmd->xfer = 0;
958 break;
959 case READ_6:
960 case READ_REVERSE:
961 case RECOVER_BUFFERED_DATA:
962 case WRITE_6:
963 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
964 if (buf[1] & 0x01) { /* fixed */
965 cmd->xfer *= dev->blocksize;
966 }
967 break;
968 case READ_16:
969 case READ_REVERSE_16:
970 case VERIFY_16:
971 case WRITE_16:
972 cmd->xfer = buf[14] | (buf[13] << 8) | (buf[12] << 16);
973 if (buf[1] & 0x01) { /* fixed */
974 cmd->xfer *= dev->blocksize;
975 }
976 break;
977 case REWIND:
978 case LOAD_UNLOAD:
979 cmd->xfer = 0;
980 break;
981 case SPACE_16:
982 cmd->xfer = buf[13] | (buf[12] << 8);
983 break;
984 case READ_POSITION:
985 switch (buf[1] & 0x1f) /* operation code */ {
986 case SHORT_FORM_BLOCK_ID:
987 case SHORT_FORM_VENDOR_SPECIFIC:
988 cmd->xfer = 20;
989 break;
990 case LONG_FORM:
991 cmd->xfer = 32;
992 break;
993 case EXTENDED_FORM:
994 cmd->xfer = buf[8] | (buf[7] << 8);
995 break;
996 default:
997 return -1;
998 }
999
1000 break;
1001 case FORMAT_UNIT:
1002 cmd->xfer = buf[4] | (buf[3] << 8);
1003 break;
1004 /* generic commands */
1005 default:
1006 return scsi_req_length(cmd, dev, buf);
1007 }
1008 return 0;
1009 }
1010
1011 static int scsi_req_medium_changer_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1012 {
1013 switch (buf[0]) {
1014 /* medium changer commands */
1015 case EXCHANGE_MEDIUM:
1016 case INITIALIZE_ELEMENT_STATUS:
1017 case INITIALIZE_ELEMENT_STATUS_WITH_RANGE:
1018 case MOVE_MEDIUM:
1019 case POSITION_TO_ELEMENT:
1020 cmd->xfer = 0;
1021 break;
1022 case READ_ELEMENT_STATUS:
1023 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16);
1024 break;
1025
1026 /* generic commands */
1027 default:
1028 return scsi_req_length(cmd, dev, buf);
1029 }
1030 return 0;
1031 }
1032
1033
1034 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
1035 {
1036 if (!cmd->xfer) {
1037 cmd->mode = SCSI_XFER_NONE;
1038 return;
1039 }
1040 switch (cmd->buf[0]) {
1041 case WRITE_6:
1042 case WRITE_10:
1043 case WRITE_VERIFY_10:
1044 case WRITE_12:
1045 case WRITE_VERIFY_12:
1046 case WRITE_16:
1047 case WRITE_VERIFY_16:
1048 case COPY:
1049 case COPY_VERIFY:
1050 case COMPARE:
1051 case CHANGE_DEFINITION:
1052 case LOG_SELECT:
1053 case MODE_SELECT:
1054 case MODE_SELECT_10:
1055 case SEND_DIAGNOSTIC:
1056 case WRITE_BUFFER:
1057 case FORMAT_UNIT:
1058 case REASSIGN_BLOCKS:
1059 case SEARCH_EQUAL:
1060 case SEARCH_HIGH:
1061 case SEARCH_LOW:
1062 case UPDATE_BLOCK:
1063 case WRITE_LONG_10:
1064 case WRITE_SAME_10:
1065 case WRITE_SAME_16:
1066 case UNMAP:
1067 case SEARCH_HIGH_12:
1068 case SEARCH_EQUAL_12:
1069 case SEARCH_LOW_12:
1070 case MEDIUM_SCAN:
1071 case SEND_VOLUME_TAG:
1072 case SEND_CUE_SHEET:
1073 case SEND_DVD_STRUCTURE:
1074 case PERSISTENT_RESERVE_OUT:
1075 case MAINTENANCE_OUT:
1076 cmd->mode = SCSI_XFER_TO_DEV;
1077 break;
1078 case ATA_PASSTHROUGH_12:
1079 case ATA_PASSTHROUGH_16:
1080 /* T_DIR */
1081 cmd->mode = (cmd->buf[2] & 0x8) ?
1082 SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV;
1083 break;
1084 default:
1085 cmd->mode = SCSI_XFER_FROM_DEV;
1086 break;
1087 }
1088 }
1089
1090 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
1091 {
1092 uint8_t *buf = cmd->buf;
1093 uint64_t lba;
1094
1095 switch (buf[0] >> 5) {
1096 case 0:
1097 lba = ldl_be_p(&buf[0]) & 0x1fffff;
1098 break;
1099 case 1:
1100 case 2:
1101 case 5:
1102 lba = ldl_be_p(&buf[2]) & 0xffffffffULL;
1103 break;
1104 case 4:
1105 lba = ldq_be_p(&buf[2]);
1106 break;
1107 default:
1108 lba = -1;
1109
1110 }
1111 return lba;
1112 }
1113
1114 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
1115 {
1116 int rc;
1117
1118 switch (buf[0] >> 5) {
1119 case 0:
1120 cmd->len = 6;
1121 break;
1122 case 1:
1123 case 2:
1124 cmd->len = 10;
1125 break;
1126 case 4:
1127 cmd->len = 16;
1128 break;
1129 case 5:
1130 cmd->len = 12;
1131 break;
1132 default:
1133 return -1;
1134 }
1135
1136 switch (dev->type) {
1137 case TYPE_TAPE:
1138 rc = scsi_req_stream_length(cmd, dev, buf);
1139 break;
1140 case TYPE_MEDIUM_CHANGER:
1141 rc = scsi_req_medium_changer_length(cmd, dev, buf);
1142 break;
1143 default:
1144 rc = scsi_req_length(cmd, dev, buf);
1145 break;
1146 }
1147
1148 if (rc != 0)
1149 return rc;
1150
1151 memcpy(cmd->buf, buf, cmd->len);
1152 scsi_cmd_xfer_mode(cmd);
1153 cmd->lba = scsi_cmd_lba(cmd);
1154 return 0;
1155 }
1156
1157 void scsi_device_report_change(SCSIDevice *dev, SCSISense sense)
1158 {
1159 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1160
1161 scsi_device_set_ua(dev, sense);
1162 if (bus->info->change) {
1163 bus->info->change(bus, dev, sense);
1164 }
1165 }
1166
1167 /*
1168 * Predefined sense codes
1169 */
1170
1171 /* No sense data available */
1172 const struct SCSISense sense_code_NO_SENSE = {
1173 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
1174 };
1175
1176 /* LUN not ready, Manual intervention required */
1177 const struct SCSISense sense_code_LUN_NOT_READY = {
1178 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
1179 };
1180
1181 /* LUN not ready, Medium not present */
1182 const struct SCSISense sense_code_NO_MEDIUM = {
1183 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
1184 };
1185
1186 /* LUN not ready, medium removal prevented */
1187 const struct SCSISense sense_code_NOT_READY_REMOVAL_PREVENTED = {
1188 .key = NOT_READY, .asc = 0x53, .ascq = 0x02
1189 };
1190
1191 /* Hardware error, internal target failure */
1192 const struct SCSISense sense_code_TARGET_FAILURE = {
1193 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
1194 };
1195
1196 /* Illegal request, invalid command operation code */
1197 const struct SCSISense sense_code_INVALID_OPCODE = {
1198 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
1199 };
1200
1201 /* Illegal request, LBA out of range */
1202 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
1203 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
1204 };
1205
1206 /* Illegal request, Invalid field in CDB */
1207 const struct SCSISense sense_code_INVALID_FIELD = {
1208 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
1209 };
1210
1211 /* Illegal request, Invalid field in parameter list */
1212 const struct SCSISense sense_code_INVALID_PARAM = {
1213 .key = ILLEGAL_REQUEST, .asc = 0x26, .ascq = 0x00
1214 };
1215
1216 /* Illegal request, Parameter list length error */
1217 const struct SCSISense sense_code_INVALID_PARAM_LEN = {
1218 .key = ILLEGAL_REQUEST, .asc = 0x1a, .ascq = 0x00
1219 };
1220
1221 /* Illegal request, LUN not supported */
1222 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
1223 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
1224 };
1225
1226 /* Illegal request, Saving parameters not supported */
1227 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
1228 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
1229 };
1230
1231 /* Illegal request, Incompatible medium installed */
1232 const struct SCSISense sense_code_INCOMPATIBLE_FORMAT = {
1233 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
1234 };
1235
1236 /* Illegal request, medium removal prevented */
1237 const struct SCSISense sense_code_ILLEGAL_REQ_REMOVAL_PREVENTED = {
1238 .key = ILLEGAL_REQUEST, .asc = 0x53, .ascq = 0x02
1239 };
1240
1241 /* Command aborted, I/O process terminated */
1242 const struct SCSISense sense_code_IO_ERROR = {
1243 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
1244 };
1245
1246 /* Command aborted, I_T Nexus loss occurred */
1247 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
1248 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
1249 };
1250
1251 /* Command aborted, Logical Unit failure */
1252 const struct SCSISense sense_code_LUN_FAILURE = {
1253 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
1254 };
1255
1256 /* Unit attention, Capacity data has changed */
1257 const struct SCSISense sense_code_CAPACITY_CHANGED = {
1258 .key = UNIT_ATTENTION, .asc = 0x2a, .ascq = 0x09
1259 };
1260
1261 /* Unit attention, Power on, reset or bus device reset occurred */
1262 const struct SCSISense sense_code_RESET = {
1263 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
1264 };
1265
1266 /* Unit attention, No medium */
1267 const struct SCSISense sense_code_UNIT_ATTENTION_NO_MEDIUM = {
1268 .key = UNIT_ATTENTION, .asc = 0x3a, .ascq = 0x00
1269 };
1270
1271 /* Unit attention, Medium may have changed */
1272 const struct SCSISense sense_code_MEDIUM_CHANGED = {
1273 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
1274 };
1275
1276 /* Unit attention, Reported LUNs data has changed */
1277 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
1278 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
1279 };
1280
1281 /* Unit attention, Device internal reset */
1282 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
1283 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
1284 };
1285
1286 /* Data Protection, Write Protected */
1287 const struct SCSISense sense_code_WRITE_PROTECTED = {
1288 .key = DATA_PROTECT, .asc = 0x27, .ascq = 0x00
1289 };
1290
1291 /*
1292 * scsi_build_sense
1293 *
1294 * Convert between fixed and descriptor sense buffers
1295 */
1296 int scsi_build_sense(uint8_t *in_buf, int in_len,
1297 uint8_t *buf, int len, bool fixed)
1298 {
1299 bool fixed_in;
1300 SCSISense sense;
1301 if (!fixed && len < 8) {
1302 return 0;
1303 }
1304
1305 if (in_len == 0) {
1306 sense.key = NO_SENSE;
1307 sense.asc = 0;
1308 sense.ascq = 0;
1309 } else {
1310 fixed_in = (in_buf[0] & 2) == 0;
1311
1312 if (fixed == fixed_in) {
1313 memcpy(buf, in_buf, MIN(len, in_len));
1314 return MIN(len, in_len);
1315 }
1316
1317 if (fixed_in) {
1318 sense.key = in_buf[2];
1319 sense.asc = in_buf[12];
1320 sense.ascq = in_buf[13];
1321 } else {
1322 sense.key = in_buf[1];
1323 sense.asc = in_buf[2];
1324 sense.ascq = in_buf[3];
1325 }
1326 }
1327
1328 memset(buf, 0, len);
1329 if (fixed) {
1330 /* Return fixed format sense buffer */
1331 buf[0] = 0x70;
1332 buf[2] = sense.key;
1333 buf[7] = 10;
1334 buf[12] = sense.asc;
1335 buf[13] = sense.ascq;
1336 return MIN(len, 18);
1337 } else {
1338 /* Return descriptor format sense buffer */
1339 buf[0] = 0x72;
1340 buf[1] = sense.key;
1341 buf[2] = sense.asc;
1342 buf[3] = sense.ascq;
1343 return 8;
1344 }
1345 }
1346
1347 static const char *scsi_command_name(uint8_t cmd)
1348 {
1349 static const char *names[] = {
1350 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
1351 [ REWIND ] = "REWIND",
1352 [ REQUEST_SENSE ] = "REQUEST_SENSE",
1353 [ FORMAT_UNIT ] = "FORMAT_UNIT",
1354 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
1355 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS/INITIALIZE ELEMENT STATUS",
1356 /* LOAD_UNLOAD and INITIALIZE_ELEMENT_STATUS use the same operation code */
1357 [ READ_6 ] = "READ_6",
1358 [ WRITE_6 ] = "WRITE_6",
1359 [ SET_CAPACITY ] = "SET_CAPACITY",
1360 [ READ_REVERSE ] = "READ_REVERSE",
1361 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
1362 [ SPACE ] = "SPACE",
1363 [ INQUIRY ] = "INQUIRY",
1364 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
1365 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
1366 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
1367 [ MODE_SELECT ] = "MODE_SELECT",
1368 [ RESERVE ] = "RESERVE",
1369 [ RELEASE ] = "RELEASE",
1370 [ COPY ] = "COPY",
1371 [ ERASE ] = "ERASE",
1372 [ MODE_SENSE ] = "MODE_SENSE",
1373 [ START_STOP ] = "START_STOP/LOAD_UNLOAD",
1374 /* LOAD_UNLOAD and START_STOP use the same operation code */
1375 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
1376 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
1377 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
1378 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
1379 [ READ_10 ] = "READ_10",
1380 [ WRITE_10 ] = "WRITE_10",
1381 [ SEEK_10 ] = "SEEK_10/POSITION_TO_ELEMENT",
1382 /* SEEK_10 and POSITION_TO_ELEMENT use the same operation code */
1383 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
1384 [ VERIFY_10 ] = "VERIFY_10",
1385 [ SEARCH_HIGH ] = "SEARCH_HIGH",
1386 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
1387 [ SEARCH_LOW ] = "SEARCH_LOW",
1388 [ SET_LIMITS ] = "SET_LIMITS",
1389 [ PRE_FETCH ] = "PRE_FETCH/READ_POSITION",
1390 /* READ_POSITION and PRE_FETCH use the same operation code */
1391 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
1392 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
1393 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA/INITIALIZE_ELEMENT_STATUS_WITH_RANGE",
1394 /* READ_DEFECT_DATA and INITIALIZE_ELEMENT_STATUS_WITH_RANGE use the same operation code */
1395 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
1396 [ COMPARE ] = "COMPARE",
1397 [ COPY_VERIFY ] = "COPY_VERIFY",
1398 [ WRITE_BUFFER ] = "WRITE_BUFFER",
1399 [ READ_BUFFER ] = "READ_BUFFER",
1400 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
1401 [ READ_LONG_10 ] = "READ_LONG_10",
1402 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
1403 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
1404 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
1405 [ UNMAP ] = "UNMAP",
1406 [ READ_TOC ] = "READ_TOC",
1407 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
1408 [ SANITIZE ] = "SANITIZE",
1409 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
1410 [ LOG_SELECT ] = "LOG_SELECT",
1411 [ LOG_SENSE ] = "LOG_SENSE",
1412 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
1413 [ RESERVE_10 ] = "RESERVE_10",
1414 [ RELEASE_10 ] = "RELEASE_10",
1415 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
1416 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
1417 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
1418 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
1419 [ EXTENDED_COPY ] = "EXTENDED_COPY",
1420 [ ATA_PASSTHROUGH_16 ] = "ATA_PASSTHROUGH_16",
1421 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
1422 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
1423 [ READ_16 ] = "READ_16",
1424 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
1425 [ WRITE_16 ] = "WRITE_16",
1426 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
1427 [ VERIFY_16 ] = "VERIFY_16",
1428 [ PRE_FETCH_16 ] = "PRE_FETCH_16",
1429 [ SYNCHRONIZE_CACHE_16 ] = "SPACE_16/SYNCHRONIZE_CACHE_16",
1430 /* SPACE_16 and SYNCHRONIZE_CACHE_16 use the same operation code */
1431 [ LOCATE_16 ] = "LOCATE_16",
1432 [ WRITE_SAME_16 ] = "ERASE_16/WRITE_SAME_16",
1433 /* ERASE_16 and WRITE_SAME_16 use the same operation code */
1434 [ SERVICE_ACTION_IN_16 ] = "SERVICE_ACTION_IN_16",
1435 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
1436 [ REPORT_LUNS ] = "REPORT_LUNS",
1437 [ ATA_PASSTHROUGH_12 ] = "BLANK/ATA_PASSTHROUGH_12",
1438 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
1439 [ EXCHANGE_MEDIUM ] = "EXCHANGE MEDIUM",
1440 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
1441 [ READ_12 ] = "READ_12",
1442 [ WRITE_12 ] = "WRITE_12",
1443 [ ERASE_12 ] = "ERASE_12/GET_PERFORMANCE",
1444 /* ERASE_12 and GET_PERFORMANCE use the same operation code */
1445 [ SERVICE_ACTION_IN_12 ] = "SERVICE_ACTION_IN_12",
1446 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
1447 [ VERIFY_12 ] = "VERIFY_12",
1448 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
1449 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
1450 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
1451 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
1452 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG/SET_STREAMING",
1453 /* SEND_VOLUME_TAG and SET_STREAMING use the same operation code */
1454 [ READ_CD ] = "READ_CD",
1455 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
1456 [ READ_DVD_STRUCTURE ] = "READ_DVD_STRUCTURE",
1457 [ RESERVE_TRACK ] = "RESERVE_TRACK",
1458 [ SEND_CUE_SHEET ] = "SEND_CUE_SHEET",
1459 [ SEND_DVD_STRUCTURE ] = "SEND_DVD_STRUCTURE",
1460 [ SET_CD_SPEED ] = "SET_CD_SPEED",
1461 [ SET_READ_AHEAD ] = "SET_READ_AHEAD",
1462 [ ALLOW_OVERWRITE ] = "ALLOW_OVERWRITE",
1463 [ MECHANISM_STATUS ] = "MECHANISM_STATUS",
1464 };
1465
1466 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1467 return "*UNKNOWN*";
1468 return names[cmd];
1469 }
1470
1471 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1472 {
1473 assert(req->refcount > 0);
1474 req->refcount++;
1475 return req;
1476 }
1477
1478 void scsi_req_unref(SCSIRequest *req)
1479 {
1480 assert(req->refcount > 0);
1481 if (--req->refcount == 0) {
1482 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, req->dev->qdev.parent_bus);
1483 if (bus->info->free_request && req->hba_private) {
1484 bus->info->free_request(bus, req->hba_private);
1485 }
1486 if (req->ops->free_req) {
1487 req->ops->free_req(req);
1488 }
1489 g_free(req);
1490 }
1491 }
1492
1493 /* Tell the device that we finished processing this chunk of I/O. It
1494 will start the next chunk or complete the command. */
1495 void scsi_req_continue(SCSIRequest *req)
1496 {
1497 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1498 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1499 req->ops->write_data(req);
1500 } else {
1501 req->ops->read_data(req);
1502 }
1503 }
1504
1505 /* Called by the devices when data is ready for the HBA. The HBA should
1506 start a DMA operation to read or fill the device's data buffer.
1507 Once it completes, calling scsi_req_continue will restart I/O. */
1508 void scsi_req_data(SCSIRequest *req, int len)
1509 {
1510 uint8_t *buf;
1511 if (req->io_canceled) {
1512 trace_scsi_req_data_canceled(req->dev->id, req->lun, req->tag, len);
1513 return;
1514 }
1515 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1516 assert(req->cmd.mode != SCSI_XFER_NONE);
1517 if (!req->sg) {
1518 req->resid -= len;
1519 req->bus->info->transfer_data(req, len);
1520 return;
1521 }
1522
1523 /* If the device calls scsi_req_data and the HBA specified a
1524 * scatter/gather list, the transfer has to happen in a single
1525 * step. */
1526 assert(!req->dma_started);
1527 req->dma_started = true;
1528
1529 buf = scsi_req_get_buf(req);
1530 if (req->cmd.mode == SCSI_XFER_FROM_DEV) {
1531 req->resid = dma_buf_read(buf, len, req->sg);
1532 } else {
1533 req->resid = dma_buf_write(buf, len, req->sg);
1534 }
1535 scsi_req_continue(req);
1536 }
1537
1538 void scsi_req_print(SCSIRequest *req)
1539 {
1540 FILE *fp = stderr;
1541 int i;
1542
1543 fprintf(fp, "[%s id=%d] %s",
1544 req->dev->qdev.parent_bus->name,
1545 req->dev->id,
1546 scsi_command_name(req->cmd.buf[0]));
1547 for (i = 1; i < req->cmd.len; i++) {
1548 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1549 }
1550 switch (req->cmd.mode) {
1551 case SCSI_XFER_NONE:
1552 fprintf(fp, " - none\n");
1553 break;
1554 case SCSI_XFER_FROM_DEV:
1555 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1556 break;
1557 case SCSI_XFER_TO_DEV:
1558 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1559 break;
1560 default:
1561 fprintf(fp, " - Oops\n");
1562 break;
1563 }
1564 }
1565
1566 void scsi_req_complete(SCSIRequest *req, int status)
1567 {
1568 assert(req->status == -1);
1569 req->status = status;
1570
1571 assert(req->sense_len <= sizeof(req->sense));
1572 if (status == GOOD) {
1573 req->sense_len = 0;
1574 }
1575
1576 if (req->sense_len) {
1577 memcpy(req->dev->sense, req->sense, req->sense_len);
1578 req->dev->sense_len = req->sense_len;
1579 req->dev->sense_is_ua = (req->ops == &reqops_unit_attention);
1580 } else {
1581 req->dev->sense_len = 0;
1582 req->dev->sense_is_ua = false;
1583 }
1584
1585 /*
1586 * Unit attention state is now stored in the device's sense buffer
1587 * if the HBA didn't do autosense. Clear the pending unit attention
1588 * flags.
1589 */
1590 scsi_clear_unit_attention(req);
1591
1592 scsi_req_ref(req);
1593 scsi_req_dequeue(req);
1594 req->bus->info->complete(req, req->status, req->resid);
1595 scsi_req_unref(req);
1596 }
1597
1598 void scsi_req_cancel(SCSIRequest *req)
1599 {
1600 trace_scsi_req_cancel(req->dev->id, req->lun, req->tag);
1601 if (!req->enqueued) {
1602 return;
1603 }
1604 scsi_req_ref(req);
1605 scsi_req_dequeue(req);
1606 req->io_canceled = true;
1607 if (req->ops->cancel_io) {
1608 req->ops->cancel_io(req);
1609 }
1610 if (req->bus->info->cancel) {
1611 req->bus->info->cancel(req);
1612 }
1613 scsi_req_unref(req);
1614 }
1615
1616 void scsi_req_abort(SCSIRequest *req, int status)
1617 {
1618 if (!req->enqueued) {
1619 return;
1620 }
1621 scsi_req_ref(req);
1622 scsi_req_dequeue(req);
1623 req->io_canceled = true;
1624 if (req->ops->cancel_io) {
1625 req->ops->cancel_io(req);
1626 }
1627 scsi_req_complete(req, status);
1628 scsi_req_unref(req);
1629 }
1630
1631 static int scsi_ua_precedence(SCSISense sense)
1632 {
1633 if (sense.key != UNIT_ATTENTION) {
1634 return INT_MAX;
1635 }
1636 if (sense.asc == 0x29 && sense.ascq == 0x04) {
1637 /* DEVICE INTERNAL RESET goes with POWER ON OCCURRED */
1638 return 1;
1639 } else if (sense.asc == 0x3F && sense.ascq == 0x01) {
1640 /* MICROCODE HAS BEEN CHANGED goes with SCSI BUS RESET OCCURRED */
1641 return 2;
1642 } else if (sense.asc == 0x29 && (sense.ascq == 0x05 || sense.ascq == 0x06)) {
1643 /* These two go with "all others". */
1644 ;
1645 } else if (sense.asc == 0x29 && sense.ascq <= 0x07) {
1646 /* POWER ON, RESET OR BUS DEVICE RESET OCCURRED = 0
1647 * POWER ON OCCURRED = 1
1648 * SCSI BUS RESET OCCURRED = 2
1649 * BUS DEVICE RESET FUNCTION OCCURRED = 3
1650 * I_T NEXUS LOSS OCCURRED = 7
1651 */
1652 return sense.ascq;
1653 } else if (sense.asc == 0x2F && sense.ascq == 0x01) {
1654 /* COMMANDS CLEARED BY POWER LOSS NOTIFICATION */
1655 return 8;
1656 }
1657 return (sense.asc << 8) | sense.ascq;
1658 }
1659
1660 void scsi_device_set_ua(SCSIDevice *sdev, SCSISense sense)
1661 {
1662 int prec1, prec2;
1663 if (sense.key != UNIT_ATTENTION) {
1664 return;
1665 }
1666 trace_scsi_device_set_ua(sdev->id, sdev->lun, sense.key,
1667 sense.asc, sense.ascq);
1668
1669 /*
1670 * Override a pre-existing unit attention condition, except for a more
1671 * important reset condition.
1672 */
1673 prec1 = scsi_ua_precedence(sdev->unit_attention);
1674 prec2 = scsi_ua_precedence(sense);
1675 if (prec2 < prec1) {
1676 sdev->unit_attention = sense;
1677 }
1678 }
1679
1680 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1681 {
1682 SCSIRequest *req;
1683
1684 while (!QTAILQ_EMPTY(&sdev->requests)) {
1685 req = QTAILQ_FIRST(&sdev->requests);
1686 scsi_req_cancel(req);
1687 }
1688
1689 scsi_device_set_ua(sdev, sense);
1690 }
1691
1692 static char *scsibus_get_dev_path(DeviceState *dev)
1693 {
1694 SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1695 DeviceState *hba = dev->parent_bus->parent;
1696 char *id;
1697 char *path;
1698
1699 id = qdev_get_dev_path(hba);
1700 if (id) {
1701 path = g_strdup_printf("%s/%d:%d:%d", id, d->channel, d->id, d->lun);
1702 } else {
1703 path = g_strdup_printf("%d:%d:%d", d->channel, d->id, d->lun);
1704 }
1705 g_free(id);
1706 return path;
1707 }
1708
1709 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1710 {
1711 SCSIDevice *d = SCSI_DEVICE(dev);
1712 char path[100];
1713
1714 snprintf(path, sizeof(path), "channel@%x/%s@%x,%x", d->channel,
1715 qdev_fw_name(dev), d->id, d->lun);
1716
1717 return strdup(path);
1718 }
1719
1720 SCSIDevice *scsi_device_find(SCSIBus *bus, int channel, int id, int lun)
1721 {
1722 BusChild *kid;
1723 SCSIDevice *target_dev = NULL;
1724
1725 QTAILQ_FOREACH_REVERSE(kid, &bus->qbus.children, ChildrenHead, sibling) {
1726 DeviceState *qdev = kid->child;
1727 SCSIDevice *dev = SCSI_DEVICE(qdev);
1728
1729 if (dev->channel == channel && dev->id == id) {
1730 if (dev->lun == lun) {
1731 return dev;
1732 }
1733 target_dev = dev;
1734 }
1735 }
1736 return target_dev;
1737 }
1738
1739 /* SCSI request list. For simplicity, pv points to the whole device */
1740
1741 static void put_scsi_requests(QEMUFile *f, void *pv, size_t size)
1742 {
1743 SCSIDevice *s = pv;
1744 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1745 SCSIRequest *req;
1746
1747 QTAILQ_FOREACH(req, &s->requests, next) {
1748 assert(!req->io_canceled);
1749 assert(req->status == -1);
1750 assert(req->enqueued);
1751
1752 qemu_put_sbyte(f, req->retry ? 1 : 2);
1753 qemu_put_buffer(f, req->cmd.buf, sizeof(req->cmd.buf));
1754 qemu_put_be32s(f, &req->tag);
1755 qemu_put_be32s(f, &req->lun);
1756 if (bus->info->save_request) {
1757 bus->info->save_request(f, req);
1758 }
1759 if (req->ops->save_request) {
1760 req->ops->save_request(f, req);
1761 }
1762 }
1763 qemu_put_sbyte(f, 0);
1764 }
1765
1766 static int get_scsi_requests(QEMUFile *f, void *pv, size_t size)
1767 {
1768 SCSIDevice *s = pv;
1769 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, s->qdev.parent_bus);
1770 int8_t sbyte;
1771
1772 while ((sbyte = qemu_get_sbyte(f)) > 0) {
1773 uint8_t buf[SCSI_CMD_BUF_SIZE];
1774 uint32_t tag;
1775 uint32_t lun;
1776 SCSIRequest *req;
1777
1778 qemu_get_buffer(f, buf, sizeof(buf));
1779 qemu_get_be32s(f, &tag);
1780 qemu_get_be32s(f, &lun);
1781 req = scsi_req_new(s, tag, lun, buf, NULL);
1782 req->retry = (sbyte == 1);
1783 if (bus->info->load_request) {
1784 req->hba_private = bus->info->load_request(f, req);
1785 }
1786 if (req->ops->load_request) {
1787 req->ops->load_request(f, req);
1788 }
1789
1790 /* Just restart it later. */
1791 scsi_req_enqueue_internal(req);
1792
1793 /* At this point, the request will be kept alive by the reference
1794 * added by scsi_req_enqueue_internal, so we can release our reference.
1795 * The HBA of course will add its own reference in the load_request
1796 * callback if it needs to hold on the SCSIRequest.
1797 */
1798 scsi_req_unref(req);
1799 }
1800
1801 return 0;
1802 }
1803
1804 static int scsi_qdev_unplug(DeviceState *qdev)
1805 {
1806 SCSIDevice *dev = SCSI_DEVICE(qdev);
1807 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
1808
1809 if (bus->info->hot_unplug) {
1810 bus->info->hot_unplug(bus, dev);
1811 }
1812 return qdev_simple_unplug_cb(qdev);
1813 }
1814
1815 static const VMStateInfo vmstate_info_scsi_requests = {
1816 .name = "scsi-requests",
1817 .get = get_scsi_requests,
1818 .put = put_scsi_requests,
1819 };
1820
1821 const VMStateDescription vmstate_scsi_device = {
1822 .name = "SCSIDevice",
1823 .version_id = 1,
1824 .minimum_version_id = 1,
1825 .minimum_version_id_old = 1,
1826 .fields = (VMStateField[]) {
1827 VMSTATE_UINT8(unit_attention.key, SCSIDevice),
1828 VMSTATE_UINT8(unit_attention.asc, SCSIDevice),
1829 VMSTATE_UINT8(unit_attention.ascq, SCSIDevice),
1830 VMSTATE_BOOL(sense_is_ua, SCSIDevice),
1831 VMSTATE_UINT8_ARRAY(sense, SCSIDevice, SCSI_SENSE_BUF_SIZE),
1832 VMSTATE_UINT32(sense_len, SCSIDevice),
1833 {
1834 .name = "requests",
1835 .version_id = 0,
1836 .field_exists = NULL,
1837 .size = 0, /* ouch */
1838 .info = &vmstate_info_scsi_requests,
1839 .flags = VMS_SINGLE,
1840 .offset = 0,
1841 },
1842 VMSTATE_END_OF_LIST()
1843 }
1844 };
1845
1846 static void scsi_device_class_init(ObjectClass *klass, void *data)
1847 {
1848 DeviceClass *k = DEVICE_CLASS(klass);
1849 k->bus_type = TYPE_SCSI_BUS;
1850 k->init = scsi_qdev_init;
1851 k->unplug = scsi_qdev_unplug;
1852 k->exit = scsi_qdev_exit;
1853 k->props = scsi_props;
1854 }
1855
1856 static TypeInfo scsi_device_type_info = {
1857 .name = TYPE_SCSI_DEVICE,
1858 .parent = TYPE_DEVICE,
1859 .instance_size = sizeof(SCSIDevice),
1860 .abstract = true,
1861 .class_size = sizeof(SCSIDeviceClass),
1862 .class_init = scsi_device_class_init,
1863 };
1864
1865 static void scsi_register_types(void)
1866 {
1867 type_register_static(&scsi_bus_info);
1868 type_register_static(&scsi_device_type_info);
1869 }
1870
1871 type_init(scsi_register_types)