| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_VDPA_H |
| 3 | #define _LINUX_VDPA_H |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/virtio.h> |
| 9 | #include <linux/vhost_iotlb.h> |
| 10 | #include <linux/virtio_net.h> |
| 11 | #include <linux/virtio_blk.h> |
| 12 | #include <linux/if_ether.h> |
| 13 | |
| 14 | /** |
| 15 | * struct vdpa_callback - vDPA callback definition. |
| 16 | * @callback: interrupt callback function |
| 17 | * @private: the data passed to the callback function |
| 18 | * @trigger: the eventfd for the callback (Optional). |
| 19 | * When it is set, the vDPA driver must guarantee that |
| 20 | * signaling it is functional equivalent to triggering |
| 21 | * the callback. Then vDPA parent can signal it directly |
| 22 | * instead of triggering the callback. |
| 23 | */ |
| 24 | struct vdpa_callback { |
| 25 | irqreturn_t (*callback)(void *data); |
| 26 | void *private; |
| 27 | struct eventfd_ctx *trigger; |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * struct vdpa_notification_area - vDPA notification area |
| 32 | * @addr: base address of the notification area |
| 33 | * @size: size of the notification area |
| 34 | */ |
| 35 | struct vdpa_notification_area { |
| 36 | resource_size_t addr; |
| 37 | resource_size_t size; |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * struct vdpa_vq_state_split - vDPA split virtqueue state |
| 42 | * @avail_index: available index |
| 43 | */ |
| 44 | struct vdpa_vq_state_split { |
| 45 | u16 avail_index; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * struct vdpa_vq_state_packed - vDPA packed virtqueue state |
| 50 | * @last_avail_counter: last driver ring wrap counter observed by device |
| 51 | * @last_avail_idx: device available index |
| 52 | * @last_used_counter: device ring wrap counter |
| 53 | * @last_used_idx: used index |
| 54 | */ |
| 55 | struct vdpa_vq_state_packed { |
| 56 | u16 last_avail_counter:1; |
| 57 | u16 last_avail_idx:15; |
| 58 | u16 last_used_counter:1; |
| 59 | u16 last_used_idx:15; |
| 60 | }; |
| 61 | |
| 62 | struct vdpa_vq_state { |
| 63 | union { |
| 64 | struct vdpa_vq_state_split split; |
| 65 | struct vdpa_vq_state_packed packed; |
| 66 | }; |
| 67 | }; |
| 68 | |
| 69 | struct vdpa_mgmt_dev; |
| 70 | |
| 71 | /** |
| 72 | * struct vdpa_device - representation of a vDPA device |
| 73 | * @dev: underlying device |
| 74 | * @vmap: the metadata passed to upper layer to be used for mapping |
| 75 | * @driver_override: driver name to force a match; do not set directly, |
| 76 | * because core frees it; use driver_set_override() to |
| 77 | * set or clear it. |
| 78 | * @config: the configuration ops for this device. |
| 79 | * @map: the map ops for this device |
| 80 | * @cf_lock: Protects get and set access to configuration layout. |
| 81 | * @index: device index |
| 82 | * @features_valid: were features initialized? for legacy guests |
| 83 | * @ngroups: the number of virtqueue groups |
| 84 | * @nas: the number of address spaces |
| 85 | * @use_va: indicate whether virtual address must be used by this device |
| 86 | * @nvqs: maximum number of supported virtqueues |
| 87 | * @mdev: management device pointer; caller must setup when registering device as part |
| 88 | * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device(). |
| 89 | */ |
| 90 | struct vdpa_device { |
| 91 | struct device dev; |
| 92 | union virtio_map vmap; |
| 93 | const char *driver_override; |
| 94 | const struct vdpa_config_ops *config; |
| 95 | const struct virtio_map_ops *map; |
| 96 | struct rw_semaphore cf_lock; /* Protects get/set config */ |
| 97 | unsigned int index; |
| 98 | bool features_valid; |
| 99 | bool use_va; |
| 100 | u32 nvqs; |
| 101 | struct vdpa_mgmt_dev *mdev; |
| 102 | unsigned int ngroups; |
| 103 | unsigned int nas; |
| 104 | }; |
| 105 | |
| 106 | /** |
| 107 | * struct vdpa_iova_range - the IOVA range support by the device |
| 108 | * @first: start of the IOVA range |
| 109 | * @last: end of the IOVA range |
| 110 | */ |
| 111 | struct vdpa_iova_range { |
| 112 | u64 first; |
| 113 | u64 last; |
| 114 | }; |
| 115 | |
| 116 | struct vdpa_dev_set_config { |
| 117 | u64 device_features; |
| 118 | struct { |
| 119 | u8 mac[ETH_ALEN]; |
| 120 | u16 mtu; |
| 121 | u16 max_vq_pairs; |
| 122 | } net; |
| 123 | u64 mask; |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * struct vdpa_map_file - file area for device memory mapping |
| 128 | * @file: vma->vm_file for the mapping |
| 129 | * @offset: mapping offset in the vm_file |
| 130 | */ |
| 131 | struct vdpa_map_file { |
| 132 | struct file *file; |
| 133 | u64 offset; |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * struct vdpa_config_ops - operations for configuring a vDPA device. |
| 138 | * Note: vDPA device drivers are required to implement all of the |
| 139 | * operations unless it is mentioned to be optional in the following |
| 140 | * list. |
| 141 | * |
| 142 | * @set_vq_address: Set the address of virtqueue |
| 143 | * @vdev: vdpa device |
| 144 | * @idx: virtqueue index |
| 145 | * @desc_area: address of desc area |
| 146 | * @driver_area: address of driver area |
| 147 | * @device_area: address of device area |
| 148 | * Returns integer: success (0) or error (< 0) |
| 149 | * @set_vq_num: Set the size of virtqueue |
| 150 | * @vdev: vdpa device |
| 151 | * @idx: virtqueue index |
| 152 | * @num: the size of virtqueue |
| 153 | * @kick_vq: Kick the virtqueue |
| 154 | * @vdev: vdpa device |
| 155 | * @idx: virtqueue index |
| 156 | * @kick_vq_with_data: Kick the virtqueue and supply extra data |
| 157 | * (only if VIRTIO_F_NOTIFICATION_DATA is negotiated) |
| 158 | * @vdev: vdpa device |
| 159 | * @data for split virtqueue: |
| 160 | * 16 bits vqn and 16 bits next available index. |
| 161 | * @data for packed virtqueue: |
| 162 | * 16 bits vqn, 15 least significant bits of |
| 163 | * next available index and 1 bit next_wrap. |
| 164 | * @set_vq_cb: Set the interrupt callback function for |
| 165 | * a virtqueue |
| 166 | * @vdev: vdpa device |
| 167 | * @idx: virtqueue index |
| 168 | * @cb: virtio-vdev interrupt callback structure |
| 169 | * @set_vq_ready: Set ready status for a virtqueue |
| 170 | * @vdev: vdpa device |
| 171 | * @idx: virtqueue index |
| 172 | * @ready: ready (true) not ready(false) |
| 173 | * @get_vq_ready: Get ready status for a virtqueue |
| 174 | * @vdev: vdpa device |
| 175 | * @idx: virtqueue index |
| 176 | * Returns boolean: ready (true) or not (false) |
| 177 | * @set_vq_state: Set the state for a virtqueue |
| 178 | * @vdev: vdpa device |
| 179 | * @idx: virtqueue index |
| 180 | * @state: pointer to set virtqueue state (last_avail_idx) |
| 181 | * Returns integer: success (0) or error (< 0) |
| 182 | * @get_vq_state: Get the state for a virtqueue |
| 183 | * @vdev: vdpa device |
| 184 | * @idx: virtqueue index |
| 185 | * @state: pointer to returned state (last_avail_idx) |
| 186 | * @get_vendor_vq_stats: Get the vendor statistics of a device. |
| 187 | * @vdev: vdpa device |
| 188 | * @idx: virtqueue index |
| 189 | * @msg: socket buffer holding stats message |
| 190 | * @extack: extack for reporting error messages |
| 191 | * Returns integer: success (0) or error (< 0) |
| 192 | * @get_vq_notification: Get the notification area for a virtqueue (optional) |
| 193 | * @vdev: vdpa device |
| 194 | * @idx: virtqueue index |
| 195 | * Returns the notification area |
| 196 | * @get_vq_irq: Get the irq number of a virtqueue (optional, |
| 197 | * but must implemented if require vq irq offloading) |
| 198 | * @vdev: vdpa device |
| 199 | * @idx: virtqueue index |
| 200 | * Returns int: irq number of a virtqueue, |
| 201 | * negative number if no irq assigned. |
| 202 | * @get_vq_size: Get the size of a specific virtqueue (optional) |
| 203 | * @vdev: vdpa device |
| 204 | * @idx: virtqueue index |
| 205 | * Return u16: the size of the virtqueue |
| 206 | * @get_vq_align: Get the virtqueue align requirement |
| 207 | * for the device |
| 208 | * @vdev: vdpa device |
| 209 | * Returns virtqueue algin requirement |
| 210 | * @get_vq_group: Get the group id for a specific |
| 211 | * virtqueue (optional) |
| 212 | * @vdev: vdpa device |
| 213 | * @idx: virtqueue index |
| 214 | * Returns u32: group id for this virtqueue |
| 215 | * @get_vq_desc_group: Get the group id for the descriptor table of |
| 216 | * a specific virtqueue (optional) |
| 217 | * @vdev: vdpa device |
| 218 | * @idx: virtqueue index |
| 219 | * Returns u32: group id for the descriptor table |
| 220 | * portion of this virtqueue. Could be different |
| 221 | * than the one from @get_vq_group, in which case |
| 222 | * the access to the descriptor table can be |
| 223 | * confined to a separate asid, isolating from |
| 224 | * the virtqueue's buffer address access. |
| 225 | * @get_device_features: Get virtio features supported by the device |
| 226 | * @vdev: vdpa device |
| 227 | * Returns the virtio features support by the |
| 228 | * device |
| 229 | * @get_backend_features: Get parent-specific backend features (optional) |
| 230 | * Returns the vdpa features supported by the |
| 231 | * device. |
| 232 | * @set_driver_features: Set virtio features supported by the driver |
| 233 | * @vdev: vdpa device |
| 234 | * @features: feature support by the driver |
| 235 | * Returns integer: success (0) or error (< 0) |
| 236 | * @get_driver_features: Get the virtio driver features in action |
| 237 | * @vdev: vdpa device |
| 238 | * Returns the virtio features accepted |
| 239 | * @set_config_cb: Set the config interrupt callback |
| 240 | * @vdev: vdpa device |
| 241 | * @cb: virtio-vdev interrupt callback structure |
| 242 | * @get_vq_num_max: Get the max size of virtqueue |
| 243 | * @vdev: vdpa device |
| 244 | * Returns u16: max size of virtqueue |
| 245 | * @get_vq_num_min: Get the min size of virtqueue (optional) |
| 246 | * @vdev: vdpa device |
| 247 | * Returns u16: min size of virtqueue |
| 248 | * @get_device_id: Get virtio device id |
| 249 | * @vdev: vdpa device |
| 250 | * Returns u32: virtio device id |
| 251 | * @get_vendor_id: Get id for the vendor that provides this device |
| 252 | * @vdev: vdpa device |
| 253 | * Returns u32: virtio vendor id |
| 254 | * @get_status: Get the device status |
| 255 | * @vdev: vdpa device |
| 256 | * Returns u8: virtio device status |
| 257 | * @set_status: Set the device status |
| 258 | * @vdev: vdpa device |
| 259 | * @status: virtio device status |
| 260 | * @reset: Reset device |
| 261 | * @vdev: vdpa device |
| 262 | * Returns integer: success (0) or error (< 0) |
| 263 | * @compat_reset: Reset device with compatibility quirks to |
| 264 | * accommodate older userspace. Only needed by |
| 265 | * parent driver which used to have bogus reset |
| 266 | * behaviour, and has to maintain such behaviour |
| 267 | * for compatibility with older userspace. |
| 268 | * Historically compliant driver only has to |
| 269 | * implement .reset, Historically non-compliant |
| 270 | * driver should implement both. |
| 271 | * @vdev: vdpa device |
| 272 | * @flags: compatibility quirks for reset |
| 273 | * Returns integer: success (0) or error (< 0) |
| 274 | * @suspend: Suspend the device (optional) |
| 275 | * @vdev: vdpa device |
| 276 | * Returns integer: success (0) or error (< 0) |
| 277 | * @resume: Resume the device (optional) |
| 278 | * @vdev: vdpa device |
| 279 | * Returns integer: success (0) or error (< 0) |
| 280 | * @get_config_size: Get the size of the configuration space includes |
| 281 | * fields that are conditional on feature bits. |
| 282 | * @vdev: vdpa device |
| 283 | * Returns size_t: configuration size |
| 284 | * @get_config: Read from device specific configuration space |
| 285 | * @vdev: vdpa device |
| 286 | * @offset: offset from the beginning of |
| 287 | * configuration space |
| 288 | * @buf: buffer used to read to |
| 289 | * @len: the length to read from |
| 290 | * configuration space |
| 291 | * @set_config: Write to device specific configuration space |
| 292 | * @vdev: vdpa device |
| 293 | * @offset: offset from the beginning of |
| 294 | * configuration space |
| 295 | * @buf: buffer used to write from |
| 296 | * @len: the length to write to |
| 297 | * configuration space |
| 298 | * @get_generation: Get device config generation (optional) |
| 299 | * @vdev: vdpa device |
| 300 | * Returns u32: device generation |
| 301 | * @get_iova_range: Get supported iova range (optional) |
| 302 | * @vdev: vdpa device |
| 303 | * Returns the iova range supported by |
| 304 | * the device. |
| 305 | * @set_vq_affinity: Set the affinity of virtqueue (optional) |
| 306 | * @vdev: vdpa device |
| 307 | * @idx: virtqueue index |
| 308 | * @cpu_mask: the affinity mask |
| 309 | * Returns integer: success (0) or error (< 0) |
| 310 | * @get_vq_affinity: Get the affinity of virtqueue (optional) |
| 311 | * @vdev: vdpa device |
| 312 | * @idx: virtqueue index |
| 313 | * Returns the affinity mask |
| 314 | * @set_group_asid: Set address space identifier for a |
| 315 | * virtqueue group (optional) |
| 316 | * @vdev: vdpa device |
| 317 | * @group: virtqueue group |
| 318 | * @asid: address space id for this group |
| 319 | * Returns integer: success (0) or error (< 0) |
| 320 | * @set_map: Set device memory mapping (optional) |
| 321 | * Needed for device that using device |
| 322 | * specific DMA translation (on-chip IOMMU) |
| 323 | * @vdev: vdpa device |
| 324 | * @asid: address space identifier |
| 325 | * @iotlb: vhost memory mapping to be |
| 326 | * used by the vDPA |
| 327 | * Returns integer: success (0) or error (< 0) |
| 328 | * @dma_map: Map an area of PA to IOVA (optional) |
| 329 | * Needed for device that using device |
| 330 | * specific DMA translation (on-chip IOMMU) |
| 331 | * and preferring incremental map. |
| 332 | * @vdev: vdpa device |
| 333 | * @asid: address space identifier |
| 334 | * @iova: iova to be mapped |
| 335 | * @size: size of the area |
| 336 | * @pa: physical address for the map |
| 337 | * @perm: device access permission (VHOST_MAP_XX) |
| 338 | * Returns integer: success (0) or error (< 0) |
| 339 | * @dma_unmap: Unmap an area of IOVA (optional but |
| 340 | * must be implemented with dma_map) |
| 341 | * Needed for device that using device |
| 342 | * specific DMA translation (on-chip IOMMU) |
| 343 | * and preferring incremental unmap. |
| 344 | * @vdev: vdpa device |
| 345 | * @asid: address space identifier |
| 346 | * @iova: iova to be unmapped |
| 347 | * @size: size of the area |
| 348 | * Returns integer: success (0) or error (< 0) |
| 349 | * @reset_map: Reset device memory mapping to the default |
| 350 | * state (optional) |
| 351 | * Needed for devices that are using device |
| 352 | * specific DMA translation and prefer mapping |
| 353 | * to be decoupled from the virtio life cycle, |
| 354 | * i.e. device .reset op does not reset mapping |
| 355 | * @vdev: vdpa device |
| 356 | * @asid: address space identifier |
| 357 | * Returns integer: success (0) or error (< 0) |
| 358 | * @get_vq_map: Get the map metadata for a specific |
| 359 | * virtqueue (optional) |
| 360 | * @vdev: vdpa device |
| 361 | * @idx: virtqueue index |
| 362 | * Returns map token union error (NULL) |
| 363 | * @bind_mm: Bind the device to a specific address space |
| 364 | * so the vDPA framework can use VA when this |
| 365 | * callback is implemented. (optional) |
| 366 | * @vdev: vdpa device |
| 367 | * @mm: address space to bind |
| 368 | * @unbind_mm: Unbind the device from the address space |
| 369 | * bound using the bind_mm callback. (optional) |
| 370 | * @vdev: vdpa device |
| 371 | * @free: Free resources that belongs to vDPA (optional) |
| 372 | * @vdev: vdpa device |
| 373 | */ |
| 374 | struct vdpa_config_ops { |
| 375 | /* Virtqueue ops */ |
| 376 | int (*set_vq_address)(struct vdpa_device *vdev, |
| 377 | u16 idx, u64 desc_area, u64 driver_area, |
| 378 | u64 device_area); |
| 379 | void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num); |
| 380 | void (*kick_vq)(struct vdpa_device *vdev, u16 idx); |
| 381 | void (*kick_vq_with_data)(struct vdpa_device *vdev, u32 data); |
| 382 | void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx, |
| 383 | struct vdpa_callback *cb); |
| 384 | void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready); |
| 385 | bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx); |
| 386 | int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, |
| 387 | const struct vdpa_vq_state *state); |
| 388 | int (*get_vq_state)(struct vdpa_device *vdev, u16 idx, |
| 389 | struct vdpa_vq_state *state); |
| 390 | int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx, |
| 391 | struct sk_buff *msg, |
| 392 | struct netlink_ext_ack *extack); |
| 393 | struct vdpa_notification_area |
| 394 | (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); |
| 395 | /* vq irq is not expected to be changed once DRIVER_OK is set */ |
| 396 | int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx); |
| 397 | u16 (*get_vq_size)(struct vdpa_device *vdev, u16 idx); |
| 398 | |
| 399 | /* Device ops */ |
| 400 | u32 (*get_vq_align)(struct vdpa_device *vdev); |
| 401 | u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx); |
| 402 | u32 (*get_vq_desc_group)(struct vdpa_device *vdev, u16 idx); |
| 403 | u64 (*get_device_features)(struct vdpa_device *vdev); |
| 404 | u64 (*get_backend_features)(const struct vdpa_device *vdev); |
| 405 | int (*set_driver_features)(struct vdpa_device *vdev, u64 features); |
| 406 | u64 (*get_driver_features)(struct vdpa_device *vdev); |
| 407 | void (*set_config_cb)(struct vdpa_device *vdev, |
| 408 | struct vdpa_callback *cb); |
| 409 | u16 (*get_vq_num_max)(struct vdpa_device *vdev); |
| 410 | u16 (*get_vq_num_min)(struct vdpa_device *vdev); |
| 411 | u32 (*get_device_id)(struct vdpa_device *vdev); |
| 412 | u32 (*get_vendor_id)(struct vdpa_device *vdev); |
| 413 | u8 (*get_status)(struct vdpa_device *vdev); |
| 414 | void (*set_status)(struct vdpa_device *vdev, u8 status); |
| 415 | int (*reset)(struct vdpa_device *vdev); |
| 416 | int (*compat_reset)(struct vdpa_device *vdev, u32 flags); |
| 417 | #define VDPA_RESET_F_CLEAN_MAP 1 |
| 418 | int (*suspend)(struct vdpa_device *vdev); |
| 419 | int (*resume)(struct vdpa_device *vdev); |
| 420 | size_t (*get_config_size)(struct vdpa_device *vdev); |
| 421 | void (*get_config)(struct vdpa_device *vdev, unsigned int offset, |
| 422 | void *buf, unsigned int len); |
| 423 | void (*set_config)(struct vdpa_device *vdev, unsigned int offset, |
| 424 | const void *buf, unsigned int len); |
| 425 | u32 (*get_generation)(struct vdpa_device *vdev); |
| 426 | struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); |
| 427 | int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx, |
| 428 | const struct cpumask *cpu_mask); |
| 429 | const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev, |
| 430 | u16 idx); |
| 431 | |
| 432 | /* DMA ops */ |
| 433 | int (*set_map)(struct vdpa_device *vdev, unsigned int asid, |
| 434 | struct vhost_iotlb *iotlb); |
| 435 | int (*dma_map)(struct vdpa_device *vdev, unsigned int asid, |
| 436 | u64 iova, u64 size, u64 pa, u32 perm, void *opaque); |
| 437 | int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid, |
| 438 | u64 iova, u64 size); |
| 439 | int (*reset_map)(struct vdpa_device *vdev, unsigned int asid); |
| 440 | int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group, |
| 441 | unsigned int asid); |
| 442 | union virtio_map (*get_vq_map)(struct vdpa_device *vdev, u16 idx); |
| 443 | int (*bind_mm)(struct vdpa_device *vdev, struct mm_struct *mm); |
| 444 | void (*unbind_mm)(struct vdpa_device *vdev); |
| 445 | |
| 446 | /* Free device resources */ |
| 447 | void (*free)(struct vdpa_device *vdev); |
| 448 | }; |
| 449 | |
| 450 | struct vdpa_device *__vdpa_alloc_device(struct device *parent, |
| 451 | const struct vdpa_config_ops *config, |
| 452 | const struct virtio_map_ops *map, |
| 453 | unsigned int ngroups, unsigned int nas, |
| 454 | size_t size, const char *name, |
| 455 | bool use_va); |
| 456 | |
| 457 | /** |
| 458 | * vdpa_alloc_device - allocate and initilaize a vDPA device |
| 459 | * |
| 460 | * @dev_struct: the type of the parent structure |
| 461 | * @member: the name of struct vdpa_device within the @dev_struct |
| 462 | * @parent: the parent device |
| 463 | * @config: the bus operations that is supported by this device |
| 464 | * @map: the map operations that is supported by this device |
| 465 | * @ngroups: the number of virtqueue groups supported by this device |
| 466 | * @nas: the number of address spaces |
| 467 | * @name: name of the vdpa device |
| 468 | * @use_va: indicate whether virtual address must be used by this device |
| 469 | * |
| 470 | * Return allocated data structure or ERR_PTR upon error |
| 471 | */ |
| 472 | #define vdpa_alloc_device(dev_struct, member, parent, config, map, \ |
| 473 | ngroups, nas, name, use_va) \ |
| 474 | container_of((__vdpa_alloc_device( \ |
| 475 | parent, config, map, ngroups, nas, \ |
| 476 | (sizeof(dev_struct) + \ |
| 477 | BUILD_BUG_ON_ZERO(offsetof( \ |
| 478 | dev_struct, member))), name, use_va)), \ |
| 479 | dev_struct, member) |
| 480 | |
| 481 | int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); |
| 482 | void vdpa_unregister_device(struct vdpa_device *vdev); |
| 483 | |
| 484 | int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs); |
| 485 | void _vdpa_unregister_device(struct vdpa_device *vdev); |
| 486 | |
| 487 | /** |
| 488 | * struct vdpa_driver - operations for a vDPA driver |
| 489 | * @driver: underlying device driver |
| 490 | * @probe: the function to call when a device is found. Returns 0 or -errno. |
| 491 | * @remove: the function to call when a device is removed. |
| 492 | */ |
| 493 | struct vdpa_driver { |
| 494 | struct device_driver driver; |
| 495 | int (*probe)(struct vdpa_device *vdev); |
| 496 | void (*remove)(struct vdpa_device *vdev); |
| 497 | }; |
| 498 | |
| 499 | #define vdpa_register_driver(drv) \ |
| 500 | __vdpa_register_driver(drv, THIS_MODULE) |
| 501 | int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); |
| 502 | void vdpa_unregister_driver(struct vdpa_driver *drv); |
| 503 | |
| 504 | #define module_vdpa_driver(__vdpa_driver) \ |
| 505 | module_driver(__vdpa_driver, vdpa_register_driver, \ |
| 506 | vdpa_unregister_driver) |
| 507 | |
| 508 | static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) |
| 509 | { |
| 510 | return container_of(driver, struct vdpa_driver, driver); |
| 511 | } |
| 512 | |
| 513 | static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) |
| 514 | { |
| 515 | return container_of(_dev, struct vdpa_device, dev); |
| 516 | } |
| 517 | |
| 518 | static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) |
| 519 | { |
| 520 | return dev_get_drvdata(dev: &vdev->dev); |
| 521 | } |
| 522 | |
| 523 | static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) |
| 524 | { |
| 525 | dev_set_drvdata(dev: &vdev->dev, data); |
| 526 | } |
| 527 | |
| 528 | static inline union virtio_map vdpa_get_map(struct vdpa_device *vdev) |
| 529 | { |
| 530 | return vdev->vmap; |
| 531 | } |
| 532 | |
| 533 | static inline int vdpa_reset(struct vdpa_device *vdev, u32 flags) |
| 534 | { |
| 535 | const struct vdpa_config_ops *ops = vdev->config; |
| 536 | int ret; |
| 537 | |
| 538 | down_write(sem: &vdev->cf_lock); |
| 539 | vdev->features_valid = false; |
| 540 | if (ops->compat_reset && flags) |
| 541 | ret = ops->compat_reset(vdev, flags); |
| 542 | else |
| 543 | ret = ops->reset(vdev); |
| 544 | up_write(sem: &vdev->cf_lock); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features) |
| 549 | { |
| 550 | const struct vdpa_config_ops *ops = vdev->config; |
| 551 | int ret; |
| 552 | |
| 553 | vdev->features_valid = true; |
| 554 | ret = ops->set_driver_features(vdev, features); |
| 555 | |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) |
| 560 | { |
| 561 | int ret; |
| 562 | |
| 563 | down_write(sem: &vdev->cf_lock); |
| 564 | ret = vdpa_set_features_unlocked(vdev, features); |
| 565 | up_write(sem: &vdev->cf_lock); |
| 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset, |
| 571 | void *buf, unsigned int len); |
| 572 | void vdpa_set_config(struct vdpa_device *dev, unsigned int offset, |
| 573 | const void *buf, unsigned int length); |
| 574 | void vdpa_set_status(struct vdpa_device *vdev, u8 status); |
| 575 | |
| 576 | /** |
| 577 | * struct vdpa_mgmtdev_ops - vdpa device ops |
| 578 | * @dev_add: Add a vdpa device using alloc and register |
| 579 | * @mdev: parent device to use for device addition |
| 580 | * @name: name of the new vdpa device |
| 581 | * @config: config attributes to apply to the device under creation |
| 582 | * Driver need to add a new device using _vdpa_register_device() |
| 583 | * after fully initializing the vdpa device. Driver must return 0 |
| 584 | * on success or appropriate error code. |
| 585 | * @dev_del: Remove a vdpa device using unregister |
| 586 | * @mdev: parent device to use for device removal |
| 587 | * @dev: vdpa device to remove |
| 588 | * Driver need to remove the specified device by calling |
| 589 | * _vdpa_unregister_device(). |
| 590 | * @dev_set_attr: change a vdpa device's attr after it was create |
| 591 | * @mdev: parent device to use for device |
| 592 | * @dev: vdpa device structure |
| 593 | * @config:Attributes to be set for the device. |
| 594 | * The driver needs to check the mask of the structure and then set |
| 595 | * the related information to the vdpa device. The driver must return 0 |
| 596 | * if set successfully. |
| 597 | */ |
| 598 | struct vdpa_mgmtdev_ops { |
| 599 | int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name, |
| 600 | const struct vdpa_dev_set_config *config); |
| 601 | void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev); |
| 602 | int (*dev_set_attr)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev, |
| 603 | const struct vdpa_dev_set_config *config); |
| 604 | }; |
| 605 | |
| 606 | /** |
| 607 | * struct vdpa_mgmt_dev - vdpa management device |
| 608 | * @device: Management parent device |
| 609 | * @ops: operations supported by management device |
| 610 | * @id_table: Pointer to device id table of supported ids |
| 611 | * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that |
| 612 | * management device support during dev_add callback |
| 613 | * @list: list entry |
| 614 | * @supported_features: features supported by device |
| 615 | * @max_supported_vqs: maximum number of virtqueues supported by device |
| 616 | */ |
| 617 | struct vdpa_mgmt_dev { |
| 618 | struct device *device; |
| 619 | const struct vdpa_mgmtdev_ops *ops; |
| 620 | struct virtio_device_id *id_table; |
| 621 | u64 config_attr_mask; |
| 622 | struct list_head list; |
| 623 | u64 supported_features; |
| 624 | u32 max_supported_vqs; |
| 625 | }; |
| 626 | |
| 627 | int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev); |
| 628 | void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev); |
| 629 | |
| 630 | #endif /* _LINUX_VDPA_H */ |
| 631 | |