vdpa.c 29.5 KB
Newer Older
T
Tiwei Bie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018-2020 Intel Corporation.
 * Copyright (C) 2020 Red Hat, Inc.
 *
 * Author: Tiwei Bie <tiwei.bie@intel.com>
 *         Jason Wang <jasowang@redhat.com>
 *
 * Thanks Michael S. Tsirkin for the valuable comments and
 * suggestions.  And thanks to Cunming Liang and Zhihong Wang for all
 * their supports.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
18
#include <linux/mm.h>
19
#include <linux/slab.h>
T
Tiwei Bie 已提交
20 21 22 23 24 25 26 27
#include <linux/iommu.h>
#include <linux/uuid.h>
#include <linux/vdpa.h>
#include <linux/nospec.h>
#include <linux/vhost.h>

#include "vhost.h"

28
enum {
29 30 31
	VHOST_VDPA_BACKEND_FEATURES =
	(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2) |
	(1ULL << VHOST_BACKEND_F_IOTLB_BATCH),
32 33
};

T
Tiwei Bie 已提交
34 35
#define VHOST_VDPA_DEV_MAX (1U << MINORBITS)

36 37 38 39 40 41 42 43
#define VHOST_VDPA_IOTLB_BUCKETS 16

struct vhost_vdpa_as {
	struct hlist_node hash_link;
	struct vhost_iotlb iotlb;
	u32 id;
};

T
Tiwei Bie 已提交
44 45 46 47 48 49
struct vhost_vdpa {
	struct vhost_dev vdev;
	struct iommu_domain *domain;
	struct vhost_virtqueue *vqs;
	struct completion completion;
	struct vdpa_device *vdpa;
50
	struct hlist_head as[VHOST_VDPA_IOTLB_BUCKETS];
T
Tiwei Bie 已提交
51 52 53
	struct device dev;
	struct cdev cdev;
	atomic_t opened;
L
Longpeng 已提交
54
	u32 nvqs;
T
Tiwei Bie 已提交
55 56
	int virtio_id;
	int minor;
57
	struct eventfd_ctx *config_ctx;
58
	int in_batch;
J
Jason Wang 已提交
59
	struct vdpa_iova_range range;
T
Tiwei Bie 已提交
60 61 62 63 64 65
};

static DEFINE_IDA(vhost_vdpa_ida);

static dev_t vhost_vdpa_major;

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
static struct vhost_vdpa_as *asid_to_as(struct vhost_vdpa *v, u32 asid)
{
	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
	struct vhost_vdpa_as *as;

	hlist_for_each_entry(as, head, hash_link)
		if (as->id == asid)
			return as;

	return NULL;
}

static struct vhost_vdpa_as *vhost_vdpa_alloc_as(struct vhost_vdpa *v, u32 asid)
{
	struct hlist_head *head = &v->as[asid % VHOST_VDPA_IOTLB_BUCKETS];
	struct vhost_vdpa_as *as;

	if (asid_to_as(v, asid))
		return NULL;

	as = kmalloc(sizeof(*as), GFP_KERNEL);
	if (!as)
		return NULL;

	vhost_iotlb_init(&as->iotlb, 0, 0);
	as->id = asid;
	hlist_add_head(&as->hash_link, head);

	return as;
}

static int vhost_vdpa_remove_as(struct vhost_vdpa *v, u32 asid)
{
	struct vhost_vdpa_as *as = asid_to_as(v, asid);

	if (!as)
		return -EINVAL;

	hlist_del(&as->hash_link);
	vhost_iotlb_reset(&as->iotlb);
	kfree(as);

	return 0;
}

T
Tiwei Bie 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123
static void handle_vq_kick(struct vhost_work *work)
{
	struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
						  poll.work);
	struct vhost_vdpa *v = container_of(vq->dev, struct vhost_vdpa, vdev);
	const struct vdpa_config_ops *ops = v->vdpa->config;

	ops->kick_vq(v->vdpa, vq - v->vqs);
}

static irqreturn_t vhost_vdpa_virtqueue_cb(void *private)
{
	struct vhost_virtqueue *vq = private;
Z
Zhu Lingshan 已提交
124
	struct eventfd_ctx *call_ctx = vq->call_ctx.ctx;
T
Tiwei Bie 已提交
125 126 127 128 129 130 131

	if (call_ctx)
		eventfd_signal(call_ctx, 1);

	return IRQ_HANDLED;
}

132 133 134 135 136 137 138 139 140 141 142
static irqreturn_t vhost_vdpa_config_cb(void *private)
{
	struct vhost_vdpa *v = private;
	struct eventfd_ctx *config_ctx = v->config_ctx;

	if (config_ctx)
		eventfd_signal(config_ctx, 1);

	return IRQ_HANDLED;
}

143 144 145 146 147 148 149 150 151 152 153
static void vhost_vdpa_setup_vq_irq(struct vhost_vdpa *v, u16 qid)
{
	struct vhost_virtqueue *vq = &v->vqs[qid];
	const struct vdpa_config_ops *ops = v->vdpa->config;
	struct vdpa_device *vdpa = v->vdpa;
	int ret, irq;

	if (!ops->get_vq_irq)
		return;

	irq = ops->get_vq_irq(vdpa, qid);
154 155 156
	if (irq < 0)
		return;

157
	irq_bypass_unregister_producer(&vq->call_ctx.producer);
158
	if (!vq->call_ctx.ctx)
159 160 161 162 163
		return;

	vq->call_ctx.producer.token = vq->call_ctx.ctx;
	vq->call_ctx.producer.irq = irq;
	ret = irq_bypass_register_producer(&vq->call_ctx.producer);
164 165 166
	if (unlikely(ret))
		dev_info(&v->dev, "vq %u, irq bypass producer (token %p) registration fails, ret =  %d\n",
			 qid, vq->call_ctx.producer.token, ret);
167 168 169 170 171 172 173 174 175
}

