4 * Copyright IBM, Corp. 2010
5 * Copyright Red Hat, Inc. 2011
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"
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
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
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 */
71 uint8_t lun
[8]; /* Logical Unit Number */
72 uint64_t tag
; /* Command identifier */
73 uint8_t task_attr
; /* Task attribute */
77 } QEMU_PACKED VirtIOSCSICmdReq
;
79 /* Response, followed by sense data and data-in */
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 */
87 } QEMU_PACKED VirtIOSCSICmdResp
;
89 /* Task Management Request */
95 } QEMU_PACKED VirtIOSCSICtrlTMFReq
;
99 } QEMU_PACKED VirtIOSCSICtrlTMFResp
;
101 /* Asynchronous notification query/subscription */
105 uint32_t event_requested
;
106 } QEMU_PACKED VirtIOSCSICtrlANReq
;
109 uint32_t event_actual
;
111 } QEMU_PACKED VirtIOSCSICtrlANResp
;
117 } QEMU_PACKED VirtIOSCSIEvent
;
122 uint32_t max_sectors
;
123 uint32_t cmd_per_lun
;
124 uint32_t event_info_size
;
127 uint16_t max_channel
;
130 } QEMU_PACKED VirtIOSCSIConfig
;
135 VirtIOSCSIConf
*conf
;
144 VirtQueue
*cmd_vqs
[0];
147 typedef struct VirtIOSCSIReq
{
150 VirtQueueElement elem
;
155 VirtIOSCSICmdReq
*cmd
;
156 VirtIOSCSICtrlTMFReq
*tmf
;
157 VirtIOSCSICtrlANReq
*an
;
161 VirtIOSCSICmdResp
*cmd
;
162 VirtIOSCSICtrlTMFResp
*tmf
;
163 VirtIOSCSICtrlANResp
*an
;
164 VirtIOSCSIEvent
*event
;
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
)
178 if (lun
[2] != 0 && !(lun
[2] >= 0x40 && lun
[2] < 0x80)) {
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
);
191 req
->sreq
->hba_private
= NULL
;
192 scsi_req_unref(req
->sreq
);
195 virtio_notify(&s
->vdev
, vq
);
198 static void virtio_scsi_bad_req(void)
200 error_report("wrong size for virtio-scsi headers");
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
);
209 qemu_sglist_add(qsgl
, *(addr
++), (sg
++)->iov_len
);
213 static void virtio_scsi_parse_req(VirtIOSCSI
*s
, VirtQueue
*vq
,
216 assert(req
->elem
.in_num
);
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);
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
)
239 req
= g_malloc(sizeof(*req
));
240 if (!virtqueue_pop(vq
, &req
->elem
)) {
245 virtio_scsi_parse_req(s
, vq
, 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
);
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
);
274 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
276 (req
->elem
.in_num
> 1 ? SCSI_XFER_FROM_DEV
: SCSI_XFER_TO_DEV
);
278 assert(req
->sreq
->cmd
.mode
== req_mode
);
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
;
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
:
299 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
->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
) {
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
;
325 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
329 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
->lun
)) {
333 qdev_reset_all(&d
->qdev
);
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
:
343 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
->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
;
361 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
362 target
= req
->req
.tmf
->lun
[1];
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
);
373 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
375 req
->resp
.tmf
->response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
382 req
->resp
.tmf
->response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
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
;
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();
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
,
426 VirtIOSCSIReq
*req
= r
->hba_private
;
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
);
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
;
449 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
451 VirtIOSCSIReq
*req
= r
->hba_private
;
456 if (req
->dev
->resetting
) {
457 req
->resp
.cmd
->response
= VIRTIO_SCSI_S_RESET
;
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
;
476 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
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
);
495 d
= virtio_scsi_device_find(s
, req
->req
.cmd
->lun
);
497 req
->resp
.cmd
->response
= VIRTIO_SCSI_S_BAD_TARGET
;
498 virtio_scsi_complete_req(req
);
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
) {
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
);
517 n
= scsi_req_enqueue(req
->sreq
);
519 scsi_req_continue(req
->sreq
);
524 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
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");
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
->sense_size
= VIRTIO_SCSI_SENSE_SIZE
;
569 s
->cdb_size
= VIRTIO_SCSI_CDB_SIZE
;
570 s
->events_dropped
= false;
573 /* The device does not have anything to save beyond the virtio data.
574 * Request data is saved with callbacks from SCSI devices.
576 static void virtio_scsi_save(QEMUFile
*f
, void *opaque
)
578 VirtIOSCSI
*s
= opaque
;
579 virtio_save(&s
->vdev
, f
);
582 static int virtio_scsi_load(QEMUFile
*f
, void *opaque
, int version_id
)
584 VirtIOSCSI
*s
= opaque
;
587 ret
= virtio_load(&s
->vdev
, f
);
594 static void virtio_scsi_push_event(VirtIOSCSI
*s
, SCSIDevice
*dev
,
595 uint32_t event
, uint32_t reason
)
597 VirtIOSCSIReq
*req
= virtio_scsi_pop_req(s
, s
->event_vq
);
598 VirtIOSCSIEvent
*evt
;
601 if (!(s
->vdev
.status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
606 s
->events_dropped
= true;
610 if (req
->elem
.out_num
|| req
->elem
.in_num
!= 1) {
611 virtio_scsi_bad_req();
614 if (s
->events_dropped
) {
615 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
616 s
->events_dropped
= false;
619 in_size
= req
->elem
.in_sg
[0].iov_len
;
620 if (in_size
< sizeof(VirtIOSCSIEvent
)) {
621 virtio_scsi_bad_req();
624 evt
= req
->resp
.event
;
625 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
627 evt
->reason
= reason
;
629 assert(event
== VIRTIO_SCSI_T_NO_EVENT
);
632 evt
->lun
[1] = dev
->id
;
634 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
635 if (dev
->lun
>= 256) {
636 evt
->lun
[2] = (dev
->lun
>> 8) | 0x40;
638 evt
->lun
[3] = dev
->lun
& 0xFF;
640 virtio_scsi_complete_req(req
);
643 static void virtio_scsi_handle_event(VirtIODevice
*vdev
, VirtQueue
*vq
)
645 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
647 if (s
->events_dropped
) {
648 virtio_scsi_push_event(s
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
652 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
654 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
656 if (((s
->vdev
.guest_features
>> VIRTIO_SCSI_F_CHANGE
) & 1) &&
657 dev
->type
!= TYPE_ROM
) {
658 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_PARAM_CHANGE
,
659 sense
.asc
| (sense
.ascq
<< 8));
663 static void virtio_scsi_hotplug(SCSIBus
*bus
, SCSIDevice
*dev
)
665 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
667 if ((s
->vdev
.guest_features
>> VIRTIO_SCSI_F_HOTPLUG
) & 1) {
668 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_TRANSPORT_RESET
,
669 VIRTIO_SCSI_EVT_RESET_RESCAN
);
673 static void virtio_scsi_hot_unplug(SCSIBus
*bus
, SCSIDevice
*dev
)
675 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
677 if ((s
->vdev
.guest_features
>> VIRTIO_SCSI_F_HOTPLUG
) & 1) {
678 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_TRANSPORT_RESET
,
679 VIRTIO_SCSI_EVT_RESET_REMOVED
);
683 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
685 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
686 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
687 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
689 .complete
= virtio_scsi_command_complete
,
690 .cancel
= virtio_scsi_request_cancelled
,
691 .change
= virtio_scsi_change
,
692 .hotplug
= virtio_scsi_hotplug
,
693 .hot_unplug
= virtio_scsi_hot_unplug
,
694 .get_sg_list
= virtio_scsi_get_sg_list
,
695 .save_request
= virtio_scsi_save_request
,
696 .load_request
= virtio_scsi_load_request
,
699 VirtIODevice
*virtio_scsi_init(DeviceState
*dev
, VirtIOSCSIConf
*proxyconf
)
702 static int virtio_scsi_id
;
706 sz
= sizeof(VirtIOSCSI
) + proxyconf
->num_queues
* sizeof(VirtQueue
*);
707 s
= (VirtIOSCSI
*)virtio_common_init("virtio-scsi", VIRTIO_ID_SCSI
,
708 sizeof(VirtIOSCSIConfig
), sz
);
713 /* TODO set up vdev function pointers */
714 s
->vdev
.get_config
= virtio_scsi_get_config
;
715 s
->vdev
.set_config
= virtio_scsi_set_config
;
716 s
->vdev
.get_features
= virtio_scsi_get_features
;
717 s
->vdev
.reset
= virtio_scsi_reset
;
719 s
->ctrl_vq
= virtio_add_queue(&s
->vdev
, VIRTIO_SCSI_VQ_SIZE
,
720 virtio_scsi_handle_ctrl
);
721 s
->event_vq
= virtio_add_queue(&s
->vdev
, VIRTIO_SCSI_VQ_SIZE
,
722 virtio_scsi_handle_event
);
723 for (i
= 0; i
< s
->conf
->num_queues
; i
++) {
724 s
->cmd_vqs
[i
] = virtio_add_queue(&s
->vdev
, VIRTIO_SCSI_VQ_SIZE
,
725 virtio_scsi_handle_cmd
);
728 scsi_bus_new(&s
->bus
, dev
, &virtio_scsi_scsi_info
);
729 if (!dev
->hotplugged
) {
730 scsi_bus_legacy_handle_cmdline(&s
->bus
);
733 register_savevm(dev
, "virtio-scsi", virtio_scsi_id
++, 1,
734 virtio_scsi_save
, virtio_scsi_load
, s
);
739 void virtio_scsi_exit(VirtIODevice
*vdev
)
741 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
742 unregister_savevm(s
->qdev
, "virtio-scsi", s
);
743 virtio_cleanup(vdev
);