virtio.c 11.0 KB
Newer Older
R
Rusty Russell 已提交
1 2 3
#include <linux/virtio.h>
#include <linux/spinlock.h>
#include <linux/virtio_config.h>
4
#include <linux/module.h>
5
#include <linux/idr.h>
6
#include <uapi/linux/virtio_ids.h>
R
Rusty Russell 已提交
7

8
/* Unique numbering for virtio devices. */
9
static DEFINE_IDA(virtio_index_ida);
10

R
Rusty Russell 已提交
11 12 13
static ssize_t device_show(struct device *_d,
			   struct device_attribute *attr, char *buf)
{
14
	struct virtio_device *dev = dev_to_virtio(_d);
15
	return sprintf(buf, "0x%04x\n", dev->id.device);
R
Rusty Russell 已提交
16
}
17 18
static DEVICE_ATTR_RO(device);

R
Rusty Russell 已提交
19 20 21
static ssize_t vendor_show(struct device *_d,
			   struct device_attribute *attr, char *buf)
{
22
	struct virtio_device *dev = dev_to_virtio(_d);
23
	return sprintf(buf, "0x%04x\n", dev->id.vendor);
R
Rusty Russell 已提交
24
}
25 26
static DEVICE_ATTR_RO(vendor);

R
Rusty Russell 已提交
27 28 29
static ssize_t status_show(struct device *_d,
			   struct device_attribute *attr, char *buf)
{
30
	struct virtio_device *dev = dev_to_virtio(_d);
31
	return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
R
Rusty Russell 已提交
32
}
33 34
static DEVICE_ATTR_RO(status);

35 36 37
static ssize_t modalias_show(struct device *_d,
			     struct device_attribute *attr, char *buf)
{
38
	struct virtio_device *dev = dev_to_virtio(_d);
39 40 41
	return sprintf(buf, "virtio:d%08Xv%08X\n",
		       dev->id.device, dev->id.vendor);
}
42 43
static DEVICE_ATTR_RO(modalias);

R
Rusty Russell 已提交
44 45 46
static ssize_t features_show(struct device *_d,
			     struct device_attribute *attr, char *buf)
{
47
	struct virtio_device *dev = dev_to_virtio(_d);
R
Rusty Russell 已提交
48 49 50 51 52
	unsigned int i;
	ssize_t len = 0;

	/* We actually represent this as a bitstring, as it could be
	 * arbitrary length in future. */
53
	for (i = 0; i < sizeof(dev->features)*8; i++)
R
Rusty Russell 已提交
54
		len += sprintf(buf+len, "%c",
55
			       __virtio_test_bit(dev, i) ? '1' : '0');
R
Rusty Russell 已提交
56 57 58
	len += sprintf(buf+len, "\n");
	return len;
}
59 60 61 62 63 64 65 66 67
static DEVICE_ATTR_RO(features);

static struct attribute *virtio_dev_attrs[] = {
	&dev_attr_device.attr,
	&dev_attr_vendor.attr,
	&dev_attr_status.attr,
	&dev_attr_modalias.attr,
	&dev_attr_features.attr,
	NULL,
R
Rusty Russell 已提交
68
};
69
ATTRIBUTE_GROUPS(virtio_dev);
R
Rusty Russell 已提交
70 71 72 73

static inline int virtio_id_match(const struct virtio_device *dev,
				  const struct virtio_device_id *id)
{
74
	if (id->device != dev->id.device && id->device != VIRTIO_DEV_ANY_ID)
R
Rusty Russell 已提交
75 76
		return 0;

77
	return id->vendor == VIRTIO_DEV_ANY_ID || id->vendor == dev->id.vendor;
R
Rusty Russell 已提交
78 79 80 81 82 83 84
}

/* This looks through all the IDs a driver claims to support.  If any of them
 * match, we return 1 and the kernel will call virtio_dev_probe(). */
static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
{
	unsigned int i;
85
	struct virtio_device *dev = dev_to_virtio(_dv);
R
Rusty Russell 已提交
86 87
	const struct virtio_device_id *ids;

88
	ids = drv_to_virtio(_dr)->id_table;
R
Rusty Russell 已提交
89 90 91 92 93 94
	for (i = 0; ids[i].device; i++)
		if (virtio_id_match(dev, &ids[i]))
			return 1;
	return 0;
}

95 96
static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
{
97
	struct virtio_device *dev = dev_to_virtio(_dv);
98 99 100 101 102

	return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
			      dev->id.device, dev->id.vendor);
}

103 104 105 106
void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
					 unsigned int fbit)
{
	unsigned int i;
107
	struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
108 109 110 111

	for (i = 0; i < drv->feature_table_size; i++)
		if (drv->feature_table[i] == fbit)
			return;
112 113 114 115 116 117 118

	if (drv->feature_table_legacy) {
		for (i = 0; i < drv->feature_table_size_legacy; i++)
			if (drv->feature_table_legacy[i] == fbit)
				return;
	}

119 120 121 122
	BUG();
}
EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void __virtio_config_changed(struct virtio_device *dev)
{
	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);

	if (!dev->config_enabled)
		dev->config_change_pending = true;
	else if (drv && drv->config_changed)
		drv->config_changed(dev);
}

