blob: 70bb26062d76f43efcf9899df26c4a774417c812 [file] [log] [blame]
Rusty Russellec3d41c2007-10-22 11:03:36 +10001#ifndef _LINUX_VIRTIO_CONFIG_H
2#define _LINUX_VIRTIO_CONFIG_H
3/* Virtio devices use a standardized configuration space to define their
4 * features and pass configuration information, but each implementation can
5 * store and access that space differently. */
6#include <linux/types.h>
7
Rusty Russella586d4f2008-02-04 23:49:56 -05008/* Status byte for guest to report progress, and synchronize features. */
Rusty Russellec3d41c2007-10-22 11:03:36 +10009/* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */
10#define VIRTIO_CONFIG_S_ACKNOWLEDGE 1
11/* We have found a driver for the device. */
12#define VIRTIO_CONFIG_S_DRIVER 2
13/* Driver has used its parts of the config, and is happy */
14#define VIRTIO_CONFIG_S_DRIVER_OK 4
15/* We've given up on this device. */
16#define VIRTIO_CONFIG_S_FAILED 0x80
17
Rusty Russellec3d41c2007-10-22 11:03:36 +100018#ifdef __KERNEL__
19struct virtio_device;
20
21/**
22 * virtio_config_ops - operations for configuring a virtio device
Rusty Russella586d4f2008-02-04 23:49:56 -050023 * @feature: search for a feature in this config
Rusty Russellec3d41c2007-10-22 11:03:36 +100024 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050025 * 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
Rusty Russellec3d41c2007-10-22 11:03:36 +100029 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050030 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100031 * buf: the buffer to write the field value into.
Rusty Russella586d4f2008-02-04 23:49:56 -050032 * len: the length of the buffer
Rusty Russellec3d41c2007-10-22 11:03:36 +100033 * Note that contents are conventionally little-endian.
Rusty Russella586d4f2008-02-04 23:49:56 -050034 * @set: write the value of a configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100035 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050036 * offset: the offset of the configuration field
Rusty Russellec3d41c2007-10-22 11:03:36 +100037 * buf: the buffer to read the field value from.
Rusty Russella586d4f2008-02-04 23:49:56 -050038 * len: the length of the buffer
Rusty Russellec3d41c2007-10-22 11:03:36 +100039 * Note that contents are conventionally little-endian.
40 * @get_status: read the status byte
41 * vdev: the virtio_device
42 * Returns the status byte
43 * @set_status: write the status byte
44 * vdev: the virtio_device
45 * status: the new status byte
Rusty Russella586d4f2008-02-04 23:49:56 -050046 * @find_vq: find a virtqueue and instantiate it.
Rusty Russellec3d41c2007-10-22 11:03:36 +100047 * vdev: the virtio_device
Rusty Russella586d4f2008-02-04 23:49:56 -050048 * index: the 0-based virtqueue number in case there's more than one.
Rusty Russellec3d41c2007-10-22 11:03:36 +100049 * callback: the virqtueue callback
Rusty Russella586d4f2008-02-04 23:49:56 -050050 * Returns the new virtqueue or ERR_PTR() (eg. -ENOENT).
Rusty Russellec3d41c2007-10-22 11:03:36 +100051 * @del_vq: free a virtqueue found by find_vq().
52 */
53struct virtio_config_ops
54{
Rusty Russella586d4f2008-02-04 23:49:56 -050055 bool (*feature)(struct virtio_device *vdev, unsigned bit);
56 void (*get)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100057 void *buf, unsigned len);
Rusty Russella586d4f2008-02-04 23:49:56 -050058 void (*set)(struct virtio_device *vdev, unsigned offset,
Rusty Russellec3d41c2007-10-22 11:03:36 +100059 const void *buf, unsigned len);
60 u8 (*get_status)(struct virtio_device *vdev);
61 void (*set_status)(struct virtio_device *vdev, u8 status);
62 struct virtqueue *(*find_vq)(struct virtio_device *vdev,
Rusty Russella586d4f2008-02-04 23:49:56 -050063 unsigned index,
Rusty Russellec3d41c2007-10-22 11:03:36 +100064 bool (*callback)(struct virtqueue *));
65 void (*del_vq)(struct virtqueue *vq);
66};
67
68/**
Rusty Russella586d4f2008-02-04 23:49:56 -050069 * virtio_config_val - look for a feature and get a single virtio config.
70 * @vdev: the virtio device
71 * @fbit: the feature bit
72 * @offset: the type to search for.
Rusty Russellec3d41c2007-10-22 11:03:36 +100073 * @val: a pointer to the value to fill in.
74 *
Rusty Russella586d4f2008-02-04 23:49:56 -050075 * The return value is -ENOENT if the feature doesn't exist. Otherwise
76 * the value is endian-corrected and returned in v. */
77#define virtio_config_val(vdev, fbit, offset, v) ({ \
78 int _err; \
79 if ((vdev)->config->feature((vdev), (fbit))) { \
80 __virtio_config_val((vdev), (offset), (v)); \
81 _err = 0; \
82 } else \
83 _err = -ENOENT; \
Rusty Russellec3d41c2007-10-22 11:03:36 +100084 _err; \
85})
86
Rusty Russellec3d41c2007-10-22 11:03:36 +100087/**
Rusty Russella586d4f2008-02-04 23:49:56 -050088 * __virtio_config_val - get a single virtio config without feature check.
89 * @vdev: the virtio device
90 * @offset: the type to search for.
91 * @val: a pointer to the value to fill in.
Rusty Russellec3d41c2007-10-22 11:03:36 +100092 *
Rusty Russella586d4f2008-02-04 23:49:56 -050093 * The value is endian-corrected and returned in v. */
94#define __virtio_config_val(vdev, offset, v) do { \
95 BUILD_BUG_ON(sizeof(*(v)) != 1 && sizeof(*(v)) != 2 \
96 && sizeof(*(v)) != 4 && sizeof(*(v)) != 8); \
97 (vdev)->config->get((vdev), (offset), (v), sizeof(*(v))); \
98 switch (sizeof(*(v))) { \
99 case 2: le16_to_cpus((__u16 *) v); break; \
100 case 4: le32_to_cpus((__u32 *) v); break; \
101 case 8: le64_to_cpus((__u64 *) v); break; \
102 } \
103} while(0)
Rusty Russellec3d41c2007-10-22 11:03:36 +1000104#endif /* __KERNEL__ */
105#endif /* _LINUX_VIRTIO_CONFIG_H */