aboutsummaryrefslogtreecommitdiff
path: root/kernel/4.13.0/e1000e/vfio-net.c
blob: dd829afb67f83448c2691ab408cd08228738a901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* Intel PRO/1000 Linux driver
 * Copyright (c) 2017, Linaro Limited
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * The full GNU General Public License is included in this distribution in
 * the file called "COPYING".
 */

#include <linux/netdevice.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/mm.h>
#include <linux/vfio.h>
#include <linux/mdev.h>

#include "e1000.h"
#include "vfio-net.h"

static struct net_device *netmdev_get_netdev(struct mdev_device *mdev);

/* Common vfio-net code: move to core */
#if 1
struct iovamap {
	u64 iova;
	void *vaddr;
	struct device *dev;
	u32 size; /* maximum of 32MB */
	enum dma_data_direction direction; /*  DMA_FROM_DEVICE... */
};

typedef struct netmdev {
	union {
		char page0[4096];
		struct {
			struct net_device *netdev;
			/* FIXME USE A LINKED LIST */
			int mappings_count;
			struct iovamap mappings[128]; /* 3.5KB */
		};
	};
	union {
		/* shadow features & statistics page */
		char page1[4096];
		struct {
			netdev_features_t	features;
			netdev_features_t	hw_features;
			netdev_features_t	wanted_features;
			netdev_features_t	vlan_features;
			netdev_features_t	hw_enc_features;
			netdev_features_t	mpls_features;
			netdev_features_t	gso_partial_features;
			struct net_device_stats	stats;
			atomic_long_t		rx_dropped;
			atomic_long_t		tx_dropped;
			atomic_long_t		rx_nohandler;
		};
	};
} netmdev;

#define VFIO_NET_OFFSET_SHIFT   40

#define VFIO_NET_OFFSET_TO_INDEX(off)	(off >> VFIO_NET_OFFSET_SHIFT)
#define VFIO_NET_INDEX_TO_OFFSET(index)	((u64)(index) << VFIO_NET_OFFSET_SHIFT)
#define VFIO_NET_OFFSET_MASK		(((u64)(1) << VFIO_NET_OFFSET_SHIFT) - 1)
#endif

/**
 * e1000e_vfio_net_open - decouple from network stack
 * @mdev: mediated device to open
 *
 * Decouple network device from the stack and make it available to userspace
 * packet reception and transmission.
 */
static int e1000e_vfio_net_open(struct mdev_device *mdev)
{
	struct net_device *netdev = netmdev_get_netdev(mdev);
	struct e1000_adapter *adapter = netdev_priv(netdev);

	dev_hold(adapter->netdev);

	set_bit(__E1000_VFIO_NET, &adapter->state);
	adapter->irq_mask =
		E1000_IMS_RXT0   |
		E1000_IMS_TXDW   |
		E1000_IMS_RXDMT0 |
		E1000_IMS_RXSEQ;

	if (netif_running(adapter->netdev))
		e1000e_reinit_locked(adapter);
	else
		e1000e_reset(adapter);

	return 0;
}

/**
 * e1000e_vfio_net_release - cleanup attach to network stack
 * @mdev: mediated device to release
 *
 * Cleanup all mediated device resources and restore network stack
 * connection.
 */
static void e1000e_vfio_net_release(struct mdev_device *mdev)
{
	struct net_device *netdev = netmdev_get_netdev(mdev);
	struct e1000_adapter *adapter = netdev_priv(netdev);

	clear_bit(__E1000_VFIO_NET, &adapter->state);
	adapter->irq_mask = 0;

	if (netif_running(adapter->netdev))
		e1000e_reinit_locked(adapter);
	else
		e1000e_reset(adapter);

	dev_put(adapter->netdev);
}

/**
 * e1000e_vfio_net_device_info_get - query vfio device information
 * @adapter: board private structure
 * @info: vfio device description
 *
 * Retrieves information about the device. Fills in provided
 * struct vfio_device_info.
 */
