pci: fix build on ppc
[dpdk.git] / drivers / bus / ifpga / ifpga_bus.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <inttypes.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <sys/queue.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15
16 #include <rte_errno.h>
17 #include <rte_bus.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
20 #include <rte_memzone.h>
21 #include <rte_eal.h>
22 #include <rte_common.h>
23 #include <rte_devargs.h>
24 #include <rte_kvargs.h>
25 #include <rte_alarm.h>
26 #include <rte_string_fns.h>
27 #include <rte_debug.h>
28
29 #include "rte_rawdev.h"
30 #include "rte_rawdev_pmd.h"
31 #include "rte_bus_ifpga.h"
32 #include "ifpga_logs.h"
33 #include "ifpga_common.h"
34
35 int ifpga_bus_logtype;
36
37 /* Forward declaration to access Intel FPGA bus
38  * on which iFPGA devices are connected
39  */
40 static struct rte_bus rte_ifpga_bus;
41
42 static struct ifpga_afu_dev_list ifpga_afu_dev_list =
43         TAILQ_HEAD_INITIALIZER(ifpga_afu_dev_list);
44 static struct ifpga_afu_drv_list ifpga_afu_drv_list =
45         TAILQ_HEAD_INITIALIZER(ifpga_afu_drv_list);
46
47
48 /* register a ifpga bus based driver */
49 void rte_ifpga_driver_register(struct rte_afu_driver *driver)
50 {
51         RTE_VERIFY(driver);
52
53         TAILQ_INSERT_TAIL(&ifpga_afu_drv_list, driver, next);
54 }
55
56 /* un-register a fpga bus based driver */
57 void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
58 {
59         TAILQ_REMOVE(&ifpga_afu_drv_list, driver, next);
60 }
61
62 static struct rte_afu_device *
63 ifpga_find_afu_dev(const struct rte_rawdev *rdev,
64         const struct rte_afu_id *afu_id)
65 {
66         struct rte_afu_device *afu_dev = NULL;
67
68         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
69                 if (afu_dev &&
70                         afu_dev->rawdev == rdev &&
71                         !ifpga_afu_id_cmp(&afu_dev->id, afu_id))
72                         return afu_dev;
73         }
74         return NULL;
75 }
76
77 struct rte_afu_device *
78 rte_ifpga_find_afu_by_name(const char *name)
79 {
80         struct rte_afu_device *afu_dev = NULL;
81
82         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
83                 if (afu_dev &&
84                         !strcmp(afu_dev->device.name, name))
85                         return afu_dev;
86         }
87         return NULL;
88 }
89
90 static const char * const valid_args[] = {
91 #define IFPGA_ARG_NAME         "ifpga"
92         IFPGA_ARG_NAME,
93 #define IFPGA_ARG_PORT         "port"
94         IFPGA_ARG_PORT,
95 #define IFPGA_AFU_BTS          "afu_bts"
96         IFPGA_AFU_BTS,
97         NULL
98 };
99
100 /*
101  * Scan the content of the FPGA bus, and the devices in the devices
102  * list
103  */
104 static struct rte_afu_device *
105 ifpga_scan_one(struct rte_rawdev *rawdev,
106                 struct rte_devargs *devargs)
107 {
108         struct rte_kvargs *kvlist = NULL;
109         struct rte_afu_device *afu_dev = NULL;
110         struct rte_afu_pr_conf afu_pr_conf;
111         int ret = 0;
112         char *path = NULL;
113
114         memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
115
116         kvlist = rte_kvargs_parse(devargs->args, valid_args);
117         if (!kvlist) {
118                 IFPGA_BUS_ERR("error when parsing param");
119                 goto end;
120         }
121
122         if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
123                 if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
124                 &rte_ifpga_get_integer32_arg, &afu_pr_conf.afu_id.port) < 0) {
125                         IFPGA_BUS_ERR("error to parse %s",
126                                      IFPGA_ARG_PORT);
127                         goto end;
128                 }
129         } else {
130                 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
131                           IFPGA_ARG_PORT);
132                 goto end;
133         }
134
135         if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
136                 if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
137                                        &rte_ifpga_get_string_arg, &path) < 0) {
138                         IFPGA_BUS_ERR("Failed to parse %s",
139                                      IFPGA_AFU_BTS);
140                         goto end;
141                 }
142                 afu_pr_conf.pr_enable = 1;
143         } else {
144                 afu_pr_conf.pr_enable = 0;
145         }
146
147         afu_pr_conf.afu_id.uuid.uuid_low = 0;
148         afu_pr_conf.afu_id.uuid.uuid_high = 0;
149
150         if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
151                 goto end;
152
153         afu_dev = calloc(1, sizeof(*afu_dev));
154         if (!afu_dev)
155                 goto end;
156
157         afu_dev->device.bus = &rte_ifpga_bus;
158         afu_dev->device.devargs = devargs;
159         afu_dev->device.numa_node = SOCKET_ID_ANY;
160         afu_dev->device.name = devargs->name;
161         afu_dev->rawdev = rawdev;
162         afu_dev->id.uuid.uuid_low  = 0;
163         afu_dev->id.uuid.uuid_high = 0;
164         afu_dev->id.port      = afu_pr_conf.afu_id.port;
165
166         if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
167                 rawdev->dev_ops->dev_info_get(rawdev, afu_dev);
168
169         if (rawdev->dev_ops &&
170                 rawdev->dev_ops->dev_start &&
171                 rawdev->dev_ops->dev_start(rawdev))
172                 goto end;
173
174         strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
175         if (rawdev->dev_ops &&
176                 rawdev->dev_ops->firmware_load &&
177                 rawdev->dev_ops->firmware_load(rawdev,
178                                 &afu_pr_conf)){
179                 IFPGA_BUS_ERR("firmware load error %d\n", ret);
180                 goto end;
181         }
182         afu_dev->id.uuid.uuid_low  = afu_pr_conf.afu_id.uuid.uuid_low;
183         afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
184
185         rte_kvargs_free(kvlist);
186         free(path);
187         return afu_dev;
188
189 end:
190         if (kvlist)
191                 rte_kvargs_free(kvlist);
192         if (path)
193                 free(path);
194         if (afu_dev)
195                 free(afu_dev);
196
197         return NULL;
198 }
199
200 /*
201  * Scan the content of the FPGA bus, and the devices in the devices
202  * list
203  */
204 static int
205 ifpga_scan(void)
206 {
207         struct rte_devargs *devargs;
208         struct rte_kvargs *kvlist = NULL;
209         struct rte_rawdev *rawdev = NULL;
210         char *name = NULL;
211         char name1[RTE_RAWDEV_NAME_MAX_LEN];
212         struct rte_afu_device *afu_dev = NULL;
213
214         /* for FPGA devices we scan the devargs_list populated via cmdline */
215         RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
216                 if (devargs->bus != &rte_ifpga_bus)
217                         continue;
218
219                 kvlist = rte_kvargs_parse(devargs->args, valid_args);
220                 if (!kvlist) {
221                         IFPGA_BUS_ERR("error when parsing param");
222                         goto end;
223                 }
224
225                 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
226                         if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
227                                        &rte_ifpga_get_string_arg, &name) < 0) {
228                                 IFPGA_BUS_ERR("error to parse %s",
229                                      IFPGA_ARG_NAME);
230                                 goto end;
231                         }
232                 } else {
233                         IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
234                           IFPGA_ARG_NAME);
235                         goto end;
236                 }
237
238                 memset(name1, 0, sizeof(name1));
239                 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
240
241                 rawdev = rte_rawdev_pmd_get_named_dev(name1);
242                 if (!rawdev)
243                         goto end;
244
245                 afu_dev = ifpga_scan_one(rawdev, devargs);
246                 if (afu_dev != NULL)
247                         TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
248         }
249
250 end:
251         if (kvlist)
252                 rte_kvargs_free(kvlist);
253         if (name)
254                 free(name);
255
256         return 0;
257 }
258
259 /*
260  * Match the AFU Driver and AFU Device using the ID Table
261  */
262 static int
263 rte_afu_match(const struct rte_afu_driver *afu_drv,
264               const struct rte_afu_device *afu_dev)
265 {
266         const struct rte_afu_uuid *id_table;
267
268         for (id_table = afu_drv->id_table;
269                 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
270              id_table++) {
271                 /* check if device's identifiers match the driver's ones */
272                 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
273                                 id_table->uuid_high !=
274                                  afu_dev->id.uuid.uuid_high)
275                         continue;
276
277                 return 1;
278         }
279
280         return 0;
281 }
282
283 static int
284 ifpga_probe_one_driver(struct rte_afu_driver *drv,
285                         struct rte_afu_device *afu_dev)
286 {
287         int ret;
288
289         if (!rte_afu_match(drv, afu_dev))
290                 /* Match of device and driver failed */
291                 return 1;
292
293         /* reference driver structure */
294         afu_dev->driver = drv;
295
296         /* call the driver probe() function */
297         ret = drv->probe(afu_dev);
298         if (ret)
299                 afu_dev->driver = NULL;
300         else
301                 afu_dev->device.driver = &drv->driver;
302
303         return ret;
304 }
305
306 static int
307 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
308 {
309         struct rte_afu_driver *drv = NULL;
310         int ret = 0;
311
312         if (afu_dev == NULL)
313                 return -1;
314
315         /* Check if a driver is already loaded */
316         if (rte_dev_is_probed(&afu_dev->device)) {
317                 IFPGA_BUS_DEBUG("Device %s is already probed\n",
318                                 rte_ifpga_device_name(afu_dev));
319                 return -EEXIST;
320         }
321
322         TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
323                 ret = ifpga_probe_one_driver(drv, afu_dev);
324                 if (ret < 0)
325                         /* negative value is an error */
326                         return ret;
327                 if (ret > 0)
328                         /* positive value means driver doesn't support it */
329                         continue;
330                 return 0;
331         }
332         if ((ret > 0) && (afu_dev->driver == NULL))
333                 return 0;
334         else
335                 return ret;
336 }
337
338 /*
339  * Scan the content of the Intel FPGA bus, and call the probe() function for
340  * all registered drivers that have a matching entry in its id_table
341  * for discovered devices.
342  */
343 static int
344 ifpga_probe(void)
345 {
346         struct rte_afu_device *afu_dev = NULL;
347         int ret = 0;
348
349         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
350                 ret = ifpga_probe_all_drivers(afu_dev);
351                 if (ret == -EEXIST)
352                         continue;
353                 if (ret < 0)
354                         IFPGA_BUS_ERR("failed to initialize %s device\n",
355                                 rte_ifpga_device_name(afu_dev));
356         }
357
358         return ret;
359 }
360
361 static int
362 ifpga_plug(struct rte_device *dev)
363 {
364         return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
365 }
366
367 static int
368 ifpga_remove_driver(struct rte_afu_device *afu_dev)
369 {
370         const char *name;
371
372         name = rte_ifpga_device_name(afu_dev);
373         if (afu_dev->driver == NULL) {
374                 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
375                 return 1;
376         }
377
378         return afu_dev->driver->remove(afu_dev);
379 }
380
381 static int
382 ifpga_unplug(struct rte_device *dev)
383 {
384         struct rte_afu_device *afu_dev = NULL;
385         int ret;
386
387         if (dev == NULL)
388                 return -EINVAL;
389
390         afu_dev = RTE_DEV_TO_AFU(dev);
391         if (!afu_dev)
392                 return -ENOENT;
393
394         ret = ifpga_remove_driver(afu_dev);
395         if (ret)
396                 return ret;
397
398         TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
399
400         rte_devargs_remove(dev->devargs);
401         free(afu_dev);
402         return 0;
403
404 }
405
406 static struct rte_device *
407 ifpga_find_device(const struct rte_device *start,
408         rte_dev_cmp_t cmp, const void *data)
409 {
410         struct rte_afu_device *afu_dev;
411
412         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
413                 if (start && &afu_dev->device == start) {
414                         start = NULL;
415                         continue;
416                 }
417                 if (cmp(&afu_dev->device, data) == 0)
418                         return &afu_dev->device;
419         }
420
421         return NULL;
422 }
423 static int
424 ifpga_parse(const char *name, void *addr)
425 {
426         int *out = addr;
427         struct rte_rawdev *rawdev = NULL;
428         char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
429         char *c1 = NULL;
430         char *c2 = NULL;
431         int port = IFPGA_BUS_DEV_PORT_MAX;
432         char str_port[8];
433         int str_port_len = 0;
434         int ret;
435
436         memset(str_port, 0, 8);
437         c1 = strchr(name, '|');
438         if (c1 != NULL) {
439                 str_port_len = c1 - name;
440                 c2 = c1 + 1;
441         }
442
443         if (str_port_len < 8 &&
444                 str_port_len > 0) {
445                 memcpy(str_port, name, str_port_len);
446                 ret = sscanf(str_port, "%d", &port);
447                 if (ret == -1)
448                         return 0;
449         }
450
451         memset(rawdev_name, 0, sizeof(rawdev_name));
452         snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
453         rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
454
455         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
456                 rawdev &&
457                 (addr != NULL))
458                 *out = port;
459
460         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
461                 rawdev)
462                 return 0;
463         else
464                 return 1;
465 }
466
467 static struct rte_bus rte_ifpga_bus = {
468         .scan        = ifpga_scan,
469         .probe       = ifpga_probe,
470         .find_device = ifpga_find_device,
471         .plug        = ifpga_plug,
472         .unplug      = ifpga_unplug,
473         .parse       = ifpga_parse,
474 };
475
476 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
477
478 RTE_INIT(ifpga_init_log)
479 {
480         ifpga_bus_logtype = rte_log_register("bus.ifpga");
481         if (ifpga_bus_logtype >= 0)
482                 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);
483 }