blob: 224f85442f3f2c3b9f50439040148bdfe8fcae4c [file] [log] [blame]
Rusty Russellec3d41c2007-10-22 11:03:36 +10001#include <linux/virtio.h>
2#include <linux/spinlock.h>
3#include <linux/virtio_config.h>
Paul Gortmakerb5a2c4f2011-07-03 16:20:30 -04004#include <linux/module.h>
Asias He90e03202012-05-03 10:20:51 +08005#include <linux/idr.h>
Michael S. Tsirkinb6098c32014-12-04 18:49:58 +02006#include <uapi/linux/virtio_ids.h>
Rusty Russellec3d41c2007-10-22 11:03:36 +10007
Rusty Russellb769f572008-05-30 15:09:42 -05008/* Unique numbering for virtio devices. */
Asias He90e03202012-05-03 10:20:51 +08009static DEFINE_IDA(virtio_index_ida);
Rusty Russellb769f572008-05-30 15:09:42 -050010
Rusty Russellec3d41c2007-10-22 11:03:36 +100011static ssize_t device_show(struct device *_d,
12 struct device_attribute *attr, char *buf)
13{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103014 struct virtio_device *dev = dev_to_virtio(_d);
Stephen Hemmingerbe6528b2010-11-09 22:20:29 -080015 return sprintf(buf, "0x%04x\n", dev->id.device);
Rusty Russellec3d41c2007-10-22 11:03:36 +100016}
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070017static DEVICE_ATTR_RO(device);
18
Rusty Russellec3d41c2007-10-22 11:03:36 +100019static ssize_t vendor_show(struct device *_d,
20 struct device_attribute *attr, char *buf)
21{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103022 struct virtio_device *dev = dev_to_virtio(_d);
Stephen Hemmingerbe6528b2010-11-09 22:20:29 -080023 return sprintf(buf, "0x%04x\n", dev->id.vendor);
Rusty Russellec3d41c2007-10-22 11:03:36 +100024}
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070025static DEVICE_ATTR_RO(vendor);
26
Rusty Russellec3d41c2007-10-22 11:03:36 +100027static ssize_t status_show(struct device *_d,
28 struct device_attribute *attr, char *buf)
29{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103030 struct virtio_device *dev = dev_to_virtio(_d);
Stephen Hemmingerbe6528b2010-11-09 22:20:29 -080031 return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
Rusty Russellec3d41c2007-10-22 11:03:36 +100032}
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070033static DEVICE_ATTR_RO(status);
34
Rusty Russellb01d9f22007-10-22 11:03:39 +100035static ssize_t modalias_show(struct device *_d,
36 struct device_attribute *attr, char *buf)
37{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103038 struct virtio_device *dev = dev_to_virtio(_d);
Rusty Russellb01d9f22007-10-22 11:03:39 +100039 return sprintf(buf, "virtio:d%08Xv%08X\n",
40 dev->id.device, dev->id.vendor);
41}
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070042static DEVICE_ATTR_RO(modalias);
43
Rusty Russella9289282009-06-12 22:16:37 -060044static ssize_t features_show(struct device *_d,
45 struct device_attribute *attr, char *buf)
46{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103047 struct virtio_device *dev = dev_to_virtio(_d);
Rusty Russella9289282009-06-12 22:16:37 -060048 unsigned int i;
49 ssize_t len = 0;
50
51 /* We actually represent this as a bitstring, as it could be
52 * arbitrary length in future. */
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020053 for (i = 0; i < sizeof(dev->features)*8; i++)
Rusty Russella9289282009-06-12 22:16:37 -060054 len += sprintf(buf+len, "%c",
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +020055 __virtio_test_bit(dev, i) ? '1' : '0');
Rusty Russella9289282009-06-12 22:16:37 -060056 len += sprintf(buf+len, "\n");
57 return len;
58}
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070059static DEVICE_ATTR_RO(features);
60
61static struct attribute *virtio_dev_attrs[] = {
62 &dev_attr_device.attr,
63 &dev_attr_vendor.attr,
64 &dev_attr_status.attr,
65 &dev_attr_modalias.attr,
66 &dev_attr_features.attr,
67 NULL,
Rusty Russellec3d41c2007-10-22 11:03:36 +100068};
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -070069ATTRIBUTE_GROUPS(virtio_dev);
Rusty Russellec3d41c2007-10-22 11:03:36 +100070
71static inline int virtio_id_match(const struct virtio_device *dev,
72 const struct virtio_device_id *id)
73{
Christian Borntraegere3353852009-05-26 15:46:10 +020074 if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
Rusty Russellec3d41c2007-10-22 11:03:36 +100075 return 0;
76
Christian Borntraegerc89e8012009-05-26 15:46:09 +020077 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
Rusty Russellec3d41c2007-10-22 11:03:36 +100078}
79
80/* This looks through all the IDs a driver claims to support. If any of them
81 * match, we return 1 and the kernel will call virtio_dev_probe(). */
82static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
83{
84 unsigned int i;
Wanlong Gao9bffdca2012-12-11 11:04:50 +103085 struct virtio_device *dev = dev_to_virtio(_dv);
Rusty Russellec3d41c2007-10-22 11:03:36 +100086 const struct virtio_device_id *ids;
87
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +080088 ids = drv_to_virtio(_dr)->id_table;
Rusty Russellec3d41c2007-10-22 11:03:36 +100089 for (i = 0; ids[i].device; i++)
90 if (virtio_id_match(dev, &ids[i]))
91 return 1;
92 return 0;
93}
94
Rusty Russellb01d9f22007-10-22 11:03:39 +100095static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
96{
Wanlong Gao9bffdca2012-12-11 11:04:50 +103097 struct virtio_device *dev = dev_to_virtio(_dv);
Rusty Russellb01d9f22007-10-22 11:03:39 +100098
99 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
100 dev->id.device, dev->id.vendor);
101}
102
Rusty Russellec3d41c2007-10-22 11:03:36 +1000103static void add_status(struct virtio_device *dev, unsigned status)
104{
105 dev->config->set_status(dev, dev->config->get_status(dev) | status);
106}
107
Rusty Russellc45a6812008-05-02 21:50:50 -0500108void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
109 unsigned int fbit)
110{
111 unsigned int i;
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800112 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
Rusty Russellc45a6812008-05-02 21:50:50 -0500113
114 for (i = 0; i < drv->feature_table_size; i++)
115 if (drv->feature_table[i] == fbit)
116 return;
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300117
118 if (drv->feature_table_legacy) {
119 for (i = 0; i < drv->feature_table_size_legacy; i++)
120 if (drv->feature_table_legacy[i] == fbit)
121 return;
122 }
123
Rusty Russellc45a6812008-05-02 21:50:50 -0500124 BUG();
125}
126EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
127
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030128static void __virtio_config_changed(struct virtio_device *dev)
129{
130 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
131
132 if (!dev->config_enabled)
133 dev->config_change_pending = true;
134 else if (drv && drv->config_changed)
135 drv->config_changed(dev);
136}
137
138void virtio_config_changed(struct virtio_device *dev)
139{
140 unsigned long flags;
141
142 spin_lock_irqsave(&dev->config_lock, flags);
143 __virtio_config_changed(dev);
144 spin_unlock_irqrestore(&dev->config_lock, flags);
145}
146EXPORT_SYMBOL_GPL(virtio_config_changed);
147
148static void virtio_config_disable(struct virtio_device *dev)
149{
150 spin_lock_irq(&dev->config_lock);
151 dev->config_enabled = false;
152 spin_unlock_irq(&dev->config_lock);
153}
154
155static void virtio_config_enable(struct virtio_device *dev)
156{
157 spin_lock_irq(&dev->config_lock);
158 dev->config_enabled = true;
159 if (dev->config_change_pending)
160 __virtio_config_changed(dev);
161 dev->config_change_pending = false;
162 spin_unlock_irq(&dev->config_lock);
163}
164
Rusty Russellec3d41c2007-10-22 11:03:36 +1000165static int virtio_dev_probe(struct device *_d)
166{
Rusty Russellc45a6812008-05-02 21:50:50 -0500167 int err, i;
Wanlong Gao9bffdca2012-12-11 11:04:50 +1030168 struct virtio_device *dev = dev_to_virtio(_d);
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800169 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200170 u64 device_features;
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300171 u64 driver_features;
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300172 u64 driver_features_legacy;
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300173 unsigned status;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000174
Rusty Russellc45a6812008-05-02 21:50:50 -0500175 /* We have a driver! */
Rusty Russellec3d41c2007-10-22 11:03:36 +1000176 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
Rusty Russellc45a6812008-05-02 21:50:50 -0500177
178 /* Figure out what features the device supports. */
179 device_features = dev->config->get_features(dev);
180
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300181 /* Figure out what features the driver supports. */
182 driver_features = 0;
Rusty Russellc45a6812008-05-02 21:50:50 -0500183 for (i = 0; i < drv->feature_table_size; i++) {
184 unsigned int f = drv->feature_table[i];
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200185 BUG_ON(f >= 64);
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300186 driver_features |= (1ULL << f);
Rusty Russellc45a6812008-05-02 21:50:50 -0500187 }
188
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300189 /* Some drivers have a separate feature table for virtio v1.0 */
190 if (drv->feature_table_legacy) {
191 driver_features_legacy = 0;
192 for (i = 0; i < drv->feature_table_size_legacy; i++) {
193 unsigned int f = drv->feature_table_legacy[i];
194 BUG_ON(f >= 64);
195 driver_features_legacy |= (1ULL << f);
196 }
197 } else {
198 driver_features_legacy = driver_features;
199 }
200
Michael S. Tsirkin747ae342014-12-01 15:52:40 +0200201 /* Detect legacy-only drivers and disable VIRTIO_F_VERSION_1. */
202 if (drv->legacy_only)
203 device_features &= ~(1ULL << VIRTIO_F_VERSION_1);
204
205 if (device_features & (1ULL << VIRTIO_F_VERSION_1))
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300206 dev->features = driver_features & device_features;
207 else
208 dev->features = driver_features_legacy & device_features;
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300209
Rusty Russellc6248962008-07-25 12:06:07 -0500210 /* Transport features always preserved to pass to finalize_features. */
Rusty Russelldd7c7bc2008-07-25 12:06:07 -0500211 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200212 if (device_features & (1ULL << i))
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200213 __virtio_set_bit(dev, i);
Rusty Russelldd7c7bc2008-07-25 12:06:07 -0500214
Rusty Russellef688e12009-06-12 22:16:35 -0600215 dev->config->finalize_features(dev);
216
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300217 if (virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
218 add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
219 status = dev->config->get_status(dev);
220 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
221 dev_err(_d, "virtio: device refuses features: %x\n",
222 status);
223 err = -ENODEV;
224 goto err;
225 }
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000226 }
Rusty Russellef688e12009-06-12 22:16:35 -0600227
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300228 err = drv->probe(dev);
229 if (err)
230 goto err;
231
232 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
233 if (drv->scan)
234 drv->scan(dev);
235
236 virtio_config_enable(dev);
237
238 return 0;
239err:
240 add_status(dev, VIRTIO_CONFIG_S_FAILED);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000241 return err;
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300242
Rusty Russellec3d41c2007-10-22 11:03:36 +1000243}
244
Rusty Russell74b25532007-11-19 11:20:42 -0500245static int virtio_dev_remove(struct device *_d)
246{
Wanlong Gao9bffdca2012-12-11 11:04:50 +1030247 struct virtio_device *dev = dev_to_virtio(_d);
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800248 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Rusty Russell74b25532007-11-19 11:20:42 -0500249
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030250 virtio_config_disable(dev);
251
Rusty Russell74b25532007-11-19 11:20:42 -0500252 drv->remove(dev);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500253
254 /* Driver should have reset device. */
Michael S. Tsirkin5543a6a2012-09-28 15:05:16 +0930255 WARN_ON_ONCE(dev->config->get_status(dev));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500256
257 /* Acknowledge the device's existence again. */
258 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
Rusty Russell74b25532007-11-19 11:20:42 -0500259 return 0;
260}
261
Mark McLoughline962fa662008-06-13 13:46:40 +0100262static struct bus_type virtio_bus = {
263 .name = "virtio",
264 .match = virtio_dev_match,
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -0700265 .dev_groups = virtio_dev_groups,
Mark McLoughline962fa662008-06-13 13:46:40 +0100266 .uevent = virtio_uevent,
267 .probe = virtio_dev_probe,
268 .remove = virtio_dev_remove,
269};
270
Michael S. Tsirkinb6098c32014-12-04 18:49:58 +0200271bool virtio_device_is_legacy_only(struct virtio_device_id id)
272{
273 return id.device == VIRTIO_ID_BALLOON;
274}
275EXPORT_SYMBOL_GPL(virtio_device_is_legacy_only);
276
Rusty Russellec3d41c2007-10-22 11:03:36 +1000277int register_virtio_driver(struct virtio_driver *driver)
278{
Rusty Russellc45a6812008-05-02 21:50:50 -0500279 /* Catch this early. */
280 BUG_ON(driver->feature_table_size && !driver->feature_table);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000281 driver->driver.bus = &virtio_bus;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000282 return driver_register(&driver->driver);
283}
284EXPORT_SYMBOL_GPL(register_virtio_driver);
285
286void unregister_virtio_driver(struct virtio_driver *driver)
287{
288 driver_unregister(&driver->driver);
289}
290EXPORT_SYMBOL_GPL(unregister_virtio_driver);
291
292int register_virtio_device(struct virtio_device *dev)
293{
294 int err;
295
296 dev->dev.bus = &virtio_bus;
Rusty Russellb769f572008-05-30 15:09:42 -0500297
298 /* Assign a unique device index and hence name. */
Asias He90e03202012-05-03 10:20:51 +0800299 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
300 if (err < 0)
301 goto out;
302
303 dev->index = err;
Kay Sievers99e0b6c2008-12-30 09:25:56 -0600304 dev_set_name(&dev->dev, "virtio%u", dev->index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000305
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030306 spin_lock_init(&dev->config_lock);
307 dev->config_enabled = false;
308 dev->config_change_pending = false;
309
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500310 /* We always start by resetting the device, in case a previous
311 * driver messed it up. This also tests that code path a little. */
312 dev->config->reset(dev);
313
Rusty Russellec3d41c2007-10-22 11:03:36 +1000314 /* Acknowledge that we've seen the device. */
315 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
316
Rusty Russell9499f5e2009-06-12 22:16:35 -0600317 INIT_LIST_HEAD(&dev->vqs);
318
Rusty Russellec3d41c2007-10-22 11:03:36 +1000319 /* device_register() causes the bus infrastructure to look for a
320 * matching driver. */
321 err = device_register(&dev->dev);
Asias He90e03202012-05-03 10:20:51 +0800322out:
Rusty Russellec3d41c2007-10-22 11:03:36 +1000323 if (err)
324 add_status(dev, VIRTIO_CONFIG_S_FAILED);
325 return err;
326}
327EXPORT_SYMBOL_GPL(register_virtio_device);
328
329void unregister_virtio_device(struct virtio_device *dev)
330{
Cornelia Huck237242b2012-11-09 14:54:12 +1030331 int index = dev->index; /* save for after device release */
332
Rusty Russellec3d41c2007-10-22 11:03:36 +1000333 device_unregister(&dev->dev);
Cornelia Huck237242b2012-11-09 14:54:12 +1030334 ida_simple_remove(&virtio_index_ida, index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000335}
336EXPORT_SYMBOL_GPL(unregister_virtio_device);
337
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030338#ifdef CONFIG_PM_SLEEP
339int virtio_device_freeze(struct virtio_device *dev)
340{
341 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
342
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030343 virtio_config_disable(dev);
344
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030345 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
346
347 if (drv && drv->freeze)
348 return drv->freeze(dev);
349
350 return 0;
351}
352EXPORT_SYMBOL_GPL(virtio_device_freeze);
353
354int virtio_device_restore(struct virtio_device *dev)
355{
356 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
357
358 /* We always start by resetting the device, in case a previous
359 * driver messed it up. */
360 dev->config->reset(dev);
361
362 /* Acknowledge that we've seen the device. */
363 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
364
365 /* Maybe driver failed before freeze.
366 * Restore the failed status, for debugging. */
367 if (dev->failed)
368 add_status(dev, VIRTIO_CONFIG_S_FAILED);
369
370 if (!drv)
371 return 0;
372
373 /* We have a driver! */
374 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
375
376 dev->config->finalize_features(dev);
377
378 if (drv->restore) {
379 int ret = drv->restore(dev);
380 if (ret) {
381 add_status(dev, VIRTIO_CONFIG_S_FAILED);
382 return ret;
383 }
384 }
385
386 /* Finally, tell the device we're all set */
387 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
388
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030389 virtio_config_enable(dev);
390
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030391 return 0;
392}
393EXPORT_SYMBOL_GPL(virtio_device_restore);
394#endif
395
Rusty Russellec3d41c2007-10-22 11:03:36 +1000396static int virtio_init(void)
397{
398 if (bus_register(&virtio_bus) != 0)
399 panic("virtio bus registration failed");
400 return 0;
401}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500402
403static void __exit virtio_exit(void)
404{
405 bus_unregister(&virtio_bus);
406}
Rusty Russellec3d41c2007-10-22 11:03:36 +1000407core_initcall(virtio_init);
Rusty Russellc6fd4702008-02-04 23:50:05 -0500408module_exit(virtio_exit);
409
410MODULE_LICENSE("GPL");