void virtio_config_changed(struct virtio_device *dev)
{
	unsigned long flags;

	spin_lock_irqsave(&dev->config_lock, flags);
	__virtio_config_changed(dev);
	spin_unlock_irqrestore(&dev->config_lock, flags);
}
EXPORT_SYMBOL_GPL(virtio_config_changed);

143
void virtio_config_disable(struct virtio_device *dev)
144 145 146 147 148
{
	spin_lock_irq(&dev->config_lock);
	dev->config_enabled = false;
	spin_unlock_irq(&dev->config_lock);
}
149
EXPORT_SYMBOL_GPL(virtio_config_disable);
150

151
void virtio_config_enable(struct virtio_device *dev)
152 153 154 155 156 157 158 159
{
	spin_lock_irq(&dev->config_lock);
	dev->config_enabled = true;
	if (dev->config_change_pending)
		__virtio_config_changed(dev);
	dev->config_change_pending = false;
	spin_unlock_irq(&dev->config_lock);
}
160 161 162 163 164 165 166
EXPORT_SYMBOL_GPL(virtio_config_enable);

void virtio_add_status(struct virtio_device *dev, unsigned int status)
{
	dev->config->set_status(dev, dev->config->get_status(dev) | status);
}
EXPORT_SYMBOL_GPL(virtio_add_status);
167

168
int virtio_finalize_features(struct virtio_device *dev)
169 170 171 172 173 174 175 176 177 178
{
	int ret = dev->config->finalize_features(dev);
	unsigned status;

	if (ret)
		return ret;

	if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1))
		return 0;

179
	virtio_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
180 181 182 183 184 185 186 187
	status = dev->config->get_status(dev);
	if (!(status & VIRTIO_CONFIG_S_FEATURES_OK)) {
		dev_err(&dev->dev, "virtio: device refuses features: %x\n",
			status);
		return -ENODEV;
	}
	return 0;
}
188
EXPORT_SYMBOL_GPL(virtio_finalize_features);
189

R
Rusty Russell 已提交
190 191
static int virtio_dev_probe(struct device *_d)
{
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;
R
Rusty Russell 已提交
198

199
	/* We have a driver! */
200
	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
201 202 203 204

	/* Figure out what features the device supports. */
	device_features = dev->config->get_features(dev);

205 206
	/* Figure out what features the driver supports. */
	driver_features = 0;
207 208
	for (i = 0; i < drv->feature_table_size; i++) {
		unsigned int f = drv->feature_table[i];
209
		BUG_ON(f >= 64);
210
		driver_features |= (1ULL << f);
211 212
	}

213 214 215 216 217 218 219 220 221 222 223 224
	/* Some drivers have a separate feature table for virtio v1.0 */
	if (drv->feature_table_legacy) {
		driver_features_legacy = 0;
		for (i = 0; i < drv->feature_table_size_legacy; i++) {
			unsigned int f = drv->feature_table_legacy[i];
			BUG_ON(f >= 64);
			driver_features_legacy |= (1ULL << f);
		}
	} else {
		driver_features_legacy = driver_features;
	}

225
	if (device_features & (1ULL << VIRTIO_F_VERSION_1))
226 227 228
		dev->features = driver_features & device_features;
	else
		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 236 237 238 239 240
	if (drv->validate) {
		err = drv->validate(dev);
		if (err)
			goto err;
	}

241
	err = virtio_finalize_features(dev);
242 243
	if (err)
		goto err;
244

R
Rusty Russell 已提交
245 246
	err = drv->probe(dev);
	if (err)
M
Michael S. Tsirkin 已提交
247
		goto err;
248

249 250 251 252
	/* If probe didn't do it, mark device DRIVER_OK ourselves. */
	if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK))
		virtio_device_ready(dev);

M
Michael S. Tsirkin 已提交
253 254
	if (drv->scan)
		drv->scan(dev);
255

M
Michael S. Tsirkin 已提交
256 257 258 259
	virtio_config_enable(dev);

	return 0;
err:
260
	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
R
Rusty Russell 已提交
261
	return err;
M
Michael S. Tsirkin 已提交
262

R
Rusty Russell 已提交
263 264
}

265 266
static int virtio_dev_remove(struct device *_d)
{
267
	struct virtio_device *dev = dev_to_virtio(_d);
268
	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
269

270 271
	virtio_config_disable(dev);

272
	drv->remove(dev);
R
Rusty Russell 已提交
273 274

	/* Driver should have reset device. */
275
	WARN_ON_ONCE(dev->config->get_status(dev));
R
Rusty Russell 已提交
276 277

	/* Acknowledge the device's existence again. */
278
	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
279 280 281
	return 0;
}

282 283 284
static struct bus_type virtio_bus = {
	.name  = "virtio",
	.match = virtio_dev_match,
285
	.dev_groups = virtio_dev_groups,
286 287 288 289 290
	.uevent = virtio_uevent,
	.probe = virtio_dev_probe,
	.remove = virtio_dev_remove,
};

