diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2007-10-21 21:03:36 -0400 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2007-10-23 01:49:54 -0400 |
commit | ec3d41c4db4c21164332826ea8d812f94f2f6886 (patch) | |
tree | 9b947e900745cfcc4df7409c6ba5583428964215 /drivers | |
parent | 47436aa4ad054c1c7c8231618e86ebd9305308dc (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>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/Kconfig | 2 | ||||
-rw-r--r-- | drivers/Makefile | 1 | ||||
-rw-r--r-- | drivers/virtio/Kconfig | 3 | ||||
-rw-r--r-- | drivers/virtio/Makefile | 1 | ||||
-rw-r--r-- | drivers/virtio/config.c | 13 | ||||
-rw-r--r-- | drivers/virtio/virtio.c | 171 |
6 files changed, 191 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" | |||
93 | source "drivers/kvm/Kconfig" | 93 | source "drivers/kvm/Kconfig" |
94 | 94 | ||
95 | source "drivers/uio/Kconfig" | 95 | source "drivers/uio/Kconfig" |
96 | |||
97 | source "drivers/virtio/Kconfig" | ||
96 | endmenu | 98 | endmenu |
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/ | |||
91 | obj-$(CONFIG_PPC_PS3) += ps3/ | 91 | obj-$(CONFIG_PPC_PS3) += ps3/ |
92 | obj-$(CONFIG_OF) += of/ | 92 | obj-$(CONFIG_OF) += of/ |
93 | obj-$(CONFIG_SSB) += ssb/ | 93 | obj-$(CONFIG_SSB) += ssb/ |
94 | obj-$(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. | ||
2 | config 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 | |||
5 | static 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 | } | ||
11 | static 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 | } | ||
17 | static 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 | } | ||
23 | static struct device_attribute virtio_dev_attrs[] = { | ||
24 | __ATTR_RO(device), | ||
25 | __ATTR_RO(vendor), | ||
26 | __ATTR_RO(status), | ||
27 | __ATTR_NULL | ||
28 | }; | ||
29 | |||
30 | static 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(). */ | ||
41 | static 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 | |||
54 | static struct bus_type virtio_bus = { | ||
55 | .name = "virtio", | ||
56 | .match = virtio_dev_match, | ||
57 | .dev_attrs = virtio_dev_attrs, | ||
58 | }; | ||
59 | |||
60 | static void add_status(struct virtio_device *dev, unsigned status) | ||
61 | { | ||
62 | dev->config->set_status(dev, dev->config->get_status(dev) | status); | ||
63 | } | ||
64 | |||
65 | static 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 | |||
81 | int 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 | } | ||
87 | EXPORT_SYMBOL_GPL(register_virtio_driver); | ||
88 | |||
89 | void unregister_virtio_driver(struct virtio_driver *driver) | ||
90 | { | ||
91 | driver_unregister(&driver->driver); | ||
92 | } | ||
93 | EXPORT_SYMBOL_GPL(unregister_virtio_driver); | ||
94 | |||
95 | int 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 | } | ||
112 | EXPORT_SYMBOL_GPL(register_virtio_device); | ||
113 | |||
114 | void unregister_virtio_device(struct virtio_device *dev) | ||
115 | { | ||
116 | device_unregister(&dev->dev); | ||
117 | } | ||
118 | EXPORT_SYMBOL_GPL(unregister_virtio_device); | ||
119 | |||
120 | int __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 | } | ||
136 | EXPORT_SYMBOL_GPL(__virtio_config_val); | ||
137 | |||
138 | int 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 | } | ||
163 | EXPORT_SYMBOL_GPL(virtio_use_bit); | ||
164 | |||
165 | static int virtio_init(void) | ||
166 | { | ||
167 | if (bus_register(&virtio_bus) != 0) | ||
168 | panic("virtio bus registration failed"); | ||
169 | return 0; | ||
170 | } | ||
171 | core_initcall(virtio_init); | ||