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