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