1 #ifndef _LINUX_VIRTIO_CONFIG_H
2 #define _LINUX_VIRTIO_CONFIG_H
6 #include <linux/virtio.h>
7 #include <linux/virtio_byteorder.h>
8 #include <uapi/linux/virtio_config.h>
13 * virtio_config_ops - operations for configuring a virtio device
14 * @get: read the value of a configuration field
15 * vdev: the virtio_device
16 * offset: the offset of the configuration field
17 * buf: the buffer to write the field value into.
18 * len: the length of the buffer
19 * @set: write the value of a configuration field
20 * vdev: the virtio_device
21 * offset: the offset of the configuration field
22 * buf: the buffer to read the field value from.
23 * len: the length of the buffer
24 * @generation: config generation counter
25 * vdev: the virtio_device
26 * Returns the config generation counter
27 * @get_status: read the status byte
28 * vdev: the virtio_device
29 * Returns the status byte
30 * @set_status: write the status byte
31 * vdev: the virtio_device
32 * status: the new status byte
33 * @reset: reset the device
34 * vdev: the virtio device
35 * After this, status and feature negotiation must be done again
36 * Device must not be reset from its vq/config callbacks, or in
37 * parallel with being added/removed.
38 * @find_vqs: find virtqueues and instantiate them.
39 * vdev: the virtio_device
40 * nvqs: the number of virtqueues to find
41 * vqs: on success, includes new virtqueues
42 * callbacks: array of callbacks, for each virtqueue
43 * include a NULL entry for vqs that do not need a callback
44 * names: array of virtqueue names (mainly for debugging)
45 * include a NULL entry for vqs unused by driver
46 * Returns 0 on success or error status
47 * @del_vqs: free virtqueues found by find_vqs().
48 * @get_features: get the array of feature bits for this device.
49 * vdev: the virtio_device
50 * Returns the first 32 feature bits (all we currently need).
51 * @finalize_features: confirm what device features we'll be using.
52 * vdev: the virtio_device
53 * This gives the final feature bits for the device: it can change
54 * the dev->feature bits if it wants.
55 * Returns 0 on success or error status
56 * @bus_name: return the bus name associated with the device
57 * vdev: the virtio_device
58 * This returns a pointer to the bus name a la pci_name from which
59 * the caller can then copy.
60 * @set_vq_affinity: set the affinity for a virtqueue.
61 * @get_vq_affinity: get the affinity for a virtqueue (optional).
63 typedef void vq_callback_t(struct virtqueue
*);
64 struct virtio_config_ops
{
65 void (*get
)(struct virtio_device
*vdev
, unsigned offset
,
66 void *buf
, unsigned len
);
67 void (*set
)(struct virtio_device
*vdev
, unsigned offset
,
68 const void *buf
, unsigned len
);
69 u32 (*generation
)(struct virtio_device
*vdev
);
70 u8 (*get_status
)(struct virtio_device
*vdev
);
71 void (*set_status
)(struct virtio_device
*vdev
, u8 status
);
72 void (*reset
)(struct virtio_device
*vdev
);
73 int (*find_vqs
)(struct virtio_device
*, unsigned nvqs
,
74 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
75 const char * const names
[], const bool *ctx
,
76 struct irq_affinity
*desc
);
77 void (*del_vqs
)(struct virtio_device
*);
78 u64 (*get_features
)(struct virtio_device
*vdev
);
79 int (*finalize_features
)(struct virtio_device
*vdev
);
80 const char *(*bus_name
)(struct virtio_device
*vdev
);
81 int (*set_vq_affinity
)(struct virtqueue
*vq
, int cpu
);
82 const struct cpumask
*(*get_vq_affinity
)(struct virtio_device
*vdev
,
86 /* If driver didn't advertise the feature, it will never appear. */
87 void virtio_check_driver_offered_feature(const struct virtio_device
*vdev
,
91 * __virtio_test_bit - helper to test feature bits. For use by transports.
92 * Devices should normally use virtio_has_feature,
93 * which includes more checks.
95 * @fbit: the feature bit
97 static inline bool __virtio_test_bit(const struct virtio_device
*vdev
,
100 /* Did you forget to fix assumptions on max features? */
101 if (__builtin_constant_p(fbit
))
102 BUILD_BUG_ON(fbit
>= 64);
106 return vdev
->features
& BIT_ULL(fbit
);
110 * __virtio_set_bit - helper to set feature bits. For use by transports.
112 * @fbit: the feature bit
114 static inline void __virtio_set_bit(struct virtio_device
*vdev
,
117 /* Did you forget to fix assumptions on max features? */
118 if (__builtin_constant_p(fbit
))
119 BUILD_BUG_ON(fbit
>= 64);
123 vdev
->features
|= BIT_ULL(fbit
);
127 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
129 * @fbit: the feature bit
131 static inline void __virtio_clear_bit(struct virtio_device
*vdev
,
134 /* Did you forget to fix assumptions on max features? */
135 if (__builtin_constant_p(fbit
))
136 BUILD_BUG_ON(fbit
>= 64);
140 vdev
->features
&= ~BIT_ULL(fbit
);
144 * virtio_has_feature - helper to determine if this device has this feature.
146 * @fbit: the feature bit
148 static inline bool virtio_has_feature(const struct virtio_device
*vdev
,
151 if (fbit
< VIRTIO_TRANSPORT_F_START
)
152 virtio_check_driver_offered_feature(vdev
, fbit
);
154 return __virtio_test_bit(vdev
, fbit
);
158 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
161 static inline bool virtio_has_iommu_quirk(const struct virtio_device
*vdev
)
164 * Note the reverse polarity of the quirk feature (compared to most
165 * other features), this is for compatibility with legacy systems.
167 return !virtio_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
);
171 struct virtqueue
*virtio_find_single_vq(struct virtio_device
*vdev
,
172 vq_callback_t
*c
, const char *n
)
174 vq_callback_t
*callbacks
[] = { c
};
175 const char *names
[] = { n
};
176 struct virtqueue
*vq
;
177 int err
= vdev
->config
->find_vqs(vdev
, 1, &vq
, callbacks
, names
, NULL
,
185 int virtio_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
186 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
187 const char * const names
[],
188 struct irq_affinity
*desc
)
190 return vdev
->config
->find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
, NULL
, desc
);
194 int virtio_find_vqs_ctx(struct virtio_device
*vdev
, unsigned nvqs
,
195 struct virtqueue
*vqs
[], vq_callback_t
*callbacks
[],
196 const char * const names
[], const bool *ctx
,
197 struct irq_affinity
*desc
)
199 return vdev
->config
->find_vqs(vdev
, nvqs
, vqs
, callbacks
, names
, ctx
,
204 * virtio_device_ready - enable vq use in probe function
207 * Driver must call this to use vqs in the probe function.
209 * Note: vqs are enabled automatically after probe returns.
212 void virtio_device_ready(struct virtio_device
*dev
)
214 unsigned status
= dev
->config
->get_status(dev
);
216 BUG_ON(status
& VIRTIO_CONFIG_S_DRIVER_OK
);
217 dev
->config
->set_status(dev
, status
| VIRTIO_CONFIG_S_DRIVER_OK
);
221 const char *virtio_bus_name(struct virtio_device
*vdev
)
223 if (!vdev
->config
->bus_name
)
225 return vdev
->config
->bus_name(vdev
);
229 * virtqueue_set_affinity - setting affinity for a virtqueue
233 * Pay attention the function are best-effort: the affinity hint may not be set
234 * due to config support, irq type and sharing.
238 int virtqueue_set_affinity(struct virtqueue
*vq
, int cpu
)
240 struct virtio_device
*vdev
= vq
->vdev
;
241 if (vdev
->config
->set_vq_affinity
)
242 return vdev
->config
->set_vq_affinity(vq
, cpu
);
246 static inline bool virtio_is_little_endian(struct virtio_device
*vdev
)
248 return virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
) ||
249 virtio_legacy_is_little_endian();
252 /* Memory accessors */
253 static inline u16
virtio16_to_cpu(struct virtio_device
*vdev
, __virtio16 val
)
255 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
258 static inline __virtio16
cpu_to_virtio16(struct virtio_device
*vdev
, u16 val
)
260 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
263 static inline u32
virtio32_to_cpu(struct virtio_device
*vdev
, __virtio32 val
)
265 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
268 static inline __virtio32
cpu_to_virtio32(struct virtio_device
*vdev
, u32 val
)
270 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
273 static inline u64
virtio64_to_cpu(struct virtio_device
*vdev
, __virtio64 val
)
275 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
278 static inline __virtio64
cpu_to_virtio64(struct virtio_device
*vdev
, u64 val
)
280 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
283 /* Config space accessors. */
284 #define virtio_cread(vdev, structname, member, ptr) \
286 /* Must match the member's type, and be integer */ \
287 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
290 switch (sizeof(*ptr)) { \
292 *(ptr) = virtio_cread8(vdev, \
293 offsetof(structname, member)); \
296 *(ptr) = virtio_cread16(vdev, \
297 offsetof(structname, member)); \
300 *(ptr) = virtio_cread32(vdev, \
301 offsetof(structname, member)); \
304 *(ptr) = virtio_cread64(vdev, \
305 offsetof(structname, member)); \
312 /* Config space accessors. */
313 #define virtio_cwrite(vdev, structname, member, ptr) \
315 /* Must match the member's type, and be integer */ \
316 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
317 BUG_ON((*ptr) == 1); \
319 switch (sizeof(*ptr)) { \
321 virtio_cwrite8(vdev, \
322 offsetof(structname, member), \
326 virtio_cwrite16(vdev, \
327 offsetof(structname, member), \
331 virtio_cwrite32(vdev, \
332 offsetof(structname, member), \
336 virtio_cwrite64(vdev, \
337 offsetof(structname, member), \
345 /* Read @count fields, @bytes each. */
346 static inline void __virtio_cread_many(struct virtio_device
*vdev
,
348 void *buf
, size_t count
, size_t bytes
)
350 u32 old
, gen
= vdev
->config
->generation
?
351 vdev
->config
->generation(vdev
) : 0;
357 for (i
= 0; i
< count
; i
++)
358 vdev
->config
->get(vdev
, offset
+ bytes
* i
,
359 buf
+ i
* bytes
, bytes
);
361 gen
= vdev
->config
->generation
?
362 vdev
->config
->generation(vdev
) : 0;
363 } while (gen
!= old
);
366 static inline void virtio_cread_bytes(struct virtio_device
*vdev
,
368 void *buf
, size_t len
)
370 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
373 static inline u8
virtio_cread8(struct virtio_device
*vdev
, unsigned int offset
)
376 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
380 static inline void virtio_cwrite8(struct virtio_device
*vdev
,
381 unsigned int offset
, u8 val
)
383 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
386 static inline u16
virtio_cread16(struct virtio_device
*vdev
,
390 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
391 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
394 static inline void virtio_cwrite16(struct virtio_device
*vdev
,
395 unsigned int offset
, u16 val
)
397 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
398 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
401 static inline u32
virtio_cread32(struct virtio_device
*vdev
,
405 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
406 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
409 static inline void virtio_cwrite32(struct virtio_device
*vdev
,
410 unsigned int offset
, u32 val
)
412 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
413 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
416 static inline u64
virtio_cread64(struct virtio_device
*vdev
,
420 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
421 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
424 static inline void virtio_cwrite64(struct virtio_device
*vdev
,
425 unsigned int offset
, u64 val
)
427 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
428 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
431 /* Conditional config space accessors. */
432 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
435 if (!virtio_has_feature(vdev, fbit)) \
438 virtio_cread((vdev), structname, member, ptr); \
442 #endif /* _LINUX_VIRTIO_CONFIG_H */