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