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