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