LCOV - code coverage report
Current view: top level - drivers/virtio - virtio.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 191 1.6 %
Date: 2022-12-09 01:23:36 Functions: 1 24 4.2 %

          Line data    Source code
       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           0 : static ssize_t device_show(struct device *_d,
      14             :                            struct device_attribute *attr, char *buf)
      15             : {
      16           0 :         struct virtio_device *dev = dev_to_virtio(_d);
      17           0 :         return sprintf(buf, "0x%04x\n", dev->id.device);
      18             : }
      19             : static DEVICE_ATTR_RO(device);
      20             : 
      21           0 : static ssize_t vendor_show(struct device *_d,
      22             :                            struct device_attribute *attr, char *buf)
      23             : {
      24           0 :         struct virtio_device *dev = dev_to_virtio(_d);
      25           0 :         return sprintf(buf, "0x%04x\n", dev->id.vendor);
      26             : }
      27             : static DEVICE_ATTR_RO(vendor);
      28             : 
      29           0 : static ssize_t status_show(struct device *_d,
      30             :                            struct device_attribute *attr, char *buf)
      31             : {
      32           0 :         struct virtio_device *dev = dev_to_virtio(_d);
      33           0 :         return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
      34             : }
      35             : static DEVICE_ATTR_RO(status);
      36             : 
      37           0 : static ssize_t modalias_show(struct device *_d,
      38             :                              struct device_attribute *attr, char *buf)
      39             : {
      40           0 :         struct virtio_device *dev = dev_to_virtio(_d);
      41           0 :         return sprintf(buf, "virtio:d%08Xv%08X\n",
      42             :                        dev->id.device, dev->id.vendor);
      43             : }
      44             : static DEVICE_ATTR_RO(modalias);
      45             : 
      46           0 : static ssize_t features_show(struct device *_d,
      47             :                              struct device_attribute *attr, char *buf)
      48             : {
      49           0 :         struct virtio_device *dev = dev_to_virtio(_d);
      50             :         unsigned int i;
      51           0 :         ssize_t len = 0;
      52             : 
      53             :         /* We actually represent this as a bitstring, as it could be
      54             :          * arbitrary length in future. */
      55           0 :         for (i = 0; i < sizeof(dev->features)*8; i++)
      56           0 :                 len += sprintf(buf+len, "%c",
      57           0 :                                __virtio_test_bit(dev, i) ? '1' : '0');
      58           0 :         len += sprintf(buf+len, "\n");
      59           0 :         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           0 :         if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
      77             :                 return 0;
      78             : 
      79           0 :         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           0 : static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
      85             : {
      86             :         unsigned int i;
      87           0 :         struct virtio_device *dev = dev_to_virtio(_dv);
      88             :         const struct virtio_device_id *ids;
      89             : 
      90           0 :         ids = drv_to_virtio(_dr)->id_table;
      91           0 :         for (i = 0; ids[i].device; i++)
      92           0 :                 if (virtio_id_match(dev, &ids[i]))
      93             :                         return 1;
      94             :         return 0;
      95             : }
      96             : 
      97           0 : static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
      98             : {
      99           0 :         struct virtio_device *dev = dev_to_virtio(_dv);
     100             : 
     101           0 :         return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
     102             :                               dev->id.device, dev->id.vendor);
     103             : }
     104             : 
     105           0 : void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
     106             :                                          unsigned int fbit)
     107             : {
     108             :         unsigned int i;
     109           0 :         struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
     110             : 
     111           0 :         for (i = 0; i < drv->feature_table_size; i++)
     112           0 :                 if (drv->feature_table[i] == fbit)
     113             :                         return;
     114             : 
     115           0 :         if (drv->feature_table_legacy) {
     116           0 :                 for (i = 0; i < drv->feature_table_size_legacy; i++)
     117           0 :                         if (drv->feature_table_legacy[i] == fbit)
     118             :                                 return;
     119             :         }
     120             : 
     121           0 :         BUG();
     122             : }
     123             : EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
     124             : 
     125             : static void __virtio_config_changed(struct virtio_device *dev)
     126             : {
     127           0 :         struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
     128             : 
     129           0 :         if (!dev->config_enabled)
     130           0 :                 dev->config_change_pending = true;
     131           0 :         else if (drv && drv->config_changed)
     132           0 :                 drv->config_changed(dev);
     133             : }
     134             : 
     135           0 : void virtio_config_changed(struct virtio_device *dev)
     136             : {
     137             :         unsigned long flags;
     138             : 
     139           0 :         spin_lock_irqsave(&dev->config_lock, flags);
     140           0 :         __virtio_config_changed(dev);
     141           0 :         spin_unlock_irqrestore(&dev->config_lock, flags);
     142           0 : }
     143             : EXPORT_SYMBOL_GPL(virtio_config_changed);
     144             : 
     145             : static void virtio_config_disable(struct virtio_device *dev)
     146             : {
     147           0 :         spin_lock_irq(&dev->config_lock);
     148           0 :         dev->config_enabled = false;
     149           0 :         spin_unlock_irq(&dev->config_lock);
     150             : }
     151             : 
     152           0 : static void virtio_config_enable(struct virtio_device *dev)
     153             : {
     154           0 :         spin_lock_irq(&dev->config_lock);
     155           0 :         dev->config_enabled = true;
     156           0 :         if (dev->config_change_pending)
     157             :                 __virtio_config_changed(dev);
     158           0 :         dev->config_change_pending = false;
     159           0 :         spin_unlock_irq(&dev->config_lock);
     160           0 : }
     161             : 
     162           0 : void virtio_add_status(struct virtio_device *dev, unsigned int status)
     163             : {
     164             :         might_sleep();
     165           0 :         dev->config->set_status(dev, dev->config->get_status(dev) | status);
     166           0 : }
     167             : EXPORT_SYMBOL_GPL(virtio_add_status);
     168             : 
     169             : /* Do some validation, then set FEATURES_OK */
     170           0 : static int virtio_features_ok(struct virtio_device *dev)
     171             : {
     172             :         unsigned status;
     173             :         int ret;
     174             : 
     175             :         might_sleep();
     176             : 
     177           0 :         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           0 :         if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
     193             :                 return 0;
     194             : 
     195           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
     196           0 :         status = dev->config->get_status(dev);
     197           0 :         if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
     198           0 :                 dev_err(&dev->dev, "virtio: device refuses features: %x\n",
     199             :                         status);
     200           0 :                 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           0 : void virtio_reset_device(struct virtio_device *dev)
     222             : {
     223           0 :         dev->config->reset(dev);
     224           0 : }
     225             : EXPORT_SYMBOL_GPL(virtio_reset_device);
     226             : 
     227           0 : static int virtio_dev_probe(struct device *_d)
     228             : {
     229             :         int err, i;
     230           0 :         struct virtio_device *dev = dev_to_virtio(_d);
     231           0 :         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           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
     238             : 
     239             :         /* Figure out what features the device supports. */
     240           0 :         device_features = dev->config->get_features(dev);
     241             : 
     242             :         /* Figure out what features the driver supports. */
     243           0 :         driver_features = 0;
     244           0 :         for (i = 0; i < drv->feature_table_size; i++) {
     245           0 :                 unsigned int f = drv->feature_table[i];
     246           0 :                 BUG_ON(f >= 64);
     247           0 :                 driver_features |= (1ULL << f);
     248             :         }
     249             : 
     250             :         /* Some drivers have a separate feature table for virtio v1.0 */
     251           0 :         if (drv->feature_table_legacy) {
     252             :                 driver_features_legacy = 0;
     253           0 :                 for (i = 0; i < drv->feature_table_size_legacy; i++) {
     254           0 :                         unsigned int f = drv->feature_table_legacy[i];
     255           0 :                         BUG_ON(f >= 64);
     256           0 :                         driver_features_legacy |= (1ULL << f);
     257             :                 }
     258             :         } else {
     259             :                 driver_features_legacy = driver_features;
     260             :         }
     261             : 
     262           0 :         if (device_features & (1ULL << VIRTIO_F_VERSION_1))
     263           0 :                 dev->features = driver_features & device_features;
     264             :         else
     265           0 :                 dev->features = driver_features_legacy & device_features;
     266             : 
     267             :         /* Transport features always preserved to pass to finalize_features. */
     268           0 :         for (i = VIRTIO_TRANSPORT_F_START; i < VIRTIO_TRANSPORT_F_END; i++)
     269           0 :                 if (device_features & (1ULL << i))
     270           0 :                         __virtio_set_bit(dev, i);
     271             : 
     272           0 :         err = dev->config->finalize_features(dev);
     273           0 :         if (err)
     274             :                 goto err;
     275             : 
     276           0 :         if (drv->validate) {
     277           0 :                 u64 features = dev->features;
     278             : 
     279           0 :                 err = drv->validate(dev);
     280           0 :                 if (err)
     281             :                         goto err;
     282             : 
     283             :                 /* Did validation change any features? Then write them again. */
     284           0 :                 if (features != dev->features) {
     285           0 :                         err = dev->config->finalize_features(dev);
     286           0 :                         if (err)
     287             :                                 goto err;
     288             :                 }
     289             :         }
     290             : 
     291           0 :         err = virtio_features_ok(dev);
     292           0 :         if (err)
     293             :                 goto err;
     294             : 
     295           0 :         err = drv->probe(dev);
     296           0 :         if (err)
     297             :                 goto err;
     298             : 
     299             :         /* If probe didn't do it, mark device DRIVER_OK ourselves. */
     300           0 :         if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
     301           0 :                 virtio_device_ready(dev);
     302             : 
     303           0 :         if (drv->scan)
     304           0 :                 drv->scan(dev);
     305             : 
     306           0 :         virtio_config_enable(dev);
     307             : 
     308           0 :         return 0;
     309             : err:
     310           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
     311           0 :         return err;
     312             : 
     313             : }
     314             : 
     315           0 : static void virtio_dev_remove(struct device *_d)
     316             : {
     317           0 :         struct virtio_device *dev = dev_to_virtio(_d);
     318           0 :         struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
     319             : 
     320             :         virtio_config_disable(dev);
     321             : 
     322           0 :         drv->remove(dev);
     323             : 
     324             :         /* Driver should have reset device. */
     325           0 :         WARN_ON_ONCE(dev->config->get_status(dev));
     326             : 
     327             :         /* Acknowledge the device's existence again. */
     328           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
     329             : 
     330           0 :         of_node_put(dev->dev.of_node);
     331           0 : }
     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           0 : int register_virtio_driver(struct virtio_driver *driver)
     343             : {
     344             :         /* Catch this early. */
     345           0 :         BUG_ON(driver->feature_table_size && !driver->feature_table);
     346           0 :         driver->driver.bus = &virtio_bus;
     347           0 :         return driver_register(&driver->driver);
     348             : }
     349             : EXPORT_SYMBOL_GPL(register_virtio_driver);
     350             : 
     351           0 : void unregister_virtio_driver(struct virtio_driver *driver)
     352             : {
     353           0 :         driver_unregister(&driver->driver);
     354           0 : }
     355             : EXPORT_SYMBOL_GPL(unregister_virtio_driver);
     356             : 
     357             : static int virtio_device_of_init(struct virtio_device *dev)
     358             : {
     359           0 :         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           0 : int register_virtio_device(struct virtio_device *dev)
     409             : {
     410             :         int err;
     411             : 
     412           0 :         dev->dev.bus = &virtio_bus;
     413           0 :         device_initialize(&dev->dev);
     414             : 
     415             :         /* Assign a unique device index and hence name. */
     416           0 :         err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
     417           0 :         if (err < 0)
     418             :                 goto out;
     419             : 
     420           0 :         dev->index = err;
     421           0 :         dev_set_name(&dev->dev, "virtio%u", dev->index);
     422             : 
     423           0 :         err = virtio_device_of_init(dev);
     424             :         if (err)
     425             :                 goto out_ida_remove;
     426             : 
     427           0 :         spin_lock_init(&dev->config_lock);
     428           0 :         dev->config_enabled = false;
     429           0 :         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           0 :         dev->config->reset(dev);
     434             : 
     435             :         /* Acknowledge that we've seen the device. */
     436           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
     437             : 
     438           0 :         INIT_LIST_HEAD(&dev->vqs);
     439           0 :         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           0 :         err = device_add(&dev->dev);
     446           0 :         if (err)
     447             :                 goto out_of_node_put;
     448             : 
     449             :         return 0;
     450             : 
     451             : out_of_node_put:
     452           0 :         of_node_put(dev->dev.of_node);
     453             : out_ida_remove:
     454           0 :         ida_simple_remove(&virtio_index_ida, dev->index);
     455             : out:
     456           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
     457           0 :         return err;
     458             : }
     459             : EXPORT_SYMBOL_GPL(register_virtio_device);
     460             : 
     461           0 : bool is_virtio_device(struct device *dev)
     462             : {
     463           0 :         return dev->bus == &virtio_bus;
     464             : }
     465             : EXPORT_SYMBOL_GPL(is_virtio_device);
     466             : 
     467           0 : void unregister_virtio_device(struct virtio_device *dev)
     468             : {
     469           0 :         int index = dev->index; /* save for after device release */
     470             : 
     471           0 :         device_unregister(&dev->dev);
     472           0 :         ida_simple_remove(&virtio_index_ida, index);
     473           0 : }
     474             : EXPORT_SYMBOL_GPL(unregister_virtio_device);
     475             : 
     476             : #ifdef CONFIG_PM_SLEEP
     477           0 : int virtio_device_freeze(struct virtio_device *dev)
     478             : {
     479           0 :         struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
     480             : 
     481           0 :         virtio_config_disable(dev);
     482             : 
     483           0 :         dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;
     484             : 
     485           0 :         if (drv && drv->freeze)
     486           0 :                 return drv->freeze(dev);
     487             : 
     488             :         return 0;
     489             : }
     490             : EXPORT_SYMBOL_GPL(virtio_device_freeze);
     491             : 
     492           0 : int virtio_device_restore(struct virtio_device *dev)
     493             : {
     494           0 :         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           0 :         dev->config->reset(dev);
     500             : 
     501             :         /* Acknowledge that we've seen the device. */
     502           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
     503             : 
     504             :         /* Maybe driver failed before freeze.
     505             :          * Restore the failed status, for debugging. */
     506           0 :         if (dev->failed)
     507             :                 virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
     508             : 
     509           0 :         if (!drv)
     510             :                 return 0;
     511             : 
     512             :         /* We have a driver! */
     513           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
     514             : 
     515           0 :         ret = dev->config->finalize_features(dev);
     516           0 :         if (ret)
     517             :                 goto err;
     518             : 
     519           0 :         ret = virtio_features_ok(dev);
     520           0 :         if (ret)
     521             :                 goto err;
     522             : 
     523           0 :         if (drv->restore) {
     524           0 :                 ret = drv->restore(dev);
     525           0 :                 if (ret)
     526             :                         goto err;
     527             :         }
     528             : 
     529             :         /* Finally, tell the device we're all set */
     530           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
     531             : 
     532           0 :         virtio_config_enable(dev);
     533             : 
     534           0 :         return 0;
     535             : 
     536             : err:
     537           0 :         virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
     538           0 :         return ret;
     539             : }
     540             : EXPORT_SYMBOL_GPL(virtio_device_restore);
     541             : #endif
     542             : 
     543           1 : static int virtio_init(void)
     544             : {
     545           1 :         if (bus_register(&virtio_bus) != 0)
     546           0 :                 panic("virtio bus registration failed");
     547           1 :         return 0;
     548             : }
     549             : 
     550           0 : static void __exit virtio_exit(void)
     551             : {
     552           0 :         bus_unregister(&virtio_bus);
     553           0 :         ida_destroy(&virtio_index_ida);
     554           0 : }
     555             : core_initcall(virtio_init);
     556             : module_exit(virtio_exit);
     557             : 
     558             : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14