vdpa_sim.c 19.0 KB
Newer Older
J
Jason Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// SPDX-License-Identifier: GPL-2.0-only
/*
 * VDPA networking device simulator.
 *
 * Copyright (c) 2020, Red Hat Inc. All rights reserved.
 *     Author: Jason Wang <jasowang@redhat.com>
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/uuid.h>
#include <linux/iommu.h>
21
#include <linux/dma-map-ops.h>
J
Jason Wang 已提交
22 23 24 25 26
#include <linux/sysfs.h>
#include <linux/file.h>
#include <linux/etherdevice.h>
#include <linux/vringh.h>
#include <linux/vdpa.h>
27
#include <linux/virtio_byteorder.h>
J
Jason Wang 已提交
28 29 30 31 32 33 34 35 36
#include <linux/vhost_iotlb.h>
#include <uapi/linux/virtio_config.h>
#include <uapi/linux/virtio_net.h>

#define DRV_VERSION  "0.1"
#define DRV_AUTHOR   "Jason Wang <jasowang@redhat.com>"
#define DRV_DESC     "vDPA Device Simulator"
#define DRV_LICENSE  "GPL v2"

J
Jason Wang 已提交
37 38 39 40
static int batch_mapping = 1;
module_param(batch_mapping, int, 0444);
MODULE_PARM_DESC(batch_mapping, "Batched mapping 1 -Enable; 0 - Disable");

41 42 43 44
static char *macaddr;
module_param(macaddr, charp, 0);
MODULE_PARM_DESC(macaddr, "Ethernet MAC address");

45 46
u8 macaddr_buf[ETH_ALEN];

J
Jason Wang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
struct vdpasim_virtqueue {
	struct vringh vring;
	struct vringh_kiov iov;
	unsigned short head;
	bool ready;
	u64 desc_addr;
	u64 device_addr;
	u64 driver_addr;
	u32 num;
	void *private;
	irqreturn_t (*cb)(void *data);
};

#define VDPASIM_QUEUE_ALIGN PAGE_SIZE
#define VDPASIM_QUEUE_MAX 256
#define VDPASIM_DEVICE_ID 0x1
#define VDPASIM_VENDOR_ID 0
#define VDPASIM_VQ_NUM 0x2
#define VDPASIM_NAME "vdpasim-netdev"

static u64 vdpasim_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
			      (1ULL << VIRTIO_F_VERSION_1)  |
69 70
			      (1ULL << VIRTIO_F_ACCESS_PLATFORM) |
			      (1ULL << VIRTIO_NET_F_MAC);
J
Jason Wang 已提交
71

72
struct vdpasim_dev_attr {
73
	size_t config_size;
74 75 76
	int nvqs;
};

J
Jason Wang 已提交
77 78 79
/* State of each vdpasim device */
struct vdpasim {
	struct vdpa_device vdpa;
80
	struct vdpasim_virtqueue *vqs;
J
Jason Wang 已提交
81
	struct work_struct work;
82
	struct vdpasim_dev_attr dev_attr;
J
Jason Wang 已提交
83 84
	/* spinlock to synchronize virtqueue state */
	spinlock_t lock;
85 86
	/* virtio config according to device type */
	void *config;
J
Jason Wang 已提交
87 88 89 90 91
	struct vhost_iotlb *iommu;
	void *buffer;
	u32 status;
	u32 generation;
	u64 features;
92 93
	/* spinlock to synchronize iommu table */
	spinlock_t iommu_lock;
J
Jason Wang 已提交
94 95
};

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/* TODO: cross-endian support */
static inline bool vdpasim_is_little_endian(struct vdpasim *vdpasim)
{
	return virtio_legacy_is_little_endian() ||
		(vdpasim->features & (1ULL << VIRTIO_F_VERSION_1));
}

static inline u16 vdpasim16_to_cpu(struct vdpasim *vdpasim, __virtio16 val)
{
	return __virtio16_to_cpu(vdpasim_is_little_endian(vdpasim), val);
}