static void vhost_vdpa_unsetup_vq_irq(struct vhost_vdpa *v, u16 qid)
{
	struct vhost_virtqueue *vq = &v->vqs[qid];

	irq_bypass_unregister_producer(&vq->call_ctx.producer);
}

176
static int vhost_vdpa_reset(struct vhost_vdpa *v)
T
Tiwei Bie 已提交
177 178 179
{
	struct vdpa_device *vdpa = v->vdpa;

180
	v->in_batch = 0;
181 182

	return vdpa_reset(vdpa);
T
Tiwei Bie 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

static long vhost_vdpa_get_device_id(struct vhost_vdpa *v, u8 __user *argp)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	u32 device_id;

	device_id = ops->get_device_id(vdpa);

	if (copy_to_user(argp, &device_id, sizeof(device_id)))
		return -EFAULT;

	return 0;
}

static long vhost_vdpa_get_status(struct vhost_vdpa *v, u8 __user *statusp)
{
	struct vdpa_device *vdpa = v->vdpa;
202
	const struct vdpa_config_ops *ops = vdpa->config;
T
Tiwei Bie 已提交
203 204
	u8 status;

205
	status = ops->get_status(vdpa);
T
Tiwei Bie 已提交
206 207 208 209 210 211 212 213 214 215 216

	if (copy_to_user(statusp, &status, sizeof(status)))
		return -EFAULT;

	return 0;
}

static long vhost_vdpa_set_status(struct vhost_vdpa *v, u8 __user *statusp)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
217
	u8 status, status_old;
L
Longpeng 已提交
218 219
	u32 nvqs = v->nvqs;
	int ret;
220
	u16 i;
T
Tiwei Bie 已提交
221 222 223 224

	if (copy_from_user(&status, statusp, sizeof(status)))
		return -EFAULT;

225
	status_old = ops->get_status(vdpa);
226

T
Tiwei Bie 已提交
227 228 229 230
	/*
	 * Userspace shouldn't remove status bits unless reset the
	 * status to 0.
	 */
231
	if (status != 0 && (status_old & ~status) != 0)
T
Tiwei Bie 已提交
232 233
		return -EINVAL;

234 235 236 237
	if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) && !(status & VIRTIO_CONFIG_S_DRIVER_OK))
		for (i = 0; i < nvqs; i++)
			vhost_vdpa_unsetup_vq_irq(v, i);

238
	if (status == 0) {
239
		ret = vdpa_reset(vdpa);
240 241 242
		if (ret)
			return ret;
	} else
243
		vdpa_set_status(vdpa, status);
T
Tiwei Bie 已提交
244

245 246 247 248
	if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && !(status_old & VIRTIO_CONFIG_S_DRIVER_OK))
		for (i = 0; i < nvqs; i++)
			vhost_vdpa_setup_vq_irq(v, i);

T
Tiwei Bie 已提交
249 250 251 252 253 254
	return 0;
}

static int vhost_vdpa_config_validate(struct vhost_vdpa *v,
				      struct vhost_vdpa_config *c)
{
255
	struct vdpa_device *vdpa = v->vdpa;
256
	size_t size = vdpa->config->get_config_size(vdpa);
T
Tiwei Bie 已提交
257

258
	if (c->len == 0 || c->off > size)
T
Tiwei Bie 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		return -EINVAL;

	if (c->len > size - c->off)
		return -E2BIG;

	return 0;
}

static long vhost_vdpa_get_config(struct vhost_vdpa *v,
				  struct vhost_vdpa_config __user *c)
{
	struct vdpa_device *vdpa = v->vdpa;
	struct vhost_vdpa_config config;
	unsigned long size = offsetof(struct vhost_vdpa_config, buf);
	u8 *buf;

	if (copy_from_user(&config, c, size))
		return -EFAULT;
	if (vhost_vdpa_config_validate(v, &config))
		return -EINVAL;
	buf = kvzalloc(config.len, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

283
	vdpa_get_config(vdpa, config.off, buf, config.len);
T
Tiwei Bie 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

	if (copy_to_user(c->buf, buf, config.len)) {
		kvfree(buf);
		return -EFAULT;
	}

	kvfree(buf);
	return 0;
}

static long vhost_vdpa_set_config(struct vhost_vdpa *v,
				  struct vhost_vdpa_config __user *c)
{
	struct vdpa_device *vdpa = v->vdpa;
	struct vhost_vdpa_config config;
	unsigned long size = offsetof(struct vhost_vdpa_config, buf);
	u8 *buf;

	if (copy_from_user(&config, c, size))
		return -EFAULT;
	if (vhost_vdpa_config_validate(v, &config))
		return -EINVAL;

T
Tian Tao 已提交
307 308 309
	buf = vmemdup_user(c->buf, config.len);
	if (IS_ERR(buf))
		return PTR_ERR(buf);
T
Tiwei Bie 已提交
310

311
	vdpa_set_config(vdpa, config.off, buf, config.len);
T
Tiwei Bie 已提交
312 313 314 315 316 317 318 319 320 321 322

	kvfree(buf);
	return 0;
}

static long vhost_vdpa_get_features(struct vhost_vdpa *v, u64 __user *featurep)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	u64 features;

323
	features = ops->get_device_features(vdpa);
T
Tiwei Bie 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346

	if (copy_to_user(featurep, &features, sizeof(features)))
		return -EFAULT;

	return 0;
}

static long vhost_vdpa_set_features(struct vhost_vdpa *v, u64 __user *featurep)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	u64 features;

	/*
	 * It's not allowed to change the features after they have
	 * been negotiated.
	 */
	if (ops->get_status(vdpa) & VIRTIO_CONFIG_S_FEATURES_OK)
		return -EBUSY;

	if (copy_from_user(&features, featurep, sizeof(features)))
		return -EFAULT;

347
	if (vdpa_set_features(vdpa, features))
T
Tiwei Bie 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		return -EINVAL;

	return 0;
}

static long vhost_vdpa_get_vring_num(struct vhost_vdpa *v, u16 __user *argp)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	u16 num;

	num = ops->get_vq_num_max(vdpa);

	if (copy_to_user(argp, &num, sizeof(num)))
		return -EFAULT;

	return 0;
}

