]> git.proxmox.com Git - mirror_qemu.git/blob - hw/scsi-bus.c
hw/scsi-bus.c: Fix use of uninitialised variable
[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 static int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf);
11 static int scsi_build_sense(uint8_t *in_buf, int in_len,
12 uint8_t *buf, int len, bool fixed);
13
14 static struct BusInfo scsi_bus_info = {
15 .name = "SCSI",
16 .size = sizeof(SCSIBus),
17 .get_fw_dev_path = scsibus_get_fw_dev_path,
18 .props = (Property[]) {
19 DEFINE_PROP_UINT32("scsi-id", SCSIDevice, id, -1),
20 DEFINE_PROP_UINT32("lun", SCSIDevice, lun, 0),
21 DEFINE_PROP_END_OF_LIST(),
22 },
23 };
24 static int next_scsi_bus;
25
26 /* Create a scsi bus, and attach devices to it. */
27 void scsi_bus_new(SCSIBus *bus, DeviceState *host, int tcq, int ndev,
28 const SCSIBusOps *ops)
29 {
30 qbus_create_inplace(&bus->qbus, &scsi_bus_info, host, NULL);
31 bus->busnr = next_scsi_bus++;
32 bus->tcq = tcq;
33 bus->ndev = ndev;
34 bus->ops = ops;
35 bus->qbus.allow_hotplug = 1;
36 }
37
38 static int scsi_qdev_init(DeviceState *qdev, DeviceInfo *base)
39 {
40 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
41 SCSIDeviceInfo *info = DO_UPCAST(SCSIDeviceInfo, qdev, base);
42 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
43 int rc = -1;
44
45 if (dev->id == -1) {
46 for (dev->id = 0; dev->id < bus->ndev; dev->id++) {
47 if (bus->devs[dev->id] == NULL)
48 break;
49 }
50 }
51 if (dev->id >= bus->ndev) {
52 error_report("bad scsi device id: %d", dev->id);
53 goto err;
54 }
55
56 if (bus->devs[dev->id]) {
57 qdev_free(&bus->devs[dev->id]->qdev);
58 }
59 bus->devs[dev->id] = dev;
60
61 dev->info = info;
62 QTAILQ_INIT(&dev->requests);
63 rc = dev->info->init(dev);
64 if (rc != 0) {
65 bus->devs[dev->id] = NULL;
66 }
67
68 err:
69 return rc;
70 }
71
72 static int scsi_qdev_exit(DeviceState *qdev)
73 {
74 SCSIDevice *dev = DO_UPCAST(SCSIDevice, qdev, qdev);
75 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, dev->qdev.parent_bus);
76
77 assert(bus->devs[dev->id] != NULL);
78 if (bus->devs[dev->id]->info->destroy) {
79 bus->devs[dev->id]->info->destroy(bus->devs[dev->id]);
80 }
81 bus->devs[dev->id] = NULL;
82 return 0;
83 }
84
85 void scsi_qdev_register(SCSIDeviceInfo *info)
86 {
87 info->qdev.bus_info = &scsi_bus_info;
88 info->qdev.init = scsi_qdev_init;
89 info->qdev.unplug = qdev_simple_unplug_cb;
90 info->qdev.exit = scsi_qdev_exit;
91 qdev_register(&info->qdev);
92 }
93
94 /* handle legacy '-drive if=scsi,...' cmd line args */
95 SCSIDevice *scsi_bus_legacy_add_drive(SCSIBus *bus, BlockDriverState *bdrv,
96 int unit, bool removable)
97 {
98 const char *driver;
99 DeviceState *dev;
100
101 driver = bdrv_is_sg(bdrv) ? "scsi-generic" : "scsi-disk";
102 dev = qdev_create(&bus->qbus, driver);
103 qdev_prop_set_uint32(dev, "scsi-id", unit);
104 if (qdev_prop_exists(dev, "removable")) {
105 qdev_prop_set_bit(dev, "removable", removable);
106 }
107 if (qdev_prop_set_drive(dev, "drive", bdrv) < 0) {
108 qdev_free(dev);
109 return NULL;
110 }
111 if (qdev_init(dev) < 0)
112 return NULL;
113 return DO_UPCAST(SCSIDevice, qdev, dev);
114 }
115
116 int scsi_bus_legacy_handle_cmdline(SCSIBus *bus)
117 {
118 Location loc;
119 DriveInfo *dinfo;
120 int res = 0, unit;
121
122 loc_push_none(&loc);
123 for (unit = 0; unit < bus->ndev; unit++) {
124 dinfo = drive_get(IF_SCSI, bus->busnr, unit);
125 if (dinfo == NULL) {
126 continue;
127 }
128 qemu_opts_loc_restore(dinfo->opts);
129 if (!scsi_bus_legacy_add_drive(bus, dinfo->bdrv, unit, false)) {
130 res = -1;
131 break;
132 }
133 }
134 loc_pop(&loc);
135 return res;
136 }
137
138 /* SCSIReqOps implementation for invalid commands. */
139
140 static int32_t scsi_invalid_command(SCSIRequest *req, uint8_t *buf)
141 {
142 scsi_req_build_sense(req, SENSE_CODE(INVALID_OPCODE));
143 scsi_req_complete(req, CHECK_CONDITION);
144 return 0;
145 }
146
147 struct SCSIReqOps reqops_invalid_opcode = {
148 .size = sizeof(SCSIRequest),
149 .send_command = scsi_invalid_command
150 };
151
152 /* SCSIReqOps implementation for unit attention conditions. */
153
154 static int32_t scsi_unit_attention(SCSIRequest *req, uint8_t *buf)
155 {
156 if (req->dev && req->dev->unit_attention.key == UNIT_ATTENTION) {
157 scsi_req_build_sense(req, req->dev->unit_attention);
158 } else if (req->bus->unit_attention.key == UNIT_ATTENTION) {
159 scsi_req_build_sense(req, req->bus->unit_attention);
160 }
161 scsi_req_complete(req, CHECK_CONDITION);
162 return 0;
163 }
164
165 struct SCSIReqOps reqops_unit_attention = {
166 .size = sizeof(SCSIRequest),
167 .send_command = scsi_unit_attention
168 };
169
170 /* SCSIReqOps implementation for REPORT LUNS and for commands sent to
171 an invalid LUN. */
172
173 typedef struct SCSITargetReq SCSITargetReq;
174
175 struct SCSITargetReq {
176 SCSIRequest req;
177 int len;
178 uint8_t buf[64];
179 };
180
181 static void store_lun(uint8_t *outbuf, int lun)
182 {
183 if (lun < 256) {
184 outbuf[1] = lun;
185 return;
186 }
187 outbuf[1] = (lun & 255);
188 outbuf[0] = (lun >> 8) | 0x40;
189 }
190
191 static bool scsi_target_emulate_report_luns(SCSITargetReq *r)
192 {
193 int len;
194 if (r->req.cmd.xfer < 16) {
195 return false;
196 }
197 if (r->req.cmd.buf[2] > 2) {
198 return false;
199 }
200 len = MIN(sizeof r->buf, r->req.cmd.xfer);
201 memset(r->buf, 0, len);
202 if (r->req.dev->lun != 0) {
203 r->buf[3] = 16;
204 r->len = 24;
205 store_lun(&r->buf[16], r->req.dev->lun);
206 } else {
207 r->buf[3] = 8;
208 r->len = 16;
209 }
210 return true;
211 }
212
213 static bool scsi_target_emulate_inquiry(SCSITargetReq *r)
214 {
215 assert(r->req.dev->lun != r->req.lun);
216 if (r->req.cmd.buf[1] & 0x2) {
217 /* Command support data - optional, not implemented */
218 return false;
219 }
220
221 if (r->req.cmd.buf[1] & 0x1) {
222 /* Vital product data */
223 uint8_t page_code = r->req.cmd.buf[2];
224 if (r->req.cmd.xfer < 4) {
225 return false;
226 }
227
228 r->buf[r->len++] = page_code ; /* this page */
229 r->buf[r->len++] = 0x00;
230
231 switch (page_code) {
232 case 0x00: /* Supported page codes, mandatory */
233 {
234 int pages;
235 pages = r->len++;
236 r->buf[r->len++] = 0x00; /* list of supported pages (this page) */
237 r->buf[pages] = r->len - pages - 1; /* number of pages */
238 break;
239 }
240 default:
241 return false;
242 }
243 /* done with EVPD */
244 assert(r->len < sizeof(r->buf));
245 r->len = MIN(r->req.cmd.xfer, r->len);
246 return true;
247 }
248
249 /* Standard INQUIRY data */
250 if (r->req.cmd.buf[2] != 0) {
251 return false;
252 }
253
254 /* PAGE CODE == 0 */
255 if (r->req.cmd.xfer < 5) {
256 return -1;
257 }
258
259 r->len = MIN(r->req.cmd.xfer, 36);
260 memset(r->buf, 0, r->len);
261 if (r->req.lun != 0) {
262 r->buf[0] = TYPE_NO_LUN;
263 } else {
264 r->buf[0] = TYPE_NOT_PRESENT | TYPE_INACTIVE;
265 r->buf[2] = 5; /* Version */
266 r->buf[3] = 2 | 0x10; /* HiSup, response data format */
267 r->buf[4] = r->len - 5; /* Additional Length = (Len - 1) - 4 */
268 r->buf[7] = 0x10 | (r->req.bus->tcq ? 0x02 : 0); /* Sync, TCQ. */
269 memcpy(&r->buf[8], "QEMU ", 8);
270 memcpy(&r->buf[16], "QEMU TARGET ", 16);
271 strncpy((char *) &r->buf[32], QEMU_VERSION, 4);
272 }
273 return true;
274 }
275
276 static int32_t scsi_target_send_command(SCSIRequest *req, uint8_t *buf)
277 {
278 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
279
280 switch (buf[0]) {
281 case REPORT_LUNS:
282 if (!scsi_target_emulate_report_luns(r)) {
283 goto illegal_request;
284 }
285 break;
286 case INQUIRY:
287 if (!scsi_target_emulate_inquiry(r)) {
288 goto illegal_request;
289 }
290 break;
291 case REQUEST_SENSE:
292 if (req->cmd.xfer < 4) {
293 goto illegal_request;
294 }
295 r->len = scsi_device_get_sense(r->req.dev, r->buf, req->cmd.xfer,
296 (req->cmd.buf[1] & 1) == 0);
297 break;
298 default:
299 scsi_req_build_sense(req, SENSE_CODE(LUN_NOT_SUPPORTED));
300 scsi_req_complete(req, CHECK_CONDITION);
301 return 0;
302 illegal_request:
303 scsi_req_build_sense(req, SENSE_CODE(INVALID_FIELD));
304 scsi_req_complete(req, CHECK_CONDITION);
305 return 0;
306 }
307
308 if (!r->len) {
309 scsi_req_complete(req, GOOD);
310 }
311 return r->len;
312 }
313
314 static void scsi_target_read_data(SCSIRequest *req)
315 {
316 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
317 uint32_t n;
318
319 n = r->len;
320 if (n > 0) {
321 r->len = 0;
322 scsi_req_data(&r->req, n);
323 } else {
324 scsi_req_complete(&r->req, GOOD);
325 }
326 }
327
328 static uint8_t *scsi_target_get_buf(SCSIRequest *req)
329 {
330 SCSITargetReq *r = DO_UPCAST(SCSITargetReq, req, req);
331
332 return r->buf;
333 }
334
335 struct SCSIReqOps reqops_target_command = {
336 .size = sizeof(SCSITargetReq),
337 .send_command = scsi_target_send_command,
338 .read_data = scsi_target_read_data,
339 .get_buf = scsi_target_get_buf,
340 };
341
342
343 SCSIRequest *scsi_req_alloc(SCSIReqOps *reqops, SCSIDevice *d, uint32_t tag,
344 uint32_t lun, void *hba_private)
345 {
346 SCSIRequest *req;
347
348 req = qemu_mallocz(reqops->size);
349 req->refcount = 1;
350 req->bus = scsi_bus_from_device(d);
351 req->dev = d;
352 req->tag = tag;
353 req->lun = lun;
354 req->hba_private = hba_private;
355 req->status = -1;
356 req->sense_len = 0;
357 req->ops = reqops;
358 trace_scsi_req_alloc(req->dev->id, req->lun, req->tag);
359 return req;
360 }
361
362 SCSIRequest *scsi_req_new(SCSIDevice *d, uint32_t tag, uint32_t lun,
363 uint8_t *buf, void *hba_private)
364 {
365 SCSIBus *bus = DO_UPCAST(SCSIBus, qbus, d->qdev.parent_bus);
366 SCSIRequest *req;
367 SCSICommand cmd;
368
369 if (scsi_req_parse(&cmd, d, buf) != 0) {
370 trace_scsi_req_parse_bad(d->id, lun, tag, buf[0]);
371 req = scsi_req_alloc(&reqops_invalid_opcode, d, tag, lun, hba_private);
372 } else {
373 trace_scsi_req_parsed(d->id, lun, tag, buf[0],
374 cmd.mode, cmd.xfer);
375 if (cmd.lba != -1) {
376 trace_scsi_req_parsed_lba(d->id, lun, tag, buf[0],
377 cmd.lba);
378 }
379
380 if ((d->unit_attention.key == UNIT_ATTENTION ||
381 bus->unit_attention.key == UNIT_ATTENTION) &&
382 (buf[0] != INQUIRY &&
383 buf[0] != REPORT_LUNS &&
384 buf[0] != GET_CONFIGURATION &&
385 buf[0] != GET_EVENT_STATUS_NOTIFICATION)) {
386 req = scsi_req_alloc(&reqops_unit_attention, d, tag, lun,
387 hba_private);
388 } else if (lun != d->lun ||
389 buf[0] == REPORT_LUNS ||
390 buf[0] == REQUEST_SENSE) {
391 req = scsi_req_alloc(&reqops_target_command, d, tag, lun,
392 hba_private);
393 } else {
394 req = d->info->alloc_req(d, tag, lun, hba_private);
395 }
396 }
397
398 req->cmd = cmd;
399 switch (buf[0]) {
400 case INQUIRY:
401 trace_scsi_inquiry(d->id, lun, tag, cmd.buf[1], cmd.buf[2]);
402 break;
403 case TEST_UNIT_READY:
404 trace_scsi_test_unit_ready(d->id, lun, tag);
405 break;
406 case REPORT_LUNS:
407 trace_scsi_report_luns(d->id, lun, tag);
408 break;
409 case REQUEST_SENSE:
410 trace_scsi_request_sense(d->id, lun, tag);
411 break;
412 default:
413 break;
414 }
415
416 return req;
417 }
418
419 uint8_t *scsi_req_get_buf(SCSIRequest *req)
420 {
421 return req->ops->get_buf(req);
422 }
423
424 static void scsi_clear_unit_attention(SCSIRequest *req)
425 {
426 SCSISense *ua;
427 if (req->dev->unit_attention.key != UNIT_ATTENTION &&
428 req->bus->unit_attention.key != UNIT_ATTENTION) {
429 return;
430 }
431
432 /*
433 * If an INQUIRY command enters the enabled command state,
434 * the device server shall [not] clear any unit attention condition;
435 * See also MMC-6, paragraphs 6.5 and 6.6.2.
436 */
437 if (req->cmd.buf[0] == INQUIRY ||
438 req->cmd.buf[0] == GET_CONFIGURATION ||
439 req->cmd.buf[0] == GET_EVENT_STATUS_NOTIFICATION) {
440 return;
441 }
442
443 if (req->dev->unit_attention.key == UNIT_ATTENTION) {
444 ua = &req->dev->unit_attention;
445 } else {
446 ua = &req->bus->unit_attention;
447 }
448
449 /*
450 * If a REPORT LUNS command enters the enabled command state, [...]
451 * the device server shall clear any pending unit attention condition
452 * with an additional sense code of REPORTED LUNS DATA HAS CHANGED.
453 */
454 if (req->cmd.buf[0] == REPORT_LUNS &&
455 !(ua->asc == SENSE_CODE(REPORTED_LUNS_CHANGED).asc &&
456 ua->ascq == SENSE_CODE(REPORTED_LUNS_CHANGED).ascq)) {
457 return;
458 }
459
460 *ua = SENSE_CODE(NO_SENSE);
461 }
462
463 int scsi_req_get_sense(SCSIRequest *req, uint8_t *buf, int len)
464 {
465 int ret;
466
467 assert(len >= 14);
468 if (!req->sense_len) {
469 return 0;
470 }
471
472 ret = scsi_build_sense(req->sense, req->sense_len, buf, len, true);
473
474 /*
475 * FIXME: clearing unit attention conditions upon autosense should be done
476 * only if the UA_INTLCK_CTRL field in the Control mode page is set to 00b
477 * (SAM-5, 5.14).
478 *
479 * We assume UA_INTLCK_CTRL to be 00b for HBAs that support autosense, and
480 * 10b for HBAs that do not support it (do not call scsi_req_get_sense).
481 * In the latter case, scsi_req_complete clears unit attention conditions
482 * after moving them to the device's sense buffer.
483 */
484 scsi_clear_unit_attention(req);
485 return ret;
486 }
487
488 int scsi_device_get_sense(SCSIDevice *dev, uint8_t *buf, int len, bool fixed)
489 {
490 return scsi_build_sense(dev->sense, dev->sense_len, buf, len, fixed);
491 }
492
493 void scsi_req_build_sense(SCSIRequest *req, SCSISense sense)
494 {
495 trace_scsi_req_build_sense(req->dev->id, req->lun, req->tag,
496 sense.key, sense.asc, sense.ascq);
497 memset(req->sense, 0, 18);
498 req->sense[0] = 0xf0;
499 req->sense[2] = sense.key;
500 req->sense[12] = sense.asc;
501 req->sense[13] = sense.ascq;
502 req->sense_len = 18;
503 }
504
505 int32_t scsi_req_enqueue(SCSIRequest *req)
506 {
507 int32_t rc;
508
509 assert(!req->enqueued);
510 scsi_req_ref(req);
511 req->enqueued = true;
512 QTAILQ_INSERT_TAIL(&req->dev->requests, req, next);
513
514 scsi_req_ref(req);
515 rc = req->ops->send_command(req, req->cmd.buf);
516 scsi_req_unref(req);
517 return rc;
518 }
519
520 static void scsi_req_dequeue(SCSIRequest *req)
521 {
522 trace_scsi_req_dequeue(req->dev->id, req->lun, req->tag);
523 if (req->enqueued) {
524 QTAILQ_REMOVE(&req->dev->requests, req, next);
525 req->enqueued = false;
526 scsi_req_unref(req);
527 }
528 }
529
530 static int scsi_req_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
531 {
532 switch (buf[0] >> 5) {
533 case 0:
534 cmd->xfer = buf[4];
535 cmd->len = 6;
536 /* length 0 means 256 blocks */
537 if (cmd->xfer == 0) {
538 cmd->xfer = 256;
539 }
540 break;
541 case 1:
542 case 2:
543 cmd->xfer = buf[8] | (buf[7] << 8);
544 cmd->len = 10;
545 break;
546 case 4:
547 cmd->xfer = buf[13] | (buf[12] << 8) | (buf[11] << 16) | (buf[10] << 24);
548 cmd->len = 16;
549 break;
550 case 5:
551 cmd->xfer = buf[9] | (buf[8] << 8) | (buf[7] << 16) | (buf[6] << 24);
552 cmd->len = 12;
553 break;
554 default:
555 return -1;
556 }
557
558 switch (buf[0]) {
559 case TEST_UNIT_READY:
560 case REWIND:
561 case START_STOP:
562 case SEEK_6:
563 case WRITE_FILEMARKS:
564 case SPACE:
565 case RESERVE:
566 case RELEASE:
567 case ERASE:
568 case ALLOW_MEDIUM_REMOVAL:
569 case VERIFY_10:
570 case SEEK_10:
571 case SYNCHRONIZE_CACHE:
572 case LOCK_UNLOCK_CACHE:
573 case LOAD_UNLOAD:
574 case SET_CD_SPEED:
575 case SET_LIMITS:
576 case WRITE_LONG_10:
577 case MOVE_MEDIUM:
578 case UPDATE_BLOCK:
579 cmd->xfer = 0;
580 break;
581 case MODE_SENSE:
582 break;
583 case WRITE_SAME_10:
584 cmd->xfer = 1;
585 break;
586 case READ_CAPACITY_10:
587 cmd->xfer = 8;
588 break;
589 case READ_BLOCK_LIMITS:
590 cmd->xfer = 6;
591 break;
592 case READ_POSITION:
593 cmd->xfer = 20;
594 break;
595 case SEND_VOLUME_TAG:
596 cmd->xfer *= 40;
597 break;
598 case MEDIUM_SCAN:
599 cmd->xfer *= 8;
600 break;
601 case WRITE_10:
602 case WRITE_VERIFY_10:
603 case WRITE_6:
604 case WRITE_12:
605 case WRITE_VERIFY_12:
606 case WRITE_16:
607 case WRITE_VERIFY_16:
608 cmd->xfer *= dev->blocksize;
609 break;
610 case READ_10:
611 case READ_6:
612 case READ_REVERSE:
613 case RECOVER_BUFFERED_DATA:
614 case READ_12:
615 case READ_16:
616 cmd->xfer *= dev->blocksize;
617 break;
618 case INQUIRY:
619 cmd->xfer = buf[4] | (buf[3] << 8);
620 break;
621 case MAINTENANCE_OUT:
622 case MAINTENANCE_IN:
623 if (dev->type == TYPE_ROM) {
624 /* GPCMD_REPORT_KEY and GPCMD_SEND_KEY from multi media commands */
625 cmd->xfer = buf[9] | (buf[8] << 8);
626 }
627 break;
628 }
629 return 0;
630 }
631
632 static int scsi_req_stream_length(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
633 {
634 switch (buf[0]) {
635 /* stream commands */
636 case READ_6:
637 case READ_REVERSE:
638 case RECOVER_BUFFERED_DATA:
639 case WRITE_6:
640 cmd->len = 6;
641 cmd->xfer = buf[4] | (buf[3] << 8) | (buf[2] << 16);
642 if (buf[1] & 0x01) { /* fixed */
643 cmd->xfer *= dev->blocksize;
644 }
645 break;
646 case REWIND:
647 case START_STOP:
648 cmd->len = 6;
649 cmd->xfer = 0;
650 break;
651 /* generic commands */
652 default:
653 return scsi_req_length(cmd, dev, buf);
654 }
655 return 0;
656 }
657
658 static void scsi_cmd_xfer_mode(SCSICommand *cmd)
659 {
660 switch (cmd->buf[0]) {
661 case WRITE_6:
662 case WRITE_10:
663 case WRITE_VERIFY_10:
664 case WRITE_12:
665 case WRITE_VERIFY_12:
666 case WRITE_16:
667 case WRITE_VERIFY_16:
668 case COPY:
669 case COPY_VERIFY:
670 case COMPARE:
671 case CHANGE_DEFINITION:
672 case LOG_SELECT:
673 case MODE_SELECT:
674 case MODE_SELECT_10:
675 case SEND_DIAGNOSTIC:
676 case WRITE_BUFFER:
677 case FORMAT_UNIT:
678 case REASSIGN_BLOCKS:
679 case SEARCH_EQUAL:
680 case SEARCH_HIGH:
681 case SEARCH_LOW:
682 case UPDATE_BLOCK:
683 case WRITE_LONG_10:
684 case WRITE_SAME_10:
685 case SEARCH_HIGH_12:
686 case SEARCH_EQUAL_12:
687 case SEARCH_LOW_12:
688 case MEDIUM_SCAN:
689 case SEND_VOLUME_TAG:
690 case PERSISTENT_RESERVE_OUT:
691 case MAINTENANCE_OUT:
692 cmd->mode = SCSI_XFER_TO_DEV;
693 break;
694 default:
695 if (cmd->xfer)
696 cmd->mode = SCSI_XFER_FROM_DEV;
697 else {
698 cmd->mode = SCSI_XFER_NONE;
699 }
700 break;
701 }
702 }
703
704 static uint64_t scsi_cmd_lba(SCSICommand *cmd)
705 {
706 uint8_t *buf = cmd->buf;
707 uint64_t lba;
708
709 switch (buf[0] >> 5) {
710 case 0:
711 lba = (uint64_t) buf[3] | ((uint64_t) buf[2] << 8) |
712 (((uint64_t) buf[1] & 0x1f) << 16);
713 break;
714 case 1:
715 case 2:
716 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
717 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
718 break;
719 case 4:
720 lba = (uint64_t) buf[9] | ((uint64_t) buf[8] << 8) |
721 ((uint64_t) buf[7] << 16) | ((uint64_t) buf[6] << 24) |
722 ((uint64_t) buf[5] << 32) | ((uint64_t) buf[4] << 40) |
723 ((uint64_t) buf[3] << 48) | ((uint64_t) buf[2] << 56);
724 break;
725 case 5:
726 lba = (uint64_t) buf[5] | ((uint64_t) buf[4] << 8) |
727 ((uint64_t) buf[3] << 16) | ((uint64_t) buf[2] << 24);
728 break;
729 default:
730 lba = -1;
731
732 }
733 return lba;
734 }
735
736 int scsi_req_parse(SCSICommand *cmd, SCSIDevice *dev, uint8_t *buf)
737 {
738 int rc;
739
740 if (dev->type == TYPE_TAPE) {
741 rc = scsi_req_stream_length(cmd, dev, buf);
742 } else {
743 rc = scsi_req_length(cmd, dev, buf);
744 }
745 if (rc != 0)
746 return rc;
747
748 memcpy(cmd->buf, buf, cmd->len);
749 scsi_cmd_xfer_mode(cmd);
750 cmd->lba = scsi_cmd_lba(cmd);
751 return 0;
752 }
753
754 /*
755 * Predefined sense codes
756 */
757
758 /* No sense data available */
759 const struct SCSISense sense_code_NO_SENSE = {
760 .key = NO_SENSE , .asc = 0x00 , .ascq = 0x00
761 };
762
763 /* LUN not ready, Manual intervention required */
764 const struct SCSISense sense_code_LUN_NOT_READY = {
765 .key = NOT_READY, .asc = 0x04, .ascq = 0x03
766 };
767
768 /* LUN not ready, Medium not present */
769 const struct SCSISense sense_code_NO_MEDIUM = {
770 .key = NOT_READY, .asc = 0x3a, .ascq = 0x00
771 };
772
773 /* Hardware error, internal target failure */
774 const struct SCSISense sense_code_TARGET_FAILURE = {
775 .key = HARDWARE_ERROR, .asc = 0x44, .ascq = 0x00
776 };
777
778 /* Illegal request, invalid command operation code */
779 const struct SCSISense sense_code_INVALID_OPCODE = {
780 .key = ILLEGAL_REQUEST, .asc = 0x20, .ascq = 0x00
781 };
782
783 /* Illegal request, LBA out of range */
784 const struct SCSISense sense_code_LBA_OUT_OF_RANGE = {
785 .key = ILLEGAL_REQUEST, .asc = 0x21, .ascq = 0x00
786 };
787
788 /* Illegal request, Invalid field in CDB */
789 const struct SCSISense sense_code_INVALID_FIELD = {
790 .key = ILLEGAL_REQUEST, .asc = 0x24, .ascq = 0x00
791 };
792
793 /* Illegal request, LUN not supported */
794 const struct SCSISense sense_code_LUN_NOT_SUPPORTED = {
795 .key = ILLEGAL_REQUEST, .asc = 0x25, .ascq = 0x00
796 };
797
798 /* Illegal request, Saving parameters not supported */
799 const struct SCSISense sense_code_SAVING_PARAMS_NOT_SUPPORTED = {
800 .key = ILLEGAL_REQUEST, .asc = 0x39, .ascq = 0x00
801 };
802
803 /* Illegal request, Incompatible medium installed */
804 const struct SCSISense sense_code_INCOMPATIBLE_MEDIUM = {
805 .key = ILLEGAL_REQUEST, .asc = 0x30, .ascq = 0x00
806 };
807
808 /* Command aborted, I/O process terminated */
809 const struct SCSISense sense_code_IO_ERROR = {
810 .key = ABORTED_COMMAND, .asc = 0x00, .ascq = 0x06
811 };
812
813 /* Command aborted, I_T Nexus loss occurred */
814 const struct SCSISense sense_code_I_T_NEXUS_LOSS = {
815 .key = ABORTED_COMMAND, .asc = 0x29, .ascq = 0x07
816 };
817
818 /* Command aborted, Logical Unit failure */
819 const struct SCSISense sense_code_LUN_FAILURE = {
820 .key = ABORTED_COMMAND, .asc = 0x3e, .ascq = 0x01
821 };
822
823 /* Unit attention, Power on, reset or bus device reset occurred */
824 const struct SCSISense sense_code_RESET = {
825 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x00
826 };
827
828 /* Unit attention, Medium may have changed */
829 const struct SCSISense sense_code_MEDIUM_CHANGED = {
830 .key = UNIT_ATTENTION, .asc = 0x28, .ascq = 0x00
831 };
832
833 /* Unit attention, Reported LUNs data has changed */
834 const struct SCSISense sense_code_REPORTED_LUNS_CHANGED = {
835 .key = UNIT_ATTENTION, .asc = 0x3f, .ascq = 0x0e
836 };
837
838 /* Unit attention, Device internal reset */
839 const struct SCSISense sense_code_DEVICE_INTERNAL_RESET = {
840 .key = UNIT_ATTENTION, .asc = 0x29, .ascq = 0x04
841 };
842
843 /*
844 * scsi_build_sense
845 *
846 * Convert between fixed and descriptor sense buffers
847 */
848 int scsi_build_sense(uint8_t *in_buf, int in_len,
849 uint8_t *buf, int len, bool fixed)
850 {
851 bool fixed_in;
852 SCSISense sense;
853 if (!fixed && len < 8) {
854 return 0;
855 }
856
857 if (in_len == 0) {
858 sense.key = NO_SENSE;
859 sense.asc = 0;
860 sense.ascq = 0;
861 } else {
862 fixed_in = (in_buf[0] & 2) == 0;
863
864 if (fixed == fixed_in) {
865 memcpy(buf, in_buf, MIN(len, in_len));
866 return MIN(len, in_len);
867 }
868
869 if (fixed_in) {
870 sense.key = in_buf[2];
871 sense.asc = in_buf[12];
872 sense.ascq = in_buf[13];
873 } else {
874 sense.key = in_buf[1];
875 sense.asc = in_buf[2];
876 sense.ascq = in_buf[3];
877 }
878 }
879
880 memset(buf, 0, len);
881 if (fixed) {
882 /* Return fixed format sense buffer */
883 buf[0] = 0xf0;
884 buf[2] = sense.key;
885 buf[7] = 7;
886 buf[12] = sense.asc;
887 buf[13] = sense.ascq;
888 return MIN(len, 18);
889 } else {
890 /* Return descriptor format sense buffer */
891 buf[0] = 0x72;
892 buf[1] = sense.key;
893 buf[2] = sense.asc;
894 buf[3] = sense.ascq;
895 return 8;
896 }
897 }
898
899 static const char *scsi_command_name(uint8_t cmd)
900 {
901 static const char *names[] = {
902 [ TEST_UNIT_READY ] = "TEST_UNIT_READY",
903 [ REWIND ] = "REWIND",
904 [ REQUEST_SENSE ] = "REQUEST_SENSE",
905 [ FORMAT_UNIT ] = "FORMAT_UNIT",
906 [ READ_BLOCK_LIMITS ] = "READ_BLOCK_LIMITS",
907 [ REASSIGN_BLOCKS ] = "REASSIGN_BLOCKS",
908 [ READ_6 ] = "READ_6",
909 [ WRITE_6 ] = "WRITE_6",
910 [ SEEK_6 ] = "SEEK_6",
911 [ READ_REVERSE ] = "READ_REVERSE",
912 [ WRITE_FILEMARKS ] = "WRITE_FILEMARKS",
913 [ SPACE ] = "SPACE",
914 [ INQUIRY ] = "INQUIRY",
915 [ RECOVER_BUFFERED_DATA ] = "RECOVER_BUFFERED_DATA",
916 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
917 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
918 [ MODE_SELECT ] = "MODE_SELECT",
919 [ RESERVE ] = "RESERVE",
920 [ RELEASE ] = "RELEASE",
921 [ COPY ] = "COPY",
922 [ ERASE ] = "ERASE",
923 [ MODE_SENSE ] = "MODE_SENSE",
924 [ START_STOP ] = "START_STOP",
925 [ RECEIVE_DIAGNOSTIC ] = "RECEIVE_DIAGNOSTIC",
926 [ SEND_DIAGNOSTIC ] = "SEND_DIAGNOSTIC",
927 [ ALLOW_MEDIUM_REMOVAL ] = "ALLOW_MEDIUM_REMOVAL",
928 [ READ_CAPACITY_10 ] = "READ_CAPACITY_10",
929 [ READ_10 ] = "READ_10",
930 [ WRITE_10 ] = "WRITE_10",
931 [ SEEK_10 ] = "SEEK_10",
932 [ WRITE_VERIFY_10 ] = "WRITE_VERIFY_10",
933 [ VERIFY_10 ] = "VERIFY_10",
934 [ SEARCH_HIGH ] = "SEARCH_HIGH",
935 [ SEARCH_EQUAL ] = "SEARCH_EQUAL",
936 [ SEARCH_LOW ] = "SEARCH_LOW",
937 [ SET_LIMITS ] = "SET_LIMITS",
938 [ PRE_FETCH ] = "PRE_FETCH",
939 /* READ_POSITION and PRE_FETCH use the same operation code */
940 [ SYNCHRONIZE_CACHE ] = "SYNCHRONIZE_CACHE",
941 [ LOCK_UNLOCK_CACHE ] = "LOCK_UNLOCK_CACHE",
942 [ READ_DEFECT_DATA ] = "READ_DEFECT_DATA",
943 [ MEDIUM_SCAN ] = "MEDIUM_SCAN",
944 [ COMPARE ] = "COMPARE",
945 [ COPY_VERIFY ] = "COPY_VERIFY",
946 [ WRITE_BUFFER ] = "WRITE_BUFFER",
947 [ READ_BUFFER ] = "READ_BUFFER",
948 [ UPDATE_BLOCK ] = "UPDATE_BLOCK",
949 [ READ_LONG_10 ] = "READ_LONG_10",
950 [ WRITE_LONG_10 ] = "WRITE_LONG_10",
951 [ CHANGE_DEFINITION ] = "CHANGE_DEFINITION",
952 [ WRITE_SAME_10 ] = "WRITE_SAME_10",
953 [ UNMAP ] = "UNMAP",
954 [ READ_TOC ] = "READ_TOC",
955 [ REPORT_DENSITY_SUPPORT ] = "REPORT_DENSITY_SUPPORT",
956 [ GET_CONFIGURATION ] = "GET_CONFIGURATION",
957 [ LOG_SELECT ] = "LOG_SELECT",
958 [ LOG_SENSE ] = "LOG_SENSE",
959 [ MODE_SELECT_10 ] = "MODE_SELECT_10",
960 [ RESERVE_10 ] = "RESERVE_10",
961 [ RELEASE_10 ] = "RELEASE_10",
962 [ MODE_SENSE_10 ] = "MODE_SENSE_10",
963 [ PERSISTENT_RESERVE_IN ] = "PERSISTENT_RESERVE_IN",
964 [ PERSISTENT_RESERVE_OUT ] = "PERSISTENT_RESERVE_OUT",
965 [ WRITE_FILEMARKS_16 ] = "WRITE_FILEMARKS_16",
966 [ EXTENDED_COPY ] = "EXTENDED_COPY",
967 [ ATA_PASSTHROUGH ] = "ATA_PASSTHROUGH",
968 [ ACCESS_CONTROL_IN ] = "ACCESS_CONTROL_IN",
969 [ ACCESS_CONTROL_OUT ] = "ACCESS_CONTROL_OUT",
970 [ READ_16 ] = "READ_16",
971 [ COMPARE_AND_WRITE ] = "COMPARE_AND_WRITE",
972 [ WRITE_16 ] = "WRITE_16",
973 [ WRITE_VERIFY_16 ] = "WRITE_VERIFY_16",
974 [ VERIFY_16 ] = "VERIFY_16",
975 [ SYNCHRONIZE_CACHE_16 ] = "SYNCHRONIZE_CACHE_16",
976 [ LOCATE_16 ] = "LOCATE_16",
977 [ WRITE_SAME_16 ] = "WRITE_SAME_16",
978 [ ERASE_16 ] = "ERASE_16",
979 [ SERVICE_ACTION_IN ] = "SERVICE_ACTION_IN",
980 [ WRITE_LONG_16 ] = "WRITE_LONG_16",
981 [ REPORT_LUNS ] = "REPORT_LUNS",
982 [ BLANK ] = "BLANK",
983 [ MAINTENANCE_IN ] = "MAINTENANCE_IN",
984 [ MAINTENANCE_OUT ] = "MAINTENANCE_OUT",
985 [ MOVE_MEDIUM ] = "MOVE_MEDIUM",
986 [ LOAD_UNLOAD ] = "LOAD_UNLOAD",
987 [ READ_12 ] = "READ_12",
988 [ WRITE_12 ] = "WRITE_12",
989 [ WRITE_VERIFY_12 ] = "WRITE_VERIFY_12",
990 [ VERIFY_12 ] = "VERIFY_12",
991 [ SEARCH_HIGH_12 ] = "SEARCH_HIGH_12",
992 [ SEARCH_EQUAL_12 ] = "SEARCH_EQUAL_12",
993 [ SEARCH_LOW_12 ] = "SEARCH_LOW_12",
994 [ READ_ELEMENT_STATUS ] = "READ_ELEMENT_STATUS",
995 [ SEND_VOLUME_TAG ] = "SEND_VOLUME_TAG",
996 [ READ_DEFECT_DATA_12 ] = "READ_DEFECT_DATA_12",
997 [ SET_CD_SPEED ] = "SET_CD_SPEED",
998 };
999
1000 if (cmd >= ARRAY_SIZE(names) || names[cmd] == NULL)
1001 return "*UNKNOWN*";
1002 return names[cmd];
1003 }
1004
1005 SCSIRequest *scsi_req_ref(SCSIRequest *req)
1006 {
1007 req->refcount++;
1008 return req;
1009 }
1010
1011 void scsi_req_unref(SCSIRequest *req)
1012 {
1013 if (--req->refcount == 0) {
1014 if (req->ops->free_req) {
1015 req->ops->free_req(req);
1016 }
1017 qemu_free(req);
1018 }
1019 }
1020
1021 /* Tell the device that we finished processing this chunk of I/O. It
1022 will start the next chunk or complete the command. */
1023 void scsi_req_continue(SCSIRequest *req)
1024 {
1025 trace_scsi_req_continue(req->dev->id, req->lun, req->tag);
1026 if (req->cmd.mode == SCSI_XFER_TO_DEV) {
1027 req->ops->write_data(req);
1028 } else {
1029 req->ops->read_data(req);
1030 }
1031 }
1032
1033 /* Called by the devices when data is ready for the HBA. The HBA should
1034 start a DMA operation to read or fill the device's data buffer.
1035 Once it completes, calling scsi_req_continue will restart I/O. */
1036 void scsi_req_data(SCSIRequest *req, int len)
1037 {
1038 trace_scsi_req_data(req->dev->id, req->lun, req->tag, len);
1039 req->bus->ops->transfer_data(req, len);
1040 }
1041
1042 void scsi_req_print(SCSIRequest *req)
1043 {
1044 FILE *fp = stderr;
1045 int i;
1046
1047 fprintf(fp, "[%s id=%d] %s",
1048 req->dev->qdev.parent_bus->name,
1049 req->dev->id,
1050 scsi_command_name(req->cmd.buf[0]));
1051 for (i = 1; i < req->cmd.len; i++) {
1052 fprintf(fp, " 0x%02x", req->cmd.buf[i]);
1053 }
1054 switch (req->cmd.mode) {
1055 case SCSI_XFER_NONE:
1056 fprintf(fp, " - none\n");
1057 break;
1058 case SCSI_XFER_FROM_DEV:
1059 fprintf(fp, " - from-dev len=%zd\n", req->cmd.xfer);
1060 break;
1061 case SCSI_XFER_TO_DEV:
1062 fprintf(fp, " - to-dev len=%zd\n", req->cmd.xfer);
1063 break;
1064 default:
1065 fprintf(fp, " - Oops\n");
1066 break;
1067 }
1068 }
1069
1070 void scsi_req_complete(SCSIRequest *req, int status)
1071 {
1072 assert(req->status == -1);
1073 req->status = status;
1074
1075 assert(req->sense_len < sizeof(req->sense));
1076 if (status == GOOD) {
1077 req->sense_len = 0;
1078 }
1079
1080 if (req->sense_len) {
1081 memcpy(req->dev->sense, req->sense, req->sense_len);
1082 }
1083 req->dev->sense_len = req->sense_len;
1084
1085 /*
1086 * Unit attention state is now stored in the device's sense buffer
1087 * if the HBA didn't do autosense. Clear the pending unit attention
1088 * flags.
1089 */
1090 scsi_clear_unit_attention(req);
1091
1092 scsi_req_ref(req);
1093 scsi_req_dequeue(req);
1094 req->bus->ops->complete(req, req->status);
1095 scsi_req_unref(req);
1096 }
1097
1098 void scsi_req_cancel(SCSIRequest *req)
1099 {
1100 if (req->ops->cancel_io) {
1101 req->ops->cancel_io(req);
1102 }
1103 scsi_req_ref(req);
1104 scsi_req_dequeue(req);
1105 if (req->bus->ops->cancel) {
1106 req->bus->ops->cancel(req);
1107 }
1108 scsi_req_unref(req);
1109 }
1110
1111 void scsi_req_abort(SCSIRequest *req, int status)
1112 {
1113 if (req->ops->cancel_io) {
1114 req->ops->cancel_io(req);
1115 }
1116 scsi_req_complete(req, status);
1117 }
1118
1119 void scsi_device_purge_requests(SCSIDevice *sdev, SCSISense sense)
1120 {
1121 SCSIRequest *req;
1122
1123 while (!QTAILQ_EMPTY(&sdev->requests)) {
1124 req = QTAILQ_FIRST(&sdev->requests);
1125 scsi_req_cancel(req);
1126 }
1127 sdev->unit_attention = sense;
1128 }
1129
1130 static char *scsibus_get_fw_dev_path(DeviceState *dev)
1131 {
1132 SCSIDevice *d = DO_UPCAST(SCSIDevice, qdev, dev);
1133 SCSIBus *bus = scsi_bus_from_device(d);
1134 char path[100];
1135 int i;
1136
1137 for (i = 0; i < bus->ndev; i++) {
1138 if (bus->devs[i] == d) {
1139 break;
1140 }
1141 }
1142
1143 assert(i != bus->ndev);
1144
1145 snprintf(path, sizeof(path), "%s@%x", qdev_fw_name(dev), i);
1146
1147 return strdup(path);
1148 }