blob: b1877d73fa563d6d48f2d55b7958f02daa8d9ad7 [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
Rusty Russellec3d41c2007-10-22 11:03:36 +1000281int register_virtio_driver(struct virtio_driver *driver)
282{
Rusty Russellc45a6812008-05-02 21:50:50 -0500283 /* Catch this early. */
284 BUG_ON(driver->feature_table_size && !driver->feature_table);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000285 driver->driver.bus = &virtio_bus;
Rusty Russellec3d41c2007-10-22 11:03:36 +1000286 return driver_register(&driver->driver);
287}
288EXPORT_SYMBOL_GPL(register_virtio_driver);
289
290void unregister_virtio_driver(struct virtio_driver *driver)
291{
292 driver_unregister(&driver->driver);
293}
294EXPORT_SYMBOL_GPL(unregister_virtio_driver);
295
296int register_virtio_device(struct virtio_device *dev)
297{
298 int err;
299
300 dev->dev.bus = &virtio_bus;
Rusty Russellb769f572008-05-30 15:09:42 -0500301
302 /* Assign a unique device index and hence name. */
Asias He90e03202012-05-03 10:20:51 +0800303 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
304 if (err < 0)
305 goto out;
306
307 dev->index = err;
Kay Sievers99e0b6c2008-12-30 09:25:56 -0600308 dev_set_name(&dev->dev, "virtio%u", dev->index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000309
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030310 spin_lock_init(&dev->config_lock);
311 dev->config_enabled = false;
312 dev->config_change_pending = false;
313
Rusty Russell6e5aa7e2008-02-04 23:50:03 -0500314 /* We always start by resetting the device, in case a previous
315 * driver messed it up. This also tests that code path a little. */
316 dev->config->reset(dev);
317
Rusty Russellec3d41c2007-10-22 11:03:36 +1000318 /* Acknowledge that we've seen the device. */
319 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
320
Rusty Russell9499f5e2009-06-12 22:16:35 -0600321 INIT_LIST_HEAD(&dev->vqs);
322
Rusty Russellec3d41c2007-10-22 11:03:36 +1000323 /* device_register() causes the bus infrastructure to look for a
324 * matching driver. */
325 err = device_register(&dev->dev);
Asias He90e03202012-05-03 10:20:51 +0800326out:
Rusty Russellec3d41c2007-10-22 11:03:36 +1000327 if (err)
328 add_status(dev, VIRTIO_CONFIG_S_FAILED);
329 return err;
330}
331EXPORT_SYMBOL_GPL(register_virtio_device);
332
333void unregister_virtio_device(struct virtio_device *dev)
334{
Cornelia Huck237242b2012-11-09 14:54:12 +1030335 int index = dev->index; /* save for after device release */
336
Rusty Russellec3d41c2007-10-22 11:03:36 +1000337 device_unregister(&dev->dev);
Cornelia Huck237242b2012-11-09 14:54:12 +1030338 ida_simple_remove(&virtio_index_ida, index);
Rusty Russellec3d41c2007-10-22 11:03:36 +1000339}
340EXPORT_SYMBOL_GPL(unregister_virtio_device);
341
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030342#ifdef CONFIG_PM_SLEEP
343int virtio_device_freeze(struct virtio_device *dev)
344{
345 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
346
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030347 virtio_config_disable(dev);
348
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030349 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
350
351 if (drv && drv->freeze)
352 return drv->freeze(dev);
353
354 return 0;
355}
356EXPORT_SYMBOL_GPL(virtio_device_freeze);
357
358int virtio_device_restore(struct virtio_device *dev)
359{
360 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200361 int ret;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030362
363 /* We always start by resetting the device, in case a previous
364 * driver messed it up. */
365 dev->config->reset(dev);
366
367 /* Acknowledge that we've seen the device. */
368 add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
369
370 /* Maybe driver failed before freeze.
371 * Restore the failed status, for debugging. */
372 if (dev->failed)
373 add_status(dev, VIRTIO_CONFIG_S_FAILED);
374
375 if (!drv)
376 return 0;
377
378 /* We have a driver! */
379 add_status(dev, VIRTIO_CONFIG_S_DRIVER);
380
Michael S. Tsirkin30683a82014-12-11 13:37:13 +0200381 ret = virtio_finalize_features(dev);
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200382 if (ret)
383 goto err;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030384
385 if (drv->restore) {
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200386 ret = drv->restore(dev);
387 if (ret)
388 goto err;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030389 }
390
391 /* Finally, tell the device we're all set */
392 add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
393
Michael S. Tsirkin22b70502014-10-15 10:21:55 +1030394 virtio_config_enable(dev);
395
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030396 return 0;
Michael S. Tsirkin5c609a52014-12-04 20:20:27 +0200397
398err:
399 add_status(dev, VIRTIO_CONFIG_S_FAILED);
400 return ret;
Michael S. Tsirkinc6716ba2014-10-14 10:40:35 +1030401}
402EXPORT_SYMBOL_GPL(virtio_device_restore);
403#endif
404
Rusty Russellec3d41c2007-10-22 11:03:36 +1000405static int virtio_init(void)
406{
407 if (bus_register(&virtio_bus) != 0)
408 panic("virtio bus registration failed");
409 return 0;
410}
Rusty Russellc6fd4702008-02-04 23:50:05 -0500411
412static void __exit virtio_exit(void)
413{
414 bus_unregister(&virtio_bus);
415}
Rusty Russellec3d41c2007-10-22 11:03:36 +1000416core_initcall(virtio_init);
Rusty Russellc6fd4702008-02-04 23:50:05 -0500417module_exit(virtio_exit);
418
419MODULE_LICENSE("GPL");