628cf13d6bf3098eca21abd73223c85dfd2d2b92
[dpdk.git] / drivers / net / ixgbe / ixgbe_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 "ixgbe_ethdev.h"
37
38 static int ixgbe_tm_capabilities_get(struct rte_eth_dev *dev,
39                                      struct rte_tm_capabilities *cap,
40                                      struct rte_tm_error *error);
41 static int ixgbe_shaper_profile_add(struct rte_eth_dev *dev,
42                                     uint32_t shaper_profile_id,
43                                     struct rte_tm_shaper_params *profile,
44                                     struct rte_tm_error *error);
45 static int ixgbe_shaper_profile_del(struct rte_eth_dev *dev,
46                                     uint32_t shaper_profile_id,
47                                     struct rte_tm_error *error);
48
49 const struct rte_tm_ops ixgbe_tm_ops = {
50         .capabilities_get = ixgbe_tm_capabilities_get,
51         .shaper_profile_add = ixgbe_shaper_profile_add,
52         .shaper_profile_delete = ixgbe_shaper_profile_del,
53 };
54
55 int
56 ixgbe_tm_ops_get(struct rte_eth_dev *dev __rte_unused,
57                  void *arg)
58 {
59         if (!arg)
60                 return -EINVAL;
61
62         *(const void **)arg = &ixgbe_tm_ops;
63
64         return 0;
65 }
66
67 void
68 ixgbe_tm_conf_init(struct rte_eth_dev *dev)
69 {
70         struct ixgbe_tm_conf *tm_conf =
71                 IXGBE_DEV_PRIVATE_TO_TM_CONF(dev->data->dev_private);
72
73         /* initialize shaper profile list */
74         TAILQ_INIT(&tm_conf->shaper_profile_list);
75 }
76
77 void
78 ixgbe_tm_conf_uninit(struct rte_eth_dev *dev)
79 {
80         struct ixgbe_tm_conf *tm_conf =
81                 IXGBE_DEV_PRIVATE_TO_TM_CONF(dev->data->dev_private);
82         struct ixgbe_tm_shaper_profile *shaper_profile;
83
84         /* Remove all shaper profiles */
85         while ((shaper_profile =
86                TAILQ_FIRST(&tm_conf->shaper_profile_list))) {
87                 TAILQ_REMOVE(&tm_conf->shaper_profile_list,
88                              shaper_profile, node);
89                 rte_free(shaper_profile);
90         }
91 }
92
93 static inline uint8_t
94 ixgbe_tc_nb_get(struct rte_eth_dev *dev)
95 {
96         struct rte_eth_conf *eth_conf;
97         uint8_t nb_tcs = 0;
98
99         eth_conf = &dev->data->dev_conf;
100         if (eth_conf->txmode.mq_mode == ETH_MQ_TX_DCB) {
101                 nb_tcs = eth_conf->tx_adv_conf.dcb_tx_conf.nb_tcs;
102         } else if (eth_conf->txmode.mq_mode == ETH_MQ_TX_VMDQ_DCB) {
103                 if (eth_conf->tx_adv_conf.vmdq_dcb_tx_conf.nb_queue_pools ==
104                     ETH_32_POOLS)
105                         nb_tcs = ETH_4_TCS;
106                 else
107                         nb_tcs = ETH_8_TCS;
108         } else {
109                 nb_tcs = 1;
110         }
111
112         return nb_tcs;
113 }
114
115 static int
116 ixgbe_tm_capabilities_get(struct rte_eth_dev *dev,
117                           struct rte_tm_capabilities *cap,
118                           struct rte_tm_error *error)
119 {
120         struct ixgbe_hw *hw = IXGBE_DEV_PRIVATE_TO_HW(dev->data->dev_private);
121         uint8_t tc_nb = ixgbe_tc_nb_get(dev);
122
123         if (!cap || !error)
124                 return -EINVAL;
125
126         if (tc_nb > hw->mac.max_tx_queues)
127                 return -EINVAL;
128
129         error->type = RTE_TM_ERROR_TYPE_NONE;
130
131         /* set all the parameters to 0 first. */
132         memset(cap, 0, sizeof(struct rte_tm_capabilities));
133
134         /**
135          * here is the max capability not the current configuration.
136          */
137         /* port + TCs + queues */
138         cap->n_nodes_max = 1 + IXGBE_DCB_MAX_TRAFFIC_CLASS +
139                            hw->mac.max_tx_queues;
140         cap->n_levels_max = 3;
141         cap->non_leaf_nodes_identical = 1;
142         cap->leaf_nodes_identical = 1;
143         cap->shaper_n_max = cap->n_nodes_max;
144         cap->shaper_private_n_max = cap->n_nodes_max;
145         cap->shaper_private_dual_rate_n_max = 0;
146         cap->shaper_private_rate_min = 0;
147         /* 10Gbps -> 1.25GBps */
148         cap->shaper_private_rate_max = 1250000000ull;
149         cap->shaper_shared_n_max = 0;
150         cap->shaper_shared_n_nodes_per_shaper_max = 0;
151         cap->shaper_shared_n_shapers_per_node_max = 0;
152         cap->shaper_shared_dual_rate_n_max = 0;
153         cap->shaper_shared_rate_min = 0;
154         cap->shaper_shared_rate_max = 0;
155         cap->sched_n_children_max = hw->mac.max_tx_queues;
156         /**
157          * HW supports SP. But no plan to support it now.
158          * So, all the nodes should have the same priority.
159          */
160         cap->sched_sp_n_priorities_max = 1;
161         cap->sched_wfq_n_children_per_group_max = 0;
162         cap->sched_wfq_n_groups_max = 0;
163         /**
164          * SW only supports fair round robin now.
165          * So, all the nodes should have the same weight.
166          */
167         cap->sched_wfq_weight_max = 1;
168         cap->cman_head_drop_supported = 0;
169         cap->dynamic_update_mask = 0;
170         cap->shaper_pkt_length_adjust_min = RTE_TM_ETH_FRAMING_OVERHEAD;
171         cap->shaper_pkt_length_adjust_max = RTE_TM_ETH_FRAMING_OVERHEAD_FCS;
172         cap->cman_wred_context_n_max = 0;
173         cap->cman_wred_context_private_n_max = 0;
174         cap->cman_wred_context_shared_n_max = 0;
175         cap->cman_wred_context_shared_n_nodes_per_context_max = 0;
176         cap->cman_wred_context_shared_n_contexts_per_node_max = 0;
177         cap->stats_mask = 0;
178
179         return 0;
180 }
181
182 static inline struct ixgbe_tm_shaper_profile *
183 ixgbe_shaper_profile_search(struct rte_eth_dev *dev,
184                             uint32_t shaper_profile_id)
185 {
186         struct ixgbe_tm_conf *tm_conf =
187                 IXGBE_DEV_PRIVATE_TO_TM_CONF(dev->data->dev_private);
188         struct ixgbe_shaper_profile_list *shaper_profile_list =
189                 &tm_conf->shaper_profile_list;
190         struct ixgbe_tm_shaper_profile *shaper_profile;
191
192         TAILQ_FOREACH(shaper_profile, shaper_profile_list, node) {
193                 if (shaper_profile_id == shaper_profile->shaper_profile_id)
194                         return shaper_profile;
195         }
196
197         return NULL;
198 }
199
200 static int
201 ixgbe_shaper_profile_param_check(struct rte_tm_shaper_params *profile,
202                                  struct rte_tm_error *error)
203 {
204         /* min rate not supported */
205         if (profile->committed.rate) {
206                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_RATE;
207                 error->message = "committed rate not supported";
208                 return -EINVAL;
209         }
210         /* min bucket size not supported */
211         if (profile->committed.size) {
212                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_COMMITTED_SIZE;
213                 error->message = "committed bucket size not supported";
214                 return -EINVAL;
215         }
216         /* max bucket size not supported */
217         if (profile->peak.size) {
218                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PEAK_SIZE;
219                 error->message = "peak bucket size not supported";
220                 return -EINVAL;
221         }
222         /* length adjustment not supported */
223         if (profile->pkt_length_adjust) {
224                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_PKT_ADJUST_LEN;
225                 error->message = "packet length adjustment not supported";
226                 return -EINVAL;
227         }
228
229         return 0;
230 }
231
232 static int
233 ixgbe_shaper_profile_add(struct rte_eth_dev *dev,
234                          uint32_t shaper_profile_id,
235                          struct rte_tm_shaper_params *profile,
236                          struct rte_tm_error *error)
237 {
238         struct ixgbe_tm_conf *tm_conf =
239                 IXGBE_DEV_PRIVATE_TO_TM_CONF(dev->data->dev_private);
240         struct ixgbe_tm_shaper_profile *shaper_profile;
241         int ret;
242
243         if (!profile || !error)
244                 return -EINVAL;
245
246         ret = ixgbe_shaper_profile_param_check(profile, error);
247         if (ret)
248                 return ret;
249
250         shaper_profile = ixgbe_shaper_profile_search(dev, shaper_profile_id);
251
252         if (shaper_profile) {
253                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
254                 error->message = "profile ID exist";
255                 return -EINVAL;
256         }
257
258         shaper_profile = rte_zmalloc("ixgbe_tm_shaper_profile",
259                                      sizeof(struct ixgbe_tm_shaper_profile),
260                                      0);
261         if (!shaper_profile)
262                 return -ENOMEM;
263         shaper_profile->shaper_profile_id = shaper_profile_id;
264         (void)rte_memcpy(&shaper_profile->profile, profile,
265                          sizeof(struct rte_tm_shaper_params));
266         TAILQ_INSERT_TAIL(&tm_conf->shaper_profile_list,
267                           shaper_profile, node);
268
269         return 0;
270 }
271
272 static int
273 ixgbe_shaper_profile_del(struct rte_eth_dev *dev,
274                          uint32_t shaper_profile_id,
275                          struct rte_tm_error *error)
276 {
277         struct ixgbe_tm_conf *tm_conf =
278                 IXGBE_DEV_PRIVATE_TO_TM_CONF(dev->data->dev_private);
279         struct ixgbe_tm_shaper_profile *shaper_profile;
280
281         if (!error)
282                 return -EINVAL;
283
284         shaper_profile = ixgbe_shaper_profile_search(dev, shaper_profile_id);
285
286         if (!shaper_profile) {
287                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE_ID;
288                 error->message = "profile ID not exist";
289                 return -EINVAL;
290         }
291
292         /* don't delete a profile if it's used by one or several nodes */
293         if (shaper_profile->reference_count) {
294                 error->type = RTE_TM_ERROR_TYPE_SHAPER_PROFILE;
295                 error->message = "profile in use";
296                 return -EINVAL;
297         }
298
299         TAILQ_REMOVE(&tm_conf->shaper_profile_list, shaper_profile, node);
300         rte_free(shaper_profile);
301
302         return 0;
303 }