xref: /linux-6.15/include/linux/vdpa.h (revision 240bf4e6)
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 /**
13d0f9164eSParav Pandit  * struct vdpa_calllback - vDPA callback definition.
14961e9c84SJason Wang  * @callback: interrupt callback function
15961e9c84SJason Wang  * @private: the data passed to the callback function
16961e9c84SJason Wang  */
17961e9c84SJason Wang struct vdpa_callback {
18961e9c84SJason Wang 	irqreturn_t (*callback)(void *data);
19961e9c84SJason Wang 	void *private;
20961e9c84SJason Wang };
21961e9c84SJason Wang 
22961e9c84SJason Wang /**
23d0f9164eSParav Pandit  * struct vdpa_notification_area - vDPA notification area
24c25a26e6SJason Wang  * @addr: base address of the notification area
25c25a26e6SJason Wang  * @size: size of the notification area
26c25a26e6SJason Wang  */
27c25a26e6SJason Wang struct vdpa_notification_area {
28c25a26e6SJason Wang 	resource_size_t addr;
29c25a26e6SJason Wang 	resource_size_t size;
30c25a26e6SJason Wang };
31c25a26e6SJason Wang 
32c25a26e6SJason Wang /**
33530a5678SJason Wang  * struct vdpa_vq_state_split - vDPA split virtqueue state
34aac50c0bSEli Cohen  * @avail_index: available index
35aac50c0bSEli Cohen  */
36530a5678SJason Wang struct vdpa_vq_state_split {
37aac50c0bSEli Cohen 	u16	avail_index;
38aac50c0bSEli Cohen };
39aac50c0bSEli Cohen 
40530a5678SJason Wang /**
41530a5678SJason Wang  * struct vdpa_vq_state_packed - vDPA packed virtqueue state
42530a5678SJason Wang  * @last_avail_counter: last driver ring wrap counter observed by device
43530a5678SJason Wang  * @last_avail_idx: device available index
44530a5678SJason Wang  * @last_used_counter: device ring wrap counter
45530a5678SJason Wang  * @last_used_idx: used index
46530a5678SJason Wang  */
47530a5678SJason Wang struct vdpa_vq_state_packed {
48530a5678SJason Wang 	u16	last_avail_counter:1;
49530a5678SJason Wang 	u16	last_avail_idx:15;
50530a5678SJason Wang 	u16	last_used_counter:1;
51530a5678SJason Wang 	u16	last_used_idx:15;
52530a5678SJason Wang };
53530a5678SJason Wang 
54530a5678SJason Wang struct vdpa_vq_state {
55530a5678SJason Wang 	union {
56530a5678SJason Wang 		struct vdpa_vq_state_split split;
57530a5678SJason Wang 		struct vdpa_vq_state_packed packed;
58530a5678SJason Wang 	};
59530a5678SJason Wang };
60530a5678SJason Wang 
6133b34750SParav Pandit struct vdpa_mgmt_dev;
6233b34750SParav Pandit 
63aac50c0bSEli Cohen /**
64d0f9164eSParav Pandit  * struct vdpa_device - representation of a vDPA device
65961e9c84SJason Wang  * @dev: underlying device
66961e9c84SJason Wang  * @dma_dev: the actual device that is performing DMA
67*240bf4e6SKrzysztof Kozlowski  * @driver_override: driver name to force a match; do not set directly,
68*240bf4e6SKrzysztof Kozlowski  *                   because core frees it; use driver_set_override() to
69*240bf4e6SKrzysztof Kozlowski  *                   set or clear it.
70961e9c84SJason Wang  * @config: the configuration ops for this device.
71ad69dd0bSParav Pandit  * @cf_mutex: Protects get and set access to configuration layout.
72961e9c84SJason Wang  * @index: device index
73452639a6SMichael S. Tsirkin  * @features_valid: were features initialized? for legacy guests
74d8945ec4SXie Yongji  * @use_va: indicate whether virtual address must be used by this device
75476c135eSParav Pandit  * @nvqs: maximum number of supported virtqueues
76903f7bcaSParav Pandit  * @mdev: management device pointer; caller must setup when registering device as part
77903f7bcaSParav Pandit  *	  of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
78961e9c84SJason Wang  */
79961e9c84SJason Wang struct vdpa_device {
80961e9c84SJason Wang 	struct device dev;
81961e9c84SJason Wang 	struct device *dma_dev;
82539fec78SStefano Garzarella 	const char *driver_override;
83961e9c84SJason Wang 	const struct vdpa_config_ops *config;
84ad69dd0bSParav Pandit 	struct mutex cf_mutex; /* Protects get/set config */
85961e9c84SJason Wang 	unsigned int index;
86452639a6SMichael S. Tsirkin 	bool features_valid;
87d8945ec4SXie Yongji 	bool use_va;
8881d46d69SLongpeng 	u32 nvqs;
89903f7bcaSParav Pandit 	struct vdpa_mgmt_dev *mdev;
90961e9c84SJason Wang };
91961e9c84SJason Wang 
92961e9c84SJason Wang /**
93d0f9164eSParav Pandit  * struct vdpa_iova_range - the IOVA range support by the device
943f1b623aSJason Wang  * @first: start of the IOVA range
953f1b623aSJason Wang  * @last: end of the IOVA range
963f1b623aSJason Wang  */
973f1b623aSJason Wang struct vdpa_iova_range {
983f1b623aSJason Wang 	u64 first;
993f1b623aSJason Wang 	u64 last;
1003f1b623aSJason Wang };
1013f1b623aSJason Wang 
102d8ca2fa5SParav Pandit struct vdpa_dev_set_config {
103d8ca2fa5SParav Pandit 	struct {
104d8ca2fa5SParav Pandit 		u8 mac[ETH_ALEN];
105d8ca2fa5SParav Pandit 		u16 mtu;
106aba21affSEli Cohen 		u16 max_vq_pairs;
107d8ca2fa5SParav Pandit 	} net;
108d8ca2fa5SParav Pandit 	u64 mask;
109d8ca2fa5SParav Pandit };
110d8ca2fa5SParav Pandit 
1113f1b623aSJason Wang /**
112d8945ec4SXie Yongji  * Corresponding file area for device memory mapping
113d8945ec4SXie Yongji  * @file: vma->vm_file for the mapping
114d8945ec4SXie Yongji  * @offset: mapping offset in the vm_file
115d8945ec4SXie Yongji  */
116d8945ec4SXie Yongji struct vdpa_map_file {
117d8945ec4SXie Yongji 	struct file *file;
118d8945ec4SXie Yongji 	u64 offset;
119d8945ec4SXie Yongji };
120d8945ec4SXie Yongji 
121d8945ec4SXie Yongji /**
122d0f9164eSParav Pandit  * struct vdpa_config_ops - operations for configuring a vDPA device.
123961e9c84SJason Wang  * Note: vDPA device drivers are required to implement all of the
124961e9c84SJason Wang  * operations unless it is mentioned to be optional in the following
125961e9c84SJason Wang  * list.
126961e9c84SJason Wang  *
127961e9c84SJason Wang  * @set_vq_address:		Set the address of virtqueue
128961e9c84SJason Wang  *				@vdev: vdpa device
129961e9c84SJason Wang  *				@idx: virtqueue index
130961e9c84SJason Wang  *				@desc_area: address of desc area
131961e9c84SJason Wang  *				@driver_area: address of driver area
132961e9c84SJason Wang  *				@device_area: address of device area
133961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
134961e9c84SJason Wang  * @set_vq_num:			Set the size of virtqueue
135961e9c84SJason Wang  *				@vdev: vdpa device
136961e9c84SJason Wang  *				@idx: virtqueue index
137961e9c84SJason Wang  *				@num: the size of virtqueue
138961e9c84SJason Wang  * @kick_vq:			Kick the virtqueue
139961e9c84SJason Wang  *				@vdev: vdpa device
140961e9c84SJason Wang  *				@idx: virtqueue index
141961e9c84SJason Wang  * @set_vq_cb:			Set the interrupt callback function for
142961e9c84SJason Wang  *				a virtqueue
143961e9c84SJason Wang  *				@vdev: vdpa device
144961e9c84SJason Wang  *				@idx: virtqueue index
145961e9c84SJason Wang  *				@cb: virtio-vdev interrupt callback structure
146961e9c84SJason Wang  * @set_vq_ready:		Set ready status for a virtqueue
147961e9c84SJason Wang  *				@vdev: vdpa device
148961e9c84SJason Wang  *				@idx: virtqueue index
149961e9c84SJason Wang  *				@ready: ready (true) not ready(false)
150961e9c84SJason Wang  * @get_vq_ready:		Get ready status for a virtqueue
151961e9c84SJason Wang  *				@vdev: vdpa device
152961e9c84SJason Wang  *				@idx: virtqueue index
153961e9c84SJason Wang  *				Returns boolean: ready (true) or not (false)
154961e9c84SJason Wang  * @set_vq_state:		Set the state for a virtqueue
155961e9c84SJason Wang  *				@vdev: vdpa device
156961e9c84SJason Wang  *				@idx: virtqueue index
157aac50c0bSEli Cohen  *				@state: pointer to set virtqueue state (last_avail_idx)
158961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
159961e9c84SJason Wang  * @get_vq_state:		Get the state for a virtqueue
160961e9c84SJason Wang  *				@vdev: vdpa device
161961e9c84SJason Wang  *				@idx: virtqueue index
162aac50c0bSEli Cohen  *				@state: pointer to returned state (last_avail_idx)
16328cc408bSEugenio Pérez  * @get_vq_notification:	Get the notification area for a virtqueue (optional)
164c25a26e6SJason Wang  *				@vdev: vdpa device
165c25a26e6SJason Wang  *				@idx: virtqueue index
166c25a26e6SJason Wang  *				Returns the notifcation area
1677164675aSZhu Lingshan  * @get_vq_irq:			Get the irq number of a virtqueue (optional,
1687164675aSZhu Lingshan  *				but must implemented if require vq irq offloading)
1697164675aSZhu Lingshan  *				@vdev: vdpa device
1707164675aSZhu Lingshan  *				@idx: virtqueue index
1717164675aSZhu Lingshan  *				Returns int: irq number of a virtqueue,
1727164675aSZhu Lingshan  *				negative number if no irq assigned.
173961e9c84SJason Wang  * @get_vq_align:		Get the virtqueue align requirement
174961e9c84SJason Wang  *				for the device
175961e9c84SJason Wang  *				@vdev: vdpa device
176961e9c84SJason Wang  *				Returns virtqueue algin requirement
177a64917bcSEli Cohen  * @get_device_features:	Get virtio features supported by the device
178961e9c84SJason Wang  *				@vdev: vdpa device
179961e9c84SJason Wang  *				Returns the virtio features support by the
180961e9c84SJason Wang  *				device
181a64917bcSEli Cohen  * @set_driver_features:	Set virtio features supported by the driver
182961e9c84SJason Wang  *				@vdev: vdpa device
183961e9c84SJason Wang  *				@features: feature support by the driver
184961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
185a64917bcSEli Cohen  * @get_driver_features:	Get the virtio driver features in action
186a64917bcSEli Cohen  *				@vdev: vdpa device
187a64917bcSEli Cohen  *				Returns the virtio features accepted
188961e9c84SJason Wang  * @set_config_cb:		Set the config interrupt callback
189961e9c84SJason Wang  *				@vdev: vdpa device
190961e9c84SJason Wang  *				@cb: virtio-vdev interrupt callback structure
191961e9c84SJason Wang  * @get_vq_num_max:		Get the max size of virtqueue
192961e9c84SJason Wang  *				@vdev: vdpa device
193961e9c84SJason Wang  *				Returns u16: max size of virtqueue
1943b970a58SWu Zongyong  * @get_vq_num_min:		Get the min size of virtqueue (optional)
1953b970a58SWu Zongyong  *				@vdev: vdpa device
1963b970a58SWu Zongyong  *				Returns u16: min size of virtqueue
197961e9c84SJason Wang  * @get_device_id:		Get virtio device id
198961e9c84SJason Wang  *				@vdev: vdpa device
199961e9c84SJason Wang  *				Returns u32: virtio device id
200961e9c84SJason Wang  * @get_vendor_id:		Get id for the vendor that provides this device
201961e9c84SJason Wang  *				@vdev: vdpa device
202961e9c84SJason Wang  *				Returns u32: virtio vendor id
203961e9c84SJason Wang  * @get_status:			Get the device status
204961e9c84SJason Wang  *				@vdev: vdpa device
205961e9c84SJason Wang  *				Returns u8: virtio device status
206961e9c84SJason Wang  * @set_status:			Set the device status
207961e9c84SJason Wang  *				@vdev: vdpa device
208961e9c84SJason Wang  *				@status: virtio device status
2090686082dSXie Yongji  * @reset:			Reset device
2100686082dSXie Yongji  *				@vdev: vdpa device
2110686082dSXie Yongji  *				Returns integer: success (0) or error (< 0)
212a61280ddSLongpeng  * @get_config_size:		Get the size of the configuration space includes
213a61280ddSLongpeng  *				fields that are conditional on feature bits.
214442706f9SStefano Garzarella  *				@vdev: vdpa device
215442706f9SStefano Garzarella  *				Returns size_t: configuration size
216961e9c84SJason Wang  * @get_config:			Read from device specific configuration space
217961e9c84SJason Wang  *				@vdev: vdpa device
218961e9c84SJason Wang  *				@offset: offset from the beginning of
219961e9c84SJason Wang  *				configuration space
220961e9c84SJason Wang  *				@buf: buffer used to read to
221961e9c84SJason Wang  *				@len: the length to read from
222961e9c84SJason Wang  *				configuration space
223961e9c84SJason Wang  * @set_config:			Write to device specific configuration space
224961e9c84SJason Wang  *				@vdev: vdpa device
225961e9c84SJason Wang  *				@offset: offset from the beginning of
226961e9c84SJason Wang  *				configuration space
227961e9c84SJason Wang  *				@buf: buffer used to write from
228961e9c84SJason Wang  *				@len: the length to write to
229961e9c84SJason Wang  *				configuration space
230961e9c84SJason Wang  * @get_generation:		Get device config generation (optional)
231961e9c84SJason Wang  *				@vdev: vdpa device
232961e9c84SJason Wang  *				Returns u32: device generation
2333f1b623aSJason Wang  * @get_iova_range:		Get supported iova range (optional)
2343f1b623aSJason Wang  *				@vdev: vdpa device
2353f1b623aSJason Wang  *				Returns the iova range supported by
2363f1b623aSJason Wang  *				the device.
237961e9c84SJason Wang  * @set_map:			Set device memory mapping (optional)
238961e9c84SJason Wang  *				Needed for device that using device
239961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
240961e9c84SJason Wang  *				@vdev: vdpa device
241961e9c84SJason Wang  *				@iotlb: vhost memory mapping to be
242961e9c84SJason Wang  *				used by the vDPA
243961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
244961e9c84SJason Wang  * @dma_map:			Map an area of PA to IOVA (optional)
245961e9c84SJason Wang  *				Needed for device that using device
246961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
247961e9c84SJason Wang  *				and preferring incremental map.
248961e9c84SJason Wang  *				@vdev: vdpa device
249961e9c84SJason Wang  *				@iova: iova to be mapped
250961e9c84SJason Wang  *				@size: size of the area
251961e9c84SJason Wang  *				@pa: physical address for the map
252961e9c84SJason Wang  *				@perm: device access permission (VHOST_MAP_XX)
253961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
254961e9c84SJason Wang  * @dma_unmap:			Unmap an area of IOVA (optional but
255961e9c84SJason Wang  *				must be implemented with dma_map)
256961e9c84SJason Wang  *				Needed for device that using device
257961e9c84SJason Wang  *				specific DMA translation (on-chip IOMMU)
258961e9c84SJason Wang  *				and preferring incremental unmap.
259961e9c84SJason Wang  *				@vdev: vdpa device
260961e9c84SJason Wang  *				@iova: iova to be unmapped
261961e9c84SJason Wang  *				@size: size of the area
262961e9c84SJason Wang  *				Returns integer: success (0) or error (< 0)
263961e9c84SJason Wang  * @free:			Free resources that belongs to vDPA (optional)
264961e9c84SJason Wang  *				@vdev: vdpa device
265961e9c84SJason Wang  */
266961e9c84SJason Wang struct vdpa_config_ops {
267961e9c84SJason Wang 	/* Virtqueue ops */
268961e9c84SJason Wang 	int (*set_vq_address)(struct vdpa_device *vdev,
269961e9c84SJason Wang 			      u16 idx, u64 desc_area, u64 driver_area,
270961e9c84SJason Wang 			      u64 device_area);
271961e9c84SJason Wang 	void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
272961e9c84SJason Wang 	void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
273961e9c84SJason Wang 	void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
274961e9c84SJason Wang 			  struct vdpa_callback *cb);
275961e9c84SJason Wang 	void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
276961e9c84SJason Wang 	bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
277aac50c0bSEli Cohen 	int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
278aac50c0bSEli Cohen 			    const struct vdpa_vq_state *state);
27923750e39SEli Cohen 	int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
280aac50c0bSEli Cohen 			    struct vdpa_vq_state *state);
281c25a26e6SJason Wang 	struct vdpa_notification_area
282c25a26e6SJason Wang 	(*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
2834c05433bSZhu Lingshan 	/* vq irq is not expected to be changed once DRIVER_OK is set */
284d0ae1fbfSWu Zongyong 	int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
285961e9c84SJason Wang 
286961e9c84SJason Wang 	/* Device ops */
287425a5070SMichael S. Tsirkin 	u32 (*get_vq_align)(struct vdpa_device *vdev);
288a64917bcSEli Cohen 	u64 (*get_device_features)(struct vdpa_device *vdev);
289a64917bcSEli Cohen 	int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
290a64917bcSEli Cohen 	u64 (*get_driver_features)(struct vdpa_device *vdev);
291961e9c84SJason Wang 	void (*set_config_cb)(struct vdpa_device *vdev,
292961e9c84SJason Wang 			      struct vdpa_callback *cb);
293961e9c84SJason Wang 	u16 (*get_vq_num_max)(struct vdpa_device *vdev);
2943b970a58SWu Zongyong 	u16 (*get_vq_num_min)(struct vdpa_device *vdev);
295961e9c84SJason Wang 	u32 (*get_device_id)(struct vdpa_device *vdev);
296961e9c84SJason Wang 	u32 (*get_vendor_id)(struct vdpa_device *vdev);
297961e9c84SJason Wang 	u8 (*get_status)(struct vdpa_device *vdev);
298961e9c84SJason Wang 	void (*set_status)(struct vdpa_device *vdev, u8 status);
2990686082dSXie Yongji 	int (*reset)(struct vdpa_device *vdev);
300442706f9SStefano Garzarella 	size_t (*get_config_size)(struct vdpa_device *vdev);
301961e9c84SJason Wang 	void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
302961e9c84SJason Wang 			   void *buf, unsigned int len);
303961e9c84SJason Wang 	void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
304961e9c84SJason Wang 			   const void *buf, unsigned int len);
305961e9c84SJason Wang 	u32 (*get_generation)(struct vdpa_device *vdev);
3063f1b623aSJason Wang 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
307961e9c84SJason Wang 
308961e9c84SJason Wang 	/* DMA ops */
309961e9c84SJason Wang 	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
310961e9c84SJason Wang 	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
311c10fb945SXie Yongji 		       u64 pa, u32 perm, void *opaque);
312961e9c84SJason Wang 	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
313961e9c84SJason Wang 
314961e9c84SJason Wang 	/* Free device resources */
315961e9c84SJason Wang 	void (*free)(struct vdpa_device *vdev);
316961e9c84SJason Wang };
317961e9c84SJason Wang 
318961e9c84SJason Wang struct vdpa_device *__vdpa_alloc_device(struct device *parent,
319961e9c84SJason Wang 					const struct vdpa_config_ops *config,
320d8945ec4SXie Yongji 					size_t size, const char *name,
321d8945ec4SXie Yongji 					bool use_va);
322961e9c84SJason Wang 
323c8d182bdSXie Yongji /**
324c8d182bdSXie Yongji  * vdpa_alloc_device - allocate and initilaize a vDPA device
325c8d182bdSXie Yongji  *
326c8d182bdSXie Yongji  * @dev_struct: the type of the parent structure
327c8d182bdSXie Yongji  * @member: the name of struct vdpa_device within the @dev_struct
328c8d182bdSXie Yongji  * @parent: the parent device
329c8d182bdSXie Yongji  * @config: the bus operations that is supported by this device
330c8d182bdSXie Yongji  * @name: name of the vdpa device
331d8945ec4SXie Yongji  * @use_va: indicate whether virtual address must be used by this device
332c8d182bdSXie Yongji  *
333c8d182bdSXie Yongji  * Return allocated data structure or ERR_PTR upon error
334c8d182bdSXie Yongji  */
335d8945ec4SXie Yongji #define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va)   \
336961e9c84SJason Wang 			  container_of(__vdpa_alloc_device( \
337f00bdce0SJason Wang 				       parent, config, \
338961e9c84SJason Wang 				       sizeof(dev_struct) + \
339961e9c84SJason Wang 				       BUILD_BUG_ON_ZERO(offsetof( \
340d8945ec4SXie Yongji 				       dev_struct, member)), name, use_va), \
341961e9c84SJason Wang 				       dev_struct, member)
342961e9c84SJason Wang 
34381d46d69SLongpeng int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
344961e9c84SJason Wang void vdpa_unregister_device(struct vdpa_device *vdev);
345961e9c84SJason Wang 
34681d46d69SLongpeng int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
347903f7bcaSParav Pandit void _vdpa_unregister_device(struct vdpa_device *vdev);
348903f7bcaSParav Pandit 
349961e9c84SJason Wang /**
350d0f9164eSParav Pandit  * struct vdpa_driver - operations for a vDPA driver
351961e9c84SJason Wang  * @driver: underlying device driver
352961e9c84SJason Wang  * @probe: the function to call when a device is found.  Returns 0 or -errno.
353961e9c84SJason Wang  * @remove: the function to call when a device is removed.
354961e9c84SJason Wang  */
355961e9c84SJason Wang struct vdpa_driver {
356961e9c84SJason Wang 	struct device_driver driver;
357961e9c84SJason Wang 	int (*probe)(struct vdpa_device *vdev);
358961e9c84SJason Wang 	void (*remove)(struct vdpa_device *vdev);
359961e9c84SJason Wang };
360961e9c84SJason Wang 
361961e9c84SJason Wang #define vdpa_register_driver(drv) \
362961e9c84SJason Wang 	__vdpa_register_driver(drv, THIS_MODULE)
363961e9c84SJason Wang int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
364961e9c84SJason Wang void vdpa_unregister_driver(struct vdpa_driver *drv);
365961e9c84SJason Wang 
366961e9c84SJason Wang #define module_vdpa_driver(__vdpa_driver) \
367961e9c84SJason Wang 	module_driver(__vdpa_driver, vdpa_register_driver,	\
368961e9c84SJason Wang 		      vdpa_unregister_driver)
369961e9c84SJason Wang 
370961e9c84SJason Wang static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
371961e9c84SJason Wang {
372961e9c84SJason Wang 	return container_of(driver, struct vdpa_driver, driver);
373961e9c84SJason Wang }
374961e9c84SJason Wang 
375961e9c84SJason Wang static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
376961e9c84SJason Wang {
377961e9c84SJason Wang 	return container_of(_dev, struct vdpa_device, dev);
378961e9c84SJason Wang }
379961e9c84SJason Wang 
380961e9c84SJason Wang static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
381961e9c84SJason Wang {
382961e9c84SJason Wang 	return dev_get_drvdata(&vdev->dev);
383961e9c84SJason Wang }
384961e9c84SJason Wang 
385961e9c84SJason Wang static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
386961e9c84SJason Wang {
387961e9c84SJason Wang 	dev_set_drvdata(&vdev->dev, data);
388961e9c84SJason Wang }
389961e9c84SJason Wang 
390961e9c84SJason Wang static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
391961e9c84SJason Wang {
392961e9c84SJason Wang 	return vdev->dma_dev;
393961e9c84SJason Wang }
394452639a6SMichael S. Tsirkin 
3950686082dSXie Yongji static inline int vdpa_reset(struct vdpa_device *vdev)
396452639a6SMichael S. Tsirkin {
397452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
398aba21affSEli Cohen 	int ret;
399452639a6SMichael S. Tsirkin 
400aba21affSEli Cohen 	mutex_lock(&vdev->cf_mutex);
401452639a6SMichael S. Tsirkin 	vdev->features_valid = false;
402aba21affSEli Cohen 	ret = ops->reset(vdev);
403aba21affSEli Cohen 	mutex_unlock(&vdev->cf_mutex);
404aba21affSEli Cohen 	return ret;
405452639a6SMichael S. Tsirkin }
406452639a6SMichael S. Tsirkin 
407e0077cc1SSi-Wei Liu static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
408452639a6SMichael S. Tsirkin {
409452639a6SMichael S. Tsirkin 	const struct vdpa_config_ops *ops = vdev->config;
410aba21affSEli Cohen 	int ret;
411aba21affSEli Cohen 
412452639a6SMichael S. Tsirkin 	vdev->features_valid = true;
413aba21affSEli Cohen 	ret = ops->set_driver_features(vdev, features);
414e0077cc1SSi-Wei Liu 
415e0077cc1SSi-Wei Liu 	return ret;
416e0077cc1SSi-Wei Liu }
417e0077cc1SSi-Wei Liu 
418e0077cc1SSi-Wei Liu static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
419e0077cc1SSi-Wei Liu {
420e0077cc1SSi-Wei Liu 	int ret;
421e0077cc1SSi-Wei Liu 
422e0077cc1SSi-Wei Liu 	mutex_lock(&vdev->cf_mutex);
423e0077cc1SSi-Wei Liu 	ret = vdpa_set_features_unlocked(vdev, features);
424aba21affSEli Cohen 	mutex_unlock(&vdev->cf_mutex);
425aba21affSEli Cohen 
426aba21affSEli Cohen 	return ret;
427452639a6SMichael S. Tsirkin }
428452639a6SMichael S. Tsirkin 
4296dbb1f16SParav Pandit void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
4306dbb1f16SParav Pandit 		     void *buf, unsigned int len);
4316dbb1f16SParav Pandit void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
4326dbb1f16SParav Pandit 		     const void *buf, unsigned int length);
43373bc0dbbSEli Cohen void vdpa_set_status(struct vdpa_device *vdev, u8 status);
43473bc0dbbSEli Cohen 
43533b34750SParav Pandit /**
436d0f9164eSParav Pandit  * struct vdpa_mgmtdev_ops - vdpa device ops
43733b34750SParav Pandit  * @dev_add: Add a vdpa device using alloc and register
43833b34750SParav Pandit  *	     @mdev: parent device to use for device addition
43933b34750SParav Pandit  *	     @name: name of the new vdpa device
440d8ca2fa5SParav Pandit  *	     @config: config attributes to apply to the device under creation
44133b34750SParav Pandit  *	     Driver need to add a new device using _vdpa_register_device()
44233b34750SParav Pandit  *	     after fully initializing the vdpa device. Driver must return 0
44333b34750SParav Pandit  *	     on success or appropriate error code.
44433b34750SParav Pandit  * @dev_del: Remove a vdpa device using unregister
44533b34750SParav Pandit  *	     @mdev: parent device to use for device removal
44633b34750SParav Pandit  *	     @dev: vdpa device to remove
44733b34750SParav Pandit  *	     Driver need to remove the specified device by calling
44833b34750SParav Pandit  *	     _vdpa_unregister_device().
44933b34750SParav Pandit  */
45033b34750SParav Pandit struct vdpa_mgmtdev_ops {
451d8ca2fa5SParav Pandit 	int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
452d8ca2fa5SParav Pandit 		       const struct vdpa_dev_set_config *config);
45333b34750SParav Pandit 	void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
45433b34750SParav Pandit };
45533b34750SParav Pandit 
456960deb33SParav Pandit /**
457960deb33SParav Pandit  * struct vdpa_mgmt_dev - vdpa management device
458960deb33SParav Pandit  * @device: Management parent device
459960deb33SParav Pandit  * @ops: operations supported by management device
460960deb33SParav Pandit  * @id_table: Pointer to device id table of supported ids
461d8ca2fa5SParav Pandit  * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
462d8ca2fa5SParav Pandit  *		      management device support during dev_add callback
463960deb33SParav Pandit  * @list: list entry
464960deb33SParav Pandit  */
46533b34750SParav Pandit struct vdpa_mgmt_dev {
46633b34750SParav Pandit 	struct device *device;
46733b34750SParav Pandit 	const struct vdpa_mgmtdev_ops *ops;
468960deb33SParav Pandit 	const struct virtio_device_id *id_table;
469d8ca2fa5SParav Pandit 	u64 config_attr_mask;
47033b34750SParav Pandit 	struct list_head list;
471cd2629f6SEli Cohen 	u64 supported_features;
472cd2629f6SEli Cohen 	u32 max_supported_vqs;
47333b34750SParav Pandit };
47433b34750SParav Pandit 
47533b34750SParav Pandit int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
47633b34750SParav Pandit void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
47733b34750SParav Pandit 
478961e9c84SJason Wang #endif /* _LINUX_VDPA_H */
479