d90313af3fb69ce9e0a20818ed913b2ff050e026
[deb_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         (void)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(uint32_t node_id, uint32_t parent_node_id,
378                       uint32_t priority, uint32_t weight,
379                       struct rte_tm_node_params *params,
380                       struct rte_tm_error *error)
381 {
382         if (node_id == RTE_TM_NODE_ID_NULL) {
383                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
384                 error->message = "invalid node id";
385                 return -EINVAL;
386         }
387
388         if (priority) {
389                 error->type = RTE_TM_ERROR_TYPE_NODE_PRIORITY;
390                 error->message = "priority should be 0";
391                 return -EINVAL;
392         }
393
394         if (weight != 1) {
395                 error->type = RTE_TM_ERROR_TYPE_NODE_WEIGHT;
396                 error->message = "weight must be 1";
397                 return -EINVAL;
398         }
399
400         /* not support shared shaper */
401         if (params->shared_shaper_id) {
402                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_SHAPER_ID;
403                 error->message = "shared shaper not supported";
404                 return -EINVAL;
405         }
406         if (params->n_shared_shapers) {
407                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_SHAPERS;
408                 error->message = "shared shaper not supported";
409                 return -EINVAL;
410         }
411
412         /* for root node */
413         if (parent_node_id == RTE_TM_NODE_ID_NULL) {
414                 if (params->nonleaf.wfq_weight_mode) {
415                         error->type =
416                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
417                         error->message = "WFQ not supported";
418                         return -EINVAL;
419                 }
420                 if (params->nonleaf.n_sp_priorities != 1) {
421                         error->type =
422                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SP_PRIORITIES;
423                         error->message = "SP priority not supported";
424                         return -EINVAL;
425                 } else if (params->nonleaf.wfq_weight_mode &&
426                            !(*params->nonleaf.wfq_weight_mode)) {
427                         error->type =
428                                 RTE_TM_ERROR_TYPE_NODE_PARAMS_WFQ_WEIGHT_MODE;
429                         error->message = "WFP should be byte mode";
430                         return -EINVAL;
431                 }
432
433                 return 0;
434         }
435
436         /* for TC or queue node */
437         if (params->leaf.cman) {
438                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_CMAN;
439                 error->message = "Congestion management not supported";
440                 return -EINVAL;
441         }
442         if (params->leaf.wred.wred_profile_id !=
443             RTE_TM_WRED_PROFILE_ID_NONE) {
444                 error->type =
445                         RTE_TM_ERROR_TYPE_NODE_PARAMS_WRED_PROFILE_ID;
446                 error->message = "WRED not supported";
447                 return -EINVAL;
448         }
449         if (params->leaf.wred.shared_wred_context_id) {
450                 error->type =
451                         RTE_TM_ERROR_TYPE_NODE_PARAMS_SHARED_WRED_CONTEXT_ID;
452                 error->message = "WRED not supported";
453                 return -EINVAL;
454         }
455         if (params->leaf.wred.n_shared_wred_contexts) {
456                 error->type =
457                         RTE_TM_ERROR_TYPE_NODE_PARAMS_N_SHARED_WRED_CONTEXTS;
458                 error->message = "WRED not supported";
459                 return -EINVAL;
460         }
461
462         return 0;
463 }
464
465 /**
466  * Now the TC and queue configuration is controlled by DCB.
467  * We need check if the node configuration follows the DCB configuration.
468  * In the future, we may use TM to cover DCB.
469  */
470 static int
471 i40e_node_add(struct rte_eth_dev *dev, uint32_t node_id,
472               uint32_t parent_node_id, uint32_t priority,
473               uint32_t weight, uint32_t level_id,
474               struct rte_tm_node_params *params,
475               struct rte_tm_error *error)
476 {
477         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
478         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
479         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
480         enum i40e_tm_node_type parent_node_type = I40E_TM_NODE_TYPE_MAX;
481         struct i40e_tm_shaper_profile *shaper_profile;
482         struct i40e_tm_node *tm_node;
483         struct i40e_tm_node *parent_node;
484         uint16_t tc_nb = 0;
485         int ret;
486
487         if (!params || !error)
488                 return -EINVAL;
489
490         /* if already committed */
491         if (pf->tm_conf.committed) {
492                 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
493                 error->message = "already committed";
494                 return -EINVAL;
495         }
496
497         ret = i40e_node_param_check(node_id, parent_node_id, priority, weight,
498                                     params, error);
499         if (ret)
500                 return ret;
501
502         /* check if the node ID is already used */
503         if (i40e_tm_node_search(dev, node_id, &node_type)) {
504                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
505                 error->message = "node id already used";
506                 return -EINVAL;
507         }
508
509         /* check the shaper profile id */
510         shaper_profile = i40e_shaper_profile_search(dev,
511                                                     params->shaper_profile_id);
512         if (!shaper_profile) {
513                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS_SHAPER_PROFILE_ID;
514                 error->message = "shaper profile not exist";
515                 return -EINVAL;
516         }
517
518         /* root node if not have a parent */
519         if (parent_node_id == RTE_TM_NODE_ID_NULL) {
520                 /* check level */
521                 if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
522                     level_id > I40E_TM_NODE_TYPE_PORT) {
523                         error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
524                         error->message = "Wrong level";
525                         return -EINVAL;
526                 }
527
528                 /* obviously no more than one root */
529                 if (pf->tm_conf.root) {
530                         error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
531                         error->message = "already have a root";
532                         return -EINVAL;
533                 }
534
535                 /* add the root node */
536                 tm_node = rte_zmalloc("i40e_tm_node",
537                                       sizeof(struct i40e_tm_node),
538                                       0);
539                 if (!tm_node)
540                         return -ENOMEM;
541                 tm_node->id = node_id;
542                 tm_node->priority = priority;
543                 tm_node->weight = weight;
544                 tm_node->reference_count = 0;
545                 tm_node->parent = NULL;
546                 tm_node->shaper_profile = shaper_profile;
547                 (void)rte_memcpy(&tm_node->params, params,
548                                  sizeof(struct rte_tm_node_params));
549                 pf->tm_conf.root = tm_node;
550
551                 /* increase the reference counter of the shaper profile */
552                 shaper_profile->reference_count++;
553
554                 return 0;
555         }
556
557         /* TC or queue node */
558         /* check the parent node */
559         parent_node = i40e_tm_node_search(dev, parent_node_id,
560                                           &parent_node_type);
561         if (!parent_node) {
562                 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
563                 error->message = "parent not exist";
564                 return -EINVAL;
565         }
566         if (parent_node_type != I40E_TM_NODE_TYPE_PORT &&
567             parent_node_type != I40E_TM_NODE_TYPE_TC) {
568                 error->type = RTE_TM_ERROR_TYPE_NODE_PARENT_NODE_ID;
569                 error->message = "parent is not port or TC";
570                 return -EINVAL;
571         }
572         /* check level */
573         if (level_id != RTE_TM_NODE_LEVEL_ID_ANY &&
574             level_id != parent_node_type + 1) {
575                 error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
576                 error->message = "Wrong level";
577                 return -EINVAL;
578         }
579
580         /* check the node number */
581         if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
582                 /* check the TC number */
583                 tc_nb = i40e_tc_nb_get(dev);
584                 if (pf->tm_conf.nb_tc_node >= tc_nb) {
585                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
586                         error->message = "too many TCs";
587                         return -EINVAL;
588                 }
589         } else {
590                 /* check the queue number */
591                 if (pf->tm_conf.nb_queue_node >= hw->func_caps.num_tx_qp) {
592                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
593                         error->message = "too many queues";
594                         return -EINVAL;
595                 }
596
597                 /**
598                  * check the node id.
599                  * For queue, the node id means queue id.
600                  */
601                 if (node_id >= hw->func_caps.num_tx_qp) {
602                         error->type = RTE_TM_ERROR_TYPE_NODE_ID;
603                         error->message = "too large queue id";
604                         return -EINVAL;
605                 }
606         }
607
608         /* add the TC or queue node */
609         tm_node = rte_zmalloc("i40e_tm_node",
610                               sizeof(struct i40e_tm_node),
611                               0);
612         if (!tm_node)
613                 return -ENOMEM;
614         tm_node->id = node_id;
615         tm_node->priority = priority;
616         tm_node->weight = weight;
617         tm_node->reference_count = 0;
618         tm_node->parent = pf->tm_conf.root;
619         tm_node->shaper_profile = shaper_profile;
620         (void)rte_memcpy(&tm_node->params, params,
621                          sizeof(struct rte_tm_node_params));
622         if (parent_node_type == I40E_TM_NODE_TYPE_PORT) {
623                 TAILQ_INSERT_TAIL(&pf->tm_conf.tc_list,
624                                   tm_node, node);
625                 pf->tm_conf.nb_tc_node++;
626         } else {
627                 TAILQ_INSERT_TAIL(&pf->tm_conf.queue_list,
628                                   tm_node, node);
629                 pf->tm_conf.nb_queue_node++;
630         }
631         tm_node->parent->reference_count++;
632
633         /* increase the reference counter of the shaper profile */
634         shaper_profile->reference_count++;
635
636         return 0;
637 }
638
639 static int
640 i40e_node_delete(struct rte_eth_dev *dev, uint32_t node_id,
641                  struct rte_tm_error *error)
642 {
643         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
644         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
645         struct i40e_tm_node *tm_node;
646
647         if (!error)
648                 return -EINVAL;
649
650         /* if already committed */
651         if (pf->tm_conf.committed) {
652                 error->type = RTE_TM_ERROR_TYPE_UNSPECIFIED;
653                 error->message = "already committed";
654                 return -EINVAL;
655         }
656
657         if (node_id == RTE_TM_NODE_ID_NULL) {
658                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
659                 error->message = "invalid node id";
660                 return -EINVAL;
661         }
662
663         /* check if the node id exists */
664         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
665         if (!tm_node) {
666                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
667                 error->message = "no such node";
668                 return -EINVAL;
669         }
670
671         /* the node should have no child */
672         if (tm_node->reference_count) {
673                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
674                 error->message =
675                         "cannot delete a node which has children";
676                 return -EINVAL;
677         }
678
679         /* root node */
680         if (node_type == I40E_TM_NODE_TYPE_PORT) {
681                 tm_node->shaper_profile->reference_count--;
682                 rte_free(tm_node);
683                 pf->tm_conf.root = NULL;
684                 return 0;
685         }
686
687         /* TC or queue node */
688         tm_node->shaper_profile->reference_count--;
689         tm_node->parent->reference_count--;
690         if (node_type == I40E_TM_NODE_TYPE_TC) {
691                 TAILQ_REMOVE(&pf->tm_conf.tc_list, tm_node, node);
692                 pf->tm_conf.nb_tc_node--;
693         } else {
694                 TAILQ_REMOVE(&pf->tm_conf.queue_list, tm_node, node);
695                 pf->tm_conf.nb_queue_node--;
696         }
697         rte_free(tm_node);
698
699         return 0;
700 }
701
702 static int
703 i40e_node_type_get(struct rte_eth_dev *dev, uint32_t node_id,
704                    int *is_leaf, struct rte_tm_error *error)
705 {
706         enum i40e_tm_node_type node_type = I40E_TM_NODE_TYPE_MAX;
707         struct i40e_tm_node *tm_node;
708
709         if (!is_leaf || !error)
710                 return -EINVAL;
711
712         if (node_id == RTE_TM_NODE_ID_NULL) {
713                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
714                 error->message = "invalid node id";
715                 return -EINVAL;
716         }
717
718         /* check if the node id exists */
719         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
720         if (!tm_node) {
721                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
722                 error->message = "no such node";
723                 return -EINVAL;
724         }
725
726         if (node_type == I40E_TM_NODE_TYPE_QUEUE)
727                 *is_leaf = true;
728         else
729                 *is_leaf = false;
730
731         return 0;
732 }
733
734 static int
735 i40e_level_capabilities_get(struct rte_eth_dev *dev,
736                             uint32_t level_id,
737                             struct rte_tm_level_capabilities *cap,
738                             struct rte_tm_error *error)
739 {
740         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
741
742         if (!cap || !error)
743                 return -EINVAL;
744
745         if (level_id >= I40E_TM_NODE_TYPE_MAX) {
746                 error->type = RTE_TM_ERROR_TYPE_LEVEL_ID;
747                 error->message = "too deep level";
748                 return -EINVAL;
749         }
750
751         /* root node */
752         if (level_id == I40E_TM_NODE_TYPE_PORT) {
753                 cap->n_nodes_max = 1;
754                 cap->n_nodes_nonleaf_max = 1;
755                 cap->n_nodes_leaf_max = 0;
756                 cap->non_leaf_nodes_identical = true;
757                 cap->leaf_nodes_identical = true;
758                 cap->nonleaf.shaper_private_supported = true;
759                 cap->nonleaf.shaper_private_dual_rate_supported = false;
760                 cap->nonleaf.shaper_private_rate_min = 0;
761                 /* 40Gbps -> 5GBps */
762                 cap->nonleaf.shaper_private_rate_max = 5000000000ull;
763                 cap->nonleaf.shaper_shared_n_max = 0;
764                 cap->nonleaf.sched_n_children_max = I40E_MAX_TRAFFIC_CLASS;
765                 cap->nonleaf.sched_sp_n_priorities_max = 1;
766                 cap->nonleaf.sched_wfq_n_children_per_group_max = 0;
767                 cap->nonleaf.sched_wfq_n_groups_max = 0;
768                 cap->nonleaf.sched_wfq_weight_max = 1;
769                 cap->nonleaf.stats_mask = 0;
770
771                 return 0;
772         }
773
774         /* TC or queue node */
775         if (level_id == I40E_TM_NODE_TYPE_TC) {
776                 /* TC */
777                 cap->n_nodes_max = I40E_MAX_TRAFFIC_CLASS;
778                 cap->n_nodes_nonleaf_max = I40E_MAX_TRAFFIC_CLASS;
779                 cap->n_nodes_leaf_max = 0;
780                 cap->non_leaf_nodes_identical = true;
781         } else {
782                 /* queue */
783                 cap->n_nodes_max = hw->func_caps.num_tx_qp;
784                 cap->n_nodes_nonleaf_max = 0;
785                 cap->n_nodes_leaf_max = hw->func_caps.num_tx_qp;
786                 cap->non_leaf_nodes_identical = true;
787         }
788         cap->leaf_nodes_identical = true;
789         cap->leaf.shaper_private_supported = true;
790         cap->leaf.shaper_private_dual_rate_supported = false;
791         cap->leaf.shaper_private_rate_min = 0;
792         /* 40Gbps -> 5GBps */
793         cap->leaf.shaper_private_rate_max = 5000000000ull;
794         cap->leaf.shaper_shared_n_max = 0;
795         cap->leaf.cman_head_drop_supported = false;
796         cap->leaf.cman_wred_context_private_supported = true;
797         cap->leaf.cman_wred_context_shared_n_max = 0;
798         cap->leaf.stats_mask = 0;
799
800         return 0;
801 }
802
803 static int
804 i40e_node_capabilities_get(struct rte_eth_dev *dev,
805                            uint32_t node_id,
806                            struct rte_tm_node_capabilities *cap,
807                            struct rte_tm_error *error)
808 {
809         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
810         enum i40e_tm_node_type node_type;
811         struct i40e_tm_node *tm_node;
812
813         if (!cap || !error)
814                 return -EINVAL;
815
816         if (node_id == RTE_TM_NODE_ID_NULL) {
817                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
818                 error->message = "invalid node id";
819                 return -EINVAL;
820         }
821
822         /* check if the node id exists */
823         tm_node = i40e_tm_node_search(dev, node_id, &node_type);
824         if (!tm_node) {
825                 error->type = RTE_TM_ERROR_TYPE_NODE_ID;
826                 error->message = "no such node";
827                 return -EINVAL;
828         }
829
830         cap->shaper_private_supported = true;
831         cap->shaper_private_dual_rate_supported = false;
832         cap->shaper_private_rate_min = 0;
833         /* 40Gbps -> 5GBps */
834         cap->shaper_private_rate_max = 5000000000ull;
835         cap->shaper_shared_n_max = 0;
836
837         if (node_type == I40E_TM_NODE_TYPE_QUEUE) {
838                 cap->leaf.cman_head_drop_supported = false;
839                 cap->leaf.cman_wred_context_private_supported = true;
840                 cap->leaf.cman_wred_context_shared_n_max = 0;
841         } else {
842                 if (node_type == I40E_TM_NODE_TYPE_PORT)
843                         cap->nonleaf.sched_n_children_max =
844                                 I40E_MAX_TRAFFIC_CLASS;
845                 else
846                         cap->nonleaf.sched_n_children_max =
847                                 hw->func_caps.num_tx_qp;
848                 cap->nonleaf.sched_sp_n_priorities_max = 1;
849                 cap->nonleaf.sched_wfq_n_children_per_group_max = 0;
850                 cap->nonleaf.sched_wfq_n_groups_max = 0;
851                 cap->nonleaf.sched_wfq_weight_max = 1;
852         }
853
854         cap->stats_mask = 0;
855
856         return 0;
857 }
858
859 static int
860 i40e_hierarchy_commit(struct rte_eth_dev *dev,
861                       int clear_on_fail,
862                       struct rte_tm_error *error)
863 {
864         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
865         struct i40e_tm_node_list *tc_list = &pf->tm_conf.tc_list;
866         struct i40e_tm_node_list *queue_list = &pf->tm_conf.queue_list;
867         struct i40e_tm_node *tm_node;
868         struct i40e_vsi *vsi;
869         struct i40e_hw *hw;
870         struct i40e_aqc_configure_vsi_ets_sla_bw_data tc_bw;
871         uint64_t bw;
872         uint8_t tc_map;
873         int ret;
874         int i;
875
876         if (!error)
877                 return -EINVAL;
878
879         /* check the setting */
880         if (!pf->tm_conf.root)
881                 goto done;
882
883         vsi = pf->main_vsi;
884         hw = I40E_VSI_TO_HW(vsi);
885
886         /**
887          * Don't support bandwidth control for port and TCs in parallel.
888          * If the port has a max bandwidth, the TCs should have none.
889          */
890         /* port */
891         bw = pf->tm_conf.root->shaper_profile->profile.peak.rate;
892         if (bw) {
893                 /* check if any TC has a max bandwidth */
894                 TAILQ_FOREACH(tm_node, tc_list, node) {
895                         if (tm_node->shaper_profile->profile.peak.rate) {
896                                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
897                                 error->message = "no port and TC max bandwidth"
898                                                  " in parallel";
899                                 goto fail_clear;
900                         }
901                 }
902
903                 /* interpret Bps to 50Mbps */
904                 bw = bw * 8 / 1000 / 1000 / I40E_QOS_BW_GRANULARITY;
905
906                 /* set the max bandwidth */
907                 ret = i40e_aq_config_vsi_bw_limit(hw, vsi->seid,
908                                                   (uint16_t)bw, 0, NULL);
909                 if (ret) {
910                         error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
911                         error->message = "fail to set port max bandwidth";
912                         goto fail_clear;
913                 }
914
915                 goto done;
916         }
917
918         /* TC */
919         memset(&tc_bw, 0, sizeof(tc_bw));
920         tc_bw.tc_valid_bits = vsi->enabled_tc;
921         tc_map = vsi->enabled_tc;
922         TAILQ_FOREACH(tm_node, tc_list, node) {
923                 if (!tm_node->reference_count) {
924                         error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
925                         error->message = "TC without queue assigned";
926                         goto fail_clear;
927                 }
928
929                 i = 0;
930                 while (i < I40E_MAX_TRAFFIC_CLASS && !(tc_map & BIT_ULL(i)))
931                         i++;
932                 if (i >= I40E_MAX_TRAFFIC_CLASS) {
933                         error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
934                         error->message = "cannot find the TC";
935                         goto fail_clear;
936                 }
937                 tc_map &= ~BIT_ULL(i);
938
939                 bw = tm_node->shaper_profile->profile.peak.rate;
940                 if (!bw)
941                         continue;
942
943                 /* interpret Bps to 50Mbps */
944                 bw = bw * 8 / 1000 / 1000 / I40E_QOS_BW_GRANULARITY;
945
946                 tc_bw.tc_bw_credits[i] = rte_cpu_to_le_16((uint16_t)bw);
947         }
948
949         TAILQ_FOREACH(tm_node, queue_list, node) {
950                 bw = tm_node->shaper_profile->profile.peak.rate;
951                 if (bw) {
952                         error->type = RTE_TM_ERROR_TYPE_NODE_PARAMS;
953                         error->message = "not support queue QoS";
954                         goto fail_clear;
955                 }
956         }
957
958         ret = i40e_aq_config_vsi_ets_sla_bw_limit(hw, vsi->seid, &tc_bw, NULL);
959         if (ret) {
960                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
961                 error->message = "fail to set TC max bandwidth";
962                 goto fail_clear;
963         }
964
965 done:
966         pf->tm_conf.committed = true;
967         return 0;
968
969 fail_clear:
970         /* clear all the traffic manager configuration */
971         if (clear_on_fail) {
972                 i40e_tm_conf_uninit(dev);
973                 i40e_tm_conf_init(dev);
974         }
975         return -EINVAL;
976 }