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