162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * AMD 10Gb Ethernet driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This file is available to you under your choice of the following two
562306a36Sopenharmony_ci * licenses:
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * License 1: GPLv2
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * This file is free software; you may copy, redistribute and/or modify
1262306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
1362306a36Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or (at
1462306a36Sopenharmony_ci * your option) any later version.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
1762306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1862306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1962306a36Sopenharmony_ci * General Public License for more details.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2262306a36Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
2562306a36Sopenharmony_ci * permission notice:
2662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
2762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
2862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
2962306a36Sopenharmony_ci *     and you.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
3262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
3362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
3462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
3562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
3662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
3762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
3862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
3962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
4262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
4562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
4762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
4862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
5462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * License 2: Modified BSD
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
6062306a36Sopenharmony_ci * All rights reserved.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
6362306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
6462306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
6562306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer.
6662306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
6762306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer in the
6862306a36Sopenharmony_ci *       documentation and/or other materials provided with the distribution.
6962306a36Sopenharmony_ci *     * Neither the name of Advanced Micro Devices, Inc. nor the
7062306a36Sopenharmony_ci *       names of its contributors may be used to endorse or promote products
7162306a36Sopenharmony_ci *       derived from this software without specific prior written permission.
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
7462306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7562306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7662306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
7762306a36Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7862306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7962306a36Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
8062306a36Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8162306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8262306a36Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
8562306a36Sopenharmony_ci * permission notice:
8662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
8762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
8862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
8962306a36Sopenharmony_ci *     and you.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
9262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
9362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
9462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
9562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
9662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
9762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
9962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
10262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
10762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
10862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
11062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
11162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
11262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
11362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
11462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci#include <linux/netdevice.h>
11862306a36Sopenharmony_ci#include <net/dcbnl.h>
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci#include "xgbe.h"
12162306a36Sopenharmony_ci#include "xgbe-common.h"
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic int xgbe_dcb_ieee_getets(struct net_device *netdev,
12462306a36Sopenharmony_ci				struct ieee_ets *ets)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	/* Set number of supported traffic classes */
12962306a36Sopenharmony_ci	ets->ets_cap = pdata->hw_feat.tc_cnt;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	if (pdata->ets) {
13262306a36Sopenharmony_ci		ets->cbs = pdata->ets->cbs;
13362306a36Sopenharmony_ci		memcpy(ets->tc_tx_bw, pdata->ets->tc_tx_bw,
13462306a36Sopenharmony_ci		       sizeof(ets->tc_tx_bw));
13562306a36Sopenharmony_ci		memcpy(ets->tc_tsa, pdata->ets->tc_tsa,
13662306a36Sopenharmony_ci		       sizeof(ets->tc_tsa));
13762306a36Sopenharmony_ci		memcpy(ets->prio_tc, pdata->ets->prio_tc,
13862306a36Sopenharmony_ci		       sizeof(ets->prio_tc));
13962306a36Sopenharmony_ci	}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	return 0;
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic int xgbe_dcb_ieee_setets(struct net_device *netdev,
14562306a36Sopenharmony_ci				struct ieee_ets *ets)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
14862306a36Sopenharmony_ci	unsigned int i, tc_ets, tc_ets_weight;
14962306a36Sopenharmony_ci	u8 max_tc = 0;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	tc_ets = 0;
15262306a36Sopenharmony_ci	tc_ets_weight = 0;
15362306a36Sopenharmony_ci	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
15462306a36Sopenharmony_ci		netif_dbg(pdata, drv, netdev,
15562306a36Sopenharmony_ci			  "TC%u: tx_bw=%hhu, rx_bw=%hhu, tsa=%hhu\n", i,
15662306a36Sopenharmony_ci			  ets->tc_tx_bw[i], ets->tc_rx_bw[i],
15762306a36Sopenharmony_ci			  ets->tc_tsa[i]);
15862306a36Sopenharmony_ci		netif_dbg(pdata, drv, netdev, "PRIO%u: TC=%hhu\n", i,
15962306a36Sopenharmony_ci			  ets->prio_tc[i]);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci		max_tc = max_t(u8, max_tc, ets->prio_tc[i]);
16262306a36Sopenharmony_ci		if ((ets->tc_tx_bw[i] || ets->tc_tsa[i]))
16362306a36Sopenharmony_ci			max_tc = max_t(u8, max_tc, i);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci		switch (ets->tc_tsa[i]) {
16662306a36Sopenharmony_ci		case IEEE_8021QAZ_TSA_STRICT:
16762306a36Sopenharmony_ci			break;
16862306a36Sopenharmony_ci		case IEEE_8021QAZ_TSA_ETS:
16962306a36Sopenharmony_ci			tc_ets = 1;
17062306a36Sopenharmony_ci			tc_ets_weight += ets->tc_tx_bw[i];
17162306a36Sopenharmony_ci			break;
17262306a36Sopenharmony_ci		default:
17362306a36Sopenharmony_ci			netif_err(pdata, drv, netdev,
17462306a36Sopenharmony_ci				  "unsupported TSA algorithm (%hhu)\n",
17562306a36Sopenharmony_ci				  ets->tc_tsa[i]);
17662306a36Sopenharmony_ci			return -EINVAL;
17762306a36Sopenharmony_ci		}
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	/* Check maximum traffic class requested */
18162306a36Sopenharmony_ci	if (max_tc >= pdata->hw_feat.tc_cnt) {
18262306a36Sopenharmony_ci		netif_err(pdata, drv, netdev,
18362306a36Sopenharmony_ci			  "exceeded number of supported traffic classes\n");
18462306a36Sopenharmony_ci		return -EINVAL;
18562306a36Sopenharmony_ci	}
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	/* Weights must add up to 100% */
18862306a36Sopenharmony_ci	if (tc_ets && (tc_ets_weight != 100)) {
18962306a36Sopenharmony_ci		netif_err(pdata, drv, netdev,
19062306a36Sopenharmony_ci			  "sum of ETS algorithm weights is not 100 (%u)\n",
19162306a36Sopenharmony_ci			  tc_ets_weight);
19262306a36Sopenharmony_ci		return -EINVAL;
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	if (!pdata->ets) {
19662306a36Sopenharmony_ci		pdata->ets = devm_kzalloc(pdata->dev, sizeof(*pdata->ets),
19762306a36Sopenharmony_ci					  GFP_KERNEL);
19862306a36Sopenharmony_ci		if (!pdata->ets)
19962306a36Sopenharmony_ci			return -ENOMEM;
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	pdata->num_tcs = max_tc + 1;
20362306a36Sopenharmony_ci	memcpy(pdata->ets, ets, sizeof(*pdata->ets));
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	pdata->hw_if.config_dcb_tc(pdata);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	return 0;
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic int xgbe_dcb_ieee_getpfc(struct net_device *netdev,
21162306a36Sopenharmony_ci				struct ieee_pfc *pfc)
21262306a36Sopenharmony_ci{
21362306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/* Set number of supported PFC traffic classes */
21662306a36Sopenharmony_ci	pfc->pfc_cap = pdata->hw_feat.tc_cnt;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	if (pdata->pfc) {
21962306a36Sopenharmony_ci		pfc->pfc_en = pdata->pfc->pfc_en;
22062306a36Sopenharmony_ci		pfc->mbc = pdata->pfc->mbc;
22162306a36Sopenharmony_ci		pfc->delay = pdata->pfc->delay;
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci	return 0;
22562306a36Sopenharmony_ci}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic int xgbe_dcb_ieee_setpfc(struct net_device *netdev,
22862306a36Sopenharmony_ci				struct ieee_pfc *pfc)
22962306a36Sopenharmony_ci{
23062306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	netif_dbg(pdata, drv, netdev,
23362306a36Sopenharmony_ci		  "cap=%d, en=%#x, mbc=%d, delay=%d\n",
23462306a36Sopenharmony_ci		  pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci	/* Check PFC for supported number of traffic classes */
23762306a36Sopenharmony_ci	if (pfc->pfc_en & ~((1 << pdata->hw_feat.tc_cnt) - 1)) {
23862306a36Sopenharmony_ci		netif_err(pdata, drv, netdev,
23962306a36Sopenharmony_ci			  "PFC requested for unsupported traffic class\n");
24062306a36Sopenharmony_ci		return -EINVAL;
24162306a36Sopenharmony_ci	}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	if (!pdata->pfc) {
24462306a36Sopenharmony_ci		pdata->pfc = devm_kzalloc(pdata->dev, sizeof(*pdata->pfc),
24562306a36Sopenharmony_ci					  GFP_KERNEL);
24662306a36Sopenharmony_ci		if (!pdata->pfc)
24762306a36Sopenharmony_ci			return -ENOMEM;
24862306a36Sopenharmony_ci	}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	memcpy(pdata->pfc, pfc, sizeof(*pdata->pfc));
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	pdata->hw_if.config_dcb_pfc(pdata);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	return 0;
25562306a36Sopenharmony_ci}
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_cistatic u8 xgbe_dcb_getdcbx(struct net_device *netdev)
25862306a36Sopenharmony_ci{
25962306a36Sopenharmony_ci	return DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic u8 xgbe_dcb_setdcbx(struct net_device *netdev, u8 dcbx)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = netdev_priv(netdev);
26562306a36Sopenharmony_ci	u8 support = xgbe_dcb_getdcbx(netdev);
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	netif_dbg(pdata, drv, netdev, "DCBX=%#hhx\n", dcbx);
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	if (dcbx & ~support)
27062306a36Sopenharmony_ci		return 1;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	if ((dcbx & support) != support)
27362306a36Sopenharmony_ci		return 1;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ci	return 0;
27662306a36Sopenharmony_ci}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_cistatic const struct dcbnl_rtnl_ops xgbe_dcbnl_ops = {
27962306a36Sopenharmony_ci	/* IEEE 802.1Qaz std */
28062306a36Sopenharmony_ci	.ieee_getets = xgbe_dcb_ieee_getets,
28162306a36Sopenharmony_ci	.ieee_setets = xgbe_dcb_ieee_setets,
28262306a36Sopenharmony_ci	.ieee_getpfc = xgbe_dcb_ieee_getpfc,
28362306a36Sopenharmony_ci	.ieee_setpfc = xgbe_dcb_ieee_setpfc,
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ci	/* DCBX configuration */
28662306a36Sopenharmony_ci	.getdcbx     = xgbe_dcb_getdcbx,
28762306a36Sopenharmony_ci	.setdcbx     = xgbe_dcb_setdcbx,
28862306a36Sopenharmony_ci};
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ciconst struct dcbnl_rtnl_ops *xgbe_get_dcbnl_ops(void)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	return &xgbe_dcbnl_ops;
29362306a36Sopenharmony_ci}
294