xref: /linux-6.15/include/linux/vdpa.h (revision 0a926fc9)
1961e9c84SJason Wang /* SPDX-License-Identifier: GPL-2.0 */
2961e9c84SJason Wang #ifndef _LINUX_VDPA_H
3961e9c84SJason Wang #define _LINUX_VDPA_H
4961e9c84SJason Wang 
5961e9c84SJason Wang #include <linux/kernel.h>
6961e9c84SJason Wang #include <linux/device.h>
7961e9c84SJason Wang #include <linux/interrupt.h>
8961e9c84SJason Wang #include <linux/vhost_iotlb.h>
9d8ca2fa5SParav Pandit #include <linux/virtio_net.h>
10d8ca2fa5SParav Pandit #include <linux/if_ether.h>
11961e9c84SJason Wang 
12961e9c84SJason Wang /**
139be5d2d4SSimon Horman  * struct vdpa_callback - vDPA callback definition.
14961e9c84SJason Wang  * @callback: interrupt callback function
15961e9c84SJason Wang  * @private: the data passed to the callback function
165e68470fSXie Yongji  * @trigger: the eventfd for the callback (Optional).
175e68470fSXie Yongji  *           When it is set, the vDPA driver must guarantee that
185e68470fSXie Yongji  *           signaling it is functional equivalent to triggering
195e68470fSXie Yongji  *           the callback. Then vDPA parent can signal it directly
205e68470fSXie Yongji  *           instead of triggering the callback.
21961e9c84SJason Wang  */
22961e9c84SJason Wang struct vdpa_callback {
23961e9c84SJason Wang 	irqreturn_t (*callback)(void *data);
24961e9c84SJason Wang 	void *private;
255e68470fSXie Yongji 	struct eventfd_ctx *trigger;
26961e9c84SJason Wang };
27961e9c84SJason Wang 
28961e9c84SJason Wang /**
29d0f9164eSParav Pandit  * struct vdpa_notification_area - vDPA notification area
30c25a26e6SJason Wang  * @addr: base address of the notification area
31c25a26e6SJason Wang  * @size: size of the notification area
32c25a26e6SJason Wang  */
33c25a26e6SJason Wang struct vdpa_notification_area {
34c25a26e6SJason Wang 	resource_size_t addr;
35c25a26e6SJason Wang 	resource_size_t size;
36c25a26e6SJason Wang };
37c25a26e6SJason Wang 
38c25a26e6SJason Wang /**
39530a5678SJason Wang  * struct vdpa_vq_state_split - vDPA split virtqueue state
40aac50c0bSEli Cohen  * @avail_index: available index
41aac50c0bSEli Cohen  */
42530a5678SJason Wang struct vdpa_vq_state_split {
43aac50c0bSEli Cohen 	u16	avail_index;
44aac50c0bSEli Cohen };
45aac50c0bSEli Cohen 
46530a5678SJason Wang /**
47530a5678SJason Wang  * struct vdpa_vq_state_packed - vDPA packed virtqueue state
48530a5678SJason Wang  * @last_avail_counter: last driver ring wrap counter observed by device
49530a5678SJason Wang  * @last_avail_idx: device available index
50530a5678SJason Wang  * @last_used_counter: device ring wrap counter
51530a5678SJason Wang  * @last_used_idx: used index
52530a5678SJason Wang  */
53530a5678SJason Wang struct vdpa_vq_state_packed {
54530a5678SJason Wang 	u16	last_avail_counter:1;
55530a5678SJason Wang 	u16	last_avail_idx:15;
56530a5678SJason Wang 	u16	last_used_counter:1;
57530a5678SJason Wang 	u16	last_used_idx:15;
58530a5678SJason Wang };
59530a5678SJason Wang 
60530a5678SJason Wang struct vdpa_vq_state {
61530a5678SJason Wang 	union {
62530a5678SJason Wang 		struct vdpa_vq_state_split split;
63530a5678SJason Wang 		struct vdpa_vq_state_packed packed;
64530a5678SJason Wang 	};
65530a5678SJason Wang };
66530a5678SJason Wang 
6733b34750SParav Pandit struct vdpa_mgmt_dev;
6833b34750SParav Pandit 
69aac50c0bSEli Cohen /**
70d0f9164eSParav Pandit  * struct vdpa_device - representation of a vDPA device
71961e9c84SJason Wang  * @dev: underlying device
72961e9c84SJason Wang  * @dma_dev: the actual device that is performing DMA
73240bf4e6SKrzysztof Kozlowski  * @driver_override: driver name to force a match; do not set directly,
74240bf4e6SKrzysztof Kozlowski  *                   because core frees it; use driver_set_override() to
75240bf4e6SKrzysztof Kozlowski  *                   set or clear it.
76961e9c84SJason Wang  * @config: the configuration ops for this device.
77a6a51adcSEli Cohen  * @cf_lock: Protects get and set access to configuration layout.
78961e9c84SJason Wang  * @index: device index
79452639a6SMichael S. Tsirkin  * @features_valid: were features initialized? for legacy guests
80db9adcbfSGautam Dawar  * @ngroups: the number of virtqueue groups
81db9adcbfSGautam Dawar  * @nas: the number of address spaces
82d8945ec4SXie Yongji  * @use_va: indicate whether virtual address must be used by this device
83476c135eSParav Pandit  * @nvqs: maximum number of supported virtqueues
84903f7bcaSParav Pandit  * @mdev: management device pointer; caller must setup when registering device as part
85903f7bcaSParav Pandit  *	  of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
86961e9c84SJason Wang  */
87961e9c84SJason Wang struct vdpa_device {
88961e9c84SJason Wang 	struct device dev;
89961e9c84SJason Wang 	struct device *dma_dev;
90539fec78SStefano Garzarella 	const char *driver_override;
91961e9c84SJason Wang 	const struct vdpa_config_ops *config;
92a6a51adcSEli Cohen 	struct rw_semaphore cf_lock; /* Protects get/set config */
93961e9c84SJason Wang 	unsigned int index;
94452639a6SMichael S. Tsirkin 	bool features_valid;
95d8945ec4SXie Yongji 	bool use_va;
9681d46d69SLongpeng 	u32 nvqs;
97903f7bcaSParav Pandit 	struct vdpa_mgmt_dev *mdev;
98d4821902SGautam Dawar 	unsigned int ngroups;
99db9adcbfSGautam Dawar 	unsigned int nas;
100961e9c84SJason Wang };
101961e9c84SJason Wang 
102961e9c84SJason Wang /**
103d0f9164eSParav Pandit  * struct vdpa_iova_range - the IOVA range support by the device
1043f1b623aSJason Wang  * @first: start of the IOVA range
1053f1b623aSJason Wang  * @last: end of the IOVA range
1063f1b623aSJason Wang  */
1073f1b623aSJason Wang struct vdpa_iova_range {
1083f1b623aSJason Wang 	u64 first;
1093f1b623aSJason Wang 	u64 last;
1103f1b623aSJason Wang };
1113f1b623aSJason Wang 
112d8ca2fa5SParav Pandit struct vdpa_dev_set_config {
11390fea5a8SJason Wang 	u64 device_features;
114d8ca2fa5SParav Pandit 	struct {
115d8ca2fa5SParav Pandit 		u8 mac[ETH_ALEN];
116d8ca2fa5SParav Pandit 		u16 mtu;
117aba21affSEli Cohen 		u16 max_vq_pairs;
118d8ca2fa5SParav Pandit 	} net;
119d8ca2fa5SParav Pandit 	u64 mask;
120d8ca2fa5SParav Pandit };
121d8ca2fa5SParav Pandit 
1223f1b623aSJason Wang /**
1239be5d2d4SSimon Horman  * struct vdpa_map_file - file area for device memory mapping
124d8945ec4SXie Yongji  * @file: vma->vm_file for the mapping
125d8945ec4SXie Yongji  * @offset: mapping offset in the vm_file
126d8945ec4SXie Yongji  */
127d8945ec4SXie Yongji struct vdpa_map_file {
128d8945ec4SXie Yongji 	struct file *file;
129d8945ec4SXie Yongji 	u64 offset;
130d8945ec4SXie Yongji };
131d8945ec4SXie Yongji 
132d8945ec4SXie Yongji /**
133d0f9164eSParav Pandit  * struct vdpa_config_ops - operations for configuring a vDPA device.
134961e9c84SJason Wang  * Note: vDPA device drivers are required to implement all of the
135961e9c84SJason Wang  * operations unless it is mentioned to be optional in the following
136961e9c84SJason Wang  * list.
137961e9c84SJason Wang  *
138961e9c84SJason Wang  * @set_vq_address:		Set the address of virtqueue
139961e9c84SJason Wang  *				@vdev: vdpa device
140961e9c84SJason Wang  *				@idx: virtqueue index
141961e9c84SJason Wang  *				@desc_area: address of desc area
142961e9c84SJason Wang  *				@driver_area: address of driver area
143961e9c84SJason Wang  *				@device_area: address of device area
144961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
145961e9c84SJason Wang  * @set_vq_num:			Set the size of virtqueue
146961e9c84SJason Wang  *				@vdev: vdpa device
147961e9c84SJason Wang  *				@idx: virtqueue index
148961e9c84SJason Wang  *				@num: the size of virtqueue
149961e9c84SJason Wang  * @kick_vq:			Kick the virtqueue
150961e9c84SJason Wang  *				@vdev: vdpa device
151961e9c84SJason Wang  *				@idx: virtqueue index
1522c4e4a22SAlvaro Karsz  * @kick_vq_with_data:		Kick the virtqueue and supply extra data
1532c4e4a22SAlvaro Karsz  *				(only if VIRTIO_F_NOTIFICATION_DATA is negotiated)
1542c4e4a22SAlvaro Karsz  *				@vdev: vdpa device
1552c4e4a22SAlvaro Karsz  *				@data for split virtqueue:
1562c4e4a22SAlvaro Karsz  *				16 bits vqn and 16 bits next available index.
1572c4e4a22SAlvaro Karsz  *				@data for packed virtqueue:
1582c4e4a22SAlvaro Karsz  *				16 bits vqn, 15 least significant bits of
1592c4e4a22SAlvaro Karsz  *				next available index and 1 bit next_wrap.
160961e9c84SJason Wang  * @set_vq_cb:			Set the interrupt callback function for
161961e9c84SJason Wang  *				a virtqueue
162961e9c84SJason Wang  *				@vdev: vdpa device
163961e9c84SJason Wang  *				@idx: virtqueue index
164961e9c84SJason Wang  *				@cb: virtio-vdev interrupt callback structure
165961e9c84SJason Wang  * @set_vq_ready:		Set ready status for a virtqueue
166961e9c84SJason Wang  *				@vdev: vdpa device
167961e9c84SJason Wang  *				@idx: virtqueue index
168961e9c84SJason Wang  *				@ready: ready (true) not ready(false)
169961e9c84SJason Wang  * @get_vq_ready:		Get ready status for a virtqueue
170961e9c84SJason Wang  *				@vdev: vdpa device
171961e9c84SJason Wang  *				@idx: virtqueue index
172961e9c84SJason Wang  *				Returns boolean: ready (true) or not (false)
173961e9c84SJason Wang  * @set_vq_state:		Set the state for a virtqueue
174961e9c84SJason Wang  *				@vdev: vdpa device
175961e9c84SJason Wang  *				@idx: virtqueue index
176aac50c0bSEli Cohen  *				@state: pointer to set virtqueue state (last_avail_idx)
177961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
178961e9c84SJason Wang  * @get_vq_state:		Get the state for a virtqueue
179961e9c84SJason Wang  *				@vdev: vdpa device
180961e9c84SJason Wang  *				@idx: virtqueue index
181aac50c0bSEli Cohen  *				@state: pointer to returned state (last_avail_idx)
1829be5d2d4SSimon Horman  * @get_vendor_vq_stats:	Get the vendor statistics of a device.
1839be5d2d4SSimon Horman  *				@vdev: vdpa device
1849be5d2d4SSimon Horman  *				@idx: virtqueue index
1859be5d2d4SSimon Horman  *				@msg: socket buffer holding stats message
1869be5d2d4SSimon Horman  *				@extack: extack for reporting error messages
1879be5d2d4SSimon Horman  *				Returns integer: success (0) or error (< 0)
18828cc408bSEugenio Pérez  * @get_vq_notification:	Get the notification area for a virtqueue (optional)
189c25a26e6SJason Wang  *				@vdev: vdpa device
190c25a26e6SJason Wang  *				@idx: virtqueue index
1919be5d2d4SSimon Horman  *				Returns the notification area
1927164675aSZhu Lingshan  * @get_vq_irq:			Get the irq number of a virtqueue (optional,
1937164675aSZhu Lingshan  *				but must implemented if require vq irq offloading)
1947164675aSZhu Lingshan  *				@vdev: vdpa device
1957164675aSZhu Lingshan  *				@idx: virtqueue index
1967164675aSZhu Lingshan  *				Returns int: irq number of a virtqueue,
1977164675aSZhu Lingshan  *				negative number if no irq assigned.
198*0a926fc9SZhu Lingshan  * @get_vq_size:		Get the size of a specific virtqueue (optional)
199*0a926fc9SZhu Lingshan  *				@vdev: vdpa device
200*0a926fc9SZhu Lingshan  *				@idx: virtqueue index
201*0a926fc9SZhu Lingshan  *				Return u16: the size of the virtqueue
202961e9c84SJason Wang  * @get_vq_align:		Get the virtqueue align requirement
203961e9c84SJason Wang  *				for the device
204961e9c84SJason Wang  *				@vdev: vdpa device
205961e9c84SJason Wang  *				Returns virtqueue algin requirement
20600d1f546SJason Wang  * @get_vq_group:		Get the group id for a specific
20700d1f546SJason Wang  *				virtqueue (optional)
208d4821902SGautam Dawar  *				@vdev: vdpa device
209d4821902SGautam Dawar  *				@idx: virtqueue index
210d4821902SGautam Dawar  *				Returns u32: group id for this virtqueue
211a72cac60SSi-Wei Liu  * @get_vq_desc_group:		Get the group id for the descriptor table of
212a72cac60SSi-Wei Liu  *				a specific virtqueue (optional)
213a72cac60SSi-Wei Liu  *				@vdev: vdpa device
214a72cac60SSi-Wei Liu  *				@idx: virtqueue index
215a72cac60SSi-Wei Liu  *				Returns u32: group id for the descriptor table
216a72cac60SSi-Wei Liu  *				portion of this virtqueue. Could be different
217a72cac60SSi-Wei Liu  *				than the one from @get_vq_group, in which case
218a72cac60SSi-Wei Liu  *				the access to the descriptor table can be
219a72cac60SSi-Wei Liu  *				confined to a separate asid, isolating from
220a72cac60SSi-Wei Liu  *				the virtqueue's buffer address access.
221a64917bcSEli Cohen  * @get_device_features:	Get virtio features supported by the device
222961e9c84SJason Wang  *				@vdev: vdpa device
223961e9c84SJason Wang  *				Returns the virtio features support by the
224961e9c84SJason Wang  *				device
225b63e5c70SEugenio Pérez  * @get_backend_features:	Get parent-specific backend features (optional)
226b63e5c70SEugenio Pérez  *				Returns the vdpa features supported by the
227b63e5c70SEugenio Pérez  *				device.
228a64917bcSEli Cohen  * @set_driver_features:	Set virtio features supported by the driver
229961e9c84SJason Wang  *				@vdev: vdpa device
230961e9c84SJason Wang  *				@features: feature support by the driver
231961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
232a64917bcSEli Cohen  * @get_driver_features:	Get the virtio driver features in action
233a64917bcSEli Cohen  *				@vdev: vdpa device
234a64917bcSEli Cohen  *				Returns the virtio features accepted
235961e9c84SJason Wang  * @set_config_cb:		Set the config interrupt callback
236961e9c84SJason Wang  *				@vdev: vdpa device
237961e9c84SJason Wang  *				@cb: virtio-vdev interrupt callback structure
238961e9c84SJason Wang  * @get_vq_num_max:		Get the max size of virtqueue
239961e9c84SJason Wang  *				@vdev: vdpa device
240961e9c84SJason Wang  *				Returns u16: max size of virtqueue
2413b970a58SWu Zongyong  * @get_vq_num_min:		Get the min size of virtqueue (optional)
2423b970a58SWu Zongyong  *				@vdev: vdpa device
2433b970a58SWu Zongyong  *				Returns u16: min size of virtqueue
244961e9c84SJason Wang  * @get_device_id:		Get virtio device id
245961e9c84SJason Wang  *				@vdev: vdpa device
246961e9c84SJason Wang  *				Returns u32: virtio device id
247961e9c84SJason Wang  * @get_vendor_id:		Get id for the vendor that provides this device
248961e9c84SJason Wang  *				@vdev: vdpa device
249961e9c84SJason Wang  *				Returns u32: virtio vendor id
250961e9c84SJason Wang  * @get_status:			Get the device status
251961e9c84SJason Wang  *				@vdev: vdpa device
252961e9c84SJason Wang  *				Returns u8: virtio device status
253961e9c84SJason Wang  * @set_status:			Set the device status
254961e9c84SJason Wang  *				@vdev: vdpa device
255961e9c84SJason Wang  *				@status: virtio device status
2560686082dSXie Yongji  * @reset:			Reset device
2570686082dSXie Yongji  *				@vdev: vdpa device
2580686082dSXie Yongji  *				Returns integer: success (0) or error (< 0)
259a26f2e4eSSi-Wei Liu  * @compat_reset:		Reset device with compatibility quirks to
260a26f2e4eSSi-Wei Liu  *				accommodate older userspace. Only needed by
261a26f2e4eSSi-Wei Liu  *				parent driver which used to have bogus reset
262a26f2e4eSSi-Wei Liu  *				behaviour, and has to maintain such behaviour
263a26f2e4eSSi-Wei Liu  *				for compatibility with older userspace.
264a26f2e4eSSi-Wei Liu  *				Historically compliant driver only has to
265a26f2e4eSSi-Wei Liu  *				implement .reset, Historically non-compliant
266a26f2e4eSSi-Wei Liu  *				driver should implement both.
267a26f2e4eSSi-Wei Liu  *				@vdev: vdpa device
268a26f2e4eSSi-Wei Liu  *				@flags: compatibility quirks for reset
269a26f2e4eSSi-Wei Liu  *				Returns integer: success (0) or error (< 0)
2701538a8a4SSebastien Boeuf  * @suspend:			Suspend the device (optional)
2711538a8a4SSebastien Boeuf  *				@vdev: vdpa device
2721538a8a4SSebastien Boeuf  *				Returns integer: success (0) or error (< 0)
2731538a8a4SSebastien Boeuf  * @resume:			Resume the device (optional)
274848ecea1SEugenio Pérez  *				@vdev: vdpa device
275848ecea1SEugenio Pérez  *				Returns integer: success (0) or error (< 0)
276a61280ddSLongpeng  * @get_config_size:		Get the size of the configuration space includes
277a61280ddSLongpeng  *				fields that are conditional on feature bits.
278442706f9SStefano Garzarella  *				@vdev: vdpa device
279442706f9SStefano Garzarella  *				Returns size_t: configuration size
280961e9c84SJason Wang  * @get_config:			Read from device specific configuration space
281961e9c84SJason Wang  *				@vdev: vdpa device
282961e9c84SJason Wang  *				@offset: offset from the beginning of
283961e9c84SJason Wang  *				configuration space
284961e9c84SJason Wang  *				@buf: buffer used to read to
285961e9c84SJason Wang  *				@len: the length to read from
286961e9c84SJason Wang  *				configuration space
287961e9c84SJason Wang  * @set_config:			Write to device specific configuration space
288961e9c84SJason Wang  *				@vdev: vdpa device
289961e9c84SJason Wang  *				@offset: offset from the beginning of
290961e9c84SJason Wang  *				configuration space
291961e9c84SJason Wang  *				@buf: buffer used to write from
292961e9c84SJason Wang  *				@len: the length to write to
293961e9c84SJason Wang  *				configuration space
294961e9c84SJason Wang  * @get_generation:		Get device config generation (optional)
295961e9c84SJason Wang  *				@vdev: vdpa device
296961e9c84SJason Wang  *				Returns u32: device generation
2973f1b623aSJason Wang  * @get_iova_range:		Get supported iova range (optional)
2983f1b623aSJason Wang  *				@vdev: vdpa device
2993f1b623aSJason Wang  *				Returns the iova range supported by
3003f1b623aSJason Wang  *				the device.
3011d246927SXie Yongji  * @set_vq_affinity:		Set the affinity of virtqueue (optional)
3021d246927SXie Yongji  *				@vdev: vdpa device
3031d246927SXie Yongji  *				@idx: virtqueue index
3041d246927SXie Yongji  *				@cpu_mask: the affinity mask
3051d246927SXie Yongji  *				Returns integer: success (0) or error (< 0)
3061d246927SXie Yongji  * @get_vq_affinity:		Get the affinity of virtqueue (optional)
3071d246927SXie Yongji  *				@vdev: vdpa device
3081d246927SXie Yongji  *				@idx: virtqueue index
3091d246927SXie Yongji  *				Returns the affinity mask
31046d554b1SGautam Dawar  * @set_group_asid:		Set address space identifier for a
31100d1f546SJason Wang  *				virtqueue group (optional)
31246d554b1SGautam Dawar  *				@vdev: vdpa device
31346d554b1SGautam Dawar  *				@group: virtqueue group
31446d554b1SGautam Dawar  *				@asid: address space id for this group
31546d554b1SGautam Dawar  *				Returns integer: success (0) or error (< 0)
316961e9c84SJason Wang  * @set_map:			Set device memory mapping (optional)
317961e9c84SJason Wang  *				Needed for device that using device
318961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
319961e9c84SJason Wang  *				@vdev: vdpa device
320db9adcbfSGautam Dawar  *				@asid: address space identifier
321961e9c84SJason Wang  *				@iotlb: vhost memory mapping to be
322961e9c84SJason Wang  *				used by the vDPA
323961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
324961e9c84SJason Wang  * @dma_map:			Map an area of PA to IOVA (optional)
325961e9c84SJason Wang  *				Needed for device that using device
326961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
327961e9c84SJason Wang  *				and preferring incremental map.
328961e9c84SJason Wang  *				@vdev: vdpa device
329db9adcbfSGautam Dawar  *				@asid: address space identifier
330961e9c84SJason Wang  *				@iova: iova to be mapped
331961e9c84SJason Wang  *				@size: size of the area
332961e9c84SJason Wang  *				@pa: physical address for the map
333961e9c84SJason Wang  *				@perm: device access permission (VHOST_MAP_XX)
334961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
335961e9c84SJason Wang  * @dma_unmap:			Unmap an area of IOVA (optional but
336961e9c84SJason Wang  *				must be implemented with dma_map)
337961e9c84SJason Wang  *				Needed for device that using device
338961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
339961e9c84SJason Wang  *				and preferring incremental unmap.
340961e9c84SJason Wang  *				@vdev: vdpa device
341db9adcbfSGautam Dawar  *				@asid: address space identifier
342961e9c84SJason Wang  *				@iova: iova to be unmapped
343961e9c84SJason Wang  *				@size: size of the area
344961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
345d2cf1b6eSSi-Wei Liu  * @reset_map:			Reset device memory mapping to the default
346d2cf1b6eSSi-Wei Liu  *				state (optional)
347d2cf1b6eSSi-Wei Liu  *				Needed for devices that are using device
348d2cf1b6eSSi-Wei Liu  *				specific DMA translation and prefer mapping
349d2cf1b6eSSi-Wei Liu  *				to be decoupled from the virtio life cycle,
350d2cf1b6eSSi-Wei Liu  *				i.e. device .reset op does not reset mapping
351d2cf1b6eSSi-Wei Liu  *				@vdev: vdpa device
352d2cf1b6eSSi-Wei Liu  *				@asid: address space identifier
353d2cf1b6eSSi-Wei Liu  *				Returns integer: success (0) or error (< 0)
35425da258fSJason Wang  * @get_vq_dma_dev:		Get the dma device for a specific
35525da258fSJason Wang  *				virtqueue (optional)
35625da258fSJason Wang  *				@vdev: vdpa device
35725da258fSJason Wang  *				@idx: virtqueue index
35825da258fSJason Wang  *				Returns pointer to structure device or error (NULL)
359c618c84dSStefano Garzarella  * @bind_mm:			Bind the device to a specific address space
360c618c84dSStefano Garzarella  *				so the vDPA framework can use VA when this
361c618c84dSStefano Garzarella  *				callback is implemented. (optional)
362c618c84dSStefano Garzarella  *				@vdev: vdpa device
363c618c84dSStefano Garzarella  *				@mm: address space to bind
364c618c84dSStefano Garzarella  * @unbind_mm:			Unbind the device from the address space
365c618c84dSStefano Garzarella  *				bound using the bind_mm callback. (optional)
366c618c84dSStefano Garzarella  *				@vdev: vdpa device
367961e9c84SJason Wang  * @free:			Free resources that belongs to vDPA (optional)
368961e9c84SJason Wang  *				@vdev: vdpa device
369961e9c84SJason Wang  */
370961e9c84SJason Wang struct vdpa_config_ops {
371961e9c84SJason Wang 	/* Virtqueue ops */
372961e9c84SJason Wang 	int (*set_vq_address)(struct vdpa_device *vdev,
373961e9c84SJason Wang 			      u16 idx, u64 desc_area, u64 driver_area,
374961e9c84SJason Wang 			      u64 device_area);
375961e9c84SJason Wang 	void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
376961e9c84SJason Wang 	void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
3772c4e4a22SAlvaro Karsz 	void (*kick_vq_with_data)(struct vdpa_device *vdev, u32 data);
378961e9c84SJason Wang 	void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
379961e9c84SJason Wang 			  struct vdpa_callback *cb);
380961e9c84SJason Wang 	void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
381961e9c84SJason Wang 	bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
382aac50c0bSEli Cohen 	int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
383aac50c0bSEli Cohen 			    const struct vdpa_vq_state *state);
38423750e39SEli Cohen 	int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
385aac50c0bSEli Cohen 			    struct vdpa_vq_state *state);
38613b00b13SEli Cohen 	int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
38713b00b13SEli Cohen 				   struct sk_buff *msg,
38813b00b13SEli Cohen 				   struct netlink_ext_ack *extack);
389c25a26e6SJason Wang 	struct vdpa_notification_area
390c25a26e6SJason Wang 	(*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
3914c05433bSZhu Lingshan 	/* vq irq is not expected to be changed once DRIVER_OK is set */
392d0ae1fbfSWu Zongyong 	int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
393*0a926fc9SZhu Lingshan 	u16 (*get_vq_size)(struct vdpa_device *vdev, u16 idx);
394961e9c84SJason Wang 
395961e9c84SJason Wang 	/* Device ops */
396425a5070SMichael S. Tsirkin 	u32 (*get_vq_align)(struct vdpa_device *vdev);
397d4821902SGautam Dawar 	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
398a72cac60SSi-Wei Liu 	u32 (*get_vq_desc_group)(struct vdpa_device *vdev, u16 idx);
399a64917bcSEli Cohen 	u64 (*get_device_features)(struct vdpa_device *vdev);
400b63e5c70SEugenio Pérez 	u64 (*get_backend_features)(const struct vdpa_device *vdev);
401a64917bcSEli Cohen 	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
402a64917bcSEli Cohen 	u64 (*get_driver_features)(struct vdpa_device *vdev);
403961e9c84SJason Wang 	void (*set_config_cb)(struct vdpa_device *vdev,
404961e9c84SJason Wang 			      struct vdpa_callback *cb);
405961e9c84SJason Wang 	u16 (*get_vq_num_max)(struct vdpa_device *vdev);
4063b970a58SWu Zongyong 	u16 (*get_vq_num_min)(struct vdpa_device *vdev);
407961e9c84SJason Wang 	u32 (*get_device_id)(struct vdpa_device *vdev);
408961e9c84SJason Wang 	u32 (*get_vendor_id)(struct vdpa_device *vdev);
409961e9c84SJason Wang 	u8 (*get_status)(struct vdpa_device *vdev);
410961e9c84SJason Wang 	void (*set_status)(struct vdpa_device *vdev, u8 status);
4110686082dSXie Yongji 	int (*reset)(struct vdpa_device *vdev);
412a26f2e4eSSi-Wei Liu 	int (*compat_reset)(struct vdpa_device *vdev, u32 flags);
413a26f2e4eSSi-Wei Liu #define VDPA_RESET_F_CLEAN_MAP 1
414848ecea1SEugenio Pérez 	int (*suspend)(struct vdpa_device *vdev);
4151538a8a4SSebastien Boeuf 	int (*resume)(struct vdpa_device *vdev);
416442706f9SStefano Garzarella 	size_t (*get_config_size)(struct vdpa_device *vdev);
417961e9c84SJason Wang 	void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
418961e9c84SJason Wang 			   void *buf, unsigned int len);
419961e9c84SJason Wang 	void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
420961e9c84SJason Wang 			   const void *buf, unsigned int len);
421961e9c84SJason Wang 	u32 (*get_generation)(struct vdpa_device *vdev);
4223f1b623aSJason Wang 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
4231d246927SXie Yongji 	int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx,
4241d246927SXie Yongji 			       const struct cpumask *cpu_mask);
4251d246927SXie Yongji 	const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev,
4261d246927SXie Yongji 						 u16 idx);
427961e9c84SJason Wang 
428961e9c84SJason Wang 	/* DMA ops */
429db9adcbfSGautam Dawar 	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
430db9adcbfSGautam Dawar 		       struct vhost_iotlb *iotlb);
431db9adcbfSGautam Dawar 	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
432db9adcbfSGautam Dawar 		       u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
433db9adcbfSGautam Dawar 	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
434db9adcbfSGautam Dawar 			 u64 iova, u64 size);
435d2cf1b6eSSi-Wei Liu 	int (*reset_map)(struct vdpa_device *vdev, unsigned int asid);
43646d554b1SGautam Dawar 	int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
43746d554b1SGautam Dawar 			      unsigned int asid);
43825da258fSJason Wang 	struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx);
439c618c84dSStefano Garzarella 	int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm);
440c618c84dSStefano Garzarella 	void (*unbind_mm)(struct vdpa_device *vdev);
441961e9c84SJason Wang 
442961e9c84SJason Wang 	/* Free device resources */
443961e9c84SJason Wang 	void (*free)(struct vdpa_device *vdev);
444961e9c84SJason Wang };
445961e9c84SJason Wang 
446961e9c84SJason Wang struct vdpa_device *__vdpa_alloc_device(struct device *parent,
447961e9c84SJason Wang 					const struct vdpa_config_ops *config,
448db9adcbfSGautam Dawar 					unsigned int ngroups, unsigned int nas,
449d8945ec4SXie Yongji 					size_t size, const char *name,
450d8945ec4SXie Yongji 					bool use_va);
451961e9c84SJason Wang 
452c8d182bdSXie Yongji /**
453c8d182bdSXie Yongji  * vdpa_alloc_device - allocate and initilaize a vDPA device
454c8d182bdSXie Yongji  *
455c8d182bdSXie Yongji  * @dev_struct: the type of the parent structure
456c8d182bdSXie Yongji  * @member: the name of struct vdpa_device within the @dev_struct
457c8d182bdSXie Yongji  * @parent: the parent device
458c8d182bdSXie Yongji  * @config: the bus operations that is supported by this device
459d4821902SGautam Dawar  * @ngroups: the number of virtqueue groups supported by this device
460db9adcbfSGautam Dawar  * @nas: the number of address spaces
461c8d182bdSXie Yongji  * @name: name of the vdpa device
462d8945ec4SXie Yongji  * @use_va: indicate whether virtual address must be used by this device
463c8d182bdSXie Yongji  *
464c8d182bdSXie Yongji  * Return allocated data structure or ERR_PTR upon error
465c8d182bdSXie Yongji  */
466db9adcbfSGautam Dawar #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
467db9adcbfSGautam Dawar 			  name, use_va) \
468d4821902SGautam Dawar 			  container_of((__vdpa_alloc_device( \
469db9adcbfSGautam Dawar 				       parent, config, ngroups, nas, \
470db9adcbfSGautam Dawar 				       (sizeof(dev_struct) + \
471961e9c84SJason Wang 				       BUILD_BUG_ON_ZERO(offsetof( \
472db9adcbfSGautam Dawar 				       dev_struct, member))), name, use_va)), \
473961e9c84SJason Wang 				       dev_struct, member)
474961e9c84SJason Wang 
47581d46d69SLongpeng int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
476961e9c84SJason Wang void vdpa_unregister_device(struct vdpa_device *vdev);
477961e9c84SJason Wang 
47881d46d69SLongpeng int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
479903f7bcaSParav Pandit void _vdpa_unregister_device(struct vdpa_device *vdev);
480903f7bcaSParav Pandit 
481961e9c84SJason Wang /**
482d0f9164eSParav Pandit  * struct vdpa_driver - operations for a vDPA driver
483961e9c84SJason Wang  * @driver: underlying device driver
484961e9c84SJason Wang  * @probe: the function to call when a device is found.  Returns 0 or -errno.
485961e9c84SJason Wang  * @remove: the function to call when a device is removed.
486961e9c84SJason Wang  */
487961e9c84SJason Wang struct vdpa_driver {
488961e9c84SJason Wang 	struct device_driver driver;
489961e9c84SJason Wang 	int (*probe)(struct vdpa_device *vdev);
490961e9c84SJason Wang 	void (*remove)(struct vdpa_device *vdev);
491961e9c84SJason Wang };
492961e9c84SJason Wang 
493961e9c84SJason Wang #define vdpa_register_driver(drv) \
494961e9c84SJason Wang 	__vdpa_register_driver(drv, THIS_MODULE)
495961e9c84SJason Wang int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
496961e9c84SJason Wang void vdpa_unregister_driver(struct vdpa_driver *drv);
497961e9c84SJason Wang 
498961e9c84SJason Wang #define module_vdpa_driver(__vdpa_driver) \
499961e9c84SJason Wang 	module_driver(__vdpa_driver, vdpa_register_driver,	\
500961e9c84SJason Wang 		      vdpa_unregister_driver)
501961e9c84SJason Wang 
502961e9c84SJason Wang static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
503961e9c84SJason Wang {
504961e9c84SJason Wang 	return container_of(driver, struct vdpa_driver, driver);
505961e9c84SJason Wang }
506961e9c84SJason Wang 
507961e9c84SJason Wang static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
508961e9c84SJason Wang {
509961e9c84SJason Wang 	return container_of(_dev, struct vdpa_device, dev);
510961e9c84SJason Wang }
511961e9c84SJason Wang 
512961e9c84SJason Wang static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
513961e9c84SJason Wang {
514961e9c84SJason Wang 	return dev_get_drvdata(&vdev->dev);
515961e9c84SJason Wang }
516961e9c84SJason Wang 
517961e9c84SJason Wang static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
518961e9c84SJason Wang {
519961e9c84SJason Wang 	dev_set_drvdata(&vdev->dev, data);
520961e9c84SJason Wang }
521961e9c84SJason Wang 
522961e9c84SJason Wang static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
523961e9c84SJason Wang {
524961e9c84SJason Wang 	return vdev->dma_dev;
525961e9c84SJason Wang }
526452639a6SMichael S. Tsirkin 
527bc91df5cSSi-Wei Liu static inline int vdpa_reset(struct vdpa_device *vdev, u32 flags)
528452639a6SMichael S. Tsirkin {
529452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
530aba21affSEli Cohen 	int ret;
531452639a6SMichael S. Tsirkin 
532a6a51adcSEli Cohen 	down_write(&vdev->cf_lock);
533452639a6SMichael S. Tsirkin 	vdev->features_valid = false;
534bc91df5cSSi-Wei Liu 	if (ops->compat_reset && flags)
535bc91df5cSSi-Wei Liu 		ret = ops->compat_reset(vdev, flags);
536bc91df5cSSi-Wei Liu 	else
537aba21affSEli Cohen 		ret = ops->reset(vdev);
538a6a51adcSEli Cohen 	up_write(&vdev->cf_lock);
539aba21affSEli Cohen 	return ret;
540452639a6SMichael S. Tsirkin }
541452639a6SMichael S. Tsirkin 
542e0077cc1SSi-Wei Liu static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
543452639a6SMichael S. Tsirkin {
544452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
545aba21affSEli Cohen 	int ret;
546aba21affSEli Cohen 
547452639a6SMichael S. Tsirkin 	vdev->features_valid = true;
548aba21affSEli Cohen 	ret = ops->set_driver_features(vdev, features);
549e0077cc1SSi-Wei Liu 
550e0077cc1SSi-Wei Liu 	return ret;
551e0077cc1SSi-Wei Liu }
552e0077cc1SSi-Wei Liu 
553e0077cc1SSi-Wei Liu static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
554e0077cc1SSi-Wei Liu {
555e0077cc1SSi-Wei Liu 	int ret;
556e0077cc1SSi-Wei Liu 
557a6a51adcSEli Cohen 	down_write(&vdev->cf_lock);
558e0077cc1SSi-Wei Liu 	ret = vdpa_set_features_unlocked(vdev, features);
559a6a51adcSEli Cohen 	up_write(&vdev->cf_lock);
560aba21affSEli Cohen 
561aba21affSEli Cohen 	return ret;
562452639a6SMichael S. Tsirkin }
563452639a6SMichael S. Tsirkin 
5646dbb1f16SParav Pandit void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
5656dbb1f16SParav Pandit 		     void *buf, unsigned int len);
5666dbb1f16SParav Pandit void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
5676dbb1f16SParav Pandit 		     const void *buf, unsigned int length);
56873bc0dbbSEli Cohen void vdpa_set_status(struct vdpa_device *vdev, u8 status);
56973bc0dbbSEli Cohen 
57033b34750SParav Pandit /**
571d0f9164eSParav Pandit  * struct vdpa_mgmtdev_ops - vdpa device ops
57233b34750SParav Pandit  * @dev_add: Add a vdpa device using alloc and register
57333b34750SParav Pandit  *	     @mdev: parent device to use for device addition
57433b34750SParav Pandit  *	     @name: name of the new vdpa device
575d8ca2fa5SParav Pandit  *	     @config: config attributes to apply to the device under creation
57633b34750SParav Pandit  *	     Driver need to add a new device using _vdpa_register_device()
57733b34750SParav Pandit  *	     after fully initializing the vdpa device. Driver must return 0
57833b34750SParav Pandit  *	     on success or appropriate error code.
57933b34750SParav Pandit  * @dev_del: Remove a vdpa device using unregister
58033b34750SParav Pandit  *	     @mdev: parent device to use for device removal
58133b34750SParav Pandit  *	     @dev: vdpa device to remove
58233b34750SParav Pandit  *	     Driver need to remove the specified device by calling
58333b34750SParav Pandit  *	     _vdpa_unregister_device().
58433b34750SParav Pandit  */
58533b34750SParav Pandit struct vdpa_mgmtdev_ops {
586d8ca2fa5SParav Pandit 	int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
587d8ca2fa5SParav Pandit 		       const struct vdpa_dev_set_config *config);
58833b34750SParav Pandit 	void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
58933b34750SParav Pandit };
59033b34750SParav Pandit 
591960deb33SParav Pandit /**
592960deb33SParav Pandit  * struct vdpa_mgmt_dev - vdpa management device
593960deb33SParav Pandit  * @device: Management parent device
594960deb33SParav Pandit  * @ops: operations supported by management device
595960deb33SParav Pandit  * @id_table: Pointer to device id table of supported ids
596d8ca2fa5SParav Pandit  * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
597d8ca2fa5SParav Pandit  *		      management device support during dev_add callback
598960deb33SParav Pandit  * @list: list entry
5999be5d2d4SSimon Horman  * @supported_features: features supported by device
6009be5d2d4SSimon Horman  * @max_supported_vqs: maximum number of virtqueues supported by device
601960deb33SParav Pandit  */
60233b34750SParav Pandit struct vdpa_mgmt_dev {
60333b34750SParav Pandit 	struct device *device;
60433b34750SParav Pandit 	const struct vdpa_mgmtdev_ops *ops;
605ffbda8e9SCindy Lu 	struct virtio_device_id *id_table;
606d8ca2fa5SParav Pandit 	u64 config_attr_mask;
60733b34750SParav Pandit 	struct list_head list;
608cd2629f6SEli Cohen 	u64 supported_features;
609cd2629f6SEli Cohen 	u32 max_supported_vqs;
61033b34750SParav Pandit };
61133b34750SParav Pandit 
61233b34750SParav Pandit int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
61333b34750SParav Pandit void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
61433b34750SParav Pandit 
615961e9c84SJason Wang #endif /* _LINUX_VDPA_H */
616