xref: /OK3568_Linux_fs/kernel/include/linux/virtio.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_VIRTIO_H
3*4882a593Smuzhiyun #define _LINUX_VIRTIO_H
4*4882a593Smuzhiyun /* Everything a virtio driver needs to work with any particular virtio
5*4882a593Smuzhiyun  * implementation. */
6*4882a593Smuzhiyun #include <linux/types.h>
7*4882a593Smuzhiyun #include <linux/scatterlist.h>
8*4882a593Smuzhiyun #include <linux/spinlock.h>
9*4882a593Smuzhiyun #include <linux/device.h>
10*4882a593Smuzhiyun #include <linux/mod_devicetable.h>
11*4882a593Smuzhiyun #include <linux/gfp.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /**
14*4882a593Smuzhiyun  * virtqueue - a queue to register buffers for sending or receiving.
15*4882a593Smuzhiyun  * @list: the chain of virtqueues for this device
16*4882a593Smuzhiyun  * @callback: the function to call when buffers are consumed (can be NULL).
17*4882a593Smuzhiyun  * @name: the name of this virtqueue (mainly for debugging)
18*4882a593Smuzhiyun  * @vdev: the virtio device this queue was created for.
19*4882a593Smuzhiyun  * @priv: a pointer for the virtqueue implementation to use.
20*4882a593Smuzhiyun  * @index: the zero-based ordinal number for this queue.
21*4882a593Smuzhiyun  * @num_free: number of elements we expect to be able to fit.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * A note on @num_free: with indirect buffers, each buffer needs one
24*4882a593Smuzhiyun  * element in the queue, otherwise a buffer will need one element per
25*4882a593Smuzhiyun  * sg element.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun struct virtqueue {
28*4882a593Smuzhiyun 	struct list_head list;
29*4882a593Smuzhiyun 	void (*callback)(struct virtqueue *vq);
30*4882a593Smuzhiyun 	const char *name;
31*4882a593Smuzhiyun 	struct virtio_device *vdev;
32*4882a593Smuzhiyun 	unsigned int index;
33*4882a593Smuzhiyun 	unsigned int num_free;
34*4882a593Smuzhiyun 	void *priv;
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun int virtqueue_add_outbuf(struct virtqueue *vq,
38*4882a593Smuzhiyun 			 struct scatterlist sg[], unsigned int num,
39*4882a593Smuzhiyun 			 void *data,
40*4882a593Smuzhiyun 			 gfp_t gfp);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun int virtqueue_add_inbuf(struct virtqueue *vq,
43*4882a593Smuzhiyun 			struct scatterlist sg[], unsigned int num,
44*4882a593Smuzhiyun 			void *data,
45*4882a593Smuzhiyun 			gfp_t gfp);
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
48*4882a593Smuzhiyun 			    struct scatterlist sg[], unsigned int num,
49*4882a593Smuzhiyun 			    void *data,
50*4882a593Smuzhiyun 			    void *ctx,
51*4882a593Smuzhiyun 			    gfp_t gfp);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun int virtqueue_add_sgs(struct virtqueue *vq,
54*4882a593Smuzhiyun 		      struct scatterlist *sgs[],
55*4882a593Smuzhiyun 		      unsigned int out_sgs,
56*4882a593Smuzhiyun 		      unsigned int in_sgs,
57*4882a593Smuzhiyun 		      void *data,
58*4882a593Smuzhiyun 		      gfp_t gfp);
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun bool virtqueue_kick(struct virtqueue *vq);
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun bool virtqueue_kick_prepare(struct virtqueue *vq);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun bool virtqueue_notify(struct virtqueue *vq);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
69*4882a593Smuzhiyun 			    void **ctx);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun void virtqueue_disable_cb(struct virtqueue *vq);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun bool virtqueue_enable_cb(struct virtqueue *vq);
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun bool virtqueue_poll(struct virtqueue *vq, unsigned);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun void *virtqueue_detach_unused_buf(struct virtqueue *vq);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun bool virtqueue_is_broken(struct virtqueue *vq);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun const struct vring *virtqueue_get_vring(struct virtqueue *vq);
88*4882a593Smuzhiyun dma_addr_t virtqueue_get_desc_addr(struct virtqueue *vq);
89*4882a593Smuzhiyun dma_addr_t virtqueue_get_avail_addr(struct virtqueue *vq);
90*4882a593Smuzhiyun dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * virtio_device - representation of a device using virtio
94*4882a593Smuzhiyun  * @index: unique position on the virtio bus
95*4882a593Smuzhiyun  * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
96*4882a593Smuzhiyun  * @config_enabled: configuration change reporting enabled
97*4882a593Smuzhiyun  * @config_change_pending: configuration change reported while disabled
98*4882a593Smuzhiyun  * @config_lock: protects configuration change reporting
99*4882a593Smuzhiyun  * @dev: underlying device.
100*4882a593Smuzhiyun  * @id: the device type identification (used to match it with a driver).
101*4882a593Smuzhiyun  * @config: the configuration ops for this device.
102*4882a593Smuzhiyun  * @vringh_config: configuration ops for host vrings.
103*4882a593Smuzhiyun  * @vqs: the list of virtqueues for this device.
104*4882a593Smuzhiyun  * @features: the features supported by both driver and device.
105*4882a593Smuzhiyun  * @priv: private pointer for the driver's use.
106*4882a593Smuzhiyun  */
107*4882a593Smuzhiyun struct virtio_device {
108*4882a593Smuzhiyun 	int index;
109*4882a593Smuzhiyun 	bool failed;
110*4882a593Smuzhiyun 	bool config_enabled;
111*4882a593Smuzhiyun 	bool config_change_pending;
112*4882a593Smuzhiyun 	spinlock_t config_lock;
113*4882a593Smuzhiyun 	struct device dev;
114*4882a593Smuzhiyun 	struct virtio_device_id id;
115*4882a593Smuzhiyun 	const struct virtio_config_ops *config;
116*4882a593Smuzhiyun 	const struct vringh_config_ops *vringh_config;
117*4882a593Smuzhiyun 	struct list_head vqs;
118*4882a593Smuzhiyun 	u64 features;
119*4882a593Smuzhiyun 	void *priv;
120*4882a593Smuzhiyun };
121*4882a593Smuzhiyun 
dev_to_virtio(struct device * _dev)122*4882a593Smuzhiyun static inline struct virtio_device *dev_to_virtio(struct device *_dev)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	return container_of(_dev, struct virtio_device, dev);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun void virtio_add_status(struct virtio_device *dev, unsigned int status);
128*4882a593Smuzhiyun int register_virtio_device(struct virtio_device *dev);
129*4882a593Smuzhiyun void unregister_virtio_device(struct virtio_device *dev);
130*4882a593Smuzhiyun bool is_virtio_device(struct device *dev);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun void virtio_break_device(struct virtio_device *dev);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun void virtio_config_changed(struct virtio_device *dev);
135*4882a593Smuzhiyun void virtio_config_disable(struct virtio_device *dev);
136*4882a593Smuzhiyun void virtio_config_enable(struct virtio_device *dev);
137*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
138*4882a593Smuzhiyun int virtio_device_freeze(struct virtio_device *dev);
139*4882a593Smuzhiyun int virtio_device_restore(struct virtio_device *dev);
140*4882a593Smuzhiyun #endif
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun size_t virtio_max_dma_size(struct virtio_device *vdev);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun #define virtio_device_for_each_vq(vdev, vq) \
145*4882a593Smuzhiyun 	list_for_each_entry(vq, &vdev->vqs, list)
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /**
148*4882a593Smuzhiyun  * virtio_driver - operations for a virtio I/O driver
149*4882a593Smuzhiyun  * @driver: underlying device driver (populate name and owner).
150*4882a593Smuzhiyun  * @id_table: the ids serviced by this driver.
151*4882a593Smuzhiyun  * @feature_table: an array of feature numbers supported by this driver.
152*4882a593Smuzhiyun  * @feature_table_size: number of entries in the feature table array.
153*4882a593Smuzhiyun  * @feature_table_legacy: same as feature_table but when working in legacy mode.
154*4882a593Smuzhiyun  * @feature_table_size_legacy: number of entries in feature table legacy array.
155*4882a593Smuzhiyun  * @probe: the function to call when a device is found.  Returns 0 or -errno.
156*4882a593Smuzhiyun  * @scan: optional function to call after successful probe; intended
157*4882a593Smuzhiyun  *    for virtio-scsi to invoke a scan.
158*4882a593Smuzhiyun  * @remove: the function to call when a device is removed.
159*4882a593Smuzhiyun  * @config_changed: optional function to call when the device configuration
160*4882a593Smuzhiyun  *    changes; may be called in interrupt context.
161*4882a593Smuzhiyun  * @freeze: optional function to call during suspend/hibernation.
162*4882a593Smuzhiyun  * @restore: optional function to call on resume.
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun struct virtio_driver {
165*4882a593Smuzhiyun 	struct device_driver driver;
166*4882a593Smuzhiyun 	const struct virtio_device_id *id_table;
167*4882a593Smuzhiyun 	const unsigned int *feature_table;
168*4882a593Smuzhiyun 	unsigned int feature_table_size;
169*4882a593Smuzhiyun 	const unsigned int *feature_table_legacy;
170*4882a593Smuzhiyun 	unsigned int feature_table_size_legacy;
171*4882a593Smuzhiyun 	int (*validate)(struct virtio_device *dev);
172*4882a593Smuzhiyun 	int (*probe)(struct virtio_device *dev);
173*4882a593Smuzhiyun 	void (*scan)(struct virtio_device *dev);
174*4882a593Smuzhiyun 	void (*remove)(struct virtio_device *dev);
175*4882a593Smuzhiyun 	void (*config_changed)(struct virtio_device *dev);
176*4882a593Smuzhiyun #ifdef CONFIG_PM
177*4882a593Smuzhiyun 	int (*freeze)(struct virtio_device *dev);
178*4882a593Smuzhiyun 	int (*restore)(struct virtio_device *dev);
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun 
drv_to_virtio(struct device_driver * drv)182*4882a593Smuzhiyun static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	return container_of(drv, struct virtio_driver, driver);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun int register_virtio_driver(struct virtio_driver *drv);
188*4882a593Smuzhiyun void unregister_virtio_driver(struct virtio_driver *drv);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun /* module_virtio_driver() - Helper macro for drivers that don't do
191*4882a593Smuzhiyun  * anything special in module init/exit.  This eliminates a lot of
192*4882a593Smuzhiyun  * boilerplate.  Each module may only use this macro once, and
193*4882a593Smuzhiyun  * calling it replaces module_init() and module_exit()
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun #define module_virtio_driver(__virtio_driver) \
196*4882a593Smuzhiyun 	module_driver(__virtio_driver, register_virtio_driver, \
197*4882a593Smuzhiyun 			unregister_virtio_driver)
198*4882a593Smuzhiyun #endif /* _LINUX_VIRTIO_H */
199