1961e9c84SJason Wang /* SPDX-License-Identifier: GPL-2.0 */ 2961e9c84SJason Wang #ifndef _LINUX_VDPA_H 3961e9c84SJason Wang #define _LINUX_VDPA_H 4961e9c84SJason Wang 5961e9c84SJason Wang #include <linux/kernel.h> 6961e9c84SJason Wang #include <linux/device.h> 7961e9c84SJason Wang #include <linux/interrupt.h> 8961e9c84SJason Wang #include <linux/vhost_iotlb.h> 9d8ca2fa5SParav Pandit #include <linux/virtio_net.h> 10d8ca2fa5SParav Pandit #include <linux/if_ether.h> 11961e9c84SJason Wang 12961e9c84SJason Wang /** 139be5d2d4SSimon Horman * struct vdpa_callback - vDPA callback definition. 14961e9c84SJason Wang * @callback: interrupt callback function 15961e9c84SJason Wang * @private: the data passed to the callback function 165e68470fSXie Yongji * @trigger: the eventfd for the callback (Optional). 175e68470fSXie Yongji * When it is set, the vDPA driver must guarantee that 185e68470fSXie Yongji * signaling it is functional equivalent to triggering 195e68470fSXie Yongji * the callback. Then vDPA parent can signal it directly 205e68470fSXie Yongji * instead of triggering the callback. 21961e9c84SJason Wang */ 22961e9c84SJason Wang struct vdpa_callback { 23961e9c84SJason Wang irqreturn_t (*callback)(void *data); 24961e9c84SJason Wang void *private; 255e68470fSXie Yongji struct eventfd_ctx *trigger; 26961e9c84SJason Wang }; 27961e9c84SJason Wang 28961e9c84SJason Wang /** 29d0f9164eSParav Pandit * struct vdpa_notification_area - vDPA notification area 30c25a26e6SJason Wang * @addr: base address of the notification area 31c25a26e6SJason Wang * @size: size of the notification area 32c25a26e6SJason Wang */ 33c25a26e6SJason Wang struct vdpa_notification_area { 34c25a26e6SJason Wang resource_size_t addr; 35c25a26e6SJason Wang resource_size_t size; 36c25a26e6SJason Wang }; 37c25a26e6SJason Wang 38c25a26e6SJason Wang /** 39530a5678SJason Wang * struct vdpa_vq_state_split - vDPA split virtqueue state 40aac50c0bSEli Cohen * @avail_index: available index 41aac50c0bSEli Cohen */ 42530a5678SJason Wang struct vdpa_vq_state_split { 43aac50c0bSEli Cohen u16 avail_index; 44aac50c0bSEli Cohen }; 45aac50c0bSEli Cohen 46530a5678SJason Wang /** 47530a5678SJason Wang * struct vdpa_vq_state_packed - vDPA packed virtqueue state 48530a5678SJason Wang * @last_avail_counter: last driver ring wrap counter observed by device 49530a5678SJason Wang * @last_avail_idx: device available index 50530a5678SJason Wang * @last_used_counter: device ring wrap counter 51530a5678SJason Wang * @last_used_idx: used index 52530a5678SJason Wang */ 53530a5678SJason Wang struct vdpa_vq_state_packed { 54530a5678SJason Wang u16 last_avail_counter:1; 55530a5678SJason Wang u16 last_avail_idx:15; 56530a5678SJason Wang u16 last_used_counter:1; 57530a5678SJason Wang u16 last_used_idx:15; 58530a5678SJason Wang }; 59530a5678SJason Wang 60530a5678SJason Wang struct vdpa_vq_state { 61530a5678SJason Wang union { 62530a5678SJason Wang struct vdpa_vq_state_split split; 63530a5678SJason Wang struct vdpa_vq_state_packed packed; 64530a5678SJason Wang }; 65530a5678SJason Wang }; 66530a5678SJason Wang 6733b34750SParav Pandit struct vdpa_mgmt_dev; 6833b34750SParav Pandit 69aac50c0bSEli Cohen /** 70d0f9164eSParav Pandit * struct vdpa_device - representation of a vDPA device 71961e9c84SJason Wang * @dev: underlying device 72961e9c84SJason Wang * @dma_dev: the actual device that is performing DMA 73240bf4e6SKrzysztof Kozlowski * @driver_override: driver name to force a match; do not set directly, 74240bf4e6SKrzysztof Kozlowski * because core frees it; use driver_set_override() to 75240bf4e6SKrzysztof Kozlowski * set or clear it. 76961e9c84SJason Wang * @config: the configuration ops for this device. 77a6a51adcSEli Cohen * @cf_lock: Protects get and set access to configuration layout. 78961e9c84SJason Wang * @index: device index 79452639a6SMichael S. Tsirkin * @features_valid: were features initialized? for legacy guests 80db9adcbfSGautam Dawar * @ngroups: the number of virtqueue groups 81db9adcbfSGautam Dawar * @nas: the number of address spaces 82d8945ec4SXie Yongji * @use_va: indicate whether virtual address must be used by this device 83476c135eSParav Pandit * @nvqs: maximum number of supported virtqueues 84903f7bcaSParav Pandit * @mdev: management device pointer; caller must setup when registering device as part 85903f7bcaSParav Pandit * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device(). 86961e9c84SJason Wang */ 87961e9c84SJason Wang struct vdpa_device { 88961e9c84SJason Wang struct device dev; 89961e9c84SJason Wang struct device *dma_dev; 90539fec78SStefano Garzarella const char *driver_override; 91961e9c84SJason Wang const struct vdpa_config_ops *config; 92a6a51adcSEli Cohen struct rw_semaphore cf_lock; /* Protects get/set config */ 93961e9c84SJason Wang unsigned int index; 94452639a6SMichael S. Tsirkin bool features_valid; 95d8945ec4SXie Yongji bool use_va; 9681d46d69SLongpeng u32 nvqs; 97903f7bcaSParav Pandit struct vdpa_mgmt_dev *mdev; 98d4821902SGautam Dawar unsigned int ngroups; 99db9adcbfSGautam Dawar unsigned int nas; 100961e9c84SJason Wang }; 101961e9c84SJason Wang 102961e9c84SJason Wang /** 103d0f9164eSParav Pandit * struct vdpa_iova_range - the IOVA range support by the device 1043f1b623aSJason Wang * @first: start of the IOVA range 1053f1b623aSJason Wang * @last: end of the IOVA range 1063f1b623aSJason Wang */ 1073f1b623aSJason Wang struct vdpa_iova_range { 1083f1b623aSJason Wang u64 first; 1093f1b623aSJason Wang u64 last; 1103f1b623aSJason Wang }; 1113f1b623aSJason Wang 112d8ca2fa5SParav Pandit struct vdpa_dev_set_config { 11390fea5a8SJason Wang u64 device_features; 114d8ca2fa5SParav Pandit struct { 115d8ca2fa5SParav Pandit u8 mac[ETH_ALEN]; 116d8ca2fa5SParav Pandit u16 mtu; 117aba21affSEli Cohen u16 max_vq_pairs; 118d8ca2fa5SParav Pandit } net; 119d8ca2fa5SParav Pandit u64 mask; 120d8ca2fa5SParav Pandit }; 121d8ca2fa5SParav Pandit 1223f1b623aSJason Wang /** 1239be5d2d4SSimon Horman * struct vdpa_map_file - file area for device memory mapping 124d8945ec4SXie Yongji * @file: vma->vm_file for the mapping 125d8945ec4SXie Yongji * @offset: mapping offset in the vm_file 126d8945ec4SXie Yongji */ 127d8945ec4SXie Yongji struct vdpa_map_file { 128d8945ec4SXie Yongji struct file *file; 129d8945ec4SXie Yongji u64 offset; 130d8945ec4SXie Yongji }; 131d8945ec4SXie Yongji 132d8945ec4SXie Yongji /** 133d0f9164eSParav Pandit * struct vdpa_config_ops - operations for configuring a vDPA device. 134961e9c84SJason Wang * Note: vDPA device drivers are required to implement all of the 135961e9c84SJason Wang * operations unless it is mentioned to be optional in the following 136961e9c84SJason Wang * list. 137961e9c84SJason Wang * 138961e9c84SJason Wang * @set_vq_address: Set the address of virtqueue 139961e9c84SJason Wang * @vdev: vdpa device 140961e9c84SJason Wang * @idx: virtqueue index 141961e9c84SJason Wang * @desc_area: address of desc area 142961e9c84SJason Wang * @driver_area: address of driver area 143961e9c84SJason Wang * @device_area: address of device area 144961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 145961e9c84SJason Wang * @set_vq_num: Set the size of virtqueue 146961e9c84SJason Wang * @vdev: vdpa device 147961e9c84SJason Wang * @idx: virtqueue index 148961e9c84SJason Wang * @num: the size of virtqueue 149961e9c84SJason Wang * @kick_vq: Kick the virtqueue 150961e9c84SJason Wang * @vdev: vdpa device 151961e9c84SJason Wang * @idx: virtqueue index 1522c4e4a22SAlvaro Karsz * @kick_vq_with_data: Kick the virtqueue and supply extra data 1532c4e4a22SAlvaro Karsz * (only if VIRTIO_F_NOTIFICATION_DATA is negotiated) 1542c4e4a22SAlvaro Karsz * @vdev: vdpa device 1552c4e4a22SAlvaro Karsz * @data for split virtqueue: 1562c4e4a22SAlvaro Karsz * 16 bits vqn and 16 bits next available index. 1572c4e4a22SAlvaro Karsz * @data for packed virtqueue: 1582c4e4a22SAlvaro Karsz * 16 bits vqn, 15 least significant bits of 1592c4e4a22SAlvaro Karsz * next available index and 1 bit next_wrap. 160961e9c84SJason Wang * @set_vq_cb: Set the interrupt callback function for 161961e9c84SJason Wang * a virtqueue 162961e9c84SJason Wang * @vdev: vdpa device 163961e9c84SJason Wang * @idx: virtqueue index 164961e9c84SJason Wang * @cb: virtio-vdev interrupt callback structure 165961e9c84SJason Wang * @set_vq_ready: Set ready status for a virtqueue 166961e9c84SJason Wang * @vdev: vdpa device 167961e9c84SJason Wang * @idx: virtqueue index 168961e9c84SJason Wang * @ready: ready (true) not ready(false) 169961e9c84SJason Wang * @get_vq_ready: Get ready status for a virtqueue 170961e9c84SJason Wang * @vdev: vdpa device 171961e9c84SJason Wang * @idx: virtqueue index 172961e9c84SJason Wang * Returns boolean: ready (true) or not (false) 173961e9c84SJason Wang * @set_vq_state: Set the state for a virtqueue 174961e9c84SJason Wang * @vdev: vdpa device 175961e9c84SJason Wang * @idx: virtqueue index 176aac50c0bSEli Cohen * @state: pointer to set virtqueue state (last_avail_idx) 177961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 178961e9c84SJason Wang * @get_vq_state: Get the state for a virtqueue 179961e9c84SJason Wang * @vdev: vdpa device 180961e9c84SJason Wang * @idx: virtqueue index 181aac50c0bSEli Cohen * @state: pointer to returned state (last_avail_idx) 1829be5d2d4SSimon Horman * @get_vendor_vq_stats: Get the vendor statistics of a device. 1839be5d2d4SSimon Horman * @vdev: vdpa device 1849be5d2d4SSimon Horman * @idx: virtqueue index 1859be5d2d4SSimon Horman * @msg: socket buffer holding stats message 1869be5d2d4SSimon Horman * @extack: extack for reporting error messages 1879be5d2d4SSimon Horman * Returns integer: success (0) or error (< 0) 18828cc408bSEugenio Pérez * @get_vq_notification: Get the notification area for a virtqueue (optional) 189c25a26e6SJason Wang * @vdev: vdpa device 190c25a26e6SJason Wang * @idx: virtqueue index 1919be5d2d4SSimon Horman * Returns the notification area 1927164675aSZhu Lingshan * @get_vq_irq: Get the irq number of a virtqueue (optional, 1937164675aSZhu Lingshan * but must implemented if require vq irq offloading) 1947164675aSZhu Lingshan * @vdev: vdpa device 1957164675aSZhu Lingshan * @idx: virtqueue index 1967164675aSZhu Lingshan * Returns int: irq number of a virtqueue, 1977164675aSZhu Lingshan * negative number if no irq assigned. 198961e9c84SJason Wang * @get_vq_align: Get the virtqueue align requirement 199961e9c84SJason Wang * for the device 200961e9c84SJason Wang * @vdev: vdpa device 201961e9c84SJason Wang * Returns virtqueue algin requirement 20200d1f546SJason Wang * @get_vq_group: Get the group id for a specific 20300d1f546SJason Wang * virtqueue (optional) 204d4821902SGautam Dawar * @vdev: vdpa device 205d4821902SGautam Dawar * @idx: virtqueue index 206d4821902SGautam Dawar * Returns u32: group id for this virtqueue 207a72cac60SSi-Wei Liu * @get_vq_desc_group: Get the group id for the descriptor table of 208a72cac60SSi-Wei Liu * a specific virtqueue (optional) 209a72cac60SSi-Wei Liu * @vdev: vdpa device 210a72cac60SSi-Wei Liu * @idx: virtqueue index 211a72cac60SSi-Wei Liu * Returns u32: group id for the descriptor table 212a72cac60SSi-Wei Liu * portion of this virtqueue. Could be different 213a72cac60SSi-Wei Liu * than the one from @get_vq_group, in which case 214a72cac60SSi-Wei Liu * the access to the descriptor table can be 215a72cac60SSi-Wei Liu * confined to a separate asid, isolating from 216a72cac60SSi-Wei Liu * the virtqueue's buffer address access. 217a64917bcSEli Cohen * @get_device_features: Get virtio features supported by the device 218961e9c84SJason Wang * @vdev: vdpa device 219961e9c84SJason Wang * Returns the virtio features support by the 220961e9c84SJason Wang * device 221b63e5c70SEugenio Pérez * @get_backend_features: Get parent-specific backend features (optional) 222b63e5c70SEugenio Pérez * Returns the vdpa features supported by the 223b63e5c70SEugenio Pérez * device. 224a64917bcSEli Cohen * @set_driver_features: Set virtio features supported by the driver 225961e9c84SJason Wang * @vdev: vdpa device 226961e9c84SJason Wang * @features: feature support by the driver 227961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 228a64917bcSEli Cohen * @get_driver_features: Get the virtio driver features in action 229a64917bcSEli Cohen * @vdev: vdpa device 230a64917bcSEli Cohen * Returns the virtio features accepted 231961e9c84SJason Wang * @set_config_cb: Set the config interrupt callback 232961e9c84SJason Wang * @vdev: vdpa device 233961e9c84SJason Wang * @cb: virtio-vdev interrupt callback structure 234961e9c84SJason Wang * @get_vq_num_max: Get the max size of virtqueue 235961e9c84SJason Wang * @vdev: vdpa device 236961e9c84SJason Wang * Returns u16: max size of virtqueue 2373b970a58SWu Zongyong * @get_vq_num_min: Get the min size of virtqueue (optional) 2383b970a58SWu Zongyong * @vdev: vdpa device 2393b970a58SWu Zongyong * Returns u16: min size of virtqueue 240961e9c84SJason Wang * @get_device_id: Get virtio device id 241961e9c84SJason Wang * @vdev: vdpa device 242961e9c84SJason Wang * Returns u32: virtio device id 243961e9c84SJason Wang * @get_vendor_id: Get id for the vendor that provides this device 244961e9c84SJason Wang * @vdev: vdpa device 245961e9c84SJason Wang * Returns u32: virtio vendor id 246961e9c84SJason Wang * @get_status: Get the device status 247961e9c84SJason Wang * @vdev: vdpa device 248961e9c84SJason Wang * Returns u8: virtio device status 249961e9c84SJason Wang * @set_status: Set the device status 250961e9c84SJason Wang * @vdev: vdpa device 251961e9c84SJason Wang * @status: virtio device status 2520686082dSXie Yongji * @reset: Reset device 2530686082dSXie Yongji * @vdev: vdpa device 2540686082dSXie Yongji * Returns integer: success (0) or error (< 0) 255*a26f2e4eSSi-Wei Liu * @compat_reset: Reset device with compatibility quirks to 256*a26f2e4eSSi-Wei Liu * accommodate older userspace. Only needed by 257*a26f2e4eSSi-Wei Liu * parent driver which used to have bogus reset 258*a26f2e4eSSi-Wei Liu * behaviour, and has to maintain such behaviour 259*a26f2e4eSSi-Wei Liu * for compatibility with older userspace. 260*a26f2e4eSSi-Wei Liu * Historically compliant driver only has to 261*a26f2e4eSSi-Wei Liu * implement .reset, Historically non-compliant 262*a26f2e4eSSi-Wei Liu * driver should implement both. 263*a26f2e4eSSi-Wei Liu * @vdev: vdpa device 264*a26f2e4eSSi-Wei Liu * @flags: compatibility quirks for reset 265*a26f2e4eSSi-Wei Liu * Returns integer: success (0) or error (< 0) 2661538a8a4SSebastien Boeuf * @suspend: Suspend the device (optional) 2671538a8a4SSebastien Boeuf * @vdev: vdpa device 2681538a8a4SSebastien Boeuf * Returns integer: success (0) or error (< 0) 2691538a8a4SSebastien Boeuf * @resume: Resume the device (optional) 270848ecea1SEugenio Pérez * @vdev: vdpa device 271848ecea1SEugenio Pérez * Returns integer: success (0) or error (< 0) 272a61280ddSLongpeng * @get_config_size: Get the size of the configuration space includes 273a61280ddSLongpeng * fields that are conditional on feature bits. 274442706f9SStefano Garzarella * @vdev: vdpa device 275442706f9SStefano Garzarella * Returns size_t: configuration size 276961e9c84SJason Wang * @get_config: Read from device specific configuration space 277961e9c84SJason Wang * @vdev: vdpa device 278961e9c84SJason Wang * @offset: offset from the beginning of 279961e9c84SJason Wang * configuration space 280961e9c84SJason Wang * @buf: buffer used to read to 281961e9c84SJason Wang * @len: the length to read from 282961e9c84SJason Wang * configuration space 283961e9c84SJason Wang * @set_config: Write to device specific configuration space 284961e9c84SJason Wang * @vdev: vdpa device 285961e9c84SJason Wang * @offset: offset from the beginning of 286961e9c84SJason Wang * configuration space 287961e9c84SJason Wang * @buf: buffer used to write from 288961e9c84SJason Wang * @len: the length to write to 289961e9c84SJason Wang * configuration space 290961e9c84SJason Wang * @get_generation: Get device config generation (optional) 291961e9c84SJason Wang * @vdev: vdpa device 292961e9c84SJason Wang * Returns u32: device generation 2933f1b623aSJason Wang * @get_iova_range: Get supported iova range (optional) 2943f1b623aSJason Wang * @vdev: vdpa device 2953f1b623aSJason Wang * Returns the iova range supported by 2963f1b623aSJason Wang * the device. 2971d246927SXie Yongji * @set_vq_affinity: Set the affinity of virtqueue (optional) 2981d246927SXie Yongji * @vdev: vdpa device 2991d246927SXie Yongji * @idx: virtqueue index 3001d246927SXie Yongji * @cpu_mask: the affinity mask 3011d246927SXie Yongji * Returns integer: success (0) or error (< 0) 3021d246927SXie Yongji * @get_vq_affinity: Get the affinity of virtqueue (optional) 3031d246927SXie Yongji * @vdev: vdpa device 3041d246927SXie Yongji * @idx: virtqueue index 3051d246927SXie Yongji * Returns the affinity mask 30646d554b1SGautam Dawar * @set_group_asid: Set address space identifier for a 30700d1f546SJason Wang * virtqueue group (optional) 30846d554b1SGautam Dawar * @vdev: vdpa device 30946d554b1SGautam Dawar * @group: virtqueue group 31046d554b1SGautam Dawar * @asid: address space id for this group 31146d554b1SGautam Dawar * Returns integer: success (0) or error (< 0) 312961e9c84SJason Wang * @set_map: Set device memory mapping (optional) 313961e9c84SJason Wang * Needed for device that using device 314961e9c84SJason Wang * specific DMA translation (on-chip IOMMU) 315961e9c84SJason Wang * @vdev: vdpa device 316db9adcbfSGautam Dawar * @asid: address space identifier 317961e9c84SJason Wang * @iotlb: vhost memory mapping to be 318961e9c84SJason Wang * used by the vDPA 319961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 320961e9c84SJason Wang * @dma_map: Map an area of PA to IOVA (optional) 321961e9c84SJason Wang * Needed for device that using device 322961e9c84SJason Wang * specific DMA translation (on-chip IOMMU) 323961e9c84SJason Wang * and preferring incremental map. 324961e9c84SJason Wang * @vdev: vdpa device 325db9adcbfSGautam Dawar * @asid: address space identifier 326961e9c84SJason Wang * @iova: iova to be mapped 327961e9c84SJason Wang * @size: size of the area 328961e9c84SJason Wang * @pa: physical address for the map 329961e9c84SJason Wang * @perm: device access permission (VHOST_MAP_XX) 330961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 331961e9c84SJason Wang * @dma_unmap: Unmap an area of IOVA (optional but 332961e9c84SJason Wang * must be implemented with dma_map) 333961e9c84SJason Wang * Needed for device that using device 334961e9c84SJason Wang * specific DMA translation (on-chip IOMMU) 335961e9c84SJason Wang * and preferring incremental unmap. 336961e9c84SJason Wang * @vdev: vdpa device 337db9adcbfSGautam Dawar * @asid: address space identifier 338961e9c84SJason Wang * @iova: iova to be unmapped 339961e9c84SJason Wang * @size: size of the area 340961e9c84SJason Wang * Returns integer: success (0) or error (< 0) 341d2cf1b6eSSi-Wei Liu * @reset_map: Reset device memory mapping to the default 342d2cf1b6eSSi-Wei Liu * state (optional) 343d2cf1b6eSSi-Wei Liu * Needed for devices that are using device 344d2cf1b6eSSi-Wei Liu * specific DMA translation and prefer mapping 345d2cf1b6eSSi-Wei Liu * to be decoupled from the virtio life cycle, 346d2cf1b6eSSi-Wei Liu * i.e. device .reset op does not reset mapping 347d2cf1b6eSSi-Wei Liu * @vdev: vdpa device 348d2cf1b6eSSi-Wei Liu * @asid: address space identifier 349d2cf1b6eSSi-Wei Liu * Returns integer: success (0) or error (< 0) 35025da258fSJason Wang * @get_vq_dma_dev: Get the dma device for a specific 35125da258fSJason Wang * virtqueue (optional) 35225da258fSJason Wang * @vdev: vdpa device 35325da258fSJason Wang * @idx: virtqueue index 35425da258fSJason Wang * Returns pointer to structure device or error (NULL) 355c618c84dSStefano Garzarella * @bind_mm: Bind the device to a specific address space 356c618c84dSStefano Garzarella * so the vDPA framework can use VA when this 357c618c84dSStefano Garzarella * callback is implemented. (optional) 358c618c84dSStefano Garzarella * @vdev: vdpa device 359c618c84dSStefano Garzarella * @mm: address space to bind 360c618c84dSStefano Garzarella * @unbind_mm: Unbind the device from the address space 361c618c84dSStefano Garzarella * bound using the bind_mm callback. (optional) 362c618c84dSStefano Garzarella * @vdev: vdpa device 363961e9c84SJason Wang * @free: Free resources that belongs to vDPA (optional) 364961e9c84SJason Wang * @vdev: vdpa device 365961e9c84SJason Wang */ 366961e9c84SJason Wang struct vdpa_config_ops { 367961e9c84SJason Wang /* Virtqueue ops */ 368961e9c84SJason Wang int (*set_vq_address)(struct vdpa_device *vdev, 369961e9c84SJason Wang u16 idx, u64 desc_area, u64 driver_area, 370961e9c84SJason Wang u64 device_area); 371961e9c84SJason Wang void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num); 372961e9c84SJason Wang void (*kick_vq)(struct vdpa_device *vdev, u16 idx); 3732c4e4a22SAlvaro Karsz void (*kick_vq_with_data)(struct vdpa_device *vdev, u32 data); 374961e9c84SJason Wang void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx, 375961e9c84SJason Wang struct vdpa_callback *cb); 376961e9c84SJason Wang void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready); 377961e9c84SJason Wang bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx); 378aac50c0bSEli Cohen int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, 379aac50c0bSEli Cohen const struct vdpa_vq_state *state); 38023750e39SEli Cohen int (*get_vq_state)(struct vdpa_device *vdev, u16 idx, 381aac50c0bSEli Cohen struct vdpa_vq_state *state); 38213b00b13SEli Cohen int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx, 38313b00b13SEli Cohen struct sk_buff *msg, 38413b00b13SEli Cohen struct netlink_ext_ack *extack); 385c25a26e6SJason Wang struct vdpa_notification_area 386c25a26e6SJason Wang (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); 3874c05433bSZhu Lingshan /* vq irq is not expected to be changed once DRIVER_OK is set */ 388d0ae1fbfSWu Zongyong int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx); 389961e9c84SJason Wang 390961e9c84SJason Wang /* Device ops */ 391425a5070SMichael S. Tsirkin u32 (*get_vq_align)(struct vdpa_device *vdev); 392d4821902SGautam Dawar u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx); 393a72cac60SSi-Wei Liu u32 (*get_vq_desc_group)(struct vdpa_device *vdev, u16 idx); 394a64917bcSEli Cohen u64 (*get_device_features)(struct vdpa_device *vdev); 395b63e5c70SEugenio Pérez u64 (*get_backend_features)(const struct vdpa_device *vdev); 396a64917bcSEli Cohen int (*set_driver_features)(struct vdpa_device *vdev, u64 features); 397a64917bcSEli Cohen u64 (*get_driver_features)(struct vdpa_device *vdev); 398961e9c84SJason Wang void (*set_config_cb)(struct vdpa_device *vdev, 399961e9c84SJason Wang struct vdpa_callback *cb); 400961e9c84SJason Wang u16 (*get_vq_num_max)(struct vdpa_device *vdev); 4013b970a58SWu Zongyong u16 (*get_vq_num_min)(struct vdpa_device *vdev); 402961e9c84SJason Wang u32 (*get_device_id)(struct vdpa_device *vdev); 403961e9c84SJason Wang u32 (*get_vendor_id)(struct vdpa_device *vdev); 404961e9c84SJason Wang u8 (*get_status)(struct vdpa_device *vdev); 405961e9c84SJason Wang void (*set_status)(struct vdpa_device *vdev, u8 status); 4060686082dSXie Yongji int (*reset)(struct vdpa_device *vdev); 407*a26f2e4eSSi-Wei Liu int (*compat_reset)(struct vdpa_device *vdev, u32 flags); 408*a26f2e4eSSi-Wei Liu #define VDPA_RESET_F_CLEAN_MAP 1 409848ecea1SEugenio Pérez int (*suspend)(struct vdpa_device *vdev); 4101538a8a4SSebastien Boeuf int (*resume)(struct vdpa_device *vdev); 411442706f9SStefano Garzarella size_t (*get_config_size)(struct vdpa_device *vdev); 412961e9c84SJason Wang void (*get_config)(struct vdpa_device *vdev, unsigned int offset, 413961e9c84SJason Wang void *buf, unsigned int len); 414961e9c84SJason Wang void (*set_config)(struct vdpa_device *vdev, unsigned int offset, 415961e9c84SJason Wang const void *buf, unsigned int len); 416961e9c84SJason Wang u32 (*get_generation)(struct vdpa_device *vdev); 4173f1b623aSJason Wang struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); 4181d246927SXie Yongji int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx, 4191d246927SXie Yongji const struct cpumask *cpu_mask); 4201d246927SXie Yongji const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev, 4211d246927SXie Yongji u16 idx); 422961e9c84SJason Wang 423961e9c84SJason Wang /* DMA ops */ 424db9adcbfSGautam Dawar int (*set_map)(struct vdpa_device *vdev, unsigned int asid, 425db9adcbfSGautam Dawar struct vhost_iotlb *iotlb); 426db9adcbfSGautam Dawar int (*dma_map)(struct vdpa_device *vdev, unsigned int asid, 427db9adcbfSGautam Dawar u64 iova, u64 size, u64 pa, u32 perm, void *opaque); 428db9adcbfSGautam Dawar int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid, 429db9adcbfSGautam Dawar u64 iova, u64 size); 430d2cf1b6eSSi-Wei Liu int (*reset_map)(struct vdpa_device *vdev, unsigned int asid); 43146d554b1SGautam Dawar int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group, 43246d554b1SGautam Dawar unsigned int asid); 43325da258fSJason Wang struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx); 434c618c84dSStefano Garzarella int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm); 435c618c84dSStefano Garzarella void (*unbind_mm)(struct vdpa_device *vdev); 436961e9c84SJason Wang 437961e9c84SJason Wang /* Free device resources */ 438961e9c84SJason Wang void (*free)(struct vdpa_device *vdev); 439961e9c84SJason Wang }; 440961e9c84SJason Wang 441961e9c84SJason Wang struct vdpa_device *__vdpa_alloc_device(struct device *parent, 442961e9c84SJason Wang const struct vdpa_config_ops *config, 443db9adcbfSGautam Dawar unsigned int ngroups, unsigned int nas, 444d8945ec4SXie Yongji size_t size, const char *name, 445d8945ec4SXie Yongji bool use_va); 446961e9c84SJason Wang 447c8d182bdSXie Yongji /** 448c8d182bdSXie Yongji * vdpa_alloc_device - allocate and initilaize a vDPA device 449c8d182bdSXie Yongji * 450c8d182bdSXie Yongji * @dev_struct: the type of the parent structure 451c8d182bdSXie Yongji * @member: the name of struct vdpa_device within the @dev_struct 452c8d182bdSXie Yongji * @parent: the parent device 453c8d182bdSXie Yongji * @config: the bus operations that is supported by this device 454d4821902SGautam Dawar * @ngroups: the number of virtqueue groups supported by this device 455db9adcbfSGautam Dawar * @nas: the number of address spaces 456c8d182bdSXie Yongji * @name: name of the vdpa device 457d8945ec4SXie Yongji * @use_va: indicate whether virtual address must be used by this device 458c8d182bdSXie Yongji * 459c8d182bdSXie Yongji * Return allocated data structure or ERR_PTR upon error 460c8d182bdSXie Yongji */ 461db9adcbfSGautam Dawar #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \ 462db9adcbfSGautam Dawar name, use_va) \ 463d4821902SGautam Dawar container_of((__vdpa_alloc_device( \ 464db9adcbfSGautam Dawar parent, config, ngroups, nas, \ 465db9adcbfSGautam Dawar (sizeof(dev_struct) + \ 466961e9c84SJason Wang BUILD_BUG_ON_ZERO(offsetof( \ 467db9adcbfSGautam Dawar dev_struct, member))), name, use_va)), \ 468961e9c84SJason Wang dev_struct, member) 469961e9c84SJason Wang 47081d46d69SLongpeng int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); 471961e9c84SJason Wang void vdpa_unregister_device(struct vdpa_device *vdev); 472961e9c84SJason Wang 47381d46d69SLongpeng int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); 474903f7bcaSParav Pandit void _vdpa_unregister_device(struct vdpa_device *vdev); 475903f7bcaSParav Pandit 476961e9c84SJason Wang /** 477d0f9164eSParav Pandit * struct vdpa_driver - operations for a vDPA driver 478961e9c84SJason Wang * @driver: underlying device driver 479961e9c84SJason Wang * @probe: the function to call when a device is found. Returns 0 or -errno. 480961e9c84SJason Wang * @remove: the function to call when a device is removed. 481961e9c84SJason Wang */ 482961e9c84SJason Wang struct vdpa_driver { 483961e9c84SJason Wang struct device_driver driver; 484961e9c84SJason Wang int (*probe)(struct vdpa_device *vdev); 485961e9c84SJason Wang void (*remove)(struct vdpa_device *vdev); 486961e9c84SJason Wang }; 487961e9c84SJason Wang 488961e9c84SJason Wang #define vdpa_register_driver(drv) \ 489961e9c84SJason Wang __vdpa_register_driver(drv, THIS_MODULE) 490961e9c84SJason Wang int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); 491961e9c84SJason Wang void vdpa_unregister_driver(struct vdpa_driver *drv); 492961e9c84SJason Wang 493961e9c84SJason Wang #define module_vdpa_driver(__vdpa_driver) \ 494961e9c84SJason Wang module_driver(__vdpa_driver, vdpa_register_driver, \ 495961e9c84SJason Wang vdpa_unregister_driver) 496961e9c84SJason Wang 497961e9c84SJason Wang static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) 498961e9c84SJason Wang { 499961e9c84SJason Wang return container_of(driver, struct vdpa_driver, driver); 500961e9c84SJason Wang } 501961e9c84SJason Wang 502961e9c84SJason Wang static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) 503961e9c84SJason Wang { 504961e9c84SJason Wang return container_of(_dev, struct vdpa_device, dev); 505961e9c84SJason Wang } 506961e9c84SJason Wang 507961e9c84SJason Wang static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) 508961e9c84SJason Wang { 509961e9c84SJason Wang return dev_get_drvdata(&vdev->dev); 510961e9c84SJason Wang } 511961e9c84SJason Wang 512961e9c84SJason Wang static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) 513961e9c84SJason Wang { 514961e9c84SJason Wang dev_set_drvdata(&vdev->dev, data); 515961e9c84SJason Wang } 516961e9c84SJason Wang 517961e9c84SJason Wang static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) 518961e9c84SJason Wang { 519961e9c84SJason Wang return vdev->dma_dev; 520961e9c84SJason Wang } 521452639a6SMichael S. Tsirkin 5220686082dSXie Yongji static inline int vdpa_reset(struct vdpa_device *vdev) 523452639a6SMichael S. Tsirkin { 524452639a6SMichael S. Tsirkin const struct vdpa_config_ops *ops = vdev->config; 525aba21affSEli Cohen int ret; 526452639a6SMichael S. Tsirkin 527a6a51adcSEli Cohen down_write(&vdev->cf_lock); 528452639a6SMichael S. Tsirkin vdev->features_valid = false; 529aba21affSEli Cohen ret = ops->reset(vdev); 530a6a51adcSEli Cohen up_write(&vdev->cf_lock); 531aba21affSEli Cohen return ret; 532452639a6SMichael S. Tsirkin } 533452639a6SMichael S. Tsirkin 534e0077cc1SSi-Wei Liu static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features) 535452639a6SMichael S. Tsirkin { 536452639a6SMichael S. Tsirkin const struct vdpa_config_ops *ops = vdev->config; 537aba21affSEli Cohen int ret; 538aba21affSEli Cohen 539452639a6SMichael S. Tsirkin vdev->features_valid = true; 540aba21affSEli Cohen ret = ops->set_driver_features(vdev, features); 541e0077cc1SSi-Wei Liu 542e0077cc1SSi-Wei Liu return ret; 543e0077cc1SSi-Wei Liu } 544e0077cc1SSi-Wei Liu 545e0077cc1SSi-Wei Liu static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) 546e0077cc1SSi-Wei Liu { 547e0077cc1SSi-Wei Liu int ret; 548e0077cc1SSi-Wei Liu 549a6a51adcSEli Cohen down_write(&vdev->cf_lock); 550e0077cc1SSi-Wei Liu ret = vdpa_set_features_unlocked(vdev, features); 551a6a51adcSEli Cohen up_write(&vdev->cf_lock); 552aba21affSEli Cohen 553aba21affSEli Cohen return ret; 554452639a6SMichael S. Tsirkin } 555452639a6SMichael S. Tsirkin 5566dbb1f16SParav Pandit void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, 5576dbb1f16SParav Pandit void *buf, unsigned int len); 5586dbb1f16SParav Pandit void vdpa_set_config(struct vdpa_device *dev, unsigned int offset, 5596dbb1f16SParav Pandit const void *buf, unsigned int length); 56073bc0dbbSEli Cohen void vdpa_set_status(struct vdpa_device *vdev, u8 status); 56173bc0dbbSEli Cohen 56233b34750SParav Pandit /** 563d0f9164eSParav Pandit * struct vdpa_mgmtdev_ops - vdpa device ops 56433b34750SParav Pandit * @dev_add: Add a vdpa device using alloc and register 56533b34750SParav Pandit * @mdev: parent device to use for device addition 56633b34750SParav Pandit * @name: name of the new vdpa device 567d8ca2fa5SParav Pandit * @config: config attributes to apply to the device under creation 56833b34750SParav Pandit * Driver need to add a new device using _vdpa_register_device() 56933b34750SParav Pandit * after fully initializing the vdpa device. Driver must return 0 57033b34750SParav Pandit * on success or appropriate error code. 57133b34750SParav Pandit * @dev_del: Remove a vdpa device using unregister 57233b34750SParav Pandit * @mdev: parent device to use for device removal 57333b34750SParav Pandit * @dev: vdpa device to remove 57433b34750SParav Pandit * Driver need to remove the specified device by calling 57533b34750SParav Pandit * _vdpa_unregister_device(). 57633b34750SParav Pandit */ 57733b34750SParav Pandit struct vdpa_mgmtdev_ops { 578d8ca2fa5SParav Pandit int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name, 579d8ca2fa5SParav Pandit const struct vdpa_dev_set_config *config); 58033b34750SParav Pandit void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev); 58133b34750SParav Pandit }; 58233b34750SParav Pandit 583960deb33SParav Pandit /** 584960deb33SParav Pandit * struct vdpa_mgmt_dev - vdpa management device 585960deb33SParav Pandit * @device: Management parent device 586960deb33SParav Pandit * @ops: operations supported by management device 587960deb33SParav Pandit * @id_table: Pointer to device id table of supported ids 588d8ca2fa5SParav Pandit * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that 589d8ca2fa5SParav Pandit * management device support during dev_add callback 590960deb33SParav Pandit * @list: list entry 5919be5d2d4SSimon Horman * @supported_features: features supported by device 5929be5d2d4SSimon Horman * @max_supported_vqs: maximum number of virtqueues supported by device 593960deb33SParav Pandit */ 59433b34750SParav Pandit struct vdpa_mgmt_dev { 59533b34750SParav Pandit struct device *device; 59633b34750SParav Pandit const struct vdpa_mgmtdev_ops *ops; 597ffbda8e9SCindy Lu struct virtio_device_id *id_table; 598d8ca2fa5SParav Pandit u64 config_attr_mask; 59933b34750SParav Pandit struct list_head list; 600cd2629f6SEli Cohen u64 supported_features; 601cd2629f6SEli Cohen u32 max_supported_vqs; 60233b34750SParav Pandit }; 60333b34750SParav Pandit 60433b34750SParav Pandit int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev); 60533b34750SParav Pandit void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev); 60633b34750SParav Pandit 607961e9c84SJason Wang #endif /* _LINUX_VDPA_H */ 608