summaryrefslogtreecommitdiff
path: root/drivers/bus/ifpga/ifpga_bus.c
blob: c54b59db2a7c9599bdadf05d31be53c6e7f1d2bc (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
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2018 Intel Corporation
 */

#include <string.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/queue.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#include <rte_errno.h>
#include <rte_bus.h>
#include <rte_per_lcore.h>
#include <rte_memory.h>
#include <rte_memzone.h>
#include <rte_eal.h>
#include <rte_common.h>
#include <rte_devargs.h>
#include <rte_kvargs.h>
#include <rte_alarm.h>
#include <rte_string_fns.h>

#include "rte_rawdev.h"
#include "rte_rawdev_pmd.h"
#include "rte_bus_ifpga.h"
#include "ifpga_logs.h"
#include "ifpga_common.h"

int ifpga_bus_logtype;

/* Forward declaration to access Intel FPGA bus
 * on which iFPGA devices are connected
 */
static struct rte_bus rte_ifpga_bus;

static struct ifpga_afu_dev_list ifpga_afu_dev_list =
	TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list);
static struct ifpga_afu_drv_list ifpga_afu_drv_list =
	TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list);


/* register a ifpga bus based driver */
void rte_ifpga_driver_register(struct rte_afu_driver *driver)
{
	RTE_VERIFY(driver);

	TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
}

/* un-register a fpga bus based driver */
void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
{
	TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
}

static struct rte_afu_device *
ifpga_find_afu_dev(const struct rte_rawdev *rdev,
	const struct rte_afu_id *afu_id)
{
	struct rte_afu_device *afu_dev = NULL;

	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
		if (afu_dev &&
			afu_dev->rawdev == rdev &&
			!ifpga_afu_id_cmp(&afu_dev->id, afu_id))
			return afu_dev;
	}
	return NULL;
}

static const char * const valid_args[] = {
#define IFPGA_ARG_NAME         "ifpga"
	IFPGA_ARG_NAME,
#define IFPGA_ARG_PORT         "port"
	IFPGA_ARG_PORT,
#define IFPGA_AFU_BTS          "afu_bts"
	IFPGA_AFU_BTS,
	NULL
};

/*
 * Scan the content of the FPGA bus, and the devices in the devices
 * list
 */
static struct rte_afu_device *
ifpga_scan_one(struct rte_rawdev *rawdev,
		struct rte_devargs *devargs)
{
	struct rte_kvargs *kvlist = NULL;
	struct rte_afu_device *afu_dev = NULL;
	struct rte_afu_pr_conf afu_pr_conf;
	int ret = 0;
	char *path = NULL;

	memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));

	kvlist = rte_kvargs_parse(devargs->args, valid_args);
	if (!kvlist) {
		IFPGA_BUS_ERR("error when parsing param");
		goto end;
	}

	if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
		if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
		&rte_ifpga_get_integer32_arg, &afu_pr_conf.afu_id.port) < 0) {
			IFPGA_BUS_ERR("error to parse %s",
				     IFPGA_ARG_PORT);
			goto end;
		}
	} else {
		IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
			  IFPGA_ARG_PORT);
		goto end;
	}

	if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
		if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
				       &rte_ifpga_get_string_arg, &path) < 0) {
			IFPGA_BUS_ERR("Failed to parse %s",
				     IFPGA_AFU_BTS);
			goto end;
		}
	} else {
		IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
			  IFPGA_AFU_BTS);
		goto end;
	}

	afu_pr_conf.afu_id.uuid.uuid_low = 0;
	afu_pr_conf.afu_id.uuid.uuid_high = 0;
	afu_pr_conf.pr_enable = path?1:0;

	if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
		goto end;

	afu_dev = calloc(1, sizeof(*afu_dev));
	if (!afu_dev)
		goto end;

	afu_dev->device.devargs = devargs;
	afu_dev->device.numa_node = SOCKET_ID_ANY;
	afu_dev->device.name = devargs->name;
	afu_dev->rawdev = rawdev;
	afu_dev->id.uuid.uuid_low  = 0;
	afu_dev->id.uuid.uuid_high = 0;
	afu_dev->id.port      = afu_pr_conf.afu_id.port;

	if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
		rawdev->dev_ops->dev_info_get(rawdev, afu_dev);

	if (rawdev->dev_ops &&
		rawdev->dev_ops->dev_start &&
		rawdev->dev_ops->dev_start(rawdev))
		goto end;

	strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
	if (rawdev->dev_ops &&
		rawdev->dev_ops->firmware_load &&
		rawdev->dev_ops->firmware_load(rawdev,
				&afu_pr_conf)){
		IFPGA_BUS_ERR("firmware load error %d\n", ret);
		goto end;
	}
	afu_dev->id.uuid.uuid_low  = afu_pr_conf.afu_id.uuid.uuid_low;
	afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;

	rte_kvargs_free(kvlist);
	free(path);
	return afu_dev;

end:
	if (kvlist)
		rte_kvargs_free(kvlist);
	if (path)
		free(path);
	if (afu_dev)
		free(afu_dev);

	return NULL;
}

/*
 * Scan the content of the FPGA bus, and the devices in the devices
 * list
 */
