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

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

#include <rte_log.h>
#include <rte_bus.h>
#include <rte_memory.h>
#include <rte_common.h>
#include <rte_malloc.h>
#include <rte_bus_vmbus.h>
#include <rte_string_fns.h>

#include "private.h"

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

static void *vmbus_map_addr;

/* Control interrupts */
void vmbus_uio_irq_control(struct rte_vmbus_device *dev, int32_t onoff)
{
	if (write(dev->intr_handle.fd, &onoff, sizeof(onoff)) < 0) {
		VMBUS_LOG(ERR, "cannot write to %d:%s",
			dev->intr_handle.fd, strerror(errno));
	}
}

int vmbus_uio_irq_read(struct rte_vmbus_device *dev)
{
	int32_t count;
	int cc;

	cc = read(dev->intr_handle.fd, &count, sizeof(count));
	if (cc < (int)sizeof(count)) {
		if (cc < 0) {
			VMBUS_LOG(ERR, "IRQ read failed %s",
				  strerror(errno));
			return -errno;
		}
		VMBUS_LOG(ERR, "can't read IRQ count");
		return -EINVAL;
	}

	return count;
}

void
vmbus_uio_free_resource(struct rte_vmbus_device *dev,
		struct mapped_vmbus_resource *uio_res)
{
	rte_free(uio_res);

	if (dev->intr_handle.uio_cfg_fd >= 0) {
		close(dev->intr_handle.uio_cfg_fd);
		dev->intr_handle.uio_cfg_fd = -1;
	}

	if (dev->intr_handle.fd >= 0) {
		close(dev->intr_handle.fd);
		dev->intr_handle.fd = -1;
		dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;
	}
}

int
vmbus_uio_alloc_resource(struct rte_vmbus_device *dev,
			 struct mapped_vmbus_resource **uio_res)
{
	char devname[PATH_MAX]; /* contains the /dev/uioX */

	/* save fd if in primary process */
	snprintf(devname, sizeof(devname), "/dev/uio%u", dev->uio_num);
	dev->intr_handle.fd = open(devname, O_RDWR);
	if (dev->intr_handle.fd < 0) {
		VMBUS_LOG(ERR, "Cannot open %s: %s",
			devname, strerror(errno));
		goto error;
	}
	dev->intr_handle.type = RTE_INTR_HANDLE_UIO_INTX;

	/* allocate the mapping details for secondary processes*/
	*uio_res = rte_zmalloc("UIO_RES", sizeof(**uio_res), 0);
	if (*uio_res == NULL) {
		VMBUS_LOG(ERR, "cannot store uio mmap details");
		goto error;
	}

	strlcpy((*uio_res)->path, devname, PATH_MAX);
	rte_uuid_copy((*uio_res)->id, dev->device_id);

	return 0;

error:
	vmbus_uio_free_resource(dev, *uio_res);
	return -1;
}

static int
find_max_end_va(const struct rte_memseg_list *msl, void *arg)
{
	size_t sz = msl->memseg_arr.len * msl->page_sz;
	void *end_va = RTE_PTR_ADD(msl->base_va, sz);
	void **max_va = arg;

	if (*max_va < end_va)
		*max_va = end_va;
	return 0;
}

/*
 * TODO: this should be part of memseg api.
 *       code is duplicated from PCI.
 */
static void *
vmbus_find_max_end_va(void)
{
	void *va = NULL;

	rte_memseg_list_walk(find_max_end_va, &va);
	return va;
}

int
vmbus_uio_map_resource_by_index(struct rte_vmbus_device *dev, int idx,
				struct mapped_vmbus_resource *uio_res,
				int flags)
{
	size_t size = dev->resource[idx].len;
	struct vmbus_map *maps = uio_res->maps;
	void *mapaddr;
	off_t offset;
	int fd;

	/* devname for mmap  */
	fd = open(uio_res->path, O_RDWR);
	if (fd < 0) {
		VMBUS_LOG(ERR, "Cannot open %s: %s",
			  uio_res->path, strerror(errno));
		return -1;
	}

	/* try mapping somewhere close to the end of hugepages */
	if (vmbus_map_addr == NULL)
		vmbus_map_addr = vmbus_find_max_end_va();

	/* offset is special in uio it indicates which resource */
	offset = idx * PAGE_SIZE;

	mapaddr = vmbus_map_resource(vmbus_map_addr, fd, offset, size, flags);
	close(fd);

	if (mapaddr == MAP_FAILED)
		return -1;

	dev->resource[idx].addr = mapaddr;
	vmbus_map_addr = RTE_PTR_ADD(mapaddr, size);

	/* Record result of successful mapping for use by secondary */
	maps[idx].addr = mapaddr;
	maps[idx].size = size;

	return 0;
}

