aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/bus/vmbus/vmbus_common.c
blob: c7165ad54fe238c4eeefa187d478117cd91ca81d (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
/* 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/queue.h>
#include <sys/mman.h>

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

#include "private.h"

int vmbus_logtype_bus;
extern struct rte_vmbus_bus rte_vmbus_bus;

/* map a particular resource from a file */
void *
vmbus_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
		   int flags)
{
	void *mapaddr;

	/* Map the memory resource of device */
	mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
		       MAP_SHARED | flags, fd, offset);
	if (mapaddr == MAP_FAILED) {
		VMBUS_LOG(ERR,
			  "mmap(%d, %p, %zu, %ld) failed: %s",
			  fd, requested_addr, size, (long)offset,
			  strerror(errno));
	}
	return mapaddr;
}

/* unmap a particular resource */
void
vmbus_unmap_resource(void *requested_addr, size_t size)
{
	if (requested_addr == NULL)
		return;

	/* Unmap the VMBUS memory resource of device */
	if (munmap(requested_addr, size)) {
		VMBUS_LOG(ERR, "munmap(%p, 0x%lx) failed: %s",
			requested_addr, (unsigned long)size,
			strerror(errno));
	} else
		VMBUS_LOG(DEBUG, "  VMBUS memory unmapped at %p",
			  requested_addr);
}

/**
 * Match the VMBUS driver and device using UUID table
 *
 * @param drv
 *	VMBUS driver from which ID table would be extracted
 * @param pci_dev
 *	VMBUS device to match against the driver
 * @return
 *	true for successful match
 *	false for unsuccessful match
 */
static bool
vmbus_match(const struct rte_vmbus_driver *dr,
	    const struct rte_vmbus_device *dev)
{
	const rte_uuid_t *id_table;

	for (id_table = dr->id_table; !rte_uuid_is_null(*id_table); ++id_table) {
		if (rte_uuid_compare(*id_table, dev->class_id) == 0)
			return true;
	}

	return false;
}

/*
 * If device ID match, call the devinit() function of the driver.
 */
static int
vmbus_probe_one_driver(struct rte_vmbus_driver *dr,
		       struct rte_vmbus_device *dev)
{
	char guid[RTE_UUID_STRLEN];
	int ret;

	if (!vmbus_match(dr, dev))
		return 1;	 /* not supported */

	rte_uuid_unparse(dev->device_id, guid, sizeof(guid));
	VMBUS_LOG(INFO, "VMBUS device %s on NUMA socket %i",
		  guid, dev->device.numa_node);

	/* TODO add blacklisted */

	/* map resources for device */
	ret = rte_vmbus_map_device(dev);
	if (ret != 0)
		return ret;

	/* reference driver structure */
	dev->driver = dr;
	dev->device.driver = &dr->driver;

	if (dev->device.numa_node < 0) {
		VMBUS_LOG(WARNING, "  Invalid NUMA socket, default to 0");
		dev->device.numa_node = 0;
	}

	/* call the driver probe() function */
	VMBUS_LOG(INFO, "  probe driver: %s", dr->driver.name);
	ret = dr->probe(dr, dev);
	if (ret) {
		dev->driver = NULL;
		rte_vmbus_unmap_device(dev);
	}

	return ret;
}

/*
 * IF device class GUID mathces, call the probe function of
 * registere drivers for the vmbus device.
 * Return -1 if initialization failed,
 * and 1 if no driver found for this device.
 */
static int
vmbus_probe_all_drivers(struct rte_vmbus_device *dev)
{
	struct rte_vmbus_driver *dr;
	int rc;

	/* Check if a driver is already loaded */
	if (dev->driver != NULL) {
		VMBUS_LOG(DEBUG, "VMBUS driver already loaded");
		return 0;
	}

	FOREACH_DRIVER_ON_VMBUS(dr) {
		rc = vmbus_probe_one_driver(dr, dev);
		if (rc < 0) /* negative is an error */
			return -1;

		if (rc > 0) /* positive driver doesn't support it */
			continue;

		return 0;
	}
	return 1;
}

/*
 * Scan the vmbus, and call the devinit() function for
 * all registered drivers that have a matching entry in its id_table
 * for discovered devices.
 */
int
rte_vmbus_probe(void)
{
	struct rte_vmbus_device *dev;
	size_t probed = 0, failed = 0;
	char ubuf[RTE_UUID_STRLEN];

	FOREACH_DEVICE_ON_VMBUS(dev) {
		probed++;

		rte_uuid_unparse(dev->device_id, ubuf, sizeof(ubuf));

		/* TODO: add whitelist/blacklist */

		if (vmbus_probe_all_drivers(dev) < 0) {
			VMBUS_LOG(NOTICE,
				"Requested device %s cannot be used", ubuf);
			rte_errno = errno;
			failed++;
		}
	}

	return (probed && probed == failed) ? -1 : 0;
}

static int
vmbus_parse(const char *name, void *addr)
{
	rte_uuid_t guid;
	int ret;

	ret = rte_uuid_parse(name, guid);
	if (ret == 0 && addr)
		memcpy(addr, &guid, sizeof(guid));

	return ret;
}

/* register vmbus driver */
void
rte_vmbus_register(struct rte_vmbus_driver *driver)
{
	VMBUS_LOG(DEBUG,
		"Registered driver %s", driver->driver.name);

	TAILQ_INSERT_TAIL(&rte_vmbus_bus.driver_list, driver, next);
	driver->bus = &rte_vmbus_bus;
}

/* unregister vmbus driver */
void
rte_vmbus_unregister(struct rte_vmbus_driver *driver)
{
	TAILQ_REMOVE(&rte_vmbus_bus.driver_list, driver, next);
	driver->bus = NULL;
}

/* Add a device to VMBUS bus */
void
vmbus_add_device(struct rte_vmbus_device *vmbus_dev)
{
	TAILQ_INSERT_TAIL(&rte_vmbus_bus.device_list, vmbus_dev, next);
}

/* Insert a device into a predefined position in VMBUS bus */
void
vmbus_insert_device(struct rte_vmbus_device *exist_vmbus_dev,
		      struct rte_vmbus_device *new_vmbus_dev)
{
	TAILQ_INSERT_BEFORE(exist_vmbus_dev, new_vmbus_dev, next);
}

/* Remove a device from VMBUS bus */
void
vmbus_remove_device(struct rte_vmbus_device *vmbus_dev)
{
	TAILQ_REMOVE(&rte_vmbus_bus.device_list, vmbus_dev, next);
}

/* VMBUS doesn't support hotplug */
static struct rte_device *
vmbus_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
		  const void *data)
{
	struct rte_vmbus_device *dev;

	FOREACH_DEVICE_ON_VMBUS(dev) {
		if (start && &dev->device == start) {
			start = NULL;
			continue;
		}
		if (cmp(&dev->device, data) == 0)
			return &dev->device;
	}

	return NULL;
}


struct rte_vmbus_bus rte_vmbus_bus = {
	.bus = {
		.scan = rte_vmbus_scan,
		.probe = rte_vmbus_probe,
		.find_device = vmbus_find_device,
		.parse = vmbus_parse,
	},
	.device_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.device_list),
	.driver_list = TAILQ_HEAD_INITIALIZER(rte_vmbus_bus.driver_list),
};

RTE_REGISTER_BUS(vmbus, rte_vmbus_bus.bus);

RTE_INIT(vmbus_init_log)
{
	vmbus_logtype_bus = rte_log_register("bus.vmbus");
	if (vmbus_logtype_bus >= 0)
		rte_log_set_level(vmbus_logtype_bus, RTE_LOG_NOTICE);
}