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