static void e1000e_vfio_net_device_info_get(struct e1000_adapter *adapter,
		struct vfio_device_info *info)
{
	info->flags = VFIO_DEVICE_FLAGS_PCI | VFIO_DEVICE_FLAGS_RESET;
	info->num_regions = VFIO_PCI_NUM_REGIONS + 3;
	info->num_irqs = 1;
}

/**
 * e1000e_vfio_net_region_info_get - query vfio region information
 * @adapter: board private structure
 * @info: vfio region description
 *
 * Retrieves information about the region. The region to query is specified in
 * @info->index. Fills in provided struct vfio_region_info.
 */
static int e1000e_vfio_net_region_info_get(struct e1000_adapter *adapter,
		struct vfio_region_info *info)
{
	int ret = 0;

	switch (info->index) {
	case VFIO_PCI_BAR0_REGION_INDEX:
		/* PCI resource 0 */
		info->offset = VFIO_NET_INDEX_TO_OFFSET(info->index);
		info->size = pci_resource_len(adapter->pdev, info->index);
		info->flags = VFIO_REGION_INFO_FLAG_MMAP;
		break;

	case VFIO_PCI_NUM_REGIONS + 1:
		/* RX descriptor rings */
		info->offset = VFIO_NET_INDEX_TO_OFFSET(info->index);
		info->size = adapter->rx_ring[0].size;
		info->flags = VFIO_REGION_INFO_FLAG_MMAP;
		break;

	case VFIO_PCI_NUM_REGIONS + 2:
		/* TX descriptor ring */
		info->offset = VFIO_NET_INDEX_TO_OFFSET(info->index);
		info->size = adapter->tx_ring[0].size;
		info->flags = VFIO_REGION_INFO_FLAG_MMAP;
		break;

	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}

/**
 * e1000e_vfio_net_irq_info_get - query vfio irq information
 * @adapter: board private structure
 * @info: vfio irq description
 *
 * Retrieves information about the irq. Fills in provided
 * struct vfio_irq_info.
 */
static void e1000e_vfio_net_irq_info_get(struct e1000_adapter *adapter,
		struct vfio_irq_info *info)
{
	info->count = 1;
	info->flags = VFIO_IRQ_INFO_EVENTFD |
		VFIO_IRQ_INFO_MASKABLE |
		VFIO_IRQ_INFO_AUTOMASKED;
}

/**
 * e1000e_vfio_net_ioctl - vfio-net ioctl
 * @mdev:
 * @cmd:
 * @arg:
 *
 * Implement device and region queries, DMA memory mapping and device reset
 **/
static long e1000e_vfio_net_ioctl(struct mdev_device *mdev, unsigned int cmd,
		unsigned long arg)
{
	struct net_device *netdev = netmdev_get_netdev(mdev);
	struct e1000_adapter *adapter = netdev_priv(netdev);
	struct vfio_device_info device_info;
	struct vfio_region_info region_info;
	struct vfio_irq_info irq_info;
	unsigned long minsz;
	int ret;

	switch (cmd) {
	case VFIO_DEVICE_GET_INFO:
		minsz = offsetofend(struct vfio_device_info, num_irqs);
		if (copy_from_user(&device_info, (void __user *)arg, minsz))
			return -EFAULT;

		if (device_info.argsz < minsz)
			return -EINVAL;

		e1000e_vfio_net_device_info_get(adapter, &device_info);

		if (copy_to_user((void __user *)arg, &device_info, minsz))
			return -EFAULT;

		return 0;

	case VFIO_DEVICE_GET_REGION_INFO:
		minsz = offsetofend(struct vfio_region_info, offset);

		if (copy_from_user(&region_info, (void __user *)arg, minsz))
			return -EFAULT;

		if (region_info.argsz < minsz)
			return -EINVAL;

		ret = e1000e_vfio_net_region_info_get(adapter, &region_info);

		if (ret < 0)
			return ret;

		if (copy_to_user((void __user *)arg, &region_info, minsz))
			return -EFAULT;

		return 0;

	case VFIO_DEVICE_GET_IRQ_INFO:
		minsz = offsetofend(struct vfio_irq_info, count);

		if (copy_from_user(&irq_info, (void __user *)arg, minsz))
			return -EFAULT;

		if (irq_info.argsz < minsz || irq_info.index != 0)
			return -EINVAL;

		e1000e_vfio_net_irq_info_get(adapter, &irq_info);

		return copy_to_user((void __user *)arg, &irq_info, minsz) ?
			-EFAULT : 0;

	case VFIO_IOMMU_MAP_DMA:
/* Common vfio-net code: move to core */
#if 1
	{
		struct netmdev *netmdev = mdev_get_drvdata(mdev);
		struct vfio_iommu_type1_dma_map map;
		struct vm_area_struct *vma;
		void *data;
		struct device *parent_dev;
		int node;
		dma_addr_t mapping;
		int ret = -EINVAL;

		/* allocate DMA area and map it where the userland asks
		 * userland need to mmap an area WITHOUT allocating pages:
		 * mmap(vaddr,size, PROT_READ | PROT_WRITE, MAP_SHARED |
		 * MAP_ANONYMOUS | MAP_NORESERVE | MAP_FIXED, -1, 0
		 * MAP_NORESERVE ensures only VA space is booked, no pages are
		 * mapped * the mapping must be the entire area, not partial on
		 * the vma
		 */

		if (netmdev->mappings_count >= 128)
			return -EFAULT;

		minsz = offsetofend(struct vfio_iommu_type1_dma_map, size);

		if (copy_from_user(&map, (void __user *)arg, minsz)) {
			ret = -EFAULT;
			goto out;
		}

		if (map.argsz < minsz)
			goto out;

		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: find_vma(%llx)\n", map.vaddr);
		/*
		 * locates the containing vma for the required map.vaddr
		 * the vma must point to the entire zone allocated by mmap in
		 * userland
		 */
		vma = find_vma(current->mm, map.vaddr);
		if (!vma)
			return -EFAULT;
		if (map.vaddr >= vma->vm_end)
			return -EFAULT;

		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: found vma(%llx) -> start=%lx end=%lx pg_off=%lx\n",
		       map.vaddr, vma->vm_start, vma->vm_end, vma->vm_pgoff);
		/* the iova will be returned as part of the ioctl to the userland */
		//parent_dev = &tp->pci_dev->dev;
		parent_dev = mdev_parent_dev(mdev);

		node = netdev->dev.parent ? dev_to_node(netdev->dev.parent) : -1;
		data = kmalloc_node(map.size, GFP_KERNEL, node);
		if (!data)
			/* return ret? */
			return -ENOMEM;

#if 0
		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: about to dma_map_single(%p, %p, %lld, DMA_FROM_DEVICE)\n",
		       parent_dev, data, map.size);
		mapping = dma_map_single(parent_dev, data, map.size,
					 DMA_FROM_DEVICE);
		if (unlikely(dma_mapping_error(parent_dev, mapping))) {
			if (net_ratelimit())
				printk(KERN_ERR"Failed to dma_map_single buffer for userland!\n");
			kfree(data);
			goto out;
		}
#else
		data = dma_alloc_coherent(parent_dev, map.size, &mapping, GFP_KERNEL);
		if (!data) {
			if (net_ratelimit())
				printk(KERN_ERR"Failed to dma_alloc_coherent buffer for userland!\n");
			goto out;
		}

#endif
		map.iova = mapping;
		ret = dma_mmap_coherent(parent_dev, vma, data, mapping, map.size);
		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: dma_map_coherent %llx -> physmem <- @%llx, %lld:%d\n",
		       map.vaddr, map.iova, map.size, ret);
		if (ret != 0) {
			dma_unmap_single(parent_dev, mapping, map.size,
					 DMA_FROM_DEVICE);
			kfree(data);
			printk(KERN_ERR"VFIO_IOMMU_MAP_DMA: io_remap_pfn_range failed\n");
				return -EFAULT;
		}

		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: recording the mapping %d\n",
		       netmdev->mappings_count);
		netmdev->mappings[netmdev->mappings_count].dev = parent_dev;
		netmdev->mappings[netmdev->mappings_count].vaddr = data;
		netmdev->mappings[netmdev->mappings_count].iova = map.iova;
		netmdev->mappings[netmdev->mappings_count].size = map.size;
		netmdev->mappings_count++;

		printk(KERN_INFO"VFIO_IOMMU_MAP_DMA: preparing response back to user\n");
		if (copy_to_user((void __user *)arg, &map, minsz))
			return -EFAULT;

		ret = 0;
