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 "qemu/osdep.h" 17 #include "qapi/error.h" 18 #include "standard-headers/linux/virtio_ids.h" 19 #include "hw/virtio/virtio-scsi.h" 20 #include "migration/qemu-file-types.h" 21 #include "qemu/defer-call.h" 22 #include "qemu/error-report.h" 23 #include "qemu/iov.h" 24 #include "qemu/module.h" 25 #include "sysemu/block-backend.h" 26 #include "sysemu/dma.h" 27 #include "hw/qdev-properties.h" 28 #include "hw/scsi/scsi.h" 29 #include "scsi/constants.h" 30 #include "hw/virtio/virtio-bus.h" 31 #include "hw/virtio/virtio-access.h" 32 #include "trace.h" 33 34 typedef struct VirtIOSCSIReq { 35 /* 36 * Note: 37 * - fields up to resp_iov are initialized by virtio_scsi_init_req; 38 * - fields starting at vring are zeroed by virtio_scsi_init_req. 39 */ 40 VirtQueueElement elem; 41 42 VirtIOSCSI *dev; 43 VirtQueue *vq; 44 QEMUSGList qsgl; 45 QEMUIOVector resp_iov; 46 47 /* Used for two-stage request submission and TMFs deferred to BH */ 48 QTAILQ_ENTRY(VirtIOSCSIReq) next; 49 50 /* Used for cancellation of request during TMFs */ 51 int remaining; 52 53 SCSIRequest *sreq; 54 size_t resp_size; 55 enum SCSIXferMode mode; 56 union { 57 VirtIOSCSICmdResp cmd; 58 VirtIOSCSICtrlTMFResp tmf; 59 VirtIOSCSICtrlANResp an; 60 VirtIOSCSIEvent event; 61 } resp; 62 union { 63 VirtIOSCSICmdReq cmd; 64 VirtIOSCSICtrlTMFReq tmf; 65 VirtIOSCSICtrlANReq an; 66 } req; 67 } VirtIOSCSIReq; 68 69 static inline int virtio_scsi_get_lun(uint8_t *lun) 70 { 71 return ((lun[2] << 8) | lun[3]) & 0x3FFF; 72 } 73 74 static inline SCSIDevice *virtio_scsi_device_get(VirtIOSCSI *s, uint8_t *lun) 75 { 76 if (lun[0] != 1) { 77 return NULL; 78 } 79 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { 80 return NULL; 81 } 82 return scsi_device_get(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); 83 } 84 85 static void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) 86 { 87 VirtIODevice *vdev = VIRTIO_DEVICE(s); 88 const size_t zero_skip = 89 offsetof(VirtIOSCSIReq, resp_iov) + sizeof(req->resp_iov); 90 91 req->vq = vq; 92 req->dev = s; 93 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, vdev->dma_as); 94 qemu_iovec_init(&req->resp_iov, 1); 95 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip); 96 } 97 98 static void virtio_scsi_free_req(VirtIOSCSIReq *req) 99 { 100 qemu_iovec_destroy(&req->resp_iov); 101 qemu_sglist_destroy(&req->qsgl); 102 g_free(req); 103 } 104 105 static void virtio_scsi_complete_req(VirtIOSCSIReq *req) 106 { 107 VirtIOSCSI *s = req->dev; 108 VirtQueue *vq = req->vq; 109 VirtIODevice *vdev = VIRTIO_DEVICE(s); 110 111 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size); 112 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); 113 if (s->dataplane_started && !s->dataplane_fenced) { 114 virtio_notify_irqfd(vdev, vq); 115 } else { 116 virtio_notify(vdev, vq); 117 } 118 119 if (req->sreq) { 120 req->sreq->hba_private = NULL; 121 scsi_req_unref(req->sreq); 122 } 123 virtio_scsi_free_req(req); 124 } 125 126 static void virtio_scsi_bad_req(VirtIOSCSIReq *req) 127 { 128 virtio_error(VIRTIO_DEVICE(req->dev), "wrong size for virtio-scsi headers"); 129 virtqueue_detach_element(req->vq, &req->elem, 0); 130 virtio_scsi_free_req(req); 131 } 132 133 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov, 134 hwaddr *addr, int num, size_t skip) 135 { 136 QEMUSGList *qsgl = &req->qsgl; 137 size_t copied = 0; 138 139 while (num) { 140 if (skip >= iov->iov_len) { 141 skip -= iov->iov_len; 142 } else { 143 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip); 144 copied += iov->iov_len - skip; 145 skip = 0; 146 } 147 iov++; 148 addr++; 149 num--; 150 } 151 152 assert(skip == 0); 153 return copied; 154 } 155 156 static int virtio_scsi_parse_req(VirtIOSCSIReq *req, 157 unsigned req_size, unsigned resp_size) 158 { 159 VirtIODevice *vdev = (VirtIODevice *) req->dev; 160 size_t in_size, out_size; 161 162 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 163 &req->req, req_size) < req_size) { 164 return -EINVAL; 165 } 166 167 if (qemu_iovec_concat_iov(&req->resp_iov, 168 req->elem.in_sg, req->elem.in_num, 0, 169 resp_size) < resp_size) { 170 return -EINVAL; 171 } 172 173 req->resp_size = resp_size; 174 175 /* Old BIOSes left some padding by mistake after the req_size/resp_size. 176 * As a workaround, always consider the first buffer as the virtio-scsi 177 * request/response, making the payload start at the second element 178 * of the iovec. 179 * 180 * The actual length of the response header, stored in req->resp_size, 181 * does not change. 182 * 183 * TODO: always disable this workaround for virtio 1.0 devices. 184 */ 185 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) { 186 if (req->elem.out_num) { 187 req_size = req->elem.out_sg[0].iov_len; 188 } 189 if (req->elem.in_num) { 190 resp_size = req->elem.in_sg[0].iov_len; 191 } 192 } 193 194 out_size = qemu_sgl_concat(req, req->elem.out_sg, 195 &req->elem.out_addr[0], req->elem.out_num, 196 req_size); 197 in_size = qemu_sgl_concat(req, req->elem.in_sg, 198 &req->elem.in_addr[0], req->elem.in_num, 199 resp_size); 200 201 if (out_size && in_size) { 202 return -ENOTSUP; 203 } 204 205 if (out_size) { 206 req->mode = SCSI_XFER_TO_DEV; 207 } else if (in_size) { 208 req->mode = SCSI_XFER_FROM_DEV; 209 } 210 211 return 0; 212 } 213 214 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq) 215 { 216 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s; 217 VirtIOSCSIReq *req; 218 219 req = virtqueue_pop(vq, sizeof(VirtIOSCSIReq) + vs->cdb_size); 220 if (!req) { 221 return NULL; 222 } 223 virtio_scsi_init_req(s, vq, req); 224 return req; 225 } 226 227 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) 228 { 229 VirtIOSCSIReq *req = sreq->hba_private; 230 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); 231 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); 232 uint32_t n = virtio_get_queue_index(req->vq) - VIRTIO_SCSI_VQ_NUM_FIXED; 233 234 assert(n < vs->conf.num_queues); 235 qemu_put_be32s(f, &n); 236 qemu_put_virtqueue_element(vdev, f, &req->elem); 237 } 238 239 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) 240 { 241 SCSIBus *bus = sreq->bus; 242 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 243 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 244 VirtIODevice *vdev = VIRTIO_DEVICE(s); 245 VirtIOSCSIReq *req; 246 uint32_t n; 247 248 qemu_get_be32s(f, &n); 249 assert(n < vs->conf.num_queues); 250 req = qemu_get_virtqueue_element(vdev, f, 251 sizeof(VirtIOSCSIReq) + vs->cdb_size); 252 virtio_scsi_init_req(s, vs->cmd_vqs[n], req); 253 254 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 255 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { 256 error_report("invalid SCSI request migration data"); 257 exit(1); 258 } 259 260 scsi_req_ref(sreq); 261 req->sreq = sreq; 262 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 263 assert(req->sreq->cmd.mode == req->mode); 264 } 265 return req; 266 } 267 268 typedef struct { 269 Notifier notifier; 270 VirtIOSCSIReq *tmf_req; 271 } VirtIOSCSICancelNotifier; 272 273 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data) 274 { 275 VirtIOSCSICancelNotifier *n = container_of(notifier, 276 VirtIOSCSICancelNotifier, 277 notifier); 278 279 if (--n->tmf_req->remaining == 0) { 280 VirtIOSCSIReq *req = n->tmf_req; 281 282 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun), 283 req->req.tmf.tag, req->resp.tmf.response); 284 virtio_scsi_complete_req(req); 285 } 286 g_free(n); 287 } 288 289 static inline void virtio_scsi_ctx_check(VirtIOSCSI *s, SCSIDevice *d) 290 { 291 if (s->dataplane_started && d && blk_is_available(d->conf.blk)) { 292 assert(blk_get_aio_context(d->conf.blk) == s->ctx); 293 } 294 } 295 296 static void virtio_scsi_do_one_tmf_bh(VirtIOSCSIReq *req) 297 { 298 VirtIOSCSI *s = req->dev; 299 SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun); 300 BusChild *kid; 301 int target; 302 303 switch (req->req.tmf.subtype) { 304 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: 305 if (!d) { 306 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; 307 goto out; 308 } 309 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 310 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN; 311 goto out; 312 } 313 qatomic_inc(&s->resetting); 314 device_cold_reset(&d->qdev); 315 qatomic_dec(&s->resetting); 316 break; 317 318 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: 319 target = req->req.tmf.lun[1]; 320 qatomic_inc(&s->resetting); 321 322 rcu_read_lock(); 323 QTAILQ_FOREACH_RCU(kid, &s->bus.qbus.children, sibling) { 324 SCSIDevice *d1 = SCSI_DEVICE(kid->child); 325 if (d1->channel == 0 && d1->id == target) { 326 device_cold_reset(&d1->qdev); 327 } 328 } 329 rcu_read_unlock(); 330 331 qatomic_dec(&s->resetting); 332 break; 333 334 default: 335 g_assert_not_reached(); 336 break; 337 } 338 339 out: 340 object_unref(OBJECT(d)); 341 342 virtio_scsi_acquire(s); 343 virtio_scsi_complete_req(req); 344 virtio_scsi_release(s); 345 } 346 347 /* Some TMFs must be processed from the main loop thread */ 348 static void virtio_scsi_do_tmf_bh(void *opaque) 349 { 350 VirtIOSCSI *s = opaque; 351 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); 352 VirtIOSCSIReq *req; 353 VirtIOSCSIReq *tmp; 354 355 GLOBAL_STATE_CODE(); 356 357 virtio_scsi_acquire(s); 358 359 QTAILQ_FOREACH_SAFE(req, &s->tmf_bh_list, next, tmp) { 360 QTAILQ_REMOVE(&s->tmf_bh_list, req, next); 361 QTAILQ_INSERT_TAIL(&reqs, req, next); 362 } 363 364 qemu_bh_delete(s->tmf_bh); 365 s->tmf_bh = NULL; 366 367 virtio_scsi_release(s); 368 369 QTAILQ_FOREACH_SAFE(req, &reqs, next, tmp) { 370 QTAILQ_REMOVE(&reqs, req, next); 371 virtio_scsi_do_one_tmf_bh(req); 372 } 373 } 374 375 static void virtio_scsi_reset_tmf_bh(VirtIOSCSI *s) 376 { 377 VirtIOSCSIReq *req; 378 VirtIOSCSIReq *tmp; 379 380 GLOBAL_STATE_CODE(); 381 382 virtio_scsi_acquire(s); 383 384 if (s->tmf_bh) { 385 qemu_bh_delete(s->tmf_bh); 386 s->tmf_bh = NULL; 387 } 388 389 QTAILQ_FOREACH_SAFE(req, &s->tmf_bh_list, next, tmp) { 390 QTAILQ_REMOVE(&s->tmf_bh_list, req, next); 391 392 /* SAM-6 6.3.2 Hard reset */ 393 req->resp.tmf.response = VIRTIO_SCSI_S_TARGET_FAILURE; 394 virtio_scsi_complete_req(req); 395 } 396 397 virtio_scsi_release(s); 398 } 399 400 static void virtio_scsi_defer_tmf_to_bh(VirtIOSCSIReq *req) 401 { 402 VirtIOSCSI *s = req->dev; 403 404 QTAILQ_INSERT_TAIL(&s->tmf_bh_list, req, next); 405 406 if (!s->tmf_bh) { 407 s->tmf_bh = qemu_bh_new(virtio_scsi_do_tmf_bh, s); 408 qemu_bh_schedule(s->tmf_bh); 409 } 410 } 411 412 /* Return 0 if the request is ready to be completed and return to guest; 413 * -EINPROGRESS if the request is submitted and will be completed later, in the 414 * case of async cancellation. */ 415 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) 416 { 417 SCSIDevice *d = virtio_scsi_device_get(s, req->req.tmf.lun); 418 SCSIRequest *r, *next; 419 int ret = 0; 420 421 virtio_scsi_ctx_check(s, d); 422 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */ 423 req->resp.tmf.response = VIRTIO_SCSI_S_OK; 424 425 /* 426 * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s() 427 * to avoid compiler errors. 428 */ 429 req->req.tmf.subtype = 430 virtio_tswap32(VIRTIO_DEVICE(s), req->req.tmf.subtype); 431 432 trace_virtio_scsi_tmf_req(virtio_scsi_get_lun(req->req.tmf.lun), 433 req->req.tmf.tag, req->req.tmf.subtype); 434 435 switch (req->req.tmf.subtype) { 436 case VIRTIO_SCSI_T_TMF_ABORT_TASK: 437 case VIRTIO_SCSI_T_TMF_QUERY_TASK: 438 if (!d) { 439 goto fail; 440 } 441 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 442 goto incorrect_lun; 443 } 444 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 445 VirtIOSCSIReq *cmd_req = r->hba_private; 446 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) { 447 break; 448 } 449 } 450 if (r) { 451 /* 452 * Assert that the request has not been completed yet, we 453 * check for it in the loop above. 454 */ 455 assert(r->hba_private); 456 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) { 457 /* "If the specified command is present in the task set, then 458 * return a service response set to FUNCTION SUCCEEDED". 459 */ 460 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 461 } else { 462 VirtIOSCSICancelNotifier *notifier; 463 464 req->remaining = 1; 465 notifier = g_new(VirtIOSCSICancelNotifier, 1); 466 notifier->tmf_req = req; 467 notifier->notifier.notify = virtio_scsi_cancel_notify; 468 scsi_req_cancel_async(r, ¬ifier->notifier); 469 ret = -EINPROGRESS; 470 } 471 } 472 break; 473 474 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: 475 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: 476 virtio_scsi_defer_tmf_to_bh(req); 477 ret = -EINPROGRESS; 478 break; 479 480 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET: 481 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: 482 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET: 483 if (!d) { 484 goto fail; 485 } 486 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 487 goto incorrect_lun; 488 } 489 490 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns. 491 * This way, if the bus starts calling back to the notifiers 492 * even before we finish the loop, virtio_scsi_cancel_notify 493 * will not complete the TMF too early. 494 */ 495 req->remaining = 1; 496 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 497 if (r->hba_private) { 498 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) { 499 /* "If there is any command present in the task set, then 500 * return a service response set to FUNCTION SUCCEEDED". 501 */ 502 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 503 break; 504 } else { 505 VirtIOSCSICancelNotifier *notifier; 506 507 req->remaining++; 508 notifier = g_new(VirtIOSCSICancelNotifier, 1); 509 notifier->notifier.notify = virtio_scsi_cancel_notify; 510 notifier->tmf_req = req; 511 scsi_req_cancel_async(r, ¬ifier->notifier); 512 } 513 } 514 } 515 if (--req->remaining > 0) { 516 ret = -EINPROGRESS; 517 } 518 break; 519 520 case VIRTIO_SCSI_T_TMF_CLEAR_ACA: 521 default: 522 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED; 523 break; 524 } 525 526 object_unref(OBJECT(d)); 527 return ret; 528 529 incorrect_lun: 530 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN; 531 object_unref(OBJECT(d)); 532 return ret; 533 534 fail: 535 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; 536 object_unref(OBJECT(d)); 537 return ret; 538 } 539 540 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req) 541 { 542 VirtIODevice *vdev = (VirtIODevice *)s; 543 uint32_t type; 544 int r = 0; 545 546 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 547 &type, sizeof(type)) < sizeof(type)) { 548 virtio_scsi_bad_req(req); 549 return; 550 } 551 552 virtio_tswap32s(vdev, &type); 553 if (type == VIRTIO_SCSI_T_TMF) { 554 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq), 555 sizeof(VirtIOSCSICtrlTMFResp)) < 0) { 556 virtio_scsi_bad_req(req); 557 return; 558 } else { 559 r = virtio_scsi_do_tmf(s, req); 560 } 561 562 } else if (type == VIRTIO_SCSI_T_AN_QUERY || 563 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { 564 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq), 565 sizeof(VirtIOSCSICtrlANResp)) < 0) { 566 virtio_scsi_bad_req(req); 567 return; 568 } else { 569 req->req.an.event_requested = 570 virtio_tswap32(VIRTIO_DEVICE(s), req->req.an.event_requested); 571 trace_virtio_scsi_an_req(virtio_scsi_get_lun(req->req.an.lun), 572 req->req.an.event_requested); 573 req->resp.an.event_actual = 0; 574 req->resp.an.response = VIRTIO_SCSI_S_OK; 575 } 576 } 577 if (r == 0) { 578 if (type == VIRTIO_SCSI_T_TMF) 579 trace_virtio_scsi_tmf_resp(virtio_scsi_get_lun(req->req.tmf.lun), 580 req->req.tmf.tag, 581 req->resp.tmf.response); 582 else if (type == VIRTIO_SCSI_T_AN_QUERY || 583 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) 584 trace_virtio_scsi_an_resp(virtio_scsi_get_lun(req->req.an.lun), 585 req->resp.an.response); 586 virtio_scsi_complete_req(req); 587 } else { 588 assert(r == -EINPROGRESS); 589 } 590 } 591 592 static void virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq) 593 { 594 VirtIOSCSIReq *req; 595 596 while ((req = virtio_scsi_pop_req(s, vq))) { 597 virtio_scsi_handle_ctrl_req(s, req); 598 } 599 } 600 601 /* 602 * If dataplane is configured but not yet started, do so now and return true on 603 * success. 604 * 605 * Dataplane is started by the core virtio code but virtqueue handler functions 606 * can also be invoked when a guest kicks before DRIVER_OK, so this helper 607 * function helps us deal with manually starting ioeventfd in that case. 608 */ 609 static bool virtio_scsi_defer_to_dataplane(VirtIOSCSI *s) 610 { 611 if (!s->ctx || s->dataplane_started) { 612 return false; 613 } 614 615 virtio_device_start_ioeventfd(&s->parent_obj.parent_obj); 616 return !s->dataplane_fenced; 617 } 618 619 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 620 { 621 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 622 623 if (virtio_scsi_defer_to_dataplane(s)) { 624 return; 625 } 626 627 virtio_scsi_acquire(s); 628 virtio_scsi_handle_ctrl_vq(s, vq); 629 virtio_scsi_release(s); 630 } 631 632 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) 633 { 634 trace_virtio_scsi_cmd_resp(virtio_scsi_get_lun(req->req.cmd.lun), 635 req->req.cmd.tag, 636 req->resp.cmd.response, 637 req->resp.cmd.status); 638 /* Sense data is not in req->resp and is copied separately 639 * in virtio_scsi_command_complete. 640 */ 641 req->resp_size = sizeof(VirtIOSCSICmdResp); 642 virtio_scsi_complete_req(req); 643 } 644 645 static void virtio_scsi_command_failed(SCSIRequest *r) 646 { 647 VirtIOSCSIReq *req = r->hba_private; 648 649 if (r->io_canceled) { 650 return; 651 } 652 653 req->resp.cmd.status = GOOD; 654 switch (r->host_status) { 655 case SCSI_HOST_NO_LUN: 656 req->resp.cmd.response = VIRTIO_SCSI_S_INCORRECT_LUN; 657 break; 658 case SCSI_HOST_BUSY: 659 req->resp.cmd.response = VIRTIO_SCSI_S_BUSY; 660 break; 661 case SCSI_HOST_TIME_OUT: 662 case SCSI_HOST_ABORTED: 663 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; 664 break; 665 case SCSI_HOST_BAD_RESPONSE: 666 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 667 break; 668 case SCSI_HOST_RESET: 669 req->resp.cmd.response = VIRTIO_SCSI_S_RESET; 670 break; 671 case SCSI_HOST_TRANSPORT_DISRUPTED: 672 req->resp.cmd.response = VIRTIO_SCSI_S_TRANSPORT_FAILURE; 673 break; 674 case SCSI_HOST_TARGET_FAILURE: 675 req->resp.cmd.response = VIRTIO_SCSI_S_TARGET_FAILURE; 676 break; 677 case SCSI_HOST_RESERVATION_ERROR: 678 req->resp.cmd.response = VIRTIO_SCSI_S_NEXUS_FAILURE; 679 break; 680 case SCSI_HOST_ALLOCATION_FAILURE: 681 case SCSI_HOST_MEDIUM_ERROR: 682 case SCSI_HOST_ERROR: 683 default: 684 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; 685 break; 686 } 687 virtio_scsi_complete_cmd_req(req); 688 } 689 690 static void virtio_scsi_command_complete(SCSIRequest *r, size_t resid) 691 { 692 VirtIOSCSIReq *req = r->hba_private; 693 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 694 uint32_t sense_len; 695 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); 696 697 if (r->io_canceled) { 698 return; 699 } 700 701 req->resp.cmd.response = VIRTIO_SCSI_S_OK; 702 req->resp.cmd.status = r->status; 703 if (req->resp.cmd.status == GOOD) { 704 req->resp.cmd.resid = virtio_tswap32(vdev, resid); 705 } else { 706 req->resp.cmd.resid = 0; 707 sense_len = scsi_req_get_sense(r, sense, sizeof(sense)); 708 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd)); 709 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd), 710 sense, sense_len); 711 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len); 712 } 713 virtio_scsi_complete_cmd_req(req); 714 } 715 716 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, 717 uint8_t *buf, size_t buf_len, 718 void *hba_private) 719 { 720 VirtIOSCSIReq *req = hba_private; 721 722 if (cmd->len == 0) { 723 cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE); 724 memcpy(cmd->buf, buf, cmd->len); 725 } 726 727 /* Extract the direction and mode directly from the request, for 728 * host device passthrough. 729 */ 730 cmd->xfer = req->qsgl.size; 731 cmd->mode = req->mode; 732 return 0; 733 } 734 735 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) 736 { 737 VirtIOSCSIReq *req = r->hba_private; 738 739 return &req->qsgl; 740 } 741 742 static void virtio_scsi_request_cancelled(SCSIRequest *r) 743 { 744 VirtIOSCSIReq *req = r->hba_private; 745 746 if (!req) { 747 return; 748 } 749 if (qatomic_read(&req->dev->resetting)) { 750 req->resp.cmd.response = VIRTIO_SCSI_S_RESET; 751 } else { 752 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; 753 } 754 virtio_scsi_complete_cmd_req(req); 755 } 756 757 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) 758 { 759 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; 760 virtio_scsi_complete_cmd_req(req); 761 } 762 763 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req) 764 { 765 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 766 SCSIDevice *d; 767 int rc; 768 769 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 770 sizeof(VirtIOSCSICmdResp) + vs->sense_size); 771 if (rc < 0) { 772 if (rc == -ENOTSUP) { 773 virtio_scsi_fail_cmd_req(req); 774 return -ENOTSUP; 775 } else { 776 virtio_scsi_bad_req(req); 777 return -EINVAL; 778 } 779 } 780 trace_virtio_scsi_cmd_req(virtio_scsi_get_lun(req->req.cmd.lun), 781 req->req.cmd.tag, req->req.cmd.cdb[0]); 782 783 d = virtio_scsi_device_get(s, req->req.cmd.lun); 784 if (!d) { 785 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 786 virtio_scsi_complete_cmd_req(req); 787 return -ENOENT; 788 } 789 virtio_scsi_ctx_check(s, d); 790 req->sreq = scsi_req_new(d, req->req.cmd.tag, 791 virtio_scsi_get_lun(req->req.cmd.lun), 792 req->req.cmd.cdb, vs->cdb_size, req); 793 794 if (req->sreq->cmd.mode != SCSI_XFER_NONE 795 && (req->sreq->cmd.mode != req->mode || 796 req->sreq->cmd.xfer > req->qsgl.size)) { 797 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN; 798 virtio_scsi_complete_cmd_req(req); 799 object_unref(OBJECT(d)); 800 return -ENOBUFS; 801 } 802 scsi_req_ref(req->sreq); 803 defer_call_begin(); 804 object_unref(OBJECT(d)); 805 return 0; 806 } 807 808 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req) 809 { 810 SCSIRequest *sreq = req->sreq; 811 if (scsi_req_enqueue(sreq)) { 812 scsi_req_continue(sreq); 813 } 814 defer_call_end(); 815 scsi_req_unref(sreq); 816 } 817 818 static void virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq) 819 { 820 VirtIOSCSIReq *req, *next; 821 int ret = 0; 822 bool suppress_notifications = virtio_queue_get_notification(vq); 823 824 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); 825 826 do { 827 if (suppress_notifications) { 828 virtio_queue_set_notification(vq, 0); 829 } 830 831 while ((req = virtio_scsi_pop_req(s, vq))) { 832 ret = virtio_scsi_handle_cmd_req_prepare(s, req); 833 if (!ret) { 834 QTAILQ_INSERT_TAIL(&reqs, req, next); 835 } else if (ret == -EINVAL) { 836 /* The device is broken and shouldn't process any request */ 837 while (!QTAILQ_EMPTY(&reqs)) { 838 req = QTAILQ_FIRST(&reqs); 839 QTAILQ_REMOVE(&reqs, req, next); 840 defer_call_end(); 841 scsi_req_unref(req->sreq); 842 virtqueue_detach_element(req->vq, &req->elem, 0); 843 virtio_scsi_free_req(req); 844 } 845 } 846 } 847 848 if (suppress_notifications) { 849 virtio_queue_set_notification(vq, 1); 850 } 851 } while (ret != -EINVAL && !virtio_queue_empty(vq)); 852 853 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) { 854 virtio_scsi_handle_cmd_req_submit(s, req); 855 } 856 } 857 858 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) 859 { 860 /* use non-QOM casts in the data path */ 861 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 862 863 if (virtio_scsi_defer_to_dataplane(s)) { 864 return; 865 } 866 867 virtio_scsi_acquire(s); 868 virtio_scsi_handle_cmd_vq(s, vq); 869 virtio_scsi_release(s); 870 } 871 872 static void virtio_scsi_get_config(VirtIODevice *vdev, 873 uint8_t *config) 874 { 875 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 876 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); 877 878 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues); 879 virtio_stl_p(vdev, &scsiconf->seg_max, 880 s->conf.seg_max_adjust ? s->conf.virtqueue_size - 2 : 128 - 2); 881 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors); 882 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun); 883 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); 884 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size); 885 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size); 886 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); 887 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); 888 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); 889 } 890 891 static void virtio_scsi_set_config(VirtIODevice *vdev, 892 const uint8_t *config) 893 { 894 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 895 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 896 897 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 || 898 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) { 899 virtio_error(vdev, 900 "bad data written to virtio-scsi configuration space"); 901 return; 902 } 903 904 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size); 905 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size); 906 } 907 908 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev, 909 uint64_t requested_features, 910 Error **errp) 911 { 912 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 913 914 /* Firstly sync all virtio-scsi possible supported features */ 915 requested_features |= s->host_features; 916 return requested_features; 917 } 918 919 static void virtio_scsi_reset(VirtIODevice *vdev) 920 { 921 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 922 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 923 924 assert(!s->dataplane_started); 925 926 virtio_scsi_reset_tmf_bh(s); 927 928 qatomic_inc(&s->resetting); 929 bus_cold_reset(BUS(&s->bus)); 930 qatomic_dec(&s->resetting); 931 932 vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 933 vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 934 s->events_dropped = false; 935 } 936 937 typedef struct { 938 uint32_t event; 939 uint32_t reason; 940 union { 941 /* Used by messages specific to a device */ 942 struct { 943 uint32_t id; 944 uint32_t lun; 945 } address; 946 }; 947 } VirtIOSCSIEventInfo; 948 949 static void virtio_scsi_push_event(VirtIOSCSI *s, 950 const VirtIOSCSIEventInfo *info) 951 { 952 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 953 VirtIOSCSIReq *req; 954 VirtIOSCSIEvent *evt; 955 VirtIODevice *vdev = VIRTIO_DEVICE(s); 956 uint32_t event = info->event; 957 uint32_t reason = info->reason; 958 959 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 960 return; 961 } 962 963 req = virtio_scsi_pop_req(s, vs->event_vq); 964 if (!req) { 965 s->events_dropped = true; 966 return; 967 } 968 969 if (s->events_dropped) { 970 event |= VIRTIO_SCSI_T_EVENTS_MISSED; 971 s->events_dropped = false; 972 } 973 974 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) { 975 virtio_scsi_bad_req(req); 976 return; 977 } 978 979 evt = &req->resp.event; 980 memset(evt, 0, sizeof(VirtIOSCSIEvent)); 981 evt->event = virtio_tswap32(vdev, event); 982 evt->reason = virtio_tswap32(vdev, reason); 983 if (event != VIRTIO_SCSI_T_EVENTS_MISSED) { 984 evt->lun[0] = 1; 985 evt->lun[1] = info->address.id; 986 987 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ 988 if (info->address.lun >= 256) { 989 evt->lun[2] = (info->address.lun >> 8) | 0x40; 990 } 991 evt->lun[3] = info->address.lun & 0xFF; 992 } 993 trace_virtio_scsi_event(virtio_scsi_get_lun(evt->lun), event, reason); 994 995 virtio_scsi_complete_req(req); 996 } 997 998 static void virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq) 999 { 1000 if (s->events_dropped) { 1001 VirtIOSCSIEventInfo info = { 1002 .event = VIRTIO_SCSI_T_NO_EVENT, 1003 }; 1004 virtio_scsi_push_event(s, &info); 1005 } 1006 } 1007 1008 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) 1009 { 1010 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 1011 1012 if (virtio_scsi_defer_to_dataplane(s)) { 1013 return; 1014 } 1015 1016 virtio_scsi_acquire(s); 1017 virtio_scsi_handle_event_vq(s, vq); 1018 virtio_scsi_release(s); 1019 } 1020 1021 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) 1022 { 1023 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 1024 VirtIODevice *vdev = VIRTIO_DEVICE(s); 1025 1026 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) && 1027 dev->type != TYPE_ROM) { 1028 VirtIOSCSIEventInfo info = { 1029 .event = VIRTIO_SCSI_T_PARAM_CHANGE, 1030 .reason = sense.asc | (sense.ascq << 8), 1031 .address = { 1032 .id = dev->id, 1033 .lun = dev->lun, 1034 }, 1035 }; 1036 1037 virtio_scsi_acquire(s); 1038 virtio_scsi_push_event(s, &info); 1039 virtio_scsi_release(s); 1040 } 1041 } 1042 1043 static void virtio_scsi_pre_hotplug(HotplugHandler *hotplug_dev, 1044 DeviceState *dev, Error **errp) 1045 { 1046 SCSIDevice *sd = SCSI_DEVICE(dev); 1047 sd->hba_supports_iothread = true; 1048 } 1049 1050 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, 1051 Error **errp) 1052 { 1053 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 1054 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 1055 SCSIDevice *sd = SCSI_DEVICE(dev); 1056 AioContext *old_context; 1057 int ret; 1058 1059 if (s->ctx && !s->dataplane_fenced) { 1060 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) { 1061 return; 1062 } 1063 old_context = blk_get_aio_context(sd->conf.blk); 1064 aio_context_acquire(old_context); 1065 ret = blk_set_aio_context(sd->conf.blk, s->ctx, errp); 1066 aio_context_release(old_context); 1067 if (ret < 0) { 1068 return; 1069 } 1070 } 1071 1072 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 1073 VirtIOSCSIEventInfo info = { 1074 .event = VIRTIO_SCSI_T_TRANSPORT_RESET, 1075 .reason = VIRTIO_SCSI_EVT_RESET_RESCAN, 1076 .address = { 1077 .id = sd->id, 1078 .lun = sd->lun, 1079 }, 1080 }; 1081 1082 virtio_scsi_acquire(s); 1083 virtio_scsi_push_event(s, &info); 1084 scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED)); 1085 virtio_scsi_release(s); 1086 } 1087 } 1088 1089 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev, 1090 Error **errp) 1091 { 1092 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 1093 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 1094 SCSIDevice *sd = SCSI_DEVICE(dev); 1095 VirtIOSCSIEventInfo info = { 1096 .event = VIRTIO_SCSI_T_TRANSPORT_RESET, 1097 .reason = VIRTIO_SCSI_EVT_RESET_REMOVED, 1098 .address = { 1099 .id = sd->id, 1100 .lun = sd->lun, 1101 }, 1102 }; 1103 1104 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp); 1105 1106 if (s->ctx) { 1107 virtio_scsi_acquire(s); 1108 /* If other users keep the BlockBackend in the iothread, that's ok */ 1109 blk_set_aio_context(sd->conf.blk, qemu_get_aio_context(), NULL); 1110 virtio_scsi_release(s); 1111 } 1112 1113 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 1114 virtio_scsi_acquire(s); 1115 virtio_scsi_push_event(s, &info); 1116 scsi_bus_set_ua(&s->bus, SENSE_CODE(REPORTED_LUNS_CHANGED)); 1117 virtio_scsi_release(s); 1118 } 1119 } 1120 1121 /* Suspend virtqueue ioeventfd processing during drain */ 1122 static void virtio_scsi_drained_begin(SCSIBus *bus) 1123 { 1124 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 1125 VirtIODevice *vdev = VIRTIO_DEVICE(s); 1126 uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED + 1127 s->parent_obj.conf.num_queues; 1128 1129 /* 1130 * Drain is called when stopping dataplane but the host notifier has 1131 * already been detached. Detaching multiple times is a no-op if nothing 1132 * else is using the monitoring same file descriptor, but avoid it just in 1133 * case. 1134 * 1135 * Also, don't detach if dataplane has not even been started yet because 1136 * the host notifier isn't attached. 1137 */ 1138 if (s->dataplane_stopping || !s->dataplane_started) { 1139 return; 1140 } 1141 1142 for (uint32_t i = 0; i < total_queues; i++) { 1143 VirtQueue *vq = virtio_get_queue(vdev, i); 1144 virtio_queue_aio_detach_host_notifier(vq, s->ctx); 1145 } 1146 } 1147 1148 /* Resume virtqueue ioeventfd processing after drain */ 1149 static void virtio_scsi_drained_end(SCSIBus *bus) 1150 { 1151 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 1152 VirtIODevice *vdev = VIRTIO_DEVICE(s); 1153 uint32_t total_queues = VIRTIO_SCSI_VQ_NUM_FIXED + 1154 s->parent_obj.conf.num_queues; 1155 1156 /* 1157 * Drain is called when stopping dataplane. Keep the host notifier detached 1158 * so it's not left dangling after dataplane is stopped. 1159 * 1160 * Also, don't attach if dataplane has not even been started yet. We're not 1161 * ready. 1162 */ 1163 if (s->dataplane_stopping || !s->dataplane_started) { 1164 return; 1165 } 1166 1167 for (uint32_t i = 0; i < total_queues; i++) { 1168 VirtQueue *vq = virtio_get_queue(vdev, i); 1169 virtio_queue_aio_attach_host_notifier(vq, s->ctx); 1170 } 1171 } 1172 1173 static struct SCSIBusInfo virtio_scsi_scsi_info = { 1174 .tcq = true, 1175 .max_channel = VIRTIO_SCSI_MAX_CHANNEL, 1176 .max_target = VIRTIO_SCSI_MAX_TARGET, 1177 .max_lun = VIRTIO_SCSI_MAX_LUN, 1178 1179 .complete = virtio_scsi_command_complete, 1180 .fail = virtio_scsi_command_failed, 1181 .cancel = virtio_scsi_request_cancelled, 1182 .change = virtio_scsi_change, 1183 .parse_cdb = virtio_scsi_parse_cdb, 1184 .get_sg_list = virtio_scsi_get_sg_list, 1185 .save_request = virtio_scsi_save_request, 1186 .load_request = virtio_scsi_load_request, 1187 .drained_begin = virtio_scsi_drained_begin, 1188 .drained_end = virtio_scsi_drained_end, 1189 }; 1190 1191 void virtio_scsi_common_realize(DeviceState *dev, 1192 VirtIOHandleOutput ctrl, 1193 VirtIOHandleOutput evt, 1194 VirtIOHandleOutput cmd, 1195 Error **errp) 1196 { 1197 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1198 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); 1199 int i; 1200 1201 virtio_init(vdev, VIRTIO_ID_SCSI, sizeof(VirtIOSCSIConfig)); 1202 1203 if (s->conf.num_queues == VIRTIO_SCSI_AUTO_NUM_QUEUES) { 1204 s->conf.num_queues = 1; 1205 } 1206 if (s->conf.num_queues == 0 || 1207 s->conf.num_queues > VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED) { 1208 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 1209 "must be a positive integer less than %d.", 1210 s->conf.num_queues, 1211 VIRTIO_QUEUE_MAX - VIRTIO_SCSI_VQ_NUM_FIXED); 1212 virtio_cleanup(vdev); 1213 return; 1214 } 1215 if (s->conf.virtqueue_size <= 2) { 1216 error_setg(errp, "invalid virtqueue_size property (= %" PRIu32 "), " 1217 "must be > 2", s->conf.virtqueue_size); 1218 return; 1219 } 1220 s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues); 1221 s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 1222 s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 1223 1224 s->ctrl_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, ctrl); 1225 s->event_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, evt); 1226 for (i = 0; i < s->conf.num_queues; i++) { 1227 s->cmd_vqs[i] = virtio_add_queue(vdev, s->conf.virtqueue_size, cmd); 1228 } 1229 } 1230 1231 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) 1232 { 1233 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1234 VirtIOSCSI *s = VIRTIO_SCSI(dev); 1235 Error *err = NULL; 1236 1237 QTAILQ_INIT(&s->tmf_bh_list); 1238 1239 virtio_scsi_common_realize(dev, 1240 virtio_scsi_handle_ctrl, 1241 virtio_scsi_handle_event, 1242 virtio_scsi_handle_cmd, 1243 &err); 1244 if (err != NULL) { 1245 error_propagate(errp, err); 1246 return; 1247 } 1248 1249 scsi_bus_init_named(&s->bus, sizeof(s->bus), dev, 1250 &virtio_scsi_scsi_info, vdev->bus_name); 1251 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */ 1252 qbus_set_hotplug_handler(BUS(&s->bus), OBJECT(dev)); 1253 1254 virtio_scsi_dataplane_setup(s, errp); 1255 } 1256 1257 void virtio_scsi_common_unrealize(DeviceState *dev) 1258 { 1259 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 1260 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 1261 int i; 1262 1263 virtio_delete_queue(vs->ctrl_vq); 1264 virtio_delete_queue(vs->event_vq); 1265 for (i = 0; i < vs->conf.num_queues; i++) { 1266 virtio_delete_queue(vs->cmd_vqs[i]); 1267 } 1268 g_free(vs->cmd_vqs); 1269 virtio_cleanup(vdev); 1270 } 1271 1272 static void virtio_scsi_device_unrealize(DeviceState *dev) 1273 { 1274 VirtIOSCSI *s = VIRTIO_SCSI(dev); 1275 1276 virtio_scsi_reset_tmf_bh(s); 1277 1278 qbus_set_hotplug_handler(BUS(&s->bus), NULL); 1279 virtio_scsi_common_unrealize(dev); 1280 } 1281 1282 static Property virtio_scsi_properties[] = { 1283 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues, 1284 VIRTIO_SCSI_AUTO_NUM_QUEUES), 1285 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI, 1286 parent_obj.conf.virtqueue_size, 256), 1287 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSI, 1288 parent_obj.conf.seg_max_adjust, true), 1289 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors, 1290 0xFFFF), 1291 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun, 1292 128), 1293 DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features, 1294 VIRTIO_SCSI_F_HOTPLUG, true), 1295 DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features, 1296 VIRTIO_SCSI_F_CHANGE, true), 1297 DEFINE_PROP_LINK("iothread", VirtIOSCSI, parent_obj.conf.iothread, 1298 TYPE_IOTHREAD, IOThread *), 1299 DEFINE_PROP_END_OF_LIST(), 1300 }; 1301 1302 static const VMStateDescription vmstate_virtio_scsi = { 1303 .name = "virtio-scsi", 1304 .minimum_version_id = 1, 1305 .version_id = 1, 1306 .fields = (VMStateField[]) { 1307 VMSTATE_VIRTIO_DEVICE, 1308 VMSTATE_END_OF_LIST() 1309 }, 1310 }; 1311 1312 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) 1313 { 1314 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 1315 DeviceClass *dc = DEVICE_CLASS(klass); 1316 1317 vdc->get_config = virtio_scsi_get_config; 1318 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1319 } 1320 1321 static void virtio_scsi_class_init(ObjectClass *klass, void *data) 1322 { 1323 DeviceClass *dc = DEVICE_CLASS(klass); 1324 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 1325 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 1326 1327 device_class_set_props(dc, virtio_scsi_properties); 1328 dc->vmsd = &vmstate_virtio_scsi; 1329 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1330 vdc->realize = virtio_scsi_device_realize; 1331 vdc->unrealize = virtio_scsi_device_unrealize; 1332 vdc->set_config = virtio_scsi_set_config; 1333 vdc->get_features = virtio_scsi_get_features; 1334 vdc->reset = virtio_scsi_reset; 1335 vdc->start_ioeventfd = virtio_scsi_dataplane_start; 1336 vdc->stop_ioeventfd = virtio_scsi_dataplane_stop; 1337 hc->pre_plug = virtio_scsi_pre_hotplug; 1338 hc->plug = virtio_scsi_hotplug; 1339 hc->unplug = virtio_scsi_hotunplug; 1340 } 1341 1342 static const TypeInfo virtio_scsi_common_info = { 1343 .name = TYPE_VIRTIO_SCSI_COMMON, 1344 .parent = TYPE_VIRTIO_DEVICE, 1345 .instance_size = sizeof(VirtIOSCSICommon), 1346 .abstract = true, 1347 .class_init = virtio_scsi_common_class_init, 1348 }; 1349 1350 static const TypeInfo virtio_scsi_info = { 1351 .name = TYPE_VIRTIO_SCSI, 1352 .parent = TYPE_VIRTIO_SCSI_COMMON, 1353 .instance_size = sizeof(VirtIOSCSI), 1354 .class_init = virtio_scsi_class_init, 1355 .interfaces = (InterfaceInfo[]) { 1356 { TYPE_HOTPLUG_HANDLER }, 1357 { } 1358 } 1359 }; 1360 1361 static void virtio_register_types(void) 1362 { 1363 type_register_static(&virtio_scsi_common_info); 1364 type_register_static(&virtio_scsi_info); 1365 } 1366 1367 type_init(virtio_register_types) 1368