R
Rusty Russell 已提交
291 292
int register_virtio_driver(struct virtio_driver *driver)
{
293 294
	/* Catch this early. */
	BUG_ON(driver->feature_table_size && !driver->feature_table);
R
Rusty Russell 已提交
295 296 297 298 299 300 301 302 303 304 305
	driver->driver.bus = &virtio_bus;
	return driver_register(&driver->driver);
}
EXPORT_SYMBOL_GPL(register_virtio_driver);

void unregister_virtio_driver(struct virtio_driver *driver)
{
	driver_unregister(&driver->driver);
}
EXPORT_SYMBOL_GPL(unregister_virtio_driver);

306 307 308 309 310 311 312 313 314
/**
 * register_virtio_device - register virtio device
 * @dev        : virtio device to be registered
 *
 * On error, the caller must call put_device on &@dev->dev (and not kfree),
 * as another code path may have obtained a reference to @dev.
 *
 * Returns: 0 on suceess, -error on failure
 */
R
Rusty Russell 已提交
315 316 317 318 319
int register_virtio_device(struct virtio_device *dev)
{
	int err;

	dev->dev.bus = &virtio_bus;
320
	device_initialize(&dev->dev);
321 322

	/* Assign a unique device index and hence name. */
323 324 325 326 327
	err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL);
	if (err < 0)
		goto out;

	dev->index = err;
328
	dev_set_name(&dev->dev, "virtio%u", dev->index);
R
Rusty Russell 已提交
329

330 331 332 333
	spin_lock_init(&dev->config_lock);
	dev->config_enabled = false;
	dev->config_change_pending = false;

R
Rusty Russell 已提交
334 335 336 337
	/* We always start by resetting the device, in case a previous
	 * driver messed it up.  This also tests that code path a little. */
	dev->config->reset(dev);

R
Rusty Russell 已提交
338
	/* Acknowledge that we've seen the device. */
339
	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
R
Rusty Russell 已提交
340

341 342
	INIT_LIST_HEAD(&dev->vqs);

343 344 345 346 347
	/*
	 * device_add() causes the bus infrastructure to look for a matching
	 * driver.
	 */
	err = device_add(&dev->dev);
348 349
	if (err)
		ida_simple_remove(&virtio_index_ida, dev->index);
350
out:
R
Rusty Russell 已提交
351
	if (err)
352
		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
R
Rusty Russell 已提交
353 354 355 356 357 358
	return err;
}
EXPORT_SYMBOL_GPL(register_virtio_device);

void unregister_virtio_device(struct virtio_device *dev)
{
359 360
	int index = dev->index; /* save for after device release */

R
Rusty Russell 已提交
361
	device_unregister(&dev->dev);
362
	ida_simple_remove(&virtio_index_ida, index);
R
Rusty Russell 已提交
363 364 365
}
EXPORT_SYMBOL_GPL(unregister_virtio_device);

366 367 368 369 370
#ifdef CONFIG_PM_SLEEP
int virtio_device_freeze(struct virtio_device *dev)
{
	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);

371 372
	virtio_config_disable(dev);

373 374 375 376 377 378 379 380 381 382 383 384
	dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED;

	if (drv && drv->freeze)
		return drv->freeze(dev);

	return 0;
}
EXPORT_SYMBOL_GPL(virtio_device_freeze);

int virtio_device_restore(struct virtio_device *dev)
{
	struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
385
	int ret;
386 387 388 389 390 391

	/* We always start by resetting the device, in case a previous
	 * driver messed it up. */
	dev->config->reset(dev);

	/* Acknowledge that we've seen the device. */
392
	virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
393 394 395 396

	/* Maybe driver failed before freeze.
	 * Restore the failed status, for debugging. */
	if (dev->failed)
397
		virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
398 399 400 401 402

	if (!drv)
		return 0;

	/* We have a driver! */
403
	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER);
404

405
	ret = virtio_finalize_features(dev);
406 407
	if (ret)
		goto err;
408 409

	if (drv->restore) {
410 411 412
		ret = drv->restore(dev);
		if (ret)
			goto err;
413 414 415
	}

	/* Finally, tell the device we're all set */
416
	virtio_add_status(dev, VIRTIO_CONFIG_S_DRIVER_OK);
417

418 419
	virtio_config_enable(dev);

420
	return 0;
421 422

err:
423
	virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);
424
	return ret;
425 426 427 428
}
EXPORT_SYMBOL_GPL(virtio_device_restore);
#endif

R
Rusty Russell 已提交
429 430 431 432 433 434
static int virtio_init(void)
{
	if (bus_register(&virtio_bus) != 0)
		panic("virtio bus registration failed");
	return 0;
}
435 436 437 438

static void __exit virtio_exit(void)
{
	bus_unregister(&virtio_bus);
439
	ida_destroy(&virtio_index_ida);
440
}
R
Rusty Russell 已提交
441
core_initcall(virtio_init);
442 443 444
module_exit(virtio_exit);

MODULE_LICENSE("GPL");