blob: 17f894a79e3162d246ec9c160678386fd25ac023 [file] [log] [blame]
Bin Meng8fb49b42018-10-15 02:21:00 -07001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
4 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
5 *
6 * VirtIO is a virtualization standard for network and disk device drivers
7 * where just the guest's device driver "knows" it is running in a virtual
8 * environment, and cooperates with the hypervisor. This enables guests to
9 * get high performance network and disk operations, and gives most of the
10 * performance benefits of paravirtualization. In the U-Boot case, the guest
11 * is U-Boot itself, while the virtual environment are normally QEMU targets
12 * like ARM, RISC-V and x86.
13 *
14 * See http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.pdf for
15 * the VirtIO specification v1.0.
16 *
17 * This file is largely based on Linux kernel virtio_*.h files
18 */
19
20#ifndef __VIRTIO_H__
21#define __VIRTIO_H__
22
Simon Glassd8bf49f2021-11-19 13:24:05 -070023#include <virtio_types.h>
Fiona Klute3be9f392024-05-01 10:54:09 +020024#include <dm/device.h>
Simon Glasscd93d622020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060026#include <linux/bug.h>
Tom Rini03de3052024-05-20 13:35:03 -060027#include <linux/typecheck.h>
Bin Meng8fb49b42018-10-15 02:21:00 -070028#define VIRTIO_ID_NET 1 /* virtio net */
29#define VIRTIO_ID_BLOCK 2 /* virtio block */
Sughosh Ganu03018ea2019-12-29 15:30:14 +053030#define VIRTIO_ID_RNG 4 /* virtio rng */
31#define VIRTIO_ID_MAX_NUM 5
Bin Meng8fb49b42018-10-15 02:21:00 -070032
33#define VIRTIO_NET_DRV_NAME "virtio-net"
34#define VIRTIO_BLK_DRV_NAME "virtio-blk"
Sughosh Ganu03018ea2019-12-29 15:30:14 +053035#define VIRTIO_RNG_DRV_NAME "virtio-rng"
Bin Meng8fb49b42018-10-15 02:21:00 -070036
37/* Status byte for guest to report progress, and synchronize features */
38
39/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
40#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
41/* We have found a driver for the device */
42#define VIRTIO_CONFIG_S_DRIVER 2
43/* Driver has used its parts of the config, and is happy */
44#define VIRTIO_CONFIG_S_DRIVER_OK 4
45/* Driver has finished configuring features */
46#define VIRTIO_CONFIG_S_FEATURES_OK 8
47/* Device entered invalid state, driver must reset it */
48#define VIRTIO_CONFIG_S_NEEDS_RESET 0x40
49/* We've given up on this device */
50#define VIRTIO_CONFIG_S_FAILED 0x80
51
52/*
53 * Virtio feature bits VIRTIO_TRANSPORT_F_START through VIRTIO_TRANSPORT_F_END
54 * are reserved for the transport being used (eg: virtio_ring, virtio_pci etc.),
55 * the rest are per-device feature bits.
56 */
57#define VIRTIO_TRANSPORT_F_START 28
58#define VIRTIO_TRANSPORT_F_END 38
59
60#ifndef VIRTIO_CONFIG_NO_LEGACY
61/*
62 * Do we get callbacks when the ring is completely used,
63 * even if we've suppressed them?
64 */
65#define VIRTIO_F_NOTIFY_ON_EMPTY 24
66
67/* Can the device handle any descriptor layout? */
68#define VIRTIO_F_ANY_LAYOUT 27
69#endif /* VIRTIO_CONFIG_NO_LEGACY */
70
71/* v1.0 compliant */
72#define VIRTIO_F_VERSION_1 32
73
74/*
75 * If clear - device has the IOMMU bypass quirk feature.
76 * If set - use platform tools to detect the IOMMU.
77 *
78 * Note the reverse polarity (compared to most other features),
79 * this is for compatibility with legacy systems.
80 */
81#define VIRTIO_F_IOMMU_PLATFORM 33
82
83/* Does the device support Single Root I/O Virtualization? */
84#define VIRTIO_F_SR_IOV 37
85
86/**
87 * virtio scatter-gather struct
88 *
89 * @addr: sg buffer address
90 * @lengh: sg buffer length
91 */
92struct virtio_sg {
93 void *addr;
94 size_t length;
95};
96
97struct virtqueue;
98
99/* virtio bus operations */
100struct dm_virtio_ops {
101 /**
102 * get_config() - read the value of a configuration field
103 *
104 * @vdev: the real virtio device
105 * @offset: the offset of the configuration field
106 * @buf: the buffer to write the field value into
107 * @len: the length of the buffer
108 * @return 0 if OK, -ve on error
109 */
110 int (*get_config)(struct udevice *vdev, unsigned int offset,
111 void *buf, unsigned int len);
112 /**
113 * set_config() - write the value of a configuration field
114 *
115 * @vdev: the real virtio device
116 * @offset: the offset of the configuration field
117 * @buf: the buffer to read the field value from
118 * @len: the length of the buffer
119 * @return 0 if OK, -ve on error
120 */
121 int (*set_config)(struct udevice *vdev, unsigned int offset,
122 const void *buf, unsigned int len);
123 /**
124 * generation() - config generation counter
125 *
126 * @vdev: the real virtio device
127 * @counter: the returned config generation counter
128 * @return 0 if OK, -ve on error
129 */
130 int (*generation)(struct udevice *vdev, u32 *counter);
131 /**
132 * get_status() - read the status byte
133 *
134 * @vdev: the real virtio device
135 * @status: the returned status byte
136 * @return 0 if OK, -ve on error
137 */
138 int (*get_status)(struct udevice *vdev, u8 *status);
139 /**
140 * set_status() - write the status byte
141 *
142 * @vdev: the real virtio device
143 * @status: the new status byte
144 * @return 0 if OK, -ve on error
145 */
146 int (*set_status)(struct udevice *vdev, u8 status);
147 /**
148 * reset() - reset the device
149 *
150 * @vdev: the real virtio device
151 * @return 0 if OK, -ve on error
152 */
153 int (*reset)(struct udevice *vdev);
154 /**
155 * get_features() - get the array of feature bits for this device
156 *
157 * @vdev: the real virtio device
158 * @features: the first 32 feature bits (all we currently need)
159 * @return 0 if OK, -ve on error
160 */
161 int (*get_features)(struct udevice *vdev, u64 *features);
162 /**
163 * set_features() - confirm what device features we'll be using
164 *
165 * @vdev: the real virtio device
166 * @return 0 if OK, -ve on error
167 */
168 int (*set_features)(struct udevice *vdev);
169 /**
170 * find_vqs() - find virtqueues and instantiate them
171 *
172 * @vdev: the real virtio device
173 * @nvqs: the number of virtqueues to find
174 * @vqs: on success, includes new virtqueues
175 * @return 0 if OK, -ve on error
176 */
177 int (*find_vqs)(struct udevice *vdev, unsigned int nvqs,
178 struct virtqueue *vqs[]);
179 /**
180 * del_vqs() - free virtqueues found by find_vqs()
181 *
182 * @vdev: the real virtio device
183 * @return 0 if OK, -ve on error
184 */
185 int (*del_vqs)(struct udevice *vdev);
186 /**
187 * notify() - notify the device to process the queue
188 *
189 * @vdev: the real virtio device
190 * @vq: virtqueue to process
191 * @return 0 if OK, -ve on error
192 */
193 int (*notify)(struct udevice *vdev, struct virtqueue *vq);
194};
195
196/* Get access to a virtio bus' operations */
197#define virtio_get_ops(dev) ((struct dm_virtio_ops *)(dev)->driver->ops)
198
199/**
200 * virtio uclass per device private data
201 *
202 * @vqs: virtualqueue for the virtio device
203 * @vdev: the real virtio device underneath
204 * @legacy: is it a legacy device?
205 * @device: virtio device ID
206 * @vendor: virtio vendor ID
207 * @features: negotiated supported features
208 * @feature_table: an array of feature supported by the driver
209 * @feature_table_size: number of entries in the feature table array
210 * @feature_table_legacy: same as feature_table but working in legacy mode
211 * @feature_table_size_legacy: number of entries in feature table legacy array
212 */
213struct virtio_dev_priv {
214 struct list_head vqs;
215 struct udevice *vdev;
216 bool legacy;
217 u32 device;
218 u32 vendor;
219 u64 features;
220 const u32 *feature_table;
221 u32 feature_table_size;
222 const u32 *feature_table_legacy;
223 u32 feature_table_size_legacy;
224};
225
226/**
227 * virtio_get_config() - read the value of a configuration field
228 *
229 * @vdev: the real virtio device
230 * @offset: the offset of the configuration field
231 * @buf: the buffer to write the field value into
232 * @len: the length of the buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100233 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700234 */
235int virtio_get_config(struct udevice *vdev, unsigned int offset,
236 void *buf, unsigned int len);
237
238/**
239 * virtio_set_config() - write the value of a configuration field
240 *
241 * @vdev: the real virtio device
242 * @offset: the offset of the configuration field
243 * @buf: the buffer to read the field value from
244 * @len: the length of the buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100245 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700246 */
247int virtio_set_config(struct udevice *vdev, unsigned int offset,
248 void *buf, unsigned int len);
249
250/**
251 * virtio_generation() - config generation counter
252 *
253 * @vdev: the real virtio device
254 * @counter: the returned config generation counter
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100255 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700256 */
257int virtio_generation(struct udevice *vdev, u32 *counter);
258
259/**
260 * virtio_get_status() - read the status byte
261 *
262 * @vdev: the real virtio device
263 * @status: the returned status byte
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100264 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700265 */
266int virtio_get_status(struct udevice *vdev, u8 *status);
267
268/**
269 * virtio_set_status() - write the status byte
270 *
271 * @vdev: the real virtio device
272 * @status: the new status byte
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100273 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700274 */
275int virtio_set_status(struct udevice *vdev, u8 status);
276
277/**
278 * virtio_reset() - reset the device
279 *
280 * @vdev: the real virtio device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100281 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700282 */
283int virtio_reset(struct udevice *vdev);
284
285/**
286 * virtio_get_features() - get the array of feature bits for this device
287 *
288 * @vdev: the real virtio device
289 * @features: the first 32 feature bits (all we currently need)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100290 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700291 */
292int virtio_get_features(struct udevice *vdev, u64 *features);
293
294/**
295 * virtio_set_features() - confirm what device features we'll be using
296 *
297 * @vdev: the real virtio device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100298 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700299 */
300int virtio_set_features(struct udevice *vdev);
301
302/**
303 * virtio_find_vqs() - find virtqueues and instantiate them
304 *
305 * @vdev: the real virtio device
306 * @nvqs: the number of virtqueues to find
307 * @vqs: on success, includes new virtqueues
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100308 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700309 */
310int virtio_find_vqs(struct udevice *vdev, unsigned int nvqs,
311 struct virtqueue *vqs[]);
312
313/**
314 * virtio_del_vqs() - free virtqueues found by find_vqs()
315 *
316 * @vdev: the real virtio device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100317 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700318 */
319int virtio_del_vqs(struct udevice *vdev);
320
321/**
322 * virtio_notify() - notify the device to process the queue
323 *
324 * @vdev: the real virtio device
325 * @vq: virtqueue to process
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100326 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700327 */
328int virtio_notify(struct udevice *vdev, struct virtqueue *vq);
329
330/**
331 * virtio_add_status() - helper to set a new status code to the device
332 *
333 * @vdev: the real virtio device
334 * @status: new status code to be added
335 */
336void virtio_add_status(struct udevice *vdev, u8 status);
337
338/**
339 * virtio_finalize_features() - helper to finalize features
340 *
341 * @vdev: the real virtio device
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100342 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700343 */
344int virtio_finalize_features(struct udevice *vdev);
345
346/**
347 * virtio_driver_features_init() - initialize driver supported features
348 *
349 * This fills in the virtio device parent per child private data with the given
350 * information, which contains driver supported features and legacy features.
351 *
352 * This API should be called in the virtio device driver's bind method, so that
353 * later virtio transport uclass driver can utilize the driver supplied features
354 * to negotiate with the device on the final supported features.
355 *
356 * @priv: virtio uclass per device private data
357 * @feature: an array of feature supported by the driver
358 * @feature_size: number of entries in the feature table array
359 * @feature_legacy: same as feature_table but working in legacy mode
360 * @feature_legacy_size:number of entries in feature table legacy array
361 */
362void virtio_driver_features_init(struct virtio_dev_priv *priv,
363 const u32 *feature,
364 u32 feature_size,
365 const u32 *feature_legacy,
366 u32 feature_legacy_size);
367
368/**
369 * virtio_init() - helper to enumerate all known virtio devices
370 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100371 * Return: 0 if OK, -ve on error
Bin Meng8fb49b42018-10-15 02:21:00 -0700372 */
373int virtio_init(void);
374
375static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
376{
377 if (little_endian)
378 return le16_to_cpu((__force __le16)val);
379 else
380 return be16_to_cpu((__force __be16)val);
381}
382
383static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
384{
385 if (little_endian)
386 return (__force __virtio16)cpu_to_le16(val);
387 else
388 return (__force __virtio16)cpu_to_be16(val);
389}
390
391static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
392{
393 if (little_endian)
394 return le32_to_cpu((__force __le32)val);
395 else
396 return be32_to_cpu((__force __be32)val);
397}
398
399static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
400{
401 if (little_endian)
402 return (__force __virtio32)cpu_to_le32(val);
403 else
404 return (__force __virtio32)cpu_to_be32(val);
405}
406
407static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
408{
409 if (little_endian)
410 return le64_to_cpu((__force __le64)val);
411 else
412 return be64_to_cpu((__force __be64)val);
413}
414
415static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
416{
417 if (little_endian)
418 return (__force __virtio64)cpu_to_le64(val);
419 else
420 return (__force __virtio64)cpu_to_be64(val);
421}
422
423/**
424 * __virtio_test_bit - helper to test feature bits
425 *
426 * For use by transports. Devices should normally use virtio_has_feature,
427 * which includes more checks.
428 *
429 * @udev: the transport device
430 * @fbit: the feature bit
431 */
432static inline bool __virtio_test_bit(struct udevice *udev, unsigned int fbit)
433{
434 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
435
436 /* Did you forget to fix assumptions on max features? */
437 if (__builtin_constant_p(fbit))
438 BUILD_BUG_ON(fbit >= 64);
439 else
440 WARN_ON(fbit >= 64);
441
442 return uc_priv->features & BIT_ULL(fbit);
443}
444
445/**
446 * __virtio_set_bit - helper to set feature bits
447 *
448 * For use by transports.
449 *
450 * @udev: the transport device
451 * @fbit: the feature bit
452 */
453static inline void __virtio_set_bit(struct udevice *udev, unsigned int fbit)
454{
455 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
456
457 /* Did you forget to fix assumptions on max features? */
458 if (__builtin_constant_p(fbit))
459 BUILD_BUG_ON(fbit >= 64);
460 else
461 WARN_ON(fbit >= 64);
462
463 uc_priv->features |= BIT_ULL(fbit);
464}
465
466/**
467 * __virtio_clear_bit - helper to clear feature bits
468 *
469 * For use by transports.
470 *
471 * @vdev: the transport device
472 * @fbit: the feature bit
473 */
474static inline void __virtio_clear_bit(struct udevice *udev, unsigned int fbit)
475{
476 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(udev);
477
478 /* Did you forget to fix assumptions on max features? */
479 if (__builtin_constant_p(fbit))
480 BUILD_BUG_ON(fbit >= 64);
481 else
482 WARN_ON(fbit >= 64);
483
484 uc_priv->features &= ~BIT_ULL(fbit);
485}
486
487/**
488 * virtio_has_feature - helper to determine if this device has this feature
489 *
490 * Note this API is only usable after the virtio device driver's bind phase,
491 * as the feature has been negotiated between the device and the driver.
492 *
493 * @vdev: the virtio device
494 * @fbit: the feature bit
495 */
496static inline bool virtio_has_feature(struct udevice *vdev, unsigned int fbit)
497{
Simon Glass73466df2020-12-19 10:40:10 -0700498 if (!(dev_get_flags(vdev) & DM_FLAG_BOUND))
Bin Meng8fb49b42018-10-15 02:21:00 -0700499 WARN_ON(true);
500
501 return __virtio_test_bit(vdev->parent, fbit);
502}
503
504static inline bool virtio_legacy_is_little_endian(void)
505{
506#ifdef __LITTLE_ENDIAN
507 return true;
508#else
509 return false;
510#endif
511}
512
513static inline bool virtio_is_little_endian(struct udevice *vdev)
514{
515 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(vdev->parent);
516
517 return !uc_priv->legacy || virtio_legacy_is_little_endian();
518}
519
520/* Memory accessors */
521static inline u16 virtio16_to_cpu(struct udevice *vdev, __virtio16 val)
522{
523 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
524}
525
526static inline __virtio16 cpu_to_virtio16(struct udevice *vdev, u16 val)
527{
528 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
529}
530
531static inline u32 virtio32_to_cpu(struct udevice *vdev, __virtio32 val)
532{
533 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
534}
535
536static inline __virtio32 cpu_to_virtio32(struct udevice *vdev, u32 val)
537{
538 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
539}
540
541static inline u64 virtio64_to_cpu(struct udevice *vdev, __virtio64 val)
542{
543 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
544}
545
546static inline __virtio64 cpu_to_virtio64(struct udevice *vdev, u64 val)
547{
548 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
549}
550
551/* Read @count fields, @bytes each */
552static inline void __virtio_cread_many(struct udevice *vdev,
553 unsigned int offset,
554 void *buf, size_t count, size_t bytes)
555{
556 u32 old, gen;
557 int i;
558
559 /* no need to check return value as generation can be optional */
560 virtio_generation(vdev, &gen);
561 do {
562 old = gen;
563
564 for (i = 0; i < count; i++)
565 virtio_get_config(vdev, offset + bytes * i,
566 buf + i * bytes, bytes);
567
568 virtio_generation(vdev, &gen);
569 } while (gen != old);
570}
571
572static inline void virtio_cread_bytes(struct udevice *vdev,
573 unsigned int offset,
574 void *buf, size_t len)
575{
576 __virtio_cread_many(vdev, offset, buf, len, 1);
577}
578
579static inline u8 virtio_cread8(struct udevice *vdev, unsigned int offset)
580{
581 u8 ret;
582
583 virtio_get_config(vdev, offset, &ret, sizeof(ret));
584 return ret;
585}
586
587static inline void virtio_cwrite8(struct udevice *vdev,
588 unsigned int offset, u8 val)
589{
590 virtio_set_config(vdev, offset, &val, sizeof(val));
591}
592
593static inline u16 virtio_cread16(struct udevice *vdev,
594 unsigned int offset)
595{
596 u16 ret;
597
598 virtio_get_config(vdev, offset, &ret, sizeof(ret));
599 return virtio16_to_cpu(vdev, (__force __virtio16)ret);
600}
601
602static inline void virtio_cwrite16(struct udevice *vdev,
603 unsigned int offset, u16 val)
604{
605 val = (__force u16)cpu_to_virtio16(vdev, val);
606 virtio_set_config(vdev, offset, &val, sizeof(val));
607}
608
609static inline u32 virtio_cread32(struct udevice *vdev,
610 unsigned int offset)
611{
612 u32 ret;
613
614 virtio_get_config(vdev, offset, &ret, sizeof(ret));
615 return virtio32_to_cpu(vdev, (__force __virtio32)ret);
616}
617
618static inline void virtio_cwrite32(struct udevice *vdev,
619 unsigned int offset, u32 val)
620{
621 val = (__force u32)cpu_to_virtio32(vdev, val);
622 virtio_set_config(vdev, offset, &val, sizeof(val));
623}
624
625static inline u64 virtio_cread64(struct udevice *vdev,
626 unsigned int offset)
627{
628 u64 ret;
629
630 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
631 return virtio64_to_cpu(vdev, (__force __virtio64)ret);
632}
633
634static inline void virtio_cwrite64(struct udevice *vdev,
635 unsigned int offset, u64 val)
636{
637 val = (__force u64)cpu_to_virtio64(vdev, val);
638 virtio_set_config(vdev, offset, &val, sizeof(val));
639}
640
641/* Config space read accessor */
642#define virtio_cread(vdev, structname, member, ptr) \
643 do { \
644 /* Must match the member's type, and be integer */ \
645 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
646 (*ptr) = 1; \
647 \
648 switch (sizeof(*ptr)) { \
649 case 1: \
650 *(ptr) = virtio_cread8(vdev, \
651 offsetof(structname, member)); \
652 break; \
653 case 2: \
654 *(ptr) = virtio_cread16(vdev, \
655 offsetof(structname, member)); \
656 break; \
657 case 4: \
658 *(ptr) = virtio_cread32(vdev, \
659 offsetof(structname, member)); \
660 break; \
661 case 8: \
662 *(ptr) = virtio_cread64(vdev, \
663 offsetof(structname, member)); \
664 break; \
665 default: \
666 WARN_ON(true); \
667 } \
668 } while (0)
669
670/* Config space write accessor */
671#define virtio_cwrite(vdev, structname, member, ptr) \
672 do { \
673 /* Must match the member's type, and be integer */ \
674 if (!typecheck(typeof((((structname *)0)->member)), *(ptr))) \
675 WARN_ON((*ptr) == 1); \
676 \
677 switch (sizeof(*ptr)) { \
678 case 1: \
679 virtio_cwrite8(vdev, \
680 offsetof(structname, member), \
681 *(ptr)); \
682 break; \
683 case 2: \
684 virtio_cwrite16(vdev, \
685 offsetof(structname, member), \
686 *(ptr)); \
687 break; \
688 case 4: \
689 virtio_cwrite32(vdev, \
690 offsetof(structname, member), \
691 *(ptr)); \
692 break; \
693 case 8: \
694 virtio_cwrite64(vdev, \
695 offsetof(structname, member), \
696 *(ptr)); \
697 break; \
698 default: \
699 WARN_ON(true); \
700 } \
701 } while (0)
702
703/* Conditional config space accessors */
704#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
705 ({ \
706 int _r = 0; \
707 if (!virtio_has_feature(vdev, fbit)) \
708 _r = -ENOENT; \
709 else \
710 virtio_cread(vdev, structname, member, ptr); \
711 _r; \
712 })
713
714#endif /* __VIRTIO_H__ */