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 "standard-headers/linux/virtio_ids.h" 17 #include "hw/virtio/virtio-scsi.h" 18 #include "qemu/error-report.h" 19 #include "qemu/iov.h" 20 #include "sysemu/block-backend.h" 21 #include <hw/scsi/scsi.h> 22 #include <block/scsi.h> 23 #include <hw/virtio/virtio-bus.h> 24 #include "hw/virtio/virtio-access.h" 25 #include "migration/migration.h" 26 27 static inline int virtio_scsi_get_lun(uint8_t *lun) 28 { 29 return ((lun[2] << 8) | lun[3]) & 0x3FFF; 30 } 31 32 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun) 33 { 34 if (lun[0] != 1) { 35 return NULL; 36 } 37 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { 38 return NULL; 39 } 40 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); 41 } 42 43 VirtIOSCSIReq *virtio_scsi_init_req(VirtIOSCSI *s, VirtQueue *vq) 44 { 45 VirtIOSCSIReq *req; 46 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)s; 47 const size_t zero_skip = offsetof(VirtIOSCSIReq, elem) 48 + sizeof(VirtQueueElement); 49 50 req = g_slice_alloc(sizeof(*req) + vs->cdb_size); 51 req->vq = vq; 52 req->dev = s; 53 qemu_sglist_init(&req->qsgl, DEVICE(s), 8, &address_space_memory); 54 qemu_iovec_init(&req->resp_iov, 1); 55 memset((uint8_t *)req + zero_skip, 0, sizeof(*req) - zero_skip); 56 return req; 57 } 58 59 void virtio_scsi_free_req(VirtIOSCSIReq *req) 60 { 61 VirtIOSCSICommon *vs = (VirtIOSCSICommon *)req->dev; 62 63 qemu_iovec_destroy(&req->resp_iov); 64 qemu_sglist_destroy(&req->qsgl); 65 g_slice_free1(sizeof(*req) + vs->cdb_size, req); 66 } 67 68 static void virtio_scsi_complete_req(VirtIOSCSIReq *req) 69 { 70 VirtIOSCSI *s = req->dev; 71 VirtQueue *vq = req->vq; 72 VirtIODevice *vdev = VIRTIO_DEVICE(s); 73 74 qemu_iovec_from_buf(&req->resp_iov, 0, &req->resp, req->resp_size); 75 if (req->vring) { 76 assert(req->vq == NULL); 77 virtio_scsi_vring_push_notify(req); 78 } else { 79 virtqueue_push(vq, &req->elem, req->qsgl.size + req->resp_iov.size); 80 virtio_notify(vdev, vq); 81 } 82 83 if (req->sreq) { 84 req->sreq->hba_private = NULL; 85 scsi_req_unref(req->sreq); 86 } 87 virtio_scsi_free_req(req); 88 } 89 90 static void virtio_scsi_bad_req(void) 91 { 92 error_report("wrong size for virtio-scsi headers"); 93 exit(1); 94 } 95 96 static size_t qemu_sgl_concat(VirtIOSCSIReq *req, struct iovec *iov, 97 hwaddr *addr, int num, size_t skip) 98 { 99 QEMUSGList *qsgl = &req->qsgl; 100 size_t copied = 0; 101 102 while (num) { 103 if (skip >= iov->iov_len) { 104 skip -= iov->iov_len; 105 } else { 106 qemu_sglist_add(qsgl, *addr + skip, iov->iov_len - skip); 107 copied += iov->iov_len - skip; 108 skip = 0; 109 } 110 iov++; 111 addr++; 112 num--; 113 } 114 115 assert(skip == 0); 116 return copied; 117 } 118 119 static int virtio_scsi_parse_req(VirtIOSCSIReq *req, 120 unsigned req_size, unsigned resp_size) 121 { 122 VirtIODevice *vdev = (VirtIODevice *) req->dev; 123 size_t in_size, out_size; 124 125 if (iov_to_buf(req->elem.out_sg, req->elem.out_num, 0, 126 &req->req, req_size) < req_size) { 127 return -EINVAL; 128 } 129 130 if (qemu_iovec_concat_iov(&req->resp_iov, 131 req->elem.in_sg, req->elem.in_num, 0, 132 resp_size) < resp_size) { 133 return -EINVAL; 134 } 135 136 req->resp_size = resp_size; 137 138 /* Old BIOSes left some padding by mistake after the req_size/resp_size. 139 * As a workaround, always consider the first buffer as the virtio-scsi 140 * request/response, making the payload start at the second element 141 * of the iovec. 142 * 143 * The actual length of the response header, stored in req->resp_size, 144 * does not change. 145 * 146 * TODO: always disable this workaround for virtio 1.0 devices. 147 */ 148 if (!virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT)) { 149 req_size = req->elem.out_sg[0].iov_len; 150 resp_size = req->elem.in_sg[0].iov_len; 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 VirtIOSCSIReq *req = virtio_scsi_init_req(s, vq); 176 if (!virtqueue_pop(vq, &req->elem)) { 177 virtio_scsi_free_req(req); 178 return NULL; 179 } 180 return req; 181 } 182 183 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) 184 { 185 VirtIOSCSIReq *req = sreq->hba_private; 186 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); 187 uint32_t n = virtio_queue_get_id(req->vq) - 2; 188 189 assert(n < vs->conf.num_queues); 190 qemu_put_be32s(f, &n); 191 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 192 } 193 194 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) 195 { 196 SCSIBus *bus = sreq->bus; 197 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 198 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 199 VirtIOSCSIReq *req; 200 uint32_t n; 201 202 qemu_get_be32s(f, &n); 203 assert(n < vs->conf.num_queues); 204 req = virtio_scsi_init_req(s, vs->cmd_vqs[n]); 205 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 206 /* TODO: add a way for SCSIBusInfo's load_request to fail, 207 * and fail migration instead of asserting here. 208 * When we do, we might be able to re-enable NDEBUG below. 209 */ 210 #ifdef NDEBUG 211 #error building with NDEBUG is not supported 212 #endif 213 assert(req->elem.in_num <= ARRAY_SIZE(req->elem.in_sg)); 214 assert(req->elem.out_num <= ARRAY_SIZE(req->elem.out_sg)); 215 216 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 217 sizeof(VirtIOSCSICmdResp) + vs->sense_size) < 0) { 218 error_report("invalid SCSI request migration data"); 219 exit(1); 220 } 221 222 scsi_req_ref(sreq); 223 req->sreq = sreq; 224 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 225 assert(req->sreq->cmd.mode == req->mode); 226 } 227 return req; 228 } 229 230 typedef struct { 231 Notifier notifier; 232 VirtIOSCSIReq *tmf_req; 233 } VirtIOSCSICancelNotifier; 234 235 static void virtio_scsi_cancel_notify(Notifier *notifier, void *data) 236 { 237 VirtIOSCSICancelNotifier *n = container_of(notifier, 238 VirtIOSCSICancelNotifier, 239 notifier); 240 241 if (--n->tmf_req->remaining == 0) { 242 virtio_scsi_complete_req(n->tmf_req); 243 } 244 g_slice_free(VirtIOSCSICancelNotifier, n); 245 } 246 247 /* Return 0 if the request is ready to be completed and return to guest; 248 * -EINPROGRESS if the request is submitted and will be completed later, in the 249 * case of async cancellation. */ 250 static int virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) 251 { 252 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf.lun); 253 SCSIRequest *r, *next; 254 BusChild *kid; 255 int target; 256 int ret = 0; 257 258 if (s->dataplane_started) { 259 assert(blk_get_aio_context(d->conf.blk) == s->ctx); 260 } 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_slice_new(VirtIOSCSICancelNotifier); 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_slice_new(VirtIOSCSICancelNotifier); 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 = DO_UPCAST(SCSIDevice, qdev, 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 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(); 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(); 402 } else { 403 r = virtio_scsi_do_tmf(s, req); 404 } 405 406 } else if (type == VIRTIO_SCSI_T_AN_QUERY || 407 type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { 408 if (virtio_scsi_parse_req(req, sizeof(VirtIOSCSICtrlANReq), 409 sizeof(VirtIOSCSICtrlANResp)) < 0) { 410 virtio_scsi_bad_req(); 411 } else { 412 req->resp.an.event_actual = 0; 413 req->resp.an.response = VIRTIO_SCSI_S_OK; 414 } 415 } 416 if (r == 0) { 417 virtio_scsi_complete_req(req); 418 } else { 419 assert(r == -EINPROGRESS); 420 } 421 } 422 423 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 424 { 425 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 426 VirtIOSCSIReq *req; 427 428 if (s->ctx && !s->dataplane_disabled) { 429 virtio_scsi_dataplane_start(s); 430 return; 431 } 432 while ((req = virtio_scsi_pop_req(s, vq))) { 433 virtio_scsi_handle_ctrl_req(s, req); 434 } 435 } 436 437 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq *req) 438 { 439 /* Sense data is not in req->resp and is copied separately 440 * in virtio_scsi_command_complete. 441 */ 442 req->resp_size = sizeof(VirtIOSCSICmdResp); 443 virtio_scsi_complete_req(req); 444 } 445 446 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, 447 size_t resid) 448 { 449 VirtIOSCSIReq *req = r->hba_private; 450 uint8_t sense[SCSI_SENSE_BUF_SIZE]; 451 uint32_t sense_len; 452 VirtIODevice *vdev = VIRTIO_DEVICE(req->dev); 453 454 if (r->io_canceled) { 455 return; 456 } 457 458 req->resp.cmd.response = VIRTIO_SCSI_S_OK; 459 req->resp.cmd.status = status; 460 if (req->resp.cmd.status == GOOD) { 461 req->resp.cmd.resid = virtio_tswap32(vdev, resid); 462 } else { 463 req->resp.cmd.resid = 0; 464 sense_len = scsi_req_get_sense(r, sense, sizeof(sense)); 465 sense_len = MIN(sense_len, req->resp_iov.size - sizeof(req->resp.cmd)); 466 qemu_iovec_from_buf(&req->resp_iov, sizeof(req->resp.cmd), 467 sense, sense_len); 468 req->resp.cmd.sense_len = virtio_tswap32(vdev, sense_len); 469 } 470 virtio_scsi_complete_cmd_req(req); 471 } 472 473 static int virtio_scsi_parse_cdb(SCSIDevice *dev, SCSICommand *cmd, 474 uint8_t *buf, void *hba_private) 475 { 476 VirtIOSCSIReq *req = hba_private; 477 478 if (cmd->len == 0) { 479 cmd->len = MIN(VIRTIO_SCSI_CDB_DEFAULT_SIZE, SCSI_CMD_BUF_SIZE); 480 memcpy(cmd->buf, buf, cmd->len); 481 } 482 483 /* Extract the direction and mode directly from the request, for 484 * host device passthrough. 485 */ 486 cmd->xfer = req->qsgl.size; 487 cmd->mode = req->mode; 488 return 0; 489 } 490 491 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) 492 { 493 VirtIOSCSIReq *req = r->hba_private; 494 495 return &req->qsgl; 496 } 497 498 static void virtio_scsi_request_cancelled(SCSIRequest *r) 499 { 500 VirtIOSCSIReq *req = r->hba_private; 501 502 if (!req) { 503 return; 504 } 505 if (req->dev->resetting) { 506 req->resp.cmd.response = VIRTIO_SCSI_S_RESET; 507 } else { 508 req->resp.cmd.response = VIRTIO_SCSI_S_ABORTED; 509 } 510 virtio_scsi_complete_cmd_req(req); 511 } 512 513 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) 514 { 515 req->resp.cmd.response = VIRTIO_SCSI_S_FAILURE; 516 virtio_scsi_complete_cmd_req(req); 517 } 518 519 bool virtio_scsi_handle_cmd_req_prepare(VirtIOSCSI *s, VirtIOSCSIReq *req) 520 { 521 VirtIOSCSICommon *vs = &s->parent_obj; 522 SCSIDevice *d; 523 int rc; 524 525 rc = virtio_scsi_parse_req(req, sizeof(VirtIOSCSICmdReq) + vs->cdb_size, 526 sizeof(VirtIOSCSICmdResp) + vs->sense_size); 527 if (rc < 0) { 528 if (rc == -ENOTSUP) { 529 virtio_scsi_fail_cmd_req(req); 530 } else { 531 virtio_scsi_bad_req(); 532 } 533 return false; 534 } 535 536 d = virtio_scsi_device_find(s, req->req.cmd.lun); 537 if (!d) { 538 req->resp.cmd.response = VIRTIO_SCSI_S_BAD_TARGET; 539 virtio_scsi_complete_cmd_req(req); 540 return false; 541 } 542 if (s->dataplane_started) { 543 assert(blk_get_aio_context(d->conf.blk) == s->ctx); 544 } 545 req->sreq = scsi_req_new(d, req->req.cmd.tag, 546 virtio_scsi_get_lun(req->req.cmd.lun), 547 req->req.cmd.cdb, req); 548 549 if (req->sreq->cmd.mode != SCSI_XFER_NONE 550 && (req->sreq->cmd.mode != req->mode || 551 req->sreq->cmd.xfer > req->qsgl.size)) { 552 req->resp.cmd.response = VIRTIO_SCSI_S_OVERRUN; 553 virtio_scsi_complete_cmd_req(req); 554 return false; 555 } 556 scsi_req_ref(req->sreq); 557 blk_io_plug(d->conf.blk); 558 return true; 559 } 560 561 void virtio_scsi_handle_cmd_req_submit(VirtIOSCSI *s, VirtIOSCSIReq *req) 562 { 563 SCSIRequest *sreq = req->sreq; 564 if (scsi_req_enqueue(sreq)) { 565 scsi_req_continue(sreq); 566 } 567 blk_io_unplug(sreq->dev->conf.blk); 568 scsi_req_unref(sreq); 569 } 570 571 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) 572 { 573 /* use non-QOM casts in the data path */ 574 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 575 VirtIOSCSIReq *req, *next; 576 QTAILQ_HEAD(, VirtIOSCSIReq) reqs = QTAILQ_HEAD_INITIALIZER(reqs); 577 578 if (s->ctx && !s->dataplane_disabled) { 579 virtio_scsi_dataplane_start(s); 580 return; 581 } 582 while ((req = virtio_scsi_pop_req(s, vq))) { 583 if (virtio_scsi_handle_cmd_req_prepare(s, req)) { 584 QTAILQ_INSERT_TAIL(&reqs, req, next); 585 } 586 } 587 588 QTAILQ_FOREACH_SAFE(req, &reqs, next, next) { 589 virtio_scsi_handle_cmd_req_submit(s, req); 590 } 591 } 592 593 static void virtio_scsi_get_config(VirtIODevice *vdev, 594 uint8_t *config) 595 { 596 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 597 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); 598 599 virtio_stl_p(vdev, &scsiconf->num_queues, s->conf.num_queues); 600 virtio_stl_p(vdev, &scsiconf->seg_max, 128 - 2); 601 virtio_stl_p(vdev, &scsiconf->max_sectors, s->conf.max_sectors); 602 virtio_stl_p(vdev, &scsiconf->cmd_per_lun, s->conf.cmd_per_lun); 603 virtio_stl_p(vdev, &scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); 604 virtio_stl_p(vdev, &scsiconf->sense_size, s->sense_size); 605 virtio_stl_p(vdev, &scsiconf->cdb_size, s->cdb_size); 606 virtio_stw_p(vdev, &scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); 607 virtio_stw_p(vdev, &scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); 608 virtio_stl_p(vdev, &scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); 609 } 610 611 static void virtio_scsi_set_config(VirtIODevice *vdev, 612 const uint8_t *config) 613 { 614 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 615 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 616 617 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) >= 65536 || 618 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) >= 256) { 619 error_report("bad data written to virtio-scsi configuration space"); 620 exit(1); 621 } 622 623 vs->sense_size = virtio_ldl_p(vdev, &scsiconf->sense_size); 624 vs->cdb_size = virtio_ldl_p(vdev, &scsiconf->cdb_size); 625 } 626 627 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev, 628 uint32_t requested_features) 629 { 630 return requested_features; 631 } 632 633 static void virtio_scsi_reset(VirtIODevice *vdev) 634 { 635 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 636 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 637 638 if (s->ctx) { 639 virtio_scsi_dataplane_stop(s); 640 } 641 s->resetting++; 642 qbus_reset_all(&s->bus.qbus); 643 s->resetting--; 644 645 vs->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 646 vs->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 647 s->events_dropped = false; 648 } 649 650 /* The device does not have anything to save beyond the virtio data. 651 * Request data is saved with callbacks from SCSI devices. 652 */ 653 static void virtio_scsi_save(QEMUFile *f, void *opaque) 654 { 655 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 656 virtio_save(vdev, f); 657 } 658 659 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id) 660 { 661 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 662 int ret; 663 664 ret = virtio_load(vdev, f, version_id); 665 if (ret) { 666 return ret; 667 } 668 return 0; 669 } 670 671 void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 672 uint32_t event, uint32_t reason) 673 { 674 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 675 VirtIOSCSIReq *req; 676 VirtIOSCSIEvent *evt; 677 VirtIODevice *vdev = VIRTIO_DEVICE(s); 678 679 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 680 return; 681 } 682 683 if (s->dataplane_started) { 684 assert(s->ctx); 685 aio_context_acquire(s->ctx); 686 } 687 688 if (s->dataplane_started) { 689 req = virtio_scsi_pop_req_vring(s, s->event_vring); 690 } else { 691 req = virtio_scsi_pop_req(s, vs->event_vq); 692 } 693 if (!req) { 694 s->events_dropped = true; 695 goto out; 696 } 697 698 if (s->events_dropped) { 699 event |= VIRTIO_SCSI_T_EVENTS_MISSED; 700 s->events_dropped = false; 701 } 702 703 if (virtio_scsi_parse_req(req, 0, sizeof(VirtIOSCSIEvent))) { 704 virtio_scsi_bad_req(); 705 } 706 707 evt = &req->resp.event; 708 memset(evt, 0, sizeof(VirtIOSCSIEvent)); 709 evt->event = virtio_tswap32(vdev, event); 710 evt->reason = virtio_tswap32(vdev, reason); 711 if (!dev) { 712 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED); 713 } else { 714 evt->lun[0] = 1; 715 evt->lun[1] = dev->id; 716 717 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ 718 if (dev->lun >= 256) { 719 evt->lun[2] = (dev->lun >> 8) | 0x40; 720 } 721 evt->lun[3] = dev->lun & 0xFF; 722 } 723 virtio_scsi_complete_req(req); 724 out: 725 if (s->dataplane_started) { 726 aio_context_release(s->ctx); 727 } 728 } 729 730 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) 731 { 732 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 733 734 if (s->ctx && !s->dataplane_disabled) { 735 virtio_scsi_dataplane_start(s); 736 return; 737 } 738 if (s->events_dropped) { 739 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 740 } 741 } 742 743 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) 744 { 745 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 746 VirtIODevice *vdev = VIRTIO_DEVICE(s); 747 748 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_CHANGE) && 749 dev->type != TYPE_ROM) { 750 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE, 751 sense.asc | (sense.ascq << 8)); 752 } 753 } 754 755 static void virtio_scsi_hotplug(HotplugHandler *hotplug_dev, DeviceState *dev, 756 Error **errp) 757 { 758 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 759 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 760 SCSIDevice *sd = SCSI_DEVICE(dev); 761 762 if (s->ctx && !s->dataplane_disabled) { 763 if (blk_op_is_blocked(sd->conf.blk, BLOCK_OP_TYPE_DATAPLANE, errp)) { 764 return; 765 } 766 blk_op_block_all(sd->conf.blk, s->blocker); 767 aio_context_acquire(s->ctx); 768 blk_set_aio_context(sd->conf.blk, s->ctx); 769 aio_context_release(s->ctx); 770 } 771 772 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 773 virtio_scsi_push_event(s, sd, 774 VIRTIO_SCSI_T_TRANSPORT_RESET, 775 VIRTIO_SCSI_EVT_RESET_RESCAN); 776 } 777 } 778 779 static void virtio_scsi_hotunplug(HotplugHandler *hotplug_dev, DeviceState *dev, 780 Error **errp) 781 { 782 VirtIODevice *vdev = VIRTIO_DEVICE(hotplug_dev); 783 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 784 SCSIDevice *sd = SCSI_DEVICE(dev); 785 786 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) { 787 virtio_scsi_push_event(s, sd, 788 VIRTIO_SCSI_T_TRANSPORT_RESET, 789 VIRTIO_SCSI_EVT_RESET_REMOVED); 790 } 791 792 if (s->ctx) { 793 blk_op_unblock_all(sd->conf.blk, s->blocker); 794 } 795 qdev_simple_device_unplug_cb(hotplug_dev, dev, errp); 796 } 797 798 static struct SCSIBusInfo virtio_scsi_scsi_info = { 799 .tcq = true, 800 .max_channel = VIRTIO_SCSI_MAX_CHANNEL, 801 .max_target = VIRTIO_SCSI_MAX_TARGET, 802 .max_lun = VIRTIO_SCSI_MAX_LUN, 803 804 .complete = virtio_scsi_command_complete, 805 .cancel = virtio_scsi_request_cancelled, 806 .change = virtio_scsi_change, 807 .parse_cdb = virtio_scsi_parse_cdb, 808 .get_sg_list = virtio_scsi_get_sg_list, 809 .save_request = virtio_scsi_save_request, 810 .load_request = virtio_scsi_load_request, 811 }; 812 813 void virtio_scsi_common_realize(DeviceState *dev, Error **errp, 814 HandleOutput ctrl, HandleOutput evt, 815 HandleOutput cmd) 816 { 817 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 818 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); 819 int i; 820 821 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, 822 sizeof(VirtIOSCSIConfig)); 823 824 if (s->conf.num_queues == 0 || 825 s->conf.num_queues > VIRTIO_PCI_QUEUE_MAX - 2) { 826 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), " 827 "must be a positive integer less than %d.", 828 s->conf.num_queues, VIRTIO_PCI_QUEUE_MAX - 2); 829 virtio_cleanup(vdev); 830 return; 831 } 832 s->cmd_vqs = g_new0(VirtQueue *, s->conf.num_queues); 833 s->sense_size = VIRTIO_SCSI_SENSE_DEFAULT_SIZE; 834 s->cdb_size = VIRTIO_SCSI_CDB_DEFAULT_SIZE; 835 836 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 837 ctrl); 838 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 839 evt); 840 for (i = 0; i < s->conf.num_queues; i++) { 841 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 842 cmd); 843 } 844 845 if (s->conf.iothread) { 846 virtio_scsi_set_iothread(VIRTIO_SCSI(s), s->conf.iothread); 847 } 848 } 849 850 /* Disable dataplane thread during live migration since it does not 851 * update the dirty memory bitmap yet. 852 */ 853 static void virtio_scsi_migration_state_changed(Notifier *notifier, void *data) 854 { 855 VirtIOSCSI *s = container_of(notifier, VirtIOSCSI, 856 migration_state_notifier); 857 MigrationState *mig = data; 858 859 if (migration_in_setup(mig)) { 860 if (!s->dataplane_started) { 861 return; 862 } 863 virtio_scsi_dataplane_stop(s); 864 s->dataplane_disabled = true; 865 } else if (migration_has_finished(mig) || 866 migration_has_failed(mig)) { 867 if (s->dataplane_started) { 868 return; 869 } 870 blk_drain_all(); /* complete in-flight non-dataplane requests */ 871 s->dataplane_disabled = false; 872 } 873 } 874 875 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) 876 { 877 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 878 VirtIOSCSI *s = VIRTIO_SCSI(dev); 879 static int virtio_scsi_id; 880 Error *err = NULL; 881 882 virtio_scsi_common_realize(dev, &err, virtio_scsi_handle_ctrl, 883 virtio_scsi_handle_event, 884 virtio_scsi_handle_cmd); 885 if (err != NULL) { 886 error_propagate(errp, err); 887 return; 888 } 889 890 scsi_bus_new(&s->bus, sizeof(s->bus), dev, 891 &virtio_scsi_scsi_info, vdev->bus_name); 892 /* override default SCSI bus hotplug-handler, with virtio-scsi's one */ 893 qbus_set_hotplug_handler(BUS(&s->bus), dev, &error_abort); 894 895 if (!dev->hotplugged) { 896 scsi_bus_legacy_handle_cmdline(&s->bus, &err); 897 if (err != NULL) { 898 error_propagate(errp, err); 899 return; 900 } 901 } 902 903 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1, 904 virtio_scsi_save, virtio_scsi_load, s); 905 s->migration_state_notifier.notify = virtio_scsi_migration_state_changed; 906 add_migration_state_change_notifier(&s->migration_state_notifier); 907 908 error_setg(&s->blocker, "block device is in use by data plane"); 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 VirtIOSCSI *s = VIRTIO_SCSI(dev); 933 934 error_free(s->blocker); 935 936 unregister_savevm(dev, "virtio-scsi", s); 937 remove_migration_state_change_notifier(&s->migration_state_notifier); 938 939 virtio_scsi_common_unrealize(dev, errp); 940 } 941 942 static Property virtio_scsi_properties[] = { 943 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf), 944 DEFINE_PROP_END_OF_LIST(), 945 }; 946 947 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) 948 { 949 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 950 DeviceClass *dc = DEVICE_CLASS(klass); 951 952 vdc->get_config = virtio_scsi_get_config; 953 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 954 } 955 956 static void virtio_scsi_class_init(ObjectClass *klass, void *data) 957 { 958 DeviceClass *dc = DEVICE_CLASS(klass); 959 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 960 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 961 962 dc->props = virtio_scsi_properties; 963 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 964 vdc->realize = virtio_scsi_device_realize; 965 vdc->unrealize = virtio_scsi_device_unrealize; 966 vdc->set_config = virtio_scsi_set_config; 967 vdc->get_features = virtio_scsi_get_features; 968 vdc->reset = virtio_scsi_reset; 969 hc->plug = virtio_scsi_hotplug; 970 hc->unplug = virtio_scsi_hotunplug; 971 } 972 973 static const TypeInfo virtio_scsi_common_info = { 974 .name = TYPE_VIRTIO_SCSI_COMMON, 975 .parent = TYPE_VIRTIO_DEVICE, 976 .instance_size = sizeof(VirtIOSCSICommon), 977 .abstract = true, 978 .class_init = virtio_scsi_common_class_init, 979 }; 980 981 static const TypeInfo virtio_scsi_info = { 982 .name = TYPE_VIRTIO_SCSI, 983 .parent = TYPE_VIRTIO_SCSI_COMMON, 984 .instance_size = sizeof(VirtIOSCSI), 985 .instance_init = virtio_scsi_instance_init, 986 .class_init = virtio_scsi_class_init, 987 .interfaces = (InterfaceInfo[]) { 988 { TYPE_HOTPLUG_HANDLER }, 989 { } 990 } 991 }; 992 993 static void virtio_register_types(void) 994 { 995 type_register_static(&virtio_scsi_common_info); 996 type_register_static(&virtio_scsi_info); 997 } 998 999 type_init(virtio_register_types) 1000