xref: /openbmc/linux/drivers/virtio/virtio.c (revision 2d091155)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/virtio.h>
3 #include <linux/spinlock.h>
4 #include <linux/virtio_config.h>
5 #include <linux/module.h>
6 #include <linux/idr.h>
7 #include <linux/of.h>
8 #include <uapi/linux/virtio_ids.h>
9 
10 /* Unique numbering for virtio devices. */
11 static DEFINE_IDA(virtio_index_ida);
12 
13 static ssize_t device_show(struct device *_d,
14 			   struct device_attribute *attr, char *buf)
15 {
16 	struct virtio_device *dev = dev_to_virtio(_d);
17 	return sprintf(buf, "0x%04x\n", dev->id.device);
18 }
19 static DEVICE_ATTR_RO(device);
20 
21 static ssize_t vendor_show(struct device *_d,
22 			   struct device_attribute *attr, char *buf)
23 {
24 	struct virtio_device *dev = dev_to_virtio(_d);
25 	return sprintf(buf, "0x%04x\n", dev->id.vendor);
26 }
27 static DEVICE_ATTR_RO(vendor);
28 
29 static ssize_t status_show(struct device *_d,
30 			   struct device_attribute *attr, char *buf)
31 {
32 	struct virtio_device *dev = dev_to_virtio(_d);
33 	return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
34 }
35 static DEVICE_ATTR_RO(status);
36 
37 static ssize_t modalias_show(struct device *_d,
38 			     struct device_attribute *attr, char *buf)
39 {
40 	struct virtio_device *dev = dev_to_virtio(_d);
41 	return sprintf(buf, "virtio:d%08Xv%08X\n",
42 		       dev->id.device, dev->id.vendor);
43 }
44 static DEVICE_ATTR_RO(modalias);
45 
46 static ssize_t features_show(struct device *_d,
47 			     struct device_attribute *attr, char *buf)
48 {
49 	struct virtio_device *dev = dev_to_virtio(_d);
50 	unsigned int i;
51 	ssize_t len = 0;
52 
53 	/* We actually represent this as a bitstring, as it could be
54 	 * arbitrary length in future. */
55 	for (i = 0; i < sizeof(dev->features)*8; i++)
56 		len += sprintf(buf+len, "%c",
57 			       __virtio_test_bit(dev, i) ? '1' : '0');
58 	len += sprintf(buf+len, "\n");
59 	return len;
60 }
61 static DEVICE_ATTR_RO(features);
62 
63 static struct attribute *virtio_dev_attrs[] = {
64 	&dev_attr_device.attr,
65 	&dev_attr_vendor.attr,
66 	&dev_attr_status.attr,
67 	&dev_attr_modalias.attr,
68 	&dev_attr_features.attr,
69 	NULL,
70 };
71 ATTRIBUTE_GROUPS(virtio_dev);
72 
73 static inline int virtio_id_match(const struct virtio_device *dev,
74 				  const struct virtio_device_id *id)
75 {
76 	if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
77 		return 0;
78 
79 	return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
80 }
81 
82 /* This looks through all the IDs a driver claims to support.  If any of them
83  * match, we return 1 and the kernel will call virtio_dev_probe(). */
84 static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
85 {
86 	unsigned int i;
87 	struct virtio_device *dev = dev_to_virtio(_dv);
88 	const struct virtio_device_id *ids;
89 
90 	ids = drv_to_virtio(_dr)->id_table;
91 	for (i = 0; ids[i].device; i++)
92 		if (virtio_id_match(dev, &ids[i]))
93 			return 1;
94 	return 0;
95 }
96 
97 static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
98 {
99 	struct virtio_device *dev = dev_to_virtio(_dv);
100 
101 	return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
102 			      dev->id.device, dev->id.vendor);
103 }
104 
105 void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
106 					 unsigned int fbit)
107 {
108 	unsigned int i;
109 	struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
110 
111 	for (i = 0; i < drv->feature_table_size; i++)
112 		if (drv->feature_table[i] == fbit)
113 			return;
114 
115 	if (drv->feature_table_legacy) {
116 		for (i = 0; i < drv->feature_table_size_legacy; i++)
117 			if (drv->feature_table_legacy[i] == fbit)
118 				return;
119 	}
120 
121 	BUG();
122 }
123 EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
124 
125 static void __virtio_config_changed(struct virtio_device *dev)
126 {
127 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
128 
129 	if (!dev->config_enabled)
130 		dev->config_change_pending = true;
131 	else if (drv && drv->config_changed)
132 		drv->config_changed(dev);
133 }
134 
135 void virtio_config_changed(struct virtio_device *dev)
136 {
137 	unsigned long flags;
138 
139 	spin_lock_irqsave(&dev->config_lock, flags);
140 	__virtio_config_changed(dev);
141 	spin_unlock_irqrestore(&dev->config_lock, flags);
142 }
143 EXPORT_SYMBOL_GPL(virtio_config_changed);
144 
145 static void virtio_config_disable(struct virtio_device *dev)
146 {
147 	spin_lock_irq(&dev->config_lock);
148 	dev->config_enabled = false;
149 	spin_unlock_irq(&dev->config_lock);
150 }
151 
152 static void virtio_config_enable(struct virtio_device *dev)
153 {
154 	spin_lock_irq(&dev->config_lock);
155 	dev->config_enabled = true;
156 	if (dev->config_change_pending)
157 		__virtio_config_changed(dev);
158 	dev->config_change_pending = false;
159 	spin_unlock_irq(&dev->config_lock);
160 }
161 
162 void virtio_add_status(struct virtio_device *dev, unsigned int status)
163 {
164 	might_sleep();
165 	dev->config->set_status(dev, dev->config->get_status(dev) | status);
166 }
167 EXPORT_SYMBOL_GPL(virtio_add_status);
168 
169 /* Do some validation, then set FEATURES_OK */
170 static int virtio_features_ok(struct virtio_device *dev)
171 {
172 	unsigned status;
173 	int ret;
174 
175 	might_sleep();
176 
177 	ret = arch_has_restricted_virtio_memory_access();
178 	if (ret) {
179 		if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
180 			dev_warn(&dev->dev,
181 				 "device must provide VIRTIO_F_VERSION_1\n");
182 			return -ENODEV;
183 		}
184 
185 		if (!virtio_has_feature(dev, VIRTIO_F_ACCESS_PLATFORM)) {
186 			dev_warn(&dev->dev,
187 				 "device must provide VIRTIO_F_ACCESS_PLATFORM\n");
188 			return -ENODEV;
189 		}
190 	}
191 
192 	if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
193 		return 0;
194 
195 	virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
196 	status = dev->config->get_status(dev);
197 	if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
198 		dev_err(&dev->dev, "virtio: device refuses features: %x\n",
199 			status);
200 		return -ENODEV;
201 	}
202 	return 0;
203 }
204 
205 /**
206  * virtio_reset_device - quiesce device for removal
207  * @dev: the device to reset
208  *
209  * Prevents device from sending interrupts and accessing memory.
210  *
211  * Generally used for cleanup during driver / device removal.
212  *
213  * Once this has been invoked, caller must ensure that
214  * virtqueue_notify / virtqueue_kick are not in progress.
215  *
216  * Note: this guarantees that vq callbacks are not in progress, however caller
217  * is responsible for preventing access from other contexts, such as a system
218  * call/workqueue/bh.  Invoking virtio_break_device then flushing any such
219  * contexts is one way to handle that.
220  * */
221 void virtio_reset_device(struct virtio_device *dev)
222 {
223 	dev->config->reset(dev);
224 }
225 EXPORT_SYMBOL_GPL(virtio_reset_device);
226 
227 static int virtio_dev_probe(struct device *_d)
228 {
229 	int err, i;
230 	struct virtio_device *dev = dev_to_virtio(_d);
231 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
232 	u64 device_features;
233 	u64 driver_features;
234 	u64 driver_features_legacy;
235 
236 	/* We have a driver! */
237 	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
238 
239 	/* Figure out what features the device supports. */
240 	device_features = dev->config->get_features(dev);
241 
242 	/* Figure out what features the driver supports. */
243 	driver_features = 0;
244 	for (i = 0; i < drv->feature_table_size; i++) {
245 		unsigned int f = drv->feature_table[i];
246 		BUG_ON(f >= 64);
247 		driver_features |= (1ULL << f);
248 	}
249 
250 	/* Some drivers have a separate feature table for virtio v1.0 */
251 	if (drv->feature_table_legacy) {
252 		driver_features_legacy = 0;
253 		for (i = 0; i < drv->feature_table_size_legacy; i++) {
254 			unsigned int f = drv->feature_table_legacy[i];
255 			BUG_ON(f >= 64);
256 			driver_features_legacy |= (1ULL << f);
257 		}
258 	} else {
259 		driver_features_legacy = driver_features;
260 	}
261 
262 	if (device_features & (1ULL << VIRTIO_F_VERSION_1))
263 		dev->features = driver_features & device_features;
264 	else
265 		dev->features = driver_features_legacy & device_features;
266 
267 	/* Transport features always preserved to pass to finalize_features. */
268 	for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
269 		if (device_features & (1ULL << i))
270 			__virtio_set_bit(dev, i);
271 
272 	err = dev->config->finalize_features(dev);
273 	if (err)
274 		goto err;
275 
276 	if (drv->validate) {
277 		u64 features = dev->features;
278 
279 		err = drv->validate(dev);
280 		if (err)
281 			goto err;
282 
283 		/* Did validation change any features? Then write them again. */
284 		if (features != dev->features) {
285 			err = dev->config->finalize_features(dev);
286 			if (err)
287 				goto err;
288 		}
289 	}
290 
291 	err = virtio_features_ok(dev);
292 	if (err)
293 		goto err;
294 
295 	err = drv->probe(dev);
296 	if (err)
297 		goto err;
298 
299 	/* If probe didn't do it, mark device DRIVER_OK ourselves. */
300 	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
301 		virtio_device_ready(dev);
302 
303 	if (drv->scan)
304 		drv->scan(dev);
305 
306 	virtio_config_enable(dev);
307 
308 	return 0;
309 err:
310 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
311 	return err;
312 
313 }
314 
315 static void virtio_dev_remove(struct device *_d)
316 {
317 	struct virtio_device *dev = dev_to_virtio(_d);
318 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
319 
320 	virtio_config_disable(dev);
321 
322 	drv->remove(dev);
323 
324 	/* Driver should have reset device. */
325 	WARN_ON_ONCE(dev->config->get_status(dev));
326 
327 	/* Acknowledge the device's existence again. */
328 	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
329 
330 	of_node_put(dev->dev.of_node);
331 }
332 
333 static struct bus_type virtio_bus = {
334 	.name  = "virtio",
335 	.match = virtio_dev_match,
336 	.dev_groups = virtio_dev_groups,
337 	.uevent = virtio_uevent,
338 	.probe = virtio_dev_probe,
339 	.remove = virtio_dev_remove,
340 };
341 
342 int register_virtio_driver(struct virtio_driver *driver)
343 {
344 	/* Catch this early. */
345 	BUG_ON(driver->feature_table_size && !driver->feature_table);
346 	driver->driver.bus = &virtio_bus;
347 	return driver_register(&driver->driver);
348 }
349 EXPORT_SYMBOL_GPL(register_virtio_driver);
350 
351 void unregister_virtio_driver(struct virtio_driver *driver)
352 {
353 	driver_unregister(&driver->driver);
354 }
355 EXPORT_SYMBOL_GPL(unregister_virtio_driver);
356 
357 static int virtio_device_of_init(struct virtio_device *dev)
358 {
359 	struct device_node *np, *pnode = dev_of_node(dev->dev.parent);
360 	char compat[] = "virtio,deviceXXXXXXXX";
361 	int ret, count;
362 
363 	if (!pnode)
364 		return 0;
365 
366 	count = of_get_available_child_count(pnode);
367 	if (!count)
368 		return 0;
369 
370 	/* There can be only 1 child node */
371 	if (WARN_ON(count > 1))
372 		return -EINVAL;
373 
374 	np = of_get_next_available_child(pnode, NULL);
375 	if (WARN_ON(!np))
376 		return -ENODEV;
377 
378 	ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device);
379 	BUG_ON(ret >= sizeof(compat));
380 
381 	/*
382 	 * On powerpc/pseries virtio devices are PCI devices so PCI
383 	 * vendor/device ids play the role of the "compatible" property.
384 	 * Simply don't init of_node in this case.
385 	 */
386 	if (!of_device_is_compatible(np, compat)) {
387 		ret = 0;
388 		goto out;
389 	}
390 
391 	dev->dev.of_node = np;
392 	return 0;
393 
394 out:
395 	of_node_put(np);
396 	return ret;
397 }
398 
399 /**
400  * register_virtio_device - register virtio device
401  * @dev        : virtio device to be registered
402  *
403  * On error, the caller must call put_device on &@dev->dev (and not kfree),
404  * as another code path may have obtained a reference to @dev.
405  *
406  * Returns: 0 on suceess, -error on failure
407  */
408 int register_virtio_device(struct virtio_device *dev)
409 {
410 	int err;
411 
412 	dev->dev.bus = &virtio_bus;
413 	device_initialize(&dev->dev);
414 
415 	/* Assign a unique device index and hence name. */
416 	err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
417 	if (err < 0)
418 		goto out;
419 
420 	dev->index = err;
421 	dev_set_name(&dev->dev, "virtio%u", dev->index);
422 
423 	err = virtio_device_of_init(dev);
424 	if (err)
425 		goto out_ida_remove;
426 
427 	spin_lock_init(&dev->config_lock);
428 	dev->config_enabled = false;
429 	dev->config_change_pending = false;
430 
431 	/* We always start by resetting the device, in case a previous
432 	 * driver messed it up.  This also tests that code path a little. */
433 	dev->config->reset(dev);
434 
435 	/* Acknowledge that we've seen the device. */
436 	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
437 
438 	INIT_LIST_HEAD(&dev->vqs);
439 	spin_lock_init(&dev->vqs_list_lock);
440 
441 	/*
442 	 * device_add() causes the bus infrastructure to look for a matching
443 	 * driver.
444 	 */
445 	err = device_add(&dev->dev);
446 	if (err)
447 		goto out_of_node_put;
448 
449 	return 0;
450 
451 out_of_node_put:
452 	of_node_put(dev->dev.of_node);
453 out_ida_remove:
454 	ida_simple_remove(&virtio_index_ida, dev->index);
455 out:
456 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
457 	return err;
458 }
459 EXPORT_SYMBOL_GPL(register_virtio_device);
460 
461 bool is_virtio_device(struct device *dev)
462 {
463 	return dev->bus == &virtio_bus;
464 }
465 EXPORT_SYMBOL_GPL(is_virtio_device);
466 
467 void unregister_virtio_device(struct virtio_device *dev)
468 {
469 	int index = dev->index; /* save for after device release */
470 
471 	device_unregister(&dev->dev);
472 	ida_simple_remove(&virtio_index_ida, index);
473 }
474 EXPORT_SYMBOL_GPL(unregister_virtio_device);
475 
476 #ifdef CONFIG_PM_SLEEP
477 int virtio_device_freeze(struct virtio_device *dev)
478 {
479 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
480 
481 	virtio_config_disable(dev);
482 
483 	dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
484 
485 	if (drv && drv->freeze)
486 		return drv->freeze(dev);
487 
488 	return 0;
489 }
490 EXPORT_SYMBOL_GPL(virtio_device_freeze);
491 
492 int virtio_device_restore(struct virtio_device *dev)
493 {
494 	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
495 	int ret;
496 
497 	/* We always start by resetting the device, in case a previous
498 	 * driver messed it up. */
499 	dev->config->reset(dev);
500 
501 	/* Acknowledge that we've seen the device. */
502 	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
503 
504 	/* Maybe driver failed before freeze.
505 	 * Restore the failed status, for debugging. */
506 	if (dev->failed)
507 		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
508 
509 	if (!drv)
510 		return 0;
511 
512 	/* We have a driver! */
513 	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
514 
515 	ret = dev->config->finalize_features(dev);
516 	if (ret)
517 		goto err;
518 
519 	ret = virtio_features_ok(dev);
520 	if (ret)
521 		goto err;
522 
523 	if (drv->restore) {
524 		ret = drv->restore(dev);
525 		if (ret)
526 			goto err;
527 	}
528 
529 	/* If restore didn't do it, mark device DRIVER_OK ourselves. */
530 	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
531 		virtio_device_ready(dev);
532 
533 	virtio_config_enable(dev);
534 
535 	return 0;
536 
537 err:
538 	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
539 	return ret;
540 }
541 EXPORT_SYMBOL_GPL(virtio_device_restore);
542 #endif
543 
544 static int virtio_init(void)
545 {
546 	if (bus_register(&virtio_bus) != 0)
547 		panic("virtio bus registration failed");
548 	return 0;
549 }
550 
551 static void __exit virtio_exit(void)
552 {
553 	bus_unregister(&virtio_bus);
554 	ida_destroy(&virtio_index_ida);
555 }
556 core_initcall(virtio_init);
557 module_exit(virtio_exit);
558 
559 MODULE_LICENSE("GPL");
560