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