367 368
static void vhost_vdpa_config_put(struct vhost_vdpa *v)
{
369
	if (v->config_ctx) {
370
		eventfd_ctx_put(v->config_ctx);
371 372
		v->config_ctx = NULL;
	}
373 374 375 376 377 378 379 380 381
}

static long vhost_vdpa_set_config_call(struct vhost_vdpa *v, u32 __user *argp)
{
	struct vdpa_callback cb;
	int fd;
	struct eventfd_ctx *ctx;

	cb.callback = vhost_vdpa_config_cb;
382
	cb.private = v;
383 384 385 386 387 388 389 390 391
	if (copy_from_user(&fd, argp, sizeof(fd)))
		return  -EFAULT;

	ctx = fd == VHOST_FILE_UNBIND ? NULL : eventfd_ctx_fdget(fd);
	swap(ctx, v->config_ctx);

	if (!IS_ERR_OR_NULL(ctx))
		eventfd_ctx_put(ctx);

392 393 394 395 396 397
	if (IS_ERR(v->config_ctx)) {
		long ret = PTR_ERR(v->config_ctx);

		v->config_ctx = NULL;
		return ret;
	}
398 399 400 401 402

	v->vdpa->config->set_config_cb(v->vdpa, &cb);

	return 0;
}
403

J
Jason Wang 已提交
404 405 406 407 408 409 410
static long vhost_vdpa_get_iova_range(struct vhost_vdpa *v, u32 __user *argp)
{
	struct vhost_vdpa_iova_range range = {
		.first = v->range.first,
		.last = v->range.last,
	};

411 412 413
	if (copy_to_user(argp, &range, sizeof(range)))
		return -EFAULT;
	return 0;
J
Jason Wang 已提交
414 415
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429
static long vhost_vdpa_get_config_size(struct vhost_vdpa *v, u32 __user *argp)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	u32 size;

	size = ops->get_config_size(vdpa);

	if (copy_to_user(argp, &size, sizeof(size)))
		return -EFAULT;

	return 0;
}

430 431 432 433 434 435 436 437 438 439
static long vhost_vdpa_get_vqs_count(struct vhost_vdpa *v, u32 __user *argp)
{
	struct vdpa_device *vdpa = v->vdpa;

	if (copy_to_user(argp, &vdpa->nvqs, sizeof(vdpa->nvqs)))
		return -EFAULT;

	return 0;
}

T
Tiwei Bie 已提交
440 441 442 443 444
static long vhost_vdpa_vring_ioctl(struct vhost_vdpa *v, unsigned int cmd,
				   void __user *argp)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
445
	struct vdpa_vq_state vq_state;
T
Tiwei Bie 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	struct vdpa_callback cb;
	struct vhost_virtqueue *vq;
	struct vhost_vring_state s;
	u32 idx;
	long r;

	r = get_user(idx, (u32 __user *)argp);
	if (r < 0)
		return r;

	if (idx >= v->nvqs)
		return -ENOBUFS;

	idx = array_index_nospec(idx, v->nvqs);
	vq = &v->vqs[idx];

462 463
	switch (cmd) {
	case VHOST_VDPA_SET_VRING_ENABLE:
T
Tiwei Bie 已提交
464 465 466 467
		if (copy_from_user(&s, argp, sizeof(s)))
			return -EFAULT;
		ops->set_vq_ready(vdpa, idx, s.num);
		return 0;
468
	case VHOST_GET_VRING_BASE:
469 470 471 472
		r = ops->get_vq_state(v->vdpa, idx, &vq_state);
		if (r)
			return r;

473
		vq->last_avail_idx = vq_state.split.avail_index;
474 475
		break;
	}
T
Tiwei Bie 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

	r = vhost_vring_ioctl(&v->vdev, cmd, argp);
	if (r)
		return r;

	switch (cmd) {
	case VHOST_SET_VRING_ADDR:
		if (ops->set_vq_address(vdpa, idx,
					(u64)(uintptr_t)vq->desc,
					(u64)(uintptr_t)vq->avail,
					(u64)(uintptr_t)vq->used))
			r = -EINVAL;
		break;

	case VHOST_SET_VRING_BASE:
491
		vq_state.split.avail_index = vq->last_avail_idx;
492
		if (ops->set_vq_state(vdpa, idx, &vq_state))
T
Tiwei Bie 已提交
493 494 495 496
			r = -EINVAL;
		break;

	case VHOST_SET_VRING_CALL:
Z
Zhu Lingshan 已提交
497
		if (vq->call_ctx.ctx) {
T
Tiwei Bie 已提交
498 499 500 501 502 503 504
			cb.callback = vhost_vdpa_virtqueue_cb;
			cb.private = vq;
		} else {
			cb.callback = NULL;
			cb.private = NULL;
		}
		ops->set_vq_cb(vdpa, idx, &cb);
505
		vhost_vdpa_setup_vq_irq(v, idx);
T
Tiwei Bie 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
		break;

	case VHOST_SET_VRING_NUM:
		ops->set_vq_num(vdpa, idx, vq->num);
		break;
	}

	return r;
}

