478e4f7e12116e43ccd624c4f09c2bdd18b1decb
[deb_dpdk.git] / drivers / bus / fslmc / portal / dpaa2_hw_dpci.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 Freescale Semiconductor, Inc 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
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <errno.h>
40
41 #include <rte_malloc.h>
42 #include <rte_memcpy.h>
43 #include <rte_string_fns.h>
44 #include <rte_cycles.h>
45 #include <rte_kvargs.h>
46 #include <rte_dev.h>
47 #include <rte_ethdev.h>
48
49 #include <fslmc_logs.h>
50 #include <fslmc_vfio.h>
51 #include <mc/fsl_dpci.h>
52 #include "portal/dpaa2_hw_pvt.h"
53 #include "portal/dpaa2_hw_dpio.h"
54
55 TAILQ_HEAD(dpci_dev_list, dpaa2_dpci_dev);
56 static struct dpci_dev_list dpci_dev_list
57         = TAILQ_HEAD_INITIALIZER(dpci_dev_list); /*!< DPCI device list */
58
59 static int
60 rte_dpaa2_create_dpci_device(struct fslmc_vfio_device *vdev __rte_unused,
61                              struct vfio_device_info *obj_info __rte_unused,
62                                 int dpci_id)
63 {
64         struct dpaa2_dpci_dev *dpci_node;
65         struct dpci_attr attr;
66         struct dpci_rx_queue_cfg rx_queue_cfg;
67         struct dpci_rx_queue_attr rx_attr;
68         int ret, i;
69
70         /* Allocate DPAA2 dpci handle */
71         dpci_node = rte_malloc(NULL, sizeof(struct dpaa2_dpci_dev), 0);
72         if (!dpci_node) {
73                 PMD_INIT_LOG(ERR, "Memory allocation failed for DPCI Device");
74                 return -1;
75         }
76
77         /* Open the dpci object */
78         dpci_node->dpci.regs = rte_mcp_ptr_list[MC_PORTAL_INDEX];
79         ret = dpci_open(&dpci_node->dpci,
80                         CMD_PRI_LOW, dpci_id, &dpci_node->token);
81         if (ret) {
82                 PMD_INIT_LOG(ERR, "Resource alloc failure with err code: %d",
83                              ret);
84                 rte_free(dpci_node);
85                 return -1;
86         }
87
88         /* Get the device attributes */
89         ret = dpci_get_attributes(&dpci_node->dpci,
90                                   CMD_PRI_LOW, dpci_node->token, &attr);
91         if (ret != 0) {
92                 PMD_INIT_LOG(ERR, "Reading device failed with err code: %d",
93                              ret);
94                 rte_free(dpci_node);
95                 return -1;
96         }
97
98         /* Set up the Rx Queue */
99         memset(&rx_queue_cfg, 0, sizeof(struct dpci_rx_queue_cfg));
100         ret = dpci_set_rx_queue(&dpci_node->dpci,
101                                 CMD_PRI_LOW,
102                                 dpci_node->token,
103                                 0, &rx_queue_cfg);
104         if (ret) {
105                 PMD_INIT_LOG(ERR, "Setting Rx queue failed with err code: %d",
106                              ret);
107                 rte_free(dpci_node);
108                 return -1;
109         }
110
111         /* Enable the device */
112         ret = dpci_enable(&dpci_node->dpci,
113                           CMD_PRI_LOW, dpci_node->token);
114         if (ret != 0) {
115                 PMD_INIT_LOG(ERR, "Enabling device failed with err code: %d",
116                              ret);
117                 rte_free(dpci_node);
118                 return -1;
119         }
120
121         for (i = 0; i < DPAA2_DPCI_MAX_QUEUES; i++) {
122                 /* Get the Rx FQID's */
123                 ret = dpci_get_rx_queue(&dpci_node->dpci,
124                                         CMD_PRI_LOW,
125                                         dpci_node->token, i,
126                                         &rx_attr);
127                 if (ret != 0) {
128                         PMD_INIT_LOG(ERR,
129                                      "Reading device failed with err code: %d",
130                                 ret);
131                         rte_free(dpci_node);
132                         return -1;
133                 }
134
135                 dpci_node->queue[i].fqid = rx_attr.fqid;
136         }
137
138         dpci_node->dpci_id = dpci_id;
139         rte_atomic16_init(&dpci_node->in_use);
140
141         TAILQ_INSERT_TAIL(&dpci_dev_list, dpci_node, next);
142
143         PMD_INIT_LOG(DEBUG, "DPAA2: Added [dpci.%d]", dpci_id);
144
145         return 0;
146 }
147
148 struct dpaa2_dpci_dev *rte_dpaa2_alloc_dpci_dev(void)
149 {
150         struct dpaa2_dpci_dev *dpci_dev = NULL;
151
152         /* Get DPCI dev handle from list using index */
153         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
154                 if (dpci_dev && rte_atomic16_test_and_set(&dpci_dev->in_use))
155                         break;
156         }
157
158         return dpci_dev;
159 }
160
161 void rte_dpaa2_free_dpci_dev(struct dpaa2_dpci_dev *dpci)
162 {
163         struct dpaa2_dpci_dev *dpci_dev = NULL;
164
165         /* Match DPCI handle and mark it free */
166         TAILQ_FOREACH(dpci_dev, &dpci_dev_list, next) {
167                 if (dpci_dev == dpci) {
168                         rte_atomic16_dec(&dpci_dev->in_use);
169                         return;
170                 }
171         }
172 }
173
174 static struct rte_dpaa2_object rte_dpaa2_dpci_obj = {
175         .object_id = DPAA2_MC_DPCI_DEVID,
176         .create = rte_dpaa2_create_dpci_device,
177 };
178
179 RTE_PMD_REGISTER_DPAA2_OBJECT(dpci, rte_dpaa2_dpci_obj);