1 /* SPDX-License-Identifier: GPL-2.0 */
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>
13 * struct vdpa_calllback - vDPA callback definition.
14 * @callback: interrupt callback function
15 * @private: the data passed to the callback function
17 struct vdpa_callback {
18 irqreturn_t (*callback)(void *data);
23 * struct vdpa_notification_area - vDPA notification area
24 * @addr: base address of the notification area
25 * @size: size of the notification area
27 struct vdpa_notification_area {
33 * struct vdpa_vq_state_split - vDPA split virtqueue state
34 * @avail_index: available index
36 struct vdpa_vq_state_split {
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
47 struct vdpa_vq_state_packed {
48 u16 last_avail_counter:1;
49 u16 last_avail_idx:15;
50 u16 last_used_counter:1;
54 struct vdpa_vq_state {
56 struct vdpa_vq_state_split split;
57 struct vdpa_vq_state_packed packed;
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
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().
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 */
91 struct vdpa_mgmt_dev *mdev;
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
101 struct vdpa_iova_range {
106 struct vdpa_dev_set_config {
116 * Corresponding file area for device memory mapping
117 * @file: vma->vm_file for the mapping
118 * @offset: mapping offset in the vm_file
120 struct vdpa_map_file {
126 * struct vdpa_config_ops - operations for configuring a vDPA device.
127 * Note: vDPA device drivers are required to implement all of the
128 * operations unless it is mentioned to be optional in the following
131 * @set_vq_address: Set the address of virtqueue
133 * @idx: virtqueue index
134 * @desc_area: address of desc area
135 * @driver_area: address of driver area
136 * @device_area: address of device area
137 * Returns integer: success (0) or error (< 0)
138 * @set_vq_num: Set the size of virtqueue
140 * @idx: virtqueue index
141 * @num: the size of virtqueue
142 * @kick_vq: Kick the virtqueue
144 * @idx: virtqueue index
145 * @set_vq_cb: Set the interrupt callback function for
148 * @idx: virtqueue index
149 * @cb: virtio-vdev interrupt callback structure
150 * @set_vq_ready: Set ready status for a virtqueue
152 * @idx: virtqueue index
153 * @ready: ready (true) not ready(false)
154 * @get_vq_ready: Get ready status for a virtqueue
156 * @idx: virtqueue index
157 * Returns boolean: ready (true) or not (false)
158 * @set_vq_state: Set the state for a virtqueue
160 * @idx: virtqueue index
161 * @state: pointer to set virtqueue state (last_avail_idx)
162 * Returns integer: success (0) or error (< 0)
163 * @get_vq_state: Get the state for a virtqueue
165 * @idx: virtqueue index
166 * @state: pointer to returned state (last_avail_idx)
167 * @get_vq_notification: Get the notification area for a virtqueue (optional)
169 * @idx: virtqueue index
170 * Returns the notifcation area
171 * @get_vq_irq: Get the irq number of a virtqueue (optional,
172 * but must implemented if require vq irq offloading)
174 * @idx: virtqueue index
175 * Returns int: irq number of a virtqueue,
176 * negative number if no irq assigned.
177 * @get_vq_align: Get the virtqueue align requirement
180 * Returns virtqueue algin requirement
181 * @get_vq_group: Get the group id for a specific
182 * virtqueue (optional)
184 * @idx: virtqueue index
185 * Returns u32: group id for this virtqueue
186 * @get_device_features: Get virtio features supported by the device
188 * Returns the virtio features support by the
190 * @set_driver_features: Set virtio features supported by the driver
192 * @features: feature support by the driver
193 * Returns integer: success (0) or error (< 0)
194 * @get_driver_features: Get the virtio driver features in action
196 * Returns the virtio features accepted
197 * @set_config_cb: Set the config interrupt callback
199 * @cb: virtio-vdev interrupt callback structure
200 * @get_vq_num_max: Get the max size of virtqueue
202 * Returns u16: max size of virtqueue
203 * @get_vq_num_min: Get the min size of virtqueue (optional)
205 * Returns u16: min size of virtqueue
206 * @get_device_id: Get virtio device id
208 * Returns u32: virtio device id
209 * @get_vendor_id: Get id for the vendor that provides this device
211 * Returns u32: virtio vendor id
212 * @get_status: Get the device status
214 * Returns u8: virtio device status
215 * @set_status: Set the device status
217 * @status: virtio device status
218 * @reset: Reset device
220 * Returns integer: success (0) or error (< 0)
221 * @suspend: Suspend or resume the device (optional)
223 * Returns integer: success (0) or error (< 0)
224 * @get_config_size: Get the size of the configuration space includes
225 * fields that are conditional on feature bits.
227 * Returns size_t: configuration size
228 * @get_config: Read from device specific configuration space
230 * @offset: offset from the beginning of
231 * configuration space
232 * @buf: buffer used to read to
233 * @len: the length to read from
234 * configuration space
235 * @set_config: Write to device specific configuration space
237 * @offset: offset from the beginning of
238 * configuration space
239 * @buf: buffer used to write from
240 * @len: the length to write to
241 * configuration space
242 * @get_generation: Get device config generation (optional)
244 * Returns u32: device generation
245 * @get_iova_range: Get supported iova range (optional)
247 * Returns the iova range supported by
249 * @set_group_asid: Set address space identifier for a
250 * virtqueue group (optional)
252 * @group: virtqueue group
253 * @asid: address space id for this group
254 * Returns integer: success (0) or error (< 0)
255 * @set_map: Set device memory mapping (optional)
256 * Needed for device that using device
257 * specific DMA translation (on-chip IOMMU)
259 * @asid: address space identifier
260 * @iotlb: vhost memory mapping to be
262 * Returns integer: success (0) or error (< 0)
263 * @dma_map: Map an area of PA to IOVA (optional)
264 * Needed for device that using device
265 * specific DMA translation (on-chip IOMMU)
266 * and preferring incremental map.
268 * @asid: address space identifier
269 * @iova: iova to be mapped
270 * @size: size of the area
271 * @pa: physical address for the map
272 * @perm: device access permission (VHOST_MAP_XX)
273 * Returns integer: success (0) or error (< 0)
274 * @dma_unmap: Unmap an area of IOVA (optional but
275 * must be implemented with dma_map)
276 * Needed for device that using device
277 * specific DMA translation (on-chip IOMMU)
278 * and preferring incremental unmap.
280 * @asid: address space identifier
281 * @iova: iova to be unmapped
282 * @size: size of the area
283 * Returns integer: success (0) or error (< 0)
284 * @free: Free resources that belongs to vDPA (optional)
287 struct vdpa_config_ops {
289 int (*set_vq_address)(struct vdpa_device *vdev,
290 u16 idx, u64 desc_area, u64 driver_area,
292 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
293 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
294 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
295 struct vdpa_callback *cb);
296 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
297 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
298 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
299 const struct vdpa_vq_state *state);
300 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
301 struct vdpa_vq_state *state);
302 int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
304 struct netlink_ext_ack *extack);
305 struct vdpa_notification_area
306 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
307 /* vq irq is not expected to be changed once DRIVER_OK is set */
308 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
311 u32 (*get_vq_align)(struct vdpa_device *vdev);
312 u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
313 u64 (*get_device_features)(struct vdpa_device *vdev);
314 int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
315 u64 (*get_driver_features)(struct vdpa_device *vdev);
316 void (*set_config_cb)(struct vdpa_device *vdev,
317 struct vdpa_callback *cb);
318 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
319 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
320 u32 (*get_device_id)(struct vdpa_device *vdev);
321 u32 (*get_vendor_id)(struct vdpa_device *vdev);
322 u8 (*get_status)(struct vdpa_device *vdev);
323 void (*set_status)(struct vdpa_device *vdev, u8 status);
324 int (*reset)(struct vdpa_device *vdev);
325 int (*suspend)(struct vdpa_device *vdev);
326 size_t (*get_config_size)(struct vdpa_device *vdev);
327 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
328 void *buf, unsigned int len);
329 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
330 const void *buf, unsigned int len);
331 u32 (*get_generation)(struct vdpa_device *vdev);
332 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
335 int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
336 struct vhost_iotlb *iotlb);
337 int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
338 u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
339 int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
341 int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
344 /* Free device resources */
345 void (*free)(struct vdpa_device *vdev);
348 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
349 const struct vdpa_config_ops *config,
350 unsigned int ngroups, unsigned int nas,
351 size_t size, const char *name,
355 * vdpa_alloc_device - allocate and initilaize a vDPA device
357 * @dev_struct: the type of the parent structure
358 * @member: the name of struct vdpa_device within the @dev_struct
359 * @parent: the parent device
360 * @config: the bus operations that is supported by this device
361 * @ngroups: the number of virtqueue groups supported by this device
362 * @nas: the number of address spaces
363 * @name: name of the vdpa device
364 * @use_va: indicate whether virtual address must be used by this device
366 * Return allocated data structure or ERR_PTR upon error
368 #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
370 container_of((__vdpa_alloc_device( \
371 parent, config, ngroups, nas, \
372 (sizeof(dev_struct) + \
373 BUILD_BUG_ON_ZERO(offsetof( \
374 dev_struct, member))), name, use_va)), \
377 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
378 void vdpa_unregister_device(struct vdpa_device *vdev);
380 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
381 void _vdpa_unregister_device(struct vdpa_device *vdev);
384 * struct vdpa_driver - operations for a vDPA driver
385 * @driver: underlying device driver
386 * @probe: the function to call when a device is found. Returns 0 or -errno.
387 * @remove: the function to call when a device is removed.
390 struct device_driver driver;
391 int (*probe)(struct vdpa_device *vdev);
392 void (*remove)(struct vdpa_device *vdev);
395 #define vdpa_register_driver(drv) \
396 __vdpa_register_driver(drv, THIS_MODULE)
397 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
398 void vdpa_unregister_driver(struct vdpa_driver *drv);
400 #define module_vdpa_driver(__vdpa_driver) \
401 module_driver(__vdpa_driver, vdpa_register_driver, \
402 vdpa_unregister_driver)
404 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
406 return container_of(driver, struct vdpa_driver, driver);
409 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
411 return container_of(_dev, struct vdpa_device, dev);
414 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
416 return dev_get_drvdata(&vdev->dev);
419 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
421 dev_set_drvdata(&vdev->dev, data);
424 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
426 return vdev->dma_dev;
429 static inline int vdpa_reset(struct vdpa_device *vdev)
431 const struct vdpa_config_ops *ops = vdev->config;
434 down_write(&vdev->cf_lock);
435 vdev->features_valid = false;
436 ret = ops->reset(vdev);
437 up_write(&vdev->cf_lock);
441 static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
443 const struct vdpa_config_ops *ops = vdev->config;
446 vdev->features_valid = true;
447 ret = ops->set_driver_features(vdev, features);
452 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
456 down_write(&vdev->cf_lock);
457 ret = vdpa_set_features_unlocked(vdev, features);
458 up_write(&vdev->cf_lock);
463 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
464 void *buf, unsigned int len);
465 void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
466 const void *buf, unsigned int length);
467 void vdpa_set_status(struct vdpa_device *vdev, u8 status);
470 * struct vdpa_mgmtdev_ops - vdpa device ops
471 * @dev_add: Add a vdpa device using alloc and register
472 * @mdev: parent device to use for device addition
473 * @name: name of the new vdpa device
474 * @config: config attributes to apply to the device under creation
475 * Driver need to add a new device using _vdpa_register_device()
476 * after fully initializing the vdpa device. Driver must return 0
477 * on success or appropriate error code.
478 * @dev_del: Remove a vdpa device using unregister
479 * @mdev: parent device to use for device removal
480 * @dev: vdpa device to remove
481 * Driver need to remove the specified device by calling
482 * _vdpa_unregister_device().
484 struct vdpa_mgmtdev_ops {
485 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
486 const struct vdpa_dev_set_config *config);
487 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
491 * struct vdpa_mgmt_dev - vdpa management device
492 * @device: Management parent device
493 * @ops: operations supported by management device
494 * @id_table: Pointer to device id table of supported ids
495 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
496 * management device support during dev_add callback
499 struct vdpa_mgmt_dev {
500 struct device *device;
501 const struct vdpa_mgmtdev_ops *ops;
502 struct virtio_device_id *id_table;
503 u64 config_attr_mask;
504 struct list_head list;
505 u64 supported_features;
506 u32 max_supported_vqs;
509 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
510 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
512 #endif /* _LINUX_VDPA_H */