static long vhost_vdpa_unlocked_ioctl(struct file *filep,
				      unsigned int cmd, unsigned long arg)
{
	struct vhost_vdpa *v = filep->private_data;
	struct vhost_dev *d = &v->vdev;
	void __user *argp = (void __user *)arg;
522 523
	u64 __user *featurep = argp;
	u64 features;
524
	long r = 0;
T
Tiwei Bie 已提交
525

526
	if (cmd == VHOST_SET_BACKEND_FEATURES) {
527 528
		if (copy_from_user(&features, featurep, sizeof(features)))
			return -EFAULT;
529 530 531 532 533 534
		if (features & ~VHOST_VDPA_BACKEND_FEATURES)
			return -EOPNOTSUPP;
		vhost_set_backend_features(&v->vdev, features);
		return 0;
	}

T
Tiwei Bie 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	mutex_lock(&d->mutex);

	switch (cmd) {
	case VHOST_VDPA_GET_DEVICE_ID:
		r = vhost_vdpa_get_device_id(v, argp);
		break;
	case VHOST_VDPA_GET_STATUS:
		r = vhost_vdpa_get_status(v, argp);
		break;
	case VHOST_VDPA_SET_STATUS:
		r = vhost_vdpa_set_status(v, argp);
		break;
	case VHOST_VDPA_GET_CONFIG:
		r = vhost_vdpa_get_config(v, argp);
		break;
	case VHOST_VDPA_SET_CONFIG:
		r = vhost_vdpa_set_config(v, argp);
		break;
	case VHOST_GET_FEATURES:
		r = vhost_vdpa_get_features(v, argp);
		break;
	case VHOST_SET_FEATURES:
		r = vhost_vdpa_set_features(v, argp);
		break;
	case VHOST_VDPA_GET_VRING_NUM:
		r = vhost_vdpa_get_vring_num(v, argp);
		break;
562 563 564 565
	case VHOST_VDPA_GET_GROUP_NUM:
		r = copy_to_user(argp, &v->vdpa->ngroups,
				 sizeof(v->vdpa->ngroups));
		break;
T
Tiwei Bie 已提交
566 567 568 569
	case VHOST_SET_LOG_BASE:
	case VHOST_SET_LOG_FD:
		r = -ENOIOCTLCMD;
		break;
570 571 572
	case VHOST_VDPA_SET_CONFIG_CALL:
		r = vhost_vdpa_set_config_call(v, argp);
		break;
573 574
	case VHOST_GET_BACKEND_FEATURES:
		features = VHOST_VDPA_BACKEND_FEATURES;
575 576
		if (copy_to_user(featurep, &features, sizeof(features)))
			r = -EFAULT;
577
		break;
J
Jason Wang 已提交
578 579 580
	case VHOST_VDPA_GET_IOVA_RANGE:
		r = vhost_vdpa_get_iova_range(v, argp);
		break;
581 582 583
	case VHOST_VDPA_GET_CONFIG_SIZE:
		r = vhost_vdpa_get_config_size(v, argp);
		break;
584 585 586
	case VHOST_VDPA_GET_VQS_COUNT:
		r = vhost_vdpa_get_vqs_count(v, argp);
		break;
T
Tiwei Bie 已提交
587 588 589 590 591 592 593 594 595 596 597
	default:
		r = vhost_dev_ioctl(&v->vdev, cmd, argp);
		if (r == -ENOIOCTLCMD)
			r = vhost_vdpa_vring_ioctl(v, cmd, argp);
		break;
	}

	mutex_unlock(&d->mutex);
	return r;
}

598 599 600
static void vhost_vdpa_pa_unmap(struct vhost_vdpa *v,
				struct vhost_iotlb *iotlb,
				u64 start, u64 last)
T
Tiwei Bie 已提交
601 602 603 604 605 606 607
{
	struct vhost_dev *dev = &v->vdev;
	struct vhost_iotlb_map *map;
	struct page *page;
	unsigned long pfn, pinned;

	while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
608 609
		pinned = PFN_DOWN(map->size);
		for (pfn = PFN_DOWN(map->addr);
T
Tiwei Bie 已提交
610 611 612 613 614 615
		     pinned > 0; pfn++, pinned--) {
			page = pfn_to_page(pfn);
			if (map->perm & VHOST_ACCESS_WO)
				set_page_dirty_lock(page);
			unpin_user_page(page);
		}
616
		atomic64_sub(PFN_DOWN(map->size), &dev->mm->pinned_vm);
T
Tiwei Bie 已提交
617 618 619 620
		vhost_iotlb_map_free(iotlb, map);
	}
}

621 622 623
static void vhost_vdpa_va_unmap(struct vhost_vdpa *v,
				struct vhost_iotlb *iotlb,
				u64 start, u64 last)
624 625 626 627 628 629 630 631 632 633 634 635
{
	struct vhost_iotlb_map *map;
	struct vdpa_map_file *map_file;

	while ((map = vhost_iotlb_itree_first(iotlb, start, last)) != NULL) {
		map_file = (struct vdpa_map_file *)map->opaque;
		fput(map_file->file);
		kfree(map_file);
		vhost_iotlb_map_free(iotlb, map);
	}
}

636 637 638
static void vhost_vdpa_iotlb_unmap(struct vhost_vdpa *v,
				   struct vhost_iotlb *iotlb,
				   u64 start, u64 last)
639
{
640 641 642
	struct vdpa_device *vdpa = v->vdpa;

	if (vdpa->use_va)
643
		return vhost_vdpa_va_unmap(v, iotlb, start, last);
644

645
	return vhost_vdpa_pa_unmap(v, iotlb, start, last);
646 647
}

T
Tiwei Bie 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
static int perm_to_iommu_flags(u32 perm)
{
	int flags = 0;

	switch (perm) {
	case VHOST_ACCESS_WO:
		flags |= IOMMU_WRITE;
		break;
	case VHOST_ACCESS_RO:
		flags |= IOMMU_READ;
		break;
	case VHOST_ACCESS_RW:
		flags |= (IOMMU_WRITE | IOMMU_READ);
		break;
	default:
		WARN(1, "invalidate vhost IOTLB permission\n");
		break;
	}

	return flags | IOMMU_CACHE;
}

670 671
static int vhost_vdpa_map(struct vhost_vdpa *v, struct vhost_iotlb *iotlb,
			  u64 iova, u64 size, u64 pa, u32 perm, void *opaque)
