s390/sclpconsole: prevent char layer callback during initialization
[qemu.git] / hw / virtio-scsi.c
blob0715865489a351ad2e6293c524267cbf561b55fd
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 qemu_sglist_init(qsgl, num, &dma_context_memory);
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;
427 uint32_t sense_len;
429 req->resp.cmd->response = VIRTIO_SCSI_S_OK;
430 req->resp.cmd->status = status;
431 if (req->resp.cmd->status == GOOD) {
432 req->resp.cmd->resid = tswap32(resid);
433 } else {
434 req->resp.cmd->resid = 0;
435 sense_len = scsi_req_get_sense(r, req->resp.cmd->sense,
436 VIRTIO_SCSI_SENSE_SIZE);
437 req->resp.cmd->sense_len = tswap32(sense_len);
439 virtio_scsi_complete_req(req);
442 static QEMUSGList *virtio_scsi_get_sg_list(SCSIRequest *r)
444 VirtIOSCSIReq *req = r->hba_private;
446 return &req->qsgl;
449 static void virtio_scsi_request_cancelled(SCSIRequest *r)
451 VirtIOSCSIReq *req = r->hba_private;
453 if (!req) {
454 return;
456 if (req->dev->resetting) {
457 req->resp.cmd->response = VIRTIO_SCSI_S_RESET;
458 } else {
459 req->resp.cmd->response = VIRTIO_SCSI_S_ABORTED;
461 virtio_scsi_complete_req(req);
464 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq *req)
466 req->resp.cmd->response = VIRTIO_SCSI_S_FAILURE;
467 virtio_scsi_complete_req(req);
470 static void virtio_scsi_handle_cmd(VirtIODevice *vdev, VirtQueue *vq)
472 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
473 VirtIOSCSIReq *req;
474 int n;
476 while ((req = virtio_scsi_pop_req(s, vq))) {
477 SCSIDevice *d;
478 int out_size, in_size;
479 if (req->elem.out_num < 1 || req->elem.in_num < 1) {
480 virtio_scsi_bad_req();
483 out_size = req->elem.out_sg[0].iov_len;
484 in_size = req->elem.in_sg[0].iov_len;
485 if (out_size < sizeof(VirtIOSCSICmdReq) + s->cdb_size ||
486 in_size < sizeof(VirtIOSCSICmdResp) + s->sense_size) {
487 virtio_scsi_bad_req();
490 if (req->elem.out_num > 1 && req->elem.in_num > 1) {
491 virtio_scsi_fail_cmd_req(req);
492 continue;
495 d = virtio_scsi_device_find(s, req->req.cmd->lun);
496 if (!d) {
497 req->resp.cmd->response = VIRTIO_SCSI_S_BAD_TARGET;
498 virtio_scsi_complete_req(req);
499 continue;
501 req->sreq = scsi_req_new(d, req->req.cmd->tag,
502 virtio_scsi_get_lun(req->req.cmd->lun),
503 req->req.cmd->cdb, req);
505 if (req->sreq->cmd.mode != SCSI_XFER_NONE) {
506 int req_mode =
507 (req->elem.in_num > 1 ? SCSI_XFER_FROM_DEV : SCSI_XFER_TO_DEV);
509 if (req->sreq->cmd.mode != req_mode ||
510 req->sreq->cmd.xfer > req->qsgl.size) {
511 req->resp.cmd->response = VIRTIO_SCSI_S_OVERRUN;
512 virtio_scsi_complete_req(req);
513 continue;
517 n = scsi_req_enqueue(req->sreq);
518 if (n) {
519 scsi_req_continue(req->sreq);
524 static void virtio_scsi_get_config(VirtIODevice *vdev,
525 uint8_t *config)
527 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
528 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
530 stl_raw(&scsiconf->num_queues, s->conf->num_queues);
531 stl_raw(&scsiconf->seg_max, 128 - 2);
532 stl_raw(&scsiconf->max_sectors, s->conf->max_sectors);
533 stl_raw(&scsiconf->cmd_per_lun, s->conf->cmd_per_lun);
534 stl_raw(&scsiconf->event_info_size, sizeof(VirtIOSCSIEvent));
535 stl_raw(&scsiconf->sense_size, s->sense_size);
536 stl_raw(&scsiconf->cdb_size, s->cdb_size);
537 stw_raw(&scsiconf->max_channel, VIRTIO_SCSI_MAX_CHANNEL);
538 stw_raw(&scsiconf->max_target, VIRTIO_SCSI_MAX_TARGET);
539 stl_raw(&scsiconf->max_lun, VIRTIO_SCSI_MAX_LUN);
542 static void virtio_scsi_set_config(VirtIODevice *vdev,
543 const uint8_t *config)
545 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
546 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
548 if ((uint32_t) ldl_raw(&scsiconf->sense_size) >= 65536 ||
549 (uint32_t) ldl_raw(&scsiconf->cdb_size) >= 256) {
550 error_report("bad data written to virtio-scsi configuration space");
551 exit(1);
554 s->sense_size = ldl_raw(&scsiconf->sense_size);
555 s->cdb_size = ldl_raw(&scsiconf->cdb_size);
558 static uint32_t virtio_scsi_get_features(VirtIODevice *vdev,
559 uint32_t requested_features)
561 return requested_features;
564 static void virtio_scsi_reset(VirtIODevice *vdev)
566 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
568 s->resetting++;
569 qbus_reset_all(&s->bus.qbus);
570 s->resetting--;
572 s->sense_size = VIRTIO_SCSI_SENSE_SIZE;
573 s->cdb_size = VIRTIO_SCSI_CDB_SIZE;
574 s->events_dropped = false;
577 /* The device does not have anything to save beyond the virtio data.
578 * Request data is saved with callbacks from SCSI devices.
580 static void virtio_scsi_save(QEMUFile *f, void *opaque)
582 VirtIOSCSI *s = opaque;
583 virtio_save(&s->vdev, f);
586 static int virtio_scsi_load(QEMUFile *f, void *opaque, int version_id)
588 VirtIOSCSI *s = opaque;
589 int ret;
591 ret = virtio_load(&s->vdev, f);
592 if (ret) {
593 return ret;
595 return 0;
598 static void virtio_scsi_push_event(VirtIOSCSI *s, SCSIDevice *dev,
599 uint32_t event, uint32_t reason)
601 VirtIOSCSIReq *req = virtio_scsi_pop_req(s, s->event_vq);
602 VirtIOSCSIEvent *evt;
603 int in_size;
605 if (!(s->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
606 return;
609 if (!req) {
610 s->events_dropped = true;
611 return;
614 if (req->elem.out_num || req->elem.in_num != 1) {
615 virtio_scsi_bad_req();
618 if (s->events_dropped) {
619 event |= VIRTIO_SCSI_T_EVENTS_MISSED;
620 s->events_dropped = false;
623 in_size = req->elem.in_sg[0].iov_len;
624 if (in_size < sizeof(VirtIOSCSIEvent)) {
625 virtio_scsi_bad_req();
628 evt = req->resp.event;
629 memset(evt, 0, sizeof(VirtIOSCSIEvent));
630 evt->event = event;
631 evt->reason = reason;
632 if (!dev) {
633 assert(event == VIRTIO_SCSI_T_NO_EVENT);
634 } else {
635 evt->lun[0] = 1;
636 evt->lun[1] = dev->id;
638 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
639 if (dev->lun >= 256) {
640 evt->lun[2] = (dev->lun >> 8) | 0x40;
642 evt->lun[3] = dev->lun & 0xFF;
644 virtio_scsi_complete_req(req);
647 static void virtio_scsi_handle_event(VirtIODevice *vdev, VirtQueue *vq)
649 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
651 if (s->events_dropped) {
652 virtio_scsi_push_event(s, NULL, VIRTIO_SCSI_T_NO_EVENT, 0);
656 static void virtio_scsi_change(SCSIBus *bus, SCSIDevice *dev, SCSISense sense)
658 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
660 if (((s->vdev.guest_features >> VIRTIO_SCSI_F_CHANGE) & 1) &&
661 dev->type != TYPE_ROM) {
662 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_PARAM_CHANGE,
663 sense.asc | (sense.ascq << 8));
667 static void virtio_scsi_hotplug(SCSIBus *bus, SCSIDevice *dev)
669 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
671 if ((s->vdev.guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
672 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET,
673 VIRTIO_SCSI_EVT_RESET_RESCAN);
677 static void virtio_scsi_hot_unplug(SCSIBus *bus, SCSIDevice *dev)
679 VirtIOSCSI *s = container_of(bus, VirtIOSCSI, bus);
681 if ((s->vdev.guest_features >> VIRTIO_SCSI_F_HOTPLUG) & 1) {
682 virtio_scsi_push_event(s, dev, VIRTIO_SCSI_T_TRANSPORT_RESET,
683 VIRTIO_SCSI_EVT_RESET_REMOVED);
687 static struct SCSIBusInfo virtio_scsi_scsi_info = {
688 .tcq = true,
689 .max_channel = VIRTIO_SCSI_MAX_CHANNEL,
690 .max_target = VIRTIO_SCSI_MAX_TARGET,
691 .max_lun = VIRTIO_SCSI_MAX_LUN,
693 .complete = virtio_scsi_command_complete,
694 .cancel = virtio_scsi_request_cancelled,
695 .change = virtio_scsi_change,
696 .hotplug = virtio_scsi_hotplug,
697 .hot_unplug = virtio_scsi_hot_unplug,
698 .get_sg_list = virtio_scsi_get_sg_list,
699 .save_request = virtio_scsi_save_request,
700 .load_request = virtio_scsi_load_request,
703 VirtIODevice *virtio_scsi_init(DeviceState *dev, VirtIOSCSIConf *proxyconf)
705 VirtIOSCSI *s;
706 static int virtio_scsi_id;
707 size_t sz;
708 int i;
710 sz = sizeof(VirtIOSCSI) + proxyconf->num_queues * sizeof(VirtQueue *);
711 s = (VirtIOSCSI *)virtio_common_init("virtio-scsi", VIRTIO_ID_SCSI,
712 sizeof(VirtIOSCSIConfig), sz);
714 s->qdev = dev;
715 s->conf = proxyconf;
717 /* TODO set up vdev function pointers */
718 s->vdev.get_config = virtio_scsi_get_config;
719 s->vdev.set_config = virtio_scsi_set_config;
720 s->vdev.get_features = virtio_scsi_get_features;
721 s->vdev.reset = virtio_scsi_reset;
723 s->ctrl_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
724 virtio_scsi_handle_ctrl);
725 s->event_vq = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
726 virtio_scsi_handle_event);
727 for (i = 0; i < s->conf->num_queues; i++) {
728 s->cmd_vqs[i] = virtio_add_queue(&s->vdev, VIRTIO_SCSI_VQ_SIZE,
729 virtio_scsi_handle_cmd);
732 scsi_bus_new(&s->bus, dev, &virtio_scsi_scsi_info);
733 if (!dev->hotplugged) {
734 scsi_bus_legacy_handle_cmdline(&s->bus);
737 register_savevm(dev, "virtio-scsi", virtio_scsi_id++, 1,
738 virtio_scsi_save, virtio_scsi_load, s);
740 return &s->vdev;
743 void virtio_scsi_exit(VirtIODevice *vdev)
745 VirtIOSCSI *s = (VirtIOSCSI *)vdev;
746 unregister_savevm(s->qdev, "virtio-scsi", s);
747 virtio_cleanup(vdev);