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