xref: /freebsd/sys/dev/axgbe/xgbe-dcb.c (revision 685dc743)
17113afc8SEmmanuel Vadot /*
27113afc8SEmmanuel Vadot  * AMD 10Gb Ethernet driver
37113afc8SEmmanuel Vadot  *
47113afc8SEmmanuel Vadot  * This file is available to you under your choice of the following two
57113afc8SEmmanuel Vadot  * licenses:
67113afc8SEmmanuel Vadot  *
77113afc8SEmmanuel Vadot  * License 1: GPLv2
87113afc8SEmmanuel Vadot  *
97113afc8SEmmanuel Vadot  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
107113afc8SEmmanuel Vadot  *
117113afc8SEmmanuel Vadot  * This file is free software; you may copy, redistribute and/or modify
127113afc8SEmmanuel Vadot  * it under the terms of the GNU General Public License as published by
137113afc8SEmmanuel Vadot  * the Free Software Foundation, either version 2 of the License, or (at
147113afc8SEmmanuel Vadot  * your option) any later version.
157113afc8SEmmanuel Vadot  *
167113afc8SEmmanuel Vadot  * This file is distributed in the hope that it will be useful, but
177113afc8SEmmanuel Vadot  * WITHOUT ANY WARRANTY; without even the implied warranty of
187113afc8SEmmanuel Vadot  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
197113afc8SEmmanuel Vadot  * General Public License for more details.
207113afc8SEmmanuel Vadot  *
217113afc8SEmmanuel Vadot  * You should have received a copy of the GNU General Public License
227113afc8SEmmanuel Vadot  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
237113afc8SEmmanuel Vadot  *
247113afc8SEmmanuel Vadot  * This file incorporates work covered by the following copyright and
257113afc8SEmmanuel Vadot  * permission notice:
267113afc8SEmmanuel Vadot  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
277113afc8SEmmanuel Vadot  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
287113afc8SEmmanuel Vadot  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
297113afc8SEmmanuel Vadot  *     and you.
307113afc8SEmmanuel Vadot  *
317113afc8SEmmanuel Vadot  *     The Software IS NOT an item of Licensed Software or Licensed Product
327113afc8SEmmanuel Vadot  *     under any End User Software License Agreement or Agreement for Licensed
337113afc8SEmmanuel Vadot  *     Product with Synopsys or any supplement thereto.  Permission is hereby
347113afc8SEmmanuel Vadot  *     granted, free of charge, to any person obtaining a copy of this software
357113afc8SEmmanuel Vadot  *     annotated with this license and the Software, to deal in the Software
367113afc8SEmmanuel Vadot  *     without restriction, including without limitation the rights to use,
377113afc8SEmmanuel Vadot  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
387113afc8SEmmanuel Vadot  *     of the Software, and to permit persons to whom the Software is furnished
397113afc8SEmmanuel Vadot  *     to do so, subject to the following conditions:
407113afc8SEmmanuel Vadot  *
417113afc8SEmmanuel Vadot  *     The above copyright notice and this permission notice shall be included
427113afc8SEmmanuel Vadot  *     in all copies or substantial portions of the Software.
437113afc8SEmmanuel Vadot  *
447113afc8SEmmanuel Vadot  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
457113afc8SEmmanuel Vadot  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
467113afc8SEmmanuel Vadot  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
477113afc8SEmmanuel Vadot  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
487113afc8SEmmanuel Vadot  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
497113afc8SEmmanuel Vadot  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
507113afc8SEmmanuel Vadot  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
517113afc8SEmmanuel Vadot  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
527113afc8SEmmanuel Vadot  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
537113afc8SEmmanuel Vadot  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
547113afc8SEmmanuel Vadot  *     THE POSSIBILITY OF SUCH DAMAGE.
557113afc8SEmmanuel Vadot  *
567113afc8SEmmanuel Vadot  *
577113afc8SEmmanuel Vadot  * License 2: Modified BSD
587113afc8SEmmanuel Vadot  *
597113afc8SEmmanuel Vadot  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
607113afc8SEmmanuel Vadot  * All rights reserved.
617113afc8SEmmanuel Vadot  *
627113afc8SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
637113afc8SEmmanuel Vadot  * modification, are permitted provided that the following conditions are met:
647113afc8SEmmanuel Vadot  *     * Redistributions of source code must retain the above copyright
657113afc8SEmmanuel Vadot  *       notice, this list of conditions and the following disclaimer.
667113afc8SEmmanuel Vadot  *     * Redistributions in binary form must reproduce the above copyright
677113afc8SEmmanuel Vadot  *       notice, this list of conditions and the following disclaimer in the
687113afc8SEmmanuel Vadot  *       documentation and/or other materials provided with the distribution.
697113afc8SEmmanuel Vadot  *     * Neither the name of Advanced Micro Devices, Inc. nor the
707113afc8SEmmanuel Vadot  *       names of its contributors may be used to endorse or promote products
717113afc8SEmmanuel Vadot  *       derived from this software without specific prior written permission.
727113afc8SEmmanuel Vadot  *
737113afc8SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
747113afc8SEmmanuel Vadot  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
757113afc8SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
767113afc8SEmmanuel Vadot  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
777113afc8SEmmanuel Vadot  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
787113afc8SEmmanuel Vadot  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
797113afc8SEmmanuel Vadot  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
807113afc8SEmmanuel Vadot  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
817113afc8SEmmanuel Vadot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
827113afc8SEmmanuel Vadot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
837113afc8SEmmanuel Vadot  *
847113afc8SEmmanuel Vadot  * This file incorporates work covered by the following copyright and
857113afc8SEmmanuel Vadot  * permission notice:
867113afc8SEmmanuel Vadot  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
877113afc8SEmmanuel Vadot  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
887113afc8SEmmanuel Vadot  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
897113afc8SEmmanuel Vadot  *     and you.
907113afc8SEmmanuel Vadot  *
917113afc8SEmmanuel Vadot  *     The Software IS NOT an item of Licensed Software or Licensed Product
927113afc8SEmmanuel Vadot  *     under any End User Software License Agreement or Agreement for Licensed
937113afc8SEmmanuel Vadot  *     Product with Synopsys or any supplement thereto.  Permission is hereby
947113afc8SEmmanuel Vadot  *     granted, free of charge, to any person obtaining a copy of this software
957113afc8SEmmanuel Vadot  *     annotated with this license and the Software, to deal in the Software
967113afc8SEmmanuel Vadot  *     without restriction, including without limitation the rights to use,
977113afc8SEmmanuel Vadot  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
987113afc8SEmmanuel Vadot  *     of the Software, and to permit persons to whom the Software is furnished
997113afc8SEmmanuel Vadot  *     to do so, subject to the following conditions:
1007113afc8SEmmanuel Vadot  *
1017113afc8SEmmanuel Vadot  *     The above copyright notice and this permission notice shall be included
1027113afc8SEmmanuel Vadot  *     in all copies or substantial portions of the Software.
1037113afc8SEmmanuel Vadot  *
1047113afc8SEmmanuel Vadot  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
1057113afc8SEmmanuel Vadot  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1067113afc8SEmmanuel Vadot  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1077113afc8SEmmanuel Vadot  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
1087113afc8SEmmanuel Vadot  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1097113afc8SEmmanuel Vadot  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1107113afc8SEmmanuel Vadot  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1117113afc8SEmmanuel Vadot  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1127113afc8SEmmanuel Vadot  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1137113afc8SEmmanuel Vadot  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
1147113afc8SEmmanuel Vadot  *     THE POSSIBILITY OF SUCH DAMAGE.
1157113afc8SEmmanuel Vadot  */
1167113afc8SEmmanuel Vadot #include <sys/cdefs.h>
1177113afc8SEmmanuel Vadot #include "xgbe.h"
1187113afc8SEmmanuel Vadot 
1197113afc8SEmmanuel Vadot #if 0
1207113afc8SEmmanuel Vadot static int xgbe_dcb_ieee_getets(struct xgbe_prv_data *pdata,
1217113afc8SEmmanuel Vadot 				struct ieee_ets *ets)
1227113afc8SEmmanuel Vadot {
1237113afc8SEmmanuel Vadot 	/* Set number of supported traffic classes */
1247113afc8SEmmanuel Vadot 	ets->ets_cap = pdata->hw_feat.tc_cnt;
1257113afc8SEmmanuel Vadot 
1267113afc8SEmmanuel Vadot 	if (pdata->ets) {
1277113afc8SEmmanuel Vadot 		ets->cbs = pdata->ets->cbs;
1287113afc8SEmmanuel Vadot 		memcpy(ets->tc_tx_bw, pdata->ets->tc_tx_bw,
1297113afc8SEmmanuel Vadot 		       sizeof(ets->tc_tx_bw));
1307113afc8SEmmanuel Vadot 		memcpy(ets->tc_tsa, pdata->ets->tc_tsa,
1317113afc8SEmmanuel Vadot 		       sizeof(ets->tc_tsa));
1327113afc8SEmmanuel Vadot 		memcpy(ets->prio_tc, pdata->ets->prio_tc,
1337113afc8SEmmanuel Vadot 		       sizeof(ets->prio_tc));
1347113afc8SEmmanuel Vadot 	}
1357113afc8SEmmanuel Vadot 
1367113afc8SEmmanuel Vadot 	return (0);
1377113afc8SEmmanuel Vadot }
1387113afc8SEmmanuel Vadot 
1397113afc8SEmmanuel Vadot static int xgbe_dcb_ieee_setets(struct xgbe_prv_data *pdata,
1407113afc8SEmmanuel Vadot 				struct ieee_ets *ets)
1417113afc8SEmmanuel Vadot {
1427113afc8SEmmanuel Vadot 	unsigned int i, tc_ets, tc_ets_weight;
1437113afc8SEmmanuel Vadot 	u8 max_tc = 0;
1447113afc8SEmmanuel Vadot 
1457113afc8SEmmanuel Vadot 	tc_ets = 0;
1467113afc8SEmmanuel Vadot 	tc_ets_weight = 0;
1477113afc8SEmmanuel Vadot 	for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1487113afc8SEmmanuel Vadot 		axgbe_printf(1,
1497113afc8SEmmanuel Vadot 			  "TC%u: tx_bw=%hhu, rx_bw=%hhu, tsa=%hhu\n", i,
1507113afc8SEmmanuel Vadot 			  ets->tc_tx_bw[i], ets->tc_rx_bw[i],
1517113afc8SEmmanuel Vadot 			  ets->tc_tsa[i]);
1527113afc8SEmmanuel Vadot 		axgbe_printf(1, "PRIO%u: TC=%hhu\n", i,
1537113afc8SEmmanuel Vadot 			  ets->prio_tc[i]);
1547113afc8SEmmanuel Vadot 
1557113afc8SEmmanuel Vadot 		max_tc = max_t(u8, max_tc, ets->prio_tc[i]);
1567113afc8SEmmanuel Vadot 		if ((ets->tc_tx_bw[i] || ets->tc_tsa[i]))
1577113afc8SEmmanuel Vadot 			max_tc = max_t(u8, max_tc, i);
1587113afc8SEmmanuel Vadot 
1597113afc8SEmmanuel Vadot 		switch (ets->tc_tsa[i]) {
1607113afc8SEmmanuel Vadot 		case IEEE_8021QAZ_TSA_STRICT:
1617113afc8SEmmanuel Vadot 			break;
1627113afc8SEmmanuel Vadot 		case IEEE_8021QAZ_TSA_ETS:
1637113afc8SEmmanuel Vadot 			tc_ets = 1;
1647113afc8SEmmanuel Vadot 			tc_ets_weight += ets->tc_tx_bw[i];
1657113afc8SEmmanuel Vadot 			break;
1667113afc8SEmmanuel Vadot 		default:
1677113afc8SEmmanuel Vadot 			axgbe_error(
1687113afc8SEmmanuel Vadot 				  "unsupported TSA algorithm (%hhu)\n",
1697113afc8SEmmanuel Vadot 				  ets->tc_tsa[i]);
1707113afc8SEmmanuel Vadot 			return (-EINVAL);
1717113afc8SEmmanuel Vadot 		}
1727113afc8SEmmanuel Vadot 	}
1737113afc8SEmmanuel Vadot 
1747113afc8SEmmanuel Vadot 	/* Check maximum traffic class requested */
1757113afc8SEmmanuel Vadot 	if (max_tc >= pdata->hw_feat.tc_cnt) {
1767113afc8SEmmanuel Vadot 		axgbe_error(
1777113afc8SEmmanuel Vadot 			  "exceeded number of supported traffic classes\n");
1787113afc8SEmmanuel Vadot 		return (-EINVAL);
1797113afc8SEmmanuel Vadot 	}
1807113afc8SEmmanuel Vadot 
1817113afc8SEmmanuel Vadot 	/* Weights must add up to 100% */
1827113afc8SEmmanuel Vadot 	if (tc_ets && (tc_ets_weight != 100)) {
1837113afc8SEmmanuel Vadot 		axgbe_error(
1847113afc8SEmmanuel Vadot 			  "sum of ETS algorithm weights is not 100 (%u)\n",
1857113afc8SEmmanuel Vadot 			  tc_ets_weight);
1867113afc8SEmmanuel Vadot 		return (-EINVAL);
1877113afc8SEmmanuel Vadot 	}
1887113afc8SEmmanuel Vadot 
1897113afc8SEmmanuel Vadot 	if (!pdata->ets) {
1907113afc8SEmmanuel Vadot 		pdata->ets = (struct ieee_ets *)malloc(sizeof(struct ieee_ets),
1917113afc8SEmmanuel Vadot                     M_AXGBE, M_NOWAIT); //TODO - when to free?
1927113afc8SEmmanuel Vadot 
1937113afc8SEmmanuel Vadot 		if (!pdata->ets)
1947113afc8SEmmanuel Vadot 			return (-ENOMEM);
1957113afc8SEmmanuel Vadot 	}
1967113afc8SEmmanuel Vadot 
1977113afc8SEmmanuel Vadot 	pdata->num_tcs = max_tc + 1;
1987113afc8SEmmanuel Vadot 	memcpy(pdata->ets, ets, sizeof(*pdata->ets));
1997113afc8SEmmanuel Vadot 
2007113afc8SEmmanuel Vadot 	pdata->hw_if.config_dcb_tc(pdata);
2017113afc8SEmmanuel Vadot 
2027113afc8SEmmanuel Vadot 	return (0);
2037113afc8SEmmanuel Vadot }
2047113afc8SEmmanuel Vadot 
2057113afc8SEmmanuel Vadot static int xgbe_dcb_ieee_getpfc(struct xgbe_prv_data *pdata,
2067113afc8SEmmanuel Vadot 				struct ieee_pfc *pfc)
2077113afc8SEmmanuel Vadot {
2087113afc8SEmmanuel Vadot 
2097113afc8SEmmanuel Vadot 	/* Set number of supported PFC traffic classes */
2107113afc8SEmmanuel Vadot 	pfc->pfc_cap = pdata->hw_feat.tc_cnt;
2117113afc8SEmmanuel Vadot 
2127113afc8SEmmanuel Vadot 	if (pdata->pfc) {
2137113afc8SEmmanuel Vadot 		pfc->pfc_en = pdata->pfc->pfc_en;
2147113afc8SEmmanuel Vadot 		pfc->mbc = pdata->pfc->mbc;
2157113afc8SEmmanuel Vadot 		pfc->delay = pdata->pfc->delay;
2167113afc8SEmmanuel Vadot 	}
2177113afc8SEmmanuel Vadot 
2187113afc8SEmmanuel Vadot 	return (0);
2197113afc8SEmmanuel Vadot }
2207113afc8SEmmanuel Vadot 
2217113afc8SEmmanuel Vadot static int xgbe_dcb_ieee_setpfc(struct xgbe_prv_data *pdata,
2227113afc8SEmmanuel Vadot 				struct ieee_pfc *pfc)
2237113afc8SEmmanuel Vadot {
2247113afc8SEmmanuel Vadot 
2257113afc8SEmmanuel Vadot 	axgbe_printf(1,
2267113afc8SEmmanuel Vadot 		  "cap=%hhu, en=%#hhx, mbc=%hhu, delay=%d\n",
2277113afc8SEmmanuel Vadot 		  pfc->pfc_cap, pfc->pfc_en, pfc->mbc, pfc->delay);
2287113afc8SEmmanuel Vadot 
2297113afc8SEmmanuel Vadot 	/* Check PFC for supported number of traffic classes */
2307113afc8SEmmanuel Vadot 	if (pfc->pfc_en & ~((1 << pdata->hw_feat.tc_cnt) - 1)) {
2317113afc8SEmmanuel Vadot 		axgbe_error(
2327113afc8SEmmanuel Vadot 			  "PFC requested for unsupported traffic class\n");
2337113afc8SEmmanuel Vadot 		return (-EINVAL);
2347113afc8SEmmanuel Vadot 	}
2357113afc8SEmmanuel Vadot 
2367113afc8SEmmanuel Vadot 	if (!pdata->pfc) {
2377113afc8SEmmanuel Vadot 		pdata->pfc = (struct ieee_pfc *)malloc(sizeof(struct ieee_pfc),
2387113afc8SEmmanuel Vadot 		    M_AXGBE, M_NOWAIT); //TODO - when to free?
2397113afc8SEmmanuel Vadot 
2407113afc8SEmmanuel Vadot 		if (!pdata->pfc)
2417113afc8SEmmanuel Vadot 			return (-ENOMEM);
2427113afc8SEmmanuel Vadot 	}
2437113afc8SEmmanuel Vadot 
2447113afc8SEmmanuel Vadot 	memcpy(pdata->pfc, pfc, sizeof(*pdata->pfc));
2457113afc8SEmmanuel Vadot 
2467113afc8SEmmanuel Vadot 	pdata->hw_if.config_dcb_pfc(pdata);
2477113afc8SEmmanuel Vadot 
2487113afc8SEmmanuel Vadot 	return (0);
2497113afc8SEmmanuel Vadot }
2507113afc8SEmmanuel Vadot 
2517113afc8SEmmanuel Vadot static u8 xgbe_dcb_getdcbx(struct xgbe_prv_data *pdata)
2527113afc8SEmmanuel Vadot {
2537113afc8SEmmanuel Vadot 	return (DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE);
2547113afc8SEmmanuel Vadot }
2557113afc8SEmmanuel Vadot 
2567113afc8SEmmanuel Vadot static u8 xgbe_dcb_setdcbx(struct xgbe_prv_data *pdata, u8 dcbx)
2577113afc8SEmmanuel Vadot {
2587113afc8SEmmanuel Vadot 	u8 support = xgbe_dcb_getdcbx(pdata);
2597113afc8SEmmanuel Vadot 
2607113afc8SEmmanuel Vadot 	axgbe_printf(1, "DCBX=%#hhx\n", dcbx);
2617113afc8SEmmanuel Vadot 
2627113afc8SEmmanuel Vadot 	if (dcbx & ~support)
2637113afc8SEmmanuel Vadot 		return (1);
2647113afc8SEmmanuel Vadot 
2657113afc8SEmmanuel Vadot 	if ((dcbx & support) != support)
2667113afc8SEmmanuel Vadot 		return (1);
2677113afc8SEmmanuel Vadot 
2687113afc8SEmmanuel Vadot 	return (0);
2697113afc8SEmmanuel Vadot }
2707113afc8SEmmanuel Vadot #endif
271