xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/amd/xgbe/xgbe-drv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * AMD 10Gb Ethernet driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This file is available to you under your choice of the following two
5*4882a593Smuzhiyun  * licenses:
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * License 1: GPLv2
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * This file is free software; you may copy, redistribute and/or modify
12*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
13*4882a593Smuzhiyun  * the Free Software Foundation, either version 2 of the License, or (at
14*4882a593Smuzhiyun  * your option) any later version.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
17*4882a593Smuzhiyun  * WITHOUT ANY WARRANTY; without even the implied warranty of
18*4882a593Smuzhiyun  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19*4882a593Smuzhiyun  * General Public License for more details.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
22*4882a593Smuzhiyun  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
25*4882a593Smuzhiyun  * permission notice:
26*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29*4882a593Smuzhiyun  *     and you.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
32*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
33*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
35*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
36*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
37*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
39*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
42*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * License 2: Modified BSD
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60*4882a593Smuzhiyun  * All rights reserved.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
63*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions are met:
64*4882a593Smuzhiyun  *     * Redistributions of source code must retain the above copyright
65*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer.
66*4882a593Smuzhiyun  *     * Redistributions in binary form must reproduce the above copyright
67*4882a593Smuzhiyun  *       notice, this list of conditions and the following disclaimer in the
68*4882a593Smuzhiyun  *       documentation and/or other materials provided with the distribution.
69*4882a593Smuzhiyun  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70*4882a593Smuzhiyun  *       names of its contributors may be used to endorse or promote products
71*4882a593Smuzhiyun  *       derived from this software without specific prior written permission.
72*4882a593Smuzhiyun  *
73*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74*4882a593Smuzhiyun  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75*4882a593Smuzhiyun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76*4882a593Smuzhiyun  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77*4882a593Smuzhiyun  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78*4882a593Smuzhiyun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79*4882a593Smuzhiyun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80*4882a593Smuzhiyun  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82*4882a593Smuzhiyun  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  * This file incorporates work covered by the following copyright and
85*4882a593Smuzhiyun  * permission notice:
86*4882a593Smuzhiyun  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87*4882a593Smuzhiyun  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88*4882a593Smuzhiyun  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89*4882a593Smuzhiyun  *     and you.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  *     The Software IS NOT an item of Licensed Software or Licensed Product
92*4882a593Smuzhiyun  *     under any End User Software License Agreement or Agreement for Licensed
93*4882a593Smuzhiyun  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94*4882a593Smuzhiyun  *     granted, free of charge, to any person obtaining a copy of this software
95*4882a593Smuzhiyun  *     annotated with this license and the Software, to deal in the Software
96*4882a593Smuzhiyun  *     without restriction, including without limitation the rights to use,
97*4882a593Smuzhiyun  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98*4882a593Smuzhiyun  *     of the Software, and to permit persons to whom the Software is furnished
99*4882a593Smuzhiyun  *     to do so, subject to the following conditions:
100*4882a593Smuzhiyun  *
101*4882a593Smuzhiyun  *     The above copyright notice and this permission notice shall be included
102*4882a593Smuzhiyun  *     in all copies or substantial portions of the Software.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105*4882a593Smuzhiyun  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106*4882a593Smuzhiyun  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107*4882a593Smuzhiyun  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108*4882a593Smuzhiyun  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109*4882a593Smuzhiyun  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110*4882a593Smuzhiyun  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111*4882a593Smuzhiyun  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112*4882a593Smuzhiyun  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113*4882a593Smuzhiyun  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114*4882a593Smuzhiyun  *     THE POSSIBILITY OF SUCH DAMAGE.
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun #include <linux/module.h>
118*4882a593Smuzhiyun #include <linux/spinlock.h>
119*4882a593Smuzhiyun #include <linux/tcp.h>
120*4882a593Smuzhiyun #include <linux/if_vlan.h>
121*4882a593Smuzhiyun #include <linux/interrupt.h>
122*4882a593Smuzhiyun #include <linux/clk.h>
123*4882a593Smuzhiyun #include <linux/if_ether.h>
124*4882a593Smuzhiyun #include <linux/net_tstamp.h>
125*4882a593Smuzhiyun #include <linux/phy.h>
126*4882a593Smuzhiyun #include <net/vxlan.h>
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun #include "xgbe.h"
129*4882a593Smuzhiyun #include "xgbe-common.h"
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun static unsigned int ecc_sec_info_threshold = 10;
132*4882a593Smuzhiyun static unsigned int ecc_sec_warn_threshold = 10000;
133*4882a593Smuzhiyun static unsigned int ecc_sec_period = 600;
134*4882a593Smuzhiyun static unsigned int ecc_ded_threshold = 2;
135*4882a593Smuzhiyun static unsigned int ecc_ded_period = 600;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun #ifdef CONFIG_AMD_XGBE_HAVE_ECC
138*4882a593Smuzhiyun /* Only expose the ECC parameters if supported */
139*4882a593Smuzhiyun module_param(ecc_sec_info_threshold, uint, 0644);
140*4882a593Smuzhiyun MODULE_PARM_DESC(ecc_sec_info_threshold,
141*4882a593Smuzhiyun 		 " ECC corrected error informational threshold setting");
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun module_param(ecc_sec_warn_threshold, uint, 0644);
144*4882a593Smuzhiyun MODULE_PARM_DESC(ecc_sec_warn_threshold,
145*4882a593Smuzhiyun 		 " ECC corrected error warning threshold setting");
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun module_param(ecc_sec_period, uint, 0644);
148*4882a593Smuzhiyun MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun module_param(ecc_ded_threshold, uint, 0644);
151*4882a593Smuzhiyun MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun module_param(ecc_ded_period, uint, 0644);
154*4882a593Smuzhiyun MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
155*4882a593Smuzhiyun #endif
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun static int xgbe_one_poll(struct napi_struct *, int);
158*4882a593Smuzhiyun static int xgbe_all_poll(struct napi_struct *, int);
159*4882a593Smuzhiyun static void xgbe_stop(struct xgbe_prv_data *);
160*4882a593Smuzhiyun 
xgbe_alloc_node(size_t size,int node)161*4882a593Smuzhiyun static void *xgbe_alloc_node(size_t size, int node)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	void *mem;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	mem = kzalloc_node(size, GFP_KERNEL, node);
166*4882a593Smuzhiyun 	if (!mem)
167*4882a593Smuzhiyun 		mem = kzalloc(size, GFP_KERNEL);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return mem;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
xgbe_free_channels(struct xgbe_prv_data * pdata)172*4882a593Smuzhiyun static void xgbe_free_channels(struct xgbe_prv_data *pdata)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	unsigned int i;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(pdata->channel); i++) {
177*4882a593Smuzhiyun 		if (!pdata->channel[i])
178*4882a593Smuzhiyun 			continue;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		kfree(pdata->channel[i]->rx_ring);
181*4882a593Smuzhiyun 		kfree(pdata->channel[i]->tx_ring);
182*4882a593Smuzhiyun 		kfree(pdata->channel[i]);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		pdata->channel[i] = NULL;
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	pdata->channel_count = 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
xgbe_alloc_channels(struct xgbe_prv_data * pdata)190*4882a593Smuzhiyun static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct xgbe_channel *channel;
193*4882a593Smuzhiyun 	struct xgbe_ring *ring;
194*4882a593Smuzhiyun 	unsigned int count, i;
195*4882a593Smuzhiyun 	unsigned int cpu;
196*4882a593Smuzhiyun 	int node;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
199*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
200*4882a593Smuzhiyun 		/* Attempt to use a CPU on the node the device is on */
201*4882a593Smuzhiyun 		cpu = cpumask_local_spread(i, dev_to_node(pdata->dev));
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		/* Set the allocation node based on the returned CPU */
204*4882a593Smuzhiyun 		node = cpu_to_node(cpu);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		channel = xgbe_alloc_node(sizeof(*channel), node);
207*4882a593Smuzhiyun 		if (!channel)
208*4882a593Smuzhiyun 			goto err_mem;
209*4882a593Smuzhiyun 		pdata->channel[i] = channel;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 		snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
212*4882a593Smuzhiyun 		channel->pdata = pdata;
213*4882a593Smuzhiyun 		channel->queue_index = i;
214*4882a593Smuzhiyun 		channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
215*4882a593Smuzhiyun 				    (DMA_CH_INC * i);
216*4882a593Smuzhiyun 		channel->node = node;
217*4882a593Smuzhiyun 		cpumask_set_cpu(cpu, &channel->affinity_mask);
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 		if (pdata->per_channel_irq)
220*4882a593Smuzhiyun 			channel->dma_irq = pdata->channel_irq[i];
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 		if (i < pdata->tx_ring_count) {
223*4882a593Smuzhiyun 			ring = xgbe_alloc_node(sizeof(*ring), node);
224*4882a593Smuzhiyun 			if (!ring)
225*4882a593Smuzhiyun 				goto err_mem;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 			spin_lock_init(&ring->lock);
228*4882a593Smuzhiyun 			ring->node = node;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 			channel->tx_ring = ring;
231*4882a593Smuzhiyun 		}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		if (i < pdata->rx_ring_count) {
234*4882a593Smuzhiyun 			ring = xgbe_alloc_node(sizeof(*ring), node);
235*4882a593Smuzhiyun 			if (!ring)
236*4882a593Smuzhiyun 				goto err_mem;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 			spin_lock_init(&ring->lock);
239*4882a593Smuzhiyun 			ring->node = node;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 			channel->rx_ring = ring;
242*4882a593Smuzhiyun 		}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 		netif_dbg(pdata, drv, pdata->netdev,
245*4882a593Smuzhiyun 			  "%s: cpu=%u, node=%d\n", channel->name, cpu, node);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 		netif_dbg(pdata, drv, pdata->netdev,
248*4882a593Smuzhiyun 			  "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
249*4882a593Smuzhiyun 			  channel->name, channel->dma_regs, channel->dma_irq,
250*4882a593Smuzhiyun 			  channel->tx_ring, channel->rx_ring);
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	pdata->channel_count = count;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	return 0;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun err_mem:
258*4882a593Smuzhiyun 	xgbe_free_channels(pdata);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	return -ENOMEM;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
xgbe_tx_avail_desc(struct xgbe_ring * ring)263*4882a593Smuzhiyun static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
264*4882a593Smuzhiyun {
265*4882a593Smuzhiyun 	return (ring->rdesc_count - (ring->cur - ring->dirty));
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
xgbe_rx_dirty_desc(struct xgbe_ring * ring)268*4882a593Smuzhiyun static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	return (ring->cur - ring->dirty);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
xgbe_maybe_stop_tx_queue(struct xgbe_channel * channel,struct xgbe_ring * ring,unsigned int count)273*4882a593Smuzhiyun static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
274*4882a593Smuzhiyun 				    struct xgbe_ring *ring, unsigned int count)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	if (count > xgbe_tx_avail_desc(ring)) {
279*4882a593Smuzhiyun 		netif_info(pdata, drv, pdata->netdev,
280*4882a593Smuzhiyun 			   "Tx queue stopped, not enough descriptors available\n");
281*4882a593Smuzhiyun 		netif_stop_subqueue(pdata->netdev, channel->queue_index);
282*4882a593Smuzhiyun 		ring->tx.queue_stopped = 1;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 		/* If we haven't notified the hardware because of xmit_more
285*4882a593Smuzhiyun 		 * support, tell it now
286*4882a593Smuzhiyun 		 */
287*4882a593Smuzhiyun 		if (ring->tx.xmit_more)
288*4882a593Smuzhiyun 			pdata->hw_if.tx_start_xmit(channel, ring);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		return NETDEV_TX_BUSY;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	return 0;
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun 
xgbe_calc_rx_buf_size(struct net_device * netdev,unsigned int mtu)296*4882a593Smuzhiyun static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	unsigned int rx_buf_size;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
301*4882a593Smuzhiyun 	rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
304*4882a593Smuzhiyun 		      ~(XGBE_RX_BUF_ALIGN - 1);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	return rx_buf_size;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
xgbe_enable_rx_tx_int(struct xgbe_prv_data * pdata,struct xgbe_channel * channel)309*4882a593Smuzhiyun static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
310*4882a593Smuzhiyun 				  struct xgbe_channel *channel)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
313*4882a593Smuzhiyun 	enum xgbe_int int_id;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	if (channel->tx_ring && channel->rx_ring)
316*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
317*4882a593Smuzhiyun 	else if (channel->tx_ring)
318*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_TI;
319*4882a593Smuzhiyun 	else if (channel->rx_ring)
320*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_RI;
321*4882a593Smuzhiyun 	else
322*4882a593Smuzhiyun 		return;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	hw_if->enable_int(channel, int_id);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun 
xgbe_enable_rx_tx_ints(struct xgbe_prv_data * pdata)327*4882a593Smuzhiyun static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	unsigned int i;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++)
332*4882a593Smuzhiyun 		xgbe_enable_rx_tx_int(pdata, pdata->channel[i]);
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
xgbe_disable_rx_tx_int(struct xgbe_prv_data * pdata,struct xgbe_channel * channel)335*4882a593Smuzhiyun static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
336*4882a593Smuzhiyun 				   struct xgbe_channel *channel)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
339*4882a593Smuzhiyun 	enum xgbe_int int_id;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	if (channel->tx_ring && channel->rx_ring)
342*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
343*4882a593Smuzhiyun 	else if (channel->tx_ring)
344*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_TI;
345*4882a593Smuzhiyun 	else if (channel->rx_ring)
346*4882a593Smuzhiyun 		int_id = XGMAC_INT_DMA_CH_SR_RI;
347*4882a593Smuzhiyun 	else
348*4882a593Smuzhiyun 		return;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	hw_if->disable_int(channel, int_id);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
xgbe_disable_rx_tx_ints(struct xgbe_prv_data * pdata)353*4882a593Smuzhiyun static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun 	unsigned int i;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++)
358*4882a593Smuzhiyun 		xgbe_disable_rx_tx_int(pdata, pdata->channel[i]);
359*4882a593Smuzhiyun }
360*4882a593Smuzhiyun 
xgbe_ecc_sec(struct xgbe_prv_data * pdata,unsigned long * period,unsigned int * count,const char * area)361*4882a593Smuzhiyun static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
362*4882a593Smuzhiyun 			 unsigned int *count, const char *area)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	if (time_before(jiffies, *period)) {
365*4882a593Smuzhiyun 		(*count)++;
366*4882a593Smuzhiyun 	} else {
367*4882a593Smuzhiyun 		*period = jiffies + (ecc_sec_period * HZ);
368*4882a593Smuzhiyun 		*count = 1;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if (*count > ecc_sec_info_threshold)
372*4882a593Smuzhiyun 		dev_warn_once(pdata->dev,
373*4882a593Smuzhiyun 			      "%s ECC corrected errors exceed informational threshold\n",
374*4882a593Smuzhiyun 			      area);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (*count > ecc_sec_warn_threshold) {
377*4882a593Smuzhiyun 		dev_warn_once(pdata->dev,
378*4882a593Smuzhiyun 			      "%s ECC corrected errors exceed warning threshold\n",
379*4882a593Smuzhiyun 			      area);
380*4882a593Smuzhiyun 		return true;
381*4882a593Smuzhiyun 	}
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	return false;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
xgbe_ecc_ded(struct xgbe_prv_data * pdata,unsigned long * period,unsigned int * count,const char * area)386*4882a593Smuzhiyun static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
387*4882a593Smuzhiyun 			 unsigned int *count, const char *area)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	if (time_before(jiffies, *period)) {
390*4882a593Smuzhiyun 		(*count)++;
391*4882a593Smuzhiyun 	} else {
392*4882a593Smuzhiyun 		*period = jiffies + (ecc_ded_period * HZ);
393*4882a593Smuzhiyun 		*count = 1;
394*4882a593Smuzhiyun 	}
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (*count > ecc_ded_threshold) {
397*4882a593Smuzhiyun 		netdev_alert(pdata->netdev,
398*4882a593Smuzhiyun 			     "%s ECC detected errors exceed threshold\n",
399*4882a593Smuzhiyun 			     area);
400*4882a593Smuzhiyun 		return true;
401*4882a593Smuzhiyun 	}
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	return false;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
xgbe_ecc_isr_task(struct tasklet_struct * t)406*4882a593Smuzhiyun static void xgbe_ecc_isr_task(struct tasklet_struct *t)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = from_tasklet(pdata, t, tasklet_ecc);
409*4882a593Smuzhiyun 	unsigned int ecc_isr;
410*4882a593Smuzhiyun 	bool stop = false;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	/* Mask status with only the interrupts we care about */
413*4882a593Smuzhiyun 	ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
414*4882a593Smuzhiyun 	ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
415*4882a593Smuzhiyun 	netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
418*4882a593Smuzhiyun 		stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
419*4882a593Smuzhiyun 				     &pdata->tx_ded_count, "TX fifo");
420*4882a593Smuzhiyun 	}
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
423*4882a593Smuzhiyun 		stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
424*4882a593Smuzhiyun 				     &pdata->rx_ded_count, "RX fifo");
425*4882a593Smuzhiyun 	}
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
428*4882a593Smuzhiyun 		stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
429*4882a593Smuzhiyun 				     &pdata->desc_ded_count,
430*4882a593Smuzhiyun 				     "descriptor cache");
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	if (stop) {
434*4882a593Smuzhiyun 		pdata->hw_if.disable_ecc_ded(pdata);
435*4882a593Smuzhiyun 		schedule_work(&pdata->stopdev_work);
436*4882a593Smuzhiyun 		goto out;
437*4882a593Smuzhiyun 	}
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
440*4882a593Smuzhiyun 		if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
441*4882a593Smuzhiyun 				 &pdata->tx_sec_count, "TX fifo"))
442*4882a593Smuzhiyun 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
446*4882a593Smuzhiyun 		if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
447*4882a593Smuzhiyun 				 &pdata->rx_sec_count, "RX fifo"))
448*4882a593Smuzhiyun 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
451*4882a593Smuzhiyun 		if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
452*4882a593Smuzhiyun 				 &pdata->desc_sec_count, "descriptor cache"))
453*4882a593Smuzhiyun 			pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun out:
456*4882a593Smuzhiyun 	/* Clear all ECC interrupts */
457*4882a593Smuzhiyun 	XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	/* Reissue interrupt if status is not clear */
460*4882a593Smuzhiyun 	if (pdata->vdata->irq_reissue_support)
461*4882a593Smuzhiyun 		XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 1);
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
xgbe_ecc_isr(int irq,void * data)464*4882a593Smuzhiyun static irqreturn_t xgbe_ecc_isr(int irq, void *data)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = data;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	if (pdata->isr_as_tasklet)
469*4882a593Smuzhiyun 		tasklet_schedule(&pdata->tasklet_ecc);
470*4882a593Smuzhiyun 	else
471*4882a593Smuzhiyun 		xgbe_ecc_isr_task(&pdata->tasklet_ecc);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	return IRQ_HANDLED;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun 
xgbe_isr_task(struct tasklet_struct * t)476*4882a593Smuzhiyun static void xgbe_isr_task(struct tasklet_struct *t)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = from_tasklet(pdata, t, tasklet_dev);
479*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
480*4882a593Smuzhiyun 	struct xgbe_channel *channel;
481*4882a593Smuzhiyun 	unsigned int dma_isr, dma_ch_isr;
482*4882a593Smuzhiyun 	unsigned int mac_isr, mac_tssr, mac_mdioisr;
483*4882a593Smuzhiyun 	unsigned int i;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* The DMA interrupt status register also reports MAC and MTL
486*4882a593Smuzhiyun 	 * interrupts. So for polling mode, we just need to check for
487*4882a593Smuzhiyun 	 * this register to be non-zero
488*4882a593Smuzhiyun 	 */
489*4882a593Smuzhiyun 	dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
490*4882a593Smuzhiyun 	if (!dma_isr)
491*4882a593Smuzhiyun 		goto isr_done;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
496*4882a593Smuzhiyun 		if (!(dma_isr & (1 << i)))
497*4882a593Smuzhiyun 			continue;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 		channel = pdata->channel[i];
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 		dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
502*4882a593Smuzhiyun 		netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
503*4882a593Smuzhiyun 			  i, dma_ch_isr);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 		/* The TI or RI interrupt bits may still be set even if using
506*4882a593Smuzhiyun 		 * per channel DMA interrupts. Check to be sure those are not
507*4882a593Smuzhiyun 		 * enabled before using the private data napi structure.
508*4882a593Smuzhiyun 		 */
509*4882a593Smuzhiyun 		if (!pdata->per_channel_irq &&
510*4882a593Smuzhiyun 		    (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
511*4882a593Smuzhiyun 		     XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
512*4882a593Smuzhiyun 			if (napi_schedule_prep(&pdata->napi)) {
513*4882a593Smuzhiyun 				/* Disable Tx and Rx interrupts */
514*4882a593Smuzhiyun 				xgbe_disable_rx_tx_ints(pdata);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 				/* Turn on polling */
517*4882a593Smuzhiyun 				__napi_schedule(&pdata->napi);
518*4882a593Smuzhiyun 			}
519*4882a593Smuzhiyun 		} else {
520*4882a593Smuzhiyun 			/* Don't clear Rx/Tx status if doing per channel DMA
521*4882a593Smuzhiyun 			 * interrupts, these will be cleared by the ISR for
522*4882a593Smuzhiyun 			 * per channel DMA interrupts.
523*4882a593Smuzhiyun 			 */
524*4882a593Smuzhiyun 			XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
525*4882a593Smuzhiyun 			XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
526*4882a593Smuzhiyun 		}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
529*4882a593Smuzhiyun 			pdata->ext_stats.rx_buffer_unavailable++;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun 		/* Restart the device on a Fatal Bus Error */
532*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
533*4882a593Smuzhiyun 			schedule_work(&pdata->restart_work);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 		/* Clear interrupt signals */
536*4882a593Smuzhiyun 		XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
537*4882a593Smuzhiyun 	}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
540*4882a593Smuzhiyun 		mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 		netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
543*4882a593Smuzhiyun 			  mac_isr);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
546*4882a593Smuzhiyun 			hw_if->tx_mmc_int(pdata);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
549*4882a593Smuzhiyun 			hw_if->rx_mmc_int(pdata);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
552*4882a593Smuzhiyun 			mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 			netif_dbg(pdata, intr, pdata->netdev,
555*4882a593Smuzhiyun 				  "MAC_TSSR=%#010x\n", mac_tssr);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 			if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
558*4882a593Smuzhiyun 				/* Read Tx Timestamp to clear interrupt */
559*4882a593Smuzhiyun 				pdata->tx_tstamp =
560*4882a593Smuzhiyun 					hw_if->get_tx_tstamp(pdata);
561*4882a593Smuzhiyun 				queue_work(pdata->dev_workqueue,
562*4882a593Smuzhiyun 					   &pdata->tx_tstamp_work);
563*4882a593Smuzhiyun 			}
564*4882a593Smuzhiyun 		}
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
567*4882a593Smuzhiyun 			mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 			netif_dbg(pdata, intr, pdata->netdev,
570*4882a593Smuzhiyun 				  "MAC_MDIOISR=%#010x\n", mac_mdioisr);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 			if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
573*4882a593Smuzhiyun 					   SNGLCOMPINT))
574*4882a593Smuzhiyun 				complete(&pdata->mdio_complete);
575*4882a593Smuzhiyun 		}
576*4882a593Smuzhiyun 	}
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun isr_done:
579*4882a593Smuzhiyun 	/* If there is not a separate AN irq, handle it here */
580*4882a593Smuzhiyun 	if (pdata->dev_irq == pdata->an_irq)
581*4882a593Smuzhiyun 		pdata->phy_if.an_isr(pdata);
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	/* If there is not a separate ECC irq, handle it here */
584*4882a593Smuzhiyun 	if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
585*4882a593Smuzhiyun 		xgbe_ecc_isr_task(&pdata->tasklet_ecc);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* If there is not a separate I2C irq, handle it here */
588*4882a593Smuzhiyun 	if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
589*4882a593Smuzhiyun 		pdata->i2c_if.i2c_isr(pdata);
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	/* Reissue interrupt if status is not clear */
592*4882a593Smuzhiyun 	if (pdata->vdata->irq_reissue_support) {
593*4882a593Smuzhiyun 		unsigned int reissue_mask;
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 		reissue_mask = 1 << 0;
596*4882a593Smuzhiyun 		if (!pdata->per_channel_irq)
597*4882a593Smuzhiyun 			reissue_mask |= 0xffff << 4;
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun 		XP_IOWRITE(pdata, XP_INT_REISSUE_EN, reissue_mask);
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun 
xgbe_isr(int irq,void * data)603*4882a593Smuzhiyun static irqreturn_t xgbe_isr(int irq, void *data)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = data;
606*4882a593Smuzhiyun 
607*4882a593Smuzhiyun 	if (pdata->isr_as_tasklet)
608*4882a593Smuzhiyun 		tasklet_schedule(&pdata->tasklet_dev);
609*4882a593Smuzhiyun 	else
610*4882a593Smuzhiyun 		xgbe_isr_task(&pdata->tasklet_dev);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	return IRQ_HANDLED;
613*4882a593Smuzhiyun }
614*4882a593Smuzhiyun 
xgbe_dma_isr(int irq,void * data)615*4882a593Smuzhiyun static irqreturn_t xgbe_dma_isr(int irq, void *data)
616*4882a593Smuzhiyun {
617*4882a593Smuzhiyun 	struct xgbe_channel *channel = data;
618*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
619*4882a593Smuzhiyun 	unsigned int dma_status;
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* Per channel DMA interrupts are enabled, so we use the per
622*4882a593Smuzhiyun 	 * channel napi structure and not the private data napi structure
623*4882a593Smuzhiyun 	 */
624*4882a593Smuzhiyun 	if (napi_schedule_prep(&channel->napi)) {
625*4882a593Smuzhiyun 		/* Disable Tx and Rx interrupts */
626*4882a593Smuzhiyun 		if (pdata->channel_irq_mode)
627*4882a593Smuzhiyun 			xgbe_disable_rx_tx_int(pdata, channel);
628*4882a593Smuzhiyun 		else
629*4882a593Smuzhiyun 			disable_irq_nosync(channel->dma_irq);
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 		/* Turn on polling */
632*4882a593Smuzhiyun 		__napi_schedule_irqoff(&channel->napi);
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/* Clear Tx/Rx signals */
636*4882a593Smuzhiyun 	dma_status = 0;
637*4882a593Smuzhiyun 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
638*4882a593Smuzhiyun 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
639*4882a593Smuzhiyun 	XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	return IRQ_HANDLED;
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun 
xgbe_tx_timer(struct timer_list * t)644*4882a593Smuzhiyun static void xgbe_tx_timer(struct timer_list *t)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	struct xgbe_channel *channel = from_timer(channel, t, tx_timer);
647*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
648*4882a593Smuzhiyun 	struct napi_struct *napi;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	DBGPR("-->xgbe_tx_timer\n");
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	if (napi_schedule_prep(napi)) {
655*4882a593Smuzhiyun 		/* Disable Tx and Rx interrupts */
656*4882a593Smuzhiyun 		if (pdata->per_channel_irq)
657*4882a593Smuzhiyun 			if (pdata->channel_irq_mode)
658*4882a593Smuzhiyun 				xgbe_disable_rx_tx_int(pdata, channel);
659*4882a593Smuzhiyun 			else
660*4882a593Smuzhiyun 				disable_irq_nosync(channel->dma_irq);
661*4882a593Smuzhiyun 		else
662*4882a593Smuzhiyun 			xgbe_disable_rx_tx_ints(pdata);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 		/* Turn on polling */
665*4882a593Smuzhiyun 		__napi_schedule(napi);
666*4882a593Smuzhiyun 	}
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	channel->tx_timer_active = 0;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	DBGPR("<--xgbe_tx_timer\n");
671*4882a593Smuzhiyun }
672*4882a593Smuzhiyun 
xgbe_service(struct work_struct * work)673*4882a593Smuzhiyun static void xgbe_service(struct work_struct *work)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(work,
676*4882a593Smuzhiyun 						   struct xgbe_prv_data,
677*4882a593Smuzhiyun 						   service_work);
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	pdata->phy_if.phy_status(pdata);
680*4882a593Smuzhiyun }
681*4882a593Smuzhiyun 
xgbe_service_timer(struct timer_list * t)682*4882a593Smuzhiyun static void xgbe_service_timer(struct timer_list *t)
683*4882a593Smuzhiyun {
684*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = from_timer(pdata, t, service_timer);
685*4882a593Smuzhiyun 
686*4882a593Smuzhiyun 	queue_work(pdata->dev_workqueue, &pdata->service_work);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	mod_timer(&pdata->service_timer, jiffies + HZ);
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun 
xgbe_init_timers(struct xgbe_prv_data * pdata)691*4882a593Smuzhiyun static void xgbe_init_timers(struct xgbe_prv_data *pdata)
692*4882a593Smuzhiyun {
693*4882a593Smuzhiyun 	struct xgbe_channel *channel;
694*4882a593Smuzhiyun 	unsigned int i;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	timer_setup(&pdata->service_timer, xgbe_service_timer, 0);
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
699*4882a593Smuzhiyun 		channel = pdata->channel[i];
700*4882a593Smuzhiyun 		if (!channel->tx_ring)
701*4882a593Smuzhiyun 			break;
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 		timer_setup(&channel->tx_timer, xgbe_tx_timer, 0);
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
xgbe_start_timers(struct xgbe_prv_data * pdata)707*4882a593Smuzhiyun static void xgbe_start_timers(struct xgbe_prv_data *pdata)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun 	mod_timer(&pdata->service_timer, jiffies + HZ);
710*4882a593Smuzhiyun }
711*4882a593Smuzhiyun 
xgbe_stop_timers(struct xgbe_prv_data * pdata)712*4882a593Smuzhiyun static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
713*4882a593Smuzhiyun {
714*4882a593Smuzhiyun 	struct xgbe_channel *channel;
715*4882a593Smuzhiyun 	unsigned int i;
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	del_timer_sync(&pdata->service_timer);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
720*4882a593Smuzhiyun 		channel = pdata->channel[i];
721*4882a593Smuzhiyun 		if (!channel->tx_ring)
722*4882a593Smuzhiyun 			break;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 		/* Deactivate the Tx timer */
725*4882a593Smuzhiyun 		del_timer_sync(&channel->tx_timer);
726*4882a593Smuzhiyun 		channel->tx_timer_active = 0;
727*4882a593Smuzhiyun 	}
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
xgbe_get_all_hw_features(struct xgbe_prv_data * pdata)730*4882a593Smuzhiyun void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
733*4882a593Smuzhiyun 	struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
736*4882a593Smuzhiyun 	mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
737*4882a593Smuzhiyun 	mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	memset(hw_feat, 0, sizeof(*hw_feat));
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* Hardware feature register 0 */
744*4882a593Smuzhiyun 	hw_feat->gmii        = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
745*4882a593Smuzhiyun 	hw_feat->vlhash      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
746*4882a593Smuzhiyun 	hw_feat->sma         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
747*4882a593Smuzhiyun 	hw_feat->rwk         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
748*4882a593Smuzhiyun 	hw_feat->mgk         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
749*4882a593Smuzhiyun 	hw_feat->mmc         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
750*4882a593Smuzhiyun 	hw_feat->aoe         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
751*4882a593Smuzhiyun 	hw_feat->ts          = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
752*4882a593Smuzhiyun 	hw_feat->eee         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
753*4882a593Smuzhiyun 	hw_feat->tx_coe      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
754*4882a593Smuzhiyun 	hw_feat->rx_coe      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
755*4882a593Smuzhiyun 	hw_feat->addn_mac    = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
756*4882a593Smuzhiyun 					      ADDMACADRSEL);
757*4882a593Smuzhiyun 	hw_feat->ts_src      = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
758*4882a593Smuzhiyun 	hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
759*4882a593Smuzhiyun 	hw_feat->vxn         = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VXN);
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	/* Hardware feature register 1 */
762*4882a593Smuzhiyun 	hw_feat->rx_fifo_size  = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
763*4882a593Smuzhiyun 						RXFIFOSIZE);
764*4882a593Smuzhiyun 	hw_feat->tx_fifo_size  = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
765*4882a593Smuzhiyun 						TXFIFOSIZE);
766*4882a593Smuzhiyun 	hw_feat->adv_ts_hi     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
767*4882a593Smuzhiyun 	hw_feat->dma_width     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
768*4882a593Smuzhiyun 	hw_feat->dcb           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
769*4882a593Smuzhiyun 	hw_feat->sph           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
770*4882a593Smuzhiyun 	hw_feat->tso           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
771*4882a593Smuzhiyun 	hw_feat->dma_debug     = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
772*4882a593Smuzhiyun 	hw_feat->rss           = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
773*4882a593Smuzhiyun 	hw_feat->tc_cnt	       = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
774*4882a593Smuzhiyun 	hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
775*4882a593Smuzhiyun 						  HASHTBLSZ);
776*4882a593Smuzhiyun 	hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
777*4882a593Smuzhiyun 						  L3L4FNUM);
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 	/* Hardware feature register 2 */
780*4882a593Smuzhiyun 	hw_feat->rx_q_cnt     = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
781*4882a593Smuzhiyun 	hw_feat->tx_q_cnt     = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
782*4882a593Smuzhiyun 	hw_feat->rx_ch_cnt    = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
783*4882a593Smuzhiyun 	hw_feat->tx_ch_cnt    = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
784*4882a593Smuzhiyun 	hw_feat->pps_out_num  = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
785*4882a593Smuzhiyun 	hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	/* Translate the Hash Table size into actual number */
788*4882a593Smuzhiyun 	switch (hw_feat->hash_table_size) {
789*4882a593Smuzhiyun 	case 0:
790*4882a593Smuzhiyun 		break;
791*4882a593Smuzhiyun 	case 1:
792*4882a593Smuzhiyun 		hw_feat->hash_table_size = 64;
793*4882a593Smuzhiyun 		break;
794*4882a593Smuzhiyun 	case 2:
795*4882a593Smuzhiyun 		hw_feat->hash_table_size = 128;
796*4882a593Smuzhiyun 		break;
797*4882a593Smuzhiyun 	case 3:
798*4882a593Smuzhiyun 		hw_feat->hash_table_size = 256;
799*4882a593Smuzhiyun 		break;
800*4882a593Smuzhiyun 	}
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/* Translate the address width setting into actual number */
803*4882a593Smuzhiyun 	switch (hw_feat->dma_width) {
804*4882a593Smuzhiyun 	case 0:
805*4882a593Smuzhiyun 		hw_feat->dma_width = 32;
806*4882a593Smuzhiyun 		break;
807*4882a593Smuzhiyun 	case 1:
808*4882a593Smuzhiyun 		hw_feat->dma_width = 40;
809*4882a593Smuzhiyun 		break;
810*4882a593Smuzhiyun 	case 2:
811*4882a593Smuzhiyun 		hw_feat->dma_width = 48;
812*4882a593Smuzhiyun 		break;
813*4882a593Smuzhiyun 	default:
814*4882a593Smuzhiyun 		hw_feat->dma_width = 32;
815*4882a593Smuzhiyun 	}
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	/* The Queue, Channel and TC counts are zero based so increment them
818*4882a593Smuzhiyun 	 * to get the actual number
819*4882a593Smuzhiyun 	 */
820*4882a593Smuzhiyun 	hw_feat->rx_q_cnt++;
821*4882a593Smuzhiyun 	hw_feat->tx_q_cnt++;
822*4882a593Smuzhiyun 	hw_feat->rx_ch_cnt++;
823*4882a593Smuzhiyun 	hw_feat->tx_ch_cnt++;
824*4882a593Smuzhiyun 	hw_feat->tc_cnt++;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 	/* Translate the fifo sizes into actual numbers */
827*4882a593Smuzhiyun 	hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
828*4882a593Smuzhiyun 	hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	if (netif_msg_probe(pdata)) {
831*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "Hardware features:\n");
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun 		/* Hardware feature register 0 */
834*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  1GbE support              : %s\n",
835*4882a593Smuzhiyun 			hw_feat->gmii ? "yes" : "no");
836*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  VLAN hash filter          : %s\n",
837*4882a593Smuzhiyun 			hw_feat->vlhash ? "yes" : "no");
838*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  MDIO interface            : %s\n",
839*4882a593Smuzhiyun 			hw_feat->sma ? "yes" : "no");
840*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Wake-up packet support    : %s\n",
841*4882a593Smuzhiyun 			hw_feat->rwk ? "yes" : "no");
842*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Magic packet support      : %s\n",
843*4882a593Smuzhiyun 			hw_feat->mgk ? "yes" : "no");
844*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Management counters       : %s\n",
845*4882a593Smuzhiyun 			hw_feat->mmc ? "yes" : "no");
846*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  ARP offload               : %s\n",
847*4882a593Smuzhiyun 			hw_feat->aoe ? "yes" : "no");
848*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  IEEE 1588-2008 Timestamp  : %s\n",
849*4882a593Smuzhiyun 			hw_feat->ts ? "yes" : "no");
850*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Energy Efficient Ethernet : %s\n",
851*4882a593Smuzhiyun 			hw_feat->eee ? "yes" : "no");
852*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  TX checksum offload       : %s\n",
853*4882a593Smuzhiyun 			hw_feat->tx_coe ? "yes" : "no");
854*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  RX checksum offload       : %s\n",
855*4882a593Smuzhiyun 			hw_feat->rx_coe ? "yes" : "no");
856*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Additional MAC addresses  : %u\n",
857*4882a593Smuzhiyun 			hw_feat->addn_mac);
858*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Timestamp source          : %s\n",
859*4882a593Smuzhiyun 			(hw_feat->ts_src == 1) ? "internal" :
860*4882a593Smuzhiyun 			(hw_feat->ts_src == 2) ? "external" :
861*4882a593Smuzhiyun 			(hw_feat->ts_src == 3) ? "internal/external" : "n/a");
862*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  SA/VLAN insertion         : %s\n",
863*4882a593Smuzhiyun 			hw_feat->sa_vlan_ins ? "yes" : "no");
864*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  VXLAN/NVGRE support       : %s\n",
865*4882a593Smuzhiyun 			hw_feat->vxn ? "yes" : "no");
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 		/* Hardware feature register 1 */
868*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  RX fifo size              : %u\n",
869*4882a593Smuzhiyun 			hw_feat->rx_fifo_size);
870*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  TX fifo size              : %u\n",
871*4882a593Smuzhiyun 			hw_feat->tx_fifo_size);
872*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  IEEE 1588 high word       : %s\n",
873*4882a593Smuzhiyun 			hw_feat->adv_ts_hi ? "yes" : "no");
874*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  DMA width                 : %u\n",
875*4882a593Smuzhiyun 			hw_feat->dma_width);
876*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Data Center Bridging      : %s\n",
877*4882a593Smuzhiyun 			hw_feat->dcb ? "yes" : "no");
878*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Split header              : %s\n",
879*4882a593Smuzhiyun 			hw_feat->sph ? "yes" : "no");
880*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  TCP Segmentation Offload  : %s\n",
881*4882a593Smuzhiyun 			hw_feat->tso ? "yes" : "no");
882*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Debug memory interface    : %s\n",
883*4882a593Smuzhiyun 			hw_feat->dma_debug ? "yes" : "no");
884*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Receive Side Scaling      : %s\n",
885*4882a593Smuzhiyun 			hw_feat->rss ? "yes" : "no");
886*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Traffic Class count       : %u\n",
887*4882a593Smuzhiyun 			hw_feat->tc_cnt);
888*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Hash table size           : %u\n",
889*4882a593Smuzhiyun 			hw_feat->hash_table_size);
890*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  L3/L4 Filters             : %u\n",
891*4882a593Smuzhiyun 			hw_feat->l3l4_filter_num);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 		/* Hardware feature register 2 */
894*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  RX queue count            : %u\n",
895*4882a593Smuzhiyun 			hw_feat->rx_q_cnt);
896*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  TX queue count            : %u\n",
897*4882a593Smuzhiyun 			hw_feat->tx_q_cnt);
898*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  RX DMA channel count      : %u\n",
899*4882a593Smuzhiyun 			hw_feat->rx_ch_cnt);
900*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  TX DMA channel count      : %u\n",
901*4882a593Smuzhiyun 			hw_feat->rx_ch_cnt);
902*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  PPS outputs               : %u\n",
903*4882a593Smuzhiyun 			hw_feat->pps_out_num);
904*4882a593Smuzhiyun 		dev_dbg(pdata->dev, "  Auxiliary snapshot inputs : %u\n",
905*4882a593Smuzhiyun 			hw_feat->aux_snap_num);
906*4882a593Smuzhiyun 	}
907*4882a593Smuzhiyun }
908*4882a593Smuzhiyun 
xgbe_vxlan_set_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)909*4882a593Smuzhiyun static int xgbe_vxlan_set_port(struct net_device *netdev, unsigned int table,
910*4882a593Smuzhiyun 			       unsigned int entry, struct udp_tunnel_info *ti)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	pdata->vxlan_port = be16_to_cpu(ti->port);
915*4882a593Smuzhiyun 	pdata->hw_if.enable_vxlan(pdata);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	return 0;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun 
xgbe_vxlan_unset_port(struct net_device * netdev,unsigned int table,unsigned int entry,struct udp_tunnel_info * ti)920*4882a593Smuzhiyun static int xgbe_vxlan_unset_port(struct net_device *netdev, unsigned int table,
921*4882a593Smuzhiyun 				 unsigned int entry, struct udp_tunnel_info *ti)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 	pdata->hw_if.disable_vxlan(pdata);
926*4882a593Smuzhiyun 	pdata->vxlan_port = 0;
927*4882a593Smuzhiyun 
928*4882a593Smuzhiyun 	return 0;
929*4882a593Smuzhiyun }
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun static const struct udp_tunnel_nic_info xgbe_udp_tunnels = {
932*4882a593Smuzhiyun 	.set_port	= xgbe_vxlan_set_port,
933*4882a593Smuzhiyun 	.unset_port	= xgbe_vxlan_unset_port,
934*4882a593Smuzhiyun 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
935*4882a593Smuzhiyun 	.tables		= {
936*4882a593Smuzhiyun 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN, },
937*4882a593Smuzhiyun 	},
938*4882a593Smuzhiyun };
939*4882a593Smuzhiyun 
xgbe_get_udp_tunnel_info(void)940*4882a593Smuzhiyun const struct udp_tunnel_nic_info *xgbe_get_udp_tunnel_info(void)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun 	return &xgbe_udp_tunnels;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun 
xgbe_napi_enable(struct xgbe_prv_data * pdata,unsigned int add)945*4882a593Smuzhiyun static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun 	struct xgbe_channel *channel;
948*4882a593Smuzhiyun 	unsigned int i;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	if (pdata->per_channel_irq) {
951*4882a593Smuzhiyun 		for (i = 0; i < pdata->channel_count; i++) {
952*4882a593Smuzhiyun 			channel = pdata->channel[i];
953*4882a593Smuzhiyun 			if (add)
954*4882a593Smuzhiyun 				netif_napi_add(pdata->netdev, &channel->napi,
955*4882a593Smuzhiyun 					       xgbe_one_poll, NAPI_POLL_WEIGHT);
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 			napi_enable(&channel->napi);
958*4882a593Smuzhiyun 		}
959*4882a593Smuzhiyun 	} else {
960*4882a593Smuzhiyun 		if (add)
961*4882a593Smuzhiyun 			netif_napi_add(pdata->netdev, &pdata->napi,
962*4882a593Smuzhiyun 				       xgbe_all_poll, NAPI_POLL_WEIGHT);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 		napi_enable(&pdata->napi);
965*4882a593Smuzhiyun 	}
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun 
xgbe_napi_disable(struct xgbe_prv_data * pdata,unsigned int del)968*4882a593Smuzhiyun static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun 	struct xgbe_channel *channel;
971*4882a593Smuzhiyun 	unsigned int i;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (pdata->per_channel_irq) {
974*4882a593Smuzhiyun 		for (i = 0; i < pdata->channel_count; i++) {
975*4882a593Smuzhiyun 			channel = pdata->channel[i];
976*4882a593Smuzhiyun 			napi_disable(&channel->napi);
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 			if (del)
979*4882a593Smuzhiyun 				netif_napi_del(&channel->napi);
980*4882a593Smuzhiyun 		}
981*4882a593Smuzhiyun 	} else {
982*4882a593Smuzhiyun 		napi_disable(&pdata->napi);
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 		if (del)
985*4882a593Smuzhiyun 			netif_napi_del(&pdata->napi);
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun }
988*4882a593Smuzhiyun 
xgbe_request_irqs(struct xgbe_prv_data * pdata)989*4882a593Smuzhiyun static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	struct xgbe_channel *channel;
992*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
993*4882a593Smuzhiyun 	unsigned int i;
994*4882a593Smuzhiyun 	int ret;
995*4882a593Smuzhiyun 
996*4882a593Smuzhiyun 	tasklet_setup(&pdata->tasklet_dev, xgbe_isr_task);
997*4882a593Smuzhiyun 	tasklet_setup(&pdata->tasklet_ecc, xgbe_ecc_isr_task);
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
1000*4882a593Smuzhiyun 			       netdev_name(netdev), pdata);
1001*4882a593Smuzhiyun 	if (ret) {
1002*4882a593Smuzhiyun 		netdev_alert(netdev, "error requesting irq %d\n",
1003*4882a593Smuzhiyun 			     pdata->dev_irq);
1004*4882a593Smuzhiyun 		return ret;
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
1008*4882a593Smuzhiyun 		ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
1009*4882a593Smuzhiyun 				       0, pdata->ecc_name, pdata);
1010*4882a593Smuzhiyun 		if (ret) {
1011*4882a593Smuzhiyun 			netdev_alert(netdev, "error requesting ecc irq %d\n",
1012*4882a593Smuzhiyun 				     pdata->ecc_irq);
1013*4882a593Smuzhiyun 			goto err_dev_irq;
1014*4882a593Smuzhiyun 		}
1015*4882a593Smuzhiyun 	}
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 	if (!pdata->per_channel_irq)
1018*4882a593Smuzhiyun 		return 0;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
1021*4882a593Smuzhiyun 		channel = pdata->channel[i];
1022*4882a593Smuzhiyun 		snprintf(channel->dma_irq_name,
1023*4882a593Smuzhiyun 			 sizeof(channel->dma_irq_name) - 1,
1024*4882a593Smuzhiyun 			 "%s-TxRx-%u", netdev_name(netdev),
1025*4882a593Smuzhiyun 			 channel->queue_index);
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 		ret = devm_request_irq(pdata->dev, channel->dma_irq,
1028*4882a593Smuzhiyun 				       xgbe_dma_isr, 0,
1029*4882a593Smuzhiyun 				       channel->dma_irq_name, channel);
1030*4882a593Smuzhiyun 		if (ret) {
1031*4882a593Smuzhiyun 			netdev_alert(netdev, "error requesting irq %d\n",
1032*4882a593Smuzhiyun 				     channel->dma_irq);
1033*4882a593Smuzhiyun 			goto err_dma_irq;
1034*4882a593Smuzhiyun 		}
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun 		irq_set_affinity_hint(channel->dma_irq,
1037*4882a593Smuzhiyun 				      &channel->affinity_mask);
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	return 0;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun err_dma_irq:
1043*4882a593Smuzhiyun 	/* Using an unsigned int, 'i' will go to UINT_MAX and exit */
1044*4882a593Smuzhiyun 	for (i--; i < pdata->channel_count; i--) {
1045*4882a593Smuzhiyun 		channel = pdata->channel[i];
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 		irq_set_affinity_hint(channel->dma_irq, NULL);
1048*4882a593Smuzhiyun 		devm_free_irq(pdata->dev, channel->dma_irq, channel);
1049*4882a593Smuzhiyun 	}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
1052*4882a593Smuzhiyun 		devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun err_dev_irq:
1055*4882a593Smuzhiyun 	devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 	return ret;
1058*4882a593Smuzhiyun }
1059*4882a593Smuzhiyun 
xgbe_free_irqs(struct xgbe_prv_data * pdata)1060*4882a593Smuzhiyun static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
1061*4882a593Smuzhiyun {
1062*4882a593Smuzhiyun 	struct xgbe_channel *channel;
1063*4882a593Smuzhiyun 	unsigned int i;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
1068*4882a593Smuzhiyun 		devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	if (!pdata->per_channel_irq)
1071*4882a593Smuzhiyun 		return;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
1074*4882a593Smuzhiyun 		channel = pdata->channel[i];
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 		irq_set_affinity_hint(channel->dma_irq, NULL);
1077*4882a593Smuzhiyun 		devm_free_irq(pdata->dev, channel->dma_irq, channel);
1078*4882a593Smuzhiyun 	}
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun 
xgbe_init_tx_coalesce(struct xgbe_prv_data * pdata)1081*4882a593Smuzhiyun void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
1082*4882a593Smuzhiyun {
1083*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1084*4882a593Smuzhiyun 
1085*4882a593Smuzhiyun 	DBGPR("-->xgbe_init_tx_coalesce\n");
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
1088*4882a593Smuzhiyun 	pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	hw_if->config_tx_coalesce(pdata);
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	DBGPR("<--xgbe_init_tx_coalesce\n");
1093*4882a593Smuzhiyun }
1094*4882a593Smuzhiyun 
xgbe_init_rx_coalesce(struct xgbe_prv_data * pdata)1095*4882a593Smuzhiyun void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
1096*4882a593Smuzhiyun {
1097*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	DBGPR("-->xgbe_init_rx_coalesce\n");
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
1102*4882a593Smuzhiyun 	pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
1103*4882a593Smuzhiyun 	pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	hw_if->config_rx_coalesce(pdata);
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	DBGPR("<--xgbe_init_rx_coalesce\n");
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun 
xgbe_free_tx_data(struct xgbe_prv_data * pdata)1110*4882a593Smuzhiyun static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
1111*4882a593Smuzhiyun {
1112*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1113*4882a593Smuzhiyun 	struct xgbe_ring *ring;
1114*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
1115*4882a593Smuzhiyun 	unsigned int i, j;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	DBGPR("-->xgbe_free_tx_data\n");
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
1120*4882a593Smuzhiyun 		ring = pdata->channel[i]->tx_ring;
1121*4882a593Smuzhiyun 		if (!ring)
1122*4882a593Smuzhiyun 			break;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 		for (j = 0; j < ring->rdesc_count; j++) {
1125*4882a593Smuzhiyun 			rdata = XGBE_GET_DESC_DATA(ring, j);
1126*4882a593Smuzhiyun 			desc_if->unmap_rdata(pdata, rdata);
1127*4882a593Smuzhiyun 		}
1128*4882a593Smuzhiyun 	}
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	DBGPR("<--xgbe_free_tx_data\n");
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun 
xgbe_free_rx_data(struct xgbe_prv_data * pdata)1133*4882a593Smuzhiyun static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1136*4882a593Smuzhiyun 	struct xgbe_ring *ring;
1137*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
1138*4882a593Smuzhiyun 	unsigned int i, j;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	DBGPR("-->xgbe_free_rx_data\n");
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
1143*4882a593Smuzhiyun 		ring = pdata->channel[i]->rx_ring;
1144*4882a593Smuzhiyun 		if (!ring)
1145*4882a593Smuzhiyun 			break;
1146*4882a593Smuzhiyun 
1147*4882a593Smuzhiyun 		for (j = 0; j < ring->rdesc_count; j++) {
1148*4882a593Smuzhiyun 			rdata = XGBE_GET_DESC_DATA(ring, j);
1149*4882a593Smuzhiyun 			desc_if->unmap_rdata(pdata, rdata);
1150*4882a593Smuzhiyun 		}
1151*4882a593Smuzhiyun 	}
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun 	DBGPR("<--xgbe_free_rx_data\n");
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun 
xgbe_phy_reset(struct xgbe_prv_data * pdata)1156*4882a593Smuzhiyun static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun 	pdata->phy_link = -1;
1159*4882a593Smuzhiyun 	pdata->phy_speed = SPEED_UNKNOWN;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	return pdata->phy_if.phy_reset(pdata);
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun 
xgbe_powerdown(struct net_device * netdev,unsigned int caller)1164*4882a593Smuzhiyun int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
1165*4882a593Smuzhiyun {
1166*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1167*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1168*4882a593Smuzhiyun 	unsigned long flags;
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 	DBGPR("-->xgbe_powerdown\n");
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	if (!netif_running(netdev) ||
1173*4882a593Smuzhiyun 	    (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
1174*4882a593Smuzhiyun 		netdev_alert(netdev, "Device is already powered down\n");
1175*4882a593Smuzhiyun 		DBGPR("<--xgbe_powerdown\n");
1176*4882a593Smuzhiyun 		return -EINVAL;
1177*4882a593Smuzhiyun 	}
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->lock, flags);
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	if (caller == XGMAC_DRIVER_CONTEXT)
1182*4882a593Smuzhiyun 		netif_device_detach(netdev);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	netif_tx_stop_all_queues(netdev);
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	xgbe_stop_timers(pdata);
1187*4882a593Smuzhiyun 	flush_workqueue(pdata->dev_workqueue);
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	hw_if->powerdown_tx(pdata);
1190*4882a593Smuzhiyun 	hw_if->powerdown_rx(pdata);
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	xgbe_napi_disable(pdata, 0);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	pdata->power_down = 1;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->lock, flags);
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	DBGPR("<--xgbe_powerdown\n");
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	return 0;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
xgbe_powerup(struct net_device * netdev,unsigned int caller)1203*4882a593Smuzhiyun int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1204*4882a593Smuzhiyun {
1205*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1206*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1207*4882a593Smuzhiyun 	unsigned long flags;
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	DBGPR("-->xgbe_powerup\n");
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 	if (!netif_running(netdev) ||
1212*4882a593Smuzhiyun 	    (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1213*4882a593Smuzhiyun 		netdev_alert(netdev, "Device is already powered up\n");
1214*4882a593Smuzhiyun 		DBGPR("<--xgbe_powerup\n");
1215*4882a593Smuzhiyun 		return -EINVAL;
1216*4882a593Smuzhiyun 	}
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->lock, flags);
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	pdata->power_down = 0;
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 	xgbe_napi_enable(pdata, 0);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	hw_if->powerup_tx(pdata);
1225*4882a593Smuzhiyun 	hw_if->powerup_rx(pdata);
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun 	if (caller == XGMAC_DRIVER_CONTEXT)
1228*4882a593Smuzhiyun 		netif_device_attach(netdev);
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	netif_tx_start_all_queues(netdev);
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	xgbe_start_timers(pdata);
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->lock, flags);
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	DBGPR("<--xgbe_powerup\n");
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 	return 0;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun 
xgbe_free_memory(struct xgbe_prv_data * pdata)1241*4882a593Smuzhiyun static void xgbe_free_memory(struct xgbe_prv_data *pdata)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	/* Free the ring descriptors and buffers */
1246*4882a593Smuzhiyun 	desc_if->free_ring_resources(pdata);
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	/* Free the channel and ring structures */
1249*4882a593Smuzhiyun 	xgbe_free_channels(pdata);
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun 
xgbe_alloc_memory(struct xgbe_prv_data * pdata)1252*4882a593Smuzhiyun static int xgbe_alloc_memory(struct xgbe_prv_data *pdata)
1253*4882a593Smuzhiyun {
1254*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1255*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
1256*4882a593Smuzhiyun 	int ret;
1257*4882a593Smuzhiyun 
1258*4882a593Smuzhiyun 	if (pdata->new_tx_ring_count) {
1259*4882a593Smuzhiyun 		pdata->tx_ring_count = pdata->new_tx_ring_count;
1260*4882a593Smuzhiyun 		pdata->tx_q_count = pdata->tx_ring_count;
1261*4882a593Smuzhiyun 		pdata->new_tx_ring_count = 0;
1262*4882a593Smuzhiyun 	}
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	if (pdata->new_rx_ring_count) {
1265*4882a593Smuzhiyun 		pdata->rx_ring_count = pdata->new_rx_ring_count;
1266*4882a593Smuzhiyun 		pdata->new_rx_ring_count = 0;
1267*4882a593Smuzhiyun 	}
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	/* Calculate the Rx buffer size before allocating rings */
1270*4882a593Smuzhiyun 	pdata->rx_buf_size = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	/* Allocate the channel and ring structures */
1273*4882a593Smuzhiyun 	ret = xgbe_alloc_channels(pdata);
1274*4882a593Smuzhiyun 	if (ret)
1275*4882a593Smuzhiyun 		return ret;
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	/* Allocate the ring descriptors and buffers */
1278*4882a593Smuzhiyun 	ret = desc_if->alloc_ring_resources(pdata);
1279*4882a593Smuzhiyun 	if (ret)
1280*4882a593Smuzhiyun 		goto err_channels;
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	/* Initialize the service and Tx timers */
1283*4882a593Smuzhiyun 	xgbe_init_timers(pdata);
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun 	return 0;
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun err_channels:
1288*4882a593Smuzhiyun 	xgbe_free_memory(pdata);
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	return ret;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
xgbe_start(struct xgbe_prv_data * pdata)1293*4882a593Smuzhiyun static int xgbe_start(struct xgbe_prv_data *pdata)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1296*4882a593Smuzhiyun 	struct xgbe_phy_if *phy_if = &pdata->phy_if;
1297*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
1298*4882a593Smuzhiyun 	unsigned int i;
1299*4882a593Smuzhiyun 	int ret;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	/* Set the number of queues */
1302*4882a593Smuzhiyun 	ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
1303*4882a593Smuzhiyun 	if (ret) {
1304*4882a593Smuzhiyun 		netdev_err(netdev, "error setting real tx queue count\n");
1305*4882a593Smuzhiyun 		return ret;
1306*4882a593Smuzhiyun 	}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
1309*4882a593Smuzhiyun 	if (ret) {
1310*4882a593Smuzhiyun 		netdev_err(netdev, "error setting real rx queue count\n");
1311*4882a593Smuzhiyun 		return ret;
1312*4882a593Smuzhiyun 	}
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	/* Set RSS lookup table data for programming */
1315*4882a593Smuzhiyun 	for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
1316*4882a593Smuzhiyun 		XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
1317*4882a593Smuzhiyun 			       i % pdata->rx_ring_count);
1318*4882a593Smuzhiyun 
1319*4882a593Smuzhiyun 	ret = hw_if->init(pdata);
1320*4882a593Smuzhiyun 	if (ret)
1321*4882a593Smuzhiyun 		return ret;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	xgbe_napi_enable(pdata, 1);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	ret = xgbe_request_irqs(pdata);
1326*4882a593Smuzhiyun 	if (ret)
1327*4882a593Smuzhiyun 		goto err_napi;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	ret = phy_if->phy_start(pdata);
1330*4882a593Smuzhiyun 	if (ret)
1331*4882a593Smuzhiyun 		goto err_irqs;
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 	hw_if->enable_tx(pdata);
1334*4882a593Smuzhiyun 	hw_if->enable_rx(pdata);
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	udp_tunnel_nic_reset_ntf(netdev);
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	netif_tx_start_all_queues(netdev);
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	xgbe_start_timers(pdata);
1341*4882a593Smuzhiyun 	queue_work(pdata->dev_workqueue, &pdata->service_work);
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	clear_bit(XGBE_STOPPED, &pdata->dev_state);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	return 0;
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun err_irqs:
1348*4882a593Smuzhiyun 	xgbe_free_irqs(pdata);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun err_napi:
1351*4882a593Smuzhiyun 	xgbe_napi_disable(pdata, 1);
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	hw_if->exit(pdata);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	return ret;
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun 
xgbe_stop(struct xgbe_prv_data * pdata)1358*4882a593Smuzhiyun static void xgbe_stop(struct xgbe_prv_data *pdata)
1359*4882a593Smuzhiyun {
1360*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1361*4882a593Smuzhiyun 	struct xgbe_phy_if *phy_if = &pdata->phy_if;
1362*4882a593Smuzhiyun 	struct xgbe_channel *channel;
1363*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
1364*4882a593Smuzhiyun 	struct netdev_queue *txq;
1365*4882a593Smuzhiyun 	unsigned int i;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	DBGPR("-->xgbe_stop\n");
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1370*4882a593Smuzhiyun 		return;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	netif_tx_stop_all_queues(netdev);
1373*4882a593Smuzhiyun 	netif_carrier_off(pdata->netdev);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	xgbe_stop_timers(pdata);
1376*4882a593Smuzhiyun 	flush_workqueue(pdata->dev_workqueue);
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun 	xgbe_vxlan_unset_port(netdev, 0, 0, NULL);
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	hw_if->disable_tx(pdata);
1381*4882a593Smuzhiyun 	hw_if->disable_rx(pdata);
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	phy_if->phy_stop(pdata);
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	xgbe_free_irqs(pdata);
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	xgbe_napi_disable(pdata, 1);
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	hw_if->exit(pdata);
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	for (i = 0; i < pdata->channel_count; i++) {
1392*4882a593Smuzhiyun 		channel = pdata->channel[i];
1393*4882a593Smuzhiyun 		if (!channel->tx_ring)
1394*4882a593Smuzhiyun 			continue;
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 		txq = netdev_get_tx_queue(netdev, channel->queue_index);
1397*4882a593Smuzhiyun 		netdev_tx_reset_queue(txq);
1398*4882a593Smuzhiyun 	}
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	set_bit(XGBE_STOPPED, &pdata->dev_state);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	DBGPR("<--xgbe_stop\n");
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun 
xgbe_stopdev(struct work_struct * work)1405*4882a593Smuzhiyun static void xgbe_stopdev(struct work_struct *work)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(work,
1408*4882a593Smuzhiyun 						   struct xgbe_prv_data,
1409*4882a593Smuzhiyun 						   stopdev_work);
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	rtnl_lock();
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	xgbe_stop(pdata);
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	xgbe_free_tx_data(pdata);
1416*4882a593Smuzhiyun 	xgbe_free_rx_data(pdata);
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	rtnl_unlock();
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	netdev_alert(pdata->netdev, "device stopped\n");
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun 
xgbe_full_restart_dev(struct xgbe_prv_data * pdata)1423*4882a593Smuzhiyun void xgbe_full_restart_dev(struct xgbe_prv_data *pdata)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	/* If not running, "restart" will happen on open */
1426*4882a593Smuzhiyun 	if (!netif_running(pdata->netdev))
1427*4882a593Smuzhiyun 		return;
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	xgbe_stop(pdata);
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	xgbe_free_memory(pdata);
1432*4882a593Smuzhiyun 	xgbe_alloc_memory(pdata);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	xgbe_start(pdata);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
xgbe_restart_dev(struct xgbe_prv_data * pdata)1437*4882a593Smuzhiyun void xgbe_restart_dev(struct xgbe_prv_data *pdata)
1438*4882a593Smuzhiyun {
1439*4882a593Smuzhiyun 	/* If not running, "restart" will happen on open */
1440*4882a593Smuzhiyun 	if (!netif_running(pdata->netdev))
1441*4882a593Smuzhiyun 		return;
1442*4882a593Smuzhiyun 
1443*4882a593Smuzhiyun 	xgbe_stop(pdata);
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	xgbe_free_tx_data(pdata);
1446*4882a593Smuzhiyun 	xgbe_free_rx_data(pdata);
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	xgbe_start(pdata);
1449*4882a593Smuzhiyun }
1450*4882a593Smuzhiyun 
xgbe_restart(struct work_struct * work)1451*4882a593Smuzhiyun static void xgbe_restart(struct work_struct *work)
1452*4882a593Smuzhiyun {
1453*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(work,
1454*4882a593Smuzhiyun 						   struct xgbe_prv_data,
1455*4882a593Smuzhiyun 						   restart_work);
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	rtnl_lock();
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	xgbe_restart_dev(pdata);
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 	rtnl_unlock();
1462*4882a593Smuzhiyun }
1463*4882a593Smuzhiyun 
xgbe_tx_tstamp(struct work_struct * work)1464*4882a593Smuzhiyun static void xgbe_tx_tstamp(struct work_struct *work)
1465*4882a593Smuzhiyun {
1466*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(work,
1467*4882a593Smuzhiyun 						   struct xgbe_prv_data,
1468*4882a593Smuzhiyun 						   tx_tstamp_work);
1469*4882a593Smuzhiyun 	struct skb_shared_hwtstamps hwtstamps;
1470*4882a593Smuzhiyun 	u64 nsec;
1471*4882a593Smuzhiyun 	unsigned long flags;
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun 	spin_lock_irqsave(&pdata->tstamp_lock, flags);
1474*4882a593Smuzhiyun 	if (!pdata->tx_tstamp_skb)
1475*4882a593Smuzhiyun 		goto unlock;
1476*4882a593Smuzhiyun 
1477*4882a593Smuzhiyun 	if (pdata->tx_tstamp) {
1478*4882a593Smuzhiyun 		nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1479*4882a593Smuzhiyun 					    pdata->tx_tstamp);
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 		memset(&hwtstamps, 0, sizeof(hwtstamps));
1482*4882a593Smuzhiyun 		hwtstamps.hwtstamp = ns_to_ktime(nsec);
1483*4882a593Smuzhiyun 		skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1484*4882a593Smuzhiyun 	}
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	dev_kfree_skb_any(pdata->tx_tstamp_skb);
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	pdata->tx_tstamp_skb = NULL;
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun unlock:
1491*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1492*4882a593Smuzhiyun }
1493*4882a593Smuzhiyun 
xgbe_get_hwtstamp_settings(struct xgbe_prv_data * pdata,struct ifreq * ifreq)1494*4882a593Smuzhiyun static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1495*4882a593Smuzhiyun 				      struct ifreq *ifreq)
1496*4882a593Smuzhiyun {
1497*4882a593Smuzhiyun 	if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1498*4882a593Smuzhiyun 			 sizeof(pdata->tstamp_config)))
1499*4882a593Smuzhiyun 		return -EFAULT;
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	return 0;
1502*4882a593Smuzhiyun }
1503*4882a593Smuzhiyun 
xgbe_set_hwtstamp_settings(struct xgbe_prv_data * pdata,struct ifreq * ifreq)1504*4882a593Smuzhiyun static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1505*4882a593Smuzhiyun 				      struct ifreq *ifreq)
1506*4882a593Smuzhiyun {
1507*4882a593Smuzhiyun 	struct hwtstamp_config config;
1508*4882a593Smuzhiyun 	unsigned int mac_tscr;
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1511*4882a593Smuzhiyun 		return -EFAULT;
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	if (config.flags)
1514*4882a593Smuzhiyun 		return -EINVAL;
1515*4882a593Smuzhiyun 
1516*4882a593Smuzhiyun 	mac_tscr = 0;
1517*4882a593Smuzhiyun 
1518*4882a593Smuzhiyun 	switch (config.tx_type) {
1519*4882a593Smuzhiyun 	case HWTSTAMP_TX_OFF:
1520*4882a593Smuzhiyun 		break;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun 	case HWTSTAMP_TX_ON:
1523*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1524*4882a593Smuzhiyun 		break;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	default:
1527*4882a593Smuzhiyun 		return -ERANGE;
1528*4882a593Smuzhiyun 	}
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	switch (config.rx_filter) {
1531*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_NONE:
1532*4882a593Smuzhiyun 		break;
1533*4882a593Smuzhiyun 
1534*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_NTP_ALL:
1535*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_ALL:
1536*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1537*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1538*4882a593Smuzhiyun 		break;
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 	/* PTP v2, UDP, any kind of event packet */
1541*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1542*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1543*4882a593Smuzhiyun 		fallthrough;	/* to PTP v1, UDP, any kind of event packet */
1544*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1545*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1546*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1547*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1548*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1549*4882a593Smuzhiyun 		break;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	/* PTP v2, UDP, Sync packet */
1552*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1553*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1554*4882a593Smuzhiyun 		fallthrough;	/* to PTP v1, UDP, Sync packet */
1555*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1556*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1557*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1558*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1559*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1560*4882a593Smuzhiyun 		break;
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	/* PTP v2, UDP, Delay_req packet */
1563*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1564*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1565*4882a593Smuzhiyun 		fallthrough;	/* to PTP v1, UDP, Delay_req packet */
1566*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1567*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1568*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1569*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1570*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1571*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1572*4882a593Smuzhiyun 		break;
1573*4882a593Smuzhiyun 
1574*4882a593Smuzhiyun 	/* 802.AS1, Ethernet, any kind of event packet */
1575*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1576*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1577*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1578*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1579*4882a593Smuzhiyun 		break;
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun 	/* 802.AS1, Ethernet, Sync packet */
1582*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1583*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1584*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1585*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1586*4882a593Smuzhiyun 		break;
1587*4882a593Smuzhiyun 
1588*4882a593Smuzhiyun 	/* 802.AS1, Ethernet, Delay_req packet */
1589*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1590*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1591*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1592*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1593*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1594*4882a593Smuzhiyun 		break;
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	/* PTP v2/802.AS1, any layer, any kind of event packet */
1597*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1598*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1599*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1600*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1601*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1602*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1603*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1604*4882a593Smuzhiyun 		break;
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	/* PTP v2/802.AS1, any layer, Sync packet */
1607*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1608*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1609*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1610*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1611*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1612*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1613*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1614*4882a593Smuzhiyun 		break;
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	/* PTP v2/802.AS1, any layer, Delay_req packet */
1617*4882a593Smuzhiyun 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1618*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1619*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1620*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1621*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1622*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1623*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1624*4882a593Smuzhiyun 		XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1625*4882a593Smuzhiyun 		break;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 	default:
1628*4882a593Smuzhiyun 		return -ERANGE;
1629*4882a593Smuzhiyun 	}
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 	pdata->hw_if.config_tstamp(pdata, mac_tscr);
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	memcpy(&pdata->tstamp_config, &config, sizeof(config));
1634*4882a593Smuzhiyun 
1635*4882a593Smuzhiyun 	return 0;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun 
xgbe_prep_tx_tstamp(struct xgbe_prv_data * pdata,struct sk_buff * skb,struct xgbe_packet_data * packet)1638*4882a593Smuzhiyun static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1639*4882a593Smuzhiyun 				struct sk_buff *skb,
1640*4882a593Smuzhiyun 				struct xgbe_packet_data *packet)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	unsigned long flags;
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun 	if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1645*4882a593Smuzhiyun 		spin_lock_irqsave(&pdata->tstamp_lock, flags);
1646*4882a593Smuzhiyun 		if (pdata->tx_tstamp_skb) {
1647*4882a593Smuzhiyun 			/* Another timestamp in progress, ignore this one */
1648*4882a593Smuzhiyun 			XGMAC_SET_BITS(packet->attributes,
1649*4882a593Smuzhiyun 				       TX_PACKET_ATTRIBUTES, PTP, 0);
1650*4882a593Smuzhiyun 		} else {
1651*4882a593Smuzhiyun 			pdata->tx_tstamp_skb = skb_get(skb);
1652*4882a593Smuzhiyun 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1653*4882a593Smuzhiyun 		}
1654*4882a593Smuzhiyun 		spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1655*4882a593Smuzhiyun 	}
1656*4882a593Smuzhiyun 
1657*4882a593Smuzhiyun 	skb_tx_timestamp(skb);
1658*4882a593Smuzhiyun }
1659*4882a593Smuzhiyun 
xgbe_prep_vlan(struct sk_buff * skb,struct xgbe_packet_data * packet)1660*4882a593Smuzhiyun static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1661*4882a593Smuzhiyun {
1662*4882a593Smuzhiyun 	if (skb_vlan_tag_present(skb))
1663*4882a593Smuzhiyun 		packet->vlan_ctag = skb_vlan_tag_get(skb);
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun 
xgbe_prep_tso(struct sk_buff * skb,struct xgbe_packet_data * packet)1666*4882a593Smuzhiyun static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1667*4882a593Smuzhiyun {
1668*4882a593Smuzhiyun 	int ret;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1671*4882a593Smuzhiyun 			    TSO_ENABLE))
1672*4882a593Smuzhiyun 		return 0;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	ret = skb_cow_head(skb, 0);
1675*4882a593Smuzhiyun 	if (ret)
1676*4882a593Smuzhiyun 		return ret;
1677*4882a593Smuzhiyun 
1678*4882a593Smuzhiyun 	if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, VXLAN)) {
1679*4882a593Smuzhiyun 		packet->header_len = skb_inner_transport_offset(skb) +
1680*4882a593Smuzhiyun 				     inner_tcp_hdrlen(skb);
1681*4882a593Smuzhiyun 		packet->tcp_header_len = inner_tcp_hdrlen(skb);
1682*4882a593Smuzhiyun 	} else {
1683*4882a593Smuzhiyun 		packet->header_len = skb_transport_offset(skb) +
1684*4882a593Smuzhiyun 				     tcp_hdrlen(skb);
1685*4882a593Smuzhiyun 		packet->tcp_header_len = tcp_hdrlen(skb);
1686*4882a593Smuzhiyun 	}
1687*4882a593Smuzhiyun 	packet->tcp_payload_len = skb->len - packet->header_len;
1688*4882a593Smuzhiyun 	packet->mss = skb_shinfo(skb)->gso_size;
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	DBGPR("  packet->header_len=%u\n", packet->header_len);
1691*4882a593Smuzhiyun 	DBGPR("  packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1692*4882a593Smuzhiyun 	      packet->tcp_header_len, packet->tcp_payload_len);
1693*4882a593Smuzhiyun 	DBGPR("  packet->mss=%u\n", packet->mss);
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	/* Update the number of packets that will ultimately be transmitted
1696*4882a593Smuzhiyun 	 * along with the extra bytes for each extra packet
1697*4882a593Smuzhiyun 	 */
1698*4882a593Smuzhiyun 	packet->tx_packets = skb_shinfo(skb)->gso_segs;
1699*4882a593Smuzhiyun 	packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun 	return 0;
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun 
xgbe_is_vxlan(struct sk_buff * skb)1704*4882a593Smuzhiyun static bool xgbe_is_vxlan(struct sk_buff *skb)
1705*4882a593Smuzhiyun {
1706*4882a593Smuzhiyun 	if (!skb->encapsulation)
1707*4882a593Smuzhiyun 		return false;
1708*4882a593Smuzhiyun 
1709*4882a593Smuzhiyun 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1710*4882a593Smuzhiyun 		return false;
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	switch (skb->protocol) {
1713*4882a593Smuzhiyun 	case htons(ETH_P_IP):
1714*4882a593Smuzhiyun 		if (ip_hdr(skb)->protocol != IPPROTO_UDP)
1715*4882a593Smuzhiyun 			return false;
1716*4882a593Smuzhiyun 		break;
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	case htons(ETH_P_IPV6):
1719*4882a593Smuzhiyun 		if (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP)
1720*4882a593Smuzhiyun 			return false;
1721*4882a593Smuzhiyun 		break;
1722*4882a593Smuzhiyun 
1723*4882a593Smuzhiyun 	default:
1724*4882a593Smuzhiyun 		return false;
1725*4882a593Smuzhiyun 	}
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun 	if (skb->inner_protocol_type != ENCAP_TYPE_ETHER ||
1728*4882a593Smuzhiyun 	    skb->inner_protocol != htons(ETH_P_TEB) ||
1729*4882a593Smuzhiyun 	    (skb_inner_mac_header(skb) - skb_transport_header(skb) !=
1730*4882a593Smuzhiyun 	     sizeof(struct udphdr) + sizeof(struct vxlanhdr)))
1731*4882a593Smuzhiyun 		return false;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	return true;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun 
xgbe_is_tso(struct sk_buff * skb)1736*4882a593Smuzhiyun static int xgbe_is_tso(struct sk_buff *skb)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1739*4882a593Smuzhiyun 		return 0;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	if (!skb_is_gso(skb))
1742*4882a593Smuzhiyun 		return 0;
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	DBGPR("  TSO packet to be processed\n");
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	return 1;
1747*4882a593Smuzhiyun }
1748*4882a593Smuzhiyun 
xgbe_packet_info(struct xgbe_prv_data * pdata,struct xgbe_ring * ring,struct sk_buff * skb,struct xgbe_packet_data * packet)1749*4882a593Smuzhiyun static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1750*4882a593Smuzhiyun 			     struct xgbe_ring *ring, struct sk_buff *skb,
1751*4882a593Smuzhiyun 			     struct xgbe_packet_data *packet)
1752*4882a593Smuzhiyun {
1753*4882a593Smuzhiyun 	skb_frag_t *frag;
1754*4882a593Smuzhiyun 	unsigned int context_desc;
1755*4882a593Smuzhiyun 	unsigned int len;
1756*4882a593Smuzhiyun 	unsigned int i;
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 	packet->skb = skb;
1759*4882a593Smuzhiyun 
1760*4882a593Smuzhiyun 	context_desc = 0;
1761*4882a593Smuzhiyun 	packet->rdesc_count = 0;
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 	packet->tx_packets = 1;
1764*4882a593Smuzhiyun 	packet->tx_bytes = skb->len;
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	if (xgbe_is_tso(skb)) {
1767*4882a593Smuzhiyun 		/* TSO requires an extra descriptor if mss is different */
1768*4882a593Smuzhiyun 		if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1769*4882a593Smuzhiyun 			context_desc = 1;
1770*4882a593Smuzhiyun 			packet->rdesc_count++;
1771*4882a593Smuzhiyun 		}
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 		/* TSO requires an extra descriptor for TSO header */
1774*4882a593Smuzhiyun 		packet->rdesc_count++;
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1777*4882a593Smuzhiyun 			       TSO_ENABLE, 1);
1778*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1779*4882a593Smuzhiyun 			       CSUM_ENABLE, 1);
1780*4882a593Smuzhiyun 	} else if (skb->ip_summed == CHECKSUM_PARTIAL)
1781*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1782*4882a593Smuzhiyun 			       CSUM_ENABLE, 1);
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	if (xgbe_is_vxlan(skb))
1785*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1786*4882a593Smuzhiyun 			       VXLAN, 1);
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 	if (skb_vlan_tag_present(skb)) {
1789*4882a593Smuzhiyun 		/* VLAN requires an extra descriptor if tag is different */
1790*4882a593Smuzhiyun 		if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
1791*4882a593Smuzhiyun 			/* We can share with the TSO context descriptor */
1792*4882a593Smuzhiyun 			if (!context_desc) {
1793*4882a593Smuzhiyun 				context_desc = 1;
1794*4882a593Smuzhiyun 				packet->rdesc_count++;
1795*4882a593Smuzhiyun 			}
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1798*4882a593Smuzhiyun 			       VLAN_CTAG, 1);
1799*4882a593Smuzhiyun 	}
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1802*4882a593Smuzhiyun 	    (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1803*4882a593Smuzhiyun 		XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1804*4882a593Smuzhiyun 			       PTP, 1);
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun 	for (len = skb_headlen(skb); len;) {
1807*4882a593Smuzhiyun 		packet->rdesc_count++;
1808*4882a593Smuzhiyun 		len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1809*4882a593Smuzhiyun 	}
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1812*4882a593Smuzhiyun 		frag = &skb_shinfo(skb)->frags[i];
1813*4882a593Smuzhiyun 		for (len = skb_frag_size(frag); len; ) {
1814*4882a593Smuzhiyun 			packet->rdesc_count++;
1815*4882a593Smuzhiyun 			len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1816*4882a593Smuzhiyun 		}
1817*4882a593Smuzhiyun 	}
1818*4882a593Smuzhiyun }
1819*4882a593Smuzhiyun 
xgbe_open(struct net_device * netdev)1820*4882a593Smuzhiyun static int xgbe_open(struct net_device *netdev)
1821*4882a593Smuzhiyun {
1822*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1823*4882a593Smuzhiyun 	int ret;
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 	/* Create the various names based on netdev name */
1826*4882a593Smuzhiyun 	snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
1827*4882a593Smuzhiyun 		 netdev_name(netdev));
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 	snprintf(pdata->ecc_name, sizeof(pdata->ecc_name) - 1, "%s-ecc",
1830*4882a593Smuzhiyun 		 netdev_name(netdev));
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 	snprintf(pdata->i2c_name, sizeof(pdata->i2c_name) - 1, "%s-i2c",
1833*4882a593Smuzhiyun 		 netdev_name(netdev));
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	/* Create workqueues */
1836*4882a593Smuzhiyun 	pdata->dev_workqueue =
1837*4882a593Smuzhiyun 		create_singlethread_workqueue(netdev_name(netdev));
1838*4882a593Smuzhiyun 	if (!pdata->dev_workqueue) {
1839*4882a593Smuzhiyun 		netdev_err(netdev, "device workqueue creation failed\n");
1840*4882a593Smuzhiyun 		return -ENOMEM;
1841*4882a593Smuzhiyun 	}
1842*4882a593Smuzhiyun 
1843*4882a593Smuzhiyun 	pdata->an_workqueue =
1844*4882a593Smuzhiyun 		create_singlethread_workqueue(pdata->an_name);
1845*4882a593Smuzhiyun 	if (!pdata->an_workqueue) {
1846*4882a593Smuzhiyun 		netdev_err(netdev, "phy workqueue creation failed\n");
1847*4882a593Smuzhiyun 		ret = -ENOMEM;
1848*4882a593Smuzhiyun 		goto err_dev_wq;
1849*4882a593Smuzhiyun 	}
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	/* Reset the phy settings */
1852*4882a593Smuzhiyun 	ret = xgbe_phy_reset(pdata);
1853*4882a593Smuzhiyun 	if (ret)
1854*4882a593Smuzhiyun 		goto err_an_wq;
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	/* Enable the clocks */
1857*4882a593Smuzhiyun 	ret = clk_prepare_enable(pdata->sysclk);
1858*4882a593Smuzhiyun 	if (ret) {
1859*4882a593Smuzhiyun 		netdev_alert(netdev, "dma clk_prepare_enable failed\n");
1860*4882a593Smuzhiyun 		goto err_an_wq;
1861*4882a593Smuzhiyun 	}
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun 	ret = clk_prepare_enable(pdata->ptpclk);
1864*4882a593Smuzhiyun 	if (ret) {
1865*4882a593Smuzhiyun 		netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1866*4882a593Smuzhiyun 		goto err_sysclk;
1867*4882a593Smuzhiyun 	}
1868*4882a593Smuzhiyun 
1869*4882a593Smuzhiyun 	INIT_WORK(&pdata->service_work, xgbe_service);
1870*4882a593Smuzhiyun 	INIT_WORK(&pdata->restart_work, xgbe_restart);
1871*4882a593Smuzhiyun 	INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
1872*4882a593Smuzhiyun 	INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun 	ret = xgbe_alloc_memory(pdata);
1875*4882a593Smuzhiyun 	if (ret)
1876*4882a593Smuzhiyun 		goto err_ptpclk;
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun 	ret = xgbe_start(pdata);
1879*4882a593Smuzhiyun 	if (ret)
1880*4882a593Smuzhiyun 		goto err_mem;
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	clear_bit(XGBE_DOWN, &pdata->dev_state);
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 	return 0;
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun err_mem:
1887*4882a593Smuzhiyun 	xgbe_free_memory(pdata);
1888*4882a593Smuzhiyun 
1889*4882a593Smuzhiyun err_ptpclk:
1890*4882a593Smuzhiyun 	clk_disable_unprepare(pdata->ptpclk);
1891*4882a593Smuzhiyun 
1892*4882a593Smuzhiyun err_sysclk:
1893*4882a593Smuzhiyun 	clk_disable_unprepare(pdata->sysclk);
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun err_an_wq:
1896*4882a593Smuzhiyun 	destroy_workqueue(pdata->an_workqueue);
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun err_dev_wq:
1899*4882a593Smuzhiyun 	destroy_workqueue(pdata->dev_workqueue);
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 	return ret;
1902*4882a593Smuzhiyun }
1903*4882a593Smuzhiyun 
xgbe_close(struct net_device * netdev)1904*4882a593Smuzhiyun static int xgbe_close(struct net_device *netdev)
1905*4882a593Smuzhiyun {
1906*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	/* Stop the device */
1909*4882a593Smuzhiyun 	xgbe_stop(pdata);
1910*4882a593Smuzhiyun 
1911*4882a593Smuzhiyun 	xgbe_free_memory(pdata);
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	/* Disable the clocks */
1914*4882a593Smuzhiyun 	clk_disable_unprepare(pdata->ptpclk);
1915*4882a593Smuzhiyun 	clk_disable_unprepare(pdata->sysclk);
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun 	flush_workqueue(pdata->an_workqueue);
1918*4882a593Smuzhiyun 	destroy_workqueue(pdata->an_workqueue);
1919*4882a593Smuzhiyun 
1920*4882a593Smuzhiyun 	flush_workqueue(pdata->dev_workqueue);
1921*4882a593Smuzhiyun 	destroy_workqueue(pdata->dev_workqueue);
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 	set_bit(XGBE_DOWN, &pdata->dev_state);
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	return 0;
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun 
xgbe_xmit(struct sk_buff * skb,struct net_device * netdev)1928*4882a593Smuzhiyun static netdev_tx_t xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1929*4882a593Smuzhiyun {
1930*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
1931*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
1932*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
1933*4882a593Smuzhiyun 	struct xgbe_channel *channel;
1934*4882a593Smuzhiyun 	struct xgbe_ring *ring;
1935*4882a593Smuzhiyun 	struct xgbe_packet_data *packet;
1936*4882a593Smuzhiyun 	struct netdev_queue *txq;
1937*4882a593Smuzhiyun 	netdev_tx_t ret;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	channel = pdata->channel[skb->queue_mapping];
1942*4882a593Smuzhiyun 	txq = netdev_get_tx_queue(netdev, channel->queue_index);
1943*4882a593Smuzhiyun 	ring = channel->tx_ring;
1944*4882a593Smuzhiyun 	packet = &ring->packet_data;
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 	ret = NETDEV_TX_OK;
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	if (skb->len == 0) {
1949*4882a593Smuzhiyun 		netif_err(pdata, tx_err, netdev,
1950*4882a593Smuzhiyun 			  "empty skb received from stack\n");
1951*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
1952*4882a593Smuzhiyun 		goto tx_netdev_return;
1953*4882a593Smuzhiyun 	}
1954*4882a593Smuzhiyun 
1955*4882a593Smuzhiyun 	/* Calculate preliminary packet info */
1956*4882a593Smuzhiyun 	memset(packet, 0, sizeof(*packet));
1957*4882a593Smuzhiyun 	xgbe_packet_info(pdata, ring, skb, packet);
1958*4882a593Smuzhiyun 
1959*4882a593Smuzhiyun 	/* Check that there are enough descriptors available */
1960*4882a593Smuzhiyun 	ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
1961*4882a593Smuzhiyun 	if (ret)
1962*4882a593Smuzhiyun 		goto tx_netdev_return;
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun 	ret = xgbe_prep_tso(skb, packet);
1965*4882a593Smuzhiyun 	if (ret) {
1966*4882a593Smuzhiyun 		netif_err(pdata, tx_err, netdev,
1967*4882a593Smuzhiyun 			  "error processing TSO packet\n");
1968*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
1969*4882a593Smuzhiyun 		goto tx_netdev_return;
1970*4882a593Smuzhiyun 	}
1971*4882a593Smuzhiyun 	xgbe_prep_vlan(skb, packet);
1972*4882a593Smuzhiyun 
1973*4882a593Smuzhiyun 	if (!desc_if->map_tx_skb(channel, skb)) {
1974*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
1975*4882a593Smuzhiyun 		goto tx_netdev_return;
1976*4882a593Smuzhiyun 	}
1977*4882a593Smuzhiyun 
1978*4882a593Smuzhiyun 	xgbe_prep_tx_tstamp(pdata, skb, packet);
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	/* Report on the actual number of bytes (to be) sent */
1981*4882a593Smuzhiyun 	netdev_tx_sent_queue(txq, packet->tx_bytes);
1982*4882a593Smuzhiyun 
1983*4882a593Smuzhiyun 	/* Configure required descriptor fields for transmission */
1984*4882a593Smuzhiyun 	hw_if->dev_xmit(channel);
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	if (netif_msg_pktdata(pdata))
1987*4882a593Smuzhiyun 		xgbe_print_pkt(netdev, skb, true);
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	/* Stop the queue in advance if there may not be enough descriptors */
1990*4882a593Smuzhiyun 	xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun 	ret = NETDEV_TX_OK;
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun tx_netdev_return:
1995*4882a593Smuzhiyun 	return ret;
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun 
xgbe_set_rx_mode(struct net_device * netdev)1998*4882a593Smuzhiyun static void xgbe_set_rx_mode(struct net_device *netdev)
1999*4882a593Smuzhiyun {
2000*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2001*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 	DBGPR("-->xgbe_set_rx_mode\n");
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 	hw_if->config_rx_mode(pdata);
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	DBGPR("<--xgbe_set_rx_mode\n");
2008*4882a593Smuzhiyun }
2009*4882a593Smuzhiyun 
xgbe_set_mac_address(struct net_device * netdev,void * addr)2010*4882a593Smuzhiyun static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
2011*4882a593Smuzhiyun {
2012*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2013*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2014*4882a593Smuzhiyun 	struct sockaddr *saddr = addr;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	DBGPR("-->xgbe_set_mac_address\n");
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	if (!is_valid_ether_addr(saddr->sa_data))
2019*4882a593Smuzhiyun 		return -EADDRNOTAVAIL;
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	hw_if->set_mac_address(pdata, netdev->dev_addr);
2024*4882a593Smuzhiyun 
2025*4882a593Smuzhiyun 	DBGPR("<--xgbe_set_mac_address\n");
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 	return 0;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun 
xgbe_ioctl(struct net_device * netdev,struct ifreq * ifreq,int cmd)2030*4882a593Smuzhiyun static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
2031*4882a593Smuzhiyun {
2032*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2033*4882a593Smuzhiyun 	int ret;
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	switch (cmd) {
2036*4882a593Smuzhiyun 	case SIOCGHWTSTAMP:
2037*4882a593Smuzhiyun 		ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
2038*4882a593Smuzhiyun 		break;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 	case SIOCSHWTSTAMP:
2041*4882a593Smuzhiyun 		ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
2042*4882a593Smuzhiyun 		break;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	default:
2045*4882a593Smuzhiyun 		ret = -EOPNOTSUPP;
2046*4882a593Smuzhiyun 	}
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	return ret;
2049*4882a593Smuzhiyun }
2050*4882a593Smuzhiyun 
xgbe_change_mtu(struct net_device * netdev,int mtu)2051*4882a593Smuzhiyun static int xgbe_change_mtu(struct net_device *netdev, int mtu)
2052*4882a593Smuzhiyun {
2053*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2054*4882a593Smuzhiyun 	int ret;
2055*4882a593Smuzhiyun 
2056*4882a593Smuzhiyun 	DBGPR("-->xgbe_change_mtu\n");
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun 	ret = xgbe_calc_rx_buf_size(netdev, mtu);
2059*4882a593Smuzhiyun 	if (ret < 0)
2060*4882a593Smuzhiyun 		return ret;
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun 	pdata->rx_buf_size = ret;
2063*4882a593Smuzhiyun 	netdev->mtu = mtu;
2064*4882a593Smuzhiyun 
2065*4882a593Smuzhiyun 	xgbe_restart_dev(pdata);
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	DBGPR("<--xgbe_change_mtu\n");
2068*4882a593Smuzhiyun 
2069*4882a593Smuzhiyun 	return 0;
2070*4882a593Smuzhiyun }
2071*4882a593Smuzhiyun 
xgbe_tx_timeout(struct net_device * netdev,unsigned int txqueue)2072*4882a593Smuzhiyun static void xgbe_tx_timeout(struct net_device *netdev, unsigned int txqueue)
2073*4882a593Smuzhiyun {
2074*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun 	netdev_warn(netdev, "tx timeout, device restarting\n");
2077*4882a593Smuzhiyun 	schedule_work(&pdata->restart_work);
2078*4882a593Smuzhiyun }
2079*4882a593Smuzhiyun 
xgbe_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * s)2080*4882a593Smuzhiyun static void xgbe_get_stats64(struct net_device *netdev,
2081*4882a593Smuzhiyun 			     struct rtnl_link_stats64 *s)
2082*4882a593Smuzhiyun {
2083*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2084*4882a593Smuzhiyun 	struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	DBGPR("-->%s\n", __func__);
2087*4882a593Smuzhiyun 
2088*4882a593Smuzhiyun 	pdata->hw_if.read_mmc_stats(pdata);
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	s->rx_packets = pstats->rxframecount_gb;
2091*4882a593Smuzhiyun 	s->rx_bytes = pstats->rxoctetcount_gb;
2092*4882a593Smuzhiyun 	s->rx_errors = pstats->rxframecount_gb -
2093*4882a593Smuzhiyun 		       pstats->rxbroadcastframes_g -
2094*4882a593Smuzhiyun 		       pstats->rxmulticastframes_g -
2095*4882a593Smuzhiyun 		       pstats->rxunicastframes_g;
2096*4882a593Smuzhiyun 	s->multicast = pstats->rxmulticastframes_g;
2097*4882a593Smuzhiyun 	s->rx_length_errors = pstats->rxlengtherror;
2098*4882a593Smuzhiyun 	s->rx_crc_errors = pstats->rxcrcerror;
2099*4882a593Smuzhiyun 	s->rx_fifo_errors = pstats->rxfifooverflow;
2100*4882a593Smuzhiyun 
2101*4882a593Smuzhiyun 	s->tx_packets = pstats->txframecount_gb;
2102*4882a593Smuzhiyun 	s->tx_bytes = pstats->txoctetcount_gb;
2103*4882a593Smuzhiyun 	s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
2104*4882a593Smuzhiyun 	s->tx_dropped = netdev->stats.tx_dropped;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	DBGPR("<--%s\n", __func__);
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun 
xgbe_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)2109*4882a593Smuzhiyun static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
2110*4882a593Smuzhiyun 				u16 vid)
2111*4882a593Smuzhiyun {
2112*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2113*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2114*4882a593Smuzhiyun 
2115*4882a593Smuzhiyun 	DBGPR("-->%s\n", __func__);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	set_bit(vid, pdata->active_vlans);
2118*4882a593Smuzhiyun 	hw_if->update_vlan_hash_table(pdata);
2119*4882a593Smuzhiyun 
2120*4882a593Smuzhiyun 	DBGPR("<--%s\n", __func__);
2121*4882a593Smuzhiyun 
2122*4882a593Smuzhiyun 	return 0;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun 
xgbe_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)2125*4882a593Smuzhiyun static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
2126*4882a593Smuzhiyun 				 u16 vid)
2127*4882a593Smuzhiyun {
2128*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2129*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	DBGPR("-->%s\n", __func__);
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	clear_bit(vid, pdata->active_vlans);
2134*4882a593Smuzhiyun 	hw_if->update_vlan_hash_table(pdata);
2135*4882a593Smuzhiyun 
2136*4882a593Smuzhiyun 	DBGPR("<--%s\n", __func__);
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	return 0;
2139*4882a593Smuzhiyun }
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun #ifdef CONFIG_NET_POLL_CONTROLLER
xgbe_poll_controller(struct net_device * netdev)2142*4882a593Smuzhiyun static void xgbe_poll_controller(struct net_device *netdev)
2143*4882a593Smuzhiyun {
2144*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2145*4882a593Smuzhiyun 	struct xgbe_channel *channel;
2146*4882a593Smuzhiyun 	unsigned int i;
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	DBGPR("-->xgbe_poll_controller\n");
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	if (pdata->per_channel_irq) {
2151*4882a593Smuzhiyun 		for (i = 0; i < pdata->channel_count; i++) {
2152*4882a593Smuzhiyun 			channel = pdata->channel[i];
2153*4882a593Smuzhiyun 			xgbe_dma_isr(channel->dma_irq, channel);
2154*4882a593Smuzhiyun 		}
2155*4882a593Smuzhiyun 	} else {
2156*4882a593Smuzhiyun 		disable_irq(pdata->dev_irq);
2157*4882a593Smuzhiyun 		xgbe_isr(pdata->dev_irq, pdata);
2158*4882a593Smuzhiyun 		enable_irq(pdata->dev_irq);
2159*4882a593Smuzhiyun 	}
2160*4882a593Smuzhiyun 
2161*4882a593Smuzhiyun 	DBGPR("<--xgbe_poll_controller\n");
2162*4882a593Smuzhiyun }
2163*4882a593Smuzhiyun #endif /* End CONFIG_NET_POLL_CONTROLLER */
2164*4882a593Smuzhiyun 
xgbe_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)2165*4882a593Smuzhiyun static int xgbe_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2166*4882a593Smuzhiyun 			 void *type_data)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2169*4882a593Smuzhiyun 	struct tc_mqprio_qopt *mqprio = type_data;
2170*4882a593Smuzhiyun 	u8 tc;
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun 	if (type != TC_SETUP_QDISC_MQPRIO)
2173*4882a593Smuzhiyun 		return -EOPNOTSUPP;
2174*4882a593Smuzhiyun 
2175*4882a593Smuzhiyun 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2176*4882a593Smuzhiyun 	tc = mqprio->num_tc;
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 	if (tc > pdata->hw_feat.tc_cnt)
2179*4882a593Smuzhiyun 		return -EINVAL;
2180*4882a593Smuzhiyun 
2181*4882a593Smuzhiyun 	pdata->num_tcs = tc;
2182*4882a593Smuzhiyun 	pdata->hw_if.config_tc(pdata);
2183*4882a593Smuzhiyun 
2184*4882a593Smuzhiyun 	return 0;
2185*4882a593Smuzhiyun }
2186*4882a593Smuzhiyun 
xgbe_fix_features(struct net_device * netdev,netdev_features_t features)2187*4882a593Smuzhiyun static netdev_features_t xgbe_fix_features(struct net_device *netdev,
2188*4882a593Smuzhiyun 					   netdev_features_t features)
2189*4882a593Smuzhiyun {
2190*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2191*4882a593Smuzhiyun 	netdev_features_t vxlan_base;
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 	vxlan_base = NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RX_UDP_TUNNEL_PORT;
2194*4882a593Smuzhiyun 
2195*4882a593Smuzhiyun 	if (!pdata->hw_feat.vxn)
2196*4882a593Smuzhiyun 		return features;
2197*4882a593Smuzhiyun 
2198*4882a593Smuzhiyun 	/* VXLAN CSUM requires VXLAN base */
2199*4882a593Smuzhiyun 	if ((features & NETIF_F_GSO_UDP_TUNNEL_CSUM) &&
2200*4882a593Smuzhiyun 	    !(features & NETIF_F_GSO_UDP_TUNNEL)) {
2201*4882a593Smuzhiyun 		netdev_notice(netdev,
2202*4882a593Smuzhiyun 			      "forcing tx udp tunnel support\n");
2203*4882a593Smuzhiyun 		features |= NETIF_F_GSO_UDP_TUNNEL;
2204*4882a593Smuzhiyun 	}
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 	/* Can't do one without doing the other */
2207*4882a593Smuzhiyun 	if ((features & vxlan_base) != vxlan_base) {
2208*4882a593Smuzhiyun 		netdev_notice(netdev,
2209*4882a593Smuzhiyun 			      "forcing both tx and rx udp tunnel support\n");
2210*4882a593Smuzhiyun 		features |= vxlan_base;
2211*4882a593Smuzhiyun 	}
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 	if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2214*4882a593Smuzhiyun 		if (!(features & NETIF_F_GSO_UDP_TUNNEL_CSUM)) {
2215*4882a593Smuzhiyun 			netdev_notice(netdev,
2216*4882a593Smuzhiyun 				      "forcing tx udp tunnel checksumming on\n");
2217*4882a593Smuzhiyun 			features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2218*4882a593Smuzhiyun 		}
2219*4882a593Smuzhiyun 	} else {
2220*4882a593Smuzhiyun 		if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM) {
2221*4882a593Smuzhiyun 			netdev_notice(netdev,
2222*4882a593Smuzhiyun 				      "forcing tx udp tunnel checksumming off\n");
2223*4882a593Smuzhiyun 			features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
2224*4882a593Smuzhiyun 		}
2225*4882a593Smuzhiyun 	}
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun 	return features;
2228*4882a593Smuzhiyun }
2229*4882a593Smuzhiyun 
xgbe_set_features(struct net_device * netdev,netdev_features_t features)2230*4882a593Smuzhiyun static int xgbe_set_features(struct net_device *netdev,
2231*4882a593Smuzhiyun 			     netdev_features_t features)
2232*4882a593Smuzhiyun {
2233*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
2234*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2235*4882a593Smuzhiyun 	netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
2236*4882a593Smuzhiyun 	int ret = 0;
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	rxhash = pdata->netdev_features & NETIF_F_RXHASH;
2239*4882a593Smuzhiyun 	rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
2240*4882a593Smuzhiyun 	rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
2241*4882a593Smuzhiyun 	rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
2242*4882a593Smuzhiyun 
2243*4882a593Smuzhiyun 	if ((features & NETIF_F_RXHASH) && !rxhash)
2244*4882a593Smuzhiyun 		ret = hw_if->enable_rss(pdata);
2245*4882a593Smuzhiyun 	else if (!(features & NETIF_F_RXHASH) && rxhash)
2246*4882a593Smuzhiyun 		ret = hw_if->disable_rss(pdata);
2247*4882a593Smuzhiyun 	if (ret)
2248*4882a593Smuzhiyun 		return ret;
2249*4882a593Smuzhiyun 
2250*4882a593Smuzhiyun 	if ((features & NETIF_F_RXCSUM) && !rxcsum)
2251*4882a593Smuzhiyun 		hw_if->enable_rx_csum(pdata);
2252*4882a593Smuzhiyun 	else if (!(features & NETIF_F_RXCSUM) && rxcsum)
2253*4882a593Smuzhiyun 		hw_if->disable_rx_csum(pdata);
2254*4882a593Smuzhiyun 
2255*4882a593Smuzhiyun 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
2256*4882a593Smuzhiyun 		hw_if->enable_rx_vlan_stripping(pdata);
2257*4882a593Smuzhiyun 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
2258*4882a593Smuzhiyun 		hw_if->disable_rx_vlan_stripping(pdata);
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
2261*4882a593Smuzhiyun 		hw_if->enable_rx_vlan_filtering(pdata);
2262*4882a593Smuzhiyun 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
2263*4882a593Smuzhiyun 		hw_if->disable_rx_vlan_filtering(pdata);
2264*4882a593Smuzhiyun 
2265*4882a593Smuzhiyun 	pdata->netdev_features = features;
2266*4882a593Smuzhiyun 
2267*4882a593Smuzhiyun 	DBGPR("<--xgbe_set_features\n");
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 	return 0;
2270*4882a593Smuzhiyun }
2271*4882a593Smuzhiyun 
xgbe_features_check(struct sk_buff * skb,struct net_device * netdev,netdev_features_t features)2272*4882a593Smuzhiyun static netdev_features_t xgbe_features_check(struct sk_buff *skb,
2273*4882a593Smuzhiyun 					     struct net_device *netdev,
2274*4882a593Smuzhiyun 					     netdev_features_t features)
2275*4882a593Smuzhiyun {
2276*4882a593Smuzhiyun 	features = vlan_features_check(skb, features);
2277*4882a593Smuzhiyun 	features = vxlan_features_check(skb, features);
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun 	return features;
2280*4882a593Smuzhiyun }
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun static const struct net_device_ops xgbe_netdev_ops = {
2283*4882a593Smuzhiyun 	.ndo_open		= xgbe_open,
2284*4882a593Smuzhiyun 	.ndo_stop		= xgbe_close,
2285*4882a593Smuzhiyun 	.ndo_start_xmit		= xgbe_xmit,
2286*4882a593Smuzhiyun 	.ndo_set_rx_mode	= xgbe_set_rx_mode,
2287*4882a593Smuzhiyun 	.ndo_set_mac_address	= xgbe_set_mac_address,
2288*4882a593Smuzhiyun 	.ndo_validate_addr	= eth_validate_addr,
2289*4882a593Smuzhiyun 	.ndo_do_ioctl		= xgbe_ioctl,
2290*4882a593Smuzhiyun 	.ndo_change_mtu		= xgbe_change_mtu,
2291*4882a593Smuzhiyun 	.ndo_tx_timeout		= xgbe_tx_timeout,
2292*4882a593Smuzhiyun 	.ndo_get_stats64	= xgbe_get_stats64,
2293*4882a593Smuzhiyun 	.ndo_vlan_rx_add_vid	= xgbe_vlan_rx_add_vid,
2294*4882a593Smuzhiyun 	.ndo_vlan_rx_kill_vid	= xgbe_vlan_rx_kill_vid,
2295*4882a593Smuzhiyun #ifdef CONFIG_NET_POLL_CONTROLLER
2296*4882a593Smuzhiyun 	.ndo_poll_controller	= xgbe_poll_controller,
2297*4882a593Smuzhiyun #endif
2298*4882a593Smuzhiyun 	.ndo_setup_tc		= xgbe_setup_tc,
2299*4882a593Smuzhiyun 	.ndo_fix_features	= xgbe_fix_features,
2300*4882a593Smuzhiyun 	.ndo_set_features	= xgbe_set_features,
2301*4882a593Smuzhiyun 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
2302*4882a593Smuzhiyun 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
2303*4882a593Smuzhiyun 	.ndo_features_check	= xgbe_features_check,
2304*4882a593Smuzhiyun };
2305*4882a593Smuzhiyun 
xgbe_get_netdev_ops(void)2306*4882a593Smuzhiyun const struct net_device_ops *xgbe_get_netdev_ops(void)
2307*4882a593Smuzhiyun {
2308*4882a593Smuzhiyun 	return &xgbe_netdev_ops;
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun 
xgbe_rx_refresh(struct xgbe_channel * channel)2311*4882a593Smuzhiyun static void xgbe_rx_refresh(struct xgbe_channel *channel)
2312*4882a593Smuzhiyun {
2313*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
2314*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2315*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
2316*4882a593Smuzhiyun 	struct xgbe_ring *ring = channel->rx_ring;
2317*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
2318*4882a593Smuzhiyun 
2319*4882a593Smuzhiyun 	while (ring->dirty != ring->cur) {
2320*4882a593Smuzhiyun 		rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 		/* Reset rdata values */
2323*4882a593Smuzhiyun 		desc_if->unmap_rdata(pdata, rdata);
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 		if (desc_if->map_rx_buffer(pdata, ring, rdata))
2326*4882a593Smuzhiyun 			break;
2327*4882a593Smuzhiyun 
2328*4882a593Smuzhiyun 		hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
2329*4882a593Smuzhiyun 
2330*4882a593Smuzhiyun 		ring->dirty++;
2331*4882a593Smuzhiyun 	}
2332*4882a593Smuzhiyun 
2333*4882a593Smuzhiyun 	/* Make sure everything is written before the register write */
2334*4882a593Smuzhiyun 	wmb();
2335*4882a593Smuzhiyun 
2336*4882a593Smuzhiyun 	/* Update the Rx Tail Pointer Register with address of
2337*4882a593Smuzhiyun 	 * the last cleaned entry */
2338*4882a593Smuzhiyun 	rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
2339*4882a593Smuzhiyun 	XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
2340*4882a593Smuzhiyun 			  lower_32_bits(rdata->rdesc_dma));
2341*4882a593Smuzhiyun }
2342*4882a593Smuzhiyun 
xgbe_create_skb(struct xgbe_prv_data * pdata,struct napi_struct * napi,struct xgbe_ring_data * rdata,unsigned int len)2343*4882a593Smuzhiyun static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
2344*4882a593Smuzhiyun 				       struct napi_struct *napi,
2345*4882a593Smuzhiyun 				       struct xgbe_ring_data *rdata,
2346*4882a593Smuzhiyun 				       unsigned int len)
2347*4882a593Smuzhiyun {
2348*4882a593Smuzhiyun 	struct sk_buff *skb;
2349*4882a593Smuzhiyun 	u8 *packet;
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
2352*4882a593Smuzhiyun 	if (!skb)
2353*4882a593Smuzhiyun 		return NULL;
2354*4882a593Smuzhiyun 
2355*4882a593Smuzhiyun 	/* Pull in the header buffer which may contain just the header
2356*4882a593Smuzhiyun 	 * or the header plus data
2357*4882a593Smuzhiyun 	 */
2358*4882a593Smuzhiyun 	dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
2359*4882a593Smuzhiyun 				      rdata->rx.hdr.dma_off,
2360*4882a593Smuzhiyun 				      rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
2361*4882a593Smuzhiyun 
2362*4882a593Smuzhiyun 	packet = page_address(rdata->rx.hdr.pa.pages) +
2363*4882a593Smuzhiyun 		 rdata->rx.hdr.pa.pages_offset;
2364*4882a593Smuzhiyun 	skb_copy_to_linear_data(skb, packet, len);
2365*4882a593Smuzhiyun 	skb_put(skb, len);
2366*4882a593Smuzhiyun 
2367*4882a593Smuzhiyun 	return skb;
2368*4882a593Smuzhiyun }
2369*4882a593Smuzhiyun 
xgbe_rx_buf1_len(struct xgbe_ring_data * rdata,struct xgbe_packet_data * packet)2370*4882a593Smuzhiyun static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
2371*4882a593Smuzhiyun 				     struct xgbe_packet_data *packet)
2372*4882a593Smuzhiyun {
2373*4882a593Smuzhiyun 	/* Always zero if not the first descriptor */
2374*4882a593Smuzhiyun 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
2375*4882a593Smuzhiyun 		return 0;
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	/* First descriptor with split header, return header length */
2378*4882a593Smuzhiyun 	if (rdata->rx.hdr_len)
2379*4882a593Smuzhiyun 		return rdata->rx.hdr_len;
2380*4882a593Smuzhiyun 
2381*4882a593Smuzhiyun 	/* First descriptor but not the last descriptor and no split header,
2382*4882a593Smuzhiyun 	 * so the full buffer was used
2383*4882a593Smuzhiyun 	 */
2384*4882a593Smuzhiyun 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2385*4882a593Smuzhiyun 		return rdata->rx.hdr.dma_len;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 	/* First descriptor and last descriptor and no split header, so
2388*4882a593Smuzhiyun 	 * calculate how much of the buffer was used
2389*4882a593Smuzhiyun 	 */
2390*4882a593Smuzhiyun 	return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
2391*4882a593Smuzhiyun }
2392*4882a593Smuzhiyun 
xgbe_rx_buf2_len(struct xgbe_ring_data * rdata,struct xgbe_packet_data * packet,unsigned int len)2393*4882a593Smuzhiyun static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
2394*4882a593Smuzhiyun 				     struct xgbe_packet_data *packet,
2395*4882a593Smuzhiyun 				     unsigned int len)
2396*4882a593Smuzhiyun {
2397*4882a593Smuzhiyun 	/* Always the full buffer if not the last descriptor */
2398*4882a593Smuzhiyun 	if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2399*4882a593Smuzhiyun 		return rdata->rx.buf.dma_len;
2400*4882a593Smuzhiyun 
2401*4882a593Smuzhiyun 	/* Last descriptor so calculate how much of the buffer was used
2402*4882a593Smuzhiyun 	 * for the last bit of data
2403*4882a593Smuzhiyun 	 */
2404*4882a593Smuzhiyun 	return rdata->rx.len - len;
2405*4882a593Smuzhiyun }
2406*4882a593Smuzhiyun 
xgbe_tx_poll(struct xgbe_channel * channel)2407*4882a593Smuzhiyun static int xgbe_tx_poll(struct xgbe_channel *channel)
2408*4882a593Smuzhiyun {
2409*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
2410*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2411*4882a593Smuzhiyun 	struct xgbe_desc_if *desc_if = &pdata->desc_if;
2412*4882a593Smuzhiyun 	struct xgbe_ring *ring = channel->tx_ring;
2413*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
2414*4882a593Smuzhiyun 	struct xgbe_ring_desc *rdesc;
2415*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
2416*4882a593Smuzhiyun 	struct netdev_queue *txq;
2417*4882a593Smuzhiyun 	int processed = 0;
2418*4882a593Smuzhiyun 	unsigned int tx_packets = 0, tx_bytes = 0;
2419*4882a593Smuzhiyun 	unsigned int cur;
2420*4882a593Smuzhiyun 
2421*4882a593Smuzhiyun 	DBGPR("-->xgbe_tx_poll\n");
2422*4882a593Smuzhiyun 
2423*4882a593Smuzhiyun 	/* Nothing to do if there isn't a Tx ring for this channel */
2424*4882a593Smuzhiyun 	if (!ring)
2425*4882a593Smuzhiyun 		return 0;
2426*4882a593Smuzhiyun 
2427*4882a593Smuzhiyun 	cur = ring->cur;
2428*4882a593Smuzhiyun 
2429*4882a593Smuzhiyun 	/* Be sure we get ring->cur before accessing descriptor data */
2430*4882a593Smuzhiyun 	smp_rmb();
2431*4882a593Smuzhiyun 
2432*4882a593Smuzhiyun 	txq = netdev_get_tx_queue(netdev, channel->queue_index);
2433*4882a593Smuzhiyun 
2434*4882a593Smuzhiyun 	while ((processed < XGBE_TX_DESC_MAX_PROC) &&
2435*4882a593Smuzhiyun 	       (ring->dirty != cur)) {
2436*4882a593Smuzhiyun 		rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2437*4882a593Smuzhiyun 		rdesc = rdata->rdesc;
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun 		if (!hw_if->tx_complete(rdesc))
2440*4882a593Smuzhiyun 			break;
2441*4882a593Smuzhiyun 
2442*4882a593Smuzhiyun 		/* Make sure descriptor fields are read after reading the OWN
2443*4882a593Smuzhiyun 		 * bit */
2444*4882a593Smuzhiyun 		dma_rmb();
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 		if (netif_msg_tx_done(pdata))
2447*4882a593Smuzhiyun 			xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
2448*4882a593Smuzhiyun 
2449*4882a593Smuzhiyun 		if (hw_if->is_last_desc(rdesc)) {
2450*4882a593Smuzhiyun 			tx_packets += rdata->tx.packets;
2451*4882a593Smuzhiyun 			tx_bytes += rdata->tx.bytes;
2452*4882a593Smuzhiyun 		}
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun 		/* Free the SKB and reset the descriptor for re-use */
2455*4882a593Smuzhiyun 		desc_if->unmap_rdata(pdata, rdata);
2456*4882a593Smuzhiyun 		hw_if->tx_desc_reset(rdata);
2457*4882a593Smuzhiyun 
2458*4882a593Smuzhiyun 		processed++;
2459*4882a593Smuzhiyun 		ring->dirty++;
2460*4882a593Smuzhiyun 	}
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun 	if (!processed)
2463*4882a593Smuzhiyun 		return 0;
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun 	netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2466*4882a593Smuzhiyun 
2467*4882a593Smuzhiyun 	if ((ring->tx.queue_stopped == 1) &&
2468*4882a593Smuzhiyun 	    (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
2469*4882a593Smuzhiyun 		ring->tx.queue_stopped = 0;
2470*4882a593Smuzhiyun 		netif_tx_wake_queue(txq);
2471*4882a593Smuzhiyun 	}
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 	DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2474*4882a593Smuzhiyun 
2475*4882a593Smuzhiyun 	return processed;
2476*4882a593Smuzhiyun }
2477*4882a593Smuzhiyun 
xgbe_rx_poll(struct xgbe_channel * channel,int budget)2478*4882a593Smuzhiyun static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2479*4882a593Smuzhiyun {
2480*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
2481*4882a593Smuzhiyun 	struct xgbe_hw_if *hw_if = &pdata->hw_if;
2482*4882a593Smuzhiyun 	struct xgbe_ring *ring = channel->rx_ring;
2483*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
2484*4882a593Smuzhiyun 	struct xgbe_packet_data *packet;
2485*4882a593Smuzhiyun 	struct net_device *netdev = pdata->netdev;
2486*4882a593Smuzhiyun 	struct napi_struct *napi;
2487*4882a593Smuzhiyun 	struct sk_buff *skb;
2488*4882a593Smuzhiyun 	struct skb_shared_hwtstamps *hwtstamps;
2489*4882a593Smuzhiyun 	unsigned int last, error, context_next, context;
2490*4882a593Smuzhiyun 	unsigned int len, buf1_len, buf2_len, max_len;
2491*4882a593Smuzhiyun 	unsigned int received = 0;
2492*4882a593Smuzhiyun 	int packet_count = 0;
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun 	/* Nothing to do if there isn't a Rx ring for this channel */
2497*4882a593Smuzhiyun 	if (!ring)
2498*4882a593Smuzhiyun 		return 0;
2499*4882a593Smuzhiyun 
2500*4882a593Smuzhiyun 	last = 0;
2501*4882a593Smuzhiyun 	context_next = 0;
2502*4882a593Smuzhiyun 
2503*4882a593Smuzhiyun 	napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 	rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2506*4882a593Smuzhiyun 	packet = &ring->packet_data;
2507*4882a593Smuzhiyun 	while (packet_count < budget) {
2508*4882a593Smuzhiyun 		DBGPR("  cur = %d\n", ring->cur);
2509*4882a593Smuzhiyun 
2510*4882a593Smuzhiyun 		/* First time in loop see if we need to restore state */
2511*4882a593Smuzhiyun 		if (!received && rdata->state_saved) {
2512*4882a593Smuzhiyun 			skb = rdata->state.skb;
2513*4882a593Smuzhiyun 			error = rdata->state.error;
2514*4882a593Smuzhiyun 			len = rdata->state.len;
2515*4882a593Smuzhiyun 		} else {
2516*4882a593Smuzhiyun 			memset(packet, 0, sizeof(*packet));
2517*4882a593Smuzhiyun 			skb = NULL;
2518*4882a593Smuzhiyun 			error = 0;
2519*4882a593Smuzhiyun 			len = 0;
2520*4882a593Smuzhiyun 		}
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun read_again:
2523*4882a593Smuzhiyun 		rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2524*4882a593Smuzhiyun 
2525*4882a593Smuzhiyun 		if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
2526*4882a593Smuzhiyun 			xgbe_rx_refresh(channel);
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 		if (hw_if->dev_read(channel))
2529*4882a593Smuzhiyun 			break;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 		received++;
2532*4882a593Smuzhiyun 		ring->cur++;
2533*4882a593Smuzhiyun 
2534*4882a593Smuzhiyun 		last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2535*4882a593Smuzhiyun 				      LAST);
2536*4882a593Smuzhiyun 		context_next = XGMAC_GET_BITS(packet->attributes,
2537*4882a593Smuzhiyun 					      RX_PACKET_ATTRIBUTES,
2538*4882a593Smuzhiyun 					      CONTEXT_NEXT);
2539*4882a593Smuzhiyun 		context = XGMAC_GET_BITS(packet->attributes,
2540*4882a593Smuzhiyun 					 RX_PACKET_ATTRIBUTES,
2541*4882a593Smuzhiyun 					 CONTEXT);
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 		/* Earlier error, just drain the remaining data */
2544*4882a593Smuzhiyun 		if ((!last || context_next) && error)
2545*4882a593Smuzhiyun 			goto read_again;
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun 		if (error || packet->errors) {
2548*4882a593Smuzhiyun 			if (packet->errors)
2549*4882a593Smuzhiyun 				netif_err(pdata, rx_err, netdev,
2550*4882a593Smuzhiyun 					  "error in received packet\n");
2551*4882a593Smuzhiyun 			dev_kfree_skb(skb);
2552*4882a593Smuzhiyun 			goto next_packet;
2553*4882a593Smuzhiyun 		}
2554*4882a593Smuzhiyun 
2555*4882a593Smuzhiyun 		if (!context) {
2556*4882a593Smuzhiyun 			/* Get the data length in the descriptor buffers */
2557*4882a593Smuzhiyun 			buf1_len = xgbe_rx_buf1_len(rdata, packet);
2558*4882a593Smuzhiyun 			len += buf1_len;
2559*4882a593Smuzhiyun 			buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
2560*4882a593Smuzhiyun 			len += buf2_len;
2561*4882a593Smuzhiyun 
2562*4882a593Smuzhiyun 			if (buf2_len > rdata->rx.buf.dma_len) {
2563*4882a593Smuzhiyun 				/* Hardware inconsistency within the descriptors
2564*4882a593Smuzhiyun 				 * that has resulted in a length underflow.
2565*4882a593Smuzhiyun 				 */
2566*4882a593Smuzhiyun 				error = 1;
2567*4882a593Smuzhiyun 				goto skip_data;
2568*4882a593Smuzhiyun 			}
2569*4882a593Smuzhiyun 
2570*4882a593Smuzhiyun 			if (!skb) {
2571*4882a593Smuzhiyun 				skb = xgbe_create_skb(pdata, napi, rdata,
2572*4882a593Smuzhiyun 						      buf1_len);
2573*4882a593Smuzhiyun 				if (!skb) {
2574*4882a593Smuzhiyun 					error = 1;
2575*4882a593Smuzhiyun 					goto skip_data;
2576*4882a593Smuzhiyun 				}
2577*4882a593Smuzhiyun 			}
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun 			if (buf2_len) {
2580*4882a593Smuzhiyun 				dma_sync_single_range_for_cpu(pdata->dev,
2581*4882a593Smuzhiyun 							rdata->rx.buf.dma_base,
2582*4882a593Smuzhiyun 							rdata->rx.buf.dma_off,
2583*4882a593Smuzhiyun 							rdata->rx.buf.dma_len,
2584*4882a593Smuzhiyun 							DMA_FROM_DEVICE);
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun 				skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2587*4882a593Smuzhiyun 						rdata->rx.buf.pa.pages,
2588*4882a593Smuzhiyun 						rdata->rx.buf.pa.pages_offset,
2589*4882a593Smuzhiyun 						buf2_len,
2590*4882a593Smuzhiyun 						rdata->rx.buf.dma_len);
2591*4882a593Smuzhiyun 				rdata->rx.buf.pa.pages = NULL;
2592*4882a593Smuzhiyun 			}
2593*4882a593Smuzhiyun 		}
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun skip_data:
2596*4882a593Smuzhiyun 		if (!last || context_next)
2597*4882a593Smuzhiyun 			goto read_again;
2598*4882a593Smuzhiyun 
2599*4882a593Smuzhiyun 		if (!skb || error) {
2600*4882a593Smuzhiyun 			dev_kfree_skb(skb);
2601*4882a593Smuzhiyun 			goto next_packet;
2602*4882a593Smuzhiyun 		}
2603*4882a593Smuzhiyun 
2604*4882a593Smuzhiyun 		/* Be sure we don't exceed the configured MTU */
2605*4882a593Smuzhiyun 		max_len = netdev->mtu + ETH_HLEN;
2606*4882a593Smuzhiyun 		if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2607*4882a593Smuzhiyun 		    (skb->protocol == htons(ETH_P_8021Q)))
2608*4882a593Smuzhiyun 			max_len += VLAN_HLEN;
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 		if (skb->len > max_len) {
2611*4882a593Smuzhiyun 			netif_err(pdata, rx_err, netdev,
2612*4882a593Smuzhiyun 				  "packet length exceeds configured MTU\n");
2613*4882a593Smuzhiyun 			dev_kfree_skb(skb);
2614*4882a593Smuzhiyun 			goto next_packet;
2615*4882a593Smuzhiyun 		}
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 		if (netif_msg_pktdata(pdata))
2618*4882a593Smuzhiyun 			xgbe_print_pkt(netdev, skb, false);
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 		skb_checksum_none_assert(skb);
2621*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(packet->attributes,
2622*4882a593Smuzhiyun 				   RX_PACKET_ATTRIBUTES, CSUM_DONE))
2623*4882a593Smuzhiyun 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(packet->attributes,
2626*4882a593Smuzhiyun 				   RX_PACKET_ATTRIBUTES, TNP)) {
2627*4882a593Smuzhiyun 			skb->encapsulation = 1;
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun 			if (XGMAC_GET_BITS(packet->attributes,
2630*4882a593Smuzhiyun 					   RX_PACKET_ATTRIBUTES, TNPCSUM_DONE))
2631*4882a593Smuzhiyun 				skb->csum_level = 1;
2632*4882a593Smuzhiyun 		}
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(packet->attributes,
2635*4882a593Smuzhiyun 				   RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2636*4882a593Smuzhiyun 			__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2637*4882a593Smuzhiyun 					       packet->vlan_ctag);
2638*4882a593Smuzhiyun 
2639*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(packet->attributes,
2640*4882a593Smuzhiyun 				   RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2641*4882a593Smuzhiyun 			u64 nsec;
2642*4882a593Smuzhiyun 
2643*4882a593Smuzhiyun 			nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2644*4882a593Smuzhiyun 						    packet->rx_tstamp);
2645*4882a593Smuzhiyun 			hwtstamps = skb_hwtstamps(skb);
2646*4882a593Smuzhiyun 			hwtstamps->hwtstamp = ns_to_ktime(nsec);
2647*4882a593Smuzhiyun 		}
2648*4882a593Smuzhiyun 
2649*4882a593Smuzhiyun 		if (XGMAC_GET_BITS(packet->attributes,
2650*4882a593Smuzhiyun 				   RX_PACKET_ATTRIBUTES, RSS_HASH))
2651*4882a593Smuzhiyun 			skb_set_hash(skb, packet->rss_hash,
2652*4882a593Smuzhiyun 				     packet->rss_hash_type);
2653*4882a593Smuzhiyun 
2654*4882a593Smuzhiyun 		skb->dev = netdev;
2655*4882a593Smuzhiyun 		skb->protocol = eth_type_trans(skb, netdev);
2656*4882a593Smuzhiyun 		skb_record_rx_queue(skb, channel->queue_index);
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun 		napi_gro_receive(napi, skb);
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun next_packet:
2661*4882a593Smuzhiyun 		packet_count++;
2662*4882a593Smuzhiyun 	}
2663*4882a593Smuzhiyun 
2664*4882a593Smuzhiyun 	/* Check if we need to save state before leaving */
2665*4882a593Smuzhiyun 	if (received && (!last || context_next)) {
2666*4882a593Smuzhiyun 		rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2667*4882a593Smuzhiyun 		rdata->state_saved = 1;
2668*4882a593Smuzhiyun 		rdata->state.skb = skb;
2669*4882a593Smuzhiyun 		rdata->state.len = len;
2670*4882a593Smuzhiyun 		rdata->state.error = error;
2671*4882a593Smuzhiyun 	}
2672*4882a593Smuzhiyun 
2673*4882a593Smuzhiyun 	DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 	return packet_count;
2676*4882a593Smuzhiyun }
2677*4882a593Smuzhiyun 
xgbe_one_poll(struct napi_struct * napi,int budget)2678*4882a593Smuzhiyun static int xgbe_one_poll(struct napi_struct *napi, int budget)
2679*4882a593Smuzhiyun {
2680*4882a593Smuzhiyun 	struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2681*4882a593Smuzhiyun 						    napi);
2682*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = channel->pdata;
2683*4882a593Smuzhiyun 	int processed = 0;
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2686*4882a593Smuzhiyun 
2687*4882a593Smuzhiyun 	/* Cleanup Tx ring first */
2688*4882a593Smuzhiyun 	xgbe_tx_poll(channel);
2689*4882a593Smuzhiyun 
2690*4882a593Smuzhiyun 	/* Process Rx ring next */
2691*4882a593Smuzhiyun 	processed = xgbe_rx_poll(channel, budget);
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun 	/* If we processed everything, we are done */
2694*4882a593Smuzhiyun 	if ((processed < budget) && napi_complete_done(napi, processed)) {
2695*4882a593Smuzhiyun 		/* Enable Tx and Rx interrupts */
2696*4882a593Smuzhiyun 		if (pdata->channel_irq_mode)
2697*4882a593Smuzhiyun 			xgbe_enable_rx_tx_int(pdata, channel);
2698*4882a593Smuzhiyun 		else
2699*4882a593Smuzhiyun 			enable_irq(channel->dma_irq);
2700*4882a593Smuzhiyun 	}
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun 	DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun 	return processed;
2705*4882a593Smuzhiyun }
2706*4882a593Smuzhiyun 
xgbe_all_poll(struct napi_struct * napi,int budget)2707*4882a593Smuzhiyun static int xgbe_all_poll(struct napi_struct *napi, int budget)
2708*4882a593Smuzhiyun {
2709*4882a593Smuzhiyun 	struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2710*4882a593Smuzhiyun 						   napi);
2711*4882a593Smuzhiyun 	struct xgbe_channel *channel;
2712*4882a593Smuzhiyun 	int ring_budget;
2713*4882a593Smuzhiyun 	int processed, last_processed;
2714*4882a593Smuzhiyun 	unsigned int i;
2715*4882a593Smuzhiyun 
2716*4882a593Smuzhiyun 	DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 	processed = 0;
2719*4882a593Smuzhiyun 	ring_budget = budget / pdata->rx_ring_count;
2720*4882a593Smuzhiyun 	do {
2721*4882a593Smuzhiyun 		last_processed = processed;
2722*4882a593Smuzhiyun 
2723*4882a593Smuzhiyun 		for (i = 0; i < pdata->channel_count; i++) {
2724*4882a593Smuzhiyun 			channel = pdata->channel[i];
2725*4882a593Smuzhiyun 
2726*4882a593Smuzhiyun 			/* Cleanup Tx ring first */
2727*4882a593Smuzhiyun 			xgbe_tx_poll(channel);
2728*4882a593Smuzhiyun 
2729*4882a593Smuzhiyun 			/* Process Rx ring next */
2730*4882a593Smuzhiyun 			if (ring_budget > (budget - processed))
2731*4882a593Smuzhiyun 				ring_budget = budget - processed;
2732*4882a593Smuzhiyun 			processed += xgbe_rx_poll(channel, ring_budget);
2733*4882a593Smuzhiyun 		}
2734*4882a593Smuzhiyun 	} while ((processed < budget) && (processed != last_processed));
2735*4882a593Smuzhiyun 
2736*4882a593Smuzhiyun 	/* If we processed everything, we are done */
2737*4882a593Smuzhiyun 	if ((processed < budget) && napi_complete_done(napi, processed)) {
2738*4882a593Smuzhiyun 		/* Enable Tx and Rx interrupts */
2739*4882a593Smuzhiyun 		xgbe_enable_rx_tx_ints(pdata);
2740*4882a593Smuzhiyun 	}
2741*4882a593Smuzhiyun 
2742*4882a593Smuzhiyun 	DBGPR("<--xgbe_all_poll: received = %d\n", processed);
2743*4882a593Smuzhiyun 
2744*4882a593Smuzhiyun 	return processed;
2745*4882a593Smuzhiyun }
2746*4882a593Smuzhiyun 
xgbe_dump_tx_desc(struct xgbe_prv_data * pdata,struct xgbe_ring * ring,unsigned int idx,unsigned int count,unsigned int flag)2747*4882a593Smuzhiyun void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2748*4882a593Smuzhiyun 		       unsigned int idx, unsigned int count, unsigned int flag)
2749*4882a593Smuzhiyun {
2750*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
2751*4882a593Smuzhiyun 	struct xgbe_ring_desc *rdesc;
2752*4882a593Smuzhiyun 
2753*4882a593Smuzhiyun 	while (count--) {
2754*4882a593Smuzhiyun 		rdata = XGBE_GET_DESC_DATA(ring, idx);
2755*4882a593Smuzhiyun 		rdesc = rdata->rdesc;
2756*4882a593Smuzhiyun 		netdev_dbg(pdata->netdev,
2757*4882a593Smuzhiyun 			   "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2758*4882a593Smuzhiyun 			   (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2759*4882a593Smuzhiyun 			   le32_to_cpu(rdesc->desc0),
2760*4882a593Smuzhiyun 			   le32_to_cpu(rdesc->desc1),
2761*4882a593Smuzhiyun 			   le32_to_cpu(rdesc->desc2),
2762*4882a593Smuzhiyun 			   le32_to_cpu(rdesc->desc3));
2763*4882a593Smuzhiyun 		idx++;
2764*4882a593Smuzhiyun 	}
2765*4882a593Smuzhiyun }
2766*4882a593Smuzhiyun 
xgbe_dump_rx_desc(struct xgbe_prv_data * pdata,struct xgbe_ring * ring,unsigned int idx)2767*4882a593Smuzhiyun void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2768*4882a593Smuzhiyun 		       unsigned int idx)
2769*4882a593Smuzhiyun {
2770*4882a593Smuzhiyun 	struct xgbe_ring_data *rdata;
2771*4882a593Smuzhiyun 	struct xgbe_ring_desc *rdesc;
2772*4882a593Smuzhiyun 
2773*4882a593Smuzhiyun 	rdata = XGBE_GET_DESC_DATA(ring, idx);
2774*4882a593Smuzhiyun 	rdesc = rdata->rdesc;
2775*4882a593Smuzhiyun 	netdev_dbg(pdata->netdev,
2776*4882a593Smuzhiyun 		   "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2777*4882a593Smuzhiyun 		   idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2778*4882a593Smuzhiyun 		   le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
2779*4882a593Smuzhiyun }
2780*4882a593Smuzhiyun 
xgbe_print_pkt(struct net_device * netdev,struct sk_buff * skb,bool tx_rx)2781*4882a593Smuzhiyun void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2782*4882a593Smuzhiyun {
2783*4882a593Smuzhiyun 	struct ethhdr *eth = (struct ethhdr *)skb->data;
2784*4882a593Smuzhiyun 	unsigned char buffer[128];
2785*4882a593Smuzhiyun 	unsigned int i;
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 	netdev_dbg(netdev, "\n************** SKB dump ****************\n");
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	netdev_dbg(netdev, "%s packet of %d bytes\n",
2790*4882a593Smuzhiyun 		   (tx_rx ? "TX" : "RX"), skb->len);
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun 	netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
2793*4882a593Smuzhiyun 	netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
2794*4882a593Smuzhiyun 	netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun 	for (i = 0; i < skb->len; i += 32) {
2797*4882a593Smuzhiyun 		unsigned int len = min(skb->len - i, 32U);
2798*4882a593Smuzhiyun 
2799*4882a593Smuzhiyun 		hex_dump_to_buffer(&skb->data[i], len, 32, 1,
2800*4882a593Smuzhiyun 				   buffer, sizeof(buffer), false);
2801*4882a593Smuzhiyun 		netdev_dbg(netdev, "  %#06x: %s\n", i, buffer);
2802*4882a593Smuzhiyun 	}
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	netdev_dbg(netdev, "\n************** SKB dump ****************\n");
2805*4882a593Smuzhiyun }
2806