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