T
Tiwei Bie 已提交
672 673 674 675 676 677
{
	struct vhost_dev *dev = &v->vdev;
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	int r = 0;

678
	r = vhost_iotlb_add_range_ctx(iotlb, iova, iova + size - 1,
679
				      pa, perm, opaque);
T
Tiwei Bie 已提交
680 681 682
	if (r)
		return r;

683
	if (ops->dma_map) {
684
		r = ops->dma_map(vdpa, 0, iova, size, pa, perm, opaque);
685 686
	} else if (ops->set_map) {
		if (!v->in_batch)
687
			r = ops->set_map(vdpa, 0, iotlb);
688
	} else {
T
Tiwei Bie 已提交
689 690
		r = iommu_map(v->domain, iova, pa, size,
			      perm_to_iommu_flags(perm));
691
	}
692
	if (r) {
693
		vhost_iotlb_del_range(iotlb, iova, iova + size - 1);
694 695 696 697
		return r;
	}

	if (!vdpa->use_va)
698
		atomic64_add(PFN_DOWN(size), &dev->mm->pinned_vm);
699

700
	return 0;
T
Tiwei Bie 已提交
701 702
}

703 704 705
static void vhost_vdpa_unmap(struct vhost_vdpa *v,
			     struct vhost_iotlb *iotlb,
			     u64 iova, u64 size)
T
Tiwei Bie 已提交
706 707 708 709
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;

710
	vhost_vdpa_iotlb_unmap(v, iotlb, iova, iova + size - 1);
T
Tiwei Bie 已提交
711

712
	if (ops->dma_map) {
713
		ops->dma_unmap(vdpa, 0, iova, size);
714 715
	} else if (ops->set_map) {
		if (!v->in_batch)
716
			ops->set_map(vdpa, 0, iotlb);
717
	} else {
T
Tiwei Bie 已提交
718
		iommu_unmap(v->domain, iova, size);
719
	}
T
Tiwei Bie 已提交
720 721
}

722
static int vhost_vdpa_va_map(struct vhost_vdpa *v,
723
			     struct vhost_iotlb *iotlb,
724 725 726 727 728 729
			     u64 iova, u64 size, u64 uaddr, u32 perm)
{
	struct vhost_dev *dev = &v->vdev;
	u64 offset, map_size, map_iova = iova;
	struct vdpa_map_file *map_file;
	struct vm_area_struct *vma;
730
	int ret = 0;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752

	mmap_read_lock(dev->mm);

	while (size) {
		vma = find_vma(dev->mm, uaddr);
		if (!vma) {
			ret = -EINVAL;
			break;
		}
		map_size = min(size, vma->vm_end - uaddr);
		if (!(vma->vm_file && (vma->vm_flags & VM_SHARED) &&
			!(vma->vm_flags & (VM_IO | VM_PFNMAP))))
			goto next;

		map_file = kzalloc(sizeof(*map_file), GFP_KERNEL);
		if (!map_file) {
			ret = -ENOMEM;
			break;
		}
		offset = (vma->vm_pgoff << PAGE_SHIFT) + uaddr - vma->vm_start;
		map_file->offset = offset;
		map_file->file = get_file(vma->vm_file);
753
		ret = vhost_vdpa_map(v, iotlb, map_iova, map_size, uaddr,
754 755 756 757 758 759 760 761 762 763 764 765
				     perm, map_file);
		if (ret) {
			fput(map_file->file);
			kfree(map_file);
			break;
		}
next:
		size -= map_size;
		uaddr += map_size;
		map_iova += map_size;
	}
	if (ret)
766
		vhost_vdpa_unmap(v, iotlb, iova, map_iova - iova);
767 768 769 770 771 772

	mmap_read_unlock(dev->mm);

	return ret;
}

773
static int vhost_vdpa_pa_map(struct vhost_vdpa *v,
774
			     struct vhost_iotlb *iotlb,
775
			     u64 iova, u64 size, u64 uaddr, u32 perm)
T
Tiwei Bie 已提交
776 777 778
{
	struct vhost_dev *dev = &v->vdev;
	struct page **page_list;
779
	unsigned long list_size = PAGE_SIZE / sizeof(struct page *);
T
Tiwei Bie 已提交
780
	unsigned int gup_flags = FOLL_LONGTERM;
781
	unsigned long npages, cur_base, map_pfn, last_pfn = 0;
782
	unsigned long lock_limit, sz2pin, nchunks, i;
783
	u64 start = iova;
784
	long pinned;
T
Tiwei Bie 已提交
785 786
	int ret = 0;

787
	/* Limit the use of memory for bookkeeping */
788 789 790 791
	page_list = (struct page **) __get_free_page(GFP_KERNEL);
	if (!page_list)
		return -ENOMEM;

792
	if (perm & VHOST_ACCESS_WO)
T
Tiwei Bie 已提交
793 794
		gup_flags |= FOLL_WRITE;

795
	npages = PFN_UP(size + (iova & ~PAGE_MASK));
796 797 798 799
	if (!npages) {
		ret = -EINVAL;
		goto free;
	}
T
Tiwei Bie 已提交
800

801
	mmap_read_lock(dev->mm);
T
Tiwei Bie 已提交
802

803
	lock_limit = PFN_DOWN(rlimit(RLIMIT_MEMLOCK));
804
	if (npages + atomic64_read(&dev->mm->pinned_vm) > lock_limit) {
805
		ret = -ENOMEM;
806
		goto unlock;
807
	}
T
Tiwei Bie 已提交
808

809
	cur_base = uaddr & PAGE_MASK;
810
	iova &= PAGE_MASK;
811
	nchunks = 0;
812 813

	while (npages) {
814 815 816 817 818 819 820 821 822 823
		sz2pin = min_t(unsigned long, npages, list_size);
		pinned = pin_user_pages(cur_base, sz2pin,
					gup_flags, page_list, NULL);
		if (sz2pin != pinned) {
			if (pinned < 0) {
				ret = pinned;
			} else {
				unpin_user_pages(page_list, pinned);
				ret = -ENOMEM;
			}
824
			goto out;
825 826
		}
		nchunks++;
827 828 829 830

		if (!last_pfn)
			map_pfn = page_to_pfn(page_list[0]);

831
		for (i = 0; i < pinned; i++) {
832 833 834 835 836
			unsigned long this_pfn = page_to_pfn(page_list[i]);
			u64 csize;

			if (last_pfn && (this_pfn != last_pfn + 1)) {
				/* Pin a contiguous chunk of memory */
837
				csize = PFN_PHYS(last_pfn - map_pfn + 1);
838
				ret = vhost_vdpa_map(v, iotlb, iova, csize,
839
						     PFN_PHYS(map_pfn),
840
						     perm, NULL);
841 842 843 844 845 846 847 848 849 850 851
				if (ret) {
					/*
					 * Unpin the pages that are left unmapped
					 * from this point on in the current
					 * page_list. The remaining outstanding
					 * ones which may stride across several
					 * chunks will be covered in the common
					 * error path subsequently.
					 */
					unpin_user_pages(&page_list[i],
							 pinned - i);
852
					goto out;
853 854
				}

855 856
				map_pfn = this_pfn;
				iova += csize;
857
				nchunks = 0;
T
Tiwei Bie 已提交
858
			}
859 860

			last_pfn = this_pfn;
T
Tiwei Bie 已提交
861
		}
862

863
		cur_base += PFN_PHYS(pinned);
864
		npages -= pinned;
T
Tiwei Bie 已提交
865 866
	}

867
	/* Pin the rest chunk */
868
	ret = vhost_vdpa_map(v, iotlb, iova, PFN_PHYS(last_pfn - map_pfn + 1),
869
			     PFN_PHYS(map_pfn), perm, NULL);
T
Tiwei Bie 已提交
870
out:
871
	if (ret) {
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
		if (nchunks) {
			unsigned long pfn;

			/*
			 * Unpin the outstanding pages which are yet to be
			 * mapped but haven't due to vdpa_map() or
			 * pin_user_pages() failure.
			 *
			 * Mapped pages are accounted in vdpa_map(), hence
			 * the corresponding unpinning will be handled by
			 * vdpa_unmap().
			 */
			WARN_ON(!last_pfn);
			for (pfn = map_pfn; pfn <= last_pfn; pfn++)
				unpin_user_page(pfn_to_page(pfn));
		}
888
		vhost_vdpa_unmap(v, iotlb, start, size);
889
	}
890
unlock:
891
	mmap_read_unlock(dev->mm);
892
free:
893
	free_page((unsigned long)page_list);
T
Tiwei Bie 已提交
894
	return ret;
895 896 897 898

}

