blob: 5ce2aa48fc6e0943031f28c2a51d639e71b52f36 [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
Michael S. Tsirkin30683a82014-12-11 13:37:13 +0200165static int virtio_finalize_features(struct virtio_device *dev)
166{
167 int ret = dev->config->finalize_features(dev);
168 unsigned status;
169
170 if (ret)
171 return ret;
172
173 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
174 return 0;
175
176 add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
177 status = dev->config->get_status(dev);
178 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
179 dev_err(&dev->dev, "virtio: device refuses features: %x\n",
180 status);
181 return -ENODEV;
182 }
183 return 0;
184}
185
Rusty Russellec3d41c2007-10-22 11:03:36 +1000186static int virtio_dev_probe(struct device *_d)
187{
Rusty Russellc45a6812008-05-02 21:50:50 -0500188 int err, i;
Wanlong Gao9bffdca2012-12-11 11:04:50 +1030189 struct virtio_device *dev = dev_to_virtio(_d);
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800190 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200191 u64 device_features;
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300192 u64 driver_features;
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300193 u64 driver_features_legacy;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000194
Rusty Russellc45a6812008-05-02 21:50:50 -0500195 /* We have a driver! */
Rusty Russellec3d41c2007-10-22 11:03:36 +1000196 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
Rusty Russellc45a6812008-05-02 21:50:50 -0500197
198 /* Figure out what features the device supports. */
199 device_features = dev->config->get_features(dev);
200
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300201 /* Figure out what features the driver supports. */
202 driver_features = 0;
Rusty Russellc45a6812008-05-02 21:50:50 -0500203 for (i = 0; i < drv->feature_table_size; i++) {
204 unsigned int f = drv->feature_table[i];
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200205 BUG_ON(f >= 64);
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300206 driver_features |= (1ULL << f);
Rusty Russellc45a6812008-05-02 21:50:50 -0500207 }
208
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300209 /* Some drivers have a separate feature table for virtio v1.0 */
210 if (drv->feature_table_legacy) {
211 driver_features_legacy = 0;
212 for (i = 0; i < drv->feature_table_size_legacy; i++) {
213 unsigned int f = drv->feature_table_legacy[i];
214 BUG_ON(f >= 64);
215 driver_features_legacy |= (1ULL << f);
216 }
217 } else {
218 driver_features_legacy = driver_features;
219 }
220
Michael S. Tsirkin747ae342014-12-01 15:52:40 +0200221 if (device_features & (1ULL << VIRTIO_F_VERSION_1))
Michael S. Tsirkinb3bb62d2014-10-23 18:07:47 +0300222 dev->features = driver_features & device_features;
223 else
224 dev->features = driver_features_legacy & device_features;
Michael S. Tsirkinc1026592014-10-23 17:57:30 +0300225
Rusty Russellc6248962008-07-25 12:06:07 -0500226 /* Transport features always preserved to pass to finalize_features. */
Rusty Russelldd7c7bc2008-07-25 12:06:07 -0500227 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
Michael S. Tsirkind0254772014-10-07 16:39:43 +0200228 if (device_features & (1ULL << i))
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200229 __virtio_set_bit(dev, i);
Rusty Russelldd7c7bc2008-07-25 12:06:07 -0500230
Michael S. Tsirkin30683a82014-12-11 13:37:13 +0200231 err = virtio_finalize_features(dev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200232 if (err)
233 goto err;
Rusty Russellef688e12009-06-12 22:16:35 -0600234
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300235 err = drv->probe(dev);
236 if (err)
237 goto err;
238
Rusty Russell5b40a7d2015-02-17 16:12:44 +1030239 /* If probe didn't do it, mark device DRIVER_OK ourselves. */
240 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
241 virtio_device_ready(dev);
242
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300243 if (drv->scan)
244 drv->scan(dev);
245
246 virtio_config_enable(dev);
247
248 return 0;
249err:
250 add_status(dev, VIRTIO_CONFIG_S_FAILED);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000251 return err;
Michael S. Tsirkincb3f6d92014-10-22 17:41:38 +0300252
Rusty Russellec3d41c2007-10-22 11:03:36 +1000253}
254
Rusty Russell74b25532007-11-19 11:20:42 -0500255static int virtio_dev_remove(struct device *_d)
256{
Wanlong Gao9bffdca2012-12-11 11:04:50 +1030257 struct virtio_device *dev = dev_to_virtio(_d);
Wanlong Gao9a2bdcc2012-12-10 16:38:33 +0800258 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Rusty Russell74b25532007-11-19 11:20:42 -0500259
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030260 virtio_config_disable(dev);
261
Rusty Russell74b25532007-11-19 11:20:42 -0500262 drv->remove(dev);
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500263
264 /* Driver should have reset device. */
Michael S. Tsirkin5543a6a2012-09-28 15:05:16 +0930265 WARN_ON_ONCE(dev->config->get_status(dev));
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500266
267 /* Acknowledge the device's existence again. */
268 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
Rusty Russell74b25532007-11-19 11:20:42 -0500269 return 0;
270}
271
Mark McLoughline962fa662008-06-13 13:46:40 +0100272static struct bus_type virtio_bus = {
273 .name = "virtio",
274 .match = virtio_dev_match,
Greg Kroah-Hartman3736dab2013-10-07 18:27:39 -0700275 .dev_groups = virtio_dev_groups,
Mark McLoughline962fa662008-06-13 13:46:40 +0100276 .uevent = virtio_uevent,
277 .probe = virtio_dev_probe,
278 .remove = virtio_dev_remove,
279};
280
Michael S. Tsirkinb6098c32014-12-04 18:49:58 +0200281bool virtio_device_is_legacy_only(struct virtio_device_id id)
282{
283 return id.device == VIRTIO_ID_BALLOON;
284}
285EXPORT_SYMBOL_GPL(virtio_device_is_legacy_only);
286
Rusty Russellec3d41c2007-10-22 11:03:36 +1000287int register_virtio_driver(struct virtio_driver *driver)
288{
Rusty Russellc45a6812008-05-02 21:50:50 -0500289 /* Catch this early. */
290 BUG_ON(driver->feature_table_size && !driver->feature_table);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000291 driver->driver.bus = &virtio_bus;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000292 return driver_register(&driver->driver);
293}
294EXPORT_SYMBOL_GPL(register_virtio_driver);
295
296void unregister_virtio_driver(struct virtio_driver *driver)
297{
298 driver_unregister(&driver->driver);
299}
300EXPORT_SYMBOL_GPL(unregister_virtio_driver);
301
302int register_virtio_device(struct virtio_device *dev)
303{
304 int err;
305
306 dev->dev.bus = &virtio_bus;
Rusty Russellb769f572008-05-30 15:09:42 -0500307
308 /* Assign a unique device index and hence name. */
Asias He90e03202012-05-03 10:20:51 +0800309 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
310 if (err < 0)
311 goto out;
312
313 dev->index = err;
Kay Sievers99e0b6c2008-12-30 09:25:56 -0600314 dev_set_name(&dev->dev, "virtio%u", dev->index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000315
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030316 spin_lock_init(&dev->config_lock);
317 dev->config_enabled = false;
318 dev->config_change_pending = false;
319
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500320 /* We always start by resetting the device, in case a previous
321 * driver messed it up. This also tests that code path a little. */
322 dev->config->reset(dev);
323
Rusty Russellec3d41c2007-10-22 11:03:36 +1000324 /* Acknowledge that we've seen the device. */
325 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
326
Rusty Russell9499f5e2009-06-12 22:16:35 -0600327 INIT_LIST_HEAD(&dev->vqs);
328
Rusty Russellec3d41c2007-10-22 11:03:36 +1000329 /* device_register() causes the bus infrastructure to look for a
330 * matching driver. */
331 err = device_register(&dev->dev);
Asias He90e03202012-05-03 10:20:51 +0800332out:
Rusty Russellec3d41c2007-10-22 11:03:36 +1000333 if (err)
334 add_status(dev, VIRTIO_CONFIG_S_FAILED);
335 return err;
336}
337EXPORT_SYMBOL_GPL(register_virtio_device);
338
339void unregister_virtio_device(struct virtio_device *dev)
340{
Cornelia Huck237242b2012-11-09 14:54:12 +1030341 int index = dev->index; /* save for after device release */
342
Rusty Russellec3d41c2007-10-22 11:03:36 +1000343 device_unregister(&dev->dev);
Cornelia Huck237242b2012-11-09 14:54:12 +1030344 ida_simple_remove(&virtio_index_ida, index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000345}
346EXPORT_SYMBOL_GPL(unregister_virtio_device);
347
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030348#ifdef CONFIG_PM_SLEEP
349int virtio_device_freeze(struct virtio_device *dev)
350{
351 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
352
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030353 virtio_config_disable(dev);
354
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030355 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
356
357 if (drv && drv->freeze)
358 return drv->freeze(dev);
359
360 return 0;
361}
362EXPORT_SYMBOL_GPL(virtio_device_freeze);
363
364int virtio_device_restore(struct virtio_device *dev)
365{
366 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200367 int ret;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030368
369 /* We always start by resetting the device, in case a previous
370 * driver messed it up. */
371 dev->config->reset(dev);
372
373 /* Acknowledge that we've seen the device. */
374 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
375
376 /* Maybe driver failed before freeze.
377 * Restore the failed status, for debugging. */
378 if (dev->failed)
379 add_status(dev, VIRTIO_CONFIG_S_FAILED);
380
381 if (!drv)
382 return 0;
383
384 /* We have a driver! */
385 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
386
Michael S. Tsirkin30683a82014-12-11 13:37:13 +0200387 ret = virtio_finalize_features(dev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200388 if (ret)
389 goto err;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030390
391 if (drv->restore) {
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200392 ret = drv->restore(dev);
393 if (ret)
394 goto err;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030395 }
396
397 /* Finally, tell the device we're all set */
398 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
399
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030400 virtio_config_enable(dev);
401
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030402 return 0;
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200403
404err:
405 add_status(dev, VIRTIO_CONFIG_S_FAILED);
406 return ret;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030407}
408EXPORT_SYMBOL_GPL(virtio_device_restore);
409#endif
410
Rusty Russellec3d41c2007-10-22 11:03:36 +1000411static int virtio_init(void)
412{
413 if (bus_register(&virtio_bus) != 0)
414 panic("virtio bus registration failed");
415 return 0;
416}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500417
418static void __exit virtio_exit(void)
419{
420 bus_unregister(&virtio_bus);
421}
Rusty Russellec3d41c2007-10-22 11:03:36 +1000422core_initcall(virtio_init);
Rusty Russellc6fd4702008-02-04 23:50:05 -0500423module_exit(virtio_exit);
424
425MODULE_LICENSE("GPL");