diff options
Diffstat (limited to 'include/linux/virtio_config.h')
| -rw-r--r-- | include/linux/virtio_config.h | 81 |
1 files changed, 44 insertions, 37 deletions
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index d581b2914b34..50db245c81ad 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h | |||
| @@ -16,27 +16,20 @@ | |||
| 16 | #define VIRTIO_CONFIG_S_FAILED 0x80 | 16 | #define VIRTIO_CONFIG_S_FAILED 0x80 |
| 17 | 17 | ||
| 18 | #ifdef __KERNEL__ | 18 | #ifdef __KERNEL__ |
| 19 | struct virtio_device; | 19 | #include <linux/virtio.h> |
| 20 | 20 | ||
| 21 | /** | 21 | /** |
| 22 | * virtio_config_ops - operations for configuring a virtio device | 22 | * virtio_config_ops - operations for configuring a virtio device |
| 23 | * @feature: search for a feature in this config | ||
| 24 | * vdev: the virtio_device | ||
| 25 | * bit: the feature bit | ||
| 26 | * Returns true if the feature is supported. Acknowledges the feature | ||
| 27 | * so the host can see it. | ||
| 28 | * @get: read the value of a configuration field | 23 | * @get: read the value of a configuration field |
| 29 | * vdev: the virtio_device | 24 | * vdev: the virtio_device |
| 30 | * offset: the offset of the configuration field | 25 | * offset: the offset of the configuration field |
| 31 | * buf: the buffer to write the field value into. | 26 | * buf: the buffer to write the field value into. |
| 32 | * len: the length of the buffer | 27 | * len: the length of the buffer |
| 33 | * Note that contents are conventionally little-endian. | ||
| 34 | * @set: write the value of a configuration field | 28 | * @set: write the value of a configuration field |
| 35 | * vdev: the virtio_device | 29 | * vdev: the virtio_device |
| 36 | * offset: the offset of the configuration field | 30 | * offset: the offset of the configuration field |
| 37 | * buf: the buffer to read the field value from. | 31 | * buf: the buffer to read the field value from. |
| 38 | * len: the length of the buffer | 32 | * len: the length of the buffer |
| 39 | * Note that contents are conventionally little-endian. | ||
| 40 | * @get_status: read the status byte | 33 | * @get_status: read the status byte |
| 41 | * vdev: the virtio_device | 34 | * vdev: the virtio_device |
| 42 | * Returns the status byte | 35 | * Returns the status byte |
| @@ -52,10 +45,15 @@ struct virtio_device; | |||
| 52 | * callback: the virqtueue callback | 45 | * callback: the virqtueue callback |
| 53 | * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT). | 46 | * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT). |
| 54 | * @del_vq: free a virtqueue found by find_vq(). | 47 | * @del_vq: free a virtqueue found by find_vq(). |
| 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 | * @set_features: confirm what device features we'll be using. | ||
| 52 | * vdev: the virtio_device | ||
| 53 | * feature: the first 32 feature bits | ||
| 55 | */ | 54 | */ |
| 56 | struct virtio_config_ops | 55 | struct virtio_config_ops |
| 57 | { | 56 | { |
| 58 | bool (*feature)(struct virtio_device *vdev, unsigned bit); | ||
| 59 | void (*get)(struct virtio_device *vdev, unsigned offset, | 57 | void (*get)(struct virtio_device *vdev, unsigned offset, |
| 60 | void *buf, unsigned len); | 58 | void *buf, unsigned len); |
| 61 | void (*set)(struct virtio_device *vdev, unsigned offset, | 59 | void (*set)(struct virtio_device *vdev, unsigned offset, |
| @@ -67,43 +65,52 @@ struct virtio_config_ops | |||
| 67 | unsigned index, | 65 | unsigned index, |
| 68 | void (*callback)(struct virtqueue *)); | 66 | void (*callback)(struct virtqueue *)); |
| 69 | void (*del_vq)(struct virtqueue *vq); | 67 | void (*del_vq)(struct virtqueue *vq); |
| 68 | u32 (*get_features)(struct virtio_device *vdev); | ||
| 69 | void (*set_features)(struct virtio_device *vdev, u32 features); | ||
| 70 | }; | 70 | }; |
| 71 | 71 | ||
| 72 | /* If driver didn't advertise the feature, it will never appear. */ | ||
| 73 | void virtio_check_driver_offered_feature(const struct virtio_device *vdev, | ||
| 74 | unsigned int fbit); | ||
| 75 | |||
| 72 | /** | 76 | /** |
| 73 | * virtio_config_val - look for a feature and get a single virtio config. | 77 | * virtio_has_feature - helper to determine if this device has this feature. |
| 74 | * @vdev: the virtio device | 78 | * @vdev: the device |
| 75 | * @fbit: the feature bit | 79 | * @fbit: the feature bit |
| 76 | * @offset: the type to search for. | 80 | */ |
| 77 | * @val: a pointer to the value to fill in. | 81 | static inline bool virtio_has_feature(const struct virtio_device *vdev, |
| 78 | * | 82 | unsigned int fbit) |
| 79 | * The return value is -ENOENT if the feature doesn't exist. Otherwise | 83 | { |
| 80 | * the value is endian-corrected and returned in v. */ | 84 | /* Did you forget to fix assumptions on max features? */ |
| 81 | #define virtio_config_val(vdev, fbit, offset, v) ({ \ | 85 | if (__builtin_constant_p(fbit)) |
| 82 | int _err; \ | 86 | BUILD_BUG_ON(fbit >= 32); |
| 83 | if ((vdev)->config->feature((vdev), (fbit))) { \ | 87 | |
| 84 | __virtio_config_val((vdev), (offset), (v)); \ | 88 | virtio_check_driver_offered_feature(vdev, fbit); |
| 85 | _err = 0; \ | 89 | return test_bit(fbit, vdev->features); |
| 86 | } else \ | 90 | } |
| 87 | _err = -ENOENT; \ | ||
| 88 | _err; \ | ||
| 89 | }) | ||
| 90 | 91 | ||
| 91 | /** | 92 | /** |
| 92 | * __virtio_config_val - get a single virtio config without feature check. | 93 | * virtio_config_val - look for a feature and get a virtio config entry. |
| 93 | * @vdev: the virtio device | 94 | * @vdev: the virtio device |
| 95 | * @fbit: the feature bit | ||
| 94 | * @offset: the type to search for. | 96 | * @offset: the type to search for. |
| 95 | * @val: a pointer to the value to fill in. | 97 | * @val: a pointer to the value to fill in. |
| 96 | * | 98 | * |
| 97 | * The value is endian-corrected and returned in v. */ | 99 | * The return value is -ENOENT if the feature doesn't exist. Otherwise |
| 98 | #define __virtio_config_val(vdev, offset, v) do { \ | 100 | * the config value is copied into whatever is pointed to by v. */ |
| 99 | BUILD_BUG_ON(sizeof(*(v)) != 1 && sizeof(*(v)) != 2 \ | 101 | #define virtio_config_val(vdev, fbit, offset, v) \ |
| 100 | && sizeof(*(v)) != 4 && sizeof(*(v)) != 8); \ | 102 | virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(v)) |
| 101 | (vdev)->config->get((vdev), (offset), (v), sizeof(*(v))); \ | 103 | |
| 102 | switch (sizeof(*(v))) { \ | 104 | static inline int virtio_config_buf(struct virtio_device *vdev, |
| 103 | case 2: le16_to_cpus((__u16 *) v); break; \ | 105 | unsigned int fbit, |
| 104 | case 4: le32_to_cpus((__u32 *) v); break; \ | 106 | unsigned int offset, |
| 105 | case 8: le64_to_cpus((__u64 *) v); break; \ | 107 | void *buf, unsigned len) |
| 106 | } \ | 108 | { |
| 107 | } while(0) | 109 | if (!virtio_has_feature(vdev, fbit)) |
| 110 | return -ENOENT; | ||
| 111 | |||
| 112 | vdev->config->get(vdev, offset, buf, len); | ||
| 113 | return 0; | ||
| 114 | } | ||
| 108 | #endif /* __KERNEL__ */ | 115 | #endif /* __KERNEL__ */ |
| 109 | #endif /* _LINUX_VIRTIO_CONFIG_H */ | 116 | #endif /* _LINUX_VIRTIO_CONFIG_H */ |