static int vhost_vdpa_process_iotlb_update(struct vhost_vdpa *v,
899
					   struct vhost_iotlb *iotlb,
900 901
					   struct vhost_iotlb_msg *msg)
{
902
	struct vdpa_device *vdpa = v->vdpa;
903 904 905 906 907 908 909 910 911 912

	if (msg->iova < v->range.first || !msg->size ||
	    msg->iova > U64_MAX - msg->size + 1 ||
	    msg->iova + msg->size - 1 > v->range.last)
		return -EINVAL;

	if (vhost_iotlb_itree_first(iotlb, msg->iova,
				    msg->iova + msg->size - 1))
		return -EEXIST;

913
	if (vdpa->use_va)
914
		return vhost_vdpa_va_map(v, iotlb, msg->iova, msg->size,
915 916
					 msg->uaddr, msg->perm);

917
	return vhost_vdpa_pa_map(v, iotlb, msg->iova, msg->size, msg->uaddr,
918
				 msg->perm);
T
Tiwei Bie 已提交
919 920
}

G
Gautam Dawar 已提交
921
static int vhost_vdpa_process_iotlb_msg(struct vhost_dev *dev, u32 asid,
T
Tiwei Bie 已提交
922 923 924
					struct vhost_iotlb_msg *msg)
{
	struct vhost_vdpa *v = container_of(dev, struct vhost_vdpa, vdev);
925 926
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
927 928
	struct vhost_vdpa_as *as = asid_to_as(v, 0);
	struct vhost_iotlb *iotlb = &as->iotlb;
T
Tiwei Bie 已提交
929 930
	int r = 0;

G
Gautam Dawar 已提交
931 932 933
	if (asid != 0)
		return -EINVAL;

934 935
	mutex_lock(&dev->mutex);

T
Tiwei Bie 已提交
936 937
	r = vhost_dev_check_owner(dev);
	if (r)
938
		goto unlock;
T
Tiwei Bie 已提交
939 940 941

	switch (msg->type) {
	case VHOST_IOTLB_UPDATE:
942
		r = vhost_vdpa_process_iotlb_update(v, iotlb, msg);
T
Tiwei Bie 已提交
943 944
		break;
	case VHOST_IOTLB_INVALIDATE:
945
		vhost_vdpa_unmap(v, iotlb, msg->iova, msg->size);
T
Tiwei Bie 已提交
946
		break;
947 948 949 950 951
	case VHOST_IOTLB_BATCH_BEGIN:
		v->in_batch = true;
		break;
	case VHOST_IOTLB_BATCH_END:
		if (v->in_batch && ops->set_map)
952
			ops->set_map(vdpa, 0, iotlb);
953 954
		v->in_batch = false;
		break;
T
Tiwei Bie 已提交
955 956 957 958
	default:
		r = -EINVAL;
		break;
	}
959 960
unlock:
	mutex_unlock(&dev->mutex);
T
Tiwei Bie 已提交
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	return r;
}

static ssize_t vhost_vdpa_chr_write_iter(struct kiocb *iocb,
					 struct iov_iter *from)
{
	struct file *file = iocb->ki_filp;
	struct vhost_vdpa *v = file->private_data;
	struct vhost_dev *dev = &v->vdev;

	return vhost_chr_write_iter(dev, from);
}

static int vhost_vdpa_alloc_domain(struct vhost_vdpa *v)
{
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	struct device *dma_dev = vdpa_get_dma_dev(vdpa);
	struct bus_type *bus;
	int ret;

	/* Device want to do DMA by itself */
	if (ops->set_map || ops->dma_map)
		return 0;

	bus = dma_dev->bus;
	if (!bus)
		return -EFAULT;

	if (!iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY))
		return -ENOTSUPP;

	v->domain = iommu_domain_alloc(bus);
	if (!v->domain)
		return -EIO;

	ret = iommu_attach_device(v->domain, dma_dev);
	if (ret)
		goto err_attach;

	return 0;

