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