static int vmbus_uio_map_primary(struct vmbus_channel *chan,
				 void **ring_buf, uint32_t *ring_size)
{
	struct mapped_vmbus_resource *uio_res;

	uio_res = vmbus_uio_find_resource(chan->device);
	if (!uio_res) {
		VMBUS_LOG(ERR, "can not find resources!");
		return -ENOMEM;
	}

	if (uio_res->nb_maps < VMBUS_MAX_RESOURCE) {
		VMBUS_LOG(ERR, "VMBUS: only %u resources found!",
			  uio_res->nb_maps);
		return -EINVAL;
	}

	*ring_size = uio_res->maps[HV_TXRX_RING_MAP].size / 2;
	*ring_buf  = uio_res->maps[HV_TXRX_RING_MAP].addr;
	return 0;
}

static int vmbus_uio_map_subchan(const struct rte_vmbus_device *dev,
				 const struct vmbus_channel *chan,
				 void **ring_buf, uint32_t *ring_size)
{
	char ring_path[PATH_MAX];
	size_t file_size;
	struct stat sb;
	void *mapaddr;
	int fd;

	snprintf(ring_path, sizeof(ring_path),
		 "%s/%s/channels/%u/ring",
		 SYSFS_VMBUS_DEVICES, dev->device.name,
		 chan->relid);

	fd = open(ring_path, O_RDWR);
	if (fd < 0) {
		VMBUS_LOG(ERR, "Cannot open %s: %s",
			  ring_path, strerror(errno));
		return -errno;
	}

	if (fstat(fd, &sb) < 0) {
		VMBUS_LOG(ERR, "Cannot state %s: %s",
			  ring_path, strerror(errno));
		close(fd);
		return -errno;
	}
	file_size = sb.st_size;

	if (file_size == 0 || (file_size & (PAGE_SIZE - 1))) {
		VMBUS_LOG(ERR, "incorrect size %s: %zu",
			  ring_path, file_size);

		close(fd);
		return -EINVAL;
	}

	mapaddr = vmbus_map_resource(vmbus_map_addr, fd,
				     0, file_size, 0);
	close(fd);

	if (mapaddr == MAP_FAILED)
		return -EIO;

	*ring_size = file_size / 2;
	*ring_buf = mapaddr;

	vmbus_map_addr = RTE_PTR_ADD(ring_buf, file_size);
	return 0;
}

int
vmbus_uio_map_secondary_subchan(const struct rte_vmbus_device *dev,
				const struct vmbus_channel *chan)
{
	const struct vmbus_br *br = &chan->txbr;
	char ring_path[PATH_MAX];
	void *mapaddr, *ring_buf;
	uint32_t ring_size;
	int fd;

	snprintf(ring_path, sizeof(ring_path),
		 "%s/%s/channels/%u/ring",
		 SYSFS_VMBUS_DEVICES, dev->device.name,
		 chan->relid);

	ring_buf = br->vbr;
	ring_size = br->dsize + sizeof(struct vmbus_bufring);
	VMBUS_LOG(INFO, "secondary ring_buf %p size %u",
		  ring_buf, ring_size);

	fd = open(ring_path, O_RDWR);
	if (fd < 0) {
		VMBUS_LOG(ERR, "Cannot open %s: %s",
			  ring_path, strerror(errno));
		return -errno;
	}

	mapaddr = vmbus_map_resource(ring_buf, fd, 0, 2 * ring_size, 0);
	close(fd);

	if (mapaddr == ring_buf)
		return 0;

	if (mapaddr == MAP_FAILED)
		VMBUS_LOG(ERR,
			  "mmap subchan %u in secondary failed", chan->relid);
	else {
		VMBUS_LOG(ERR,
			  "mmap subchan %u in secondary address mismatch",
			  chan->relid);
		vmbus_unmap_resource(mapaddr, 2 * ring_size);
	}
	return -1;
}

int vmbus_uio_map_rings(struct vmbus_channel *chan)
{
	const struct rte_vmbus_device *dev = chan->device;
	uint32_t ring_size;
	void *ring_buf;
	int ret;

	/* Primary channel */
	if (chan->subchannel_id == 0)
		ret = vmbus_uio_map_primary(chan, &ring_buf, &ring_size);
	else
		ret = vmbus_uio_map_subchan(dev, chan, &ring_buf, &ring_size);

	if (ret)
		return ret;

	vmbus_br_setup(&chan->txbr, ring_buf, ring_size);
	vmbus_br_setup(&chan->rxbr, (char *)ring_buf + ring_size, ring_size);
	return 0;
}

