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