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