1
2#ifndef _LINUX_VIRTIO_H
3#define _LINUX_VIRTIO_H
4
5
6#include <linux/types.h>
7#include <linux/scatterlist.h>
8#include <linux/spinlock.h>
9#include <linux/device.h>
10#include <linux/mod_devicetable.h>
11#include <linux/gfp.h>
12#include <linux/vringh.h>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28struct virtqueue {
29 struct list_head list;
30 void (*callback)(struct virtqueue *vq);
31 const char *name;
32 struct virtio_device *vdev;
33 unsigned int index;
34 unsigned int num_free;
35 void *priv;
36};
37
38int virtqueue_add_outbuf(struct virtqueue *vq,
39 struct scatterlist sg[], unsigned int num,
40 void *data,
41 gfp_t gfp);
42
43int virtqueue_add_inbuf(struct virtqueue *vq,
44 struct scatterlist sg[], unsigned int num,
45 void *data,
46 gfp_t gfp);
47
48int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
49 struct scatterlist sg[], unsigned int num,
50 void *data,
51 void *ctx,
52 gfp_t gfp);
53
54int virtqueue_add_sgs(struct virtqueue *vq,
55 struct scatterlist *sgs[],
56 unsigned int out_sgs,
57 unsigned int in_sgs,
58 void *data,
59 gfp_t gfp);
60
61bool virtqueue_kick(struct virtqueue *vq);
62
63bool virtqueue_kick_prepare(struct virtqueue *vq);
64
65bool virtqueue_notify(struct virtqueue *vq);
66
67void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
68
69void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
70 void **ctx);
71
72void virtqueue_disable_cb(struct virtqueue *vq);
73
74bool virtqueue_enable_cb(struct virtqueue *vq);
75
76unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
77
78bool virtqueue_poll(struct virtqueue *vq, unsigned);
79
80bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
81
82void *virtqueue_detach_unused_buf(struct virtqueue *vq);
83
84unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
85
86bool virtqueue_is_broken(struct virtqueue *vq);
87
88const struct vring *virtqueue_get_vring(struct virtqueue *vq);
89dma_addr_t virtqueue_get_desc_addr(struct virtqueue *vq);
90dma_addr_t virtqueue_get_avail_addr(struct virtqueue *vq);
91dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
92
93
94
95
96
97static inline void *virtqueue_get_desc(struct virtqueue *vq)
98{
99 return virtqueue_get_vring(vq)->desc;
100}
101static inline void *virtqueue_get_avail(struct virtqueue *vq)
102{
103 return virtqueue_get_vring(vq)->avail;
104}
105static inline void *virtqueue_get_used(struct virtqueue *vq)
106{
107 return virtqueue_get_vring(vq)->used;
108}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125struct virtio_device {
126 int index;
127 bool failed;
128 bool config_enabled;
129 bool config_change_pending;
130 spinlock_t config_lock;
131 struct device dev;
132 struct virtio_device_id id;
133 const struct virtio_config_ops *config;
134 const struct vringh_config_ops *vringh_config;
135 struct list_head vqs;
136 u64 features;
137 void *priv;
138};
139
140static inline struct virtio_device *dev_to_virtio(struct device *_dev)
141{
142 return container_of(_dev, struct virtio_device, dev);
143}
144
145void virtio_add_status(struct virtio_device *dev, unsigned int status);
146int register_virtio_device(struct virtio_device *dev);
147void unregister_virtio_device(struct virtio_device *dev);
148
149void virtio_break_device(struct virtio_device *dev);
150
151void virtio_config_changed(struct virtio_device *dev);
152void virtio_config_disable(struct virtio_device *dev);
153void virtio_config_enable(struct virtio_device *dev);
154int virtio_finalize_features(struct virtio_device *dev);
155#ifdef CONFIG_PM_SLEEP
156int virtio_device_freeze(struct virtio_device *dev);
157int virtio_device_restore(struct virtio_device *dev);
158#endif
159
160#define virtio_device_for_each_vq(vdev, vq) \
161 list_for_each_entry(vq, &vdev->vqs, list)
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180struct virtio_driver {
181 struct device_driver driver;
182 const struct virtio_device_id *id_table;
183 const unsigned int *feature_table;
184 unsigned int feature_table_size;
185 const unsigned int *feature_table_legacy;
186 unsigned int feature_table_size_legacy;
187 int (*validate)(struct virtio_device *dev);
188 int (*probe)(struct virtio_device *dev);
189 void (*scan)(struct virtio_device *dev);
190 void (*remove)(struct virtio_device *dev);
191 void (*config_changed)(struct virtio_device *dev);
192#ifdef CONFIG_PM
193 int (*freeze)(struct virtio_device *dev);
194 int (*restore)(struct virtio_device *dev);
195#endif
196};
197
198static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
199{
200 return container_of(drv, struct virtio_driver, driver);
201}
202
203int register_virtio_driver(struct virtio_driver *drv);
204void unregister_virtio_driver(struct virtio_driver *drv);
205
206
207
208
209
210
211#define module_virtio_driver(__virtio_driver) \
212 module_driver(__virtio_driver, register_virtio_driver, \
213 unregister_virtio_driver)
214#endif
215