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