net/dpaa2: add more RSS distributions
[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-2021 NXP
5  *
6  */
7
8 #include <time.h>
9 #include <net/if.h>
10
11 #include <rte_mbuf.h>
12 #include <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         if (!rte_eth_dev_is_valid_port(port_id)) {
45                 DPAA2_PMD_WARN("Invalid port id %u", port_id);
46                 return -EINVAL;
47         }
48
49         if (strcmp(eth_dev->device->driver->name,
50                         RTE_STR(NET_DPAA2_PMD_DRIVER_NAME))) {
51                 DPAA2_PMD_WARN("Not a valid dpaa2 port");
52                 return -EINVAL;
53         }
54
55         p_params = rte_zmalloc(
56                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
57         if (!p_params) {
58                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
59                 return -ENOMEM;
60         }
61
62         kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_DATA;
63         kg_cfg.extracts[0].extract.from_data.offset = offset;
64         kg_cfg.extracts[0].extract.from_data.size = size;
65         kg_cfg.extracts[0].num_of_byte_masks = 0;
66         kg_cfg.num_extracts = 1;
67
68         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
69         if (ret) {
70                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
71                 rte_free(p_params);
72                 return ret;
73         }
74
75         memset(&tc_cfg, 0, sizeof(struct dpni_rx_tc_dist_cfg));
76         tc_cfg.key_cfg_iova = (size_t)(DPAA2_VADDR_TO_IOVA(p_params));
77         tc_cfg.dist_size = eth_dev->data->nb_rx_queues;
78         tc_cfg.dist_mode = DPNI_DIST_MODE_HASH;
79
80         ret = dpni_set_rx_tc_dist(dpni, CMD_PRI_LOW, priv->token, tc_index,
81                                   &tc_cfg);
82         rte_free(p_params);
83         if (ret) {
84                 DPAA2_PMD_ERR(
85                              "Setting distribution for Rx failed with err: %d",
86                              ret);
87                 return ret;
88         }
89
90         return 0;
91 }
92
93 int
94 dpaa2_setup_flow_dist(struct rte_eth_dev *eth_dev,
95         uint64_t req_dist_set, int tc_index)
96 {
97         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
98         struct fsl_mc_io *dpni = priv->hw;
99         struct dpni_rx_dist_cfg tc_cfg;
100         struct dpkg_profile_cfg kg_cfg;
101         void *p_params;
102         int ret, tc_dist_queues;
103
104         /*TC distribution size is set with dist_queues or
105          * nb_rx_queues % dist_queues in order of TC priority index.
106          * Calculating dist size for this tc_index:-
107          */
108         tc_dist_queues = eth_dev->data->nb_rx_queues -
109                 tc_index * priv->dist_queues;
110         if (tc_dist_queues <= 0) {
111                 DPAA2_PMD_INFO("No distribution on TC%d", tc_index);
112                 return 0;
113         }
114
115         if (tc_dist_queues > priv->dist_queues)
116                 tc_dist_queues = priv->dist_queues;
117
118         p_params = rte_malloc(
119                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
120         if (!p_params) {
121                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
122                 return -ENOMEM;
123         }
124
125         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
126         memset(&tc_cfg, 0, sizeof(struct dpni_rx_dist_cfg));
127
128         ret = dpaa2_distset_to_dpkg_profile_cfg(req_dist_set, &kg_cfg);
129         if (ret) {
130                 DPAA2_PMD_ERR("Given RSS Hash (%" PRIx64 ") not supported",
131                               req_dist_set);
132                 rte_free(p_params);
133                 return ret;
134         }
135
136         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
137         tc_cfg.dist_size = tc_dist_queues;
138         tc_cfg.enable = true;
139         tc_cfg.tc = tc_index;
140
141         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
142         if (ret) {
143                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
144                 rte_free(p_params);
145                 return ret;
146         }
147
148         ret = dpni_set_rx_hash_dist(dpni, CMD_PRI_LOW, priv->token, &tc_cfg);
149         rte_free(p_params);
150         if (ret) {
151                 DPAA2_PMD_ERR(
152                              "Setting distribution for Rx failed with err: %d",
153                              ret);
154                 return ret;
155         }
156
157         return 0;
158 }
159
160 int dpaa2_remove_flow_dist(
161         struct rte_eth_dev *eth_dev,
162         uint8_t tc_index)
163 {
164         struct dpaa2_dev_priv *priv = eth_dev->data->dev_private;
165         struct fsl_mc_io *dpni = priv->hw;
166         struct dpni_rx_dist_cfg tc_cfg;
167         struct dpkg_profile_cfg kg_cfg;
168         void *p_params;
169         int ret;
170
171         p_params = rte_malloc(
172                 NULL, DIST_PARAM_IOVA_SIZE, RTE_CACHE_LINE_SIZE);
173         if (!p_params) {
174                 DPAA2_PMD_ERR("Unable to allocate flow-dist parameters");
175                 return -ENOMEM;
176         }
177
178         memset(&tc_cfg, 0, sizeof(struct dpni_rx_dist_cfg));
179         tc_cfg.dist_size = 0;
180         tc_cfg.key_cfg_iova = (uint64_t)(DPAA2_VADDR_TO_IOVA(p_params));
181         tc_cfg.enable = true;
182         tc_cfg.tc = tc_index;
183
184         memset(p_params, 0, DIST_PARAM_IOVA_SIZE);
185         kg_cfg.num_extracts = 0;
186         ret = dpkg_prepare_key_cfg(&kg_cfg, p_params);
187         if (ret) {
188                 DPAA2_PMD_ERR("Unable to prepare extract parameters");
189                 rte_free(p_params);
190                 return ret;
191         }
192
193         ret = dpni_set_rx_hash_dist(dpni, CMD_PRI_LOW, priv->token,
194                         &tc_cfg);
195         rte_free(p_params);
196         if (ret)
197                 DPAA2_PMD_ERR(
198                              "Setting distribution for Rx failed with err: %d",
199                              ret);
200         return ret;
201 }
202
203 int
204 dpaa2_distset_to_dpkg_profile_cfg(
205                 uint64_t req_dist_set,
206                 struct dpkg_profile_cfg *kg_cfg)
207 {
208         uint32_t loop = 0, i = 0;
209         uint64_t dist_field = 0;
210         int l2_configured = 0, l3_configured = 0;
211         int l4_configured = 0, sctp_configured = 0;
212         int mpls_configured = 0;
213         int vlan_configured = 0;
214         int esp_configured = 0;
215         int ah_configured = 0;
216         int pppoe_configured = 0;
217
218         memset(kg_cfg, 0, sizeof(struct dpkg_profile_cfg));
219         while (req_dist_set) {
220                 if (req_dist_set % 2 != 0) {
221                         dist_field = 1ULL << loop;
222                         switch (dist_field) {
223                         case ETH_RSS_L2_PAYLOAD:
224                         case ETH_RSS_ETH:
225
226                                 if (l2_configured)
227                                         break;
228                                 l2_configured = 1;
229
230                                 kg_cfg->extracts[i].extract.from_hdr.prot =
231                                         NET_PROT_ETH;
232                                 kg_cfg->extracts[i].extract.from_hdr.field =
233                                         NH_FLD_ETH_TYPE;
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                                 break;
240
241                         case ETH_RSS_PPPOE:
242                                 if (pppoe_configured)
243                                         break;
244                                 kg_cfg->extracts[i].extract.from_hdr.prot =
245                                         NET_PROT_PPPOE;
246                                 kg_cfg->extracts[i].extract.from_hdr.field =
247                                         NH_FLD_PPPOE_SID;
248                                 kg_cfg->extracts[i].type =
249                                         DPKG_EXTRACT_FROM_HDR;
250                                 kg_cfg->extracts[i].extract.from_hdr.type =
251                                         DPKG_FULL_FIELD;
252                                 i++;
253                                 break;
254
255                         case ETH_RSS_ESP:
256                                 if (esp_configured)
257                                         break;
258                                 esp_configured = 1;
259
260                                 kg_cfg->extracts[i].extract.from_hdr.prot =
261                                         NET_PROT_IPSEC_ESP;
262                                 kg_cfg->extracts[i].extract.from_hdr.field =
263                                         NH_FLD_IPSEC_ESP_SPI;
264                                 kg_cfg->extracts[i].type =
265                                         DPKG_EXTRACT_FROM_HDR;
266                                 kg_cfg->extracts[i].extract.from_hdr.type =
267                                         DPKG_FULL_FIELD;
268                                 i++;
269                                 break;
270
271                         case ETH_RSS_AH:
272                                 if (ah_configured)
273                                         break;
274                                 ah_configured = 1;
275
276                                 kg_cfg->extracts[i].extract.from_hdr.prot =
277                                         NET_PROT_IPSEC_AH;
278                                 kg_cfg->extracts[i].extract.from_hdr.field =
279                                         NH_FLD_IPSEC_AH_SPI;
280                                 kg_cfg->extracts[i].type =
281                                         DPKG_EXTRACT_FROM_HDR;
282                                 kg_cfg->extracts[i].extract.from_hdr.type =
283                                         DPKG_FULL_FIELD;
284                                 i++;
285                                 break;
286
287                         case ETH_RSS_C_VLAN:
288                         case ETH_RSS_S_VLAN:
289                                 if (vlan_configured)
290                                         break;
291                                 vlan_configured = 1;
292
293                                 kg_cfg->extracts[i].extract.from_hdr.prot =
294                                         NET_PROT_VLAN;
295                                 kg_cfg->extracts[i].extract.from_hdr.field =
296                                         NH_FLD_VLAN_TCI;
297                                 kg_cfg->extracts[i].type =
298                                         DPKG_EXTRACT_FROM_HDR;
299                                 kg_cfg->extracts[i].extract.from_hdr.type =
300                                         DPKG_FULL_FIELD;
301                                 i++;
302                                 break;
303
304                         case ETH_RSS_MPLS:
305
306                                 if (mpls_configured)
307                                         break;
308                                 mpls_configured = 1;
309
310                                 kg_cfg->extracts[i].extract.from_hdr.prot =
311                                         NET_PROT_MPLS;
312                                 kg_cfg->extracts[i].extract.from_hdr.field =
313                                         NH_FLD_MPLS_MPLSL_1;
314                                 kg_cfg->extracts[i].type =
315                                         DPKG_EXTRACT_FROM_HDR;
316                                 kg_cfg->extracts[i].extract.from_hdr.type =
317                                         DPKG_FULL_FIELD;
318                                 i++;
319
320                                 kg_cfg->extracts[i].extract.from_hdr.prot =
321                                         NET_PROT_MPLS;
322                                 kg_cfg->extracts[i].extract.from_hdr.field =
323                                         NH_FLD_MPLS_MPLSL_2;
324                                 kg_cfg->extracts[i].type =
325                                         DPKG_EXTRACT_FROM_HDR;
326                                 kg_cfg->extracts[i].extract.from_hdr.type =
327                                         DPKG_FULL_FIELD;
328                                 i++;
329
330                                 kg_cfg->extracts[i].extract.from_hdr.prot =
331                                         NET_PROT_MPLS;
332                                 kg_cfg->extracts[i].extract.from_hdr.field =
333                                         NH_FLD_MPLS_MPLSL_N;
334                                 kg_cfg->extracts[i].type =
335                                         DPKG_EXTRACT_FROM_HDR;
336                                 kg_cfg->extracts[i].extract.from_hdr.type =
337                                         DPKG_FULL_FIELD;
338                                 i++;
339                                 break;
340
341                         case ETH_RSS_IPV4:
342                         case ETH_RSS_FRAG_IPV4:
343                         case ETH_RSS_NONFRAG_IPV4_OTHER:
344                         case ETH_RSS_IPV6:
345                         case ETH_RSS_FRAG_IPV6:
346                         case ETH_RSS_NONFRAG_IPV6_OTHER:
347                         case ETH_RSS_IPV6_EX:
348
349                                 if (l3_configured)
350                                         break;
351                                 l3_configured = 1;
352
353                                 kg_cfg->extracts[i].extract.from_hdr.prot =
354                                         NET_PROT_IP;
355                                 kg_cfg->extracts[i].extract.from_hdr.field =
356                                         NH_FLD_IP_SRC;
357                                 kg_cfg->extracts[i].type =
358                                         DPKG_EXTRACT_FROM_HDR;
359                                 kg_cfg->extracts[i].extract.from_hdr.type =
360                                         DPKG_FULL_FIELD;
361                                 i++;
362
363                                 kg_cfg->extracts[i].extract.from_hdr.prot =
364                                         NET_PROT_IP;
365                                 kg_cfg->extracts[i].extract.from_hdr.field =
366                                         NH_FLD_IP_DST;
367                                 kg_cfg->extracts[i].type =
368                                         DPKG_EXTRACT_FROM_HDR;
369                                 kg_cfg->extracts[i].extract.from_hdr.type =
370                                         DPKG_FULL_FIELD;
371                                 i++;
372
373                                 kg_cfg->extracts[i].extract.from_hdr.prot =
374                                         NET_PROT_IP;
375                                 kg_cfg->extracts[i].extract.from_hdr.field =
376                                         NH_FLD_IP_PROTO;
377                                 kg_cfg->extracts[i].type =
378                                         DPKG_EXTRACT_FROM_HDR;
379                                 kg_cfg->extracts[i].extract.from_hdr.type =
380                                         DPKG_FULL_FIELD;
381                                 kg_cfg->num_extracts++;
382                                 i++;
383                         break;
384
385                         case ETH_RSS_NONFRAG_IPV4_TCP:
386                         case ETH_RSS_NONFRAG_IPV6_TCP:
387                         case ETH_RSS_NONFRAG_IPV4_UDP:
388                         case ETH_RSS_NONFRAG_IPV6_UDP:
389                         case ETH_RSS_IPV6_TCP_EX:
390                         case ETH_RSS_IPV6_UDP_EX:
391
392                                 if (l4_configured)
393                                         break;
394                                 l4_configured = 1;
395
396                                 kg_cfg->extracts[i].extract.from_hdr.prot =
397                                         NET_PROT_TCP;
398                                 kg_cfg->extracts[i].extract.from_hdr.field =
399                                         NH_FLD_TCP_PORT_SRC;
400                                 kg_cfg->extracts[i].type =
401                                         DPKG_EXTRACT_FROM_HDR;
402                                 kg_cfg->extracts[i].extract.from_hdr.type =
403                                         DPKG_FULL_FIELD;
404                                 i++;
405
406                                 kg_cfg->extracts[i].extract.from_hdr.prot =
407                                         NET_PROT_TCP;
408                                 kg_cfg->extracts[i].extract.from_hdr.field =
409                                         NH_FLD_TCP_PORT_SRC;
410                                 kg_cfg->extracts[i].type =
411                                         DPKG_EXTRACT_FROM_HDR;
412                                 kg_cfg->extracts[i].extract.from_hdr.type =
413                                         DPKG_FULL_FIELD;
414                                 i++;
415                                 break;
416
417                         case ETH_RSS_NONFRAG_IPV4_SCTP:
418                         case ETH_RSS_NONFRAG_IPV6_SCTP:
419
420                                 if (sctp_configured)
421                                         break;
422                                 sctp_configured = 1;
423
424                                 kg_cfg->extracts[i].extract.from_hdr.prot =
425                                         NET_PROT_SCTP;
426                                 kg_cfg->extracts[i].extract.from_hdr.field =
427                                         NH_FLD_SCTP_PORT_SRC;
428                                 kg_cfg->extracts[i].type =
429                                         DPKG_EXTRACT_FROM_HDR;
430                                 kg_cfg->extracts[i].extract.from_hdr.type =
431                                         DPKG_FULL_FIELD;
432                                 i++;
433
434                                 kg_cfg->extracts[i].extract.from_hdr.prot =
435                                         NET_PROT_SCTP;
436                                 kg_cfg->extracts[i].extract.from_hdr.field =
437                                         NH_FLD_SCTP_PORT_DST;
438                                 kg_cfg->extracts[i].type =
439                                         DPKG_EXTRACT_FROM_HDR;
440                                 kg_cfg->extracts[i].extract.from_hdr.type =
441                                         DPKG_FULL_FIELD;
442                                 i++;
443                                 break;
444
445                         default:
446                                 DPAA2_PMD_WARN(
447                                       "unsupported flow dist option 0x%" PRIx64,
448                                              dist_field);
449                                 return -EINVAL;
450                         }
451                 }
452                 req_dist_set = req_dist_set >> 1;
453                 loop++;
454         }
455         kg_cfg->num_extracts = i;
456         return 0;
457 }
458
459 int
460 dpaa2_attach_bp_list(struct dpaa2_dev_priv *priv,
461                      void *blist)
462 {
463         /* Function to attach a DPNI with a buffer pool list. Buffer pool list
464          * handle is passed in blist.
465          */
466         int32_t retcode;
467         struct fsl_mc_io *dpni = priv->hw;
468         struct dpni_pools_cfg bpool_cfg;
469         struct dpaa2_bp_list *bp_list = (struct dpaa2_bp_list *)blist;
470         struct dpni_buffer_layout layout;
471         int tot_size;
472
473         /* ... rx buffer layout .
474          * Check alignment for buffer layouts first
475          */
476
477         /* ... rx buffer layout ... */
478         tot_size = RTE_PKTMBUF_HEADROOM;
479         tot_size = RTE_ALIGN_CEIL(tot_size, DPAA2_PACKET_LAYOUT_ALIGN);
480
481         memset(&layout, 0, sizeof(struct dpni_buffer_layout));
482         layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
483                          DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
484                          DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
485                          DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
486                          DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
487                          DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE;
488
489         layout.pass_timestamp = true;
490         layout.pass_frame_status = 1;
491         layout.private_data_size = DPAA2_FD_PTA_SIZE;
492         layout.pass_parser_result = 1;
493         layout.data_align = DPAA2_PACKET_LAYOUT_ALIGN;
494         layout.data_head_room = tot_size - DPAA2_FD_PTA_SIZE -
495                                 DPAA2_MBUF_HW_ANNOTATION;
496         retcode = dpni_set_buffer_layout(dpni, CMD_PRI_LOW, priv->token,
497                                          DPNI_QUEUE_RX, &layout);
498         if (retcode) {
499                 DPAA2_PMD_ERR("Error configuring buffer pool Rx layout (%d)",
500                              retcode);
501                 return retcode;
502         }
503
504         /*Attach buffer pool to the network interface as described by the user*/
505         memset(&bpool_cfg, 0, sizeof(struct dpni_pools_cfg));
506         bpool_cfg.num_dpbp = 1;
507         bpool_cfg.pools[0].dpbp_id = bp_list->buf_pool.dpbp_node->dpbp_id;
508         bpool_cfg.pools[0].backup_pool = 0;
509         bpool_cfg.pools[0].buffer_size = RTE_ALIGN_CEIL(bp_list->buf_pool.size,
510                                                 DPAA2_PACKET_LAYOUT_ALIGN);
511         bpool_cfg.pools[0].priority_mask = 0;
512
513         retcode = dpni_set_pools(dpni, CMD_PRI_LOW, priv->token, &bpool_cfg);
514         if (retcode != 0) {
515                 DPAA2_PMD_ERR("Error configuring buffer pool on interface."
516                               " bpid = %d error code = %d",
517                               bpool_cfg.pools[0].dpbp_id, retcode);
518                 return retcode;
519         }
520
521         priv->bp_list = bp_list;
522         return 0;
523 }