static inline __virtio16 cpu_to_vdpasim16(struct vdpasim *vdpasim, u16 val)
{
	return __cpu_to_virtio16(vdpasim_is_little_endian(vdpasim), val);
}

J
Jason Wang 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static struct vdpasim *vdpasim_dev;

static struct vdpasim *vdpa_to_sim(struct vdpa_device *vdpa)
{
	return container_of(vdpa, struct vdpasim, vdpa);
}

static struct vdpasim *dev_to_sim(struct device *dev)
{
	struct vdpa_device *vdpa = dev_to_vdpa(dev);

	return vdpa_to_sim(vdpa);
}

static void vdpasim_queue_ready(struct vdpasim *vdpasim, unsigned int idx)
{
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

131 132 133 134 135 136 137
	vringh_init_iotlb(&vq->vring, vdpasim_features,
			  VDPASIM_QUEUE_MAX, false,
			  (struct vring_desc *)(uintptr_t)vq->desc_addr,
			  (struct vring_avail *)
			  (uintptr_t)vq->driver_addr,
			  (struct vring_used *)
			  (uintptr_t)vq->device_addr);
J
Jason Wang 已提交
138 139 140 141
}

static void vdpasim_vq_reset(struct vdpasim_virtqueue *vq)
{
142
	vq->ready = false;
J
Jason Wang 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155
	vq->desc_addr = 0;
	vq->driver_addr = 0;
	vq->device_addr = 0;
	vq->cb = NULL;
	vq->private = NULL;
	vringh_init_iotlb(&vq->vring, vdpasim_features, VDPASIM_QUEUE_MAX,
			  false, NULL, NULL, NULL);
}

static void vdpasim_reset(struct vdpasim *vdpasim)
{
	int i;

156
	for (i = 0; i < vdpasim->dev_attr.nvqs; i++)
J
Jason Wang 已提交
157 158
		vdpasim_vq_reset(&vdpasim->vqs[i]);

159
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
160
	vhost_iotlb_reset(vdpasim->iommu);
161
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
162 163 164 165 166 167 168 169 170 171 172 173

	vdpasim->features = 0;
	vdpasim->status = 0;
	++vdpasim->generation;
}

static void vdpasim_work(struct work_struct *work)
{
	struct vdpasim *vdpasim = container_of(work, struct
						 vdpasim, work);
	struct vdpasim_virtqueue *txq = &vdpasim->vqs[1];
	struct vdpasim_virtqueue *rxq = &vdpasim->vqs[0];
174 175
	ssize_t read, write;
	size_t total_write;
J
Jason Wang 已提交
176
	int pkts = 0;
177
	int err;
J
Jason Wang 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

	spin_lock(&vdpasim->lock);

	if (!(vdpasim->status & VIRTIO_CONFIG_S_DRIVER_OK))
		goto out;

	if (!txq->ready || !rxq->ready)
		goto out;

	while (true) {
		total_write = 0;
		err = vringh_getdesc_iotlb(&txq->vring, &txq->iov, NULL,
					   &txq->head, GFP_ATOMIC);
		if (err <= 0)
			break;

		err = vringh_getdesc_iotlb(&rxq->vring, NULL, &rxq->iov,
					   &rxq->head, GFP_ATOMIC);
		if (err <= 0) {
			vringh_complete_iotlb(&txq->vring, txq->head, 0);
			break;
		}

		while (true) {
			read = vringh_iov_pull_iotlb(&txq->vring, &txq->iov,
						     vdpasim->buffer,
						     PAGE_SIZE);
			if (read <= 0)
				break;

			write = vringh_iov_push_iotlb(&rxq->vring, &rxq->iov,
						      vdpasim->buffer, read);
			if (write <= 0)
				break;

			total_write += write;
		}

		/* Make sure data is wrote before advancing index */
		smp_wmb();

		vringh_complete_iotlb(&txq->vring, txq->head, 0);
		vringh_complete_iotlb(&rxq->vring, rxq->head, total_write);

		/* Make sure used is visible before rasing the interrupt. */
		smp_wmb();

		local_bh_disable();
		if (txq->cb)
			txq->cb(txq->private);
		if (rxq->cb)
			rxq->cb(rxq->private);
		local_bh_enable();

		if (++pkts > 4) {
			schedule_work(&vdpasim->work);
			goto out;
		}
	}

out:
	spin_unlock(&vdpasim->lock);
}