static int
ifpga_scan(void)
{
	struct rte_devargs *devargs;
	struct rte_kvargs *kvlist = NULL;
	struct rte_rawdev *rawdev = NULL;
	char *name = NULL;
	char name1[RTE_RAWDEV_NAME_MAX_LEN];
	struct rte_afu_device *afu_dev = NULL;

	/* for FPGA devices we scan the devargs_list populated via cmdline */
	RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
		if (devargs->bus != &rte_ifpga_bus)
			continue;

		kvlist = rte_kvargs_parse(devargs->args, valid_args);
		if (!kvlist) {
			IFPGA_BUS_ERR("error when parsing param");
			goto end;
		}

		if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
			if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
				       &rte_ifpga_get_string_arg, &name) < 0) {
				IFPGA_BUS_ERR("error to parse %s",
				     IFPGA_ARG_NAME);
				goto end;
			}
		} else {
			IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
			  IFPGA_ARG_NAME);
			goto end;
		}

		memset(name1, 0, sizeof(name1));
		snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);

		rawdev = rte_rawdev_pmd_get_named_dev(name1);
		if (!rawdev)
			goto end;

		afu_dev = ifpga_scan_one(rawdev, devargs);
		if (afu_dev != NULL)
			TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
	}

end:
	if (kvlist)
		rte_kvargs_free(kvlist);
	if (name)
		free(name);

	return 0;
}

/*
 * Match the AFU Driver and AFU Device using the ID Table
 */
static int
rte_afu_match(const struct rte_afu_driver *afu_drv,
	      const struct rte_afu_device *afu_dev)
{
	const struct rte_afu_uuid *id_table;

	for (id_table = afu_drv->id_table;
		((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
	     id_table++) {
		/* check if device's identifiers match the driver's ones */
		if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
				id_table->uuid_high !=
				 afu_dev->id.uuid.uuid_high)
			continue;

		return 1;
	}

	return 0;
}

static int
ifpga_probe_one_driver(struct rte_afu_driver *drv,
			struct rte_afu_device *afu_dev)
{
	int ret;

	if (!rte_afu_match(drv, afu_dev))
		/* Match of device and driver failed */
		return 1;

	/* reference driver structure */
	afu_dev->driver = drv;
	afu_dev->device.driver = &drv->driver;

	/* call the driver probe() function */
	ret = drv->probe(afu_dev);
	if (ret) {
		afu_dev->driver = NULL;
		afu_dev->device.driver = NULL;
	}

	return ret;
}

static int
ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
{
	struct rte_afu_driver *drv = NULL;
	int ret = 0;

	if (afu_dev == NULL)
		return -1;

	/* Check if a driver is already loaded */
	if (afu_dev->driver != NULL)
		return 0;

	TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
		if (ifpga_probe_one_driver(drv, afu_dev)) {
			ret = -1;
			break;
		}
	}
	return ret;
}

/*
 * Scan the content of the Intel FPGA bus, and call the probe() function for
 * all registered drivers that have a matching entry in its id_table
 * for discovered devices.
 */
static int
ifpga_probe(void)
{
	struct rte_afu_device *afu_dev = NULL;
	int ret = 0;

	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
		if (afu_dev->device.driver)
			continue;

		ret = ifpga_probe_all_drivers(afu_dev);
		if (ret < 0)
			IFPGA_BUS_ERR("failed to initialize %s device\n",
				rte_ifpga_device_name(afu_dev));
		}

	return ret;
}

static int
ifpga_plug(struct rte_device *dev)
{
	return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
}

static int
ifpga_remove_driver(struct rte_afu_device *afu_dev)
{
	const char *name;

	name = rte_ifpga_device_name(afu_dev);
	if (afu_dev->driver == NULL) {
		IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
		return 1;
	}

	return afu_dev->driver->remove(afu_dev);
}

static int
ifpga_unplug(struct rte_device *dev)
{
	struct rte_afu_device *afu_dev = NULL;
	struct rte_devargs *devargs = NULL;
	int ret;

	if (dev == NULL)
		return -EINVAL;

	afu_dev = RTE_DEV_TO_AFU(dev);
	if (!afu_dev)
		return -ENOENT;

	devargs = dev->devargs;

	ret = ifpga_remove_driver(afu_dev);
	if (ret)
		return ret;

	TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);

	rte_devargs_remove(devargs->bus->name, devargs->name);
	free(afu_dev);
	return 0;

}

static struct rte_device *
ifpga_find_device(const struct rte_device *start,
	rte_dev_cmp_t cmp, const void *data)
{
	struct rte_afu_device *afu_dev;

	TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
		if (start && &afu_dev->device == start) {
			start = NULL;
			continue;
		}
		if (cmp(&afu_dev->device, data) == 0)
			return &afu_dev->device;
	}

	return NULL;
}
static int
ifpga_parse(const char *name, void *addr)
{
	int *out = addr;
	struct rte_rawdev *rawdev = NULL;
	char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
	char *c1 = NULL;
	char *c2 = NULL;
	int port = IFPGA_BUS_DEV_PORT_MAX;
	char str_port[8];
	int str_port_len = 0;
	int ret;

	memset(str_port, 0, 8);
	c1 = strchr(name, '|');
	if (c1 != NULL) {
		str_port_len = c1 - name;
		c2 = c1 + 1;
	}

	if (str_port_len < 8 &&
		str_port_len > 0) {
		memcpy(str_port, name, str_port_len);
		ret = sscanf(str_port, "%d", &port);
		if (ret == -1)
			return 0;
	}

	memset(rawdev_name, 0, sizeof(rawdev_name));
	snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
	rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);

	if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
		rawdev &&
		(addr != NULL))
		*out = port;

	if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
		rawdev)
		return 0;
	else
		return 1;
}

static struct rte_bus rte_ifpga_bus = {
	.scan        = ifpga_scan,
	.probe       = ifpga_probe,
	.find_device = ifpga_find_device,
	.plug        = ifpga_plug,
	.unplug      = ifpga_unplug,
	.parse       = ifpga_parse,
};

RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);

RTE_INIT(ifpga_init_log)
{
	ifpga_bus_logtype = rte_log_register("bus.ifpga");
	if (ifpga_bus_logtype >= 0)
		rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);
}