out:
		return ret;
	}
#endif
		return -EOPNOTSUPP;

	case VFIO_DEVICE_RESET:
		if (netif_running(adapter->netdev))
			e1000e_reinit_locked(adapter);
		else
			e1000e_reset(adapter);

		return 0;

	case 500:
		e1000e_trigger_lsc(adapter);
		return 0;

        default:
                return -EOPNOTSUPP;
	}
}

static int e1000e_vfio_net_mmap(struct mdev_device *mdev,
		struct vm_area_struct *vma)
{
	struct net_device *netdev = netmdev_get_netdev(mdev);
	struct e1000_adapter *adapter = netdev_priv(netdev);
	u64 phys_len, req_len, pgoff, req_start;
	unsigned long phys_pfn;
	unsigned int index;

	if (vma->vm_end < vma->vm_start)
		return -EINVAL;
	if ((vma->vm_flags & VM_SHARED) == 0)
		return -EINVAL;

	index = vma->vm_pgoff >> (VFIO_NET_OFFSET_SHIFT - PAGE_SHIFT);

	switch (index) {
	case VFIO_PCI_BAR0_REGION_INDEX:
		/* PCI resource 0 */
		phys_pfn = pci_resource_start(adapter->pdev, index) >> PAGE_SHIFT;
		phys_len = pci_resource_len(adapter->pdev, index);
		break;

	case VFIO_PCI_NUM_REGIONS + 1:
		/* RX descriptor rings */
		phys_pfn = (u64)virt_to_phys(adapter->rx_ring[0].desc) >> PAGE_SHIFT;
		phys_len = adapter->rx_ring[0].size;
		break;

	case VFIO_PCI_NUM_REGIONS + 2:
		/* TX descriptor rings */
		phys_pfn = (u64)virt_to_phys(adapter->tx_ring[0].desc) >> PAGE_SHIFT;
		phys_len = adapter->tx_ring[0].size;
		break;

	default:
		return -EINVAL;
	}

