1#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
3
4#include <linux/err.h>
5#include <linux/bug.h>
6#include <linux/virtio.h>
7#include <linux/virtio_byteorder.h>
8#include <uapi/linux/virtio_config.h>
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60typedef void vq_callback_t(struct virtqueue *);
61struct 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);
79};
80
81
82void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
83 unsigned int fbit);
84
85
86
87
88
89
90
91
92static inline bool __virtio_test_bit(const struct virtio_device *vdev,
93 unsigned int fbit)
94{
95
96 if (__builtin_constant_p(fbit))
97 BUILD_BUG_ON(fbit >= 64);
98 else
99 BUG_ON(fbit >= 64);
100
101 return vdev->features & BIT_ULL(fbit);
102}
103
104
105
106
107
108
109static inline void __virtio_set_bit(struct virtio_device *vdev,
110 unsigned int fbit)
111{
112
113 if (__builtin_constant_p(fbit))
114 BUILD_BUG_ON(fbit >= 64);
115 else
116 BUG_ON(fbit >= 64);
117
118 vdev->features |= BIT_ULL(fbit);
119}
120
121
122
123
124
125
126static inline void __virtio_clear_bit(struct virtio_device *vdev,
127 unsigned int fbit)
128{
129
130 if (__builtin_constant_p(fbit))
131 BUILD_BUG_ON(fbit >= 64);
132 else
133 BUG_ON(fbit >= 64);
134
135 vdev->features &= ~BIT_ULL(fbit);
136}
137
138
139
140
141
142
143static inline bool virtio_has_feature(const struct virtio_device *vdev,
144 unsigned int fbit)
145{
146 if (fbit < VIRTIO_TRANSPORT_F_START)
147 virtio_check_driver_offered_feature(vdev, fbit);
148
149 return __virtio_test_bit(vdev, fbit);
150}
151
152
153
154
155
156
157
158
159
160
161#define virtio_config_val(vdev, fbit, offset, v) \
162 virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
163
164#define virtio_config_val_len(vdev, fbit, offset, v, len) \
165 virtio_config_buf((vdev), (fbit), (offset), (v), (len))
166
167static inline int virtio_config_buf(struct virtio_device *vdev,
168 unsigned int fbit,
169 unsigned int offset,
170 void *buf, unsigned len)
171{
172 if (!virtio_has_feature(vdev, fbit))
173 return -ENOENT;
174
175 vdev->config->get(vdev, offset, buf, len);
176 return 0;
177}
178
179
180
181
182
183static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
184{
185
186
187
188
189 return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
190}
191
192static inline
193struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
194 vq_callback_t *c, const char *n)
195{
196 vq_callback_t *callbacks[] = { c };
197 const char *names[] = { n };
198 struct virtqueue *vq;
199 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
200 if (err < 0)
201 return ERR_PTR(err);
202 return vq;
203}
204
205
206
207
208
209
210
211
212
213static inline
214void virtio_device_ready(struct virtio_device *dev)
215{
216 unsigned status = dev->config->get_status(dev);
217
218 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
219 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
220}
221
222static inline
223const char *virtio_bus_name(struct virtio_device *vdev)
224{
225 if (!vdev->config->bus_name)
226 return "virtio";
227 return vdev->config->bus_name(vdev);
228}
229
230
231
232
233
234
235
236
237
238
239static inline
240int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
241{
242 struct virtio_device *vdev = vq->vdev;
243 if (vdev->config->set_vq_affinity)
244 return vdev->config->set_vq_affinity(vq, cpu);
245 return 0;
246}
247
248static inline bool virtio_is_little_endian(struct virtio_device *vdev)
249{
250 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
251 virtio_legacy_is_little_endian();
252}
253
254
255static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
256{
257 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
258}
259
260static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
261{
262 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
263}
264
265static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
266{
267 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
268}
269
270static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
271{
272 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
273}
274
275static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
276{
277 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
278}
279
280static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
281{
282 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
283}
284
285
286#define virtio_cread(vdev, structname, member, ptr) \
287 do { \
288 \
289 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
290 (*ptr) = 1; \
291 \
292 switch (sizeof(*ptr)) { \
293 case 1: \
294 *(ptr) = virtio_cread8(vdev, \
295 offsetof(structname, member)); \
296 break; \
297 case 2: \
298 *(ptr) = virtio_cread16(vdev, \
299 offsetof(structname, member)); \
300 break; \
301 case 4: \
302 *(ptr) = virtio_cread32(vdev, \
303 offsetof(structname, member)); \
304 break; \
305 case 8: \
306 *(ptr) = virtio_cread64(vdev, \
307 offsetof(structname, member)); \
308 break; \
309 default: \
310 BUG(); \
311 } \
312 } while(0)
313
314
315#define virtio_cwrite(vdev, structname, member, ptr) \
316 do { \
317 \
318 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
319 BUG_ON((*ptr) == 1); \
320 \
321 switch (sizeof(*ptr)) { \
322 case 1: \
323 virtio_cwrite8(vdev, \
324 offsetof(structname, member), \
325 *(ptr)); \
326 break; \
327 case 2: \
328 virtio_cwrite16(vdev, \
329 offsetof(structname, member), \
330 *(ptr)); \
331 break; \
332 case 4: \
333 virtio_cwrite32(vdev, \
334 offsetof(structname, member), \
335 *(ptr)); \
336 break; \
337 case 8: \
338 virtio_cwrite64(vdev, \
339 offsetof(structname, member), \
340 *(ptr)); \
341 break; \
342 default: \
343 BUG(); \
344 } \
345 } while(0)
346
347
348static inline void __virtio_cread_many(struct virtio_device *vdev,
349 unsigned int offset,
350 void *buf, size_t count, size_t bytes)
351{
352 u32 old, gen = vdev->config->generation ?
353 vdev->config->generation(vdev) : 0;
354 int i;
355
356 do {
357 old = gen;
358
359 for (i = 0; i < count; i++)
360 vdev->config->get(vdev, offset + bytes * i,
361 buf + i * bytes, bytes);
362
363 gen = vdev->config->generation ?
364 vdev->config->generation(vdev) : 0;
365 } while (gen != old);
366}
367
368static inline void virtio_cread_bytes(struct virtio_device *vdev,
369 unsigned int offset,
370 void *buf, size_t len)
371{
372 __virtio_cread_many(vdev, offset, buf, len, 1);
373}
374
375static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
376{
377 u8 ret;
378 vdev->config->get(vdev, offset, &ret, sizeof(ret));
379 return ret;
380}
381
382static inline void virtio_cwrite8(struct virtio_device *vdev,
383 unsigned int offset, u8 val)
384{
385 vdev->config->set(vdev, offset, &val, sizeof(val));
386}
387
388static inline u16 virtio_cread16(struct virtio_device *vdev,
389 unsigned int offset)
390{
391 u16 ret;
392 vdev->config->get(vdev, offset, &ret, sizeof(ret));
393 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
394}
395
396static inline void virtio_cwrite16(struct virtio_device *vdev,
397 unsigned int offset, u16 val)
398{
399 val = (__force u16)cpu_to_virtio16(vdev, val);
400 vdev->config->set(vdev, offset, &val, sizeof(val));
401}
402
403static inline u32 virtio_cread32(struct virtio_device *vdev,
404 unsigned int offset)
405{
406 u32 ret;
407 vdev->config->get(vdev, offset, &ret, sizeof(ret));
408 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
409}
410
411static inline void virtio_cwrite32(struct virtio_device *vdev,
412 unsigned int offset, u32 val)
413{
414 val = (__force u32)cpu_to_virtio32(vdev, val);
415 vdev->config->set(vdev, offset, &val, sizeof(val));
416}
417
418static inline u64 virtio_cread64(struct virtio_device *vdev,
419 unsigned int offset)
420{
421 u64 ret;
422 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
423 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
424}
425
426static inline void virtio_cwrite64(struct virtio_device *vdev,
427 unsigned int offset, u64 val)
428{
429 val = (__force u64)cpu_to_virtio64(vdev, val);
430 vdev->config->set(vdev, offset, &val, sizeof(val));
431}
432
433
434#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
435 ({ \
436 int _r = 0; \
437 if (!virtio_has_feature(vdev, fbit)) \
438 _r = -ENOENT; \
439 else \
440 virtio_cread((vdev), structname, member, ptr); \
441 _r; \
442 })
443
444#endif
445