dc2b3ad811f503dc49fdce54560b5ae3829d784d
[dpdk.git] / drivers / bus / dpaa / dpaa_bus.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2017 NXP.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of NXP nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /* System headers */
33 #include <stdio.h>
34 #include <inttypes.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sched.h>
38 #include <signal.h>
39 #include <pthread.h>
40 #include <sys/types.h>
41 #include <sys/syscall.h>
42
43 #include <rte_config.h>
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_interrupts.h>
47 #include <rte_log.h>
48 #include <rte_debug.h>
49 #include <rte_pci.h>
50 #include <rte_atomic.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_alarm.h>
57 #include <rte_ether.h>
58 #include <rte_ethdev.h>
59 #include <rte_malloc.h>
60 #include <rte_ring.h>
61 #include <rte_bus.h>
62
63 #include <rte_dpaa_bus.h>
64 #include <rte_dpaa_logs.h>
65
66 #include <fsl_usd.h>
67 #include <fsl_qman.h>
68 #include <fsl_bman.h>
69 #include <of.h>
70 #include <netcfg.h>
71
72 int dpaa_logtype_bus;
73 int dpaa_logtype_mempool;
74
75 struct rte_dpaa_bus rte_dpaa_bus;
76 struct netcfg_info *dpaa_netcfg;
77
78 /* define a variable to hold the portal_key, once created.*/
79 pthread_key_t dpaa_portal_key;
80
81 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
82
83 static inline void
84 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
85 {
86         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
87 }
88
89 static inline void
90 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
91 {
92         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
93 }
94
95 static void dpaa_clean_device_list(void);
96
97 static int
98 dpaa_create_device_list(void)
99 {
100         int i;
101         int ret;
102         struct rte_dpaa_device *dev;
103         struct fm_eth_port_cfg *cfg;
104         struct fman_if *fman_intf;
105
106         /* Creating Ethernet Devices */
107         for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
108                 dev = calloc(1, sizeof(struct rte_dpaa_device));
109                 if (!dev) {
110                         DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
111                         ret = -ENOMEM;
112                         goto cleanup;
113                 }
114
115                 cfg = &dpaa_netcfg->port_cfg[i];
116                 fman_intf = cfg->fman_if;
117
118                 /* Device identifiers */
119                 dev->id.fman_id = fman_intf->fman_idx + 1;
120                 dev->id.mac_id = fman_intf->mac_idx;
121                 dev->device_type = FSL_DPAA_ETH;
122                 dev->id.dev_id = i;
123
124                 /* Create device name */
125                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
126                 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
127                         fman_intf->mac_idx);
128                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
129                 dev->device.name = dev->name;
130
131                 dpaa_add_to_device_list(dev);
132         }
133
134         rte_dpaa_bus.device_count = i;
135
136         return 0;
137
138 cleanup:
139         dpaa_clean_device_list();
140         return ret;
141 }
142
143 static void
144 dpaa_clean_device_list(void)
145 {
146         struct rte_dpaa_device *dev = NULL;
147         struct rte_dpaa_device *tdev = NULL;
148
149         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
150                 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
151                 free(dev);
152                 dev = NULL;
153         }
154 }
155
156 /** XXX move this function into a separate file */
157 static int
158 _dpaa_portal_init(void *arg)
159 {
160         cpu_set_t cpuset;
161         pthread_t id;
162         uint32_t cpu = rte_lcore_id();
163         int ret;
164         struct dpaa_portal *dpaa_io_portal;
165
166         BUS_INIT_FUNC_TRACE();
167
168         if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
169                 cpu = rte_get_master_lcore();
170         /* if the core id is not supported */
171         else
172                 if (cpu >= RTE_MAX_LCORE)
173                         return -1;
174
175         /* Set CPU affinity for this thread */
176         CPU_ZERO(&cpuset);
177         CPU_SET(cpu, &cpuset);
178         id = pthread_self();
179         ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
180         if (ret) {
181                 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
182                         "core :%d with ret: %d", cpu, ret);
183                 return ret;
184         }
185
186         /* Initialise bman thread portals */
187         ret = bman_thread_init();
188         if (ret) {
189                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
190                         "core %d with ret: %d", cpu, ret);
191                 return ret;
192         }
193
194         DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
195
196         /* Initialise qman thread portals */
197         ret = qman_thread_init();
198         if (ret) {
199                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
200                         "core %d with ret: %d", cpu, ret);
201                 bman_thread_finish();
202                 return ret;
203         }
204
205         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
206
207         dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
208                                     RTE_CACHE_LINE_SIZE);
209         if (!dpaa_io_portal) {
210                 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
211                 bman_thread_finish();
212                 qman_thread_finish();
213                 return -ENOMEM;
214         }
215
216         dpaa_io_portal->qman_idx = qman_get_portal_index();
217         dpaa_io_portal->bman_idx = bman_get_portal_index();
218         dpaa_io_portal->tid = syscall(SYS_gettid);
219
220         ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
221         if (ret) {
222                 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
223                             "core %d with ret: %d", cpu, ret);
224                 dpaa_portal_finish(NULL);
225
226                 return ret;
227         }
228
229         RTE_PER_LCORE(_dpaa_io) = true;
230
231         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
232
233         return 0;
234 }
235
236 /*
237  * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
238  * XXX Complete this
239  */
240 int
241 rte_dpaa_portal_init(void *arg)
242 {
243         if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
244                 return _dpaa_portal_init(arg);
245
246         return 0;
247 }
248
249 void
250 dpaa_portal_finish(void *arg)
251 {
252         struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
253
254         if (!dpaa_io_portal) {
255                 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
256                 return;
257         }
258
259         bman_thread_finish();
260         qman_thread_finish();
261
262         pthread_setspecific(dpaa_portal_key, NULL);
263
264         rte_free(dpaa_io_portal);
265         dpaa_io_portal = NULL;
266
267         RTE_PER_LCORE(_dpaa_io) = false;
268 }
269
270 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
271 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
272
273 static int
274 rte_dpaa_bus_scan(void)
275 {
276         int ret;
277
278         BUS_INIT_FUNC_TRACE();
279
280         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
281             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
282                 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
283                 return 0;
284         }
285
286         /* Load the device-tree driver */
287         ret = of_init();
288         if (ret) {
289                 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
290                 return -1;
291         }
292
293         /* Get the interface configurations from device-tree */
294         dpaa_netcfg = netcfg_acquire();
295         if (!dpaa_netcfg) {
296                 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
297                 return -EINVAL;
298         }
299
300         RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
301
302         if (!dpaa_netcfg->num_ethports) {
303                 DPAA_BUS_LOG(INFO, "no network interfaces available");
304                 /* This is not an error */
305                 return 0;
306         }
307
308         DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
309                      dpaa_netcfg, dpaa_netcfg->num_ethports);
310
311 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
312         dump_netcfg(dpaa_netcfg);
313 #endif
314
315         DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
316                      dpaa_netcfg->num_ethports);
317         ret = dpaa_create_device_list();
318         if (ret) {
319                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
320                 return ret;
321         }
322
323         /* create the key, supplying a function that'll be invoked
324          * when a portal affined thread will be deleted.
325          */
326         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
327         if (ret) {
328                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
329                 dpaa_clean_device_list();
330                 return ret;
331         }
332
333         DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
334                     (unsigned int)dpaa_portal_key, ret);
335
336         return 0;
337 }
338
339 /* register a dpaa bus based dpaa driver */
340 void
341 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
342 {
343         RTE_VERIFY(driver);
344
345         BUS_INIT_FUNC_TRACE();
346
347         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
348         /* Update Bus references */
349         driver->dpaa_bus = &rte_dpaa_bus;
350 }
351
352 /* un-register a dpaa bus based dpaa driver */
353 void
354 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
355 {
356         struct rte_dpaa_bus *dpaa_bus;
357
358         BUS_INIT_FUNC_TRACE();
359
360         dpaa_bus = driver->dpaa_bus;
361
362         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
363         /* Update Bus references */
364         driver->dpaa_bus = NULL;
365 }
366
367 static int
368 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
369                       struct rte_dpaa_device *dev)
370 {
371         int ret = -1;
372
373         BUS_INIT_FUNC_TRACE();
374
375         if (!drv || !dev) {
376                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
377                 return ret;
378         }
379
380         if (drv->drv_type == dev->device_type) {
381                 DPAA_BUS_INFO("Device: %s matches for driver: %s",
382                               dev->name, drv->driver.name);
383                 ret = 0; /* Found a match */
384         }
385
386         return ret;
387 }
388
389 static int
390 rte_dpaa_bus_probe(void)
391 {
392         int ret = -1;
393         struct rte_dpaa_device *dev;
394         struct rte_dpaa_driver *drv;
395
396         BUS_INIT_FUNC_TRACE();
397
398         /* For each registered driver, and device, call the driver->probe */
399         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
400                 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
401                         ret = rte_dpaa_device_match(drv, dev);
402                         if (ret)
403                                 continue;
404
405                         if (!drv->probe)
406                                 continue;
407
408                         ret = drv->probe(drv, dev);
409                         if (ret)
410                                 DPAA_BUS_ERR("Unable to probe.\n");
411                         break;
412                 }
413         }
414         return 0;
415 }
416
417 static struct rte_device *
418 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
419                      const void *data)
420 {
421         struct rte_dpaa_device *dev;
422
423         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
424                 if (start && &dev->device == start) {
425                         start = NULL;  /* starting point found */
426                         continue;
427                 }
428
429                 if (cmp(&dev->device, data) == 0)
430                         return &dev->device;
431         }
432
433         return NULL;
434 }
435
436 struct rte_dpaa_bus rte_dpaa_bus = {
437         .bus = {
438                 .scan = rte_dpaa_bus_scan,
439                 .probe = rte_dpaa_bus_probe,
440                 .find_device = rte_dpaa_find_device,
441         },
442         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
443         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
444         .device_count = 0,
445 };
446
447 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
448
449 RTE_INIT(dpaa_init_log);
450 static void
451 dpaa_init_log(void)
452 {
453         dpaa_logtype_bus = rte_log_register("bus.dpaa");
454         if (dpaa_logtype_bus >= 0)
455                 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
456
457         dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
458         if (dpaa_logtype_mempool >= 0)
459                 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
460 }