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