blob: 53c8e0eac638c921b209345ac0fbe87dc11df937 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
175 might_sleep();
176
177 if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
178 return 0;
179
180 virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
181 status = dev->config->get_status(dev);
182 if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
183 dev_err(&dev->dev, "virtio: device refuses features: %x\n",
184 status);
185 return -ENODEV;
186 }
187 return 0;
188}
189
190static int virtio_dev_probe(struct device *_d)
191{
192 int err, i;
193 struct virtio_device *dev = dev_to_virtio(_d);
194 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
195 u64 device_features;
196 u64 driver_features;
197 u64 driver_features_legacy;
198
199 /* We have a driver! */
200 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
201
202 /* Figure out what features the device supports. */
203 device_features = dev->config->get_features(dev);
204
205 /* Figure out what features the driver supports. */
206 driver_features = 0;
207 for (i = 0; i < drv->feature_table_size; i++) {
208 unsigned int f = drv->feature_table[i];
209 BUG_ON(f >= 64);
210 driver_features |= (1ULL << f);
211 }
212
213 /* Some drivers have a separate feature table for virtio v1.0 */
214 if (drv->feature_table_legacy) {
215 driver_features_legacy = 0;
216 for (i = 0; i < drv->feature_table_size_legacy; i++) {
217 unsigned int f = drv->feature_table_legacy[i];
218 BUG_ON(f >= 64);
219 driver_features_legacy |= (1ULL << f);
220 }
221 } else {
222 driver_features_legacy = driver_features;
223 }
224
225 if (device_features & (1ULL << VIRTIO_F_VERSION_1))
226 dev->features = driver_features & device_features;
227 else
228 dev->features = driver_features_legacy & device_features;
229
230 /* Transport features always preserved to pass to finalize_features. */
231 for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
232 if (device_features & (1ULL << i))
233 __virtio_set_bit(dev, i);
234
235 err = dev->config->finalize_features(dev);
236 if (err)
237 goto err;
238
239 if (drv->validate) {
240 u64 features = dev->features;
241
242 err = drv->validate(dev);
243 if (err)
244 goto err;
245
246 /* Did validation change any features? Then write them again. */
247 if (features != dev->features) {
248 err = dev->config->finalize_features(dev);
249 if (err)
250 goto err;
251 }
252 }
253
254 err = virtio_features_ok(dev);
255 if (err)
256 goto err;
257
258 err = drv->probe(dev);
259 if (err)
260 goto err;
261
262 /* If probe didn't do it, mark device DRIVER_OK ourselves. */
263 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
264 virtio_device_ready(dev);
265
266 if (drv->scan)
267 drv->scan(dev);
268
269 virtio_config_enable(dev);
270
271 return 0;
272err:
273 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
274 return err;
275
276}
277
278static int virtio_dev_remove(struct device *_d)
279{
280 struct virtio_device *dev = dev_to_virtio(_d);
281 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
282
283 virtio_config_disable(dev);
284
285 drv->remove(dev);
286
287 /* Driver should have reset device. */
288 WARN_ON_ONCE(dev->config->get_status(dev));
289
290 /* Acknowledge the device's existence again. */
291 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
292 return 0;
293}
294
295static struct bus_type virtio_bus = {
296 .name = "virtio",
297 .match = virtio_dev_match,
298 .dev_groups = virtio_dev_groups,
299 .uevent = virtio_uevent,
300 .probe = virtio_dev_probe,
301 .remove = virtio_dev_remove,
302};
303
304int register_virtio_driver(struct virtio_driver *driver)
305{
306 /* Catch this early. */
307 BUG_ON(driver->feature_table_size && !driver->feature_table);
308 driver->driver.bus = &virtio_bus;
309 return driver_register(&driver->driver);
310}
311EXPORT_SYMBOL_GPL(register_virtio_driver);
312
313void unregister_virtio_driver(struct virtio_driver *driver)
314{
315 driver_unregister(&driver->driver);
316}
317EXPORT_SYMBOL_GPL(unregister_virtio_driver);
318
319/**
320 * register_virtio_device - register virtio device
321 * @dev : virtio device to be registered
322 *
323 * On error, the caller must call put_device on &@dev->dev (and not kfree),
324 * as another code path may have obtained a reference to @dev.
325 *
326 * Returns: 0 on suceess, -error on failure
327 */
328int register_virtio_device(struct virtio_device *dev)
329{
330 int err;
331
332 dev->dev.bus = &virtio_bus;
333 device_initialize(&dev->dev);
334
335 /* Assign a unique device index and hence name. */
336 err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
337 if (err < 0)
338 goto out;
339
340 dev->index = err;
341 dev_set_name(&dev->dev, "virtio%u", dev->index);
342
343 spin_lock_init(&dev->config_lock);
344 dev->config_enabled = false;
345 dev->config_change_pending = false;
346
347 /* We always start by resetting the device, in case a previous
348 * driver messed it up. This also tests that code path a little. */
349 dev->config->reset(dev);
350
351 /* Acknowledge that we've seen the device. */
352 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
353
354 INIT_LIST_HEAD(&dev->vqs);
355
356 /*
357 * device_add() causes the bus infrastructure to look for a matching
358 * driver.
359 */
360 err = device_add(&dev->dev);
361 if (err)
362 ida_simple_remove(&virtio_index_ida, dev->index);
363out:
364 if (err)
365 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
366 return err;
367}
368EXPORT_SYMBOL_GPL(register_virtio_device);
369
370void unregister_virtio_device(struct virtio_device *dev)
371{
372 int index = dev->index; /* save for after device release */
373
374 device_unregister(&dev->dev);
375 ida_simple_remove(&virtio_index_ida, index);
376}
377EXPORT_SYMBOL_GPL(unregister_virtio_device);
378
379#ifdef CONFIG_PM_SLEEP
380int virtio_device_freeze(struct virtio_device *dev)
381{
382 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
383 int ret;
384
385 virtio_config_disable(dev);
386
387 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
388
389 if (drv && drv->freeze) {
390 ret = drv->freeze(dev);
391 if (ret) {
392 virtio_config_enable(dev);
393 return ret;
394 }
395 }
396
397 return 0;
398}
399EXPORT_SYMBOL_GPL(virtio_device_freeze);
400
401int virtio_device_restore(struct virtio_device *dev)
402{
403 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
404 int ret;
405
406 /* We always start by resetting the device, in case a previous
407 * driver messed it up. */
408 dev->config->reset(dev);
409
410 /* Acknowledge that we've seen the device. */
411 virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
412
413 /* Maybe driver failed before freeze.
414 * Restore the failed status, for debugging. */
415 if (dev->failed)
416 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
417
418 if (!drv)
419 return 0;
420
421 /* We have a driver! */
422 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
423
424 ret = dev->config->finalize_features(dev);
425 if (ret)
426 goto err;
427
428 ret = virtio_features_ok(dev);
429 if (ret)
430 goto err;
431
432 if (drv->restore) {
433 ret = drv->restore(dev);
434 if (ret)
435 goto err;
436 }
437
438 /* Finally, tell the device we're all set */
439 virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
440
441 virtio_config_enable(dev);
442
443 return 0;
444
445err:
446 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
447 return ret;
448}
449EXPORT_SYMBOL_GPL(virtio_device_restore);
450#endif
451
452static int virtio_init(void)
453{
454 if (bus_register(&virtio_bus) != 0)
455 panic("virtio bus registration failed");
456 return 0;
457}
458
459static void __exit virtio_exit(void)
460{
461 bus_unregister(&virtio_bus);
462 ida_destroy(&virtio_index_ida);
463}
464core_initcall(virtio_init);
465module_exit(virtio_exit);
466
467MODULE_LICENSE("GPL");