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 static int mlx5e_dcbnl_ieee_getets(struct net_device *netdev,
41 struct mlx5e_priv *priv = netdev_priv(netdev);
43 if (!MLX5_CAP_GEN(priv->mdev, ets))
46 memcpy(ets, &priv->params.ets, sizeof(*ets));
51 MLX5E_VENDOR_TC_GROUP_NUM = 7,
52 MLX5E_ETS_TC_GROUP_NUM = 0,
55 static void mlx5e_build_tc_group(struct ieee_ets *ets, u8 *tc_group, int max_tc)
57 bool any_tc_mapped_to_ets = false;
61 for (i = 0; i <= max_tc; i++)
62 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS)
63 any_tc_mapped_to_ets = true;
65 strict_group = any_tc_mapped_to_ets ? 1 : 0;
67 for (i = 0; i <= max_tc; i++) {
68 switch (ets->tc_tsa[i]) {
69 case IEEE_8021QAZ_TSA_VENDOR:
70 tc_group[i] = MLX5E_VENDOR_TC_GROUP_NUM;
72 case IEEE_8021QAZ_TSA_STRICT:
73 tc_group[i] = strict_group++;
75 case IEEE_8021QAZ_TSA_ETS:
76 tc_group[i] = MLX5E_ETS_TC_GROUP_NUM;
82 static void mlx5e_build_tc_tx_bw(struct ieee_ets *ets, u8 *tc_tx_bw,
83 u8 *tc_group, int max_tc)
87 for (i = 0; i <= max_tc; i++) {
88 switch (ets->tc_tsa[i]) {
89 case IEEE_8021QAZ_TSA_VENDOR:
90 tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
92 case IEEE_8021QAZ_TSA_STRICT:
93 tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
95 case IEEE_8021QAZ_TSA_ETS:
96 tc_tx_bw[i] = ets->tc_tx_bw[i] ?: MLX5E_MIN_BW_ALLOC;
102 int mlx5e_dcbnl_ieee_setets_core(struct mlx5e_priv *priv, struct ieee_ets *ets)
104 struct mlx5_core_dev *mdev = priv->mdev;
105 u8 tc_tx_bw[IEEE_8021QAZ_MAX_TCS];
106 u8 tc_group[IEEE_8021QAZ_MAX_TCS];
107 int max_tc = mlx5_max_tc(mdev);
110 if (!MLX5_CAP_GEN(mdev, ets))
113 mlx5e_build_tc_group(ets, tc_group, max_tc);
114 mlx5e_build_tc_tx_bw(ets, tc_tx_bw, tc_group, max_tc);
116 err = mlx5_set_port_prio_tc(mdev, ets->prio_tc);
120 err = mlx5_set_port_tc_group(mdev, tc_group);
124 return mlx5_set_port_tc_bw_alloc(mdev, tc_tx_bw);
127 static int mlx5e_dbcnl_validate_ets(struct ieee_ets *ets)
132 /* Validate Priority */
133 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
134 if (ets->prio_tc[i] >= MLX5E_MAX_PRIORITY)
138 /* Validate Bandwidth Sum */
139 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
140 if (ets->tc_tsa[i] == IEEE_8021QAZ_TSA_ETS)
141 bw_sum += ets->tc_tx_bw[i];
144 if (bw_sum != 0 && bw_sum != 100)
149 static int mlx5e_dcbnl_ieee_setets(struct net_device *netdev,
150 struct ieee_ets *ets)
152 struct mlx5e_priv *priv = netdev_priv(netdev);
155 err = mlx5e_dbcnl_validate_ets(ets);
159 err = mlx5e_dcbnl_ieee_setets_core(priv, ets);
163 memcpy(&priv->params.ets, ets, sizeof(*ets));
164 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
169 static u8 mlx5e_dcbnl_getdcbx(struct net_device *dev)
171 return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
174 static u8 mlx5e_dcbnl_setdcbx(struct net_device *dev, u8 mode)
176 if ((mode & DCB_CAP_DCBX_LLD_MANAGED) ||
177 (mode & DCB_CAP_DCBX_VER_CEE) ||
178 !(mode & DCB_CAP_DCBX_VER_IEEE) ||
179 !(mode & DCB_CAP_DCBX_HOST))
185 const struct dcbnl_rtnl_ops mlx5e_dcbnl_ops = {
186 .ieee_getets = mlx5e_dcbnl_ieee_getets,
187 .ieee_setets = mlx5e_dcbnl_ieee_setets,
188 .getdcbx = mlx5e_dcbnl_getdcbx,
189 .setdcbx = mlx5e_dcbnl_setdcbx,