vdpa: support exposing the config size to userspace
[linux-block.git] / include / linux / vdpa.h
CommitLineData
961e9c84
JW
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_VDPA_H
3#define _LINUX_VDPA_H
4
5#include <linux/kernel.h>
6#include <linux/device.h>
7#include <linux/interrupt.h>
8#include <linux/vhost_iotlb.h>
d8ca2fa5
PP
9#include <linux/virtio_net.h>
10#include <linux/if_ether.h>
961e9c84
JW
11
12/**
d0f9164e 13 * struct vdpa_calllback - vDPA callback definition.
961e9c84
JW
14 * @callback: interrupt callback function
15 * @private: the data passed to the callback function
16 */
17struct vdpa_callback {
18 irqreturn_t (*callback)(void *data);
19 void *private;
20};
21
c25a26e6 22/**
d0f9164e 23 * struct vdpa_notification_area - vDPA notification area
c25a26e6
JW
24 * @addr: base address of the notification area
25 * @size: size of the notification area
26 */
27struct vdpa_notification_area {
28 resource_size_t addr;
29 resource_size_t size;
30};
31
aac50c0b 32/**
530a5678 33 * struct vdpa_vq_state_split - vDPA split virtqueue state
aac50c0b
EC
34 * @avail_index: available index
35 */
530a5678 36struct vdpa_vq_state_split {
aac50c0b
EC
37 u16 avail_index;
38};
39
530a5678
JW
40/**
41 * struct vdpa_vq_state_packed - vDPA packed virtqueue state
42 * @last_avail_counter: last driver ring wrap counter observed by device
43 * @last_avail_idx: device available index
44 * @last_used_counter: device ring wrap counter
45 * @last_used_idx: used index
46 */
47struct vdpa_vq_state_packed {
86e17a51
XY
48 u16 last_avail_counter:1;
49 u16 last_avail_idx:15;
50 u16 last_used_counter:1;
51 u16 last_used_idx:15;
530a5678
JW
52};
53
54struct vdpa_vq_state {
86e17a51
XY
55 union {
56 struct vdpa_vq_state_split split;
57 struct vdpa_vq_state_packed packed;
58 };
530a5678
JW
59};
60
33b34750
PP
61struct vdpa_mgmt_dev;
62
961e9c84 63/**
d0f9164e 64 * struct vdpa_device - representation of a vDPA device
961e9c84
JW
65 * @dev: underlying device
66 * @dma_dev: the actual device that is performing DMA
539fec78 67 * @driver_override: driver name to force a match
961e9c84 68 * @config: the configuration ops for this device.
ad69dd0b 69 * @cf_mutex: Protects get and set access to configuration layout.
961e9c84 70 * @index: device index
452639a6 71 * @features_valid: were features initialized? for legacy guests
d8945ec4 72 * @use_va: indicate whether virtual address must be used by this device
476c135e 73 * @nvqs: maximum number of supported virtqueues
903f7bca
PP
74 * @mdev: management device pointer; caller must setup when registering device as part
75 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
961e9c84
JW
76 */
77struct vdpa_device {
78 struct device dev;
79 struct device *dma_dev;
539fec78 80 const char *driver_override;
961e9c84 81 const struct vdpa_config_ops *config;
ad69dd0b 82 struct mutex cf_mutex; /* Protects get/set config */
961e9c84 83 unsigned int index;
452639a6 84 bool features_valid;
d8945ec4 85 bool use_va;
a9974489 86 int nvqs;
903f7bca 87 struct vdpa_mgmt_dev *mdev;
961e9c84
JW
88};
89
3f1b623a 90/**
d0f9164e 91 * struct vdpa_iova_range - the IOVA range support by the device
3f1b623a
JW
92 * @first: start of the IOVA range
93 * @last: end of the IOVA range
94 */
95struct vdpa_iova_range {
96 u64 first;
97 u64 last;
98};
99
d8ca2fa5
PP
100struct vdpa_dev_set_config {
101 struct {
102 u8 mac[ETH_ALEN];
103 u16 mtu;
aba21aff 104 u16 max_vq_pairs;
d8ca2fa5
PP
105 } net;
106 u64 mask;
107};
108
d8945ec4
XY
109/**
110 * Corresponding file area for device memory mapping
111 * @file: vma->vm_file for the mapping
112 * @offset: mapping offset in the vm_file
113 */
114struct vdpa_map_file {
115 struct file *file;
116 u64 offset;
117};
118
961e9c84 119/**
d0f9164e 120 * struct vdpa_config_ops - operations for configuring a vDPA device.
961e9c84
JW
121 * Note: vDPA device drivers are required to implement all of the
122 * operations unless it is mentioned to be optional in the following
123 * list.
124 *
125 * @set_vq_address: Set the address of virtqueue
126 * @vdev: vdpa device
127 * @idx: virtqueue index
128 * @desc_area: address of desc area
129 * @driver_area: address of driver area
130 * @device_area: address of device area
131 * Returns integer: success (0) or error (< 0)
132 * @set_vq_num: Set the size of virtqueue
133 * @vdev: vdpa device
134 * @idx: virtqueue index
135 * @num: the size of virtqueue
136 * @kick_vq: Kick the virtqueue
137 * @vdev: vdpa device
138 * @idx: virtqueue index
139 * @set_vq_cb: Set the interrupt callback function for
140 * a virtqueue
141 * @vdev: vdpa device
142 * @idx: virtqueue index
143 * @cb: virtio-vdev interrupt callback structure
144 * @set_vq_ready: Set ready status for a virtqueue
145 * @vdev: vdpa device
146 * @idx: virtqueue index
147 * @ready: ready (true) not ready(false)
148 * @get_vq_ready: Get ready status for a virtqueue
149 * @vdev: vdpa device
150 * @idx: virtqueue index
151 * Returns boolean: ready (true) or not (false)
152 * @set_vq_state: Set the state for a virtqueue
153 * @vdev: vdpa device
154 * @idx: virtqueue index
aac50c0b 155 * @state: pointer to set virtqueue state (last_avail_idx)
961e9c84
JW
156 * Returns integer: success (0) or error (< 0)
157 * @get_vq_state: Get the state for a virtqueue
158 * @vdev: vdpa device
159 * @idx: virtqueue index
aac50c0b 160 * @state: pointer to returned state (last_avail_idx)
28cc408b 161 * @get_vq_notification: Get the notification area for a virtqueue (optional)
c25a26e6
JW
162 * @vdev: vdpa device
163 * @idx: virtqueue index
164 * Returns the notifcation area
7164675a
ZL
165 * @get_vq_irq: Get the irq number of a virtqueue (optional,
166 * but must implemented if require vq irq offloading)
167 * @vdev: vdpa device
168 * @idx: virtqueue index
169 * Returns int: irq number of a virtqueue,
170 * negative number if no irq assigned.
961e9c84
JW
171 * @get_vq_align: Get the virtqueue align requirement
172 * for the device
173 * @vdev: vdpa device
174 * Returns virtqueue algin requirement
a64917bc 175 * @get_device_features: Get virtio features supported by the device
961e9c84
JW
176 * @vdev: vdpa device
177 * Returns the virtio features support by the
178 * device
a64917bc 179 * @set_driver_features: Set virtio features supported by the driver
961e9c84
JW
180 * @vdev: vdpa device
181 * @features: feature support by the driver
182 * Returns integer: success (0) or error (< 0)
a64917bc
EC
183 * @get_driver_features: Get the virtio driver features in action
184 * @vdev: vdpa device
185 * Returns the virtio features accepted
961e9c84
JW
186 * @set_config_cb: Set the config interrupt callback
187 * @vdev: vdpa device
188 * @cb: virtio-vdev interrupt callback structure
189 * @get_vq_num_max: Get the max size of virtqueue
190 * @vdev: vdpa device
191 * Returns u16: max size of virtqueue
3b970a58
WZ
192 * @get_vq_num_min: Get the min size of virtqueue (optional)
193 * @vdev: vdpa device
194 * Returns u16: min size of virtqueue
961e9c84
JW
195 * @get_device_id: Get virtio device id
196 * @vdev: vdpa device
197 * Returns u32: virtio device id
198 * @get_vendor_id: Get id for the vendor that provides this device
199 * @vdev: vdpa device
200 * Returns u32: virtio vendor id
201 * @get_status: Get the device status
202 * @vdev: vdpa device
203 * Returns u8: virtio device status
204 * @set_status: Set the device status
205 * @vdev: vdpa device
206 * @status: virtio device status
0686082d
XY
207 * @reset: Reset device
208 * @vdev: vdpa device
209 * Returns integer: success (0) or error (< 0)
a61280dd
L
210 * @get_config_size: Get the size of the configuration space includes
211 * fields that are conditional on feature bits.
442706f9
SG
212 * @vdev: vdpa device
213 * Returns size_t: configuration size
961e9c84
JW
214 * @get_config: Read from device specific configuration space
215 * @vdev: vdpa device
216 * @offset: offset from the beginning of
217 * configuration space
218 * @buf: buffer used to read to
219 * @len: the length to read from
220 * configuration space
221 * @set_config: Write to device specific configuration space
222 * @vdev: vdpa device
223 * @offset: offset from the beginning of
224 * configuration space
225 * @buf: buffer used to write from
226 * @len: the length to write to
227 * configuration space
228 * @get_generation: Get device config generation (optional)
229 * @vdev: vdpa device
230 * Returns u32: device generation
3f1b623a
JW
231 * @get_iova_range: Get supported iova range (optional)
232 * @vdev: vdpa device
233 * Returns the iova range supported by
234 * the device.
961e9c84
JW
235 * @set_map: Set device memory mapping (optional)
236 * Needed for device that using device
237 * specific DMA translation (on-chip IOMMU)
238 * @vdev: vdpa device
239 * @iotlb: vhost memory mapping to be
240 * used by the vDPA
241 * Returns integer: success (0) or error (< 0)
242 * @dma_map: Map an area of PA to IOVA (optional)
243 * Needed for device that using device
244 * specific DMA translation (on-chip IOMMU)
245 * and preferring incremental map.
246 * @vdev: vdpa device
247 * @iova: iova to be mapped
248 * @size: size of the area
249 * @pa: physical address for the map
250 * @perm: device access permission (VHOST_MAP_XX)
251 * Returns integer: success (0) or error (< 0)
252 * @dma_unmap: Unmap an area of IOVA (optional but
253 * must be implemented with dma_map)
254 * Needed for device that using device
255 * specific DMA translation (on-chip IOMMU)
256 * and preferring incremental unmap.
257 * @vdev: vdpa device
258 * @iova: iova to be unmapped
259 * @size: size of the area
260 * Returns integer: success (0) or error (< 0)
261 * @free: Free resources that belongs to vDPA (optional)
262 * @vdev: vdpa device
263 */
264struct vdpa_config_ops {
265 /* Virtqueue ops */
266 int (*set_vq_address)(struct vdpa_device *vdev,
267 u16 idx, u64 desc_area, u64 driver_area,
268 u64 device_area);
269 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
270 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
271 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
272 struct vdpa_callback *cb);
273 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
274 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
aac50c0b
EC
275 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
276 const struct vdpa_vq_state *state);
23750e39
EC
277 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
278 struct vdpa_vq_state *state);
c25a26e6
JW
279 struct vdpa_notification_area
280 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
4c05433b 281 /* vq irq is not expected to be changed once DRIVER_OK is set */
d0ae1fbf 282 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
961e9c84
JW
283
284 /* Device ops */
425a5070 285 u32 (*get_vq_align)(struct vdpa_device *vdev);
a64917bc
EC
286 u64 (*get_device_features)(struct vdpa_device *vdev);
287 int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
288 u64 (*get_driver_features)(struct vdpa_device *vdev);
961e9c84
JW
289 void (*set_config_cb)(struct vdpa_device *vdev,
290 struct vdpa_callback *cb);
291 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
3b970a58 292 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
961e9c84
JW
293 u32 (*get_device_id)(struct vdpa_device *vdev);
294 u32 (*get_vendor_id)(struct vdpa_device *vdev);
295 u8 (*get_status)(struct vdpa_device *vdev);
296 void (*set_status)(struct vdpa_device *vdev, u8 status);
0686082d 297 int (*reset)(struct vdpa_device *vdev);
442706f9 298 size_t (*get_config_size)(struct vdpa_device *vdev);
961e9c84
JW
299 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
300 void *buf, unsigned int len);
301 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
302 const void *buf, unsigned int len);
303 u32 (*get_generation)(struct vdpa_device *vdev);
3f1b623a 304 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
961e9c84
JW
305
306 /* DMA ops */
307 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
308 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
c10fb945 309 u64 pa, u32 perm, void *opaque);
961e9c84
JW
310 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
311
312 /* Free device resources */
313 void (*free)(struct vdpa_device *vdev);
314};
315
316struct vdpa_device *__vdpa_alloc_device(struct device *parent,
317 const struct vdpa_config_ops *config,
d8945ec4
XY
318 size_t size, const char *name,
319 bool use_va);
961e9c84 320
c8d182bd
XY
321/**
322 * vdpa_alloc_device - allocate and initilaize a vDPA device
323 *
324 * @dev_struct: the type of the parent structure
325 * @member: the name of struct vdpa_device within the @dev_struct
326 * @parent: the parent device
327 * @config: the bus operations that is supported by this device
328 * @name: name of the vdpa device
d8945ec4 329 * @use_va: indicate whether virtual address must be used by this device
c8d182bd
XY
330 *
331 * Return allocated data structure or ERR_PTR upon error
332 */
d8945ec4 333#define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va) \
961e9c84 334 container_of(__vdpa_alloc_device( \
f00bdce0 335 parent, config, \
961e9c84
JW
336 sizeof(dev_struct) + \
337 BUILD_BUG_ON_ZERO(offsetof( \
d8945ec4 338 dev_struct, member)), name, use_va), \
961e9c84
JW
339 dev_struct, member)
340
f00bdce0 341int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
961e9c84
JW
342void vdpa_unregister_device(struct vdpa_device *vdev);
343
f00bdce0 344int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
903f7bca
PP
345void _vdpa_unregister_device(struct vdpa_device *vdev);
346
961e9c84 347/**
d0f9164e 348 * struct vdpa_driver - operations for a vDPA driver
961e9c84
JW
349 * @driver: underlying device driver
350 * @probe: the function to call when a device is found. Returns 0 or -errno.
351 * @remove: the function to call when a device is removed.
352 */
353struct vdpa_driver {
354 struct device_driver driver;
355 int (*probe)(struct vdpa_device *vdev);
356 void (*remove)(struct vdpa_device *vdev);
357};
358
359#define vdpa_register_driver(drv) \
360 __vdpa_register_driver(drv, THIS_MODULE)
361int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
362void vdpa_unregister_driver(struct vdpa_driver *drv);
363
364#define module_vdpa_driver(__vdpa_driver) \
365 module_driver(__vdpa_driver, vdpa_register_driver, \
366 vdpa_unregister_driver)
367
368static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
369{
370 return container_of(driver, struct vdpa_driver, driver);
371}
372
373static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
374{
375 return container_of(_dev, struct vdpa_device, dev);
376}
377
378static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
379{
380 return dev_get_drvdata(&vdev->dev);
381}
382
383static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
384{
385 dev_set_drvdata(&vdev->dev, data);
386}
387
388static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
389{
390 return vdev->dma_dev;
391}
452639a6 392
0686082d 393static inline int vdpa_reset(struct vdpa_device *vdev)
452639a6 394{
86e17a51 395 const struct vdpa_config_ops *ops = vdev->config;
aba21aff 396 int ret;
452639a6 397
aba21aff 398 mutex_lock(&vdev->cf_mutex);
452639a6 399 vdev->features_valid = false;
aba21aff
EC
400 ret = ops->reset(vdev);
401 mutex_unlock(&vdev->cf_mutex);
402 return ret;
452639a6
MT
403}
404
e0077cc1 405static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
452639a6 406{
86e17a51 407 const struct vdpa_config_ops *ops = vdev->config;
aba21aff
EC
408 int ret;
409
452639a6 410 vdev->features_valid = true;
aba21aff 411 ret = ops->set_driver_features(vdev, features);
e0077cc1
SWL
412
413 return ret;
414}
415
416static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
417{
418 int ret;
419
420 mutex_lock(&vdev->cf_mutex);
421 ret = vdpa_set_features_unlocked(vdev, features);
422 mutex_unlock(&vdev->cf_mutex);
aba21aff
EC
423
424 return ret;
452639a6
MT
425}
426
6dbb1f16
PP
427void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
428 void *buf, unsigned int len);
429void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
430 const void *buf, unsigned int length);
73bc0dbb
EC
431void vdpa_set_status(struct vdpa_device *vdev, u8 status);
432
33b34750 433/**
d0f9164e
PP
434 * struct vdpa_mgmtdev_ops - vdpa device ops
435 * @dev_add: Add a vdpa device using alloc and register
436 * @mdev: parent device to use for device addition
437 * @name: name of the new vdpa device
d8ca2fa5 438 * @config: config attributes to apply to the device under creation
d0f9164e
PP
439 * Driver need to add a new device using _vdpa_register_device()
440 * after fully initializing the vdpa device. Driver must return 0
441 * on success or appropriate error code.
442 * @dev_del: Remove a vdpa device using unregister
443 * @mdev: parent device to use for device removal
444 * @dev: vdpa device to remove
445 * Driver need to remove the specified device by calling
446 * _vdpa_unregister_device().
33b34750
PP
447 */
448struct vdpa_mgmtdev_ops {
d8ca2fa5
PP
449 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
450 const struct vdpa_dev_set_config *config);
33b34750
PP
451 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
452};
453
960deb33
PP
454/**
455 * struct vdpa_mgmt_dev - vdpa management device
456 * @device: Management parent device
457 * @ops: operations supported by management device
458 * @id_table: Pointer to device id table of supported ids
d8ca2fa5
PP
459 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
460 * management device support during dev_add callback
960deb33
PP
461 * @list: list entry
462 */
33b34750
PP
463struct vdpa_mgmt_dev {
464 struct device *device;
465 const struct vdpa_mgmtdev_ops *ops;
960deb33 466 const struct virtio_device_id *id_table;
d8ca2fa5 467 u64 config_attr_mask;
33b34750 468 struct list_head list;
cd2629f6
EC
469 u64 supported_features;
470 u32 max_supported_vqs;
33b34750
PP
471};
472
473int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
474void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
475
961e9c84 476#endif /* _LINUX_VDPA_H */