2fb285c1cb58b5a962c04f032039b10184167f24
[deb_dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpbp.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <errno.h>
41
42 #include <rte_malloc.h>
43 #include <rte_memcpy.h>
44 #include <rte_string_fns.h>
45 #include <rte_cycles.h>
46 #include <rte_kvargs.h>
47 #include <rte_dev.h>
48 #include <rte_ethdev.h>
49
50 #include <fslmc_logs.h>
51 #include <fslmc_vfio.h>
52 #include <mc/fsl_dpbp.h>
53 #include "portal/dpaa2_hw_pvt.h"
54 #include "portal/dpaa2_hw_dpio.h"
55
56 TAILQ_HEAD(dpbp_device_list, dpaa2_dpbp_dev);
57 static struct dpbp_device_list *dpbp_dev_list; /*!< DPBP device list */
58
59 int
60 dpaa2_create_dpbp_device(
61                 int dpbp_id)
62 {
63         struct dpaa2_dpbp_dev *dpbp_node;
64         int ret;
65
66         if (!dpbp_dev_list) {
67                 dpbp_dev_list = malloc(sizeof(struct dpbp_device_list));
68                 if (!dpbp_dev_list) {
69                         PMD_INIT_LOG(ERR, "Memory alloc failed in DPBP list\n");
70                         return -1;
71                 }
72                 /* Initialize the DPBP List */
73                 TAILQ_INIT(dpbp_dev_list);
74         }
75
76         /* Allocate DPAA2 dpbp handle */
77         dpbp_node = (struct dpaa2_dpbp_dev *)
78                         malloc(sizeof(struct dpaa2_dpbp_dev));
79         if (!dpbp_node) {
80                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPBP Device");
81                 return -1;
82         }
83
84         /* Open the dpbp object */
85         dpbp_node->dpbp.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
86         ret = dpbp_open(&dpbp_node->dpbp,
87                         CMD_PRI_LOW, dpbp_id, &dpbp_node->token);
88         if (ret) {
89                 PMD_INIT_LOG(ERR, "Resource alloc failure with err code: %d",
90                              ret);
91                 free(dpbp_node);
92                 return -1;
93         }
94
95         /* Clean the device first */
96         ret = dpbp_reset(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
97         if (ret) {
98                 PMD_INIT_LOG(ERR, "Failure cleaning dpbp device with"
99                                         " error code %d\n", ret);
100                 dpbp_close(&dpbp_node->dpbp, CMD_PRI_LOW, dpbp_node->token);
101                 free(dpbp_node);
102                 return -1;
103         }
104
105         dpbp_node->dpbp_id = dpbp_id;
106         rte_atomic16_init(&dpbp_node->in_use);
107
108         TAILQ_INSERT_HEAD(dpbp_dev_list, dpbp_node, next);
109
110         PMD_INIT_LOG(DEBUG, "Buffer pool resource initialized %d", dpbp_id);
111
112         return 0;
113 }
114
115 struct dpaa2_dpbp_dev *dpaa2_alloc_dpbp_dev(void)
116 {
117         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
118
119         /* Get DPBP dev handle from list using index */
120         TAILQ_FOREACH(dpbp_dev, dpbp_dev_list, next) {
121                 if (dpbp_dev && rte_atomic16_test_and_set(&dpbp_dev->in_use))
122                         break;
123         }
124
125         return dpbp_dev;
126 }
127
128 void dpaa2_free_dpbp_dev(struct dpaa2_dpbp_dev *dpbp)
129 {
130         struct dpaa2_dpbp_dev *dpbp_dev = NULL;
131
132         /* Match DPBP handle and mark it free */
133         TAILQ_FOREACH(dpbp_dev, dpbp_dev_list, next) {
134                 if (dpbp_dev == dpbp) {
135                         rte_atomic16_dec(&dpbp_dev->in_use);
136                         return;
137                 }
138         }
139 }