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 "hw/virtio/virtio-scsi.h"
17 #include "qemu/error-report.h"
19 #include <hw/scsi/scsi.h>
20 #include <block/scsi.h>
21 #include <hw/virtio/virtio-bus.h>
22 #include "hw/virtio/virtio-access.h"
24 typedef struct VirtIOSCSIReq
{
27 VirtQueueElement elem
;
31 enum SCSIXferMode mode
;
32 QEMUIOVector resp_iov
;
34 VirtIOSCSICmdResp cmd
;
35 VirtIOSCSICtrlTMFResp tmf
;
36 VirtIOSCSICtrlANResp an
;
37 VirtIOSCSIEvent event
;
44 VirtIOSCSICtrlTMFReq tmf
;
45 VirtIOSCSICtrlANReq an
;
49 QEMU_BUILD_BUG_ON(offsetof(VirtIOSCSIReq
, req
.cdb
) !=
50 offsetof(VirtIOSCSIReq
, req
.cmd
) + sizeof(VirtIOSCSICmdReq
));
52 static inline int virtio_scsi_get_lun(uint8_t *lun
)
54 return ((lun
[2] << 8) | lun
[3]) & 0x3FFF;
57 static inline SCSIDevice
*virtio_scsi_device_find(VirtIOSCSI
*s
, uint8_t *lun
)
62 if (lun
[2] != 0 && !(lun
[2] >= 0x40 && lun
[2] < 0x80)) {
65 return scsi_device_find(&s
->bus
, 0, lun
[1], virtio_scsi_get_lun(lun
));
68 static VirtIOSCSIReq
*virtio_scsi_init_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
71 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
73 req
= g_malloc0(sizeof(*req
) + vs
->cdb_size
);
78 qemu_sglist_init(&req
->qsgl
, DEVICE(s
), 8, &address_space_memory
);
79 qemu_iovec_init(&req
->resp_iov
, 1);
83 static void virtio_scsi_free_req(VirtIOSCSIReq
*req
)
85 qemu_iovec_destroy(&req
->resp_iov
);
86 qemu_sglist_destroy(&req
->qsgl
);
90 static void virtio_scsi_complete_req(VirtIOSCSIReq
*req
)
92 VirtIOSCSI
*s
= req
->dev
;
93 VirtQueue
*vq
= req
->vq
;
94 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
96 qemu_iovec_from_buf(&req
->resp_iov
, 0, &req
->resp
, req
->resp_size
);
97 virtqueue_push(vq
, &req
->elem
, req
->qsgl
.size
+ req
->resp_iov
.size
);
99 req
->sreq
->hba_private
= NULL
;
100 scsi_req_unref(req
->sreq
);
102 virtio_scsi_free_req(req
);
103 virtio_notify(vdev
, vq
);
106 static void virtio_scsi_bad_req(void)
108 error_report("wrong size for virtio-scsi headers");
112 static size_t qemu_sgl_concat(VirtIOSCSIReq
*req
, struct iovec
*iov
,
113 hwaddr
*addr
, int num
, size_t skip
)
115 QEMUSGList
*qsgl
= &req
->qsgl
;
119 if (skip
>= iov
->iov_len
) {
120 skip
-= iov
->iov_len
;
122 qemu_sglist_add(qsgl
, *addr
+ skip
, iov
->iov_len
- skip
);
123 copied
+= iov
->iov_len
- skip
;
135 static int virtio_scsi_parse_req(VirtIOSCSIReq
*req
,
136 unsigned req_size
, unsigned resp_size
)
138 size_t in_size
, out_size
;
140 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
141 &req
->req
, req_size
) < req_size
) {
145 if (qemu_iovec_concat_iov(&req
->resp_iov
,
146 req
->elem
.in_sg
, req
->elem
.in_num
, 0,
147 resp_size
) < resp_size
) {
150 req
->resp_size
= resp_size
;
152 out_size
= qemu_sgl_concat(req
, req
->elem
.out_sg
,
153 &req
->elem
.out_addr
[0], req
->elem
.out_num
,
155 in_size
= qemu_sgl_concat(req
, req
->elem
.in_sg
,
156 &req
->elem
.in_addr
[0], req
->elem
.in_num
,
159 if (out_size
&& in_size
) {
164 req
->mode
= SCSI_XFER_TO_DEV
;
165 } else if (in_size
) {
166 req
->mode
= SCSI_XFER_FROM_DEV
;
172 static VirtIOSCSIReq
*virtio_scsi_pop_req(VirtIOSCSI
*s
, VirtQueue
*vq
)
174 VirtIOSCSIReq
*req
= virtio_scsi_init_req(s
, vq
);
175 if (!virtqueue_pop(vq
, &req
->elem
)) {
176 virtio_scsi_free_req(req
);
182 static void virtio_scsi_save_request(QEMUFile
*f
, SCSIRequest
*sreq
)
184 VirtIOSCSIReq
*req
= sreq
->hba_private
;
185 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(req
->dev
);
186 uint32_t n
= virtio_queue_get_id(req
->vq
) - 2;
188 assert(n
< vs
->conf
.num_queues
);
189 qemu_put_be32s(f
, &n
);
190 qemu_put_buffer(f
, (unsigned char *)&req
->elem
, sizeof(req
->elem
));
193 static void *virtio_scsi_load_request(QEMUFile
*f
, SCSIRequest
*sreq
)
195 SCSIBus
*bus
= sreq
->bus
;
196 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
197 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
201 qemu_get_be32s(f
, &n
);
202 assert(n
< vs
->conf
.num_queues
);
203 req
= virtio_scsi_init_req(s
, vs
->cmd_vqs
[n
]);
204 qemu_get_buffer(f
, (unsigned char *)&req
->elem
, sizeof(req
->elem
));
205 /* TODO: add a way for SCSIBusInfo's load_request to fail,
206 * and fail migration instead of asserting here.
207 * When we do, we might be able to re-enable NDEBUG below.
210 #error building with NDEBUG is not supported
212 assert(req
->elem
.in_num
<= ARRAY_SIZE(req
->elem
.in_sg
));
213 assert(req
->elem
.out_num
<= ARRAY_SIZE(req
->elem
.out_sg
));
215 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
216 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
) < 0) {
217 error_report("invalid SCSI request migration data");
223 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
) {
224 assert(req
->sreq
->cmd
.mode
== req
->mode
);
229 static void virtio_scsi_do_tmf(VirtIOSCSI
*s
, VirtIOSCSIReq
*req
)
231 SCSIDevice
*d
= virtio_scsi_device_find(s
, req
->req
.tmf
.lun
);
232 SCSIRequest
*r
, *next
;
236 /* Here VIRTIO_SCSI_S_OK means "FUNCTION COMPLETE". */
237 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_OK
;
239 virtio_tswap32s(VIRTIO_DEVICE(s
), &req
->req
.tmf
.subtype
);
240 switch (req
->req
.tmf
.subtype
) {
241 case VIRTIO_SCSI_T_TMF_ABORT_TASK
:
242 case VIRTIO_SCSI_T_TMF_QUERY_TASK
:
246 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
249 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
250 VirtIOSCSIReq
*cmd_req
= r
->hba_private
;
251 if (cmd_req
&& cmd_req
->req
.cmd
.tag
== req
->req
.tmf
.tag
) {
257 * Assert that the request has not been completed yet, we
258 * check for it in the loop above.
260 assert(r
->hba_private
);
261 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK
) {
262 /* "If the specified command is present in the task set, then
263 * return a service response set to FUNCTION SUCCEEDED".
265 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
272 case VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET
:
276 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
280 qdev_reset_all(&d
->qdev
);
284 case VIRTIO_SCSI_T_TMF_ABORT_TASK_SET
:
285 case VIRTIO_SCSI_T_TMF_CLEAR_TASK_SET
:
286 case VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
:
290 if (d
->lun
!= virtio_scsi_get_lun(req
->req
.tmf
.lun
)) {
293 QTAILQ_FOREACH_SAFE(r
, &d
->requests
, next
, next
) {
294 if (r
->hba_private
) {
295 if (req
->req
.tmf
.subtype
== VIRTIO_SCSI_T_TMF_QUERY_TASK_SET
) {
296 /* "If there is any command present in the task set, then
297 * return a service response set to FUNCTION SUCCEEDED".
299 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_SUCCEEDED
;
308 case VIRTIO_SCSI_T_TMF_I_T_NEXUS_RESET
:
309 target
= req
->req
.tmf
.lun
[1];
311 QTAILQ_FOREACH(kid
, &s
->bus
.qbus
.children
, sibling
) {
312 d
= DO_UPCAST(SCSIDevice
, qdev
, kid
->child
);
313 if (d
->channel
== 0 && d
->id
== target
) {
314 qdev_reset_all(&d
->qdev
);
320 case VIRTIO_SCSI_T_TMF_CLEAR_ACA
:
322 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_FUNCTION_REJECTED
;
329 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_INCORRECT_LUN
;
333 req
->resp
.tmf
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
336 static void virtio_scsi_handle_ctrl(VirtIODevice
*vdev
, VirtQueue
*vq
)
338 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
341 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
344 if (iov_to_buf(req
->elem
.out_sg
, req
->elem
.out_num
, 0,
345 &type
, sizeof(type
)) < sizeof(type
)) {
346 virtio_scsi_bad_req();
350 virtio_tswap32s(vdev
, &req
->req
.tmf
.type
);
351 if (req
->req
.tmf
.type
== VIRTIO_SCSI_T_TMF
) {
352 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlTMFReq
),
353 sizeof(VirtIOSCSICtrlTMFResp
)) < 0) {
354 virtio_scsi_bad_req();
356 virtio_scsi_do_tmf(s
, req
);
359 } else if (req
->req
.tmf
.type
== VIRTIO_SCSI_T_AN_QUERY
||
360 req
->req
.tmf
.type
== VIRTIO_SCSI_T_AN_SUBSCRIBE
) {
361 if (virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICtrlANReq
),
362 sizeof(VirtIOSCSICtrlANResp
)) < 0) {
363 virtio_scsi_bad_req();
365 req
->resp
.an
.event_actual
= 0;
366 req
->resp
.an
.response
= VIRTIO_SCSI_S_OK
;
369 virtio_scsi_complete_req(req
);
373 static void virtio_scsi_complete_cmd_req(VirtIOSCSIReq
*req
)
375 /* Sense data is not in req->resp and is copied separately
376 * in virtio_scsi_command_complete.
378 req
->resp_size
= sizeof(VirtIOSCSICmdResp
);
379 virtio_scsi_complete_req(req
);
382 static void virtio_scsi_command_complete(SCSIRequest
*r
, uint32_t status
,
385 VirtIOSCSIReq
*req
= r
->hba_private
;
386 uint8_t sense
[SCSI_SENSE_BUF_SIZE
];
388 VirtIODevice
*vdev
= VIRTIO_DEVICE(req
->dev
);
390 if (r
->io_canceled
) {
394 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OK
;
395 req
->resp
.cmd
.status
= status
;
396 if (req
->resp
.cmd
.status
== GOOD
) {
397 req
->resp
.cmd
.resid
= virtio_tswap32(vdev
, resid
);
399 req
->resp
.cmd
.resid
= 0;
400 sense_len
= scsi_req_get_sense(r
, sense
, sizeof(sense
));
401 sense_len
= MIN(sense_len
, req
->resp_iov
.size
- sizeof(req
->resp
.cmd
));
402 qemu_iovec_from_buf(&req
->resp_iov
, sizeof(req
->resp
.cmd
),
403 &req
->resp
, sense_len
);
404 req
->resp
.cmd
.sense_len
= virtio_tswap32(vdev
, sense_len
);
406 virtio_scsi_complete_cmd_req(req
);
409 static QEMUSGList
*virtio_scsi_get_sg_list(SCSIRequest
*r
)
411 VirtIOSCSIReq
*req
= r
->hba_private
;
416 static void virtio_scsi_request_cancelled(SCSIRequest
*r
)
418 VirtIOSCSIReq
*req
= r
->hba_private
;
423 if (req
->dev
->resetting
) {
424 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_RESET
;
426 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_ABORTED
;
428 virtio_scsi_complete_cmd_req(req
);
431 static void virtio_scsi_fail_cmd_req(VirtIOSCSIReq
*req
)
433 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_FAILURE
;
434 virtio_scsi_complete_cmd_req(req
);
437 static void virtio_scsi_handle_cmd(VirtIODevice
*vdev
, VirtQueue
*vq
)
439 /* use non-QOM casts in the data path */
440 VirtIOSCSI
*s
= (VirtIOSCSI
*)vdev
;
441 VirtIOSCSICommon
*vs
= &s
->parent_obj
;
446 while ((req
= virtio_scsi_pop_req(s
, vq
))) {
450 rc
= virtio_scsi_parse_req(req
, sizeof(VirtIOSCSICmdReq
) + vs
->cdb_size
,
451 sizeof(VirtIOSCSICmdResp
) + vs
->sense_size
);
453 if (rc
== -ENOTSUP
) {
454 virtio_scsi_fail_cmd_req(req
);
456 virtio_scsi_bad_req();
461 d
= virtio_scsi_device_find(s
, req
->req
.cmd
.lun
);
463 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_BAD_TARGET
;
464 virtio_scsi_complete_cmd_req(req
);
467 req
->sreq
= scsi_req_new(d
, req
->req
.cmd
.tag
,
468 virtio_scsi_get_lun(req
->req
.cmd
.lun
),
471 if (req
->sreq
->cmd
.mode
!= SCSI_XFER_NONE
472 && (req
->sreq
->cmd
.mode
!= req
->mode
||
473 req
->sreq
->cmd
.xfer
> req
->qsgl
.size
)) {
474 req
->resp
.cmd
.response
= VIRTIO_SCSI_S_OVERRUN
;
475 virtio_scsi_complete_cmd_req(req
);
479 n
= scsi_req_enqueue(req
->sreq
);
481 scsi_req_continue(req
->sreq
);
486 static void virtio_scsi_get_config(VirtIODevice
*vdev
,
489 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
490 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(vdev
);
492 virtio_stl_p(vdev
, &scsiconf
->num_queues
, s
->conf
.num_queues
);
493 virtio_stl_p(vdev
, &scsiconf
->seg_max
, 128 - 2);
494 virtio_stl_p(vdev
, &scsiconf
->max_sectors
, s
->conf
.max_sectors
);
495 virtio_stl_p(vdev
, &scsiconf
->cmd_per_lun
, s
->conf
.cmd_per_lun
);
496 virtio_stl_p(vdev
, &scsiconf
->event_info_size
, sizeof(VirtIOSCSIEvent
));
497 virtio_stl_p(vdev
, &scsiconf
->sense_size
, s
->sense_size
);
498 virtio_stl_p(vdev
, &scsiconf
->cdb_size
, s
->cdb_size
);
499 virtio_stw_p(vdev
, &scsiconf
->max_channel
, VIRTIO_SCSI_MAX_CHANNEL
);
500 virtio_stw_p(vdev
, &scsiconf
->max_target
, VIRTIO_SCSI_MAX_TARGET
);
501 virtio_stl_p(vdev
, &scsiconf
->max_lun
, VIRTIO_SCSI_MAX_LUN
);
504 static void virtio_scsi_set_config(VirtIODevice
*vdev
,
505 const uint8_t *config
)
507 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
508 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
510 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) >= 65536 ||
511 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) >= 256) {
512 error_report("bad data written to virtio-scsi configuration space");
516 vs
->sense_size
= virtio_ldl_p(vdev
, &scsiconf
->sense_size
);
517 vs
->cdb_size
= virtio_ldl_p(vdev
, &scsiconf
->cdb_size
);
520 static uint32_t virtio_scsi_get_features(VirtIODevice
*vdev
,
521 uint32_t requested_features
)
523 return requested_features
;
526 static void virtio_scsi_reset(VirtIODevice
*vdev
)
528 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
529 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
532 qbus_reset_all(&s
->bus
.qbus
);
535 vs
->sense_size
= VIRTIO_SCSI_SENSE_SIZE
;
536 vs
->cdb_size
= VIRTIO_SCSI_CDB_SIZE
;
537 s
->events_dropped
= false;
540 /* The device does not have anything to save beyond the virtio data.
541 * Request data is saved with callbacks from SCSI devices.
543 static void virtio_scsi_save(QEMUFile
*f
, void *opaque
)
545 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
546 virtio_save(vdev
, f
);
549 static int virtio_scsi_load(QEMUFile
*f
, void *opaque
, int version_id
)
551 VirtIODevice
*vdev
= VIRTIO_DEVICE(opaque
);
554 ret
= virtio_load(vdev
, f
, version_id
);
561 static void virtio_scsi_push_event(VirtIOSCSI
*s
, SCSIDevice
*dev
,
562 uint32_t event
, uint32_t reason
)
564 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
566 VirtIOSCSIEvent
*evt
;
567 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
569 if (!(vdev
->status
& VIRTIO_CONFIG_S_DRIVER_OK
)) {
573 req
= virtio_scsi_pop_req(s
, vs
->event_vq
);
575 s
->events_dropped
= true;
579 if (s
->events_dropped
) {
580 event
|= VIRTIO_SCSI_T_EVENTS_MISSED
;
581 s
->events_dropped
= false;
584 if (virtio_scsi_parse_req(req
, 0, sizeof(VirtIOSCSIEvent
))) {
585 virtio_scsi_bad_req();
588 evt
= &req
->resp
.event
;
589 memset(evt
, 0, sizeof(VirtIOSCSIEvent
));
590 evt
->event
= virtio_tswap32(vdev
, event
);
591 evt
->reason
= virtio_tswap32(vdev
, reason
);
593 assert(event
== VIRTIO_SCSI_T_EVENTS_MISSED
);
596 evt
->lun
[1] = dev
->id
;
598 /* Linux wants us to keep the same encoding we use for REPORT LUNS. */
599 if (dev
->lun
>= 256) {
600 evt
->lun
[2] = (dev
->lun
>> 8) | 0x40;
602 evt
->lun
[3] = dev
->lun
& 0xFF;
604 virtio_scsi_complete_req(req
);
607 static void virtio_scsi_handle_event(VirtIODevice
*vdev
, VirtQueue
*vq
)
609 VirtIOSCSI
*s
= VIRTIO_SCSI(vdev
);
611 if (s
->events_dropped
) {
612 virtio_scsi_push_event(s
, NULL
, VIRTIO_SCSI_T_NO_EVENT
, 0);
616 static void virtio_scsi_change(SCSIBus
*bus
, SCSIDevice
*dev
, SCSISense sense
)
618 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
619 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
621 if (((vdev
->guest_features
>> VIRTIO_SCSI_F_CHANGE
) & 1) &&
622 dev
->type
!= TYPE_ROM
) {
623 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_PARAM_CHANGE
,
624 sense
.asc
| (sense
.ascq
<< 8));
628 static void virtio_scsi_hotplug(SCSIBus
*bus
, SCSIDevice
*dev
)
630 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
631 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
633 if ((vdev
->guest_features
>> VIRTIO_SCSI_F_HOTPLUG
) & 1) {
634 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_TRANSPORT_RESET
,
635 VIRTIO_SCSI_EVT_RESET_RESCAN
);
639 static void virtio_scsi_hot_unplug(SCSIBus
*bus
, SCSIDevice
*dev
)
641 VirtIOSCSI
*s
= container_of(bus
, VirtIOSCSI
, bus
);
642 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
644 if ((vdev
->guest_features
>> VIRTIO_SCSI_F_HOTPLUG
) & 1) {
645 virtio_scsi_push_event(s
, dev
, VIRTIO_SCSI_T_TRANSPORT_RESET
,
646 VIRTIO_SCSI_EVT_RESET_REMOVED
);
650 static struct SCSIBusInfo virtio_scsi_scsi_info
= {
652 .max_channel
= VIRTIO_SCSI_MAX_CHANNEL
,
653 .max_target
= VIRTIO_SCSI_MAX_TARGET
,
654 .max_lun
= VIRTIO_SCSI_MAX_LUN
,
656 .complete
= virtio_scsi_command_complete
,
657 .cancel
= virtio_scsi_request_cancelled
,
658 .change
= virtio_scsi_change
,
659 .hotplug
= virtio_scsi_hotplug
,
660 .hot_unplug
= virtio_scsi_hot_unplug
,
661 .get_sg_list
= virtio_scsi_get_sg_list
,
662 .save_request
= virtio_scsi_save_request
,
663 .load_request
= virtio_scsi_load_request
,
666 void virtio_scsi_common_realize(DeviceState
*dev
, Error
**errp
,
667 HandleOutput ctrl
, HandleOutput evt
,
670 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
671 VirtIOSCSICommon
*s
= VIRTIO_SCSI_COMMON(dev
);
674 virtio_init(vdev
, "virtio-scsi", VIRTIO_ID_SCSI
,
675 sizeof(VirtIOSCSIConfig
));
677 s
->cmd_vqs
= g_malloc0(s
->conf
.num_queues
* sizeof(VirtQueue
*));
678 s
->sense_size
= VIRTIO_SCSI_SENSE_SIZE
;
679 s
->cdb_size
= VIRTIO_SCSI_CDB_SIZE
;
681 s
->ctrl_vq
= virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
,
683 s
->event_vq
= virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
,
685 for (i
= 0; i
< s
->conf
.num_queues
; i
++) {
686 s
->cmd_vqs
[i
] = virtio_add_queue(vdev
, VIRTIO_SCSI_VQ_SIZE
,
691 static void virtio_scsi_device_realize(DeviceState
*dev
, Error
**errp
)
693 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
694 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
695 static int virtio_scsi_id
;
698 virtio_scsi_common_realize(dev
, &err
, virtio_scsi_handle_ctrl
,
699 virtio_scsi_handle_event
,
700 virtio_scsi_handle_cmd
);
702 error_propagate(errp
, err
);
706 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), dev
,
707 &virtio_scsi_scsi_info
, vdev
->bus_name
);
709 if (!dev
->hotplugged
) {
710 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
712 error_propagate(errp
, err
);
717 register_savevm(dev
, "virtio-scsi", virtio_scsi_id
++, 1,
718 virtio_scsi_save
, virtio_scsi_load
, s
);
721 void virtio_scsi_common_unrealize(DeviceState
*dev
, Error
**errp
)
723 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
724 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
727 virtio_cleanup(vdev
);
730 static void virtio_scsi_device_unrealize(DeviceState
*dev
, Error
**errp
)
732 VirtIOSCSI
*s
= VIRTIO_SCSI(dev
);
734 unregister_savevm(dev
, "virtio-scsi", s
);
736 virtio_scsi_common_unrealize(dev
, errp
);
739 static Property virtio_scsi_properties
[] = {
740 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSI
, parent_obj
.conf
),
741 DEFINE_PROP_END_OF_LIST(),
744 static void virtio_scsi_common_class_init(ObjectClass
*klass
, void *data
)
746 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
747 DeviceClass
*dc
= DEVICE_CLASS(klass
);
749 vdc
->get_config
= virtio_scsi_get_config
;
750 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
753 static void virtio_scsi_class_init(ObjectClass
*klass
, void *data
)
755 DeviceClass
*dc
= DEVICE_CLASS(klass
);
756 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
758 dc
->props
= virtio_scsi_properties
;
759 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
760 vdc
->realize
= virtio_scsi_device_realize
;
761 vdc
->unrealize
= virtio_scsi_device_unrealize
;
762 vdc
->set_config
= virtio_scsi_set_config
;
763 vdc
->get_features
= virtio_scsi_get_features
;
764 vdc
->reset
= virtio_scsi_reset
;
767 static const TypeInfo virtio_scsi_common_info
= {
768 .name
= TYPE_VIRTIO_SCSI_COMMON
,
769 .parent
= TYPE_VIRTIO_DEVICE
,
770 .instance_size
= sizeof(VirtIOSCSICommon
),
772 .class_init
= virtio_scsi_common_class_init
,
775 static const TypeInfo virtio_scsi_info
= {
776 .name
= TYPE_VIRTIO_SCSI
,
777 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
778 .instance_size
= sizeof(VirtIOSCSI
),
779 .class_init
= virtio_scsi_class_init
,
782 static void virtio_register_types(void)
784 type_register_static(&virtio_scsi_common_info
);
785 type_register_static(&virtio_scsi_info
);
788 type_init(virtio_register_types
)