virtio_pci_modern: Fix the comment of virtio_pci_find_capability()
[linux-block.git] / include / linux / vdpa.h
CommitLineData
961e9c84
JW
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_VDPA_H
3#define _LINUX_VDPA_H
4
5#include <linux/kernel.h>
6#include <linux/device.h>
7#include <linux/interrupt.h>
8#include <linux/vhost_iotlb.h>
9
10/**
11 * vDPA callback definition.
12 * @callback: interrupt callback function
13 * @private: the data passed to the callback function
14 */
15struct vdpa_callback {
16 irqreturn_t (*callback)(void *data);
17 void *private;
18};
19
c25a26e6
JW
20/**
21 * vDPA notification area
22 * @addr: base address of the notification area
23 * @size: size of the notification area
24 */
25struct vdpa_notification_area {
26 resource_size_t addr;
27 resource_size_t size;
28};
29
961e9c84
JW
30/**
31 * vDPA device - representation of a vDPA device
32 * @dev: underlying device
33 * @dma_dev: the actual device that is performing DMA
34 * @config: the configuration ops for this device.
35 * @index: device index
452639a6 36 * @features_valid: were features initialized? for legacy guests
961e9c84
JW
37 */
38struct vdpa_device {
39 struct device dev;
40 struct device *dma_dev;
41 const struct vdpa_config_ops *config;
42 unsigned int index;
452639a6 43 bool features_valid;
961e9c84
JW
44};
45
46/**
47 * vDPA_config_ops - operations for configuring a vDPA device.
48 * Note: vDPA device drivers are required to implement all of the
49 * operations unless it is mentioned to be optional in the following
50 * list.
51 *
52 * @set_vq_address: Set the address of virtqueue
53 * @vdev: vdpa device
54 * @idx: virtqueue index
55 * @desc_area: address of desc area
56 * @driver_area: address of driver area
57 * @device_area: address of device area
58 * Returns integer: success (0) or error (< 0)
59 * @set_vq_num: Set the size of virtqueue
60 * @vdev: vdpa device
61 * @idx: virtqueue index
62 * @num: the size of virtqueue
63 * @kick_vq: Kick the virtqueue
64 * @vdev: vdpa device
65 * @idx: virtqueue index
66 * @set_vq_cb: Set the interrupt callback function for
67 * a virtqueue
68 * @vdev: vdpa device
69 * @idx: virtqueue index
70 * @cb: virtio-vdev interrupt callback structure
71 * @set_vq_ready: Set ready status for a virtqueue
72 * @vdev: vdpa device
73 * @idx: virtqueue index
74 * @ready: ready (true) not ready(false)
75 * @get_vq_ready: Get ready status for a virtqueue
76 * @vdev: vdpa device
77 * @idx: virtqueue index
78 * Returns boolean: ready (true) or not (false)
79 * @set_vq_state: Set the state for a virtqueue
80 * @vdev: vdpa device
81 * @idx: virtqueue index
82 * @state: virtqueue state (last_avail_idx)
83 * Returns integer: success (0) or error (< 0)
84 * @get_vq_state: Get the state for a virtqueue
85 * @vdev: vdpa device
86 * @idx: virtqueue index
87 * Returns virtqueue state (last_avail_idx)
c25a26e6
JW
88 * @get_vq_notification: Get the notification area for a virtqueue
89 * @vdev: vdpa device
90 * @idx: virtqueue index
91 * Returns the notifcation area
7164675a
ZL
92 * @get_vq_irq: Get the irq number of a virtqueue (optional,
93 * but must implemented if require vq irq offloading)
94 * @vdev: vdpa device
95 * @idx: virtqueue index
96 * Returns int: irq number of a virtqueue,
97 * negative number if no irq assigned.
961e9c84
JW
98 * @get_vq_align: Get the virtqueue align requirement
99 * for the device
100 * @vdev: vdpa device
101 * Returns virtqueue algin requirement
102 * @get_features: Get virtio features supported by the device
103 * @vdev: vdpa device
104 * Returns the virtio features support by the
105 * device
106 * @set_features: Set virtio features supported by the driver
107 * @vdev: vdpa device
108 * @features: feature support by the driver
109 * Returns integer: success (0) or error (< 0)
110 * @set_config_cb: Set the config interrupt callback
111 * @vdev: vdpa device
112 * @cb: virtio-vdev interrupt callback structure
113 * @get_vq_num_max: Get the max size of virtqueue
114 * @vdev: vdpa device
115 * Returns u16: max size of virtqueue
116 * @get_device_id: Get virtio device id
117 * @vdev: vdpa device
118 * Returns u32: virtio device id
119 * @get_vendor_id: Get id for the vendor that provides this device
120 * @vdev: vdpa device
121 * Returns u32: virtio vendor id
122 * @get_status: Get the device status
123 * @vdev: vdpa device
124 * Returns u8: virtio device status
125 * @set_status: Set the device status
126 * @vdev: vdpa device
127 * @status: virtio device status
128 * @get_config: Read from device specific configuration space
129 * @vdev: vdpa device
130 * @offset: offset from the beginning of
131 * configuration space
132 * @buf: buffer used to read to
133 * @len: the length to read from
134 * configuration space
135 * @set_config: Write to device specific configuration space
136 * @vdev: vdpa device
137 * @offset: offset from the beginning of
138 * configuration space
139 * @buf: buffer used to write from
140 * @len: the length to write to
141 * configuration space
142 * @get_generation: Get device config generation (optional)
143 * @vdev: vdpa device
144 * Returns u32: device generation
145 * @set_map: Set device memory mapping (optional)
146 * Needed for device that using device
147 * specific DMA translation (on-chip IOMMU)
148 * @vdev: vdpa device
149 * @iotlb: vhost memory mapping to be
150 * used by the vDPA
151 * Returns integer: success (0) or error (< 0)
152 * @dma_map: Map an area of PA to IOVA (optional)
153 * Needed for device that using device
154 * specific DMA translation (on-chip IOMMU)
155 * and preferring incremental map.
156 * @vdev: vdpa device
157 * @iova: iova to be mapped
158 * @size: size of the area
159 * @pa: physical address for the map
160 * @perm: device access permission (VHOST_MAP_XX)
161 * Returns integer: success (0) or error (< 0)
162 * @dma_unmap: Unmap an area of IOVA (optional but
163 * must be implemented with dma_map)
164 * Needed for device that using device
165 * specific DMA translation (on-chip IOMMU)
166 * and preferring incremental unmap.
167 * @vdev: vdpa device
168 * @iova: iova to be unmapped
169 * @size: size of the area
170 * Returns integer: success (0) or error (< 0)
171 * @free: Free resources that belongs to vDPA (optional)
172 * @vdev: vdpa device
173 */
174struct vdpa_config_ops {
175 /* Virtqueue ops */
176 int (*set_vq_address)(struct vdpa_device *vdev,
177 u16 idx, u64 desc_area, u64 driver_area,
178 u64 device_area);
179 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
180 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
181 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
182 struct vdpa_callback *cb);
183 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
184 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
185 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, u64 state);
186 u64 (*get_vq_state)(struct vdpa_device *vdev, u16 idx);
c25a26e6
JW
187 struct vdpa_notification_area
188 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
7164675a 189 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
961e9c84
JW
190
191 /* Device ops */
425a5070 192 u32 (*get_vq_align)(struct vdpa_device *vdev);
961e9c84
JW
193 u64 (*get_features)(struct vdpa_device *vdev);
194 int (*set_features)(struct vdpa_device *vdev, u64 features);
195 void (*set_config_cb)(struct vdpa_device *vdev,
196 struct vdpa_callback *cb);
197 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
198 u32 (*get_device_id)(struct vdpa_device *vdev);
199 u32 (*get_vendor_id)(struct vdpa_device *vdev);
200 u8 (*get_status)(struct vdpa_device *vdev);
201 void (*set_status)(struct vdpa_device *vdev, u8 status);
202 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
203 void *buf, unsigned int len);
204 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
205 const void *buf, unsigned int len);
206 u32 (*get_generation)(struct vdpa_device *vdev);
207
208 /* DMA ops */
209 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
210 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
211 u64 pa, u32 perm);
212 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
213
214 /* Free device resources */
215 void (*free)(struct vdpa_device *vdev);
216};
217
218struct vdpa_device *__vdpa_alloc_device(struct device *parent,
219 const struct vdpa_config_ops *config,
220 size_t size);
221
222#define vdpa_alloc_device(dev_struct, member, parent, config) \
223 container_of(__vdpa_alloc_device( \
224 parent, config, \
225 sizeof(dev_struct) + \
226 BUILD_BUG_ON_ZERO(offsetof( \
227 dev_struct, member))), \
228 dev_struct, member)
229
230int vdpa_register_device(struct vdpa_device *vdev);
231void vdpa_unregister_device(struct vdpa_device *vdev);
232
233/**
234 * vdpa_driver - operations for a vDPA driver
235 * @driver: underlying device driver
236 * @probe: the function to call when a device is found. Returns 0 or -errno.
237 * @remove: the function to call when a device is removed.
238 */
239struct vdpa_driver {
240 struct device_driver driver;
241 int (*probe)(struct vdpa_device *vdev);
242 void (*remove)(struct vdpa_device *vdev);
243};
244
245#define vdpa_register_driver(drv) \
246 __vdpa_register_driver(drv, THIS_MODULE)
247int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
248void vdpa_unregister_driver(struct vdpa_driver *drv);
249
250#define module_vdpa_driver(__vdpa_driver) \
251 module_driver(__vdpa_driver, vdpa_register_driver, \
252 vdpa_unregister_driver)
253
254static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
255{
256 return container_of(driver, struct vdpa_driver, driver);
257}
258
259static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
260{
261 return container_of(_dev, struct vdpa_device, dev);
262}
263
264static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
265{
266 return dev_get_drvdata(&vdev->dev);
267}
268
269static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
270{
271 dev_set_drvdata(&vdev->dev, data);
272}
273
274static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
275{
276 return vdev->dma_dev;
277}
452639a6
MT
278
279static inline void vdpa_reset(struct vdpa_device *vdev)
280{
281 const struct vdpa_config_ops *ops = vdev->config;
282
283 vdev->features_valid = false;
284 ops->set_status(vdev, 0);
285}
286
287static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
288{
289 const struct vdpa_config_ops *ops = vdev->config;
290
291 vdev->features_valid = true;
292 return ops->set_features(vdev, features);
293}
294
295
296static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
297 void *buf, unsigned int len)
298{
299 const struct vdpa_config_ops *ops = vdev->config;
300
301 /*
302 * Config accesses aren't supposed to trigger before features are set.
303 * If it does happen we assume a legacy guest.
304 */
305 if (!vdev->features_valid)
306 vdpa_set_features(vdev, 0);
307 ops->get_config(vdev, offset, buf, len);
308}
309
961e9c84 310#endif /* _LINUX_VDPA_H */