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
68 * @config: the configuration ops for this device.
69 * @cf_lock: Protects get and set access to configuration layout.
70 * @index: device index
71 * @features_valid: were features initialized? for legacy guests
72 * @ngroups: the number of virtqueue groups
73 * @nas: the number of address spaces
74 * @use_va: indicate whether virtual address must be used by this device
75 * @nvqs: maximum number of supported virtqueues
76 * @mdev: management device pointer; caller must setup when registering device as part
77 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
81 struct device *dma_dev;
82 const char *driver_override;
83 const struct vdpa_config_ops *config;
84 struct rw_semaphore cf_lock; /* Protects get/set config */
89 struct vdpa_mgmt_dev *mdev;
95 * struct vdpa_iova_range - the IOVA range support by the device
96 * @first: start of the IOVA range
97 * @last: end of the IOVA range
99 struct vdpa_iova_range {
104 struct vdpa_dev_set_config {
114 * Corresponding file area for device memory mapping
115 * @file: vma->vm_file for the mapping
116 * @offset: mapping offset in the vm_file
118 struct vdpa_map_file {
124 * struct vdpa_config_ops - operations for configuring a vDPA device.
125 * Note: vDPA device drivers are required to implement all of the
126 * operations unless it is mentioned to be optional in the following
129 * @set_vq_address: Set the address of virtqueue
131 * @idx: virtqueue index
132 * @desc_area: address of desc area
133 * @driver_area: address of driver area
134 * @device_area: address of device area
135 * Returns integer: success (0) or error (< 0)
136 * @set_vq_num: Set the size of virtqueue
138 * @idx: virtqueue index
139 * @num: the size of virtqueue
140 * @kick_vq: Kick the virtqueue
142 * @idx: virtqueue index
143 * @set_vq_cb: Set the interrupt callback function for
146 * @idx: virtqueue index
147 * @cb: virtio-vdev interrupt callback structure
148 * @set_vq_ready: Set ready status for a virtqueue
150 * @idx: virtqueue index
151 * @ready: ready (true) not ready(false)
152 * @get_vq_ready: Get ready status for a virtqueue
154 * @idx: virtqueue index
155 * Returns boolean: ready (true) or not (false)
156 * @set_vq_state: Set the state for a virtqueue
158 * @idx: virtqueue index
159 * @state: pointer to set virtqueue state (last_avail_idx)
160 * Returns integer: success (0) or error (< 0)
161 * @get_vq_state: Get the state for a virtqueue
163 * @idx: virtqueue index
164 * @state: pointer to returned state (last_avail_idx)
165 * @get_vq_notification: Get the notification area for a virtqueue (optional)
167 * @idx: virtqueue index
168 * Returns the notifcation area
169 * @get_vq_irq: Get the irq number of a virtqueue (optional,
170 * but must implemented if require vq irq offloading)
172 * @idx: virtqueue index
173 * Returns int: irq number of a virtqueue,
174 * negative number if no irq assigned.
175 * @get_vq_align: Get the virtqueue align requirement
178 * Returns virtqueue algin requirement
179 * @get_vq_group: Get the group id for a specific virtqueue
181 * @idx: virtqueue index
182 * Returns u32: group id for this virtqueue
183 * @get_device_features: Get virtio features supported by the device
185 * Returns the virtio features support by the
187 * @set_driver_features: Set virtio features supported by the driver
189 * @features: feature support by the driver
190 * Returns integer: success (0) or error (< 0)
191 * @get_driver_features: Get the virtio driver features in action
193 * Returns the virtio features accepted
194 * @set_config_cb: Set the config interrupt callback
196 * @cb: virtio-vdev interrupt callback structure
197 * @get_vq_num_max: Get the max size of virtqueue
199 * Returns u16: max size of virtqueue
200 * @get_vq_num_min: Get the min size of virtqueue (optional)
202 * Returns u16: min size of virtqueue
203 * @get_device_id: Get virtio device id
205 * Returns u32: virtio device id
206 * @get_vendor_id: Get id for the vendor that provides this device
208 * Returns u32: virtio vendor id
209 * @get_status: Get the device status
211 * Returns u8: virtio device status
212 * @set_status: Set the device status
214 * @status: virtio device status
215 * @reset: Reset device
217 * Returns integer: success (0) or error (< 0)
218 * @get_config_size: Get the size of the configuration space includes
219 * fields that are conditional on feature bits.
221 * Returns size_t: configuration size
222 * @get_config: Read from device specific configuration space
224 * @offset: offset from the beginning of
225 * configuration space
226 * @buf: buffer used to read to
227 * @len: the length to read from
228 * configuration space
229 * @set_config: Write to device specific configuration space
231 * @offset: offset from the beginning of
232 * configuration space
233 * @buf: buffer used to write from
234 * @len: the length to write to
235 * configuration space
236 * @get_generation: Get device config generation (optional)
238 * Returns u32: device generation
239 * @get_iova_range: Get supported iova range (optional)
241 * Returns the iova range supported by
243 * @set_group_asid: Set address space identifier for a
246 * @group: virtqueue group
247 * @asid: address space id for this group
248 * Returns integer: success (0) or error (< 0)
249 * @set_map: Set device memory mapping (optional)
250 * Needed for device that using device
251 * specific DMA translation (on-chip IOMMU)
253 * @asid: address space identifier
254 * @iotlb: vhost memory mapping to be
256 * Returns integer: success (0) or error (< 0)
257 * @dma_map: Map an area of PA to IOVA (optional)
258 * Needed for device that using device
259 * specific DMA translation (on-chip IOMMU)
260 * and preferring incremental map.
262 * @asid: address space identifier
263 * @iova: iova to be mapped
264 * @size: size of the area
265 * @pa: physical address for the map
266 * @perm: device access permission (VHOST_MAP_XX)
267 * Returns integer: success (0) or error (< 0)
268 * @dma_unmap: Unmap an area of IOVA (optional but
269 * must be implemented with dma_map)
270 * Needed for device that using device
271 * specific DMA translation (on-chip IOMMU)
272 * and preferring incremental unmap.
274 * @asid: address space identifier
275 * @iova: iova to be unmapped
276 * @size: size of the area
277 * Returns integer: success (0) or error (< 0)
278 * @free: Free resources that belongs to vDPA (optional)
281 struct vdpa_config_ops {
283 int (*set_vq_address)(struct vdpa_device *vdev,
284 u16 idx, u64 desc_area, u64 driver_area,
286 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
287 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
288 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
289 struct vdpa_callback *cb);
290 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
291 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
292 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
293 const struct vdpa_vq_state *state);
294 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
295 struct vdpa_vq_state *state);
296 int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
298 struct netlink_ext_ack *extack);
299 struct vdpa_notification_area
300 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
301 /* vq irq is not expected to be changed once DRIVER_OK is set */
302 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
305 u32 (*get_vq_align)(struct vdpa_device *vdev);
306 u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
307 u64 (*get_device_features)(struct vdpa_device *vdev);
308 int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
309 u64 (*get_driver_features)(struct vdpa_device *vdev);
310 void (*set_config_cb)(struct vdpa_device *vdev,
311 struct vdpa_callback *cb);
312 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
313 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
314 u32 (*get_device_id)(struct vdpa_device *vdev);
315 u32 (*get_vendor_id)(struct vdpa_device *vdev);
316 u8 (*get_status)(struct vdpa_device *vdev);
317 void (*set_status)(struct vdpa_device *vdev, u8 status);
318 int (*reset)(struct vdpa_device *vdev);
319 size_t (*get_config_size)(struct vdpa_device *vdev);
320 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
321 void *buf, unsigned int len);
322 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
323 const void *buf, unsigned int len);
324 u32 (*get_generation)(struct vdpa_device *vdev);
325 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
328 int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
329 struct vhost_iotlb *iotlb);
330 int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
331 u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
332 int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
334 int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
337 /* Free device resources */
338 void (*free)(struct vdpa_device *vdev);
341 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
342 const struct vdpa_config_ops *config,
343 unsigned int ngroups, unsigned int nas,
344 size_t size, const char *name,
348 * vdpa_alloc_device - allocate and initilaize a vDPA device
350 * @dev_struct: the type of the parent structure
351 * @member: the name of struct vdpa_device within the @dev_struct
352 * @parent: the parent device
353 * @config: the bus operations that is supported by this device
354 * @ngroups: the number of virtqueue groups supported by this device
355 * @nas: the number of address spaces
356 * @name: name of the vdpa device
357 * @use_va: indicate whether virtual address must be used by this device
359 * Return allocated data structure or ERR_PTR upon error
361 #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
363 container_of((__vdpa_alloc_device( \
364 parent, config, ngroups, nas, \
365 (sizeof(dev_struct) + \
366 BUILD_BUG_ON_ZERO(offsetof( \
367 dev_struct, member))), name, use_va)), \
370 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
371 void vdpa_unregister_device(struct vdpa_device *vdev);
373 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
374 void _vdpa_unregister_device(struct vdpa_device *vdev);
377 * struct vdpa_driver - operations for a vDPA driver
378 * @driver: underlying device driver
379 * @probe: the function to call when a device is found. Returns 0 or -errno.
380 * @remove: the function to call when a device is removed.
383 struct device_driver driver;
384 int (*probe)(struct vdpa_device *vdev);
385 void (*remove)(struct vdpa_device *vdev);
388 #define vdpa_register_driver(drv) \
389 __vdpa_register_driver(drv, THIS_MODULE)
390 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
391 void vdpa_unregister_driver(struct vdpa_driver *drv);
393 #define module_vdpa_driver(__vdpa_driver) \
394 module_driver(__vdpa_driver, vdpa_register_driver, \
395 vdpa_unregister_driver)
397 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
399 return container_of(driver, struct vdpa_driver, driver);
402 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
404 return container_of(_dev, struct vdpa_device, dev);
407 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
409 return dev_get_drvdata(&vdev->dev);
412 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
414 dev_set_drvdata(&vdev->dev, data);
417 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
419 return vdev->dma_dev;
422 static inline int vdpa_reset(struct vdpa_device *vdev)
424 const struct vdpa_config_ops *ops = vdev->config;
427 down_write(&vdev->cf_lock);
428 vdev->features_valid = false;
429 ret = ops->reset(vdev);
430 up_write(&vdev->cf_lock);
434 static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
436 const struct vdpa_config_ops *ops = vdev->config;
439 vdev->features_valid = true;
440 ret = ops->set_driver_features(vdev, features);
445 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
449 down_write(&vdev->cf_lock);
450 ret = vdpa_set_features_unlocked(vdev, features);
451 up_write(&vdev->cf_lock);
456 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
457 void *buf, unsigned int len);
458 void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
459 const void *buf, unsigned int length);
460 void vdpa_set_status(struct vdpa_device *vdev, u8 status);
463 * struct vdpa_mgmtdev_ops - vdpa device ops
464 * @dev_add: Add a vdpa device using alloc and register
465 * @mdev: parent device to use for device addition
466 * @name: name of the new vdpa device
467 * @config: config attributes to apply to the device under creation
468 * Driver need to add a new device using _vdpa_register_device()
469 * after fully initializing the vdpa device. Driver must return 0
470 * on success or appropriate error code.
471 * @dev_del: Remove a vdpa device using unregister
472 * @mdev: parent device to use for device removal
473 * @dev: vdpa device to remove
474 * Driver need to remove the specified device by calling
475 * _vdpa_unregister_device().
477 struct vdpa_mgmtdev_ops {
478 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
479 const struct vdpa_dev_set_config *config);
480 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
484 * struct vdpa_mgmt_dev - vdpa management device
485 * @device: Management parent device
486 * @ops: operations supported by management device
487 * @id_table: Pointer to device id table of supported ids
488 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
489 * management device support during dev_add callback
492 struct vdpa_mgmt_dev {
493 struct device *device;
494 const struct vdpa_mgmtdev_ops *ops;
495 struct virtio_device_id *id_table;
496 u64 config_attr_mask;
497 struct list_head list;
498 u64 supported_features;
499 u32 max_supported_vqs;
502 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
503 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
505 #endif /* _LINUX_VDPA_H */