static int dir_to_perm(enum dma_data_direction dir)
{
	int perm = -EFAULT;

	switch (dir) {
	case DMA_FROM_DEVICE:
		perm = VHOST_MAP_WO;
		break;
	case DMA_TO_DEVICE:
		perm = VHOST_MAP_RO;
		break;
	case DMA_BIDIRECTIONAL:
		perm = VHOST_MAP_RW;
		break;
	default:
		break;
	}

	return perm;
}

static dma_addr_t vdpasim_map_page(struct device *dev, struct page *page,
				   unsigned long offset, size_t size,
				   enum dma_data_direction dir,
				   unsigned long attrs)
{
	struct vdpasim *vdpasim = dev_to_sim(dev);
	struct vhost_iotlb *iommu = vdpasim->iommu;
	u64 pa = (page_to_pfn(page) << PAGE_SHIFT) + offset;
	int ret, perm = dir_to_perm(dir);

	if (perm < 0)
		return DMA_MAPPING_ERROR;

	/* For simplicity, use identical mapping to avoid e.g iova
	 * allocator.
	 */
279
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
280 281
	ret = vhost_iotlb_add_range(iommu, pa, pa + size - 1,
				    pa, dir_to_perm(dir));
282
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295
	if (ret)
		return DMA_MAPPING_ERROR;

	return (dma_addr_t)(pa);
}

static void vdpasim_unmap_page(struct device *dev, dma_addr_t dma_addr,
			       size_t size, enum dma_data_direction dir,
			       unsigned long attrs)
{
	struct vdpasim *vdpasim = dev_to_sim(dev);
	struct vhost_iotlb *iommu = vdpasim->iommu;

296
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
297 298
	vhost_iotlb_del_range(iommu, (u64)dma_addr,
			      (u64)dma_addr + size - 1);
299
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
300 301 302 303 304 305 306 307 308 309 310
}

static void *vdpasim_alloc_coherent(struct device *dev, size_t size,
				    dma_addr_t *dma_addr, gfp_t flag,
				    unsigned long attrs)
{
	struct vdpasim *vdpasim = dev_to_sim(dev);
	struct vhost_iotlb *iommu = vdpasim->iommu;
	void *addr = kmalloc(size, flag);
	int ret;

311 312
	spin_lock(&vdpasim->iommu_lock);
	if (!addr) {
J
Jason Wang 已提交
313
		*dma_addr = DMA_MAPPING_ERROR;
314
	} else {
J
Jason Wang 已提交
315 316 317 318 319 320 321 322 323 324 325 326
		u64 pa = virt_to_phys(addr);

		ret = vhost_iotlb_add_range(iommu, (u64)pa,
					    (u64)pa + size - 1,
					    pa, VHOST_MAP_RW);
		if (ret) {
			*dma_addr = DMA_MAPPING_ERROR;
			kfree(addr);
			addr = NULL;
		} else
			*dma_addr = (dma_addr_t)pa;
	}
327
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
328 329 330 331 332 333 334 335 336 337 338

	return addr;
}

