Blame view

include/linux/virtio.h 6.26 KB
ec3d41c4d   Rusty Russell   Virtio interface
1
2
3
4
5
6
7
8
9
  #ifndef _LINUX_VIRTIO_H
  #define _LINUX_VIRTIO_H
  /* Everything a virtio driver needs to work with any particular virtio
   * implementation. */
  #include <linux/types.h>
  #include <linux/scatterlist.h>
  #include <linux/spinlock.h>
  #include <linux/device.h>
  #include <linux/mod_devicetable.h>
bbd603efb   Michael S. Tsirkin   virtio: add_buf_gfp
10
  #include <linux/gfp.h>
3beee86a4   Sjur Brændeland   virtio: Introduce...
11
  #include <linux/vringh.h>
ec3d41c4d   Rusty Russell   Virtio interface
12
13
14
  
  /**
   * virtqueue - a queue to register buffers for sending or receiving.
9499f5e7e   Rusty Russell   virtio: add names...
15
   * @list: the chain of virtqueues for this device
ec3d41c4d   Rusty Russell   Virtio interface
16
   * @callback: the function to call when buffers are consumed (can be NULL).
9499f5e7e   Rusty Russell   virtio: add names...
17
   * @name: the name of this virtqueue (mainly for debugging)
ec3d41c4d   Rusty Russell   Virtio interface
18
   * @vdev: the virtio device this queue was created for.
ec3d41c4d   Rusty Russell   Virtio interface
19
   * @priv: a pointer for the virtqueue implementation to use.
06ca287db   Rusty Russell   virtio: move queu...
20
21
22
23
24
25
   * @index: the zero-based ordinal number for this queue.
   * @num_free: number of elements we expect to be able to fit.
   *
   * A note on @num_free: with indirect buffers, each buffer needs one
   * element in the queue, otherwise a buffer will need one element per
   * sg element.
ec3d41c4d   Rusty Russell   Virtio interface
26
   */
9499f5e7e   Rusty Russell   virtio: add names...
27
28
  struct virtqueue {
  	struct list_head list;
18445c4d5   Rusty Russell   virtio: explicit ...
29
  	void (*callback)(struct virtqueue *vq);
9499f5e7e   Rusty Russell   virtio: add names...
30
  	const char *name;
ec3d41c4d   Rusty Russell   Virtio interface
31
  	struct virtio_device *vdev;
06ca287db   Rusty Russell   virtio: move queu...
32
33
  	unsigned int index;
  	unsigned int num_free;
ec3d41c4d   Rusty Russell   Virtio interface
34
35
  	void *priv;
  };
282edb364   Rusty Russell   virtio_ring: virt...
36
37
38
39
40
41
42
43
44
  int virtqueue_add_outbuf(struct virtqueue *vq,
  			 struct scatterlist sg[], unsigned int num,
  			 void *data,
  			 gfp_t gfp);
  
  int virtqueue_add_inbuf(struct virtqueue *vq,
  			struct scatterlist sg[], unsigned int num,
  			void *data,
  			gfp_t gfp);
13816c768   Rusty Russell   virtio_ring: virt...
45
46
47
48
49
50
  int virtqueue_add_sgs(struct virtqueue *vq,
  		      struct scatterlist *sgs[],
  		      unsigned int out_sgs,
  		      unsigned int in_sgs,
  		      void *data,
  		      gfp_t gfp);
5b1bf7cb6   Heinz Graalfs   virtio_ring: let ...
51
  bool virtqueue_kick(struct virtqueue *vq);
ec3d41c4d   Rusty Russell   Virtio interface
52

41f0377f7   Rusty Russell   virtio: support u...
53
  bool virtqueue_kick_prepare(struct virtqueue *vq);
5b1bf7cb6   Heinz Graalfs   virtio_ring: let ...
54
  bool virtqueue_notify(struct virtqueue *vq);
41f0377f7   Rusty Russell   virtio: support u...
55

7c5e9ed0c   Michael S. Tsirkin   virtio_ring: remo...
56
  void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
316f25f5b   Michael S. Tsirkin   virtio: add virtq...
57

7c5e9ed0c   Michael S. Tsirkin   virtio_ring: remo...
58
  void virtqueue_disable_cb(struct virtqueue *vq);
316f25f5b   Michael S. Tsirkin   virtio: add virtq...
59

