aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2007-10-21 21:03:36 -0400
committerRusty Russell <rusty@rustcorp.com.au>2007-10-23 01:49:54 -0400
commitec3d41c4db4c21164332826ea8d812f94f2f6886 (patch)
tree9b947e900745cfcc4df7409c6ba5583428964215
parent47436aa4ad054c1c7c8231618e86ebd9305308dc (diff)
Virtio interface
This attempts to implement a "virtual I/O" layer which should allow common drivers to be efficiently used across most virtual I/O mechanisms. It will no-doubt need further enhancement. The virtio drivers add buffers to virtio queues; as the buffers are consumed the driver "interrupt" callbacks are invoked. There is also a generic implementation of config space which drivers can query to get setup information from the host. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Cc: Dor Laor <dor.laor@qumranet.com> Cc: Arnd Bergmann <arnd@arndb.de>
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/virtio/Kconfig3
-rw-r--r--drivers/virtio/Makefile1
-rw-r--r--drivers/virtio/config.c13
-rw-r--r--drivers/virtio/virtio.c171
-rw-r--r--include/linux/Kbuild1
-rw-r--r--include/linux/mod_devicetable.h6
-rw-r--r--include/linux/virtio.h110
-rw-r--r--include/linux/virtio_config.h111
10 files changed, 419 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig
index d945ffc57c25..f4076d9e9902 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -93,4 +93,6 @@ source "drivers/auxdisplay/Kconfig"
93source "drivers/kvm/Kconfig" 93source "drivers/kvm/Kconfig"
94 94
95source "drivers/uio/Kconfig" 95source "drivers/uio/Kconfig"
96
97source "drivers/virtio/Kconfig"
96endmenu 98endmenu
diff --git a/drivers/Makefile b/drivers/Makefile
index cfe38ffff28a..560496b43306 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -91,3 +91,4 @@ obj-$(CONFIG_HID) += hid/
91obj-$(CONFIG_PPC_PS3) += ps3/ 91obj-$(CONFIG_PPC_PS3) += ps3/
92obj-$(CONFIG_OF) += of/ 92obj-$(CONFIG_OF) += of/
93obj-$(CONFIG_SSB) += ssb/ 93obj-$(CONFIG_SSB) += ssb/
94obj-$(CONFIG_VIRTIO) += virtio/
diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig
new file mode 100644
index 000000000000..bce84b56a659
--- /dev/null
+++ b/drivers/virtio/Kconfig
@@ -0,0 +1,3 @@
1# Virtio always gets selected by whoever wants it.
2config VIRTIO
3 bool
diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile
new file mode 100644
index 000000000000..af0d57dad3eb
--- /dev/null
+++ b/drivers/virtio/Makefile
@@ -0,0 +1 @@
obj-$(CONFIG_VIRTIO) += virtio.o
diff --git a/drivers/virtio/config.c b/drivers/virtio/config.c
new file mode 100644
index 000000000000..983d482fba40
--- /dev/null
+++ b/drivers/virtio/config.c
@@ -0,0 +1,13 @@
1/* Configuration space parsing helpers for virtio.
2 *
3 * The configuration is [type][len][... len bytes ...] fields.
4 *
5 * Copyright 2007 Rusty Russell, IBM Corporation.
6 * GPL v2 or later.
7 */
8#include <linux/err.h>
9#include <linux/virtio.h>
10#include <linux/virtio_config.h>
11#include <linux/bug.h>
12#include <asm/system.h>
13
diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c
new file mode 100644
index 000000000000..f640e0b732b7
--- /dev/null
+++ b/drivers/virtio/virtio.c
@@ -0,0 +1,171 @@
1#include <linux/virtio.h>
2#include <linux/spinlock.h>
3#include <linux/virtio_config.h>
4
5static ssize_t device_show(struct device *_d,
6 struct device_attribute *attr, char *buf)
7{
8 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
9 return sprintf(buf, "%hu", dev->id.device);
10}
11static ssize_t vendor_show(struct device *_d,
12 struct device_attribute *attr, char *buf)
13{
14 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
15 return sprintf(buf, "%hu", dev->id.vendor);
16}
17static ssize_t status_show(struct device *_d,
18 struct device_attribute *attr, char *buf)
19{
20 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
21 return sprintf(buf, "0x%08x", dev->config->get_status(dev));
22}
23static struct device_attribute virtio_dev_attrs[] = {
24 __ATTR_RO(device),
25 __ATTR_RO(vendor),
26 __ATTR_RO(status),
27 __ATTR_NULL
28};
29
30static inline int virtio_id_match(const struct virtio_device *dev,
31 const struct virtio_device_id *id)
32{
33 if (id->device != dev->id.device)
34 return 0;
35
36 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor != dev->id.vendor;
37}
38
39/* This looks through all the IDs a driver claims to support. If any of them
40 * match, we return 1 and the kernel will call virtio_dev_probe(). */
41static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
42{
43 unsigned int i;
44 struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
45 const struct virtio_device_id *ids;
46
47 ids = container_of(_dr, struct virtio_driver, driver)->id_table;
48 for (i = 0; ids[i].device; i++)
49 if (virtio_id_match(dev, &ids[i]))
50 return 1;
51 return 0;
52}
53
54static struct bus_type virtio_bus = {
55 .name = "virtio",
56 .match = virtio_dev_match,
57 .dev_attrs = virtio_dev_attrs,
58};
59
60static void add_status(struct virtio_device *dev, unsigned status)
61{
62 dev->config->set_status(dev, dev->config->get_status(dev) | status);
63}
64
65static int virtio_dev_probe(struct device *_d)
66{
67 int err;
68 struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
69 struct virtio_driver *drv = container_of(dev->dev.driver,
70 struct virtio_driver, driver);
71
72 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
73 err = drv->probe(dev);
74 if (err)
75 add_status(dev, VIRTIO_CONFIG_S_FAILED);
76 else
77 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
78 return err;
79}
80
81int register_virtio_driver(struct virtio_driver *driver)
82{
83 driver->driver.bus = &virtio_bus;
84 driver->driver.probe = virtio_dev_probe;
85 return driver_register(&driver->driver);
86}
87EXPORT_SYMBOL_GPL(register_virtio_driver);
88
89void unregister_virtio_driver(struct virtio_driver *driver)
90{
91 driver_unregister(&driver->driver);
92}
93EXPORT_SYMBOL_GPL(unregister_virtio_driver);
94
95int register_virtio_device(struct virtio_device *dev)
96{
97 int err;
98
99 dev->dev.bus = &virtio_bus;
100 sprintf(dev->dev.bus_id, "%u", dev->index);
101
102 /* Acknowledge that we've seen the device. */
103 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
104
105 /* device_register() causes the bus infrastructure to look for a
106 * matching driver. */
107 err = device_register(&dev->dev);
108 if (err)
109 add_status(dev, VIRTIO_CONFIG_S_FAILED);
110 return err;
111}
112EXPORT_SYMBOL_GPL(register_virtio_device);
113
114void unregister_virtio_device(struct virtio_device *dev)
115{
116 device_unregister(&dev->dev);
117}
118EXPORT_SYMBOL_GPL(unregister_virtio_device);
119
120int __virtio_config_val(struct virtio_device *vdev,
121 u8 type, void *val, size_t size)
122{
123 void *token;
124 unsigned int len;
125
126 token = vdev->config->find(vdev, type, &len);
127 if (!token)
128 return -ENOENT;
129
130 if (len != size)
131 return -EIO;
132
133 vdev->config->get(vdev, token, val, size);
134 return 0;
135}
136EXPORT_SYMBOL_GPL(__virtio_config_val);
137
138int virtio_use_bit(struct virtio_device *vdev,
139 void *token, unsigned int len, unsigned int bitnum)
140{
141 unsigned long bits[16];
142
143 /* This makes it convenient to pass-through find() results. */
144 if (!token)
145 return 0;
146
147 /* bit not in range of this bitfield? */
148 if (bitnum * 8 >= len / 2)
149 return 0;
150
151 /* Giant feature bitfields are silly. */
152 BUG_ON(len > sizeof(bits));
153 vdev->config->get(vdev, token, bits, len);
154
155 if (!test_bit(bitnum, bits))
156 return 0;
157
158 /* Set acknowledge bit, and write it back. */
159 set_bit(bitnum + len * 8 / 2, bits);
160 vdev->config->set(vdev, token, bits, len);
161 return 1;
162}
163EXPORT_SYMBOL_GPL(virtio_use_bit);
164
165static int virtio_init(void)
166{
167 if (bus_register(&virtio_bus) != 0)
168 panic("virtio bus registration failed");
169 return 0;
170}
171core_initcall(virtio_init);
diff --git a/include/linux/Kbuild b/include/linux/Kbuild
index 758834538a19..e5208f283a67 100644
--- a/include/linux/Kbuild
+++ b/include/linux/Kbuild
@@ -343,6 +343,7 @@ unifdef-y += user.h
343unifdef-y += utsname.h 343unifdef-y += utsname.h
344unifdef-y += videodev2.h 344unifdef-y += videodev2.h
345unifdef-y += videodev.h 345unifdef-y += videodev.h
346unifdef-y += virtio_config.h
346unifdef-y += wait.h 347unifdef-y += wait.h
347unifdef-y += wanrouter.h 348unifdef-y += wanrouter.h
348unifdef-y += watchdog.h 349unifdef-y += watchdog.h
diff --git a/include/linux/mod_devicetable.h b/include/linux/mod_devicetable.h
index 522b0dd836cf..e9fddb42f26c 100644
--- a/include/linux/mod_devicetable.h
+++ b/include/linux/mod_devicetable.h
@@ -361,4 +361,10 @@ struct ssb_device_id {
361#define SSB_ANY_ID 0xFFFF 361#define SSB_ANY_ID 0xFFFF
362#define SSB_ANY_REV 0xFF 362#define SSB_ANY_REV 0xFF
363 363
364struct virtio_device_id {
365 __u32 device;
366 __u32 vendor;
367};
368#define VIRTIO_DEV_ANY_ID 0xffffffff
369
364#endif /* LINUX_MOD_DEVICETABLE_H */ 370#endif /* LINUX_MOD_DEVICETABLE_H */
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
new file mode 100644
index 000000000000..14e1379876d3
--- /dev/null
+++ b/include/linux/virtio.h
@@ -0,0 +1,110 @@
1#ifndef _LINUX_VIRTIO_H
2#define _LINUX_VIRTIO_H
3/* Everything a virtio driver needs to work with any particular virtio
4 * implementation. */
5#include <linux/types.h>
6#include <linux/scatterlist.h>
7#include <linux/spinlock.h>
8#include <linux/device.h>
9#include <linux/mod_devicetable.h>
10
11/**
12 * virtqueue - a queue to register buffers for sending or receiving.
13 * @callback: the function to call when buffers are consumed (can be NULL).
14 * If this returns false, callbacks are suppressed until vq_ops->restart
15 * is called.
16 * @vdev: the virtio device this queue was created for.
17 * @vq_ops: the operations for this virtqueue (see below).
18 * @priv: a pointer for the virtqueue implementation to use.
19 */
20struct virtqueue
21{
22 bool (*callback)(struct virtqueue *vq);
23 struct virtio_device *vdev;
24 struct virtqueue_ops *vq_ops;
25 void *priv;
26};
27
28/**
29 * virtqueue_ops - operations for virtqueue abstraction layer
30 * @add_buf: expose buffer to other end
31 * vq: the struct virtqueue we're talking about.
32 * sg: the description of the buffer(s).
33 * out_num: the number of sg readable by other side
34 * in_num: the number of sg which are writable (after readable ones)
35 * data: the token identifying the buffer.
36 * Returns 0 or an error.
37 * @kick: update after add_buf
38 * vq: the struct virtqueue
39 * After one or more add_buf calls, invoke this to kick the other side.
40 * @get_buf: get the next used buffer
41 * vq: the struct virtqueue we're talking about.
42 * len: the length written into the buffer
43 * Returns NULL or the "data" token handed to add_buf.
44 * @restart: restart callbacks after callback returned false.
45 * vq: the struct virtqueue we're talking about.
46 * This returns "false" (and doesn't re-enable) if there are pending
47 * buffers in the queue, to avoid a race.
48 * @shutdown: "unadd" all buffers.
49 * vq: the struct virtqueue we're talking about.
50 * Remove everything from the queue.
51 *
52 * Locking rules are straightforward: the driver is responsible for
53 * locking. No two operations may be invoked simultaneously.
54 *
55 * All operations can be called in any context.
56 */
57struct virtqueue_ops {
58 int (*add_buf)(struct virtqueue *vq,
59 struct scatterlist sg[],
60 unsigned int out_num,
61 unsigned int in_num,
62 void *data);
63
64 void (*kick)(struct virtqueue *vq);
65
66 void *(*get_buf)(struct virtqueue *vq, unsigned int *len);
67
68 bool (*restart)(struct virtqueue *vq);
69
70 void (*shutdown)(struct virtqueue *vq);
71};
72
73/**
74 * virtio_device - representation of a device using virtio
75 * @index: unique position on the virtio bus
76 * @dev: underlying device.
77 * @id: the device type identification (used to match it with a driver).
78 * @config: the configuration ops for this device.
79 * @priv: private pointer for the driver's use.
80 */
81struct virtio_device
82{
83 int index;
84 struct device dev;
85 struct virtio_device_id id;
86 struct virtio_config_ops *config;
87 void *priv;
88};
89
90int register_virtio_device(struct virtio_device *dev);
91void unregister_virtio_device(struct virtio_device *dev);
92
93/**
94 * virtio_driver - operations for a virtio I/O driver
95 * @driver: underlying device driver (populate name and owner).
96 * @id_table: the ids serviced by this driver.
97 * @probe: the function to call when a device is found. Returns a token for
98 * remove, or PTR_ERR().
99 * @remove: the function when a device is removed.
100 */
101struct virtio_driver {
102 struct device_driver driver;
103 const struct virtio_device_id *id_table;
104 int (*probe)(struct virtio_device *dev);
105 void (*remove)(struct virtio_device *dev);
106};
107
108int register_virtio_driver(struct virtio_driver *drv);
109void unregister_virtio_driver(struct virtio_driver *drv);
110#endif /* _LINUX_VIRTIO_H */
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
new file mode 100644
index 000000000000..bcc01888df78
--- /dev/null
+++ b/include/linux/virtio_config.h
@@ -0,0 +1,111 @@
1#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
8/* Status byte for guest to report progress, and synchronize config. */
9/* 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
18/* Feature byte (actually 7 bits availabe): */
19/* Requirements/features of the virtio implementation. */
20#define VIRTIO_CONFIG_F_VIRTIO 1
21/* Requirements/features of the virtqueue (may have more than one). */
22#define VIRTIO_CONFIG_F_VIRTQUEUE 2
23
24#ifdef __KERNEL__
25struct virtio_device;
26
27/**
28 * virtio_config_ops - operations for configuring a virtio device
29 * @find: search for the next configuration field of the given type.
30 * vdev: the virtio_device
31 * type: the feature type
32 * len: the (returned) length of the field if found.
33 * Returns a token if found, or NULL. Never returnes the same field twice
34 * (ie. it's used up).
35 * @get: read the value of a configuration field after find().
36 * vdev: the virtio_device
37 * token: the token returned from find().
38 * buf: the buffer to write the field value into.
39 * len: the length of the buffer (given by find()).
40 * Note that contents are conventionally little-endian.
41 * @set: write the value of a configuration field after find().
42 * vdev: the virtio_device
43 * token: the token returned from find().
44 * buf: the buffer to read the field value from.
45 * len: the length of the buffer (given by find()).
46 * Note that contents are conventionally little-endian.
47 * @get_status: read the status byte
48 * vdev: the virtio_device
49 * Returns the status byte
50 * @set_status: write the status byte
51 * vdev: the virtio_device
52 * status: the new status byte
53 * @find_vq: find the first VIRTIO_CONFIG_F_VIRTQUEUE and create a virtqueue.
54 * vdev: the virtio_device
55 * callback: the virqtueue callback
56 * Returns the new virtqueue or ERR_PTR().
57 * @del_vq: free a virtqueue found by find_vq().
58 */
59struct virtio_config_ops
60{
61 void *(*find)(struct virtio_device *vdev, u8 type, unsigned *len);
62 void (*get)(struct virtio_device *vdev, void *token,
63 void *buf, unsigned len);
64 void (*set)(struct virtio_device *vdev, void *token,
65 const void *buf, unsigned len);
66 u8 (*get_status)(struct virtio_device *vdev);
67 void (*set_status)(struct virtio_device *vdev, u8 status);
68 struct virtqueue *(*find_vq)(struct virtio_device *vdev,
69 bool (*callback)(struct virtqueue *));
70 void (*del_vq)(struct virtqueue *vq);
71};
72
73/**
74 * virtio_config_val - get a single virtio config and mark it used.
75 * @config: the virtio config space
76 * @type: the type to search for.
77 * @val: a pointer to the value to fill in.
78 *
79 * Once used, the config type is marked with VIRTIO_CONFIG_F_USED so it can't
80 * be found again. This version does endian conversion. */
81#define virtio_config_val(vdev, type, v) ({ \
82 int _err = __virtio_config_val((vdev),(type),(v),sizeof(*(v))); \
83 \
84 BUILD_BUG_ON(sizeof(*(v)) != 1 && sizeof(*(v)) != 2 \
85 && sizeof(*(v)) != 4 && sizeof(*(v)) != 8); \
86 if (!_err) { \
87 switch (sizeof(*(v))) { \
88 case 2: le16_to_cpus((__u16 *) v); break; \
89 case 4: le32_to_cpus((__u32 *) v); break; \
90 case 8: le64_to_cpus((__u64 *) v); break; \
91 } \
92 } \
93 _err; \
94})
95
96int __virtio_config_val(struct virtio_device *dev,
97 u8 type, void *val, size_t size);
98
99/**
100 * virtio_use_bit - helper to use a feature bit in a bitfield value.
101 * @dev: the virtio device
102 * @token: the token as returned from vdev->config->find().
103 * @len: the length of the field.
104 * @bitnum: the bit to test.
105 *
106 * If handed a NULL token, it returns false, otherwise returns bit status.
107 * If it's one, it sets the mirroring acknowledgement bit. */
108int virtio_use_bit(struct virtio_device *vdev,
109 void *token, unsigned int len, unsigned int bitnum);
110#endif /* __KERNEL__ */
111#endif /* _LINUX_VIRTIO_CONFIG_H */