Merge tag 'sound-5.16-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[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>
d8ca2fa5
PP
9#include <linux/virtio_net.h>
10#include <linux/if_ether.h>
961e9c84
JW
11
12/**
d0f9164e 13 * struct vdpa_calllback - vDPA callback definition.
961e9c84
JW
14 * @callback: interrupt callback function
15 * @private: the data passed to the callback function
16 */
17struct vdpa_callback {
18 irqreturn_t (*callback)(void *data);
19 void *private;
20};
21
c25a26e6 22/**
d0f9164e 23 * struct vdpa_notification_area - vDPA notification area
c25a26e6
JW
24 * @addr: base address of the notification area
25 * @size: size of the notification area
26 */
27struct vdpa_notification_area {
28 resource_size_t addr;
29 resource_size_t size;
30};
31
aac50c0b 32/**
530a5678 33 * struct vdpa_vq_state_split - vDPA split virtqueue state
aac50c0b
EC
34 * @avail_index: available index
35 */
530a5678 36struct vdpa_vq_state_split {
aac50c0b
EC
37 u16 avail_index;
38};
39
530a5678
JW
40/**
41 * struct vdpa_vq_state_packed - vDPA packed virtqueue state
42 * @last_avail_counter: last driver ring wrap counter observed by device
43 * @last_avail_idx: device available index
44 * @last_used_counter: device ring wrap counter
45 * @last_used_idx: used index
46 */
47struct vdpa_vq_state_packed {
86e17a51
XY
48 u16 last_avail_counter:1;
49 u16 last_avail_idx:15;
50 u16 last_used_counter:1;
51 u16 last_used_idx:15;
530a5678
JW
52};
53
54struct vdpa_vq_state {
86e17a51
XY
55 union {
56 struct vdpa_vq_state_split split;
57 struct vdpa_vq_state_packed packed;
58 };
530a5678
JW
59};
60
33b34750
PP
61struct vdpa_mgmt_dev;
62
961e9c84 63/**
d0f9164e 64 * struct vdpa_device - representation of a vDPA device
961e9c84
JW
65 * @dev: underlying device
66 * @dma_dev: the actual device that is performing DMA
67 * @config: the configuration ops for this device.
ad69dd0b 68 * @cf_mutex: Protects get and set access to configuration layout.
961e9c84 69 * @index: device index
452639a6 70 * @features_valid: were features initialized? for legacy guests
d8945ec4 71 * @use_va: indicate whether virtual address must be used by this device
476c135e 72 * @nvqs: maximum number of supported virtqueues
903f7bca
PP
73 * @mdev: management device pointer; caller must setup when registering device as part
74 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
961e9c84
JW
75 */
76struct vdpa_device {
77 struct device dev;
78 struct device *dma_dev;
79 const struct vdpa_config_ops *config;
ad69dd0b 80 struct mutex cf_mutex; /* Protects get/set config */
961e9c84 81 unsigned int index;
452639a6 82 bool features_valid;
d8945ec4 83 bool use_va;
a9974489 84 int nvqs;
903f7bca 85 struct vdpa_mgmt_dev *mdev;
961e9c84
JW
86};
87
3f1b623a 88/**
d0f9164e 89 * struct vdpa_iova_range - the IOVA range support by the device
3f1b623a
JW
90 * @first: start of the IOVA range
91 * @last: end of the IOVA range
92 */
93struct vdpa_iova_range {
94 u64 first;
95 u64 last;
96};
97
d8ca2fa5
PP
98struct vdpa_dev_set_config {
99 struct {
100 u8 mac[ETH_ALEN];
101 u16 mtu;
102 } net;
103 u64 mask;
104};
105
d8945ec4
XY
106/**
107 * Corresponding file area for device memory mapping
108 * @file: vma->vm_file for the mapping
109 * @offset: mapping offset in the vm_file
110 */
111struct vdpa_map_file {
112 struct file *file;
113 u64 offset;
114};
115
961e9c84 116/**
d0f9164e 117 * struct vdpa_config_ops - operations for configuring a vDPA device.
961e9c84
JW
118 * Note: vDPA device drivers are required to implement all of the
119 * operations unless it is mentioned to be optional in the following
120 * list.
121 *
122 * @set_vq_address: Set the address of virtqueue
123 * @vdev: vdpa device
124 * @idx: virtqueue index
125 * @desc_area: address of desc area
126 * @driver_area: address of driver area
127 * @device_area: address of device area
128 * Returns integer: success (0) or error (< 0)
129 * @set_vq_num: Set the size of virtqueue
130 * @vdev: vdpa device
131 * @idx: virtqueue index
132 * @num: the size of virtqueue
133 * @kick_vq: Kick the virtqueue
134 * @vdev: vdpa device
135 * @idx: virtqueue index
136 * @set_vq_cb: Set the interrupt callback function for
137 * a virtqueue
138 * @vdev: vdpa device
139 * @idx: virtqueue index
140 * @cb: virtio-vdev interrupt callback structure
141 * @set_vq_ready: Set ready status for a virtqueue
142 * @vdev: vdpa device
143 * @idx: virtqueue index
144 * @ready: ready (true) not ready(false)
145 * @get_vq_ready: Get ready status for a virtqueue
146 * @vdev: vdpa device
147 * @idx: virtqueue index
148 * Returns boolean: ready (true) or not (false)
149 * @set_vq_state: Set the state for a virtqueue
150 * @vdev: vdpa device
151 * @idx: virtqueue index
aac50c0b 152 * @state: pointer to set virtqueue state (last_avail_idx)
961e9c84
JW
153 * Returns integer: success (0) or error (< 0)
154 * @get_vq_state: Get the state for a virtqueue
155 * @vdev: vdpa device
156 * @idx: virtqueue index
aac50c0b 157 * @state: pointer to returned state (last_avail_idx)
86e17a51 158 * @get_vq_notification: Get the notification area for a virtqueue
c25a26e6
JW
159 * @vdev: vdpa device
160 * @idx: virtqueue index
161 * Returns the notifcation area
7164675a
ZL
162 * @get_vq_irq: Get the irq number of a virtqueue (optional,
163 * but must implemented if require vq irq offloading)
164 * @vdev: vdpa device
165 * @idx: virtqueue index
166 * Returns int: irq number of a virtqueue,
167 * negative number if no irq assigned.
961e9c84
JW
168 * @get_vq_align: Get the virtqueue align requirement
169 * for the device
170 * @vdev: vdpa device
171 * Returns virtqueue algin requirement
172 * @get_features: Get virtio features supported by the device
173 * @vdev: vdpa device
174 * Returns the virtio features support by the
175 * device
176 * @set_features: Set virtio features supported by the driver
177 * @vdev: vdpa device
178 * @features: feature support by the driver
179 * Returns integer: success (0) or error (< 0)
180 * @set_config_cb: Set the config interrupt callback
181 * @vdev: vdpa device
182 * @cb: virtio-vdev interrupt callback structure
183 * @get_vq_num_max: Get the max size of virtqueue
184 * @vdev: vdpa device
185 * Returns u16: max size of virtqueue
3b970a58
WZ
186 * @get_vq_num_min: Get the min size of virtqueue (optional)
187 * @vdev: vdpa device
188 * Returns u16: min size of virtqueue
961e9c84
JW
189 * @get_device_id: Get virtio device id
190 * @vdev: vdpa device
191 * Returns u32: virtio device id
192 * @get_vendor_id: Get id for the vendor that provides this device
193 * @vdev: vdpa device
194 * Returns u32: virtio vendor id
195 * @get_status: Get the device status
196 * @vdev: vdpa device
197 * Returns u8: virtio device status
198 * @set_status: Set the device status
199 * @vdev: vdpa device
200 * @status: virtio device status
0686082d
XY
201 * @reset: Reset device
202 * @vdev: vdpa device
203 * Returns integer: success (0) or error (< 0)
442706f9
SG
204 * @get_config_size: Get the size of the configuration space
205 * @vdev: vdpa device
206 * Returns size_t: configuration size
961e9c84
JW
207 * @get_config: Read from device specific configuration space
208 * @vdev: vdpa device
209 * @offset: offset from the beginning of
210 * configuration space
211 * @buf: buffer used to read to
212 * @len: the length to read from
213 * configuration space
214 * @set_config: Write to device specific configuration space
215 * @vdev: vdpa device
216 * @offset: offset from the beginning of
217 * configuration space
218 * @buf: buffer used to write from
219 * @len: the length to write to
220 * configuration space
221 * @get_generation: Get device config generation (optional)
222 * @vdev: vdpa device
223 * Returns u32: device generation
3f1b623a
JW
224 * @get_iova_range: Get supported iova range (optional)
225 * @vdev: vdpa device
226 * Returns the iova range supported by
227 * the device.
961e9c84
JW
228 * @set_map: Set device memory mapping (optional)
229 * Needed for device that using device
230 * specific DMA translation (on-chip IOMMU)
231 * @vdev: vdpa device
232 * @iotlb: vhost memory mapping to be
233 * used by the vDPA
234 * Returns integer: success (0) or error (< 0)
235 * @dma_map: Map an area of PA to IOVA (optional)
236 * Needed for device that using device
237 * specific DMA translation (on-chip IOMMU)
238 * and preferring incremental map.
239 * @vdev: vdpa device
240 * @iova: iova to be mapped
241 * @size: size of the area
242 * @pa: physical address for the map
243 * @perm: device access permission (VHOST_MAP_XX)
244 * Returns integer: success (0) or error (< 0)
245 * @dma_unmap: Unmap an area of IOVA (optional but
246 * must be implemented with dma_map)
247 * Needed for device that using device
248 * specific DMA translation (on-chip IOMMU)
249 * and preferring incremental unmap.
250 * @vdev: vdpa device
251 * @iova: iova to be unmapped
252 * @size: size of the area
253 * Returns integer: success (0) or error (< 0)
254 * @free: Free resources that belongs to vDPA (optional)
255 * @vdev: vdpa device
256 */
257struct vdpa_config_ops {
258 /* Virtqueue ops */
259 int (*set_vq_address)(struct vdpa_device *vdev,
260 u16 idx, u64 desc_area, u64 driver_area,
261 u64 device_area);
262 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
263 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
264 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
265 struct vdpa_callback *cb);
266 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
267 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
aac50c0b
EC
268 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
269 const struct vdpa_vq_state *state);
23750e39
EC
270 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
271 struct vdpa_vq_state *state);
c25a26e6
JW
272 struct vdpa_notification_area
273 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
4c05433b 274 /* vq irq is not expected to be changed once DRIVER_OK is set */
d0ae1fbf 275 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
961e9c84
JW
276
277 /* Device ops */
425a5070 278 u32 (*get_vq_align)(struct vdpa_device *vdev);
961e9c84
JW
279 u64 (*get_features)(struct vdpa_device *vdev);
280 int (*set_features)(struct vdpa_device *vdev, u64 features);
281 void (*set_config_cb)(struct vdpa_device *vdev,
282 struct vdpa_callback *cb);
283 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
3b970a58 284 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
961e9c84
JW
285 u32 (*get_device_id)(struct vdpa_device *vdev);
286 u32 (*get_vendor_id)(struct vdpa_device *vdev);
287 u8 (*get_status)(struct vdpa_device *vdev);
288 void (*set_status)(struct vdpa_device *vdev, u8 status);
0686082d 289 int (*reset)(struct vdpa_device *vdev);
442706f9 290 size_t (*get_config_size)(struct vdpa_device *vdev);
961e9c84
JW
291 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
292 void *buf, unsigned int len);
293 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
294 const void *buf, unsigned int len);
295 u32 (*get_generation)(struct vdpa_device *vdev);
3f1b623a 296 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
961e9c84
JW
297
298 /* DMA ops */
299 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
300 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
c10fb945 301 u64 pa, u32 perm, void *opaque);
961e9c84
JW
302 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
303
304 /* Free device resources */
305 void (*free)(struct vdpa_device *vdev);
306};
307
308struct vdpa_device *__vdpa_alloc_device(struct device *parent,
309 const struct vdpa_config_ops *config,
d8945ec4
XY
310 size_t size, const char *name,
311 bool use_va);
961e9c84 312
c8d182bd
XY
313/**
314 * vdpa_alloc_device - allocate and initilaize a vDPA device
315 *
316 * @dev_struct: the type of the parent structure
317 * @member: the name of struct vdpa_device within the @dev_struct
318 * @parent: the parent device
319 * @config: the bus operations that is supported by this device
320 * @name: name of the vdpa device
d8945ec4 321 * @use_va: indicate whether virtual address must be used by this device
c8d182bd
XY
322 *
323 * Return allocated data structure or ERR_PTR upon error
324 */
d8945ec4 325#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va) \
961e9c84 326 container_of(__vdpa_alloc_device( \
f00bdce0 327 parent, config, \
961e9c84
JW
328 sizeof(dev_struct) + \
329 BUILD_BUG_ON_ZERO(offsetof( \
d8945ec4 330 dev_struct, member)), name, use_va), \
961e9c84
JW
331 dev_struct, member)
332
f00bdce0 333int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
961e9c84
JW
334void vdpa_unregister_device(struct vdpa_device *vdev);
335
f00bdce0 336int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
903f7bca
PP
337void _vdpa_unregister_device(struct vdpa_device *vdev);
338
961e9c84 339/**
d0f9164e 340 * struct vdpa_driver - operations for a vDPA driver
961e9c84
JW
341 * @driver: underlying device driver
342 * @probe: the function to call when a device is found. Returns 0 or -errno.
343 * @remove: the function to call when a device is removed.
344 */
345struct vdpa_driver {
346 struct device_driver driver;
347 int (*probe)(struct vdpa_device *vdev);
348 void (*remove)(struct vdpa_device *vdev);
349};
350
351#define vdpa_register_driver(drv) \
352 __vdpa_register_driver(drv, THIS_MODULE)
353int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
354void vdpa_unregister_driver(struct vdpa_driver *drv);
355
356#define module_vdpa_driver(__vdpa_driver) \
357 module_driver(__vdpa_driver, vdpa_register_driver, \
358 vdpa_unregister_driver)
359
360static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
361{
362 return container_of(driver, struct vdpa_driver, driver);
363}
364
365static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
366{
367 return container_of(_dev, struct vdpa_device, dev);
368}
369
370static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
371{
372 return dev_get_drvdata(&vdev->dev);
373}
374
375static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
376{
377 dev_set_drvdata(&vdev->dev, data);
378}
379
380static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
381{
382 return vdev->dma_dev;
383}
452639a6 384
0686082d 385static inline int vdpa_reset(struct vdpa_device *vdev)
452639a6 386{
86e17a51 387 const struct vdpa_config_ops *ops = vdev->config;
452639a6
MT
388
389 vdev->features_valid = false;
0686082d 390 return ops->reset(vdev);
452639a6
MT
391}
392
393static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
394{
86e17a51 395 const struct vdpa_config_ops *ops = vdev->config;
452639a6
MT
396
397 vdev->features_valid = true;
86e17a51 398 return ops->set_features(vdev, features);
452639a6
MT
399}
400
6dbb1f16
PP
401void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
402 void *buf, unsigned int len);
403void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
404 const void *buf, unsigned int length);
33b34750 405/**
d0f9164e
PP
406 * struct vdpa_mgmtdev_ops - vdpa device ops
407 * @dev_add: Add a vdpa device using alloc and register
408 * @mdev: parent device to use for device addition
409 * @name: name of the new vdpa device
d8ca2fa5 410 * @config: config attributes to apply to the device under creation
d0f9164e
PP
411 * Driver need to add a new device using _vdpa_register_device()
412 * after fully initializing the vdpa device. Driver must return 0
413 * on success or appropriate error code.
414 * @dev_del: Remove a vdpa device using unregister
415 * @mdev: parent device to use for device removal
416 * @dev: vdpa device to remove
417 * Driver need to remove the specified device by calling
418 * _vdpa_unregister_device().
33b34750
PP
419 */
420struct vdpa_mgmtdev_ops {
d8ca2fa5
PP
421 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
422 const struct vdpa_dev_set_config *config);
33b34750
PP
423 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
424};
425
960deb33
PP
426/**
427 * struct vdpa_mgmt_dev - vdpa management device
428 * @device: Management parent device
429 * @ops: operations supported by management device
430 * @id_table: Pointer to device id table of supported ids
d8ca2fa5
PP
431 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
432 * management device support during dev_add callback
960deb33
PP
433 * @list: list entry
434 */
33b34750
PP
435struct vdpa_mgmt_dev {
436 struct device *device;
437 const struct vdpa_mgmtdev_ops *ops;
960deb33 438 const struct virtio_device_id *id_table;
d8ca2fa5 439 u64 config_attr_mask;
33b34750
PP
440 struct list_head list;
441};
442
443int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
444void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
445
961e9c84 446#endif /* _LINUX_VDPA_H */