	req_len = vma->vm_end - vma->vm_start;
	pgoff = vma->vm_pgoff &
		((1U << (VFIO_NET_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
	req_start = pgoff << PAGE_SHIFT;

	if (req_start + req_len > phys_len)
		return -EINVAL;

	vma->vm_private_data = NULL;
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
	vma->vm_pgoff = phys_pfn + pgoff;

	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
			       req_len, vma->vm_page_prot);
}

/* Common vfio-net code: move to core */
#if 1
/*
SYSFS structure for the controlling device
*/

static ssize_t available_instances_show(struct kobject *kobj, struct device *dev,
					char *buf)
{
	return scnprintf(buf, PAGE_SIZE, "%d\n", 1);
}
static MDEV_TYPE_ATTR_RO(available_instances);

static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
			       char *buf)
{
	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
}
static MDEV_TYPE_ATTR_RO(device_api);

static struct attribute *sysfs_vfnetdev_attributes[] = {
	&mdev_type_attr_available_instances.attr,
	&mdev_type_attr_device_api.attr,
	NULL,
};

static struct attribute_group sysfs_vfnetdev_type = {
	.name = "vfnetdev",
	.attrs = sysfs_vfnetdev_attributes,
};

/* Only 1 supported for now */
static struct attribute_group *sysfs_type_list[] = {
	&sysfs_vfnetdev_type,
	NULL
};

/*
 * libraries
 */
static struct net_device *netmdev_get_netdev(struct mdev_device *mdev)
{
	struct netmdev *netmdev;