static int vmbus_uio_sysfs_read(const char *dir, const char *name,
				unsigned long *val, unsigned long max_range)
{
	char path[PATH_MAX];
	FILE *f;
	int ret;

	snprintf(path, sizeof(path), "%s/%s", dir, name);
	f = fopen(path, "r");
	if (!f) {
		VMBUS_LOG(ERR, "can't open %s:%s",
			  path, strerror(errno));
		return -errno;
	}

	if (fscanf(f, "%lu", val) != 1)
		ret = -EIO;
	else if (*val > max_range)
		ret = -ERANGE;
	else
		ret = 0;
	fclose(f);

	return ret;
}

static bool vmbus_uio_ring_present(const struct rte_vmbus_device *dev,
				   uint32_t relid)
{
	char ring_path[PATH_MAX];

	/* Check if kernel has subchannel sysfs files */
	snprintf(ring_path, sizeof(ring_path),
		 "%s/%s/channels/%u/ring",
		 SYSFS_VMBUS_DEVICES, dev->device.name, relid);

	return access(ring_path, R_OK|W_OK) == 0;
}

bool vmbus_uio_subchannels_supported(const struct rte_vmbus_device *dev,
				     const struct vmbus_channel *chan)
{
	return vmbus_uio_ring_present(dev, chan->relid);
}

static bool vmbus_isnew_subchannel(struct vmbus_channel *primary,
				   unsigned long id)
{
	const struct vmbus_channel *c;

	STAILQ_FOREACH(c, &primary->subchannel_list, next) {
		if (c->relid == id)
			return false;
	}
	return true;
}

int vmbus_uio_get_subchan(struct vmbus_channel *primary,
			  struct vmbus_channel **subchan)
{
	const struct rte_vmbus_device *dev = primary->device;
	char chan_path[PATH_MAX], subchan_path[PATH_MAX];
	struct dirent *ent;
	DIR *chan_dir;
	int err;

	snprintf(chan_path, sizeof(chan_path),
		 "%s/%s/channels",
		 SYSFS_VMBUS_DEVICES, dev->device.name);

	chan_dir = opendir(chan_path);
	if (!chan_dir) {
		VMBUS_LOG(ERR, "cannot open %s: %s",
			  chan_path, strerror(errno));
		return -errno;
	}

	while ((ent = readdir(chan_dir))) {
		unsigned long relid, subid, monid;
		char *endp;

		if (ent->d_name[0] == '.')
			continue;

		errno = 0;
		relid = strtoul(ent->d_name, &endp, 0);
		if (*endp || errno != 0 || relid > UINT16_MAX) {
			VMBUS_LOG(NOTICE, "not a valid channel relid: %s",
				  ent->d_name);
			continue;
		}

		if (!vmbus_isnew_subchannel(primary, relid)) {
			VMBUS_LOG(DEBUG, "skip already found channel: %lu",
				  relid);
			continue;
		}

		if (!vmbus_uio_ring_present(dev, relid)) {
			VMBUS_LOG(DEBUG, "ring mmap not found (yet) for: %lu",
				  relid);
			continue;
		}

		snprintf(subchan_path, sizeof(subchan_path), "%s/%lu",
			 chan_path, relid);
		err = vmbus_uio_sysfs_read(subchan_path, "subchannel_id",
					   &subid, UINT16_MAX);
		if (err) {
			VMBUS_LOG(NOTICE, "no subchannel_id in %s:%s",
				  subchan_path, strerror(-err));
			goto fail;
		}

		if (subid == 0)
			continue;	/* skip primary channel */

		err = vmbus_uio_sysfs_read(subchan_path, "monitor_id",
					   &monid, UINT8_MAX);
		if (err) {
			VMBUS_LOG(NOTICE, "no monitor_id in %s:%s",
				  subchan_path, strerror(-err));
			goto fail;
		}

		err = vmbus_chan_create(dev, relid, subid, monid, subchan);
		if (err) {
			VMBUS_LOG(ERR, "subchannel setup failed");
			goto fail;
		}
		break;
	}
	closedir(chan_dir);

	return (ent == NULL) ? -ENOENT : 0;
fail:
	closedir(chan_dir);
	return err;
}