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