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