]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
ec3d41c4 RR |
2 | #ifndef _LINUX_VIRTIO_CONFIG_H |
3 | #define _LINUX_VIRTIO_CONFIG_H | |
b4f68be6 | 4 | |
d2a7ddda | 5 | #include <linux/err.h> |
187f1882 | 6 | #include <linux/bug.h> |
72e61eb4 | 7 | #include <linux/virtio.h> |
eef960a0 | 8 | #include <linux/virtio_byteorder.h> |
607ca46e | 9 | #include <uapi/linux/virtio_config.h> |
ec3d41c4 | 10 | |
fb5e31d9 CH |
11 | struct irq_affinity; |
12 | ||
ec3d41c4 RR |
13 | /** |
14 | * virtio_config_ops - operations for configuring a virtio device | |
a586d4f6 | 15 | * @get: read the value of a configuration field |
ec3d41c4 | 16 | * vdev: the virtio_device |
a586d4f6 | 17 | * offset: the offset of the configuration field |
ec3d41c4 | 18 | * buf: the buffer to write the field value into. |
a586d4f6 | 19 | * len: the length of the buffer |
a586d4f6 | 20 | * @set: write the value of a configuration field |
ec3d41c4 | 21 | * vdev: the virtio_device |
a586d4f6 | 22 | * offset: the offset of the configuration field |
ec3d41c4 | 23 | * buf: the buffer to read the field value from. |
a586d4f6 | 24 | * len: the length of the buffer |
d71de9ec MT |
25 | * @generation: config generation counter |
26 | * vdev: the virtio_device | |
27 | * Returns the config generation counter | |
ec3d41c4 RR |
28 | * @get_status: read the status byte |
29 | * vdev: the virtio_device | |
30 | * Returns the status byte | |
31 | * @set_status: write the status byte | |
32 | * vdev: the virtio_device | |
33 | * status: the new status byte | |
6e5aa7ef RR |
34 | * @reset: reset the device |
35 | * vdev: the virtio device | |
36 | * After this, status and feature negotiation must be done again | |
e6af578c MT |
37 | * Device must not be reset from its vq/config callbacks, or in |
38 | * parallel with being added/removed. | |
d2a7ddda | 39 | * @find_vqs: find virtqueues and instantiate them. |
ec3d41c4 | 40 | * vdev: the virtio_device |
d2a7ddda MT |
41 | * nvqs: the number of virtqueues to find |
42 | * vqs: on success, includes new virtqueues | |
43 | * callbacks: array of callbacks, for each virtqueue | |
6457f126 | 44 | * include a NULL entry for vqs that do not need a callback |
d2a7ddda | 45 | * names: array of virtqueue names (mainly for debugging) |
6457f126 | 46 | * include a NULL entry for vqs unused by driver |
d2a7ddda MT |
47 | * Returns 0 on success or error status |
48 | * @del_vqs: free virtqueues found by find_vqs(). | |
c45a6816 RR |
49 | * @get_features: get the array of feature bits for this device. |
50 | * vdev: the virtio_device | |
51 | * Returns the first 32 feature bits (all we currently need). | |
c624896e | 52 | * @finalize_features: confirm what device features we'll be using. |
c45a6816 | 53 | * vdev: the virtio_device |
c624896e RR |
54 | * This gives the final feature bits for the device: it can change |
55 | * the dev->feature bits if it wants. | |
5c609a5e | 56 | * Returns 0 on success or error status |
66846048 RJ |
57 | * @bus_name: return the bus name associated with the device |
58 | * vdev: the virtio_device | |
59 | * This returns a pointer to the bus name a la pci_name from which | |
60 | * the caller can then copy. | |
75a0a52b | 61 | * @set_vq_affinity: set the affinity for a virtqueue. |
bbaba479 | 62 | * @get_vq_affinity: get the affinity for a virtqueue (optional). |
ec3d41c4 | 63 | */ |
d2a7ddda | 64 | typedef void vq_callback_t(struct virtqueue *); |
1842f23c | 65 | struct virtio_config_ops { |
a586d4f6 | 66 | void (*get)(struct virtio_device *vdev, unsigned offset, |
ec3d41c4 | 67 | void *buf, unsigned len); |
a586d4f6 | 68 | void (*set)(struct virtio_device *vdev, unsigned offset, |
ec3d41c4 | 69 | const void *buf, unsigned len); |
d71de9ec | 70 | u32 (*generation)(struct virtio_device *vdev); |
ec3d41c4 RR |
71 | u8 (*get_status)(struct virtio_device *vdev); |
72 | void (*set_status)(struct virtio_device *vdev, u8 status); | |
6e5aa7ef | 73 | void (*reset)(struct virtio_device *vdev); |
d2a7ddda | 74 | int (*find_vqs)(struct virtio_device *, unsigned nvqs, |
fb5e31d9 | 75 | struct virtqueue *vqs[], vq_callback_t *callbacks[], |
f94682dd MT |
76 | const char * const names[], const bool *ctx, |
77 | struct irq_affinity *desc); | |
d2a7ddda | 78 | void (*del_vqs)(struct virtio_device *); |
d0254773 | 79 | u64 (*get_features)(struct virtio_device *vdev); |
5c609a5e | 80 | int (*finalize_features)(struct virtio_device *vdev); |
66846048 | 81 | const char *(*bus_name)(struct virtio_device *vdev); |
19e226e8 CR |
82 | int (*set_vq_affinity)(struct virtqueue *vq, |
83 | const struct cpumask *cpu_mask); | |
bbaba479 CH |
84 | const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev, |
85 | int index); | |
ec3d41c4 RR |
86 | }; |
87 | ||
c45a6816 RR |
88 | /* If driver didn't advertise the feature, it will never appear. */ |
89 | void virtio_check_driver_offered_feature(const struct virtio_device *vdev, | |
90 | unsigned int fbit); | |
91 | ||
92 | /** | |
d4024af5 MT |
93 | * __virtio_test_bit - helper to test feature bits. For use by transports. |
94 | * Devices should normally use virtio_has_feature, | |
95 | * which includes more checks. | |
c45a6816 RR |
96 | * @vdev: the device |
97 | * @fbit: the feature bit | |
98 | */ | |
d4024af5 MT |
99 | static inline bool __virtio_test_bit(const struct virtio_device *vdev, |
100 | unsigned int fbit) | |
101 | { | |
102 | /* Did you forget to fix assumptions on max features? */ | |
103 | if (__builtin_constant_p(fbit)) | |
d0254773 | 104 | BUILD_BUG_ON(fbit >= 64); |
d4024af5 | 105 | else |
d0254773 | 106 | BUG_ON(fbit >= 64); |
d4024af5 | 107 | |
d0254773 | 108 | return vdev->features & BIT_ULL(fbit); |
d4024af5 MT |
109 | } |
110 | ||
111 | /** | |
112 | * __virtio_set_bit - helper to set feature bits. For use by transports. | |
113 | * @vdev: the device | |
114 | * @fbit: the feature bit | |
115 | */ | |
116 | static inline void __virtio_set_bit(struct virtio_device *vdev, | |
117 | unsigned int fbit) | |
118 | { | |
119 | /* Did you forget to fix assumptions on max features? */ | |
120 | if (__builtin_constant_p(fbit)) | |
d0254773 | 121 | BUILD_BUG_ON(fbit >= 64); |
d4024af5 | 122 | else |
d0254773 | 123 | BUG_ON(fbit >= 64); |
d4024af5 | 124 | |
d0254773 | 125 | vdev->features |= BIT_ULL(fbit); |
d4024af5 MT |
126 | } |
127 | ||
128 | /** | |
129 | * __virtio_clear_bit - helper to clear feature bits. For use by transports. | |
130 | * @vdev: the device | |
131 | * @fbit: the feature bit | |
132 | */ | |
133 | static inline void __virtio_clear_bit(struct virtio_device *vdev, | |
c45a6816 RR |
134 | unsigned int fbit) |
135 | { | |
136 | /* Did you forget to fix assumptions on max features? */ | |
1765e3a4 | 137 | if (__builtin_constant_p(fbit)) |
d0254773 | 138 | BUILD_BUG_ON(fbit >= 64); |
1765e3a4 | 139 | else |
d0254773 | 140 | BUG_ON(fbit >= 64); |
c45a6816 | 141 | |
d0254773 | 142 | vdev->features &= ~BIT_ULL(fbit); |
d4024af5 MT |
143 | } |
144 | ||
145 | /** | |
146 | * virtio_has_feature - helper to determine if this device has this feature. | |
147 | * @vdev: the device | |
148 | * @fbit: the feature bit | |
149 | */ | |
150 | static inline bool virtio_has_feature(const struct virtio_device *vdev, | |
151 | unsigned int fbit) | |
152 | { | |
ee006b35 MM |
153 | if (fbit < VIRTIO_TRANSPORT_F_START) |
154 | virtio_check_driver_offered_feature(vdev, fbit); | |
155 | ||
d4024af5 | 156 | return __virtio_test_bit(vdev, fbit); |
c45a6816 RR |
157 | } |
158 | ||
1a937693 MT |
159 | /** |
160 | * virtio_has_iommu_quirk - determine whether this device has the iommu quirk | |
161 | * @vdev: the device | |
162 | */ | |
163 | static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev) | |
164 | { | |
165 | /* | |
166 | * Note the reverse polarity of the quirk feature (compared to most | |
167 | * other features), this is for compatibility with legacy systems. | |
168 | */ | |
169 | return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); | |
170 | } | |
171 | ||
d2a7ddda MT |
172 | static inline |
173 | struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev, | |
174 | vq_callback_t *c, const char *n) | |
175 | { | |
176 | vq_callback_t *callbacks[] = { c }; | |
177 | const char *names[] = { n }; | |
178 | struct virtqueue *vq; | |
f94682dd MT |
179 | int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL, |
180 | NULL); | |
d2a7ddda MT |
181 | if (err < 0) |
182 | return ERR_PTR(err); | |
183 | return vq; | |
184 | } | |
66846048 | 185 | |
9b2bbdb2 MT |
186 | static inline |
187 | int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs, | |
188 | struct virtqueue *vqs[], vq_callback_t *callbacks[], | |
189 | const char * const names[], | |
190 | struct irq_affinity *desc) | |
191 | { | |
f94682dd MT |
192 | return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc); |
193 | } | |
194 | ||
195 | static inline | |
196 | int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs, | |
197 | struct virtqueue *vqs[], vq_callback_t *callbacks[], | |
198 | const char * const names[], const bool *ctx, | |
199 | struct irq_affinity *desc) | |
200 | { | |
201 | return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, | |
202 | desc); | |
9b2bbdb2 MT |
203 | } |
204 | ||
3569db59 MT |
205 | /** |
206 | * virtio_device_ready - enable vq use in probe function | |
207 | * @vdev: the device | |
208 | * | |
209 | * Driver must call this to use vqs in the probe function. | |
210 | * | |
211 | * Note: vqs are enabled automatically after probe returns. | |
212 | */ | |
213 | static inline | |
214 | void 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 | ||
66846048 RJ |
222 | static inline |
223 | const 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 | ||
75a0a52b JW |
230 | /** |
231 | * virtqueue_set_affinity - setting affinity for a virtqueue | |
232 | * @vq: the virtqueue | |
233 | * @cpu: the cpu no. | |
234 | * | |
235 | * Pay attention the function are best-effort: the affinity hint may not be set | |
236 | * due to config support, irq type and sharing. | |
237 | * | |
238 | */ | |
239 | static inline | |
19e226e8 | 240 | int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask) |
75a0a52b JW |
241 | { |
242 | struct virtio_device *vdev = vq->vdev; | |
243 | if (vdev->config->set_vq_affinity) | |
19e226e8 | 244 | return vdev->config->set_vq_affinity(vq, cpu_mask); |
75a0a52b JW |
245 | return 0; |
246 | } | |
247 | ||
cf561f0d GK |
248 | static inline bool virtio_is_little_endian(struct virtio_device *vdev) |
249 | { | |
7d824109 GK |
250 | return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) || |
251 | virtio_legacy_is_little_endian(); | |
cf561f0d GK |
252 | } |
253 | ||
eef960a0 MT |
254 | /* Memory accessors */ |
255 | static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) | |
256 | { | |
cf561f0d | 257 | return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
258 | } |
259 | ||
260 | static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) | |
261 | { | |
cf561f0d | 262 | return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
263 | } |
264 | ||
265 | static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) | |
266 | { | |
cf561f0d | 267 | return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
268 | } |
269 | ||
270 | static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) | |
271 | { | |
cf561f0d | 272 | return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
273 | } |
274 | ||
275 | static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) | |
276 | { | |
cf561f0d | 277 | return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
278 | } |
279 | ||
280 | static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) | |
281 | { | |
cf561f0d | 282 | return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); |
eef960a0 MT |
283 | } |
284 | ||
0b90d062 RR |
285 | /* Config space accessors. */ |
286 | #define virtio_cread(vdev, structname, member, ptr) \ | |
287 | do { \ | |
288 | /* Must match the member's type, and be integer */ \ | |
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 | /* Config space accessors. */ | |
315 | #define virtio_cwrite(vdev, structname, member, ptr) \ | |
316 | do { \ | |
317 | /* Must match the member's type, and be integer */ \ | |
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 | ||
d71de9ec MT |
347 | /* Read @count fields, @bytes each. */ |
348 | static 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 | ||
0b90d062 RR |
368 | static inline void virtio_cread_bytes(struct virtio_device *vdev, |
369 | unsigned int offset, | |
370 | void *buf, size_t len) | |
371 | { | |
d71de9ec | 372 | __virtio_cread_many(vdev, offset, buf, len, 1); |
0b90d062 RR |
373 | } |
374 | ||
caa0e2d0 MT |
375 | static 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 | ||
0b90d062 RR |
382 | static 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 | ||
388 | static 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)); | |
92e6d743 | 393 | return virtio16_to_cpu(vdev, (__force __virtio16)ret); |
0b90d062 RR |
394 | } |
395 | ||
396 | static inline void virtio_cwrite16(struct virtio_device *vdev, | |
397 | unsigned int offset, u16 val) | |
398 | { | |
92e6d743 | 399 | val = (__force u16)cpu_to_virtio16(vdev, val); |
0b90d062 RR |
400 | vdev->config->set(vdev, offset, &val, sizeof(val)); |
401 | } | |
402 | ||
403 | static 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)); | |
92e6d743 | 408 | return virtio32_to_cpu(vdev, (__force __virtio32)ret); |
0b90d062 RR |
409 | } |
410 | ||
411 | static inline void virtio_cwrite32(struct virtio_device *vdev, | |
412 | unsigned int offset, u32 val) | |
413 | { | |
92e6d743 | 414 | val = (__force u32)cpu_to_virtio32(vdev, val); |
0b90d062 RR |
415 | vdev->config->set(vdev, offset, &val, sizeof(val)); |
416 | } | |
417 | ||
418 | static inline u64 virtio_cread64(struct virtio_device *vdev, | |
419 | unsigned int offset) | |
420 | { | |
421 | u64 ret; | |
d71de9ec | 422 | __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); |
92e6d743 | 423 | return virtio64_to_cpu(vdev, (__force __virtio64)ret); |
0b90d062 RR |
424 | } |
425 | ||
426 | static inline void virtio_cwrite64(struct virtio_device *vdev, | |
427 | unsigned int offset, u64 val) | |
428 | { | |
92e6d743 | 429 | val = (__force u64)cpu_to_virtio64(vdev, val); |
0b90d062 RR |
430 | vdev->config->set(vdev, offset, &val, sizeof(val)); |
431 | } | |
432 | ||
433 | /* Conditional config space accessors. */ | |
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 | }) | |
75a0a52b | 443 | |
ec3d41c4 | 444 | #endif /* _LINUX_VIRTIO_CONFIG_H */ |