drivers: use SPDX tag in NXP dpaa files
[dpdk.git] / drivers / bus / dpaa / dpaa_bus.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2017 NXP
4  *
5  */
6 /* System headers */
7 #include <stdio.h>
8 #include <inttypes.h>
9 #include <unistd.h>
10 #include <limits.h>
11 #include <sched.h>
12 #include <signal.h>
13 #include <pthread.h>
14 #include <sys/types.h>
15 #include <sys/syscall.h>
16
17 #include <rte_byteorder.h>
18 #include <rte_common.h>
19 #include <rte_interrupts.h>
20 #include <rte_log.h>
21 #include <rte_debug.h>
22 #include <rte_pci.h>
23 #include <rte_atomic.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_memory.h>
26 #include <rte_tailq.h>
27 #include <rte_eal.h>
28 #include <rte_alarm.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
32 #include <rte_ring.h>
33 #include <rte_bus.h>
34
35 #include <rte_dpaa_bus.h>
36 #include <rte_dpaa_logs.h>
37
38 #include <fsl_usd.h>
39 #include <fsl_qman.h>
40 #include <fsl_bman.h>
41 #include <of.h>
42 #include <netcfg.h>
43
44 int dpaa_logtype_bus;
45 int dpaa_logtype_mempool;
46 int dpaa_logtype_pmd;
47
48 struct rte_dpaa_bus rte_dpaa_bus;
49 struct netcfg_info *dpaa_netcfg;
50
51 /* define a variable to hold the portal_key, once created.*/
52 pthread_key_t dpaa_portal_key;
53
54 RTE_DEFINE_PER_LCORE(bool, _dpaa_io);
55
56 static inline void
57 dpaa_add_to_device_list(struct rte_dpaa_device *dev)
58 {
59         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
60 }
61
62 static inline void
63 dpaa_remove_from_device_list(struct rte_dpaa_device *dev)
64 {
65         TAILQ_INSERT_TAIL(&rte_dpaa_bus.device_list, dev, next);
66 }
67
68 /*
69  * Reads the SEC device from DTS
70  * Returns -1 if SEC devices not available, 0 otherwise
71  */
72 static inline int
73 dpaa_sec_available(void)
74 {
75         const struct device_node *caam_node;
76
77         for_each_compatible_node(caam_node, NULL, "fsl,sec-v4.0") {
78                 return 0;
79         }
80
81         return -1;
82 }
83
84 static void dpaa_clean_device_list(void);
85
86 static int
87 dpaa_create_device_list(void)
88 {
89         int i;
90         int ret;
91         struct rte_dpaa_device *dev;
92         struct fm_eth_port_cfg *cfg;
93         struct fman_if *fman_intf;
94
95         /* Creating Ethernet Devices */
96         for (i = 0; i < dpaa_netcfg->num_ethports; i++) {
97                 dev = calloc(1, sizeof(struct rte_dpaa_device));
98                 if (!dev) {
99                         DPAA_BUS_LOG(ERR, "Failed to allocate ETH devices");
100                         ret = -ENOMEM;
101                         goto cleanup;
102                 }
103
104                 cfg = &dpaa_netcfg->port_cfg[i];
105                 fman_intf = cfg->fman_if;
106
107                 /* Device identifiers */
108                 dev->id.fman_id = fman_intf->fman_idx + 1;
109                 dev->id.mac_id = fman_intf->mac_idx;
110                 dev->device_type = FSL_DPAA_ETH;
111                 dev->id.dev_id = i;
112
113                 /* Create device name */
114                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
115                 sprintf(dev->name, "fm%d-mac%d", (fman_intf->fman_idx + 1),
116                         fman_intf->mac_idx);
117                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
118                 dev->device.name = dev->name;
119
120                 dpaa_add_to_device_list(dev);
121         }
122
123         rte_dpaa_bus.device_count = i;
124
125         /* Unlike case of ETH, RTE_LIBRTE_DPAA_MAX_CRYPTODEV SEC devices are
126          * constantly created only if "sec" property is found in the device
127          * tree. Logically there is no limit for number of devices (QI
128          * interfaces) that can be created.
129          */
130
131         if (dpaa_sec_available()) {
132                 DPAA_BUS_LOG(INFO, "DPAA SEC devices are not available");
133                 return 0;
134         }
135
136         /* Creating SEC Devices */
137         for (i = 0; i < RTE_LIBRTE_DPAA_MAX_CRYPTODEV; i++) {
138                 dev = calloc(1, sizeof(struct rte_dpaa_device));
139                 if (!dev) {
140                         DPAA_BUS_LOG(ERR, "Failed to allocate SEC devices");
141                         ret = -1;
142                         goto cleanup;
143                 }
144
145                 dev->device_type = FSL_DPAA_CRYPTO;
146                 dev->id.dev_id = rte_dpaa_bus.device_count + i;
147
148                 /* Even though RTE_CRYPTODEV_NAME_MAX_LEN is valid length of
149                  * crypto PMD, using RTE_ETH_NAME_MAX_LEN as that is the size
150                  * allocated for dev->name/
151                  */
152                 memset(dev->name, 0, RTE_ETH_NAME_MAX_LEN);
153                 sprintf(dev->name, "dpaa-sec%d", i);
154                 DPAA_BUS_LOG(DEBUG, "Device added: %s", dev->name);
155
156                 dpaa_add_to_device_list(dev);
157         }
158
159         rte_dpaa_bus.device_count += i;
160
161         return 0;
162
163 cleanup:
164         dpaa_clean_device_list();
165         return ret;
166 }
167
168 static void
169 dpaa_clean_device_list(void)
170 {
171         struct rte_dpaa_device *dev = NULL;
172         struct rte_dpaa_device *tdev = NULL;
173
174         TAILQ_FOREACH_SAFE(dev, &rte_dpaa_bus.device_list, next, tdev) {
175                 TAILQ_REMOVE(&rte_dpaa_bus.device_list, dev, next);
176                 free(dev);
177                 dev = NULL;
178         }
179 }
180
181 /** XXX move this function into a separate file */
182 static int
183 _dpaa_portal_init(void *arg)
184 {
185         cpu_set_t cpuset;
186         pthread_t id;
187         uint32_t cpu = rte_lcore_id();
188         int ret;
189         struct dpaa_portal *dpaa_io_portal;
190
191         BUS_INIT_FUNC_TRACE();
192
193         if ((uint64_t)arg == 1 || cpu == LCORE_ID_ANY)
194                 cpu = rte_get_master_lcore();
195         /* if the core id is not supported */
196         else
197                 if (cpu >= RTE_MAX_LCORE)
198                         return -1;
199
200         /* Set CPU affinity for this thread */
201         CPU_ZERO(&cpuset);
202         CPU_SET(cpu, &cpuset);
203         id = pthread_self();
204         ret = pthread_setaffinity_np(id, sizeof(cpu_set_t), &cpuset);
205         if (ret) {
206                 DPAA_BUS_LOG(ERR, "pthread_setaffinity_np failed on "
207                         "core :%d with ret: %d", cpu, ret);
208                 return ret;
209         }
210
211         /* Initialise bman thread portals */
212         ret = bman_thread_init();
213         if (ret) {
214                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
215                         "core %d with ret: %d", cpu, ret);
216                 return ret;
217         }
218
219         DPAA_BUS_LOG(DEBUG, "BMAN thread initialized");
220
221         /* Initialise qman thread portals */
222         ret = qman_thread_init();
223         if (ret) {
224                 DPAA_BUS_LOG(ERR, "bman_thread_init failed on "
225                         "core %d with ret: %d", cpu, ret);
226                 bman_thread_finish();
227                 return ret;
228         }
229
230         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
231
232         dpaa_io_portal = rte_malloc(NULL, sizeof(struct dpaa_portal),
233                                     RTE_CACHE_LINE_SIZE);
234         if (!dpaa_io_portal) {
235                 DPAA_BUS_LOG(ERR, "Unable to allocate memory");
236                 bman_thread_finish();
237                 qman_thread_finish();
238                 return -ENOMEM;
239         }
240
241         dpaa_io_portal->qman_idx = qman_get_portal_index();
242         dpaa_io_portal->bman_idx = bman_get_portal_index();
243         dpaa_io_portal->tid = syscall(SYS_gettid);
244
245         ret = pthread_setspecific(dpaa_portal_key, (void *)dpaa_io_portal);
246         if (ret) {
247                 DPAA_BUS_LOG(ERR, "pthread_setspecific failed on "
248                             "core %d with ret: %d", cpu, ret);
249                 dpaa_portal_finish(NULL);
250
251                 return ret;
252         }
253
254         RTE_PER_LCORE(_dpaa_io) = true;
255
256         DPAA_BUS_LOG(DEBUG, "QMAN thread initialized");
257
258         return 0;
259 }
260
261 /*
262  * rte_dpaa_portal_init - Wrapper over _dpaa_portal_init with thread level check
263  * XXX Complete this
264  */
265 int
266 rte_dpaa_portal_init(void *arg)
267 {
268         if (unlikely(!RTE_PER_LCORE(_dpaa_io)))
269                 return _dpaa_portal_init(arg);
270
271         return 0;
272 }
273
274 void
275 dpaa_portal_finish(void *arg)
276 {
277         struct dpaa_portal *dpaa_io_portal = (struct dpaa_portal *)arg;
278
279         if (!dpaa_io_portal) {
280                 DPAA_BUS_LOG(DEBUG, "Portal already cleaned");
281                 return;
282         }
283
284         bman_thread_finish();
285         qman_thread_finish();
286
287         pthread_setspecific(dpaa_portal_key, NULL);
288
289         rte_free(dpaa_io_portal);
290         dpaa_io_portal = NULL;
291
292         RTE_PER_LCORE(_dpaa_io) = false;
293 }
294
295 #define DPAA_DEV_PATH1 "/sys/devices/platform/soc/soc:fsl,dpaa"
296 #define DPAA_DEV_PATH2 "/sys/devices/platform/fsl,dpaa"
297
298 static int
299 rte_dpaa_bus_scan(void)
300 {
301         int ret;
302
303         BUS_INIT_FUNC_TRACE();
304
305         if ((access(DPAA_DEV_PATH1, F_OK) != 0) &&
306             (access(DPAA_DEV_PATH2, F_OK) != 0)) {
307                 RTE_LOG(DEBUG, EAL, "DPAA Bus not present. Skipping.\n");
308                 return 0;
309         }
310
311         /* Load the device-tree driver */
312         ret = of_init();
313         if (ret) {
314                 DPAA_BUS_LOG(ERR, "of_init failed with ret: %d", ret);
315                 return -1;
316         }
317
318         /* Get the interface configurations from device-tree */
319         dpaa_netcfg = netcfg_acquire();
320         if (!dpaa_netcfg) {
321                 DPAA_BUS_LOG(ERR, "netcfg_acquire failed");
322                 return -EINVAL;
323         }
324
325         RTE_LOG(NOTICE, EAL, "DPAA Bus Detected\n");
326
327         if (!dpaa_netcfg->num_ethports) {
328                 DPAA_BUS_LOG(INFO, "no network interfaces available");
329                 /* This is not an error */
330                 return 0;
331         }
332
333         DPAA_BUS_LOG(DEBUG, "Bus: Address of netcfg=%p, Ethports=%d",
334                      dpaa_netcfg, dpaa_netcfg->num_ethports);
335
336 #ifdef RTE_LIBRTE_DPAA_DEBUG_DRIVER
337         dump_netcfg(dpaa_netcfg);
338 #endif
339
340         DPAA_BUS_LOG(DEBUG, "Number of devices = %d\n",
341                      dpaa_netcfg->num_ethports);
342         ret = dpaa_create_device_list();
343         if (ret) {
344                 DPAA_BUS_LOG(ERR, "Unable to create device list. (%d)", ret);
345                 return ret;
346         }
347
348         /* create the key, supplying a function that'll be invoked
349          * when a portal affined thread will be deleted.
350          */
351         ret = pthread_key_create(&dpaa_portal_key, dpaa_portal_finish);
352         if (ret) {
353                 DPAA_BUS_LOG(DEBUG, "Unable to create pthread key. (%d)", ret);
354                 dpaa_clean_device_list();
355                 return ret;
356         }
357
358         DPAA_BUS_LOG(DEBUG, "dpaa_portal_key=%u, ret=%d\n",
359                     (unsigned int)dpaa_portal_key, ret);
360
361         return 0;
362 }
363
364 /* register a dpaa bus based dpaa driver */
365 void
366 rte_dpaa_driver_register(struct rte_dpaa_driver *driver)
367 {
368         RTE_VERIFY(driver);
369
370         BUS_INIT_FUNC_TRACE();
371
372         TAILQ_INSERT_TAIL(&rte_dpaa_bus.driver_list, driver, next);
373         /* Update Bus references */
374         driver->dpaa_bus = &rte_dpaa_bus;
375 }
376
377 /* un-register a dpaa bus based dpaa driver */
378 void
379 rte_dpaa_driver_unregister(struct rte_dpaa_driver *driver)
380 {
381         struct rte_dpaa_bus *dpaa_bus;
382
383         BUS_INIT_FUNC_TRACE();
384
385         dpaa_bus = driver->dpaa_bus;
386
387         TAILQ_REMOVE(&dpaa_bus->driver_list, driver, next);
388         /* Update Bus references */
389         driver->dpaa_bus = NULL;
390 }
391
392 static int
393 rte_dpaa_device_match(struct rte_dpaa_driver *drv,
394                       struct rte_dpaa_device *dev)
395 {
396         int ret = -1;
397
398         BUS_INIT_FUNC_TRACE();
399
400         if (!drv || !dev) {
401                 DPAA_BUS_DEBUG("Invalid drv or dev received.");
402                 return ret;
403         }
404
405         if (drv->drv_type == dev->device_type) {
406                 DPAA_BUS_INFO("Device: %s matches for driver: %s",
407                               dev->name, drv->driver.name);
408                 ret = 0; /* Found a match */
409         }
410
411         return ret;
412 }
413
414 static int
415 rte_dpaa_bus_probe(void)
416 {
417         int ret = -1;
418         struct rte_dpaa_device *dev;
419         struct rte_dpaa_driver *drv;
420
421         BUS_INIT_FUNC_TRACE();
422
423         /* For each registered driver, and device, call the driver->probe */
424         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
425                 TAILQ_FOREACH(drv, &rte_dpaa_bus.driver_list, next) {
426                         ret = rte_dpaa_device_match(drv, dev);
427                         if (ret)
428                                 continue;
429
430                         if (!drv->probe)
431                                 continue;
432
433                         ret = drv->probe(drv, dev);
434                         if (ret)
435                                 DPAA_BUS_ERR("Unable to probe.\n");
436                         break;
437                 }
438         }
439         return 0;
440 }
441
442 static struct rte_device *
443 rte_dpaa_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
444                      const void *data)
445 {
446         struct rte_dpaa_device *dev;
447
448         TAILQ_FOREACH(dev, &rte_dpaa_bus.device_list, next) {
449                 if (start && &dev->device == start) {
450                         start = NULL;  /* starting point found */
451                         continue;
452                 }
453
454                 if (cmp(&dev->device, data) == 0)
455                         return &dev->device;
456         }
457
458         return NULL;
459 }
460
461 /*
462  * Get iommu class of DPAA2 devices on the bus.
463  */
464 static enum rte_iova_mode
465 rte_dpaa_get_iommu_class(void)
466 {
467         return RTE_IOVA_PA;
468 }
469
470 struct rte_dpaa_bus rte_dpaa_bus = {
471         .bus = {
472                 .scan = rte_dpaa_bus_scan,
473                 .probe = rte_dpaa_bus_probe,
474                 .find_device = rte_dpaa_find_device,
475                 .get_iommu_class = rte_dpaa_get_iommu_class,
476         },
477         .device_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.device_list),
478         .driver_list = TAILQ_HEAD_INITIALIZER(rte_dpaa_bus.driver_list),
479         .device_count = 0,
480 };
481
482 RTE_REGISTER_BUS(FSL_DPAA_BUS_NAME, rte_dpaa_bus.bus);
483
484 RTE_INIT(dpaa_init_log);
485 static void
486 dpaa_init_log(void)
487 {
488         dpaa_logtype_bus = rte_log_register("bus.dpaa");
489         if (dpaa_logtype_bus >= 0)
490                 rte_log_set_level(dpaa_logtype_bus, RTE_LOG_NOTICE);
491
492         dpaa_logtype_mempool = rte_log_register("mempool.dpaa");
493         if (dpaa_logtype_mempool >= 0)
494                 rte_log_set_level(dpaa_logtype_mempool, RTE_LOG_NOTICE);
495
496         dpaa_logtype_pmd = rte_log_register("pmd.dpaa");
497         if (dpaa_logtype_pmd >= 0)
498                 rte_log_set_level(dpaa_logtype_pmd, RTE_LOG_NOTICE);
499 }