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