2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
32 #include <linux/device.h>
33 #include <linux/netdevice.h>
36 #define MLX5E_MAX_PRIORITY 8
38 #define MLX5E_100MB (100000)
39 #define MLX5E_1GB (1000000)
41 #define MLX5E_CEE_STATE_UP 1
42 #define MLX5E_CEE_STATE_DOWN 0
44 static int mlx5e_dcbnl_ieee_getets(struct net_device *netdev,
47 struct mlx5e_priv *priv = netdev_priv(netdev);
48 struct mlx5_core_dev *mdev = priv->mdev;
52 if (!MLX5_CAP_GEN(priv->mdev, ets))
55 ets->ets_cap = mlx5_max_tc(priv->mdev) + 1;
56 for (i = 0; i < ets->ets_cap; i++) {
57 err = mlx5_query_port_prio_tc(mdev, i, &ets->prio_tc[i]);
62 for (i = 0; i < ets->ets_cap; i++) {
63 err = mlx5_query_port_tc_bw_alloc(mdev, i, &ets->tc_tx_bw[i]);
66 if (ets->tc_tx_bw[i] < MLX5E_MAX_BW_ALLOC)
67 priv->dcbx.tc_tsa[i] = IEEE_8021QAZ_TSA_ETS;
70 memcpy(ets->tc_tsa, priv->dcbx.tc_tsa, sizeof(ets->tc_tsa));
76 MLX5E_VENDOR_TC_GROUP_NUM = 7,
77 MLX5E_ETS_TC_GROUP_NUM = 0,
80 static void mlx5e_build_tc_group(struct ieee_ets *ets, u8 *tc_group, int max_tc)
82 bool any_tc_mapped_to_ets = false;
86 for (i = 0; i <= max_tc; i++)
87 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS)
88 any_tc_mapped_to_ets = true;
90 strict_group = any_tc_mapped_to_ets ? 1 : 0;
92 for (i = 0; i <= max_tc; i++) {
93 switch (ets->tc_tsa[i]) {
94 case IEEE_8021QAZ_TSA_VENDOR:
95 tc_group[i] = MLX5E_VENDOR_TC_GROUP_NUM;
97 case IEEE_8021QAZ_TSA_STRICT:
98 tc_group[i] = strict_group++;
100 case IEEE_8021QAZ_TSA_ETS:
101 tc_group[i] = MLX5E_ETS_TC_GROUP_NUM;
107 static void mlx5e_build_tc_tx_bw(struct ieee_ets *ets, u8 *tc_tx_bw,
108 u8 *tc_group, int max_tc)
112 for (i = 0; i <= max_tc; i++) {
113 switch (ets->tc_tsa[i]) {
114 case IEEE_8021QAZ_TSA_VENDOR:
115 tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
117 case IEEE_8021QAZ_TSA_STRICT:
118 tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
120 case IEEE_8021QAZ_TSA_ETS:
121 tc_tx_bw[i] = ets->tc_tx_bw[i];
127 int mlx5e_dcbnl_ieee_setets_core(struct mlx5e_priv *priv, struct ieee_ets *ets)
129 struct mlx5_core_dev *mdev = priv->mdev;
130 u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS];
131 u8 tc_group[IEEE_8021QAZ_MAX_TCS];
132 int max_tc = mlx5_max_tc(mdev);
135 mlx5e_build_tc_group(ets, tc_group, max_tc);
136 mlx5e_build_tc_tx_bw(ets, tc_tx_bw, tc_group, max_tc);
138 err = mlx5_set_port_prio_tc(mdev, ets->prio_tc);
142 err = mlx5_set_port_tc_group(mdev, tc_group);
146 err = mlx5_set_port_tc_bw_alloc(mdev, tc_tx_bw);
151 memcpy(priv->dcbx.tc_tsa, ets->tc_tsa, sizeof(ets->tc_tsa));
156 static int mlx5e_dbcnl_validate_ets(struct net_device *netdev,
157 struct ieee_ets *ets)
162 /* Validate Priority */
163 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
164 if (ets->prio_tc[i] >= MLX5E_MAX_PRIORITY) {
166 "Failed to validate ETS: priority value greater than max(%d)\n",
172 /* Validate Bandwidth Sum */
173 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
174 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS) {
175 if (!ets->tc_tx_bw[i]) {
177 "Failed to validate ETS: BW 0 is illegal\n");
181 bw_sum += ets->tc_tx_bw[i];
185 if (bw_sum != 0 && bw_sum != 100) {
187 "Failed to validate ETS: BW sum is illegal\n");
193 static int mlx5e_dcbnl_ieee_setets(struct net_device *netdev,
194 struct ieee_ets *ets)
196 struct mlx5e_priv *priv = netdev_priv(netdev);
199 if (!MLX5_CAP_GEN(priv->mdev, ets))
202 err = mlx5e_dbcnl_validate_ets(netdev, ets);
206 err = mlx5e_dcbnl_ieee_setets_core(priv, ets);
213 static int mlx5e_dcbnl_ieee_getpfc(struct net_device *dev,
214 struct ieee_pfc *pfc)
216 struct mlx5e_priv *priv = netdev_priv(dev);
217 struct mlx5_core_dev *mdev = priv->mdev;
218 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
221 pfc->pfc_cap = mlx5_max_tc(mdev) + 1;
222 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
223 pfc->requests[i] = PPORT_PER_PRIO_GET(pstats, i, tx_pause);
224 pfc->indications[i] = PPORT_PER_PRIO_GET(pstats, i, rx_pause);
227 return mlx5_query_port_pfc(mdev, &pfc->pfc_en, NULL);
230 static int mlx5e_dcbnl_ieee_setpfc(struct net_device *dev,
231 struct ieee_pfc *pfc)
233 struct mlx5e_priv *priv = netdev_priv(dev);
234 struct mlx5_core_dev *mdev = priv->mdev;
238 mlx5_query_port_pfc(mdev, &curr_pfc_en, NULL);
240 if (pfc->pfc_en == curr_pfc_en)
243 ret = mlx5_set_port_pfc(mdev, pfc->pfc_en, pfc->pfc_en);
244 mlx5_toggle_port_link(mdev);
249 static u8 mlx5e_dcbnl_getdcbx(struct net_device *dev)
251 return DCB_CAP_DCBX_HOST |
252 DCB_CAP_DCBX_VER_IEEE |
253 DCB_CAP_DCBX_VER_CEE;
256 static u8 mlx5e_dcbnl_setdcbx(struct net_device *dev, u8 mode)
258 if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
259 !(mode & DCB_CAP_DCBX_VER_CEE) ||
260 !(mode & DCB_CAP_DCBX_VER_IEEE) ||
261 !(mode & DCB_CAP_DCBX_HOST))
267 static int mlx5e_dcbnl_ieee_getmaxrate(struct net_device *netdev,
268 struct ieee_maxrate *maxrate)
270 struct mlx5e_priv *priv = netdev_priv(netdev);
271 struct mlx5_core_dev *mdev = priv->mdev;
272 u8 max_bw_value[IEEE_8021QAZ_MAX_TCS];
273 u8 max_bw_unit[IEEE_8021QAZ_MAX_TCS];
277 err = mlx5_query_port_ets_rate_limit(mdev, max_bw_value, max_bw_unit);
281 memset(maxrate->tc_maxrate, 0, sizeof(maxrate->tc_maxrate));
283 for (i = 0; i <= mlx5_max_tc(mdev); i++) {
284 switch (max_bw_unit[i]) {
285 case MLX5_100_MBPS_UNIT:
286 maxrate->tc_maxrate[i] = max_bw_value[i] * MLX5E_100MB;
289 maxrate->tc_maxrate[i] = max_bw_value[i] * MLX5E_1GB;
291 case MLX5_BW_NO_LIMIT:
294 WARN(true, "non-supported BW unit");
302 static int mlx5e_dcbnl_ieee_setmaxrate(struct net_device *netdev,
303 struct ieee_maxrate *maxrate)
305 struct mlx5e_priv *priv = netdev_priv(netdev);
306 struct mlx5_core_dev *mdev = priv->mdev;
307 u8 max_bw_value[IEEE_8021QAZ_MAX_TCS];
308 u8 max_bw_unit[IEEE_8021QAZ_MAX_TCS];
309 __u64 upper_limit_mbps = roundup(255 * MLX5E_100MB, MLX5E_1GB);
312 memset(max_bw_value, 0, sizeof(max_bw_value));
313 memset(max_bw_unit, 0, sizeof(max_bw_unit));
315 for (i = 0; i <= mlx5_max_tc(mdev); i++) {
316 if (!maxrate->tc_maxrate[i]) {
317 max_bw_unit[i] = MLX5_BW_NO_LIMIT;
320 if (maxrate->tc_maxrate[i] < upper_limit_mbps) {
321 max_bw_value[i] = div_u64(maxrate->tc_maxrate[i],
323 max_bw_value[i] = max_bw_value[i] ? max_bw_value[i] : 1;
324 max_bw_unit[i] = MLX5_100_MBPS_UNIT;
326 max_bw_value[i] = div_u64(maxrate->tc_maxrate[i],
328 max_bw_unit[i] = MLX5_GBPS_UNIT;
332 return mlx5_modify_port_ets_rate_limit(mdev, max_bw_value, max_bw_unit);
335 static u8 mlx5e_dcbnl_setall(struct net_device *netdev)
337 struct mlx5e_priv *priv = netdev_priv(netdev);
338 struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
339 struct mlx5_core_dev *mdev = priv->mdev;
345 if (!MLX5_CAP_GEN(mdev, ets))
348 memset(&ets, 0, sizeof(ets));
349 memset(&pfc, 0, sizeof(pfc));
351 ets.ets_cap = IEEE_8021QAZ_MAX_TCS;
352 for (i = 0; i < CEE_DCBX_MAX_PGS; i++) {
353 ets.tc_tx_bw[i] = cee_cfg->pg_bw_pct[i];
354 ets.tc_rx_bw[i] = cee_cfg->pg_bw_pct[i];
355 ets.tc_tsa[i] = IEEE_8021QAZ_TSA_ETS;
356 ets.prio_tc[i] = cee_cfg->prio_to_pg_map[i];
359 err = mlx5e_dbcnl_validate_ets(netdev, &ets);
362 "%s, Failed to validate ETS: %d\n", __func__, err);
366 err = mlx5e_dcbnl_ieee_setets_core(priv, &ets);
369 "%s, Failed to set ETS: %d\n", __func__, err);
374 pfc.pfc_cap = mlx5_max_tc(mdev) + 1;
375 if (!cee_cfg->pfc_enable)
378 for (i = 0; i < CEE_DCBX_MAX_PRIO; i++)
379 pfc.pfc_en |= cee_cfg->pfc_setting[i] << i;
381 err = mlx5e_dcbnl_ieee_setpfc(netdev, &pfc);
384 "%s, Failed to set PFC: %d\n", __func__, err);
388 return err ? MLX5_DCB_NO_CHG : MLX5_DCB_CHG_RESET;
391 static u8 mlx5e_dcbnl_getstate(struct net_device *netdev)
393 return MLX5E_CEE_STATE_UP;
396 static void mlx5e_dcbnl_getpermhwaddr(struct net_device *netdev,
399 struct mlx5e_priv *priv = netdev_priv(netdev);
404 mlx5_query_nic_vport_mac_address(priv->mdev, 0, perm_addr);
407 static void mlx5e_dcbnl_setpgtccfgtx(struct net_device *netdev,
408 int priority, u8 prio_type,
409 u8 pgid, u8 bw_pct, u8 up_map)
411 struct mlx5e_priv *priv = netdev_priv(netdev);
412 struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
414 if (priority >= CEE_DCBX_MAX_PRIO) {
416 "%s, priority is out of range\n", __func__);
420 if (pgid >= CEE_DCBX_MAX_PGS) {
422 "%s, priority group is out of range\n", __func__);
426 cee_cfg->prio_to_pg_map[priority] = pgid;
429 static void mlx5e_dcbnl_setpgbwgcfgtx(struct net_device *netdev,
432 struct mlx5e_priv *priv = netdev_priv(netdev);
433 struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
435 if (pgid >= CEE_DCBX_MAX_PGS) {
437 "%s, priority group is out of range\n", __func__);
441 cee_cfg->pg_bw_pct[pgid] = bw_pct;
444 static void mlx5e_dcbnl_getpgtccfgtx(struct net_device *netdev,
445 int priority, u8 *prio_type,
446 u8 *pgid, u8 *bw_pct, u8 *up_map)
448 struct mlx5e_priv *priv = netdev_priv(netdev);
449 struct mlx5_core_dev *mdev = priv->mdev;
451 if (priority >= CEE_DCBX_MAX_PRIO) {
453 "%s, priority is out of range\n", __func__);
461 if (mlx5_query_port_prio_tc(mdev, priority, pgid))
465 static void mlx5e_dcbnl_getpgbwgcfgtx(struct net_device *netdev,
466 int pgid, u8 *bw_pct)
468 struct mlx5e_priv *priv = netdev_priv(netdev);
469 struct mlx5_core_dev *mdev = priv->mdev;
471 if (pgid >= CEE_DCBX_MAX_PGS) {
473 "%s, priority group is out of range\n", __func__);
477 if (mlx5_query_port_tc_bw_alloc(mdev, pgid, bw_pct))
481 static void mlx5e_dcbnl_setpfccfg(struct net_device *netdev,
482 int priority, u8 setting)
484 struct mlx5e_priv *priv = netdev_priv(netdev);
485 struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
487 if (priority >= CEE_DCBX_MAX_PRIO) {
489 "%s, priority is out of range\n", __func__);
496 cee_cfg->pfc_setting[priority] = setting;
500 mlx5e_dcbnl_get_priority_pfc(struct net_device *netdev,
501 int priority, u8 *setting)
506 err = mlx5e_dcbnl_ieee_getpfc(netdev, &pfc);
511 *setting = (pfc.pfc_en >> priority) & 0x01;
516 static void mlx5e_dcbnl_getpfccfg(struct net_device *netdev,
517 int priority, u8 *setting)
519 if (priority >= CEE_DCBX_MAX_PRIO) {
521 "%s, priority is out of range\n", __func__);
528 mlx5e_dcbnl_get_priority_pfc(netdev, priority, setting);
531 static u8 mlx5e_dcbnl_getcap(struct net_device *netdev,
534 struct mlx5e_priv *priv = netdev_priv(netdev);
535 struct mlx5_core_dev *mdev = priv->mdev;
539 case DCB_CAP_ATTR_PG:
542 case DCB_CAP_ATTR_PFC:
545 case DCB_CAP_ATTR_UP2TC:
548 case DCB_CAP_ATTR_PG_TCS:
549 *cap = 1 << mlx5_max_tc(mdev);
551 case DCB_CAP_ATTR_PFC_TCS:
552 *cap = 1 << mlx5_max_tc(mdev);
554 case DCB_CAP_ATTR_GSP:
557 case DCB_CAP_ATTR_BCN:
560 case DCB_CAP_ATTR_DCBX:
561 *cap = (DCB_CAP_DCBX_LLD_MANAGED |
562 DCB_CAP_DCBX_VER_CEE |
563 DCB_CAP_DCBX_STATIC);
574 static int mlx5e_dcbnl_getnumtcs(struct net_device *netdev,
577 struct mlx5e_priv *priv = netdev_priv(netdev);
578 struct mlx5_core_dev *mdev = priv->mdev;
581 case DCB_NUMTCS_ATTR_PG:
582 case DCB_NUMTCS_ATTR_PFC:
583 *num = mlx5_max_tc(mdev) + 1;
592 static u8 mlx5e_dcbnl_getpfcstate(struct net_device *netdev)
596 if (mlx5e_dcbnl_ieee_getpfc(netdev, &pfc))
597 return MLX5E_CEE_STATE_DOWN;
599 return pfc.pfc_en ? MLX5E_CEE_STATE_UP : MLX5E_CEE_STATE_DOWN;
602 static void mlx5e_dcbnl_setpfcstate(struct net_device *netdev, u8 state)
604 struct mlx5e_priv *priv = netdev_priv(netdev);
605 struct mlx5e_cee_config *cee_cfg = &priv->dcbx.cee_cfg;
607 if ((state != MLX5E_CEE_STATE_UP) && (state != MLX5E_CEE_STATE_DOWN))
610 cee_cfg->pfc_enable = state;
613 const struct dcbnl_rtnl_ops mlx5e_dcbnl_ops = {
614 .ieee_getets = mlx5e_dcbnl_ieee_getets,
615 .ieee_setets = mlx5e_dcbnl_ieee_setets,
616 .ieee_getmaxrate = mlx5e_dcbnl_ieee_getmaxrate,
617 .ieee_setmaxrate = mlx5e_dcbnl_ieee_setmaxrate,
618 .ieee_getpfc = mlx5e_dcbnl_ieee_getpfc,
619 .ieee_setpfc = mlx5e_dcbnl_ieee_setpfc,
620 .getdcbx = mlx5e_dcbnl_getdcbx,
621 .setdcbx = mlx5e_dcbnl_setdcbx,
624 .setall = mlx5e_dcbnl_setall,
625 .getstate = mlx5e_dcbnl_getstate,
626 .getpermhwaddr = mlx5e_dcbnl_getpermhwaddr,
628 .setpgtccfgtx = mlx5e_dcbnl_setpgtccfgtx,
629 .setpgbwgcfgtx = mlx5e_dcbnl_setpgbwgcfgtx,
630 .getpgtccfgtx = mlx5e_dcbnl_getpgtccfgtx,
631 .getpgbwgcfgtx = mlx5e_dcbnl_getpgbwgcfgtx,
633 .setpfccfg = mlx5e_dcbnl_setpfccfg,
634 .getpfccfg = mlx5e_dcbnl_getpfccfg,
635 .getcap = mlx5e_dcbnl_getcap,
636 .getnumtcs = mlx5e_dcbnl_getnumtcs,
637 .getpfcstate = mlx5e_dcbnl_getpfcstate,
638 .setpfcstate = mlx5e_dcbnl_setpfcstate,