vduse: Add sysfs interface for irq callback affinity
[linux-2.6-block.git] / include / linux / vdpa.h
CommitLineData
961e9c84
JW
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/vhost_iotlb.h>
d8ca2fa5
PP
9#include <linux/virtio_net.h>
10#include <linux/if_ether.h>
961e9c84
JW
11
12/**
d0f9164e 13 * struct vdpa_calllback - vDPA callback definition.
961e9c84
JW
14 * @callback: interrupt callback function
15 * @private: the data passed to the callback function
16 */
17struct vdpa_callback {
18 irqreturn_t (*callback)(void *data);
19 void *private;
20};
21
c25a26e6 22/**
d0f9164e 23 * struct vdpa_notification_area - vDPA notification area
c25a26e6
JW
24 * @addr: base address of the notification area
25 * @size: size of the notification area
26 */
27struct vdpa_notification_area {
28 resource_size_t addr;
29 resource_size_t size;
30};
31
aac50c0b 32/**
530a5678 33 * struct vdpa_vq_state_split - vDPA split virtqueue state
aac50c0b
EC
34 * @avail_index: available index
35 */
530a5678 36struct vdpa_vq_state_split {
aac50c0b
EC
37 u16 avail_index;
38};
39
530a5678
JW
40/**
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
46 */
47struct vdpa_vq_state_packed {
86e17a51
XY
48 u16 last_avail_counter:1;
49 u16 last_avail_idx:15;
50 u16 last_used_counter:1;
51 u16 last_used_idx:15;
530a5678
JW
52};
53
54struct vdpa_vq_state {
86e17a51
XY
55 union {
56 struct vdpa_vq_state_split split;
57 struct vdpa_vq_state_packed packed;
58 };
530a5678
JW
59};
60
33b34750
PP
61struct vdpa_mgmt_dev;
62
961e9c84 63/**
d0f9164e 64 * struct vdpa_device - representation of a vDPA device
961e9c84
JW
65 * @dev: underlying device
66 * @dma_dev: the actual device that is performing DMA
240bf4e6
KK
67 * @driver_override: driver name to force a match; do not set directly,
68 * because core frees it; use driver_set_override() to
69 * set or clear it.
961e9c84 70 * @config: the configuration ops for this device.
a6a51adc 71 * @cf_lock: Protects get and set access to configuration layout.
961e9c84 72 * @index: device index
452639a6 73 * @features_valid: were features initialized? for legacy guests
db9adcbf
GD
74 * @ngroups: the number of virtqueue groups
75 * @nas: the number of address spaces
d8945ec4 76 * @use_va: indicate whether virtual address must be used by this device
476c135e 77 * @nvqs: maximum number of supported virtqueues
903f7bca
PP
78 * @mdev: management device pointer; caller must setup when registering device as part
79 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
961e9c84
JW
80 */
81struct vdpa_device {
82 struct device dev;
83 struct device *dma_dev;
539fec78 84 const char *driver_override;
961e9c84 85 const struct vdpa_config_ops *config;
a6a51adc 86 struct rw_semaphore cf_lock; /* Protects get/set config */
961e9c84 87 unsigned int index;
452639a6 88 bool features_valid;
d8945ec4 89 bool use_va;
81d46d69 90 u32 nvqs;
903f7bca 91 struct vdpa_mgmt_dev *mdev;
d4821902 92 unsigned int ngroups;
db9adcbf 93 unsigned int nas;
961e9c84
JW
94};
95
3f1b623a 96/**
d0f9164e 97 * struct vdpa_iova_range - the IOVA range support by the device
3f1b623a
JW
98 * @first: start of the IOVA range
99 * @last: end of the IOVA range
100 */
101struct vdpa_iova_range {
102 u64 first;
103 u64 last;
104};
105
d8ca2fa5 106struct vdpa_dev_set_config {
90fea5a8 107 u64 device_features;
d8ca2fa5
PP
108 struct {
109 u8 mac[ETH_ALEN];
110 u16 mtu;
aba21aff 111 u16 max_vq_pairs;
d8ca2fa5
PP
112 } net;
113 u64 mask;
114};
115
d8945ec4
XY
116/**
117 * Corresponding file area for device memory mapping
118 * @file: vma->vm_file for the mapping
119 * @offset: mapping offset in the vm_file
120 */
121struct vdpa_map_file {
122 struct file *file;
123 u64 offset;
124};
125
961e9c84 126/**
d0f9164e 127 * struct vdpa_config_ops - operations for configuring a vDPA device.
961e9c84
JW
128 * Note: vDPA device drivers are required to implement all of the
129 * operations unless it is mentioned to be optional in the following
130 * list.
131 *
132 * @set_vq_address: Set the address of virtqueue
133 * @vdev: vdpa device
134 * @idx: virtqueue index
135 * @desc_area: address of desc area
136 * @driver_area: address of driver area
137 * @device_area: address of device area
138 * Returns integer: success (0) or error (< 0)
139 * @set_vq_num: Set the size of virtqueue
140 * @vdev: vdpa device
141 * @idx: virtqueue index
142 * @num: the size of virtqueue
143 * @kick_vq: Kick the virtqueue
144 * @vdev: vdpa device
145 * @idx: virtqueue index
146 * @set_vq_cb: Set the interrupt callback function for
147 * a virtqueue
148 * @vdev: vdpa device
149 * @idx: virtqueue index
150 * @cb: virtio-vdev interrupt callback structure
151 * @set_vq_ready: Set ready status for a virtqueue
152 * @vdev: vdpa device
153 * @idx: virtqueue index
154 * @ready: ready (true) not ready(false)
155 * @get_vq_ready: Get ready status for a virtqueue
156 * @vdev: vdpa device
157 * @idx: virtqueue index
158 * Returns boolean: ready (true) or not (false)
159 * @set_vq_state: Set the state for a virtqueue
160 * @vdev: vdpa device
161 * @idx: virtqueue index
aac50c0b 162 * @state: pointer to set virtqueue state (last_avail_idx)
961e9c84
JW
163 * Returns integer: success (0) or error (< 0)
164 * @get_vq_state: Get the state for a virtqueue
165 * @vdev: vdpa device
166 * @idx: virtqueue index
aac50c0b 167 * @state: pointer to returned state (last_avail_idx)
28cc408b 168 * @get_vq_notification: Get the notification area for a virtqueue (optional)
c25a26e6
JW
169 * @vdev: vdpa device
170 * @idx: virtqueue index
171 * Returns the notifcation area
7164675a
ZL
172 * @get_vq_irq: Get the irq number of a virtqueue (optional,
173 * but must implemented if require vq irq offloading)
174 * @vdev: vdpa device
175 * @idx: virtqueue index
176 * Returns int: irq number of a virtqueue,
177 * negative number if no irq assigned.
961e9c84
JW
178 * @get_vq_align: Get the virtqueue align requirement
179 * for the device
180 * @vdev: vdpa device
181 * Returns virtqueue algin requirement
00d1f546
JW
182 * @get_vq_group: Get the group id for a specific
183 * virtqueue (optional)
d4821902
GD
184 * @vdev: vdpa device
185 * @idx: virtqueue index
186 * Returns u32: group id for this virtqueue
a64917bc 187 * @get_device_features: Get virtio features supported by the device
961e9c84
JW
188 * @vdev: vdpa device
189 * Returns the virtio features support by the
190 * device
a64917bc 191 * @set_driver_features: Set virtio features supported by the driver
961e9c84
JW
192 * @vdev: vdpa device
193 * @features: feature support by the driver
194 * Returns integer: success (0) or error (< 0)
a64917bc
EC
195 * @get_driver_features: Get the virtio driver features in action
196 * @vdev: vdpa device
197 * Returns the virtio features accepted
961e9c84
JW
198 * @set_config_cb: Set the config interrupt callback
199 * @vdev: vdpa device
200 * @cb: virtio-vdev interrupt callback structure
201 * @get_vq_num_max: Get the max size of virtqueue
202 * @vdev: vdpa device
203 * Returns u16: max size of virtqueue
3b970a58
WZ
204 * @get_vq_num_min: Get the min size of virtqueue (optional)
205 * @vdev: vdpa device
206 * Returns u16: min size of virtqueue
961e9c84
JW
207 * @get_device_id: Get virtio device id
208 * @vdev: vdpa device
209 * Returns u32: virtio device id
210 * @get_vendor_id: Get id for the vendor that provides this device
211 * @vdev: vdpa device
212 * Returns u32: virtio vendor id
213 * @get_status: Get the device status
214 * @vdev: vdpa device
215 * Returns u8: virtio device status
216 * @set_status: Set the device status
217 * @vdev: vdpa device
218 * @status: virtio device status
0686082d
XY
219 * @reset: Reset device
220 * @vdev: vdpa device
221 * Returns integer: success (0) or error (< 0)
1538a8a4
SB
222 * @suspend: Suspend the device (optional)
223 * @vdev: vdpa device
224 * Returns integer: success (0) or error (< 0)
225 * @resume: Resume the device (optional)
848ecea1
EP
226 * @vdev: vdpa device
227 * Returns integer: success (0) or error (< 0)
a61280dd
L
228 * @get_config_size: Get the size of the configuration space includes
229 * fields that are conditional on feature bits.
442706f9
SG
230 * @vdev: vdpa device
231 * Returns size_t: configuration size
961e9c84
JW
232 * @get_config: Read from device specific configuration space
233 * @vdev: vdpa device
234 * @offset: offset from the beginning of
235 * configuration space
236 * @buf: buffer used to read to
237 * @len: the length to read from
238 * configuration space
239 * @set_config: Write to device specific configuration space
240 * @vdev: vdpa device
241 * @offset: offset from the beginning of
242 * configuration space
243 * @buf: buffer used to write from
244 * @len: the length to write to
245 * configuration space
246 * @get_generation: Get device config generation (optional)
247 * @vdev: vdpa device
248 * Returns u32: device generation
3f1b623a
JW
249 * @get_iova_range: Get supported iova range (optional)
250 * @vdev: vdpa device
251 * Returns the iova range supported by
252 * the device.
1d246927
XY
253 * @set_vq_affinity: Set the affinity of virtqueue (optional)
254 * @vdev: vdpa device
255 * @idx: virtqueue index
256 * @cpu_mask: the affinity mask
257 * Returns integer: success (0) or error (< 0)
258 * @get_vq_affinity: Get the affinity of virtqueue (optional)
259 * @vdev: vdpa device
260 * @idx: virtqueue index
261 * Returns the affinity mask
46d554b1 262 * @set_group_asid: Set address space identifier for a
00d1f546 263 * virtqueue group (optional)
46d554b1
GD
264 * @vdev: vdpa device
265 * @group: virtqueue group
266 * @asid: address space id for this group
267 * Returns integer: success (0) or error (< 0)
961e9c84
JW
268 * @set_map: Set device memory mapping (optional)
269 * Needed for device that using device
270 * specific DMA translation (on-chip IOMMU)
271 * @vdev: vdpa device
db9adcbf 272 * @asid: address space identifier
961e9c84
JW
273 * @iotlb: vhost memory mapping to be
274 * used by the vDPA
275 * Returns integer: success (0) or error (< 0)
276 * @dma_map: Map an area of PA to IOVA (optional)
277 * Needed for device that using device
278 * specific DMA translation (on-chip IOMMU)
279 * and preferring incremental map.
280 * @vdev: vdpa device
db9adcbf 281 * @asid: address space identifier
961e9c84
JW
282 * @iova: iova to be mapped
283 * @size: size of the area
284 * @pa: physical address for the map
285 * @perm: device access permission (VHOST_MAP_XX)
286 * Returns integer: success (0) or error (< 0)
287 * @dma_unmap: Unmap an area of IOVA (optional but
288 * must be implemented with dma_map)
289 * Needed for device that using device
290 * specific DMA translation (on-chip IOMMU)
291 * and preferring incremental unmap.
292 * @vdev: vdpa device
db9adcbf 293 * @asid: address space identifier
961e9c84
JW
294 * @iova: iova to be unmapped
295 * @size: size of the area
296 * Returns integer: success (0) or error (< 0)
25da258f
JW
297 * @get_vq_dma_dev: Get the dma device for a specific
298 * virtqueue (optional)
299 * @vdev: vdpa device
300 * @idx: virtqueue index
301 * Returns pointer to structure device or error (NULL)
961e9c84
JW
302 * @free: Free resources that belongs to vDPA (optional)
303 * @vdev: vdpa device
304 */
305struct vdpa_config_ops {
306 /* Virtqueue ops */
307 int (*set_vq_address)(struct vdpa_device *vdev,
308 u16 idx, u64 desc_area, u64 driver_area,
309 u64 device_area);
310 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
311 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
312 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
313 struct vdpa_callback *cb);
314 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
315 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
aac50c0b
EC
316 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
317 const struct vdpa_vq_state *state);
23750e39
EC
318 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
319 struct vdpa_vq_state *state);
13b00b13
EC
320 int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
321 struct sk_buff *msg,
322 struct netlink_ext_ack *extack);
c25a26e6
JW
323 struct vdpa_notification_area
324 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
4c05433b 325 /* vq irq is not expected to be changed once DRIVER_OK is set */
d0ae1fbf 326 int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
961e9c84
JW
327
328 /* Device ops */
425a5070 329 u32 (*get_vq_align)(struct vdpa_device *vdev);
d4821902 330 u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
a64917bc
EC
331 u64 (*get_device_features)(struct vdpa_device *vdev);
332 int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
333 u64 (*get_driver_features)(struct vdpa_device *vdev);
961e9c84
JW
334 void (*set_config_cb)(struct vdpa_device *vdev,
335 struct vdpa_callback *cb);
336 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
3b970a58 337 u16 (*get_vq_num_min)(struct vdpa_device *vdev);
961e9c84
JW
338 u32 (*get_device_id)(struct vdpa_device *vdev);
339 u32 (*get_vendor_id)(struct vdpa_device *vdev);
340 u8 (*get_status)(struct vdpa_device *vdev);
341 void (*set_status)(struct vdpa_device *vdev, u8 status);
0686082d 342 int (*reset)(struct vdpa_device *vdev);
848ecea1 343 int (*suspend)(struct vdpa_device *vdev);
1538a8a4 344 int (*resume)(struct vdpa_device *vdev);
442706f9 345 size_t (*get_config_size)(struct vdpa_device *vdev);
961e9c84
JW
346 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
347 void *buf, unsigned int len);
348 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
349 const void *buf, unsigned int len);
350 u32 (*get_generation)(struct vdpa_device *vdev);
3f1b623a 351 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
1d246927
XY
352 int (*set_vq_affinity)(struct vdpa_device *vdev, u16 idx,
353 const struct cpumask *cpu_mask);
354 const struct cpumask *(*get_vq_affinity)(struct vdpa_device *vdev,
355 u16 idx);
961e9c84
JW
356
357 /* DMA ops */
db9adcbf
GD
358 int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
359 struct vhost_iotlb *iotlb);
360 int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
361 u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
362 int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
363 u64 iova, u64 size);
46d554b1
GD
364 int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
365 unsigned int asid);
25da258f 366 struct device *(*get_vq_dma_dev)(struct vdpa_device *vdev, u16 idx);
961e9c84
JW
367
368 /* Free device resources */
369 void (*free)(struct vdpa_device *vdev);
370};
371
372struct vdpa_device *__vdpa_alloc_device(struct device *parent,
373 const struct vdpa_config_ops *config,
db9adcbf 374 unsigned int ngroups, unsigned int nas,
d8945ec4
XY
375 size_t size, const char *name,
376 bool use_va);
961e9c84 377
c8d182bd
XY
378/**
379 * vdpa_alloc_device - allocate and initilaize a vDPA device
380 *
381 * @dev_struct: the type of the parent structure
382 * @member: the name of struct vdpa_device within the @dev_struct
383 * @parent: the parent device
384 * @config: the bus operations that is supported by this device
d4821902 385 * @ngroups: the number of virtqueue groups supported by this device
db9adcbf 386 * @nas: the number of address spaces
c8d182bd 387 * @name: name of the vdpa device
d8945ec4 388 * @use_va: indicate whether virtual address must be used by this device
c8d182bd
XY
389 *
390 * Return allocated data structure or ERR_PTR upon error
391 */
db9adcbf
GD
392#define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
393 name, use_va) \
d4821902 394 container_of((__vdpa_alloc_device( \
db9adcbf
GD
395 parent, config, ngroups, nas, \
396 (sizeof(dev_struct) + \
961e9c84 397 BUILD_BUG_ON_ZERO(offsetof( \
db9adcbf 398 dev_struct, member))), name, use_va)), \
961e9c84
JW
399 dev_struct, member)
400
81d46d69 401int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
961e9c84
JW
402void vdpa_unregister_device(struct vdpa_device *vdev);
403
81d46d69 404int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
903f7bca
PP
405void _vdpa_unregister_device(struct vdpa_device *vdev);
406
961e9c84 407/**
d0f9164e 408 * struct vdpa_driver - operations for a vDPA driver
961e9c84
JW
409 * @driver: underlying device driver
410 * @probe: the function to call when a device is found. Returns 0 or -errno.
411 * @remove: the function to call when a device is removed.
412 */
413struct vdpa_driver {
414 struct device_driver driver;
415 int (*probe)(struct vdpa_device *vdev);
416 void (*remove)(struct vdpa_device *vdev);
417};
418
419#define vdpa_register_driver(drv) \
420 __vdpa_register_driver(drv, THIS_MODULE)
421int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
422void vdpa_unregister_driver(struct vdpa_driver *drv);
423
424#define module_vdpa_driver(__vdpa_driver) \
425 module_driver(__vdpa_driver, vdpa_register_driver, \
426 vdpa_unregister_driver)
427
428static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
429{
430 return container_of(driver, struct vdpa_driver, driver);
431}
432
433static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
434{
435 return container_of(_dev, struct vdpa_device, dev);
436}
437
438static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
439{
440 return dev_get_drvdata(&vdev->dev);
441}
442
443static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
444{
445 dev_set_drvdata(&vdev->dev, data);
446}
447
448static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
449{
450 return vdev->dma_dev;
451}
452639a6 452
0686082d 453static inline int vdpa_reset(struct vdpa_device *vdev)
452639a6 454{
86e17a51 455 const struct vdpa_config_ops *ops = vdev->config;
aba21aff 456 int ret;
452639a6 457
a6a51adc 458 down_write(&vdev->cf_lock);
452639a6 459 vdev->features_valid = false;
aba21aff 460 ret = ops->reset(vdev);
a6a51adc 461 up_write(&vdev->cf_lock);
aba21aff 462 return ret;
452639a6
MT
463}
464
e0077cc1 465static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
452639a6 466{
86e17a51 467 const struct vdpa_config_ops *ops = vdev->config;
aba21aff
EC
468 int ret;
469
452639a6 470 vdev->features_valid = true;
aba21aff 471 ret = ops->set_driver_features(vdev, features);
e0077cc1
SWL
472
473 return ret;
474}
475
476static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
477{
478 int ret;
479
a6a51adc 480 down_write(&vdev->cf_lock);
e0077cc1 481 ret = vdpa_set_features_unlocked(vdev, features);
a6a51adc 482 up_write(&vdev->cf_lock);
aba21aff
EC
483
484 return ret;
452639a6
MT
485}
486
6dbb1f16
PP
487void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
488 void *buf, unsigned int len);
489void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
490 const void *buf, unsigned int length);
73bc0dbb
EC
491void vdpa_set_status(struct vdpa_device *vdev, u8 status);
492
33b34750 493/**
d0f9164e
PP
494 * struct vdpa_mgmtdev_ops - vdpa device ops
495 * @dev_add: Add a vdpa device using alloc and register
496 * @mdev: parent device to use for device addition
497 * @name: name of the new vdpa device
d8ca2fa5 498 * @config: config attributes to apply to the device under creation
d0f9164e
PP
499 * Driver need to add a new device using _vdpa_register_device()
500 * after fully initializing the vdpa device. Driver must return 0
501 * on success or appropriate error code.
502 * @dev_del: Remove a vdpa device using unregister
503 * @mdev: parent device to use for device removal
504 * @dev: vdpa device to remove
505 * Driver need to remove the specified device by calling
506 * _vdpa_unregister_device().
33b34750
PP
507 */
508struct vdpa_mgmtdev_ops {
d8ca2fa5
PP
509 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
510 const struct vdpa_dev_set_config *config);
33b34750
PP
511 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
512};
513
960deb33
PP
514/**
515 * struct vdpa_mgmt_dev - vdpa management device
516 * @device: Management parent device
517 * @ops: operations supported by management device
518 * @id_table: Pointer to device id table of supported ids
d8ca2fa5
PP
519 * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
520 * management device support during dev_add callback
960deb33
PP
521 * @list: list entry
522 */
33b34750
PP
523struct vdpa_mgmt_dev {
524 struct device *device;
525 const struct vdpa_mgmtdev_ops *ops;
ffbda8e9 526 struct virtio_device_id *id_table;
d8ca2fa5 527 u64 config_attr_mask;
33b34750 528 struct list_head list;
cd2629f6
EC
529 u64 supported_features;
530 u32 max_supported_vqs;
33b34750
PP
531};
532
533int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
534void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
535
961e9c84 536#endif /* _LINUX_VDPA_H */