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 * @generation: config generation counter
23 * vdev: the virtio_device
24 * Returns the config generation counter
25 * @get_status: read the status byte
26 * vdev: the virtio_device
27 * Returns the status byte
28 * @set_status: write the status byte
29 * vdev: the virtio_device
30 * status: the new status byte
31 * @reset: reset the device
32 * vdev: the virtio device
33 * After this, status and feature negotiation must be done again
34 * Device must not be reset from its vq/config callbacks, or in
35 * parallel with being added/removed.
36 * @find_vqs: find virtqueues and instantiate them.
37 * vdev: the virtio_device
38 * nvqs: the number of virtqueues to find
39 * vqs: on success, includes new virtqueues
40 * callbacks: array of callbacks, for each virtqueue
41 * include a NULL entry for vqs that do not need a callback
42 * names: array of virtqueue names (mainly for debugging)
43 * include a NULL entry for vqs unused by driver
44 * Returns 0 on success or error status
45 * @del_vqs: free virtqueues found by find_vqs().
46 * @get_features: get the array of feature bits for this device.
47 * vdev: the virtio_device
48 * Returns the first 32 feature bits (all we currently need).
49 * @finalize_features: confirm what device features we'll be using.
50 * vdev: the virtio_device
51 * This gives the final feature bits for the device: it can change
52 * the dev->feature bits if it wants.
53 * Returns 0 on success or error status
54 * @bus_name: return the bus name associated with the device
55 * vdev: the virtio_device
56 * This returns a pointer to the bus name a la pci_name from which
57 * the caller can then copy.
58 * @set_vq_affinity: set the affinity for a virtqueue.
60 typedef void vq_callback_t(struct virtqueue
*);
61 struct virtio_config_ops
{
62 void (*get
)(struct virtio_device
*vdev
, unsigned offset
,
63 void *buf
, unsigned len
);
64 void (*set
)(struct virtio_device
*vdev
, unsigned offset
,
65 const void *buf
, unsigned len
);
66 u32 (*generation
)(struct virtio_device
*vdev
);
67 u8 (*get_status
)(struct virtio_device
*vdev
);
68 void (*set_status
)(struct virtio_device
*vdev
, u8 status
);
69 void (*reset
)(struct virtio_device
*vdev
);
70 int (*find_vqs
)(struct virtio_device
*, unsigned nvqs
,
71 struct virtqueue
*vqs
[],
72 vq_callback_t
*callbacks
[],
73 const char * const names
[]);
74 void (*del_vqs
)(struct virtio_device
*);
75 u64 (*get_features
)(struct virtio_device
*vdev
);
76 int (*finalize_features
)(struct virtio_device
*vdev
);
77 const char *(*bus_name
)(struct virtio_device
*vdev
);
78 int (*set_vq_affinity
)(struct virtqueue
*vq
, int cpu
);
81 /* If driver didn't advertise the feature, it will never appear. */
82 void virtio_check_driver_offered_feature(const struct virtio_device
*vdev
,
86 * __virtio_test_bit - helper to test feature bits. For use by transports.
87 * Devices should normally use virtio_has_feature,
88 * which includes more checks.
90 * @fbit: the feature bit
92 static inline bool __virtio_test_bit(const struct virtio_device
*vdev
,
95 /* Did you forget to fix assumptions on max features? */
96 if (__builtin_constant_p(fbit
))
97 BUILD_BUG_ON(fbit
>= 64);
101 return vdev
->features
& BIT_ULL(fbit
);
105 * __virtio_set_bit - helper to set feature bits. For use by transports.
107 * @fbit: the feature bit
109 static inline void __virtio_set_bit(struct virtio_device
*vdev
,
112 /* Did you forget to fix assumptions on max features? */
113 if (__builtin_constant_p(fbit
))
114 BUILD_BUG_ON(fbit
>= 64);
118 vdev
->features
|= BIT_ULL(fbit
);
122 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
124 * @fbit: the feature bit
126 static inline void __virtio_clear_bit(struct virtio_device
*vdev
,
129 /* Did you forget to fix assumptions on max features? */
130 if (__builtin_constant_p(fbit
))
131 BUILD_BUG_ON(fbit
>= 64);
135 vdev
->features
&= ~BIT_ULL(fbit
);
139 * virtio_has_feature - helper to determine if this device has this feature.
141 * @fbit: the feature bit
143 static inline bool virtio_has_feature(const struct virtio_device
*vdev
,
146 if (fbit
< VIRTIO_TRANSPORT_F_START
)
147 virtio_check_driver_offered_feature(vdev
, fbit
);
149 return __virtio_test_bit(vdev
, fbit
);
153 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
156 static inline bool virtio_has_iommu_quirk(const struct virtio_device
*vdev
)
159 * Note the reverse polarity of the quirk feature (compared to most
160 * other features), this is for compatibility with legacy systems.
162 return !virtio_has_feature(vdev
, VIRTIO_F_IOMMU_PLATFORM
);
166 struct virtqueue
*virtio_find_single_vq(struct virtio_device
*vdev
,
167 vq_callback_t
*c
, const char *n
)
169 vq_callback_t
*callbacks
[] = { c
};
170 const char *names
[] = { n
};
171 struct virtqueue
*vq
;
172 int err
= vdev
->config
->find_vqs(vdev
, 1, &vq
, callbacks
, names
);
179 * virtio_device_ready - enable vq use in probe function
182 * Driver must call this to use vqs in the probe function.
184 * Note: vqs are enabled automatically after probe returns.
187 void virtio_device_ready(struct virtio_device
*dev
)
189 unsigned status
= dev
->config
->get_status(dev
);
191 BUG_ON(status
& VIRTIO_CONFIG_S_DRIVER_OK
);
192 dev
->config
->set_status(dev
, status
| VIRTIO_CONFIG_S_DRIVER_OK
);
196 const char *virtio_bus_name(struct virtio_device
*vdev
)
198 if (!vdev
->config
->bus_name
)
200 return vdev
->config
->bus_name(vdev
);
204 * virtqueue_set_affinity - setting affinity for a virtqueue
208 * Pay attention the function are best-effort: the affinity hint may not be set
209 * due to config support, irq type and sharing.
213 int virtqueue_set_affinity(struct virtqueue
*vq
, int cpu
)
215 struct virtio_device
*vdev
= vq
->vdev
;
216 if (vdev
->config
->set_vq_affinity
)
217 return vdev
->config
->set_vq_affinity(vq
, cpu
);
221 static inline bool virtio_is_little_endian(struct virtio_device
*vdev
)
223 return virtio_has_feature(vdev
, VIRTIO_F_VERSION_1
) ||
224 virtio_legacy_is_little_endian();
227 /* Memory accessors */
228 static inline u16
virtio16_to_cpu(struct virtio_device
*vdev
, __virtio16 val
)
230 return __virtio16_to_cpu(virtio_is_little_endian(vdev
), val
);
233 static inline __virtio16
cpu_to_virtio16(struct virtio_device
*vdev
, u16 val
)
235 return __cpu_to_virtio16(virtio_is_little_endian(vdev
), val
);
238 static inline u32
virtio32_to_cpu(struct virtio_device
*vdev
, __virtio32 val
)
240 return __virtio32_to_cpu(virtio_is_little_endian(vdev
), val
);
243 static inline __virtio32
cpu_to_virtio32(struct virtio_device
*vdev
, u32 val
)
245 return __cpu_to_virtio32(virtio_is_little_endian(vdev
), val
);
248 static inline u64
virtio64_to_cpu(struct virtio_device
*vdev
, __virtio64 val
)
250 return __virtio64_to_cpu(virtio_is_little_endian(vdev
), val
);
253 static inline __virtio64
cpu_to_virtio64(struct virtio_device
*vdev
, u64 val
)
255 return __cpu_to_virtio64(virtio_is_little_endian(vdev
), val
);
258 /* Config space accessors. */
259 #define virtio_cread(vdev, structname, member, ptr) \
261 /* Must match the member's type, and be integer */ \
262 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
265 switch (sizeof(*ptr)) { \
267 *(ptr) = virtio_cread8(vdev, \
268 offsetof(structname, member)); \
271 *(ptr) = virtio_cread16(vdev, \
272 offsetof(structname, member)); \
275 *(ptr) = virtio_cread32(vdev, \
276 offsetof(structname, member)); \
279 *(ptr) = virtio_cread64(vdev, \
280 offsetof(structname, member)); \
287 /* Config space accessors. */
288 #define virtio_cwrite(vdev, structname, member, ptr) \
290 /* Must match the member's type, and be integer */ \
291 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
292 BUG_ON((*ptr) == 1); \
294 switch (sizeof(*ptr)) { \
296 virtio_cwrite8(vdev, \
297 offsetof(structname, member), \
301 virtio_cwrite16(vdev, \
302 offsetof(structname, member), \
306 virtio_cwrite32(vdev, \
307 offsetof(structname, member), \
311 virtio_cwrite64(vdev, \
312 offsetof(structname, member), \
320 /* Read @count fields, @bytes each. */
321 static inline void __virtio_cread_many(struct virtio_device
*vdev
,
323 void *buf
, size_t count
, size_t bytes
)
325 u32 old
, gen
= vdev
->config
->generation
?
326 vdev
->config
->generation(vdev
) : 0;
332 for (i
= 0; i
< count
; i
++)
333 vdev
->config
->get(vdev
, offset
+ bytes
* i
,
334 buf
+ i
* bytes
, bytes
);
336 gen
= vdev
->config
->generation
?
337 vdev
->config
->generation(vdev
) : 0;
338 } while (gen
!= old
);
341 static inline void virtio_cread_bytes(struct virtio_device
*vdev
,
343 void *buf
, size_t len
)
345 __virtio_cread_many(vdev
, offset
, buf
, len
, 1);
348 static inline u8
virtio_cread8(struct virtio_device
*vdev
, unsigned int offset
)
351 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
355 static inline void virtio_cwrite8(struct virtio_device
*vdev
,
356 unsigned int offset
, u8 val
)
358 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
361 static inline u16
virtio_cread16(struct virtio_device
*vdev
,
365 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
366 return virtio16_to_cpu(vdev
, (__force __virtio16
)ret
);
369 static inline void virtio_cwrite16(struct virtio_device
*vdev
,
370 unsigned int offset
, u16 val
)
372 val
= (__force u16
)cpu_to_virtio16(vdev
, val
);
373 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
376 static inline u32
virtio_cread32(struct virtio_device
*vdev
,
380 vdev
->config
->get(vdev
, offset
, &ret
, sizeof(ret
));
381 return virtio32_to_cpu(vdev
, (__force __virtio32
)ret
);
384 static inline void virtio_cwrite32(struct virtio_device
*vdev
,
385 unsigned int offset
, u32 val
)
387 val
= (__force u32
)cpu_to_virtio32(vdev
, val
);
388 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
391 static inline u64
virtio_cread64(struct virtio_device
*vdev
,
395 __virtio_cread_many(vdev
, offset
, &ret
, 1, sizeof(ret
));
396 return virtio64_to_cpu(vdev
, (__force __virtio64
)ret
);
399 static inline void virtio_cwrite64(struct virtio_device
*vdev
,
400 unsigned int offset
, u64 val
)
402 val
= (__force u64
)cpu_to_virtio64(vdev
, val
);
403 vdev
->config
->set(vdev
, offset
, &val
, sizeof(val
));
406 /* Conditional config space accessors. */
407 #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
410 if (!virtio_has_feature(vdev, fbit)) \
413 virtio_cread((vdev), structname, member, ptr); \
417 #endif /* _LINUX_VIRTIO_CONFIG_H */