static void vdpasim_free_coherent(struct device *dev, size_t size,
				  void *vaddr, dma_addr_t dma_addr,
				  unsigned long attrs)
{
	struct vdpasim *vdpasim = dev_to_sim(dev);
	struct vhost_iotlb *iommu = vdpasim->iommu;

339
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
340 341
	vhost_iotlb_del_range(iommu, (u64)dma_addr,
			      (u64)dma_addr + size - 1);
342 343
	spin_unlock(&vdpasim->iommu_lock);

J
Jason Wang 已提交
344 345 346 347 348 349 350 351 352 353 354
	kfree(phys_to_virt((uintptr_t)dma_addr));
}

static const struct dma_map_ops vdpasim_dma_ops = {
	.map_page = vdpasim_map_page,
	.unmap_page = vdpasim_unmap_page,
	.alloc = vdpasim_alloc_coherent,
	.free = vdpasim_free_coherent,
};

static const struct vdpa_config_ops vdpasim_net_config_ops;
J
Jason Wang 已提交
355
static const struct vdpa_config_ops vdpasim_net_batch_config_ops;
J
Jason Wang 已提交
356

357
static struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr)
J
Jason Wang 已提交
358
{
J
Jason Wang 已提交
359
	const struct vdpa_config_ops *ops;
J
Jason Wang 已提交
360 361
	struct vdpasim *vdpasim;
	struct device *dev;
362
	int i, ret = -ENOMEM;
J
Jason Wang 已提交
363

J
Jason Wang 已提交
364 365 366 367 368
	if (batch_mapping)
		ops = &vdpasim_net_batch_config_ops;
	else
		ops = &vdpasim_net_config_ops;

369 370
	vdpasim = vdpa_alloc_device(struct vdpasim, vdpa, NULL, ops,
				    dev_attr->nvqs);
J
Jason Wang 已提交
371 372 373
	if (!vdpasim)
		goto err_alloc;

374
	vdpasim->dev_attr = *dev_attr;
J
Jason Wang 已提交
375 376
	INIT_WORK(&vdpasim->work, vdpasim_work);
	spin_lock_init(&vdpasim->lock);
M
Michael S. Tsirkin 已提交
377
	spin_lock_init(&vdpasim->iommu_lock);
J
Jason Wang 已提交
378 379

	dev = &vdpasim->vdpa.dev;
L
Laurent Vivier 已提交
380 381 382
	dev->dma_mask = &dev->coherent_dma_mask;
	if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
		goto err_iommu;
J
Jason Wang 已提交
383 384
	set_dma_ops(dev, &vdpasim_dma_ops);

385 386 387 388
	vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL);
	if (!vdpasim->config)
		goto err_iommu;

389
	vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue),
390 391 392 393
			       GFP_KERNEL);
	if (!vdpasim->vqs)
		goto err_iommu;

J
Jason Wang 已提交
394 395 396 397 398 399 400 401
	vdpasim->iommu = vhost_iotlb_alloc(2048, 0);
	if (!vdpasim->iommu)
		goto err_iommu;

	vdpasim->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (!vdpasim->buffer)
		goto err_iommu;

402
	if (macaddr) {
403 404
		mac_pton(macaddr, macaddr_buf);
		if (!is_valid_ether_addr(macaddr_buf)) {
405 406 407 408
			ret = -EADDRNOTAVAIL;
			goto err_iommu;
		}
	} else {
409
		eth_random_addr(macaddr_buf);
410
	}
J
Jason Wang 已提交
411

412
	for (i = 0; i < dev_attr->nvqs; i++)
413
		vringh_set_iotlb(&vdpasim->vqs[i].vring, vdpasim->iommu);
J
Jason Wang 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

	vdpasim->vdpa.dma_dev = dev;
	ret = vdpa_register_device(&vdpasim->vdpa);
	if (ret)
		goto err_iommu;

	return vdpasim;

err_iommu:
	put_device(dev);
err_alloc:
	return ERR_PTR(ret);
}

static int vdpasim_set_vq_address(struct vdpa_device *vdpa, u16 idx,
				  u64 desc_area, u64 driver_area,
				  u64 device_area)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	vq->desc_addr = desc_area;
	vq->driver_addr = driver_area;
	vq->device_addr = device_area;

	return 0;
}

