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