GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_dcbnl.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 #include <linux/device.h>
33 #include <linux/netdevice.h>
34 #include "en.h"
35
36 #define MLX5E_MAX_PRIORITY 8
37
38 #define MLX5E_100MB (100000)
39 #define MLX5E_1GB   (1000000)
40
41 #define MLX5E_CEE_STATE_UP    1
42 #define MLX5E_CEE_STATE_DOWN  0
43
44 enum {
45         MLX5E_VENDOR_TC_GROUP_NUM = 7,
46         MLX5E_LOWEST_PRIO_GROUP   = 0,
47 };
48
49 /* If dcbx mode is non-host set the dcbx mode to host.
50  */
51 static int mlx5e_dcbnl_set_dcbx_mode(struct mlx5e_priv *priv,
52                                      enum mlx5_dcbx_oper_mode mode)
53 {
54         struct mlx5_core_dev *mdev = priv->mdev;
55         u32 param[MLX5_ST_SZ_DW(dcbx_param)];
56         int err;
57
58         err = mlx5_query_port_dcbx_param(mdev, param);
59         if (err)
60                 return err;
61
62         MLX5_SET(dcbx_param, param, version_admin, mode);
63         if (mode != MLX5E_DCBX_PARAM_VER_OPER_HOST)
64                 MLX5_SET(dcbx_param, param, willing_admin, 1);
65
66         return mlx5_set_port_dcbx_param(mdev, param);
67 }
68
69 static int mlx5e_dcbnl_switch_to_host_mode(struct mlx5e_priv *priv)
70 {
71         struct mlx5e_dcbx *dcbx = &priv->dcbx;
72         int err;
73
74         if (!MLX5_CAP_GEN(priv->mdev, dcbx))
75                 return 0;
76
77         if (dcbx->mode == MLX5E_DCBX_PARAM_VER_OPER_HOST)
78                 return 0;
79
80         err = mlx5e_dcbnl_set_dcbx_mode(priv, MLX5E_DCBX_PARAM_VER_OPER_HOST);
81         if (err)
82                 return err;
83
84         dcbx->mode = MLX5E_DCBX_PARAM_VER_OPER_HOST;
85         return 0;
86 }
87
88 static int mlx5e_dcbnl_ieee_getets(struct net_device *netdev,
89                                    struct ieee_ets *ets)
90 {
91         struct mlx5e_priv *priv = netdev_priv(netdev);
92         struct mlx5_core_dev *mdev = priv->mdev;
93         u8 tc_group[IEEE_8021QAZ_MAX_TCS];
94         bool is_tc_group_6_exist = false;
95         bool is_zero_bw_ets_tc = false;
96         int err = 0;
97         int i;
98
99         if (!MLX5_CAP_GEN(priv->mdev, ets))
100                 return -EOPNOTSUPP;
101
102         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
103                 err = mlx5_query_port_prio_tc(mdev, i, &ets->prio_tc[i]);
104                 if (err)
105                         return err;
106         }
107
108         ets->ets_cap = mlx5_max_tc(priv->mdev) + 1;
109         for (i = 0; i < ets->ets_cap; i++) {
110                 err = mlx5_query_port_tc_group(mdev, i, &tc_group[i]);
111                 if (err)
112                         return err;
113
114                 err = mlx5_query_port_tc_bw_alloc(mdev, i, &ets->tc_tx_bw[i]);
115                 if (err)
116                         return err;
117
118                 if (ets->tc_tx_bw[i] < MLX5E_MAX_BW_ALLOC &&
119                     tc_group[i] == (MLX5E_LOWEST_PRIO_GROUP + 1))
120                         is_zero_bw_ets_tc = true;
121
122                 if (tc_group[i] == (MLX5E_VENDOR_TC_GROUP_NUM - 1))
123                         is_tc_group_6_exist = true;
124         }
125
126         /* Report 0% ets tc if exits*/
127         if (is_zero_bw_ets_tc) {
128                 for (i = 0; i < ets->ets_cap; i++)
129                         if (tc_group[i] == MLX5E_LOWEST_PRIO_GROUP)
130                                 ets->tc_tx_bw[i] = 0;
131         }
132
133         /* Update tc_tsa based on fw setting*/
134         for (i = 0; i < ets->ets_cap; i++) {
135                 if (ets->tc_tx_bw[i] < MLX5E_MAX_BW_ALLOC)
136                         priv->dcbx.tc_tsa[i] = IEEE_8021QAZ_TSA_ETS;
137                 else if (tc_group[i] == MLX5E_VENDOR_TC_GROUP_NUM &&
138                          !is_tc_group_6_exist)
139                         priv->dcbx.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
140         }
141         memcpy(ets->tc_tsa, priv->dcbx.tc_tsa, sizeof(ets->tc_tsa));
142
143         return err;
144 }
145
146 static void mlx5e_build_tc_group(struct ieee_ets *ets, u8 *tc_group, int max_tc)
147 {
148         bool any_tc_mapped_to_ets = false;
149         bool ets_zero_bw = false;
150         int strict_group;
151         int i;
152
153         for (i = 0; i <= max_tc; i++) {
154                 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS) {
155                         any_tc_mapped_to_ets = true;
156                         if (!ets->tc_tx_bw[i])
157                                 ets_zero_bw = true;
158                 }
159         }
160
161         /* strict group has higher priority than ets group */
162         strict_group = MLX5E_LOWEST_PRIO_GROUP;
163         if (any_tc_mapped_to_ets)
164                 strict_group++;
165         if (ets_zero_bw)
166                 strict_group++;
167
168         for (i = 0; i <= max_tc; i++) {
169                 switch (ets->tc_tsa[i]) {
170                 case IEEE_8021QAZ_TSA_VENDOR:
171                         tc_group[i] = MLX5E_VENDOR_TC_GROUP_NUM;
172                         break;
173                 case IEEE_8021QAZ_TSA_STRICT:
174                         tc_group[i] = strict_group++;
175                         break;
176                 case IEEE_8021QAZ_TSA_ETS:
177                         tc_group[i] = MLX5E_LOWEST_PRIO_GROUP;
178                         if (ets->tc_tx_bw[i] && ets_zero_bw)
179                                 tc_group[i] = MLX5E_LOWEST_PRIO_GROUP + 1;
180                         break;
181                 }
182         }
183 }
184
185 static void mlx5e_build_tc_tx_bw(struct ieee_ets *ets, u8 *tc_tx_bw,
186                                  u8 *tc_group, int max_tc)
187 {
188         int bw_for_ets_zero_bw_tc = 0;
189         int last_ets_zero_bw_tc = -1;
190         int num_ets_zero_bw = 0;
191         int i;
192
193         for (i = 0; i <= max_tc; i++) {
194                 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS &&
195                     !ets->tc_tx_bw[i]) {
196                         num_ets_zero_bw++;
197                         last_ets_zero_bw_tc = i;
198                 }
199         }
200
201         if (num_ets_zero_bw)
202                 bw_for_ets_zero_bw_tc = MLX5E_MAX_BW_ALLOC / num_ets_zero_bw;
203
204         for (i = 0; i <= max_tc; i++) {
205                 switch (ets->tc_tsa[i]) {
206                 case IEEE_8021QAZ_TSA_VENDOR:
207                         tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
208                         break;
209                 case IEEE_8021QAZ_TSA_STRICT:
210                         tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
211                         break;
212                 case IEEE_8021QAZ_TSA_ETS:
213                         tc_tx_bw[i] = ets->tc_tx_bw[i] ?
214                                       ets->tc_tx_bw[i] :
215                                       bw_for_ets_zero_bw_tc;
216                         break;
217                 }
218         }
219
220         /* Make sure the total bw for ets zero bw group is 100% */
221         if (last_ets_zero_bw_tc != -1)
222                 tc_tx_bw[last_ets_zero_bw_tc] +=
223                         MLX5E_MAX_BW_ALLOC % num_ets_zero_bw;
224 }
225
226 /* If there are ETS BW 0,
227  *   Set ETS group # to 1 for all ETS non zero BW tcs. Their sum must be 100%.
228  *   Set group #0 to all the ETS BW 0 tcs and
229  *     equally splits the 100% BW between them
230  *   Report both group #0 and #1 as ETS type.
231  *     All the tcs in group #0 will be reported with 0% BW.
232  */
233 int mlx5e_dcbnl_ieee_setets_core(struct mlx5e_priv *priv, struct ieee_ets *ets)
234 {
235         struct mlx5_core_dev *mdev = priv->mdev;
236         u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS];
237         u8 tc_group[IEEE_8021QAZ_MAX_TCS];
238         int max_tc = mlx5_max_tc(mdev);
239         int err;
240
241         mlx5e_build_tc_group(ets, tc_group, max_tc);
242         mlx5e_build_tc_tx_bw(ets, tc_tx_bw, tc_group, max_tc);
243
244         err = mlx5_set_port_prio_tc(mdev, ets->prio_tc);
245         if (err)
246                 return err;
247
248         err = mlx5_set_port_tc_group(mdev, tc_group);
249         if (err)
250                 return err;
251
252         err = mlx5_set_port_tc_bw_alloc(mdev, tc_tx_bw);
253
254         if (err)
255                 return err;
256
257         memcpy(priv->dcbx.tc_tsa, ets->tc_tsa, sizeof(ets->tc_tsa));
258         return err;
259 }
260
261 static int mlx5e_dbcnl_validate_ets(struct net_device *netdev,
262                                     struct ieee_ets *ets,
263                                     bool zero_sum_allowed)
264 {
265         bool have_ets_tc = false;
266         int bw_sum = 0;
267         int i;
268
269         /* Validate Priority */
270         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
271                 if (ets->prio_tc[i] >= MLX5E_MAX_PRIORITY) {
272                         netdev_err(netdev,
273                                    "Failed to validate ETS: priority value greater than max(%d)\n",
274                                     MLX5E_MAX_PRIORITY);
275                         return -EINVAL;
276                 }
277         }
278
279         /* Validate Bandwidth Sum */
280         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
281                 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS) {
282                         have_ets_tc = true;
283                         bw_sum += ets->tc_tx_bw[i];
284                 }
285         }
286
287         if (have_ets_tc && bw_sum != 100) {
288                 if (bw_sum || (!bw_sum && !zero_sum_allowed))
289                         netdev_err(netdev,
290                                    "Failed to validate ETS: BW sum is illegal\n");
291                 return -EINVAL;
292         }
293         return 0;
294 }
295
296 static int mlx5e_dcbnl_ieee_setets(struct net_device *netdev,
297                                    struct ieee_ets *ets)
298 {
299         struct mlx5e_priv *priv = netdev_priv(netdev);
300         int err;
301
302         if (!MLX5_CAP_GEN(priv->mdev, ets))
303                 return -EOPNOTSUPP;
304
305         err = mlx5e_dbcnl_validate_ets(netdev, ets, false);
306         if (err)
307                 return err;
308
309         err = mlx5e_dcbnl_ieee_setets_core(priv, ets);
310         if (err)
311                 return err;
312
313         return 0;
314 }
315
316 static int mlx5e_dcbnl_ieee_getpfc(struct net_device *dev,
317                                    struct ieee_pfc *pfc)
318 {
319         struct mlx5e_priv *priv = netdev_priv(dev);
320         struct mlx5_core_dev *mdev = priv->mdev;
321         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
322         int i;
323
324         pfc->pfc_cap = mlx5_max_tc(mdev) + 1;
325         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
326                 pfc->requests[i]    = PPORT_PER_PRIO_GET(pstats, i, tx_pause);
327                 pfc->indications[i] = PPORT_PER_PRIO_GET(pstats, i, rx_pause);
328         }
329
330         return mlx5_query_port_pfc(mdev, &pfc->pfc_en, NULL);
331 }
332
333 static int mlx5e_dcbnl_ieee_setpfc(struct net_device *dev,
334                                    struct ieee_pfc *pfc)
335 {
336         struct mlx5e_priv *priv = netdev_priv(dev);
337         struct mlx5_core_dev *mdev = priv->mdev;
338         u8 curr_pfc_en;
339         int ret;
340
341         mlx5_query_port_pfc(mdev, &curr_pfc_en, NULL);
342
343         if (pfc->pfc_en == curr_pfc_en)
344                 return 0;
345
346         ret = mlx5_set_port_pfc(mdev, pfc->pfc_en, pfc->pfc_en);
347         mlx5_toggle_port_link(mdev);
348
349         return ret;
350 }
351
352 static u8 mlx5e_dcbnl_getdcbx(struct net_device *dev)
353 {
354         struct mlx5e_priv *priv = netdev_priv(dev);
355
356         return priv->dcbx.cap;
357 }
358
359 static u8 mlx5e_dcbnl_setdcbx(struct net_device *dev, u8 mode)
360 {
361         struct mlx5e_priv *priv = netdev_priv(dev);
362         struct mlx5e_dcbx *dcbx = &priv->dcbx;
363
364         if (mode & DCB_CAP_DCBX_LLD_MANAGED)
365                 return 1;
366
367         if ((!mode) && MLX5_CAP_GEN(priv->mdev, dcbx)) {
368                 if (dcbx->mode == MLX5E_DCBX_PARAM_VER_OPER_AUTO)
369                         return 0;
370
371                 /* set dcbx to fw controlled */
372                 if (!mlx5e_dcbnl_set_dcbx_mode(priv, MLX5E_DCBX_PARAM_VER_OPER_AUTO)) {
373                         dcbx->mode = MLX5E_DCBX_PARAM_VER_OPER_AUTO;
374                         dcbx->cap &= ~DCB_CAP_DCBX_HOST;
375                         return 0;
376                 }
377
378                 return 1;
379         }
380
381         if (!(mode & DCB_CAP_DCBX_HOST))
382                 return 1;
383
384         if (mlx5e_dcbnl_switch_to_host_mode(netdev_priv(dev)))
385                 return 1;
386
387         dcbx->cap = mode;
388
389         return 0;
390 }
391
392 static int mlx5e_dcbnl_ieee_getmaxrate(struct net_device *netdev,
393                                        struct ieee_maxrate *maxrate)
394 {
395         struct mlx5e_priv *priv    = netdev_priv(netdev);
396         struct mlx5_core_dev *mdev = priv->mdev;
397         u8 max_bw_value[IEEE_8021QAZ_MAX_TCS];
398         u8 max_bw_unit[IEEE_8021QAZ_MAX_TCS];
399         int err;
400         int i;
401
402         err = mlx5_query_port_ets_rate_limit(mdev, max_bw_value, max_bw_unit);
403         if (err)
404                 return err;
405
406         memset(maxrate->tc_maxrate, 0, sizeof(maxrate->tc_maxrate));
407
408         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
409                 switch (max_bw_unit[i]) {
410                 case MLX5_100_MBPS_UNIT:
411                         maxrate->tc_maxrate[i] = max_bw_value[i] * MLX5E_100MB;
412                         break;
413                 case MLX5_GBPS_UNIT:
414                         maxrate->tc_maxrate[i] = max_bw_value[i] * MLX5E_1GB;
415                         break;
416                 case MLX5_BW_NO_LIMIT:
417                         break;
418                 default:
419                         WARN(true, "non-supported BW unit");
420                         break;
421                 }
422         }
423
424         return 0;
425 }
426
427 static int mlx5e_dcbnl_ieee_setmaxrate(struct net_device *netdev,
428                                        struct ieee_maxrate *maxrate)
429 {
430         struct mlx5e_priv *priv    = netdev_priv(netdev);
431         struct mlx5_core_dev *mdev = priv->mdev;
432         u8 max_bw_value[IEEE_8021QAZ_MAX_TCS];
433         u8 max_bw_unit[IEEE_8021QAZ_MAX_TCS];
434         __u64 upper_limit_mbps = roundup(255 * MLX5E_100MB, MLX5E_1GB);
435         int i;
436
437         memset(max_bw_value, 0, sizeof(max_bw_value));
438         memset(max_bw_unit, 0, sizeof(max_bw_unit));
439
440         for (i = 0; i <= mlx5_max_tc(mdev); i++) {
441                 if (!maxrate->tc_maxrate[i]) {
442                         max_bw_unit[i]  = MLX5_BW_NO_LIMIT;
443                         continue;
444                 }
445                 if (maxrate->tc_maxrate[i] < upper_limit_mbps) {
446                         max_bw_value[i] = div_u64(maxrate->tc_maxrate[i],
447                                                   MLX5E_100MB);
448                         max_bw_value[i] = max_bw_value[i] ? max_bw_value[i] : 1;
449                         max_bw_unit[i]  = MLX5_100_MBPS_UNIT;
450                 } else {
451                         max_bw_value[i] = div_u64(maxrate->tc_maxrate[i],
452                                                   MLX5E_1GB);
453                         max_bw_unit[i]  = MLX5_GBPS_UNIT;
454                 }
455         }
456
457         return mlx5_modify_port_ets_rate_limit(mdev, max_bw_value, max_bw_unit);
458 }
459
460 static u8 mlx5e_dcbnl_setall(struct net_device *netdev)
461 {
462         struct mlx5e_priv *priv = netdev_priv(netdev);
463         struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
464         struct mlx5_core_dev *mdev = priv->mdev;
465         struct ieee_ets ets;
466         struct ieee_pfc pfc;
467         int err = -EOPNOTSUPP;
468         int i;
469
470         if (!MLX5_CAP_GEN(mdev, ets))
471                 goto out;
472
473         memset(&ets, 0, sizeof(ets));
474         memset(&pfc, 0, sizeof(pfc));
475
476         ets.ets_cap = IEEE_8021QAZ_MAX_TCS;
477         for (i = 0; i < CEE_DCBX_MAX_PGS; i++) {
478                 ets.tc_tx_bw[i] = cee_cfg->pg_bw_pct[i];
479                 ets.tc_rx_bw[i] = cee_cfg->pg_bw_pct[i];
480                 ets.tc_tsa[i]   = IEEE_8021QAZ_TSA_ETS;
481                 ets.prio_tc[i]  = cee_cfg->prio_to_pg_map[i];
482         }
483
484         err = mlx5e_dbcnl_validate_ets(netdev, &ets, true);
485         if (err)
486                 goto out;
487
488         err = mlx5e_dcbnl_ieee_setets_core(priv, &ets);
489         if (err) {
490                 netdev_err(netdev,
491                            "%s, Failed to set ETS: %d\n", __func__, err);
492                 goto out;
493         }
494
495         /* Set PFC */
496         pfc.pfc_cap = mlx5_max_tc(mdev) + 1;
497         if (!cee_cfg->pfc_enable)
498                 pfc.pfc_en = 0;
499         else
500                 for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
501                         pfc.pfc_en |= cee_cfg->pfc_setting[i] << i;
502
503         err = mlx5e_dcbnl_ieee_setpfc(netdev, &pfc);
504         if (err) {
505                 netdev_err(netdev,
506                            "%s, Failed to set PFC: %d\n", __func__, err);
507                 goto out;
508         }
509 out:
510         return err ? MLX5_DCB_NO_CHG : MLX5_DCB_CHG_RESET;
511 }
512
513 static u8 mlx5e_dcbnl_getstate(struct net_device *netdev)
514 {
515         return MLX5E_CEE_STATE_UP;
516 }
517
518 static void mlx5e_dcbnl_getpermhwaddr(struct net_device *netdev,
519                                       u8 *perm_addr)
520 {
521         struct mlx5e_priv *priv = netdev_priv(netdev);
522
523         if (!perm_addr)
524                 return;
525
526         memset(perm_addr, 0xff, MAX_ADDR_LEN);
527
528         mlx5_query_nic_vport_mac_address(priv->mdev, 0, perm_addr);
529 }
530
531 static void mlx5e_dcbnl_setpgtccfgtx(struct net_device *netdev,
532                                      int priority, u8 prio_type,
533                                      u8 pgid, u8 bw_pct, u8 up_map)
534 {
535         struct mlx5e_priv *priv = netdev_priv(netdev);
536         struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
537
538         if (priority >= CEE_DCBX_MAX_PRIO) {
539                 netdev_err(netdev,
540                            "%s, priority is out of range\n", __func__);
541                 return;
542         }
543
544         if (pgid >= CEE_DCBX_MAX_PGS) {
545                 netdev_err(netdev,
546                            "%s, priority group is out of range\n", __func__);
547                 return;
548         }
549
550         cee_cfg->prio_to_pg_map[priority] = pgid;
551 }
552
553 static void mlx5e_dcbnl_setpgbwgcfgtx(struct net_device *netdev,
554                                       int pgid, u8 bw_pct)
555 {
556         struct mlx5e_priv *priv = netdev_priv(netdev);
557         struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
558
559         if (pgid >= CEE_DCBX_MAX_PGS) {
560                 netdev_err(netdev,
561                            "%s, priority group is out of range\n", __func__);
562                 return;
563         }
564
565         cee_cfg->pg_bw_pct[pgid] = bw_pct;
566 }
567
568 static void mlx5e_dcbnl_getpgtccfgtx(struct net_device *netdev,
569                                      int priority, u8 *prio_type,
570                                      u8 *pgid, u8 *bw_pct, u8 *up_map)
571 {
572         struct mlx5e_priv *priv = netdev_priv(netdev);
573         struct mlx5_core_dev *mdev = priv->mdev;
574
575         if (!MLX5_CAP_GEN(priv->mdev, ets)) {
576                 netdev_err(netdev, "%s, ets is not supported\n", __func__);
577                 return;
578         }
579
580         if (priority >= CEE_DCBX_MAX_PRIO) {
581                 netdev_err(netdev,
582                            "%s, priority is out of range\n", __func__);
583                 return;
584         }
585
586         *prio_type = 0;
587         *bw_pct = 0;
588         *up_map = 0;
589
590         if (mlx5_query_port_prio_tc(mdev, priority, pgid))
591                 *pgid = 0;
592 }
593
594 static void mlx5e_dcbnl_getpgbwgcfgtx(struct net_device *netdev,
595                                       int pgid, u8 *bw_pct)
596 {
597         struct ieee_ets ets;
598
599         if (pgid >= CEE_DCBX_MAX_PGS) {
600                 netdev_err(netdev,
601                            "%s, priority group is out of range\n", __func__);
602                 return;
603         }
604
605         mlx5e_dcbnl_ieee_getets(netdev, &ets);
606         *bw_pct = ets.tc_tx_bw[pgid];
607 }
608
609 static void mlx5e_dcbnl_setpfccfg(struct net_device *netdev,
610                                   int priority, u8 setting)
611 {
612         struct mlx5e_priv *priv = netdev_priv(netdev);
613         struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
614
615         if (priority >= CEE_DCBX_MAX_PRIO) {
616                 netdev_err(netdev,
617                            "%s, priority is out of range\n", __func__);
618                 return;
619         }
620
621         if (setting > 1)
622                 return;
623
624         cee_cfg->pfc_setting[priority] = setting;
625 }
626
627 static int
628 mlx5e_dcbnl_get_priority_pfc(struct net_device *netdev,
629                              int priority, u8 *setting)
630 {
631         struct ieee_pfc pfc;
632         int err;
633
634         err = mlx5e_dcbnl_ieee_getpfc(netdev, &pfc);
635
636         if (err)
637                 *setting = 0;
638         else
639                 *setting = (pfc.pfc_en >> priority) & 0x01;
640
641         return err;
642 }
643
644 static void mlx5e_dcbnl_getpfccfg(struct net_device *netdev,
645                                   int priority, u8 *setting)
646 {
647         if (priority >= CEE_DCBX_MAX_PRIO) {
648                 netdev_err(netdev,
649                            "%s, priority is out of range\n", __func__);
650                 return;
651         }
652
653         if (!setting)
654                 return;
655
656         mlx5e_dcbnl_get_priority_pfc(netdev, priority, setting);
657 }
658
659 static u8 mlx5e_dcbnl_getcap(struct net_device *netdev,
660                              int capid, u8 *cap)
661 {
662         struct mlx5e_priv *priv = netdev_priv(netdev);
663         struct mlx5_core_dev *mdev = priv->mdev;
664         u8 rval = 0;
665
666         switch (capid) {
667         case DCB_CAP_ATTR_PG:
668                 *cap = true;
669                 break;
670         case DCB_CAP_ATTR_PFC:
671                 *cap = true;
672                 break;
673         case DCB_CAP_ATTR_UP2TC:
674                 *cap = false;
675                 break;
676         case DCB_CAP_ATTR_PG_TCS:
677                 *cap = 1 << mlx5_max_tc(mdev);
678                 break;
679         case DCB_CAP_ATTR_PFC_TCS:
680                 *cap = 1 << mlx5_max_tc(mdev);
681                 break;
682         case DCB_CAP_ATTR_GSP:
683                 *cap = false;
684                 break;
685         case DCB_CAP_ATTR_BCN:
686                 *cap = false;
687                 break;
688         case DCB_CAP_ATTR_DCBX:
689                 *cap = priv->dcbx.cap |
690                        DCB_CAP_DCBX_VER_CEE |
691                        DCB_CAP_DCBX_VER_IEEE;
692                 break;
693         default:
694                 *cap = 0;
695                 rval = 1;
696                 break;
697         }
698
699         return rval;
700 }
701
702 static int mlx5e_dcbnl_getnumtcs(struct net_device *netdev,
703                                  int tcs_id, u8 *num)
704 {
705         struct mlx5e_priv *priv = netdev_priv(netdev);
706         struct mlx5_core_dev *mdev = priv->mdev;
707
708         switch (tcs_id) {
709         case DCB_NUMTCS_ATTR_PG:
710         case DCB_NUMTCS_ATTR_PFC:
711                 *num = mlx5_max_tc(mdev) + 1;
712                 break;
713         default:
714                 return -EINVAL;
715         }
716
717         return 0;
718 }
719
720 static u8 mlx5e_dcbnl_getpfcstate(struct net_device *netdev)
721 {
722         struct ieee_pfc pfc;
723
724         if (mlx5e_dcbnl_ieee_getpfc(netdev, &pfc))
725                 return MLX5E_CEE_STATE_DOWN;
726
727         return pfc.pfc_en ? MLX5E_CEE_STATE_UP : MLX5E_CEE_STATE_DOWN;
728 }
729
730 static void mlx5e_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
731 {
732         struct mlx5e_priv *priv = netdev_priv(netdev);
733         struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
734
735         if ((state != MLX5E_CEE_STATE_UP) && (state != MLX5E_CEE_STATE_DOWN))
736                 return;
737
738         cee_cfg->pfc_enable = state;
739 }
740
741 const struct dcbnl_rtnl_ops mlx5e_dcbnl_ops = {
742         .ieee_getets    = mlx5e_dcbnl_ieee_getets,
743         .ieee_setets    = mlx5e_dcbnl_ieee_setets,
744         .ieee_getmaxrate = mlx5e_dcbnl_ieee_getmaxrate,
745         .ieee_setmaxrate = mlx5e_dcbnl_ieee_setmaxrate,
746         .ieee_getpfc    = mlx5e_dcbnl_ieee_getpfc,
747         .ieee_setpfc    = mlx5e_dcbnl_ieee_setpfc,
748         .getdcbx        = mlx5e_dcbnl_getdcbx,
749         .setdcbx        = mlx5e_dcbnl_setdcbx,
750
751 /* CEE interfaces */
752         .setall         = mlx5e_dcbnl_setall,
753         .getstate       = mlx5e_dcbnl_getstate,
754         .getpermhwaddr  = mlx5e_dcbnl_getpermhwaddr,
755
756         .setpgtccfgtx   = mlx5e_dcbnl_setpgtccfgtx,
757         .setpgbwgcfgtx  = mlx5e_dcbnl_setpgbwgcfgtx,
758         .getpgtccfgtx   = mlx5e_dcbnl_getpgtccfgtx,
759         .getpgbwgcfgtx  = mlx5e_dcbnl_getpgbwgcfgtx,
760
761         .setpfccfg      = mlx5e_dcbnl_setpfccfg,
762         .getpfccfg      = mlx5e_dcbnl_getpfccfg,
763         .getcap         = mlx5e_dcbnl_getcap,
764         .getnumtcs      = mlx5e_dcbnl_getnumtcs,
765         .getpfcstate    = mlx5e_dcbnl_getpfcstate,
766         .setpfcstate    = mlx5e_dcbnl_setpfcstate,
767 };
768
769 static void mlx5e_dcbnl_query_dcbx_mode(struct mlx5e_priv *priv,
770                                         enum mlx5_dcbx_oper_mode *mode)
771 {
772         u32 out[MLX5_ST_SZ_DW(dcbx_param)];
773
774         *mode = MLX5E_DCBX_PARAM_VER_OPER_HOST;
775
776         if (!mlx5_query_port_dcbx_param(priv->mdev, out))
777                 *mode = MLX5_GET(dcbx_param, out, version_oper);
778
779         /* From driver's point of view, we only care if the mode
780          * is host (HOST) or non-host (AUTO)
781          */
782         if (*mode != MLX5E_DCBX_PARAM_VER_OPER_HOST)
783                 *mode = MLX5E_DCBX_PARAM_VER_OPER_AUTO;
784 }
785
786 static void mlx5e_ets_init(struct mlx5e_priv *priv)
787 {
788         int i;
789         struct ieee_ets ets;
790
791         if (!MLX5_CAP_GEN(priv->mdev, ets))
792                 return;
793
794         memset(&ets, 0, sizeof(ets));
795         ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
796         for (i = 0; i < ets.ets_cap; i++) {
797                 ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
798                 ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
799                 ets.prio_tc[i] = i;
800         }
801
802         /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
803         ets.prio_tc[0] = 1;
804         ets.prio_tc[1] = 0;
805
806         mlx5e_dcbnl_ieee_setets_core(priv, &ets);
807 }
808
809 void mlx5e_dcbnl_initialize(struct mlx5e_priv *priv)
810 {
811         struct mlx5e_dcbx *dcbx = &priv->dcbx;
812
813         if (!MLX5_CAP_GEN(priv->mdev, qos))
814                 return;
815
816         if (MLX5_CAP_GEN(priv->mdev, dcbx))
817                 mlx5e_dcbnl_query_dcbx_mode(priv, &dcbx->mode);
818
819         priv->dcbx.cap = DCB_CAP_DCBX_VER_CEE |
820                          DCB_CAP_DCBX_VER_IEEE;
821         if (priv->dcbx.mode == MLX5E_DCBX_PARAM_VER_OPER_HOST)
822                 priv->dcbx.cap |= DCB_CAP_DCBX_HOST;
823
824         mlx5e_ets_init(priv);
825 }