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