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