at v4.13 13 kB view raw
1#ifndef _LINUX_VIRTIO_CONFIG_H 2#define _LINUX_VIRTIO_CONFIG_H 3 4#include <linux/err.h> 5#include <linux/bug.h> 6#include <linux/virtio.h> 7#include <linux/virtio_byteorder.h> 8#include <uapi/linux/virtio_config.h> 9 10struct irq_affinity; 11 12/** 13 * virtio_config_ops - operations for configuring a virtio device 14 * @get: read the value of a configuration field 15 * vdev: the virtio_device 16 * offset: the offset of the configuration field 17 * buf: the buffer to write the field value into. 18 * len: the length of the buffer 19 * @set: write the value of a configuration field 20 * vdev: the virtio_device 21 * offset: the offset of the configuration field 22 * buf: the buffer to read the field value from. 23 * len: the length of the buffer 24 * @generation: config generation counter 25 * vdev: the virtio_device 26 * Returns the config generation counter 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 33 * @reset: reset the device 34 * vdev: the virtio device 35 * After this, status and feature negotiation must be done again 36 * Device must not be reset from its vq/config callbacks, or in 37 * parallel with being added/removed. 38 * @find_vqs: find virtqueues and instantiate them. 39 * vdev: the virtio_device 40 * nvqs: the number of virtqueues to find 41 * vqs: on success, includes new virtqueues 42 * callbacks: array of callbacks, for each virtqueue 43 * include a NULL entry for vqs that do not need a callback 44 * names: array of virtqueue names (mainly for debugging) 45 * include a NULL entry for vqs unused by driver 46 * Returns 0 on success or error status 47 * @del_vqs: free virtqueues found by find_vqs(). 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). 51 * @finalize_features: confirm what device features we'll be using. 52 * vdev: the virtio_device 53 * This gives the final feature bits for the device: it can change 54 * the dev->feature bits if it wants. 55 * Returns 0 on success or error status 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. 60 * @set_vq_affinity: set the affinity for a virtqueue. 61 * @get_vq_affinity: get the affinity for a virtqueue (optional). 62 */ 63typedef void vq_callback_t(struct virtqueue *); 64struct virtio_config_ops { 65 void (*get)(struct virtio_device *vdev, unsigned offset, 66 void *buf, unsigned len); 67 void (*set)(struct virtio_device *vdev, unsigned offset, 68 const void *buf, unsigned len); 69 u32 (*generation)(struct virtio_device *vdev); 70 u8 (*get_status)(struct virtio_device *vdev); 71 void (*set_status)(struct virtio_device *vdev, u8 status); 72 void (*reset)(struct virtio_device *vdev); 73 int (*find_vqs)(struct virtio_device *, unsigned nvqs, 74 struct virtqueue *vqs[], vq_callback_t *callbacks[], 75 const char * const names[], const bool *ctx, 76 struct irq_affinity *desc); 77 void (*del_vqs)(struct virtio_device *); 78 u64 (*get_features)(struct virtio_device *vdev); 79 int (*finalize_features)(struct virtio_device *vdev); 80 const char *(*bus_name)(struct virtio_device *vdev); 81 int (*set_vq_affinity)(struct virtqueue *vq, int cpu); 82 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev, 83 int index); 84}; 85 86/* If driver didn't advertise the feature, it will never appear. */ 87void virtio_check_driver_offered_feature(const struct virtio_device *vdev, 88 unsigned int fbit); 89 90/** 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. 94 * @vdev: the device 95 * @fbit: the feature bit 96 */ 97static 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)) 102 BUILD_BUG_ON(fbit >= 64); 103 else 104 BUG_ON(fbit >= 64); 105 106 return vdev->features & BIT_ULL(fbit); 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 */ 114static 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)) 119 BUILD_BUG_ON(fbit >= 64); 120 else 121 BUG_ON(fbit >= 64); 122 123 vdev->features |= BIT_ULL(fbit); 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 */ 131static inline void __virtio_clear_bit(struct virtio_device *vdev, 132 unsigned int fbit) 133{ 134 /* Did you forget to fix assumptions on max features? */ 135 if (__builtin_constant_p(fbit)) 136 BUILD_BUG_ON(fbit >= 64); 137 else 138 BUG_ON(fbit >= 64); 139 140 vdev->features &= ~BIT_ULL(fbit); 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 */ 148static inline bool virtio_has_feature(const struct virtio_device *vdev, 149 unsigned int fbit) 150{ 151 if (fbit < VIRTIO_TRANSPORT_F_START) 152 virtio_check_driver_offered_feature(vdev, fbit); 153 154 return __virtio_test_bit(vdev, fbit); 155} 156 157/** 158 * virtio_has_iommu_quirk - determine whether this device has the iommu quirk 159 * @vdev: the device 160 */ 161static 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 170static inline 171struct 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; 177 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL, 178 NULL); 179 if (err < 0) 180 return ERR_PTR(err); 181 return vq; 182} 183 184static inline 185int 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{ 190 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc); 191} 192 193static inline 194int 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); 201} 202 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 */ 211static inline 212void 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 220static inline 221const 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 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 */ 237static inline 238int 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 246static inline bool virtio_is_little_endian(struct virtio_device *vdev) 247{ 248 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) || 249 virtio_legacy_is_little_endian(); 250} 251 252/* Memory accessors */ 253static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) 254{ 255 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); 256} 257 258static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) 259{ 260 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); 261} 262 263static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) 264{ 265 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); 266} 267 268static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) 269{ 270 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); 271} 272 273static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) 274{ 275 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); 276} 277 278static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) 279{ 280 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); 281} 282 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 345/* Read @count fields, @bytes each. */ 346static 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 366static inline void virtio_cread_bytes(struct virtio_device *vdev, 367 unsigned int offset, 368 void *buf, size_t len) 369{ 370 __virtio_cread_many(vdev, offset, buf, len, 1); 371} 372 373static 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 380static 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 386static 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)); 391 return virtio16_to_cpu(vdev, (__force __virtio16)ret); 392} 393 394static inline void virtio_cwrite16(struct virtio_device *vdev, 395 unsigned int offset, u16 val) 396{ 397 val = (__force u16)cpu_to_virtio16(vdev, val); 398 vdev->config->set(vdev, offset, &val, sizeof(val)); 399} 400 401static 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)); 406 return virtio32_to_cpu(vdev, (__force __virtio32)ret); 407} 408 409static inline void virtio_cwrite32(struct virtio_device *vdev, 410 unsigned int offset, u32 val) 411{ 412 val = (__force u32)cpu_to_virtio32(vdev, val); 413 vdev->config->set(vdev, offset, &val, sizeof(val)); 414} 415 416static inline u64 virtio_cread64(struct virtio_device *vdev, 417 unsigned int offset) 418{ 419 u64 ret; 420 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); 421 return virtio64_to_cpu(vdev, (__force __virtio64)ret); 422} 423 424static inline void virtio_cwrite64(struct virtio_device *vdev, 425 unsigned int offset, u64 val) 426{ 427 val = (__force u64)cpu_to_virtio64(vdev, val); 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 }) 441 442#endif /* _LINUX_VIRTIO_CONFIG_H */