static void vdpasim_set_vq_num(struct vdpa_device *vdpa, u16 idx, u32 num)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	vq->num = num;
}

static void vdpasim_kick_vq(struct vdpa_device *vdpa, u16 idx)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	if (vq->ready)
		schedule_work(&vdpasim->work);
}

static void vdpasim_set_vq_cb(struct vdpa_device *vdpa, u16 idx,
			      struct vdpa_callback *cb)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	vq->cb = cb->callback;
	vq->private = cb->private;
}

static void vdpasim_set_vq_ready(struct vdpa_device *vdpa, u16 idx, bool ready)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	spin_lock(&vdpasim->lock);
	vq->ready = ready;
	if (vq->ready)
		vdpasim_queue_ready(vdpasim, idx);
	spin_unlock(&vdpasim->lock);
}

static bool vdpasim_get_vq_ready(struct vdpa_device *vdpa, u16 idx)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];

	return vq->ready;
}

489 490
static int vdpasim_set_vq_state(struct vdpa_device *vdpa, u16 idx,
				const struct vdpa_vq_state *state)
J
Jason Wang 已提交
491 492 493 494 495 496
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
	struct vringh *vrh = &vq->vring;

	spin_lock(&vdpasim->lock);
497
	vrh->last_avail_idx = state->avail_index;
J
Jason Wang 已提交
498 499 500 501 502
	spin_unlock(&vdpasim->lock);

	return 0;
}

503 504
static int vdpasim_get_vq_state(struct vdpa_device *vdpa, u16 idx,
				struct vdpa_vq_state *state)
J
Jason Wang 已提交
505 506 507 508 509
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vdpasim_virtqueue *vq = &vdpasim->vqs[idx];
	struct vringh *vrh = &vq->vring;

510
	state->avail_index = vrh->last_avail_idx;
511
	return 0;
J
Jason Wang 已提交
512 513
}

514
static u32 vdpasim_get_vq_align(struct vdpa_device *vdpa)
J
Jason Wang 已提交
515 516 517 518 519 520 521 522 523 524 525 526
{
	return VDPASIM_QUEUE_ALIGN;
}

static u64 vdpasim_get_features(struct vdpa_device *vdpa)
{
	return vdpasim_features;
}

static int vdpasim_set_features(struct vdpa_device *vdpa, u64 features)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
527 528
	struct virtio_net_config *config =
		(struct virtio_net_config *)vdpasim->config;
J
Jason Wang 已提交
529 530

	/* DMA mapping must be done by driver */
531
	if (!(features & (1ULL << VIRTIO_F_ACCESS_PLATFORM)))
J
Jason Wang 已提交
532 533 534 535
		return -EINVAL;

	vdpasim->features = features & vdpasim_features;

536 537 538 539 540 541 542 543
	/* We generally only know whether guest is using the legacy interface
	 * here, so generally that's the earliest we can set config fields.
	 * Note: We actually require VIRTIO_F_ACCESS_PLATFORM above which
	 * implies VIRTIO_F_VERSION_1, but let's not try to be clever here.
	 */

	config->mtu = cpu_to_vdpasim16(vdpasim, 1500);
	config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
544 545
	memcpy(config->mac, macaddr_buf, ETH_ALEN);

J
Jason Wang 已提交
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	return 0;
}

static void vdpasim_set_config_cb(struct vdpa_device *vdpa,
				  struct vdpa_callback *cb)
{
	/* We don't support config interrupt */
}

static u16 vdpasim_get_vq_num_max(struct vdpa_device *vdpa)
{
	return VDPASIM_QUEUE_MAX;
}

static u32 vdpasim_get_device_id(struct vdpa_device *vdpa)
{
	return VDPASIM_DEVICE_ID;
}

static u32 vdpasim_get_vendor_id(struct vdpa_device *vdpa)
{
	return VDPASIM_VENDOR_ID;
}