err_attach:
	iommu_domain_free(v->domain);
	return ret;
}

static void vhost_vdpa_free_domain(struct vhost_vdpa *v)
{
	struct vdpa_device *vdpa = v->vdpa;
	struct device *dma_dev = vdpa_get_dma_dev(vdpa);

	if (v->domain) {
		iommu_detach_device(v->domain, dma_dev);
		iommu_domain_free(v->domain);
	}

	v->domain = NULL;
}

J
Jason Wang 已提交
1022 1023 1024 1025 1026 1027 1028 1029
static void vhost_vdpa_set_iova_range(struct vhost_vdpa *v)
{
	struct vdpa_iova_range *range = &v->range;
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;

	if (ops->get_iova_range) {
		*range = ops->get_iova_range(vdpa);
1030 1031 1032
	} else if (v->domain && v->domain->geometry.force_aperture) {
		range->first = v->domain->geometry.aperture_start;
		range->last = v->domain->geometry.aperture_end;
J
Jason Wang 已提交
1033 1034 1035 1036 1037 1038
	} else {
		range->first = 0;
		range->last = ULLONG_MAX;
	}
}

1039 1040 1041 1042 1043 1044 1045
static void vhost_vdpa_cleanup(struct vhost_vdpa *v)
{
	vhost_dev_cleanup(&v->vdev);
	kfree(v->vdev.vqs);
	vhost_vdpa_remove_as(v, 0);
}

T
Tiwei Bie 已提交
1046 1047 1048 1049 1050
static int vhost_vdpa_open(struct inode *inode, struct file *filep)
{
	struct vhost_vdpa *v;
	struct vhost_dev *dev;
	struct vhost_virtqueue **vqs;
L
Longpeng 已提交
1051 1052
	int r, opened;
	u32 i, nvqs;
T
Tiwei Bie 已提交
1053 1054 1055 1056 1057 1058 1059 1060

	v = container_of(inode->i_cdev, struct vhost_vdpa, cdev);

	opened = atomic_cmpxchg(&v->opened, 0, 1);
	if (opened)
		return -EBUSY;

	nvqs = v->nvqs;
1061 1062 1063
	r = vhost_vdpa_reset(v);
	if (r)
		goto err;
T
Tiwei Bie 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075

	vqs = kmalloc_array(nvqs, sizeof(*vqs), GFP_KERNEL);
	if (!vqs) {
		r = -ENOMEM;
		goto err;
	}

	dev = &v->vdev;
	for (i = 0; i < nvqs; i++) {
		vqs[i] = &v->vqs[i];
		vqs[i]->handle_kick = handle_vq_kick;
	}
1076
	vhost_dev_init(dev, vqs, nvqs, 0, 0, 0, false,
T
Tiwei Bie 已提交
1077 1078
		       vhost_vdpa_process_iotlb_msg);

1079 1080
	if (!vhost_vdpa_alloc_as(v, 0))
		goto err_alloc_as;
T
Tiwei Bie 已提交
1081 1082 1083

	r = vhost_vdpa_alloc_domain(v);
	if (r)
1084
		goto err_alloc_as;
T
Tiwei Bie 已提交
1085

J
Jason Wang 已提交
1086 1087
	vhost_vdpa_set_iova_range(v);

T
Tiwei Bie 已提交
1088 1089 1090 1091
	filep->private_data = v;

	return 0;

1092 1093
err_alloc_as:
	vhost_vdpa_cleanup(v);
T
Tiwei Bie 已提交
1094 1095 1096 1097 1098
err:
	atomic_dec(&v->opened);
	return r;
}

1099 1100
static void vhost_vdpa_clean_irq(struct vhost_vdpa *v)
{
L
Longpeng 已提交
1101
	u32 i;
1102

1103 1104
	for (i = 0; i < v->nvqs; i++)
		vhost_vdpa_unsetup_vq_irq(v, i);
1105 1106
}

T
Tiwei Bie 已提交
1107 1108 1109 1110 1111 1112 1113
static int vhost_vdpa_release(struct inode *inode, struct file *filep)
{
	struct vhost_vdpa *v = filep->private_data;
	struct vhost_dev *d = &v->vdev;

	mutex_lock(&d->mutex);
	filep->private_data = NULL;
1114
	vhost_vdpa_clean_irq(v);
T
Tiwei Bie 已提交
1115 1116 1117
	vhost_vdpa_reset(v);
	vhost_dev_stop(&v->vdev);
	vhost_vdpa_free_domain(v);
1118
	vhost_vdpa_config_put(v);
T
Tiwei Bie 已提交
1119 1120 1121 1122 1123 1124 1125 1126 1127
	vhost_dev_cleanup(&v->vdev);
	mutex_unlock(&d->mutex);

	atomic_dec(&v->opened);
	complete(&v->completion);

	return 0;
}

1128
#ifdef CONFIG_MMU
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static vm_fault_t vhost_vdpa_fault(struct vm_fault *vmf)
{
	struct vhost_vdpa *v = vmf->vma->vm_file->private_data;
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	struct vdpa_notification_area notify;
	struct vm_area_struct *vma = vmf->vma;
	u16 index = vma->vm_pgoff;

	notify = ops->get_vq_notification(vdpa, index);

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	if (remap_pfn_range(vma, vmf->address & PAGE_MASK,
1142
			    PFN_DOWN(notify.addr), PAGE_SIZE,
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
			    vma->vm_page_prot))
		return VM_FAULT_SIGBUS;

	return VM_FAULT_NOPAGE;
}

static const struct vm_operations_struct vhost_vdpa_vm_ops = {
	.fault = vhost_vdpa_fault,
};

