d69156bcc8f158417a327f5fcfc66e0f315a248a
[dpdk.git] / drivers / net / dpaa2 / base / dpaa2_hw_dpni.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016-2019 NXP
5  *
6  */
7
8 #include <time.h>
9 #include <net/if.h>
10
11 #include <rte_mbuf.h>
12 #include <rte_ethdev_driver.h>
13 #include <rte_malloc.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_cycles.h>
17 #include <rte_kvargs.h>
18 #include <rte_dev.h>
19
20 #include <dpaa2_pmd_logs.h>
21 #include <dpaa2_hw_pvt.h>
22 #include <dpaa2_hw_mempool.h>
23
24 #include "../dpaa2_ethdev.h"
25
26 int
27 dpaa2_distset_to_dpkg_profile_cfg(
28                 uint64_t req_dist_set,
29                 struct dpkg_profile_cfg *kg_cfg);
30
31 int
32 rte_pmd_dpaa2_set_custom_hash(uint16_t port_id,
33                               uint16_t offset,
34                               uint8_t size)
35 {
36         struct rte_eth_dev *eth_dev = &rte_eth_devices[port_id];
37         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
38         struct fsl_mc_io *dpni = priv->hw;
39         struct dpni_rx_tc_dist_cfg tc_cfg;
40         struct dpkg_profile_cfg kg_cfg;
41         void *p_params;
42         int ret, tc_index = 0;
43
44         p_params = rte_zmalloc(
45                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
46         if (!p_params) {
47                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
48                 return -ENOMEM;
49         }
50
51         kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_DATA;
52         kg_cfg.extracts[0].extract.from_data.offset = offset;
53         kg_cfg.extracts[0].extract.from_data.size = size;
54         kg_cfg.extracts[0].num_of_byte_masks = 0;
55         kg_cfg.num_extracts = 1;
56
57         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
58         if (ret) {
59                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
60                 rte_free(p_params);
61                 return ret;
62         }
63
64         memset(&tc_cfg, 0, sizeof(struct dpni_rx_tc_dist_cfg));
65         tc_cfg.key_cfg_iova = (size_t)(DPAA2_VADDR_TO_IOVA(p_params));
66         tc_cfg.dist_size = eth_dev->data->nb_rx_queues;
67         tc_cfg.dist_mode = DPNI_DIST_MODE_HASH;
68
69         ret = dpni_set_rx_tc_dist(dpni, CMD_PRI_LOW, priv->token, tc_index,
70                                   &tc_cfg);
71         rte_free(p_params);
72         if (ret) {
73                 DPAA2_PMD_ERR(
74                              "Setting distribution for Rx failed with err: %d",
75                              ret);
76                 return ret;
77         }
78
79         return 0;
80 }
81
82 int
83 dpaa2_setup_flow_dist(struct rte_eth_dev *eth_dev,
84         uint64_t req_dist_set, int tc_index)
85 {
86         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
87         struct fsl_mc_io *dpni = priv->hw;
88         struct dpni_rx_dist_cfg tc_cfg;
89         struct dpkg_profile_cfg kg_cfg;
90         void *p_params;
91         int ret;
92
93         p_params = rte_malloc(
94                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
95         if (!p_params) {
96                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
97                 return -ENOMEM;
98         }
99
100         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
101         memset(&tc_cfg, 0, sizeof(struct dpni_rx_dist_cfg));
102
103         ret = dpaa2_distset_to_dpkg_profile_cfg(req_dist_set, &kg_cfg);
104         if (ret) {
105                 DPAA2_PMD_ERR("Given RSS Hash (%" PRIx64 ") not supported",
106                               req_dist_set);
107                 rte_free(p_params);
108                 return ret;
109         }
110
111         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
112         tc_cfg.dist_size = priv->dist_queues;
113         tc_cfg.enable = true;
114         tc_cfg.tc = tc_index;
115
116         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
117         if (ret) {
118                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
119                 rte_free(p_params);
120                 return ret;
121         }
122
123         ret = dpni_set_rx_hash_dist(dpni, CMD_PRI_LOW, priv->token, &tc_cfg);
124         rte_free(p_params);
125         if (ret) {
126                 DPAA2_PMD_ERR(
127                              "Setting distribution for Rx failed with err: %d",
128                              ret);
129                 return ret;
130         }
131
132         return 0;
133 }
134
135 int dpaa2_remove_flow_dist(
136         struct rte_eth_dev *eth_dev,
137         uint8_t tc_index)
138 {
139         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
140         struct fsl_mc_io *dpni = priv->hw;
141         struct dpni_rx_dist_cfg tc_cfg;
142         struct dpkg_profile_cfg kg_cfg;
143         void *p_params;
144         int ret;
145
146         p_params = rte_malloc(
147                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
148         if (!p_params) {
149                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
150                 return -ENOMEM;
151         }
152
153         memset(&tc_cfg, 0, sizeof(struct dpni_rx_dist_cfg));
154         tc_cfg.dist_size = 0;
155         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
156         tc_cfg.enable = true;
157         tc_cfg.tc = tc_index;
158
159         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
160         kg_cfg.num_extracts = 0;
161         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
162         if (ret) {
163                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
164                 rte_free(p_params);
165                 return ret;
166         }
167
168         ret = dpni_set_rx_hash_dist(dpni, CMD_PRI_LOW, priv->token,
169                         &tc_cfg);
170         rte_free(p_params);
171         if (ret)
172                 DPAA2_PMD_ERR(
173                              "Setting distribution for Rx failed with err: %d",
174                              ret);
175         return ret;
176 }
177
178 int
179 dpaa2_distset_to_dpkg_profile_cfg(
180                 uint64_t req_dist_set,
181                 struct dpkg_profile_cfg *kg_cfg)
182 {
183         uint32_t loop = 0, i = 0, dist_field = 0;
184         int l2_configured = 0, l3_configured = 0;
185         int l4_configured = 0, sctp_configured = 0;
186
187         memset(kg_cfg, 0, sizeof(struct dpkg_profile_cfg));
188         while (req_dist_set) {
189                 if (req_dist_set % 2 != 0) {
190                         dist_field = 1U << loop;
191                         switch (dist_field) {
192                         case ETH_RSS_L2_PAYLOAD:
193
194                                 if (l2_configured)
195                                         break;
196                                 l2_configured = 1;
197
198                                 kg_cfg->extracts[i].extract.from_hdr.prot =
199                                         NET_PROT_ETH;
200                                 kg_cfg->extracts[i].extract.from_hdr.field =
201                                         NH_FLD_ETH_TYPE;
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                         break;
208
209                         case ETH_RSS_IPV4:
210                         case ETH_RSS_FRAG_IPV4:
211                         case ETH_RSS_NONFRAG_IPV4_OTHER:
212                         case ETH_RSS_IPV6:
213                         case ETH_RSS_FRAG_IPV6:
214                         case ETH_RSS_NONFRAG_IPV6_OTHER:
215                         case ETH_RSS_IPV6_EX:
216
217                                 if (l3_configured)
218                                         break;
219                                 l3_configured = 1;
220
221                                 kg_cfg->extracts[i].extract.from_hdr.prot =
222                                         NET_PROT_IP;
223                                 kg_cfg->extracts[i].extract.from_hdr.field =
224                                         NH_FLD_IP_SRC;
225                                 kg_cfg->extracts[i].type =
226                                         DPKG_EXTRACT_FROM_HDR;
227                                 kg_cfg->extracts[i].extract.from_hdr.type =
228                                         DPKG_FULL_FIELD;
229                                 i++;
230
231                                 kg_cfg->extracts[i].extract.from_hdr.prot =
232                                         NET_PROT_IP;
233                                 kg_cfg->extracts[i].extract.from_hdr.field =
234                                         NH_FLD_IP_DST;
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_IP;
243                                 kg_cfg->extracts[i].extract.from_hdr.field =
244                                         NH_FLD_IP_PROTO;
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                                 kg_cfg->num_extracts++;
250                                 i++;
251                         break;
252
253                         case ETH_RSS_NONFRAG_IPV4_TCP:
254                         case ETH_RSS_NONFRAG_IPV6_TCP:
255                         case ETH_RSS_NONFRAG_IPV4_UDP:
256                         case ETH_RSS_NONFRAG_IPV6_UDP:
257                         case ETH_RSS_IPV6_TCP_EX:
258                         case ETH_RSS_IPV6_UDP_EX:
259
260                                 if (l4_configured)
261                                         break;
262                                 l4_configured = 1;
263
264                                 kg_cfg->extracts[i].extract.from_hdr.prot =
265                                         NET_PROT_TCP;
266                                 kg_cfg->extracts[i].extract.from_hdr.field =
267                                         NH_FLD_TCP_PORT_SRC;
268                                 kg_cfg->extracts[i].type =
269                                         DPKG_EXTRACT_FROM_HDR;
270                                 kg_cfg->extracts[i].extract.from_hdr.type =
271                                         DPKG_FULL_FIELD;
272                                 i++;
273
274                                 kg_cfg->extracts[i].extract.from_hdr.prot =
275                                         NET_PROT_TCP;
276                                 kg_cfg->extracts[i].extract.from_hdr.field =
277                                         NH_FLD_TCP_PORT_SRC;
278                                 kg_cfg->extracts[i].type =
279                                         DPKG_EXTRACT_FROM_HDR;
280                                 kg_cfg->extracts[i].extract.from_hdr.type =
281                                         DPKG_FULL_FIELD;
282                                 i++;
283                                 break;
284
285                         case ETH_RSS_NONFRAG_IPV4_SCTP:
286                         case ETH_RSS_NONFRAG_IPV6_SCTP:
287
288                                 if (sctp_configured)
289                                         break;
290                                 sctp_configured = 1;
291
292                                 kg_cfg->extracts[i].extract.from_hdr.prot =
293                                         NET_PROT_SCTP;
294                                 kg_cfg->extracts[i].extract.from_hdr.field =
295                                         NH_FLD_SCTP_PORT_SRC;
296                                 kg_cfg->extracts[i].type =
297                                         DPKG_EXTRACT_FROM_HDR;
298                                 kg_cfg->extracts[i].extract.from_hdr.type =
299                                         DPKG_FULL_FIELD;
300                                 i++;
301
302                                 kg_cfg->extracts[i].extract.from_hdr.prot =
303                                         NET_PROT_SCTP;
304                                 kg_cfg->extracts[i].extract.from_hdr.field =
305                                         NH_FLD_SCTP_PORT_DST;
306                                 kg_cfg->extracts[i].type =
307                                         DPKG_EXTRACT_FROM_HDR;
308                                 kg_cfg->extracts[i].extract.from_hdr.type =
309                                         DPKG_FULL_FIELD;
310                                 i++;
311                                 break;
312
313                         default:
314                                 DPAA2_PMD_WARN(
315                                              "Unsupported flow dist option %x",
316                                              dist_field);
317                                 return -EINVAL;
318                         }
319                 }
320                 req_dist_set = req_dist_set >> 1;
321                 loop++;
322         }
323         kg_cfg->num_extracts = i;
324         return 0;
325 }
326
327 int
328 dpaa2_attach_bp_list(struct dpaa2_dev_priv *priv,
329                      void *blist)
330 {
331         /* Function to attach a DPNI with a buffer pool list. Buffer pool list
332          * handle is passed in blist.
333          */
334         int32_t retcode;
335         struct fsl_mc_io *dpni = priv->hw;
336         struct dpni_pools_cfg bpool_cfg;
337         struct dpaa2_bp_list *bp_list = (struct dpaa2_bp_list *)blist;
338         struct dpni_buffer_layout layout;
339         int tot_size;
340
341         /* ... rx buffer layout .
342          * Check alignment for buffer layouts first
343          */
344
345         /* ... rx buffer layout ... */
346         tot_size = RTE_PKTMBUF_HEADROOM;
347         tot_size = RTE_ALIGN_CEIL(tot_size, DPAA2_PACKET_LAYOUT_ALIGN);
348
349         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
350         layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
351                          DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
352                          DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
353                          DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
354                          DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
355                          DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
356
357         layout.pass_timestamp = true;
358         layout.pass_frame_status = 1;
359         layout.private_data_size = DPAA2_FD_PTA_SIZE;
360         layout.pass_parser_result = 1;
361         layout.data_align = DPAA2_PACKET_LAYOUT_ALIGN;
362         layout.data_head_room = tot_size - DPAA2_FD_PTA_SIZE -
363                                 DPAA2_MBUF_HW_ANNOTATION;
364         retcode = dpni_set_buffer_layout(dpni, CMD_PRI_LOW, priv->token,
365                                          DPNI_QUEUE_RX, &layout);
366         if (retcode) {
367                 DPAA2_PMD_ERR("Error configuring buffer pool Rx layout (%d)",
368                              retcode);
369                 return retcode;
370         }
371
372         /*Attach buffer pool to the network interface as described by the user*/
373         memset(&bpool_cfg, 0, sizeof(struct dpni_pools_cfg));
374         bpool_cfg.num_dpbp = 1;
375         bpool_cfg.pools[0].dpbp_id = bp_list->buf_pool.dpbp_node->dpbp_id;
376         bpool_cfg.pools[0].backup_pool = 0;
377         bpool_cfg.pools[0].buffer_size = RTE_ALIGN_CEIL(bp_list->buf_pool.size,
378                                                 DPAA2_PACKET_LAYOUT_ALIGN);
379         bpool_cfg.pools[0].priority_mask = 0;
380
381         retcode = dpni_set_pools(dpni, CMD_PRI_LOW, priv->token, &bpool_cfg);
382         if (retcode != 0) {
383                 DPAA2_PMD_ERR("Error configuring buffer pool on interface."
384                               " bpid = %d error code = %d",
385                               bpool_cfg.pools[0].dpbp_id, retcode);
386                 return retcode;
387         }
388
389         priv->bp_list = bp_list;
390         return 0;
391 }