slirp: remove unused function u_sleep
[qemu/ar7.git] / hw / virtio-scsi.c
blobb54c7895fca5e2100f4628fb5ed6b27c3c54a812
1 /*
2 * Virtio SCSI HBA
4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
7 * Authors:
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
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.
16 #include "virtio-scsi.h"
17 #include <hw/scsi.h>
18 #include <hw/scsi-defs.h>
20 #define VIRTIO_SCSI_VQ_SIZE 128
21 #define VIRTIO_SCSI_CDB_SIZE 32
22 #define VIRTIO_SCSI_SENSE_SIZE 96
23 #define VIRTIO_SCSI_MAX_CHANNEL 0
24 #define VIRTIO_SCSI_MAX_TARGET 255
25 #define VIRTIO_SCSI_MAX_LUN 16383
27 /* Response codes */
28 #define VIRTIO_SCSI_S_OK 0
29 #define VIRTIO_SCSI_S_OVERRUN 1
30 #define VIRTIO_SCSI_S_ABORTED 2
31 #define VIRTIO_SCSI_S_BAD_TARGET 3
32 #define VIRTIO_SCSI_S_RESET 4
33 #define VIRTIO_SCSI_S_BUSY 5
34 #define VIRTIO_SCSI_S_TRANSPORT_FAILURE 6
35 #define VIRTIO_SCSI_S_TARGET_FAILURE 7
36 #define VIRTIO_SCSI_S_NEXUS_FAILURE 8
37 #define VIRTIO_SCSI_S_FAILURE 9
38 #define VIRTIO_SCSI_S_FUNCTION_SUCCEEDED 10
39 #define VIRTIO_SCSI_S_FUNCTION_REJECTED 11
40 #define VIRTIO_SCSI_S_INCORRECT_LUN 12
42 /* Controlq type codes. */
43 #define VIRTIO_SCSI_T_TMF 0
44 #define VIRTIO_SCSI_T_AN_QUERY 1
45 #define VIRTIO_SCSI_T_AN_SUBSCRIBE 2
47 /* Valid TMF subtypes. */
48 #define VIRTIO_SCSI_T_TMF_ABORT_TASK 0
49 #define VIRTIO_SCSI_T_TMF_ABORT_TASK_SET 1
50 #define VIRTIO_SCSI_T_TMF_CLEAR_ACA 2
51 #define VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET 3
52 #define VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET 4
53 #define VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET 5
54 #define VIRTIO_SCSI_T_TMF_QUERY_TASK 6
55 #define VIRTIO_SCSI_T_TMF_QUERY_TASK_SET 7
57 /* Events. */
58 #define VIRTIO_SCSI_T_EVENTS_MISSED 0x80000000
59 #define VIRTIO_SCSI_T_NO_EVENT 0
60 #define VIRTIO_SCSI_T_TRANSPORT_RESET 1
61 #define VIRTIO_SCSI_T_ASYNC_NOTIFY 2
62 #define VIRTIO_SCSI_T_PARAM_CHANGE 3
64 /* Reasons for transport reset event */
65 #define VIRTIO_SCSI_EVT_RESET_HARD 0
66 #define VIRTIO_SCSI_EVT_RESET_RESCAN 1
67 #define VIRTIO_SCSI_EVT_RESET_REMOVED 2
69 /* SCSI command request, followed by data-out */
70 typedef struct {
71 uint8_t lun[8]; /* Logical Unit Number */
72 uint64_t tag; /* Command identifier */
73 uint8_t task_attr; /* Task attribute */
74 uint8_t prio;
75 uint8_t crn;
76 uint8_t cdb[];
77 } QEMU_PACKED VirtIOSCSICmdReq;
79 /* Response, followed by sense data and data-in */
80 typedef struct {
81 uint32_t sense_len; /* Sense data length */
82 uint32_t resid; /* Residual bytes in data buffer */
83 uint16_t status_qualifier; /* Status qualifier */
84 uint8_t status; /* Command completion status */
85 uint8_t response; /* Response values */
86 uint8_t sense[];
87 } QEMU_PACKED VirtIOSCSICmdResp;
89 /* Task Management Request */
90 typedef struct {
91 uint32_t type;
92 uint32_t subtype;
93 uint8_t lun[8];
94 uint64_t tag;
95 } QEMU_PACKED VirtIOSCSICtrlTMFReq;
97 typedef struct {
98 uint8_t response;
99 } QEMU_PACKED VirtIOSCSICtrlTMFResp;
101 /* Asynchronous notification query/subscription */
102 typedef struct {
103 uint32_t type;
104 uint8_t lun[8];
105 uint32_t event_requested;
106 } QEMU_PACKED VirtIOSCSICtrlANReq;
108 typedef struct {
109 uint32_t event_actual;
110 uint8_t response;
111 } QEMU_PACKED VirtIOSCSICtrlANResp;
113 typedef struct {
114 uint32_t event;
115 uint8_t lun[8];
116 uint32_t reason;
117 } QEMU_PACKED VirtIOSCSIEvent;
119 typedef struct {
120 uint32_t num_queues;
121 uint32_t seg_max;
122 uint32_t max_sectors;
123 uint32_t cmd_per_lun;
124 uint32_t event_info_size;
125 uint32_t sense_size;
126 uint32_t cdb_size;
127 uint16_t max_channel;
128 uint16_t max_target;
129 uint32_t max_lun;
130 } QEMU_PACKED VirtIOSCSIConfig;
132 typedef struct {
133 VirtIODevice vdev;
134 DeviceState *qdev;
135 VirtIOSCSIConf *conf;
137 SCSIBus bus;
138 uint32_t sense_size;
139 uint32_t cdb_size;
140 int resetting;
141 bool events_dropped;
142 VirtQueue *ctrl_vq;
143 VirtQueue *event_vq;
144 VirtQueue *cmd_vqs[0];
145 } VirtIOSCSI;
147 typedef struct VirtIOSCSIReq {
148 VirtIOSCSI *dev;
149 VirtQueue *vq;
150 VirtQueueElement elem;
151 QEMUSGList qsgl;
152 SCSIRequest *sreq;
153 union {
154 char *buf;
155 VirtIOSCSICmdReq *cmd;
156 VirtIOSCSICtrlTMFReq *tmf;
157 VirtIOSCSICtrlANReq *an;
158 } req;
159 union {
160 char *buf;
161 VirtIOSCSICmdResp *cmd;
162 VirtIOSCSICtrlTMFResp *tmf;
163 VirtIOSCSICtrlANResp *an;
164 VirtIOSCSIEvent *event;
165 } resp;
166 } VirtIOSCSIReq;
168 static inline int virtio_scsi_get_lun(uint8_t *lun)
170 return ((lun[2] << 8) | lun[3]) & 0x3FFF;
173 static inline SCSIDevice *virtio_scsi_device_find(VirtIOSCSI *s, uint8_t *lun)
175 if (lun[0] != 1) {
176 return NULL;
178 if (lun[2] != 0 && !(lun[2] >= 0x40 && lun[2] < 0x80)) {
179 return NULL;
181 return scsi_device_find(&s->bus, 0, lun[1], virtio_scsi_get_lun(lun));
184 static void virtio_scsi_complete_req(VirtIOSCSIReq *req)
186 VirtIOSCSI *s = req->dev;
187 VirtQueue *vq = req->vq;
188 virtqueue_push(vq, &req->elem, req->qsgl.size + req->elem.in_sg[0].iov_len);
189 qemu_sglist_destroy(&req->qsgl);
190 if (req->sreq) {
191 req->sreq->hba_private = NULL;
192 scsi_req_unref(req->sreq);
194 g_free(req);
195 virtio_notify(&s->vdev, vq);
198 static void virtio_scsi_bad_req(void)
200 error_report("wrong size for virtio-scsi headers");
201 exit(1);
204 static void qemu_sgl_init_external(QEMUSGList *qsgl, struct iovec *sg,
205 hwaddr *addr, int num)
207 memset(qsgl, 0, sizeof(*qsgl));
208 while (num--) {
209 qemu_sglist_add(qsgl, *(addr++), (sg++)->iov_len);
213 static void virtio_scsi_parse_req(VirtIOSCSI *s, VirtQueue *vq,
214 VirtIOSCSIReq *req)
216 assert(req->elem.in_num);
217 req->vq = vq;
218 req->dev = s;
219 req->sreq = NULL;
220 if (req->elem.out_num) {
221 req->req.buf = req->elem.out_sg[0].iov_base;
223 req->resp.buf = req->elem.in_sg[0].iov_base;
225 if (req->elem.out_num > 1) {
226 qemu_sgl_init_external(&req->qsgl, &req->elem.out_sg[1],
227 &req->elem.out_addr[1],
228 req->elem.out_num - 1);
229 } else {
230 qemu_sgl_init_external(&req->qsgl, &req->elem.in_sg[1],
231 &req->elem.in_addr[1],
232 req->elem.in_num - 1);
236 static VirtIOSCSIReq *virtio_scsi_pop_req(VirtIOSCSI *s, VirtQueue *vq)
238 VirtIOSCSIReq *req;
239 req = g_malloc(sizeof(*req));
240 if (!virtqueue_pop(vq, &req->elem)) {
241 g_free(req);
242 return NULL;
245 virtio_scsi_parse_req(s, vq, req);
246 return req;
249 static void virtio_scsi_save_request(QEMUFile *f, SCSIRequest *sreq)
251 VirtIOSCSIReq *req = sreq->hba_private;
252 uint32_t n = virtio_queue_get_id(req->vq) - 2;
254 assert(n < req->dev->conf->num_queues);
255 qemu_put_be32s(f, &n);
256 qemu_put_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
259 static void *virtio_scsi_load_request(QEMUFile *f, SCSIRequest *sreq)
261 SCSIBus *bus = sreq->bus;
262 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
263 VirtIOSCSIReq *req;
264 uint32_t n;
266 req = g_malloc(sizeof(*req));
267 qemu_get_be32s(f, &n);
268 assert(n < s->conf->num_queues);
269 qemu_get_buffer(f, (unsigned char *)&req->elem, sizeof(req->elem));
270 virtio_scsi_parse_req(s, s->cmd_vqs[n], req);
272 scsi_req_ref(sreq);
273 req->sreq = sreq;
274 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
275 int req_mode =
276 (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
278 assert(req->sreq->cmd.mode == req_mode);
280 return req;
283 static void virtio_scsi_do_tmf(VirtIOSCSI *s, VirtIOSCSIReq *req)
285 SCSIDevice *d = virtio_scsi_device_find(s, req->req.tmf->lun);
286 SCSIRequest *r, *next;
287 BusChild *kid;
288 int target;
290 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
291 req->resp.tmf->response = VIRTIO_SCSI_S_OK;
293 switch (req->req.tmf->subtype) {
294 case VIRTIO_SCSI_T_TMF_ABORT_TASK:
295 case VIRTIO_SCSI_T_TMF_QUERY_TASK:
296 if (!d) {
297 goto fail;
299 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) {
300 goto incorrect_lun;
302 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
303 VirtIOSCSIReq *cmd_req = r->hba_private;
304 if (cmd_req && cmd_req->req.cmd->tag == req->req.tmf->tag) {
305 break;
308 if (r) {
310 * Assert that the request has not been completed yet, we
311 * check for it in the loop above.
313 assert(r->hba_private);
314 if (req->req.tmf->subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK) {
315 /* "If the specified command is present in the task set, then
316 * return a service response set to FUNCTION SUCCEEDED".
318 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
319 } else {
320 scsi_req_cancel(r);
323 break;
325 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET:
326 if (!d) {
327 goto fail;
329 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) {
330 goto incorrect_lun;
332 s->resetting++;
333 qdev_reset_all(&d->qdev);
334 s->resetting--;
335 break;
337 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET:
338 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET:
339 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET:
340 if (!d) {
341 goto fail;
343 if (d->lun != virtio_scsi_get_lun(req->req.tmf->lun)) {
344 goto incorrect_lun;
346 QTAILQ_FOREACH_SAFE(r, &d->requests, next, next) {
347 if (r->hba_private) {
348 if (req->req.tmf->subtype == VIRTIO_SCSI_T_TMF_QUERY_TASK_SET) {
349 /* "If there is any command present in the task set, then
350 * return a service response set to FUNCTION SUCCEEDED".
352 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_SUCCEEDED;
353 break;
354 } else {
355 scsi_req_cancel(r);
359 break;
361 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET:
362 target = req->req.tmf->lun[1];
363 s->resetting++;
364 QTAILQ_FOREACH(kid, &s->bus.qbus.children, sibling) {
365 d = DO_UPCAST(SCSIDevice, qdev, kid->child);
366 if (d->channel == 0 && d->id == target) {
367 qdev_reset_all(&d->qdev);
370 s->resetting--;
371 break;
373 case VIRTIO_SCSI_T_TMF_CLEAR_ACA:
374 default:
375 req->resp.tmf->response = VIRTIO_SCSI_S_FUNCTION_REJECTED;
376 break;
379 return;
381 incorrect_lun:
382 req->resp.tmf->response = VIRTIO_SCSI_S_INCORRECT_LUN;
383 return;
385 fail:
386 req->resp.tmf->response = VIRTIO_SCSI_S_BAD_TARGET;
389 static void virtio_scsi_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
391 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
392 VirtIOSCSIReq *req;
394 while ((req = virtio_scsi_pop_req(s, vq))) {
395 int out_size, in_size;
396 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
397 virtio_scsi_bad_req();
398 continue;
401 out_size = req->elem.out_sg[0].iov_len;
402 in_size = req->elem.in_sg[0].iov_len;
403 if (req->req.tmf->type == VIRTIO_SCSI_T_TMF) {
404 if (out_size < sizeof(VirtIOSCSICtrlTMFReq) ||
405 in_size < sizeof(VirtIOSCSICtrlTMFResp)) {
406 virtio_scsi_bad_req();
408 virtio_scsi_do_tmf(s, req);
410 } else if (req->req.tmf->type == VIRTIO_SCSI_T_AN_QUERY ||
411 req->req.tmf->type == VIRTIO_SCSI_T_AN_SUBSCRIBE) {
412 if (out_size < sizeof(VirtIOSCSICtrlANReq) ||
413 in_size < sizeof(VirtIOSCSICtrlANResp)) {
414 virtio_scsi_bad_req();
416 req->resp.an->event_actual = 0;
417 req->resp.an->response = VIRTIO_SCSI_S_OK;
419 virtio_scsi_complete_req(req);
423 static void virtio_scsi_command_complete(SCSIRequest *r, uint32_t status,
424 size_t resid)
426 VirtIOSCSIReq *req = r->hba_private;
428 req->resp.cmd->response = VIRTIO_SCSI_S_OK;
429 req->resp.cmd->status = status;
430 if (req->resp.cmd->status == GOOD) {
431 req->resp.cmd->resid = resid;
432 } else {
433 req->resp.cmd->resid = 0;
434 req->resp.cmd->sense_len =
435 scsi_req_get_sense(r, req->resp.cmd->sense, VIRTIO_SCSI_SENSE_SIZE);
437 virtio_scsi_complete_req(req);
440 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
442 VirtIOSCSIReq *req = r->hba_private;
444 return &req->qsgl;
447 static void virtio_scsi_request_cancelled(SCSIRequest *r)
449 VirtIOSCSIReq *req = r->hba_private;
451 if (!req) {
452 return;
454 if (req->dev->resetting) {
455 req->resp.cmd->response = VIRTIO_SCSI_S_RESET;
456 } else {
457 req->resp.cmd->response = VIRTIO_SCSI_S_ABORTED;
459 virtio_scsi_complete_req(req);
462 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
464 req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE;
465 virtio_scsi_complete_req(req);
468 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
470 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
471 VirtIOSCSIReq *req;
472 int n;
474 while ((req = virtio_scsi_pop_req(s, vq))) {
475 SCSIDevice *d;
476 int out_size, in_size;
477 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
478 virtio_scsi_bad_req();
481 out_size = req->elem.out_sg[0].iov_len;
482 in_size = req->elem.in_sg[0].iov_len;
483 if (out_size < sizeof(VirtIOSCSICmdReq) + s->cdb_size ||
484 in_size < sizeof(VirtIOSCSICmdResp) + s->sense_size) {
485 virtio_scsi_bad_req();
488 if (req->elem.out_num > 1 && req->elem.in_num > 1) {
489 virtio_scsi_fail_cmd_req(req);
490 continue;
493 d = virtio_scsi_device_find(s, req->req.cmd->lun);
494 if (!d) {
495 req->resp.cmd->response = VIRTIO_SCSI_S_BAD_TARGET;
496 virtio_scsi_complete_req(req);
497 continue;
499 req->sreq = scsi_req_new(d, req->req.cmd->tag,
500 virtio_scsi_get_lun(req->req.cmd->lun),
501 req->req.cmd->cdb, req);
503 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
504 int req_mode =
505 (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
507 if (req->sreq->cmd.mode != req_mode ||
508 req->sreq->cmd.xfer > req->qsgl.size) {
509 req->resp.cmd->response = VIRTIO_SCSI_S_OVERRUN;
510 virtio_scsi_complete_req(req);
511 continue;
515 n = scsi_req_enqueue(req->sreq);
516 if (n) {
517 scsi_req_continue(req->sreq);
522 static void virtio_scsi_get_config(VirtIODevice *vdev,
523 uint8_t *config)
525 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
526 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
528 stl_raw(&scsiconf->num_queues, s->conf->num_queues);
529 stl_raw(&scsiconf->seg_max, 128 - 2);
530 stl_raw(&scsiconf->max_sectors, s->conf->max_sectors);
531 stl_raw(&scsiconf->cmd_per_lun, s->conf->cmd_per_lun);
532 stl_raw(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
533 stl_raw(&scsiconf->sense_size, s->sense_size);
534 stl_raw(&scsiconf->cdb_size, s->cdb_size);
535 stl_raw(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
536 stl_raw(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
537 stl_raw(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
540 static void virtio_scsi_set_config(VirtIODevice *vdev,
541 const uint8_t *config)
543 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
544 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
546 if ((uint32_t) ldl_raw(&scsiconf->sense_size) >= 65536 ||
547 (uint32_t) ldl_raw(&scsiconf->cdb_size) >= 256) {
548 error_report("bad data written to virtio-scsi configuration space");
549 exit(1);
552 s->sense_size = ldl_raw(&scsiconf->sense_size);
553 s->cdb_size = ldl_raw(&scsiconf->cdb_size);
556 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
557 uint32_t requested_features)
559 return requested_features;
562 static void virtio_scsi_reset(VirtIODevice *vdev)
564 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
566 s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
567 s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
568 s->events_dropped = false;
571 /* The device does not have anything to save beyond the virtio data.
572 * Request data is saved with callbacks from SCSI devices.
574 static void virtio_scsi_save(QEMUFile *f, void *opaque)
576 VirtIOSCSI *s = opaque;
577 virtio_save(&s->vdev, f);
580 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
582 VirtIOSCSI *s = opaque;
583 int ret;
585 ret = virtio_load(&s->vdev, f);
586 if (ret) {
587 return ret;
589 return 0;
592 static void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
593 uint32_t event, uint32_t reason)
595 VirtIOSCSIReq *req = virtio_scsi_pop_req(s, s->event_vq);
596 VirtIOSCSIEvent *evt;
597 int in_size;
599 if (!req) {
600 s->events_dropped = true;
601 return;
604 if (req->elem.out_num || req->elem.in_num != 1) {
605 virtio_scsi_bad_req();
608 if (s->events_dropped) {
609 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
610 s->events_dropped = false;
613 in_size = req->elem.in_sg[0].iov_len;
614 if (in_size < sizeof(VirtIOSCSIEvent)) {
615 virtio_scsi_bad_req();
618 evt = req->resp.event;
619 memset(evt, 0, sizeof(VirtIOSCSIEvent));
620 evt->event = event;
621 evt->reason = reason;
622 if (!dev) {
623 assert(event == VIRTIO_SCSI_T_NO_EVENT);
624 } else {
625 evt->lun[0] = 1;
626 evt->lun[1] = dev->id;
628 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
629 if (dev->lun >= 256) {
630 evt->lun[2] = (dev->lun >> 8) | 0x40;
632 evt->lun[3] = dev->lun & 0xFF;
634 virtio_scsi_complete_req(req);
637 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
639 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
641 if (s->events_dropped) {
642 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
646 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
648 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
650 if (((s->vdev.guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) &&
651 (s->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK) &&
652 dev->type != TYPE_ROM) {
653 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
654 sense.asc | (sense.ascq << 8));
658 static void virtio_scsi_hotplug(SCSIBus *bus, SCSIDevice *dev)
660 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
662 if (((s->vdev.guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) &&
663 (s->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
664 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET,
665 VIRTIO_SCSI_EVT_RESET_RESCAN);
669 static void virtio_scsi_hot_unplug(SCSIBus *bus, SCSIDevice *dev)
671 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
673 if ((s->vdev.guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
674 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET,
675 VIRTIO_SCSI_EVT_RESET_REMOVED);
679 static struct SCSIBusInfo virtio_scsi_scsi_info = {
680 .tcq = true,
681 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
682 .max_target = VIRTIO_SCSI_MAX_TARGET,
683 .max_lun = VIRTIO_SCSI_MAX_LUN,
685 .complete = virtio_scsi_command_complete,
686 .cancel = virtio_scsi_request_cancelled,
687 .change = virtio_scsi_change,
688 .hotplug = virtio_scsi_hotplug,
689 .hot_unplug = virtio_scsi_hot_unplug,
690 .get_sg_list = virtio_scsi_get_sg_list,
691 .save_request = virtio_scsi_save_request,
692 .load_request = virtio_scsi_load_request,
695 VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
697 VirtIOSCSI *s;
698 static int virtio_scsi_id;
699 size_t sz;
700 int i;
702 sz = sizeof(VirtIOSCSI) + proxyconf->num_queues * sizeof(VirtQueue *);
703 s = (VirtIOSCSI *)virtio_common_init("virtio-scsi", VIRTIO_ID_SCSI,
704 sizeof(VirtIOSCSIConfig), sz);
706 s->qdev = dev;
707 s->conf = proxyconf;
709 /* TODO set up vdev function pointers */
710 s->vdev.get_config = virtio_scsi_get_config;
711 s->vdev.set_config = virtio_scsi_set_config;
712 s->vdev.get_features = virtio_scsi_get_features;
713 s->vdev.reset = virtio_scsi_reset;
715 s->ctrl_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
716 virtio_scsi_handle_ctrl);
717 s->event_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
718 virtio_scsi_handle_event);
719 for (i = 0; i < s->conf->num_queues; i++) {
720 s->cmd_vqs[i] = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
721 virtio_scsi_handle_cmd);
724 scsi_bus_new(&s->bus, dev, &virtio_scsi_scsi_info);
725 if (!dev->hotplugged) {
726 scsi_bus_legacy_handle_cmdline(&s->bus);
729 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
730 virtio_scsi_save, virtio_scsi_load, s);
732 return &s->vdev;
735 void virtio_scsi_exit(VirtIODevice *vdev)
737 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
738 unregister_savevm(s->qdev, "virtio-scsi", s);
739 virtio_cleanup(vdev);