![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0 */ 0002 #ifndef _LINUX_VDPA_H 0003 #define _LINUX_VDPA_H 0004 0005 #include <linux/kernel.h> 0006 #include <linux/device.h> 0007 #include <linux/interrupt.h> 0008 #include <linux/vhost_iotlb.h> 0009 #include <linux/virtio_net.h> 0010 #include <linux/if_ether.h> 0011 0012 /** 0013 * struct vdpa_calllback - vDPA callback definition. 0014 * @callback: interrupt callback function 0015 * @private: the data passed to the callback function 0016 */ 0017 struct vdpa_callback { 0018 irqreturn_t (*callback)(void *data); 0019 void *private; 0020 }; 0021 0022 /** 0023 * struct vdpa_notification_area - vDPA notification area 0024 * @addr: base address of the notification area 0025 * @size: size of the notification area 0026 */ 0027 struct vdpa_notification_area { 0028 resource_size_t addr; 0029 resource_size_t size; 0030 }; 0031 0032 /** 0033 * struct vdpa_vq_state_split - vDPA split virtqueue state 0034 * @avail_index: available index 0035 */ 0036 struct vdpa_vq_state_split { 0037 u16 avail_index; 0038 }; 0039 0040 /** 0041 * struct vdpa_vq_state_packed - vDPA packed virtqueue state 0042 * @last_avail_counter: last driver ring wrap counter observed by device 0043 * @last_avail_idx: device available index 0044 * @last_used_counter: device ring wrap counter 0045 * @last_used_idx: used index 0046 */ 0047 struct vdpa_vq_state_packed { 0048 u16 last_avail_counter:1; 0049 u16 last_avail_idx:15; 0050 u16 last_used_counter:1; 0051 u16 last_used_idx:15; 0052 }; 0053 0054 struct vdpa_vq_state { 0055 union { 0056 struct vdpa_vq_state_split split; 0057 struct vdpa_vq_state_packed packed; 0058 }; 0059 }; 0060 0061 struct vdpa_mgmt_dev; 0062 0063 /** 0064 * struct vdpa_device - representation of a vDPA device 0065 * @dev: underlying device 0066 * @dma_dev: the actual device that is performing DMA 0067 * @driver_override: driver name to force a match; do not set directly, 0068 * because core frees it; use driver_set_override() to 0069 * set or clear it. 0070 * @config: the configuration ops for this device. 0071 * @cf_lock: Protects get and set access to configuration layout. 0072 * @index: device index 0073 * @features_valid: were features initialized? for legacy guests 0074 * @ngroups: the number of virtqueue groups 0075 * @nas: the number of address spaces 0076 * @use_va: indicate whether virtual address must be used by this device 0077 * @nvqs: maximum number of supported virtqueues 0078 * @mdev: management device pointer; caller must setup when registering device as part 0079 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device(). 0080 */ 0081 struct vdpa_device { 0082 struct device dev; 0083 struct device *dma_dev; 0084 const char *driver_override; 0085 const struct vdpa_config_ops *config; 0086 struct rw_semaphore cf_lock; /* Protects get/set config */ 0087 unsigned int index; 0088 bool features_valid; 0089 bool use_va; 0090 u32 nvqs; 0091 struct vdpa_mgmt_dev *mdev; 0092 unsigned int ngroups; 0093 unsigned int nas; 0094 }; 0095 0096 /** 0097 * struct vdpa_iova_range - the IOVA range support by the device 0098 * @first: start of the IOVA range 0099 * @last: end of the IOVA range 0100 */ 0101 struct vdpa_iova_range { 0102 u64 first; 0103 u64 last; 0104 }; 0105 0106 struct vdpa_dev_set_config { 0107 struct { 0108 u8 mac[ETH_ALEN]; 0109 u16 mtu; 0110 u16 max_vq_pairs; 0111 } net; 0112 u64 mask; 0113 }; 0114 0115 /** 0116 * Corresponding file area for device memory mapping 0117 * @file: vma->vm_file for the mapping 0118 * @offset: mapping offset in the vm_file 0119 */ 0120 struct vdpa_map_file { 0121 struct file *file; 0122 u64 offset; 0123 }; 0124 0125 /** 0126 * struct vdpa_config_ops - operations for configuring a vDPA device. 0127 * Note: vDPA device drivers are required to implement all of the 0128 * operations unless it is mentioned to be optional in the following 0129 * list. 0130 * 0131 * @set_vq_address: Set the address of virtqueue 0132 * @vdev: vdpa device 0133 * @idx: virtqueue index 0134 * @desc_area: address of desc area 0135 * @driver_area: address of driver area 0136 * @device_area: address of device area 0137 * Returns integer: success (0) or error (< 0) 0138 * @set_vq_num: Set the size of virtqueue 0139 * @vdev: vdpa device 0140 * @idx: virtqueue index 0141 * @num: the size of virtqueue 0142 * @kick_vq: Kick the virtqueue 0143 * @vdev: vdpa device 0144 * @idx: virtqueue index 0145 * @set_vq_cb: Set the interrupt callback function for 0146 * a virtqueue 0147 * @vdev: vdpa device 0148 * @idx: virtqueue index 0149 * @cb: virtio-vdev interrupt callback structure 0150 * @set_vq_ready: Set ready status for a virtqueue 0151 * @vdev: vdpa device 0152 * @idx: virtqueue index 0153 * @ready: ready (true) not ready(false) 0154 * @get_vq_ready: Get ready status for a virtqueue 0155 * @vdev: vdpa device 0156 * @idx: virtqueue index 0157 * Returns boolean: ready (true) or not (false) 0158 * @set_vq_state: Set the state for a virtqueue 0159 * @vdev: vdpa device 0160 * @idx: virtqueue index 0161 * @state: pointer to set virtqueue state (last_avail_idx) 0162 * Returns integer: success (0) or error (< 0) 0163 * @get_vq_state: Get the state for a virtqueue 0164 * @vdev: vdpa device 0165 * @idx: virtqueue index 0166 * @state: pointer to returned state (last_avail_idx) 0167 * @get_vq_notification: Get the notification area for a virtqueue (optional) 0168 * @vdev: vdpa device 0169 * @idx: virtqueue index 0170 * Returns the notifcation area 0171 * @get_vq_irq: Get the irq number of a virtqueue (optional, 0172 * but must implemented if require vq irq offloading) 0173 * @vdev: vdpa device 0174 * @idx: virtqueue index 0175 * Returns int: irq number of a virtqueue, 0176 * negative number if no irq assigned. 0177 * @get_vq_align: Get the virtqueue align requirement 0178 * for the device 0179 * @vdev: vdpa device 0180 * Returns virtqueue algin requirement 0181 * @get_vq_group: Get the group id for a specific 0182 * virtqueue (optional) 0183 * @vdev: vdpa device 0184 * @idx: virtqueue index 0185 * Returns u32: group id for this virtqueue 0186 * @get_device_features: Get virtio features supported by the device 0187 * @vdev: vdpa device 0188 * Returns the virtio features support by the 0189 * device 0190 * @set_driver_features: Set virtio features supported by the driver 0191 * @vdev: vdpa device 0192 * @features: feature support by the driver 0193 * Returns integer: success (0) or error (< 0) 0194 * @get_driver_features: Get the virtio driver features in action 0195 * @vdev: vdpa device 0196 * Returns the virtio features accepted 0197 * @set_config_cb: Set the config interrupt callback 0198 * @vdev: vdpa device 0199 * @cb: virtio-vdev interrupt callback structure 0200 * @get_vq_num_max: Get the max size of virtqueue 0201 * @vdev: vdpa device 0202 * Returns u16: max size of virtqueue 0203 * @get_vq_num_min: Get the min size of virtqueue (optional) 0204 * @vdev: vdpa device 0205 * Returns u16: min size of virtqueue 0206 * @get_device_id: Get virtio device id 0207 * @vdev: vdpa device 0208 * Returns u32: virtio device id 0209 * @get_vendor_id: Get id for the vendor that provides this device 0210 * @vdev: vdpa device 0211 * Returns u32: virtio vendor id 0212 * @get_status: Get the device status 0213 * @vdev: vdpa device 0214 * Returns u8: virtio device status 0215 * @set_status: Set the device status 0216 * @vdev: vdpa device 0217 * @status: virtio device status 0218 * @reset: Reset device 0219 * @vdev: vdpa device 0220 * Returns integer: success (0) or error (< 0) 0221 * @suspend: Suspend or resume the device (optional) 0222 * @vdev: vdpa device 0223 * Returns integer: success (0) or error (< 0) 0224 * @get_config_size: Get the size of the configuration space includes 0225 * fields that are conditional on feature bits. 0226 * @vdev: vdpa device 0227 * Returns size_t: configuration size 0228 * @get_config: Read from device specific configuration space 0229 * @vdev: vdpa device 0230 * @offset: offset from the beginning of 0231 * configuration space 0232 * @buf: buffer used to read to 0233 * @len: the length to read from 0234 * configuration space 0235 * @set_config: Write to device specific configuration space 0236 * @vdev: vdpa device 0237 * @offset: offset from the beginning of 0238 * configuration space 0239 * @buf: buffer used to write from 0240 * @len: the length to write to 0241 * configuration space 0242 * @get_generation: Get device config generation (optional) 0243 * @vdev: vdpa device 0244 * Returns u32: device generation 0245 * @get_iova_range: Get supported iova range (optional) 0246 * @vdev: vdpa device 0247 * Returns the iova range supported by 0248 * the device. 0249 * @set_group_asid: Set address space identifier for a 0250 * virtqueue group (optional) 0251 * @vdev: vdpa device 0252 * @group: virtqueue group 0253 * @asid: address space id for this group 0254 * Returns integer: success (0) or error (< 0) 0255 * @set_map: Set device memory mapping (optional) 0256 * Needed for device that using device 0257 * specific DMA translation (on-chip IOMMU) 0258 * @vdev: vdpa device 0259 * @asid: address space identifier 0260 * @iotlb: vhost memory mapping to be 0261 * used by the vDPA 0262 * Returns integer: success (0) or error (< 0) 0263 * @dma_map: Map an area of PA to IOVA (optional) 0264 * Needed for device that using device 0265 * specific DMA translation (on-chip IOMMU) 0266 * and preferring incremental map. 0267 * @vdev: vdpa device 0268 * @asid: address space identifier 0269 * @iova: iova to be mapped 0270 * @size: size of the area 0271 * @pa: physical address for the map 0272 * @perm: device access permission (VHOST_MAP_XX) 0273 * Returns integer: success (0) or error (< 0) 0274 * @dma_unmap: Unmap an area of IOVA (optional but 0275 * must be implemented with dma_map) 0276 * Needed for device that using device 0277 * specific DMA translation (on-chip IOMMU) 0278 * and preferring incremental unmap. 0279 * @vdev: vdpa device 0280 * @asid: address space identifier 0281 * @iova: iova to be unmapped 0282 * @size: size of the area 0283 * Returns integer: success (0) or error (< 0) 0284 * @free: Free resources that belongs to vDPA (optional) 0285 * @vdev: vdpa device 0286 */ 0287 struct vdpa_config_ops { 0288 /* Virtqueue ops */ 0289 int (*set_vq_address)(struct vdpa_device *vdev, 0290 u16 idx, u64 desc_area, u64 driver_area, 0291 u64 device_area); 0292 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num); 0293 void (*kick_vq)(struct vdpa_device *vdev, u16 idx); 0294 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx, 0295 struct vdpa_callback *cb); 0296 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready); 0297 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx); 0298 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, 0299 const struct vdpa_vq_state *state); 0300 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx, 0301 struct vdpa_vq_state *state); 0302 int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx, 0303 struct sk_buff *msg, 0304 struct netlink_ext_ack *extack); 0305 struct vdpa_notification_area 0306 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); 0307 /* vq irq is not expected to be changed once DRIVER_OK is set */ 0308 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx); 0309 0310 /* Device ops */ 0311 u32 (*get_vq_align)(struct vdpa_device *vdev); 0312 u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx); 0313 u64 (*get_device_features)(struct vdpa_device *vdev); 0314 int (*set_driver_features)(struct vdpa_device *vdev, u64 features); 0315 u64 (*get_driver_features)(struct vdpa_device *vdev); 0316 void (*set_config_cb)(struct vdpa_device *vdev, 0317 struct vdpa_callback *cb); 0318 u16 (*get_vq_num_max)(struct vdpa_device *vdev); 0319 u16 (*get_vq_num_min)(struct vdpa_device *vdev); 0320 u32 (*get_device_id)(struct vdpa_device *vdev); 0321 u32 (*get_vendor_id)(struct vdpa_device *vdev); 0322 u8 (*get_status)(struct vdpa_device *vdev); 0323 void (*set_status)(struct vdpa_device *vdev, u8 status); 0324 int (*reset)(struct vdpa_device *vdev); 0325 int (*suspend)(struct vdpa_device *vdev); 0326 size_t (*get_config_size)(struct vdpa_device *vdev); 0327 void (*get_config)(struct vdpa_device *vdev, unsigned int offset, 0328 void *buf, unsigned int len); 0329 void (*set_config)(struct vdpa_device *vdev, unsigned int offset, 0330 const void *buf, unsigned int len); 0331 u32 (*get_generation)(struct vdpa_device *vdev); 0332 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); 0333 0334 /* DMA ops */ 0335 int (*set_map)(struct vdpa_device *vdev, unsigned int asid, 0336 struct vhost_iotlb *iotlb); 0337 int (*dma_map)(struct vdpa_device *vdev, unsigned int asid, 0338 u64 iova, u64 size, u64 pa, u32 perm, void *opaque); 0339 int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid, 0340 u64 iova, u64 size); 0341 int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group, 0342 unsigned int asid); 0343 0344 /* Free device resources */ 0345 void (*free)(struct vdpa_device *vdev); 0346 }; 0347 0348 struct vdpa_device *__vdpa_alloc_device(struct device *parent, 0349 const struct vdpa_config_ops *config, 0350 unsigned int ngroups, unsigned int nas, 0351 size_t size, const char *name, 0352 bool use_va); 0353 0354 /** 0355 * vdpa_alloc_device - allocate and initilaize a vDPA device 0356 * 0357 * @dev_struct: the type of the parent structure 0358 * @member: the name of struct vdpa_device within the @dev_struct 0359 * @parent: the parent device 0360 * @config: the bus operations that is supported by this device 0361 * @ngroups: the number of virtqueue groups supported by this device 0362 * @nas: the number of address spaces 0363 * @name: name of the vdpa device 0364 * @use_va: indicate whether virtual address must be used by this device 0365 * 0366 * Return allocated data structure or ERR_PTR upon error 0367 */ 0368 #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \ 0369 name, use_va) \ 0370 container_of((__vdpa_alloc_device( \ 0371 parent, config, ngroups, nas, \ 0372 (sizeof(dev_struct) + \ 0373 BUILD_BUG_ON_ZERO(offsetof( \ 0374 dev_struct, member))), name, use_va)), \ 0375 dev_struct, member) 0376 0377 int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); 0378 void vdpa_unregister_device(struct vdpa_device *vdev); 0379 0380 int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); 0381 void _vdpa_unregister_device(struct vdpa_device *vdev); 0382 0383 /** 0384 * struct vdpa_driver - operations for a vDPA driver 0385 * @driver: underlying device driver 0386 * @probe: the function to call when a device is found. Returns 0 or -errno. 0387 * @remove: the function to call when a device is removed. 0388 */ 0389 struct vdpa_driver { 0390 struct device_driver driver; 0391 int (*probe)(struct vdpa_device *vdev); 0392 void (*remove)(struct vdpa_device *vdev); 0393 }; 0394 0395 #define vdpa_register_driver(drv) \ 0396 __vdpa_register_driver(drv, THIS_MODULE) 0397 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); 0398 void vdpa_unregister_driver(struct vdpa_driver *drv); 0399 0400 #define module_vdpa_driver(__vdpa_driver) \ 0401 module_driver(__vdpa_driver, vdpa_register_driver, \ 0402 vdpa_unregister_driver) 0403 0404 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) 0405 { 0406 return container_of(driver, struct vdpa_driver, driver); 0407 } 0408 0409 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) 0410 { 0411 return container_of(_dev, struct vdpa_device, dev); 0412 } 0413 0414 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) 0415 { 0416 return dev_get_drvdata(&vdev->dev); 0417 } 0418 0419 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) 0420 { 0421 dev_set_drvdata(&vdev->dev, data); 0422 } 0423 0424 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) 0425 { 0426 return vdev->dma_dev; 0427 } 0428 0429 static inline int vdpa_reset(struct vdpa_device *vdev) 0430 { 0431 const struct vdpa_config_ops *ops = vdev->config; 0432 int ret; 0433 0434 down_write(&vdev->cf_lock); 0435 vdev->features_valid = false; 0436 ret = ops->reset(vdev); 0437 up_write(&vdev->cf_lock); 0438 return ret; 0439 } 0440 0441 static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features) 0442 { 0443 const struct vdpa_config_ops *ops = vdev->config; 0444 int ret; 0445 0446 vdev->features_valid = true; 0447 ret = ops->set_driver_features(vdev, features); 0448 0449 return ret; 0450 } 0451 0452 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) 0453 { 0454 int ret; 0455 0456 down_write(&vdev->cf_lock); 0457 ret = vdpa_set_features_unlocked(vdev, features); 0458 up_write(&vdev->cf_lock); 0459 0460 return ret; 0461 } 0462 0463 void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, 0464 void *buf, unsigned int len); 0465 void vdpa_set_config(struct vdpa_device *dev, unsigned int offset, 0466 const void *buf, unsigned int length); 0467 void vdpa_set_status(struct vdpa_device *vdev, u8 status); 0468 0469 /** 0470 * struct vdpa_mgmtdev_ops - vdpa device ops 0471 * @dev_add: Add a vdpa device using alloc and register 0472 * @mdev: parent device to use for device addition 0473 * @name: name of the new vdpa device 0474 * @config: config attributes to apply to the device under creation 0475 * Driver need to add a new device using _vdpa_register_device() 0476 * after fully initializing the vdpa device. Driver must return 0 0477 * on success or appropriate error code. 0478 * @dev_del: Remove a vdpa device using unregister 0479 * @mdev: parent device to use for device removal 0480 * @dev: vdpa device to remove 0481 * Driver need to remove the specified device by calling 0482 * _vdpa_unregister_device(). 0483 */ 0484 struct vdpa_mgmtdev_ops { 0485 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name, 0486 const struct vdpa_dev_set_config *config); 0487 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev); 0488 }; 0489 0490 /** 0491 * struct vdpa_mgmt_dev - vdpa management device 0492 * @device: Management parent device 0493 * @ops: operations supported by management device 0494 * @id_table: Pointer to device id table of supported ids 0495 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that 0496 * management device support during dev_add callback 0497 * @list: list entry 0498 */ 0499 struct vdpa_mgmt_dev { 0500 struct device *device; 0501 const struct vdpa_mgmtdev_ops *ops; 0502 struct virtio_device_id *id_table; 0503 u64 config_attr_mask; 0504 struct list_head list; 0505 u64 supported_features; 0506 u32 max_supported_vqs; 0507 }; 0508 0509 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev); 0510 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev); 0511 0512 #endif /* _LINUX_VDPA_H */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |