]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/virtio_config.h
virtio: wrap find_vqs
[karo-tx-linux.git] / include / linux / virtio_config.h
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 struct irq_affinity;
11
12 /**
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).
62  */
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[], struct irq_affinity *desc);
76         void (*del_vqs)(struct virtio_device *);
77         u64 (*get_features)(struct virtio_device *vdev);
78         int (*finalize_features)(struct virtio_device *vdev);
79         const char *(*bus_name)(struct virtio_device *vdev);
80         int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
81         const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev,
82                         int index);
83 };
84
85 /* If driver didn't advertise the feature, it will never appear. */
86 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
87                                          unsigned int fbit);
88
89 /**
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.
93  * @vdev: the device
94  * @fbit: the feature bit
95  */
96 static 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))
101                 BUILD_BUG_ON(fbit >= 64);
102         else
103                 BUG_ON(fbit >= 64);
104
105         return vdev->features & BIT_ULL(fbit);
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  */
113 static 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))
118                 BUILD_BUG_ON(fbit >= 64);
119         else
120                 BUG_ON(fbit >= 64);
121
122         vdev->features |= BIT_ULL(fbit);
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  */
130 static inline void __virtio_clear_bit(struct virtio_device *vdev,
131                                       unsigned int fbit)
132 {
133         /* Did you forget to fix assumptions on max features? */
134         if (__builtin_constant_p(fbit))
135                 BUILD_BUG_ON(fbit >= 64);
136         else
137                 BUG_ON(fbit >= 64);
138
139         vdev->features &= ~BIT_ULL(fbit);
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  */
147 static inline bool virtio_has_feature(const struct virtio_device *vdev,
148                                       unsigned int fbit)
149 {
150         if (fbit < VIRTIO_TRANSPORT_F_START)
151                 virtio_check_driver_offered_feature(vdev, fbit);
152
153         return __virtio_test_bit(vdev, fbit);
154 }
155
156 /**
157  * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
158  * @vdev: the device
159  */
160 static 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
169 static inline
170 struct 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;
176         int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL);
177         if (err < 0)
178                 return ERR_PTR(err);
179         return vq;
180 }
181
182 static inline
183 int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
184                         struct virtqueue *vqs[], vq_callback_t *callbacks[],
185                         const char * const names[],
186                         struct irq_affinity *desc)
187 {
188         return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, desc);
189 }
190
191 /**
192  * virtio_device_ready - enable vq use in probe function
193  * @vdev: the device
194  *
195  * Driver must call this to use vqs in the probe function.
196  *
197  * Note: vqs are enabled automatically after probe returns.
198  */
199 static inline
200 void virtio_device_ready(struct virtio_device *dev)
201 {
202         unsigned status = dev->config->get_status(dev);
203
204         BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
205         dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
206 }
207
208 static inline
209 const char *virtio_bus_name(struct virtio_device *vdev)
210 {
211         if (!vdev->config->bus_name)
212                 return "virtio";
213         return vdev->config->bus_name(vdev);
214 }
215
216 /**
217  * virtqueue_set_affinity - setting affinity for a virtqueue
218  * @vq: the virtqueue
219  * @cpu: the cpu no.
220  *
221  * Pay attention the function are best-effort: the affinity hint may not be set
222  * due to config support, irq type and sharing.
223  *
224  */
225 static inline
226 int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
227 {
228         struct virtio_device *vdev = vq->vdev;
229         if (vdev->config->set_vq_affinity)
230                 return vdev->config->set_vq_affinity(vq, cpu);
231         return 0;
232 }
233
234 static inline bool virtio_is_little_endian(struct virtio_device *vdev)
235 {
236         return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
237                 virtio_legacy_is_little_endian();
238 }
239
240 /* Memory accessors */
241 static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
242 {
243         return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
244 }
245
246 static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
247 {
248         return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
249 }
250
251 static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
252 {
253         return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
254 }
255
256 static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
257 {
258         return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
259 }
260
261 static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
262 {
263         return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
264 }
265
266 static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
267 {
268         return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
269 }
270
271 /* Config space accessors. */
272 #define virtio_cread(vdev, structname, member, ptr)                     \
273         do {                                                            \
274                 /* Must match the member's type, and be integer */      \
275                 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
276                         (*ptr) = 1;                                     \
277                                                                         \
278                 switch (sizeof(*ptr)) {                                 \
279                 case 1:                                                 \
280                         *(ptr) = virtio_cread8(vdev,                    \
281                                                offsetof(structname, member)); \
282                         break;                                          \
283                 case 2:                                                 \
284                         *(ptr) = virtio_cread16(vdev,                   \
285                                                 offsetof(structname, member)); \
286                         break;                                          \
287                 case 4:                                                 \
288                         *(ptr) = virtio_cread32(vdev,                   \
289                                                 offsetof(structname, member)); \
290                         break;                                          \
291                 case 8:                                                 \
292                         *(ptr) = virtio_cread64(vdev,                   \
293                                                 offsetof(structname, member)); \
294                         break;                                          \
295                 default:                                                \
296                         BUG();                                          \
297                 }                                                       \
298         } while(0)
299
300 /* Config space accessors. */
301 #define virtio_cwrite(vdev, structname, member, ptr)                    \
302         do {                                                            \
303                 /* Must match the member's type, and be integer */      \
304                 if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
305                         BUG_ON((*ptr) == 1);                            \
306                                                                         \
307                 switch (sizeof(*ptr)) {                                 \
308                 case 1:                                                 \
309                         virtio_cwrite8(vdev,                            \
310                                        offsetof(structname, member),    \
311                                        *(ptr));                         \
312                         break;                                          \
313                 case 2:                                                 \
314                         virtio_cwrite16(vdev,                           \
315                                         offsetof(structname, member),   \
316                                         *(ptr));                        \
317                         break;                                          \
318                 case 4:                                                 \
319                         virtio_cwrite32(vdev,                           \
320                                         offsetof(structname, member),   \
321                                         *(ptr));                        \
322                         break;                                          \
323                 case 8:                                                 \
324                         virtio_cwrite64(vdev,                           \
325                                         offsetof(structname, member),   \
326                                         *(ptr));                        \
327                         break;                                          \
328                 default:                                                \
329                         BUG();                                          \
330                 }                                                       \
331         } while(0)
332
333 /* Read @count fields, @bytes each. */
334 static inline void __virtio_cread_many(struct virtio_device *vdev,
335                                        unsigned int offset,
336                                        void *buf, size_t count, size_t bytes)
337 {
338         u32 old, gen = vdev->config->generation ?
339                 vdev->config->generation(vdev) : 0;
340         int i;
341
342         do {
343                 old = gen;
344
345                 for (i = 0; i < count; i++)
346                         vdev->config->get(vdev, offset + bytes * i,
347                                           buf + i * bytes, bytes);
348
349                 gen = vdev->config->generation ?
350                         vdev->config->generation(vdev) : 0;
351         } while (gen != old);
352 }
353
354 static inline void virtio_cread_bytes(struct virtio_device *vdev,
355                                       unsigned int offset,
356                                       void *buf, size_t len)
357 {
358         __virtio_cread_many(vdev, offset, buf, len, 1);
359 }
360
361 static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
362 {
363         u8 ret;
364         vdev->config->get(vdev, offset, &ret, sizeof(ret));
365         return ret;
366 }
367
368 static inline void virtio_cwrite8(struct virtio_device *vdev,
369                                   unsigned int offset, u8 val)
370 {
371         vdev->config->set(vdev, offset, &val, sizeof(val));
372 }
373
374 static inline u16 virtio_cread16(struct virtio_device *vdev,
375                                  unsigned int offset)
376 {
377         u16 ret;
378         vdev->config->get(vdev, offset, &ret, sizeof(ret));
379         return virtio16_to_cpu(vdev, (__force __virtio16)ret);
380 }
381
382 static inline void virtio_cwrite16(struct virtio_device *vdev,
383                                    unsigned int offset, u16 val)
384 {
385         val = (__force u16)cpu_to_virtio16(vdev, val);
386         vdev->config->set(vdev, offset, &val, sizeof(val));
387 }
388
389 static inline u32 virtio_cread32(struct virtio_device *vdev,
390                                  unsigned int offset)
391 {
392         u32 ret;
393         vdev->config->get(vdev, offset, &ret, sizeof(ret));
394         return virtio32_to_cpu(vdev, (__force __virtio32)ret);
395 }
396
397 static inline void virtio_cwrite32(struct virtio_device *vdev,
398                                    unsigned int offset, u32 val)
399 {
400         val = (__force u32)cpu_to_virtio32(vdev, val);
401         vdev->config->set(vdev, offset, &val, sizeof(val));
402 }
403
404 static inline u64 virtio_cread64(struct virtio_device *vdev,
405                                  unsigned int offset)
406 {
407         u64 ret;
408         __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
409         return virtio64_to_cpu(vdev, (__force __virtio64)ret);
410 }
411
412 static inline void virtio_cwrite64(struct virtio_device *vdev,
413                                    unsigned int offset, u64 val)
414 {
415         val = (__force u64)cpu_to_virtio64(vdev, val);
416         vdev->config->set(vdev, offset, &val, sizeof(val));
417 }
418
419 /* Conditional config space accessors. */
420 #define virtio_cread_feature(vdev, fbit, structname, member, ptr)       \
421         ({                                                              \
422                 int _r = 0;                                             \
423                 if (!virtio_has_feature(vdev, fbit))                    \
424                         _r = -ENOENT;                                   \
425                 else                                                    \
426                         virtio_cread((vdev), structname, member, ptr);  \
427                 _r;                                                     \
428         })
429
430 #endif /* _LINUX_VIRTIO_CONFIG_H */