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