static u8 vdpasim_get_status(struct vdpa_device *vdpa)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	u8 status;

	spin_lock(&vdpasim->lock);
	status = vdpasim->status;
	spin_unlock(&vdpasim->lock);

579
	return status;
J
Jason Wang 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
}

static void vdpasim_set_status(struct vdpa_device *vdpa, u8 status)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);

	spin_lock(&vdpasim->lock);
	vdpasim->status = status;
	if (status == 0)
		vdpasim_reset(vdpasim);
	spin_unlock(&vdpasim->lock);
}

static void vdpasim_get_config(struct vdpa_device *vdpa, unsigned int offset,
			     void *buf, unsigned int len)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);

598 599
	if (offset + len < vdpasim->dev_attr.config_size)
		memcpy(buf, vdpasim->config + offset, len);
J
Jason Wang 已提交
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
}

static void vdpasim_set_config(struct vdpa_device *vdpa, unsigned int offset,
			     const void *buf, unsigned int len)
{
	/* No writable config supportted by vdpasim */
}

static u32 vdpasim_get_generation(struct vdpa_device *vdpa)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);

	return vdpasim->generation;
}

J
Jason Wang 已提交
615 616 617 618 619 620 621 622 623 624
static struct vdpa_iova_range vdpasim_get_iova_range(struct vdpa_device *vdpa)
{
	struct vdpa_iova_range range = {
		.first = 0ULL,
		.last = ULLONG_MAX,
	};

	return range;
}

J
Jason Wang 已提交
625 626 627 628 629 630 631 632
static int vdpasim_set_map(struct vdpa_device *vdpa,
			   struct vhost_iotlb *iotlb)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
	struct vhost_iotlb_map *map;
	u64 start = 0ULL, last = 0ULL - 1;
	int ret;

633
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
634 635 636 637 638 639 640 641 642
	vhost_iotlb_reset(vdpasim->iommu);

	for (map = vhost_iotlb_itree_first(iotlb, start, last); map;
	     map = vhost_iotlb_itree_next(map, start, last)) {
		ret = vhost_iotlb_add_range(vdpasim->iommu, map->start,
					    map->last, map->addr, map->perm);
		if (ret)
			goto err;
	}
643
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
644 645 646 647
	return 0;

err:
	vhost_iotlb_reset(vdpasim->iommu);
648
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
649 650 651 652 653 654 655
	return ret;
}

static int vdpasim_dma_map(struct vdpa_device *vdpa, u64 iova, u64 size,
			   u64 pa, u32 perm)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);
656
	int ret;
J
Jason Wang 已提交
657

658 659 660 661 662 663
	spin_lock(&vdpasim->iommu_lock);
	ret = vhost_iotlb_add_range(vdpasim->iommu, iova, iova + size - 1, pa,
				    perm);
	spin_unlock(&vdpasim->iommu_lock);

	return ret;
J
Jason Wang 已提交
664 665 666 667 668 669
}

static int vdpasim_dma_unmap(struct vdpa_device *vdpa, u64 iova, u64 size)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);

670
	spin_lock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
671
	vhost_iotlb_del_range(vdpasim->iommu, iova, iova + size - 1);
672
	spin_unlock(&vdpasim->iommu_lock);
J
Jason Wang 已提交
673 674 675 676 677 678 679 680 681 682 683 684

	return 0;
}

static void vdpasim_free(struct vdpa_device *vdpa)
{
	struct vdpasim *vdpasim = vdpa_to_sim(vdpa);

	cancel_work_sync(&vdpasim->work);
	kfree(vdpasim->buffer);
	if (vdpasim->iommu)
		vhost_iotlb_free(vdpasim->iommu);
685
	kfree(vdpasim->vqs);
686
	kfree(vdpasim->config);
J
Jason Wang 已提交
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}

