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 <hw/scsi/scsi.h> 19 #include <block/scsi.h> 20 #include <hw/virtio/virtio-bus.h> 21 22 typedef struct VirtIOSCSIReq { 23 VirtIOSCSI *dev; 24 VirtQueue *vq; 25 VirtQueueElement elem; 26 QEMUSGList qsgl; 27 SCSIRequest *sreq; 28 union { 29 char *buf; 30 VirtIOSCSICmdReq *cmd; 31 VirtIOSCSICtrlTMFReq *tmf; 32 VirtIOSCSICtrlANReq *an; 33 } req; 34 union { 35 char *buf; 36 VirtIOSCSICmdResp *cmd; 37 VirtIOSCSICtrlTMFResp *tmf; 38 VirtIOSCSICtrlANResp *an; 39 VirtIOSCSIEvent *event; 40 } resp; 41 } VirtIOSCSIReq; 42 43 static inline int virtio_scsi_get_lun(uint8_t *lun) 44 { 45 return ((lun[2] << 8) | lun[3]) & 0x3FFF; 46 } 47 48 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun) 49 { 50 if (lun[0] != 1) { 51 return NULL; 52 } 53 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) { 54 return NULL; 55 } 56 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun)); 57 } 58 59 static void virtio_scsi_complete_req(VirtIOSCSIReq *req) 60 { 61 VirtIOSCSI *s = req->dev; 62 VirtQueue *vq = req->vq; 63 VirtIODevice *vdev = VIRTIO_DEVICE(s); 64 virtqueue_push(vq, &req->elem, req->qsgl.size + req->elem.in_sg[0].iov_len); 65 qemu_sglist_destroy(&req->qsgl); 66 if (req->sreq) { 67 req->sreq->hba_private = NULL; 68 scsi_req_unref(req->sreq); 69 } 70 g_free(req); 71 virtio_notify(vdev, vq); 72 } 73 74 static void virtio_scsi_bad_req(void) 75 { 76 error_report("wrong size for virtio-scsi headers"); 77 exit(1); 78 } 79 80 static void qemu_sgl_init_external(VirtIOSCSIReq *req, struct iovec *sg, 81 hwaddr *addr, int num) 82 { 83 QEMUSGList *qsgl = &req->qsgl; 84 85 qemu_sglist_init(qsgl, DEVICE(req->dev), num, &address_space_memory); 86 while (num--) { 87 qemu_sglist_add(qsgl, *(addr++), (sg++)->iov_len); 88 } 89 } 90 91 static void virtio_scsi_parse_req(VirtIOSCSI *s, VirtQueue *vq, 92 VirtIOSCSIReq *req) 93 { 94 assert(req->elem.in_num); 95 req->vq = vq; 96 req->dev = s; 97 req->sreq = NULL; 98 if (req->elem.out_num) { 99 req->req.buf = req->elem.out_sg[0].iov_base; 100 } 101 req->resp.buf = req->elem.in_sg[0].iov_base; 102 103 if (req->elem.out_num > 1) { 104 qemu_sgl_init_external(req, &req->elem.out_sg[1], 105 &req->elem.out_addr[1], 106 req->elem.out_num - 1); 107 } else { 108 qemu_sgl_init_external(req, &req->elem.in_sg[1], 109 &req->elem.in_addr[1], 110 req->elem.in_num - 1); 111 } 112 } 113 114 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq) 115 { 116 VirtIOSCSIReq *req; 117 req = g_malloc(sizeof(*req)); 118 if (!virtqueue_pop(vq, &req->elem)) { 119 g_free(req); 120 return NULL; 121 } 122 123 virtio_scsi_parse_req(s, vq, req); 124 return req; 125 } 126 127 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq) 128 { 129 VirtIOSCSIReq *req = sreq->hba_private; 130 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(req->dev); 131 uint32_t n = virtio_queue_get_id(req->vq) - 2; 132 133 assert(n < vs->conf.num_queues); 134 qemu_put_be32s(f, &n); 135 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 136 } 137 138 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq) 139 { 140 SCSIBus *bus = sreq->bus; 141 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 142 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 143 VirtIOSCSIReq *req; 144 uint32_t n; 145 146 req = g_malloc(sizeof(*req)); 147 qemu_get_be32s(f, &n); 148 assert(n < vs->conf.num_queues); 149 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem)); 150 virtio_scsi_parse_req(s, vs->cmd_vqs[n], req); 151 152 scsi_req_ref(sreq); 153 req->sreq = sreq; 154 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 155 int req_mode = 156 (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV); 157 158 assert(req->sreq->cmd.mode == req_mode); 159 } 160 return req; 161 } 162 163 static void virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req) 164 { 165 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf->lun); 166 SCSIRequest *r, *next; 167 BusChild *kid; 168 int target; 169 170 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */ 171 req->resp.tmf->response = VIRTIO_SCSI_S_OK; 172 173 switch (req->req.tmf->subtype) { 174 case VIRTIO_SCSI_T_TMF_ABORT_TASK: 175 case VIRTIO_SCSI_T_TMF_QUERY_TASK: 176 if (!d) { 177 goto fail; 178 } 179 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) { 180 goto incorrect_lun; 181 } 182 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 183 VirtIOSCSIReq *cmd_req = r->hba_private; 184 if (cmd_req && cmd_req->req.cmd->tag == req->req.tmf->tag) { 185 break; 186 } 187 } 188 if (r) { 189 /* 190 * Assert that the request has not been completed yet, we 191 * check for it in the loop above. 192 */ 193 assert(r->hba_private); 194 if (req->req.tmf->subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) { 195 /* "If the specified command is present in the task set, then 196 * return a service response set to FUNCTION SUCCEEDED". 197 */ 198 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 199 } else { 200 scsi_req_cancel(r); 201 } 202 } 203 break; 204 205 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET: 206 if (!d) { 207 goto fail; 208 } 209 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) { 210 goto incorrect_lun; 211 } 212 s->resetting++; 213 qdev_reset_all(&d->qdev); 214 s->resetting--; 215 break; 216 217 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET: 218 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET: 219 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET: 220 if (!d) { 221 goto fail; 222 } 223 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) { 224 goto incorrect_lun; 225 } 226 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) { 227 if (r->hba_private) { 228 if (req->req.tmf->subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) { 229 /* "If there is any command present in the task set, then 230 * return a service response set to FUNCTION SUCCEEDED". 231 */ 232 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED; 233 break; 234 } else { 235 scsi_req_cancel(r); 236 } 237 } 238 } 239 break; 240 241 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET: 242 target = req->req.tmf->lun[1]; 243 s->resetting++; 244 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) { 245 d = DO_UPCAST(SCSIDevice, qdev, kid->child); 246 if (d->channel == 0 && d->id == target) { 247 qdev_reset_all(&d->qdev); 248 } 249 } 250 s->resetting--; 251 break; 252 253 case VIRTIO_SCSI_T_TMF_CLEAR_ACA: 254 default: 255 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_REJECTED; 256 break; 257 } 258 259 return; 260 261 incorrect_lun: 262 req->resp.tmf->response = VIRTIO_SCSI_S_INCORRECT_LUN; 263 return; 264 265 fail: 266 req->resp.tmf->response = VIRTIO_SCSI_S_BAD_TARGET; 267 } 268 269 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) 270 { 271 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 272 VirtIOSCSIReq *req; 273 274 while ((req = virtio_scsi_pop_req(s, vq))) { 275 int out_size, in_size; 276 if (req->elem.out_num < 1 || req->elem.in_num < 1) { 277 virtio_scsi_bad_req(); 278 continue; 279 } 280 281 out_size = req->elem.out_sg[0].iov_len; 282 in_size = req->elem.in_sg[0].iov_len; 283 if (req->req.tmf->type == VIRTIO_SCSI_T_TMF) { 284 if (out_size < sizeof(VirtIOSCSICtrlTMFReq) || 285 in_size < sizeof(VirtIOSCSICtrlTMFResp)) { 286 virtio_scsi_bad_req(); 287 } 288 virtio_scsi_do_tmf(s, req); 289 290 } else if (req->req.tmf->type == VIRTIO_SCSI_T_AN_QUERY || 291 req->req.tmf->type == VIRTIO_SCSI_T_AN_SUBSCRIBE) { 292 if (out_size < sizeof(VirtIOSCSICtrlANReq) || 293 in_size < sizeof(VirtIOSCSICtrlANResp)) { 294 virtio_scsi_bad_req(); 295 } 296 req->resp.an->event_actual = 0; 297 req->resp.an->response = VIRTIO_SCSI_S_OK; 298 } 299 virtio_scsi_complete_req(req); 300 } 301 } 302 303 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status, 304 size_t resid) 305 { 306 VirtIOSCSIReq *req = r->hba_private; 307 uint32_t sense_len; 308 309 if (r->io_canceled) { 310 return; 311 } 312 313 req->resp.cmd->response = VIRTIO_SCSI_S_OK; 314 req->resp.cmd->status = status; 315 if (req->resp.cmd->status == GOOD) { 316 req->resp.cmd->resid = tswap32(resid); 317 } else { 318 req->resp.cmd->resid = 0; 319 sense_len = scsi_req_get_sense(r, req->resp.cmd->sense, 320 VIRTIO_SCSI_SENSE_SIZE); 321 req->resp.cmd->sense_len = tswap32(sense_len); 322 } 323 virtio_scsi_complete_req(req); 324 } 325 326 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r) 327 { 328 VirtIOSCSIReq *req = r->hba_private; 329 330 return &req->qsgl; 331 } 332 333 static void virtio_scsi_request_cancelled(SCSIRequest *r) 334 { 335 VirtIOSCSIReq *req = r->hba_private; 336 337 if (!req) { 338 return; 339 } 340 if (req->dev->resetting) { 341 req->resp.cmd->response = VIRTIO_SCSI_S_RESET; 342 } else { 343 req->resp.cmd->response = VIRTIO_SCSI_S_ABORTED; 344 } 345 virtio_scsi_complete_req(req); 346 } 347 348 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req) 349 { 350 req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE; 351 virtio_scsi_complete_req(req); 352 } 353 354 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq) 355 { 356 /* use non-QOM casts in the data path */ 357 VirtIOSCSI *s = (VirtIOSCSI *)vdev; 358 VirtIOSCSICommon *vs = &s->parent_obj; 359 360 VirtIOSCSIReq *req; 361 int n; 362 363 while ((req = virtio_scsi_pop_req(s, vq))) { 364 SCSIDevice *d; 365 int out_size, in_size; 366 if (req->elem.out_num < 1 || req->elem.in_num < 1) { 367 virtio_scsi_bad_req(); 368 } 369 370 out_size = req->elem.out_sg[0].iov_len; 371 in_size = req->elem.in_sg[0].iov_len; 372 if (out_size < sizeof(VirtIOSCSICmdReq) + vs->cdb_size || 373 in_size < sizeof(VirtIOSCSICmdResp) + vs->sense_size) { 374 virtio_scsi_bad_req(); 375 } 376 377 if (req->elem.out_num > 1 && req->elem.in_num > 1) { 378 virtio_scsi_fail_cmd_req(req); 379 continue; 380 } 381 382 d = virtio_scsi_device_find(s, req->req.cmd->lun); 383 if (!d) { 384 req->resp.cmd->response = VIRTIO_SCSI_S_BAD_TARGET; 385 virtio_scsi_complete_req(req); 386 continue; 387 } 388 req->sreq = scsi_req_new(d, req->req.cmd->tag, 389 virtio_scsi_get_lun(req->req.cmd->lun), 390 req->req.cmd->cdb, req); 391 392 if (req->sreq->cmd.mode != SCSI_XFER_NONE) { 393 int req_mode = 394 (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV); 395 396 if (req->sreq->cmd.mode != req_mode || 397 req->sreq->cmd.xfer > req->qsgl.size) { 398 req->resp.cmd->response = VIRTIO_SCSI_S_OVERRUN; 399 virtio_scsi_complete_req(req); 400 continue; 401 } 402 } 403 404 n = scsi_req_enqueue(req->sreq); 405 if (n) { 406 scsi_req_continue(req->sreq); 407 } 408 } 409 } 410 411 static void virtio_scsi_get_config(VirtIODevice *vdev, 412 uint8_t *config) 413 { 414 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 415 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(vdev); 416 417 stl_raw(&scsiconf->num_queues, s->conf.num_queues); 418 stl_raw(&scsiconf->seg_max, 128 - 2); 419 stl_raw(&scsiconf->max_sectors, s->conf.max_sectors); 420 stl_raw(&scsiconf->cmd_per_lun, s->conf.cmd_per_lun); 421 stl_raw(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent)); 422 stl_raw(&scsiconf->sense_size, s->sense_size); 423 stl_raw(&scsiconf->cdb_size, s->cdb_size); 424 stw_raw(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL); 425 stw_raw(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET); 426 stl_raw(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN); 427 } 428 429 static void virtio_scsi_set_config(VirtIODevice *vdev, 430 const uint8_t *config) 431 { 432 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config; 433 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 434 435 if ((uint32_t) ldl_raw(&scsiconf->sense_size) >= 65536 || 436 (uint32_t) ldl_raw(&scsiconf->cdb_size) >= 256) { 437 error_report("bad data written to virtio-scsi configuration space"); 438 exit(1); 439 } 440 441 vs->sense_size = ldl_raw(&scsiconf->sense_size); 442 vs->cdb_size = ldl_raw(&scsiconf->cdb_size); 443 } 444 445 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev, 446 uint32_t requested_features) 447 { 448 return requested_features; 449 } 450 451 static void virtio_scsi_reset(VirtIODevice *vdev) 452 { 453 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 454 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev); 455 456 s->resetting++; 457 qbus_reset_all(&s->bus.qbus); 458 s->resetting--; 459 460 vs->sense_size = VIRTIO_SCSI_SENSE_SIZE; 461 vs->cdb_size = VIRTIO_SCSI_CDB_SIZE; 462 s->events_dropped = false; 463 } 464 465 /* The device does not have anything to save beyond the virtio data. 466 * Request data is saved with callbacks from SCSI devices. 467 */ 468 static void virtio_scsi_save(QEMUFile *f, void *opaque) 469 { 470 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 471 virtio_save(vdev, f); 472 } 473 474 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id) 475 { 476 VirtIODevice *vdev = VIRTIO_DEVICE(opaque); 477 int ret; 478 479 ret = virtio_load(vdev, f); 480 if (ret) { 481 return ret; 482 } 483 return 0; 484 } 485 486 static void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev, 487 uint32_t event, uint32_t reason) 488 { 489 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s); 490 VirtIOSCSIReq *req = virtio_scsi_pop_req(s, vs->event_vq); 491 VirtIOSCSIEvent *evt; 492 VirtIODevice *vdev = VIRTIO_DEVICE(s); 493 int in_size; 494 495 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { 496 return; 497 } 498 499 if (!req) { 500 s->events_dropped = true; 501 return; 502 } 503 504 if (req->elem.out_num || req->elem.in_num != 1) { 505 virtio_scsi_bad_req(); 506 } 507 508 if (s->events_dropped) { 509 event |= VIRTIO_SCSI_T_EVENTS_MISSED; 510 s->events_dropped = false; 511 } 512 513 in_size = req->elem.in_sg[0].iov_len; 514 if (in_size < sizeof(VirtIOSCSIEvent)) { 515 virtio_scsi_bad_req(); 516 } 517 518 evt = req->resp.event; 519 memset(evt, 0, sizeof(VirtIOSCSIEvent)); 520 evt->event = event; 521 evt->reason = reason; 522 if (!dev) { 523 assert(event == VIRTIO_SCSI_T_EVENTS_MISSED); 524 } else { 525 evt->lun[0] = 1; 526 evt->lun[1] = dev->id; 527 528 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */ 529 if (dev->lun >= 256) { 530 evt->lun[2] = (dev->lun >> 8) | 0x40; 531 } 532 evt->lun[3] = dev->lun & 0xFF; 533 } 534 virtio_scsi_complete_req(req); 535 } 536 537 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq) 538 { 539 VirtIOSCSI *s = VIRTIO_SCSI(vdev); 540 541 if (s->events_dropped) { 542 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0); 543 } 544 } 545 546 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense) 547 { 548 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 549 VirtIODevice *vdev = VIRTIO_DEVICE(s); 550 551 if (((vdev->guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) && 552 dev->type != TYPE_ROM) { 553 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE, 554 sense.asc | (sense.ascq << 8)); 555 } 556 } 557 558 static void virtio_scsi_hotplug(SCSIBus *bus, SCSIDevice *dev) 559 { 560 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 561 VirtIODevice *vdev = VIRTIO_DEVICE(s); 562 563 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) { 564 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET, 565 VIRTIO_SCSI_EVT_RESET_RESCAN); 566 } 567 } 568 569 static void virtio_scsi_hot_unplug(SCSIBus *bus, SCSIDevice *dev) 570 { 571 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus); 572 VirtIODevice *vdev = VIRTIO_DEVICE(s); 573 574 if ((vdev->guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) { 575 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET, 576 VIRTIO_SCSI_EVT_RESET_REMOVED); 577 } 578 } 579 580 static struct SCSIBusInfo virtio_scsi_scsi_info = { 581 .tcq = true, 582 .max_channel = VIRTIO_SCSI_MAX_CHANNEL, 583 .max_target = VIRTIO_SCSI_MAX_TARGET, 584 .max_lun = VIRTIO_SCSI_MAX_LUN, 585 586 .complete = virtio_scsi_command_complete, 587 .cancel = virtio_scsi_request_cancelled, 588 .change = virtio_scsi_change, 589 .hotplug = virtio_scsi_hotplug, 590 .hot_unplug = virtio_scsi_hot_unplug, 591 .get_sg_list = virtio_scsi_get_sg_list, 592 .save_request = virtio_scsi_save_request, 593 .load_request = virtio_scsi_load_request, 594 }; 595 596 void virtio_scsi_common_realize(DeviceState *dev, Error **errp) 597 { 598 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 599 VirtIOSCSICommon *s = VIRTIO_SCSI_COMMON(dev); 600 int i; 601 602 virtio_init(vdev, "virtio-scsi", VIRTIO_ID_SCSI, 603 sizeof(VirtIOSCSIConfig)); 604 605 s->cmd_vqs = g_malloc0(s->conf.num_queues * sizeof(VirtQueue *)); 606 s->sense_size = VIRTIO_SCSI_SENSE_SIZE; 607 s->cdb_size = VIRTIO_SCSI_CDB_SIZE; 608 609 s->ctrl_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 610 virtio_scsi_handle_ctrl); 611 s->event_vq = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 612 virtio_scsi_handle_event); 613 for (i = 0; i < s->conf.num_queues; i++) { 614 s->cmd_vqs[i] = virtio_add_queue(vdev, VIRTIO_SCSI_VQ_SIZE, 615 virtio_scsi_handle_cmd); 616 } 617 } 618 619 static void virtio_scsi_device_realize(DeviceState *dev, Error **errp) 620 { 621 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 622 VirtIOSCSI *s = VIRTIO_SCSI(dev); 623 static int virtio_scsi_id; 624 Error *err = NULL; 625 626 virtio_scsi_common_realize(dev, &err); 627 if (err != NULL) { 628 error_propagate(errp, err); 629 return; 630 } 631 632 scsi_bus_new(&s->bus, sizeof(s->bus), dev, 633 &virtio_scsi_scsi_info, vdev->bus_name); 634 635 if (!dev->hotplugged) { 636 scsi_bus_legacy_handle_cmdline(&s->bus, &err); 637 if (err != NULL) { 638 error_propagate(errp, err); 639 return; 640 } 641 } 642 643 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1, 644 virtio_scsi_save, virtio_scsi_load, s); 645 } 646 647 void virtio_scsi_common_unrealize(DeviceState *dev, Error **errp) 648 { 649 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 650 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev); 651 652 g_free(vs->cmd_vqs); 653 virtio_cleanup(vdev); 654 } 655 656 static void virtio_scsi_device_unrealize(DeviceState *dev, Error **errp) 657 { 658 VirtIOSCSI *s = VIRTIO_SCSI(dev); 659 660 unregister_savevm(dev, "virtio-scsi", s); 661 662 virtio_scsi_common_unrealize(dev, errp); 663 } 664 665 static Property virtio_scsi_properties[] = { 666 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI, parent_obj.conf), 667 DEFINE_PROP_END_OF_LIST(), 668 }; 669 670 static void virtio_scsi_common_class_init(ObjectClass *klass, void *data) 671 { 672 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 673 DeviceClass *dc = DEVICE_CLASS(klass); 674 675 vdc->get_config = virtio_scsi_get_config; 676 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 677 } 678 679 static void virtio_scsi_class_init(ObjectClass *klass, void *data) 680 { 681 DeviceClass *dc = DEVICE_CLASS(klass); 682 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 683 684 dc->props = virtio_scsi_properties; 685 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 686 vdc->realize = virtio_scsi_device_realize; 687 vdc->unrealize = virtio_scsi_device_unrealize; 688 vdc->set_config = virtio_scsi_set_config; 689 vdc->get_features = virtio_scsi_get_features; 690 vdc->reset = virtio_scsi_reset; 691 } 692 693 static const TypeInfo virtio_scsi_common_info = { 694 .name = TYPE_VIRTIO_SCSI_COMMON, 695 .parent = TYPE_VIRTIO_DEVICE, 696 .instance_size = sizeof(VirtIOSCSICommon), 697 .abstract = true, 698 .class_init = virtio_scsi_common_class_init, 699 }; 700 701 static const TypeInfo virtio_scsi_info = { 702 .name = TYPE_VIRTIO_SCSI, 703 .parent = TYPE_VIRTIO_SCSI_COMMON, 704 .instance_size = sizeof(VirtIOSCSI), 705 .class_init = virtio_scsi_class_init, 706 }; 707 708 static void virtio_register_types(void) 709 { 710 type_register_static(&virtio_scsi_common_info); 711 type_register_static(&virtio_scsi_info); 712 } 713 714 type_init(virtio_register_types) 715