blob: 14f6cf650ecfe441c221a4fe5ffb36a0365c31dd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Rusty Russellec3d41c2007-10-22 11:03:36 +10002#ifndef _LINUX_VIRTIO_H
3#define _LINUX_VIRTIO_H
4/* Everything a virtio driver needs to work with any particular virtio
5 * implementation. */
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>
Michael S. Tsirkinbbd603e2010-04-29 17:26:37 +030011#include <linux/gfp.h>
Sjur Brændeland3beee862013-03-20 13:51:24 +103012#include <linux/vringh.h>
Rusty Russellec3d41c2007-10-22 11:03:36 +100013
14/**
15 * virtqueue - a queue to register buffers for sending or receiving.
Rusty Russell9499f5e2009-06-12 22:16:35 -060016 * @list: the chain of virtqueues for this device
Rusty Russellec3d41c2007-10-22 11:03:36 +100017 * @callback: the function to call when buffers are consumed (can be NULL).
Rusty Russell9499f5e2009-06-12 22:16:35 -060018 * @name: the name of this virtqueue (mainly for debugging)
Rusty Russellec3d41c2007-10-22 11:03:36 +100019 * @vdev: the virtio device this queue was created for.
Rusty Russellec3d41c2007-10-22 11:03:36 +100020 * @priv: a pointer for the virtqueue implementation to use.
Rusty Russell06ca2872012-10-16 23:56:14 +103021 * @index: the zero-based ordinal number for this queue.
22 * @num_free: number of elements we expect to be able to fit.
23 *
24 * A note on @num_free: with indirect buffers, each buffer needs one
25 * element in the queue, otherwise a buffer will need one element per
26 * sg element.
Rusty Russellec3d41c2007-10-22 11:03:36 +100027 */
Rusty Russell9499f5e2009-06-12 22:16:35 -060028struct virtqueue {
29 struct list_head list;
Rusty Russell18445c42008-02-04 23:49:57 -050030 void (*callback)(struct virtqueue *vq);
Rusty Russell9499f5e2009-06-12 22:16:35 -060031 const char *name;
Rusty Russellec3d41c2007-10-22 11:03:36 +100032 struct virtio_device *vdev;
Rusty Russell06ca2872012-10-16 23:56:14 +103033 unsigned int index;
34 unsigned int num_free;
Rusty Russellec3d41c2007-10-22 11:03:36 +100035 void *priv;
36};
37
Rusty Russell282edb32013-03-20 15:44:26 +103038int 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
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +020048int 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
Rusty Russell13816c72013-03-20 15:37:09 +103054int 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
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +103061bool virtqueue_kick(struct virtqueue *vq);
Rusty Russellec3d41c2007-10-22 11:03:36 +100062
Rusty Russell41f03772012-01-12 15:44:43 +103063bool virtqueue_kick_prepare(struct virtqueue *vq);
64
Heinz Graalfs5b1bf7c2013-10-29 09:39:48 +103065bool virtqueue_notify(struct virtqueue *vq);
Rusty Russell41f03772012-01-12 15:44:43 +103066
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +030067void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
Rusty Russellec3d41c2007-10-22 11:03:36 +100068
Michael S. Tsirkin5a08b042017-02-07 06:15:13 +020069void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
70 void **ctx);
71
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +030072void virtqueue_disable_cb(struct virtqueue *vq);
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +030073
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +030074bool virtqueue_enable_cb(struct virtqueue *vq);
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +030075
Michael S. Tsirkincc229882013-07-09 13:19:18 +030076unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
77
78bool virtqueue_poll(struct virtqueue *vq, unsigned);
79
Michael S. Tsirkin7ab358c2011-05-20 02:11:14 +030080bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
81
Michael S. Tsirkin7c5e9ed2010-04-12 16:19:07 +030082void *virtqueue_detach_unused_buf(struct virtqueue *vq);
Michael S. Tsirkin316f25f2010-04-12 16:18:25 +030083
Rick Jones8f9f4662011-10-19 08:10:59 +000084unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
85
Heinz Graalfsb3b32c92013-10-29 09:40:19 +103086bool virtqueue_is_broken(struct virtqueue *vq);
87
Andy Lutomirski2a2d1382016-02-02 21:46:37 -080088const 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 * Legacy accessors -- in almost all cases, these are the wrong functions
95 * to use.
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}
Cornelia Huck89062652014-10-07 16:39:47 +0200109
Rusty Russellec3d41c2007-10-22 11:03:36 +1000110/**
111 * virtio_device - representation of a device using virtio
112 * @index: unique position on the virtio bus
Paul Bollecbd7f8d2014-11-10 09:33:29 +1030113 * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030114 * @config_enabled: configuration change reporting enabled
115 * @config_change_pending: configuration change reported while disabled
116 * @config_lock: protects configuration change reporting
Rusty Russellec3d41c2007-10-22 11:03:36 +1000117 * @dev: underlying device.
118 * @id: the device type identification (used to match it with a driver).
119 * @config: the configuration ops for this device.
Sjur Brændeland3beee862013-03-20 13:51:24 +1030120 * @vringh_config: configuration ops for host vrings.
Rusty Russell9499f5e2009-06-12 22:16:35 -0600121 * @vqs: the list of virtqueues for this device.
Rusty Russellc45a6812008-05-02 21:50:50 -0500122 * @features: the features supported by both driver and device.
Rusty Russellec3d41c2007-10-22 11:03:36 +1000123 * @priv: private pointer for the driver's use.
124 */
Rusty Russell9499f5e2009-06-12 22:16:35 -0600125struct virtio_device {
Rusty Russellec3d41c2007-10-22 11:03:36 +1000126 int index;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030127 bool failed;
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030128 bool config_enabled;
129 bool config_change_pending;
130 spinlock_t config_lock;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000131 struct device dev;
132 struct virtio_device_id id;
Stephen Hemminger93503932013-02-10 15:57:38 +1030133 const struct virtio_config_ops *config;
Sjur Brændeland3beee862013-03-20 13:51:24 +1030134 const struct vringh_config_ops *vringh_config;
Rusty Russell9499f5e2009-06-12 22:16:35 -0600135 struct list_head vqs;
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200136 u64 features;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000137 void *priv;
138};
139
Wanlong Gao9bffdca2012-12-11 11:04:50 +1030140static inline struct virtio_device *dev_to_virtio(struct device *_dev)
141{
142 return container_of(_dev, struct virtio_device, dev);
143}
144
John Fastabend9fe7bfc2017-02-02 19:16:01 -0800145void virtio_add_status(struct virtio_device *dev, unsigned int status);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000146int register_virtio_device(struct virtio_device *dev);
147void unregister_virtio_device(struct virtio_device *dev);
148
Rusty Russelle2dcdfe2014-04-28 11:15:08 +0930149void virtio_break_device(struct virtio_device *dev);
150
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030151void virtio_config_changed(struct virtio_device *dev);
John Fastabend9fe7bfc2017-02-02 19:16:01 -0800152void virtio_config_disable(struct virtio_device *dev);
153void virtio_config_enable(struct virtio_device *dev);
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030154#ifdef CONFIG_PM_SLEEP
155int virtio_device_freeze(struct virtio_device *dev);
156int virtio_device_restore(struct virtio_device *dev);
157#endif
Michael S. Tsirkin016c98c2014-10-14 10:40:34 +1030158
Michael S. Tsirkin7ae93ff2018-04-20 20:22:40 +0300159#define virtio_device_for_each_vq(vdev, vq) \
160 list_for_each_entry(vq, &vdev->vqs, list)
161
Rusty Russellec3d41c2007-10-22 11:03:36 +1000162/**
163 * virtio_driver - operations for a virtio I/O driver
164 * @driver: underlying device driver (populate name and owner).
165 * @id_table: the ids serviced by this driver.
Wang Sheng-Hui5f41f8b2011-08-25 21:04:05 +0800166 * @feature_table: an array of feature numbers supported by this driver.
Rusty Russellc45a6812008-05-02 21:50:50 -0500167 * @feature_table_size: number of entries in the feature table array.
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300168 * @feature_table_legacy: same as feature_table but when working in legacy mode.
169 * @feature_table_size_legacy: number of entries in feature table legacy array.
Rusty Russell20f77f52009-06-12 22:16:33 -0600170 * @probe: the function to call when a device is found. Returns 0 or -errno.
Cornelia Huck9ea762a2017-03-30 13:13:33 +0200171 * @scan: optional function to call after successful probe; intended
172 * for virtio-scsi to invoke a scan.
Wang Sheng-Hui5f41f8b2011-08-25 21:04:05 +0800173 * @remove: the function to call when a device is removed.
Rusty Russellf957d1f2008-02-04 23:49:58 -0500174 * @config_changed: optional function to call when the device configuration
175 * changes; may be called in interrupt context.
Cornelia Huck9ea762a2017-03-30 13:13:33 +0200176 * @freeze: optional function to call during suspend/hibernation.
177 * @restore: optional function to call on resume.
Rusty Russellec3d41c2007-10-22 11:03:36 +1000178 */
179struct virtio_driver {
180 struct device_driver driver;
181 const struct virtio_device_id *id_table;
Rusty Russellc45a6812008-05-02 21:50:50 -0500182 const unsigned int *feature_table;
183 unsigned int feature_table_size;
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300184 const unsigned int *feature_table_legacy;
185 unsigned int feature_table_size_legacy;
Michael S. Tsirkin404123c2017-03-29 19:06:20 +0300186 int (*validate)(struct virtio_device *dev);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000187 int (*probe)(struct virtio_device *dev);
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000188 void (*scan)(struct virtio_device *dev);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000189 void (*remove)(struct virtio_device *dev);
Rusty Russellf957d1f2008-02-04 23:49:58 -0500190 void (*config_changed)(struct virtio_device *dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530191#ifdef CONFIG_PM
192 int (*freeze)(struct virtio_device *dev);
Amit Shahf0fe6f12011-12-22 16:58:26 +0530193 int (*restore)(struct virtio_device *dev);
194#endif
Rusty Russellec3d41c2007-10-22 11:03:36 +1000195};
196
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800197static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
198{
199 return container_of(drv, struct virtio_driver, driver);
200}
201
Rusty Russellec3d41c2007-10-22 11:03:36 +1000202int register_virtio_driver(struct virtio_driver *drv);
203void unregister_virtio_driver(struct virtio_driver *drv);
Sjur Brændeland6e105e02013-02-13 15:52:36 +1030204
205/* module_virtio_driver() - Helper macro for drivers that don't do
206 * anything special in module init/exit. This eliminates a lot of
207 * boilerplate. Each module may only use this macro once, and
208 * calling it replaces module_init() and module_exit()
209 */
210#define module_virtio_driver(__virtio_driver) \
211 module_driver(__virtio_driver, register_virtio_driver, \
212 unregister_virtio_driver)
Rusty Russellec3d41c2007-10-22 11:03:36 +1000213#endif /* _LINUX_VIRTIO_H */