67df70122f0e5d25e518ec55aca4617ca1b3b4c1
[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
50 const struct rte_tm_ops i40e_tm_ops = {
51         .capabilities_get = i40e_tm_capabilities_get,
52         .shaper_profile_add = i40e_shaper_profile_add,
53         .shaper_profile_delete = i40e_shaper_profile_del,
54 };
55
56 int
57 i40e_tm_ops_get(struct rte_eth_dev *dev __rte_unused,
58                 void *arg)
59 {
60         if (!arg)
61                 return -EINVAL;
62
63         *(const void **)arg = &i40e_tm_ops;
64
65         return 0;
66 }
67
68 void
69 i40e_tm_conf_init(struct rte_eth_dev *dev)
70 {
71         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
72
73         /* initialize shaper profile list */
74         TAILQ_INIT(&pf->tm_conf.shaper_profile_list);
75 }
76
77 void
78 i40e_tm_conf_uninit(struct rte_eth_dev *dev)
79 {
80         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
81         struct i40e_tm_shaper_profile *shaper_profile;
82
83         /* Remove all shaper profiles */
84         while ((shaper_profile =
85                TAILQ_FIRST(&pf->tm_conf.shaper_profile_list))) {
86                 TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list,
87                              shaper_profile, node);
88                 rte_free(shaper_profile);
89         }
90 }
91
92 static inline uint16_t
93 i40e_tc_nb_get(struct rte_eth_dev *dev)
94 {
95         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
96         struct i40e_vsi *main_vsi = pf->main_vsi;
97         uint16_t sum = 0;
98         int i;
99
100         for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
101                 if (main_vsi->enabled_tc & BIT_ULL(i))
102                         sum++;
103         }
104
105         return sum;
106 }
107
108 static int
109 i40e_tm_capabilities_get(struct rte_eth_dev *dev,
110                          struct rte_tm_capabilities *cap,
111                          struct rte_tm_error *error)
112 {
113         struct i40e_hw *hw = I40E_DEV_PRIVATE_TO_HW(dev->data->dev_private);
114         uint16_t tc_nb = i40e_tc_nb_get(dev);
115
116         if (!cap || !error)
117                 return -EINVAL;
118
119         if (tc_nb > hw->func_caps.num_tx_qp)
120                 return -EINVAL;
121
122         error->type = RTE_TM_ERROR_TYPE_NONE;
123
124         /* set all the parameters to 0 first. */
125         memset(cap, 0, sizeof(struct rte_tm_capabilities));
126
127         /**
128          * support port + TCs + queues
129          * here shows the max capability not the current configuration.
130          */
131         cap->n_nodes_max = 1 + I40E_MAX_TRAFFIC_CLASS + hw->func_caps.num_tx_qp;
132         cap->n_levels_max = 3; /* port, TC, queue */
133         cap->non_leaf_nodes_identical = 1;
134         cap->leaf_nodes_identical = 1;
135         cap->shaper_n_max = cap->n_nodes_max;
136         cap->shaper_private_n_max = cap->n_nodes_max;
137         cap->shaper_private_dual_rate_n_max = 0;
138         cap->shaper_private_rate_min = 0;
139         /* 40Gbps -> 5GBps */
140         cap->shaper_private_rate_max = 5000000000ull;
141         cap->shaper_shared_n_max = 0;
142         cap->shaper_shared_n_nodes_per_shaper_max = 0;
143         cap->shaper_shared_n_shapers_per_node_max = 0;
144         cap->shaper_shared_dual_rate_n_max = 0;
145         cap->shaper_shared_rate_min = 0;
146         cap->shaper_shared_rate_max = 0;
147         cap->sched_n_children_max = hw->func_caps.num_tx_qp;
148         /**
149          * HW supports SP. But no plan to support it now.
150          * So, all the nodes should have the same priority.
151          */
152         cap->sched_sp_n_priorities_max = 1;
153         cap->sched_wfq_n_children_per_group_max = 0;
154         cap->sched_wfq_n_groups_max = 0;
155         /**
156          * SW only supports fair round robin now.
157          * So, all the nodes should have the same weight.
158          */
159         cap->sched_wfq_weight_max = 1;
160         cap->cman_head_drop_supported = 0;
161         cap->dynamic_update_mask = 0;
162         cap->shaper_pkt_length_adjust_min = RTE_TM_ETH_FRAMING_OVERHEAD;
163         cap->shaper_pkt_length_adjust_max = RTE_TM_ETH_FRAMING_OVERHEAD_FCS;
164         cap->cman_wred_context_n_max = 0;
165         cap->cman_wred_context_private_n_max = 0;
166         cap->cman_wred_context_shared_n_max = 0;
167         cap->cman_wred_context_shared_n_nodes_per_context_max = 0;
168         cap->cman_wred_context_shared_n_contexts_per_node_max = 0;
169         cap->stats_mask = 0;
170
171         return 0;
172 }
173
174 static inline struct i40e_tm_shaper_profile *
175 i40e_shaper_profile_search(struct rte_eth_dev *dev,
176                            uint32_t shaper_profile_id)
177 {
178         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
179         struct i40e_shaper_profile_list *shaper_profile_list =
180                 &pf->tm_conf.shaper_profile_list;
181         struct i40e_tm_shaper_profile *shaper_profile;
182
183         TAILQ_FOREACH(shaper_profile, shaper_profile_list, node) {
184                 if (shaper_profile_id == shaper_profile->shaper_profile_id)
185                         return shaper_profile;
186         }
187
188         return NULL;
189 }
190
191 static int
192 i40e_shaper_profile_param_check(struct rte_tm_shaper_params *profile,
193                                 struct rte_tm_error *error)
194 {
195         /* min rate not supported */
196         if (profile->committed.rate) {
197                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_RATE;
198                 error->message = "committed rate not supported";
199                 return -EINVAL;
200         }
201         /* min bucket size not supported */
202         if (profile->committed.size) {
203                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_SIZE;
204                 error->message = "committed bucket size not supported";
205                 return -EINVAL;
206         }
207         /* max bucket size not supported */
208         if (profile->peak.size) {
209                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PEAK_SIZE;
210                 error->message = "peak bucket size not supported";
211                 return -EINVAL;
212         }
213         /* length adjustment not supported */
214         if (profile->pkt_length_adjust) {
215                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PKT_ADJUST_LEN;
216                 error->message = "packet length adjustment not supported";
217                 return -EINVAL;
218         }
219
220         return 0;
221 }
222
223 static int
224 i40e_shaper_profile_add(struct rte_eth_dev *dev,
225                         uint32_t shaper_profile_id,
226                         struct rte_tm_shaper_params *profile,
227                         struct rte_tm_error *error)
228 {
229         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
230         struct i40e_tm_shaper_profile *shaper_profile;
231         int ret;
232
233         if (!profile || !error)
234                 return -EINVAL;
235
236         ret = i40e_shaper_profile_param_check(profile, error);
237         if (ret)
238                 return ret;
239
240         shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
241
242         if (shaper_profile) {
243                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
244                 error->message = "profile ID exist";
245                 return -EINVAL;
246         }
247
248         shaper_profile = rte_zmalloc("i40e_tm_shaper_profile",
249                                      sizeof(struct i40e_tm_shaper_profile),
250                                      0);
251         if (!shaper_profile)
252                 return -ENOMEM;
253         shaper_profile->shaper_profile_id = shaper_profile_id;
254         (void)rte_memcpy(&shaper_profile->profile, profile,
255                          sizeof(struct rte_tm_shaper_params));
256         TAILQ_INSERT_TAIL(&pf->tm_conf.shaper_profile_list,
257                           shaper_profile, node);
258
259         return 0;
260 }
261
262 static int
263 i40e_shaper_profile_del(struct rte_eth_dev *dev,
264                         uint32_t shaper_profile_id,
265                         struct rte_tm_error *error)
266 {
267         struct i40e_pf *pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
268         struct i40e_tm_shaper_profile *shaper_profile;
269
270         if (!error)
271                 return -EINVAL;
272
273         shaper_profile = i40e_shaper_profile_search(dev, shaper_profile_id);
274
275         if (!shaper_profile) {
276                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
277                 error->message = "profile ID not exist";
278                 return -EINVAL;
279         }
280
281         /* don't delete a profile if it's used by one or several nodes */
282         if (shaper_profile->reference_count) {
283                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
284                 error->message = "profile in use";
285                 return -EINVAL;
286         }
287
288         TAILQ_REMOVE(&pf->tm_conf.shaper_profile_list, shaper_profile, node);
289         rte_free(shaper_profile);
290
291         return 0;
292 }