Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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