35c3b32f65e0df7a49c465a3f56deb1cbe39d58a
[dpdk.git] / drivers / net / i40e / i40e_tm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  *   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 Intel Corporation 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 <rte_malloc.h>
35
36 #include "base/i40e_prototype.h"
37 #include "i40e_ethdev.h"
38
39 static int i40e_tm_capabilities_get(struct rte_eth_dev *dev,
40                                     struct rte_tm_capabilities *cap,
41                                     struct rte_tm_error *error);
42 static int i40e_shaper_profile_add(struct rte_eth_dev *dev,
43                                    uint32_t shaper_profile_id,
44                                    struct rte_tm_shaper_params *profile,
45                                    struct rte_tm_error *error);
46 static int i40e_shaper_profile_del(struct rte_eth_dev *dev,
47                                    uint32_t shaper_profile_id,
48                                    struct rte_tm_error *error);
49 static int i40e_node_add(struct rte_eth_dev *dev, uint32_t node_id,
50                          uint32_t parent_node_id, uint32_t priority,
51                          uint32_t weight, uint32_t level_id,
52                          struct rte_tm_node_params *params,
53                          struct rte_tm_error *error);
54 static int i40e_node_delete(struct rte_eth_dev *dev, uint32_t node_id,
55                             struct rte_tm_error *error);
56 static int i40e_node_type_get(struct rte_eth_dev *dev, uint32_t node_id,
57                               int *is_leaf, struct rte_tm_error *error);
58 static int i40e_level_capabilities_get(struct rte_eth_dev *dev,
59                                        uint32_t level_id,
60                                        struct rte_tm_level_capabilities *cap,
61                                        struct rte_tm_error *error);
62 static int i40e_node_capabilities_get(struct rte_eth_dev *dev,
63                                       uint32_t node_id,
64                                       struct rte_tm_node_capabilities *cap,
65                                       struct rte_tm_error *error);
66
67 const struct rte_tm_ops i40e_tm_ops = {
68         .capabilities_get = i40e_tm_capabilities_get,
69         .shaper_profile_add = i40e_shaper_profile_add,
70         .shaper_profile_delete = i40e_shaper_profile_del,
71         .node_add = i40e_node_add,
72         .node_delete = i40e_node_delete,
73         .node_type_get = i40e_node_type_get,
74         .level_capabilities_get = i40e_level_capabilities_get,
75         .node_capabilities_get = i40e_node_capabilities_get,
76 };
77
78 int
79 i40e_tm_ops_get(struct rte_eth_dev *dev __rte_unused,
80                 void *arg)
81 {
82         if (!arg)
83                 return -EINVAL;
84
85         *(const void **)arg = &i40e_tm_ops;
86
87         return 0;
88 }
89
90 void
91 i40e_tm_conf_init(struct rte_eth_dev *dev)
92 {
93         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
94
95         /* initialize shaper profile list */
96         TAILQ_INIT(&pf->tm_conf.shaper_profile_list);
97
98         /* initialize node configuration */
99         pf->tm_conf.root = NULL;
100         TAILQ_INIT(&pf->tm_conf.tc_list);
101         TAILQ_INIT(&pf->tm_conf.queue_list);
102         pf->tm_conf.nb_tc_node = 0;
103         pf->tm_conf.nb_queue_node = 0;
104         pf->tm_conf.committed = false;
105 }
106
107 void
108 i40e_tm_conf_uninit(struct rte_eth_dev *dev)
109 {
110         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
111         struct i40e_tm_shaper_profile *shaper_profile;
112         struct i40e_tm_node *tm_node;
113
114         /* clear node configuration */
115         while ((tm_node = TAILQ_FIRST(&pf->tm_conf.queue_list))) {
116                 TAILQ_REMOVE(&pf->tm_conf.queue_list, tm_node, node);
117                 rte_free(tm_node);
118         }
119         pf->tm_conf.nb_queue_node = 0;
120         while ((tm_node = TAILQ_FIRST(&pf->tm_conf.tc_list))) {
121                 TAILQ_REMOVE(&pf->tm_conf.tc_list, tm_node, node);
122                 rte_free(tm_node);
123         }
124         pf->tm_conf.nb_tc_node = 0;
125         if (pf->tm_conf.root) {
126                 rte_free(pf->tm_conf.root);
127                 pf->tm_conf.root = NULL;
128         }
129
130         /* Remove all shaper profiles */
131         while ((shaper_profile =
132                TAILQ_FIRST(&pf->tm_conf.shaper_profile_list))) {
133                 TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list,
134                              shaper_profile, node);
135                 rte_free(shaper_profile);
136         }
137 }
138
139 static inline uint16_t
140 i40e_tc_nb_get(struct rte_eth_dev *dev)
141 {
142         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
143         struct i40e_vsi *main_vsi = pf->main_vsi;
144         uint16_t sum = 0;
145         int i;
146
147         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
148                 if (main_vsi->enabled_tc & BIT_ULL(i))
149                         sum++;
150         }
151
152         return sum;
153 }
154
155 static int
156 i40e_tm_capabilities_get(struct rte_eth_dev *dev,
157                          struct rte_tm_capabilities *cap,
158                          struct rte_tm_error *error)
159 {
160         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
161         uint16_t tc_nb = i40e_tc_nb_get(dev);
162
163         if (!cap || !error)
164                 return -EINVAL;
165
166         if (tc_nb > hw->func_caps.num_tx_qp)
167                 return -EINVAL;
168
169         error->type = RTE_TM_ERROR_TYPE_NONE;
170
171         /* set all the parameters to 0 first. */
172         memset(cap, 0, sizeof(struct rte_tm_capabilities));
173
174         /**
175          * support port + TCs + queues
176          * here shows the max capability not the current configuration.
177          */
178         cap->n_nodes_max = 1 + I40E_MAX_TRAFFIC_CLASS + hw->func_caps.num_tx_qp;
179         cap->n_levels_max = 3; /* port, TC, queue */
180         cap->non_leaf_nodes_identical = 1;
181         cap->leaf_nodes_identical = 1;
182         cap->shaper_n_max = cap->n_nodes_max;
183         cap->shaper_private_n_max = cap->n_nodes_max;
184         cap->shaper_private_dual_rate_n_max = 0;
185         cap->shaper_private_rate_min = 0;
186         /* 40Gbps -> 5GBps */
187         cap->shaper_private_rate_max = 5000000000ull;
188         cap->shaper_shared_n_max = 0;
189         cap->shaper_shared_n_nodes_per_shaper_max = 0;
190         cap->shaper_shared_n_shapers_per_node_max = 0;
191         cap->shaper_shared_dual_rate_n_max = 0;
192         cap->shaper_shared_rate_min = 0;
193         cap->shaper_shared_rate_max = 0;
194         cap->sched_n_children_max = hw->func_caps.num_tx_qp;
195         /**
196          * HW supports SP. But no plan to support it now.
197          * So, all the nodes should have the same priority.
198          */
199         cap->sched_sp_n_priorities_max = 1;
200         cap->sched_wfq_n_children_per_group_max = 0;
201         cap->sched_wfq_n_groups_max = 0;
202         /**
203          * SW only supports fair round robin now.
204          * So, all the nodes should have the same weight.
205          */
206         cap->sched_wfq_weight_max = 1;
207         cap->cman_head_drop_supported = 0;
208         cap->dynamic_update_mask = 0;
209         cap->shaper_pkt_length_adjust_min = RTE_TM_ETH_FRAMING_OVERHEAD;
210         cap->shaper_pkt_length_adjust_max = RTE_TM_ETH_FRAMING_OVERHEAD_FCS;
211         cap->cman_wred_context_n_max = 0;
212         cap->cman_wred_context_private_n_max = 0;
213         cap->cman_wred_context_shared_n_max = 0;
214         cap->cman_wred_context_shared_n_nodes_per_context_max = 0;
215         cap->cman_wred_context_shared_n_contexts_per_node_max = 0;
216         cap->stats_mask = 0;
217
218         return 0;
219 }
220
221 static inline struct i40e_tm_shaper_profile *
222 i40e_shaper_profile_search(struct rte_eth_dev *dev,
223                            uint32_t shaper_profile_id)
224 {
225         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
226         struct i40e_shaper_profile_list *shaper_profile_list =
227                 &pf->tm_conf.shaper_profile_list;
228         struct i40e_tm_shaper_profile *shaper_profile;
229
230         TAILQ_FOREACH(shaper_profile, shaper_profile_list, node) {
231                 if (shaper_profile_id == shaper_profile->shaper_profile_id)
232                         return shaper_profile;
233         }
234
235         return NULL;
236 }
237
238 static int
239 i40e_shaper_profile_param_check(struct rte_tm_shaper_params *profile,
240                                 struct rte_tm_error *error)
241 {
242         /* min rate not supported */
243         if (profile->committed.rate) {
244                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_RATE;
245                 error->message = "committed rate not supported";
246                 return -EINVAL;
247         }
248         /* min bucket size not supported */
249         if (profile->committed.size) {
250                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_SIZE;
251                 error->message = "committed bucket size not supported";
252                 return -EINVAL;
253         }
254         /* max bucket size not supported */
255         if (profile->peak.size) {
256                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PEAK_SIZE;
257                 error->message = "peak bucket size not supported";
258                 return -EINVAL;
259         }
260         /* length adjustment not supported */
261         if (profile->pkt_length_adjust) {
262                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PKT_ADJUST_LEN;
263                 error->message = "packet length adjustment not supported";
264                 return -EINVAL;
265         }
266
267         return 0;
268 }
269
270 static int
271 i40e_shaper_profile_add(struct rte_eth_dev *dev,
272                         uint32_t shaper_profile_id,
273                         struct rte_tm_shaper_params *profile,
274                         struct rte_tm_error *error)
275 {
276         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
277         struct i40e_tm_shaper_profile *shaper_profile;
278         int ret;
279
280         if (!profile || !error)
281                 return -EINVAL;
282
283         ret = i40e_shaper_profile_param_check(profile, error);
284         if (ret)
285                 return ret;
286
287         shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
288
289         if (shaper_profile) {
290                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
291                 error->message = "profile ID exist";
292                 return -EINVAL;
293         }
294
295         shaper_profile = rte_zmalloc("i40e_tm_shaper_profile",
296                                      sizeof(struct i40e_tm_shaper_profile),
297                                      0);
298         if (!shaper_profile)
299                 return -ENOMEM;
300         shaper_profile->shaper_profile_id = shaper_profile_id;
301         (void)rte_memcpy(&shaper_profile->profile, profile,
302                          sizeof(struct rte_tm_shaper_params));
303         TAILQ_INSERT_TAIL(&pf->tm_conf.shaper_profile_list,
304                           shaper_profile, node);
305
306         return 0;
307 }
308
309 static int
310 i40e_shaper_profile_del(struct rte_eth_dev *dev,
311                         uint32_t shaper_profile_id,
312                         struct rte_tm_error *error)
313 {
314         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
315         struct i40e_tm_shaper_profile *shaper_profile;
316
317         if (!error)
318                 return -EINVAL;
319
320         shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
321
322         if (!shaper_profile) {
323                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
324                 error->message = "profile ID not exist";
325                 return -EINVAL;
326         }
327
328         /* don't delete a profile if it's used by one or several nodes */
329         if (shaper_profile->reference_count) {
330                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
331                 error->message = "profile in use";
332                 return -EINVAL;
333         }
334
335         TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list, shaper_profile, node);
336         rte_free(shaper_profile);
337
338         return 0;
339 }
340
341 static inline struct i40e_tm_node *
342 i40e_tm_node_search(struct rte_eth_dev *dev,
343                     uint32_t node_id, enum i40e_tm_node_type *node_type)
344 {
345         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
346         struct i40e_tm_node_list *queue_list = &pf->tm_conf.queue_list;
347         struct i40e_tm_node_list *tc_list = &pf->tm_conf.tc_list;
348         struct i40e_tm_node *tm_node;
349
350         if (pf->tm_conf.root && pf->tm_conf.root->id == node_id) {
351                 *node_type = I40E_TM_NODE_TYPE_PORT;
352                 return pf->tm_conf.root;
353         }
354
355         TAILQ_FOREACH(tm_node, tc_list, node) {
356                 if (tm_node->id == node_id) {
357                         *node_type = I40E_TM_NODE_TYPE_TC;
358                         return tm_node;
359                 }
360         }
361
362         TAILQ_FOREACH(tm_node, queue_list, node) {
363                 if (tm_node->id == node_id) {
364                         *node_type = I40E_TM_NODE_TYPE_QUEUE;
365                         return tm_node;
366                 }
367         }
368
369         return NULL;
370 }
371
372 static int
373 i40e_node_param_check(uint32_t node_id, uint32_t parent_node_id,
374                       uint32_t priority, uint32_t weight,
375                       struct rte_tm_node_params *params,
376                       struct rte_tm_error *error)
377 {
378         if (node_id == RTE_TM_NODE_ID_NULL) {
379                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
380                 error->message = "invalid node id";
381                 return -EINVAL;
382         }
383
384         if (priority) {
385                 error->type = RTE_TM_ERROR_TYPE_NODE_PRIORITY;
386                 error->message = "priority should be 0";
387                 return -EINVAL;
388         }
389
390         if (weight != 1) {
391                 error->type = RTE_TM_ERROR_TYPE_NODE_WEIGHT;
392                 error->message = "weight must be 1";
393                 return -EINVAL;
394         }
395
396         /* not support shared shaper */
397         if (params->shared_shaper_id) {
398                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_SHAPER_ID;
399                 error->message = "shared shaper not supported";
400                 return -EINVAL;
401         }
402         if (params->n_shared_shapers) {
403                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_SHAPERS;
404                 error->message = "shared shaper not supported";
405                 return -EINVAL;
406         }
407
408         /* for root node */
409         if (parent_node_id == RTE_TM_NODE_ID_NULL) {
410                 if (params->nonleaf.wfq_weight_mode) {
411                         error->type =
412                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
413                         error->message = "WFQ not supported";
414                         return -EINVAL;
415                 }
416                 if (params->nonleaf.n_sp_priorities != 1) {
417                         error->type =
418                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SP_PRIORITIES;
419                         error->message = "SP priority not supported";
420                         return -EINVAL;
421                 } else if (params->nonleaf.wfq_weight_mode &&
422                            !(*params->nonleaf.wfq_weight_mode)) {
423                         error->type =
424                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
425                         error->message = "WFP should be byte mode";
426                         return -EINVAL;
427                 }
428
429                 return 0;
430         }
431
432         /* for TC or queue node */
433         if (params->leaf.cman) {
434                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_CMAN;
435                 error->message = "Congestion management not supported";
436                 return -EINVAL;
437         }
438         if (params->leaf.wred.wred_profile_id !=
439             RTE_TM_WRED_PROFILE_ID_NONE) {
440                 error->type =
441                         RTE_TM_ERROR_TYPE_NODE_PARAMS_WRED_PROFILE_ID;
442                 error->message = "WRED not supported";
443                 return -EINVAL;
444         }
445         if (params->leaf.wred.shared_wred_context_id) {
446                 error->type =
447                         RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_WRED_CONTEXT_ID;
448                 error->message = "WRED not supported";
449                 return -EINVAL;
450         }
451         if (params->leaf.wred.n_shared_wred_contexts) {
452                 error->type =
453                         RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_WRED_CONTEXTS;
454                 error->message = "WRED not supported";
455                 return -EINVAL;
456         }
457
458         return 0;
459 }
460
461 /**
462  * Now the TC and queue configuration is controlled by DCB.
463  * We need check if the node configuration follows the DCB configuration.
464  * In the future, we may use TM to cover DCB.
465  */
466 static int
467 i40e_node_add(struct rte_eth_dev *dev, uint32_t node_id,
468               uint32_t parent_node_id, uint32_t priority,
469               uint32_t weight, uint32_t level_id,
470               struct rte_tm_node_params *params,
471               struct rte_tm_error *error)
472 {
473         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
474         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
475         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
476         enum i40e_tm_node_type parent_node_type = I40E_TM_NODE_TYPE_MAX;
477         struct i40e_tm_shaper_profile *shaper_profile;
478         struct i40e_tm_node *tm_node;
479         struct i40e_tm_node *parent_node;
480         uint16_t tc_nb = 0;
481         int ret;
482
483         if (!params || !error)
484                 return -EINVAL;
485
486         /* if already committed */
487         if (pf->tm_conf.committed) {
488                 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
489                 error->message = "already committed";
490                 return -EINVAL;
491         }
492
493         ret = i40e_node_param_check(node_id, parent_node_id, priority, weight,
494                                     params, error);
495         if (ret)
496                 return ret;
497
498         /* check if the node ID is already used */
499         if (i40e_tm_node_search(dev, node_id, &node_type)) {
500                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
501                 error->message = "node id already used";
502                 return -EINVAL;
503         }
504
505         /* check the shaper profile id */
506         shaper_profile = i40e_shaper_profile_search(dev,
507                                                     params->shaper_profile_id);
508         if (!shaper_profile) {
509                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHAPER_PROFILE_ID;
510                 error->message = "shaper profile not exist";
511                 return -EINVAL;
512         }
513
514         /* root node if not have a parent */
515         if (parent_node_id == RTE_TM_NODE_ID_NULL) {
516                 /* check level */
517                 if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
518                     level_id > I40E_TM_NODE_TYPE_PORT) {
519                         error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
520                         error->message = "Wrong level";
521                         return -EINVAL;
522                 }
523
524                 /* obviously no more than one root */
525                 if (pf->tm_conf.root) {
526                         error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
527                         error->message = "already have a root";
528                         return -EINVAL;
529                 }
530
531                 /* add the root node */
532                 tm_node = rte_zmalloc("i40e_tm_node",
533                                       sizeof(struct i40e_tm_node),
534                                       0);
535                 if (!tm_node)
536                         return -ENOMEM;
537                 tm_node->id = node_id;
538                 tm_node->priority = priority;
539                 tm_node->weight = weight;
540                 tm_node->reference_count = 0;
541                 tm_node->parent = NULL;
542                 tm_node->shaper_profile = shaper_profile;
543                 (void)rte_memcpy(&tm_node->params, params,
544                                  sizeof(struct rte_tm_node_params));
545                 pf->tm_conf.root = tm_node;
546
547                 /* increase the reference counter of the shaper profile */
548                 shaper_profile->reference_count++;
549
550                 return 0;
551         }
552
553         /* TC or queue node */
554         /* check the parent node */
555         parent_node = i40e_tm_node_search(dev, parent_node_id,
556                                           &parent_node_type);
557         if (!parent_node) {
558                 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
559                 error->message = "parent not exist";
560                 return -EINVAL;
561         }
562         if (parent_node_type != I40E_TM_NODE_TYPE_PORT &&
563             parent_node_type != I40E_TM_NODE_TYPE_TC) {
564                 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
565                 error->message = "parent is not port or TC";
566                 return -EINVAL;
567         }
568         /* check level */
569         if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
570             level_id != parent_node_type + 1) {
571                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
572                 error->message = "Wrong level";
573                 return -EINVAL;
574         }
575
576         /* check the node number */
577         if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
578                 /* check the TC number */
579                 tc_nb = i40e_tc_nb_get(dev);
580                 if (pf->tm_conf.nb_tc_node >= tc_nb) {
581                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
582                         error->message = "too many TCs";
583                         return -EINVAL;
584                 }
585         } else {
586                 /* check the queue number */
587                 if (pf->tm_conf.nb_queue_node >= hw->func_caps.num_tx_qp) {
588                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
589                         error->message = "too many queues";
590                         return -EINVAL;
591                 }
592
593                 /**
594                  * check the node id.
595                  * For queue, the node id means queue id.
596                  */
597                 if (node_id >= hw->func_caps.num_tx_qp) {
598                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
599                         error->message = "too large queue id";
600                         return -EINVAL;
601                 }
602         }
603
604         /* add the TC or queue node */
605         tm_node = rte_zmalloc("i40e_tm_node",
606                               sizeof(struct i40e_tm_node),
607                               0);
608         if (!tm_node)
609                 return -ENOMEM;
610         tm_node->id = node_id;
611         tm_node->priority = priority;
612         tm_node->weight = weight;
613         tm_node->reference_count = 0;
614         tm_node->parent = pf->tm_conf.root;
615         tm_node->shaper_profile = shaper_profile;
616         (void)rte_memcpy(&tm_node->params, params,
617                          sizeof(struct rte_tm_node_params));
618         if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
619                 TAILQ_INSERT_TAIL(&pf->tm_conf.tc_list,
620                                   tm_node, node);
621                 pf->tm_conf.nb_tc_node++;
622         } else {
623                 TAILQ_INSERT_TAIL(&pf->tm_conf.queue_list,
624                                   tm_node, node);
625                 pf->tm_conf.nb_queue_node++;
626         }
627         tm_node->parent->reference_count++;
628
629         /* increase the reference counter of the shaper profile */
630         shaper_profile->reference_count++;
631
632         return 0;
633 }
634
635 static int
636 i40e_node_delete(struct rte_eth_dev *dev, uint32_t node_id,
637                  struct rte_tm_error *error)
638 {
639         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
640         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
641         struct i40e_tm_node *tm_node;
642
643         if (!error)
644                 return -EINVAL;
645
646         /* if already committed */
647         if (pf->tm_conf.committed) {
648                 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
649                 error->message = "already committed";
650                 return -EINVAL;
651         }
652
653         if (node_id == RTE_TM_NODE_ID_NULL) {
654                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
655                 error->message = "invalid node id";
656                 return -EINVAL;
657         }
658
659         /* check if the node id exists */
660         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
661         if (!tm_node) {
662                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
663                 error->message = "no such node";
664                 return -EINVAL;
665         }
666
667         /* the node should have no child */
668         if (tm_node->reference_count) {
669                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
670                 error->message =
671                         "cannot delete a node which has children";
672                 return -EINVAL;
673         }
674
675         /* root node */
676         if (node_type == I40E_TM_NODE_TYPE_PORT) {
677                 tm_node->shaper_profile->reference_count--;
678                 rte_free(tm_node);
679                 pf->tm_conf.root = NULL;
680                 return 0;
681         }
682
683         /* TC or queue node */
684         tm_node->shaper_profile->reference_count--;
685         tm_node->parent->reference_count--;
686         if (node_type == I40E_TM_NODE_TYPE_TC) {
687                 TAILQ_REMOVE(&pf->tm_conf.tc_list, tm_node, node);
688                 pf->tm_conf.nb_tc_node--;
689         } else {
690                 TAILQ_REMOVE(&pf->tm_conf.queue_list, tm_node, node);
691                 pf->tm_conf.nb_queue_node--;
692         }
693         rte_free(tm_node);
694
695         return 0;
696 }
697
698 static int
699 i40e_node_type_get(struct rte_eth_dev *dev, uint32_t node_id,
700                    int *is_leaf, struct rte_tm_error *error)
701 {
702         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
703         struct i40e_tm_node *tm_node;
704
705         if (!is_leaf || !error)
706                 return -EINVAL;
707
708         if (node_id == RTE_TM_NODE_ID_NULL) {
709                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
710                 error->message = "invalid node id";
711                 return -EINVAL;
712         }
713
714         /* check if the node id exists */
715         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
716         if (!tm_node) {
717                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
718                 error->message = "no such node";
719                 return -EINVAL;
720         }
721
722         if (node_type == I40E_TM_NODE_TYPE_QUEUE)
723                 *is_leaf = true;
724         else
725                 *is_leaf = false;
726
727         return 0;
728 }
729
730 static int
731 i40e_level_capabilities_get(struct rte_eth_dev *dev,
732                             uint32_t level_id,
733                             struct rte_tm_level_capabilities *cap,
734                             struct rte_tm_error *error)
735 {
736         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
737
738         if (!cap || !error)
739                 return -EINVAL;
740
741         if (level_id >= I40E_TM_NODE_TYPE_MAX) {
742                 error->type = RTE_TM_ERROR_TYPE_LEVEL_ID;
743                 error->message = "too deep level";
744                 return -EINVAL;
745         }
746
747         /* root node */
748         if (level_id == I40E_TM_NODE_TYPE_PORT) {
749                 cap->n_nodes_max = 1;
750                 cap->n_nodes_nonleaf_max = 1;
751                 cap->n_nodes_leaf_max = 0;
752                 cap->non_leaf_nodes_identical = true;
753                 cap->leaf_nodes_identical = true;
754                 cap->nonleaf.shaper_private_supported = true;
755                 cap->nonleaf.shaper_private_dual_rate_supported = false;
756                 cap->nonleaf.shaper_private_rate_min = 0;
757                 /* 40Gbps -> 5GBps */
758                 cap->nonleaf.shaper_private_rate_max = 5000000000ull;
759                 cap->nonleaf.shaper_shared_n_max = 0;
760                 cap->nonleaf.sched_n_children_max = I40E_MAX_TRAFFIC_CLASS;
761                 cap->nonleaf.sched_sp_n_priorities_max = 1;
762                 cap->nonleaf.sched_wfq_n_children_per_group_max = 0;
763                 cap->nonleaf.sched_wfq_n_groups_max = 0;
764                 cap->nonleaf.sched_wfq_weight_max = 1;
765                 cap->nonleaf.stats_mask = 0;
766
767                 return 0;
768         }
769
770         /* TC or queue node */
771         if (level_id == I40E_TM_NODE_TYPE_TC) {
772                 /* TC */
773                 cap->n_nodes_max = I40E_MAX_TRAFFIC_CLASS;
774                 cap->n_nodes_nonleaf_max = I40E_MAX_TRAFFIC_CLASS;
775                 cap->n_nodes_leaf_max = 0;
776                 cap->non_leaf_nodes_identical = true;
777         } else {
778                 /* queue */
779                 cap->n_nodes_max = hw->func_caps.num_tx_qp;
780                 cap->n_nodes_nonleaf_max = 0;
781                 cap->n_nodes_leaf_max = hw->func_caps.num_tx_qp;
782                 cap->non_leaf_nodes_identical = true;
783         }
784         cap->leaf_nodes_identical = true;
785         cap->leaf.shaper_private_supported = true;
786         cap->leaf.shaper_private_dual_rate_supported = false;
787         cap->leaf.shaper_private_rate_min = 0;
788         /* 40Gbps -> 5GBps */
789         cap->leaf.shaper_private_rate_max = 5000000000ull;
790         cap->leaf.shaper_shared_n_max = 0;
791         cap->leaf.cman_head_drop_supported = false;
792         cap->leaf.cman_wred_context_private_supported = true;
793         cap->leaf.cman_wred_context_shared_n_max = 0;
794         cap->leaf.stats_mask = 0;
795
796         return 0;
797 }
798
799 static int
800 i40e_node_capabilities_get(struct rte_eth_dev *dev,
801                            uint32_t node_id,
802                            struct rte_tm_node_capabilities *cap,
803                            struct rte_tm_error *error)
804 {
805         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
806         enum i40e_tm_node_type node_type;
807         struct i40e_tm_node *tm_node;
808
809         if (!cap || !error)
810                 return -EINVAL;
811
812         if (node_id == RTE_TM_NODE_ID_NULL) {
813                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
814                 error->message = "invalid node id";
815                 return -EINVAL;
816         }
817
818         /* check if the node id exists */
819         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
820         if (!tm_node) {
821                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
822                 error->message = "no such node";
823                 return -EINVAL;
824         }
825
826         cap->shaper_private_supported = true;
827         cap->shaper_private_dual_rate_supported = false;
828         cap->shaper_private_rate_min = 0;
829         /* 40Gbps -> 5GBps */
830         cap->shaper_private_rate_max = 5000000000ull;
831         cap->shaper_shared_n_max = 0;
832
833         if (node_type == I40E_TM_NODE_TYPE_QUEUE) {
834                 cap->leaf.cman_head_drop_supported = false;
835                 cap->leaf.cman_wred_context_private_supported = true;
836                 cap->leaf.cman_wred_context_shared_n_max = 0;
837         } else {
838                 if (node_type == I40E_TM_NODE_TYPE_PORT)
839                         cap->nonleaf.sched_n_children_max =
840                                 I40E_MAX_TRAFFIC_CLASS;
841                 else
842                         cap->nonleaf.sched_n_children_max =
843                                 hw->func_caps.num_tx_qp;
844                 cap->nonleaf.sched_sp_n_priorities_max = 1;
845                 cap->nonleaf.sched_wfq_n_children_per_group_max = 0;
846                 cap->nonleaf.sched_wfq_n_groups_max = 0;
847                 cap->nonleaf.sched_wfq_weight_max = 1;
848         }
849
850         cap->stats_mask = 0;
851
852         return 0;
853 }