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