static const struct vdpa_config_ops vdpasim_net_config_ops = {
	.set_vq_address         = vdpasim_set_vq_address,
	.set_vq_num             = vdpasim_set_vq_num,
	.kick_vq                = vdpasim_kick_vq,
	.set_vq_cb              = vdpasim_set_vq_cb,
	.set_vq_ready           = vdpasim_set_vq_ready,
	.get_vq_ready           = vdpasim_get_vq_ready,
	.set_vq_state           = vdpasim_set_vq_state,
	.get_vq_state           = vdpasim_get_vq_state,
	.get_vq_align           = vdpasim_get_vq_align,
	.get_features           = vdpasim_get_features,
	.set_features           = vdpasim_set_features,
	.set_config_cb          = vdpasim_set_config_cb,
	.get_vq_num_max         = vdpasim_get_vq_num_max,
	.get_device_id          = vdpasim_get_device_id,
	.get_vendor_id          = vdpasim_get_vendor_id,
	.get_status             = vdpasim_get_status,
	.set_status             = vdpasim_set_status,
	.get_config             = vdpasim_get_config,
	.set_config             = vdpasim_set_config,
	.get_generation         = vdpasim_get_generation,
J
Jason Wang 已提交
710
	.get_iova_range         = vdpasim_get_iova_range,
J
Jason Wang 已提交
711 712 713 714 715
	.dma_map                = vdpasim_dma_map,
	.dma_unmap              = vdpasim_dma_unmap,
	.free                   = vdpasim_free,
};

J
Jason Wang 已提交
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
static const struct vdpa_config_ops vdpasim_net_batch_config_ops = {
	.set_vq_address         = vdpasim_set_vq_address,
	.set_vq_num             = vdpasim_set_vq_num,
	.kick_vq                = vdpasim_kick_vq,
	.set_vq_cb              = vdpasim_set_vq_cb,
	.set_vq_ready           = vdpasim_set_vq_ready,
	.get_vq_ready           = vdpasim_get_vq_ready,
	.set_vq_state           = vdpasim_set_vq_state,
	.get_vq_state           = vdpasim_get_vq_state,
	.get_vq_align           = vdpasim_get_vq_align,
	.get_features           = vdpasim_get_features,
	.set_features           = vdpasim_set_features,
	.set_config_cb          = vdpasim_set_config_cb,
	.get_vq_num_max         = vdpasim_get_vq_num_max,
	.get_device_id          = vdpasim_get_device_id,
	.get_vendor_id          = vdpasim_get_vendor_id,
	.get_status             = vdpasim_get_status,
	.set_status             = vdpasim_set_status,
	.get_config             = vdpasim_get_config,
	.set_config             = vdpasim_set_config,
	.get_generation         = vdpasim_get_generation,
J
Jason Wang 已提交
737
	.get_iova_range         = vdpasim_get_iova_range,
J
Jason Wang 已提交
738 739 740 741
	.set_map                = vdpasim_set_map,
	.free                   = vdpasim_free,
};

J
Jason Wang 已提交
742 743
static int __init vdpasim_dev_init(void)
{
744 745 746
	struct vdpasim_dev_attr dev_attr = {};

	dev_attr.nvqs = VDPASIM_VQ_NUM;
747
	dev_attr.config_size = sizeof(struct virtio_net_config);
748 749

	vdpasim_dev = vdpasim_create(&dev_attr);
J
Jason Wang 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

	if (!IS_ERR(vdpasim_dev))
		return 0;

	return PTR_ERR(vdpasim_dev);
}

static void __exit vdpasim_dev_exit(void)
{
	struct vdpa_device *vdpa = &vdpasim_dev->vdpa;

	vdpa_unregister_device(vdpa);
}

module_init(vdpasim_dev_init)
module_exit(vdpasim_dev_exit)

MODULE_VERSION(DRV_VERSION);
MODULE_LICENSE(DRV_LICENSE);
MODULE_AUTHOR(DRV_AUTHOR);
MODULE_DESCRIPTION(DRV_DESC);