xref: /linux-6.15/include/linux/vdpa.h (revision a72cac60)
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.
198961e9c84SJason Wang  * @get_vq_align:		Get the virtqueue align requirement
199961e9c84SJason Wang  *				for the device
200961e9c84SJason Wang  *				@vdev: vdpa device
201961e9c84SJason Wang  *				Returns virtqueue algin requirement
20200d1f546SJason Wang  * @get_vq_group:		Get the group id for a specific
20300d1f546SJason Wang  *				virtqueue (optional)
204d4821902SGautam Dawar  *				@vdev: vdpa device
205d4821902SGautam Dawar  *				@idx: virtqueue index
206d4821902SGautam Dawar  *				Returns u32: group id for this virtqueue
207*a72cac60SSi-Wei Liu  * @get_vq_desc_group:		Get the group id for the descriptor table of
208*a72cac60SSi-Wei Liu  *				a specific virtqueue (optional)
209*a72cac60SSi-Wei Liu  *				@vdev: vdpa device
210*a72cac60SSi-Wei Liu  *				@idx: virtqueue index
211*a72cac60SSi-Wei Liu  *				Returns u32: group id for the descriptor table
212*a72cac60SSi-Wei Liu  *				portion of this virtqueue. Could be different
213*a72cac60SSi-Wei Liu  *				than the one from @get_vq_group, in which case
214*a72cac60SSi-Wei Liu  *				the access to the descriptor table can be
215*a72cac60SSi-Wei Liu  *				confined to a separate asid, isolating from
216*a72cac60SSi-Wei Liu  *				the virtqueue's buffer address access.
217a64917bcSEli Cohen  * @get_device_features:	Get virtio features supported by the device
218961e9c84SJason Wang  *				@vdev: vdpa device
219961e9c84SJason Wang  *				Returns the virtio features support by the
220961e9c84SJason Wang  *				device
221b63e5c70SEugenio Pérez  * @get_backend_features:	Get parent-specific backend features (optional)
222b63e5c70SEugenio Pérez  *				Returns the vdpa features supported by the
223b63e5c70SEugenio Pérez  *				device.
224a64917bcSEli Cohen  * @set_driver_features:	Set virtio features supported by the driver
225961e9c84SJason Wang  *				@vdev: vdpa device
226961e9c84SJason Wang  *				@features: feature support by the driver
227961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
228a64917bcSEli Cohen  * @get_driver_features:	Get the virtio driver features in action
229a64917bcSEli Cohen  *				@vdev: vdpa device
230a64917bcSEli Cohen  *				Returns the virtio features accepted
231961e9c84SJason Wang  * @set_config_cb:		Set the config interrupt callback
232961e9c84SJason Wang  *				@vdev: vdpa device
233961e9c84SJason Wang  *				@cb: virtio-vdev interrupt callback structure
234961e9c84SJason Wang  * @get_vq_num_max:		Get the max size of virtqueue
235961e9c84SJason Wang  *				@vdev: vdpa device
236961e9c84SJason Wang  *				Returns u16: max size of virtqueue
2373b970a58SWu Zongyong  * @get_vq_num_min:		Get the min size of virtqueue (optional)
2383b970a58SWu Zongyong  *				@vdev: vdpa device
2393b970a58SWu Zongyong  *				Returns u16: min size of virtqueue
240961e9c84SJason Wang  * @get_device_id:		Get virtio device id
241961e9c84SJason Wang  *				@vdev: vdpa device
242961e9c84SJason Wang  *				Returns u32: virtio device id
243961e9c84SJason Wang  * @get_vendor_id:		Get id for the vendor that provides this device
244961e9c84SJason Wang  *				@vdev: vdpa device
245961e9c84SJason Wang  *				Returns u32: virtio vendor id
246961e9c84SJason Wang  * @get_status:			Get the device status
247961e9c84SJason Wang  *				@vdev: vdpa device
248961e9c84SJason Wang  *				Returns u8: virtio device status
249961e9c84SJason Wang  * @set_status:			Set the device status
250961e9c84SJason Wang  *				@vdev: vdpa device
251961e9c84SJason Wang  *				@status: virtio device status
2520686082dSXie Yongji  * @reset:			Reset device
2530686082dSXie Yongji  *				@vdev: vdpa device
2540686082dSXie Yongji  *				Returns integer: success (0) or error (< 0)
2551538a8a4SSebastien Boeuf  * @suspend:			Suspend the device (optional)
2561538a8a4SSebastien Boeuf  *				@vdev: vdpa device
2571538a8a4SSebastien Boeuf  *				Returns integer: success (0) or error (< 0)
2581538a8a4SSebastien Boeuf  * @resume:			Resume the device (optional)
259848ecea1SEugenio Pérez  *				@vdev: vdpa device
260848ecea1SEugenio Pérez  *				Returns integer: success (0) or error (< 0)
261a61280ddSLongpeng  * @get_config_size:		Get the size of the configuration space includes
262a61280ddSLongpeng  *				fields that are conditional on feature bits.
263442706f9SStefano Garzarella  *				@vdev: vdpa device
264442706f9SStefano Garzarella  *				Returns size_t: configuration size
265961e9c84SJason Wang  * @get_config:			Read from device specific configuration space
266961e9c84SJason Wang  *				@vdev: vdpa device
267961e9c84SJason Wang  *				@offset: offset from the beginning of
268961e9c84SJason Wang  *				configuration space
269961e9c84SJason Wang  *				@buf: buffer used to read to
270961e9c84SJason Wang  *				@len: the length to read from
271961e9c84SJason Wang  *				configuration space
272961e9c84SJason Wang  * @set_config:			Write to device specific configuration space
273961e9c84SJason Wang  *				@vdev: vdpa device
274961e9c84SJason Wang  *				@offset: offset from the beginning of
275961e9c84SJason Wang  *				configuration space
276961e9c84SJason Wang  *				@buf: buffer used to write from
277961e9c84SJason Wang  *				@len: the length to write to
278961e9c84SJason Wang  *				configuration space
279961e9c84SJason Wang  * @get_generation:		Get device config generation (optional)
280961e9c84SJason Wang  *				@vdev: vdpa device
281961e9c84SJason Wang  *				Returns u32: device generation
2823f1b623aSJason Wang  * @get_iova_range:		Get supported iova range (optional)
2833f1b623aSJason Wang  *				@vdev: vdpa device
2843f1b623aSJason Wang  *				Returns the iova range supported by
2853f1b623aSJason Wang  *				the device.
2861d246927SXie Yongji  * @set_vq_affinity:		Set the affinity of virtqueue (optional)
2871d246927SXie Yongji  *				@vdev: vdpa device
2881d246927SXie Yongji  *				@idx: virtqueue index
2891d246927SXie Yongji  *				@cpu_mask: the affinity mask
2901d246927SXie Yongji  *				Returns integer: success (0) or error (< 0)
2911d246927SXie Yongji  * @get_vq_affinity:		Get the affinity of virtqueue (optional)
2921d246927SXie Yongji  *				@vdev: vdpa device
2931d246927SXie Yongji  *				@idx: virtqueue index
2941d246927SXie Yongji  *				Returns the affinity mask
29546d554b1SGautam Dawar  * @set_group_asid:		Set address space identifier for a
29600d1f546SJason Wang  *				virtqueue group (optional)
29746d554b1SGautam Dawar  *				@vdev: vdpa device
29846d554b1SGautam Dawar  *				@group: virtqueue group
29946d554b1SGautam Dawar  *				@asid: address space id for this group
30046d554b1SGautam Dawar  *				Returns integer: success (0) or error (< 0)
301961e9c84SJason Wang  * @set_map:			Set device memory mapping (optional)
302961e9c84SJason Wang  *				Needed for device that using device
303961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
304961e9c84SJason Wang  *				@vdev: vdpa device
305db9adcbfSGautam Dawar  *				@asid: address space identifier
306961e9c84SJason Wang  *				@iotlb: vhost memory mapping to be
307961e9c84SJason Wang  *				used by the vDPA
308961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
309961e9c84SJason Wang  * @dma_map:			Map an area of PA to IOVA (optional)
310961e9c84SJason Wang  *				Needed for device that using device
311961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
312961e9c84SJason Wang  *				and preferring incremental map.
313961e9c84SJason Wang  *				@vdev: vdpa device
314db9adcbfSGautam Dawar  *				@asid: address space identifier
315961e9c84SJason Wang  *				@iova: iova to be mapped
316961e9c84SJason Wang  *				@size: size of the area
317961e9c84SJason Wang  *				@pa: physical address for the map
318961e9c84SJason Wang  *				@perm: device access permission (VHOST_MAP_XX)
319961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
320961e9c84SJason Wang  * @dma_unmap:			Unmap an area of IOVA (optional but
321961e9c84SJason Wang  *				must be implemented with dma_map)
322961e9c84SJason Wang  *				Needed for device that using device
323961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
324961e9c84SJason Wang  *				and preferring incremental unmap.
325961e9c84SJason Wang  *				@vdev: vdpa device
326db9adcbfSGautam Dawar  *				@asid: address space identifier
327961e9c84SJason Wang  *				@iova: iova to be unmapped
328961e9c84SJason Wang  *				@size: size of the area
329961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
33025da258fSJason Wang  * @get_vq_dma_dev:		Get the dma device for a specific
33125da258fSJason Wang  *				virtqueue (optional)
33225da258fSJason Wang  *				@vdev: vdpa device
33325da258fSJason Wang  *				@idx: virtqueue index
33425da258fSJason Wang  *				Returns pointer to structure device or error (NULL)
335c618c84dSStefano Garzarella  * @bind_mm:			Bind the device to a specific address space
336c618c84dSStefano Garzarella  *				so the vDPA framework can use VA when this
337c618c84dSStefano Garzarella  *				callback is implemented. (optional)
338c618c84dSStefano Garzarella  *				@vdev: vdpa device
339c618c84dSStefano Garzarella  *				@mm: address space to bind
340c618c84dSStefano Garzarella  * @unbind_mm:			Unbind the device from the address space
341c618c84dSStefano Garzarella  *				bound using the bind_mm callback. (optional)
342c618c84dSStefano Garzarella  *				@vdev: vdpa device
343961e9c84SJason Wang  * @free:			Free resources that belongs to vDPA (optional)
344961e9c84SJason Wang  *				@vdev: vdpa device
345961e9c84SJason Wang  */
346961e9c84SJason Wang struct vdpa_config_ops {
347961e9c84SJason Wang 	/* Virtqueue ops */
348961e9c84SJason Wang 	int (*set_vq_address)(struct vdpa_device *vdev,
349961e9c84SJason Wang 			      u16 idx, u64 desc_area, u64 driver_area,
350961e9c84SJason Wang 			      u64 device_area);
351961e9c84SJason Wang 	void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
352961e9c84SJason Wang 	void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
3532c4e4a22SAlvaro Karsz 	void (*kick_vq_with_data)(struct vdpa_device *vdev, u32 data);
354961e9c84SJason Wang 	void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
355961e9c84SJason Wang 			  struct vdpa_callback *cb);
356961e9c84SJason Wang 	void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
357961e9c84SJason Wang 	bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
358aac50c0bSEli Cohen 	int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
359aac50c0bSEli Cohen 			    const struct vdpa_vq_state *state);
36023750e39SEli Cohen 	int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
361aac50c0bSEli Cohen 			    struct vdpa_vq_state *state);
36213b00b13SEli Cohen 	int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
36313b00b13SEli Cohen 				   struct sk_buff *msg,
36413b00b13SEli Cohen 				   struct netlink_ext_ack *extack);
365c25a26e6SJason Wang 	struct vdpa_notification_area
366c25a26e6SJason Wang 	(*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
3674c05433bSZhu Lingshan 	/* vq irq is not expected to be changed once DRIVER_OK is set */
368d0ae1fbfSWu Zongyong 	int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
369961e9c84SJason Wang 
370961e9c84SJason Wang 	/* Device ops */
371425a5070SMichael S. Tsirkin 	u32 (*get_vq_align)(struct vdpa_device *vdev);
372d4821902SGautam Dawar 	u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
373*a72cac60SSi-Wei Liu 	u32 (*get_vq_desc_group)(struct vdpa_device *vdev, u16 idx);
374a64917bcSEli Cohen 	u64 (*get_device_features)(struct vdpa_device *vdev);
375b63e5c70SEugenio Pérez 	u64 (*get_backend_features)(const struct vdpa_device *vdev);
376a64917bcSEli Cohen 	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
377a64917bcSEli Cohen 	u64 (*get_driver_features)(struct vdpa_device *vdev);
378961e9c84SJason Wang 	void (*set_config_cb)(struct vdpa_device *vdev,
379961e9c84SJason Wang 			      struct vdpa_callback *cb);
380961e9c84SJason Wang 	u16 (*get_vq_num_max)(struct vdpa_device *vdev);
3813b970a58SWu Zongyong 	u16 (*get_vq_num_min)(struct vdpa_device *vdev);
382961e9c84SJason Wang 	u32 (*get_device_id)(struct vdpa_device *vdev);
383961e9c84SJason Wang 	u32 (*get_vendor_id)(struct vdpa_device *vdev);
384961e9c84SJason Wang 	u8 (*get_status)(struct vdpa_device *vdev);
385961e9c84SJason Wang 	void (*set_status)(struct vdpa_device *vdev, u8 status);
3860686082dSXie Yongji 	int (*reset)(struct vdpa_device *vdev);
387848ecea1SEugenio Pérez 	int (*suspend)(struct vdpa_device *vdev);
3881538a8a4SSebastien Boeuf 	int (*resume)(struct vdpa_device *vdev);
389442706f9SStefano Garzarella 	size_t (*get_config_size)(struct vdpa_device *vdev);
390961e9c84SJason Wang 	void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
391961e9c84SJason Wang 			   void *buf, unsigned int len);
392961e9c84SJason Wang 	void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
393961e9c84SJason Wang 			   const void *buf, unsigned int len);
394961e9c84SJason Wang 	u32 (*get_generation)(struct vdpa_device *vdev);
3953f1b623aSJason Wang 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
3961d246927SXie Yongji 	int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx,
3971d246927SXie Yongji 			       const struct cpumask *cpu_mask);
3981d246927SXie Yongji 	const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev,
3991d246927SXie Yongji 						 u16 idx);
400961e9c84SJason Wang 
401961e9c84SJason Wang 	/* DMA ops */
402db9adcbfSGautam Dawar 	int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
403db9adcbfSGautam Dawar 		       struct vhost_iotlb *iotlb);
404db9adcbfSGautam Dawar 	int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
405db9adcbfSGautam Dawar 		       u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
406db9adcbfSGautam Dawar 	int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
407db9adcbfSGautam Dawar 			 u64 iova, u64 size);
40846d554b1SGautam Dawar 	int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
40946d554b1SGautam Dawar 			      unsigned int asid);
41025da258fSJason Wang 	struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx);
411c618c84dSStefano Garzarella 	int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm);
412c618c84dSStefano Garzarella 	void (*unbind_mm)(struct vdpa_device *vdev);
413961e9c84SJason Wang 
414961e9c84SJason Wang 	/* Free device resources */
415961e9c84SJason Wang 	void (*free)(struct vdpa_device *vdev);
416961e9c84SJason Wang };
417961e9c84SJason Wang 
418961e9c84SJason Wang struct vdpa_device *__vdpa_alloc_device(struct device *parent,
419961e9c84SJason Wang 					const struct vdpa_config_ops *config,
420db9adcbfSGautam Dawar 					unsigned int ngroups, unsigned int nas,
421d8945ec4SXie Yongji 					size_t size, const char *name,
422d8945ec4SXie Yongji 					bool use_va);
423961e9c84SJason Wang 
424c8d182bdSXie Yongji /**
425c8d182bdSXie Yongji  * vdpa_alloc_device - allocate and initilaize a vDPA device
426c8d182bdSXie Yongji  *
427c8d182bdSXie Yongji  * @dev_struct: the type of the parent structure
428c8d182bdSXie Yongji  * @member: the name of struct vdpa_device within the @dev_struct
429c8d182bdSXie Yongji  * @parent: the parent device
430c8d182bdSXie Yongji  * @config: the bus operations that is supported by this device
431d4821902SGautam Dawar  * @ngroups: the number of virtqueue groups supported by this device
432db9adcbfSGautam Dawar  * @nas: the number of address spaces
433c8d182bdSXie Yongji  * @name: name of the vdpa device
434d8945ec4SXie Yongji  * @use_va: indicate whether virtual address must be used by this device
435c8d182bdSXie Yongji  *
436c8d182bdSXie Yongji  * Return allocated data structure or ERR_PTR upon error
437c8d182bdSXie Yongji  */
438db9adcbfSGautam Dawar #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
439db9adcbfSGautam Dawar 			  name, use_va) \
440d4821902SGautam Dawar 			  container_of((__vdpa_alloc_device( \
441db9adcbfSGautam Dawar 				       parent, config, ngroups, nas, \
442db9adcbfSGautam Dawar 				       (sizeof(dev_struct) + \
443961e9c84SJason Wang 				       BUILD_BUG_ON_ZERO(offsetof( \
444db9adcbfSGautam Dawar 				       dev_struct, member))), name, use_va)), \
445961e9c84SJason Wang 				       dev_struct, member)
446961e9c84SJason Wang 
44781d46d69SLongpeng int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
448961e9c84SJason Wang void vdpa_unregister_device(struct vdpa_device *vdev);
449961e9c84SJason Wang 
45081d46d69SLongpeng int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
451903f7bcaSParav Pandit void _vdpa_unregister_device(struct vdpa_device *vdev);
452903f7bcaSParav Pandit 
453961e9c84SJason Wang /**
454d0f9164eSParav Pandit  * struct vdpa_driver - operations for a vDPA driver
455961e9c84SJason Wang  * @driver: underlying device driver
456961e9c84SJason Wang  * @probe: the function to call when a device is found.  Returns 0 or -errno.
457961e9c84SJason Wang  * @remove: the function to call when a device is removed.
458961e9c84SJason Wang  */
459961e9c84SJason Wang struct vdpa_driver {
460961e9c84SJason Wang 	struct device_driver driver;
461961e9c84SJason Wang 	int (*probe)(struct vdpa_device *vdev);
462961e9c84SJason Wang 	void (*remove)(struct vdpa_device *vdev);
463961e9c84SJason Wang };
464961e9c84SJason Wang 
465961e9c84SJason Wang #define vdpa_register_driver(drv) \
466961e9c84SJason Wang 	__vdpa_register_driver(drv, THIS_MODULE)
467961e9c84SJason Wang int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
468961e9c84SJason Wang void vdpa_unregister_driver(struct vdpa_driver *drv);
469961e9c84SJason Wang 
470961e9c84SJason Wang #define module_vdpa_driver(__vdpa_driver) \
471961e9c84SJason Wang 	module_driver(__vdpa_driver, vdpa_register_driver,	\
472961e9c84SJason Wang 		      vdpa_unregister_driver)
473961e9c84SJason Wang 
474961e9c84SJason Wang static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
475961e9c84SJason Wang {
476961e9c84SJason Wang 	return container_of(driver, struct vdpa_driver, driver);
477961e9c84SJason Wang }
478961e9c84SJason Wang 
479961e9c84SJason Wang static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
480961e9c84SJason Wang {
481961e9c84SJason Wang 	return container_of(_dev, struct vdpa_device, dev);
482961e9c84SJason Wang }
483961e9c84SJason Wang 
484961e9c84SJason Wang static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
485961e9c84SJason Wang {
486961e9c84SJason Wang 	return dev_get_drvdata(&vdev->dev);
487961e9c84SJason Wang }
488961e9c84SJason Wang 
489961e9c84SJason Wang static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
490961e9c84SJason Wang {
491961e9c84SJason Wang 	dev_set_drvdata(&vdev->dev, data);
492961e9c84SJason Wang }
493961e9c84SJason Wang 
494961e9c84SJason Wang static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
495961e9c84SJason Wang {
496961e9c84SJason Wang 	return vdev->dma_dev;
497961e9c84SJason Wang }
498452639a6SMichael S. Tsirkin 
4990686082dSXie Yongji static inline int vdpa_reset(struct vdpa_device *vdev)
500452639a6SMichael S. Tsirkin {
501452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
502aba21affSEli Cohen 	int ret;
503452639a6SMichael S. Tsirkin 
504a6a51adcSEli Cohen 	down_write(&vdev->cf_lock);
505452639a6SMichael S. Tsirkin 	vdev->features_valid = false;
506aba21affSEli Cohen 	ret = ops->reset(vdev);
507a6a51adcSEli Cohen 	up_write(&vdev->cf_lock);
508aba21affSEli Cohen 	return ret;
509452639a6SMichael S. Tsirkin }
510452639a6SMichael S. Tsirkin 
511e0077cc1SSi-Wei Liu static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
512452639a6SMichael S. Tsirkin {
513452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
514aba21affSEli Cohen 	int ret;
515aba21affSEli Cohen 
516452639a6SMichael S. Tsirkin 	vdev->features_valid = true;
517aba21affSEli Cohen 	ret = ops->set_driver_features(vdev, features);
518e0077cc1SSi-Wei Liu 
519e0077cc1SSi-Wei Liu 	return ret;
520e0077cc1SSi-Wei Liu }
521e0077cc1SSi-Wei Liu 
522e0077cc1SSi-Wei Liu static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
523e0077cc1SSi-Wei Liu {
524e0077cc1SSi-Wei Liu 	int ret;
525e0077cc1SSi-Wei Liu 
526a6a51adcSEli Cohen 	down_write(&vdev->cf_lock);
527e0077cc1SSi-Wei Liu 	ret = vdpa_set_features_unlocked(vdev, features);
528a6a51adcSEli Cohen 	up_write(&vdev->cf_lock);
529aba21affSEli Cohen 
530aba21affSEli Cohen 	return ret;
531452639a6SMichael S. Tsirkin }
532452639a6SMichael S. Tsirkin 
5336dbb1f16SParav Pandit void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
5346dbb1f16SParav Pandit 		     void *buf, unsigned int len);
5356dbb1f16SParav Pandit void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
5366dbb1f16SParav Pandit 		     const void *buf, unsigned int length);
53773bc0dbbSEli Cohen void vdpa_set_status(struct vdpa_device *vdev, u8 status);
53873bc0dbbSEli Cohen 
53933b34750SParav Pandit /**
540d0f9164eSParav Pandit  * struct vdpa_mgmtdev_ops - vdpa device ops
54133b34750SParav Pandit  * @dev_add: Add a vdpa device using alloc and register
54233b34750SParav Pandit  *	     @mdev: parent device to use for device addition
54333b34750SParav Pandit  *	     @name: name of the new vdpa device
544d8ca2fa5SParav Pandit  *	     @config: config attributes to apply to the device under creation
54533b34750SParav Pandit  *	     Driver need to add a new device using _vdpa_register_device()
54633b34750SParav Pandit  *	     after fully initializing the vdpa device. Driver must return 0
54733b34750SParav Pandit  *	     on success or appropriate error code.
54833b34750SParav Pandit  * @dev_del: Remove a vdpa device using unregister
54933b34750SParav Pandit  *	     @mdev: parent device to use for device removal
55033b34750SParav Pandit  *	     @dev: vdpa device to remove
55133b34750SParav Pandit  *	     Driver need to remove the specified device by calling
55233b34750SParav Pandit  *	     _vdpa_unregister_device().
55333b34750SParav Pandit  */
55433b34750SParav Pandit struct vdpa_mgmtdev_ops {
555d8ca2fa5SParav Pandit 	int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
556d8ca2fa5SParav Pandit 		       const struct vdpa_dev_set_config *config);
55733b34750SParav Pandit 	void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
55833b34750SParav Pandit };
55933b34750SParav Pandit 
560960deb33SParav Pandit /**
561960deb33SParav Pandit  * struct vdpa_mgmt_dev - vdpa management device
562960deb33SParav Pandit  * @device: Management parent device
563960deb33SParav Pandit  * @ops: operations supported by management device
564960deb33SParav Pandit  * @id_table: Pointer to device id table of supported ids
565d8ca2fa5SParav Pandit  * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
566d8ca2fa5SParav Pandit  *		      management device support during dev_add callback
567960deb33SParav Pandit  * @list: list entry
5689be5d2d4SSimon Horman  * @supported_features: features supported by device
5699be5d2d4SSimon Horman  * @max_supported_vqs: maximum number of virtqueues supported by device
570960deb33SParav Pandit  */
57133b34750SParav Pandit struct vdpa_mgmt_dev {
57233b34750SParav Pandit 	struct device *device;
57333b34750SParav Pandit 	const struct vdpa_mgmtdev_ops *ops;
574ffbda8e9SCindy Lu 	struct virtio_device_id *id_table;
575d8ca2fa5SParav Pandit 	u64 config_attr_mask;
57633b34750SParav Pandit 	struct list_head list;
577cd2629f6SEli Cohen 	u64 supported_features;
578cd2629f6SEli Cohen 	u32 max_supported_vqs;
57933b34750SParav Pandit };
58033b34750SParav Pandit 
58133b34750SParav Pandit int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
58233b34750SParav Pandit void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
58333b34750SParav Pandit 
584961e9c84SJason Wang #endif /* _LINUX_VDPA_H */
585