7c5e9ed0c   Michael S. Tsirkin   virtio_ring: remo...
60
  bool virtqueue_enable_cb(struct virtqueue *vq);
316f25f5b   Michael S. Tsirkin   virtio: add virtq...
61

cc229884d   Michael S. Tsirkin   virtio: support u...
62
63
64
  unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
  
  bool virtqueue_poll(struct virtqueue *vq, unsigned);
7ab358c23   Michael S. Tsirkin   virtio: add api f...
65
  bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
7c5e9ed0c   Michael S. Tsirkin   virtio_ring: remo...
66
  void *virtqueue_detach_unused_buf(struct virtqueue *vq);
316f25f5b   Michael S. Tsirkin   virtio: add virtq...
67

8f9f4668b   Rick Jones   Add ethtool -g su...
68
  unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
17bb6d408   Jason Wang   virtio-ring: move...
69

b3b32c941   Heinz Graalfs   virtio_ring: add ...
70
  bool virtqueue_is_broken(struct virtqueue *vq);
2a2d1382f   Andy Lutomirski   virtio: Add impro...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  const struct vring *virtqueue_get_vring(struct virtqueue *vq);
  dma_addr_t virtqueue_get_desc_addr(struct virtqueue *vq);
  dma_addr_t virtqueue_get_avail_addr(struct virtqueue *vq);
  dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
  
  /*
   * Legacy accessors -- in almost all cases, these are the wrong functions
   * to use.
   */
  static inline void *virtqueue_get_desc(struct virtqueue *vq)
  {
  	return virtqueue_get_vring(vq)->desc;
  }
  static inline void *virtqueue_get_avail(struct virtqueue *vq)
  {
  	return virtqueue_get_vring(vq)->avail;
  }
  static inline void *virtqueue_get_used(struct virtqueue *vq)
  {
  	return virtqueue_get_vring(vq)->used;
  }
890626521   Cornelia Huck   virtio: allow tra...
92

ec3d41c4d   Rusty Russell   Virtio interface
93
94
95
  /**
   * virtio_device - representation of a device using virtio
   * @index: unique position on the virtio bus
cbd7f8d68   Paul Bolle   virtio: Fix comme...
96
   * @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
22b7050a0   Michael S. Tsirkin   virtio: defer con...
97
98
99
   * @config_enabled: configuration change reporting enabled
   * @config_change_pending: configuration change reported while disabled
   * @config_lock: protects configuration change reporting
ec3d41c4d   Rusty Russell   Virtio interface
100
101
102
   * @dev: underlying device.
   * @id: the device type identification (used to match it with a driver).
   * @config: the configuration ops for this device.
3beee86a4   Sjur Brændeland   virtio: Introduce...
103
   * @vringh_config: configuration ops for host vrings.
9499f5e7e   Rusty Russell   virtio: add names...
104
   * @vqs: the list of virtqueues for this device.
c45a6816c   Rusty Russell   virtio: explicit ...
105
   * @features: the features supported by both driver and device.
ec3d41c4d   Rusty Russell   Virtio interface
106
107
   * @priv: private pointer for the driver's use.
   */
9499f5e7e   Rusty Russell   virtio: add names...
108
  struct virtio_device {
ec3d41c4d   Rusty Russell   Virtio interface
109
  	int index;
c6716bae5   Michael S. Tsirkin   virtio-pci: move ...
110
  	bool failed;
22b7050a0   Michael S. Tsirkin   virtio: defer con...
111
112
113
  	bool config_enabled;
  	bool config_change_pending;
  	spinlock_t config_lock;
ec3d41c4d   Rusty Russell   Virtio interface
114
115
  	struct device dev;
  	struct virtio_device_id id;
935039323   Stephen Hemminger   virtio: make conf...
116
  	const struct virtio_config_ops *config;
3beee86a4   Sjur Brændeland   virtio: Introduce...
117
  	const struct vringh_config_ops *vringh_config;
9499f5e7e   Rusty Russell   virtio: add names...
118
  	struct list_head vqs;
d02547736   Michael S. Tsirkin   virtio: add suppo...
119
  	u64 features;
ec3d41c4d   Rusty Russell   Virtio interface
120
121
  	void *priv;
  };
9bffdca8c   Wanlong Gao   virtio: use dev_t...
122
123
124
125
  static inline struct virtio_device *dev_to_virtio(struct device *_dev)
  {
  	return container_of(_dev, struct virtio_device, dev);
  }
