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_mutex: Protects get and set access to configuration layout.
70 * @index: device index
71 * @features_valid: were features initialized? for legacy guests
72 * @use_va: indicate whether virtual address must be used by this device
73 * @nvqs: maximum number of supported virtqueues
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().
79 struct device *dma_dev;
80 const char *driver_override;
81 const struct vdpa_config_ops *config;
82 struct mutex cf_mutex; /* Protects get/set config */
87 struct vdpa_mgmt_dev *mdev;
91 * struct vdpa_iova_range - the IOVA range support by the device
92 * @first: start of the IOVA range
93 * @last: end of the IOVA range
95 struct vdpa_iova_range {
100 struct vdpa_dev_set_config {
110 * Corresponding file area for device memory mapping
111 * @file: vma->vm_file for the mapping
112 * @offset: mapping offset in the vm_file
114 struct vdpa_map_file {
120 * struct vdpa_config_ops - operations for configuring a vDPA device.
121 * Note: vDPA device drivers are required to implement all of the
122 * operations unless it is mentioned to be optional in the following
125 * @set_vq_address: Set the address of virtqueue
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
134 * @idx: virtqueue index
135 * @num: the size of virtqueue
136 * @kick_vq: Kick the virtqueue
138 * @idx: virtqueue index
139 * @set_vq_cb: Set the interrupt callback function for
142 * @idx: virtqueue index
143 * @cb: virtio-vdev interrupt callback structure
144 * @set_vq_ready: Set ready status for a virtqueue
146 * @idx: virtqueue index
147 * @ready: ready (true) not ready(false)
148 * @get_vq_ready: Get ready status for a virtqueue
150 * @idx: virtqueue index
151 * Returns boolean: ready (true) or not (false)
152 * @set_vq_state: Set the state for a virtqueue
154 * @idx: virtqueue index
155 * @state: pointer to set virtqueue state (last_avail_idx)
156 * Returns integer: success (0) or error (< 0)
157 * @get_vq_state: Get the state for a virtqueue
159 * @idx: virtqueue index
160 * @state: pointer to returned state (last_avail_idx)
161 * @get_vq_notification: Get the notification area for a virtqueue (optional)
163 * @idx: virtqueue index
164 * Returns the notifcation area
165 * @get_vq_irq: Get the irq number of a virtqueue (optional,
166 * but must implemented if require vq irq offloading)
168 * @idx: virtqueue index
169 * Returns int: irq number of a virtqueue,
170 * negative number if no irq assigned.
171 * @get_vq_align: Get the virtqueue align requirement
174 * Returns virtqueue algin requirement
175 * @get_device_features: Get virtio features supported by the device
177 * Returns the virtio features support by the
179 * @set_driver_features: Set virtio features supported by the driver
181 * @features: feature support by the driver
182 * Returns integer: success (0) or error (< 0)
183 * @get_driver_features: Get the virtio driver features in action
185 * Returns the virtio features accepted
186 * @set_config_cb: Set the config interrupt callback
188 * @cb: virtio-vdev interrupt callback structure
189 * @get_vq_num_max: Get the max size of virtqueue
191 * Returns u16: max size of virtqueue
192 * @get_vq_num_min: Get the min size of virtqueue (optional)
194 * Returns u16: min size of virtqueue
195 * @get_device_id: Get virtio device id
197 * Returns u32: virtio device id
198 * @get_vendor_id: Get id for the vendor that provides this device
200 * Returns u32: virtio vendor id
201 * @get_status: Get the device status
203 * Returns u8: virtio device status
204 * @set_status: Set the device status
206 * @status: virtio device status
207 * @reset: Reset device
209 * Returns integer: success (0) or error (< 0)
210 * @get_config_size: Get the size of the configuration space
212 * Returns size_t: configuration size
213 * @get_config: Read from device specific configuration space
215 * @offset: offset from the beginning of
216 * configuration space
217 * @buf: buffer used to read to
218 * @len: the length to read from
219 * configuration space
220 * @set_config: Write to device specific configuration space
222 * @offset: offset from the beginning of
223 * configuration space
224 * @buf: buffer used to write from
225 * @len: the length to write to
226 * configuration space
227 * @get_generation: Get device config generation (optional)
229 * Returns u32: device generation
230 * @get_iova_range: Get supported iova range (optional)
232 * Returns the iova range supported by
234 * @set_map: Set device memory mapping (optional)
235 * Needed for device that using device
236 * specific DMA translation (on-chip IOMMU)
238 * @iotlb: vhost memory mapping to be
240 * Returns integer: success (0) or error (< 0)
241 * @dma_map: Map an area of PA to IOVA (optional)
242 * Needed for device that using device
243 * specific DMA translation (on-chip IOMMU)
244 * and preferring incremental map.
246 * @iova: iova to be mapped
247 * @size: size of the area
248 * @pa: physical address for the map
249 * @perm: device access permission (VHOST_MAP_XX)
250 * Returns integer: success (0) or error (< 0)
251 * @dma_unmap: Unmap an area of IOVA (optional but
252 * must be implemented with dma_map)
253 * Needed for device that using device
254 * specific DMA translation (on-chip IOMMU)
255 * and preferring incremental unmap.
257 * @iova: iova to be unmapped
258 * @size: size of the area
259 * Returns integer: success (0) or error (< 0)
260 * @free: Free resources that belongs to vDPA (optional)
263 struct vdpa_config_ops {
265 int (*set_vq_address)(struct vdpa_device *vdev,
266 u16 idx, u64 desc_area, u64 driver_area,
268 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
269 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
270 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
271 struct vdpa_callback *cb);
272 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
273 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
274 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
275 const struct vdpa_vq_state *state);
276 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
277 struct vdpa_vq_state *state);
278 struct vdpa_notification_area
279 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
280 /* vq irq is not expected to be changed once DRIVER_OK is set */
281 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
284 u32 (*get_vq_align)(struct vdpa_device *vdev);
285 u64 (*get_device_features)(struct vdpa_device *vdev);
286 int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
287 u64 (*get_driver_features)(struct vdpa_device *vdev);
288 void (*set_config_cb)(struct vdpa_device *vdev,
289 struct vdpa_callback *cb);
290 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
291 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
292 u32 (*get_device_id)(struct vdpa_device *vdev);
293 u32 (*get_vendor_id)(struct vdpa_device *vdev);
294 u8 (*get_status)(struct vdpa_device *vdev);
295 void (*set_status)(struct vdpa_device *vdev, u8 status);
296 int (*reset)(struct vdpa_device *vdev);
297 size_t (*get_config_size)(struct vdpa_device *vdev);
298 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
299 void *buf, unsigned int len);
300 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
301 const void *buf, unsigned int len);
302 u32 (*get_generation)(struct vdpa_device *vdev);
303 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
306 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
307 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
308 u64 pa, u32 perm, void *opaque);
309 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
311 /* Free device resources */
312 void (*free)(struct vdpa_device *vdev);
315 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
316 const struct vdpa_config_ops *config,
317 size_t size, const char *name,
321 * vdpa_alloc_device - allocate and initilaize a vDPA device
323 * @dev_struct: the type of the parent structure
324 * @member: the name of struct vdpa_device within the @dev_struct
325 * @parent: the parent device
326 * @config: the bus operations that is supported by this device
327 * @name: name of the vdpa device
328 * @use_va: indicate whether virtual address must be used by this device
330 * Return allocated data structure or ERR_PTR upon error
332 #define vdpa_alloc_device(dev_struct, member, parent, config, name, use_va) \
333 container_of(__vdpa_alloc_device( \
335 sizeof(dev_struct) + \
336 BUILD_BUG_ON_ZERO(offsetof( \
337 dev_struct, member)), name, use_va), \
340 int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
341 void vdpa_unregister_device(struct vdpa_device *vdev);
343 int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
344 void _vdpa_unregister_device(struct vdpa_device *vdev);
347 * struct vdpa_driver - operations for a vDPA driver
348 * @driver: underlying device driver
349 * @probe: the function to call when a device is found. Returns 0 or -errno.
350 * @remove: the function to call when a device is removed.
353 struct device_driver driver;
354 int (*probe)(struct vdpa_device *vdev);
355 void (*remove)(struct vdpa_device *vdev);
358 #define vdpa_register_driver(drv) \
359 __vdpa_register_driver(drv, THIS_MODULE)
360 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
361 void vdpa_unregister_driver(struct vdpa_driver *drv);
363 #define module_vdpa_driver(__vdpa_driver) \
364 module_driver(__vdpa_driver, vdpa_register_driver, \
365 vdpa_unregister_driver)
367 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
369 return container_of(driver, struct vdpa_driver, driver);
372 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
374 return container_of(_dev, struct vdpa_device, dev);
377 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
379 return dev_get_drvdata(&vdev->dev);
382 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
384 dev_set_drvdata(&vdev->dev, data);
387 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
389 return vdev->dma_dev;
392 static inline int vdpa_reset(struct vdpa_device *vdev)
394 const struct vdpa_config_ops *ops = vdev->config;
397 mutex_lock(&vdev->cf_mutex);
398 vdev->features_valid = false;
399 ret = ops->reset(vdev);
400 mutex_unlock(&vdev->cf_mutex);
404 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features, bool locked)
406 const struct vdpa_config_ops *ops = vdev->config;
410 mutex_lock(&vdev->cf_mutex);
412 vdev->features_valid = true;
413 ret = ops->set_driver_features(vdev, features);
415 mutex_unlock(&vdev->cf_mutex);
420 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
421 void *buf, unsigned int len);
422 void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
423 const void *buf, unsigned int length);
424 void vdpa_set_status(struct vdpa_device *vdev, u8 status);
427 * struct vdpa_mgmtdev_ops - vdpa device ops
428 * @dev_add: Add a vdpa device using alloc and register
429 * @mdev: parent device to use for device addition
430 * @name: name of the new vdpa device
431 * @config: config attributes to apply to the device under creation
432 * Driver need to add a new device using _vdpa_register_device()
433 * after fully initializing the vdpa device. Driver must return 0
434 * on success or appropriate error code.
435 * @dev_del: Remove a vdpa device using unregister
436 * @mdev: parent device to use for device removal
437 * @dev: vdpa device to remove
438 * Driver need to remove the specified device by calling
439 * _vdpa_unregister_device().
441 struct vdpa_mgmtdev_ops {
442 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
443 const struct vdpa_dev_set_config *config);
444 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
448 * struct vdpa_mgmt_dev - vdpa management device
449 * @device: Management parent device
450 * @ops: operations supported by management device
451 * @id_table: Pointer to device id table of supported ids
452 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
453 * management device support during dev_add callback
456 struct vdpa_mgmt_dev {
457 struct device *device;
458 const struct vdpa_mgmtdev_ops *ops;
459 const struct virtio_device_id *id_table;
460 u64 config_attr_mask;
461 struct list_head list;
462 u64 supported_features;
463 u32 max_supported_vqs;
466 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
467 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
469 #endif /* _LINUX_VDPA_H */