1 /* 2 * Virtio SCSI HBA 3 * 4 * Copyright IBM, Corp. 2010 5 * Copyright Red Hat, Inc. 2011 6 * 7 * Authors: 8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> 9 * Paolo Bonzini <pbonzini@redhat.com> 10 * 11 * This work is licensed under the terms of the GNU GPL, version 2 or later. 12 * See the COPYING file in the top-level directory. 13 * 14 */ 15 16 #include "hw/virtio/virtio-scsi.h" 17 #include "qemu/error-report.h" 18 #include "qemu/iov.h" 19 #include <hw/scsi/scsi.h> 20 #include <block/scsi.h> 21 #include <hw/virtio/virtio-bus.h> 22 23 typedef struct VirtIOSCSIReq { 24 VirtIOSCSI *dev; 25 VirtQueue *vq; 26 VirtQueueElement elem; 27 QEMUSGList qsgl; 28 SCSIRequest *sreq; 29 size_t resp_size; 30 enum SCSIXferMode mode; 31 QEMUIOVector resp_iov; 32 union { 33 VirtIOSCSICmdResp cmd; 34 VirtIOSCSICtrlTMFResp tmf; 35 VirtIOSCSICtrlANResp an; 36 VirtIOSCSIEvent event; 37 } resp; 38 union { 39 struct { 40 VirtIOSCSICmdReq cmd; 41 uint8_t cdb[]; 42 } QEMU_PACKED; 43 VirtIOSCSICtrlTMFReq tmf; 44 VirtIOSCSICtrlANReq an; 45 } req; 46 } VirtIOSCSIReq; 47 48 QEMU_BUILD_BUG_ON(offsetof(VirtIOSCSIReq, req.cdb) != 49 offsetof(VirtIOSCSIReq, req.cmd) + sizeof(VirtIOSCSICmdReq)); 50 51 static inline int virtio_scsi_get_lun(uint8_t *lun) 52 { 53 return ((lun[2] << 8) | lun[3]) & 0x3FFF; 54 } 55 56 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun) 57 { 58 if (lun[0] != 1) { 59 return NULL; 60 } 61 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { 62 return NULL; 63 } 64 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); 65 } 66 67 static VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq) 68 { 69 VirtIOSCSIReq *req; 70 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 71 72 req = g_malloc0(sizeof(*req) + vs->cdb_size); 73 74 req->vq = vq; 75 req->dev = s; 76 req->sreq = NULL; 77 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory); 78 qemu_iovec_init(&req->resp_iov, 1); 79 return req; 80 } 81 82 static void virtio_scsi_free_req(VirtIOSCSIReq *req) 83 { 84 qemu_iovec_destroy(&req->resp_iov); 85 qemu_sglist_destroy(&req->qsgl); 86 g_free(req); 87 } 88 89 static void virtio_scsi_complete_req(VirtIOSCSIReq *req) 90 { 91 VirtIOSCSI *s = req->dev; 92 VirtQueue *vq = req->vq; 93 VirtIODevice *vdev = VIRTIO_DEVICE(s); 94 95 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size); 96 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); 97 if (req->sreq) { 98 req->sreq->hba_private = NULL; 99 scsi_req_unref(req->sreq); 100 } 101 virtio_scsi_free_req(req); 102 virtio_notify(vdev, vq); 103 } 104 105 static void virtio_scsi_bad_req(void) 106 { 107 error_report("wrong size for virtio-scsi headers"); 108 exit(1); 109 } 110 111 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov, 112 hwaddr *addr, int num, size_t skip) 113 { 114 QEMUSGList *qsgl = &req->qsgl; 115 size_t copied = 0; 116 117 while (num) { 118 if (skip >= iov->iov_len) { 119 skip -= iov->iov_len; 120 } else { 121 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip); 122 copied += iov->iov_len - skip; 123 skip = 0; 124 } 125 iov++; 126 addr++; 127 num--; 128 } 129 130 assert(skip == 0); 131 return copied; 132 } 133 134 static int virtio_scsi_parse_req(VirtIOSCSIReq *req, 135 unsigned req_size, unsigned resp_size) 136 { 137 size_t in_size, out_size; 138 139 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 140 &req->req, req_size) < req_size) { 141 return -EINVAL; 142 } 143 144 if (qemu_iovec_concat_iov(&req->resp_iov, 145 req->elem.in_sg, req->elem.in_num, 0, 146 resp_size) < resp_size) { 147 return -EINVAL; 148 } 149 req->resp_size = resp_size; 150 151 out_size = qemu_sgl_concat(req, req->elem.out_sg, 152 &req->elem.out_addr[0], req->elem.out_num, 153 req_size); 154 in_size = qemu_sgl_concat(req, req->elem.in_sg, 155 &req->elem.in_addr[0], req->elem.in_num, 156 resp_size); 157 158 if (out_size && in_size) { 159 return -ENOTSUP; 160 } 161 162 if (out_size) { 163 req->mode = SCSI_XFER_TO_DEV; 164 } else if (in_size) { 165 req->mode = SCSI_XFER_FROM_DEV; 166 } 167 168 return 0; 169 } 170 171 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq) 172 { 173 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq); 174 if (!virtqueue_pop(vq, &req->elem)) { 175 virtio_scsi_free_req(req); 176 return NULL; 177 } 178 return req; 179 } 180 181 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) 182 { 183 VirtIOSCSIReq *req = sreq->hba_private; 184 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); 185 uint32_t n = virtio_queue_get_id(req->vq) - 2; 186 187 assert(n < vs->conf.num_queues); 188 qemu_put_be32s(f, &n); 189 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 190 } 191 192 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) 193 { 194 SCSIBus *bus = sreq->bus; 195 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 196 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 197 VirtIOSCSIReq *req; 198 uint32_t n; 199 200 qemu_get_be32s(f, &n); 201 assert(n < vs->conf.num_queues); 202 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]); 203 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 204 /* TODO: add a way for SCSIBusInfo's load_request to fail, 205 * and fail migration instead of asserting here. 206 * When we do, we might be able to re-enable NDEBUG below. 207 */ 208 #ifdef NDEBUG 209 #error building with NDEBUG is not supported 210 #endif 211 assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg)); 212 assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg)); 213 214 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 215 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { 216 error_report("invalid SCSI request migration data"); 217 exit(1); 218 } 219 220 scsi_req_ref(sreq); 221 req->sreq = sreq; 222 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 223 assert(req->sreq->cmd.mode == req->mode); 224 } 225 return req; 226 } 227 228 static void virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) 229 { 230 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun); 231 SCSIRequest *r, *next; 232 BusChild *kid; 233 int target; 234 235 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */ 236 req->resp.tmf.response = VIRTIO_SCSI_S_OK; 237 238 tswap32s(&req->req.tmf.subtype); 239 switch (req->req.tmf.subtype) { 240 case VIRTIO_SCSI_T_TMF_ABORT_TASK: 241 case VIRTIO_SCSI_T_TMF_QUERY_TASK: 242 if (!d) { 243 goto fail; 244 } 245 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 246 goto incorrect_lun; 247 } 248 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 249 VirtIOSCSIReq *cmd_req = r->hba_private; 250 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) { 251 break; 252 } 253 } 254 if (r) { 255 /* 256 * Assert that the request has not been completed yet, we 257 * check for it in the loop above. 258 */ 259 assert(r->hba_private); 260 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) { 261 /* "If the specified command is present in the task set, then 262 * return a service response set to FUNCTION SUCCEEDED". 263 */ 264 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 265 } else { 266 scsi_req_cancel(r); 267 } 268 } 269 break; 270 271 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: 272 if (!d) { 273 goto fail; 274 } 275 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 276 goto incorrect_lun; 277 } 278 s->resetting++; 279 qdev_reset_all(&d->qdev); 280 s->resetting--; 281 break; 282 283 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET: 284 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: 285 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET: 286 if (!d) { 287 goto fail; 288 } 289 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 290 goto incorrect_lun; 291 } 292 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 293 if (r->hba_private) { 294 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) { 295 /* "If there is any command present in the task set, then 296 * return a service response set to FUNCTION SUCCEEDED". 297 */ 298 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 299 break; 300 } else { 301 scsi_req_cancel(r); 302 } 303 } 304 } 305 break; 306 307 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: 308 target = req->req.tmf.lun[1]; 309 s->resetting++; 310 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { 311 d = DO_UPCAST(SCSIDevice, qdev, kid->child); 312 if (d->channel == 0 && d->id == target) { 313 qdev_reset_all(&d->qdev); 314 } 315 } 316 s->resetting--; 317 break; 318 319 case VIRTIO_SCSI_T_TMF_CLEAR_ACA: 320 default: 321 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED; 322 break; 323 } 324 325 return; 326 327 incorrect_lun: 328 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN; 329 return; 330 331 fail: 332 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; 333 } 334 335 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 336 { 337 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 338 VirtIOSCSIReq *req; 339 340 while ((req = virtio_scsi_pop_req(s, vq))) { 341 int type; 342 343 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 344 &type, sizeof(type)) < sizeof(type)) { 345 virtio_scsi_bad_req(); 346 continue; 347 } 348 349 tswap32s(&req->req.tmf.type); 350 if (req->req.tmf.type == VIRTIO_SCSI_T_TMF) { 351 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq), 352 sizeof(VirtIOSCSICtrlTMFResp)) < 0) { 353 virtio_scsi_bad_req(); 354 } else { 355 virtio_scsi_do_tmf(s, req); 356 } 357 358 } else if (req->req.tmf.type == VIRTIO_SCSI_T_AN_QUERY || 359 req->req.tmf.type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { 360 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq), 361 sizeof(VirtIOSCSICtrlANResp)) < 0) { 362 virtio_scsi_bad_req(); 363 } else { 364 req->resp.an.event_actual = 0; 365 req->resp.an.response = VIRTIO_SCSI_S_OK; 366 } 367 } 368 virtio_scsi_complete_req(req); 369 } 370 } 371 372 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) 373 { 374 /* Sense data is not in req->resp and is copied separately 375 * in virtio_scsi_command_complete. 376 */ 377 req->resp_size = sizeof(VirtIOSCSICmdResp); 378 virtio_scsi_complete_req(req); 379 } 380 381 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, 382 size_t resid) 383 { 384 VirtIOSCSIReq *req = r->hba_private; 385 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 386 uint32_t sense_len; 387 388 if (r->io_canceled) { 389 return; 390 } 391 392 req->resp.cmd.response = VIRTIO_SCSI_S_OK; 393 req->resp.cmd.status = status; 394 if (req->resp.cmd.status == GOOD) { 395 req->resp.cmd.resid = tswap32(resid); 396 } else { 397 req->resp.cmd.resid = 0; 398 sense_len = scsi_req_get_sense(r, sense, sizeof(sense)); 399 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd)); 400 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd), 401 &req->resp, sense_len); 402 req->resp.cmd.sense_len = tswap32(sense_len); 403 } 404 virtio_scsi_complete_cmd_req(req); 405 } 406 407 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) 408 { 409 VirtIOSCSIReq *req = r->hba_private; 410 411 return &req->qsgl; 412 } 413 414 static void virtio_scsi_request_cancelled(SCSIRequest *r) 415 { 416 VirtIOSCSIReq *req = r->hba_private; 417 418 if (!req) { 419 return; 420 } 421 if (req->dev->resetting) { 422 req->resp.cmd.response = VIRTIO_SCSI_S_RESET; 423 } else { 424 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; 425 } 426 virtio_scsi_complete_cmd_req(req); 427 } 428 429 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) 430 { 431 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; 432 virtio_scsi_complete_cmd_req(req); 433 } 434 435 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) 436 { 437 /* use non-QOM casts in the data path */ 438 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 439 VirtIOSCSICommon *vs = &s->parent_obj; 440 441 VirtIOSCSIReq *req; 442 int n; 443 444 while ((req = virtio_scsi_pop_req(s, vq))) { 445 SCSIDevice *d; 446 int rc; 447 448 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 449 sizeof(VirtIOSCSICmdResp) + vs->sense_size); 450 if (rc < 0) { 451 if (rc == -ENOTSUP) { 452 virtio_scsi_fail_cmd_req(req); 453 } else { 454 virtio_scsi_bad_req(); 455 } 456 continue; 457 } 458 459 d = virtio_scsi_device_find(s, req->req.cmd.lun); 460 if (!d) { 461 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 462 virtio_scsi_complete_cmd_req(req); 463 continue; 464 } 465 req->sreq = scsi_req_new(d, req->req.cmd.tag, 466 virtio_scsi_get_lun(req->req.cmd.lun), 467 req->req.cdb, req); 468 469 if (req->sreq->cmd.mode != SCSI_XFER_NONE 470 && (req->sreq->cmd.mode != req->mode || 471 req->sreq->cmd.xfer > req->qsgl.size)) { 472 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN; 473 virtio_scsi_complete_cmd_req(req); 474 continue; 475 } 476 477 n = scsi_req_enqueue(req->sreq); 478 if (n) { 479 scsi_req_continue(req->sreq); 480 } 481 } 482 } 483 484 static void virtio_scsi_get_config(VirtIODevice *vdev, 485 uint8_t *config) 486 { 487 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 488 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); 489 490 stl_p(&scsiconf->num_queues, s->conf.num_queues); 491 stl_p(&scsiconf->seg_max, 128 - 2); 492 stl_p(&scsiconf->max_sectors, s->conf.max_sectors); 493 stl_p(&scsiconf->cmd_per_lun, s->conf.cmd_per_lun); 494 stl_p(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); 495 stl_p(&scsiconf->sense_size, s->sense_size); 496 stl_p(&scsiconf->cdb_size, s->cdb_size); 497 stw_p(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); 498 stw_p(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); 499 stl_p(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); 500 } 501 502 static void virtio_scsi_set_config(VirtIODevice *vdev, 503 const uint8_t *config) 504 { 505 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 506 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 507 508 if ((uint32_t) ldl_p(&scsiconf->sense_size) >= 65536 || 509 (uint32_t) ldl_p(&scsiconf->cdb_size) >= 256) { 510 error_report("bad data written to virtio-scsi configuration space"); 511 exit(1); 512 } 513 514 vs->sense_size = ldl_p(&scsiconf->sense_size); 515 vs->cdb_size = ldl_p(&scsiconf->cdb_size); 516 } 517 518 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev, 519 uint32_t requested_features) 520 { 521 return requested_features; 522 } 523 524 static void virtio_scsi_reset(VirtIODevice *vdev) 525 { 526 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 527 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 528 529 s->resetting++; 530 qbus_reset_all(&s->bus.qbus); 531 s->resetting--; 532 533 vs->sense_size = VIRTIO_SCSI_SENSE_SIZE; 534 vs->cdb_size = VIRTIO_SCSI_CDB_SIZE; 535 s->events_dropped = false; 536 } 537 538 /* The device does not have anything to save beyond the virtio data. 539 * Request data is saved with callbacks from SCSI devices. 540 */ 541 static void virtio_scsi_save(QEMUFile *f, void *opaque) 542 { 543 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 544 virtio_save(vdev, f); 545 } 546 547 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id) 548 { 549 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 550 int ret; 551 552 ret = virtio_load(vdev, f); 553 if (ret) { 554 return ret; 555 } 556 return 0; 557 } 558 559 static void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 560 uint32_t event, uint32_t reason) 561 { 562 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 563 VirtIOSCSIReq *req; 564 VirtIOSCSIEvent *evt; 565 VirtIODevice *vdev = VIRTIO_DEVICE(s); 566 int in_size; 567 568 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 569 return; 570 } 571 572 req = virtio_scsi_pop_req(s, vs->event_vq); 573 if (!req) { 574 s->events_dropped = true; 575 return; 576 } 577 578 if (req->elem.out_num) { 579 virtio_scsi_bad_req(); 580 } 581 582 if (s->events_dropped) { 583 event |= VIRTIO_SCSI_T_EVENTS_MISSED; 584 s->events_dropped = false; 585 } 586 587 in_size = iov_size(req->elem.in_sg, req->elem.in_num); 588 if (in_size < sizeof(VirtIOSCSIEvent)) { 589 virtio_scsi_bad_req(); 590 } 591 592 evt = &req->resp.event; 593 memset(evt, 0, sizeof(VirtIOSCSIEvent)); 594 evt->event = event; 595 evt->reason = reason; 596 if (!dev) { 597 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED); 598 } else { 599 evt->lun[0] = 1; 600 evt->lun[1] = dev->id; 601 602 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ 603 if (dev->lun >= 256) { 604 evt->lun[2] = (dev->lun >> 8) | 0x40; 605 } 606 evt->lun[3] = dev->lun & 0xFF; 607 } 608 virtio_scsi_complete_req(req); 609 } 610 611 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) 612 { 613 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 614 615 if (s->events_dropped) { 616 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 617 } 618 } 619 620 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) 621 { 622 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 623 VirtIODevice *vdev = VIRTIO_DEVICE(s); 624 625 if (((vdev->guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) && 626 dev->type != TYPE_ROM) { 627 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE, 628 sense.asc | (sense.ascq << 8)); 629 } 630 } 631 632 static void virtio_scsi_hotplug(SCSIBus *bus, SCSIDevice *dev) 633 { 634 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 635 VirtIODevice *vdev = VIRTIO_DEVICE(s); 636 637 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) { 638 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET, 639 VIRTIO_SCSI_EVT_RESET_RESCAN); 640 } 641 } 642 643 static void virtio_scsi_hot_unplug(SCSIBus *bus, SCSIDevice *dev) 644 { 645 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 646 VirtIODevice *vdev = VIRTIO_DEVICE(s); 647 648 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) { 649 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET, 650 VIRTIO_SCSI_EVT_RESET_REMOVED); 651 } 652 } 653 654 static struct SCSIBusInfo virtio_scsi_scsi_info = { 655 .tcq = true, 656 .max_channel = VIRTIO_SCSI_MAX_CHANNEL, 657 .max_target = VIRTIO_SCSI_MAX_TARGET, 658 .max_lun = VIRTIO_SCSI_MAX_LUN, 659 660 .complete = virtio_scsi_command_complete, 661 .cancel = virtio_scsi_request_cancelled, 662 .change = virtio_scsi_change, 663 .hotplug = virtio_scsi_hotplug, 664 .hot_unplug = virtio_scsi_hot_unplug, 665 .get_sg_list = virtio_scsi_get_sg_list, 666 .save_request = virtio_scsi_save_request, 667 .load_request = virtio_scsi_load_request, 668 }; 669 670 void virtio_scsi_common_realize(DeviceState *dev, Error **errp, 671 HandleOutput ctrl, HandleOutput evt, 672 HandleOutput cmd) 673 { 674 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 675 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); 676 int i; 677 678 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, 679 sizeof(VirtIOSCSIConfig)); 680 681 s->cmd_vqs = g_malloc0(s->conf.num_queues * sizeof(VirtQueue *)); 682 s->sense_size = VIRTIO_SCSI_SENSE_SIZE; 683 s->cdb_size = VIRTIO_SCSI_CDB_SIZE; 684 685 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 686 ctrl); 687 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 688 evt); 689 for (i = 0; i < s->conf.num_queues; i++) { 690 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 691 cmd); 692 } 693 } 694 695 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) 696 { 697 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 698 VirtIOSCSI *s = VIRTIO_SCSI(dev); 699 static int virtio_scsi_id; 700 Error *err = NULL; 701 702 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl, 703 virtio_scsi_handle_event, 704 virtio_scsi_handle_cmd); 705 if (err != NULL) { 706 error_propagate(errp, err); 707 return; 708 } 709 710 scsi_bus_new(&s->bus, sizeof(s->bus), dev, 711 &virtio_scsi_scsi_info, vdev->bus_name); 712 713 if (!dev->hotplugged) { 714 scsi_bus_legacy_handle_cmdline(&s->bus, &err); 715 if (err != NULL) { 716 error_propagate(errp, err); 717 return; 718 } 719 } 720 721 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1, 722 virtio_scsi_save, virtio_scsi_load, s); 723 } 724 725 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp) 726 { 727 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 728 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 729 730 g_free(vs->cmd_vqs); 731 virtio_cleanup(vdev); 732 } 733 734 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp) 735 { 736 VirtIOSCSI *s = VIRTIO_SCSI(dev); 737 738 unregister_savevm(dev, "virtio-scsi", s); 739 740 virtio_scsi_common_unrealize(dev, errp); 741 } 742 743 static Property virtio_scsi_properties[] = { 744 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf), 745 DEFINE_PROP_END_OF_LIST(), 746 }; 747 748 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) 749 { 750 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 751 DeviceClass *dc = DEVICE_CLASS(klass); 752 753 vdc->get_config = virtio_scsi_get_config; 754 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 755 } 756 757 static void virtio_scsi_class_init(ObjectClass *klass, void *data) 758 { 759 DeviceClass *dc = DEVICE_CLASS(klass); 760 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 761 762 dc->props = virtio_scsi_properties; 763 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 764 vdc->realize = virtio_scsi_device_realize; 765 vdc->unrealize = virtio_scsi_device_unrealize; 766 vdc->set_config = virtio_scsi_set_config; 767 vdc->get_features = virtio_scsi_get_features; 768 vdc->reset = virtio_scsi_reset; 769 } 770 771 static const TypeInfo virtio_scsi_common_info = { 772 .name = TYPE_VIRTIO_SCSI_COMMON, 773 .parent = TYPE_VIRTIO_DEVICE, 774 .instance_size = sizeof(VirtIOSCSICommon), 775 .abstract = true, 776 .class_init = virtio_scsi_common_class_init, 777 }; 778 779 static const TypeInfo virtio_scsi_info = { 780 .name = TYPE_VIRTIO_SCSI, 781 .parent = TYPE_VIRTIO_SCSI_COMMON, 782 .instance_size = sizeof(VirtIOSCSI), 783 .class_init = virtio_scsi_class_init, 784 }; 785 786 static void virtio_register_types(void) 787 { 788 type_register_static(&virtio_scsi_common_info); 789 type_register_static(&virtio_scsi_info); 790 } 791 792 type_init(virtio_register_types) 793