bus/fpga: use strlcpy instead of strncpy
[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         } else {
129                 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
130                           IFPGA_AFU_BTS);
131                 goto end;
132         }
133
134         afu_pr_conf.afu_id.uuid.uuid_low = 0;
135         afu_pr_conf.afu_id.uuid.uuid_high = 0;
136         afu_pr_conf.pr_enable = path?1:0;
137
138         if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
139                 goto end;
140
141         afu_dev = calloc(1, sizeof(*afu_dev));
142         if (!afu_dev)
143                 goto end;
144
145         afu_dev->device.devargs = devargs;
146         afu_dev->device.numa_node = SOCKET_ID_ANY;
147         afu_dev->device.name = devargs->name;
148         afu_dev->rawdev = rawdev;
149         afu_dev->id.uuid.uuid_low  = 0;
150         afu_dev->id.uuid.uuid_high = 0;
151         afu_dev->id.port      = afu_pr_conf.afu_id.port;
152
153         if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
154                 rawdev->dev_ops->dev_info_get(rawdev, afu_dev);
155
156         if (rawdev->dev_ops &&
157                 rawdev->dev_ops->dev_start &&
158                 rawdev->dev_ops->dev_start(rawdev))
159                 goto free_dev;
160
161         strlcpy(afu_pr_conf.bs_path, path, sizeof(afu_pr_conf.bs_path));
162         if (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 free_dev;
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         return afu_dev;
172
173 free_dev:
174         free(afu_dev);
175 end:
176         if (kvlist)
177                 rte_kvargs_free(kvlist);
178         if (path)
179                 free(path);
180
181         return NULL;
182 }
183
184 /*
185  * Scan the content of the FPGA bus, and the devices in the devices
186  * list
187  */
188 static int
189 ifpga_scan(void)
190 {
191         struct rte_devargs *devargs;
192         struct rte_kvargs *kvlist = NULL;
193         struct rte_rawdev *rawdev = NULL;
194         char *name = NULL;
195         char name1[RTE_RAWDEV_NAME_MAX_LEN];
196         struct rte_afu_device *afu_dev = NULL;
197
198         /* for FPGA devices we scan the devargs_list populated via cmdline */
199         RTE_EAL_DEVARGS_FOREACH(IFPGA_ARG_NAME, devargs) {
200                 if (devargs->bus != &rte_ifpga_bus)
201                         continue;
202
203                 kvlist = rte_kvargs_parse(devargs->args, valid_args);
204                 if (!kvlist) {
205                         IFPGA_BUS_ERR("error when parsing param");
206                         goto end;
207                 }
208
209                 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
210                         if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
211                                        &rte_ifpga_get_string_arg, &name) < 0) {
212                                 IFPGA_BUS_ERR("error to parse %s",
213                                      IFPGA_ARG_NAME);
214                                 goto end;
215                         }
216                 } else {
217                         IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
218                           IFPGA_ARG_NAME);
219                         goto end;
220                 }
221
222                 memset(name1, 0, sizeof(name1));
223                 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
224
225                 rawdev = rte_rawdev_pmd_get_named_dev(name1);
226                 if (!rawdev)
227                         goto end;
228
229                 afu_dev = ifpga_scan_one(rawdev, devargs);
230                 if (afu_dev != NULL)
231                         TAILQ_INSERT_TAIL(&ifpga_afu_dev_list, afu_dev, next);
232         }
233
234 end:
235         if (kvlist)
236                 rte_kvargs_free(kvlist);
237         if (name)
238                 free(name);
239
240         return 0;
241 }
242
243 /*
244  * Match the AFU Driver and AFU Device using the ID Table
245  */
246 static int
247 rte_afu_match(const struct rte_afu_driver *afu_drv,
248               const struct rte_afu_device *afu_dev)
249 {
250         const struct rte_afu_uuid *id_table;
251
252         for (id_table = afu_drv->id_table;
253                 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
254              id_table++) {
255                 /* check if device's identifiers match the driver's ones */
256                 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
257                                 id_table->uuid_high !=
258                                  afu_dev->id.uuid.uuid_high)
259                         continue;
260
261                 return 1;
262         }
263
264         return 0;
265 }
266
267 static int
268 ifpga_probe_one_driver(struct rte_afu_driver *drv,
269                         struct rte_afu_device *afu_dev)
270 {
271         int ret;
272
273         if (!rte_afu_match(drv, afu_dev))
274                 /* Match of device and driver failed */
275                 return 1;
276
277         /* reference driver structure */
278         afu_dev->driver = drv;
279         afu_dev->device.driver = &drv->driver;
280
281         /* call the driver probe() function */
282         ret = drv->probe(afu_dev);
283         if (ret) {
284                 afu_dev->driver = NULL;
285                 afu_dev->device.driver = NULL;
286         }
287
288         return ret;
289 }
290
291 static int
292 ifpga_probe_all_drivers(struct rte_afu_device *afu_dev)
293 {
294         struct rte_afu_driver *drv = NULL;
295         int ret = 0;
296
297         if (afu_dev == NULL)
298                 return -1;
299
300         /* Check if a driver is already loaded */
301         if (afu_dev->driver != NULL)
302                 return 0;
303
304         TAILQ_FOREACH(drv, &ifpga_afu_drv_list, next) {
305                 if (ifpga_probe_one_driver(drv, afu_dev)) {
306                         ret = -1;
307                         break;
308                 }
309         }
310         return ret;
311 }
312
313 /*
314  * Scan the content of the Intel FPGA bus, and call the probe() function for
315  * all registered drivers that have a matching entry in its id_table
316  * for discovered devices.
317  */
318 static int
319 ifpga_probe(void)
320 {
321         struct rte_afu_device *afu_dev = NULL;
322         int ret = 0;
323
324         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
325                 if (afu_dev->device.driver)
326                         continue;
327
328                 ret = ifpga_probe_all_drivers(afu_dev);
329                 if (ret < 0)
330                         IFPGA_BUS_ERR("failed to initialize %s device\n",
331                                 rte_ifpga_device_name(afu_dev));
332                 }
333
334         return ret;
335 }
336
337 static int
338 ifpga_plug(struct rte_device *dev)
339 {
340         return ifpga_probe_all_drivers(RTE_DEV_TO_AFU(dev));
341 }
342
343 static int
344 ifpga_remove_driver(struct rte_afu_device *afu_dev)
345 {
346         const char *name;
347         const struct rte_afu_driver *driver;
348
349         name = rte_ifpga_device_name(afu_dev);
350         if (!afu_dev->device.driver) {
351                 IFPGA_BUS_DEBUG("no driver attach to device %s\n", name);
352                 return 1;
353         }
354
355         driver = RTE_DRV_TO_AFU_CONST(afu_dev->device.driver);
356         return driver->remove(afu_dev);
357 }
358
359 static int
360 ifpga_unplug(struct rte_device *dev)
361 {
362         struct rte_afu_device *afu_dev = NULL;
363         struct rte_devargs *devargs = NULL;
364         int ret;
365
366         if (dev == NULL)
367                 return -EINVAL;
368
369         afu_dev = RTE_DEV_TO_AFU(dev);
370         if (!dev)
371                 return -ENOENT;
372
373         devargs = dev->devargs;
374
375         ret = ifpga_remove_driver(afu_dev);
376         if (ret)
377                 return ret;
378
379         TAILQ_REMOVE(&ifpga_afu_dev_list, afu_dev, next);
380
381         rte_devargs_remove(devargs->bus->name, devargs->name);
382         free(afu_dev);
383         return 0;
384
385 }
386
387 static struct rte_device *
388 ifpga_find_device(const struct rte_device *start,
389         rte_dev_cmp_t cmp, const void *data)
390 {
391         struct rte_afu_device *afu_dev;
392
393         TAILQ_FOREACH(afu_dev, &ifpga_afu_dev_list, next) {
394                 if (start && &afu_dev->device == start) {
395                         start = NULL;
396                         continue;
397                 }
398                 if (cmp(&afu_dev->device, data) == 0)
399                         return &afu_dev->device;
400         }
401
402         return NULL;
403 }
404 static int
405 ifpga_parse(const char *name, void *addr)
406 {
407         int *out = addr;
408         struct rte_rawdev *rawdev = NULL;
409         char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
410         char *c1 = NULL;
411         char *c2 = NULL;
412         int port = IFPGA_BUS_DEV_PORT_MAX;
413         char str_port[8];
414         int str_port_len = 0;
415         int ret;
416
417         memset(str_port, 0, 8);
418         c1 = strchr(name, '|');
419         if (c1 != NULL) {
420                 str_port_len = c1 - name;
421                 c2 = c1 + 1;
422         }
423
424         if (str_port_len < 8 &&
425                 str_port_len > 0) {
426                 memcpy(str_port, name, str_port_len);
427                 ret = sscanf(str_port, "%d", &port);
428                 if (ret == -1)
429                         return 0;
430         }
431
432         memset(rawdev_name, 0, sizeof(rawdev_name));
433         snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
434         rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
435
436         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
437                 rawdev &&
438                 (addr != NULL))
439                 *out = port;
440
441         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
442                 rawdev)
443                 return 0;
444         else
445                 return 1;
446 }
447
448 static struct rte_bus rte_ifpga_bus = {
449         .scan        = ifpga_scan,
450         .probe       = ifpga_probe,
451         .find_device = ifpga_find_device,
452         .plug        = ifpga_plug,
453         .unplug      = ifpga_unplug,
454         .parse       = ifpga_parse,
455 };
456
457 RTE_REGISTER_BUS(IFPGA_BUS_NAME, rte_ifpga_bus);
458
459 RTE_INIT(ifpga_init_log)
460 {
461         ifpga_bus_logtype = rte_log_register("bus.ifpga");
462         if (ifpga_bus_logtype >= 0)
463                 rte_log_set_level(ifpga_bus_logtype, RTE_LOG_NOTICE);
464 }