Lines Matching +full:virtio +full:- +full:device

1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/virtio.h>
11 /* Unique numbering for virtio devices. */
14 static ssize_t device_show(struct device *_d, in device_show()
18 return sysfs_emit(buf, "0x%04x\n", dev->id.device); in device_show()
20 static DEVICE_ATTR_RO(device);
22 static ssize_t vendor_show(struct device *_d, in vendor_show()
26 return sysfs_emit(buf, "0x%04x\n", dev->id.vendor); in vendor_show()
30 static ssize_t status_show(struct device *_d, in status_show()
34 return sysfs_emit(buf, "0x%08x\n", dev->config->get_status(dev)); in status_show()
38 static ssize_t modalias_show(struct device *_d, in modalias_show()
42 return sysfs_emit(buf, "virtio:d%08Xv%08X\n", in modalias_show()
43 dev->id.device, dev->id.vendor); in modalias_show()
47 static ssize_t features_show(struct device *_d, in features_show()
56 for (i = 0; i < sizeof(dev->features)*8; i++) in features_show()
77 if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID) in virtio_id_match()
80 return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor; in virtio_id_match()
85 static int virtio_dev_match(struct device *_dv, const struct device_driver *_dr) in virtio_dev_match()
91 ids = drv_to_virtio(_dr)->id_table; in virtio_dev_match()
92 for (i = 0; ids[i].device; i++) in virtio_dev_match()
98 static int virtio_uevent(const struct device *_dv, struct kobj_uevent_env *env) in virtio_uevent()
102 return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X", in virtio_uevent()
103 dev->id.device, dev->id.vendor); in virtio_uevent()
110 struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver); in virtio_check_driver_offered_feature()
112 for (i = 0; i < drv->feature_table_size; i++) in virtio_check_driver_offered_feature()
113 if (drv->feature_table[i] == fbit) in virtio_check_driver_offered_feature()
116 if (drv->feature_table_legacy) { in virtio_check_driver_offered_feature()
117 for (i = 0; i < drv->feature_table_size_legacy; i++) in virtio_check_driver_offered_feature()
118 if (drv->feature_table_legacy[i] == fbit) in virtio_check_driver_offered_feature()
128 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); in __virtio_config_changed()
130 if (!dev->config_core_enabled || dev->config_driver_disabled) in __virtio_config_changed()
131 dev->config_change_pending = true; in __virtio_config_changed()
132 else if (drv && drv->config_changed) { in __virtio_config_changed()
133 drv->config_changed(dev); in __virtio_config_changed()
134 dev->config_change_pending = false; in __virtio_config_changed()
142 spin_lock_irqsave(&dev->config_lock, flags); in virtio_config_changed()
144 spin_unlock_irqrestore(&dev->config_lock, flags); in virtio_config_changed()
149 * virtio_config_driver_disable - disable config change reporting by drivers
150 * @dev: the device to reset
157 spin_lock_irq(&dev->config_lock); in virtio_config_driver_disable()
158 dev->config_driver_disabled = true; in virtio_config_driver_disable()
159 spin_unlock_irq(&dev->config_lock); in virtio_config_driver_disable()
164 * virtio_config_driver_enable - enable config change reporting by drivers
165 * @dev: the device to reset
172 spin_lock_irq(&dev->config_lock); in virtio_config_driver_enable()
173 dev->config_driver_disabled = false; in virtio_config_driver_enable()
174 if (dev->config_change_pending) in virtio_config_driver_enable()
176 spin_unlock_irq(&dev->config_lock); in virtio_config_driver_enable()
182 spin_lock_irq(&dev->config_lock); in virtio_config_core_disable()
183 dev->config_core_enabled = false; in virtio_config_core_disable()
184 spin_unlock_irq(&dev->config_lock); in virtio_config_core_disable()
189 spin_lock_irq(&dev->config_lock); in virtio_config_core_enable()
190 dev->config_core_enabled = true; in virtio_config_core_enable()
191 if (dev->config_change_pending) in virtio_config_core_enable()
193 spin_unlock_irq(&dev->config_lock); in virtio_config_core_enable()
199 dev->config->set_status(dev, dev->config->get_status(dev) | status); in virtio_add_status()
212 dev_warn(&dev->dev, in virtio_features_ok()
213 "device must provide VIRTIO_F_VERSION_1\n"); in virtio_features_ok()
214 return -ENODEV; in virtio_features_ok()
218 dev_warn(&dev->dev, in virtio_features_ok()
219 "device must provide VIRTIO_F_ACCESS_PLATFORM\n"); in virtio_features_ok()
220 return -ENODEV; in virtio_features_ok()
228 status = dev->config->get_status(dev); in virtio_features_ok()
230 dev_err(&dev->dev, "virtio: device refuses features: %x\n", in virtio_features_ok()
232 return -ENODEV; in virtio_features_ok()
238 * virtio_reset_device - quiesce device for removal
239 * @dev: the device to reset
241 * Prevents device from sending interrupts and accessing memory.
243 * Generally used for cleanup during driver / device removal.
260 * vq->broken as true. in virtio_reset_device()
266 dev->config->reset(dev); in virtio_reset_device()
270 static int virtio_dev_probe(struct device *_d) in virtio_dev_probe()
274 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); in virtio_dev_probe()
282 /* Figure out what features the device supports. */ in virtio_dev_probe()
283 device_features = dev->config->get_features(dev); in virtio_dev_probe()
287 for (i = 0; i < drv->feature_table_size; i++) { in virtio_dev_probe()
288 unsigned int f = drv->feature_table[i]; in virtio_dev_probe()
293 /* Some drivers have a separate feature table for virtio v1.0 */ in virtio_dev_probe()
294 if (drv->feature_table_legacy) { in virtio_dev_probe()
296 for (i = 0; i < drv->feature_table_size_legacy; i++) { in virtio_dev_probe()
297 unsigned int f = drv->feature_table_legacy[i]; in virtio_dev_probe()
306 dev->features = driver_features & device_features; in virtio_dev_probe()
308 dev->features = driver_features_legacy & device_features; in virtio_dev_probe()
318 err = dev->config->finalize_features(dev); in virtio_dev_probe()
322 if (drv->validate) { in virtio_dev_probe()
323 u64 features = dev->features; in virtio_dev_probe()
325 err = drv->validate(dev); in virtio_dev_probe()
330 if (features != dev->features) { in virtio_dev_probe()
331 err = dev->config->finalize_features(dev); in virtio_dev_probe()
341 err = drv->probe(dev); in virtio_dev_probe()
345 /* If probe didn't do it, mark device DRIVER_OK ourselves. */ in virtio_dev_probe()
346 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) in virtio_dev_probe()
349 if (drv->scan) in virtio_dev_probe()
350 drv->scan(dev); in virtio_dev_probe()
362 static void virtio_dev_remove(struct device *_d) in virtio_dev_remove()
365 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); in virtio_dev_remove()
369 drv->remove(dev); in virtio_dev_remove()
371 /* Driver should have reset device. */ in virtio_dev_remove()
372 WARN_ON_ONCE(dev->config->get_status(dev)); in virtio_dev_remove()
374 /* Acknowledge the device's existence again. */ in virtio_dev_remove()
377 of_node_put(dev->dev.of_node); in virtio_dev_remove()
381 .name = "virtio",
392 BUG_ON(driver->feature_table_size && !driver->feature_table); in __register_virtio_driver()
393 driver->driver.bus = &virtio_bus; in __register_virtio_driver()
394 driver->driver.owner = owner; in __register_virtio_driver()
396 return driver_register(&driver->driver); in __register_virtio_driver()
402 driver_unregister(&driver->driver); in unregister_virtio_driver()
408 struct device_node *np, *pnode = dev_of_node(dev->dev.parent); in virtio_device_of_init()
409 char compat[] = "virtio,deviceXXXXXXXX"; in virtio_device_of_init()
421 return -EINVAL; in virtio_device_of_init()
425 return -ENODEV; in virtio_device_of_init()
427 ret = snprintf(compat, sizeof(compat), "virtio,device%x", dev->id.device); in virtio_device_of_init()
431 * On powerpc/pseries virtio devices are PCI devices so PCI in virtio_device_of_init()
432 * vendor/device ids play the role of the "compatible" property. in virtio_device_of_init()
440 dev->dev.of_node = np; in virtio_device_of_init()
449 * register_virtio_device - register virtio device
450 * @dev : virtio device to be registered
452 * On error, the caller must call put_device on &@dev->dev (and not kfree),
455 * Returns: 0 on suceess, -error on failure
461 dev->dev.bus = &virtio_bus; in register_virtio_device()
462 device_initialize(&dev->dev); in register_virtio_device()
464 /* Assign a unique device index and hence name. */ in register_virtio_device()
469 dev->index = err; in register_virtio_device()
470 err = dev_set_name(&dev->dev, "virtio%u", dev->index); in register_virtio_device()
478 spin_lock_init(&dev->config_lock); in register_virtio_device()
479 dev->config_core_enabled = false; in register_virtio_device()
480 dev->config_change_pending = false; in register_virtio_device()
482 INIT_LIST_HEAD(&dev->vqs); in register_virtio_device()
483 spin_lock_init(&dev->vqs_list_lock); in register_virtio_device()
485 /* We always start by resetting the device, in case a previous in register_virtio_device()
489 /* Acknowledge that we've seen the device. */ in register_virtio_device()
498 err = device_add(&dev->dev); in register_virtio_device()
505 of_node_put(dev->dev.of_node); in register_virtio_device()
507 ida_free(&virtio_index_ida, dev->index); in register_virtio_device()
514 bool is_virtio_device(struct device *dev) in is_virtio_device()
516 return dev->bus == &virtio_bus; in is_virtio_device()
522 int index = dev->index; /* save for after device release */ in unregister_virtio_device()
524 device_unregister(&dev->dev); in unregister_virtio_device()
533 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); in virtio_device_freeze()
538 dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; in virtio_device_freeze()
540 if (drv && drv->freeze) { in virtio_device_freeze()
541 ret = drv->freeze(dev); in virtio_device_freeze()
554 struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); in virtio_device_restore()
557 /* We always start by resetting the device, in case a previous in virtio_device_restore()
561 /* Acknowledge that we've seen the device. */ in virtio_device_restore()
566 if (dev->failed) in virtio_device_restore()
575 ret = dev->config->finalize_features(dev); in virtio_device_restore()
583 if (drv->restore) { in virtio_device_restore()
584 ret = drv->restore(dev); in virtio_device_restore()
589 /* If restore didn't do it, mark device DRIVER_OK ourselves. */ in virtio_device_restore()
590 if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) in virtio_device_restore()
607 panic("virtio bus registration failed"); in virtio_init()
621 MODULE_DESCRIPTION("Virtio core interface");