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