drivers/dpaa2: remove duplicate includes
[dpdk.git] / drivers / net / dpaa2 / base / dpaa2_hw_dpni.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright 2016 NXP.
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 <time.h>
35 #include <net/if.h>
36
37 #include <rte_mbuf.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_cycles.h>
43 #include <rte_kvargs.h>
44 #include <rte_dev.h>
45
46 #include <fslmc_logs.h>
47 #include <dpaa2_hw_pvt.h>
48 #include <dpaa2_hw_mempool.h>
49
50 #include "../dpaa2_ethdev.h"
51
52 static void
53 dpaa2_distset_to_dpkg_profile_cfg(
54                 uint32_t req_dist_set,
55                 struct dpkg_profile_cfg *kg_cfg);
56
57 int
58 dpaa2_setup_flow_dist(struct rte_eth_dev *eth_dev,
59                       uint32_t req_dist_set)
60 {
61         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
62         struct fsl_mc_io *dpni = priv->hw;
63         struct dpni_rx_tc_dist_cfg tc_cfg;
64         struct dpkg_profile_cfg kg_cfg;
65         void *p_params;
66         int ret, tc_index = 0;
67
68         p_params = rte_malloc(
69                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
70         if (!p_params) {
71                 RTE_LOG(ERR, PMD, "Memory unavaialble\n");
72                 return -ENOMEM;
73         }
74         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
75         memset(&tc_cfg, 0, sizeof(struct dpni_rx_tc_dist_cfg));
76
77         dpaa2_distset_to_dpkg_profile_cfg(req_dist_set, &kg_cfg);
78         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
79         tc_cfg.dist_size = eth_dev->data->nb_rx_queues;
80         tc_cfg.dist_mode = DPNI_DIST_MODE_HASH;
81
82         ret = dpni_prepare_key_cfg(&kg_cfg, p_params);
83         if (ret) {
84                 RTE_LOG(ERR, PMD, "Unable to prepare extract parameters\n");
85                 rte_free(p_params);
86                 return ret;
87         }
88
89         ret = dpni_set_rx_tc_dist(dpni, CMD_PRI_LOW, priv->token, tc_index,
90                                   &tc_cfg);
91         rte_free(p_params);
92         if (ret) {
93                 RTE_LOG(ERR, PMD,
94                         "Setting distribution for Rx failed with err: %d\n",
95                         ret);
96                 return ret;
97         }
98
99         return 0;
100 }
101
102 int dpaa2_remove_flow_dist(
103         struct rte_eth_dev *eth_dev,
104         uint8_t tc_index)
105 {
106         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
107         struct fsl_mc_io *dpni = priv->hw;
108         struct dpni_rx_tc_dist_cfg tc_cfg;
109         struct dpkg_profile_cfg kg_cfg;
110         void *p_params;
111         int ret;
112
113         p_params = rte_malloc(
114                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
115         if (!p_params) {
116                 RTE_LOG(ERR, PMD, "Memory unavaialble\n");
117                 return -ENOMEM;
118         }
119         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
120         memset(&tc_cfg, 0, sizeof(struct dpni_rx_tc_dist_cfg));
121
122         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
123         tc_cfg.dist_size = 0;
124         tc_cfg.dist_mode = DPNI_DIST_MODE_NONE;
125
126         ret = dpni_prepare_key_cfg(&kg_cfg, p_params);
127         if (ret) {
128                 RTE_LOG(ERR, PMD, "Unable to prepare extract parameters\n");
129                 rte_free(p_params);
130                 return ret;
131         }
132
133         ret = dpni_set_rx_tc_dist(dpni, CMD_PRI_LOW, priv->token, tc_index,
134                                   &tc_cfg);
135         rte_free(p_params);
136         if (ret) {
137                 RTE_LOG(ERR, PMD,
138                         "Setting distribution for Rx failed with err: %d\n",
139                         ret);
140                 return ret;
141         }
142         return ret;
143 }
144
145 static void
146 dpaa2_distset_to_dpkg_profile_cfg(
147                 uint32_t req_dist_set,
148                 struct dpkg_profile_cfg *kg_cfg)
149 {
150         uint32_t loop = 0, i = 0, dist_field = 0;
151         int l2_configured = 0, l3_configured = 0;
152         int l4_configured = 0, sctp_configured = 0;
153
154         memset(kg_cfg, 0, sizeof(struct dpkg_profile_cfg));
155         while (req_dist_set) {
156                 if (req_dist_set % 2 != 0) {
157                         dist_field = 1U << loop;
158                         switch (dist_field) {
159                         case ETH_RSS_L2_PAYLOAD:
160
161                                 if (l2_configured)
162                                         break;
163                                 l2_configured = 1;
164
165                                 kg_cfg->extracts[i].extract.from_hdr.prot =
166                                         NET_PROT_ETH;
167                                 kg_cfg->extracts[i].extract.from_hdr.field =
168                                         NH_FLD_ETH_TYPE;
169                                 kg_cfg->extracts[i].type =
170                                         DPKG_EXTRACT_FROM_HDR;
171                                 kg_cfg->extracts[i].extract.from_hdr.type =
172                                         DPKG_FULL_FIELD;
173                                 i++;
174                         break;
175
176                         case ETH_RSS_IPV4:
177                         case ETH_RSS_FRAG_IPV4:
178                         case ETH_RSS_NONFRAG_IPV4_OTHER:
179                         case ETH_RSS_IPV6:
180                         case ETH_RSS_FRAG_IPV6:
181                         case ETH_RSS_NONFRAG_IPV6_OTHER:
182                         case ETH_RSS_IPV6_EX:
183
184                                 if (l3_configured)
185                                         break;
186                                 l3_configured = 1;
187
188                                 kg_cfg->extracts[i].extract.from_hdr.prot =
189                                         NET_PROT_IP;
190                                 kg_cfg->extracts[i].extract.from_hdr.field =
191                                         NH_FLD_IP_SRC;
192                                 kg_cfg->extracts[i].type =
193                                         DPKG_EXTRACT_FROM_HDR;
194                                 kg_cfg->extracts[i].extract.from_hdr.type =
195                                         DPKG_FULL_FIELD;
196                                 i++;
197
198                                 kg_cfg->extracts[i].extract.from_hdr.prot =
199                                         NET_PROT_IP;
200                                 kg_cfg->extracts[i].extract.from_hdr.field =
201                                         NH_FLD_IP_DST;
202                                 kg_cfg->extracts[i].type =
203                                         DPKG_EXTRACT_FROM_HDR;
204                                 kg_cfg->extracts[i].extract.from_hdr.type =
205                                         DPKG_FULL_FIELD;
206                                 i++;
207
208                                 kg_cfg->extracts[i].extract.from_hdr.prot =
209                                         NET_PROT_IP;
210                                 kg_cfg->extracts[i].extract.from_hdr.field =
211                                         NH_FLD_IP_PROTO;
212                                 kg_cfg->extracts[i].type =
213                                         DPKG_EXTRACT_FROM_HDR;
214                                 kg_cfg->extracts[i].extract.from_hdr.type =
215                                         DPKG_FULL_FIELD;
216                                 kg_cfg->num_extracts++;
217                                 i++;
218                         break;
219
220                         case ETH_RSS_NONFRAG_IPV4_TCP:
221                         case ETH_RSS_NONFRAG_IPV6_TCP:
222                         case ETH_RSS_NONFRAG_IPV4_UDP:
223                         case ETH_RSS_NONFRAG_IPV6_UDP:
224                         case ETH_RSS_IPV6_TCP_EX:
225                         case ETH_RSS_IPV6_UDP_EX:
226
227                                 if (l4_configured)
228                                         break;
229                                 l4_configured = 1;
230
231                                 kg_cfg->extracts[i].extract.from_hdr.prot =
232                                         NET_PROT_TCP;
233                                 kg_cfg->extracts[i].extract.from_hdr.field =
234                                         NH_FLD_TCP_PORT_SRC;
235                                 kg_cfg->extracts[i].type =
236                                         DPKG_EXTRACT_FROM_HDR;
237                                 kg_cfg->extracts[i].extract.from_hdr.type =
238                                         DPKG_FULL_FIELD;
239                                 i++;
240
241                                 kg_cfg->extracts[i].extract.from_hdr.prot =
242                                         NET_PROT_TCP;
243                                 kg_cfg->extracts[i].extract.from_hdr.field =
244                                         NH_FLD_TCP_PORT_SRC;
245                                 kg_cfg->extracts[i].type =
246                                         DPKG_EXTRACT_FROM_HDR;
247                                 kg_cfg->extracts[i].extract.from_hdr.type =
248                                         DPKG_FULL_FIELD;
249                                 i++;
250                                 break;
251
252                         case ETH_RSS_NONFRAG_IPV4_SCTP:
253                         case ETH_RSS_NONFRAG_IPV6_SCTP:
254
255                                 if (sctp_configured)
256                                         break;
257                                 sctp_configured = 1;
258
259                                 kg_cfg->extracts[i].extract.from_hdr.prot =
260                                         NET_PROT_SCTP;
261                                 kg_cfg->extracts[i].extract.from_hdr.field =
262                                         NH_FLD_SCTP_PORT_SRC;
263                                 kg_cfg->extracts[i].type =
264                                         DPKG_EXTRACT_FROM_HDR;
265                                 kg_cfg->extracts[i].extract.from_hdr.type =
266                                         DPKG_FULL_FIELD;
267                                 i++;
268
269                                 kg_cfg->extracts[i].extract.from_hdr.prot =
270                                         NET_PROT_SCTP;
271                                 kg_cfg->extracts[i].extract.from_hdr.field =
272                                         NH_FLD_SCTP_PORT_DST;
273                                 kg_cfg->extracts[i].type =
274                                         DPKG_EXTRACT_FROM_HDR;
275                                 kg_cfg->extracts[i].extract.from_hdr.type =
276                                         DPKG_FULL_FIELD;
277                                 i++;
278                                 break;
279
280                         default:
281                                 PMD_DRV_LOG(WARNING, "Bad flow distribution"
282                                             " option %x\n", dist_field);
283                         }
284                 }
285                 req_dist_set = req_dist_set >> 1;
286                 loop++;
287         }
288         kg_cfg->num_extracts = i;
289 }
290
291 int
292 dpaa2_attach_bp_list(struct dpaa2_dev_priv *priv,
293                      void *blist)
294 {
295         /* Function to attach a DPNI with a buffer pool list. Buffer pool list
296          * handle is passed in blist.
297          */
298         int32_t retcode;
299         struct fsl_mc_io *dpni = priv->hw;
300         struct dpni_pools_cfg bpool_cfg;
301         struct dpaa2_bp_list *bp_list = (struct dpaa2_bp_list *)blist;
302         struct dpni_buffer_layout layout;
303         int tot_size;
304
305         /* ... rx buffer layout .
306          * Check alignment for buffer layouts first
307          */
308
309         /* ... rx buffer layout ... */
310         tot_size = RTE_PKTMBUF_HEADROOM;
311         tot_size = RTE_ALIGN_CEIL(tot_size, DPAA2_PACKET_LAYOUT_ALIGN);
312
313         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
314         layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
315                          DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
316                          DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
317                          DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
318                          DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
319
320         layout.pass_frame_status = 1;
321         layout.private_data_size = DPAA2_FD_PTA_SIZE;
322         layout.pass_parser_result = 1;
323         layout.data_align = DPAA2_PACKET_LAYOUT_ALIGN;
324         layout.data_head_room = tot_size - DPAA2_FD_PTA_SIZE -
325                                 DPAA2_MBUF_HW_ANNOTATION;
326         retcode = dpni_set_buffer_layout(dpni, CMD_PRI_LOW, priv->token,
327                                          DPNI_QUEUE_RX, &layout);
328         if (retcode) {
329                 PMD_INIT_LOG(ERR, "Err(%d) in setting rx buffer layout\n",
330                              retcode);
331                 return retcode;
332         }
333
334         /*Attach buffer pool to the network interface as described by the user*/
335         bpool_cfg.num_dpbp = 1;
336         bpool_cfg.pools[0].dpbp_id = bp_list->buf_pool.dpbp_node->dpbp_id;
337         bpool_cfg.pools[0].backup_pool = 0;
338         bpool_cfg.pools[0].buffer_size = RTE_ALIGN_CEIL(bp_list->buf_pool.size,
339                                                 DPAA2_PACKET_LAYOUT_ALIGN);
340
341         retcode = dpni_set_pools(dpni, CMD_PRI_LOW, priv->token, &bpool_cfg);
342         if (retcode != 0) {
343                 PMD_INIT_LOG(ERR, "Error in attaching the buffer pool list"
344                                 " bpid = %d Error code = %d\n",
345                                 bpool_cfg.pools[0].dpbp_id, retcode);
346                 return retcode;
347         }
348
349         priv->bp_list = bp_list;
350         return 0;
351 }