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>
11 * virtio_config_ops - operations for configuring a virtio device
12 * @get: read the value of a configuration field
13 * vdev: the virtio_device
14 * offset: the offset of the configuration field
15 * buf: the buffer to write the field value into.
16 * len: the length of the buffer
17 * @set: write the value of a configuration field
18 * vdev: the virtio_device
19 * offset: the offset of the configuration field
20 * buf: the buffer to read the field value from.
21 * len: the length of the buffer
22 * @get_status: read the status byte
23 * vdev: the virtio_device
24 * Returns the status byte
25 * @set_status: write the status byte
26 * vdev: the virtio_device
27 * status: the new status byte
28 * @reset: reset the device
29 * vdev: the virtio device
30 * After this, status and feature negotiation must be done again
31 * Device must not be reset from its vq/config callbacks, or in
32 * parallel with being added/removed.
33 * @find_vqs: find virtqueues and instantiate them.
34 * vdev: the virtio_device
35 * nvqs: the number of virtqueues to find
36 * vqs: on success, includes new virtqueues
37 * callbacks: array of callbacks, for each virtqueue
38 * include a NULL entry for vqs that do not need a callback
39 * names: array of virtqueue names (mainly for debugging)
40 * include a NULL entry for vqs unused by driver
41 * Returns 0 on success or error status
42 * @del_vqs: free virtqueues found by find_vqs().
43 * @get_features: get the array of feature bits for this device.
44 * vdev: the virtio_device
45 * Returns the first 32 feature bits (all we currently need).
46 * @finalize_features: confirm what device features we'll be using.
47 * vdev: the virtio_device
48 * This gives the final feature bits for the device: it can change
49 * the dev->feature bits if it wants.
50 * Returns 0 on success or error status
51 * @bus_name: return the bus name associated with the device
52 * vdev: the virtio_device
53 * This returns a pointer to the bus name a la pci_name from which
54 * the caller can then copy.
55 * @set_vq_affinity: set the affinity for a virtqueue.
57 typedef void vq_callback_t(struct virtqueue *);
58 struct virtio_config_ops {
59 void (*get)(struct virtio_device *vdev, unsigned offset,
60 void *buf, unsigned len);
61 void (*set)(struct virtio_device *vdev, unsigned offset,
62 const void *buf, unsigned len);
63 u8 (*get_status)(struct virtio_device *vdev);
64 void (*set_status)(struct virtio_device *vdev, u8 status);
65 void (*reset)(struct virtio_device *vdev);
66 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
67 struct virtqueue *vqs[],
68 vq_callback_t *callbacks[],
70 void (*del_vqs)(struct virtio_device *);
71 u64 (*get_features)(struct virtio_device *vdev);
72 int (*finalize_features)(struct virtio_device *vdev);
73 const char *(*bus_name)(struct virtio_device *vdev);
74 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
77 /* If driver didn't advertise the feature, it will never appear. */
78 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
82 * __virtio_test_bit - helper to test feature bits. For use by transports.
83 * Devices should normally use virtio_has_feature,
84 * which includes more checks.
86 * @fbit: the feature bit
88 static inline bool __virtio_test_bit(const struct virtio_device *vdev,
91 /* Did you forget to fix assumptions on max features? */
92 if (__builtin_constant_p(fbit))
93 BUILD_BUG_ON(fbit >= 64);
97 return vdev->features & BIT_ULL(fbit);
101 * __virtio_set_bit - helper to set feature bits. For use by transports.
103 * @fbit: the feature bit
105 static inline void __virtio_set_bit(struct virtio_device *vdev,
108 /* Did you forget to fix assumptions on max features? */
109 if (__builtin_constant_p(fbit))
110 BUILD_BUG_ON(fbit >= 64);
114 vdev->features |= BIT_ULL(fbit);
118 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
120 * @fbit: the feature bit
122 static inline void __virtio_clear_bit(struct virtio_device *vdev,
125 /* Did you forget to fix assumptions on max features? */
126 if (__builtin_constant_p(fbit))
127 BUILD_BUG_ON(fbit >= 64);
131 vdev->features &= ~BIT_ULL(fbit);
135 * virtio_has_feature - helper to determine if this device has this feature.
137 * @fbit: the feature bit
139 static inline bool virtio_has_feature(const struct virtio_device *vdev,
142 if (fbit < VIRTIO_TRANSPORT_F_START)
143 virtio_check_driver_offered_feature(vdev, fbit);
145 return __virtio_test_bit(vdev, fbit);
149 struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
150 vq_callback_t *c, const char *n)
152 vq_callback_t *callbacks[] = { c };
153 const char *names[] = { n };
154 struct virtqueue *vq;
155 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
162 * virtio_device_ready - enable vq use in probe function
165 * Driver must call this to use vqs in the probe function.
167 * Note: vqs are enabled automatically after probe returns.
170 void virtio_device_ready(struct virtio_device *dev)
172 unsigned status = dev->config->get_status(dev);
174 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
175 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
179 const char *virtio_bus_name(struct virtio_device *vdev)
181 if (!vdev->config->bus_name)
183 return vdev->config->bus_name(vdev);
187 * virtqueue_set_affinity - setting affinity for a virtqueue
191 * Pay attention the function are best-effort: the affinity hint may not be set
192 * due to config support, irq type and sharing.
196 int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
198 struct virtio_device *vdev = vq->vdev;
199 if (vdev->config->set_vq_affinity)
200 return vdev->config->set_vq_affinity(vq, cpu);
204 /* Memory accessors */
205 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
207 return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
210 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
212 return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
215 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
217 return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
220 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
222 return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
225 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
227 return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
230 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
232 return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
235 /* Config space accessors. */
236 #define virtio_cread(vdev, structname, member, ptr) \
238 /* Must match the member's type, and be integer */ \
239 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
242 switch (sizeof(*ptr)) { \
244 *(ptr) = virtio_cread8(vdev, \
245 offsetof(structname, member)); \
248 *(ptr) = virtio_cread16(vdev, \
249 offsetof(structname, member)); \
252 *(ptr) = virtio_cread32(vdev, \
253 offsetof(structname, member)); \
256 *(ptr) = virtio_cread64(vdev, \
257 offsetof(structname, member)); \
264 /* Config space accessors. */
265 #define virtio_cwrite(vdev, structname, member, ptr) \
267 /* Must match the member's type, and be integer */ \
268 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
269 BUG_ON((*ptr) == 1); \
271 switch (sizeof(*ptr)) { \
273 virtio_cwrite8(vdev, \
274 offsetof(structname, member), \
278 virtio_cwrite16(vdev, \
279 offsetof(structname, member), \
283 virtio_cwrite32(vdev, \
284 offsetof(structname, member), \
288 virtio_cwrite64(vdev, \
289 offsetof(structname, member), \
297 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
300 vdev->config->get(vdev, offset, &ret, sizeof(ret));
304 static inline void virtio_cread_bytes(struct virtio_device *vdev,
306 void *buf, size_t len)
308 vdev->config->get(vdev, offset, buf, len);
311 static inline void virtio_cwrite8(struct virtio_device *vdev,
312 unsigned int offset, u8 val)
314 vdev->config->set(vdev, offset, &val, sizeof(val));
317 static inline u16 virtio_cread16(struct virtio_device *vdev,
321 vdev->config->get(vdev, offset, &ret, sizeof(ret));
322 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
325 static inline void virtio_cwrite16(struct virtio_device *vdev,
326 unsigned int offset, u16 val)
328 val = (__force u16)cpu_to_virtio16(vdev, val);
329 vdev->config->set(vdev, offset, &val, sizeof(val));
332 static inline u32 virtio_cread32(struct virtio_device *vdev,
336 vdev->config->get(vdev, offset, &ret, sizeof(ret));
337 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
340 static inline void virtio_cwrite32(struct virtio_device *vdev,
341 unsigned int offset, u32 val)
343 val = (__force u32)cpu_to_virtio32(vdev, val);
344 vdev->config->set(vdev, offset, &val, sizeof(val));
347 static inline u64 virtio_cread64(struct virtio_device *vdev,
351 vdev->config->get(vdev, offset, &ret, sizeof(ret));
352 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
355 static inline void virtio_cwrite64(struct virtio_device *vdev,
356 unsigned int offset, u64 val)
358 val = (__force u64)cpu_to_virtio64(vdev, val);
359 vdev->config->set(vdev, offset, &val, sizeof(val));
362 /* Conditional config space accessors. */
363 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
366 if (!virtio_has_feature(vdev, fbit)) \
369 virtio_cread((vdev), structname, member, ptr); \
373 #endif /* _LINUX_VIRTIO_CONFIG_H */