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