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