xref: /openbmc/linux/drivers/net/ethernet/amd/xgbe/xgbe-dev.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1c5aa9e3bSLendacky, Thomas /*
2c5aa9e3bSLendacky, Thomas  * AMD 10Gb Ethernet driver
3c5aa9e3bSLendacky, Thomas  *
4c5aa9e3bSLendacky, Thomas  * This file is available to you under your choice of the following two
5c5aa9e3bSLendacky, Thomas  * licenses:
6c5aa9e3bSLendacky, Thomas  *
7c5aa9e3bSLendacky, Thomas  * License 1: GPLv2
8c5aa9e3bSLendacky, Thomas  *
9b4eee84fSLendacky, Thomas  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
10c5aa9e3bSLendacky, Thomas  *
11c5aa9e3bSLendacky, Thomas  * This file is free software; you may copy, redistribute and/or modify
12c5aa9e3bSLendacky, Thomas  * it under the terms of the GNU General Public License as published by
13c5aa9e3bSLendacky, Thomas  * the Free Software Foundation, either version 2 of the License, or (at
14c5aa9e3bSLendacky, Thomas  * your option) any later version.
15c5aa9e3bSLendacky, Thomas  *
16c5aa9e3bSLendacky, Thomas  * This file is distributed in the hope that it will be useful, but
17c5aa9e3bSLendacky, Thomas  * WITHOUT ANY WARRANTY; without even the implied warranty of
18c5aa9e3bSLendacky, Thomas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19c5aa9e3bSLendacky, Thomas  * General Public License for more details.
20c5aa9e3bSLendacky, Thomas  *
21c5aa9e3bSLendacky, Thomas  * You should have received a copy of the GNU General Public License
22c5aa9e3bSLendacky, Thomas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23c5aa9e3bSLendacky, Thomas  *
24c5aa9e3bSLendacky, Thomas  * This file incorporates work covered by the following copyright and
25c5aa9e3bSLendacky, Thomas  * permission notice:
26c5aa9e3bSLendacky, Thomas  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27c5aa9e3bSLendacky, Thomas  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28c5aa9e3bSLendacky, Thomas  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29c5aa9e3bSLendacky, Thomas  *     and you.
30c5aa9e3bSLendacky, Thomas  *
31c5aa9e3bSLendacky, Thomas  *     The Software IS NOT an item of Licensed Software or Licensed Product
32c5aa9e3bSLendacky, Thomas  *     under any End User Software License Agreement or Agreement for Licensed
33c5aa9e3bSLendacky, Thomas  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34c5aa9e3bSLendacky, Thomas  *     granted, free of charge, to any person obtaining a copy of this software
35c5aa9e3bSLendacky, Thomas  *     annotated with this license and the Software, to deal in the Software
36c5aa9e3bSLendacky, Thomas  *     without restriction, including without limitation the rights to use,
37c5aa9e3bSLendacky, Thomas  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38c5aa9e3bSLendacky, Thomas  *     of the Software, and to permit persons to whom the Software is furnished
39c5aa9e3bSLendacky, Thomas  *     to do so, subject to the following conditions:
40c5aa9e3bSLendacky, Thomas  *
41c5aa9e3bSLendacky, Thomas  *     The above copyright notice and this permission notice shall be included
42c5aa9e3bSLendacky, Thomas  *     in all copies or substantial portions of the Software.
43c5aa9e3bSLendacky, Thomas  *
44c5aa9e3bSLendacky, Thomas  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45c5aa9e3bSLendacky, Thomas  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46c5aa9e3bSLendacky, Thomas  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47c5aa9e3bSLendacky, Thomas  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48c5aa9e3bSLendacky, Thomas  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49c5aa9e3bSLendacky, Thomas  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50c5aa9e3bSLendacky, Thomas  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51c5aa9e3bSLendacky, Thomas  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52c5aa9e3bSLendacky, Thomas  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53c5aa9e3bSLendacky, Thomas  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54c5aa9e3bSLendacky, Thomas  *     THE POSSIBILITY OF SUCH DAMAGE.
55c5aa9e3bSLendacky, Thomas  *
56c5aa9e3bSLendacky, Thomas  *
57c5aa9e3bSLendacky, Thomas  * License 2: Modified BSD
58c5aa9e3bSLendacky, Thomas  *
59b4eee84fSLendacky, Thomas  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60c5aa9e3bSLendacky, Thomas  * All rights reserved.
61c5aa9e3bSLendacky, Thomas  *
62c5aa9e3bSLendacky, Thomas  * Redistribution and use in source and binary forms, with or without
63c5aa9e3bSLendacky, Thomas  * modification, are permitted provided that the following conditions are met:
64c5aa9e3bSLendacky, Thomas  *     * Redistributions of source code must retain the above copyright
65c5aa9e3bSLendacky, Thomas  *       notice, this list of conditions and the following disclaimer.
66c5aa9e3bSLendacky, Thomas  *     * Redistributions in binary form must reproduce the above copyright
67c5aa9e3bSLendacky, Thomas  *       notice, this list of conditions and the following disclaimer in the
68c5aa9e3bSLendacky, Thomas  *       documentation and/or other materials provided with the distribution.
69c5aa9e3bSLendacky, Thomas  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70c5aa9e3bSLendacky, Thomas  *       names of its contributors may be used to endorse or promote products
71c5aa9e3bSLendacky, Thomas  *       derived from this software without specific prior written permission.
72c5aa9e3bSLendacky, Thomas  *
73c5aa9e3bSLendacky, Thomas  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74c5aa9e3bSLendacky, Thomas  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75c5aa9e3bSLendacky, Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76c5aa9e3bSLendacky, Thomas  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77c5aa9e3bSLendacky, Thomas  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78c5aa9e3bSLendacky, Thomas  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79c5aa9e3bSLendacky, Thomas  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80c5aa9e3bSLendacky, Thomas  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81c5aa9e3bSLendacky, Thomas  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82c5aa9e3bSLendacky, Thomas  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83c5aa9e3bSLendacky, Thomas  *
84c5aa9e3bSLendacky, Thomas  * This file incorporates work covered by the following copyright and
85c5aa9e3bSLendacky, Thomas  * permission notice:
86c5aa9e3bSLendacky, Thomas  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87c5aa9e3bSLendacky, Thomas  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88c5aa9e3bSLendacky, Thomas  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89c5aa9e3bSLendacky, Thomas  *     and you.
90c5aa9e3bSLendacky, Thomas  *
91c5aa9e3bSLendacky, Thomas  *     The Software IS NOT an item of Licensed Software or Licensed Product
92c5aa9e3bSLendacky, Thomas  *     under any End User Software License Agreement or Agreement for Licensed
93c5aa9e3bSLendacky, Thomas  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94c5aa9e3bSLendacky, Thomas  *     granted, free of charge, to any person obtaining a copy of this software
95c5aa9e3bSLendacky, Thomas  *     annotated with this license and the Software, to deal in the Software
96c5aa9e3bSLendacky, Thomas  *     without restriction, including without limitation the rights to use,
97c5aa9e3bSLendacky, Thomas  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98c5aa9e3bSLendacky, Thomas  *     of the Software, and to permit persons to whom the Software is furnished
99c5aa9e3bSLendacky, Thomas  *     to do so, subject to the following conditions:
100c5aa9e3bSLendacky, Thomas  *
101c5aa9e3bSLendacky, Thomas  *     The above copyright notice and this permission notice shall be included
102c5aa9e3bSLendacky, Thomas  *     in all copies or substantial portions of the Software.
103c5aa9e3bSLendacky, Thomas  *
104c5aa9e3bSLendacky, Thomas  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105c5aa9e3bSLendacky, Thomas  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106c5aa9e3bSLendacky, Thomas  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107c5aa9e3bSLendacky, Thomas  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108c5aa9e3bSLendacky, Thomas  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109c5aa9e3bSLendacky, Thomas  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110c5aa9e3bSLendacky, Thomas  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111c5aa9e3bSLendacky, Thomas  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112c5aa9e3bSLendacky, Thomas  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113c5aa9e3bSLendacky, Thomas  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114c5aa9e3bSLendacky, Thomas  *     THE POSSIBILITY OF SUCH DAMAGE.
115c5aa9e3bSLendacky, Thomas  */
116c5aa9e3bSLendacky, Thomas 
117c5aa9e3bSLendacky, Thomas #include <linux/phy.h>
118c3152d47SLendacky, Thomas #include <linux/mdio.h>
119c5aa9e3bSLendacky, Thomas #include <linux/clk.h>
120801c62d9SLendacky, Thomas #include <linux/bitrev.h>
121b85e4d89SLendacky, Thomas #include <linux/crc32.h>
1225d258b48SKrzysztof Kozlowski #include <linux/crc32poly.h>
123c5aa9e3bSLendacky, Thomas 
124c5aa9e3bSLendacky, Thomas #include "xgbe.h"
125c5aa9e3bSLendacky, Thomas #include "xgbe-common.h"
126c5aa9e3bSLendacky, Thomas 
xgbe_get_max_frame(struct xgbe_prv_data * pdata)12743e0dcf7SLendacky, Thomas static inline unsigned int xgbe_get_max_frame(struct xgbe_prv_data *pdata)
12843e0dcf7SLendacky, Thomas {
12943e0dcf7SLendacky, Thomas 	return pdata->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13043e0dcf7SLendacky, Thomas }
13143e0dcf7SLendacky, Thomas 
xgbe_usec_to_riwt(struct xgbe_prv_data * pdata,unsigned int usec)132c5aa9e3bSLendacky, Thomas static unsigned int xgbe_usec_to_riwt(struct xgbe_prv_data *pdata,
133c5aa9e3bSLendacky, Thomas 				      unsigned int usec)
134c5aa9e3bSLendacky, Thomas {
135c5aa9e3bSLendacky, Thomas 	unsigned long rate;
136c5aa9e3bSLendacky, Thomas 	unsigned int ret;
137c5aa9e3bSLendacky, Thomas 
138c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_usec_to_riwt\n");
139c5aa9e3bSLendacky, Thomas 
14082a19035SLendacky, Thomas 	rate = pdata->sysclk_rate;
141c5aa9e3bSLendacky, Thomas 
142c5aa9e3bSLendacky, Thomas 	/*
143c5aa9e3bSLendacky, Thomas 	 * Convert the input usec value to the watchdog timer value. Each
144c5aa9e3bSLendacky, Thomas 	 * watchdog timer value is equivalent to 256 clock cycles.
145c5aa9e3bSLendacky, Thomas 	 * Calculate the required value as:
146c5aa9e3bSLendacky, Thomas 	 *   ( usec * ( system_clock_mhz / 10^6 ) / 256
147c5aa9e3bSLendacky, Thomas 	 */
148c5aa9e3bSLendacky, Thomas 	ret = (usec * (rate / 1000000)) / 256;
149c5aa9e3bSLendacky, Thomas 
150c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_usec_to_riwt\n");
151c5aa9e3bSLendacky, Thomas 
152c5aa9e3bSLendacky, Thomas 	return ret;
153c5aa9e3bSLendacky, Thomas }
154c5aa9e3bSLendacky, Thomas 
xgbe_riwt_to_usec(struct xgbe_prv_data * pdata,unsigned int riwt)155c5aa9e3bSLendacky, Thomas static unsigned int xgbe_riwt_to_usec(struct xgbe_prv_data *pdata,
156c5aa9e3bSLendacky, Thomas 				      unsigned int riwt)
157c5aa9e3bSLendacky, Thomas {
158c5aa9e3bSLendacky, Thomas 	unsigned long rate;
159c5aa9e3bSLendacky, Thomas 	unsigned int ret;
160c5aa9e3bSLendacky, Thomas 
161c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_riwt_to_usec\n");
162c5aa9e3bSLendacky, Thomas 
16382a19035SLendacky, Thomas 	rate = pdata->sysclk_rate;
164c5aa9e3bSLendacky, Thomas 
165c5aa9e3bSLendacky, Thomas 	/*
166c5aa9e3bSLendacky, Thomas 	 * Convert the input watchdog timer value to the usec value. Each
167c5aa9e3bSLendacky, Thomas 	 * watchdog timer value is equivalent to 256 clock cycles.
168c5aa9e3bSLendacky, Thomas 	 * Calculate the required value as:
169c5aa9e3bSLendacky, Thomas 	 *   ( riwt * 256 ) / ( system_clock_mhz / 10^6 )
170c5aa9e3bSLendacky, Thomas 	 */
171c5aa9e3bSLendacky, Thomas 	ret = (riwt * 256) / (rate / 1000000);
172c5aa9e3bSLendacky, Thomas 
173c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_riwt_to_usec\n");
174c5aa9e3bSLendacky, Thomas 
175c5aa9e3bSLendacky, Thomas 	return ret;
176c5aa9e3bSLendacky, Thomas }
177c5aa9e3bSLendacky, Thomas 
xgbe_config_pbl_val(struct xgbe_prv_data * pdata)1787e1e6b86SLendacky, Thomas static int xgbe_config_pbl_val(struct xgbe_prv_data *pdata)
179c5aa9e3bSLendacky, Thomas {
1807e1e6b86SLendacky, Thomas 	unsigned int pblx8, pbl;
181c5aa9e3bSLendacky, Thomas 	unsigned int i;
182c5aa9e3bSLendacky, Thomas 
1837e1e6b86SLendacky, Thomas 	pblx8 = DMA_PBL_X8_DISABLE;
1847e1e6b86SLendacky, Thomas 	pbl = pdata->pbl;
1857e1e6b86SLendacky, Thomas 
1867e1e6b86SLendacky, Thomas 	if (pdata->pbl > 32) {
1877e1e6b86SLendacky, Thomas 		pblx8 = DMA_PBL_X8_ENABLE;
1887e1e6b86SLendacky, Thomas 		pbl >>= 3;
1897e1e6b86SLendacky, Thomas 	}
1907e1e6b86SLendacky, Thomas 
1917e1e6b86SLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
19218f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, PBLX8,
1937e1e6b86SLendacky, Thomas 				       pblx8);
194c5aa9e3bSLendacky, Thomas 
1957e1e6b86SLendacky, Thomas 		if (pdata->channel[i]->tx_ring)
1967e1e6b86SLendacky, Thomas 			XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR,
1977e1e6b86SLendacky, Thomas 					       PBL, pbl);
198c5aa9e3bSLendacky, Thomas 
1997e1e6b86SLendacky, Thomas 		if (pdata->channel[i]->rx_ring)
2007e1e6b86SLendacky, Thomas 			XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR,
2017e1e6b86SLendacky, Thomas 					       PBL, pbl);
202c5aa9e3bSLendacky, Thomas 	}
203c5aa9e3bSLendacky, Thomas 
204c5aa9e3bSLendacky, Thomas 	return 0;
205c5aa9e3bSLendacky, Thomas }
206c5aa9e3bSLendacky, Thomas 
xgbe_config_osp_mode(struct xgbe_prv_data * pdata)207c5aa9e3bSLendacky, Thomas static int xgbe_config_osp_mode(struct xgbe_prv_data *pdata)
208c5aa9e3bSLendacky, Thomas {
209c5aa9e3bSLendacky, Thomas 	unsigned int i;
210c5aa9e3bSLendacky, Thomas 
21118f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
21218f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
213c5aa9e3bSLendacky, Thomas 			break;
214c5aa9e3bSLendacky, Thomas 
21518f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, OSP,
216c5aa9e3bSLendacky, Thomas 				       pdata->tx_osp_mode);
217c5aa9e3bSLendacky, Thomas 	}
218c5aa9e3bSLendacky, Thomas 
219c5aa9e3bSLendacky, Thomas 	return 0;
220c5aa9e3bSLendacky, Thomas }
221c5aa9e3bSLendacky, Thomas 
xgbe_config_rsf_mode(struct xgbe_prv_data * pdata,unsigned int val)222c5aa9e3bSLendacky, Thomas static int xgbe_config_rsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
223c5aa9e3bSLendacky, Thomas {
224c5aa9e3bSLendacky, Thomas 	unsigned int i;
225c5aa9e3bSLendacky, Thomas 
226853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
227c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RSF, val);
228c5aa9e3bSLendacky, Thomas 
229c5aa9e3bSLendacky, Thomas 	return 0;
230c5aa9e3bSLendacky, Thomas }
231c5aa9e3bSLendacky, Thomas 
xgbe_config_tsf_mode(struct xgbe_prv_data * pdata,unsigned int val)232c5aa9e3bSLendacky, Thomas static int xgbe_config_tsf_mode(struct xgbe_prv_data *pdata, unsigned int val)
233c5aa9e3bSLendacky, Thomas {
234c5aa9e3bSLendacky, Thomas 	unsigned int i;
235c5aa9e3bSLendacky, Thomas 
236853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
237c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TSF, val);
238c5aa9e3bSLendacky, Thomas 
239c5aa9e3bSLendacky, Thomas 	return 0;
240c5aa9e3bSLendacky, Thomas }
241c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_threshold(struct xgbe_prv_data * pdata,unsigned int val)242c5aa9e3bSLendacky, Thomas static int xgbe_config_rx_threshold(struct xgbe_prv_data *pdata,
243c5aa9e3bSLendacky, Thomas 				    unsigned int val)
244c5aa9e3bSLendacky, Thomas {
245c5aa9e3bSLendacky, Thomas 	unsigned int i;
246c5aa9e3bSLendacky, Thomas 
247853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
248c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RTC, val);
249c5aa9e3bSLendacky, Thomas 
250c5aa9e3bSLendacky, Thomas 	return 0;
251c5aa9e3bSLendacky, Thomas }
252c5aa9e3bSLendacky, Thomas 
xgbe_config_tx_threshold(struct xgbe_prv_data * pdata,unsigned int val)253c5aa9e3bSLendacky, Thomas static int xgbe_config_tx_threshold(struct xgbe_prv_data *pdata,
254c5aa9e3bSLendacky, Thomas 				    unsigned int val)
255c5aa9e3bSLendacky, Thomas {
256c5aa9e3bSLendacky, Thomas 	unsigned int i;
257c5aa9e3bSLendacky, Thomas 
258853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
259c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TTC, val);
260c5aa9e3bSLendacky, Thomas 
261c5aa9e3bSLendacky, Thomas 	return 0;
262c5aa9e3bSLendacky, Thomas }
263c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_coalesce(struct xgbe_prv_data * pdata)264c5aa9e3bSLendacky, Thomas static int xgbe_config_rx_coalesce(struct xgbe_prv_data *pdata)
265c5aa9e3bSLendacky, Thomas {
266c5aa9e3bSLendacky, Thomas 	unsigned int i;
267c5aa9e3bSLendacky, Thomas 
26818f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
26918f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
270c5aa9e3bSLendacky, Thomas 			break;
271c5aa9e3bSLendacky, Thomas 
27218f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RIWT, RWT,
273c5aa9e3bSLendacky, Thomas 				       pdata->rx_riwt);
274c5aa9e3bSLendacky, Thomas 	}
275c5aa9e3bSLendacky, Thomas 
276c5aa9e3bSLendacky, Thomas 	return 0;
277c5aa9e3bSLendacky, Thomas }
278c5aa9e3bSLendacky, Thomas 
xgbe_config_tx_coalesce(struct xgbe_prv_data * pdata)279c5aa9e3bSLendacky, Thomas static int xgbe_config_tx_coalesce(struct xgbe_prv_data *pdata)
280c5aa9e3bSLendacky, Thomas {
281c5aa9e3bSLendacky, Thomas 	return 0;
282c5aa9e3bSLendacky, Thomas }
283c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_buffer_size(struct xgbe_prv_data * pdata)284c5aa9e3bSLendacky, Thomas static void xgbe_config_rx_buffer_size(struct xgbe_prv_data *pdata)
285c5aa9e3bSLendacky, Thomas {
286c5aa9e3bSLendacky, Thomas 	unsigned int i;
287c5aa9e3bSLendacky, Thomas 
28818f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
28918f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
290c5aa9e3bSLendacky, Thomas 			break;
291c5aa9e3bSLendacky, Thomas 
29218f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, RBSZ,
293c5aa9e3bSLendacky, Thomas 				       pdata->rx_buf_size);
294c5aa9e3bSLendacky, Thomas 	}
295c5aa9e3bSLendacky, Thomas }
296c5aa9e3bSLendacky, Thomas 
xgbe_config_tso_mode(struct xgbe_prv_data * pdata)297c5aa9e3bSLendacky, Thomas static void xgbe_config_tso_mode(struct xgbe_prv_data *pdata)
298c5aa9e3bSLendacky, Thomas {
299c5aa9e3bSLendacky, Thomas 	unsigned int i;
300c5aa9e3bSLendacky, Thomas 
30118f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
30218f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
303c5aa9e3bSLendacky, Thomas 			break;
304c5aa9e3bSLendacky, Thomas 
30518f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, TSE, 1);
306c5aa9e3bSLendacky, Thomas 	}
307c5aa9e3bSLendacky, Thomas }
308c5aa9e3bSLendacky, Thomas 
xgbe_config_sph_mode(struct xgbe_prv_data * pdata)309174fd259SLendacky, Thomas static void xgbe_config_sph_mode(struct xgbe_prv_data *pdata)
310174fd259SLendacky, Thomas {
311174fd259SLendacky, Thomas 	unsigned int i;
312174fd259SLendacky, Thomas 
31318f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
31418f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
315174fd259SLendacky, Thomas 			break;
316174fd259SLendacky, Thomas 
31718f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_CR, SPH, 1);
318174fd259SLendacky, Thomas 	}
319174fd259SLendacky, Thomas 
320174fd259SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, HDSMS, XGBE_SPH_HDSMS_SIZE);
321174fd259SLendacky, Thomas }
322174fd259SLendacky, Thomas 
xgbe_write_rss_reg(struct xgbe_prv_data * pdata,unsigned int type,unsigned int index,unsigned int val)3235b9dfe29SLendacky, Thomas static int xgbe_write_rss_reg(struct xgbe_prv_data *pdata, unsigned int type,
3245b9dfe29SLendacky, Thomas 			      unsigned int index, unsigned int val)
3255b9dfe29SLendacky, Thomas {
3265b9dfe29SLendacky, Thomas 	unsigned int wait;
3275b9dfe29SLendacky, Thomas 	int ret = 0;
3285b9dfe29SLendacky, Thomas 
3295b9dfe29SLendacky, Thomas 	mutex_lock(&pdata->rss_mutex);
3305b9dfe29SLendacky, Thomas 
3315b9dfe29SLendacky, Thomas 	if (XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB)) {
3325b9dfe29SLendacky, Thomas 		ret = -EBUSY;
3335b9dfe29SLendacky, Thomas 		goto unlock;
3345b9dfe29SLendacky, Thomas 	}
3355b9dfe29SLendacky, Thomas 
3365b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_RSSDR, val);
3375b9dfe29SLendacky, Thomas 
3385b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, RSSIA, index);
3395b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, ADDRT, type);
3405b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, CT, 0);
3415b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSAR, OB, 1);
3425b9dfe29SLendacky, Thomas 
3435b9dfe29SLendacky, Thomas 	wait = 1000;
3445b9dfe29SLendacky, Thomas 	while (wait--) {
3455b9dfe29SLendacky, Thomas 		if (!XGMAC_IOREAD_BITS(pdata, MAC_RSSAR, OB))
3465b9dfe29SLendacky, Thomas 			goto unlock;
3475b9dfe29SLendacky, Thomas 
3485b9dfe29SLendacky, Thomas 		usleep_range(1000, 1500);
3495b9dfe29SLendacky, Thomas 	}
3505b9dfe29SLendacky, Thomas 
3515b9dfe29SLendacky, Thomas 	ret = -EBUSY;
3525b9dfe29SLendacky, Thomas 
3535b9dfe29SLendacky, Thomas unlock:
3545b9dfe29SLendacky, Thomas 	mutex_unlock(&pdata->rss_mutex);
3555b9dfe29SLendacky, Thomas 
3565b9dfe29SLendacky, Thomas 	return ret;
3575b9dfe29SLendacky, Thomas }
3585b9dfe29SLendacky, Thomas 
xgbe_write_rss_hash_key(struct xgbe_prv_data * pdata)3595b9dfe29SLendacky, Thomas static int xgbe_write_rss_hash_key(struct xgbe_prv_data *pdata)
3605b9dfe29SLendacky, Thomas {
3615b9dfe29SLendacky, Thomas 	unsigned int key_regs = sizeof(pdata->rss_key) / sizeof(u32);
3625b9dfe29SLendacky, Thomas 	unsigned int *key = (unsigned int *)&pdata->rss_key;
3635b9dfe29SLendacky, Thomas 	int ret;
3645b9dfe29SLendacky, Thomas 
3655b9dfe29SLendacky, Thomas 	while (key_regs--) {
3665b9dfe29SLendacky, Thomas 		ret = xgbe_write_rss_reg(pdata, XGBE_RSS_HASH_KEY_TYPE,
3675b9dfe29SLendacky, Thomas 					 key_regs, *key++);
3685b9dfe29SLendacky, Thomas 		if (ret)
3695b9dfe29SLendacky, Thomas 			return ret;
3705b9dfe29SLendacky, Thomas 	}
3715b9dfe29SLendacky, Thomas 
3725b9dfe29SLendacky, Thomas 	return 0;
3735b9dfe29SLendacky, Thomas }
3745b9dfe29SLendacky, Thomas 
xgbe_write_rss_lookup_table(struct xgbe_prv_data * pdata)3755b9dfe29SLendacky, Thomas static int xgbe_write_rss_lookup_table(struct xgbe_prv_data *pdata)
3765b9dfe29SLendacky, Thomas {
3775b9dfe29SLendacky, Thomas 	unsigned int i;
3785b9dfe29SLendacky, Thomas 	int ret;
3795b9dfe29SLendacky, Thomas 
3805b9dfe29SLendacky, Thomas 	for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++) {
3815b9dfe29SLendacky, Thomas 		ret = xgbe_write_rss_reg(pdata,
3825b9dfe29SLendacky, Thomas 					 XGBE_RSS_LOOKUP_TABLE_TYPE, i,
3835b9dfe29SLendacky, Thomas 					 pdata->rss_table[i]);
3845b9dfe29SLendacky, Thomas 		if (ret)
3855b9dfe29SLendacky, Thomas 			return ret;
3865b9dfe29SLendacky, Thomas 	}
3875b9dfe29SLendacky, Thomas 
3885b9dfe29SLendacky, Thomas 	return 0;
3895b9dfe29SLendacky, Thomas }
3905b9dfe29SLendacky, Thomas 
xgbe_set_rss_hash_key(struct xgbe_prv_data * pdata,const u8 * key)391f6ac8628SLendacky, Thomas static int xgbe_set_rss_hash_key(struct xgbe_prv_data *pdata, const u8 *key)
392f6ac8628SLendacky, Thomas {
393f6ac8628SLendacky, Thomas 	memcpy(pdata->rss_key, key, sizeof(pdata->rss_key));
394f6ac8628SLendacky, Thomas 
395f6ac8628SLendacky, Thomas 	return xgbe_write_rss_hash_key(pdata);
396f6ac8628SLendacky, Thomas }
397f6ac8628SLendacky, Thomas 
xgbe_set_rss_lookup_table(struct xgbe_prv_data * pdata,const u32 * table)398f6ac8628SLendacky, Thomas static int xgbe_set_rss_lookup_table(struct xgbe_prv_data *pdata,
399f6ac8628SLendacky, Thomas 				     const u32 *table)
400f6ac8628SLendacky, Thomas {
401f6ac8628SLendacky, Thomas 	unsigned int i;
402f6ac8628SLendacky, Thomas 
403f6ac8628SLendacky, Thomas 	for (i = 0; i < ARRAY_SIZE(pdata->rss_table); i++)
404f6ac8628SLendacky, Thomas 		XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH, table[i]);
405f6ac8628SLendacky, Thomas 
406f6ac8628SLendacky, Thomas 	return xgbe_write_rss_lookup_table(pdata);
407f6ac8628SLendacky, Thomas }
408f6ac8628SLendacky, Thomas 
xgbe_enable_rss(struct xgbe_prv_data * pdata)4095b9dfe29SLendacky, Thomas static int xgbe_enable_rss(struct xgbe_prv_data *pdata)
4105b9dfe29SLendacky, Thomas {
4115b9dfe29SLendacky, Thomas 	int ret;
4125b9dfe29SLendacky, Thomas 
4135b9dfe29SLendacky, Thomas 	if (!pdata->hw_feat.rss)
4145b9dfe29SLendacky, Thomas 		return -EOPNOTSUPP;
4155b9dfe29SLendacky, Thomas 
4165b9dfe29SLendacky, Thomas 	/* Program the hash key */
4175b9dfe29SLendacky, Thomas 	ret = xgbe_write_rss_hash_key(pdata);
4185b9dfe29SLendacky, Thomas 	if (ret)
4195b9dfe29SLendacky, Thomas 		return ret;
4205b9dfe29SLendacky, Thomas 
4215b9dfe29SLendacky, Thomas 	/* Program the lookup table */
4225b9dfe29SLendacky, Thomas 	ret = xgbe_write_rss_lookup_table(pdata);
4235b9dfe29SLendacky, Thomas 	if (ret)
4245b9dfe29SLendacky, Thomas 		return ret;
4255b9dfe29SLendacky, Thomas 
4265b9dfe29SLendacky, Thomas 	/* Set the RSS options */
4275b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_RSSCR, pdata->rss_options);
4285b9dfe29SLendacky, Thomas 
4295b9dfe29SLendacky, Thomas 	/* Enable RSS */
4305b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 1);
4315b9dfe29SLendacky, Thomas 
4325b9dfe29SLendacky, Thomas 	return 0;
4335b9dfe29SLendacky, Thomas }
4345b9dfe29SLendacky, Thomas 
xgbe_disable_rss(struct xgbe_prv_data * pdata)4355b9dfe29SLendacky, Thomas static int xgbe_disable_rss(struct xgbe_prv_data *pdata)
4365b9dfe29SLendacky, Thomas {
4375b9dfe29SLendacky, Thomas 	if (!pdata->hw_feat.rss)
4385b9dfe29SLendacky, Thomas 		return -EOPNOTSUPP;
4395b9dfe29SLendacky, Thomas 
4405b9dfe29SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RSSCR, RSSE, 0);
4415b9dfe29SLendacky, Thomas 
4425b9dfe29SLendacky, Thomas 	return 0;
4435b9dfe29SLendacky, Thomas }
4445b9dfe29SLendacky, Thomas 
xgbe_config_rss(struct xgbe_prv_data * pdata)4455b9dfe29SLendacky, Thomas static void xgbe_config_rss(struct xgbe_prv_data *pdata)
4465b9dfe29SLendacky, Thomas {
4475b9dfe29SLendacky, Thomas 	int ret;
4485b9dfe29SLendacky, Thomas 
4495b9dfe29SLendacky, Thomas 	if (!pdata->hw_feat.rss)
4505b9dfe29SLendacky, Thomas 		return;
4515b9dfe29SLendacky, Thomas 
4525b9dfe29SLendacky, Thomas 	if (pdata->netdev->features & NETIF_F_RXHASH)
4535b9dfe29SLendacky, Thomas 		ret = xgbe_enable_rss(pdata);
4545b9dfe29SLendacky, Thomas 	else
4555b9dfe29SLendacky, Thomas 		ret = xgbe_disable_rss(pdata);
4565b9dfe29SLendacky, Thomas 
4575b9dfe29SLendacky, Thomas 	if (ret)
4585b9dfe29SLendacky, Thomas 		netdev_err(pdata->netdev,
4595b9dfe29SLendacky, Thomas 			   "error configuring RSS, RSS disabled\n");
4605b9dfe29SLendacky, Thomas }
4615b9dfe29SLendacky, Thomas 
xgbe_is_pfc_queue(struct xgbe_prv_data * pdata,unsigned int queue)46243e0dcf7SLendacky, Thomas static bool xgbe_is_pfc_queue(struct xgbe_prv_data *pdata,
46343e0dcf7SLendacky, Thomas 			      unsigned int queue)
46443e0dcf7SLendacky, Thomas {
46543e0dcf7SLendacky, Thomas 	unsigned int prio, tc;
46643e0dcf7SLendacky, Thomas 
46743e0dcf7SLendacky, Thomas 	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
46843e0dcf7SLendacky, Thomas 		/* Does this queue handle the priority? */
46943e0dcf7SLendacky, Thomas 		if (pdata->prio2q_map[prio] != queue)
47043e0dcf7SLendacky, Thomas 			continue;
47143e0dcf7SLendacky, Thomas 
47243e0dcf7SLendacky, Thomas 		/* Get the Traffic Class for this priority */
47343e0dcf7SLendacky, Thomas 		tc = pdata->ets->prio_tc[prio];
47443e0dcf7SLendacky, Thomas 
47543e0dcf7SLendacky, Thomas 		/* Check if PFC is enabled for this traffic class */
47643e0dcf7SLendacky, Thomas 		if (pdata->pfc->pfc_en & (1 << tc))
47743e0dcf7SLendacky, Thomas 			return true;
47843e0dcf7SLendacky, Thomas 	}
47943e0dcf7SLendacky, Thomas 
48043e0dcf7SLendacky, Thomas 	return false;
48143e0dcf7SLendacky, Thomas }
48243e0dcf7SLendacky, Thomas 
xgbe_set_vxlan_id(struct xgbe_prv_data * pdata)4831a510ccfSLendacky, Thomas static void xgbe_set_vxlan_id(struct xgbe_prv_data *pdata)
4841a510ccfSLendacky, Thomas {
4851a510ccfSLendacky, Thomas 	/* Program the VXLAN port */
4861a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TIR, TNID, pdata->vxlan_port);
4871a510ccfSLendacky, Thomas 
4881a510ccfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev, "VXLAN tunnel id set to %hx\n",
4891a510ccfSLendacky, Thomas 		  pdata->vxlan_port);
4901a510ccfSLendacky, Thomas }
4911a510ccfSLendacky, Thomas 
xgbe_enable_vxlan(struct xgbe_prv_data * pdata)4921a510ccfSLendacky, Thomas static void xgbe_enable_vxlan(struct xgbe_prv_data *pdata)
4931a510ccfSLendacky, Thomas {
4941a510ccfSLendacky, Thomas 	if (!pdata->hw_feat.vxn)
4951a510ccfSLendacky, Thomas 		return;
4961a510ccfSLendacky, Thomas 
4971a510ccfSLendacky, Thomas 	/* Program the VXLAN port */
4981a510ccfSLendacky, Thomas 	xgbe_set_vxlan_id(pdata);
4991a510ccfSLendacky, Thomas 
5001a510ccfSLendacky, Thomas 	/* Allow for IPv6/UDP zero-checksum VXLAN packets */
5011a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VUCC, 1);
5021a510ccfSLendacky, Thomas 
5031a510ccfSLendacky, Thomas 	/* Enable VXLAN tunneling mode */
5041a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, VNM, 0);
5051a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, VNE, 1);
5061a510ccfSLendacky, Thomas 
5071a510ccfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev, "VXLAN acceleration enabled\n");
5081a510ccfSLendacky, Thomas }
5091a510ccfSLendacky, Thomas 
xgbe_disable_vxlan(struct xgbe_prv_data * pdata)5101a510ccfSLendacky, Thomas static void xgbe_disable_vxlan(struct xgbe_prv_data *pdata)
5111a510ccfSLendacky, Thomas {
5121a510ccfSLendacky, Thomas 	if (!pdata->hw_feat.vxn)
5131a510ccfSLendacky, Thomas 		return;
5141a510ccfSLendacky, Thomas 
5151a510ccfSLendacky, Thomas 	/* Disable tunneling mode */
5161a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, VNE, 0);
5171a510ccfSLendacky, Thomas 
5181a510ccfSLendacky, Thomas 	/* Clear IPv6/UDP zero-checksum VXLAN packets setting */
5191a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VUCC, 0);
5201a510ccfSLendacky, Thomas 
5211a510ccfSLendacky, Thomas 	/* Clear the VXLAN port */
5221a510ccfSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TIR, TNID, 0);
5231a510ccfSLendacky, Thomas 
5241a510ccfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev, "VXLAN acceleration disabled\n");
5251a510ccfSLendacky, Thomas }
5261a510ccfSLendacky, Thomas 
xgbe_get_fc_queue_count(struct xgbe_prv_data * pdata)527579923d8SRaju Rangoju static unsigned int xgbe_get_fc_queue_count(struct xgbe_prv_data *pdata)
528579923d8SRaju Rangoju {
529579923d8SRaju Rangoju 	unsigned int max_q_count = XGMAC_MAX_FLOW_CONTROL_QUEUES;
530579923d8SRaju Rangoju 
531579923d8SRaju Rangoju 	/* From MAC ver 30H the TFCR is per priority, instead of per queue */
532579923d8SRaju Rangoju 	if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) >= 0x30)
533579923d8SRaju Rangoju 		return max_q_count;
534579923d8SRaju Rangoju 	else
535579923d8SRaju Rangoju 		return min_t(unsigned int, pdata->tx_q_count, max_q_count);
536579923d8SRaju Rangoju }
537579923d8SRaju Rangoju 
xgbe_disable_tx_flow_control(struct xgbe_prv_data * pdata)538c5aa9e3bSLendacky, Thomas static int xgbe_disable_tx_flow_control(struct xgbe_prv_data *pdata)
539c5aa9e3bSLendacky, Thomas {
540c5aa9e3bSLendacky, Thomas 	unsigned int reg, reg_val;
541579923d8SRaju Rangoju 	unsigned int i, q_count;
542c5aa9e3bSLendacky, Thomas 
543c5aa9e3bSLendacky, Thomas 	/* Clear MTL flow control */
544853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
545c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, 0);
546c5aa9e3bSLendacky, Thomas 
547c5aa9e3bSLendacky, Thomas 	/* Clear MAC flow control */
548579923d8SRaju Rangoju 	q_count = xgbe_get_fc_queue_count(pdata);
549c5aa9e3bSLendacky, Thomas 	reg = MAC_Q0TFCR;
550c5aa9e3bSLendacky, Thomas 	for (i = 0; i < q_count; i++) {
551c5aa9e3bSLendacky, Thomas 		reg_val = XGMAC_IOREAD(pdata, reg);
552c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 0);
553c5aa9e3bSLendacky, Thomas 		XGMAC_IOWRITE(pdata, reg, reg_val);
554c5aa9e3bSLendacky, Thomas 
555c5aa9e3bSLendacky, Thomas 		reg += MAC_QTFCR_INC;
556c5aa9e3bSLendacky, Thomas 	}
557c5aa9e3bSLendacky, Thomas 
558c5aa9e3bSLendacky, Thomas 	return 0;
559c5aa9e3bSLendacky, Thomas }
560c5aa9e3bSLendacky, Thomas 
xgbe_enable_tx_flow_control(struct xgbe_prv_data * pdata)561c5aa9e3bSLendacky, Thomas static int xgbe_enable_tx_flow_control(struct xgbe_prv_data *pdata)
562c5aa9e3bSLendacky, Thomas {
5638dba2a2aSLendacky, Thomas 	struct ieee_pfc *pfc = pdata->pfc;
5648dba2a2aSLendacky, Thomas 	struct ieee_ets *ets = pdata->ets;
565c5aa9e3bSLendacky, Thomas 	unsigned int reg, reg_val;
566579923d8SRaju Rangoju 	unsigned int i, q_count;
567c5aa9e3bSLendacky, Thomas 
568c5aa9e3bSLendacky, Thomas 	/* Set MTL flow control */
5698dba2a2aSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++) {
5708dba2a2aSLendacky, Thomas 		unsigned int ehfc = 0;
5718dba2a2aSLendacky, Thomas 
57243e0dcf7SLendacky, Thomas 		if (pdata->rx_rfd[i]) {
57343e0dcf7SLendacky, Thomas 			/* Flow control thresholds are established */
5748dba2a2aSLendacky, Thomas 			if (pfc && ets) {
57543e0dcf7SLendacky, Thomas 				if (xgbe_is_pfc_queue(pdata, i))
5768dba2a2aSLendacky, Thomas 					ehfc = 1;
5778dba2a2aSLendacky, Thomas 			} else {
5788dba2a2aSLendacky, Thomas 				ehfc = 1;
5798dba2a2aSLendacky, Thomas 			}
58043e0dcf7SLendacky, Thomas 		}
5818dba2a2aSLendacky, Thomas 
5828dba2a2aSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, EHFC, ehfc);
5838dba2a2aSLendacky, Thomas 
5848dba2a2aSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev,
5858dba2a2aSLendacky, Thomas 			  "flow control %s for RXq%u\n",
5868dba2a2aSLendacky, Thomas 			  ehfc ? "enabled" : "disabled", i);
5878dba2a2aSLendacky, Thomas 	}
588c5aa9e3bSLendacky, Thomas 
589c5aa9e3bSLendacky, Thomas 	/* Set MAC flow control */
590579923d8SRaju Rangoju 	q_count = xgbe_get_fc_queue_count(pdata);
591c5aa9e3bSLendacky, Thomas 	reg = MAC_Q0TFCR;
592c5aa9e3bSLendacky, Thomas 	for (i = 0; i < q_count; i++) {
593c5aa9e3bSLendacky, Thomas 		reg_val = XGMAC_IOREAD(pdata, reg);
594c5aa9e3bSLendacky, Thomas 
595c5aa9e3bSLendacky, Thomas 		/* Enable transmit flow control */
596c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, TFE, 1);
597c5aa9e3bSLendacky, Thomas 		/* Set pause time */
598c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, 0xffff);
599c5aa9e3bSLendacky, Thomas 
600c5aa9e3bSLendacky, Thomas 		XGMAC_IOWRITE(pdata, reg, reg_val);
601c5aa9e3bSLendacky, Thomas 
602c5aa9e3bSLendacky, Thomas 		reg += MAC_QTFCR_INC;
603c5aa9e3bSLendacky, Thomas 	}
604c5aa9e3bSLendacky, Thomas 
605c5aa9e3bSLendacky, Thomas 	return 0;
606c5aa9e3bSLendacky, Thomas }
607c5aa9e3bSLendacky, Thomas 
xgbe_disable_rx_flow_control(struct xgbe_prv_data * pdata)608c5aa9e3bSLendacky, Thomas static int xgbe_disable_rx_flow_control(struct xgbe_prv_data *pdata)
609c5aa9e3bSLendacky, Thomas {
610c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 0);
611c5aa9e3bSLendacky, Thomas 
612c5aa9e3bSLendacky, Thomas 	return 0;
613c5aa9e3bSLendacky, Thomas }
614c5aa9e3bSLendacky, Thomas 
xgbe_enable_rx_flow_control(struct xgbe_prv_data * pdata)615c5aa9e3bSLendacky, Thomas static int xgbe_enable_rx_flow_control(struct xgbe_prv_data *pdata)
616c5aa9e3bSLendacky, Thomas {
617c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, RFE, 1);
618c5aa9e3bSLendacky, Thomas 
619c5aa9e3bSLendacky, Thomas 	return 0;
620c5aa9e3bSLendacky, Thomas }
621c5aa9e3bSLendacky, Thomas 
xgbe_config_tx_flow_control(struct xgbe_prv_data * pdata)622c5aa9e3bSLendacky, Thomas static int xgbe_config_tx_flow_control(struct xgbe_prv_data *pdata)
623c5aa9e3bSLendacky, Thomas {
624fca2d994SLendacky, Thomas 	struct ieee_pfc *pfc = pdata->pfc;
625fca2d994SLendacky, Thomas 
626fca2d994SLendacky, Thomas 	if (pdata->tx_pause || (pfc && pfc->pfc_en))
627c5aa9e3bSLendacky, Thomas 		xgbe_enable_tx_flow_control(pdata);
628c5aa9e3bSLendacky, Thomas 	else
629c5aa9e3bSLendacky, Thomas 		xgbe_disable_tx_flow_control(pdata);
630c5aa9e3bSLendacky, Thomas 
631c5aa9e3bSLendacky, Thomas 	return 0;
632c5aa9e3bSLendacky, Thomas }
633c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_flow_control(struct xgbe_prv_data * pdata)634c5aa9e3bSLendacky, Thomas static int xgbe_config_rx_flow_control(struct xgbe_prv_data *pdata)
635c5aa9e3bSLendacky, Thomas {
636fca2d994SLendacky, Thomas 	struct ieee_pfc *pfc = pdata->pfc;
637fca2d994SLendacky, Thomas 
638fca2d994SLendacky, Thomas 	if (pdata->rx_pause || (pfc && pfc->pfc_en))
639c5aa9e3bSLendacky, Thomas 		xgbe_enable_rx_flow_control(pdata);
640c5aa9e3bSLendacky, Thomas 	else
641c5aa9e3bSLendacky, Thomas 		xgbe_disable_rx_flow_control(pdata);
642c5aa9e3bSLendacky, Thomas 
643c5aa9e3bSLendacky, Thomas 	return 0;
644c5aa9e3bSLendacky, Thomas }
645c5aa9e3bSLendacky, Thomas 
xgbe_config_flow_control(struct xgbe_prv_data * pdata)646c5aa9e3bSLendacky, Thomas static void xgbe_config_flow_control(struct xgbe_prv_data *pdata)
647c5aa9e3bSLendacky, Thomas {
648fca2d994SLendacky, Thomas 	struct ieee_pfc *pfc = pdata->pfc;
649fca2d994SLendacky, Thomas 
650c5aa9e3bSLendacky, Thomas 	xgbe_config_tx_flow_control(pdata);
651c5aa9e3bSLendacky, Thomas 	xgbe_config_rx_flow_control(pdata);
652fca2d994SLendacky, Thomas 
653fca2d994SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RFCR, PFCE,
654fca2d994SLendacky, Thomas 			   (pfc && pfc->pfc_en) ? 1 : 0);
655c5aa9e3bSLendacky, Thomas }
656c5aa9e3bSLendacky, Thomas 
xgbe_enable_dma_interrupts(struct xgbe_prv_data * pdata)657c5aa9e3bSLendacky, Thomas static void xgbe_enable_dma_interrupts(struct xgbe_prv_data *pdata)
658c5aa9e3bSLendacky, Thomas {
659c5aa9e3bSLendacky, Thomas 	struct xgbe_channel *channel;
660f602b976STom Lendacky 	unsigned int i, ver;
661c5aa9e3bSLendacky, Thomas 
6624c70dd8aSLendacky, Thomas 	/* Set the interrupt mode if supported */
6634c70dd8aSLendacky, Thomas 	if (pdata->channel_irq_mode)
6644c70dd8aSLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, DMA_MR, INTM,
6654c70dd8aSLendacky, Thomas 				   pdata->channel_irq_mode);
6664c70dd8aSLendacky, Thomas 
667f602b976STom Lendacky 	ver = XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER);
668f602b976STom Lendacky 
66918f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
67018f9f0acSLendacky, Thomas 		channel = pdata->channel[i];
67118f9f0acSLendacky, Thomas 
672c5aa9e3bSLendacky, Thomas 		/* Clear all the interrupts which are set */
673caa575afSLendacky, Thomas 		XGMAC_DMA_IOWRITE(channel, DMA_CH_SR,
674caa575afSLendacky, Thomas 				  XGMAC_DMA_IOREAD(channel, DMA_CH_SR));
675c5aa9e3bSLendacky, Thomas 
676c5aa9e3bSLendacky, Thomas 		/* Clear all interrupt enable bits */
677caa575afSLendacky, Thomas 		channel->curr_ier = 0;
678c5aa9e3bSLendacky, Thomas 
679c5aa9e3bSLendacky, Thomas 		/* Enable following interrupts
680c5aa9e3bSLendacky, Thomas 		 *   NIE  - Normal Interrupt Summary Enable
681c5aa9e3bSLendacky, Thomas 		 *   AIE  - Abnormal Interrupt Summary Enable
682c5aa9e3bSLendacky, Thomas 		 *   FBEE - Fatal Bus Error Enable
683c5aa9e3bSLendacky, Thomas 		 */
684f602b976STom Lendacky 		if (ver < 0x21) {
685f602b976STom Lendacky 			XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, NIE20, 1);
686f602b976STom Lendacky 			XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, AIE20, 1);
687f602b976STom Lendacky 		} else {
688caa575afSLendacky, Thomas 			XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, NIE, 1);
689caa575afSLendacky, Thomas 			XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, AIE, 1);
690f602b976STom Lendacky 		}
691caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 1);
692c5aa9e3bSLendacky, Thomas 
693c5aa9e3bSLendacky, Thomas 		if (channel->tx_ring) {
694c5aa9e3bSLendacky, Thomas 			/* Enable the following Tx interrupts
6959227dc5eSLendacky, Thomas 			 *   TIE  - Transmit Interrupt Enable (unless using
6964c70dd8aSLendacky, Thomas 			 *          per channel interrupts in edge triggered
6974c70dd8aSLendacky, Thomas 			 *          mode)
698c5aa9e3bSLendacky, Thomas 			 */
6994c70dd8aSLendacky, Thomas 			if (!pdata->per_channel_irq || pdata->channel_irq_mode)
700caa575afSLendacky, Thomas 				XGMAC_SET_BITS(channel->curr_ier,
701caa575afSLendacky, Thomas 					       DMA_CH_IER, TIE, 1);
702c5aa9e3bSLendacky, Thomas 		}
703c5aa9e3bSLendacky, Thomas 		if (channel->rx_ring) {
704c5aa9e3bSLendacky, Thomas 			/* Enable following Rx interrupts
705c5aa9e3bSLendacky, Thomas 			 *   RBUE - Receive Buffer Unavailable Enable
7069227dc5eSLendacky, Thomas 			 *   RIE  - Receive Interrupt Enable (unless using
7074c70dd8aSLendacky, Thomas 			 *          per channel interrupts in edge triggered
7084c70dd8aSLendacky, Thomas 			 *          mode)
709c5aa9e3bSLendacky, Thomas 			 */
710caa575afSLendacky, Thomas 			XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 1);
7114c70dd8aSLendacky, Thomas 			if (!pdata->per_channel_irq || pdata->channel_irq_mode)
712caa575afSLendacky, Thomas 				XGMAC_SET_BITS(channel->curr_ier,
713caa575afSLendacky, Thomas 					       DMA_CH_IER, RIE, 1);
714c5aa9e3bSLendacky, Thomas 		}
715c5aa9e3bSLendacky, Thomas 
716caa575afSLendacky, Thomas 		XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
717c5aa9e3bSLendacky, Thomas 	}
718c5aa9e3bSLendacky, Thomas }
719c5aa9e3bSLendacky, Thomas 
xgbe_enable_mtl_interrupts(struct xgbe_prv_data * pdata)720c5aa9e3bSLendacky, Thomas static void xgbe_enable_mtl_interrupts(struct xgbe_prv_data *pdata)
721c5aa9e3bSLendacky, Thomas {
722c5aa9e3bSLendacky, Thomas 	unsigned int mtl_q_isr;
723c5aa9e3bSLendacky, Thomas 	unsigned int q_count, i;
724c5aa9e3bSLendacky, Thomas 
725c5aa9e3bSLendacky, Thomas 	q_count = max(pdata->hw_feat.tx_q_cnt, pdata->hw_feat.rx_q_cnt);
726c5aa9e3bSLendacky, Thomas 	for (i = 0; i < q_count; i++) {
727c5aa9e3bSLendacky, Thomas 		/* Clear all the interrupts which are set */
728c5aa9e3bSLendacky, Thomas 		mtl_q_isr = XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR);
729c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_ISR, mtl_q_isr);
730c5aa9e3bSLendacky, Thomas 
731c5aa9e3bSLendacky, Thomas 		/* No MTL interrupts to be enabled */
73291f87345SLendacky, Thomas 		XGMAC_MTL_IOWRITE(pdata, i, MTL_Q_IER, 0);
733c5aa9e3bSLendacky, Thomas 	}
734c5aa9e3bSLendacky, Thomas }
735c5aa9e3bSLendacky, Thomas 
xgbe_enable_mac_interrupts(struct xgbe_prv_data * pdata)736c5aa9e3bSLendacky, Thomas static void xgbe_enable_mac_interrupts(struct xgbe_prv_data *pdata)
737c5aa9e3bSLendacky, Thomas {
73823e4eef7SLendacky, Thomas 	unsigned int mac_ier = 0;
73923e4eef7SLendacky, Thomas 
74023e4eef7SLendacky, Thomas 	/* Enable Timestamp interrupt */
74123e4eef7SLendacky, Thomas 	XGMAC_SET_BITS(mac_ier, MAC_IER, TSIE, 1);
74223e4eef7SLendacky, Thomas 
74323e4eef7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_IER, mac_ier);
744c5aa9e3bSLendacky, Thomas 
745c5aa9e3bSLendacky, Thomas 	/* Enable all counter interrupts */
746a3ba7c98SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_RIER, ALL_INTERRUPTS, 0xffffffff);
747a3ba7c98SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_TIER, ALL_INTERRUPTS, 0xffffffff);
748732f2ab7SLendacky, Thomas 
749732f2ab7SLendacky, Thomas 	/* Enable MDIO single command completion interrupt */
750732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_MDIOIER, SNGLCOMPIE, 1);
751c5aa9e3bSLendacky, Thomas }
752c5aa9e3bSLendacky, Thomas 
xgbe_enable_ecc_interrupts(struct xgbe_prv_data * pdata)753e78332b2SLendacky, Thomas static void xgbe_enable_ecc_interrupts(struct xgbe_prv_data *pdata)
754e78332b2SLendacky, Thomas {
755e78332b2SLendacky, Thomas 	unsigned int ecc_isr, ecc_ier = 0;
756e78332b2SLendacky, Thomas 
757e78332b2SLendacky, Thomas 	if (!pdata->vdata->ecc_support)
758e78332b2SLendacky, Thomas 		return;
759e78332b2SLendacky, Thomas 
760e78332b2SLendacky, Thomas 	/* Clear all the interrupts which are set */
761e78332b2SLendacky, Thomas 	ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
762e78332b2SLendacky, Thomas 	XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
763e78332b2SLendacky, Thomas 
764e78332b2SLendacky, Thomas 	/* Enable ECC interrupts */
765e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 1);
766e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 1);
767e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 1);
768e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 1);
769e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 1);
770e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 1);
771e78332b2SLendacky, Thomas 
772e78332b2SLendacky, Thomas 	XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
773e78332b2SLendacky, Thomas }
774e78332b2SLendacky, Thomas 
xgbe_disable_ecc_ded(struct xgbe_prv_data * pdata)775e78332b2SLendacky, Thomas static void xgbe_disable_ecc_ded(struct xgbe_prv_data *pdata)
776e78332b2SLendacky, Thomas {
777e78332b2SLendacky, Thomas 	unsigned int ecc_ier;
778e78332b2SLendacky, Thomas 
779e78332b2SLendacky, Thomas 	ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
780e78332b2SLendacky, Thomas 
781e78332b2SLendacky, Thomas 	/* Disable ECC DED interrupts */
782e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_DED, 0);
783e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_DED, 0);
784e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_DED, 0);
785e78332b2SLendacky, Thomas 
786e78332b2SLendacky, Thomas 	XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
787e78332b2SLendacky, Thomas }
788e78332b2SLendacky, Thomas 
xgbe_disable_ecc_sec(struct xgbe_prv_data * pdata,enum xgbe_ecc_sec sec)789e78332b2SLendacky, Thomas static void xgbe_disable_ecc_sec(struct xgbe_prv_data *pdata,
790e78332b2SLendacky, Thomas 				 enum xgbe_ecc_sec sec)
791e78332b2SLendacky, Thomas {
792e78332b2SLendacky, Thomas 	unsigned int ecc_ier;
793e78332b2SLendacky, Thomas 
794e78332b2SLendacky, Thomas 	ecc_ier = XP_IOREAD(pdata, XP_ECC_IER);
795e78332b2SLendacky, Thomas 
796e78332b2SLendacky, Thomas 	/* Disable ECC SEC interrupt */
797e78332b2SLendacky, Thomas 	switch (sec) {
798e78332b2SLendacky, Thomas 	case XGBE_ECC_SEC_TX:
799e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, TX_SEC, 0);
800e78332b2SLendacky, Thomas 		break;
801e78332b2SLendacky, Thomas 	case XGBE_ECC_SEC_RX:
802e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, RX_SEC, 0);
803e78332b2SLendacky, Thomas 		break;
804e78332b2SLendacky, Thomas 	case XGBE_ECC_SEC_DESC:
805e78332b2SLendacky, Thomas 	XP_SET_BITS(ecc_ier, XP_ECC_IER, DESC_SEC, 0);
806e78332b2SLendacky, Thomas 		break;
807e78332b2SLendacky, Thomas 	}
808e78332b2SLendacky, Thomas 
809e78332b2SLendacky, Thomas 	XP_IOWRITE(pdata, XP_ECC_IER, ecc_ier);
810e78332b2SLendacky, Thomas }
811e78332b2SLendacky, Thomas 
xgbe_set_speed(struct xgbe_prv_data * pdata,int speed)812e57f7a3fSLendacky, Thomas static int xgbe_set_speed(struct xgbe_prv_data *pdata, int speed)
813c5aa9e3bSLendacky, Thomas {
814e57f7a3fSLendacky, Thomas 	unsigned int ss;
815c3152d47SLendacky, Thomas 
816e57f7a3fSLendacky, Thomas 	switch (speed) {
81707445f3cSRaju Rangoju 	case SPEED_10:
81807445f3cSRaju Rangoju 		ss = 0x07;
81907445f3cSRaju Rangoju 		break;
820e57f7a3fSLendacky, Thomas 	case SPEED_1000:
821e57f7a3fSLendacky, Thomas 		ss = 0x03;
822e57f7a3fSLendacky, Thomas 		break;
823e57f7a3fSLendacky, Thomas 	case SPEED_2500:
824e57f7a3fSLendacky, Thomas 		ss = 0x02;
825e57f7a3fSLendacky, Thomas 		break;
826e57f7a3fSLendacky, Thomas 	case SPEED_10000:
827e57f7a3fSLendacky, Thomas 		ss = 0x00;
828e57f7a3fSLendacky, Thomas 		break;
829e57f7a3fSLendacky, Thomas 	default:
830e57f7a3fSLendacky, Thomas 		return -EINVAL;
831c5aa9e3bSLendacky, Thomas 	}
832c5aa9e3bSLendacky, Thomas 
833e57f7a3fSLendacky, Thomas 	if (XGMAC_IOREAD_BITS(pdata, MAC_TCR, SS) != ss)
834e57f7a3fSLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, MAC_TCR, SS, ss);
835c5aa9e3bSLendacky, Thomas 
836c5aa9e3bSLendacky, Thomas 	return 0;
837c5aa9e3bSLendacky, Thomas }
838c5aa9e3bSLendacky, Thomas 
xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data * pdata)839b4eee84fSLendacky, Thomas static int xgbe_enable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
840b4eee84fSLendacky, Thomas {
841b4eee84fSLendacky, Thomas 	/* Put the VLAN tag in the Rx descriptor */
842b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLRXS, 1);
843b4eee84fSLendacky, Thomas 
844b4eee84fSLendacky, Thomas 	/* Don't check the VLAN type */
845b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, DOVLTC, 1);
846b4eee84fSLendacky, Thomas 
847b4eee84fSLendacky, Thomas 	/* Check only C-TAG (0x8100) packets */
848b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ERSVLM, 0);
849b4eee84fSLendacky, Thomas 
850b4eee84fSLendacky, Thomas 	/* Don't consider an S-TAG (0x88A8) packet as a VLAN packet */
851b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ESVL, 0);
852b4eee84fSLendacky, Thomas 
853b4eee84fSLendacky, Thomas 	/* Enable VLAN tag stripping */
854b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0x3);
855b4eee84fSLendacky, Thomas 
856b4eee84fSLendacky, Thomas 	return 0;
857b4eee84fSLendacky, Thomas }
858b4eee84fSLendacky, Thomas 
xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data * pdata)859b4eee84fSLendacky, Thomas static int xgbe_disable_rx_vlan_stripping(struct xgbe_prv_data *pdata)
860b4eee84fSLendacky, Thomas {
861b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, EVLS, 0);
862b4eee84fSLendacky, Thomas 
863b4eee84fSLendacky, Thomas 	return 0;
864b4eee84fSLendacky, Thomas }
865b4eee84fSLendacky, Thomas 
xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data * pdata)866b4eee84fSLendacky, Thomas static int xgbe_enable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
867b4eee84fSLendacky, Thomas {
868b4eee84fSLendacky, Thomas 	/* Enable VLAN filtering */
869b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
870b4eee84fSLendacky, Thomas 
871b4eee84fSLendacky, Thomas 	/* Enable VLAN Hash Table filtering */
872b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTHM, 1);
873b4eee84fSLendacky, Thomas 
874b4eee84fSLendacky, Thomas 	/* Disable VLAN tag inverse matching */
875b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VTIM, 0);
876b4eee84fSLendacky, Thomas 
877b4eee84fSLendacky, Thomas 	/* Only filter on the lower 12-bits of the VLAN tag */
878b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, ETV, 1);
879b4eee84fSLendacky, Thomas 
880b4eee84fSLendacky, Thomas 	/* In order for the VLAN Hash Table filtering to be effective,
881b4eee84fSLendacky, Thomas 	 * the VLAN tag identifier in the VLAN Tag Register must not
882b4eee84fSLendacky, Thomas 	 * be zero.  Set the VLAN tag identifier to "1" to enable the
883b4eee84fSLendacky, Thomas 	 * VLAN Hash Table filtering.  This implies that a VLAN tag of
884b4eee84fSLendacky, Thomas 	 * 1 will always pass filtering.
885b4eee84fSLendacky, Thomas 	 */
886b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANTR, VL, 1);
887b4eee84fSLendacky, Thomas 
888b4eee84fSLendacky, Thomas 	return 0;
889b4eee84fSLendacky, Thomas }
890b4eee84fSLendacky, Thomas 
xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data * pdata)891b4eee84fSLendacky, Thomas static int xgbe_disable_rx_vlan_filtering(struct xgbe_prv_data *pdata)
892b4eee84fSLendacky, Thomas {
893b4eee84fSLendacky, Thomas 	/* Disable VLAN filtering */
894b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
895b4eee84fSLendacky, Thomas 
896b4eee84fSLendacky, Thomas 	return 0;
897b4eee84fSLendacky, Thomas }
898b4eee84fSLendacky, Thomas 
xgbe_vid_crc32_le(__le16 vid_le)899b4eee84fSLendacky, Thomas static u32 xgbe_vid_crc32_le(__le16 vid_le)
900b4eee84fSLendacky, Thomas {
901b4eee84fSLendacky, Thomas 	u32 crc = ~0;
902b4eee84fSLendacky, Thomas 	u32 temp = 0;
903b4eee84fSLendacky, Thomas 	unsigned char *data = (unsigned char *)&vid_le;
904b4eee84fSLendacky, Thomas 	unsigned char data_byte = 0;
905b4eee84fSLendacky, Thomas 	int i, bits;
906b4eee84fSLendacky, Thomas 
907b4eee84fSLendacky, Thomas 	bits = get_bitmask_order(VLAN_VID_MASK);
908b4eee84fSLendacky, Thomas 	for (i = 0; i < bits; i++) {
909b4eee84fSLendacky, Thomas 		if ((i % 8) == 0)
910b4eee84fSLendacky, Thomas 			data_byte = data[i / 8];
911b4eee84fSLendacky, Thomas 
912b4eee84fSLendacky, Thomas 		temp = ((crc & 1) ^ data_byte) & 1;
913b4eee84fSLendacky, Thomas 		crc >>= 1;
914b4eee84fSLendacky, Thomas 		data_byte >>= 1;
915b4eee84fSLendacky, Thomas 
916b4eee84fSLendacky, Thomas 		if (temp)
9175d258b48SKrzysztof Kozlowski 			crc ^= CRC32_POLY_LE;
918b4eee84fSLendacky, Thomas 	}
919b4eee84fSLendacky, Thomas 
920b4eee84fSLendacky, Thomas 	return crc;
921b4eee84fSLendacky, Thomas }
922b4eee84fSLendacky, Thomas 
xgbe_update_vlan_hash_table(struct xgbe_prv_data * pdata)923b4eee84fSLendacky, Thomas static int xgbe_update_vlan_hash_table(struct xgbe_prv_data *pdata)
924b4eee84fSLendacky, Thomas {
925b4eee84fSLendacky, Thomas 	u32 crc;
926b4eee84fSLendacky, Thomas 	u16 vid;
927b4eee84fSLendacky, Thomas 	__le16 vid_le;
928b4eee84fSLendacky, Thomas 	u16 vlan_hash_table = 0;
929b4eee84fSLendacky, Thomas 
930b4eee84fSLendacky, Thomas 	/* Generate the VLAN Hash Table value */
931b4eee84fSLendacky, Thomas 	for_each_set_bit(vid, pdata->active_vlans, VLAN_N_VID) {
932b4eee84fSLendacky, Thomas 		/* Get the CRC32 value of the VLAN ID */
933b4eee84fSLendacky, Thomas 		vid_le = cpu_to_le16(vid);
934b4eee84fSLendacky, Thomas 		crc = bitrev32(~xgbe_vid_crc32_le(vid_le)) >> 28;
935b4eee84fSLendacky, Thomas 
936b4eee84fSLendacky, Thomas 		vlan_hash_table |= (1 << crc);
937b4eee84fSLendacky, Thomas 	}
938b4eee84fSLendacky, Thomas 
939b4eee84fSLendacky, Thomas 	/* Set the VLAN Hash Table filtering register */
940b4eee84fSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANHTR, VLHT, vlan_hash_table);
941b4eee84fSLendacky, Thomas 
942b4eee84fSLendacky, Thomas 	return 0;
943b4eee84fSLendacky, Thomas }
944b4eee84fSLendacky, Thomas 
xgbe_set_promiscuous_mode(struct xgbe_prv_data * pdata,unsigned int enable)945c5aa9e3bSLendacky, Thomas static int xgbe_set_promiscuous_mode(struct xgbe_prv_data *pdata,
946c5aa9e3bSLendacky, Thomas 				     unsigned int enable)
947c5aa9e3bSLendacky, Thomas {
948c5aa9e3bSLendacky, Thomas 	unsigned int val = enable ? 1 : 0;
949c5aa9e3bSLendacky, Thomas 
950c5aa9e3bSLendacky, Thomas 	if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == val)
951c5aa9e3bSLendacky, Thomas 		return 0;
952c5aa9e3bSLendacky, Thomas 
95334bf65dfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev, "%s promiscuous mode\n",
95434bf65dfSLendacky, Thomas 		  enable ? "entering" : "leaving");
955c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, val);
956c5aa9e3bSLendacky, Thomas 
957b4eee84fSLendacky, Thomas 	/* Hardware will still perform VLAN filtering in promiscuous mode */
958b4eee84fSLendacky, Thomas 	if (enable) {
959b4eee84fSLendacky, Thomas 		xgbe_disable_rx_vlan_filtering(pdata);
960b4eee84fSLendacky, Thomas 	} else {
961b4eee84fSLendacky, Thomas 		if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
962b4eee84fSLendacky, Thomas 			xgbe_enable_rx_vlan_filtering(pdata);
963b4eee84fSLendacky, Thomas 	}
964b4eee84fSLendacky, Thomas 
965c5aa9e3bSLendacky, Thomas 	return 0;
966c5aa9e3bSLendacky, Thomas }
967c5aa9e3bSLendacky, Thomas 
xgbe_set_all_multicast_mode(struct xgbe_prv_data * pdata,unsigned int enable)968c5aa9e3bSLendacky, Thomas static int xgbe_set_all_multicast_mode(struct xgbe_prv_data *pdata,
969c5aa9e3bSLendacky, Thomas 				       unsigned int enable)
970c5aa9e3bSLendacky, Thomas {
971c5aa9e3bSLendacky, Thomas 	unsigned int val = enable ? 1 : 0;
972c5aa9e3bSLendacky, Thomas 
973c5aa9e3bSLendacky, Thomas 	if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PM) == val)
974c5aa9e3bSLendacky, Thomas 		return 0;
975c5aa9e3bSLendacky, Thomas 
97634bf65dfSLendacky, Thomas 	netif_dbg(pdata, drv, pdata->netdev, "%s allmulti mode\n",
97734bf65dfSLendacky, Thomas 		  enable ? "entering" : "leaving");
978c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PM, val);
979c5aa9e3bSLendacky, Thomas 
980c5aa9e3bSLendacky, Thomas 	return 0;
981c5aa9e3bSLendacky, Thomas }
982c5aa9e3bSLendacky, Thomas 
xgbe_set_mac_reg(struct xgbe_prv_data * pdata,struct netdev_hw_addr * ha,unsigned int * mac_reg)983b85e4d89SLendacky, Thomas static void xgbe_set_mac_reg(struct xgbe_prv_data *pdata,
984b85e4d89SLendacky, Thomas 			     struct netdev_hw_addr *ha, unsigned int *mac_reg)
985c5aa9e3bSLendacky, Thomas {
986c5aa9e3bSLendacky, Thomas 	unsigned int mac_addr_hi, mac_addr_lo;
987c5aa9e3bSLendacky, Thomas 	u8 *mac_addr;
988c5aa9e3bSLendacky, Thomas 
989c5aa9e3bSLendacky, Thomas 	mac_addr_lo = 0;
990c5aa9e3bSLendacky, Thomas 	mac_addr_hi = 0;
991b85e4d89SLendacky, Thomas 
992b85e4d89SLendacky, Thomas 	if (ha) {
993c5aa9e3bSLendacky, Thomas 		mac_addr = (u8 *)&mac_addr_lo;
994c5aa9e3bSLendacky, Thomas 		mac_addr[0] = ha->addr[0];
995c5aa9e3bSLendacky, Thomas 		mac_addr[1] = ha->addr[1];
996c5aa9e3bSLendacky, Thomas 		mac_addr[2] = ha->addr[2];
997c5aa9e3bSLendacky, Thomas 		mac_addr[3] = ha->addr[3];
998c5aa9e3bSLendacky, Thomas 		mac_addr = (u8 *)&mac_addr_hi;
999c5aa9e3bSLendacky, Thomas 		mac_addr[0] = ha->addr[4];
1000c5aa9e3bSLendacky, Thomas 		mac_addr[1] = ha->addr[5];
1001c5aa9e3bSLendacky, Thomas 
100234bf65dfSLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev,
100334bf65dfSLendacky, Thomas 			  "adding mac address %pM at %#x\n",
100434bf65dfSLendacky, Thomas 			  ha->addr, *mac_reg);
1005c5aa9e3bSLendacky, Thomas 
1006c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS(mac_addr_hi, MAC_MACA1HR, AE, 1);
1007c5aa9e3bSLendacky, Thomas 	}
1008c5aa9e3bSLendacky, Thomas 
1009b85e4d89SLendacky, Thomas 	XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_hi);
1010b85e4d89SLendacky, Thomas 	*mac_reg += MAC_MACA_INC;
1011b85e4d89SLendacky, Thomas 	XGMAC_IOWRITE(pdata, *mac_reg, mac_addr_lo);
1012b85e4d89SLendacky, Thomas 	*mac_reg += MAC_MACA_INC;
1013b85e4d89SLendacky, Thomas }
1014c5aa9e3bSLendacky, Thomas 
xgbe_set_mac_addn_addrs(struct xgbe_prv_data * pdata)1015b85e4d89SLendacky, Thomas static void xgbe_set_mac_addn_addrs(struct xgbe_prv_data *pdata)
1016b85e4d89SLendacky, Thomas {
1017b85e4d89SLendacky, Thomas 	struct net_device *netdev = pdata->netdev;
1018b85e4d89SLendacky, Thomas 	struct netdev_hw_addr *ha;
1019b85e4d89SLendacky, Thomas 	unsigned int mac_reg;
1020b85e4d89SLendacky, Thomas 	unsigned int addn_macs;
1021c5aa9e3bSLendacky, Thomas 
1022b85e4d89SLendacky, Thomas 	mac_reg = MAC_MACA1HR;
1023b85e4d89SLendacky, Thomas 	addn_macs = pdata->hw_feat.addn_mac;
1024c5aa9e3bSLendacky, Thomas 
1025b85e4d89SLendacky, Thomas 	if (netdev_uc_count(netdev) > addn_macs) {
1026b85e4d89SLendacky, Thomas 		xgbe_set_promiscuous_mode(pdata, 1);
1027b85e4d89SLendacky, Thomas 	} else {
1028b85e4d89SLendacky, Thomas 		netdev_for_each_uc_addr(ha, netdev) {
1029b85e4d89SLendacky, Thomas 			xgbe_set_mac_reg(pdata, ha, &mac_reg);
1030b85e4d89SLendacky, Thomas 			addn_macs--;
1031b85e4d89SLendacky, Thomas 		}
1032c5aa9e3bSLendacky, Thomas 
1033b85e4d89SLendacky, Thomas 		if (netdev_mc_count(netdev) > addn_macs) {
1034b85e4d89SLendacky, Thomas 			xgbe_set_all_multicast_mode(pdata, 1);
1035b85e4d89SLendacky, Thomas 		} else {
1036b85e4d89SLendacky, Thomas 			netdev_for_each_mc_addr(ha, netdev) {
1037b85e4d89SLendacky, Thomas 				xgbe_set_mac_reg(pdata, ha, &mac_reg);
1038b85e4d89SLendacky, Thomas 				addn_macs--;
1039b85e4d89SLendacky, Thomas 			}
1040c5aa9e3bSLendacky, Thomas 		}
1041c5aa9e3bSLendacky, Thomas 	}
1042c5aa9e3bSLendacky, Thomas 
1043c5aa9e3bSLendacky, Thomas 	/* Clear remaining additional MAC address entries */
1044b85e4d89SLendacky, Thomas 	while (addn_macs--)
1045b85e4d89SLendacky, Thomas 		xgbe_set_mac_reg(pdata, NULL, &mac_reg);
1046c5aa9e3bSLendacky, Thomas }
1047c5aa9e3bSLendacky, Thomas 
xgbe_set_mac_hash_table(struct xgbe_prv_data * pdata)1048b85e4d89SLendacky, Thomas static void xgbe_set_mac_hash_table(struct xgbe_prv_data *pdata)
1049b85e4d89SLendacky, Thomas {
1050b85e4d89SLendacky, Thomas 	struct net_device *netdev = pdata->netdev;
1051b85e4d89SLendacky, Thomas 	struct netdev_hw_addr *ha;
1052b85e4d89SLendacky, Thomas 	unsigned int hash_reg;
1053b85e4d89SLendacky, Thomas 	unsigned int hash_table_shift, hash_table_count;
1054b85e4d89SLendacky, Thomas 	u32 hash_table[XGBE_MAC_HASH_TABLE_SIZE];
1055b85e4d89SLendacky, Thomas 	u32 crc;
1056b85e4d89SLendacky, Thomas 	unsigned int i;
1057b85e4d89SLendacky, Thomas 
1058b85e4d89SLendacky, Thomas 	hash_table_shift = 26 - (pdata->hw_feat.hash_table_size >> 7);
1059b85e4d89SLendacky, Thomas 	hash_table_count = pdata->hw_feat.hash_table_size / 32;
1060b85e4d89SLendacky, Thomas 	memset(hash_table, 0, sizeof(hash_table));
1061b85e4d89SLendacky, Thomas 
1062b85e4d89SLendacky, Thomas 	/* Build the MAC Hash Table register values */
1063b85e4d89SLendacky, Thomas 	netdev_for_each_uc_addr(ha, netdev) {
1064b85e4d89SLendacky, Thomas 		crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1065b85e4d89SLendacky, Thomas 		crc >>= hash_table_shift;
1066b85e4d89SLendacky, Thomas 		hash_table[crc >> 5] |= (1 << (crc & 0x1f));
1067b85e4d89SLendacky, Thomas 	}
1068b85e4d89SLendacky, Thomas 
1069b85e4d89SLendacky, Thomas 	netdev_for_each_mc_addr(ha, netdev) {
1070b85e4d89SLendacky, Thomas 		crc = bitrev32(~crc32_le(~0, ha->addr, ETH_ALEN));
1071b85e4d89SLendacky, Thomas 		crc >>= hash_table_shift;
1072b85e4d89SLendacky, Thomas 		hash_table[crc >> 5] |= (1 << (crc & 0x1f));
1073b85e4d89SLendacky, Thomas 	}
1074b85e4d89SLendacky, Thomas 
1075b85e4d89SLendacky, Thomas 	/* Set the MAC Hash Table registers */
1076b85e4d89SLendacky, Thomas 	hash_reg = MAC_HTR0;
1077b85e4d89SLendacky, Thomas 	for (i = 0; i < hash_table_count; i++) {
1078b85e4d89SLendacky, Thomas 		XGMAC_IOWRITE(pdata, hash_reg, hash_table[i]);
1079b85e4d89SLendacky, Thomas 		hash_reg += MAC_HTR_INC;
1080b85e4d89SLendacky, Thomas 	}
1081b85e4d89SLendacky, Thomas }
1082b85e4d89SLendacky, Thomas 
xgbe_add_mac_addresses(struct xgbe_prv_data * pdata)1083b85e4d89SLendacky, Thomas static int xgbe_add_mac_addresses(struct xgbe_prv_data *pdata)
1084b85e4d89SLendacky, Thomas {
1085b85e4d89SLendacky, Thomas 	if (pdata->hw_feat.hash_table_size)
1086b85e4d89SLendacky, Thomas 		xgbe_set_mac_hash_table(pdata);
1087b85e4d89SLendacky, Thomas 	else
1088b85e4d89SLendacky, Thomas 		xgbe_set_mac_addn_addrs(pdata);
1089b85e4d89SLendacky, Thomas 
1090c5aa9e3bSLendacky, Thomas 	return 0;
1091c5aa9e3bSLendacky, Thomas }
1092c5aa9e3bSLendacky, Thomas 
xgbe_set_mac_address(struct xgbe_prv_data * pdata,const u8 * addr)109376660757SJakub Kicinski static int xgbe_set_mac_address(struct xgbe_prv_data *pdata, const u8 *addr)
1094c5aa9e3bSLendacky, Thomas {
1095c5aa9e3bSLendacky, Thomas 	unsigned int mac_addr_hi, mac_addr_lo;
1096c5aa9e3bSLendacky, Thomas 
1097c5aa9e3bSLendacky, Thomas 	mac_addr_hi = (addr[5] <<  8) | (addr[4] <<  0);
1098c5aa9e3bSLendacky, Thomas 	mac_addr_lo = (addr[3] << 24) | (addr[2] << 16) |
1099c5aa9e3bSLendacky, Thomas 		      (addr[1] <<  8) | (addr[0] <<  0);
1100c5aa9e3bSLendacky, Thomas 
1101c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MACA0HR, mac_addr_hi);
1102c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MACA0LR, mac_addr_lo);
1103c5aa9e3bSLendacky, Thomas 
1104c5aa9e3bSLendacky, Thomas 	return 0;
1105c5aa9e3bSLendacky, Thomas }
1106c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_mode(struct xgbe_prv_data * pdata)1107b876382bSLendacky, Thomas static int xgbe_config_rx_mode(struct xgbe_prv_data *pdata)
1108b876382bSLendacky, Thomas {
1109b876382bSLendacky, Thomas 	struct net_device *netdev = pdata->netdev;
1110b876382bSLendacky, Thomas 	unsigned int pr_mode, am_mode;
1111b876382bSLendacky, Thomas 
1112b876382bSLendacky, Thomas 	pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1113b876382bSLendacky, Thomas 	am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1114b876382bSLendacky, Thomas 
1115b876382bSLendacky, Thomas 	xgbe_set_promiscuous_mode(pdata, pr_mode);
1116b876382bSLendacky, Thomas 	xgbe_set_all_multicast_mode(pdata, am_mode);
1117b876382bSLendacky, Thomas 
1118b876382bSLendacky, Thomas 	xgbe_add_mac_addresses(pdata);
1119b876382bSLendacky, Thomas 
1120b876382bSLendacky, Thomas 	return 0;
1121b876382bSLendacky, Thomas }
1122b876382bSLendacky, Thomas 
xgbe_clr_gpio(struct xgbe_prv_data * pdata,unsigned int gpio)1123732f2ab7SLendacky, Thomas static int xgbe_clr_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1124732f2ab7SLendacky, Thomas {
1125732f2ab7SLendacky, Thomas 	unsigned int reg;
1126732f2ab7SLendacky, Thomas 
11271c1f619eSLendacky, Thomas 	if (gpio > 15)
1128732f2ab7SLendacky, Thomas 		return -EINVAL;
1129732f2ab7SLendacky, Thomas 
1130732f2ab7SLendacky, Thomas 	reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1131732f2ab7SLendacky, Thomas 
1132732f2ab7SLendacky, Thomas 	reg &= ~(1 << (gpio + 16));
1133732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1134732f2ab7SLendacky, Thomas 
1135732f2ab7SLendacky, Thomas 	return 0;
1136732f2ab7SLendacky, Thomas }
1137732f2ab7SLendacky, Thomas 
xgbe_set_gpio(struct xgbe_prv_data * pdata,unsigned int gpio)1138732f2ab7SLendacky, Thomas static int xgbe_set_gpio(struct xgbe_prv_data *pdata, unsigned int gpio)
1139732f2ab7SLendacky, Thomas {
1140732f2ab7SLendacky, Thomas 	unsigned int reg;
1141732f2ab7SLendacky, Thomas 
11421c1f619eSLendacky, Thomas 	if (gpio > 15)
1143732f2ab7SLendacky, Thomas 		return -EINVAL;
1144732f2ab7SLendacky, Thomas 
1145732f2ab7SLendacky, Thomas 	reg = XGMAC_IOREAD(pdata, MAC_GPIOSR);
1146732f2ab7SLendacky, Thomas 
1147732f2ab7SLendacky, Thomas 	reg |= (1 << (gpio + 16));
1148732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_GPIOSR, reg);
1149732f2ab7SLendacky, Thomas 
1150732f2ab7SLendacky, Thomas 	return 0;
1151732f2ab7SLendacky, Thomas }
1152732f2ab7SLendacky, Thomas 
xgbe_read_mmd_regs_v2(struct xgbe_prv_data * pdata,int prtad,int mmd_reg)1153b03a4a6fSLendacky, Thomas static int xgbe_read_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1154b03a4a6fSLendacky, Thomas 				 int mmd_reg)
1155b03a4a6fSLendacky, Thomas {
1156b03a4a6fSLendacky, Thomas 	unsigned long flags;
1157b03a4a6fSLendacky, Thomas 	unsigned int mmd_address, index, offset;
1158b03a4a6fSLendacky, Thomas 	int mmd_data;
1159b03a4a6fSLendacky, Thomas 
1160*47e61593SAndrew Lunn 	if (mmd_reg & XGBE_ADDR_C45)
1161*47e61593SAndrew Lunn 		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
1162b03a4a6fSLendacky, Thomas 	else
1163b03a4a6fSLendacky, Thomas 		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1164b03a4a6fSLendacky, Thomas 
1165b03a4a6fSLendacky, Thomas 	/* The PCS registers are accessed using mmio. The underlying
1166b03a4a6fSLendacky, Thomas 	 * management interface uses indirect addressing to access the MMD
1167b03a4a6fSLendacky, Thomas 	 * register sets. This requires accessing of the PCS register in two
1168b03a4a6fSLendacky, Thomas 	 * phases, an address phase and a data phase.
1169b03a4a6fSLendacky, Thomas 	 *
1170b03a4a6fSLendacky, Thomas 	 * The mmio interface is based on 16-bit offsets and values. All
1171b03a4a6fSLendacky, Thomas 	 * register offsets must therefore be adjusted by left shifting the
1172b03a4a6fSLendacky, Thomas 	 * offset 1 bit and reading 16 bits of data.
1173b03a4a6fSLendacky, Thomas 	 */
1174b03a4a6fSLendacky, Thomas 	mmd_address <<= 1;
1175b03a4a6fSLendacky, Thomas 	index = mmd_address & ~pdata->xpcs_window_mask;
1176b03a4a6fSLendacky, Thomas 	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1177b03a4a6fSLendacky, Thomas 
1178b03a4a6fSLendacky, Thomas 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
11794eccbfc3SLendacky, Thomas 	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
1180b03a4a6fSLendacky, Thomas 	mmd_data = XPCS16_IOREAD(pdata, offset);
1181b03a4a6fSLendacky, Thomas 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1182b03a4a6fSLendacky, Thomas 
1183b03a4a6fSLendacky, Thomas 	return mmd_data;
1184b03a4a6fSLendacky, Thomas }
1185b03a4a6fSLendacky, Thomas 
xgbe_write_mmd_regs_v2(struct xgbe_prv_data * pdata,int prtad,int mmd_reg,int mmd_data)1186b03a4a6fSLendacky, Thomas static void xgbe_write_mmd_regs_v2(struct xgbe_prv_data *pdata, int prtad,
1187b03a4a6fSLendacky, Thomas 				   int mmd_reg, int mmd_data)
1188b03a4a6fSLendacky, Thomas {
1189b03a4a6fSLendacky, Thomas 	unsigned long flags;
1190b03a4a6fSLendacky, Thomas 	unsigned int mmd_address, index, offset;
1191b03a4a6fSLendacky, Thomas 
1192*47e61593SAndrew Lunn 	if (mmd_reg & XGBE_ADDR_C45)
1193*47e61593SAndrew Lunn 		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
1194b03a4a6fSLendacky, Thomas 	else
1195b03a4a6fSLendacky, Thomas 		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1196b03a4a6fSLendacky, Thomas 
1197b03a4a6fSLendacky, Thomas 	/* The PCS registers are accessed using mmio. The underlying
1198b03a4a6fSLendacky, Thomas 	 * management interface uses indirect addressing to access the MMD
1199b03a4a6fSLendacky, Thomas 	 * register sets. This requires accessing of the PCS register in two
1200b03a4a6fSLendacky, Thomas 	 * phases, an address phase and a data phase.
1201b03a4a6fSLendacky, Thomas 	 *
1202b03a4a6fSLendacky, Thomas 	 * The mmio interface is based on 16-bit offsets and values. All
1203b03a4a6fSLendacky, Thomas 	 * register offsets must therefore be adjusted by left shifting the
1204b03a4a6fSLendacky, Thomas 	 * offset 1 bit and writing 16 bits of data.
1205b03a4a6fSLendacky, Thomas 	 */
1206b03a4a6fSLendacky, Thomas 	mmd_address <<= 1;
1207b03a4a6fSLendacky, Thomas 	index = mmd_address & ~pdata->xpcs_window_mask;
1208b03a4a6fSLendacky, Thomas 	offset = pdata->xpcs_window + (mmd_address & pdata->xpcs_window_mask);
1209b03a4a6fSLendacky, Thomas 
1210b03a4a6fSLendacky, Thomas 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
12114eccbfc3SLendacky, Thomas 	XPCS32_IOWRITE(pdata, pdata->xpcs_window_sel_reg, index);
1212b03a4a6fSLendacky, Thomas 	XPCS16_IOWRITE(pdata, offset, mmd_data);
1213b03a4a6fSLendacky, Thomas 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1214b03a4a6fSLendacky, Thomas }
1215b03a4a6fSLendacky, Thomas 
xgbe_read_mmd_regs_v1(struct xgbe_prv_data * pdata,int prtad,int mmd_reg)1216b03a4a6fSLendacky, Thomas static int xgbe_read_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1217c5aa9e3bSLendacky, Thomas 				 int mmd_reg)
1218c5aa9e3bSLendacky, Thomas {
1219ced3fcaeSLendacky, Thomas 	unsigned long flags;
1220c5aa9e3bSLendacky, Thomas 	unsigned int mmd_address;
1221c5aa9e3bSLendacky, Thomas 	int mmd_data;
1222c5aa9e3bSLendacky, Thomas 
1223*47e61593SAndrew Lunn 	if (mmd_reg & XGBE_ADDR_C45)
1224*47e61593SAndrew Lunn 		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
1225c5aa9e3bSLendacky, Thomas 	else
1226c5aa9e3bSLendacky, Thomas 		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1227c5aa9e3bSLendacky, Thomas 
1228c5aa9e3bSLendacky, Thomas 	/* The PCS registers are accessed using mmio. The underlying APB3
1229c5aa9e3bSLendacky, Thomas 	 * management interface uses indirect addressing to access the MMD
1230c5aa9e3bSLendacky, Thomas 	 * register sets. This requires accessing of the PCS register in two
1231c5aa9e3bSLendacky, Thomas 	 * phases, an address phase and a data phase.
1232c5aa9e3bSLendacky, Thomas 	 *
1233c5aa9e3bSLendacky, Thomas 	 * The mmio interface is based on 32-bit offsets and values. All
1234c5aa9e3bSLendacky, Thomas 	 * register offsets must therefore be adjusted by left shifting the
1235c5aa9e3bSLendacky, Thomas 	 * offset 2 bits and reading 32 bits of data.
1236c5aa9e3bSLendacky, Thomas 	 */
1237ced3fcaeSLendacky, Thomas 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
1238b03a4a6fSLendacky, Thomas 	XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1239b03a4a6fSLendacky, Thomas 	mmd_data = XPCS32_IOREAD(pdata, (mmd_address & 0xff) << 2);
1240ced3fcaeSLendacky, Thomas 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1241c5aa9e3bSLendacky, Thomas 
1242c5aa9e3bSLendacky, Thomas 	return mmd_data;
1243c5aa9e3bSLendacky, Thomas }
1244c5aa9e3bSLendacky, Thomas 
xgbe_write_mmd_regs_v1(struct xgbe_prv_data * pdata,int prtad,int mmd_reg,int mmd_data)1245b03a4a6fSLendacky, Thomas static void xgbe_write_mmd_regs_v1(struct xgbe_prv_data *pdata, int prtad,
1246c5aa9e3bSLendacky, Thomas 				   int mmd_reg, int mmd_data)
1247c5aa9e3bSLendacky, Thomas {
1248c5aa9e3bSLendacky, Thomas 	unsigned int mmd_address;
1249ced3fcaeSLendacky, Thomas 	unsigned long flags;
1250c5aa9e3bSLendacky, Thomas 
1251*47e61593SAndrew Lunn 	if (mmd_reg & XGBE_ADDR_C45)
1252*47e61593SAndrew Lunn 		mmd_address = mmd_reg & ~XGBE_ADDR_C45;
1253c5aa9e3bSLendacky, Thomas 	else
1254c5aa9e3bSLendacky, Thomas 		mmd_address = (pdata->mdio_mmd << 16) | (mmd_reg & 0xffff);
1255c5aa9e3bSLendacky, Thomas 
1256c5aa9e3bSLendacky, Thomas 	/* The PCS registers are accessed using mmio. The underlying APB3
1257c5aa9e3bSLendacky, Thomas 	 * management interface uses indirect addressing to access the MMD
1258c5aa9e3bSLendacky, Thomas 	 * register sets. This requires accessing of the PCS register in two
1259c5aa9e3bSLendacky, Thomas 	 * phases, an address phase and a data phase.
1260c5aa9e3bSLendacky, Thomas 	 *
1261c5aa9e3bSLendacky, Thomas 	 * The mmio interface is based on 32-bit offsets and values. All
1262c5aa9e3bSLendacky, Thomas 	 * register offsets must therefore be adjusted by left shifting the
1263b03a4a6fSLendacky, Thomas 	 * offset 2 bits and writing 32 bits of data.
1264c5aa9e3bSLendacky, Thomas 	 */
1265ced3fcaeSLendacky, Thomas 	spin_lock_irqsave(&pdata->xpcs_lock, flags);
1266b03a4a6fSLendacky, Thomas 	XPCS32_IOWRITE(pdata, PCS_V1_WINDOW_SELECT, mmd_address >> 8);
1267b03a4a6fSLendacky, Thomas 	XPCS32_IOWRITE(pdata, (mmd_address & 0xff) << 2, mmd_data);
1268ced3fcaeSLendacky, Thomas 	spin_unlock_irqrestore(&pdata->xpcs_lock, flags);
1269c5aa9e3bSLendacky, Thomas }
1270c5aa9e3bSLendacky, Thomas 
xgbe_read_mmd_regs(struct xgbe_prv_data * pdata,int prtad,int mmd_reg)1271b03a4a6fSLendacky, Thomas static int xgbe_read_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1272b03a4a6fSLendacky, Thomas 			      int mmd_reg)
1273b03a4a6fSLendacky, Thomas {
1274b03a4a6fSLendacky, Thomas 	switch (pdata->vdata->xpcs_access) {
1275b03a4a6fSLendacky, Thomas 	case XGBE_XPCS_ACCESS_V1:
1276b03a4a6fSLendacky, Thomas 		return xgbe_read_mmd_regs_v1(pdata, prtad, mmd_reg);
1277b03a4a6fSLendacky, Thomas 
1278b03a4a6fSLendacky, Thomas 	case XGBE_XPCS_ACCESS_V2:
1279b03a4a6fSLendacky, Thomas 	default:
1280b03a4a6fSLendacky, Thomas 		return xgbe_read_mmd_regs_v2(pdata, prtad, mmd_reg);
1281b03a4a6fSLendacky, Thomas 	}
1282b03a4a6fSLendacky, Thomas }
1283b03a4a6fSLendacky, Thomas 
xgbe_write_mmd_regs(struct xgbe_prv_data * pdata,int prtad,int mmd_reg,int mmd_data)1284b03a4a6fSLendacky, Thomas static void xgbe_write_mmd_regs(struct xgbe_prv_data *pdata, int prtad,
1285b03a4a6fSLendacky, Thomas 				int mmd_reg, int mmd_data)
1286b03a4a6fSLendacky, Thomas {
1287b03a4a6fSLendacky, Thomas 	switch (pdata->vdata->xpcs_access) {
1288b03a4a6fSLendacky, Thomas 	case XGBE_XPCS_ACCESS_V1:
1289b03a4a6fSLendacky, Thomas 		return xgbe_write_mmd_regs_v1(pdata, prtad, mmd_reg, mmd_data);
1290b03a4a6fSLendacky, Thomas 
1291b03a4a6fSLendacky, Thomas 	case XGBE_XPCS_ACCESS_V2:
1292b03a4a6fSLendacky, Thomas 	default:
1293b03a4a6fSLendacky, Thomas 		return xgbe_write_mmd_regs_v2(pdata, prtad, mmd_reg, mmd_data);
1294b03a4a6fSLendacky, Thomas 	}
1295b03a4a6fSLendacky, Thomas }
1296b03a4a6fSLendacky, Thomas 
xgbe_create_mdio_sca_c22(int port,int reg)1297070f6186SAndrew Lunn static unsigned int xgbe_create_mdio_sca_c22(int port, int reg)
12985ab3121bSLendacky, Thomas {
1299070f6186SAndrew Lunn 	unsigned int mdio_sca;
13005ab3121bSLendacky, Thomas 
1301070f6186SAndrew Lunn 	mdio_sca = 0;
1302070f6186SAndrew Lunn 	XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, RA, reg);
1303070f6186SAndrew Lunn 	XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, PA, port);
1304070f6186SAndrew Lunn 
1305070f6186SAndrew Lunn 	return mdio_sca;
1306070f6186SAndrew Lunn }
1307070f6186SAndrew Lunn 
xgbe_create_mdio_sca_c45(int port,unsigned int da,int reg)1308070f6186SAndrew Lunn static unsigned int xgbe_create_mdio_sca_c45(int port, unsigned int da, int reg)
1309070f6186SAndrew Lunn {
1310070f6186SAndrew Lunn 	unsigned int mdio_sca;
13115ab3121bSLendacky, Thomas 
13125ab3121bSLendacky, Thomas 	mdio_sca = 0;
13135ab3121bSLendacky, Thomas 	XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, RA, reg);
13145ab3121bSLendacky, Thomas 	XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, PA, port);
13155ab3121bSLendacky, Thomas 	XGMAC_SET_BITS(mdio_sca, MAC_MDIOSCAR, DA, da);
13165ab3121bSLendacky, Thomas 
13175ab3121bSLendacky, Thomas 	return mdio_sca;
13185ab3121bSLendacky, Thomas }
13195ab3121bSLendacky, Thomas 
xgbe_write_ext_mii_regs(struct xgbe_prv_data * pdata,unsigned int mdio_sca,u16 val)1320070f6186SAndrew Lunn static int xgbe_write_ext_mii_regs(struct xgbe_prv_data *pdata,
1321070f6186SAndrew Lunn 				   unsigned int mdio_sca, u16 val)
1322732f2ab7SLendacky, Thomas {
1323070f6186SAndrew Lunn 	unsigned int mdio_sccd;
1324732f2ab7SLendacky, Thomas 
1325732f2ab7SLendacky, Thomas 	reinit_completion(&pdata->mdio_complete);
1326732f2ab7SLendacky, Thomas 
1327732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1328732f2ab7SLendacky, Thomas 
1329732f2ab7SLendacky, Thomas 	mdio_sccd = 0;
1330732f2ab7SLendacky, Thomas 	XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, DATA, val);
1331732f2ab7SLendacky, Thomas 	XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 1);
1332732f2ab7SLendacky, Thomas 	XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1333732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1334732f2ab7SLendacky, Thomas 
1335732f2ab7SLendacky, Thomas 	if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1336732f2ab7SLendacky, Thomas 		netdev_err(pdata->netdev, "mdio write operation timed out\n");
1337732f2ab7SLendacky, Thomas 		return -ETIMEDOUT;
1338732f2ab7SLendacky, Thomas 	}
1339732f2ab7SLendacky, Thomas 
1340732f2ab7SLendacky, Thomas 	return 0;
1341732f2ab7SLendacky, Thomas }
1342732f2ab7SLendacky, Thomas 
xgbe_write_ext_mii_regs_c22(struct xgbe_prv_data * pdata,int addr,int reg,u16 val)1343070f6186SAndrew Lunn static int xgbe_write_ext_mii_regs_c22(struct xgbe_prv_data *pdata, int addr,
1344070f6186SAndrew Lunn 				       int reg, u16 val)
1345732f2ab7SLendacky, Thomas {
1346070f6186SAndrew Lunn 	unsigned int mdio_sca;
1347070f6186SAndrew Lunn 
1348070f6186SAndrew Lunn 	mdio_sca = xgbe_create_mdio_sca_c22(addr, reg);
1349070f6186SAndrew Lunn 
1350070f6186SAndrew Lunn 	return xgbe_write_ext_mii_regs(pdata, mdio_sca, val);
1351070f6186SAndrew Lunn }
1352070f6186SAndrew Lunn 
xgbe_write_ext_mii_regs_c45(struct xgbe_prv_data * pdata,int addr,int devad,int reg,u16 val)1353070f6186SAndrew Lunn static int xgbe_write_ext_mii_regs_c45(struct xgbe_prv_data *pdata, int addr,
1354070f6186SAndrew Lunn 				       int devad, int reg, u16 val)
1355070f6186SAndrew Lunn {
1356070f6186SAndrew Lunn 	unsigned int mdio_sca;
1357070f6186SAndrew Lunn 
1358070f6186SAndrew Lunn 	mdio_sca = xgbe_create_mdio_sca_c45(addr, devad, reg);
1359070f6186SAndrew Lunn 
1360070f6186SAndrew Lunn 	return xgbe_write_ext_mii_regs(pdata, mdio_sca, val);
1361070f6186SAndrew Lunn }
1362070f6186SAndrew Lunn 
xgbe_read_ext_mii_regs(struct xgbe_prv_data * pdata,unsigned int mdio_sca)1363070f6186SAndrew Lunn static int xgbe_read_ext_mii_regs(struct xgbe_prv_data *pdata,
1364070f6186SAndrew Lunn 				  unsigned int mdio_sca)
1365070f6186SAndrew Lunn {
1366070f6186SAndrew Lunn 	unsigned int mdio_sccd;
1367732f2ab7SLendacky, Thomas 
1368732f2ab7SLendacky, Thomas 	reinit_completion(&pdata->mdio_complete);
1369732f2ab7SLendacky, Thomas 
1370732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MDIOSCAR, mdio_sca);
1371732f2ab7SLendacky, Thomas 
1372732f2ab7SLendacky, Thomas 	mdio_sccd = 0;
1373732f2ab7SLendacky, Thomas 	XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, CMD, 3);
1374732f2ab7SLendacky, Thomas 	XGMAC_SET_BITS(mdio_sccd, MAC_MDIOSCCDR, BUSY, 1);
1375732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MDIOSCCDR, mdio_sccd);
1376732f2ab7SLendacky, Thomas 
1377732f2ab7SLendacky, Thomas 	if (!wait_for_completion_timeout(&pdata->mdio_complete, HZ)) {
1378732f2ab7SLendacky, Thomas 		netdev_err(pdata->netdev, "mdio read operation timed out\n");
1379732f2ab7SLendacky, Thomas 		return -ETIMEDOUT;
1380732f2ab7SLendacky, Thomas 	}
1381732f2ab7SLendacky, Thomas 
1382732f2ab7SLendacky, Thomas 	return XGMAC_IOREAD_BITS(pdata, MAC_MDIOSCCDR, DATA);
1383732f2ab7SLendacky, Thomas }
1384732f2ab7SLendacky, Thomas 
xgbe_read_ext_mii_regs_c22(struct xgbe_prv_data * pdata,int addr,int reg)1385070f6186SAndrew Lunn static int xgbe_read_ext_mii_regs_c22(struct xgbe_prv_data *pdata, int addr,
1386070f6186SAndrew Lunn 				      int reg)
1387070f6186SAndrew Lunn {
1388070f6186SAndrew Lunn 	unsigned int mdio_sca;
1389070f6186SAndrew Lunn 
1390070f6186SAndrew Lunn 	mdio_sca = xgbe_create_mdio_sca_c22(addr, reg);
1391070f6186SAndrew Lunn 
1392070f6186SAndrew Lunn 	return xgbe_read_ext_mii_regs(pdata, mdio_sca);
1393070f6186SAndrew Lunn }
1394070f6186SAndrew Lunn 
xgbe_read_ext_mii_regs_c45(struct xgbe_prv_data * pdata,int addr,int devad,int reg)1395070f6186SAndrew Lunn static int xgbe_read_ext_mii_regs_c45(struct xgbe_prv_data *pdata, int addr,
1396070f6186SAndrew Lunn 				      int devad, int reg)
1397070f6186SAndrew Lunn {
1398070f6186SAndrew Lunn 	unsigned int mdio_sca;
1399070f6186SAndrew Lunn 
1400070f6186SAndrew Lunn 	mdio_sca = xgbe_create_mdio_sca_c45(addr, devad, reg);
1401070f6186SAndrew Lunn 
1402070f6186SAndrew Lunn 	return xgbe_read_ext_mii_regs(pdata, mdio_sca);
1403070f6186SAndrew Lunn }
1404070f6186SAndrew Lunn 
xgbe_set_ext_mii_mode(struct xgbe_prv_data * pdata,unsigned int port,enum xgbe_mdio_mode mode)1405732f2ab7SLendacky, Thomas static int xgbe_set_ext_mii_mode(struct xgbe_prv_data *pdata, unsigned int port,
1406732f2ab7SLendacky, Thomas 				 enum xgbe_mdio_mode mode)
1407732f2ab7SLendacky, Thomas {
1408b42c6761SLendacky, Thomas 	unsigned int reg_val = XGMAC_IOREAD(pdata, MAC_MDIOCL22R);
1409732f2ab7SLendacky, Thomas 
1410732f2ab7SLendacky, Thomas 	switch (mode) {
1411732f2ab7SLendacky, Thomas 	case XGBE_MDIO_MODE_CL22:
1412732f2ab7SLendacky, Thomas 		if (port > XGMAC_MAX_C22_PORT)
1413732f2ab7SLendacky, Thomas 			return -EINVAL;
1414732f2ab7SLendacky, Thomas 		reg_val |= (1 << port);
1415732f2ab7SLendacky, Thomas 		break;
1416732f2ab7SLendacky, Thomas 	case XGBE_MDIO_MODE_CL45:
1417732f2ab7SLendacky, Thomas 		break;
1418732f2ab7SLendacky, Thomas 	default:
1419732f2ab7SLendacky, Thomas 		return -EINVAL;
1420732f2ab7SLendacky, Thomas 	}
1421732f2ab7SLendacky, Thomas 
1422732f2ab7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_MDIOCL22R, reg_val);
1423732f2ab7SLendacky, Thomas 
1424732f2ab7SLendacky, Thomas 	return 0;
1425732f2ab7SLendacky, Thomas }
1426732f2ab7SLendacky, Thomas 
xgbe_tx_complete(struct xgbe_ring_desc * rdesc)1427c5aa9e3bSLendacky, Thomas static int xgbe_tx_complete(struct xgbe_ring_desc *rdesc)
1428c5aa9e3bSLendacky, Thomas {
1429c5aa9e3bSLendacky, Thomas 	return !XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN);
1430c5aa9e3bSLendacky, Thomas }
1431c5aa9e3bSLendacky, Thomas 
xgbe_disable_rx_csum(struct xgbe_prv_data * pdata)1432c5aa9e3bSLendacky, Thomas static int xgbe_disable_rx_csum(struct xgbe_prv_data *pdata)
1433c5aa9e3bSLendacky, Thomas {
1434c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 0);
1435c5aa9e3bSLendacky, Thomas 
1436c5aa9e3bSLendacky, Thomas 	return 0;
1437c5aa9e3bSLendacky, Thomas }
1438c5aa9e3bSLendacky, Thomas 
xgbe_enable_rx_csum(struct xgbe_prv_data * pdata)1439c5aa9e3bSLendacky, Thomas static int xgbe_enable_rx_csum(struct xgbe_prv_data *pdata)
1440c5aa9e3bSLendacky, Thomas {
1441c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, IPC, 1);
1442c5aa9e3bSLendacky, Thomas 
1443c5aa9e3bSLendacky, Thomas 	return 0;
1444c5aa9e3bSLendacky, Thomas }
1445c5aa9e3bSLendacky, Thomas 
xgbe_tx_desc_reset(struct xgbe_ring_data * rdata)1446c5aa9e3bSLendacky, Thomas static void xgbe_tx_desc_reset(struct xgbe_ring_data *rdata)
1447c5aa9e3bSLendacky, Thomas {
1448c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc = rdata->rdesc;
1449c5aa9e3bSLendacky, Thomas 
1450c5aa9e3bSLendacky, Thomas 	/* Reset the Tx descriptor
1451c5aa9e3bSLendacky, Thomas 	 *   Set buffer 1 (lo) address to zero
1452c5aa9e3bSLendacky, Thomas 	 *   Set buffer 1 (hi) address to zero
1453c5aa9e3bSLendacky, Thomas 	 *   Reset all other control bits (IC, TTSE, B2L & B1L)
1454c5aa9e3bSLendacky, Thomas 	 *   Reset all other control bits (OWN, CTXT, FD, LD, CPC, CIC, etc)
1455c5aa9e3bSLendacky, Thomas 	 */
1456c5aa9e3bSLendacky, Thomas 	rdesc->desc0 = 0;
1457c5aa9e3bSLendacky, Thomas 	rdesc->desc1 = 0;
1458c5aa9e3bSLendacky, Thomas 	rdesc->desc2 = 0;
1459c5aa9e3bSLendacky, Thomas 	rdesc->desc3 = 0;
146008dcc47cSLendacky, Thomas 
146108dcc47cSLendacky, Thomas 	/* Make sure ownership is written to the descriptor */
1462ceb8f6beSLendacky, Thomas 	dma_wmb();
1463c5aa9e3bSLendacky, Thomas }
1464c5aa9e3bSLendacky, Thomas 
xgbe_tx_desc_init(struct xgbe_channel * channel)1465c5aa9e3bSLendacky, Thomas static void xgbe_tx_desc_init(struct xgbe_channel *channel)
1466c5aa9e3bSLendacky, Thomas {
1467c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->tx_ring;
1468c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
1469c5aa9e3bSLendacky, Thomas 	int i;
1470c5aa9e3bSLendacky, Thomas 	int start_index = ring->cur;
1471c5aa9e3bSLendacky, Thomas 
1472c5aa9e3bSLendacky, Thomas 	DBGPR("-->tx_desc_init\n");
1473c5aa9e3bSLendacky, Thomas 
1474c5aa9e3bSLendacky, Thomas 	/* Initialze all descriptors */
1475c5aa9e3bSLendacky, Thomas 	for (i = 0; i < ring->rdesc_count; i++) {
1476d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, i);
1477c5aa9e3bSLendacky, Thomas 
147808dcc47cSLendacky, Thomas 		/* Initialize Tx descriptor */
147908dcc47cSLendacky, Thomas 		xgbe_tx_desc_reset(rdata);
1480c5aa9e3bSLendacky, Thomas 	}
1481c5aa9e3bSLendacky, Thomas 
1482c5aa9e3bSLendacky, Thomas 	/* Update the total number of Tx descriptors */
1483c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_TDRLR, ring->rdesc_count - 1);
1484c5aa9e3bSLendacky, Thomas 
1485c5aa9e3bSLendacky, Thomas 	/* Update the starting address of descriptor ring */
1486d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, start_index);
1487c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_HI,
1488c5aa9e3bSLendacky, Thomas 			  upper_32_bits(rdata->rdesc_dma));
1489c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_TDLR_LO,
1490c5aa9e3bSLendacky, Thomas 			  lower_32_bits(rdata->rdesc_dma));
1491c5aa9e3bSLendacky, Thomas 
1492c5aa9e3bSLendacky, Thomas 	DBGPR("<--tx_desc_init\n");
1493c5aa9e3bSLendacky, Thomas }
1494c5aa9e3bSLendacky, Thomas 
xgbe_rx_desc_reset(struct xgbe_prv_data * pdata,struct xgbe_ring_data * rdata,unsigned int index)14958dee19e6SLendacky, Thomas static void xgbe_rx_desc_reset(struct xgbe_prv_data *pdata,
14968dee19e6SLendacky, Thomas 			       struct xgbe_ring_data *rdata, unsigned int index)
1497c5aa9e3bSLendacky, Thomas {
1498c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc = rdata->rdesc;
14998dee19e6SLendacky, Thomas 	unsigned int rx_usecs = pdata->rx_usecs;
15008dee19e6SLendacky, Thomas 	unsigned int rx_frames = pdata->rx_frames;
15018dee19e6SLendacky, Thomas 	unsigned int inte;
1502cfbfd86bSLendacky, Thomas 	dma_addr_t hdr_dma, buf_dma;
15038dee19e6SLendacky, Thomas 
15048dee19e6SLendacky, Thomas 	if (!rx_usecs && !rx_frames) {
15058dee19e6SLendacky, Thomas 		/* No coalescing, interrupt for every descriptor */
15068dee19e6SLendacky, Thomas 		inte = 1;
15078dee19e6SLendacky, Thomas 	} else {
15088dee19e6SLendacky, Thomas 		/* Set interrupt based on Rx frame coalescing setting */
15098dee19e6SLendacky, Thomas 		if (rx_frames && !((index + 1) % rx_frames))
15108dee19e6SLendacky, Thomas 			inte = 1;
15118dee19e6SLendacky, Thomas 		else
15128dee19e6SLendacky, Thomas 			inte = 0;
15138dee19e6SLendacky, Thomas 	}
1514c5aa9e3bSLendacky, Thomas 
1515c5aa9e3bSLendacky, Thomas 	/* Reset the Rx descriptor
1516174fd259SLendacky, Thomas 	 *   Set buffer 1 (lo) address to header dma address (lo)
1517174fd259SLendacky, Thomas 	 *   Set buffer 1 (hi) address to header dma address (hi)
1518174fd259SLendacky, Thomas 	 *   Set buffer 2 (lo) address to buffer dma address (lo)
1519174fd259SLendacky, Thomas 	 *   Set buffer 2 (hi) address to buffer dma address (hi) and
1520174fd259SLendacky, Thomas 	 *     set control bits OWN and INTE
1521c5aa9e3bSLendacky, Thomas 	 */
1522cfbfd86bSLendacky, Thomas 	hdr_dma = rdata->rx.hdr.dma_base + rdata->rx.hdr.dma_off;
1523cfbfd86bSLendacky, Thomas 	buf_dma = rdata->rx.buf.dma_base + rdata->rx.buf.dma_off;
1524cfbfd86bSLendacky, Thomas 	rdesc->desc0 = cpu_to_le32(lower_32_bits(hdr_dma));
1525cfbfd86bSLendacky, Thomas 	rdesc->desc1 = cpu_to_le32(upper_32_bits(hdr_dma));
1526cfbfd86bSLendacky, Thomas 	rdesc->desc2 = cpu_to_le32(lower_32_bits(buf_dma));
1527cfbfd86bSLendacky, Thomas 	rdesc->desc3 = cpu_to_le32(upper_32_bits(buf_dma));
1528c5aa9e3bSLendacky, Thomas 
15298dee19e6SLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, INTE, inte);
1530c5aa9e3bSLendacky, Thomas 
1531c5aa9e3bSLendacky, Thomas 	/* Since the Rx DMA engine is likely running, make sure everything
1532c5aa9e3bSLendacky, Thomas 	 * is written to the descriptor(s) before setting the OWN bit
1533c5aa9e3bSLendacky, Thomas 	 * for the descriptor
1534c5aa9e3bSLendacky, Thomas 	 */
1535ceb8f6beSLendacky, Thomas 	dma_wmb();
1536c5aa9e3bSLendacky, Thomas 
1537c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN, 1);
1538c5aa9e3bSLendacky, Thomas 
1539c5aa9e3bSLendacky, Thomas 	/* Make sure ownership is written to the descriptor */
1540ceb8f6beSLendacky, Thomas 	dma_wmb();
1541c5aa9e3bSLendacky, Thomas }
1542c5aa9e3bSLendacky, Thomas 
xgbe_rx_desc_init(struct xgbe_channel * channel)1543c5aa9e3bSLendacky, Thomas static void xgbe_rx_desc_init(struct xgbe_channel *channel)
1544c5aa9e3bSLendacky, Thomas {
1545c5aa9e3bSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
1546c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->rx_ring;
1547c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
1548c5aa9e3bSLendacky, Thomas 	unsigned int start_index = ring->cur;
1549c5aa9e3bSLendacky, Thomas 	unsigned int i;
1550c5aa9e3bSLendacky, Thomas 
1551c5aa9e3bSLendacky, Thomas 	DBGPR("-->rx_desc_init\n");
1552c5aa9e3bSLendacky, Thomas 
1553c5aa9e3bSLendacky, Thomas 	/* Initialize all descriptors */
1554c5aa9e3bSLendacky, Thomas 	for (i = 0; i < ring->rdesc_count; i++) {
1555d0a8ba6cSLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, i);
1556c5aa9e3bSLendacky, Thomas 
155708dcc47cSLendacky, Thomas 		/* Initialize Rx descriptor */
15588dee19e6SLendacky, Thomas 		xgbe_rx_desc_reset(pdata, rdata, i);
155908dcc47cSLendacky, Thomas 	}
1560c5aa9e3bSLendacky, Thomas 
1561c5aa9e3bSLendacky, Thomas 	/* Update the total number of Rx descriptors */
1562c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDRLR, ring->rdesc_count - 1);
1563c5aa9e3bSLendacky, Thomas 
1564c5aa9e3bSLendacky, Thomas 	/* Update the starting address of descriptor ring */
1565d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, start_index);
1566c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_HI,
1567c5aa9e3bSLendacky, Thomas 			  upper_32_bits(rdata->rdesc_dma));
1568c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDLR_LO,
1569c5aa9e3bSLendacky, Thomas 			  lower_32_bits(rdata->rdesc_dma));
1570c5aa9e3bSLendacky, Thomas 
1571c5aa9e3bSLendacky, Thomas 	/* Update the Rx Descriptor Tail Pointer */
1572d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, start_index + ring->rdesc_count - 1);
1573c5aa9e3bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1574c5aa9e3bSLendacky, Thomas 			  lower_32_bits(rdata->rdesc_dma));
1575c5aa9e3bSLendacky, Thomas 
1576c5aa9e3bSLendacky, Thomas 	DBGPR("<--rx_desc_init\n");
1577c5aa9e3bSLendacky, Thomas }
1578c5aa9e3bSLendacky, Thomas 
xgbe_update_tstamp_addend(struct xgbe_prv_data * pdata,unsigned int addend)157923e4eef7SLendacky, Thomas static void xgbe_update_tstamp_addend(struct xgbe_prv_data *pdata,
158023e4eef7SLendacky, Thomas 				      unsigned int addend)
158123e4eef7SLendacky, Thomas {
15829018ff53SLendacky, Thomas 	unsigned int count = 10000;
15839018ff53SLendacky, Thomas 
158423e4eef7SLendacky, Thomas 	/* Set the addend register value and tell the device */
158523e4eef7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_TSAR, addend);
158623e4eef7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSADDREG, 1);
158723e4eef7SLendacky, Thomas 
158823e4eef7SLendacky, Thomas 	/* Wait for addend update to complete */
15899018ff53SLendacky, Thomas 	while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSADDREG))
159023e4eef7SLendacky, Thomas 		udelay(5);
15919018ff53SLendacky, Thomas 
15929018ff53SLendacky, Thomas 	if (!count)
15939018ff53SLendacky, Thomas 		netdev_err(pdata->netdev,
15949018ff53SLendacky, Thomas 			   "timed out updating timestamp addend register\n");
159523e4eef7SLendacky, Thomas }
159623e4eef7SLendacky, Thomas 
xgbe_set_tstamp_time(struct xgbe_prv_data * pdata,unsigned int sec,unsigned int nsec)159723e4eef7SLendacky, Thomas static void xgbe_set_tstamp_time(struct xgbe_prv_data *pdata, unsigned int sec,
159823e4eef7SLendacky, Thomas 				 unsigned int nsec)
159923e4eef7SLendacky, Thomas {
16009018ff53SLendacky, Thomas 	unsigned int count = 10000;
16019018ff53SLendacky, Thomas 
160223e4eef7SLendacky, Thomas 	/* Set the time values and tell the device */
160323e4eef7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_STSUR, sec);
160423e4eef7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_STNUR, nsec);
160523e4eef7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TSCR, TSINIT, 1);
160623e4eef7SLendacky, Thomas 
160723e4eef7SLendacky, Thomas 	/* Wait for time update to complete */
16089018ff53SLendacky, Thomas 	while (--count && XGMAC_IOREAD_BITS(pdata, MAC_TSCR, TSINIT))
160923e4eef7SLendacky, Thomas 		udelay(5);
16109018ff53SLendacky, Thomas 
16119018ff53SLendacky, Thomas 	if (!count)
16129018ff53SLendacky, Thomas 		netdev_err(pdata->netdev, "timed out initializing timestamp\n");
161323e4eef7SLendacky, Thomas }
161423e4eef7SLendacky, Thomas 
xgbe_get_tstamp_time(struct xgbe_prv_data * pdata)161523e4eef7SLendacky, Thomas static u64 xgbe_get_tstamp_time(struct xgbe_prv_data *pdata)
161623e4eef7SLendacky, Thomas {
161723e4eef7SLendacky, Thomas 	u64 nsec;
161823e4eef7SLendacky, Thomas 
161923e4eef7SLendacky, Thomas 	nsec = XGMAC_IOREAD(pdata, MAC_STSR);
162023e4eef7SLendacky, Thomas 	nsec *= NSEC_PER_SEC;
162123e4eef7SLendacky, Thomas 	nsec += XGMAC_IOREAD(pdata, MAC_STNR);
162223e4eef7SLendacky, Thomas 
162323e4eef7SLendacky, Thomas 	return nsec;
162423e4eef7SLendacky, Thomas }
162523e4eef7SLendacky, Thomas 
xgbe_get_tx_tstamp(struct xgbe_prv_data * pdata)162623e4eef7SLendacky, Thomas static u64 xgbe_get_tx_tstamp(struct xgbe_prv_data *pdata)
162723e4eef7SLendacky, Thomas {
1628aba9777aSLendacky, Thomas 	unsigned int tx_snr, tx_ssr;
162923e4eef7SLendacky, Thomas 	u64 nsec;
163023e4eef7SLendacky, Thomas 
1631aba9777aSLendacky, Thomas 	if (pdata->vdata->tx_tstamp_workaround) {
163223e4eef7SLendacky, Thomas 		tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1633aba9777aSLendacky, Thomas 		tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1634aba9777aSLendacky, Thomas 	} else {
1635aba9777aSLendacky, Thomas 		tx_ssr = XGMAC_IOREAD(pdata, MAC_TXSSR);
1636aba9777aSLendacky, Thomas 		tx_snr = XGMAC_IOREAD(pdata, MAC_TXSNR);
1637aba9777aSLendacky, Thomas 	}
1638aba9777aSLendacky, Thomas 
163923e4eef7SLendacky, Thomas 	if (XGMAC_GET_BITS(tx_snr, MAC_TXSNR, TXTSSTSMIS))
164023e4eef7SLendacky, Thomas 		return 0;
164123e4eef7SLendacky, Thomas 
1642aba9777aSLendacky, Thomas 	nsec = tx_ssr;
164323e4eef7SLendacky, Thomas 	nsec *= NSEC_PER_SEC;
164423e4eef7SLendacky, Thomas 	nsec += tx_snr;
164523e4eef7SLendacky, Thomas 
164623e4eef7SLendacky, Thomas 	return nsec;
164723e4eef7SLendacky, Thomas }
164823e4eef7SLendacky, Thomas 
xgbe_get_rx_tstamp(struct xgbe_packet_data * packet,struct xgbe_ring_desc * rdesc)164923e4eef7SLendacky, Thomas static void xgbe_get_rx_tstamp(struct xgbe_packet_data *packet,
165023e4eef7SLendacky, Thomas 			       struct xgbe_ring_desc *rdesc)
165123e4eef7SLendacky, Thomas {
165223e4eef7SLendacky, Thomas 	u64 nsec;
165323e4eef7SLendacky, Thomas 
165423e4eef7SLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSA) &&
165523e4eef7SLendacky, Thomas 	    !XGMAC_GET_BITS_LE(rdesc->desc3, RX_CONTEXT_DESC3, TSD)) {
165623e4eef7SLendacky, Thomas 		nsec = le32_to_cpu(rdesc->desc1);
165723e4eef7SLendacky, Thomas 		nsec <<= 32;
165823e4eef7SLendacky, Thomas 		nsec |= le32_to_cpu(rdesc->desc0);
165923e4eef7SLendacky, Thomas 		if (nsec != 0xffffffffffffffffULL) {
166023e4eef7SLendacky, Thomas 			packet->rx_tstamp = nsec;
166123e4eef7SLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
166223e4eef7SLendacky, Thomas 				       RX_TSTAMP, 1);
166323e4eef7SLendacky, Thomas 		}
166423e4eef7SLendacky, Thomas 	}
166523e4eef7SLendacky, Thomas }
166623e4eef7SLendacky, Thomas 
xgbe_config_tstamp(struct xgbe_prv_data * pdata,unsigned int mac_tscr)166723e4eef7SLendacky, Thomas static int xgbe_config_tstamp(struct xgbe_prv_data *pdata,
166823e4eef7SLendacky, Thomas 			      unsigned int mac_tscr)
166923e4eef7SLendacky, Thomas {
167023e4eef7SLendacky, Thomas 	/* Set one nano-second accuracy */
167123e4eef7SLendacky, Thomas 	XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCTRLSSR, 1);
167223e4eef7SLendacky, Thomas 
167323e4eef7SLendacky, Thomas 	/* Set fine timestamp update */
167423e4eef7SLendacky, Thomas 	XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSCFUPDT, 1);
167523e4eef7SLendacky, Thomas 
167623e4eef7SLendacky, Thomas 	/* Overwrite earlier timestamps */
167723e4eef7SLendacky, Thomas 	XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TXTSSTSM, 1);
167823e4eef7SLendacky, Thomas 
167923e4eef7SLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_TSCR, mac_tscr);
168023e4eef7SLendacky, Thomas 
168123e4eef7SLendacky, Thomas 	/* Exit if timestamping is not enabled */
168223e4eef7SLendacky, Thomas 	if (!XGMAC_GET_BITS(mac_tscr, MAC_TSCR, TSENA))
168323e4eef7SLendacky, Thomas 		return 0;
168423e4eef7SLendacky, Thomas 
168523e4eef7SLendacky, Thomas 	/* Initialize time registers */
168623e4eef7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SSINC, XGBE_TSTAMP_SSINC);
168723e4eef7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_SSIR, SNSINC, XGBE_TSTAMP_SNSINC);
168823e4eef7SLendacky, Thomas 	xgbe_update_tstamp_addend(pdata, pdata->tstamp_addend);
168923e4eef7SLendacky, Thomas 	xgbe_set_tstamp_time(pdata, 0, 0);
169023e4eef7SLendacky, Thomas 
169123e4eef7SLendacky, Thomas 	/* Initialize the timecounter */
169223e4eef7SLendacky, Thomas 	timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
169323e4eef7SLendacky, Thomas 			 ktime_to_ns(ktime_get_real()));
169423e4eef7SLendacky, Thomas 
169523e4eef7SLendacky, Thomas 	return 0;
169623e4eef7SLendacky, Thomas }
169723e4eef7SLendacky, Thomas 
xgbe_tx_start_xmit(struct xgbe_channel * channel,struct xgbe_ring * ring)169816958a2bSLendacky, Thomas static void xgbe_tx_start_xmit(struct xgbe_channel *channel,
169916958a2bSLendacky, Thomas 			       struct xgbe_ring *ring)
170016958a2bSLendacky, Thomas {
170116958a2bSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
170216958a2bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
170316958a2bSLendacky, Thomas 
1704ceb8f6beSLendacky, Thomas 	/* Make sure everything is written before the register write */
1705ceb8f6beSLendacky, Thomas 	wmb();
1706ceb8f6beSLendacky, Thomas 
170716958a2bSLendacky, Thomas 	/* Issue a poll command to Tx DMA by writing address
170816958a2bSLendacky, Thomas 	 * of next immediate free descriptor */
170916958a2bSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
171016958a2bSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_TDTR_LO,
171116958a2bSLendacky, Thomas 			  lower_32_bits(rdata->rdesc_dma));
171216958a2bSLendacky, Thomas 
1713c635eaacSLendacky, Thomas 	/* Start the Tx timer */
171416958a2bSLendacky, Thomas 	if (pdata->tx_usecs && !channel->tx_timer_active) {
171516958a2bSLendacky, Thomas 		channel->tx_timer_active = 1;
1716c635eaacSLendacky, Thomas 		mod_timer(&channel->tx_timer,
1717c635eaacSLendacky, Thomas 			  jiffies + usecs_to_jiffies(pdata->tx_usecs));
171816958a2bSLendacky, Thomas 	}
171916958a2bSLendacky, Thomas 
172016958a2bSLendacky, Thomas 	ring->tx.xmit_more = 0;
172116958a2bSLendacky, Thomas }
172216958a2bSLendacky, Thomas 
xgbe_dev_xmit(struct xgbe_channel * channel)1723a9d41981SLendacky, Thomas static void xgbe_dev_xmit(struct xgbe_channel *channel)
1724c5aa9e3bSLendacky, Thomas {
1725c5aa9e3bSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
1726c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->tx_ring;
1727c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
1728c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
1729c5aa9e3bSLendacky, Thomas 	struct xgbe_packet_data *packet = &ring->packet_data;
173080a788c9SLendacky, Thomas 	unsigned int tx_packets, tx_bytes;
17311a510ccfSLendacky, Thomas 	unsigned int csum, tso, vlan, vxlan;
1732c5aa9e3bSLendacky, Thomas 	unsigned int tso_context, vlan_context;
1733eb79e640SLendacky, Thomas 	unsigned int tx_set_ic;
1734c5aa9e3bSLendacky, Thomas 	int start_index = ring->cur;
1735a83ef427SLendacky, Thomas 	int cur_index = ring->cur;
1736c5aa9e3bSLendacky, Thomas 	int i;
1737c5aa9e3bSLendacky, Thomas 
1738a9d41981SLendacky, Thomas 	DBGPR("-->xgbe_dev_xmit\n");
1739c5aa9e3bSLendacky, Thomas 
174080a788c9SLendacky, Thomas 	tx_packets = packet->tx_packets;
174180a788c9SLendacky, Thomas 	tx_bytes = packet->tx_bytes;
174280a788c9SLendacky, Thomas 
1743c5aa9e3bSLendacky, Thomas 	csum = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1744c5aa9e3bSLendacky, Thomas 			      CSUM_ENABLE);
1745c5aa9e3bSLendacky, Thomas 	tso = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1746c5aa9e3bSLendacky, Thomas 			     TSO_ENABLE);
1747c5aa9e3bSLendacky, Thomas 	vlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1748c5aa9e3bSLendacky, Thomas 			      VLAN_CTAG);
17491a510ccfSLendacky, Thomas 	vxlan = XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
17501a510ccfSLendacky, Thomas 			       VXLAN);
1751c5aa9e3bSLendacky, Thomas 
1752c5aa9e3bSLendacky, Thomas 	if (tso && (packet->mss != ring->tx.cur_mss))
1753c5aa9e3bSLendacky, Thomas 		tso_context = 1;
1754c5aa9e3bSLendacky, Thomas 	else
1755c5aa9e3bSLendacky, Thomas 		tso_context = 0;
1756c5aa9e3bSLendacky, Thomas 
1757c5aa9e3bSLendacky, Thomas 	if (vlan && (packet->vlan_ctag != ring->tx.cur_vlan_ctag))
1758c5aa9e3bSLendacky, Thomas 		vlan_context = 1;
1759c5aa9e3bSLendacky, Thomas 	else
1760c5aa9e3bSLendacky, Thomas 		vlan_context = 0;
1761c5aa9e3bSLendacky, Thomas 
1762eb79e640SLendacky, Thomas 	/* Determine if an interrupt should be generated for this Tx:
1763eb79e640SLendacky, Thomas 	 *   Interrupt:
1764eb79e640SLendacky, Thomas 	 *     - Tx frame count exceeds the frame count setting
1765eb79e640SLendacky, Thomas 	 *     - Addition of Tx frame count to the frame count since the
1766eb79e640SLendacky, Thomas 	 *       last interrupt was set exceeds the frame count setting
1767eb79e640SLendacky, Thomas 	 *   No interrupt:
1768eb79e640SLendacky, Thomas 	 *     - No frame count setting specified (ethtool -C ethX tx-frames 0)
1769eb79e640SLendacky, Thomas 	 *     - Addition of Tx frame count to the frame count since the
1770eb79e640SLendacky, Thomas 	 *       last interrupt was set does not exceed the frame count setting
1771eb79e640SLendacky, Thomas 	 */
177280a788c9SLendacky, Thomas 	ring->coalesce_count += tx_packets;
1773eb79e640SLendacky, Thomas 	if (!pdata->tx_frames)
1774eb79e640SLendacky, Thomas 		tx_set_ic = 0;
177580a788c9SLendacky, Thomas 	else if (tx_packets > pdata->tx_frames)
1776eb79e640SLendacky, Thomas 		tx_set_ic = 1;
177780a788c9SLendacky, Thomas 	else if ((ring->coalesce_count % pdata->tx_frames) < tx_packets)
1778eb79e640SLendacky, Thomas 		tx_set_ic = 1;
1779eb79e640SLendacky, Thomas 	else
1780eb79e640SLendacky, Thomas 		tx_set_ic = 0;
1781c5aa9e3bSLendacky, Thomas 
1782a83ef427SLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, cur_index);
1783c5aa9e3bSLendacky, Thomas 	rdesc = rdata->rdesc;
1784c5aa9e3bSLendacky, Thomas 
1785c5aa9e3bSLendacky, Thomas 	/* Create a context descriptor if this is a TSO packet */
1786c5aa9e3bSLendacky, Thomas 	if (tso_context || vlan_context) {
1787c5aa9e3bSLendacky, Thomas 		if (tso_context) {
178834bf65dfSLendacky, Thomas 			netif_dbg(pdata, tx_queued, pdata->netdev,
178934bf65dfSLendacky, Thomas 				  "TSO context descriptor, mss=%u\n",
1790c5aa9e3bSLendacky, Thomas 				  packet->mss);
1791c5aa9e3bSLendacky, Thomas 
1792c5aa9e3bSLendacky, Thomas 			/* Set the MSS size */
1793c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc2, TX_CONTEXT_DESC2,
1794c5aa9e3bSLendacky, Thomas 					  MSS, packet->mss);
1795c5aa9e3bSLendacky, Thomas 
1796c5aa9e3bSLendacky, Thomas 			/* Mark it as a CONTEXT descriptor */
1797c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1798c5aa9e3bSLendacky, Thomas 					  CTXT, 1);
1799c5aa9e3bSLendacky, Thomas 
1800c5aa9e3bSLendacky, Thomas 			/* Indicate this descriptor contains the MSS */
1801c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1802c5aa9e3bSLendacky, Thomas 					  TCMSSV, 1);
1803c5aa9e3bSLendacky, Thomas 
1804c5aa9e3bSLendacky, Thomas 			ring->tx.cur_mss = packet->mss;
1805c5aa9e3bSLendacky, Thomas 		}
1806c5aa9e3bSLendacky, Thomas 
1807c5aa9e3bSLendacky, Thomas 		if (vlan_context) {
180834bf65dfSLendacky, Thomas 			netif_dbg(pdata, tx_queued, pdata->netdev,
180934bf65dfSLendacky, Thomas 				  "VLAN context descriptor, ctag=%u\n",
1810c5aa9e3bSLendacky, Thomas 				  packet->vlan_ctag);
1811c5aa9e3bSLendacky, Thomas 
1812c5aa9e3bSLendacky, Thomas 			/* Mark it as a CONTEXT descriptor */
1813c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1814c5aa9e3bSLendacky, Thomas 					  CTXT, 1);
1815c5aa9e3bSLendacky, Thomas 
1816c5aa9e3bSLendacky, Thomas 			/* Set the VLAN tag */
1817c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1818c5aa9e3bSLendacky, Thomas 					  VT, packet->vlan_ctag);
1819c5aa9e3bSLendacky, Thomas 
1820c5aa9e3bSLendacky, Thomas 			/* Indicate this descriptor contains the VLAN tag */
1821c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_CONTEXT_DESC3,
1822c5aa9e3bSLendacky, Thomas 					  VLTV, 1);
1823c5aa9e3bSLendacky, Thomas 
1824c5aa9e3bSLendacky, Thomas 			ring->tx.cur_vlan_ctag = packet->vlan_ctag;
1825c5aa9e3bSLendacky, Thomas 		}
1826c5aa9e3bSLendacky, Thomas 
1827a83ef427SLendacky, Thomas 		cur_index++;
1828a83ef427SLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
1829c5aa9e3bSLendacky, Thomas 		rdesc = rdata->rdesc;
1830c5aa9e3bSLendacky, Thomas 	}
1831c5aa9e3bSLendacky, Thomas 
1832c5aa9e3bSLendacky, Thomas 	/* Update buffer address (for TSO this is the header) */
1833c5aa9e3bSLendacky, Thomas 	rdesc->desc0 =  cpu_to_le32(lower_32_bits(rdata->skb_dma));
1834c5aa9e3bSLendacky, Thomas 	rdesc->desc1 =  cpu_to_le32(upper_32_bits(rdata->skb_dma));
1835c5aa9e3bSLendacky, Thomas 
1836c5aa9e3bSLendacky, Thomas 	/* Update the buffer length */
1837c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1838c5aa9e3bSLendacky, Thomas 			  rdata->skb_dma_len);
1839c5aa9e3bSLendacky, Thomas 
1840c5aa9e3bSLendacky, Thomas 	/* VLAN tag insertion check */
1841c5aa9e3bSLendacky, Thomas 	if (vlan)
1842c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, VTIR,
1843c5aa9e3bSLendacky, Thomas 				  TX_NORMAL_DESC2_VLAN_INSERT);
1844c5aa9e3bSLendacky, Thomas 
184523e4eef7SLendacky, Thomas 	/* Timestamp enablement check */
184623e4eef7SLendacky, Thomas 	if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
184723e4eef7SLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, TTSE, 1);
184823e4eef7SLendacky, Thomas 
1849c5aa9e3bSLendacky, Thomas 	/* Mark it as First Descriptor */
1850c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FD, 1);
1851c5aa9e3bSLendacky, Thomas 
1852c5aa9e3bSLendacky, Thomas 	/* Mark it as a NORMAL descriptor */
1853c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1854c5aa9e3bSLendacky, Thomas 
1855c5aa9e3bSLendacky, Thomas 	/* Set OWN bit if not the first descriptor */
1856a83ef427SLendacky, Thomas 	if (cur_index != start_index)
1857c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1858c5aa9e3bSLendacky, Thomas 
1859c5aa9e3bSLendacky, Thomas 	if (tso) {
1860c5aa9e3bSLendacky, Thomas 		/* Enable TSO */
1861c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TSE, 1);
1862c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPPL,
1863c5aa9e3bSLendacky, Thomas 				  packet->tcp_payload_len);
1864c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, TCPHDRLEN,
1865c5aa9e3bSLendacky, Thomas 				  packet->tcp_header_len / 4);
18665452b2dfSLendacky, Thomas 
186780a788c9SLendacky, Thomas 		pdata->ext_stats.tx_tso_packets += tx_packets;
1868c5aa9e3bSLendacky, Thomas 	} else {
1869c5aa9e3bSLendacky, Thomas 		/* Enable CRC and Pad Insertion */
1870c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CPC, 0);
1871c5aa9e3bSLendacky, Thomas 
1872c5aa9e3bSLendacky, Thomas 		/* Enable HW CSUM */
1873c5aa9e3bSLendacky, Thomas 		if (csum)
1874c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1875c5aa9e3bSLendacky, Thomas 					  CIC, 0x3);
1876c5aa9e3bSLendacky, Thomas 
1877c5aa9e3bSLendacky, Thomas 		/* Set the total length to be transmitted */
1878c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, FL,
1879c5aa9e3bSLendacky, Thomas 				  packet->length);
1880c5aa9e3bSLendacky, Thomas 	}
1881c5aa9e3bSLendacky, Thomas 
18823010608dSLendacky, Thomas 	if (vxlan) {
18831a510ccfSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, VNP,
18841a510ccfSLendacky, Thomas 				  TX_NORMAL_DESC3_VXLAN_PACKET);
18851a510ccfSLendacky, Thomas 
18863010608dSLendacky, Thomas 		pdata->ext_stats.tx_vxlan_packets += packet->tx_packets;
18873010608dSLendacky, Thomas 	}
18883010608dSLendacky, Thomas 
1889a83ef427SLendacky, Thomas 	for (i = cur_index - start_index + 1; i < packet->rdesc_count; i++) {
1890a83ef427SLendacky, Thomas 		cur_index++;
1891a83ef427SLendacky, Thomas 		rdata = XGBE_GET_DESC_DATA(ring, cur_index);
1892c5aa9e3bSLendacky, Thomas 		rdesc = rdata->rdesc;
1893c5aa9e3bSLendacky, Thomas 
1894c5aa9e3bSLendacky, Thomas 		/* Update buffer address */
1895c5aa9e3bSLendacky, Thomas 		rdesc->desc0 = cpu_to_le32(lower_32_bits(rdata->skb_dma));
1896c5aa9e3bSLendacky, Thomas 		rdesc->desc1 = cpu_to_le32(upper_32_bits(rdata->skb_dma));
1897c5aa9e3bSLendacky, Thomas 
1898c5aa9e3bSLendacky, Thomas 		/* Update the buffer length */
1899c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, HL_B1L,
1900c5aa9e3bSLendacky, Thomas 				  rdata->skb_dma_len);
1901c5aa9e3bSLendacky, Thomas 
1902c5aa9e3bSLendacky, Thomas 		/* Set OWN bit */
1903c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1904c5aa9e3bSLendacky, Thomas 
1905c5aa9e3bSLendacky, Thomas 		/* Mark it as NORMAL descriptor */
1906c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT, 0);
1907c5aa9e3bSLendacky, Thomas 
1908c5aa9e3bSLendacky, Thomas 		/* Enable HW CSUM */
1909c5aa9e3bSLendacky, Thomas 		if (csum)
1910c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3,
1911c5aa9e3bSLendacky, Thomas 					  CIC, 0x3);
1912c5aa9e3bSLendacky, Thomas 	}
1913c5aa9e3bSLendacky, Thomas 
1914c5aa9e3bSLendacky, Thomas 	/* Set LAST bit for the last descriptor */
1915c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD, 1);
1916c5aa9e3bSLendacky, Thomas 
1917eb79e640SLendacky, Thomas 	/* Set IC bit based on Tx coalescing settings */
1918eb79e640SLendacky, Thomas 	if (tx_set_ic)
1919eb79e640SLendacky, Thomas 		XGMAC_SET_BITS_LE(rdesc->desc2, TX_NORMAL_DESC2, IC, 1);
1920eb79e640SLendacky, Thomas 
19215fb4b86aSLendacky, Thomas 	/* Save the Tx info to report back during cleanup */
192280a788c9SLendacky, Thomas 	rdata->tx.packets = tx_packets;
192380a788c9SLendacky, Thomas 	rdata->tx.bytes = tx_bytes;
192480a788c9SLendacky, Thomas 
192580a788c9SLendacky, Thomas 	pdata->ext_stats.txq_packets[channel->queue_index] += tx_packets;
192680a788c9SLendacky, Thomas 	pdata->ext_stats.txq_bytes[channel->queue_index] += tx_bytes;
19275fb4b86aSLendacky, Thomas 
1928c5aa9e3bSLendacky, Thomas 	/* In case the Tx DMA engine is running, make sure everything
1929c5aa9e3bSLendacky, Thomas 	 * is written to the descriptor(s) before setting the OWN bit
1930c5aa9e3bSLendacky, Thomas 	 * for the first descriptor
1931c5aa9e3bSLendacky, Thomas 	 */
1932ceb8f6beSLendacky, Thomas 	dma_wmb();
1933c5aa9e3bSLendacky, Thomas 
1934c5aa9e3bSLendacky, Thomas 	/* Set OWN bit for the first descriptor */
1935d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, start_index);
1936c5aa9e3bSLendacky, Thomas 	rdesc = rdata->rdesc;
1937c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, OWN, 1);
1938c5aa9e3bSLendacky, Thomas 
193934bf65dfSLendacky, Thomas 	if (netif_msg_tx_queued(pdata))
194034bf65dfSLendacky, Thomas 		xgbe_dump_tx_desc(pdata, ring, start_index,
194134bf65dfSLendacky, Thomas 				  packet->rdesc_count, 1);
1942c5aa9e3bSLendacky, Thomas 
1943c5aa9e3bSLendacky, Thomas 	/* Make sure ownership is written to the descriptor */
194420986ed8SLendacky, Thomas 	smp_wmb();
1945c5aa9e3bSLendacky, Thomas 
1946a83ef427SLendacky, Thomas 	ring->cur = cur_index + 1;
19476b16f9eeSFlorian Westphal 	if (!netdev_xmit_more() ||
194816958a2bSLendacky, Thomas 	    netif_xmit_stopped(netdev_get_tx_queue(pdata->netdev,
194916958a2bSLendacky, Thomas 						   channel->queue_index)))
195016958a2bSLendacky, Thomas 		xgbe_tx_start_xmit(channel, ring);
195116958a2bSLendacky, Thomas 	else
195216958a2bSLendacky, Thomas 		ring->tx.xmit_more = 1;
1953c5aa9e3bSLendacky, Thomas 
1954c5aa9e3bSLendacky, Thomas 	DBGPR("  %s: descriptors %u to %u written\n",
1955c5aa9e3bSLendacky, Thomas 	      channel->name, start_index & (ring->rdesc_count - 1),
1956c5aa9e3bSLendacky, Thomas 	      (ring->cur - 1) & (ring->rdesc_count - 1));
1957c5aa9e3bSLendacky, Thomas 
1958a9d41981SLendacky, Thomas 	DBGPR("<--xgbe_dev_xmit\n");
1959c5aa9e3bSLendacky, Thomas }
1960c5aa9e3bSLendacky, Thomas 
xgbe_dev_read(struct xgbe_channel * channel)1961c5aa9e3bSLendacky, Thomas static int xgbe_dev_read(struct xgbe_channel *channel)
1962c5aa9e3bSLendacky, Thomas {
19635452b2dfSLendacky, Thomas 	struct xgbe_prv_data *pdata = channel->pdata;
1964c5aa9e3bSLendacky, Thomas 	struct xgbe_ring *ring = channel->rx_ring;
1965c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_data *rdata;
1966c5aa9e3bSLendacky, Thomas 	struct xgbe_ring_desc *rdesc;
1967c5aa9e3bSLendacky, Thomas 	struct xgbe_packet_data *packet = &ring->packet_data;
19685452b2dfSLendacky, Thomas 	struct net_device *netdev = pdata->netdev;
19695b9dfe29SLendacky, Thomas 	unsigned int err, etlt, l34t;
1970c5aa9e3bSLendacky, Thomas 
1971c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_dev_read: cur = %d\n", ring->cur);
1972c5aa9e3bSLendacky, Thomas 
1973d0a8ba6cSLendacky, Thomas 	rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1974c5aa9e3bSLendacky, Thomas 	rdesc = rdata->rdesc;
1975c5aa9e3bSLendacky, Thomas 
1976c5aa9e3bSLendacky, Thomas 	/* Check for data availability */
1977c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, OWN))
1978c5aa9e3bSLendacky, Thomas 		return 1;
1979c5aa9e3bSLendacky, Thomas 
19805449e271SLendacky, Thomas 	/* Make sure descriptor fields are read after reading the OWN bit */
1981ceb8f6beSLendacky, Thomas 	dma_rmb();
19825449e271SLendacky, Thomas 
198334bf65dfSLendacky, Thomas 	if (netif_msg_rx_status(pdata))
198434bf65dfSLendacky, Thomas 		xgbe_dump_rx_desc(pdata, ring, ring->cur);
1985c5aa9e3bSLendacky, Thomas 
198623e4eef7SLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CTXT)) {
198723e4eef7SLendacky, Thomas 		/* Timestamp Context Descriptor */
198823e4eef7SLendacky, Thomas 		xgbe_get_rx_tstamp(packet, rdesc);
198923e4eef7SLendacky, Thomas 
199023e4eef7SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
199123e4eef7SLendacky, Thomas 			       CONTEXT, 1);
199223e4eef7SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
199323e4eef7SLendacky, Thomas 			       CONTEXT_NEXT, 0);
199423e4eef7SLendacky, Thomas 		return 0;
199523e4eef7SLendacky, Thomas 	}
199623e4eef7SLendacky, Thomas 
199723e4eef7SLendacky, Thomas 	/* Normal Descriptor, be sure Context Descriptor bit is off */
199823e4eef7SLendacky, Thomas 	XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, CONTEXT, 0);
199923e4eef7SLendacky, Thomas 
200023e4eef7SLendacky, Thomas 	/* Indicate if a Context Descriptor is next */
200123e4eef7SLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, CDA))
200223e4eef7SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
200323e4eef7SLendacky, Thomas 			       CONTEXT_NEXT, 1);
200423e4eef7SLendacky, Thomas 
2005174fd259SLendacky, Thomas 	/* Get the header length */
20065452b2dfSLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, FD)) {
2007622c36f1SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2008622c36f1SLendacky, Thomas 			       FIRST, 1);
2009c9f140ebSLendacky, Thomas 		rdata->rx.hdr_len = XGMAC_GET_BITS_LE(rdesc->desc2,
2010174fd259SLendacky, Thomas 						      RX_NORMAL_DESC2, HL);
20115452b2dfSLendacky, Thomas 		if (rdata->rx.hdr_len)
20125452b2dfSLendacky, Thomas 			pdata->ext_stats.rx_split_header_packets++;
2013622c36f1SLendacky, Thomas 	} else {
2014622c36f1SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2015622c36f1SLendacky, Thomas 			       FIRST, 0);
20165452b2dfSLendacky, Thomas 	}
2017174fd259SLendacky, Thomas 
20185b9dfe29SLendacky, Thomas 	/* Get the RSS hash */
20195b9dfe29SLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, RSV)) {
20205b9dfe29SLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
20215b9dfe29SLendacky, Thomas 			       RSS_HASH, 1);
20225b9dfe29SLendacky, Thomas 
20235b9dfe29SLendacky, Thomas 		packet->rss_hash = le32_to_cpu(rdesc->desc1);
20245b9dfe29SLendacky, Thomas 
20255b9dfe29SLendacky, Thomas 		l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
20265b9dfe29SLendacky, Thomas 		switch (l34t) {
20275b9dfe29SLendacky, Thomas 		case RX_DESC3_L34T_IPV4_TCP:
20285b9dfe29SLendacky, Thomas 		case RX_DESC3_L34T_IPV4_UDP:
20295b9dfe29SLendacky, Thomas 		case RX_DESC3_L34T_IPV6_TCP:
20305b9dfe29SLendacky, Thomas 		case RX_DESC3_L34T_IPV6_UDP:
20315b9dfe29SLendacky, Thomas 			packet->rss_hash_type = PKT_HASH_TYPE_L4;
2032b6267d3eSDan Carpenter 			break;
20335b9dfe29SLendacky, Thomas 		default:
20345b9dfe29SLendacky, Thomas 			packet->rss_hash_type = PKT_HASH_TYPE_L3;
20355b9dfe29SLendacky, Thomas 		}
20365b9dfe29SLendacky, Thomas 	}
20375b9dfe29SLendacky, Thomas 
2038c5aa9e3bSLendacky, Thomas 	/* Not all the data has been transferred for this packet */
2039622c36f1SLendacky, Thomas 	if (!XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, LD))
2040c5aa9e3bSLendacky, Thomas 		return 0;
2041c5aa9e3bSLendacky, Thomas 
2042c5aa9e3bSLendacky, Thomas 	/* This is the last of the data for this packet */
2043c5aa9e3bSLendacky, Thomas 	XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2044622c36f1SLendacky, Thomas 		       LAST, 1);
2045622c36f1SLendacky, Thomas 
2046622c36f1SLendacky, Thomas 	/* Get the packet length */
2047622c36f1SLendacky, Thomas 	rdata->rx.len = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, PL);
2048c5aa9e3bSLendacky, Thomas 
2049c5aa9e3bSLendacky, Thomas 	/* Set checksum done indicator as appropriate */
20501a510ccfSLendacky, Thomas 	if (netdev->features & NETIF_F_RXCSUM) {
2051c5aa9e3bSLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2052c5aa9e3bSLendacky, Thomas 			       CSUM_DONE, 1);
20531a510ccfSLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
20541a510ccfSLendacky, Thomas 			       TNPCSUM_DONE, 1);
20551a510ccfSLendacky, Thomas 	}
20561a510ccfSLendacky, Thomas 
20571a510ccfSLendacky, Thomas 	/* Set the tunneled packet indicator */
20581a510ccfSLendacky, Thomas 	if (XGMAC_GET_BITS_LE(rdesc->desc2, RX_NORMAL_DESC2, TNP)) {
20591a510ccfSLendacky, Thomas 		XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
20601a510ccfSLendacky, Thomas 			       TNP, 1);
20613010608dSLendacky, Thomas 		pdata->ext_stats.rx_vxlan_packets++;
20621a510ccfSLendacky, Thomas 
20631a510ccfSLendacky, Thomas 		l34t = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, L34T);
20641a510ccfSLendacky, Thomas 		switch (l34t) {
20651a510ccfSLendacky, Thomas 		case RX_DESC3_L34T_IPV4_UNKNOWN:
20661a510ccfSLendacky, Thomas 		case RX_DESC3_L34T_IPV6_UNKNOWN:
20671a510ccfSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
20681a510ccfSLendacky, Thomas 				       TNPCSUM_DONE, 0);
20691a510ccfSLendacky, Thomas 			break;
20701a510ccfSLendacky, Thomas 		}
20711a510ccfSLendacky, Thomas 	}
2072c5aa9e3bSLendacky, Thomas 
2073c5aa9e3bSLendacky, Thomas 	/* Check for errors (only valid in last descriptor) */
2074c5aa9e3bSLendacky, Thomas 	err = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ES);
2075c5aa9e3bSLendacky, Thomas 	etlt = XGMAC_GET_BITS_LE(rdesc->desc3, RX_NORMAL_DESC3, ETLT);
207634bf65dfSLendacky, Thomas 	netif_dbg(pdata, rx_status, netdev, "err=%u, etlt=%#x\n", err, etlt);
2077c5aa9e3bSLendacky, Thomas 
20787bba35bdSLendacky, Thomas 	if (!err || !etlt) {
20797bba35bdSLendacky, Thomas 		/* No error if err is 0 or etlt is 0 */
2080c52e9c63SLendacky, Thomas 		if ((etlt == 0x09) &&
2081c52e9c63SLendacky, Thomas 		    (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
2082c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2083c5aa9e3bSLendacky, Thomas 				       VLAN_CTAG, 1);
2084c5aa9e3bSLendacky, Thomas 			packet->vlan_ctag = XGMAC_GET_BITS_LE(rdesc->desc0,
2085c5aa9e3bSLendacky, Thomas 							      RX_NORMAL_DESC0,
2086c5aa9e3bSLendacky, Thomas 							      OVT);
208734bf65dfSLendacky, Thomas 			netif_dbg(pdata, rx_status, netdev, "vlan-ctag=%#06x\n",
208834bf65dfSLendacky, Thomas 				  packet->vlan_ctag);
2089c5aa9e3bSLendacky, Thomas 		}
2090c5aa9e3bSLendacky, Thomas 	} else {
20911a510ccfSLendacky, Thomas 		unsigned int tnp = XGMAC_GET_BITS(packet->attributes,
20921a510ccfSLendacky, Thomas 						  RX_PACKET_ATTRIBUTES, TNP);
20931a510ccfSLendacky, Thomas 
20941a510ccfSLendacky, Thomas 		if ((etlt == 0x05) || (etlt == 0x06)) {
2095c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2096c5aa9e3bSLendacky, Thomas 				       CSUM_DONE, 0);
20971a510ccfSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
20981a510ccfSLendacky, Thomas 				       TNPCSUM_DONE, 0);
20993010608dSLendacky, Thomas 			pdata->ext_stats.rx_csum_errors++;
21001a510ccfSLendacky, Thomas 		} else if (tnp && ((etlt == 0x09) || (etlt == 0x0a))) {
21011a510ccfSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
21021a510ccfSLendacky, Thomas 				       CSUM_DONE, 0);
21031a510ccfSLendacky, Thomas 			XGMAC_SET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
21041a510ccfSLendacky, Thomas 				       TNPCSUM_DONE, 0);
21053010608dSLendacky, Thomas 			pdata->ext_stats.rx_vxlan_csum_errors++;
21061a510ccfSLendacky, Thomas 		} else {
2107c5aa9e3bSLendacky, Thomas 			XGMAC_SET_BITS(packet->errors, RX_PACKET_ERRORS,
2108c5aa9e3bSLendacky, Thomas 				       FRAME, 1);
2109c5aa9e3bSLendacky, Thomas 		}
21101a510ccfSLendacky, Thomas 	}
2111c5aa9e3bSLendacky, Thomas 
211280a788c9SLendacky, Thomas 	pdata->ext_stats.rxq_packets[channel->queue_index]++;
211380a788c9SLendacky, Thomas 	pdata->ext_stats.rxq_bytes[channel->queue_index] += rdata->rx.len;
211480a788c9SLendacky, Thomas 
2115c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_dev_read: %s - descriptor=%u (cur=%d)\n", channel->name,
2116c5aa9e3bSLendacky, Thomas 	      ring->cur & (ring->rdesc_count - 1), ring->cur);
2117c5aa9e3bSLendacky, Thomas 
2118c5aa9e3bSLendacky, Thomas 	return 0;
2119c5aa9e3bSLendacky, Thomas }
2120c5aa9e3bSLendacky, Thomas 
xgbe_is_context_desc(struct xgbe_ring_desc * rdesc)2121c5aa9e3bSLendacky, Thomas static int xgbe_is_context_desc(struct xgbe_ring_desc *rdesc)
2122c5aa9e3bSLendacky, Thomas {
2123c5aa9e3bSLendacky, Thomas 	/* Rx and Tx share CTXT bit, so check TDES3.CTXT bit */
2124c5aa9e3bSLendacky, Thomas 	return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, CTXT);
2125c5aa9e3bSLendacky, Thomas }
2126c5aa9e3bSLendacky, Thomas 
xgbe_is_last_desc(struct xgbe_ring_desc * rdesc)2127c5aa9e3bSLendacky, Thomas static int xgbe_is_last_desc(struct xgbe_ring_desc *rdesc)
2128c5aa9e3bSLendacky, Thomas {
2129c5aa9e3bSLendacky, Thomas 	/* Rx and Tx share LD bit, so check TDES3.LD bit */
2130c5aa9e3bSLendacky, Thomas 	return XGMAC_GET_BITS_LE(rdesc->desc3, TX_NORMAL_DESC3, LD);
2131c5aa9e3bSLendacky, Thomas }
2132c5aa9e3bSLendacky, Thomas 
xgbe_enable_int(struct xgbe_channel * channel,enum xgbe_int int_id)2133c5aa9e3bSLendacky, Thomas static int xgbe_enable_int(struct xgbe_channel *channel,
2134c5aa9e3bSLendacky, Thomas 			   enum xgbe_int int_id)
2135c5aa9e3bSLendacky, Thomas {
2136c5aa9e3bSLendacky, Thomas 	switch (int_id) {
2137c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TI:
2138caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 1);
2139c5aa9e3bSLendacky, Thomas 		break;
2140c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TPS:
2141caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TXSE, 1);
2142c5aa9e3bSLendacky, Thomas 		break;
2143c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TBU:
2144caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TBUE, 1);
2145c5aa9e3bSLendacky, Thomas 		break;
2146c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RI:
2147caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 1);
2148c5aa9e3bSLendacky, Thomas 		break;
2149c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RBU:
2150caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 1);
2151c5aa9e3bSLendacky, Thomas 		break;
2152c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RPS:
2153caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RSE, 1);
21549867e8fbSLendacky, Thomas 		break;
21559867e8fbSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TI_RI:
2156caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 1);
2157caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 1);
2158c5aa9e3bSLendacky, Thomas 		break;
2159c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_FBE:
2160caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 1);
2161c5aa9e3bSLendacky, Thomas 		break;
2162c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_ALL:
2163caa575afSLendacky, Thomas 		channel->curr_ier |= channel->saved_ier;
2164c5aa9e3bSLendacky, Thomas 		break;
2165c5aa9e3bSLendacky, Thomas 	default:
2166c5aa9e3bSLendacky, Thomas 		return -1;
2167c5aa9e3bSLendacky, Thomas 	}
2168c5aa9e3bSLendacky, Thomas 
2169caa575afSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
21709867e8fbSLendacky, Thomas 
2171c5aa9e3bSLendacky, Thomas 	return 0;
2172c5aa9e3bSLendacky, Thomas }
2173c5aa9e3bSLendacky, Thomas 
xgbe_disable_int(struct xgbe_channel * channel,enum xgbe_int int_id)2174c5aa9e3bSLendacky, Thomas static int xgbe_disable_int(struct xgbe_channel *channel,
2175c5aa9e3bSLendacky, Thomas 			    enum xgbe_int int_id)
2176c5aa9e3bSLendacky, Thomas {
2177c5aa9e3bSLendacky, Thomas 	switch (int_id) {
2178c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TI:
2179caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 0);
2180c5aa9e3bSLendacky, Thomas 		break;
2181c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TPS:
2182caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TXSE, 0);
2183c5aa9e3bSLendacky, Thomas 		break;
2184c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TBU:
2185caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TBUE, 0);
2186c5aa9e3bSLendacky, Thomas 		break;
2187c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RI:
2188caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 0);
2189c5aa9e3bSLendacky, Thomas 		break;
2190c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RBU:
2191caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RBUE, 0);
2192c5aa9e3bSLendacky, Thomas 		break;
2193c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_RPS:
2194caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RSE, 0);
21959867e8fbSLendacky, Thomas 		break;
21969867e8fbSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_TI_RI:
2197caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, TIE, 0);
2198caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, RIE, 0);
2199c5aa9e3bSLendacky, Thomas 		break;
2200c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_CH_SR_FBE:
2201caa575afSLendacky, Thomas 		XGMAC_SET_BITS(channel->curr_ier, DMA_CH_IER, FBEE, 0);
2202c5aa9e3bSLendacky, Thomas 		break;
2203c5aa9e3bSLendacky, Thomas 	case XGMAC_INT_DMA_ALL:
2204caa575afSLendacky, Thomas 		channel->saved_ier = channel->curr_ier;
2205caa575afSLendacky, Thomas 		channel->curr_ier = 0;
2206c5aa9e3bSLendacky, Thomas 		break;
2207c5aa9e3bSLendacky, Thomas 	default:
2208c5aa9e3bSLendacky, Thomas 		return -1;
2209c5aa9e3bSLendacky, Thomas 	}
2210c5aa9e3bSLendacky, Thomas 
2211caa575afSLendacky, Thomas 	XGMAC_DMA_IOWRITE(channel, DMA_CH_IER, channel->curr_ier);
22129867e8fbSLendacky, Thomas 
2213c5aa9e3bSLendacky, Thomas 	return 0;
2214c5aa9e3bSLendacky, Thomas }
2215c5aa9e3bSLendacky, Thomas 
__xgbe_exit(struct xgbe_prv_data * pdata)22165ffc0335SLendacky, Thomas static int __xgbe_exit(struct xgbe_prv_data *pdata)
2217c5aa9e3bSLendacky, Thomas {
2218c5aa9e3bSLendacky, Thomas 	unsigned int count = 2000;
2219c5aa9e3bSLendacky, Thomas 
2220c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_exit\n");
2221c5aa9e3bSLendacky, Thomas 
2222c5aa9e3bSLendacky, Thomas 	/* Issue a software reset */
2223c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, DMA_MR, SWR, 1);
2224c5aa9e3bSLendacky, Thomas 	usleep_range(10, 15);
2225c5aa9e3bSLendacky, Thomas 
2226c5aa9e3bSLendacky, Thomas 	/* Poll Until Poll Condition */
2227c7557e6aSDan Carpenter 	while (--count && XGMAC_IOREAD_BITS(pdata, DMA_MR, SWR))
2228c5aa9e3bSLendacky, Thomas 		usleep_range(500, 600);
2229c5aa9e3bSLendacky, Thomas 
2230c5aa9e3bSLendacky, Thomas 	if (!count)
2231c5aa9e3bSLendacky, Thomas 		return -EBUSY;
2232c5aa9e3bSLendacky, Thomas 
2233c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_exit\n");
2234c5aa9e3bSLendacky, Thomas 
2235c5aa9e3bSLendacky, Thomas 	return 0;
2236c5aa9e3bSLendacky, Thomas }
2237c5aa9e3bSLendacky, Thomas 
xgbe_exit(struct xgbe_prv_data * pdata)22385ffc0335SLendacky, Thomas static int xgbe_exit(struct xgbe_prv_data *pdata)
22395ffc0335SLendacky, Thomas {
22405ffc0335SLendacky, Thomas 	int ret;
22415ffc0335SLendacky, Thomas 
22425ffc0335SLendacky, Thomas 	/* To guard against possible incorrectly generated interrupts,
22435ffc0335SLendacky, Thomas 	 * issue the software reset twice.
22445ffc0335SLendacky, Thomas 	 */
22455ffc0335SLendacky, Thomas 	ret = __xgbe_exit(pdata);
22465ffc0335SLendacky, Thomas 	if (ret)
22475ffc0335SLendacky, Thomas 		return ret;
22485ffc0335SLendacky, Thomas 
22495ffc0335SLendacky, Thomas 	return __xgbe_exit(pdata);
22505ffc0335SLendacky, Thomas }
22515ffc0335SLendacky, Thomas 
xgbe_flush_tx_queues(struct xgbe_prv_data * pdata)2252c5aa9e3bSLendacky, Thomas static int xgbe_flush_tx_queues(struct xgbe_prv_data *pdata)
2253c5aa9e3bSLendacky, Thomas {
2254c5aa9e3bSLendacky, Thomas 	unsigned int i, count;
2255c5aa9e3bSLendacky, Thomas 
2256a9a4a2d9SLendacky, Thomas 	if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) < 0x21)
2257a9a4a2d9SLendacky, Thomas 		return 0;
2258a9a4a2d9SLendacky, Thomas 
2259853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
2260c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, FTQ, 1);
2261c5aa9e3bSLendacky, Thomas 
2262c5aa9e3bSLendacky, Thomas 	/* Poll Until Poll Condition */
2263853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++) {
2264c5aa9e3bSLendacky, Thomas 		count = 2000;
2265c7557e6aSDan Carpenter 		while (--count && XGMAC_MTL_IOREAD_BITS(pdata, i,
2266c5aa9e3bSLendacky, Thomas 							MTL_Q_TQOMR, FTQ))
2267c5aa9e3bSLendacky, Thomas 			usleep_range(500, 600);
2268c5aa9e3bSLendacky, Thomas 
2269c5aa9e3bSLendacky, Thomas 		if (!count)
2270c5aa9e3bSLendacky, Thomas 			return -EBUSY;
2271c5aa9e3bSLendacky, Thomas 	}
2272c5aa9e3bSLendacky, Thomas 
2273c5aa9e3bSLendacky, Thomas 	return 0;
2274c5aa9e3bSLendacky, Thomas }
2275c5aa9e3bSLendacky, Thomas 
xgbe_config_dma_bus(struct xgbe_prv_data * pdata)2276c5aa9e3bSLendacky, Thomas static void xgbe_config_dma_bus(struct xgbe_prv_data *pdata)
2277c5aa9e3bSLendacky, Thomas {
22786f595959SLendacky, Thomas 	unsigned int sbmr;
22796f595959SLendacky, Thomas 
22806f595959SLendacky, Thomas 	sbmr = XGMAC_IOREAD(pdata, DMA_SBMR);
22816f595959SLendacky, Thomas 
2282c5aa9e3bSLendacky, Thomas 	/* Set enhanced addressing mode */
22836f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, EAME, 1);
2284c5aa9e3bSLendacky, Thomas 
2285c5aa9e3bSLendacky, Thomas 	/* Set the System Bus mode */
22866f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, UNDEF, 1);
22876f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, BLEN, pdata->blen >> 2);
22886f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, AAL, pdata->aal);
22896f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, RD_OSR_LMT, pdata->rd_osr_limit - 1);
22906f595959SLendacky, Thomas 	XGMAC_SET_BITS(sbmr, DMA_SBMR, WR_OSR_LMT, pdata->wr_osr_limit - 1);
22916f595959SLendacky, Thomas 
22926f595959SLendacky, Thomas 	XGMAC_IOWRITE(pdata, DMA_SBMR, sbmr);
22936f595959SLendacky, Thomas 
22946f595959SLendacky, Thomas 	/* Set descriptor fetching threshold */
22956f595959SLendacky, Thomas 	if (pdata->vdata->tx_desc_prefetch)
22966f595959SLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, DMA_TXEDMACR, TDPS,
22976f595959SLendacky, Thomas 				   pdata->vdata->tx_desc_prefetch);
22986f595959SLendacky, Thomas 
22996f595959SLendacky, Thomas 	if (pdata->vdata->rx_desc_prefetch)
23006f595959SLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, DMA_RXEDMACR, RDPS,
23016f595959SLendacky, Thomas 				   pdata->vdata->rx_desc_prefetch);
2302c5aa9e3bSLendacky, Thomas }
2303c5aa9e3bSLendacky, Thomas 
xgbe_config_dma_cache(struct xgbe_prv_data * pdata)2304c5aa9e3bSLendacky, Thomas static void xgbe_config_dma_cache(struct xgbe_prv_data *pdata)
2305c5aa9e3bSLendacky, Thomas {
23069916716aSLendacky, Thomas 	XGMAC_IOWRITE(pdata, DMA_AXIARCR, pdata->arcr);
23079916716aSLendacky, Thomas 	XGMAC_IOWRITE(pdata, DMA_AXIAWCR, pdata->awcr);
23086f595959SLendacky, Thomas 	if (pdata->awarcr)
23096f595959SLendacky, Thomas 		XGMAC_IOWRITE(pdata, DMA_AXIAWARCR, pdata->awarcr);
2310c5aa9e3bSLendacky, Thomas }
2311c5aa9e3bSLendacky, Thomas 
xgbe_config_mtl_mode(struct xgbe_prv_data * pdata)2312c5aa9e3bSLendacky, Thomas static void xgbe_config_mtl_mode(struct xgbe_prv_data *pdata)
2313c5aa9e3bSLendacky, Thomas {
2314c5aa9e3bSLendacky, Thomas 	unsigned int i;
2315c5aa9e3bSLendacky, Thomas 
2316fca2d994SLendacky, Thomas 	/* Set Tx to weighted round robin scheduling algorithm */
2317c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_WRR);
2318c5aa9e3bSLendacky, Thomas 
2319fca2d994SLendacky, Thomas 	/* Set Tx traffic classes to use WRR algorithm with equal weights */
2320fca2d994SLendacky, Thomas 	for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
2321fca2d994SLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
2322fca2d994SLendacky, Thomas 				       MTL_TSA_ETS);
2323fca2d994SLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW, 1);
2324fca2d994SLendacky, Thomas 	}
2325c5aa9e3bSLendacky, Thomas 
2326c5aa9e3bSLendacky, Thomas 	/* Set Rx to strict priority algorithm */
2327c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MTL_OMR, RAA, MTL_RAA_SP);
2328c5aa9e3bSLendacky, Thomas }
2329c5aa9e3bSLendacky, Thomas 
xgbe_queue_flow_control_threshold(struct xgbe_prv_data * pdata,unsigned int queue,unsigned int q_fifo_size)233043e0dcf7SLendacky, Thomas static void xgbe_queue_flow_control_threshold(struct xgbe_prv_data *pdata,
233143e0dcf7SLendacky, Thomas 					      unsigned int queue,
233243e0dcf7SLendacky, Thomas 					      unsigned int q_fifo_size)
233343e0dcf7SLendacky, Thomas {
233443e0dcf7SLendacky, Thomas 	unsigned int frame_fifo_size;
233543e0dcf7SLendacky, Thomas 	unsigned int rfa, rfd;
233643e0dcf7SLendacky, Thomas 
233743e0dcf7SLendacky, Thomas 	frame_fifo_size = XGMAC_FLOW_CONTROL_ALIGN(xgbe_get_max_frame(pdata));
233843e0dcf7SLendacky, Thomas 
233943e0dcf7SLendacky, Thomas 	if (pdata->pfcq[queue] && (q_fifo_size > pdata->pfc_rfa)) {
234043e0dcf7SLendacky, Thomas 		/* PFC is active for this queue */
234143e0dcf7SLendacky, Thomas 		rfa = pdata->pfc_rfa;
234243e0dcf7SLendacky, Thomas 		rfd = rfa + frame_fifo_size;
234343e0dcf7SLendacky, Thomas 		if (rfd > XGMAC_FLOW_CONTROL_MAX)
234443e0dcf7SLendacky, Thomas 			rfd = XGMAC_FLOW_CONTROL_MAX;
234543e0dcf7SLendacky, Thomas 		if (rfa >= XGMAC_FLOW_CONTROL_MAX)
234643e0dcf7SLendacky, Thomas 			rfa = XGMAC_FLOW_CONTROL_MAX - XGMAC_FLOW_CONTROL_UNIT;
234743e0dcf7SLendacky, Thomas 	} else {
234843e0dcf7SLendacky, Thomas 		/* This path deals with just maximum frame sizes which are
234943e0dcf7SLendacky, Thomas 		 * limited to a jumbo frame of 9,000 (plus headers, etc.)
235043e0dcf7SLendacky, Thomas 		 * so we can never exceed the maximum allowable RFA/RFD
235143e0dcf7SLendacky, Thomas 		 * values.
235243e0dcf7SLendacky, Thomas 		 */
235343e0dcf7SLendacky, Thomas 		if (q_fifo_size <= 2048) {
235443e0dcf7SLendacky, Thomas 			/* rx_rfd to zero to signal no flow control */
235543e0dcf7SLendacky, Thomas 			pdata->rx_rfa[queue] = 0;
235643e0dcf7SLendacky, Thomas 			pdata->rx_rfd[queue] = 0;
235743e0dcf7SLendacky, Thomas 			return;
235843e0dcf7SLendacky, Thomas 		}
235943e0dcf7SLendacky, Thomas 
236043e0dcf7SLendacky, Thomas 		if (q_fifo_size <= 4096) {
236143e0dcf7SLendacky, Thomas 			/* Between 2048 and 4096 */
236243e0dcf7SLendacky, Thomas 			pdata->rx_rfa[queue] = 0;	/* Full - 1024 bytes */
236343e0dcf7SLendacky, Thomas 			pdata->rx_rfd[queue] = 1;	/* Full - 1536 bytes */
236443e0dcf7SLendacky, Thomas 			return;
236543e0dcf7SLendacky, Thomas 		}
236643e0dcf7SLendacky, Thomas 
236743e0dcf7SLendacky, Thomas 		if (q_fifo_size <= frame_fifo_size) {
236843e0dcf7SLendacky, Thomas 			/* Between 4096 and max-frame */
236943e0dcf7SLendacky, Thomas 			pdata->rx_rfa[queue] = 2;	/* Full - 2048 bytes */
237043e0dcf7SLendacky, Thomas 			pdata->rx_rfd[queue] = 5;	/* Full - 3584 bytes */
237143e0dcf7SLendacky, Thomas 			return;
237243e0dcf7SLendacky, Thomas 		}
237343e0dcf7SLendacky, Thomas 
237443e0dcf7SLendacky, Thomas 		if (q_fifo_size <= (frame_fifo_size * 3)) {
237543e0dcf7SLendacky, Thomas 			/* Between max-frame and 3 max-frames,
237643e0dcf7SLendacky, Thomas 			 * trigger if we get just over a frame of data and
237743e0dcf7SLendacky, Thomas 			 * resume when we have just under half a frame left.
237843e0dcf7SLendacky, Thomas 			 */
237943e0dcf7SLendacky, Thomas 			rfa = q_fifo_size - frame_fifo_size;
238043e0dcf7SLendacky, Thomas 			rfd = rfa + (frame_fifo_size / 2);
238143e0dcf7SLendacky, Thomas 		} else {
238243e0dcf7SLendacky, Thomas 			/* Above 3 max-frames - trigger when just over
238343e0dcf7SLendacky, Thomas 			 * 2 frames of space available
238443e0dcf7SLendacky, Thomas 			 */
238543e0dcf7SLendacky, Thomas 			rfa = frame_fifo_size * 2;
238643e0dcf7SLendacky, Thomas 			rfa += XGMAC_FLOW_CONTROL_UNIT;
238743e0dcf7SLendacky, Thomas 			rfd = rfa + frame_fifo_size;
238843e0dcf7SLendacky, Thomas 		}
238943e0dcf7SLendacky, Thomas 	}
239043e0dcf7SLendacky, Thomas 
239143e0dcf7SLendacky, Thomas 	pdata->rx_rfa[queue] = XGMAC_FLOW_CONTROL_VALUE(rfa);
239243e0dcf7SLendacky, Thomas 	pdata->rx_rfd[queue] = XGMAC_FLOW_CONTROL_VALUE(rfd);
239343e0dcf7SLendacky, Thomas }
239443e0dcf7SLendacky, Thomas 
xgbe_calculate_flow_control_threshold(struct xgbe_prv_data * pdata,unsigned int * fifo)239543e0dcf7SLendacky, Thomas static void xgbe_calculate_flow_control_threshold(struct xgbe_prv_data *pdata,
239643e0dcf7SLendacky, Thomas 						  unsigned int *fifo)
239743e0dcf7SLendacky, Thomas {
239843e0dcf7SLendacky, Thomas 	unsigned int q_fifo_size;
239943e0dcf7SLendacky, Thomas 	unsigned int i;
240043e0dcf7SLendacky, Thomas 
240143e0dcf7SLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++) {
240243e0dcf7SLendacky, Thomas 		q_fifo_size = (fifo[i] + 1) * XGMAC_FIFO_UNIT;
240343e0dcf7SLendacky, Thomas 
240443e0dcf7SLendacky, Thomas 		xgbe_queue_flow_control_threshold(pdata, i, q_fifo_size);
240543e0dcf7SLendacky, Thomas 	}
240643e0dcf7SLendacky, Thomas }
240743e0dcf7SLendacky, Thomas 
xgbe_config_flow_control_threshold(struct xgbe_prv_data * pdata)240843e0dcf7SLendacky, Thomas static void xgbe_config_flow_control_threshold(struct xgbe_prv_data *pdata)
240943e0dcf7SLendacky, Thomas {
241043e0dcf7SLendacky, Thomas 	unsigned int i;
241143e0dcf7SLendacky, Thomas 
241243e0dcf7SLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++) {
241343e0dcf7SLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFA,
241443e0dcf7SLendacky, Thomas 				       pdata->rx_rfa[i]);
241543e0dcf7SLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQFCR, RFD,
241643e0dcf7SLendacky, Thomas 				       pdata->rx_rfd[i]);
241743e0dcf7SLendacky, Thomas 	}
241843e0dcf7SLendacky, Thomas }
241943e0dcf7SLendacky, Thomas 
xgbe_get_tx_fifo_size(struct xgbe_prv_data * pdata)2420586e3cfbSLendacky, Thomas static unsigned int xgbe_get_tx_fifo_size(struct xgbe_prv_data *pdata)
2421586e3cfbSLendacky, Thomas {
2422586e3cfbSLendacky, Thomas 	/* The configured value may not be the actual amount of fifo RAM */
2423bd8255d8SLendacky, Thomas 	return min_t(unsigned int, pdata->tx_max_fifo_size,
2424bd8255d8SLendacky, Thomas 		     pdata->hw_feat.tx_fifo_size);
2425586e3cfbSLendacky, Thomas }
2426586e3cfbSLendacky, Thomas 
xgbe_get_rx_fifo_size(struct xgbe_prv_data * pdata)2427586e3cfbSLendacky, Thomas static unsigned int xgbe_get_rx_fifo_size(struct xgbe_prv_data *pdata)
2428586e3cfbSLendacky, Thomas {
2429586e3cfbSLendacky, Thomas 	/* The configured value may not be the actual amount of fifo RAM */
2430bd8255d8SLendacky, Thomas 	return min_t(unsigned int, pdata->rx_max_fifo_size,
2431bd8255d8SLendacky, Thomas 		     pdata->hw_feat.rx_fifo_size);
2432586e3cfbSLendacky, Thomas }
2433586e3cfbSLendacky, Thomas 
xgbe_calculate_equal_fifo(unsigned int fifo_size,unsigned int queue_count,unsigned int * fifo)2434586e3cfbSLendacky, Thomas static void xgbe_calculate_equal_fifo(unsigned int fifo_size,
2435586e3cfbSLendacky, Thomas 				      unsigned int queue_count,
2436586e3cfbSLendacky, Thomas 				      unsigned int *fifo)
2437c5aa9e3bSLendacky, Thomas {
24389c439e4bSLendacky, Thomas 	unsigned int q_fifo_size;
24399c439e4bSLendacky, Thomas 	unsigned int p_fifo;
2440586e3cfbSLendacky, Thomas 	unsigned int i;
2441c5aa9e3bSLendacky, Thomas 
2442586e3cfbSLendacky, Thomas 	q_fifo_size = fifo_size / queue_count;
2443c5aa9e3bSLendacky, Thomas 
244443e0dcf7SLendacky, Thomas 	/* Calculate the fifo setting by dividing the queue's fifo size
244543e0dcf7SLendacky, Thomas 	 * by the fifo allocation increment (with 0 representing the
244643e0dcf7SLendacky, Thomas 	 * base allocation increment so decrement the result by 1).
24479c439e4bSLendacky, Thomas 	 */
244843e0dcf7SLendacky, Thomas 	p_fifo = q_fifo_size / XGMAC_FIFO_UNIT;
24499c439e4bSLendacky, Thomas 	if (p_fifo)
24509c439e4bSLendacky, Thomas 		p_fifo--;
2451c5aa9e3bSLendacky, Thomas 
245243e0dcf7SLendacky, Thomas 	/* Distribute the fifo equally amongst the queues */
2453586e3cfbSLendacky, Thomas 	for (i = 0; i < queue_count; i++)
2454586e3cfbSLendacky, Thomas 		fifo[i] = p_fifo;
2455c5aa9e3bSLendacky, Thomas }
2456c5aa9e3bSLendacky, Thomas 
xgbe_set_nonprio_fifos(unsigned int fifo_size,unsigned int queue_count,unsigned int * fifo)245743e0dcf7SLendacky, Thomas static unsigned int xgbe_set_nonprio_fifos(unsigned int fifo_size,
245843e0dcf7SLendacky, Thomas 					   unsigned int queue_count,
245943e0dcf7SLendacky, Thomas 					   unsigned int *fifo)
246043e0dcf7SLendacky, Thomas {
246143e0dcf7SLendacky, Thomas 	unsigned int i;
246243e0dcf7SLendacky, Thomas 
246343e0dcf7SLendacky, Thomas 	BUILD_BUG_ON_NOT_POWER_OF_2(XGMAC_FIFO_MIN_ALLOC);
246443e0dcf7SLendacky, Thomas 
246543e0dcf7SLendacky, Thomas 	if (queue_count <= IEEE_8021QAZ_MAX_TCS)
246643e0dcf7SLendacky, Thomas 		return fifo_size;
246743e0dcf7SLendacky, Thomas 
246843e0dcf7SLendacky, Thomas 	/* Rx queues 9 and up are for specialized packets,
246943e0dcf7SLendacky, Thomas 	 * such as PTP or DCB control packets, etc. and
247043e0dcf7SLendacky, Thomas 	 * don't require a large fifo
247143e0dcf7SLendacky, Thomas 	 */
247243e0dcf7SLendacky, Thomas 	for (i = IEEE_8021QAZ_MAX_TCS; i < queue_count; i++) {
247343e0dcf7SLendacky, Thomas 		fifo[i] = (XGMAC_FIFO_MIN_ALLOC / XGMAC_FIFO_UNIT) - 1;
247443e0dcf7SLendacky, Thomas 		fifo_size -= XGMAC_FIFO_MIN_ALLOC;
247543e0dcf7SLendacky, Thomas 	}
247643e0dcf7SLendacky, Thomas 
247743e0dcf7SLendacky, Thomas 	return fifo_size;
247843e0dcf7SLendacky, Thomas }
247943e0dcf7SLendacky, Thomas 
xgbe_get_pfc_delay(struct xgbe_prv_data * pdata)248043e0dcf7SLendacky, Thomas static unsigned int xgbe_get_pfc_delay(struct xgbe_prv_data *pdata)
248143e0dcf7SLendacky, Thomas {
248243e0dcf7SLendacky, Thomas 	unsigned int delay;
248343e0dcf7SLendacky, Thomas 
248443e0dcf7SLendacky, Thomas 	/* If a delay has been provided, use that */
248543e0dcf7SLendacky, Thomas 	if (pdata->pfc->delay)
248643e0dcf7SLendacky, Thomas 		return pdata->pfc->delay / 8;
248743e0dcf7SLendacky, Thomas 
248843e0dcf7SLendacky, Thomas 	/* Allow for two maximum size frames */
248943e0dcf7SLendacky, Thomas 	delay = xgbe_get_max_frame(pdata);
249043e0dcf7SLendacky, Thomas 	delay += XGMAC_ETH_PREAMBLE;
249143e0dcf7SLendacky, Thomas 	delay *= 2;
249243e0dcf7SLendacky, Thomas 
249343e0dcf7SLendacky, Thomas 	/* Allow for PFC frame */
249443e0dcf7SLendacky, Thomas 	delay += XGMAC_PFC_DATA_LEN;
249543e0dcf7SLendacky, Thomas 	delay += ETH_HLEN + ETH_FCS_LEN;
249643e0dcf7SLendacky, Thomas 	delay += XGMAC_ETH_PREAMBLE;
249743e0dcf7SLendacky, Thomas 
249843e0dcf7SLendacky, Thomas 	/* Allow for miscellaneous delays (LPI exit, cable, etc.) */
249943e0dcf7SLendacky, Thomas 	delay += XGMAC_PFC_DELAYS;
250043e0dcf7SLendacky, Thomas 
250143e0dcf7SLendacky, Thomas 	return delay;
250243e0dcf7SLendacky, Thomas }
250343e0dcf7SLendacky, Thomas 
xgbe_get_pfc_queues(struct xgbe_prv_data * pdata)250443e0dcf7SLendacky, Thomas static unsigned int xgbe_get_pfc_queues(struct xgbe_prv_data *pdata)
250543e0dcf7SLendacky, Thomas {
250643e0dcf7SLendacky, Thomas 	unsigned int count, prio_queues;
250743e0dcf7SLendacky, Thomas 	unsigned int i;
250843e0dcf7SLendacky, Thomas 
250943e0dcf7SLendacky, Thomas 	if (!pdata->pfc->pfc_en)
251043e0dcf7SLendacky, Thomas 		return 0;
251143e0dcf7SLendacky, Thomas 
251243e0dcf7SLendacky, Thomas 	count = 0;
251343e0dcf7SLendacky, Thomas 	prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
251443e0dcf7SLendacky, Thomas 	for (i = 0; i < prio_queues; i++) {
251543e0dcf7SLendacky, Thomas 		if (!xgbe_is_pfc_queue(pdata, i))
251643e0dcf7SLendacky, Thomas 			continue;
251743e0dcf7SLendacky, Thomas 
251843e0dcf7SLendacky, Thomas 		pdata->pfcq[i] = 1;
251943e0dcf7SLendacky, Thomas 		count++;
252043e0dcf7SLendacky, Thomas 	}
252143e0dcf7SLendacky, Thomas 
252243e0dcf7SLendacky, Thomas 	return count;
252343e0dcf7SLendacky, Thomas }
252443e0dcf7SLendacky, Thomas 
xgbe_calculate_dcb_fifo(struct xgbe_prv_data * pdata,unsigned int fifo_size,unsigned int * fifo)252543e0dcf7SLendacky, Thomas static void xgbe_calculate_dcb_fifo(struct xgbe_prv_data *pdata,
252643e0dcf7SLendacky, Thomas 				    unsigned int fifo_size,
252743e0dcf7SLendacky, Thomas 				    unsigned int *fifo)
252843e0dcf7SLendacky, Thomas {
252943e0dcf7SLendacky, Thomas 	unsigned int q_fifo_size, rem_fifo, addn_fifo;
253043e0dcf7SLendacky, Thomas 	unsigned int prio_queues;
253143e0dcf7SLendacky, Thomas 	unsigned int pfc_count;
253243e0dcf7SLendacky, Thomas 	unsigned int i;
253343e0dcf7SLendacky, Thomas 
253443e0dcf7SLendacky, Thomas 	q_fifo_size = XGMAC_FIFO_ALIGN(xgbe_get_max_frame(pdata));
253543e0dcf7SLendacky, Thomas 	prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
253643e0dcf7SLendacky, Thomas 	pfc_count = xgbe_get_pfc_queues(pdata);
253743e0dcf7SLendacky, Thomas 
253843e0dcf7SLendacky, Thomas 	if (!pfc_count || ((q_fifo_size * prio_queues) > fifo_size)) {
253943e0dcf7SLendacky, Thomas 		/* No traffic classes with PFC enabled or can't do lossless */
254043e0dcf7SLendacky, Thomas 		xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
254143e0dcf7SLendacky, Thomas 		return;
254243e0dcf7SLendacky, Thomas 	}
254343e0dcf7SLendacky, Thomas 
254443e0dcf7SLendacky, Thomas 	/* Calculate how much fifo we have to play with */
254543e0dcf7SLendacky, Thomas 	rem_fifo = fifo_size - (q_fifo_size * prio_queues);
254643e0dcf7SLendacky, Thomas 
254743e0dcf7SLendacky, Thomas 	/* Calculate how much more than base fifo PFC needs, which also
254843e0dcf7SLendacky, Thomas 	 * becomes the threshold activation point (RFA)
254943e0dcf7SLendacky, Thomas 	 */
255043e0dcf7SLendacky, Thomas 	pdata->pfc_rfa = xgbe_get_pfc_delay(pdata);
255143e0dcf7SLendacky, Thomas 	pdata->pfc_rfa = XGMAC_FLOW_CONTROL_ALIGN(pdata->pfc_rfa);
255243e0dcf7SLendacky, Thomas 
255343e0dcf7SLendacky, Thomas 	if (pdata->pfc_rfa > q_fifo_size) {
255443e0dcf7SLendacky, Thomas 		addn_fifo = pdata->pfc_rfa - q_fifo_size;
255543e0dcf7SLendacky, Thomas 		addn_fifo = XGMAC_FIFO_ALIGN(addn_fifo);
255643e0dcf7SLendacky, Thomas 	} else {
255743e0dcf7SLendacky, Thomas 		addn_fifo = 0;
255843e0dcf7SLendacky, Thomas 	}
255943e0dcf7SLendacky, Thomas 
256043e0dcf7SLendacky, Thomas 	/* Calculate DCB fifo settings:
256143e0dcf7SLendacky, Thomas 	 *   - distribute remaining fifo between the VLAN priority
256243e0dcf7SLendacky, Thomas 	 *     queues based on traffic class PFC enablement and overall
256343e0dcf7SLendacky, Thomas 	 *     priority (0 is lowest priority, so start at highest)
256443e0dcf7SLendacky, Thomas 	 */
256543e0dcf7SLendacky, Thomas 	i = prio_queues;
256643e0dcf7SLendacky, Thomas 	while (i > 0) {
256743e0dcf7SLendacky, Thomas 		i--;
256843e0dcf7SLendacky, Thomas 
256943e0dcf7SLendacky, Thomas 		fifo[i] = (q_fifo_size / XGMAC_FIFO_UNIT) - 1;
257043e0dcf7SLendacky, Thomas 
257143e0dcf7SLendacky, Thomas 		if (!pdata->pfcq[i] || !addn_fifo)
257243e0dcf7SLendacky, Thomas 			continue;
257343e0dcf7SLendacky, Thomas 
257443e0dcf7SLendacky, Thomas 		if (addn_fifo > rem_fifo) {
257543e0dcf7SLendacky, Thomas 			netdev_warn(pdata->netdev,
257643e0dcf7SLendacky, Thomas 				    "RXq%u cannot set needed fifo size\n", i);
257743e0dcf7SLendacky, Thomas 			if (!rem_fifo)
257843e0dcf7SLendacky, Thomas 				continue;
257943e0dcf7SLendacky, Thomas 
258043e0dcf7SLendacky, Thomas 			addn_fifo = rem_fifo;
258143e0dcf7SLendacky, Thomas 		}
258243e0dcf7SLendacky, Thomas 
258343e0dcf7SLendacky, Thomas 		fifo[i] += (addn_fifo / XGMAC_FIFO_UNIT);
258443e0dcf7SLendacky, Thomas 		rem_fifo -= addn_fifo;
258543e0dcf7SLendacky, Thomas 	}
258643e0dcf7SLendacky, Thomas 
258743e0dcf7SLendacky, Thomas 	if (rem_fifo) {
258843e0dcf7SLendacky, Thomas 		unsigned int inc_fifo = rem_fifo / prio_queues;
258943e0dcf7SLendacky, Thomas 
259043e0dcf7SLendacky, Thomas 		/* Distribute remaining fifo across queues */
259143e0dcf7SLendacky, Thomas 		for (i = 0; i < prio_queues; i++)
259243e0dcf7SLendacky, Thomas 			fifo[i] += (inc_fifo / XGMAC_FIFO_UNIT);
259343e0dcf7SLendacky, Thomas 	}
259443e0dcf7SLendacky, Thomas }
259543e0dcf7SLendacky, Thomas 
xgbe_config_tx_fifo_size(struct xgbe_prv_data * pdata)2596c5aa9e3bSLendacky, Thomas static void xgbe_config_tx_fifo_size(struct xgbe_prv_data *pdata)
2597c5aa9e3bSLendacky, Thomas {
25989c439e4bSLendacky, Thomas 	unsigned int fifo_size;
2599586e3cfbSLendacky, Thomas 	unsigned int fifo[XGBE_MAX_QUEUES];
2600c5aa9e3bSLendacky, Thomas 	unsigned int i;
2601c5aa9e3bSLendacky, Thomas 
2602586e3cfbSLendacky, Thomas 	fifo_size = xgbe_get_tx_fifo_size(pdata);
2603586e3cfbSLendacky, Thomas 
2604586e3cfbSLendacky, Thomas 	xgbe_calculate_equal_fifo(fifo_size, pdata->tx_q_count, fifo);
2605c5aa9e3bSLendacky, Thomas 
2606853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
2607586e3cfbSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TQS, fifo[i]);
2608c5aa9e3bSLendacky, Thomas 
260934bf65dfSLendacky, Thomas 	netif_info(pdata, drv, pdata->netdev,
2610600c8811SLendacky, Thomas 		   "%d Tx hardware queues, %d byte fifo per queue\n",
261143e0dcf7SLendacky, Thomas 		   pdata->tx_q_count, ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
2612c5aa9e3bSLendacky, Thomas }
2613c5aa9e3bSLendacky, Thomas 
xgbe_config_rx_fifo_size(struct xgbe_prv_data * pdata)2614c5aa9e3bSLendacky, Thomas static void xgbe_config_rx_fifo_size(struct xgbe_prv_data *pdata)
2615c5aa9e3bSLendacky, Thomas {
26169c439e4bSLendacky, Thomas 	unsigned int fifo_size;
2617586e3cfbSLendacky, Thomas 	unsigned int fifo[XGBE_MAX_QUEUES];
261843e0dcf7SLendacky, Thomas 	unsigned int prio_queues;
2619c5aa9e3bSLendacky, Thomas 	unsigned int i;
2620c5aa9e3bSLendacky, Thomas 
262143e0dcf7SLendacky, Thomas 	/* Clear any DCB related fifo/queue information */
262243e0dcf7SLendacky, Thomas 	memset(pdata->pfcq, 0, sizeof(pdata->pfcq));
262343e0dcf7SLendacky, Thomas 	pdata->pfc_rfa = 0;
2624586e3cfbSLendacky, Thomas 
262543e0dcf7SLendacky, Thomas 	fifo_size = xgbe_get_rx_fifo_size(pdata);
262643e0dcf7SLendacky, Thomas 	prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
262743e0dcf7SLendacky, Thomas 
262843e0dcf7SLendacky, Thomas 	/* Assign a minimum fifo to the non-VLAN priority queues */
262943e0dcf7SLendacky, Thomas 	fifo_size = xgbe_set_nonprio_fifos(fifo_size, pdata->rx_q_count, fifo);
263043e0dcf7SLendacky, Thomas 
263143e0dcf7SLendacky, Thomas 	if (pdata->pfc && pdata->ets)
263243e0dcf7SLendacky, Thomas 		xgbe_calculate_dcb_fifo(pdata, fifo_size, fifo);
263343e0dcf7SLendacky, Thomas 	else
263443e0dcf7SLendacky, Thomas 		xgbe_calculate_equal_fifo(fifo_size, prio_queues, fifo);
2635c5aa9e3bSLendacky, Thomas 
2636853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
2637586e3cfbSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_RQOMR, RQS, fifo[i]);
2638c5aa9e3bSLendacky, Thomas 
263943e0dcf7SLendacky, Thomas 	xgbe_calculate_flow_control_threshold(pdata, fifo);
264043e0dcf7SLendacky, Thomas 	xgbe_config_flow_control_threshold(pdata);
264143e0dcf7SLendacky, Thomas 
264243e0dcf7SLendacky, Thomas 	if (pdata->pfc && pdata->ets && pdata->pfc->pfc_en) {
264334bf65dfSLendacky, Thomas 		netif_info(pdata, drv, pdata->netdev,
264443e0dcf7SLendacky, Thomas 			   "%u Rx hardware queues\n", pdata->rx_q_count);
264543e0dcf7SLendacky, Thomas 		for (i = 0; i < pdata->rx_q_count; i++)
264643e0dcf7SLendacky, Thomas 			netif_info(pdata, drv, pdata->netdev,
264743e0dcf7SLendacky, Thomas 				   "RxQ%u, %u byte fifo queue\n", i,
264843e0dcf7SLendacky, Thomas 				   ((fifo[i] + 1) * XGMAC_FIFO_UNIT));
264943e0dcf7SLendacky, Thomas 	} else {
265043e0dcf7SLendacky, Thomas 		netif_info(pdata, drv, pdata->netdev,
265143e0dcf7SLendacky, Thomas 			   "%u Rx hardware queues, %u byte fifo per queue\n",
265243e0dcf7SLendacky, Thomas 			   pdata->rx_q_count,
265343e0dcf7SLendacky, Thomas 			   ((fifo[0] + 1) * XGMAC_FIFO_UNIT));
265443e0dcf7SLendacky, Thomas 	}
2655c5aa9e3bSLendacky, Thomas }
2656c5aa9e3bSLendacky, Thomas 
xgbe_config_queue_mapping(struct xgbe_prv_data * pdata)2657fca2d994SLendacky, Thomas static void xgbe_config_queue_mapping(struct xgbe_prv_data *pdata)
2658c5aa9e3bSLendacky, Thomas {
2659fca2d994SLendacky, Thomas 	unsigned int qptc, qptc_extra, queue;
2660fca2d994SLendacky, Thomas 	unsigned int prio_queues;
2661fca2d994SLendacky, Thomas 	unsigned int ppq, ppq_extra, prio;
2662fca2d994SLendacky, Thomas 	unsigned int mask;
2663fca2d994SLendacky, Thomas 	unsigned int i, j, reg, reg_val;
2664fca2d994SLendacky, Thomas 
2665fca2d994SLendacky, Thomas 	/* Map the MTL Tx Queues to Traffic Classes
2666fca2d994SLendacky, Thomas 	 *   Note: Tx Queues >= Traffic Classes
2667fca2d994SLendacky, Thomas 	 */
2668fca2d994SLendacky, Thomas 	qptc = pdata->tx_q_count / pdata->hw_feat.tc_cnt;
2669fca2d994SLendacky, Thomas 	qptc_extra = pdata->tx_q_count % pdata->hw_feat.tc_cnt;
2670fca2d994SLendacky, Thomas 
2671fca2d994SLendacky, Thomas 	for (i = 0, queue = 0; i < pdata->hw_feat.tc_cnt; i++) {
2672fca2d994SLendacky, Thomas 		for (j = 0; j < qptc; j++) {
267334bf65dfSLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
267434bf65dfSLendacky, Thomas 				  "TXq%u mapped to TC%u\n", queue, i);
2675fca2d994SLendacky, Thomas 			XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2676fca2d994SLendacky, Thomas 					       Q2TCMAP, i);
2677fca2d994SLendacky, Thomas 			pdata->q2tc_map[queue++] = i;
2678fca2d994SLendacky, Thomas 		}
2679fca2d994SLendacky, Thomas 
2680fca2d994SLendacky, Thomas 		if (i < qptc_extra) {
268134bf65dfSLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
268234bf65dfSLendacky, Thomas 				  "TXq%u mapped to TC%u\n", queue, i);
2683fca2d994SLendacky, Thomas 			XGMAC_MTL_IOWRITE_BITS(pdata, queue, MTL_Q_TQOMR,
2684fca2d994SLendacky, Thomas 					       Q2TCMAP, i);
2685fca2d994SLendacky, Thomas 			pdata->q2tc_map[queue++] = i;
2686fca2d994SLendacky, Thomas 		}
2687fca2d994SLendacky, Thomas 	}
2688fca2d994SLendacky, Thomas 
2689fca2d994SLendacky, Thomas 	/* Map the 8 VLAN priority values to available MTL Rx queues */
269043e0dcf7SLendacky, Thomas 	prio_queues = XGMAC_PRIO_QUEUES(pdata->rx_q_count);
2691fca2d994SLendacky, Thomas 	ppq = IEEE_8021QAZ_MAX_TCS / prio_queues;
2692fca2d994SLendacky, Thomas 	ppq_extra = IEEE_8021QAZ_MAX_TCS % prio_queues;
2693fca2d994SLendacky, Thomas 
2694fca2d994SLendacky, Thomas 	reg = MAC_RQC2R;
2695fca2d994SLendacky, Thomas 	reg_val = 0;
2696fca2d994SLendacky, Thomas 	for (i = 0, prio = 0; i < prio_queues;) {
2697fca2d994SLendacky, Thomas 		mask = 0;
2698fca2d994SLendacky, Thomas 		for (j = 0; j < ppq; j++) {
269934bf65dfSLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
270034bf65dfSLendacky, Thomas 				  "PRIO%u mapped to RXq%u\n", prio, i);
2701fca2d994SLendacky, Thomas 			mask |= (1 << prio);
2702fca2d994SLendacky, Thomas 			pdata->prio2q_map[prio++] = i;
2703fca2d994SLendacky, Thomas 		}
2704fca2d994SLendacky, Thomas 
2705fca2d994SLendacky, Thomas 		if (i < ppq_extra) {
270634bf65dfSLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
270734bf65dfSLendacky, Thomas 				  "PRIO%u mapped to RXq%u\n", prio, i);
2708fca2d994SLendacky, Thomas 			mask |= (1 << prio);
2709fca2d994SLendacky, Thomas 			pdata->prio2q_map[prio++] = i;
2710fca2d994SLendacky, Thomas 		}
2711fca2d994SLendacky, Thomas 
2712fca2d994SLendacky, Thomas 		reg_val |= (mask << ((i++ % MAC_RQC2_Q_PER_REG) << 3));
2713fca2d994SLendacky, Thomas 
2714fca2d994SLendacky, Thomas 		if ((i % MAC_RQC2_Q_PER_REG) && (i != prio_queues))
2715fca2d994SLendacky, Thomas 			continue;
2716fca2d994SLendacky, Thomas 
2717fca2d994SLendacky, Thomas 		XGMAC_IOWRITE(pdata, reg, reg_val);
2718fca2d994SLendacky, Thomas 		reg += MAC_RQC2_INC;
2719fca2d994SLendacky, Thomas 		reg_val = 0;
2720fca2d994SLendacky, Thomas 	}
2721c5aa9e3bSLendacky, Thomas 
2722c5aa9e3bSLendacky, Thomas 	/* Select dynamic mapping of MTL Rx queue to DMA Rx channel */
2723c5aa9e3bSLendacky, Thomas 	reg = MTL_RQDCM0R;
2724c5aa9e3bSLendacky, Thomas 	reg_val = 0;
2725fca2d994SLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count;) {
2726c5aa9e3bSLendacky, Thomas 		reg_val |= (0x80 << ((i++ % MTL_RQDCM_Q_PER_REG) << 3));
2727c5aa9e3bSLendacky, Thomas 
2728fca2d994SLendacky, Thomas 		if ((i % MTL_RQDCM_Q_PER_REG) && (i != pdata->rx_q_count))
2729c5aa9e3bSLendacky, Thomas 			continue;
2730c5aa9e3bSLendacky, Thomas 
2731c5aa9e3bSLendacky, Thomas 		XGMAC_IOWRITE(pdata, reg, reg_val);
2732c5aa9e3bSLendacky, Thomas 
2733c5aa9e3bSLendacky, Thomas 		reg += MTL_RQDCM_INC;
2734c5aa9e3bSLendacky, Thomas 		reg_val = 0;
2735c5aa9e3bSLendacky, Thomas 	}
2736c5aa9e3bSLendacky, Thomas }
2737c5aa9e3bSLendacky, Thomas 
xgbe_config_tc(struct xgbe_prv_data * pdata)273843e0dcf7SLendacky, Thomas static void xgbe_config_tc(struct xgbe_prv_data *pdata)
2739c5aa9e3bSLendacky, Thomas {
274043e0dcf7SLendacky, Thomas 	unsigned int offset, queue, prio;
274143e0dcf7SLendacky, Thomas 	u8 i;
2742c5aa9e3bSLendacky, Thomas 
274343e0dcf7SLendacky, Thomas 	netdev_reset_tc(pdata->netdev);
274443e0dcf7SLendacky, Thomas 	if (!pdata->num_tcs)
274543e0dcf7SLendacky, Thomas 		return;
2746c5aa9e3bSLendacky, Thomas 
274743e0dcf7SLendacky, Thomas 	netdev_set_num_tc(pdata->netdev, pdata->num_tcs);
274843e0dcf7SLendacky, Thomas 
274943e0dcf7SLendacky, Thomas 	for (i = 0, queue = 0, offset = 0; i < pdata->num_tcs; i++) {
275043e0dcf7SLendacky, Thomas 		while ((queue < pdata->tx_q_count) &&
275143e0dcf7SLendacky, Thomas 		       (pdata->q2tc_map[queue] == i))
275243e0dcf7SLendacky, Thomas 			queue++;
275343e0dcf7SLendacky, Thomas 
275443e0dcf7SLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "TC%u using TXq%u-%u\n",
275543e0dcf7SLendacky, Thomas 			  i, offset, queue - 1);
275643e0dcf7SLendacky, Thomas 		netdev_set_tc_queue(pdata->netdev, i, queue - offset, offset);
275743e0dcf7SLendacky, Thomas 		offset = queue;
275843e0dcf7SLendacky, Thomas 	}
275943e0dcf7SLendacky, Thomas 
276043e0dcf7SLendacky, Thomas 	if (!pdata->ets)
276143e0dcf7SLendacky, Thomas 		return;
276243e0dcf7SLendacky, Thomas 
276343e0dcf7SLendacky, Thomas 	for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++)
276443e0dcf7SLendacky, Thomas 		netdev_set_prio_tc_map(pdata->netdev, prio,
276543e0dcf7SLendacky, Thomas 				       pdata->ets->prio_tc[prio]);
276643e0dcf7SLendacky, Thomas }
276743e0dcf7SLendacky, Thomas 
xgbe_config_dcb_tc(struct xgbe_prv_data * pdata)276843e0dcf7SLendacky, Thomas static void xgbe_config_dcb_tc(struct xgbe_prv_data *pdata)
276943e0dcf7SLendacky, Thomas {
277043e0dcf7SLendacky, Thomas 	struct ieee_ets *ets = pdata->ets;
277143e0dcf7SLendacky, Thomas 	unsigned int total_weight, min_weight, weight;
277243e0dcf7SLendacky, Thomas 	unsigned int mask, reg, reg_val;
277343e0dcf7SLendacky, Thomas 	unsigned int i, prio;
277443e0dcf7SLendacky, Thomas 
277543e0dcf7SLendacky, Thomas 	if (!ets)
277643e0dcf7SLendacky, Thomas 		return;
277743e0dcf7SLendacky, Thomas 
277843e0dcf7SLendacky, Thomas 	/* Set Tx to deficit weighted round robin scheduling algorithm (when
277943e0dcf7SLendacky, Thomas 	 * traffic class is using ETS algorithm)
278043e0dcf7SLendacky, Thomas 	 */
278143e0dcf7SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MTL_OMR, ETSALG, MTL_ETSALG_DWRR);
278243e0dcf7SLendacky, Thomas 
278343e0dcf7SLendacky, Thomas 	/* Set Traffic Class algorithms */
278443e0dcf7SLendacky, Thomas 	total_weight = pdata->netdev->mtu * pdata->hw_feat.tc_cnt;
278543e0dcf7SLendacky, Thomas 	min_weight = total_weight / 100;
278643e0dcf7SLendacky, Thomas 	if (!min_weight)
278743e0dcf7SLendacky, Thomas 		min_weight = 1;
278843e0dcf7SLendacky, Thomas 
278943e0dcf7SLendacky, Thomas 	for (i = 0; i < pdata->hw_feat.tc_cnt; i++) {
279043e0dcf7SLendacky, Thomas 		/* Map the priorities to the traffic class */
279143e0dcf7SLendacky, Thomas 		mask = 0;
279243e0dcf7SLendacky, Thomas 		for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; prio++) {
279343e0dcf7SLendacky, Thomas 			if (ets->prio_tc[prio] == i)
279443e0dcf7SLendacky, Thomas 				mask |= (1 << prio);
279543e0dcf7SLendacky, Thomas 		}
279643e0dcf7SLendacky, Thomas 		mask &= 0xff;
279743e0dcf7SLendacky, Thomas 
279843e0dcf7SLendacky, Thomas 		netif_dbg(pdata, drv, pdata->netdev, "TC%u PRIO mask=%#x\n",
279943e0dcf7SLendacky, Thomas 			  i, mask);
280043e0dcf7SLendacky, Thomas 		reg = MTL_TCPM0R + (MTL_TCPM_INC * (i / MTL_TCPM_TC_PER_REG));
280143e0dcf7SLendacky, Thomas 		reg_val = XGMAC_IOREAD(pdata, reg);
280243e0dcf7SLendacky, Thomas 
280343e0dcf7SLendacky, Thomas 		reg_val &= ~(0xff << ((i % MTL_TCPM_TC_PER_REG) << 3));
280443e0dcf7SLendacky, Thomas 		reg_val |= (mask << ((i % MTL_TCPM_TC_PER_REG) << 3));
280543e0dcf7SLendacky, Thomas 
280643e0dcf7SLendacky, Thomas 		XGMAC_IOWRITE(pdata, reg, reg_val);
280743e0dcf7SLendacky, Thomas 
280843e0dcf7SLendacky, Thomas 		/* Set the traffic class algorithm */
280943e0dcf7SLendacky, Thomas 		switch (ets->tc_tsa[i]) {
281043e0dcf7SLendacky, Thomas 		case IEEE_8021QAZ_TSA_STRICT:
281143e0dcf7SLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
281243e0dcf7SLendacky, Thomas 				  "TC%u using SP\n", i);
281343e0dcf7SLendacky, Thomas 			XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
281443e0dcf7SLendacky, Thomas 					       MTL_TSA_SP);
281543e0dcf7SLendacky, Thomas 			break;
281643e0dcf7SLendacky, Thomas 		case IEEE_8021QAZ_TSA_ETS:
281743e0dcf7SLendacky, Thomas 			weight = total_weight * ets->tc_tx_bw[i] / 100;
281843e0dcf7SLendacky, Thomas 			weight = clamp(weight, min_weight, total_weight);
281943e0dcf7SLendacky, Thomas 
282043e0dcf7SLendacky, Thomas 			netif_dbg(pdata, drv, pdata->netdev,
282143e0dcf7SLendacky, Thomas 				  "TC%u using DWRR (weight %u)\n", i, weight);
282243e0dcf7SLendacky, Thomas 			XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_ETSCR, TSA,
282343e0dcf7SLendacky, Thomas 					       MTL_TSA_ETS);
282443e0dcf7SLendacky, Thomas 			XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_TC_QWR, QW,
282543e0dcf7SLendacky, Thomas 					       weight);
282643e0dcf7SLendacky, Thomas 			break;
282743e0dcf7SLendacky, Thomas 		}
282843e0dcf7SLendacky, Thomas 	}
282943e0dcf7SLendacky, Thomas 
283043e0dcf7SLendacky, Thomas 	xgbe_config_tc(pdata);
283143e0dcf7SLendacky, Thomas }
283243e0dcf7SLendacky, Thomas 
xgbe_config_dcb_pfc(struct xgbe_prv_data * pdata)283343e0dcf7SLendacky, Thomas static void xgbe_config_dcb_pfc(struct xgbe_prv_data *pdata)
283443e0dcf7SLendacky, Thomas {
283543e0dcf7SLendacky, Thomas 	if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
283643e0dcf7SLendacky, Thomas 		/* Just stop the Tx queues while Rx fifo is changed */
283743e0dcf7SLendacky, Thomas 		netif_tx_stop_all_queues(pdata->netdev);
283843e0dcf7SLendacky, Thomas 
283943e0dcf7SLendacky, Thomas 		/* Suspend Rx so that fifo's can be adjusted */
284043e0dcf7SLendacky, Thomas 		pdata->hw_if.disable_rx(pdata);
284143e0dcf7SLendacky, Thomas 	}
284243e0dcf7SLendacky, Thomas 
284343e0dcf7SLendacky, Thomas 	xgbe_config_rx_fifo_size(pdata);
284443e0dcf7SLendacky, Thomas 	xgbe_config_flow_control(pdata);
284543e0dcf7SLendacky, Thomas 
284643e0dcf7SLendacky, Thomas 	if (!test_bit(XGBE_DOWN, &pdata->dev_state)) {
284743e0dcf7SLendacky, Thomas 		/* Resume Rx */
284843e0dcf7SLendacky, Thomas 		pdata->hw_if.enable_rx(pdata);
284943e0dcf7SLendacky, Thomas 
285043e0dcf7SLendacky, Thomas 		/* Resume Tx queues */
285143e0dcf7SLendacky, Thomas 		netif_tx_start_all_queues(pdata->netdev);
2852c5aa9e3bSLendacky, Thomas 	}
2853c5aa9e3bSLendacky, Thomas }
2854c5aa9e3bSLendacky, Thomas 
xgbe_config_mac_address(struct xgbe_prv_data * pdata)2855c5aa9e3bSLendacky, Thomas static void xgbe_config_mac_address(struct xgbe_prv_data *pdata)
2856c5aa9e3bSLendacky, Thomas {
2857c5aa9e3bSLendacky, Thomas 	xgbe_set_mac_address(pdata, pdata->netdev->dev_addr);
2858b85e4d89SLendacky, Thomas 
2859b85e4d89SLendacky, Thomas 	/* Filtering is done using perfect filtering and hash filtering */
2860b85e4d89SLendacky, Thomas 	if (pdata->hw_feat.hash_table_size) {
2861b85e4d89SLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
2862b85e4d89SLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
2863b85e4d89SLendacky, Thomas 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, HMC, 1);
2864b85e4d89SLendacky, Thomas 	}
2865c5aa9e3bSLendacky, Thomas }
2866c5aa9e3bSLendacky, Thomas 
xgbe_config_jumbo_enable(struct xgbe_prv_data * pdata)2867c5aa9e3bSLendacky, Thomas static void xgbe_config_jumbo_enable(struct xgbe_prv_data *pdata)
2868c5aa9e3bSLendacky, Thomas {
2869c5aa9e3bSLendacky, Thomas 	unsigned int val;
2870c5aa9e3bSLendacky, Thomas 
2871c5aa9e3bSLendacky, Thomas 	val = (pdata->netdev->mtu > XGMAC_STD_PACKET_MTU) ? 1 : 0;
2872c5aa9e3bSLendacky, Thomas 
2873c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, JE, val);
2874c5aa9e3bSLendacky, Thomas }
2875c5aa9e3bSLendacky, Thomas 
xgbe_config_mac_speed(struct xgbe_prv_data * pdata)2876916102c6SLendacky, Thomas static void xgbe_config_mac_speed(struct xgbe_prv_data *pdata)
2877916102c6SLendacky, Thomas {
2878e57f7a3fSLendacky, Thomas 	xgbe_set_speed(pdata, pdata->phy_speed);
2879916102c6SLendacky, Thomas }
2880916102c6SLendacky, Thomas 
xgbe_config_checksum_offload(struct xgbe_prv_data * pdata)2881c5aa9e3bSLendacky, Thomas static void xgbe_config_checksum_offload(struct xgbe_prv_data *pdata)
2882c5aa9e3bSLendacky, Thomas {
2883c5aa9e3bSLendacky, Thomas 	if (pdata->netdev->features & NETIF_F_RXCSUM)
2884c5aa9e3bSLendacky, Thomas 		xgbe_enable_rx_csum(pdata);
2885c5aa9e3bSLendacky, Thomas 	else
2886c5aa9e3bSLendacky, Thomas 		xgbe_disable_rx_csum(pdata);
2887c5aa9e3bSLendacky, Thomas }
2888c5aa9e3bSLendacky, Thomas 
xgbe_config_vlan_support(struct xgbe_prv_data * pdata)2889c5aa9e3bSLendacky, Thomas static void xgbe_config_vlan_support(struct xgbe_prv_data *pdata)
2890c5aa9e3bSLendacky, Thomas {
28916e5eed04SLendacky, Thomas 	/* Indicate that VLAN Tx CTAGs come from context descriptors */
28926e5eed04SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, CSVL, 0);
28936e5eed04SLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_VLANIR, VLTI, 1);
28946e5eed04SLendacky, Thomas 
2895801c62d9SLendacky, Thomas 	/* Set the current VLAN Hash Table register value */
2896801c62d9SLendacky, Thomas 	xgbe_update_vlan_hash_table(pdata);
2897801c62d9SLendacky, Thomas 
2898801c62d9SLendacky, Thomas 	if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
2899801c62d9SLendacky, Thomas 		xgbe_enable_rx_vlan_filtering(pdata);
2900801c62d9SLendacky, Thomas 	else
2901801c62d9SLendacky, Thomas 		xgbe_disable_rx_vlan_filtering(pdata);
2902801c62d9SLendacky, Thomas 
2903c5aa9e3bSLendacky, Thomas 	if (pdata->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
2904c5aa9e3bSLendacky, Thomas 		xgbe_enable_rx_vlan_stripping(pdata);
2905c5aa9e3bSLendacky, Thomas 	else
2906c5aa9e3bSLendacky, Thomas 		xgbe_disable_rx_vlan_stripping(pdata);
2907c5aa9e3bSLendacky, Thomas }
2908c5aa9e3bSLendacky, Thomas 
xgbe_mmc_read(struct xgbe_prv_data * pdata,unsigned int reg_lo)290960265108SLendacky, Thomas static u64 xgbe_mmc_read(struct xgbe_prv_data *pdata, unsigned int reg_lo)
291060265108SLendacky, Thomas {
291160265108SLendacky, Thomas 	bool read_hi;
291260265108SLendacky, Thomas 	u64 val;
291360265108SLendacky, Thomas 
2914e5a20b90SLendacky, Thomas 	if (pdata->vdata->mmc_64bit) {
2915e5a20b90SLendacky, Thomas 		switch (reg_lo) {
2916e5a20b90SLendacky, Thomas 		/* These registers are always 32 bit */
2917e5a20b90SLendacky, Thomas 		case MMC_RXRUNTERROR:
2918e5a20b90SLendacky, Thomas 		case MMC_RXJABBERERROR:
2919e5a20b90SLendacky, Thomas 		case MMC_RXUNDERSIZE_G:
2920e5a20b90SLendacky, Thomas 		case MMC_RXOVERSIZE_G:
2921e5a20b90SLendacky, Thomas 		case MMC_RXWATCHDOGERROR:
2922e5a20b90SLendacky, Thomas 			read_hi = false;
2923e5a20b90SLendacky, Thomas 			break;
2924e5a20b90SLendacky, Thomas 
2925e5a20b90SLendacky, Thomas 		default:
2926e5a20b90SLendacky, Thomas 			read_hi = true;
2927e5a20b90SLendacky, Thomas 		}
2928e5a20b90SLendacky, Thomas 	} else {
292960265108SLendacky, Thomas 		switch (reg_lo) {
293060265108SLendacky, Thomas 		/* These registers are always 64 bit */
293160265108SLendacky, Thomas 		case MMC_TXOCTETCOUNT_GB_LO:
293260265108SLendacky, Thomas 		case MMC_TXOCTETCOUNT_G_LO:
293360265108SLendacky, Thomas 		case MMC_RXOCTETCOUNT_GB_LO:
293460265108SLendacky, Thomas 		case MMC_RXOCTETCOUNT_G_LO:
293560265108SLendacky, Thomas 			read_hi = true;
293660265108SLendacky, Thomas 			break;
293760265108SLendacky, Thomas 
293860265108SLendacky, Thomas 		default:
293960265108SLendacky, Thomas 			read_hi = false;
29403947d78aSLendacky, Thomas 		}
2941e5a20b90SLendacky, Thomas 	}
294260265108SLendacky, Thomas 
294360265108SLendacky, Thomas 	val = XGMAC_IOREAD(pdata, reg_lo);
294460265108SLendacky, Thomas 
294560265108SLendacky, Thomas 	if (read_hi)
294660265108SLendacky, Thomas 		val |= ((u64)XGMAC_IOREAD(pdata, reg_lo + 4) << 32);
294760265108SLendacky, Thomas 
294860265108SLendacky, Thomas 	return val;
294960265108SLendacky, Thomas }
295060265108SLendacky, Thomas 
xgbe_tx_mmc_int(struct xgbe_prv_data * pdata)2951c5aa9e3bSLendacky, Thomas static void xgbe_tx_mmc_int(struct xgbe_prv_data *pdata)
2952c5aa9e3bSLendacky, Thomas {
2953c5aa9e3bSLendacky, Thomas 	struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
2954c5aa9e3bSLendacky, Thomas 	unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_TISR);
2955c5aa9e3bSLendacky, Thomas 
2956c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_GB))
2957c5aa9e3bSLendacky, Thomas 		stats->txoctetcount_gb +=
295860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
2959c5aa9e3bSLendacky, Thomas 
2960c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_GB))
2961c5aa9e3bSLendacky, Thomas 		stats->txframecount_gb +=
296260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
2963c5aa9e3bSLendacky, Thomas 
2964c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_G))
2965c5aa9e3bSLendacky, Thomas 		stats->txbroadcastframes_g +=
296660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
2967c5aa9e3bSLendacky, Thomas 
2968c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_G))
2969c5aa9e3bSLendacky, Thomas 		stats->txmulticastframes_g +=
297060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
2971c5aa9e3bSLendacky, Thomas 
2972c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX64OCTETS_GB))
2973c5aa9e3bSLendacky, Thomas 		stats->tx64octets_gb +=
297460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
2975c5aa9e3bSLendacky, Thomas 
2976c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX65TO127OCTETS_GB))
2977c5aa9e3bSLendacky, Thomas 		stats->tx65to127octets_gb +=
297860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
2979c5aa9e3bSLendacky, Thomas 
2980c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX128TO255OCTETS_GB))
2981c5aa9e3bSLendacky, Thomas 		stats->tx128to255octets_gb +=
298260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
2983c5aa9e3bSLendacky, Thomas 
2984c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX256TO511OCTETS_GB))
2985c5aa9e3bSLendacky, Thomas 		stats->tx256to511octets_gb +=
298660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
2987c5aa9e3bSLendacky, Thomas 
2988c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX512TO1023OCTETS_GB))
2989c5aa9e3bSLendacky, Thomas 		stats->tx512to1023octets_gb +=
299060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
2991c5aa9e3bSLendacky, Thomas 
2992c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TX1024TOMAXOCTETS_GB))
2993c5aa9e3bSLendacky, Thomas 		stats->tx1024tomaxoctets_gb +=
299460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
2995c5aa9e3bSLendacky, Thomas 
2996c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNICASTFRAMES_GB))
2997c5aa9e3bSLendacky, Thomas 		stats->txunicastframes_gb +=
299860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
2999c5aa9e3bSLendacky, Thomas 
3000c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXMULTICASTFRAMES_GB))
3001c5aa9e3bSLendacky, Thomas 		stats->txmulticastframes_gb +=
300260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
3003c5aa9e3bSLendacky, Thomas 
3004c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXBROADCASTFRAMES_GB))
3005c5aa9e3bSLendacky, Thomas 		stats->txbroadcastframes_g +=
300660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
3007c5aa9e3bSLendacky, Thomas 
3008c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXUNDERFLOWERROR))
3009c5aa9e3bSLendacky, Thomas 		stats->txunderflowerror +=
301060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
3011c5aa9e3bSLendacky, Thomas 
3012c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXOCTETCOUNT_G))
3013c5aa9e3bSLendacky, Thomas 		stats->txoctetcount_g +=
301460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
3015c5aa9e3bSLendacky, Thomas 
3016c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXFRAMECOUNT_G))
3017c5aa9e3bSLendacky, Thomas 		stats->txframecount_g +=
301860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
3019c5aa9e3bSLendacky, Thomas 
3020c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXPAUSEFRAMES))
3021c5aa9e3bSLendacky, Thomas 		stats->txpauseframes +=
302260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
3023c5aa9e3bSLendacky, Thomas 
3024c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_TISR, TXVLANFRAMES_G))
3025c5aa9e3bSLendacky, Thomas 		stats->txvlanframes_g +=
302660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
3027c5aa9e3bSLendacky, Thomas }
3028c5aa9e3bSLendacky, Thomas 
xgbe_rx_mmc_int(struct xgbe_prv_data * pdata)3029c5aa9e3bSLendacky, Thomas static void xgbe_rx_mmc_int(struct xgbe_prv_data *pdata)
3030c5aa9e3bSLendacky, Thomas {
3031c5aa9e3bSLendacky, Thomas 	struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
3032c5aa9e3bSLendacky, Thomas 	unsigned int mmc_isr = XGMAC_IOREAD(pdata, MMC_RISR);
3033c5aa9e3bSLendacky, Thomas 
3034c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFRAMECOUNT_GB))
3035c5aa9e3bSLendacky, Thomas 		stats->rxframecount_gb +=
303660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
3037c5aa9e3bSLendacky, Thomas 
3038c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_GB))
3039c5aa9e3bSLendacky, Thomas 		stats->rxoctetcount_gb +=
304060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
3041c5aa9e3bSLendacky, Thomas 
3042c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOCTETCOUNT_G))
3043c5aa9e3bSLendacky, Thomas 		stats->rxoctetcount_g +=
304460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
3045c5aa9e3bSLendacky, Thomas 
3046c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXBROADCASTFRAMES_G))
3047c5aa9e3bSLendacky, Thomas 		stats->rxbroadcastframes_g +=
304860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
3049c5aa9e3bSLendacky, Thomas 
3050c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXMULTICASTFRAMES_G))
3051c5aa9e3bSLendacky, Thomas 		stats->rxmulticastframes_g +=
305260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
3053c5aa9e3bSLendacky, Thomas 
3054c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXCRCERROR))
3055c5aa9e3bSLendacky, Thomas 		stats->rxcrcerror +=
305660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
3057c5aa9e3bSLendacky, Thomas 
3058c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXRUNTERROR))
3059c5aa9e3bSLendacky, Thomas 		stats->rxrunterror +=
306060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
3061c5aa9e3bSLendacky, Thomas 
3062c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXJABBERERROR))
3063c5aa9e3bSLendacky, Thomas 		stats->rxjabbererror +=
306460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
3065c5aa9e3bSLendacky, Thomas 
3066c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNDERSIZE_G))
3067c5aa9e3bSLendacky, Thomas 		stats->rxundersize_g +=
306860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
3069c5aa9e3bSLendacky, Thomas 
3070c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOVERSIZE_G))
3071c5aa9e3bSLendacky, Thomas 		stats->rxoversize_g +=
307260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
3073c5aa9e3bSLendacky, Thomas 
3074c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX64OCTETS_GB))
3075c5aa9e3bSLendacky, Thomas 		stats->rx64octets_gb +=
307660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
3077c5aa9e3bSLendacky, Thomas 
3078c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX65TO127OCTETS_GB))
3079c5aa9e3bSLendacky, Thomas 		stats->rx65to127octets_gb +=
308060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
3081c5aa9e3bSLendacky, Thomas 
3082c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX128TO255OCTETS_GB))
3083c5aa9e3bSLendacky, Thomas 		stats->rx128to255octets_gb +=
308460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
3085c5aa9e3bSLendacky, Thomas 
3086c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX256TO511OCTETS_GB))
3087c5aa9e3bSLendacky, Thomas 		stats->rx256to511octets_gb +=
308860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
3089c5aa9e3bSLendacky, Thomas 
3090c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX512TO1023OCTETS_GB))
3091c5aa9e3bSLendacky, Thomas 		stats->rx512to1023octets_gb +=
309260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
3093c5aa9e3bSLendacky, Thomas 
3094c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RX1024TOMAXOCTETS_GB))
3095c5aa9e3bSLendacky, Thomas 		stats->rx1024tomaxoctets_gb +=
309660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
3097c5aa9e3bSLendacky, Thomas 
3098c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXUNICASTFRAMES_G))
3099c5aa9e3bSLendacky, Thomas 		stats->rxunicastframes_g +=
310060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
3101c5aa9e3bSLendacky, Thomas 
3102c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXLENGTHERROR))
3103c5aa9e3bSLendacky, Thomas 		stats->rxlengtherror +=
310460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
3105c5aa9e3bSLendacky, Thomas 
3106c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXOUTOFRANGETYPE))
3107c5aa9e3bSLendacky, Thomas 		stats->rxoutofrangetype +=
310860265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
3109c5aa9e3bSLendacky, Thomas 
3110c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXPAUSEFRAMES))
3111c5aa9e3bSLendacky, Thomas 		stats->rxpauseframes +=
311260265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
3113c5aa9e3bSLendacky, Thomas 
3114c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXFIFOOVERFLOW))
3115c5aa9e3bSLendacky, Thomas 		stats->rxfifooverflow +=
311660265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
3117c5aa9e3bSLendacky, Thomas 
3118c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXVLANFRAMES_GB))
3119c5aa9e3bSLendacky, Thomas 		stats->rxvlanframes_gb +=
312060265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
3121c5aa9e3bSLendacky, Thomas 
3122c5aa9e3bSLendacky, Thomas 	if (XGMAC_GET_BITS(mmc_isr, MMC_RISR, RXWATCHDOGERROR))
3123c5aa9e3bSLendacky, Thomas 		stats->rxwatchdogerror +=
312460265108SLendacky, Thomas 			xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
3125c5aa9e3bSLendacky, Thomas }
3126c5aa9e3bSLendacky, Thomas 
xgbe_read_mmc_stats(struct xgbe_prv_data * pdata)3127c5aa9e3bSLendacky, Thomas static void xgbe_read_mmc_stats(struct xgbe_prv_data *pdata)
3128c5aa9e3bSLendacky, Thomas {
3129c5aa9e3bSLendacky, Thomas 	struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
3130c5aa9e3bSLendacky, Thomas 
3131c5aa9e3bSLendacky, Thomas 	/* Freeze counters */
3132c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 1);
3133c5aa9e3bSLendacky, Thomas 
3134c5aa9e3bSLendacky, Thomas 	stats->txoctetcount_gb +=
313560265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_GB_LO);
3136c5aa9e3bSLendacky, Thomas 
3137c5aa9e3bSLendacky, Thomas 	stats->txframecount_gb +=
313860265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_GB_LO);
3139c5aa9e3bSLendacky, Thomas 
3140c5aa9e3bSLendacky, Thomas 	stats->txbroadcastframes_g +=
314160265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_G_LO);
3142c5aa9e3bSLendacky, Thomas 
3143c5aa9e3bSLendacky, Thomas 	stats->txmulticastframes_g +=
314460265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_G_LO);
3145c5aa9e3bSLendacky, Thomas 
3146c5aa9e3bSLendacky, Thomas 	stats->tx64octets_gb +=
314760265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX64OCTETS_GB_LO);
3148c5aa9e3bSLendacky, Thomas 
3149c5aa9e3bSLendacky, Thomas 	stats->tx65to127octets_gb +=
315060265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX65TO127OCTETS_GB_LO);
3151c5aa9e3bSLendacky, Thomas 
3152c5aa9e3bSLendacky, Thomas 	stats->tx128to255octets_gb +=
315360265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX128TO255OCTETS_GB_LO);
3154c5aa9e3bSLendacky, Thomas 
3155c5aa9e3bSLendacky, Thomas 	stats->tx256to511octets_gb +=
315660265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX256TO511OCTETS_GB_LO);
3157c5aa9e3bSLendacky, Thomas 
3158c5aa9e3bSLendacky, Thomas 	stats->tx512to1023octets_gb +=
315960265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX512TO1023OCTETS_GB_LO);
3160c5aa9e3bSLendacky, Thomas 
3161c5aa9e3bSLendacky, Thomas 	stats->tx1024tomaxoctets_gb +=
316260265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TX1024TOMAXOCTETS_GB_LO);
3163c5aa9e3bSLendacky, Thomas 
3164c5aa9e3bSLendacky, Thomas 	stats->txunicastframes_gb +=
316560265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXUNICASTFRAMES_GB_LO);
3166c5aa9e3bSLendacky, Thomas 
3167c5aa9e3bSLendacky, Thomas 	stats->txmulticastframes_gb +=
316860265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXMULTICASTFRAMES_GB_LO);
3169c5aa9e3bSLendacky, Thomas 
3170c5aa9e3bSLendacky, Thomas 	stats->txbroadcastframes_g +=
317160265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXBROADCASTFRAMES_GB_LO);
3172c5aa9e3bSLendacky, Thomas 
3173c5aa9e3bSLendacky, Thomas 	stats->txunderflowerror +=
317460265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXUNDERFLOWERROR_LO);
3175c5aa9e3bSLendacky, Thomas 
3176c5aa9e3bSLendacky, Thomas 	stats->txoctetcount_g +=
317760265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXOCTETCOUNT_G_LO);
3178c5aa9e3bSLendacky, Thomas 
3179c5aa9e3bSLendacky, Thomas 	stats->txframecount_g +=
318060265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXFRAMECOUNT_G_LO);
3181c5aa9e3bSLendacky, Thomas 
3182c5aa9e3bSLendacky, Thomas 	stats->txpauseframes +=
318360265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXPAUSEFRAMES_LO);
3184c5aa9e3bSLendacky, Thomas 
3185c5aa9e3bSLendacky, Thomas 	stats->txvlanframes_g +=
318660265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_TXVLANFRAMES_G_LO);
3187c5aa9e3bSLendacky, Thomas 
3188c5aa9e3bSLendacky, Thomas 	stats->rxframecount_gb +=
318960265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXFRAMECOUNT_GB_LO);
3190c5aa9e3bSLendacky, Thomas 
3191c5aa9e3bSLendacky, Thomas 	stats->rxoctetcount_gb +=
319260265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_GB_LO);
3193c5aa9e3bSLendacky, Thomas 
3194c5aa9e3bSLendacky, Thomas 	stats->rxoctetcount_g +=
319560265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXOCTETCOUNT_G_LO);
3196c5aa9e3bSLendacky, Thomas 
3197c5aa9e3bSLendacky, Thomas 	stats->rxbroadcastframes_g +=
319860265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXBROADCASTFRAMES_G_LO);
3199c5aa9e3bSLendacky, Thomas 
3200c5aa9e3bSLendacky, Thomas 	stats->rxmulticastframes_g +=
320160265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXMULTICASTFRAMES_G_LO);
3202c5aa9e3bSLendacky, Thomas 
3203c5aa9e3bSLendacky, Thomas 	stats->rxcrcerror +=
320460265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXCRCERROR_LO);
3205c5aa9e3bSLendacky, Thomas 
3206c5aa9e3bSLendacky, Thomas 	stats->rxrunterror +=
320760265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXRUNTERROR);
3208c5aa9e3bSLendacky, Thomas 
3209c5aa9e3bSLendacky, Thomas 	stats->rxjabbererror +=
321060265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXJABBERERROR);
3211c5aa9e3bSLendacky, Thomas 
3212c5aa9e3bSLendacky, Thomas 	stats->rxundersize_g +=
321360265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXUNDERSIZE_G);
3214c5aa9e3bSLendacky, Thomas 
3215c5aa9e3bSLendacky, Thomas 	stats->rxoversize_g +=
321660265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXOVERSIZE_G);
3217c5aa9e3bSLendacky, Thomas 
3218c5aa9e3bSLendacky, Thomas 	stats->rx64octets_gb +=
321960265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX64OCTETS_GB_LO);
3220c5aa9e3bSLendacky, Thomas 
3221c5aa9e3bSLendacky, Thomas 	stats->rx65to127octets_gb +=
322260265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX65TO127OCTETS_GB_LO);
3223c5aa9e3bSLendacky, Thomas 
3224c5aa9e3bSLendacky, Thomas 	stats->rx128to255octets_gb +=
322560265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX128TO255OCTETS_GB_LO);
3226c5aa9e3bSLendacky, Thomas 
3227c5aa9e3bSLendacky, Thomas 	stats->rx256to511octets_gb +=
322860265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX256TO511OCTETS_GB_LO);
3229c5aa9e3bSLendacky, Thomas 
3230c5aa9e3bSLendacky, Thomas 	stats->rx512to1023octets_gb +=
323160265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX512TO1023OCTETS_GB_LO);
3232c5aa9e3bSLendacky, Thomas 
3233c5aa9e3bSLendacky, Thomas 	stats->rx1024tomaxoctets_gb +=
323460265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RX1024TOMAXOCTETS_GB_LO);
3235c5aa9e3bSLendacky, Thomas 
3236c5aa9e3bSLendacky, Thomas 	stats->rxunicastframes_g +=
323760265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXUNICASTFRAMES_G_LO);
3238c5aa9e3bSLendacky, Thomas 
3239c5aa9e3bSLendacky, Thomas 	stats->rxlengtherror +=
324060265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXLENGTHERROR_LO);
3241c5aa9e3bSLendacky, Thomas 
3242c5aa9e3bSLendacky, Thomas 	stats->rxoutofrangetype +=
324360265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXOUTOFRANGETYPE_LO);
3244c5aa9e3bSLendacky, Thomas 
3245c5aa9e3bSLendacky, Thomas 	stats->rxpauseframes +=
324660265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXPAUSEFRAMES_LO);
3247c5aa9e3bSLendacky, Thomas 
3248c5aa9e3bSLendacky, Thomas 	stats->rxfifooverflow +=
324960265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXFIFOOVERFLOW_LO);
3250c5aa9e3bSLendacky, Thomas 
3251c5aa9e3bSLendacky, Thomas 	stats->rxvlanframes_gb +=
325260265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXVLANFRAMES_GB_LO);
3253c5aa9e3bSLendacky, Thomas 
3254c5aa9e3bSLendacky, Thomas 	stats->rxwatchdogerror +=
325560265108SLendacky, Thomas 		xgbe_mmc_read(pdata, MMC_RXWATCHDOGERROR);
3256c5aa9e3bSLendacky, Thomas 
3257c5aa9e3bSLendacky, Thomas 	/* Un-freeze counters */
3258c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_CR, MCF, 0);
3259c5aa9e3bSLendacky, Thomas }
3260c5aa9e3bSLendacky, Thomas 
xgbe_config_mmc(struct xgbe_prv_data * pdata)3261c5aa9e3bSLendacky, Thomas static void xgbe_config_mmc(struct xgbe_prv_data *pdata)
3262c5aa9e3bSLendacky, Thomas {
3263c5aa9e3bSLendacky, Thomas 	/* Set counters to reset on read */
3264c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_CR, ROR, 1);
3265c5aa9e3bSLendacky, Thomas 
3266c5aa9e3bSLendacky, Thomas 	/* Reset the counters */
3267c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MMC_CR, CR, 1);
3268c5aa9e3bSLendacky, Thomas }
3269c5aa9e3bSLendacky, Thomas 
xgbe_txq_prepare_tx_stop(struct xgbe_prv_data * pdata,unsigned int queue)32704b8acdf5SLendacky, Thomas static void xgbe_txq_prepare_tx_stop(struct xgbe_prv_data *pdata,
32714b8acdf5SLendacky, Thomas 				     unsigned int queue)
32724b8acdf5SLendacky, Thomas {
32734b8acdf5SLendacky, Thomas 	unsigned int tx_status;
32744b8acdf5SLendacky, Thomas 	unsigned long tx_timeout;
32754b8acdf5SLendacky, Thomas 
32764b8acdf5SLendacky, Thomas 	/* The Tx engine cannot be stopped if it is actively processing
32774b8acdf5SLendacky, Thomas 	 * packets. Wait for the Tx queue to empty the Tx fifo.  Don't
32784b8acdf5SLendacky, Thomas 	 * wait forever though...
32794b8acdf5SLendacky, Thomas 	 */
32804b8acdf5SLendacky, Thomas 	tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
32814b8acdf5SLendacky, Thomas 	while (time_before(jiffies, tx_timeout)) {
32824b8acdf5SLendacky, Thomas 		tx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_TQDR);
32834b8acdf5SLendacky, Thomas 		if ((XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TRCSTS) != 1) &&
32844b8acdf5SLendacky, Thomas 		    (XGMAC_GET_BITS(tx_status, MTL_Q_TQDR, TXQSTS) == 0))
32854b8acdf5SLendacky, Thomas 			break;
32864b8acdf5SLendacky, Thomas 
32874b8acdf5SLendacky, Thomas 		usleep_range(500, 1000);
32884b8acdf5SLendacky, Thomas 	}
32894b8acdf5SLendacky, Thomas 
32904b8acdf5SLendacky, Thomas 	if (!time_before(jiffies, tx_timeout))
32914b8acdf5SLendacky, Thomas 		netdev_info(pdata->netdev,
32924b8acdf5SLendacky, Thomas 			    "timed out waiting for Tx queue %u to empty\n",
32934b8acdf5SLendacky, Thomas 			    queue);
32944b8acdf5SLendacky, Thomas }
32954b8acdf5SLendacky, Thomas 
xgbe_prepare_tx_stop(struct xgbe_prv_data * pdata,unsigned int queue)329616edd34eSLendacky, Thomas static void xgbe_prepare_tx_stop(struct xgbe_prv_data *pdata,
32974b8acdf5SLendacky, Thomas 				 unsigned int queue)
329816edd34eSLendacky, Thomas {
329916edd34eSLendacky, Thomas 	unsigned int tx_dsr, tx_pos, tx_qidx;
330016edd34eSLendacky, Thomas 	unsigned int tx_status;
330116edd34eSLendacky, Thomas 	unsigned long tx_timeout;
330216edd34eSLendacky, Thomas 
33034b8acdf5SLendacky, Thomas 	if (XGMAC_GET_BITS(pdata->hw_feat.version, MAC_VR, SNPSVER) > 0x20)
33044b8acdf5SLendacky, Thomas 		return xgbe_txq_prepare_tx_stop(pdata, queue);
33054b8acdf5SLendacky, Thomas 
330616edd34eSLendacky, Thomas 	/* Calculate the status register to read and the position within */
33074b8acdf5SLendacky, Thomas 	if (queue < DMA_DSRX_FIRST_QUEUE) {
330816edd34eSLendacky, Thomas 		tx_dsr = DMA_DSR0;
33094b8acdf5SLendacky, Thomas 		tx_pos = (queue * DMA_DSR_Q_WIDTH) + DMA_DSR0_TPS_START;
331016edd34eSLendacky, Thomas 	} else {
33114b8acdf5SLendacky, Thomas 		tx_qidx = queue - DMA_DSRX_FIRST_QUEUE;
331216edd34eSLendacky, Thomas 
331316edd34eSLendacky, Thomas 		tx_dsr = DMA_DSR1 + ((tx_qidx / DMA_DSRX_QPR) * DMA_DSRX_INC);
331416edd34eSLendacky, Thomas 		tx_pos = ((tx_qidx % DMA_DSRX_QPR) * DMA_DSR_Q_WIDTH) +
331516edd34eSLendacky, Thomas 			 DMA_DSRX_TPS_START;
331616edd34eSLendacky, Thomas 	}
331716edd34eSLendacky, Thomas 
331816edd34eSLendacky, Thomas 	/* The Tx engine cannot be stopped if it is actively processing
331916edd34eSLendacky, Thomas 	 * descriptors. Wait for the Tx engine to enter the stopped or
332016edd34eSLendacky, Thomas 	 * suspended state.  Don't wait forever though...
332116edd34eSLendacky, Thomas 	 */
332216edd34eSLendacky, Thomas 	tx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
332316edd34eSLendacky, Thomas 	while (time_before(jiffies, tx_timeout)) {
332416edd34eSLendacky, Thomas 		tx_status = XGMAC_IOREAD(pdata, tx_dsr);
332516edd34eSLendacky, Thomas 		tx_status = GET_BITS(tx_status, tx_pos, DMA_DSR_TPS_WIDTH);
332616edd34eSLendacky, Thomas 		if ((tx_status == DMA_TPS_STOPPED) ||
332716edd34eSLendacky, Thomas 		    (tx_status == DMA_TPS_SUSPENDED))
332816edd34eSLendacky, Thomas 			break;
332916edd34eSLendacky, Thomas 
333016edd34eSLendacky, Thomas 		usleep_range(500, 1000);
333116edd34eSLendacky, Thomas 	}
333216edd34eSLendacky, Thomas 
333316edd34eSLendacky, Thomas 	if (!time_before(jiffies, tx_timeout))
333416edd34eSLendacky, Thomas 		netdev_info(pdata->netdev,
333516edd34eSLendacky, Thomas 			    "timed out waiting for Tx DMA channel %u to stop\n",
33364b8acdf5SLendacky, Thomas 			    queue);
333716edd34eSLendacky, Thomas }
333816edd34eSLendacky, Thomas 
xgbe_enable_tx(struct xgbe_prv_data * pdata)3339c5aa9e3bSLendacky, Thomas static void xgbe_enable_tx(struct xgbe_prv_data *pdata)
3340c5aa9e3bSLendacky, Thomas {
3341c5aa9e3bSLendacky, Thomas 	unsigned int i;
3342c5aa9e3bSLendacky, Thomas 
3343c5aa9e3bSLendacky, Thomas 	/* Enable each Tx DMA channel */
334418f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
334518f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
3346c5aa9e3bSLendacky, Thomas 			break;
3347c5aa9e3bSLendacky, Thomas 
334818f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
3349c5aa9e3bSLendacky, Thomas 	}
3350c5aa9e3bSLendacky, Thomas 
3351c5aa9e3bSLendacky, Thomas 	/* Enable each Tx queue */
3352853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
3353c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN,
3354c5aa9e3bSLendacky, Thomas 				       MTL_Q_ENABLED);
3355c5aa9e3bSLendacky, Thomas 
3356c5aa9e3bSLendacky, Thomas 	/* Enable MAC Tx */
3357c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3358c5aa9e3bSLendacky, Thomas }
3359c5aa9e3bSLendacky, Thomas 
xgbe_disable_tx(struct xgbe_prv_data * pdata)3360c5aa9e3bSLendacky, Thomas static void xgbe_disable_tx(struct xgbe_prv_data *pdata)
3361c5aa9e3bSLendacky, Thomas {
3362c5aa9e3bSLendacky, Thomas 	unsigned int i;
3363c5aa9e3bSLendacky, Thomas 
336416edd34eSLendacky, Thomas 	/* Prepare for Tx DMA channel stop */
33654b8acdf5SLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
33664b8acdf5SLendacky, Thomas 		xgbe_prepare_tx_stop(pdata, i);
336716edd34eSLendacky, Thomas 
3368c5aa9e3bSLendacky, Thomas 	/* Disable MAC Tx */
3369c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3370c5aa9e3bSLendacky, Thomas 
3371c5aa9e3bSLendacky, Thomas 	/* Disable each Tx queue */
3372853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
3373c5aa9e3bSLendacky, Thomas 		XGMAC_MTL_IOWRITE_BITS(pdata, i, MTL_Q_TQOMR, TXQEN, 0);
3374c5aa9e3bSLendacky, Thomas 
3375c5aa9e3bSLendacky, Thomas 	/* Disable each Tx DMA channel */
337618f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
337718f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
3378c5aa9e3bSLendacky, Thomas 			break;
3379c5aa9e3bSLendacky, Thomas 
338018f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
3381c5aa9e3bSLendacky, Thomas 	}
3382c5aa9e3bSLendacky, Thomas }
3383c5aa9e3bSLendacky, Thomas 
xgbe_prepare_rx_stop(struct xgbe_prv_data * pdata,unsigned int queue)3384c3727d61SLendacky, Thomas static void xgbe_prepare_rx_stop(struct xgbe_prv_data *pdata,
3385c3727d61SLendacky, Thomas 				 unsigned int queue)
3386c3727d61SLendacky, Thomas {
3387c3727d61SLendacky, Thomas 	unsigned int rx_status;
3388c3727d61SLendacky, Thomas 	unsigned long rx_timeout;
3389c3727d61SLendacky, Thomas 
3390c3727d61SLendacky, Thomas 	/* The Rx engine cannot be stopped if it is actively processing
3391c3727d61SLendacky, Thomas 	 * packets. Wait for the Rx queue to empty the Rx fifo.  Don't
3392c3727d61SLendacky, Thomas 	 * wait forever though...
3393c3727d61SLendacky, Thomas 	 */
3394c3727d61SLendacky, Thomas 	rx_timeout = jiffies + (XGBE_DMA_STOP_TIMEOUT * HZ);
3395c3727d61SLendacky, Thomas 	while (time_before(jiffies, rx_timeout)) {
3396c3727d61SLendacky, Thomas 		rx_status = XGMAC_MTL_IOREAD(pdata, queue, MTL_Q_RQDR);
3397c3727d61SLendacky, Thomas 		if ((XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, PRXQ) == 0) &&
3398c3727d61SLendacky, Thomas 		    (XGMAC_GET_BITS(rx_status, MTL_Q_RQDR, RXQSTS) == 0))
3399c3727d61SLendacky, Thomas 			break;
3400c3727d61SLendacky, Thomas 
3401c3727d61SLendacky, Thomas 		usleep_range(500, 1000);
3402c3727d61SLendacky, Thomas 	}
3403c3727d61SLendacky, Thomas 
3404c3727d61SLendacky, Thomas 	if (!time_before(jiffies, rx_timeout))
3405c3727d61SLendacky, Thomas 		netdev_info(pdata->netdev,
3406c3727d61SLendacky, Thomas 			    "timed out waiting for Rx queue %u to empty\n",
3407c3727d61SLendacky, Thomas 			    queue);
3408c3727d61SLendacky, Thomas }
3409c3727d61SLendacky, Thomas 
xgbe_enable_rx(struct xgbe_prv_data * pdata)3410c5aa9e3bSLendacky, Thomas static void xgbe_enable_rx(struct xgbe_prv_data *pdata)
3411c5aa9e3bSLendacky, Thomas {
3412c5aa9e3bSLendacky, Thomas 	unsigned int reg_val, i;
3413c5aa9e3bSLendacky, Thomas 
3414c5aa9e3bSLendacky, Thomas 	/* Enable each Rx DMA channel */
341518f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
341618f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
3417c5aa9e3bSLendacky, Thomas 			break;
3418c5aa9e3bSLendacky, Thomas 
341918f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
3420c5aa9e3bSLendacky, Thomas 	}
3421c5aa9e3bSLendacky, Thomas 
3422c5aa9e3bSLendacky, Thomas 	/* Enable each Rx queue */
3423c5aa9e3bSLendacky, Thomas 	reg_val = 0;
3424853eb16bSLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
3425c5aa9e3bSLendacky, Thomas 		reg_val |= (0x02 << (i << 1));
3426c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_RQC0R, reg_val);
3427c5aa9e3bSLendacky, Thomas 
3428c5aa9e3bSLendacky, Thomas 	/* Enable MAC Rx */
3429c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 1);
3430c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 1);
3431c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 1);
3432c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 1);
3433c5aa9e3bSLendacky, Thomas }
3434c5aa9e3bSLendacky, Thomas 
xgbe_disable_rx(struct xgbe_prv_data * pdata)3435c5aa9e3bSLendacky, Thomas static void xgbe_disable_rx(struct xgbe_prv_data *pdata)
3436c5aa9e3bSLendacky, Thomas {
3437c5aa9e3bSLendacky, Thomas 	unsigned int i;
3438c5aa9e3bSLendacky, Thomas 
3439c5aa9e3bSLendacky, Thomas 	/* Disable MAC Rx */
3440c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, DCRCC, 0);
3441c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, CST, 0);
3442c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, ACS, 0);
3443c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_RCR, RE, 0);
3444c5aa9e3bSLendacky, Thomas 
3445c3727d61SLendacky, Thomas 	/* Prepare for Rx DMA channel stop */
3446c3727d61SLendacky, Thomas 	for (i = 0; i < pdata->rx_q_count; i++)
3447c3727d61SLendacky, Thomas 		xgbe_prepare_rx_stop(pdata, i);
3448c3727d61SLendacky, Thomas 
3449c5aa9e3bSLendacky, Thomas 	/* Disable each Rx queue */
3450c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE(pdata, MAC_RQC0R, 0);
3451c5aa9e3bSLendacky, Thomas 
3452c5aa9e3bSLendacky, Thomas 	/* Disable each Rx DMA channel */
345318f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
345418f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
3455c5aa9e3bSLendacky, Thomas 			break;
3456c5aa9e3bSLendacky, Thomas 
345718f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
3458c5aa9e3bSLendacky, Thomas 	}
3459c5aa9e3bSLendacky, Thomas }
3460c5aa9e3bSLendacky, Thomas 
xgbe_powerup_tx(struct xgbe_prv_data * pdata)3461c5aa9e3bSLendacky, Thomas static void xgbe_powerup_tx(struct xgbe_prv_data *pdata)
3462c5aa9e3bSLendacky, Thomas {
3463c5aa9e3bSLendacky, Thomas 	unsigned int i;
3464c5aa9e3bSLendacky, Thomas 
3465c5aa9e3bSLendacky, Thomas 	/* Enable each Tx DMA channel */
346618f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
346718f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
3468c5aa9e3bSLendacky, Thomas 			break;
3469c5aa9e3bSLendacky, Thomas 
347018f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 1);
3471c5aa9e3bSLendacky, Thomas 	}
3472c5aa9e3bSLendacky, Thomas 
3473c5aa9e3bSLendacky, Thomas 	/* Enable MAC Tx */
3474c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 1);
3475c5aa9e3bSLendacky, Thomas }
3476c5aa9e3bSLendacky, Thomas 
xgbe_powerdown_tx(struct xgbe_prv_data * pdata)3477c5aa9e3bSLendacky, Thomas static void xgbe_powerdown_tx(struct xgbe_prv_data *pdata)
3478c5aa9e3bSLendacky, Thomas {
3479c5aa9e3bSLendacky, Thomas 	unsigned int i;
3480c5aa9e3bSLendacky, Thomas 
348116edd34eSLendacky, Thomas 	/* Prepare for Tx DMA channel stop */
34824b8acdf5SLendacky, Thomas 	for (i = 0; i < pdata->tx_q_count; i++)
34834b8acdf5SLendacky, Thomas 		xgbe_prepare_tx_stop(pdata, i);
348416edd34eSLendacky, Thomas 
3485c5aa9e3bSLendacky, Thomas 	/* Disable MAC Tx */
3486c5aa9e3bSLendacky, Thomas 	XGMAC_IOWRITE_BITS(pdata, MAC_TCR, TE, 0);
3487c5aa9e3bSLendacky, Thomas 
3488c5aa9e3bSLendacky, Thomas 	/* Disable each Tx DMA channel */
348918f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
349018f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->tx_ring)
3491c5aa9e3bSLendacky, Thomas 			break;
3492c5aa9e3bSLendacky, Thomas 
349318f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_TCR, ST, 0);
3494c5aa9e3bSLendacky, Thomas 	}
3495c5aa9e3bSLendacky, Thomas }
3496c5aa9e3bSLendacky, Thomas 
xgbe_powerup_rx(struct xgbe_prv_data * pdata)3497c5aa9e3bSLendacky, Thomas static void xgbe_powerup_rx(struct xgbe_prv_data *pdata)
3498c5aa9e3bSLendacky, Thomas {
3499c5aa9e3bSLendacky, Thomas 	unsigned int i;
3500c5aa9e3bSLendacky, Thomas 
3501c5aa9e3bSLendacky, Thomas 	/* Enable each Rx DMA channel */
350218f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
350318f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
3504c5aa9e3bSLendacky, Thomas 			break;
3505c5aa9e3bSLendacky, Thomas 
350618f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 1);
3507c5aa9e3bSLendacky, Thomas 	}
3508c5aa9e3bSLendacky, Thomas }
3509c5aa9e3bSLendacky, Thomas 
xgbe_powerdown_rx(struct xgbe_prv_data * pdata)3510c5aa9e3bSLendacky, Thomas static void xgbe_powerdown_rx(struct xgbe_prv_data *pdata)
3511c5aa9e3bSLendacky, Thomas {
3512c5aa9e3bSLendacky, Thomas 	unsigned int i;
3513c5aa9e3bSLendacky, Thomas 
3514c5aa9e3bSLendacky, Thomas 	/* Disable each Rx DMA channel */
351518f9f0acSLendacky, Thomas 	for (i = 0; i < pdata->channel_count; i++) {
351618f9f0acSLendacky, Thomas 		if (!pdata->channel[i]->rx_ring)
3517c5aa9e3bSLendacky, Thomas 			break;
3518c5aa9e3bSLendacky, Thomas 
351918f9f0acSLendacky, Thomas 		XGMAC_DMA_IOWRITE_BITS(pdata->channel[i], DMA_CH_RCR, SR, 0);
3520c5aa9e3bSLendacky, Thomas 	}
3521c5aa9e3bSLendacky, Thomas }
3522c5aa9e3bSLendacky, Thomas 
xgbe_init(struct xgbe_prv_data * pdata)3523c5aa9e3bSLendacky, Thomas static int xgbe_init(struct xgbe_prv_data *pdata)
3524c5aa9e3bSLendacky, Thomas {
3525c5aa9e3bSLendacky, Thomas 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
3526c5aa9e3bSLendacky, Thomas 	int ret;
3527c5aa9e3bSLendacky, Thomas 
3528c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_init\n");
3529c5aa9e3bSLendacky, Thomas 
3530c5aa9e3bSLendacky, Thomas 	/* Flush Tx queues */
3531c5aa9e3bSLendacky, Thomas 	ret = xgbe_flush_tx_queues(pdata);
3532738f7f64SLendacky, Thomas 	if (ret) {
3533738f7f64SLendacky, Thomas 		netdev_err(pdata->netdev, "error flushing TX queues\n");
3534c5aa9e3bSLendacky, Thomas 		return ret;
3535738f7f64SLendacky, Thomas 	}
3536c5aa9e3bSLendacky, Thomas 
3537c5aa9e3bSLendacky, Thomas 	/*
3538c5aa9e3bSLendacky, Thomas 	 * Initialize DMA related features
3539c5aa9e3bSLendacky, Thomas 	 */
3540c5aa9e3bSLendacky, Thomas 	xgbe_config_dma_bus(pdata);
3541c5aa9e3bSLendacky, Thomas 	xgbe_config_dma_cache(pdata);
3542c5aa9e3bSLendacky, Thomas 	xgbe_config_osp_mode(pdata);
35437e1e6b86SLendacky, Thomas 	xgbe_config_pbl_val(pdata);
3544c5aa9e3bSLendacky, Thomas 	xgbe_config_rx_coalesce(pdata);
3545c5aa9e3bSLendacky, Thomas 	xgbe_config_tx_coalesce(pdata);
3546c5aa9e3bSLendacky, Thomas 	xgbe_config_rx_buffer_size(pdata);
3547c5aa9e3bSLendacky, Thomas 	xgbe_config_tso_mode(pdata);
3548174fd259SLendacky, Thomas 	xgbe_config_sph_mode(pdata);
35495b9dfe29SLendacky, Thomas 	xgbe_config_rss(pdata);
3550c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_tx_desc_init(pdata);
3551c5aa9e3bSLendacky, Thomas 	desc_if->wrapper_rx_desc_init(pdata);
3552c5aa9e3bSLendacky, Thomas 	xgbe_enable_dma_interrupts(pdata);
3553c5aa9e3bSLendacky, Thomas 
3554c5aa9e3bSLendacky, Thomas 	/*
3555c5aa9e3bSLendacky, Thomas 	 * Initialize MTL related features
3556c5aa9e3bSLendacky, Thomas 	 */
3557c5aa9e3bSLendacky, Thomas 	xgbe_config_mtl_mode(pdata);
3558fca2d994SLendacky, Thomas 	xgbe_config_queue_mapping(pdata);
3559c5aa9e3bSLendacky, Thomas 	xgbe_config_tsf_mode(pdata, pdata->tx_sf_mode);
3560c5aa9e3bSLendacky, Thomas 	xgbe_config_rsf_mode(pdata, pdata->rx_sf_mode);
3561c5aa9e3bSLendacky, Thomas 	xgbe_config_tx_threshold(pdata, pdata->tx_threshold);
3562c5aa9e3bSLendacky, Thomas 	xgbe_config_rx_threshold(pdata, pdata->rx_threshold);
3563c5aa9e3bSLendacky, Thomas 	xgbe_config_tx_fifo_size(pdata);
3564c5aa9e3bSLendacky, Thomas 	xgbe_config_rx_fifo_size(pdata);
3565c5aa9e3bSLendacky, Thomas 	/*TODO: Error Packet and undersized good Packet forwarding enable
3566c5aa9e3bSLendacky, Thomas 		(FEP and FUP)
3567c5aa9e3bSLendacky, Thomas 	 */
3568fca2d994SLendacky, Thomas 	xgbe_config_dcb_tc(pdata);
3569c5aa9e3bSLendacky, Thomas 	xgbe_enable_mtl_interrupts(pdata);
3570c5aa9e3bSLendacky, Thomas 
3571c5aa9e3bSLendacky, Thomas 	/*
3572c5aa9e3bSLendacky, Thomas 	 * Initialize MAC related features
3573c5aa9e3bSLendacky, Thomas 	 */
3574c5aa9e3bSLendacky, Thomas 	xgbe_config_mac_address(pdata);
3575b876382bSLendacky, Thomas 	xgbe_config_rx_mode(pdata);
3576c5aa9e3bSLendacky, Thomas 	xgbe_config_jumbo_enable(pdata);
3577c5aa9e3bSLendacky, Thomas 	xgbe_config_flow_control(pdata);
3578916102c6SLendacky, Thomas 	xgbe_config_mac_speed(pdata);
3579c5aa9e3bSLendacky, Thomas 	xgbe_config_checksum_offload(pdata);
3580c5aa9e3bSLendacky, Thomas 	xgbe_config_vlan_support(pdata);
3581c5aa9e3bSLendacky, Thomas 	xgbe_config_mmc(pdata);
3582c5aa9e3bSLendacky, Thomas 	xgbe_enable_mac_interrupts(pdata);
3583c5aa9e3bSLendacky, Thomas 
3584e78332b2SLendacky, Thomas 	/*
3585e78332b2SLendacky, Thomas 	 * Initialize ECC related features
3586e78332b2SLendacky, Thomas 	 */
3587e78332b2SLendacky, Thomas 	xgbe_enable_ecc_interrupts(pdata);
3588e78332b2SLendacky, Thomas 
3589c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_init\n");
3590c5aa9e3bSLendacky, Thomas 
3591c5aa9e3bSLendacky, Thomas 	return 0;
3592c5aa9e3bSLendacky, Thomas }
3593c5aa9e3bSLendacky, Thomas 
xgbe_init_function_ptrs_dev(struct xgbe_hw_if * hw_if)3594c5aa9e3bSLendacky, Thomas void xgbe_init_function_ptrs_dev(struct xgbe_hw_if *hw_if)
3595c5aa9e3bSLendacky, Thomas {
3596c5aa9e3bSLendacky, Thomas 	DBGPR("-->xgbe_init_function_ptrs\n");
3597c5aa9e3bSLendacky, Thomas 
3598c5aa9e3bSLendacky, Thomas 	hw_if->tx_complete = xgbe_tx_complete;
3599c5aa9e3bSLendacky, Thomas 
3600c5aa9e3bSLendacky, Thomas 	hw_if->set_mac_address = xgbe_set_mac_address;
3601b876382bSLendacky, Thomas 	hw_if->config_rx_mode = xgbe_config_rx_mode;
3602c5aa9e3bSLendacky, Thomas 
3603c5aa9e3bSLendacky, Thomas 	hw_if->enable_rx_csum = xgbe_enable_rx_csum;
3604c5aa9e3bSLendacky, Thomas 	hw_if->disable_rx_csum = xgbe_disable_rx_csum;
3605c5aa9e3bSLendacky, Thomas 
3606c5aa9e3bSLendacky, Thomas 	hw_if->enable_rx_vlan_stripping = xgbe_enable_rx_vlan_stripping;
3607c5aa9e3bSLendacky, Thomas 	hw_if->disable_rx_vlan_stripping = xgbe_disable_rx_vlan_stripping;
3608801c62d9SLendacky, Thomas 	hw_if->enable_rx_vlan_filtering = xgbe_enable_rx_vlan_filtering;
3609801c62d9SLendacky, Thomas 	hw_if->disable_rx_vlan_filtering = xgbe_disable_rx_vlan_filtering;
3610801c62d9SLendacky, Thomas 	hw_if->update_vlan_hash_table = xgbe_update_vlan_hash_table;
3611c5aa9e3bSLendacky, Thomas 
3612c5aa9e3bSLendacky, Thomas 	hw_if->read_mmd_regs = xgbe_read_mmd_regs;
3613c5aa9e3bSLendacky, Thomas 	hw_if->write_mmd_regs = xgbe_write_mmd_regs;
3614c5aa9e3bSLendacky, Thomas 
3615e57f7a3fSLendacky, Thomas 	hw_if->set_speed = xgbe_set_speed;
3616c5aa9e3bSLendacky, Thomas 
3617732f2ab7SLendacky, Thomas 	hw_if->set_ext_mii_mode = xgbe_set_ext_mii_mode;
3618070f6186SAndrew Lunn 	hw_if->read_ext_mii_regs_c22 = xgbe_read_ext_mii_regs_c22;
3619070f6186SAndrew Lunn 	hw_if->write_ext_mii_regs_c22 = xgbe_write_ext_mii_regs_c22;
3620070f6186SAndrew Lunn 	hw_if->read_ext_mii_regs_c45 = xgbe_read_ext_mii_regs_c45;
3621070f6186SAndrew Lunn 	hw_if->write_ext_mii_regs_c45 = xgbe_write_ext_mii_regs_c45;
3622732f2ab7SLendacky, Thomas 
3623732f2ab7SLendacky, Thomas 	hw_if->set_gpio = xgbe_set_gpio;
3624732f2ab7SLendacky, Thomas 	hw_if->clr_gpio = xgbe_clr_gpio;
3625732f2ab7SLendacky, Thomas 
3626c5aa9e3bSLendacky, Thomas 	hw_if->enable_tx = xgbe_enable_tx;
3627c5aa9e3bSLendacky, Thomas 	hw_if->disable_tx = xgbe_disable_tx;
3628c5aa9e3bSLendacky, Thomas 	hw_if->enable_rx = xgbe_enable_rx;
3629c5aa9e3bSLendacky, Thomas 	hw_if->disable_rx = xgbe_disable_rx;
3630c5aa9e3bSLendacky, Thomas 
3631c5aa9e3bSLendacky, Thomas 	hw_if->powerup_tx = xgbe_powerup_tx;
3632c5aa9e3bSLendacky, Thomas 	hw_if->powerdown_tx = xgbe_powerdown_tx;
3633c5aa9e3bSLendacky, Thomas 	hw_if->powerup_rx = xgbe_powerup_rx;
3634c5aa9e3bSLendacky, Thomas 	hw_if->powerdown_rx = xgbe_powerdown_rx;
3635c5aa9e3bSLendacky, Thomas 
3636a9d41981SLendacky, Thomas 	hw_if->dev_xmit = xgbe_dev_xmit;
3637c5aa9e3bSLendacky, Thomas 	hw_if->dev_read = xgbe_dev_read;
3638c5aa9e3bSLendacky, Thomas 	hw_if->enable_int = xgbe_enable_int;
3639c5aa9e3bSLendacky, Thomas 	hw_if->disable_int = xgbe_disable_int;
3640c5aa9e3bSLendacky, Thomas 	hw_if->init = xgbe_init;
3641c5aa9e3bSLendacky, Thomas 	hw_if->exit = xgbe_exit;
3642c5aa9e3bSLendacky, Thomas 
3643c5aa9e3bSLendacky, Thomas 	/* Descriptor related Sequences have to be initialized here */
3644c5aa9e3bSLendacky, Thomas 	hw_if->tx_desc_init = xgbe_tx_desc_init;
3645c5aa9e3bSLendacky, Thomas 	hw_if->rx_desc_init = xgbe_rx_desc_init;
3646c5aa9e3bSLendacky, Thomas 	hw_if->tx_desc_reset = xgbe_tx_desc_reset;
3647c5aa9e3bSLendacky, Thomas 	hw_if->rx_desc_reset = xgbe_rx_desc_reset;
3648c5aa9e3bSLendacky, Thomas 	hw_if->is_last_desc = xgbe_is_last_desc;
3649c5aa9e3bSLendacky, Thomas 	hw_if->is_context_desc = xgbe_is_context_desc;
365016958a2bSLendacky, Thomas 	hw_if->tx_start_xmit = xgbe_tx_start_xmit;
3651c5aa9e3bSLendacky, Thomas 
3652c5aa9e3bSLendacky, Thomas 	/* For FLOW ctrl */
3653c5aa9e3bSLendacky, Thomas 	hw_if->config_tx_flow_control = xgbe_config_tx_flow_control;
3654c5aa9e3bSLendacky, Thomas 	hw_if->config_rx_flow_control = xgbe_config_rx_flow_control;
3655c5aa9e3bSLendacky, Thomas 
3656c5aa9e3bSLendacky, Thomas 	/* For RX coalescing */
3657c5aa9e3bSLendacky, Thomas 	hw_if->config_rx_coalesce = xgbe_config_rx_coalesce;
3658c5aa9e3bSLendacky, Thomas 	hw_if->config_tx_coalesce = xgbe_config_tx_coalesce;
3659c5aa9e3bSLendacky, Thomas 	hw_if->usec_to_riwt = xgbe_usec_to_riwt;
3660c5aa9e3bSLendacky, Thomas 	hw_if->riwt_to_usec = xgbe_riwt_to_usec;
3661c5aa9e3bSLendacky, Thomas 
3662c5aa9e3bSLendacky, Thomas 	/* For RX and TX threshold config */
3663c5aa9e3bSLendacky, Thomas 	hw_if->config_rx_threshold = xgbe_config_rx_threshold;
3664c5aa9e3bSLendacky, Thomas 	hw_if->config_tx_threshold = xgbe_config_tx_threshold;
3665c5aa9e3bSLendacky, Thomas 
3666c5aa9e3bSLendacky, Thomas 	/* For RX and TX Store and Forward Mode config */
3667c5aa9e3bSLendacky, Thomas 	hw_if->config_rsf_mode = xgbe_config_rsf_mode;
3668c5aa9e3bSLendacky, Thomas 	hw_if->config_tsf_mode = xgbe_config_tsf_mode;
3669c5aa9e3bSLendacky, Thomas 
3670c5aa9e3bSLendacky, Thomas 	/* For TX DMA Operating on Second Frame config */
3671c5aa9e3bSLendacky, Thomas 	hw_if->config_osp_mode = xgbe_config_osp_mode;
3672c5aa9e3bSLendacky, Thomas 
3673c5aa9e3bSLendacky, Thomas 	/* For MMC statistics support */
3674c5aa9e3bSLendacky, Thomas 	hw_if->tx_mmc_int = xgbe_tx_mmc_int;
3675c5aa9e3bSLendacky, Thomas 	hw_if->rx_mmc_int = xgbe_rx_mmc_int;
3676c5aa9e3bSLendacky, Thomas 	hw_if->read_mmc_stats = xgbe_read_mmc_stats;
3677c5aa9e3bSLendacky, Thomas 
367823e4eef7SLendacky, Thomas 	/* For PTP config */
367923e4eef7SLendacky, Thomas 	hw_if->config_tstamp = xgbe_config_tstamp;
368023e4eef7SLendacky, Thomas 	hw_if->update_tstamp_addend = xgbe_update_tstamp_addend;
368123e4eef7SLendacky, Thomas 	hw_if->set_tstamp_time = xgbe_set_tstamp_time;
368223e4eef7SLendacky, Thomas 	hw_if->get_tstamp_time = xgbe_get_tstamp_time;
368323e4eef7SLendacky, Thomas 	hw_if->get_tx_tstamp = xgbe_get_tx_tstamp;
368423e4eef7SLendacky, Thomas 
3685fca2d994SLendacky, Thomas 	/* For Data Center Bridging config */
3686b3b71597SLendacky, Thomas 	hw_if->config_tc = xgbe_config_tc;
3687fca2d994SLendacky, Thomas 	hw_if->config_dcb_tc = xgbe_config_dcb_tc;
3688fca2d994SLendacky, Thomas 	hw_if->config_dcb_pfc = xgbe_config_dcb_pfc;
3689fca2d994SLendacky, Thomas 
36905b9dfe29SLendacky, Thomas 	/* For Receive Side Scaling */
36915b9dfe29SLendacky, Thomas 	hw_if->enable_rss = xgbe_enable_rss;
36925b9dfe29SLendacky, Thomas 	hw_if->disable_rss = xgbe_disable_rss;
3693f6ac8628SLendacky, Thomas 	hw_if->set_rss_hash_key = xgbe_set_rss_hash_key;
3694f6ac8628SLendacky, Thomas 	hw_if->set_rss_lookup_table = xgbe_set_rss_lookup_table;
36955b9dfe29SLendacky, Thomas 
3696e78332b2SLendacky, Thomas 	/* For ECC */
3697e78332b2SLendacky, Thomas 	hw_if->disable_ecc_ded = xgbe_disable_ecc_ded;
3698e78332b2SLendacky, Thomas 	hw_if->disable_ecc_sec = xgbe_disable_ecc_sec;
3699e78332b2SLendacky, Thomas 
37001a510ccfSLendacky, Thomas 	/* For VXLAN */
37011a510ccfSLendacky, Thomas 	hw_if->enable_vxlan = xgbe_enable_vxlan;
37021a510ccfSLendacky, Thomas 	hw_if->disable_vxlan = xgbe_disable_vxlan;
37031a510ccfSLendacky, Thomas 	hw_if->set_vxlan_id = xgbe_set_vxlan_id;
37041a510ccfSLendacky, Thomas 
3705c5aa9e3bSLendacky, Thomas 	DBGPR("<--xgbe_init_function_ptrs\n");
3706c5aa9e3bSLendacky, Thomas }
3707