]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/virtio_config.h
Merge tag 'efi-urgent' of git://git.kernel.org/pub/scm/linux/kernel/git/efi/efi into...
[mirror_ubuntu-artful-kernel.git] / include / linux / virtio_config.h
CommitLineData
ec3d41c4
RR
1#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
b4f68be6 3
d2a7ddda 4#include <linux/err.h>
187f1882 5#include <linux/bug.h>
72e61eb4 6#include <linux/virtio.h>
eef960a0 7#include <linux/virtio_byteorder.h>
607ca46e 8#include <uapi/linux/virtio_config.h>
ec3d41c4 9
fb5e31d9
CH
10struct irq_affinity;
11
ec3d41c4
RR
12/**
13 * virtio_config_ops - operations for configuring a virtio device
a586d4f6 14 * @get: read the value of a configuration field
ec3d41c4 15 * vdev: the virtio_device
a586d4f6 16 * offset: the offset of the configuration field
ec3d41c4 17 * buf: the buffer to write the field value into.
a586d4f6 18 * len: the length of the buffer
a586d4f6 19 * @set: write the value of a configuration field
ec3d41c4 20 * vdev: the virtio_device
a586d4f6 21 * offset: the offset of the configuration field
ec3d41c4 22 * buf: the buffer to read the field value from.
a586d4f6 23 * len: the length of the buffer
d71de9ec
MT
24 * @generation: config generation counter
25 * vdev: the virtio_device
26 * Returns the config generation counter
ec3d41c4
RR
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
6e5aa7ef
RR
33 * @reset: reset the device
34 * vdev: the virtio device
35 * After this, status and feature negotiation must be done again
e6af578c
MT
36 * Device must not be reset from its vq/config callbacks, or in
37 * parallel with being added/removed.
d2a7ddda 38 * @find_vqs: find virtqueues and instantiate them.
ec3d41c4 39 * vdev: the virtio_device
d2a7ddda
MT
40 * nvqs: the number of virtqueues to find
41 * vqs: on success, includes new virtqueues
42 * callbacks: array of callbacks, for each virtqueue
6457f126 43 * include a NULL entry for vqs that do not need a callback
d2a7ddda 44 * names: array of virtqueue names (mainly for debugging)
6457f126 45 * include a NULL entry for vqs unused by driver
d2a7ddda
MT
46 * Returns 0 on success or error status
47 * @del_vqs: free virtqueues found by find_vqs().
c45a6816
RR
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).
c624896e 51 * @finalize_features: confirm what device features we'll be using.
c45a6816 52 * vdev: the virtio_device
c624896e
RR
53 * This gives the final feature bits for the device: it can change
54 * the dev->feature bits if it wants.
5c609a5e 55 * Returns 0 on success or error status
66846048
RJ
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.
75a0a52b 60 * @set_vq_affinity: set the affinity for a virtqueue.
bbaba479 61 * @get_vq_affinity: get the affinity for a virtqueue (optional).
ec3d41c4 62 */
d2a7ddda 63typedef void vq_callback_t(struct virtqueue *);
1842f23c 64struct virtio_config_ops {
a586d4f6 65 void (*get)(struct virtio_device *vdev, unsigned offset,
ec3d41c4 66 void *buf, unsigned len);
a586d4f6 67 void (*set)(struct virtio_device *vdev, unsigned offset,
ec3d41c4 68 const void *buf, unsigned len);
d71de9ec 69 u32 (*generation)(struct virtio_device *vdev);
ec3d41c4
RR
70 u8 (*get_status)(struct virtio_device *vdev);
71 void (*set_status)(struct virtio_device *vdev, u8 status);
6e5aa7ef 72 void (*reset)(struct virtio_device *vdev);
d2a7ddda 73 int (*find_vqs)(struct virtio_device *, unsigned nvqs,
fb5e31d9
CH
74 struct virtqueue *vqs[], vq_callback_t *callbacks[],
75 const char * const names[], struct irq_affinity *desc);
d2a7ddda 76 void (*del_vqs)(struct virtio_device *);
d0254773 77 u64 (*get_features)(struct virtio_device *vdev);
5c609a5e 78 int (*finalize_features)(struct virtio_device *vdev);
66846048 79 const char *(*bus_name)(struct virtio_device *vdev);
75a0a52b 80 int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
bbaba479
CH
81 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
82 int index);
ec3d41c4
RR
83};
84
c45a6816
RR
85/* If driver didn't advertise the feature, it will never appear. */
86void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
87 unsigned int fbit);
88
89/**
d4024af5
MT
90 * __virtio_test_bit - helper to test feature bits. For use by transports.
91 * Devices should normally use virtio_has_feature,
92 * which includes more checks.
c45a6816
RR
93 * @vdev: the device
94 * @fbit: the feature bit
95 */
d4024af5
MT
96static inline bool __virtio_test_bit(const struct virtio_device *vdev,
97 unsigned int fbit)
98{
99 /* Did you forget to fix assumptions on max features? */
100 if (__builtin_constant_p(fbit))
d0254773 101 BUILD_BUG_ON(fbit >= 64);
d4024af5 102 else
d0254773 103 BUG_ON(fbit >= 64);
d4024af5 104
d0254773 105 return vdev->features & BIT_ULL(fbit);
d4024af5
MT
106}
107
108/**
109 * __virtio_set_bit - helper to set feature bits. For use by transports.
110 * @vdev: the device
111 * @fbit: the feature bit
112 */
113static inline void __virtio_set_bit(struct virtio_device *vdev,
114 unsigned int fbit)
115{
116 /* Did you forget to fix assumptions on max features? */
117 if (__builtin_constant_p(fbit))
d0254773 118 BUILD_BUG_ON(fbit >= 64);
d4024af5 119 else
d0254773 120 BUG_ON(fbit >= 64);
d4024af5 121
d0254773 122 vdev->features |= BIT_ULL(fbit);
d4024af5
MT
123}
124
125/**
126 * __virtio_clear_bit - helper to clear feature bits. For use by transports.
127 * @vdev: the device
128 * @fbit: the feature bit
129 */
130static inline void __virtio_clear_bit(struct virtio_device *vdev,
c45a6816
RR
131 unsigned int fbit)
132{
133 /* Did you forget to fix assumptions on max features? */
1765e3a4 134 if (__builtin_constant_p(fbit))
d0254773 135 BUILD_BUG_ON(fbit >= 64);
1765e3a4 136 else
d0254773 137 BUG_ON(fbit >= 64);
c45a6816 138
d0254773 139 vdev->features &= ~BIT_ULL(fbit);
d4024af5
MT
140}
141
142/**
143 * virtio_has_feature - helper to determine if this device has this feature.
144 * @vdev: the device
145 * @fbit: the feature bit
146 */
147static inline bool virtio_has_feature(const struct virtio_device *vdev,
148 unsigned int fbit)
149{
ee006b35
MM
150 if (fbit < VIRTIO_TRANSPORT_F_START)
151 virtio_check_driver_offered_feature(vdev, fbit);
152
d4024af5 153 return __virtio_test_bit(vdev, fbit);
c45a6816
RR
154}
155
1a937693
MT
156/**
157 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
158 * @vdev: the device
159 */
160static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
161{
162 /*
163 * Note the reverse polarity of the quirk feature (compared to most
164 * other features), this is for compatibility with legacy systems.
165 */
166 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
167}
168
d2a7ddda
MT
169static inline
170struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
171 vq_callback_t *c, const char *n)
172{
173 vq_callback_t *callbacks[] = { c };
174 const char *names[] = { n };
175 struct virtqueue *vq;
fb5e31d9 176 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL);
d2a7ddda
MT
177 if (err < 0)
178 return ERR_PTR(err);
179 return vq;
180}
66846048 181
3569db59
MT
182/**
183 * virtio_device_ready - enable vq use in probe function
184 * @vdev: the device
185 *
186 * Driver must call this to use vqs in the probe function.
187 *
188 * Note: vqs are enabled automatically after probe returns.
189 */
190static inline
191void virtio_device_ready(struct virtio_device *dev)
192{
193 unsigned status = dev->config->get_status(dev);
194
195 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
196 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
197}
198
66846048
RJ
199static inline
200const char *virtio_bus_name(struct virtio_device *vdev)
201{
202 if (!vdev->config->bus_name)
203 return "virtio";
204 return vdev->config->bus_name(vdev);
205}
206
75a0a52b
JW
207/**
208 * virtqueue_set_affinity - setting affinity for a virtqueue
209 * @vq: the virtqueue
210 * @cpu: the cpu no.
211 *
212 * Pay attention the function are best-effort: the affinity hint may not be set
213 * due to config support, irq type and sharing.
214 *
215 */
216static inline
217int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
218{
219 struct virtio_device *vdev = vq->vdev;
220 if (vdev->config->set_vq_affinity)
221 return vdev->config->set_vq_affinity(vq, cpu);
222 return 0;
223}
224
cf561f0d
GK
225static inline bool virtio_is_little_endian(struct virtio_device *vdev)
226{
7d824109
GK
227 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
228 virtio_legacy_is_little_endian();
cf561f0d
GK
229}
230
eef960a0
MT
231/* Memory accessors */
232static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
233{
cf561f0d 234 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
235}
236
237static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
238{
cf561f0d 239 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
eef960a0
MT
240}
241
242static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
243{
cf561f0d 244 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
245}
246
247static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
248{
cf561f0d 249 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
eef960a0
MT
250}
251
252static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
253{
cf561f0d 254 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
eef960a0
MT
255}
256
257static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
258{
cf561f0d 259 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
eef960a0
MT
260}
261
0b90d062
RR
262/* Config space accessors. */
263#define virtio_cread(vdev, structname, member, ptr) \
264 do { \
265 /* Must match the member's type, and be integer */ \
266 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
267 (*ptr) = 1; \
268 \
269 switch (sizeof(*ptr)) { \
270 case 1: \
271 *(ptr) = virtio_cread8(vdev, \
272 offsetof(structname, member)); \
273 break; \
274 case 2: \
275 *(ptr) = virtio_cread16(vdev, \
276 offsetof(structname, member)); \
277 break; \
278 case 4: \
279 *(ptr) = virtio_cread32(vdev, \
280 offsetof(structname, member)); \
281 break; \
282 case 8: \
283 *(ptr) = virtio_cread64(vdev, \
284 offsetof(structname, member)); \
285 break; \
286 default: \
287 BUG(); \
288 } \
289 } while(0)
290
291/* Config space accessors. */
292#define virtio_cwrite(vdev, structname, member, ptr) \
293 do { \
294 /* Must match the member's type, and be integer */ \
295 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
296 BUG_ON((*ptr) == 1); \
297 \
298 switch (sizeof(*ptr)) { \
299 case 1: \
300 virtio_cwrite8(vdev, \
301 offsetof(structname, member), \
302 *(ptr)); \
303 break; \
304 case 2: \
305 virtio_cwrite16(vdev, \
306 offsetof(structname, member), \
307 *(ptr)); \
308 break; \
309 case 4: \
310 virtio_cwrite32(vdev, \
311 offsetof(structname, member), \
312 *(ptr)); \
313 break; \
314 case 8: \
315 virtio_cwrite64(vdev, \
316 offsetof(structname, member), \
317 *(ptr)); \
318 break; \
319 default: \
320 BUG(); \
321 } \
322 } while(0)
323
d71de9ec
MT
324/* Read @count fields, @bytes each. */
325static inline void __virtio_cread_many(struct virtio_device *vdev,
326 unsigned int offset,
327 void *buf, size_t count, size_t bytes)
328{
329 u32 old, gen = vdev->config->generation ?
330 vdev->config->generation(vdev) : 0;
331 int i;
332
333 do {
334 old = gen;
335
336 for (i = 0; i < count; i++)
337 vdev->config->get(vdev, offset + bytes * i,
338 buf + i * bytes, bytes);
339
340 gen = vdev->config->generation ?
341 vdev->config->generation(vdev) : 0;
342 } while (gen != old);
343}
344
0b90d062
RR
345static inline void virtio_cread_bytes(struct virtio_device *vdev,
346 unsigned int offset,
347 void *buf, size_t len)
348{
d71de9ec 349 __virtio_cread_many(vdev, offset, buf, len, 1);
0b90d062
RR
350}
351
caa0e2d0
MT
352static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
353{
354 u8 ret;
355 vdev->config->get(vdev, offset, &ret, sizeof(ret));
356 return ret;
357}
358
0b90d062
RR
359static inline void virtio_cwrite8(struct virtio_device *vdev,
360 unsigned int offset, u8 val)
361{
362 vdev->config->set(vdev, offset, &val, sizeof(val));
363}
364
365static inline u16 virtio_cread16(struct virtio_device *vdev,
366 unsigned int offset)
367{
368 u16 ret;
369 vdev->config->get(vdev, offset, &ret, sizeof(ret));
92e6d743 370 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
0b90d062
RR
371}
372
373static inline void virtio_cwrite16(struct virtio_device *vdev,
374 unsigned int offset, u16 val)
375{
92e6d743 376 val = (__force u16)cpu_to_virtio16(vdev, val);
0b90d062
RR
377 vdev->config->set(vdev, offset, &val, sizeof(val));
378}
379
380static inline u32 virtio_cread32(struct virtio_device *vdev,
381 unsigned int offset)
382{
383 u32 ret;
384 vdev->config->get(vdev, offset, &ret, sizeof(ret));
92e6d743 385 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
0b90d062
RR
386}
387
388static inline void virtio_cwrite32(struct virtio_device *vdev,
389 unsigned int offset, u32 val)
390{
92e6d743 391 val = (__force u32)cpu_to_virtio32(vdev, val);
0b90d062
RR
392 vdev->config->set(vdev, offset, &val, sizeof(val));
393}
394
395static inline u64 virtio_cread64(struct virtio_device *vdev,
396 unsigned int offset)
397{
398 u64 ret;
d71de9ec 399 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
92e6d743 400 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
0b90d062
RR
401}
402
403static inline void virtio_cwrite64(struct virtio_device *vdev,
404 unsigned int offset, u64 val)
405{
92e6d743 406 val = (__force u64)cpu_to_virtio64(vdev, val);
0b90d062
RR
407 vdev->config->set(vdev, offset, &val, sizeof(val));
408}
409
410/* Conditional config space accessors. */
411#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
412 ({ \
413 int _r = 0; \
414 if (!virtio_has_feature(vdev, fbit)) \
415 _r = -ENOENT; \
416 else \
417 virtio_cread((vdev), structname, member, ptr); \
418 _r; \
419 })
75a0a52b 420
ec3d41c4 421#endif /* _LINUX_VIRTIO_CONFIG_H */