aboutsummaryrefslogtreecommitdiff
path: root/src/userspace_io.c
blob: 6f29b2d4924b602abc456919f9ea6633147d1dcf (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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <endian.h>
#include <stdlib.h>
#include <errno.h>

#include <arpa/inet.h>

#include <sys/ioctl.h>
#include <sys/mman.h>

#include <linux/vfio.h>

/* Us */
#include <drivers/driver_ops.h>
#include <vfio_api.h>
#include <mm_api.h>
#include <common.h>

/* FIXME modularize driver load */
extern const struct driver_ops r8169_ops;

static char help_msg[] = {
	"\n"
	" -d: device uuid\n"
	" -i: device id in /dev/vfio\n"
};

static void usage(char *name)
{
	printf("usage: %s [OPTIONS] \n%s\n", name, help_msg);
}

int main(int argc, char *argv[])
{
	/* FIXME select proper *_ops once we have loadable modules */
	struct vfio_group_status group_status = { .argsz = sizeof(group_status) };
	struct vfio_iommu_type1_info iommu_info = { .argsz = sizeof(iommu_info) };
	struct vfio_device_info device_info = { .argsz = sizeof(device_info) };
	struct vfio_region_info region_info = { .argsz = sizeof(region_info) };
	struct iomem rx_data;
	struct driver_ops exec_ops = r8169_ops;
	void *iobase, *iocur, *rxring;
	int container = -1, group = -1, device = -1;
	int ret, c, group_id, cnt, i = 0;
	char group_uuid[64]; /* 37 should be enough */
	char *rx_buff[256];
	__u32 opts = 0;
	/* make this allocated pointer? */
	void *mmaps[4096];
	struct vfio_region_info regions[4096];

	while ((c = getopt(argc, argv, "d:i:")) != -1) {
		switch (c) {
		case 'd':
			strncpy(group_uuid, optarg, sizeof(group_uuid));
			opts |= OPT_UUID;
			break;
		case 'i':
			group_id = atoi(optarg);
			opts |= OPT_DEV;
			break;
		}
	}

	if ((opts & (OPT_UUID | OPT_DEV)) != (OPT_UUID | OPT_DEV)) {
		usage(argv[0]);
		return -1;
	}

	iobase = iomem_init();
	if (!iobase)
		return -ENOMEM;
	iocur = iobase;

	/* Create a new container */
	container = get_container();
	if (container < 0)
		goto out;

	group = get_group(group_id);
	if (group < 0)
		goto out;

	device = vfio_init_dev(group, container, &group_status, &iommu_info,
			       &device_info, group_uuid);

	/* FIXME munmap and release properly on fails */
	/* FIXME make this generic for devices, maybe driver specific callback? */
	/* Get device specific Rx descriptors */
	/* Get device specific BARs + user specified regions */
	for (i = 0; i <= device_info.num_regions; i++) {
		ret = vfio_get_region(device, &region_info, i);
		/* API returns -EINVAL for unimplemented BARs */
		if (!region_info.size || ret)
			continue;
		printf("Region:%d size %lu, offset 0x%lx, flags 0x%x %s\n", i,
		       (unsigned long)region_info.size,
		       (unsigned long)region_info.offset, region_info.flags,
		       (region_info.flags & VFIO_REGION_INFO_FLAG_MMAP) ?
		       "mmap allowed" : "mmap forbidden");
		if (!(region_info.flags & VFIO_REGION_INFO_FLAG_MMAP))
			continue;
		mmaps[cnt] = mmap(NULL, region_info.size, PROT_READ | PROT_WRITE,
				  MAP_SHARED, device, region_info.offset);
		if (mmaps[cnt] == MAP_FAILED) {
			printf("mmap failed\n");
			goto out;
		}
		memcpy(&regions[cnt], &region_info, sizeof(regions[cnt]));
		if (i < device_info.num_regions)
			cnt++;
	}

	printf("Total regions: %d\n", cnt + 1);
	/* FIXME THIS NEEDS TO BE ON DRIVER CALLBACK API
	 * It's currently pointing on the rxdesc area
	 */
	rxring = mmaps[cnt - 1];

#define DBG //define to print pci BAR1
#if defined DBG
	int j;
	size_t sz;
	sz = regions[1].size / sizeof(uint32_t *);
	for (j = 0; j < sz; j++) {
		if (!(j % 8) && j != 0)
			printf("\n");
			printf("%08x ", ((uint32_t *)mmaps[1])[j]);
	}
	printf("\n");
#endif

	printf("iobase: %p iocur : %p\n", iobase, iocur);
	ret = iomem_alloc(device, 2 * 1024 * 1024, &iocur, &rx_data);
	if (ret)
		goto out;
	printf("iobase: %p iocur : %p\n", iobase, iocur);

	if (exec_ops.rx_fill(device, rxring, rx_data, rx_buff) != 0) {
		printf("Could not fill ring\n");
		goto out;
	}

	ioctl(device, 500, NULL);
	exec_ops.recv(rxring, rx_buff);

out:
	/* FIXME munmap and release properly on fails */
	//if (rxring)
		//munmap(rxring, region_info.size);
	if (group)
		close(group);
	if (container)
		close(container);
	return -1;
}