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