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