xref: /openbmc/qemu/include/hw/virtio/virtio-scsi.h (revision d27532e4)
1 /*
2  * Virtio SCSI HBA
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  *  Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef QEMU_VIRTIO_SCSI_H
15 #define QEMU_VIRTIO_SCSI_H
16 #include "qom/object.h"
17 
18 /* Override CDB/sense data size: they are dynamic (guest controlled) in QEMU */
19 #define VIRTIO_SCSI_CDB_SIZE 0
20 #define VIRTIO_SCSI_SENSE_SIZE 0
21 #include "standard-headers/linux/virtio_scsi.h"
22 #include "hw/virtio/virtio.h"
23 #include "hw/scsi/scsi.h"
24 #include "chardev/char-fe.h"
25 #include "sysemu/iothread.h"
26 
27 #define TYPE_VIRTIO_SCSI_COMMON "virtio-scsi-common"
28 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSCSICommon, VIRTIO_SCSI_COMMON)
29 
30 #define TYPE_VIRTIO_SCSI "virtio-scsi-device"
31 OBJECT_DECLARE_SIMPLE_TYPE(VirtIOSCSI, VIRTIO_SCSI)
32 
33 #define VIRTIO_SCSI_MAX_CHANNEL 0
34 #define VIRTIO_SCSI_MAX_TARGET  255
35 #define VIRTIO_SCSI_MAX_LUN     16383
36 
37 /* Number of virtqueues that are always present */
38 #define VIRTIO_SCSI_VQ_NUM_FIXED    2
39 
40 #define VIRTIO_SCSI_AUTO_NUM_QUEUES UINT32_MAX
41 
42 typedef struct virtio_scsi_cmd_req VirtIOSCSICmdReq;
43 typedef struct virtio_scsi_cmd_resp VirtIOSCSICmdResp;
44 typedef struct virtio_scsi_ctrl_tmf_req VirtIOSCSICtrlTMFReq;
45 typedef struct virtio_scsi_ctrl_tmf_resp VirtIOSCSICtrlTMFResp;
46 typedef struct virtio_scsi_ctrl_an_req VirtIOSCSICtrlANReq;
47 typedef struct virtio_scsi_ctrl_an_resp VirtIOSCSICtrlANResp;
48 typedef struct virtio_scsi_event VirtIOSCSIEvent;
49 typedef struct virtio_scsi_config VirtIOSCSIConfig;
50 
51 struct VirtIOSCSIConf {
52     uint32_t num_queues;
53     uint32_t virtqueue_size;
54     bool seg_max_adjust;
55     uint32_t max_sectors;
56     uint32_t cmd_per_lun;
57     char *vhostfd;
58     char *wwpn;
59     CharBackend chardev;
60     uint32_t boot_tpgt;
61     IOThread *iothread;
62 };
63 
64 struct VirtIOSCSI;
65 
66 struct VirtIOSCSICommon {
67     VirtIODevice parent_obj;
68     VirtIOSCSIConf conf;
69 
70     uint32_t sense_size;
71     uint32_t cdb_size;
72     VirtQueue *ctrl_vq;
73     VirtQueue *event_vq;
74     VirtQueue **cmd_vqs;
75 };
76 
77 struct VirtIOSCSIReq;
78 
79 struct VirtIOSCSI {
80     VirtIOSCSICommon parent_obj;
81 
82     SCSIBus bus;
83     int resetting; /* written from main loop thread, read from any thread */
84     bool events_dropped;
85 
86     /*
87      * TMFs deferred to main loop BH. These fields are protected by
88      * virtio_scsi_acquire().
89      */
90     QEMUBH *tmf_bh;
91     QTAILQ_HEAD(, VirtIOSCSIReq) tmf_bh_list;
92 
93     /* Fields for dataplane below */
94     AioContext *ctx; /* one iothread per virtio-scsi-pci for now */
95 
96     bool dataplane_started;
97     bool dataplane_starting;
98     bool dataplane_stopping;
99     bool dataplane_fenced;
100     uint32_t host_features;
101 };
102 
103 static inline void virtio_scsi_acquire(VirtIOSCSI *s)
104 {
105     if (s->ctx) {
106         aio_context_acquire(s->ctx);
107     }
108 }
109 
110 static inline void virtio_scsi_release(VirtIOSCSI *s)
111 {
112     if (s->ctx) {
113         aio_context_release(s->ctx);
114     }
115 }
116 
117 void virtio_scsi_common_realize(DeviceState *dev,
118                                 VirtIOHandleOutput ctrl,
119                                 VirtIOHandleOutput evt,
120                                 VirtIOHandleOutput cmd,
121                                 Error **errp);
122 
123 void virtio_scsi_common_unrealize(DeviceState *dev);
124 
125 void virtio_scsi_dataplane_setup(VirtIOSCSI *s, Error **errp);
126 int virtio_scsi_dataplane_start(VirtIODevice *s);
127 void virtio_scsi_dataplane_stop(VirtIODevice *s);
128 
129 #endif /* QEMU_VIRTIO_SCSI_H */
130