ec3d41c4d   Rusty Russell   Virtio interface
126
127
  int register_virtio_device(struct virtio_device *dev);
  void unregister_virtio_device(struct virtio_device *dev);
e2dcdfe95   Rusty Russell   virtio: virtio_br...
128
  void virtio_break_device(struct virtio_device *dev);
016c98c6f   Michael S. Tsirkin   virtio: unify con...
129
  void virtio_config_changed(struct virtio_device *dev);
c6716bae5   Michael S. Tsirkin   virtio-pci: move ...
130
131
132
133
  #ifdef CONFIG_PM_SLEEP
  int virtio_device_freeze(struct virtio_device *dev);
  int virtio_device_restore(struct virtio_device *dev);
  #endif
016c98c6f   Michael S. Tsirkin   virtio: unify con...
134

ec3d41c4d   Rusty Russell   Virtio interface
135
136
137
138
  /**
   * virtio_driver - operations for a virtio I/O driver
   * @driver: underlying device driver (populate name and owner).
   * @id_table: the ids serviced by this driver.
5f41f8bfc   Wang Sheng-Hui   virtio.h: correct...
139
   * @feature_table: an array of feature numbers supported by this driver.
c45a6816c   Rusty Russell   virtio: explicit ...
140
   * @feature_table_size: number of entries in the feature table array.
b3bb62d11   Michael S. Tsirkin   virtio: add legac...
141
142
   * @feature_table_legacy: same as feature_table but when working in legacy mode.
   * @feature_table_size_legacy: number of entries in feature table legacy array.
20f77f565   Rusty Russell   virtio: fix obsol...
143
   * @probe: the function to call when a device is found.  Returns 0 or -errno.
5f41f8bfc   Wang Sheng-Hui   virtio.h: correct...
144
   * @remove: the function to call when a device is removed.
f957d1f05   Rusty Russell   virtio: configura...
145
146
   * @config_changed: optional function to call when the device configuration
   *    changes; may be called in interrupt context.
ec3d41c4d   Rusty Russell   Virtio interface
147
148
149
150
   */
  struct virtio_driver {
  	struct device_driver driver;
  	const struct virtio_device_id *id_table;
c45a6816c   Rusty Russell   virtio: explicit ...
151
152
  	const unsigned int *feature_table;
  	unsigned int feature_table_size;
b3bb62d11   Michael S. Tsirkin   virtio: add legac...
153
154
  	const unsigned int *feature_table_legacy;
  	unsigned int feature_table_size_legacy;
ec3d41c4d   Rusty Russell   Virtio interface
155
  	int (*probe)(struct virtio_device *dev);
59057fbc3   Nicholas Bellinger   [SCSI] virtio-scs...
156
  	void (*scan)(struct virtio_device *dev);
ec3d41c4d   Rusty Russell   Virtio interface
157
  	void (*remove)(struct virtio_device *dev);
f957d1f05   Rusty Russell   virtio: configura...
158
  	void (*config_changed)(struct virtio_device *dev);
f0fe6f115   Amit Shah   virtio: pci: add ...
159
160
  #ifdef CONFIG_PM
  	int (*freeze)(struct virtio_device *dev);
f0fe6f115   Amit Shah   virtio: pci: add ...
161
162
  	int (*restore)(struct virtio_device *dev);
  #endif
ec3d41c4d   Rusty Russell   Virtio interface
163
  };
9a2bdcc85   Wanlong Gao   virtio: add drv_t...
164
165
166
167
  static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
  {
  	return container_of(drv, struct virtio_driver, driver);
  }
ec3d41c4d   Rusty Russell   Virtio interface
168
169
  int register_virtio_driver(struct virtio_driver *drv);
  void unregister_virtio_driver(struct virtio_driver *drv);
6e105e059   Sjur Brændeland   virtio: Add modul...
170
171
172
173
174
175
176
177
178
  
  /* module_virtio_driver() - Helper macro for drivers that don't do
   * anything special in module init/exit.  This eliminates a lot of
   * boilerplate.  Each module may only use this macro once, and
   * calling it replaces module_init() and module_exit()
   */
  #define module_virtio_driver(__virtio_driver) \
  	module_driver(__virtio_driver, register_virtio_driver, \
  			unregister_virtio_driver)
ec3d41c4d   Rusty Russell   Virtio interface
179
  #endif /* _LINUX_VIRTIO_H */