static int vhost_vdpa_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct vhost_vdpa *v = vma->vm_file->private_data;
	struct vdpa_device *vdpa = v->vdpa;
	const struct vdpa_config_ops *ops = vdpa->config;
	struct vdpa_notification_area notify;
1159
	unsigned long index = vma->vm_pgoff;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181

	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
		return -EINVAL;
	if ((vma->vm_flags & VM_SHARED) == 0)
		return -EINVAL;
	if (vma->vm_flags & VM_READ)
		return -EINVAL;
	if (index > 65535)
		return -EINVAL;
	if (!ops->get_vq_notification)
		return -ENOTSUPP;

	/* To be safe and easily modelled by userspace, We only
	 * support the doorbell which sits on the page boundary and
	 * does not share the page with other registers.
	 */
	notify = ops->get_vq_notification(vdpa, index);
	if (notify.addr & (PAGE_SIZE - 1))
		return -EINVAL;
	if (vma->vm_end - vma->vm_start != notify.size)
		return -ENOTSUPP;

1182
	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1183 1184 1185
	vma->vm_ops = &vhost_vdpa_vm_ops;
	return 0;
}
1186
#endif /* CONFIG_MMU */
1187

T
Tiwei Bie 已提交
1188 1189 1190 1191 1192 1193
static const struct file_operations vhost_vdpa_fops = {
	.owner		= THIS_MODULE,
	.open		= vhost_vdpa_open,
	.release	= vhost_vdpa_release,
	.write_iter	= vhost_vdpa_chr_write_iter,
	.unlocked_ioctl	= vhost_vdpa_unlocked_ioctl,
1194
#ifdef CONFIG_MMU
1195
	.mmap		= vhost_vdpa_mmap,
1196
#endif /* CONFIG_MMU */
T
Tiwei Bie 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
	.compat_ioctl	= compat_ptr_ioctl,
};

static void vhost_vdpa_release_dev(struct device *device)
{
	struct vhost_vdpa *v =
	       container_of(device, struct vhost_vdpa, dev);

	ida_simple_remove(&vhost_vdpa_ida, v->minor);
	kfree(v->vqs);
	kfree(v);
}

static int vhost_vdpa_probe(struct vdpa_device *vdpa)
{
	const struct vdpa_config_ops *ops = vdpa->config;
	struct vhost_vdpa *v;
M
Max Gurtovoy 已提交
1214
	int minor;
1215
	int i, r;
T
Tiwei Bie 已提交
1216

1217 1218 1219 1220
	/* Only support 1 address space and 1 groups */
	if (vdpa->ngroups != 1 || vdpa->nas != 1)
		return -EOPNOTSUPP;

T
Tiwei Bie 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	v = kzalloc(sizeof(*v), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
	if (!v)
		return -ENOMEM;

	minor = ida_simple_get(&vhost_vdpa_ida, 0,
			       VHOST_VDPA_DEV_MAX, GFP_KERNEL);
	if (minor < 0) {
		kfree(v);
		return minor;
	}

	atomic_set(&v->opened, 0);
	v->minor = minor;
	v->vdpa = vdpa;
M
Max Gurtovoy 已提交
1235
	v->nvqs = vdpa->nvqs;
T
Tiwei Bie 已提交
1236 1237 1238 1239 1240 1241
	v->virtio_id = ops->get_device_id(vdpa);

	device_initialize(&v->dev);
	v->dev.release = vhost_vdpa_release_dev;
	v->dev.parent = &vdpa->dev;
	v->dev.devt = MKDEV(MAJOR(vhost_vdpa_major), minor);
M
Max Gurtovoy 已提交
1242
	v->vqs = kmalloc_array(v->nvqs, sizeof(struct vhost_virtqueue),
T
Tiwei Bie 已提交
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
			       GFP_KERNEL);
	if (!v->vqs) {
		r = -ENOMEM;
		goto err;
	}

	r = dev_set_name(&v->dev, "vhost-vdpa-%u", minor);
	if (r)
		goto err;

	cdev_init(&v->cdev, &vhost_vdpa_fops);
	v->cdev.owner = THIS_MODULE;

	r = cdev_device_add(&v->cdev, &v->dev);
	if (r)
		goto err;

	init_completion(&v->completion);
	vdpa_set_drvdata(vdpa, v);

1263 1264 1265
	for (i = 0; i < VHOST_VDPA_IOTLB_BUCKETS; i++)
		INIT_HLIST_HEAD(&v->as[i]);

T
Tiwei Bie 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
	return 0;

err:
	put_device(&v->dev);
	return r;
}

static void vhost_vdpa_remove(struct vdpa_device *vdpa)
{
	struct vhost_vdpa *v = vdpa_get_drvdata(vdpa);
	int opened;

	cdev_device_del(&v->cdev, &v->dev);

	do {
		opened = atomic_cmpxchg(&v->opened, 0, 1);
		if (!opened)
			break;
		wait_for_completion(&v->completion);
	} while (1);

	put_device(&v->dev);
}

static struct vdpa_driver vhost_vdpa_driver = {
	.driver = {
		.name	= "vhost_vdpa",
	},
	.probe	= vhost_vdpa_probe,
	.remove	= vhost_vdpa_remove,
};

static int __init vhost_vdpa_init(void)
{
	int r;

	r = alloc_chrdev_region(&vhost_vdpa_major, 0, VHOST_VDPA_DEV_MAX,
				"vhost-vdpa");
	if (r)
		goto err_alloc_chrdev;

	r = vdpa_register_driver(&vhost_vdpa_driver);
	if (r)
		goto err_vdpa_register_driver;

	return 0;

err_vdpa_register_driver:
	unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
err_alloc_chrdev:
	return r;
}
module_init(vhost_vdpa_init);

static void __exit vhost_vdpa_exit(void)
{
	vdpa_unregister_driver(&vhost_vdpa_driver);
	unregister_chrdev_region(vhost_vdpa_major, VHOST_VDPA_DEV_MAX);
}
module_exit(vhost_vdpa_exit);

MODULE_VERSION("0.0.1");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("vDPA-based vhost backend for virtio");