Merge tag 'arm64-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/cmarinas...
[deliverable/linux.git] / drivers / virtio / virtio.c
CommitLineData
ec3d41c4
RR
1#include <linux/virtio.h>
2#include <linux/spinlock.h>
3#include <linux/virtio_config.h>
b5a2c4f1 4#include <linux/module.h>
90e03207 5#include <linux/idr.h>
ec3d41c4 6
b769f579 7/* Unique numbering for virtio devices. */
90e03207 8static DEFINE_IDA(virtio_index_ida);
b769f579 9
ec3d41c4
RR
10static ssize_t device_show(struct device *_d,
11 struct device_attribute *attr, char *buf)
12{
9bffdca8 13 struct virtio_device *dev = dev_to_virtio(_d);
be6528b2 14 return sprintf(buf, "0x%04x\n", dev->id.device);
ec3d41c4 15}
3736dab6
GKH
16static DEVICE_ATTR_RO(device);
17
ec3d41c4
RR
18static ssize_t vendor_show(struct device *_d,
19 struct device_attribute *attr, char *buf)
20{
9bffdca8 21 struct virtio_device *dev = dev_to_virtio(_d);
be6528b2 22 return sprintf(buf, "0x%04x\n", dev->id.vendor);
ec3d41c4 23}
3736dab6
GKH
24static DEVICE_ATTR_RO(vendor);
25
ec3d41c4
RR
26static ssize_t status_show(struct device *_d,
27 struct device_attribute *attr, char *buf)
28{
9bffdca8 29 struct virtio_device *dev = dev_to_virtio(_d);
be6528b2 30 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
ec3d41c4 31}
3736dab6
GKH
32static DEVICE_ATTR_RO(status);
33
b01d9f28
RR
34static ssize_t modalias_show(struct device *_d,
35 struct device_attribute *attr, char *buf)
36{
9bffdca8 37 struct virtio_device *dev = dev_to_virtio(_d);
b01d9f28
RR
38 return sprintf(buf, "virtio:d%08Xv%08X\n",
39 dev->id.device, dev->id.vendor);
40}
3736dab6
GKH
41static DEVICE_ATTR_RO(modalias);
42
a9289282
RR
43static ssize_t features_show(struct device *_d,
44 struct device_attribute *attr, char *buf)
45{
9bffdca8 46 struct virtio_device *dev = dev_to_virtio(_d);
a9289282
RR
47 unsigned int i;
48 ssize_t len = 0;
49
50 /* We actually represent this as a bitstring, as it could be
51 * arbitrary length in future. */
52 for (i = 0; i < ARRAY_SIZE(dev->features)*BITS_PER_LONG; i++)
53 len += sprintf(buf+len, "%c",
54 test_bit(i, dev->features) ? '1' : '0');
55 len += sprintf(buf+len, "\n");
56 return len;
57}
3736dab6
GKH
58static DEVICE_ATTR_RO(features);
59
60static struct attribute *virtio_dev_attrs[] = {
61 &dev_attr_device.attr,
62 &dev_attr_vendor.attr,
63 &dev_attr_status.attr,
64 &dev_attr_modalias.attr,
65 &dev_attr_features.attr,
66 NULL,
ec3d41c4 67};
3736dab6 68ATTRIBUTE_GROUPS(virtio_dev);
ec3d41c4
RR
69
70static inline int virtio_id_match(const struct virtio_device *dev,
71 const struct virtio_device_id *id)
72{
e3353853 73 if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
ec3d41c4
RR
74 return 0;
75
c89e8016 76 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
ec3d41c4
RR
77}
78
79/* This looks through all the IDs a driver claims to support. If any of them
80 * match, we return 1 and the kernel will call virtio_dev_probe(). */
81static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
82{
83 unsigned int i;
9bffdca8 84 struct virtio_device *dev = dev_to_virtio(_dv);
ec3d41c4
RR
85 const struct virtio_device_id *ids;
86
9a2bdcc8 87 ids = drv_to_virtio(_dr)->id_table;
ec3d41c4
RR
88 for (i = 0; ids[i].device; i++)
89 if (virtio_id_match(dev, &ids[i]))
90 return 1;
91 return 0;
92}
93
b01d9f28
RR
94static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
95{
9bffdca8 96 struct virtio_device *dev = dev_to_virtio(_dv);
b01d9f28
RR
97
98 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
99 dev->id.device, dev->id.vendor);
100}
101
ec3d41c4
RR
102static void add_status(struct virtio_device *dev, unsigned status)
103{
104 dev->config->set_status(dev, dev->config->get_status(dev) | status);
105}
106
c45a6816
RR
107void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
108 unsigned int fbit)
109{
110 unsigned int i;
9a2bdcc8 111 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
c45a6816
RR
112
113 for (i = 0; i < drv->feature_table_size; i++)
114 if (drv->feature_table[i] == fbit)
115 return;
116 BUG();
117}
118EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
119
ec3d41c4
RR
120static int virtio_dev_probe(struct device *_d)
121{
c45a6816 122 int err, i;
9bffdca8 123 struct virtio_device *dev = dev_to_virtio(_d);
9a2bdcc8 124 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
c45a6816 125 u32 device_features;
ec3d41c4 126
c45a6816 127 /* We have a driver! */
ec3d41c4 128 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
c45a6816
RR
129
130 /* Figure out what features the device supports. */
131 device_features = dev->config->get_features(dev);
132
133 /* Features supported by both device and driver into dev->features. */
134 memset(dev->features, 0, sizeof(dev->features));
135 for (i = 0; i < drv->feature_table_size; i++) {
136 unsigned int f = drv->feature_table[i];
137 BUG_ON(f >= 32);
138 if (device_features & (1 << f))
139 set_bit(f, dev->features);
140 }
141
c624896e 142 /* Transport features always preserved to pass to finalize_features. */
dd7c7bc4
RR
143 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
144 if (device_features & (1 << i))
145 set_bit(i, dev->features);
146
ef688e15
RR
147 dev->config->finalize_features(dev);
148
ec3d41c4
RR
149 err = drv->probe(dev);
150 if (err)
151 add_status(dev, VIRTIO_CONFIG_S_FAILED);
59057fbc 152 else {
b92dea67 153 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
59057fbc
NB
154 if (drv->scan)
155 drv->scan(dev);
156 }
ef688e15 157
ec3d41c4
RR
158 return err;
159}
160
74b2553f
RR
161static int virtio_dev_remove(struct device *_d)
162{
9bffdca8 163 struct virtio_device *dev = dev_to_virtio(_d);
9a2bdcc8 164 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
74b2553f 165
74b2553f 166 drv->remove(dev);
6e5aa7ef
RR
167
168 /* Driver should have reset device. */
5543a6ac 169 WARN_ON_ONCE(dev->config->get_status(dev));
6e5aa7ef
RR
170
171 /* Acknowledge the device's existence again. */
172 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
74b2553f
RR
173 return 0;
174}
175
e962fa66
MM
176static struct bus_type virtio_bus = {
177 .name = "virtio",
178 .match = virtio_dev_match,
3736dab6 179 .dev_groups = virtio_dev_groups,
e962fa66
MM
180 .uevent = virtio_uevent,
181 .probe = virtio_dev_probe,
182 .remove = virtio_dev_remove,
183};
184
ec3d41c4
RR
185int register_virtio_driver(struct virtio_driver *driver)
186{
c45a6816
RR
187 /* Catch this early. */
188 BUG_ON(driver->feature_table_size && !driver->feature_table);
ec3d41c4 189 driver->driver.bus = &virtio_bus;
ec3d41c4
RR
190 return driver_register(&driver->driver);
191}
192EXPORT_SYMBOL_GPL(register_virtio_driver);
193
194void unregister_virtio_driver(struct virtio_driver *driver)
195{
196 driver_unregister(&driver->driver);
197}
198EXPORT_SYMBOL_GPL(unregister_virtio_driver);
199
200int register_virtio_device(struct virtio_device *dev)
201{
202 int err;
203
204 dev->dev.bus = &virtio_bus;
b769f579
RR
205
206 /* Assign a unique device index and hence name. */
90e03207
AH
207 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
208 if (err < 0)
209 goto out;
210
211 dev->index = err;
99e0b6c8 212 dev_set_name(&dev->dev, "virtio%u", dev->index);
ec3d41c4 213
6e5aa7ef
RR
214 /* We always start by resetting the device, in case a previous
215 * driver messed it up. This also tests that code path a little. */
216 dev->config->reset(dev);
217
ec3d41c4
RR
218 /* Acknowledge that we've seen the device. */
219 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
220
9499f5e7
RR
221 INIT_LIST_HEAD(&dev->vqs);
222
ec3d41c4
RR
223 /* device_register() causes the bus infrastructure to look for a
224 * matching driver. */
225 err = device_register(&dev->dev);
90e03207 226out:
ec3d41c4
RR
227 if (err)
228 add_status(dev, VIRTIO_CONFIG_S_FAILED);
229 return err;
230}
231EXPORT_SYMBOL_GPL(register_virtio_device);
232
233void unregister_virtio_device(struct virtio_device *dev)
234{
237242bd
CH
235 int index = dev->index; /* save for after device release */
236
ec3d41c4 237 device_unregister(&dev->dev);
237242bd 238 ida_simple_remove(&virtio_index_ida, index);
ec3d41c4
RR
239}
240EXPORT_SYMBOL_GPL(unregister_virtio_device);
241
ec3d41c4
RR
242static int virtio_init(void)
243{
244 if (bus_register(&virtio_bus) != 0)
245 panic("virtio bus registration failed");
246 return 0;
247}
c6fd4701
RR
248
249static void __exit virtio_exit(void)
250{
251 bus_unregister(&virtio_bus);
252}
ec3d41c4 253core_initcall(virtio_init);
c6fd4701
RR
254module_exit(virtio_exit);
255
256MODULE_LICENSE("GPL");
This page took 0.699953 seconds and 5 git commands to generate.