summaryrefslogtreecommitdiffstats
path: root/drivers/bus/vmbus/linux/vmbus_bus.c
blob: a4755a3878dfb4179909ac4e0de4acb131b3312c (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2018, Microsoft Corporation.
 * All Rights Reserved.
 */

#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>

#include <rte_eal.h>
#include <rte_uuid.h>
#include <rte_tailq.h>
#include <rte_log.h>
#include <rte_devargs.h>
#include <rte_memory.h>
#include <rte_malloc.h>
#include <rte_bus_vmbus.h>

#include "eal_filesystem.h"
#include "private.h"

/** Pathname of VMBUS devices directory. */
#define SYSFS_VMBUS_DEVICES "/sys/bus/vmbus/devices"

extern struct rte_vmbus_bus rte_vmbus_bus;

/* Read sysfs file to get UUID */
static int
parse_sysfs_uuid(const char *filename, rte_uuid_t uu)
{
	char buf[BUFSIZ];
	char *cp, *in = buf;
	FILE *f;

	f = fopen(filename, "r");
	if (f == NULL) {
		VMBUS_LOG(ERR, "cannot open sysfs value %s: %s",
			  filename, strerror(errno));
		return -1;
	}

	if (fgets(buf, sizeof(buf), f) == NULL) {
		VMBUS_LOG(ERR, "cannot read sysfs value %s",
				filename);
		fclose(f);
		return -1;
	}
	fclose(f);

	cp = strchr(buf, '\n');
	if (cp)
		*cp = '\0';

	/* strip { } notation */
	if (buf[0] == '{') {
		in = buf + 1;
		cp = strchr(in, '}');
		if (cp)
			*cp = '\0';
	}

	if (rte_uuid_parse(in, uu) < 0) {
		VMBUS_LOG(ERR, "%s %s not a valid UUID",
			filename, buf);
		return -1;
	}

	return 0;
}

static int
get_sysfs_string(const char *filename, char *buf, size_t buflen)
{
	char *cp;
	FILE *f;

	f = fopen(filename, "r");
	if (f == NULL) {
		VMBUS_LOG(ERR, "cannot open sysfs value %s:%s",
			  filename, strerror(errno));
		return -1;
	}

	if (fgets(buf, buflen, f) == NULL) {
		VMBUS_LOG(ERR, "cannot read sysfs value %s",
				filename);
		fclose(f);
		return -1;
	}
	fclose(f);

	/* remove trailing newline */
	cp = memchr(buf, '\n', buflen);
	if (cp)
		*cp = '\0';

	return 0;
}

static int
vmbus_get_uio_dev(const struct rte_vmbus_device *dev,
		  char *dstbuf, size_t buflen)
{
	char dirname[PATH_MAX];
	unsigned int uio_num;
	struct dirent *e;
	DIR *dir;

	/* Assume recent kernel where uio is in uio/uioX */
	snprintf(dirname, sizeof(dirname),
		 SYSFS_VMBUS_DEVICES "/%s/uio", dev->device.name);

	dir = opendir(dirname);
	if (dir == NULL)
		return -1; /* Not a UIO device */

	/* take the first file starting with "uio" */
	while ((e = readdir(dir)) != NULL) {
		const int prefix_len = 3;
		char *endptr;

		if (strncmp(e->d_name, "uio", prefix_len) != 0)
			continue;

		/* try uio%d */
		errno = 0;
		uio_num = strtoull(e->d_name + prefix_len, &endptr, 10);
		if (errno == 0 && endptr != (e->d_name + prefix_len)) {
			snprintf(dstbuf, buflen, "%s/uio%u", dirname, uio_num);
			break;
		}
	}
	closedir(dir);

	if (e == NULL)
		return -1;

	return uio_num;
}

/* Check map names with kernel names */
static const char *map_names[VMBUS_MAX_RESOURCE] = {
	[HV_TXRX_RING_MAP] = "txrx_rings",
	[HV_INT_PAGE_MAP]  = "int_page",
	[HV_MON_PAGE_MAP]  = "monitor_page",
	[HV_RECV_BUF_MAP]  = "recv:",
	[HV_SEND_BUF_MAP]  = "send:",
};


/* map the resources of a vmbus device in virtual memory */
int
rte_vmbus_map_device(struct rte_vmbus_device *dev)
{
	char uioname[PATH_MAX], filename[PATH_MAX];
	char dirname[PATH_MAX], mapname[64];
	int i;

	dev->uio_num = vmbus_get_uio_dev(dev, uioname, sizeof(uioname));
	if (dev->uio_num < 0) {
		VMBUS_LOG(DEBUG, "Not managed by UIO driver, skipped");
		return 1;
	}

	/* Extract resource value */
	for (i = 0; i < VMBUS_MAX_RESOURCE; i++) {
		struct rte_mem_resource *res = &dev->resource[i];
		unsigned long len, gpad = 0;
		char *cp;

		snprintf(dirname, sizeof(dirname),
			 "%s/maps/map%d", uioname, i);

		snprintf(filename, sizeof(filename),
			 "%s/name", dirname);

		if (get_sysfs_string(filename, mapname, sizeof(mapname)) < 0) {
			VMBUS_LOG(ERR, "could not read %s", filename);
			return -1;
		}

		if (strncmp(map_names[i], mapname, strlen(map_names[i])) != 0) {
			VMBUS_LOG(ERR,
				"unexpected resource %s (expected %s)",
				mapname, map_names[i]);
			return -1;
		}

		snprintf(filename, sizeof(filename),
			 "%s/size", dirname);
		if (eal_parse_sysfs_value(filename, &len) < 0) {
			VMBUS_LOG(ERR,
				"could not read %s", filename);
			return -1;
		}
		res->len = len;

		/* both send and receive buffers have gpad in name */
		cp = memchr(mapname, ':', sizeof(mapname));
		if (cp)
			gpad = strtoul(cp+1, NULL, 0);

		/* put the GPAD value in physical address */
		res->phys_addr = gpad;
	}

	return vmbus_uio_map_resource(dev);
}

void
rte_vmbus_unmap_device(struct rte_vmbus_device *dev)
{
	vmbus_uio_unmap_resource(dev);
}

/* Scan one vmbus sysfs entry, and fill the devices list from it. */
static int
vmbus_scan_one(const char *name)
{
	struct rte_vmbus_device *dev, *dev2;
	char filename[PATH_MAX];
	char dirname[PATH_MAX];
	unsigned long tmp;

	dev = calloc(1, sizeof(*dev));
	if (dev == NULL)
		return -1;

	dev->device.bus = &rte_vmbus_bus.bus;
	dev->device.name = strdup(name);
	if (!dev->device.name)
		goto error;

	/* sysfs base directory
	 *   /sys/bus/vmbus/devices/7a08391f-f5a0-4ac0-9802-d13fd964f8df
	 * or on older kernel
	 *   /sys/bus/vmbus/devices/vmbus_1
	 */
	snprintf(dirname, sizeof(dirname), "%s/%s",
		 SYSFS_VMBUS_DEVICES, name);

	/* get device id */
	snprintf(filename, sizeof(filename), "%s/device_id", dirname);
	if (parse_sysfs_uuid(filename, dev->device_id) < 0)
		goto error;

	/* get device class  */
	snprintf(filename, sizeof(filename), "%s/class_id", dirname);
	if (parse_sysfs_uuid(filename, dev->class_id) < 0)
		goto error;

	/* get relid */
	snprintf(filename, sizeof(filename), "%s/id", dirname);
	if (eal_parse_sysfs_value(filename, &tmp) < 0)
		goto error;
	dev->relid = tmp;

	/* get monitor id */
	snprintf(filename, sizeof(filename), "%s/monitor_id", dirname);
	if (eal_parse_sysfs_value(filename, &tmp) < 0)
		goto error;
	dev->monitor_id = tmp;

	/* get numa node (if present) */
	snprintf(filename, sizeof(filename), "%s/numa_node",
		 dirname);

	if (access(filename, R_OK) == 0) {
		if (eal_parse_sysfs_value(filename, &tmp) < 0)
			goto error;
		dev->device.numa_node = tmp;
	} else {
		/* if no NUMA support, set default to 0 */
		dev->device.numa_node = SOCKET_ID_ANY;
	}

	dev->device.devargs = vmbus_devargs_lookup(dev);

	/* device is valid, add in list (sorted) */
	VMBUS_LOG(DEBUG, "Adding vmbus device %s", name);

	TAILQ_FOREACH(dev2, &rte_vmbus_bus.device_list, next) {
		int ret;

		ret = rte_uuid_compare(dev->device_id, dev2->device_id);
		if (ret > 0)
			continue;

		if (ret < 0) {
			vmbus_insert_device(dev2, dev);
		} else { /* already registered */
			VMBUS_LOG(NOTICE,
				"%s already registered", name);
			free(dev);
		}
		return 0;
	}

	vmbus_add_device(dev);
	return 0;
error:
	VMBUS_LOG(DEBUG, "failed");

	free(dev);
	return -1;
}

/*
 * Scan the content of the vmbus, and the devices in the devices list
 */
int
rte_vmbus_scan(void)
{
	struct dirent *e;
	DIR *dir;

	dir = opendir(SYSFS_VMBUS_DEVICES);
	if (dir == NULL) {
		if (errno == ENOENT)
			return 0;

		VMBUS_LOG(ERR, "opendir %s failed: %s",
			  SYSFS_VMBUS_DEVICES, strerror(errno));
		return -1;
	}

	while ((e = readdir(dir)) != NULL) {
		if (e->d_name[0] == '.')
			continue;

		if (vmbus_scan_one(e->d_name) < 0)
			goto error;
	}
	closedir(dir);
	return 0;

error:
	closedir(dir);
	return -1;
}

void rte_vmbus_irq_mask(struct rte_vmbus_device *device)
{
	vmbus_uio_irq_control(device, 1);
}

void rte_vmbus_irq_unmask(struct rte_vmbus_device *device)
{
	vmbus_uio_irq_control(device, 0);
}

int rte_vmbus_irq_read(struct rte_vmbus_device *device)
{
	return vmbus_uio_irq_read(device);
}