	netmdev = mdev_get_drvdata(mdev);
	if (!netmdev)
		return NULL;

	return netmdev->netdev;
}
/*
 * SYSFS structure for created mdevices
 */
static ssize_t netdev_show(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	struct mdev_device *mdev;
	struct net_device *netdev;

	mdev = mdev_from_dev(dev);
	if (!mdev)
		return scnprintf(buf, PAGE_SIZE, "mdev not found\n");

	netdev = netmdev_get_netdev(mdev);
	if (!netdev)
		return scnprintf(buf, PAGE_SIZE, "ndev-mdev not found\n");

	return scnprintf(buf, PAGE_SIZE, "%.16s\n", netdev->name);
}

static ssize_t netdev_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	struct mdev_device *mdev;
	struct net_device *port;
	struct netmdev *netmdev;
	char name[IFNAMSIZ+1];

	if (count < 2)
		return -EINVAL;

	mdev = mdev_from_dev(dev);
	if (!mdev)
		return -ENODEV;

	netmdev = mdev_get_drvdata(mdev);
	if (netmdev)
		return -ENODEV;

	netmdev = kzalloc(sizeof(*netmdev), GFP_KERNEL);
	if (!netmdev)
		return -ENOMEM;
	mdev_set_drvdata(mdev, netmdev);

	if (count > IFNAMSIZ)
		return -ENODEV;

	memset(name, 0, sizeof(name));
	scnprintf(name, IFNAMSIZ + 1, "%.*s", (int)count - 1, buf);
	port = dev_get_by_name(&init_net, name);
	if (!port)
		return -ENODEV;

	/* FIXME find a way to check if this is the parent device */
	//if (&port->dev != mdev_parent_dev(mdev)) return -1;

	netmdev->netdev = port;

	return count;
}

static DEVICE_ATTR_RW(netdev);
static struct attribute *sysfs_mdev_vfnetdev_attributes[] = {
	&dev_attr_netdev.attr,
	NULL,
};

static struct attribute_group sysfs_mdev_vfnetdev_group = {
	.name = "vfnetdev",
	.attrs = sysfs_mdev_vfnetdev_attributes,
};

static const struct attribute_group *sysfs_mdev_groups[] = {
	&sysfs_mdev_vfnetdev_group,
	NULL,
};

static int e1000_vfio_net_create(struct kobject *kobj, struct mdev_device *mdev)
{
	return 0;
}

static int e1000_vfio_net_remove(struct mdev_device *mdev)
{
	struct netmdev *netmdev = mdev_get_drvdata(mdev);
	struct net_device *port;

	printk(KERN_INFO "%s %d\n", __func__, __LINE__);
	port = netmdev_get_netdev(mdev);
	dev_put(port);
	kfree(netmdev);
	mdev_set_drvdata(mdev, NULL);

	return 0;
}
#endif

const struct mdev_parent_ops e1000_vfio_net_ops = {
	.supported_type_groups = sysfs_type_list,
	.mdev_attr_groups = sysfs_mdev_groups,
	.create = e1000_vfio_net_create,
	.remove = e1000_vfio_net_remove,

	.open = e1000e_vfio_net_open,
	.release = e1000e_vfio_net_release,

	.read = NULL,
	.write = NULL,
	.mmap = e1000e_vfio_net_mmap,
	.ioctl = e1000e_vfio_net_ioctl,
};