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/error-report.h" 22 #include "qemu/iov.h" 23 #include "qemu/module.h" 24 #include "sysemu/block-backend.h" 25 #include "hw/qdev-properties.h" 26 #include "hw/scsi/scsi.h" 27 #include "scsi/constants.h" 28 #include "hw/virtio/virtio-bus.h" 29 #include "hw/virtio/virtio-access.h" 30 31 static inline int virtio_scsi_get_lun(uint8_t *lun) 32 { 33 return ((lun[2] << 8) | lun[3]) & 0x3FFF; 34 } 35 36 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun) 37 { 38 if (lun[0] != 1) { 39 return NULL; 40 } 41 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { 42 return NULL; 43 } 44 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); 45 } 46 47 void virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq, VirtIOSCSIReq *req) 48 { 49 VirtIODevice *vdev = VIRTIO_DEVICE(s); 50 const size_t zero_skip = 51 offsetof(VirtIOSCSIReq, resp_iov) + sizeof(req->resp_iov); 52 53 req->vq = vq; 54 req->dev = s; 55 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, vdev->dma_as); 56 qemu_iovec_init(&req->resp_iov, 1); 57 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip); 58 } 59 60 void virtio_scsi_free_req(VirtIOSCSIReq *req) 61 { 62 qemu_iovec_destroy(&req->resp_iov); 63 qemu_sglist_destroy(&req->qsgl); 64 g_free(req); 65 } 66 67 static void virtio_scsi_complete_req(VirtIOSCSIReq *req) 68 { 69 VirtIOSCSI *s = req->dev; 70 VirtQueue *vq = req->vq; 71 VirtIODevice *vdev = VIRTIO_DEVICE(s); 72 73 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size); 74 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); 75 if (s->dataplane_started && !s->dataplane_fenced) { 76 virtio_notify_irqfd(vdev, vq); 77 } else { 78 virtio_notify(vdev, vq); 79 } 80 81 if (req->sreq) { 82 req->sreq->hba_private = NULL; 83 scsi_req_unref(req->sreq); 84 } 85 virtio_scsi_free_req(req); 86 } 87 88 static void virtio_scsi_bad_req(VirtIOSCSIReq *req) 89 { 90 virtio_error(VIRTIO_DEVICE(req->dev), "wrong size for virtio-scsi headers"); 91 virtqueue_detach_element(req->vq, &req->elem, 0); 92 virtio_scsi_free_req(req); 93 } 94 95 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov, 96 hwaddr *addr, int num, size_t skip) 97 { 98 QEMUSGList *qsgl = &req->qsgl; 99 size_t copied = 0; 100 101 while (num) { 102 if (skip >= iov->iov_len) { 103 skip -= iov->iov_len; 104 } else { 105 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip); 106 copied += iov->iov_len - skip; 107 skip = 0; 108 } 109 iov++; 110 addr++; 111 num--; 112 } 113 114 assert(skip == 0); 115 return copied; 116 } 117 118 static int virtio_scsi_parse_req(VirtIOSCSIReq *req, 119 unsigned req_size, unsigned resp_size) 120 { 121 VirtIODevice *vdev = (VirtIODevice *) req->dev; 122 size_t in_size, out_size; 123 124 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 125 &req->req, req_size) < req_size) { 126 return -EINVAL; 127 } 128 129 if (qemu_iovec_concat_iov(&req->resp_iov, 130 req->elem.in_sg, req->elem.in_num, 0, 131 resp_size) < resp_size) { 132 return -EINVAL; 133 } 134 135 req->resp_size = resp_size; 136 137 /* Old BIOSes left some padding by mistake after the req_size/resp_size. 138 * As a workaround, always consider the first buffer as the virtio-scsi 139 * request/response, making the payload start at the second element 140 * of the iovec. 141 * 142 * The actual length of the response header, stored in req->resp_size, 143 * does not change. 144 * 145 * TODO: always disable this workaround for virtio 1.0 devices. 146 */ 147 if (!virtio_vdev_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) { 148 if (req->elem.out_num) { 149 req_size = req->elem.out_sg[0].iov_len; 150 } 151 if (req->elem.in_num) { 152 resp_size = req->elem.in_sg[0].iov_len; 153 } 154 } 155 156 out_size = qemu_sgl_concat(req, req->elem.out_sg, 157 &req->elem.out_addr[0], req->elem.out_num, 158 req_size); 159 in_size = qemu_sgl_concat(req, req->elem.in_sg, 160 &req->elem.in_addr[0], req->elem.in_num, 161 resp_size); 162 163 if (out_size && in_size) { 164 return -ENOTSUP; 165 } 166 167 if (out_size) { 168 req->mode = SCSI_XFER_TO_DEV; 169 } else if (in_size) { 170 req->mode = SCSI_XFER_FROM_DEV; 171 } 172 173 return 0; 174 } 175 176 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq) 177 { 178 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s; 179 VirtIOSCSIReq *req; 180 181 req = virtqueue_pop(vq, sizeof(VirtIOSCSIReq) + vs->cdb_size); 182 if (!req) { 183 return NULL; 184 } 185 virtio_scsi_init_req(s, vq, req); 186 return req; 187 } 188 189 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) 190 { 191 VirtIOSCSIReq *req = sreq->hba_private; 192 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); 193 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); 194 uint32_t n = virtio_get_queue_index(req->vq) - 2; 195 196 assert(n < vs->conf.num_queues); 197 qemu_put_be32s(f, &n); 198 qemu_put_virtqueue_element(vdev, f, &req->elem); 199 } 200 201 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) 202 { 203 SCSIBus *bus = sreq->bus; 204 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 205 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 206 VirtIODevice *vdev = VIRTIO_DEVICE(s); 207 VirtIOSCSIReq *req; 208 uint32_t n; 209 210 qemu_get_be32s(f, &n); 211 assert(n < vs->conf.num_queues); 212 req = qemu_get_virtqueue_element(vdev, f, 213 sizeof(VirtIOSCSIReq) + vs->cdb_size); 214 virtio_scsi_init_req(s, vs->cmd_vqs[n], req); 215 216 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 217 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { 218 error_report("invalid SCSI request migration data"); 219 exit(1); 220 } 221 222 scsi_req_ref(sreq); 223 req->sreq = sreq; 224 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 225 assert(req->sreq->cmd.mode == req->mode); 226 } 227 return req; 228 } 229 230 typedef struct { 231 Notifier notifier; 232 VirtIOSCSIReq *tmf_req; 233 } VirtIOSCSICancelNotifier; 234 235 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data) 236 { 237 VirtIOSCSICancelNotifier *n = container_of(notifier, 238 VirtIOSCSICancelNotifier, 239 notifier); 240 241 if (--n->tmf_req->remaining == 0) { 242 virtio_scsi_complete_req(n->tmf_req); 243 } 244 g_free(n); 245 } 246 247 static inline void virtio_scsi_ctx_check(VirtIOSCSI *s, SCSIDevice *d) 248 { 249 if (s->dataplane_started && d && blk_is_available(d->conf.blk)) { 250 assert(blk_get_aio_context(d->conf.blk) == s->ctx); 251 } 252 } 253 254 /* Return 0 if the request is ready to be completed and return to guest; 255 * -EINPROGRESS if the request is submitted and will be completed later, in the 256 * case of async cancellation. */ 257 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) 258 { 259 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun); 260 SCSIRequest *r, *next; 261 BusChild *kid; 262 int target; 263 int ret = 0; 264 265 virtio_scsi_ctx_check(s, d); 266 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */ 267 req->resp.tmf.response = VIRTIO_SCSI_S_OK; 268 269 /* 270 * req->req.tmf has the QEMU_PACKED attribute. Don't use virtio_tswap32s() 271 * to avoid compiler errors. 272 */ 273 req->req.tmf.subtype = 274 virtio_tswap32(VIRTIO_DEVICE(s), req->req.tmf.subtype); 275 276 switch (req->req.tmf.subtype) { 277 case VIRTIO_SCSI_T_TMF_ABORT_TASK: 278 case VIRTIO_SCSI_T_TMF_QUERY_TASK: 279 if (!d) { 280 goto fail; 281 } 282 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 283 goto incorrect_lun; 284 } 285 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 286 VirtIOSCSIReq *cmd_req = r->hba_private; 287 if (cmd_req && cmd_req->req.cmd.tag == req->req.tmf.tag) { 288 break; 289 } 290 } 291 if (r) { 292 /* 293 * Assert that the request has not been completed yet, we 294 * check for it in the loop above. 295 */ 296 assert(r->hba_private); 297 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) { 298 /* "If the specified command is present in the task set, then 299 * return a service response set to FUNCTION SUCCEEDED". 300 */ 301 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 302 } else { 303 VirtIOSCSICancelNotifier *notifier; 304 305 req->remaining = 1; 306 notifier = g_new(VirtIOSCSICancelNotifier, 1); 307 notifier->tmf_req = req; 308 notifier->notifier.notify = virtio_scsi_cancel_notify; 309 scsi_req_cancel_async(r, ¬ifier->notifier); 310 ret = -EINPROGRESS; 311 } 312 } 313 break; 314 315 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: 316 if (!d) { 317 goto fail; 318 } 319 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 320 goto incorrect_lun; 321 } 322 s->resetting++; 323 qdev_reset_all(&d->qdev); 324 s->resetting--; 325 break; 326 327 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET: 328 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: 329 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET: 330 if (!d) { 331 goto fail; 332 } 333 if (d->lun != virtio_scsi_get_lun(req->req.tmf.lun)) { 334 goto incorrect_lun; 335 } 336 337 /* Add 1 to "remaining" until virtio_scsi_do_tmf returns. 338 * This way, if the bus starts calling back to the notifiers 339 * even before we finish the loop, virtio_scsi_cancel_notify 340 * will not complete the TMF too early. 341 */ 342 req->remaining = 1; 343 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 344 if (r->hba_private) { 345 if (req->req.tmf.subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) { 346 /* "If there is any command present in the task set, then 347 * return a service response set to FUNCTION SUCCEEDED". 348 */ 349 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 350 break; 351 } else { 352 VirtIOSCSICancelNotifier *notifier; 353 354 req->remaining++; 355 notifier = g_new(VirtIOSCSICancelNotifier, 1); 356 notifier->notifier.notify = virtio_scsi_cancel_notify; 357 notifier->tmf_req = req; 358 scsi_req_cancel_async(r, ¬ifier->notifier); 359 } 360 } 361 } 362 if (--req->remaining > 0) { 363 ret = -EINPROGRESS; 364 } 365 break; 366 367 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: 368 target = req->req.tmf.lun[1]; 369 s->resetting++; 370 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { 371 d = SCSI_DEVICE(kid->child); 372 if (d->channel == 0 && d->id == target) { 373 qdev_reset_all(&d->qdev); 374 } 375 } 376 s->resetting--; 377 break; 378 379 case VIRTIO_SCSI_T_TMF_CLEAR_ACA: 380 default: 381 req->resp.tmf.response = VIRTIO_SCSI_S_FUNCTION_REJECTED; 382 break; 383 } 384 385 return ret; 386 387 incorrect_lun: 388 req->resp.tmf.response = VIRTIO_SCSI_S_INCORRECT_LUN; 389 return ret; 390 391 fail: 392 req->resp.tmf.response = VIRTIO_SCSI_S_BAD_TARGET; 393 return ret; 394 } 395 396 static void virtio_scsi_handle_ctrl_req(VirtIOSCSI *s, VirtIOSCSIReq *req) 397 { 398 VirtIODevice *vdev = (VirtIODevice *)s; 399 uint32_t type; 400 int r = 0; 401 402 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 403 &type, sizeof(type)) < sizeof(type)) { 404 virtio_scsi_bad_req(req); 405 return; 406 } 407 408 virtio_tswap32s(vdev, &type); 409 if (type == VIRTIO_SCSI_T_TMF) { 410 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlTMFReq), 411 sizeof(VirtIOSCSICtrlTMFResp)) < 0) { 412 virtio_scsi_bad_req(req); 413 return; 414 } else { 415 r = virtio_scsi_do_tmf(s, req); 416 } 417 418 } else if (type == VIRTIO_SCSI_T_AN_QUERY || 419 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { 420 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq), 421 sizeof(VirtIOSCSICtrlANResp)) < 0) { 422 virtio_scsi_bad_req(req); 423 return; 424 } else { 425 req->resp.an.event_actual = 0; 426 req->resp.an.response = VIRTIO_SCSI_S_OK; 427 } 428 } 429 if (r == 0) { 430 virtio_scsi_complete_req(req); 431 } else { 432 assert(r == -EINPROGRESS); 433 } 434 } 435 436 bool virtio_scsi_handle_ctrl_vq(VirtIOSCSI *s, VirtQueue *vq) 437 { 438 VirtIOSCSIReq *req; 439 bool progress = false; 440 441 while ((req = virtio_scsi_pop_req(s, vq))) { 442 progress = true; 443 virtio_scsi_handle_ctrl_req(s, req); 444 } 445 return progress; 446 } 447 448 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 449 { 450 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 451 452 if (s->ctx) { 453 virtio_device_start_ioeventfd(vdev); 454 if (!s->dataplane_fenced) { 455 return; 456 } 457 } 458 virtio_scsi_acquire(s); 459 virtio_scsi_handle_ctrl_vq(s, vq); 460 virtio_scsi_release(s); 461 } 462 463 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) 464 { 465 /* Sense data is not in req->resp and is copied separately 466 * in virtio_scsi_command_complete. 467 */ 468 req->resp_size = sizeof(VirtIOSCSICmdResp); 469 virtio_scsi_complete_req(req); 470 } 471 472 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, 473 size_t resid) 474 { 475 VirtIOSCSIReq *req = r->hba_private; 476 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 477 uint32_t sense_len; 478 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); 479 480 if (r->io_canceled) { 481 return; 482 } 483 484 req->resp.cmd.response = VIRTIO_SCSI_S_OK; 485 req->resp.cmd.status = status; 486 if (req->resp.cmd.status == GOOD) { 487 req->resp.cmd.resid = virtio_tswap32(vdev, resid); 488 } else { 489 req->resp.cmd.resid = 0; 490 sense_len = scsi_req_get_sense(r, sense, sizeof(sense)); 491 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd)); 492 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd), 493 sense, sense_len); 494 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len); 495 } 496 virtio_scsi_complete_cmd_req(req); 497 } 498 499 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, 500 uint8_t *buf, void *hba_private) 501 { 502 VirtIOSCSIReq *req = hba_private; 503 504 if (cmd->len == 0) { 505 cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE); 506 memcpy(cmd->buf, buf, cmd->len); 507 } 508 509 /* Extract the direction and mode directly from the request, for 510 * host device passthrough. 511 */ 512 cmd->xfer = req->qsgl.size; 513 cmd->mode = req->mode; 514 return 0; 515 } 516 517 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) 518 { 519 VirtIOSCSIReq *req = r->hba_private; 520 521 return &req->qsgl; 522 } 523 524 static void virtio_scsi_request_cancelled(SCSIRequest *r) 525 { 526 VirtIOSCSIReq *req = r->hba_private; 527 528 if (!req) { 529 return; 530 } 531 if (req->dev->resetting) { 532 req->resp.cmd.response = VIRTIO_SCSI_S_RESET; 533 } else { 534 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; 535 } 536 virtio_scsi_complete_cmd_req(req); 537 } 538 539 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) 540 { 541 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; 542 virtio_scsi_complete_cmd_req(req); 543 } 544 545 static int virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req) 546 { 547 VirtIOSCSICommon *vs = &s->parent_obj; 548 SCSIDevice *d; 549 int rc; 550 551 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 552 sizeof(VirtIOSCSICmdResp) + vs->sense_size); 553 if (rc < 0) { 554 if (rc == -ENOTSUP) { 555 virtio_scsi_fail_cmd_req(req); 556 return -ENOTSUP; 557 } else { 558 virtio_scsi_bad_req(req); 559 return -EINVAL; 560 } 561 } 562 563 d = virtio_scsi_device_find(s, req->req.cmd.lun); 564 if (!d) { 565 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 566 virtio_scsi_complete_cmd_req(req); 567 return -ENOENT; 568 } 569 virtio_scsi_ctx_check(s, d); 570 req->sreq = scsi_req_new(d, req->req.cmd.tag, 571 virtio_scsi_get_lun(req->req.cmd.lun), 572 req->req.cmd.cdb, req); 573 574 if (req->sreq->cmd.mode != SCSI_XFER_NONE 575 && (req->sreq->cmd.mode != req->mode || 576 req->sreq->cmd.xfer > req->qsgl.size)) { 577 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN; 578 virtio_scsi_complete_cmd_req(req); 579 return -ENOBUFS; 580 } 581 scsi_req_ref(req->sreq); 582 blk_io_plug(d->conf.blk); 583 return 0; 584 } 585 586 static void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req) 587 { 588 SCSIRequest *sreq = req->sreq; 589 if (scsi_req_enqueue(sreq)) { 590 scsi_req_continue(sreq); 591 } 592 blk_io_unplug(sreq->dev->conf.blk); 593 scsi_req_unref(sreq); 594 } 595 596 bool virtio_scsi_handle_cmd_vq(VirtIOSCSI *s, VirtQueue *vq) 597 { 598 VirtIOSCSIReq *req, *next; 599 int ret = 0; 600 bool suppress_notifications = virtio_queue_get_notification(vq); 601 bool progress = false; 602 603 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); 604 605 do { 606 if (suppress_notifications) { 607 virtio_queue_set_notification(vq, 0); 608 } 609 610 while ((req = virtio_scsi_pop_req(s, vq))) { 611 progress = true; 612 ret = virtio_scsi_handle_cmd_req_prepare(s, req); 613 if (!ret) { 614 QTAILQ_INSERT_TAIL(&reqs, req, next); 615 } else if (ret == -EINVAL) { 616 /* The device is broken and shouldn't process any request */ 617 while (!QTAILQ_EMPTY(&reqs)) { 618 req = QTAILQ_FIRST(&reqs); 619 QTAILQ_REMOVE(&reqs, req, next); 620 blk_io_unplug(req->sreq->dev->conf.blk); 621 scsi_req_unref(req->sreq); 622 virtqueue_detach_element(req->vq, &req->elem, 0); 623 virtio_scsi_free_req(req); 624 } 625 } 626 } 627 628 if (suppress_notifications) { 629 virtio_queue_set_notification(vq, 1); 630 } 631 } while (ret != -EINVAL && !virtio_queue_empty(vq)); 632 633 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) { 634 virtio_scsi_handle_cmd_req_submit(s, req); 635 } 636 return progress; 637 } 638 639 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) 640 { 641 /* use non-QOM casts in the data path */ 642 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 643 644 if (s->ctx) { 645 virtio_device_start_ioeventfd(vdev); 646 if (!s->dataplane_fenced) { 647 return; 648 } 649 } 650 virtio_scsi_acquire(s); 651 virtio_scsi_handle_cmd_vq(s, vq); 652 virtio_scsi_release(s); 653 } 654 655 static void virtio_scsi_get_config(VirtIODevice *vdev, 656 uint8_t *config) 657 { 658 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 659 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); 660 661 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues); 662 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2); 663 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors); 664 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun); 665 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); 666 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size); 667 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size); 668 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); 669 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); 670 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); 671 } 672 673 static void virtio_scsi_set_config(VirtIODevice *vdev, 674 const uint8_t *config) 675 { 676 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 677 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 678 679 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 || 680 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) { 681 virtio_error(vdev, 682 "bad data written to virtio-scsi configuration space"); 683 return; 684 } 685 686 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size); 687 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size); 688 } 689 690 static uint64_t virtio_scsi_get_features(VirtIODevice *vdev, 691 uint64_t requested_features, 692 Error **errp) 693 { 694 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 695 696 /* Firstly sync all virtio-scsi possible supported features */ 697 requested_features |= s->host_features; 698 return requested_features; 699 } 700 701 static void virtio_scsi_reset(VirtIODevice *vdev) 702 { 703 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 704 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 705 706 assert(!s->dataplane_started); 707 s->resetting++; 708 qbus_reset_all(BUS(&s->bus)); 709 s->resetting--; 710 711 vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 712 vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 713 s->events_dropped = false; 714 } 715 716 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 717 uint32_t event, uint32_t reason) 718 { 719 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 720 VirtIOSCSIReq *req; 721 VirtIOSCSIEvent *evt; 722 VirtIODevice *vdev = VIRTIO_DEVICE(s); 723 724 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 725 return; 726 } 727 728 req = virtio_scsi_pop_req(s, vs->event_vq); 729 if (!req) { 730 s->events_dropped = true; 731 return; 732 } 733 734 if (s->events_dropped) { 735 event |= VIRTIO_SCSI_T_EVENTS_MISSED; 736 s->events_dropped = false; 737 } 738 739 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) { 740 virtio_scsi_bad_req(req); 741 return; 742 } 743 744 evt = &req->resp.event; 745 memset(evt, 0, sizeof(VirtIOSCSIEvent)); 746 evt->event = virtio_tswap32(vdev, event); 747 evt->reason = virtio_tswap32(vdev, reason); 748 if (!dev) { 749 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED); 750 } else { 751 evt->lun[0] = 1; 752 evt->lun[1] = dev->id; 753 754 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ 755 if (dev->lun >= 256) { 756 evt->lun[2] = (dev->lun >> 8) | 0x40; 757 } 758 evt->lun[3] = dev->lun & 0xFF; 759 } 760 virtio_scsi_complete_req(req); 761 } 762 763 bool virtio_scsi_handle_event_vq(VirtIOSCSI *s, VirtQueue *vq) 764 { 765 if (s->events_dropped) { 766 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 767 return true; 768 } 769 return false; 770 } 771 772 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) 773 { 774 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 775 776 if (s->ctx) { 777 virtio_device_start_ioeventfd(vdev); 778 if (!s->dataplane_fenced) { 779 return; 780 } 781 } 782 virtio_scsi_acquire(s); 783 virtio_scsi_handle_event_vq(s, vq); 784 virtio_scsi_release(s); 785 } 786 787 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) 788 { 789 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 790 VirtIODevice *vdev = VIRTIO_DEVICE(s); 791 792 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) && 793 dev->type != TYPE_ROM) { 794 virtio_scsi_acquire(s); 795 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE, 796 sense.asc | (sense.ascq << 8)); 797 virtio_scsi_release(s); 798 } 799 } 800 801 static void virtio_scsi_pre_hotplug(HotplugHandler *hotplug_dev, 802 DeviceState *dev, Error **errp) 803 { 804 SCSIDevice *sd = SCSI_DEVICE(dev); 805 sd->hba_supports_iothread = true; 806 } 807 808 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, 809 Error **errp) 810 { 811 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 812 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 813 SCSIDevice *sd = SCSI_DEVICE(dev); 814 int ret; 815 816 if (s->ctx && !s->dataplane_fenced) { 817 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) { 818 return; 819 } 820 virtio_scsi_acquire(s); 821 ret = blk_set_aio_context(sd->conf.blk, s->ctx, errp); 822 virtio_scsi_release(s); 823 if (ret < 0) { 824 return; 825 } 826 } 827 828 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 829 virtio_scsi_acquire(s); 830 virtio_scsi_push_event(s, sd, 831 VIRTIO_SCSI_T_TRANSPORT_RESET, 832 VIRTIO_SCSI_EVT_RESET_RESCAN); 833 virtio_scsi_release(s); 834 } 835 } 836 837 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev, 838 Error **errp) 839 { 840 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 841 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 842 SCSIDevice *sd = SCSI_DEVICE(dev); 843 AioContext *ctx = s->ctx ?: qemu_get_aio_context(); 844 845 if (virtio_vdev_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 846 virtio_scsi_acquire(s); 847 virtio_scsi_push_event(s, sd, 848 VIRTIO_SCSI_T_TRANSPORT_RESET, 849 VIRTIO_SCSI_EVT_RESET_REMOVED); 850 virtio_scsi_release(s); 851 } 852 853 aio_disable_external(ctx); 854 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp); 855 aio_enable_external(ctx); 856 857 if (s->ctx) { 858 virtio_scsi_acquire(s); 859 /* If other users keep the BlockBackend in the iothread, that's ok */ 860 blk_set_aio_context(sd->conf.blk, qemu_get_aio_context(), NULL); 861 virtio_scsi_release(s); 862 } 863 } 864 865 static struct SCSIBusInfo virtio_scsi_scsi_info = { 866 .tcq = true, 867 .max_channel = VIRTIO_SCSI_MAX_CHANNEL, 868 .max_target = VIRTIO_SCSI_MAX_TARGET, 869 .max_lun = VIRTIO_SCSI_MAX_LUN, 870 871 .complete = virtio_scsi_command_complete, 872 .cancel = virtio_scsi_request_cancelled, 873 .change = virtio_scsi_change, 874 .parse_cdb = virtio_scsi_parse_cdb, 875 .get_sg_list = virtio_scsi_get_sg_list, 876 .save_request = virtio_scsi_save_request, 877 .load_request = virtio_scsi_load_request, 878 }; 879 880 void virtio_scsi_common_realize(DeviceState *dev, 881 VirtIOHandleOutput ctrl, 882 VirtIOHandleOutput evt, 883 VirtIOHandleOutput cmd, 884 Error **errp) 885 { 886 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 887 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); 888 int i; 889 890 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, 891 sizeof(VirtIOSCSIConfig)); 892 893 if (s->conf.num_queues == 0 || 894 s->conf.num_queues > VIRTIO_QUEUE_MAX - 2) { 895 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 896 "must be a positive integer less than %d.", 897 s->conf.num_queues, VIRTIO_QUEUE_MAX - 2); 898 virtio_cleanup(vdev); 899 return; 900 } 901 s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues); 902 s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 903 s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 904 905 s->ctrl_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, ctrl); 906 s->event_vq = virtio_add_queue(vdev, s->conf.virtqueue_size, evt); 907 for (i = 0; i < s->conf.num_queues; i++) { 908 s->cmd_vqs[i] = virtio_add_queue(vdev, s->conf.virtqueue_size, cmd); 909 } 910 } 911 912 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) 913 { 914 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 915 VirtIOSCSI *s = VIRTIO_SCSI(dev); 916 Error *err = NULL; 917 918 virtio_scsi_common_realize(dev, 919 virtio_scsi_handle_ctrl, 920 virtio_scsi_handle_event, 921 virtio_scsi_handle_cmd, 922 &err); 923 if (err != NULL) { 924 error_propagate(errp, err); 925 return; 926 } 927 928 scsi_bus_new(&s->bus, sizeof(s->bus), dev, 929 &virtio_scsi_scsi_info, vdev->bus_name); 930 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */ 931 qbus_set_hotplug_handler(BUS(&s->bus), OBJECT(dev), &error_abort); 932 933 virtio_scsi_dataplane_setup(s, errp); 934 } 935 936 void virtio_scsi_common_unrealize(DeviceState *dev) 937 { 938 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 939 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 940 941 g_free(vs->cmd_vqs); 942 virtio_cleanup(vdev); 943 } 944 945 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp) 946 { 947 VirtIOSCSI *s = VIRTIO_SCSI(dev); 948 949 qbus_set_hotplug_handler(BUS(&s->bus), NULL, &error_abort); 950 virtio_scsi_common_unrealize(dev); 951 } 952 953 static Property virtio_scsi_properties[] = { 954 DEFINE_PROP_UINT32("num_queues", VirtIOSCSI, parent_obj.conf.num_queues, 1), 955 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSI, 956 parent_obj.conf.virtqueue_size, 128), 957 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSI, parent_obj.conf.max_sectors, 958 0xFFFF), 959 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSI, parent_obj.conf.cmd_per_lun, 960 128), 961 DEFINE_PROP_BIT("hotplug", VirtIOSCSI, host_features, 962 VIRTIO_SCSI_F_HOTPLUG, true), 963 DEFINE_PROP_BIT("param_change", VirtIOSCSI, host_features, 964 VIRTIO_SCSI_F_CHANGE, true), 965 DEFINE_PROP_LINK("iothread", VirtIOSCSI, parent_obj.conf.iothread, 966 TYPE_IOTHREAD, IOThread *), 967 DEFINE_PROP_END_OF_LIST(), 968 }; 969 970 static const VMStateDescription vmstate_virtio_scsi = { 971 .name = "virtio-scsi", 972 .minimum_version_id = 1, 973 .version_id = 1, 974 .fields = (VMStateField[]) { 975 VMSTATE_VIRTIO_DEVICE, 976 VMSTATE_END_OF_LIST() 977 }, 978 }; 979 980 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) 981 { 982 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 983 DeviceClass *dc = DEVICE_CLASS(klass); 984 985 vdc->get_config = virtio_scsi_get_config; 986 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 987 } 988 989 static void virtio_scsi_class_init(ObjectClass *klass, void *data) 990 { 991 DeviceClass *dc = DEVICE_CLASS(klass); 992 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 993 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 994 995 dc->props = virtio_scsi_properties; 996 dc->vmsd = &vmstate_virtio_scsi; 997 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 998 vdc->realize = virtio_scsi_device_realize; 999 vdc->unrealize = virtio_scsi_device_unrealize; 1000 vdc->set_config = virtio_scsi_set_config; 1001 vdc->get_features = virtio_scsi_get_features; 1002 vdc->reset = virtio_scsi_reset; 1003 vdc->start_ioeventfd = virtio_scsi_dataplane_start; 1004 vdc->stop_ioeventfd = virtio_scsi_dataplane_stop; 1005 hc->pre_plug = virtio_scsi_pre_hotplug; 1006 hc->plug = virtio_scsi_hotplug; 1007 hc->unplug = virtio_scsi_hotunplug; 1008 } 1009 1010 static const TypeInfo virtio_scsi_common_info = { 1011 .name = TYPE_VIRTIO_SCSI_COMMON, 1012 .parent = TYPE_VIRTIO_DEVICE, 1013 .instance_size = sizeof(VirtIOSCSICommon), 1014 .abstract = true, 1015 .class_init = virtio_scsi_common_class_init, 1016 }; 1017 1018 static const TypeInfo virtio_scsi_info = { 1019 .name = TYPE_VIRTIO_SCSI, 1020 .parent = TYPE_VIRTIO_SCSI_COMMON, 1021 .instance_size = sizeof(VirtIOSCSI), 1022 .class_init = virtio_scsi_class_init, 1023 .interfaces = (InterfaceInfo[]) { 1024 { TYPE_HOTPLUG_HANDLER }, 1025 { } 1026 } 1027 }; 1028 1029 static void virtio_register_types(void) 1030 { 1031 type_register_static(&virtio_scsi_common_info); 1032 type_register_static(&virtio_scsi_info); 1033 } 1034 1035 type_init(virtio_register_types) 1036