at v5.9 16 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _LINUX_VIRTIO_CONFIG_H 3#define _LINUX_VIRTIO_CONFIG_H 4 5#include <linux/err.h> 6#include <linux/bug.h> 7#include <linux/virtio.h> 8#include <linux/virtio_byteorder.h> 9#include <linux/compiler_types.h> 10#include <uapi/linux/virtio_config.h> 11 12struct irq_affinity; 13 14/** 15 * virtio_config_ops - operations for configuring a virtio device 16 * Note: Do not assume that a transport implements all of the operations 17 * getting/setting a value as a simple read/write! Generally speaking, 18 * any of @get/@set, @get_status/@set_status, or @get_features/ 19 * @finalize_features are NOT safe to be called from an atomic 20 * context. 21 * @get: read the value of a configuration field 22 * vdev: the virtio_device 23 * offset: the offset of the configuration field 24 * buf: the buffer to write the field value into. 25 * len: the length of the buffer 26 * @set: write the value of a configuration field 27 * vdev: the virtio_device 28 * offset: the offset of the configuration field 29 * buf: the buffer to read the field value from. 30 * len: the length of the buffer 31 * @generation: config generation counter (optional) 32 * vdev: the virtio_device 33 * Returns the config generation counter 34 * @get_status: read the status byte 35 * vdev: the virtio_device 36 * Returns the status byte 37 * @set_status: write the status byte 38 * vdev: the virtio_device 39 * status: the new status byte 40 * @reset: reset the device 41 * vdev: the virtio device 42 * After this, status and feature negotiation must be done again 43 * Device must not be reset from its vq/config callbacks, or in 44 * parallel with being added/removed. 45 * @find_vqs: find virtqueues and instantiate them. 46 * vdev: the virtio_device 47 * nvqs: the number of virtqueues to find 48 * vqs: on success, includes new virtqueues 49 * callbacks: array of callbacks, for each virtqueue 50 * include a NULL entry for vqs that do not need a callback 51 * names: array of virtqueue names (mainly for debugging) 52 * include a NULL entry for vqs unused by driver 53 * Returns 0 on success or error status 54 * @del_vqs: free virtqueues found by find_vqs(). 55 * @get_features: get the array of feature bits for this device. 56 * vdev: the virtio_device 57 * Returns the first 64 feature bits (all we currently need). 58 * @finalize_features: confirm what device features we'll be using. 59 * vdev: the virtio_device 60 * This gives the final feature bits for the device: it can change 61 * the dev->feature bits if it wants. 62 * Returns 0 on success or error status 63 * @bus_name: return the bus name associated with the device (optional) 64 * vdev: the virtio_device 65 * This returns a pointer to the bus name a la pci_name from which 66 * the caller can then copy. 67 * @set_vq_affinity: set the affinity for a virtqueue (optional). 68 * @get_vq_affinity: get the affinity for a virtqueue (optional). 69 */ 70typedef void vq_callback_t(struct virtqueue *); 71struct virtio_config_ops { 72 void (*get)(struct virtio_device *vdev, unsigned offset, 73 void *buf, unsigned len); 74 void (*set)(struct virtio_device *vdev, unsigned offset, 75 const void *buf, unsigned len); 76 u32 (*generation)(struct virtio_device *vdev); 77 u8 (*get_status)(struct virtio_device *vdev); 78 void (*set_status)(struct virtio_device *vdev, u8 status); 79 void (*reset)(struct virtio_device *vdev); 80 int (*find_vqs)(struct virtio_device *, unsigned nvqs, 81 struct virtqueue *vqs[], vq_callback_t *callbacks[], 82 const char * const names[], const bool *ctx, 83 struct irq_affinity *desc); 84 void (*del_vqs)(struct virtio_device *); 85 u64 (*get_features)(struct virtio_device *vdev); 86 int (*finalize_features)(struct virtio_device *vdev); 87 const char *(*bus_name)(struct virtio_device *vdev); 88 int (*set_vq_affinity)(struct virtqueue *vq, 89 const struct cpumask *cpu_mask); 90 const struct cpumask *(*get_vq_affinity)(struct virtio_device *vdev, 91 int index); 92}; 93 94/* If driver didn't advertise the feature, it will never appear. */ 95void virtio_check_driver_offered_feature(const struct virtio_device *vdev, 96 unsigned int fbit); 97 98/** 99 * __virtio_test_bit - helper to test feature bits. For use by transports. 100 * Devices should normally use virtio_has_feature, 101 * which includes more checks. 102 * @vdev: the device 103 * @fbit: the feature bit 104 */ 105static inline bool __virtio_test_bit(const 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)) 110 BUILD_BUG_ON(fbit >= 64); 111 else 112 BUG_ON(fbit >= 64); 113 114 return vdev->features & BIT_ULL(fbit); 115} 116 117/** 118 * __virtio_set_bit - helper to set feature bits. For use by transports. 119 * @vdev: the device 120 * @fbit: the feature bit 121 */ 122static inline void __virtio_set_bit(struct virtio_device *vdev, 123 unsigned int fbit) 124{ 125 /* Did you forget to fix assumptions on max features? */ 126 if (__builtin_constant_p(fbit)) 127 BUILD_BUG_ON(fbit >= 64); 128 else 129 BUG_ON(fbit >= 64); 130 131 vdev->features |= BIT_ULL(fbit); 132} 133 134/** 135 * __virtio_clear_bit - helper to clear feature bits. For use by transports. 136 * @vdev: the device 137 * @fbit: the feature bit 138 */ 139static inline void __virtio_clear_bit(struct virtio_device *vdev, 140 unsigned int fbit) 141{ 142 /* Did you forget to fix assumptions on max features? */ 143 if (__builtin_constant_p(fbit)) 144 BUILD_BUG_ON(fbit >= 64); 145 else 146 BUG_ON(fbit >= 64); 147 148 vdev->features &= ~BIT_ULL(fbit); 149} 150 151/** 152 * virtio_has_feature - helper to determine if this device has this feature. 153 * @vdev: the device 154 * @fbit: the feature bit 155 */ 156static inline bool virtio_has_feature(const struct virtio_device *vdev, 157 unsigned int fbit) 158{ 159 if (fbit < VIRTIO_TRANSPORT_F_START) 160 virtio_check_driver_offered_feature(vdev, fbit); 161 162 return __virtio_test_bit(vdev, fbit); 163} 164 165/** 166 * virtio_has_dma_quirk - determine whether this device has the DMA quirk 167 * @vdev: the device 168 */ 169static inline bool virtio_has_dma_quirk(const struct virtio_device *vdev) 170{ 171 /* 172 * Note the reverse polarity of the quirk feature (compared to most 173 * other features), this is for compatibility with legacy systems. 174 */ 175 return !virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM); 176} 177 178static inline 179struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev, 180 vq_callback_t *c, const char *n) 181{ 182 vq_callback_t *callbacks[] = { c }; 183 const char *names[] = { n }; 184 struct virtqueue *vq; 185 int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names, NULL, 186 NULL); 187 if (err < 0) 188 return ERR_PTR(err); 189 return vq; 190} 191 192static inline 193int virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs, 194 struct virtqueue *vqs[], vq_callback_t *callbacks[], 195 const char * const names[], 196 struct irq_affinity *desc) 197{ 198 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, NULL, desc); 199} 200 201static inline 202int virtio_find_vqs_ctx(struct virtio_device *vdev, unsigned nvqs, 203 struct virtqueue *vqs[], vq_callback_t *callbacks[], 204 const char * const names[], const bool *ctx, 205 struct irq_affinity *desc) 206{ 207 return vdev->config->find_vqs(vdev, nvqs, vqs, callbacks, names, ctx, 208 desc); 209} 210 211/** 212 * virtio_device_ready - enable vq use in probe function 213 * @vdev: the device 214 * 215 * Driver must call this to use vqs in the probe function. 216 * 217 * Note: vqs are enabled automatically after probe returns. 218 */ 219static inline 220void virtio_device_ready(struct virtio_device *dev) 221{ 222 unsigned status = dev->config->get_status(dev); 223 224 BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK); 225 dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK); 226} 227 228static inline 229const char *virtio_bus_name(struct virtio_device *vdev) 230{ 231 if (!vdev->config->bus_name) 232 return "virtio"; 233 return vdev->config->bus_name(vdev); 234} 235 236/** 237 * virtqueue_set_affinity - setting affinity for a virtqueue 238 * @vq: the virtqueue 239 * @cpu: the cpu no. 240 * 241 * Pay attention the function are best-effort: the affinity hint may not be set 242 * due to config support, irq type and sharing. 243 * 244 */ 245static inline 246int virtqueue_set_affinity(struct virtqueue *vq, const struct cpumask *cpu_mask) 247{ 248 struct virtio_device *vdev = vq->vdev; 249 if (vdev->config->set_vq_affinity) 250 return vdev->config->set_vq_affinity(vq, cpu_mask); 251 return 0; 252} 253 254static inline bool virtio_is_little_endian(struct virtio_device *vdev) 255{ 256 return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) || 257 virtio_legacy_is_little_endian(); 258} 259 260/* Memory accessors */ 261static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val) 262{ 263 return __virtio16_to_cpu(virtio_is_little_endian(vdev), val); 264} 265 266static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val) 267{ 268 return __cpu_to_virtio16(virtio_is_little_endian(vdev), val); 269} 270 271static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val) 272{ 273 return __virtio32_to_cpu(virtio_is_little_endian(vdev), val); 274} 275 276static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val) 277{ 278 return __cpu_to_virtio32(virtio_is_little_endian(vdev), val); 279} 280 281static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val) 282{ 283 return __virtio64_to_cpu(virtio_is_little_endian(vdev), val); 284} 285 286static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val) 287{ 288 return __cpu_to_virtio64(virtio_is_little_endian(vdev), val); 289} 290 291#define virtio_to_cpu(vdev, x) \ 292 _Generic((x), \ 293 __u8: (x), \ 294 __virtio16: virtio16_to_cpu((vdev), (x)), \ 295 __virtio32: virtio32_to_cpu((vdev), (x)), \ 296 __virtio64: virtio64_to_cpu((vdev), (x)) \ 297 ) 298 299#define cpu_to_virtio(vdev, x, m) \ 300 _Generic((m), \ 301 __u8: (x), \ 302 __virtio16: cpu_to_virtio16((vdev), (x)), \ 303 __virtio32: cpu_to_virtio32((vdev), (x)), \ 304 __virtio64: cpu_to_virtio64((vdev), (x)) \ 305 ) 306 307#define __virtio_native_type(structname, member) \ 308 typeof(virtio_to_cpu(NULL, ((structname*)0)->member)) 309 310/* Config space accessors. */ 311#define virtio_cread(vdev, structname, member, ptr) \ 312 do { \ 313 typeof(((structname*)0)->member) virtio_cread_v; \ 314 \ 315 might_sleep(); \ 316 /* Sanity check: must match the member's type */ \ 317 typecheck(typeof(virtio_to_cpu((vdev), virtio_cread_v)), *(ptr)); \ 318 \ 319 switch (sizeof(virtio_cread_v)) { \ 320 case 1: \ 321 case 2: \ 322 case 4: \ 323 vdev->config->get((vdev), \ 324 offsetof(structname, member), \ 325 &virtio_cread_v, \ 326 sizeof(virtio_cread_v)); \ 327 break; \ 328 default: \ 329 __virtio_cread_many((vdev), \ 330 offsetof(structname, member), \ 331 &virtio_cread_v, \ 332 1, \ 333 sizeof(virtio_cread_v)); \ 334 break; \ 335 } \ 336 *(ptr) = virtio_to_cpu(vdev, virtio_cread_v); \ 337 } while(0) 338 339/* Config space accessors. */ 340#define virtio_cwrite(vdev, structname, member, ptr) \ 341 do { \ 342 typeof(((structname*)0)->member) virtio_cwrite_v = \ 343 cpu_to_virtio(vdev, *(ptr), ((structname*)0)->member); \ 344 \ 345 might_sleep(); \ 346 /* Sanity check: must match the member's type */ \ 347 typecheck(typeof(virtio_to_cpu((vdev), virtio_cwrite_v)), *(ptr)); \ 348 \ 349 vdev->config->set((vdev), offsetof(structname, member), \ 350 &virtio_cwrite_v, \ 351 sizeof(virtio_cwrite_v)); \ 352 } while(0) 353 354/* 355 * Nothing virtio-specific about these, but let's worry about generalizing 356 * these later. 357 */ 358#define virtio_le_to_cpu(x) \ 359 _Generic((x), \ 360 __u8: (u8)(x), \ 361 __le16: (u16)le16_to_cpu(x), \ 362 __le32: (u32)le32_to_cpu(x), \ 363 __le64: (u64)le64_to_cpu(x) \ 364 ) 365 366#define virtio_cpu_to_le(x, m) \ 367 _Generic((m), \ 368 __u8: (x), \ 369 __le16: cpu_to_le16(x), \ 370 __le32: cpu_to_le32(x), \ 371 __le64: cpu_to_le64(x) \ 372 ) 373 374/* LE (e.g. modern) Config space accessors. */ 375#define virtio_cread_le(vdev, structname, member, ptr) \ 376 do { \ 377 typeof(((structname*)0)->member) virtio_cread_v; \ 378 \ 379 might_sleep(); \ 380 /* Sanity check: must match the member's type */ \ 381 typecheck(typeof(virtio_le_to_cpu(virtio_cread_v)), *(ptr)); \ 382 \ 383 switch (sizeof(virtio_cread_v)) { \ 384 case 1: \ 385 case 2: \ 386 case 4: \ 387 vdev->config->get((vdev), \ 388 offsetof(structname, member), \ 389 &virtio_cread_v, \ 390 sizeof(virtio_cread_v)); \ 391 break; \ 392 default: \ 393 __virtio_cread_many((vdev), \ 394 offsetof(structname, member), \ 395 &virtio_cread_v, \ 396 1, \ 397 sizeof(virtio_cread_v)); \ 398 break; \ 399 } \ 400 *(ptr) = virtio_le_to_cpu(virtio_cread_v); \ 401 } while(0) 402 403#define virtio_cwrite_le(vdev, structname, member, ptr) \ 404 do { \ 405 typeof(((structname*)0)->member) virtio_cwrite_v = \ 406 virtio_cpu_to_le(*(ptr), ((structname*)0)->member); \ 407 \ 408 might_sleep(); \ 409 /* Sanity check: must match the member's type */ \ 410 typecheck(typeof(virtio_le_to_cpu(virtio_cwrite_v)), *(ptr)); \ 411 \ 412 vdev->config->set((vdev), offsetof(structname, member), \ 413 &virtio_cwrite_v, \ 414 sizeof(virtio_cwrite_v)); \ 415 } while(0) 416 417 418/* Read @count fields, @bytes each. */ 419static inline void __virtio_cread_many(struct virtio_device *vdev, 420 unsigned int offset, 421 void *buf, size_t count, size_t bytes) 422{ 423 u32 old, gen = vdev->config->generation ? 424 vdev->config->generation(vdev) : 0; 425 int i; 426 427 might_sleep(); 428 do { 429 old = gen; 430 431 for (i = 0; i < count; i++) 432 vdev->config->get(vdev, offset + bytes * i, 433 buf + i * bytes, bytes); 434 435 gen = vdev->config->generation ? 436 vdev->config->generation(vdev) : 0; 437 } while (gen != old); 438} 439 440static inline void virtio_cread_bytes(struct virtio_device *vdev, 441 unsigned int offset, 442 void *buf, size_t len) 443{ 444 __virtio_cread_many(vdev, offset, buf, len, 1); 445} 446 447static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset) 448{ 449 u8 ret; 450 451 might_sleep(); 452 vdev->config->get(vdev, offset, &ret, sizeof(ret)); 453 return ret; 454} 455 456static inline void virtio_cwrite8(struct virtio_device *vdev, 457 unsigned int offset, u8 val) 458{ 459 might_sleep(); 460 vdev->config->set(vdev, offset, &val, sizeof(val)); 461} 462 463static inline u16 virtio_cread16(struct virtio_device *vdev, 464 unsigned int offset) 465{ 466 __virtio16 ret; 467 468 might_sleep(); 469 vdev->config->get(vdev, offset, &ret, sizeof(ret)); 470 return virtio16_to_cpu(vdev, ret); 471} 472 473static inline void virtio_cwrite16(struct virtio_device *vdev, 474 unsigned int offset, u16 val) 475{ 476 __virtio16 v; 477 478 might_sleep(); 479 v = cpu_to_virtio16(vdev, val); 480 vdev->config->set(vdev, offset, &v, sizeof(v)); 481} 482 483static inline u32 virtio_cread32(struct virtio_device *vdev, 484 unsigned int offset) 485{ 486 __virtio32 ret; 487 488 might_sleep(); 489 vdev->config->get(vdev, offset, &ret, sizeof(ret)); 490 return virtio32_to_cpu(vdev, ret); 491} 492 493static inline void virtio_cwrite32(struct virtio_device *vdev, 494 unsigned int offset, u32 val) 495{ 496 __virtio32 v; 497 498 might_sleep(); 499 v = cpu_to_virtio32(vdev, val); 500 vdev->config->set(vdev, offset, &v, sizeof(v)); 501} 502 503static inline u64 virtio_cread64(struct virtio_device *vdev, 504 unsigned int offset) 505{ 506 __virtio64 ret; 507 508 __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret)); 509 return virtio64_to_cpu(vdev, ret); 510} 511 512static inline void virtio_cwrite64(struct virtio_device *vdev, 513 unsigned int offset, u64 val) 514{ 515 __virtio64 v; 516 517 might_sleep(); 518 v = cpu_to_virtio64(vdev, val); 519 vdev->config->set(vdev, offset, &v, sizeof(v)); 520} 521 522/* Conditional config space accessors. */ 523#define virtio_cread_feature(vdev, fbit, structname, member, ptr) \ 524 ({ \ 525 int _r = 0; \ 526 if (!virtio_has_feature(vdev, fbit)) \ 527 _r = -ENOENT; \ 528 else \ 529 virtio_cread((vdev), structname, member, ptr); \ 530 _r; \ 531 }) 532 533/* Conditional config space accessors. */ 534#define virtio_cread_le_feature(vdev, fbit, structname, member, ptr) \ 535 ({ \ 536 int _r = 0; \ 537 if (!virtio_has_feature(vdev, fbit)) \ 538 _r = -ENOENT; \ 539 else \ 540 virtio_cread_le((vdev), structname, member, ptr); \ 541 _r; \ 542 }) 543#endif /* _LINUX_VIRTIO_CONFIG_H */