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>
10c2475a9aSZhu Lingshan #include <linux/virtio_blk.h>
11d8ca2fa5SParav Pandit #include <linux/if_ether.h>
12961e9c84SJason Wang
13961e9c84SJason Wang /**
149be5d2d4SSimon Horman * struct vdpa_callback - vDPA callback definition.
15961e9c84SJason Wang * @callback: interrupt callback function
16961e9c84SJason Wang * @private: the data passed to the callback function
175e68470fSXie Yongji * @trigger: the eventfd for the callback (Optional).
185e68470fSXie Yongji * When it is set, the vDPA driver must guarantee that
195e68470fSXie Yongji * signaling it is functional equivalent to triggering
205e68470fSXie Yongji * the callback. Then vDPA parent can signal it directly
215e68470fSXie Yongji * instead of triggering the callback.
22961e9c84SJason Wang */
23961e9c84SJason Wang struct vdpa_callback {
24961e9c84SJason Wang irqreturn_t (*callback)(void *data);
25961e9c84SJason Wang void *private;
265e68470fSXie Yongji struct eventfd_ctx *trigger;
27961e9c84SJason Wang };
28961e9c84SJason Wang
29961e9c84SJason Wang /**
30d0f9164eSParav Pandit * struct vdpa_notification_area - vDPA notification area
31c25a26e6SJason Wang * @addr: base address of the notification area
32c25a26e6SJason Wang * @size: size of the notification area
33c25a26e6SJason Wang */
34c25a26e6SJason Wang struct vdpa_notification_area {
35c25a26e6SJason Wang resource_size_t addr;
36c25a26e6SJason Wang resource_size_t size;
37c25a26e6SJason Wang };
38c25a26e6SJason Wang
39c25a26e6SJason Wang /**
40530a5678SJason Wang * struct vdpa_vq_state_split - vDPA split virtqueue state
41aac50c0bSEli Cohen * @avail_index: available index
42aac50c0bSEli Cohen */
43530a5678SJason Wang struct vdpa_vq_state_split {
44aac50c0bSEli Cohen u16 avail_index;
45aac50c0bSEli Cohen };
46aac50c0bSEli Cohen
47530a5678SJason Wang /**
48530a5678SJason Wang * struct vdpa_vq_state_packed - vDPA packed virtqueue state
49530a5678SJason Wang * @last_avail_counter: last driver ring wrap counter observed by device
50530a5678SJason Wang * @last_avail_idx: device available index
51530a5678SJason Wang * @last_used_counter: device ring wrap counter
52530a5678SJason Wang * @last_used_idx: used index
53530a5678SJason Wang */
54530a5678SJason Wang struct vdpa_vq_state_packed {
55530a5678SJason Wang u16 last_avail_counter:1;
56530a5678SJason Wang u16 last_avail_idx:15;
57530a5678SJason Wang u16 last_used_counter:1;
58530a5678SJason Wang u16 last_used_idx:15;
59530a5678SJason Wang };
60530a5678SJason Wang
61530a5678SJason Wang struct vdpa_vq_state {
62530a5678SJason Wang union {
63530a5678SJason Wang struct vdpa_vq_state_split split;
64530a5678SJason Wang struct vdpa_vq_state_packed packed;
65530a5678SJason Wang };
66530a5678SJason Wang };
67530a5678SJason Wang
6833b34750SParav Pandit struct vdpa_mgmt_dev;
6933b34750SParav Pandit
70aac50c0bSEli Cohen /**
71d0f9164eSParav Pandit * struct vdpa_device - representation of a vDPA device
72961e9c84SJason Wang * @dev: underlying device
73961e9c84SJason Wang * @dma_dev: the actual device that is performing DMA
74240bf4e6SKrzysztof Kozlowski * @driver_override: driver name to force a match; do not set directly,
75240bf4e6SKrzysztof Kozlowski * because core frees it; use driver_set_override() to
76240bf4e6SKrzysztof Kozlowski * set or clear it.
77961e9c84SJason Wang * @config: the configuration ops for this device.
78a6a51adcSEli Cohen * @cf_lock: Protects get and set access to configuration layout.
79961e9c84SJason Wang * @index: device index
80452639a6SMichael S. Tsirkin * @features_valid: were features initialized? for legacy guests
81db9adcbfSGautam Dawar * @ngroups: the number of virtqueue groups
82db9adcbfSGautam Dawar * @nas: the number of address spaces
83d8945ec4SXie Yongji * @use_va: indicate whether virtual address must be used by this device
84476c135eSParav Pandit * @nvqs: maximum number of supported virtqueues
85903f7bcaSParav Pandit * @mdev: management device pointer; caller must setup when registering device as part
86903f7bcaSParav Pandit * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
87961e9c84SJason Wang */
88961e9c84SJason Wang struct vdpa_device {
89961e9c84SJason Wang struct device dev;
90961e9c84SJason Wang struct device *dma_dev;
91539fec78SStefano Garzarella const char *driver_override;
92961e9c84SJason Wang const struct vdpa_config_ops *config;
93a6a51adcSEli Cohen struct rw_semaphore cf_lock; /* Protects get/set config */
94961e9c84SJason Wang unsigned int index;
95452639a6SMichael S. Tsirkin bool features_valid;
96d8945ec4SXie Yongji bool use_va;
9781d46d69SLongpeng u32 nvqs;
98903f7bcaSParav Pandit struct vdpa_mgmt_dev *mdev;
99d4821902SGautam Dawar unsigned int ngroups;
100db9adcbfSGautam Dawar unsigned int nas;
101961e9c84SJason Wang };
102961e9c84SJason Wang
103961e9c84SJason Wang /**
104d0f9164eSParav Pandit * struct vdpa_iova_range - the IOVA range support by the device
1053f1b623aSJason Wang * @first: start of the IOVA range
1063f1b623aSJason Wang * @last: end of the IOVA range
1073f1b623aSJason Wang */
1083f1b623aSJason Wang struct vdpa_iova_range {
1093f1b623aSJason Wang u64 first;
1103f1b623aSJason Wang u64 last;
1113f1b623aSJason Wang };
1123f1b623aSJason Wang
113d8ca2fa5SParav Pandit struct vdpa_dev_set_config {
11490fea5a8SJason Wang u64 device_features;
115d8ca2fa5SParav Pandit struct {
116d8ca2fa5SParav Pandit u8 mac[ETH_ALEN];
117d8ca2fa5SParav Pandit u16 mtu;
118aba21affSEli Cohen u16 max_vq_pairs;
119d8ca2fa5SParav Pandit } net;
120d8ca2fa5SParav Pandit u64 mask;
121d8ca2fa5SParav Pandit };
122d8ca2fa5SParav Pandit
1233f1b623aSJason Wang /**
1249be5d2d4SSimon Horman * struct vdpa_map_file - file area for device memory mapping
125d8945ec4SXie Yongji * @file: vma->vm_file for the mapping
126d8945ec4SXie Yongji * @offset: mapping offset in the vm_file
127d8945ec4SXie Yongji */
128d8945ec4SXie Yongji struct vdpa_map_file {
129d8945ec4SXie Yongji struct file *file;
130d8945ec4SXie Yongji u64 offset;
131d8945ec4SXie Yongji };
132d8945ec4SXie Yongji
133d8945ec4SXie Yongji /**
134d0f9164eSParav Pandit * struct vdpa_config_ops - operations for configuring a vDPA device.
135961e9c84SJason Wang * Note: vDPA device drivers are required to implement all of the
136961e9c84SJason Wang * operations unless it is mentioned to be optional in the following
137961e9c84SJason Wang * list.
138961e9c84SJason Wang *
139961e9c84SJason Wang * @set_vq_address: Set the address of virtqueue
140961e9c84SJason Wang * @vdev: vdpa device
141961e9c84SJason Wang * @idx: virtqueue index
142961e9c84SJason Wang * @desc_area: address of desc area
143961e9c84SJason Wang * @driver_area: address of driver area
144961e9c84SJason Wang * @device_area: address of device area
145961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
146961e9c84SJason Wang * @set_vq_num: Set the size of virtqueue
147961e9c84SJason Wang * @vdev: vdpa device
148961e9c84SJason Wang * @idx: virtqueue index
149961e9c84SJason Wang * @num: the size of virtqueue
150961e9c84SJason Wang * @kick_vq: Kick the virtqueue
151961e9c84SJason Wang * @vdev: vdpa device
152961e9c84SJason Wang * @idx: virtqueue index
1532c4e4a22SAlvaro Karsz * @kick_vq_with_data: Kick the virtqueue and supply extra data
1542c4e4a22SAlvaro Karsz * (only if VIRTIO_F_NOTIFICATION_DATA is negotiated)
1552c4e4a22SAlvaro Karsz * @vdev: vdpa device
1562c4e4a22SAlvaro Karsz * @data for split virtqueue:
1572c4e4a22SAlvaro Karsz * 16 bits vqn and 16 bits next available index.
1582c4e4a22SAlvaro Karsz * @data for packed virtqueue:
1592c4e4a22SAlvaro Karsz * 16 bits vqn, 15 least significant bits of
1602c4e4a22SAlvaro Karsz * next available index and 1 bit next_wrap.
161961e9c84SJason Wang * @set_vq_cb: Set the interrupt callback function for
162961e9c84SJason Wang * a virtqueue
163961e9c84SJason Wang * @vdev: vdpa device
164961e9c84SJason Wang * @idx: virtqueue index
165961e9c84SJason Wang * @cb: virtio-vdev interrupt callback structure
166961e9c84SJason Wang * @set_vq_ready: Set ready status for a virtqueue
167961e9c84SJason Wang * @vdev: vdpa device
168961e9c84SJason Wang * @idx: virtqueue index
169961e9c84SJason Wang * @ready: ready (true) not ready(false)
170961e9c84SJason Wang * @get_vq_ready: Get ready status for a virtqueue
171961e9c84SJason Wang * @vdev: vdpa device
172961e9c84SJason Wang * @idx: virtqueue index
173961e9c84SJason Wang * Returns boolean: ready (true) or not (false)
174961e9c84SJason Wang * @set_vq_state: Set the state for a virtqueue
175961e9c84SJason Wang * @vdev: vdpa device
176961e9c84SJason Wang * @idx: virtqueue index
177aac50c0bSEli Cohen * @state: pointer to set virtqueue state (last_avail_idx)
178961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
179961e9c84SJason Wang * @get_vq_state: Get the state for a virtqueue
180961e9c84SJason Wang * @vdev: vdpa device
181961e9c84SJason Wang * @idx: virtqueue index
182aac50c0bSEli Cohen * @state: pointer to returned state (last_avail_idx)
1839be5d2d4SSimon Horman * @get_vendor_vq_stats: Get the vendor statistics of a device.
1849be5d2d4SSimon Horman * @vdev: vdpa device
1859be5d2d4SSimon Horman * @idx: virtqueue index
1869be5d2d4SSimon Horman * @msg: socket buffer holding stats message
1879be5d2d4SSimon Horman * @extack: extack for reporting error messages
1889be5d2d4SSimon Horman * Returns integer: success (0) or error (< 0)
18928cc408bSEugenio Pérez * @get_vq_notification: Get the notification area for a virtqueue (optional)
190c25a26e6SJason Wang * @vdev: vdpa device
191c25a26e6SJason Wang * @idx: virtqueue index
1929be5d2d4SSimon Horman * Returns the notification area
1937164675aSZhu Lingshan * @get_vq_irq: Get the irq number of a virtqueue (optional,
1947164675aSZhu Lingshan * but must implemented if require vq irq offloading)
1957164675aSZhu Lingshan * @vdev: vdpa device
1967164675aSZhu Lingshan * @idx: virtqueue index
1977164675aSZhu Lingshan * Returns int: irq number of a virtqueue,
1987164675aSZhu Lingshan * negative number if no irq assigned.
1990a926fc9SZhu Lingshan * @get_vq_size: Get the size of a specific virtqueue (optional)
2000a926fc9SZhu Lingshan * @vdev: vdpa device
2010a926fc9SZhu Lingshan * @idx: virtqueue index
2020a926fc9SZhu Lingshan * Return u16: the size of the virtqueue
203961e9c84SJason Wang * @get_vq_align: Get the virtqueue align requirement
204961e9c84SJason Wang * for the device
205961e9c84SJason Wang * @vdev: vdpa device
206961e9c84SJason Wang * Returns virtqueue algin requirement
20700d1f546SJason Wang * @get_vq_group: Get the group id for a specific
20800d1f546SJason Wang * virtqueue (optional)
209d4821902SGautam Dawar * @vdev: vdpa device
210d4821902SGautam Dawar * @idx: virtqueue index
211d4821902SGautam Dawar * Returns u32: group id for this virtqueue
212a72cac60SSi-Wei Liu * @get_vq_desc_group: Get the group id for the descriptor table of
213a72cac60SSi-Wei Liu * a specific virtqueue (optional)
214a72cac60SSi-Wei Liu * @vdev: vdpa device
215a72cac60SSi-Wei Liu * @idx: virtqueue index
216a72cac60SSi-Wei Liu * Returns u32: group id for the descriptor table
217a72cac60SSi-Wei Liu * portion of this virtqueue. Could be different
218a72cac60SSi-Wei Liu * than the one from @get_vq_group, in which case
219a72cac60SSi-Wei Liu * the access to the descriptor table can be
220a72cac60SSi-Wei Liu * confined to a separate asid, isolating from
221a72cac60SSi-Wei Liu * the virtqueue's buffer address access.
222a64917bcSEli Cohen * @get_device_features: Get virtio features supported by the device
223961e9c84SJason Wang * @vdev: vdpa device
224961e9c84SJason Wang * Returns the virtio features support by the
225961e9c84SJason Wang * device
226b63e5c70SEugenio Pérez * @get_backend_features: Get parent-specific backend features (optional)
227b63e5c70SEugenio Pérez * Returns the vdpa features supported by the
228b63e5c70SEugenio Pérez * device.
229a64917bcSEli Cohen * @set_driver_features: Set virtio features supported by the driver
230961e9c84SJason Wang * @vdev: vdpa device
231961e9c84SJason Wang * @features: feature support by the driver
232961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
233a64917bcSEli Cohen * @get_driver_features: Get the virtio driver features in action
234a64917bcSEli Cohen * @vdev: vdpa device
235a64917bcSEli Cohen * Returns the virtio features accepted
236961e9c84SJason Wang * @set_config_cb: Set the config interrupt callback
237961e9c84SJason Wang * @vdev: vdpa device
238961e9c84SJason Wang * @cb: virtio-vdev interrupt callback structure
239961e9c84SJason Wang * @get_vq_num_max: Get the max size of virtqueue
240961e9c84SJason Wang * @vdev: vdpa device
241961e9c84SJason Wang * Returns u16: max size of virtqueue
2423b970a58SWu Zongyong * @get_vq_num_min: Get the min size of virtqueue (optional)
2433b970a58SWu Zongyong * @vdev: vdpa device
2443b970a58SWu Zongyong * Returns u16: min size of virtqueue
245961e9c84SJason Wang * @get_device_id: Get virtio device id
246961e9c84SJason Wang * @vdev: vdpa device
247961e9c84SJason Wang * Returns u32: virtio device id
248961e9c84SJason Wang * @get_vendor_id: Get id for the vendor that provides this device
249961e9c84SJason Wang * @vdev: vdpa device
250961e9c84SJason Wang * Returns u32: virtio vendor id
251961e9c84SJason Wang * @get_status: Get the device status
252961e9c84SJason Wang * @vdev: vdpa device
253961e9c84SJason Wang * Returns u8: virtio device status
254961e9c84SJason Wang * @set_status: Set the device status
255961e9c84SJason Wang * @vdev: vdpa device
256961e9c84SJason Wang * @status: virtio device status
2570686082dSXie Yongji * @reset: Reset device
2580686082dSXie Yongji * @vdev: vdpa device
2590686082dSXie Yongji * Returns integer: success (0) or error (< 0)
260a26f2e4eSSi-Wei Liu * @compat_reset: Reset device with compatibility quirks to
261a26f2e4eSSi-Wei Liu * accommodate older userspace. Only needed by
262a26f2e4eSSi-Wei Liu * parent driver which used to have bogus reset
263a26f2e4eSSi-Wei Liu * behaviour, and has to maintain such behaviour
264a26f2e4eSSi-Wei Liu * for compatibility with older userspace.
265a26f2e4eSSi-Wei Liu * Historically compliant driver only has to
266a26f2e4eSSi-Wei Liu * implement .reset, Historically non-compliant
267a26f2e4eSSi-Wei Liu * driver should implement both.
268a26f2e4eSSi-Wei Liu * @vdev: vdpa device
269a26f2e4eSSi-Wei Liu * @flags: compatibility quirks for reset
270a26f2e4eSSi-Wei Liu * Returns integer: success (0) or error (< 0)
2711538a8a4SSebastien Boeuf * @suspend: Suspend the device (optional)
2721538a8a4SSebastien Boeuf * @vdev: vdpa device
2731538a8a4SSebastien Boeuf * Returns integer: success (0) or error (< 0)
2741538a8a4SSebastien Boeuf * @resume: Resume the device (optional)
275848ecea1SEugenio Pérez * @vdev: vdpa device
276848ecea1SEugenio Pérez * Returns integer: success (0) or error (< 0)
277a61280ddSLongpeng * @get_config_size: Get the size of the configuration space includes
278a61280ddSLongpeng * fields that are conditional on feature bits.
279442706f9SStefano Garzarella * @vdev: vdpa device
280442706f9SStefano Garzarella * Returns size_t: configuration size
281961e9c84SJason Wang * @get_config: Read from device specific configuration space
282961e9c84SJason Wang * @vdev: vdpa device
283961e9c84SJason Wang * @offset: offset from the beginning of
284961e9c84SJason Wang * configuration space
285961e9c84SJason Wang * @buf: buffer used to read to
286961e9c84SJason Wang * @len: the length to read from
287961e9c84SJason Wang * configuration space
288961e9c84SJason Wang * @set_config: Write to device specific configuration space
289961e9c84SJason Wang * @vdev: vdpa device
290961e9c84SJason Wang * @offset: offset from the beginning of
291961e9c84SJason Wang * configuration space
292961e9c84SJason Wang * @buf: buffer used to write from
293961e9c84SJason Wang * @len: the length to write to
294961e9c84SJason Wang * configuration space
295961e9c84SJason Wang * @get_generation: Get device config generation (optional)
296961e9c84SJason Wang * @vdev: vdpa device
297961e9c84SJason Wang * Returns u32: device generation
2983f1b623aSJason Wang * @get_iova_range: Get supported iova range (optional)
2993f1b623aSJason Wang * @vdev: vdpa device
3003f1b623aSJason Wang * Returns the iova range supported by
3013f1b623aSJason Wang * the device.
3021d246927SXie Yongji * @set_vq_affinity: Set the affinity of virtqueue (optional)
3031d246927SXie Yongji * @vdev: vdpa device
3041d246927SXie Yongji * @idx: virtqueue index
3051d246927SXie Yongji * @cpu_mask: the affinity mask
3061d246927SXie Yongji * Returns integer: success (0) or error (< 0)
3071d246927SXie Yongji * @get_vq_affinity: Get the affinity of virtqueue (optional)
3081d246927SXie Yongji * @vdev: vdpa device
3091d246927SXie Yongji * @idx: virtqueue index
3101d246927SXie Yongji * Returns the affinity mask
31146d554b1SGautam Dawar * @set_group_asid: Set address space identifier for a
31200d1f546SJason Wang * virtqueue group (optional)
31346d554b1SGautam Dawar * @vdev: vdpa device
31446d554b1SGautam Dawar * @group: virtqueue group
31546d554b1SGautam Dawar * @asid: address space id for this group
31646d554b1SGautam Dawar * Returns integer: success (0) or error (< 0)
317961e9c84SJason Wang * @set_map: Set device memory mapping (optional)
318961e9c84SJason Wang * Needed for device that using device
319961e9c84SJason Wang * specific DMA translation (on-chip IOMMU)
320961e9c84SJason Wang * @vdev: vdpa device
321db9adcbfSGautam Dawar * @asid: address space identifier
322961e9c84SJason Wang * @iotlb: vhost memory mapping to be
323961e9c84SJason Wang * used by the vDPA
324961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
325961e9c84SJason Wang * @dma_map: Map an area of PA to IOVA (optional)
326961e9c84SJason Wang * Needed for device that using device
327961e9c84SJason Wang * specific DMA translation (on-chip IOMMU)
328961e9c84SJason Wang * and preferring incremental map.
329961e9c84SJason Wang * @vdev: vdpa device
330db9adcbfSGautam Dawar * @asid: address space identifier
331961e9c84SJason Wang * @iova: iova to be mapped
332961e9c84SJason Wang * @size: size of the area
333961e9c84SJason Wang * @pa: physical address for the map
334961e9c84SJason Wang * @perm: device access permission (VHOST_MAP_XX)
335961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
336961e9c84SJason Wang * @dma_unmap: Unmap an area of IOVA (optional but
337961e9c84SJason Wang * must be implemented with dma_map)
338961e9c84SJason Wang * Needed for device that using device
339961e9c84SJason Wang * specific DMA translation (on-chip IOMMU)
340961e9c84SJason Wang * and preferring incremental unmap.
341961e9c84SJason Wang * @vdev: vdpa device
342db9adcbfSGautam Dawar * @asid: address space identifier
343961e9c84SJason Wang * @iova: iova to be unmapped
344961e9c84SJason Wang * @size: size of the area
345961e9c84SJason Wang * Returns integer: success (0) or error (< 0)
346d2cf1b6eSSi-Wei Liu * @reset_map: Reset device memory mapping to the default
347d2cf1b6eSSi-Wei Liu * state (optional)
348d2cf1b6eSSi-Wei Liu * Needed for devices that are using device
349d2cf1b6eSSi-Wei Liu * specific DMA translation and prefer mapping
350d2cf1b6eSSi-Wei Liu * to be decoupled from the virtio life cycle,
351d2cf1b6eSSi-Wei Liu * i.e. device .reset op does not reset mapping
352d2cf1b6eSSi-Wei Liu * @vdev: vdpa device
353d2cf1b6eSSi-Wei Liu * @asid: address space identifier
354d2cf1b6eSSi-Wei Liu * Returns integer: success (0) or error (< 0)
35525da258fSJason Wang * @get_vq_dma_dev: Get the dma device for a specific
35625da258fSJason Wang * virtqueue (optional)
35725da258fSJason Wang * @vdev: vdpa device
35825da258fSJason Wang * @idx: virtqueue index
35925da258fSJason Wang * Returns pointer to structure device or error (NULL)
360c618c84dSStefano Garzarella * @bind_mm: Bind the device to a specific address space
361c618c84dSStefano Garzarella * so the vDPA framework can use VA when this
362c618c84dSStefano Garzarella * callback is implemented. (optional)
363c618c84dSStefano Garzarella * @vdev: vdpa device
364c618c84dSStefano Garzarella * @mm: address space to bind
365c618c84dSStefano Garzarella * @unbind_mm: Unbind the device from the address space
366c618c84dSStefano Garzarella * bound using the bind_mm callback. (optional)
367c618c84dSStefano Garzarella * @vdev: vdpa device
368961e9c84SJason Wang * @free: Free resources that belongs to vDPA (optional)
369961e9c84SJason Wang * @vdev: vdpa device
370961e9c84SJason Wang */
371961e9c84SJason Wang struct vdpa_config_ops {
372961e9c84SJason Wang /* Virtqueue ops */
373961e9c84SJason Wang int (*set_vq_address)(struct vdpa_device *vdev,
374961e9c84SJason Wang u16 idx, u64 desc_area, u64 driver_area,
375961e9c84SJason Wang u64 device_area);
376961e9c84SJason Wang void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
377961e9c84SJason Wang void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
3782c4e4a22SAlvaro Karsz void (*kick_vq_with_data)(struct vdpa_device *vdev, u32 data);
379961e9c84SJason Wang void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
380961e9c84SJason Wang struct vdpa_callback *cb);
381961e9c84SJason Wang void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
382961e9c84SJason Wang bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
383aac50c0bSEli Cohen int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
384aac50c0bSEli Cohen const struct vdpa_vq_state *state);
38523750e39SEli Cohen int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
386aac50c0bSEli Cohen struct vdpa_vq_state *state);
38713b00b13SEli Cohen int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
38813b00b13SEli Cohen struct sk_buff *msg,
38913b00b13SEli Cohen struct netlink_ext_ack *extack);
390c25a26e6SJason Wang struct vdpa_notification_area
391c25a26e6SJason Wang (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
3924c05433bSZhu Lingshan /* vq irq is not expected to be changed once DRIVER_OK is set */
393d0ae1fbfSWu Zongyong int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
3940a926fc9SZhu Lingshan u16 (*get_vq_size)(struct vdpa_device *vdev, u16 idx);
395961e9c84SJason Wang
396961e9c84SJason Wang /* Device ops */
397425a5070SMichael S. Tsirkin u32 (*get_vq_align)(struct vdpa_device *vdev);
398d4821902SGautam Dawar u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
399a72cac60SSi-Wei Liu u32 (*get_vq_desc_group)(struct vdpa_device *vdev, u16 idx);
400a64917bcSEli Cohen u64 (*get_device_features)(struct vdpa_device *vdev);
401b63e5c70SEugenio Pérez u64 (*get_backend_features)(const struct vdpa_device *vdev);
402a64917bcSEli Cohen int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
403a64917bcSEli Cohen u64 (*get_driver_features)(struct vdpa_device *vdev);
404961e9c84SJason Wang void (*set_config_cb)(struct vdpa_device *vdev,
405961e9c84SJason Wang struct vdpa_callback *cb);
406961e9c84SJason Wang u16 (*get_vq_num_max)(struct vdpa_device *vdev);
4073b970a58SWu Zongyong u16 (*get_vq_num_min)(struct vdpa_device *vdev);
408961e9c84SJason Wang u32 (*get_device_id)(struct vdpa_device *vdev);
409961e9c84SJason Wang u32 (*get_vendor_id)(struct vdpa_device *vdev);
410961e9c84SJason Wang u8 (*get_status)(struct vdpa_device *vdev);
411961e9c84SJason Wang void (*set_status)(struct vdpa_device *vdev, u8 status);
4120686082dSXie Yongji int (*reset)(struct vdpa_device *vdev);
413a26f2e4eSSi-Wei Liu int (*compat_reset)(struct vdpa_device *vdev, u32 flags);
414a26f2e4eSSi-Wei Liu #define VDPA_RESET_F_CLEAN_MAP 1
415848ecea1SEugenio Pérez int (*suspend)(struct vdpa_device *vdev);
4161538a8a4SSebastien Boeuf int (*resume)(struct vdpa_device *vdev);
417442706f9SStefano Garzarella size_t (*get_config_size)(struct vdpa_device *vdev);
418961e9c84SJason Wang void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
419961e9c84SJason Wang void *buf, unsigned int len);
420961e9c84SJason Wang void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
421961e9c84SJason Wang const void *buf, unsigned int len);
422961e9c84SJason Wang u32 (*get_generation)(struct vdpa_device *vdev);
4233f1b623aSJason Wang struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
4241d246927SXie Yongji int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx,
4251d246927SXie Yongji const struct cpumask *cpu_mask);
4261d246927SXie Yongji const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev,
4271d246927SXie Yongji u16 idx);
428961e9c84SJason Wang
429961e9c84SJason Wang /* DMA ops */
430db9adcbfSGautam Dawar int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
431db9adcbfSGautam Dawar struct vhost_iotlb *iotlb);
432db9adcbfSGautam Dawar int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
433db9adcbfSGautam Dawar u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
434db9adcbfSGautam Dawar int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
435db9adcbfSGautam Dawar u64 iova, u64 size);
436d2cf1b6eSSi-Wei Liu int (*reset_map)(struct vdpa_device *vdev, unsigned int asid);
43746d554b1SGautam Dawar int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
43846d554b1SGautam Dawar unsigned int asid);
43925da258fSJason Wang struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx);
440c618c84dSStefano Garzarella int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm);
441c618c84dSStefano Garzarella void (*unbind_mm)(struct vdpa_device *vdev);
442961e9c84SJason Wang
443961e9c84SJason Wang /* Free device resources */
444961e9c84SJason Wang void (*free)(struct vdpa_device *vdev);
445961e9c84SJason Wang };
446961e9c84SJason Wang
447961e9c84SJason Wang struct vdpa_device *__vdpa_alloc_device(struct device *parent,
448961e9c84SJason Wang const struct vdpa_config_ops *config,
449db9adcbfSGautam Dawar unsigned int ngroups, unsigned int nas,
450d8945ec4SXie Yongji size_t size, const char *name,
451d8945ec4SXie Yongji bool use_va);
452961e9c84SJason Wang
453c8d182bdSXie Yongji /**
454c8d182bdSXie Yongji * vdpa_alloc_device - allocate and initilaize a vDPA device
455c8d182bdSXie Yongji *
456c8d182bdSXie Yongji * @dev_struct: the type of the parent structure
457c8d182bdSXie Yongji * @member: the name of struct vdpa_device within the @dev_struct
458c8d182bdSXie Yongji * @parent: the parent device
459c8d182bdSXie Yongji * @config: the bus operations that is supported by this device
460d4821902SGautam Dawar * @ngroups: the number of virtqueue groups supported by this device
461db9adcbfSGautam Dawar * @nas: the number of address spaces
462c8d182bdSXie Yongji * @name: name of the vdpa device
463d8945ec4SXie Yongji * @use_va: indicate whether virtual address must be used by this device
464c8d182bdSXie Yongji *
465c8d182bdSXie Yongji * Return allocated data structure or ERR_PTR upon error
466c8d182bdSXie Yongji */
467db9adcbfSGautam Dawar #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
468db9adcbfSGautam Dawar name, use_va) \
469d4821902SGautam Dawar container_of((__vdpa_alloc_device( \
470db9adcbfSGautam Dawar parent, config, ngroups, nas, \
471db9adcbfSGautam Dawar (sizeof(dev_struct) + \
472961e9c84SJason Wang BUILD_BUG_ON_ZERO(offsetof( \
473db9adcbfSGautam Dawar dev_struct, member))), name, use_va)), \
474961e9c84SJason Wang dev_struct, member)
475961e9c84SJason Wang
47681d46d69SLongpeng int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
477961e9c84SJason Wang void vdpa_unregister_device(struct vdpa_device *vdev);
478961e9c84SJason Wang
47981d46d69SLongpeng int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
480903f7bcaSParav Pandit void _vdpa_unregister_device(struct vdpa_device *vdev);
481903f7bcaSParav Pandit
482961e9c84SJason Wang /**
483d0f9164eSParav Pandit * struct vdpa_driver - operations for a vDPA driver
484961e9c84SJason Wang * @driver: underlying device driver
485961e9c84SJason Wang * @probe: the function to call when a device is found. Returns 0 or -errno.
486961e9c84SJason Wang * @remove: the function to call when a device is removed.
487961e9c84SJason Wang */
488961e9c84SJason Wang struct vdpa_driver {
489961e9c84SJason Wang struct device_driver driver;
490961e9c84SJason Wang int (*probe)(struct vdpa_device *vdev);
491961e9c84SJason Wang void (*remove)(struct vdpa_device *vdev);
492961e9c84SJason Wang };
493961e9c84SJason Wang
494961e9c84SJason Wang #define vdpa_register_driver(drv) \
495961e9c84SJason Wang __vdpa_register_driver(drv, THIS_MODULE)
496961e9c84SJason Wang int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
497961e9c84SJason Wang void vdpa_unregister_driver(struct vdpa_driver *drv);
498961e9c84SJason Wang
499961e9c84SJason Wang #define module_vdpa_driver(__vdpa_driver) \
500961e9c84SJason Wang module_driver(__vdpa_driver, vdpa_register_driver, \
501961e9c84SJason Wang vdpa_unregister_driver)
502961e9c84SJason Wang
drv_to_vdpa(struct device_driver * driver)503961e9c84SJason Wang static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
504961e9c84SJason Wang {
505961e9c84SJason Wang return container_of(driver, struct vdpa_driver, driver);
506961e9c84SJason Wang }
507961e9c84SJason Wang
dev_to_vdpa(struct device * _dev)508961e9c84SJason Wang static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
509961e9c84SJason Wang {
510961e9c84SJason Wang return container_of(_dev, struct vdpa_device, dev);
511961e9c84SJason Wang }
512961e9c84SJason Wang
vdpa_get_drvdata(const struct vdpa_device * vdev)513961e9c84SJason Wang static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
514961e9c84SJason Wang {
515961e9c84SJason Wang return dev_get_drvdata(&vdev->dev);
516961e9c84SJason Wang }
517961e9c84SJason Wang
vdpa_set_drvdata(struct vdpa_device * vdev,void * data)518961e9c84SJason Wang static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
519961e9c84SJason Wang {
520961e9c84SJason Wang dev_set_drvdata(&vdev->dev, data);
521961e9c84SJason Wang }
522961e9c84SJason Wang
vdpa_get_dma_dev(struct vdpa_device * vdev)523961e9c84SJason Wang static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
524961e9c84SJason Wang {
525961e9c84SJason Wang return vdev->dma_dev;
526961e9c84SJason Wang }
527452639a6SMichael S. Tsirkin
vdpa_reset(struct vdpa_device * vdev,u32 flags)528bc91df5cSSi-Wei Liu static inline int vdpa_reset(struct vdpa_device *vdev, u32 flags)
529452639a6SMichael S. Tsirkin {
530452639a6SMichael S. Tsirkin const struct vdpa_config_ops *ops = vdev->config;
531aba21affSEli Cohen int ret;
532452639a6SMichael S. Tsirkin
533a6a51adcSEli Cohen down_write(&vdev->cf_lock);
534452639a6SMichael S. Tsirkin vdev->features_valid = false;
535bc91df5cSSi-Wei Liu if (ops->compat_reset && flags)
536bc91df5cSSi-Wei Liu ret = ops->compat_reset(vdev, flags);
537bc91df5cSSi-Wei Liu else
538aba21affSEli Cohen ret = ops->reset(vdev);
539a6a51adcSEli Cohen up_write(&vdev->cf_lock);
540aba21affSEli Cohen return ret;
541452639a6SMichael S. Tsirkin }
542452639a6SMichael S. Tsirkin
vdpa_set_features_unlocked(struct vdpa_device * vdev,u64 features)543e0077cc1SSi-Wei Liu static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
544452639a6SMichael S. Tsirkin {
545452639a6SMichael S. Tsirkin const struct vdpa_config_ops *ops = vdev->config;
546aba21affSEli Cohen int ret;
547aba21affSEli Cohen
548452639a6SMichael S. Tsirkin vdev->features_valid = true;
549aba21affSEli Cohen ret = ops->set_driver_features(vdev, features);
550e0077cc1SSi-Wei Liu
551e0077cc1SSi-Wei Liu return ret;
552e0077cc1SSi-Wei Liu }
553e0077cc1SSi-Wei Liu
vdpa_set_features(struct vdpa_device * vdev,u64 features)554e0077cc1SSi-Wei Liu static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
555e0077cc1SSi-Wei Liu {
556e0077cc1SSi-Wei Liu int ret;
557e0077cc1SSi-Wei Liu
558a6a51adcSEli Cohen down_write(&vdev->cf_lock);
559e0077cc1SSi-Wei Liu ret = vdpa_set_features_unlocked(vdev, features);
560a6a51adcSEli Cohen up_write(&vdev->cf_lock);
561aba21affSEli Cohen
562aba21affSEli Cohen return ret;
563452639a6SMichael S. Tsirkin }
564452639a6SMichael S. Tsirkin
5656dbb1f16SParav Pandit void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
5666dbb1f16SParav Pandit void *buf, unsigned int len);
5676dbb1f16SParav Pandit void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
5686dbb1f16SParav Pandit const void *buf, unsigned int length);
56973bc0dbbSEli Cohen void vdpa_set_status(struct vdpa_device *vdev, u8 status);
57073bc0dbbSEli Cohen
57133b34750SParav Pandit /**
572d0f9164eSParav Pandit * struct vdpa_mgmtdev_ops - vdpa device ops
57333b34750SParav Pandit * @dev_add: Add a vdpa device using alloc and register
57433b34750SParav Pandit * @mdev: parent device to use for device addition
57533b34750SParav Pandit * @name: name of the new vdpa device
576d8ca2fa5SParav Pandit * @config: config attributes to apply to the device under creation
57733b34750SParav Pandit * Driver need to add a new device using _vdpa_register_device()
57833b34750SParav Pandit * after fully initializing the vdpa device. Driver must return 0
57933b34750SParav Pandit * on success or appropriate error code.
58033b34750SParav Pandit * @dev_del: Remove a vdpa device using unregister
58133b34750SParav Pandit * @mdev: parent device to use for device removal
58233b34750SParav Pandit * @dev: vdpa device to remove
58333b34750SParav Pandit * Driver need to remove the specified device by calling
58433b34750SParav Pandit * _vdpa_unregister_device().
585*2f87e9cfSCindy Lu * @dev_set_attr: change a vdpa device's attr after it was create
586*2f87e9cfSCindy Lu * @mdev: parent device to use for device
587*2f87e9cfSCindy Lu * @dev: vdpa device structure
588*2f87e9cfSCindy Lu * @config:Attributes to be set for the device.
589*2f87e9cfSCindy Lu * The driver needs to check the mask of the structure and then set
590*2f87e9cfSCindy Lu * the related information to the vdpa device. The driver must return 0
591*2f87e9cfSCindy Lu * if set successfully.
59233b34750SParav Pandit */
59333b34750SParav Pandit struct vdpa_mgmtdev_ops {
594d8ca2fa5SParav Pandit int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
595d8ca2fa5SParav Pandit const struct vdpa_dev_set_config *config);
59633b34750SParav Pandit void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
597*2f87e9cfSCindy Lu int (*dev_set_attr)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev,
598*2f87e9cfSCindy Lu const struct vdpa_dev_set_config *config);
59933b34750SParav Pandit };
60033b34750SParav Pandit
601960deb33SParav Pandit /**
602960deb33SParav Pandit * struct vdpa_mgmt_dev - vdpa management device
603960deb33SParav Pandit * @device: Management parent device
604960deb33SParav Pandit * @ops: operations supported by management device
605960deb33SParav Pandit * @id_table: Pointer to device id table of supported ids
606d8ca2fa5SParav Pandit * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
607d8ca2fa5SParav Pandit * management device support during dev_add callback
608960deb33SParav Pandit * @list: list entry
6099be5d2d4SSimon Horman * @supported_features: features supported by device
6109be5d2d4SSimon Horman * @max_supported_vqs: maximum number of virtqueues supported by device
611960deb33SParav Pandit */
61233b34750SParav Pandit struct vdpa_mgmt_dev {
61333b34750SParav Pandit struct device *device;
61433b34750SParav Pandit const struct vdpa_mgmtdev_ops *ops;
615ffbda8e9SCindy Lu struct virtio_device_id *id_table;
616d8ca2fa5SParav Pandit u64 config_attr_mask;
61733b34750SParav Pandit struct list_head list;
618cd2629f6SEli Cohen u64 supported_features;
619cd2629f6SEli Cohen u32 max_supported_vqs;
62033b34750SParav Pandit };
62133b34750SParav Pandit
62233b34750SParav Pandit int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
62333b34750SParav Pandit void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
62433b34750SParav Pandit
625961e9c84SJason Wang #endif /* _LINUX_VDPA_H */
626