47ae72749e27daa19d944f04b91712ba5917390d
[dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpio.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016-2019 NXP
5  *
6  */
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <fcntl.h>
12 #include <errno.h>
13 #include <stdarg.h>
14 #include <inttypes.h>
15 #include <signal.h>
16 #include <pthread.h>
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/ioctl.h>
20 #include <sys/stat.h>
21 #include <sys/mman.h>
22 #include <sys/syscall.h>
23 #include <sys/epoll.h>
24 #include<sys/eventfd.h>
25
26 #include <rte_mbuf.h>
27 #include <rte_ethdev_driver.h>
28 #include <rte_malloc.h>
29 #include <rte_memcpy.h>
30 #include <rte_string_fns.h>
31 #include <rte_cycles.h>
32 #include <rte_kvargs.h>
33 #include <rte_dev.h>
34
35 #include <fslmc_logs.h>
36 #include <rte_fslmc.h>
37 #include "dpaa2_hw_pvt.h"
38 #include "dpaa2_hw_dpio.h"
39 #include <mc/fsl_dpmng.h>
40
41 #define NUM_HOST_CPUS RTE_MAX_LCORE
42
43 struct dpaa2_io_portal_t dpaa2_io_portal[RTE_MAX_LCORE];
44 RTE_DEFINE_PER_LCORE(struct dpaa2_io_portal_t, _dpaa2_io);
45
46 struct swp_active_dqs rte_global_active_dqs_list[NUM_MAX_SWP];
47
48 TAILQ_HEAD(dpio_dev_list, dpaa2_dpio_dev);
49 static struct dpio_dev_list dpio_dev_list
50         = TAILQ_HEAD_INITIALIZER(dpio_dev_list); /*!< DPIO device list */
51 static uint32_t io_space_count;
52
53 /* Variable to store DPAA2 platform type */
54 uint32_t dpaa2_svr_family;
55
56 /* Physical core id for lcores running on dpaa2. */
57 /* DPAA2 only support 1 lcore to 1 phy cpu mapping */
58 static unsigned int dpaa2_cpu[RTE_MAX_LCORE];
59
60 /* Variable to store DPAA2 DQRR size */
61 uint8_t dpaa2_dqrr_size;
62 /* Variable to store DPAA2 EQCR size */
63 uint8_t dpaa2_eqcr_size;
64
65 /* Variable to hold the portal_key, once created.*/
66 static pthread_key_t dpaa2_portal_key;
67
68 /*Stashing Macros default for LS208x*/
69 static int dpaa2_core_cluster_base = 0x04;
70 static int dpaa2_cluster_sz = 2;
71
72 /* For LS208X platform There are four clusters with following mapping:
73  * Cluster 1 (ID = x04) : CPU0, CPU1;
74  * Cluster 2 (ID = x05) : CPU2, CPU3;
75  * Cluster 3 (ID = x06) : CPU4, CPU5;
76  * Cluster 4 (ID = x07) : CPU6, CPU7;
77  */
78 /* For LS108X platform There are two clusters with following mapping:
79  * Cluster 1 (ID = x02) : CPU0, CPU1, CPU2, CPU3;
80  * Cluster 2 (ID = x03) : CPU4, CPU5, CPU6, CPU7;
81  */
82 /* For LX2160 platform There are four clusters with following mapping:
83  * Cluster 1 (ID = x00) : CPU0, CPU1;
84  * Cluster 2 (ID = x01) : CPU2, CPU3;
85  * Cluster 3 (ID = x02) : CPU4, CPU5;
86  * Cluster 4 (ID = x03) : CPU6, CPU7;
87  * Cluster 1 (ID = x04) : CPU8, CPU9;
88  * Cluster 2 (ID = x05) : CPU10, CP11;
89  * Cluster 3 (ID = x06) : CPU12, CPU13;
90  * Cluster 4 (ID = x07) : CPU14, CPU15;
91  */
92
93 static int
94 dpaa2_get_core_id(void)
95 {
96         rte_cpuset_t cpuset;
97         int i, ret, cpu_id = -1;
98
99         ret = pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t),
100                 &cpuset);
101         if (ret) {
102                 DPAA2_BUS_ERR("pthread_getaffinity_np() failed");
103                 return ret;
104         }
105
106         for (i = 0; i < RTE_MAX_LCORE; i++) {
107                 if (CPU_ISSET(i, &cpuset)) {
108                         if (cpu_id == -1)
109                                 cpu_id = i;
110                         else
111                                 /* Multiple cpus are affined */
112                                 return -1;
113                 }
114         }
115
116         return cpu_id;
117 }
118
119 static int
120 dpaa2_core_cluster_sdest(int cpu_id)
121 {
122         int x = cpu_id / dpaa2_cluster_sz;
123
124         return dpaa2_core_cluster_base + x;
125 }
126
127 #ifdef RTE_LIBRTE_PMD_DPAA2_EVENTDEV
128 static void
129 dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id)
130 {
131 #define STRING_LEN      28
132 #define COMMAND_LEN     50
133         uint32_t cpu_mask = 1;
134         int ret;
135         size_t len = 0;
136         char *temp = NULL, *token = NULL;
137         char string[STRING_LEN], command[COMMAND_LEN];
138         FILE *file;
139
140         snprintf(string, STRING_LEN, "dpio.%d", dpio_id);
141         file = fopen("/proc/interrupts", "r");
142         if (!file) {
143                 DPAA2_BUS_WARN("Failed to open /proc/interrupts file");
144                 return;
145         }
146         while (getline(&temp, &len, file) != -1) {
147                 if ((strstr(temp, string)) != NULL) {
148                         token = strtok(temp, ":");
149                         break;
150                 }
151         }
152
153         if (!token) {
154                 DPAA2_BUS_WARN("Failed to get interrupt id for dpio.%d",
155                                dpio_id);
156                 if (temp)
157                         free(temp);
158                 fclose(file);
159                 return;
160         }
161
162         cpu_mask = cpu_mask << dpaa2_cpu[cpu_id];
163         snprintf(command, COMMAND_LEN, "echo %X > /proc/irq/%s/smp_affinity",
164                  cpu_mask, token);
165         ret = system(command);
166         if (ret < 0)
167                 DPAA2_BUS_DEBUG(
168                         "Failed to affine interrupts on respective core");
169         else
170                 DPAA2_BUS_DEBUG(" %s command is executed", command);
171
172         free(temp);
173         fclose(file);
174 }
175
176 static int dpaa2_dpio_intr_init(struct dpaa2_dpio_dev *dpio_dev, int cpu_id)
177 {
178         struct epoll_event epoll_ev;
179         int eventfd, dpio_epoll_fd, ret;
180         int threshold = 0x3, timeout = 0xFF;
181
182         dpio_epoll_fd = epoll_create(1);
183         ret = rte_dpaa2_intr_enable(&dpio_dev->intr_handle, 0);
184         if (ret) {
185                 DPAA2_BUS_ERR("Interrupt registeration failed");
186                 return -1;
187         }
188
189         if (getenv("DPAA2_PORTAL_INTR_THRESHOLD"))
190                 threshold = atoi(getenv("DPAA2_PORTAL_INTR_THRESHOLD"));
191
192         if (getenv("DPAA2_PORTAL_INTR_TIMEOUT"))
193                 sscanf(getenv("DPAA2_PORTAL_INTR_TIMEOUT"), "%x", &timeout);
194
195         qbman_swp_interrupt_set_trigger(dpio_dev->sw_portal,
196                                         QBMAN_SWP_INTERRUPT_DQRI);
197         qbman_swp_interrupt_clear_status(dpio_dev->sw_portal, 0xffffffff);
198         qbman_swp_interrupt_set_inhibit(dpio_dev->sw_portal, 0);
199         qbman_swp_dqrr_thrshld_write(dpio_dev->sw_portal, threshold);
200         qbman_swp_intr_timeout_write(dpio_dev->sw_portal, timeout);
201
202         eventfd = dpio_dev->intr_handle.fd;
203         epoll_ev.events = EPOLLIN | EPOLLPRI | EPOLLET;
204         epoll_ev.data.fd = eventfd;
205
206         ret = epoll_ctl(dpio_epoll_fd, EPOLL_CTL_ADD, eventfd, &epoll_ev);
207         if (ret < 0) {
208                 DPAA2_BUS_ERR("epoll_ctl failed");
209                 return -1;
210         }
211         dpio_dev->epoll_fd = dpio_epoll_fd;
212
213         dpaa2_affine_dpio_intr_to_respective_core(dpio_dev->hw_id, cpu_id);
214
215         return 0;
216 }
217
218 static void dpaa2_dpio_intr_deinit(struct dpaa2_dpio_dev *dpio_dev)
219 {
220         int ret;
221
222         ret = rte_dpaa2_intr_disable(&dpio_dev->intr_handle, 0);
223         if (ret)
224                 DPAA2_BUS_ERR("DPIO interrupt disable failed");
225
226         close(dpio_dev->epoll_fd);
227 }
228 #endif
229
230 static int
231 dpaa2_configure_stashing(struct dpaa2_dpio_dev *dpio_dev)
232 {
233         int sdest, ret;
234         int cpu_id;
235
236         /* Set the Stashing Destination */
237         cpu_id = dpaa2_get_core_id();
238         if (cpu_id < 0) {
239                 DPAA2_BUS_ERR("Thread not affined to a single core");
240                 return -1;
241         }
242
243         /* Set the STASH Destination depending on Current CPU ID.
244          * Valid values of SDEST are 4,5,6,7. Where,
245          */
246         sdest = dpaa2_core_cluster_sdest(cpu_id);
247         DPAA2_BUS_DEBUG("Portal= %d  CPU= %u SDEST= %d",
248                         dpio_dev->index, cpu_id, sdest);
249
250         ret = dpio_set_stashing_destination(dpio_dev->dpio, CMD_PRI_LOW,
251                                             dpio_dev->token, sdest);
252         if (ret) {
253                 DPAA2_BUS_ERR("%d ERROR in SDEST",  ret);
254                 return -1;
255         }
256
257 #ifdef RTE_LIBRTE_PMD_DPAA2_EVENTDEV
258         if (dpaa2_dpio_intr_init(dpio_dev, cpu_id)) {
259                 DPAA2_BUS_ERR("Interrupt registration failed for dpio");
260                 return -1;
261         }
262 #endif
263
264         return 0;
265 }
266
267 static void dpaa2_put_qbman_swp(struct dpaa2_dpio_dev *dpio_dev)
268 {
269         if (dpio_dev) {
270 #ifdef RTE_LIBRTE_PMD_DPAA2_EVENTDEV
271                 dpaa2_dpio_intr_deinit(dpio_dev);
272 #endif
273                 rte_atomic16_clear(&dpio_dev->ref_count);
274         }
275 }
276
277 static struct dpaa2_dpio_dev *dpaa2_get_qbman_swp(void)
278 {
279         struct dpaa2_dpio_dev *dpio_dev = NULL;
280         int ret;
281
282         /* Get DPIO dev handle from list using index */
283         TAILQ_FOREACH(dpio_dev, &dpio_dev_list, next) {
284                 if (dpio_dev && rte_atomic16_test_and_set(&dpio_dev->ref_count))
285                         break;
286         }
287         if (!dpio_dev)
288                 return NULL;
289
290         DPAA2_BUS_DEBUG("New Portal %p (%d) affined thread - %lu",
291                         dpio_dev, dpio_dev->index, syscall(SYS_gettid));
292
293         ret = dpaa2_configure_stashing(dpio_dev);
294         if (ret) {
295                 DPAA2_BUS_ERR("dpaa2_configure_stashing failed");
296                 return NULL;
297         }
298
299         ret = pthread_setspecific(dpaa2_portal_key, (void *)dpio_dev);
300         if (ret) {
301                 DPAA2_BUS_ERR("pthread_setspecific failed with ret: %d", ret);
302                 dpaa2_put_qbman_swp(dpio_dev);
303                 return NULL;
304         }
305
306         return dpio_dev;
307 }
308
309 int
310 dpaa2_affine_qbman_swp(void)
311 {
312         struct dpaa2_dpio_dev *dpio_dev;
313         uint64_t tid = syscall(SYS_gettid);
314
315         /* Populate the dpaa2_io_portal structure */
316         if (!RTE_PER_LCORE(_dpaa2_io).dpio_dev) {
317                 dpio_dev = dpaa2_get_qbman_swp();
318                 if (!dpio_dev) {
319                         DPAA2_BUS_ERR("No software portal resource left");
320                         return -1;
321                 }
322                 RTE_PER_LCORE(_dpaa2_io).dpio_dev = dpio_dev;
323
324                 DPAA2_BUS_INFO(
325                         "DPAA Portal=%p (%d) is affined to thread %" PRIu64,
326                         dpio_dev, dpio_dev->index, tid);
327         }
328         return 0;
329 }
330
331 int
332 dpaa2_affine_qbman_ethrx_swp(void)
333 {
334         struct dpaa2_dpio_dev *dpio_dev;
335         uint64_t tid = syscall(SYS_gettid);
336
337         /* Populate the dpaa2_io_portal structure */
338         if (!RTE_PER_LCORE(_dpaa2_io).ethrx_dpio_dev) {
339                 dpio_dev = dpaa2_get_qbman_swp();
340                 if (!dpio_dev) {
341                         DPAA2_BUS_ERR("No software portal resource left");
342                         return -1;
343                 }
344                 RTE_PER_LCORE(_dpaa2_io).ethrx_dpio_dev = dpio_dev;
345
346                 DPAA2_BUS_INFO(
347                         "DPAA Portal=%p (%d) is affined for eth rx to thread %"
348                         PRIu64, dpio_dev, dpio_dev->index, tid);
349         }
350         return 0;
351 }
352
353 static void dpaa2_portal_finish(void *arg)
354 {
355         RTE_SET_USED(arg);
356
357         dpaa2_put_qbman_swp(RTE_PER_LCORE(_dpaa2_io).dpio_dev);
358         dpaa2_put_qbman_swp(RTE_PER_LCORE(_dpaa2_io).ethrx_dpio_dev);
359
360         pthread_setspecific(dpaa2_portal_key, NULL);
361 }
362
363 /*
364  * This checks for not supported lcore mappings as well as get the physical
365  * cpuid for the lcore.
366  * one lcore can only map to 1 cpu i.e. 1@10-14 not supported.
367  * one cpu can be mapped to more than one lcores.
368  */
369 static int
370 dpaa2_check_lcore_cpuset(void)
371 {
372         unsigned int lcore_id, i;
373         int ret = 0;
374
375         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++)
376                 dpaa2_cpu[lcore_id] = 0xffffffff;
377
378         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
379                 rte_cpuset_t cpuset = rte_lcore_cpuset(lcore_id);
380
381                 for (i = 0; i < CPU_SETSIZE; i++) {
382                         if (!CPU_ISSET(i, &cpuset))
383                                 continue;
384                         if (i >= RTE_MAX_LCORE) {
385                                 DPAA2_BUS_ERR("ERR:lcore map to core %u (>= %u) not supported",
386                                         i, RTE_MAX_LCORE);
387                                 ret = -1;
388                                 continue;
389                         }
390                         RTE_LOG(DEBUG, EAL, "lcore id = %u cpu=%u\n",
391                                 lcore_id, i);
392                         if (dpaa2_cpu[lcore_id] != 0xffffffff) {
393                                 DPAA2_BUS_ERR("ERR:lcore map to multi-cpu not supported");
394                                 ret = -1;
395                                 continue;
396                         }
397                         dpaa2_cpu[lcore_id] = i;
398                 }
399         }
400         return ret;
401 }
402
403 static int
404 dpaa2_create_dpio_device(int vdev_fd,
405                          struct vfio_device_info *obj_info,
406                          int object_id)
407 {
408         struct dpaa2_dpio_dev *dpio_dev = NULL;
409         struct vfio_region_info reg_info = { .argsz = sizeof(reg_info)};
410         struct qbman_swp_desc p_des;
411         struct dpio_attr attr;
412         int ret;
413         static int check_lcore_cpuset;
414
415         if (obj_info->num_regions < NUM_DPIO_REGIONS) {
416                 DPAA2_BUS_ERR("Not sufficient number of DPIO regions");
417                 return -1;
418         }
419
420         dpio_dev = rte_zmalloc(NULL, sizeof(struct dpaa2_dpio_dev),
421                               RTE_CACHE_LINE_SIZE);
422         if (!dpio_dev) {
423                 DPAA2_BUS_ERR("Memory allocation failed for DPIO Device");
424                 return -1;
425         }
426
427         dpio_dev->dpio = NULL;
428         dpio_dev->hw_id = object_id;
429         rte_atomic16_init(&dpio_dev->ref_count);
430         /* Using single portal  for all devices */
431         dpio_dev->mc_portal = dpaa2_get_mcp_ptr(MC_PORTAL_INDEX);
432
433         if (!check_lcore_cpuset) {
434                 check_lcore_cpuset = 1;
435
436                 if (dpaa2_check_lcore_cpuset() < 0)
437                         goto err;
438         }
439
440         dpio_dev->dpio = rte_zmalloc(NULL, sizeof(struct fsl_mc_io),
441                                      RTE_CACHE_LINE_SIZE);
442         if (!dpio_dev->dpio) {
443                 DPAA2_BUS_ERR("Memory allocation failure");
444                 goto err;
445         }
446
447         dpio_dev->dpio->regs = dpio_dev->mc_portal;
448         if (dpio_open(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->hw_id,
449                       &dpio_dev->token)) {
450                 DPAA2_BUS_ERR("Failed to allocate IO space");
451                 goto err;
452         }
453
454         if (dpio_reset(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
455                 DPAA2_BUS_ERR("Failed to reset dpio");
456                 goto err;
457         }
458
459         if (dpio_enable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token)) {
460                 DPAA2_BUS_ERR("Failed to Enable dpio");
461                 goto err;
462         }
463
464         if (dpio_get_attributes(dpio_dev->dpio, CMD_PRI_LOW,
465                                 dpio_dev->token, &attr)) {
466                 DPAA2_BUS_ERR("DPIO Get attribute failed");
467                 goto err;
468         }
469
470         /* find the SoC type for the first time */
471         if (!dpaa2_svr_family) {
472                 struct mc_soc_version mc_plat_info = {0};
473
474                 if (mc_get_soc_version(dpio_dev->dpio,
475                                        CMD_PRI_LOW, &mc_plat_info)) {
476                         DPAA2_BUS_ERR("Unable to get SoC version information");
477                 } else if ((mc_plat_info.svr & 0xffff0000) == SVR_LS1080A) {
478                         dpaa2_core_cluster_base = 0x02;
479                         dpaa2_cluster_sz = 4;
480                         DPAA2_BUS_DEBUG("LS108x (A53) Platform Detected");
481                 } else if ((mc_plat_info.svr & 0xffff0000) == SVR_LX2160A) {
482                         dpaa2_core_cluster_base = 0x00;
483                         dpaa2_cluster_sz = 2;
484                         DPAA2_BUS_DEBUG("LX2160 Platform Detected");
485                 }
486                 dpaa2_svr_family = (mc_plat_info.svr & 0xffff0000);
487
488                 if (dpaa2_svr_family == SVR_LX2160A) {
489                         dpaa2_dqrr_size = DPAA2_LX2_DQRR_RING_SIZE;
490                         dpaa2_eqcr_size = DPAA2_LX2_EQCR_RING_SIZE;
491                 } else {
492                         dpaa2_dqrr_size = DPAA2_DQRR_RING_SIZE;
493                         dpaa2_eqcr_size = DPAA2_EQCR_RING_SIZE;
494                 }
495         }
496
497         if (dpaa2_svr_family == SVR_LX2160A)
498                 reg_info.index = DPAA2_SWP_CENA_MEM_REGION;
499         else
500                 reg_info.index = DPAA2_SWP_CENA_REGION;
501
502         if (ioctl(vdev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
503                 DPAA2_BUS_ERR("vfio: error getting region info");
504                 goto err;
505         }
506
507         dpio_dev->ce_size = reg_info.size;
508         dpio_dev->qbman_portal_ce_paddr = (size_t)mmap(NULL, reg_info.size,
509                                 PROT_WRITE | PROT_READ, MAP_SHARED,
510                                 vdev_fd, reg_info.offset);
511
512         reg_info.index = DPAA2_SWP_CINH_REGION;
513         if (ioctl(vdev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info)) {
514                 DPAA2_BUS_ERR("vfio: error getting region info");
515                 goto err;
516         }
517
518         dpio_dev->ci_size = reg_info.size;
519         dpio_dev->qbman_portal_ci_paddr = (size_t)mmap(NULL, reg_info.size,
520                                 PROT_WRITE | PROT_READ, MAP_SHARED,
521                                 vdev_fd, reg_info.offset);
522
523         /* Configure & setup SW portal */
524         p_des.block = NULL;
525         p_des.idx = attr.qbman_portal_id;
526         p_des.cena_bar = (void *)(dpio_dev->qbman_portal_ce_paddr);
527         p_des.cinh_bar = (void *)(dpio_dev->qbman_portal_ci_paddr);
528         p_des.irq = -1;
529         p_des.qman_version = attr.qbman_version;
530         p_des.eqcr_mode = qman_eqcr_vb_ring;
531         p_des.cena_access_mode = qman_cena_fastest_access;
532
533         dpio_dev->sw_portal = qbman_swp_init(&p_des);
534         if (dpio_dev->sw_portal == NULL) {
535                 DPAA2_BUS_ERR("QBMan SW Portal Init failed");
536                 goto err;
537         }
538
539         io_space_count++;
540         dpio_dev->index = io_space_count;
541
542         if (rte_dpaa2_vfio_setup_intr(&dpio_dev->intr_handle, vdev_fd, 1)) {
543                 DPAA2_BUS_ERR("Fail to setup interrupt for %d",
544                               dpio_dev->hw_id);
545                 goto err;
546         }
547
548         dpio_dev->eqresp = rte_zmalloc(NULL, MAX_EQ_RESP_ENTRIES *
549                                      (sizeof(struct qbman_result) +
550                                      sizeof(struct eqresp_metadata)),
551                                      RTE_CACHE_LINE_SIZE);
552         if (!dpio_dev->eqresp) {
553                 DPAA2_BUS_ERR("Memory allocation failed for eqresp");
554                 goto err;
555         }
556         dpio_dev->eqresp_meta = (struct eqresp_metadata *)(dpio_dev->eqresp +
557                                 MAX_EQ_RESP_ENTRIES);
558
559
560         TAILQ_INSERT_TAIL(&dpio_dev_list, dpio_dev, next);
561
562         if (!dpaa2_portal_key) {
563                 /* create the key, supplying a function that'll be invoked
564                  * when a portal affined thread will be deleted.
565                  */
566                 ret = pthread_key_create(&dpaa2_portal_key,
567                                          dpaa2_portal_finish);
568                 if (ret) {
569                         DPAA2_BUS_DEBUG("Unable to create pthread key (%d)",
570                                         ret);
571                         goto err;
572                 }
573         }
574
575         return 0;
576
577 err:
578         if (dpio_dev->dpio) {
579                 dpio_disable(dpio_dev->dpio, CMD_PRI_LOW, dpio_dev->token);
580                 dpio_close(dpio_dev->dpio, CMD_PRI_LOW,  dpio_dev->token);
581                 rte_free(dpio_dev->eqresp);
582                 rte_free(dpio_dev->dpio);
583         }
584
585         rte_free(dpio_dev);
586
587         /* For each element in the list, cleanup */
588         TAILQ_FOREACH(dpio_dev, &dpio_dev_list, next) {
589                 if (dpio_dev->dpio) {
590                         dpio_disable(dpio_dev->dpio, CMD_PRI_LOW,
591                                 dpio_dev->token);
592                         dpio_close(dpio_dev->dpio, CMD_PRI_LOW,
593                                 dpio_dev->token);
594                         rte_free(dpio_dev->dpio);
595                 }
596                 rte_free(dpio_dev);
597         }
598
599         /* Preventing re-use of the list with old entries */
600         TAILQ_INIT(&dpio_dev_list);
601
602         return -1;
603 }
604
605 void
606 dpaa2_free_dq_storage(struct queue_storage_info_t *q_storage)
607 {
608         int i = 0;
609
610         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
611                 if (q_storage->dq_storage[i])
612                         rte_free(q_storage->dq_storage[i]);
613         }
614 }
615
616 int
617 dpaa2_alloc_dq_storage(struct queue_storage_info_t *q_storage)
618 {
619         int i = 0;
620
621         for (i = 0; i < NUM_DQS_PER_QUEUE; i++) {
622                 q_storage->dq_storage[i] = rte_malloc(NULL,
623                         dpaa2_dqrr_size * sizeof(struct qbman_result),
624                         RTE_CACHE_LINE_SIZE);
625                 if (!q_storage->dq_storage[i])
626                         goto fail;
627         }
628         return 0;
629 fail:
630         while (--i >= 0)
631                 rte_free(q_storage->dq_storage[i]);
632
633         return -1;
634 }
635
636 uint32_t
637 dpaa2_free_eq_descriptors(void)
638 {
639         struct dpaa2_dpio_dev *dpio_dev = DPAA2_PER_LCORE_DPIO;
640         struct qbman_result *eqresp;
641         struct eqresp_metadata *eqresp_meta;
642         struct dpaa2_queue *txq;
643
644         while (dpio_dev->eqresp_ci != dpio_dev->eqresp_pi) {
645                 eqresp = &dpio_dev->eqresp[dpio_dev->eqresp_ci];
646                 eqresp_meta = &dpio_dev->eqresp_meta[dpio_dev->eqresp_ci];
647
648                 if (!qbman_result_eqresp_rspid(eqresp))
649                         break;
650
651                 if (qbman_result_eqresp_rc(eqresp)) {
652                         txq = eqresp_meta->dpaa2_q;
653                         txq->cb_eqresp_free(dpio_dev->eqresp_ci);
654                 }
655                 qbman_result_eqresp_set_rspid(eqresp, 0);
656
657                 dpio_dev->eqresp_ci + 1 < MAX_EQ_RESP_ENTRIES ?
658                         dpio_dev->eqresp_ci++ : (dpio_dev->eqresp_ci = 0);
659         }
660
661         /* Return 1 less entry so that PI and CI are never same in a
662          * case there all the EQ responses are in use.
663          */
664         if (dpio_dev->eqresp_ci > dpio_dev->eqresp_pi)
665                 return dpio_dev->eqresp_ci - dpio_dev->eqresp_pi - 1;
666         else
667                 return dpio_dev->eqresp_ci - dpio_dev->eqresp_pi +
668                         MAX_EQ_RESP_ENTRIES - 1;
669 }
670
671 static struct rte_dpaa2_object rte_dpaa2_dpio_obj = {
672         .dev_type = DPAA2_IO,
673         .create = dpaa2_create_dpio_device,
674 };
675
676 RTE_PMD_REGISTER_DPAA2_OBJECT(dpio, rte_dpaa2_dpio_obj);