2 * AMD 10Gb Ethernet driver
4 * This file is available to you under your choice of the following two
9 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
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.
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.
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/>.
24 * This file incorporates work covered by the following copyright and
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
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:
41 * The above copyright notice and this permission notice shall be included
42 * in all copies or substantial portions of the Software.
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.
57 * License 2: Modified BSD
59 * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
60 * All rights reserved.
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.
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.
84 * This file incorporates work covered by the following copyright and
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
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:
101 * The above copyright notice and this permission notice shall be included
102 * in all copies or substantial portions of the Software.
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.
117 #include <linux/module.h>
118 #include <linux/spinlock.h>
119 #include <linux/tcp.h>
120 #include <linux/if_vlan.h>
121 #include <linux/interrupt.h>
122 #include <net/busy_poll.h>
123 #include <linux/clk.h>
124 #include <linux/if_ether.h>
125 #include <linux/net_tstamp.h>
126 #include <linux/phy.h>
127 #include <net/vxlan.h>
130 #include "xgbe-common.h"
132 static unsigned int ecc_sec_info_threshold = 10;
133 static unsigned int ecc_sec_warn_threshold = 10000;
134 static unsigned int ecc_sec_period = 600;
135 static unsigned int ecc_ded_threshold = 2;
136 static unsigned int ecc_ded_period = 600;
138 #ifdef CONFIG_AMD_XGBE_HAVE_ECC
139 /* Only expose the ECC parameters if supported */
140 module_param(ecc_sec_info_threshold, uint, 0644);
141 MODULE_PARM_DESC(ecc_sec_info_threshold,
142 " ECC corrected error informational threshold setting");
144 module_param(ecc_sec_warn_threshold, uint, 0644);
145 MODULE_PARM_DESC(ecc_sec_warn_threshold,
146 " ECC corrected error warning threshold setting");
148 module_param(ecc_sec_period, uint, 0644);
149 MODULE_PARM_DESC(ecc_sec_period, " ECC corrected error period (in seconds)");
151 module_param(ecc_ded_threshold, uint, 0644);
152 MODULE_PARM_DESC(ecc_ded_threshold, " ECC detected error threshold setting");
154 module_param(ecc_ded_period, uint, 0644);
155 MODULE_PARM_DESC(ecc_ded_period, " ECC detected error period (in seconds)");
158 static int xgbe_one_poll(struct napi_struct *, int);
159 static int xgbe_all_poll(struct napi_struct *, int);
160 static void xgbe_stop(struct xgbe_prv_data *);
162 static void *xgbe_alloc_node(size_t size, int node)
166 mem = kzalloc_node(size, GFP_KERNEL, node);
168 mem = kzalloc(size, GFP_KERNEL);
173 static void xgbe_free_channels(struct xgbe_prv_data *pdata)
177 for (i = 0; i < ARRAY_SIZE(pdata->channel); i++) {
178 if (!pdata->channel[i])
181 kfree(pdata->channel[i]->rx_ring);
182 kfree(pdata->channel[i]->tx_ring);
183 kfree(pdata->channel[i]);
185 pdata->channel[i] = NULL;
188 pdata->channel_count = 0;
191 static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
193 struct xgbe_channel *channel;
194 struct xgbe_ring *ring;
195 unsigned int count, i;
199 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
200 for (i = 0; i < count; i++) {
201 /* Attempt to use a CPU on the node the device is on */
202 cpu = cpumask_local_spread(i, dev_to_node(pdata->dev));
204 /* Set the allocation node based on the returned CPU */
205 node = cpu_to_node(cpu);
207 channel = xgbe_alloc_node(sizeof(*channel), node);
210 pdata->channel[i] = channel;
212 snprintf(channel->name, sizeof(channel->name), "channel-%u", i);
213 channel->pdata = pdata;
214 channel->queue_index = i;
215 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
217 channel->node = node;
218 cpumask_set_cpu(cpu, &channel->affinity_mask);
220 if (pdata->per_channel_irq)
221 channel->dma_irq = pdata->channel_irq[i];
223 if (i < pdata->tx_ring_count) {
224 ring = xgbe_alloc_node(sizeof(*ring), node);
228 spin_lock_init(&ring->lock);
231 channel->tx_ring = ring;
234 if (i < pdata->rx_ring_count) {
235 ring = xgbe_alloc_node(sizeof(*ring), node);
239 spin_lock_init(&ring->lock);
242 channel->rx_ring = ring;
245 netif_dbg(pdata, drv, pdata->netdev,
246 "%s: cpu=%u, node=%d\n", channel->name, cpu, node);
248 netif_dbg(pdata, drv, pdata->netdev,
249 "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
250 channel->name, channel->dma_regs, channel->dma_irq,
251 channel->tx_ring, channel->rx_ring);
254 pdata->channel_count = count;
259 xgbe_free_channels(pdata);
264 static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
266 return (ring->rdesc_count - (ring->cur - ring->dirty));
269 static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring *ring)
271 return (ring->cur - ring->dirty);
274 static int xgbe_maybe_stop_tx_queue(struct xgbe_channel *channel,
275 struct xgbe_ring *ring, unsigned int count)
277 struct xgbe_prv_data *pdata = channel->pdata;
279 if (count > xgbe_tx_avail_desc(ring)) {
280 netif_info(pdata, drv, pdata->netdev,
281 "Tx queue stopped, not enough descriptors available\n");
282 netif_stop_subqueue(pdata->netdev, channel->queue_index);
283 ring->tx.queue_stopped = 1;
285 /* If we haven't notified the hardware because of xmit_more
286 * support, tell it now
288 if (ring->tx.xmit_more)
289 pdata->hw_if.tx_start_xmit(channel, ring);
291 return NETDEV_TX_BUSY;
297 static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
299 unsigned int rx_buf_size;
301 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
302 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
304 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
305 ~(XGBE_RX_BUF_ALIGN - 1);
310 static void xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata,
311 struct xgbe_channel *channel)
313 struct xgbe_hw_if *hw_if = &pdata->hw_if;
314 enum xgbe_int int_id;
316 if (channel->tx_ring && channel->rx_ring)
317 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
318 else if (channel->tx_ring)
319 int_id = XGMAC_INT_DMA_CH_SR_TI;
320 else if (channel->rx_ring)
321 int_id = XGMAC_INT_DMA_CH_SR_RI;
325 hw_if->enable_int(channel, int_id);
328 static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
332 for (i = 0; i < pdata->channel_count; i++)
333 xgbe_enable_rx_tx_int(pdata, pdata->channel[i]);
336 static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata,
337 struct xgbe_channel *channel)
339 struct xgbe_hw_if *hw_if = &pdata->hw_if;
340 enum xgbe_int int_id;
342 if (channel->tx_ring && channel->rx_ring)
343 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
344 else if (channel->tx_ring)
345 int_id = XGMAC_INT_DMA_CH_SR_TI;
346 else if (channel->rx_ring)
347 int_id = XGMAC_INT_DMA_CH_SR_RI;
351 hw_if->disable_int(channel, int_id);
354 static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
358 for (i = 0; i < pdata->channel_count; i++)
359 xgbe_disable_rx_tx_int(pdata, pdata->channel[i]);
362 static bool xgbe_ecc_sec(struct xgbe_prv_data *pdata, unsigned long *period,
363 unsigned int *count, const char *area)
365 if (time_before(jiffies, *period)) {
368 *period = jiffies + (ecc_sec_period * HZ);
372 if (*count > ecc_sec_info_threshold)
373 dev_warn_once(pdata->dev,
374 "%s ECC corrected errors exceed informational threshold\n",
377 if (*count > ecc_sec_warn_threshold) {
378 dev_warn_once(pdata->dev,
379 "%s ECC corrected errors exceed warning threshold\n",
387 static bool xgbe_ecc_ded(struct xgbe_prv_data *pdata, unsigned long *period,
388 unsigned int *count, const char *area)
390 if (time_before(jiffies, *period)) {
393 *period = jiffies + (ecc_ded_period * HZ);
397 if (*count > ecc_ded_threshold) {
398 netdev_alert(pdata->netdev,
399 "%s ECC detected errors exceed threshold\n",
407 static void xgbe_ecc_isr_task(unsigned long data)
409 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
410 unsigned int ecc_isr;
413 /* Mask status with only the interrupts we care about */
414 ecc_isr = XP_IOREAD(pdata, XP_ECC_ISR);
415 ecc_isr &= XP_IOREAD(pdata, XP_ECC_IER);
416 netif_dbg(pdata, intr, pdata->netdev, "ECC_ISR=%#010x\n", ecc_isr);
418 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_DED)) {
419 stop |= xgbe_ecc_ded(pdata, &pdata->tx_ded_period,
420 &pdata->tx_ded_count, "TX fifo");
423 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_DED)) {
424 stop |= xgbe_ecc_ded(pdata, &pdata->rx_ded_period,
425 &pdata->rx_ded_count, "RX fifo");
428 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_DED)) {
429 stop |= xgbe_ecc_ded(pdata, &pdata->desc_ded_period,
430 &pdata->desc_ded_count,
435 pdata->hw_if.disable_ecc_ded(pdata);
436 schedule_work(&pdata->stopdev_work);
440 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, TX_SEC)) {
441 if (xgbe_ecc_sec(pdata, &pdata->tx_sec_period,
442 &pdata->tx_sec_count, "TX fifo"))
443 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_TX);
446 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, RX_SEC))
447 if (xgbe_ecc_sec(pdata, &pdata->rx_sec_period,
448 &pdata->rx_sec_count, "RX fifo"))
449 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_RX);
451 if (XP_GET_BITS(ecc_isr, XP_ECC_ISR, DESC_SEC))
452 if (xgbe_ecc_sec(pdata, &pdata->desc_sec_period,
453 &pdata->desc_sec_count, "descriptor cache"))
454 pdata->hw_if.disable_ecc_sec(pdata, XGBE_ECC_SEC_DESC);
457 /* Clear all ECC interrupts */
458 XP_IOWRITE(pdata, XP_ECC_ISR, ecc_isr);
460 /* Reissue interrupt if status is not clear */
461 if (pdata->vdata->irq_reissue_support)
462 XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 1);
465 static irqreturn_t xgbe_ecc_isr(int irq, void *data)
467 struct xgbe_prv_data *pdata = data;
469 if (pdata->isr_as_tasklet)
470 tasklet_schedule(&pdata->tasklet_ecc);
472 xgbe_ecc_isr_task((unsigned long)pdata);
477 static void xgbe_isr_task(unsigned long data)
479 struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
480 struct xgbe_hw_if *hw_if = &pdata->hw_if;
481 struct xgbe_channel *channel;
482 unsigned int dma_isr, dma_ch_isr;
483 unsigned int mac_isr, mac_tssr, mac_mdioisr;
486 /* The DMA interrupt status register also reports MAC and MTL
487 * interrupts. So for polling mode, we just need to check for
488 * this register to be non-zero
490 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
494 netif_dbg(pdata, intr, pdata->netdev, "DMA_ISR=%#010x\n", dma_isr);
496 for (i = 0; i < pdata->channel_count; i++) {
497 if (!(dma_isr & (1 << i)))
500 channel = pdata->channel[i];
502 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
503 netif_dbg(pdata, intr, pdata->netdev, "DMA_CH%u_ISR=%#010x\n",
506 /* The TI or RI interrupt bits may still be set even if using
507 * per channel DMA interrupts. Check to be sure those are not
508 * enabled before using the private data napi structure.
510 if (!pdata->per_channel_irq &&
511 (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
512 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
513 if (napi_schedule_prep(&pdata->napi)) {
514 /* Disable Tx and Rx interrupts */
515 xgbe_disable_rx_tx_ints(pdata);
517 /* Turn on polling */
518 __napi_schedule(&pdata->napi);
521 /* Don't clear Rx/Tx status if doing per channel DMA
522 * interrupts, these will be cleared by the ISR for
523 * per channel DMA interrupts.
525 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
526 XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
529 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
530 pdata->ext_stats.rx_buffer_unavailable++;
532 /* Restart the device on a Fatal Bus Error */
533 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
534 schedule_work(&pdata->restart_work);
536 /* Clear interrupt signals */
537 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
540 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
541 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
543 netif_dbg(pdata, intr, pdata->netdev, "MAC_ISR=%#010x\n",
546 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
547 hw_if->tx_mmc_int(pdata);
549 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
550 hw_if->rx_mmc_int(pdata);
552 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
553 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
555 netif_dbg(pdata, intr, pdata->netdev,
556 "MAC_TSSR=%#010x\n", mac_tssr);
558 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
559 /* Read Tx Timestamp to clear interrupt */
561 hw_if->get_tx_tstamp(pdata);
562 queue_work(pdata->dev_workqueue,
563 &pdata->tx_tstamp_work);
567 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
568 mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
570 netif_dbg(pdata, intr, pdata->netdev,
571 "MAC_MDIOISR=%#010x\n", mac_mdioisr);
573 if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
575 complete(&pdata->mdio_complete);
580 /* If there is not a separate AN irq, handle it here */
581 if (pdata->dev_irq == pdata->an_irq)
582 pdata->phy_if.an_isr(pdata);
584 /* If there is not a separate ECC irq, handle it here */
585 if (pdata->vdata->ecc_support && (pdata->dev_irq == pdata->ecc_irq))
586 xgbe_ecc_isr_task((unsigned long)pdata);
588 /* If there is not a separate I2C irq, handle it here */
589 if (pdata->vdata->i2c_support && (pdata->dev_irq == pdata->i2c_irq))
590 pdata->i2c_if.i2c_isr(pdata);
592 /* Reissue interrupt if status is not clear */
593 if (pdata->vdata->irq_reissue_support) {
594 unsigned int reissue_mask;
596 reissue_mask = 1 << 0;
597 if (!pdata->per_channel_irq)
598 reissue_mask |= 0xffff << 4;
600 XP_IOWRITE(pdata, XP_INT_REISSUE_EN, reissue_mask);
604 static irqreturn_t xgbe_isr(int irq, void *data)
606 struct xgbe_prv_data *pdata = data;
608 if (pdata->isr_as_tasklet)
609 tasklet_schedule(&pdata->tasklet_dev);
611 xgbe_isr_task((unsigned long)pdata);
616 static irqreturn_t xgbe_dma_isr(int irq, void *data)
618 struct xgbe_channel *channel = data;
619 struct xgbe_prv_data *pdata = channel->pdata;
620 unsigned int dma_status;
622 /* Per channel DMA interrupts are enabled, so we use the per
623 * channel napi structure and not the private data napi structure
625 if (napi_schedule_prep(&channel->napi)) {
626 /* Disable Tx and Rx interrupts */
627 if (pdata->channel_irq_mode)
628 xgbe_disable_rx_tx_int(pdata, channel);
630 disable_irq_nosync(channel->dma_irq);
632 /* Turn on polling */
633 __napi_schedule_irqoff(&channel->napi);
636 /* Clear Tx/Rx signals */
638 XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
639 XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
640 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
645 static void xgbe_tx_timer(struct timer_list *t)
647 struct xgbe_channel *channel = from_timer(channel, t, tx_timer);
648 struct xgbe_prv_data *pdata = channel->pdata;
649 struct napi_struct *napi;
651 DBGPR("-->xgbe_tx_timer\n");
653 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
655 if (napi_schedule_prep(napi)) {
656 /* Disable Tx and Rx interrupts */
657 if (pdata->per_channel_irq)
658 if (pdata->channel_irq_mode)
659 xgbe_disable_rx_tx_int(pdata, channel);
661 disable_irq_nosync(channel->dma_irq);
663 xgbe_disable_rx_tx_ints(pdata);
665 /* Turn on polling */
666 __napi_schedule(napi);
669 channel->tx_timer_active = 0;
671 DBGPR("<--xgbe_tx_timer\n");
674 static void xgbe_service(struct work_struct *work)
676 struct xgbe_prv_data *pdata = container_of(work,
677 struct xgbe_prv_data,
680 pdata->phy_if.phy_status(pdata);
683 static void xgbe_service_timer(struct timer_list *t)
685 struct xgbe_prv_data *pdata = from_timer(pdata, t, service_timer);
687 queue_work(pdata->dev_workqueue, &pdata->service_work);
689 mod_timer(&pdata->service_timer, jiffies + HZ);
692 static void xgbe_init_timers(struct xgbe_prv_data *pdata)
694 struct xgbe_channel *channel;
697 timer_setup(&pdata->service_timer, xgbe_service_timer, 0);
699 for (i = 0; i < pdata->channel_count; i++) {
700 channel = pdata->channel[i];
701 if (!channel->tx_ring)
704 timer_setup(&channel->tx_timer, xgbe_tx_timer, 0);
708 static void xgbe_start_timers(struct xgbe_prv_data *pdata)
710 mod_timer(&pdata->service_timer, jiffies + HZ);
713 static void xgbe_stop_timers(struct xgbe_prv_data *pdata)
715 struct xgbe_channel *channel;
718 del_timer_sync(&pdata->service_timer);
720 for (i = 0; i < pdata->channel_count; i++) {
721 channel = pdata->channel[i];
722 if (!channel->tx_ring)
725 /* Deactivate the Tx timer */
726 del_timer_sync(&channel->tx_timer);
727 channel->tx_timer_active = 0;
731 void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
733 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
734 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
736 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
737 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
738 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
740 memset(hw_feat, 0, sizeof(*hw_feat));
742 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
744 /* Hardware feature register 0 */
745 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
746 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
747 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
748 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
749 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
750 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
751 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
752 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
753 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
754 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
755 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
756 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
758 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
759 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
760 hw_feat->vxn = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VXN);
762 /* Hardware feature register 1 */
763 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
765 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
767 hw_feat->adv_ts_hi = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADVTHWORD);
768 hw_feat->dma_width = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, ADDR64);
769 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
770 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
771 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
772 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
773 hw_feat->rss = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, RSSEN);
774 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
775 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
777 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
780 /* Hardware feature register 2 */
781 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
782 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
783 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
784 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
785 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
786 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
788 /* Translate the Hash Table size into actual number */
789 switch (hw_feat->hash_table_size) {
793 hw_feat->hash_table_size = 64;
796 hw_feat->hash_table_size = 128;
799 hw_feat->hash_table_size = 256;
803 /* Translate the address width setting into actual number */
804 switch (hw_feat->dma_width) {
806 hw_feat->dma_width = 32;
809 hw_feat->dma_width = 40;
812 hw_feat->dma_width = 48;
815 hw_feat->dma_width = 32;
818 /* The Queue, Channel and TC counts are zero based so increment them
819 * to get the actual number
823 hw_feat->rx_ch_cnt++;
824 hw_feat->tx_ch_cnt++;
827 /* Translate the fifo sizes into actual numbers */
828 hw_feat->rx_fifo_size = 1 << (hw_feat->rx_fifo_size + 7);
829 hw_feat->tx_fifo_size = 1 << (hw_feat->tx_fifo_size + 7);
831 if (netif_msg_probe(pdata)) {
832 dev_dbg(pdata->dev, "Hardware features:\n");
834 /* Hardware feature register 0 */
835 dev_dbg(pdata->dev, " 1GbE support : %s\n",
836 hw_feat->gmii ? "yes" : "no");
837 dev_dbg(pdata->dev, " VLAN hash filter : %s\n",
838 hw_feat->vlhash ? "yes" : "no");
839 dev_dbg(pdata->dev, " MDIO interface : %s\n",
840 hw_feat->sma ? "yes" : "no");
841 dev_dbg(pdata->dev, " Wake-up packet support : %s\n",
842 hw_feat->rwk ? "yes" : "no");
843 dev_dbg(pdata->dev, " Magic packet support : %s\n",
844 hw_feat->mgk ? "yes" : "no");
845 dev_dbg(pdata->dev, " Management counters : %s\n",
846 hw_feat->mmc ? "yes" : "no");
847 dev_dbg(pdata->dev, " ARP offload : %s\n",
848 hw_feat->aoe ? "yes" : "no");
849 dev_dbg(pdata->dev, " IEEE 1588-2008 Timestamp : %s\n",
850 hw_feat->ts ? "yes" : "no");
851 dev_dbg(pdata->dev, " Energy Efficient Ethernet : %s\n",
852 hw_feat->eee ? "yes" : "no");
853 dev_dbg(pdata->dev, " TX checksum offload : %s\n",
854 hw_feat->tx_coe ? "yes" : "no");
855 dev_dbg(pdata->dev, " RX checksum offload : %s\n",
856 hw_feat->rx_coe ? "yes" : "no");
857 dev_dbg(pdata->dev, " Additional MAC addresses : %u\n",
859 dev_dbg(pdata->dev, " Timestamp source : %s\n",
860 (hw_feat->ts_src == 1) ? "internal" :
861 (hw_feat->ts_src == 2) ? "external" :
862 (hw_feat->ts_src == 3) ? "internal/external" : "n/a");
863 dev_dbg(pdata->dev, " SA/VLAN insertion : %s\n",
864 hw_feat->sa_vlan_ins ? "yes" : "no");
865 dev_dbg(pdata->dev, " VXLAN/NVGRE support : %s\n",
866 hw_feat->vxn ? "yes" : "no");
868 /* Hardware feature register 1 */
869 dev_dbg(pdata->dev, " RX fifo size : %u\n",
870 hw_feat->rx_fifo_size);
871 dev_dbg(pdata->dev, " TX fifo size : %u\n",
872 hw_feat->tx_fifo_size);
873 dev_dbg(pdata->dev, " IEEE 1588 high word : %s\n",
874 hw_feat->adv_ts_hi ? "yes" : "no");
875 dev_dbg(pdata->dev, " DMA width : %u\n",
877 dev_dbg(pdata->dev, " Data Center Bridging : %s\n",
878 hw_feat->dcb ? "yes" : "no");
879 dev_dbg(pdata->dev, " Split header : %s\n",
880 hw_feat->sph ? "yes" : "no");
881 dev_dbg(pdata->dev, " TCP Segmentation Offload : %s\n",
882 hw_feat->tso ? "yes" : "no");
883 dev_dbg(pdata->dev, " Debug memory interface : %s\n",
884 hw_feat->dma_debug ? "yes" : "no");
885 dev_dbg(pdata->dev, " Receive Side Scaling : %s\n",
886 hw_feat->rss ? "yes" : "no");
887 dev_dbg(pdata->dev, " Traffic Class count : %u\n",
889 dev_dbg(pdata->dev, " Hash table size : %u\n",
890 hw_feat->hash_table_size);
891 dev_dbg(pdata->dev, " L3/L4 Filters : %u\n",
892 hw_feat->l3l4_filter_num);
894 /* Hardware feature register 2 */
895 dev_dbg(pdata->dev, " RX queue count : %u\n",
897 dev_dbg(pdata->dev, " TX queue count : %u\n",
899 dev_dbg(pdata->dev, " RX DMA channel count : %u\n",
901 dev_dbg(pdata->dev, " TX DMA channel count : %u\n",
903 dev_dbg(pdata->dev, " PPS outputs : %u\n",
904 hw_feat->pps_out_num);
905 dev_dbg(pdata->dev, " Auxiliary snapshot inputs : %u\n",
906 hw_feat->aux_snap_num);
910 static void xgbe_disable_vxlan_offloads(struct xgbe_prv_data *pdata)
912 struct net_device *netdev = pdata->netdev;
914 if (!pdata->vxlan_offloads_set)
917 netdev_info(netdev, "disabling VXLAN offloads\n");
919 netdev->hw_enc_features &= ~(NETIF_F_SG |
926 NETIF_F_GSO_UDP_TUNNEL |
927 NETIF_F_GSO_UDP_TUNNEL_CSUM);
929 netdev->features &= ~(NETIF_F_GSO_UDP_TUNNEL |
930 NETIF_F_GSO_UDP_TUNNEL_CSUM);
932 pdata->vxlan_offloads_set = 0;
935 static void xgbe_disable_vxlan_hw(struct xgbe_prv_data *pdata)
937 if (!pdata->vxlan_port_set)
940 pdata->hw_if.disable_vxlan(pdata);
942 pdata->vxlan_port_set = 0;
943 pdata->vxlan_port = 0;
946 static void xgbe_disable_vxlan_accel(struct xgbe_prv_data *pdata)
948 xgbe_disable_vxlan_offloads(pdata);
950 xgbe_disable_vxlan_hw(pdata);
953 static void xgbe_enable_vxlan_offloads(struct xgbe_prv_data *pdata)
955 struct net_device *netdev = pdata->netdev;
957 if (pdata->vxlan_offloads_set)
960 netdev_info(netdev, "enabling VXLAN offloads\n");
962 netdev->hw_enc_features |= NETIF_F_SG |
969 pdata->vxlan_features;
971 netdev->features |= pdata->vxlan_features;
973 pdata->vxlan_offloads_set = 1;
976 static void xgbe_enable_vxlan_hw(struct xgbe_prv_data *pdata)
978 struct xgbe_vxlan_data *vdata;
980 if (pdata->vxlan_port_set)
983 if (list_empty(&pdata->vxlan_ports))
986 vdata = list_first_entry(&pdata->vxlan_ports,
987 struct xgbe_vxlan_data, list);
989 pdata->vxlan_port_set = 1;
990 pdata->vxlan_port = be16_to_cpu(vdata->port);
992 pdata->hw_if.enable_vxlan(pdata);
995 static void xgbe_enable_vxlan_accel(struct xgbe_prv_data *pdata)
997 /* VXLAN acceleration desired? */
998 if (!pdata->vxlan_features)
1001 /* VXLAN acceleration possible? */
1002 if (pdata->vxlan_force_disable)
1005 xgbe_enable_vxlan_hw(pdata);
1007 xgbe_enable_vxlan_offloads(pdata);
1010 static void xgbe_reset_vxlan_accel(struct xgbe_prv_data *pdata)
1012 xgbe_disable_vxlan_hw(pdata);
1014 if (pdata->vxlan_features)
1015 xgbe_enable_vxlan_offloads(pdata);
1017 pdata->vxlan_force_disable = 0;
1020 static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
1022 struct xgbe_channel *channel;
1025 if (pdata->per_channel_irq) {
1026 for (i = 0; i < pdata->channel_count; i++) {
1027 channel = pdata->channel[i];
1029 netif_napi_add(pdata->netdev, &channel->napi,
1030 xgbe_one_poll, NAPI_POLL_WEIGHT);
1032 napi_enable(&channel->napi);
1036 netif_napi_add(pdata->netdev, &pdata->napi,
1037 xgbe_all_poll, NAPI_POLL_WEIGHT);
1039 napi_enable(&pdata->napi);
1043 static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
1045 struct xgbe_channel *channel;
1048 if (pdata->per_channel_irq) {
1049 for (i = 0; i < pdata->channel_count; i++) {
1050 channel = pdata->channel[i];
1051 napi_disable(&channel->napi);
1054 netif_napi_del(&channel->napi);
1057 napi_disable(&pdata->napi);
1060 netif_napi_del(&pdata->napi);
1064 static int xgbe_request_irqs(struct xgbe_prv_data *pdata)
1066 struct xgbe_channel *channel;
1067 struct net_device *netdev = pdata->netdev;
1071 tasklet_init(&pdata->tasklet_dev, xgbe_isr_task, (unsigned long)pdata);
1072 tasklet_init(&pdata->tasklet_ecc, xgbe_ecc_isr_task,
1073 (unsigned long)pdata);
1075 ret = devm_request_irq(pdata->dev, pdata->dev_irq, xgbe_isr, 0,
1076 netdev_name(netdev), pdata);
1078 netdev_alert(netdev, "error requesting irq %d\n",
1083 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq)) {
1084 ret = devm_request_irq(pdata->dev, pdata->ecc_irq, xgbe_ecc_isr,
1085 0, pdata->ecc_name, pdata);
1087 netdev_alert(netdev, "error requesting ecc irq %d\n",
1093 if (!pdata->per_channel_irq)
1096 for (i = 0; i < pdata->channel_count; i++) {
1097 channel = pdata->channel[i];
1098 snprintf(channel->dma_irq_name,
1099 sizeof(channel->dma_irq_name) - 1,
1100 "%s-TxRx-%u", netdev_name(netdev),
1101 channel->queue_index);
1103 ret = devm_request_irq(pdata->dev, channel->dma_irq,
1105 channel->dma_irq_name, channel);
1107 netdev_alert(netdev, "error requesting irq %d\n",
1112 irq_set_affinity_hint(channel->dma_irq,
1113 &channel->affinity_mask);
1119 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
1120 for (i--; i < pdata->channel_count; i--) {
1121 channel = pdata->channel[i];
1123 irq_set_affinity_hint(channel->dma_irq, NULL);
1124 devm_free_irq(pdata->dev, channel->dma_irq, channel);
1127 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
1128 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
1131 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
1136 static void xgbe_free_irqs(struct xgbe_prv_data *pdata)
1138 struct xgbe_channel *channel;
1141 devm_free_irq(pdata->dev, pdata->dev_irq, pdata);
1143 if (pdata->vdata->ecc_support && (pdata->dev_irq != pdata->ecc_irq))
1144 devm_free_irq(pdata->dev, pdata->ecc_irq, pdata);
1146 if (!pdata->per_channel_irq)
1149 for (i = 0; i < pdata->channel_count; i++) {
1150 channel = pdata->channel[i];
1152 irq_set_affinity_hint(channel->dma_irq, NULL);
1153 devm_free_irq(pdata->dev, channel->dma_irq, channel);
1157 void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
1159 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1161 DBGPR("-->xgbe_init_tx_coalesce\n");
1163 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
1164 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
1166 hw_if->config_tx_coalesce(pdata);
1168 DBGPR("<--xgbe_init_tx_coalesce\n");
1171 void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
1173 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1175 DBGPR("-->xgbe_init_rx_coalesce\n");
1177 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
1178 pdata->rx_usecs = XGMAC_INIT_DMA_RX_USECS;
1179 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
1181 hw_if->config_rx_coalesce(pdata);
1183 DBGPR("<--xgbe_init_rx_coalesce\n");
1186 static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
1188 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1189 struct xgbe_ring *ring;
1190 struct xgbe_ring_data *rdata;
1193 DBGPR("-->xgbe_free_tx_data\n");
1195 for (i = 0; i < pdata->channel_count; i++) {
1196 ring = pdata->channel[i]->tx_ring;
1200 for (j = 0; j < ring->rdesc_count; j++) {
1201 rdata = XGBE_GET_DESC_DATA(ring, j);
1202 desc_if->unmap_rdata(pdata, rdata);
1206 DBGPR("<--xgbe_free_tx_data\n");
1209 static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
1211 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1212 struct xgbe_ring *ring;
1213 struct xgbe_ring_data *rdata;
1216 DBGPR("-->xgbe_free_rx_data\n");
1218 for (i = 0; i < pdata->channel_count; i++) {
1219 ring = pdata->channel[i]->rx_ring;
1223 for (j = 0; j < ring->rdesc_count; j++) {
1224 rdata = XGBE_GET_DESC_DATA(ring, j);
1225 desc_if->unmap_rdata(pdata, rdata);
1229 DBGPR("<--xgbe_free_rx_data\n");
1232 static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
1234 pdata->phy_link = -1;
1235 pdata->phy_speed = SPEED_UNKNOWN;
1237 return pdata->phy_if.phy_reset(pdata);
1240 int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
1242 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1243 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1244 unsigned long flags;
1246 DBGPR("-->xgbe_powerdown\n");
1248 if (!netif_running(netdev) ||
1249 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
1250 netdev_alert(netdev, "Device is already powered down\n");
1251 DBGPR("<--xgbe_powerdown\n");
1255 spin_lock_irqsave(&pdata->lock, flags);
1257 if (caller == XGMAC_DRIVER_CONTEXT)
1258 netif_device_detach(netdev);
1260 netif_tx_stop_all_queues(netdev);
1262 xgbe_stop_timers(pdata);
1263 flush_workqueue(pdata->dev_workqueue);
1265 hw_if->powerdown_tx(pdata);
1266 hw_if->powerdown_rx(pdata);
1268 xgbe_napi_disable(pdata, 0);
1270 pdata->power_down = 1;
1272 spin_unlock_irqrestore(&pdata->lock, flags);
1274 DBGPR("<--xgbe_powerdown\n");
1279 int xgbe_powerup(struct net_device *netdev, unsigned int caller)
1281 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1282 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1283 unsigned long flags;
1285 DBGPR("-->xgbe_powerup\n");
1287 if (!netif_running(netdev) ||
1288 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
1289 netdev_alert(netdev, "Device is already powered up\n");
1290 DBGPR("<--xgbe_powerup\n");
1294 spin_lock_irqsave(&pdata->lock, flags);
1296 pdata->power_down = 0;
1298 xgbe_napi_enable(pdata, 0);
1300 hw_if->powerup_tx(pdata);
1301 hw_if->powerup_rx(pdata);
1303 if (caller == XGMAC_DRIVER_CONTEXT)
1304 netif_device_attach(netdev);
1306 netif_tx_start_all_queues(netdev);
1308 xgbe_start_timers(pdata);
1310 spin_unlock_irqrestore(&pdata->lock, flags);
1312 DBGPR("<--xgbe_powerup\n");
1317 static void xgbe_free_memory(struct xgbe_prv_data *pdata)
1319 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1321 /* Free the ring descriptors and buffers */
1322 desc_if->free_ring_resources(pdata);
1324 /* Free the channel and ring structures */
1325 xgbe_free_channels(pdata);
1328 static int xgbe_alloc_memory(struct xgbe_prv_data *pdata)
1330 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1331 struct net_device *netdev = pdata->netdev;
1334 if (pdata->new_tx_ring_count) {
1335 pdata->tx_ring_count = pdata->new_tx_ring_count;
1336 pdata->tx_q_count = pdata->tx_ring_count;
1337 pdata->new_tx_ring_count = 0;
1340 if (pdata->new_rx_ring_count) {
1341 pdata->rx_ring_count = pdata->new_rx_ring_count;
1342 pdata->new_rx_ring_count = 0;
1345 /* Calculate the Rx buffer size before allocating rings */
1346 pdata->rx_buf_size = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1348 /* Allocate the channel and ring structures */
1349 ret = xgbe_alloc_channels(pdata);
1353 /* Allocate the ring descriptors and buffers */
1354 ret = desc_if->alloc_ring_resources(pdata);
1358 /* Initialize the service and Tx timers */
1359 xgbe_init_timers(pdata);
1364 xgbe_free_memory(pdata);
1369 static int xgbe_start(struct xgbe_prv_data *pdata)
1371 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1372 struct xgbe_phy_if *phy_if = &pdata->phy_if;
1373 struct net_device *netdev = pdata->netdev;
1377 /* Set the number of queues */
1378 ret = netif_set_real_num_tx_queues(netdev, pdata->tx_ring_count);
1380 netdev_err(netdev, "error setting real tx queue count\n");
1384 ret = netif_set_real_num_rx_queues(netdev, pdata->rx_ring_count);
1386 netdev_err(netdev, "error setting real rx queue count\n");
1390 /* Set RSS lookup table data for programming */
1391 for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
1392 XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
1393 i % pdata->rx_ring_count);
1395 ret = hw_if->init(pdata);
1399 xgbe_napi_enable(pdata, 1);
1401 ret = xgbe_request_irqs(pdata);
1405 ret = phy_if->phy_start(pdata);
1409 hw_if->enable_tx(pdata);
1410 hw_if->enable_rx(pdata);
1412 udp_tunnel_get_rx_info(netdev);
1414 netif_tx_start_all_queues(netdev);
1416 xgbe_start_timers(pdata);
1417 queue_work(pdata->dev_workqueue, &pdata->service_work);
1419 clear_bit(XGBE_STOPPED, &pdata->dev_state);
1424 xgbe_free_irqs(pdata);
1427 xgbe_napi_disable(pdata, 1);
1434 static void xgbe_stop(struct xgbe_prv_data *pdata)
1436 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1437 struct xgbe_phy_if *phy_if = &pdata->phy_if;
1438 struct xgbe_channel *channel;
1439 struct net_device *netdev = pdata->netdev;
1440 struct netdev_queue *txq;
1443 DBGPR("-->xgbe_stop\n");
1445 if (test_bit(XGBE_STOPPED, &pdata->dev_state))
1448 netif_tx_stop_all_queues(netdev);
1449 netif_carrier_off(pdata->netdev);
1451 xgbe_stop_timers(pdata);
1452 flush_workqueue(pdata->dev_workqueue);
1454 xgbe_reset_vxlan_accel(pdata);
1456 hw_if->disable_tx(pdata);
1457 hw_if->disable_rx(pdata);
1459 phy_if->phy_stop(pdata);
1461 xgbe_free_irqs(pdata);
1463 xgbe_napi_disable(pdata, 1);
1467 for (i = 0; i < pdata->channel_count; i++) {
1468 channel = pdata->channel[i];
1469 if (!channel->tx_ring)
1472 txq = netdev_get_tx_queue(netdev, channel->queue_index);
1473 netdev_tx_reset_queue(txq);
1476 set_bit(XGBE_STOPPED, &pdata->dev_state);
1478 DBGPR("<--xgbe_stop\n");
1481 static void xgbe_stopdev(struct work_struct *work)
1483 struct xgbe_prv_data *pdata = container_of(work,
1484 struct xgbe_prv_data,
1491 xgbe_free_tx_data(pdata);
1492 xgbe_free_rx_data(pdata);
1496 netdev_alert(pdata->netdev, "device stopped\n");
1499 void xgbe_full_restart_dev(struct xgbe_prv_data *pdata)
1501 /* If not running, "restart" will happen on open */
1502 if (!netif_running(pdata->netdev))
1507 xgbe_free_memory(pdata);
1508 xgbe_alloc_memory(pdata);
1513 void xgbe_restart_dev(struct xgbe_prv_data *pdata)
1515 /* If not running, "restart" will happen on open */
1516 if (!netif_running(pdata->netdev))
1521 xgbe_free_tx_data(pdata);
1522 xgbe_free_rx_data(pdata);
1527 static void xgbe_restart(struct work_struct *work)
1529 struct xgbe_prv_data *pdata = container_of(work,
1530 struct xgbe_prv_data,
1535 xgbe_restart_dev(pdata);
1540 static void xgbe_tx_tstamp(struct work_struct *work)
1542 struct xgbe_prv_data *pdata = container_of(work,
1543 struct xgbe_prv_data,
1545 struct skb_shared_hwtstamps hwtstamps;
1547 unsigned long flags;
1549 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1550 if (!pdata->tx_tstamp_skb)
1553 if (pdata->tx_tstamp) {
1554 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1557 memset(&hwtstamps, 0, sizeof(hwtstamps));
1558 hwtstamps.hwtstamp = ns_to_ktime(nsec);
1559 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
1562 dev_kfree_skb_any(pdata->tx_tstamp_skb);
1564 pdata->tx_tstamp_skb = NULL;
1567 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1570 static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
1571 struct ifreq *ifreq)
1573 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
1574 sizeof(pdata->tstamp_config)))
1580 static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
1581 struct ifreq *ifreq)
1583 struct hwtstamp_config config;
1584 unsigned int mac_tscr;
1586 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
1594 switch (config.tx_type) {
1595 case HWTSTAMP_TX_OFF:
1598 case HWTSTAMP_TX_ON:
1599 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1606 switch (config.rx_filter) {
1607 case HWTSTAMP_FILTER_NONE:
1610 case HWTSTAMP_FILTER_NTP_ALL:
1611 case HWTSTAMP_FILTER_ALL:
1612 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
1613 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1616 /* PTP v2, UDP, any kind of event packet */
1617 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1618 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1619 /* PTP v1, UDP, any kind of event packet */
1620 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1621 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1622 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1623 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1624 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1627 /* PTP v2, UDP, Sync packet */
1628 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1629 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1630 /* PTP v1, UDP, Sync packet */
1631 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1632 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1633 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1634 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1635 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1638 /* PTP v2, UDP, Delay_req packet */
1639 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1640 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1641 /* PTP v1, UDP, Delay_req packet */
1642 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1643 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1644 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1645 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1646 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1647 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1650 /* 802.AS1, Ethernet, any kind of event packet */
1651 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1652 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1653 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1654 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1657 /* 802.AS1, Ethernet, Sync packet */
1658 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1659 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1660 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1661 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1664 /* 802.AS1, Ethernet, Delay_req packet */
1665 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1666 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
1667 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1668 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1669 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1672 /* PTP v2/802.AS1, any layer, any kind of event packet */
1673 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1674 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1675 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1676 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1677 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1678 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1679 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1682 /* PTP v2/802.AS1, any layer, Sync packet */
1683 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1684 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1685 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1686 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1687 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1688 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1689 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1692 /* PTP v2/802.AS1, any layer, Delay_req packet */
1693 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1694 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1695 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1696 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1697 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1698 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1699 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1700 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1707 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1709 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1714 static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1715 struct sk_buff *skb,
1716 struct xgbe_packet_data *packet)
1718 unsigned long flags;
1720 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1721 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1722 if (pdata->tx_tstamp_skb) {
1723 /* Another timestamp in progress, ignore this one */
1724 XGMAC_SET_BITS(packet->attributes,
1725 TX_PACKET_ATTRIBUTES, PTP, 0);
1727 pdata->tx_tstamp_skb = skb_get(skb);
1728 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1730 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1733 skb_tx_timestamp(skb);
1736 static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1738 if (skb_vlan_tag_present(skb))
1739 packet->vlan_ctag = skb_vlan_tag_get(skb);
1742 static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1746 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1750 ret = skb_cow_head(skb, 0);
1754 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, VXLAN)) {
1755 packet->header_len = skb_inner_transport_offset(skb) +
1756 inner_tcp_hdrlen(skb);
1757 packet->tcp_header_len = inner_tcp_hdrlen(skb);
1759 packet->header_len = skb_transport_offset(skb) +
1761 packet->tcp_header_len = tcp_hdrlen(skb);
1763 packet->tcp_payload_len = skb->len - packet->header_len;
1764 packet->mss = skb_shinfo(skb)->gso_size;
1766 DBGPR(" packet->header_len=%u\n", packet->header_len);
1767 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1768 packet->tcp_header_len, packet->tcp_payload_len);
1769 DBGPR(" packet->mss=%u\n", packet->mss);
1771 /* Update the number of packets that will ultimately be transmitted
1772 * along with the extra bytes for each extra packet
1774 packet->tx_packets = skb_shinfo(skb)->gso_segs;
1775 packet->tx_bytes += (packet->tx_packets - 1) * packet->header_len;
1780 static bool xgbe_is_vxlan(struct xgbe_prv_data *pdata, struct sk_buff *skb)
1782 struct xgbe_vxlan_data *vdata;
1784 if (pdata->vxlan_force_disable)
1787 if (!skb->encapsulation)
1790 if (skb->ip_summed != CHECKSUM_PARTIAL)
1793 switch (skb->protocol) {
1794 case htons(ETH_P_IP):
1795 if (ip_hdr(skb)->protocol != IPPROTO_UDP)
1799 case htons(ETH_P_IPV6):
1800 if (ipv6_hdr(skb)->nexthdr != IPPROTO_UDP)
1808 /* See if we have the UDP port in our list */
1809 list_for_each_entry(vdata, &pdata->vxlan_ports, list) {
1810 if ((skb->protocol == htons(ETH_P_IP)) &&
1811 (vdata->sa_family == AF_INET) &&
1812 (vdata->port == udp_hdr(skb)->dest))
1814 else if ((skb->protocol == htons(ETH_P_IPV6)) &&
1815 (vdata->sa_family == AF_INET6) &&
1816 (vdata->port == udp_hdr(skb)->dest))
1823 static int xgbe_is_tso(struct sk_buff *skb)
1825 if (skb->ip_summed != CHECKSUM_PARTIAL)
1828 if (!skb_is_gso(skb))
1831 DBGPR(" TSO packet to be processed\n");
1836 static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1837 struct xgbe_ring *ring, struct sk_buff *skb,
1838 struct xgbe_packet_data *packet)
1840 struct skb_frag_struct *frag;
1841 unsigned int context_desc;
1848 packet->rdesc_count = 0;
1850 packet->tx_packets = 1;
1851 packet->tx_bytes = skb->len;
1853 if (xgbe_is_tso(skb)) {
1854 /* TSO requires an extra descriptor if mss is different */
1855 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1857 packet->rdesc_count++;
1860 /* TSO requires an extra descriptor for TSO header */
1861 packet->rdesc_count++;
1863 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1865 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1867 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1868 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1871 if (xgbe_is_vxlan(pdata, skb))
1872 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1875 if (skb_vlan_tag_present(skb)) {
1876 /* VLAN requires an extra descriptor if tag is different */
1877 if (skb_vlan_tag_get(skb) != ring->tx.cur_vlan_ctag)
1878 /* We can share with the TSO context descriptor */
1879 if (!context_desc) {
1881 packet->rdesc_count++;
1884 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1888 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1889 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1890 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1893 for (len = skb_headlen(skb); len;) {
1894 packet->rdesc_count++;
1895 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1898 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1899 frag = &skb_shinfo(skb)->frags[i];
1900 for (len = skb_frag_size(frag); len; ) {
1901 packet->rdesc_count++;
1902 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1907 static int xgbe_open(struct net_device *netdev)
1909 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1912 /* Create the various names based on netdev name */
1913 snprintf(pdata->an_name, sizeof(pdata->an_name) - 1, "%s-pcs",
1914 netdev_name(netdev));
1916 snprintf(pdata->ecc_name, sizeof(pdata->ecc_name) - 1, "%s-ecc",
1917 netdev_name(netdev));
1919 snprintf(pdata->i2c_name, sizeof(pdata->i2c_name) - 1, "%s-i2c",
1920 netdev_name(netdev));
1922 /* Create workqueues */
1923 pdata->dev_workqueue =
1924 create_singlethread_workqueue(netdev_name(netdev));
1925 if (!pdata->dev_workqueue) {
1926 netdev_err(netdev, "device workqueue creation failed\n");
1930 pdata->an_workqueue =
1931 create_singlethread_workqueue(pdata->an_name);
1932 if (!pdata->an_workqueue) {
1933 netdev_err(netdev, "phy workqueue creation failed\n");
1938 /* Reset the phy settings */
1939 ret = xgbe_phy_reset(pdata);
1943 /* Enable the clocks */
1944 ret = clk_prepare_enable(pdata->sysclk);
1946 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
1950 ret = clk_prepare_enable(pdata->ptpclk);
1952 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1956 INIT_WORK(&pdata->service_work, xgbe_service);
1957 INIT_WORK(&pdata->restart_work, xgbe_restart);
1958 INIT_WORK(&pdata->stopdev_work, xgbe_stopdev);
1959 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
1961 ret = xgbe_alloc_memory(pdata);
1965 ret = xgbe_start(pdata);
1969 clear_bit(XGBE_DOWN, &pdata->dev_state);
1974 xgbe_free_memory(pdata);
1977 clk_disable_unprepare(pdata->ptpclk);
1980 clk_disable_unprepare(pdata->sysclk);
1983 destroy_workqueue(pdata->an_workqueue);
1986 destroy_workqueue(pdata->dev_workqueue);
1991 static int xgbe_close(struct net_device *netdev)
1993 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1995 /* Stop the device */
1998 xgbe_free_memory(pdata);
2000 /* Disable the clocks */
2001 clk_disable_unprepare(pdata->ptpclk);
2002 clk_disable_unprepare(pdata->sysclk);
2004 flush_workqueue(pdata->an_workqueue);
2005 destroy_workqueue(pdata->an_workqueue);
2007 flush_workqueue(pdata->dev_workqueue);
2008 destroy_workqueue(pdata->dev_workqueue);
2010 set_bit(XGBE_DOWN, &pdata->dev_state);
2015 static netdev_tx_t xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
2017 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2018 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2019 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2020 struct xgbe_channel *channel;
2021 struct xgbe_ring *ring;
2022 struct xgbe_packet_data *packet;
2023 struct netdev_queue *txq;
2026 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
2028 channel = pdata->channel[skb->queue_mapping];
2029 txq = netdev_get_tx_queue(netdev, channel->queue_index);
2030 ring = channel->tx_ring;
2031 packet = &ring->packet_data;
2035 if (skb->len == 0) {
2036 netif_err(pdata, tx_err, netdev,
2037 "empty skb received from stack\n");
2038 dev_kfree_skb_any(skb);
2039 goto tx_netdev_return;
2042 /* Calculate preliminary packet info */
2043 memset(packet, 0, sizeof(*packet));
2044 xgbe_packet_info(pdata, ring, skb, packet);
2046 /* Check that there are enough descriptors available */
2047 ret = xgbe_maybe_stop_tx_queue(channel, ring, packet->rdesc_count);
2049 goto tx_netdev_return;
2051 ret = xgbe_prep_tso(skb, packet);
2053 netif_err(pdata, tx_err, netdev,
2054 "error processing TSO packet\n");
2055 dev_kfree_skb_any(skb);
2056 goto tx_netdev_return;
2058 xgbe_prep_vlan(skb, packet);
2060 if (!desc_if->map_tx_skb(channel, skb)) {
2061 dev_kfree_skb_any(skb);
2062 goto tx_netdev_return;
2065 xgbe_prep_tx_tstamp(pdata, skb, packet);
2067 /* Report on the actual number of bytes (to be) sent */
2068 netdev_tx_sent_queue(txq, packet->tx_bytes);
2070 /* Configure required descriptor fields for transmission */
2071 hw_if->dev_xmit(channel);
2073 if (netif_msg_pktdata(pdata))
2074 xgbe_print_pkt(netdev, skb, true);
2076 /* Stop the queue in advance if there may not be enough descriptors */
2077 xgbe_maybe_stop_tx_queue(channel, ring, XGBE_TX_MAX_DESCS);
2085 static void xgbe_set_rx_mode(struct net_device *netdev)
2087 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2088 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2090 DBGPR("-->xgbe_set_rx_mode\n");
2092 hw_if->config_rx_mode(pdata);
2094 DBGPR("<--xgbe_set_rx_mode\n");
2097 static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
2099 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2100 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2101 struct sockaddr *saddr = addr;
2103 DBGPR("-->xgbe_set_mac_address\n");
2105 if (!is_valid_ether_addr(saddr->sa_data))
2106 return -EADDRNOTAVAIL;
2108 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
2110 hw_if->set_mac_address(pdata, netdev->dev_addr);
2112 DBGPR("<--xgbe_set_mac_address\n");
2117 static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
2119 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2124 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
2128 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
2138 static int xgbe_change_mtu(struct net_device *netdev, int mtu)
2140 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2143 DBGPR("-->xgbe_change_mtu\n");
2145 ret = xgbe_calc_rx_buf_size(netdev, mtu);
2149 pdata->rx_buf_size = ret;
2152 xgbe_restart_dev(pdata);
2154 DBGPR("<--xgbe_change_mtu\n");
2159 static void xgbe_tx_timeout(struct net_device *netdev)
2161 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2163 netdev_warn(netdev, "tx timeout, device restarting\n");
2164 schedule_work(&pdata->restart_work);
2167 static void xgbe_get_stats64(struct net_device *netdev,
2168 struct rtnl_link_stats64 *s)
2170 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2171 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
2173 DBGPR("-->%s\n", __func__);
2175 pdata->hw_if.read_mmc_stats(pdata);
2177 s->rx_packets = pstats->rxframecount_gb;
2178 s->rx_bytes = pstats->rxoctetcount_gb;
2179 s->rx_errors = pstats->rxframecount_gb -
2180 pstats->rxbroadcastframes_g -
2181 pstats->rxmulticastframes_g -
2182 pstats->rxunicastframes_g;
2183 s->multicast = pstats->rxmulticastframes_g;
2184 s->rx_length_errors = pstats->rxlengtherror;
2185 s->rx_crc_errors = pstats->rxcrcerror;
2186 s->rx_fifo_errors = pstats->rxfifooverflow;
2188 s->tx_packets = pstats->txframecount_gb;
2189 s->tx_bytes = pstats->txoctetcount_gb;
2190 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
2191 s->tx_dropped = netdev->stats.tx_dropped;
2193 DBGPR("<--%s\n", __func__);
2196 static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
2199 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2200 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2202 DBGPR("-->%s\n", __func__);
2204 set_bit(vid, pdata->active_vlans);
2205 hw_if->update_vlan_hash_table(pdata);
2207 DBGPR("<--%s\n", __func__);
2212 static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
2215 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2216 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2218 DBGPR("-->%s\n", __func__);
2220 clear_bit(vid, pdata->active_vlans);
2221 hw_if->update_vlan_hash_table(pdata);
2223 DBGPR("<--%s\n", __func__);
2228 #ifdef CONFIG_NET_POLL_CONTROLLER
2229 static void xgbe_poll_controller(struct net_device *netdev)
2231 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2232 struct xgbe_channel *channel;
2235 DBGPR("-->xgbe_poll_controller\n");
2237 if (pdata->per_channel_irq) {
2238 for (i = 0; i < pdata->channel_count; i++) {
2239 channel = pdata->channel[i];
2240 xgbe_dma_isr(channel->dma_irq, channel);
2243 disable_irq(pdata->dev_irq);
2244 xgbe_isr(pdata->dev_irq, pdata);
2245 enable_irq(pdata->dev_irq);
2248 DBGPR("<--xgbe_poll_controller\n");
2250 #endif /* End CONFIG_NET_POLL_CONTROLLER */
2252 static int xgbe_setup_tc(struct net_device *netdev, enum tc_setup_type type,
2255 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2256 struct tc_mqprio_qopt *mqprio = type_data;
2259 if (type != TC_SETUP_QDISC_MQPRIO)
2262 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2263 tc = mqprio->num_tc;
2265 if (tc > pdata->hw_feat.tc_cnt)
2268 pdata->num_tcs = tc;
2269 pdata->hw_if.config_tc(pdata);
2274 static netdev_features_t xgbe_fix_features(struct net_device *netdev,
2275 netdev_features_t features)
2277 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2278 netdev_features_t vxlan_base, vxlan_mask;
2280 vxlan_base = NETIF_F_GSO_UDP_TUNNEL | NETIF_F_RX_UDP_TUNNEL_PORT;
2281 vxlan_mask = vxlan_base | NETIF_F_GSO_UDP_TUNNEL_CSUM;
2283 pdata->vxlan_features = features & vxlan_mask;
2285 /* Only fix VXLAN-related features */
2286 if (!pdata->vxlan_features)
2289 /* If VXLAN isn't supported then clear any features:
2290 * This is needed because NETIF_F_RX_UDP_TUNNEL_PORT gets
2291 * automatically set if ndo_udp_tunnel_add is set.
2293 if (!pdata->hw_feat.vxn)
2294 return features & ~vxlan_mask;
2296 /* VXLAN CSUM requires VXLAN base */
2297 if ((features & NETIF_F_GSO_UDP_TUNNEL_CSUM) &&
2298 !(features & NETIF_F_GSO_UDP_TUNNEL)) {
2299 netdev_notice(netdev,
2300 "forcing tx udp tunnel support\n");
2301 features |= NETIF_F_GSO_UDP_TUNNEL;
2304 /* Can't do one without doing the other */
2305 if ((features & vxlan_base) != vxlan_base) {
2306 netdev_notice(netdev,
2307 "forcing both tx and rx udp tunnel support\n");
2308 features |= vxlan_base;
2311 if (features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2312 if (!(features & NETIF_F_GSO_UDP_TUNNEL_CSUM)) {
2313 netdev_notice(netdev,
2314 "forcing tx udp tunnel checksumming on\n");
2315 features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
2318 if (features & NETIF_F_GSO_UDP_TUNNEL_CSUM) {
2319 netdev_notice(netdev,
2320 "forcing tx udp tunnel checksumming off\n");
2321 features &= ~NETIF_F_GSO_UDP_TUNNEL_CSUM;
2325 pdata->vxlan_features = features & vxlan_mask;
2327 /* Adjust UDP Tunnel based on current state */
2328 if (pdata->vxlan_force_disable) {
2329 netdev_notice(netdev,
2330 "VXLAN acceleration disabled, turning off udp tunnel features\n");
2331 features &= ~vxlan_mask;
2337 static int xgbe_set_features(struct net_device *netdev,
2338 netdev_features_t features)
2340 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2341 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2342 netdev_features_t rxhash, rxcsum, rxvlan, rxvlan_filter;
2343 netdev_features_t udp_tunnel;
2346 rxhash = pdata->netdev_features & NETIF_F_RXHASH;
2347 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
2348 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
2349 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
2350 udp_tunnel = pdata->netdev_features & NETIF_F_GSO_UDP_TUNNEL;
2352 if ((features & NETIF_F_RXHASH) && !rxhash)
2353 ret = hw_if->enable_rss(pdata);
2354 else if (!(features & NETIF_F_RXHASH) && rxhash)
2355 ret = hw_if->disable_rss(pdata);
2359 if ((features & NETIF_F_RXCSUM) && !rxcsum)
2360 hw_if->enable_rx_csum(pdata);
2361 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
2362 hw_if->disable_rx_csum(pdata);
2364 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
2365 hw_if->enable_rx_vlan_stripping(pdata);
2366 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
2367 hw_if->disable_rx_vlan_stripping(pdata);
2369 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
2370 hw_if->enable_rx_vlan_filtering(pdata);
2371 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
2372 hw_if->disable_rx_vlan_filtering(pdata);
2374 if ((features & NETIF_F_GSO_UDP_TUNNEL) && !udp_tunnel)
2375 xgbe_enable_vxlan_accel(pdata);
2376 else if (!(features & NETIF_F_GSO_UDP_TUNNEL) && udp_tunnel)
2377 xgbe_disable_vxlan_accel(pdata);
2379 pdata->netdev_features = features;
2381 DBGPR("<--xgbe_set_features\n");
2386 static void xgbe_udp_tunnel_add(struct net_device *netdev,
2387 struct udp_tunnel_info *ti)
2389 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2390 struct xgbe_vxlan_data *vdata;
2392 if (!pdata->hw_feat.vxn)
2395 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2398 pdata->vxlan_port_count++;
2400 netif_dbg(pdata, drv, netdev,
2401 "adding VXLAN tunnel, family=%hx/port=%hx\n",
2402 ti->sa_family, be16_to_cpu(ti->port));
2404 if (pdata->vxlan_force_disable)
2407 vdata = kzalloc(sizeof(*vdata), GFP_ATOMIC);
2409 /* Can no longer properly track VXLAN ports */
2410 pdata->vxlan_force_disable = 1;
2411 netif_dbg(pdata, drv, netdev,
2412 "internal error, disabling VXLAN accelerations\n");
2414 xgbe_disable_vxlan_accel(pdata);
2418 vdata->sa_family = ti->sa_family;
2419 vdata->port = ti->port;
2421 list_add_tail(&vdata->list, &pdata->vxlan_ports);
2423 /* First port added? */
2424 if (pdata->vxlan_port_count == 1) {
2425 xgbe_enable_vxlan_accel(pdata);
2431 static void xgbe_udp_tunnel_del(struct net_device *netdev,
2432 struct udp_tunnel_info *ti)
2434 struct xgbe_prv_data *pdata = netdev_priv(netdev);
2435 struct xgbe_vxlan_data *vdata;
2437 if (!pdata->hw_feat.vxn)
2440 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2443 netif_dbg(pdata, drv, netdev,
2444 "deleting VXLAN tunnel, family=%hx/port=%hx\n",
2445 ti->sa_family, be16_to_cpu(ti->port));
2447 /* Don't need safe version since loop terminates with deletion */
2448 list_for_each_entry(vdata, &pdata->vxlan_ports, list) {
2449 if (vdata->sa_family != ti->sa_family)
2452 if (vdata->port != ti->port)
2455 list_del(&vdata->list);
2461 pdata->vxlan_port_count--;
2462 if (!pdata->vxlan_port_count) {
2463 xgbe_reset_vxlan_accel(pdata);
2468 if (pdata->vxlan_force_disable)
2471 /* See if VXLAN tunnel id needs to be changed */
2472 vdata = list_first_entry(&pdata->vxlan_ports,
2473 struct xgbe_vxlan_data, list);
2474 if (pdata->vxlan_port == be16_to_cpu(vdata->port))
2477 pdata->vxlan_port = be16_to_cpu(vdata->port);
2478 pdata->hw_if.set_vxlan_id(pdata);
2481 static netdev_features_t xgbe_features_check(struct sk_buff *skb,
2482 struct net_device *netdev,
2483 netdev_features_t features)
2485 features = vlan_features_check(skb, features);
2486 features = vxlan_features_check(skb, features);
2491 static const struct net_device_ops xgbe_netdev_ops = {
2492 .ndo_open = xgbe_open,
2493 .ndo_stop = xgbe_close,
2494 .ndo_start_xmit = xgbe_xmit,
2495 .ndo_set_rx_mode = xgbe_set_rx_mode,
2496 .ndo_set_mac_address = xgbe_set_mac_address,
2497 .ndo_validate_addr = eth_validate_addr,
2498 .ndo_do_ioctl = xgbe_ioctl,
2499 .ndo_change_mtu = xgbe_change_mtu,
2500 .ndo_tx_timeout = xgbe_tx_timeout,
2501 .ndo_get_stats64 = xgbe_get_stats64,
2502 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
2503 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
2504 #ifdef CONFIG_NET_POLL_CONTROLLER
2505 .ndo_poll_controller = xgbe_poll_controller,
2507 .ndo_setup_tc = xgbe_setup_tc,
2508 .ndo_fix_features = xgbe_fix_features,
2509 .ndo_set_features = xgbe_set_features,
2510 .ndo_udp_tunnel_add = xgbe_udp_tunnel_add,
2511 .ndo_udp_tunnel_del = xgbe_udp_tunnel_del,
2512 .ndo_features_check = xgbe_features_check,
2515 const struct net_device_ops *xgbe_get_netdev_ops(void)
2517 return &xgbe_netdev_ops;
2520 static void xgbe_rx_refresh(struct xgbe_channel *channel)
2522 struct xgbe_prv_data *pdata = channel->pdata;
2523 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2524 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2525 struct xgbe_ring *ring = channel->rx_ring;
2526 struct xgbe_ring_data *rdata;
2528 while (ring->dirty != ring->cur) {
2529 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2531 /* Reset rdata values */
2532 desc_if->unmap_rdata(pdata, rdata);
2534 if (desc_if->map_rx_buffer(pdata, ring, rdata))
2537 hw_if->rx_desc_reset(pdata, rdata, ring->dirty);
2542 /* Make sure everything is written before the register write */
2545 /* Update the Rx Tail Pointer Register with address of
2546 * the last cleaned entry */
2547 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty - 1);
2548 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
2549 lower_32_bits(rdata->rdesc_dma));
2552 static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
2553 struct napi_struct *napi,
2554 struct xgbe_ring_data *rdata,
2557 struct sk_buff *skb;
2560 skb = napi_alloc_skb(napi, rdata->rx.hdr.dma_len);
2564 /* Pull in the header buffer which may contain just the header
2565 * or the header plus data
2567 dma_sync_single_range_for_cpu(pdata->dev, rdata->rx.hdr.dma_base,
2568 rdata->rx.hdr.dma_off,
2569 rdata->rx.hdr.dma_len, DMA_FROM_DEVICE);
2571 packet = page_address(rdata->rx.hdr.pa.pages) +
2572 rdata->rx.hdr.pa.pages_offset;
2573 skb_copy_to_linear_data(skb, packet, len);
2579 static unsigned int xgbe_rx_buf1_len(struct xgbe_ring_data *rdata,
2580 struct xgbe_packet_data *packet)
2582 /* Always zero if not the first descriptor */
2583 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, FIRST))
2586 /* First descriptor with split header, return header length */
2587 if (rdata->rx.hdr_len)
2588 return rdata->rx.hdr_len;
2590 /* First descriptor but not the last descriptor and no split header,
2591 * so the full buffer was used
2593 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2594 return rdata->rx.hdr.dma_len;
2596 /* First descriptor and last descriptor and no split header, so
2597 * calculate how much of the buffer was used
2599 return min_t(unsigned int, rdata->rx.hdr.dma_len, rdata->rx.len);
2602 static unsigned int xgbe_rx_buf2_len(struct xgbe_ring_data *rdata,
2603 struct xgbe_packet_data *packet,
2606 /* Always the full buffer if not the last descriptor */
2607 if (!XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES, LAST))
2608 return rdata->rx.buf.dma_len;
2610 /* Last descriptor so calculate how much of the buffer was used
2611 * for the last bit of data
2613 return rdata->rx.len - len;
2616 static int xgbe_tx_poll(struct xgbe_channel *channel)
2618 struct xgbe_prv_data *pdata = channel->pdata;
2619 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2620 struct xgbe_desc_if *desc_if = &pdata->desc_if;
2621 struct xgbe_ring *ring = channel->tx_ring;
2622 struct xgbe_ring_data *rdata;
2623 struct xgbe_ring_desc *rdesc;
2624 struct net_device *netdev = pdata->netdev;
2625 struct netdev_queue *txq;
2627 unsigned int tx_packets = 0, tx_bytes = 0;
2630 DBGPR("-->xgbe_tx_poll\n");
2632 /* Nothing to do if there isn't a Tx ring for this channel */
2638 /* Be sure we get ring->cur before accessing descriptor data */
2641 txq = netdev_get_tx_queue(netdev, channel->queue_index);
2643 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
2644 (ring->dirty != cur)) {
2645 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
2646 rdesc = rdata->rdesc;
2648 if (!hw_if->tx_complete(rdesc))
2651 /* Make sure descriptor fields are read after reading the OWN
2655 if (netif_msg_tx_done(pdata))
2656 xgbe_dump_tx_desc(pdata, ring, ring->dirty, 1, 0);
2658 if (hw_if->is_last_desc(rdesc)) {
2659 tx_packets += rdata->tx.packets;
2660 tx_bytes += rdata->tx.bytes;
2663 /* Free the SKB and reset the descriptor for re-use */
2664 desc_if->unmap_rdata(pdata, rdata);
2665 hw_if->tx_desc_reset(rdata);
2674 netdev_tx_completed_queue(txq, tx_packets, tx_bytes);
2676 if ((ring->tx.queue_stopped == 1) &&
2677 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
2678 ring->tx.queue_stopped = 0;
2679 netif_tx_wake_queue(txq);
2682 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
2687 static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
2689 struct xgbe_prv_data *pdata = channel->pdata;
2690 struct xgbe_hw_if *hw_if = &pdata->hw_if;
2691 struct xgbe_ring *ring = channel->rx_ring;
2692 struct xgbe_ring_data *rdata;
2693 struct xgbe_packet_data *packet;
2694 struct net_device *netdev = pdata->netdev;
2695 struct napi_struct *napi;
2696 struct sk_buff *skb;
2697 struct skb_shared_hwtstamps *hwtstamps;
2698 unsigned int last, error, context_next, context;
2699 unsigned int len, buf1_len, buf2_len, max_len;
2700 unsigned int received = 0;
2701 int packet_count = 0;
2703 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
2705 /* Nothing to do if there isn't a Rx ring for this channel */
2712 napi = (pdata->per_channel_irq) ? &channel->napi : &pdata->napi;
2714 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2715 packet = &ring->packet_data;
2716 while (packet_count < budget) {
2717 DBGPR(" cur = %d\n", ring->cur);
2719 /* First time in loop see if we need to restore state */
2720 if (!received && rdata->state_saved) {
2721 skb = rdata->state.skb;
2722 error = rdata->state.error;
2723 len = rdata->state.len;
2725 memset(packet, 0, sizeof(*packet));
2732 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2734 if (xgbe_rx_dirty_desc(ring) > (XGBE_RX_DESC_CNT >> 3))
2735 xgbe_rx_refresh(channel);
2737 if (hw_if->dev_read(channel))
2743 last = XGMAC_GET_BITS(packet->attributes, RX_PACKET_ATTRIBUTES,
2745 context_next = XGMAC_GET_BITS(packet->attributes,
2746 RX_PACKET_ATTRIBUTES,
2748 context = XGMAC_GET_BITS(packet->attributes,
2749 RX_PACKET_ATTRIBUTES,
2752 /* Earlier error, just drain the remaining data */
2753 if ((!last || context_next) && error)
2756 if (error || packet->errors) {
2758 netif_err(pdata, rx_err, netdev,
2759 "error in received packet\n");
2765 /* Get the data length in the descriptor buffers */
2766 buf1_len = xgbe_rx_buf1_len(rdata, packet);
2768 buf2_len = xgbe_rx_buf2_len(rdata, packet, len);
2771 if (buf2_len > rdata->rx.buf.dma_len) {
2772 /* Hardware inconsistency within the descriptors
2773 * that has resulted in a length underflow.
2780 skb = xgbe_create_skb(pdata, napi, rdata,
2789 dma_sync_single_range_for_cpu(pdata->dev,
2790 rdata->rx.buf.dma_base,
2791 rdata->rx.buf.dma_off,
2792 rdata->rx.buf.dma_len,
2795 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2796 rdata->rx.buf.pa.pages,
2797 rdata->rx.buf.pa.pages_offset,
2799 rdata->rx.buf.dma_len);
2800 rdata->rx.buf.pa.pages = NULL;
2805 if (!last || context_next)
2808 if (!skb || error) {
2813 /* Be sure we don't exceed the configured MTU */
2814 max_len = netdev->mtu + ETH_HLEN;
2815 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
2816 (skb->protocol == htons(ETH_P_8021Q)))
2817 max_len += VLAN_HLEN;
2819 if (skb->len > max_len) {
2820 netif_err(pdata, rx_err, netdev,
2821 "packet length exceeds configured MTU\n");
2826 if (netif_msg_pktdata(pdata))
2827 xgbe_print_pkt(netdev, skb, false);
2829 skb_checksum_none_assert(skb);
2830 if (XGMAC_GET_BITS(packet->attributes,
2831 RX_PACKET_ATTRIBUTES, CSUM_DONE))
2832 skb->ip_summed = CHECKSUM_UNNECESSARY;
2834 if (XGMAC_GET_BITS(packet->attributes,
2835 RX_PACKET_ATTRIBUTES, TNP)) {
2836 skb->encapsulation = 1;
2838 if (XGMAC_GET_BITS(packet->attributes,
2839 RX_PACKET_ATTRIBUTES, TNPCSUM_DONE))
2840 skb->csum_level = 1;
2843 if (XGMAC_GET_BITS(packet->attributes,
2844 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
2845 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
2848 if (XGMAC_GET_BITS(packet->attributes,
2849 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
2852 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
2854 hwtstamps = skb_hwtstamps(skb);
2855 hwtstamps->hwtstamp = ns_to_ktime(nsec);
2858 if (XGMAC_GET_BITS(packet->attributes,
2859 RX_PACKET_ATTRIBUTES, RSS_HASH))
2860 skb_set_hash(skb, packet->rss_hash,
2861 packet->rss_hash_type);
2864 skb->protocol = eth_type_trans(skb, netdev);
2865 skb_record_rx_queue(skb, channel->queue_index);
2867 napi_gro_receive(napi, skb);
2873 /* Check if we need to save state before leaving */
2874 if (received && (!last || context_next)) {
2875 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
2876 rdata->state_saved = 1;
2877 rdata->state.skb = skb;
2878 rdata->state.len = len;
2879 rdata->state.error = error;
2882 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
2884 return packet_count;
2887 static int xgbe_one_poll(struct napi_struct *napi, int budget)
2889 struct xgbe_channel *channel = container_of(napi, struct xgbe_channel,
2891 struct xgbe_prv_data *pdata = channel->pdata;
2894 DBGPR("-->xgbe_one_poll: budget=%d\n", budget);
2896 /* Cleanup Tx ring first */
2897 xgbe_tx_poll(channel);
2899 /* Process Rx ring next */
2900 processed = xgbe_rx_poll(channel, budget);
2902 /* If we processed everything, we are done */
2903 if ((processed < budget) && napi_complete_done(napi, processed)) {
2904 /* Enable Tx and Rx interrupts */
2905 if (pdata->channel_irq_mode)
2906 xgbe_enable_rx_tx_int(pdata, channel);
2908 enable_irq(channel->dma_irq);
2911 DBGPR("<--xgbe_one_poll: received = %d\n", processed);
2916 static int xgbe_all_poll(struct napi_struct *napi, int budget)
2918 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
2920 struct xgbe_channel *channel;
2922 int processed, last_processed;
2925 DBGPR("-->xgbe_all_poll: budget=%d\n", budget);
2928 ring_budget = budget / pdata->rx_ring_count;
2930 last_processed = processed;
2932 for (i = 0; i < pdata->channel_count; i++) {
2933 channel = pdata->channel[i];
2935 /* Cleanup Tx ring first */
2936 xgbe_tx_poll(channel);
2938 /* Process Rx ring next */
2939 if (ring_budget > (budget - processed))
2940 ring_budget = budget - processed;
2941 processed += xgbe_rx_poll(channel, ring_budget);
2943 } while ((processed < budget) && (processed != last_processed));
2945 /* If we processed everything, we are done */
2946 if ((processed < budget) && napi_complete_done(napi, processed)) {
2947 /* Enable Tx and Rx interrupts */
2948 xgbe_enable_rx_tx_ints(pdata);
2951 DBGPR("<--xgbe_all_poll: received = %d\n", processed);
2956 void xgbe_dump_tx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2957 unsigned int idx, unsigned int count, unsigned int flag)
2959 struct xgbe_ring_data *rdata;
2960 struct xgbe_ring_desc *rdesc;
2963 rdata = XGBE_GET_DESC_DATA(ring, idx);
2964 rdesc = rdata->rdesc;
2965 netdev_dbg(pdata->netdev,
2966 "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
2967 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2968 le32_to_cpu(rdesc->desc0),
2969 le32_to_cpu(rdesc->desc1),
2970 le32_to_cpu(rdesc->desc2),
2971 le32_to_cpu(rdesc->desc3));
2976 void xgbe_dump_rx_desc(struct xgbe_prv_data *pdata, struct xgbe_ring *ring,
2979 struct xgbe_ring_data *rdata;
2980 struct xgbe_ring_desc *rdesc;
2982 rdata = XGBE_GET_DESC_DATA(ring, idx);
2983 rdesc = rdata->rdesc;
2984 netdev_dbg(pdata->netdev,
2985 "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2986 idx, le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
2987 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
2990 void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
2992 struct ethhdr *eth = (struct ethhdr *)skb->data;
2993 unsigned char buffer[128];
2996 netdev_dbg(netdev, "\n************** SKB dump ****************\n");
2998 netdev_dbg(netdev, "%s packet of %d bytes\n",
2999 (tx_rx ? "TX" : "RX"), skb->len);
3001 netdev_dbg(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
3002 netdev_dbg(netdev, "Src MAC addr: %pM\n", eth->h_source);
3003 netdev_dbg(netdev, "Protocol: %#06hx\n", ntohs(eth->h_proto));
3005 for (i = 0; i < skb->len; i += 32) {
3006 unsigned int len = min(skb->len - i, 32U);
3008 hex_dump_to_buffer(&skb->data[i], len, 32, 1,
3009 buffer, sizeof(buffer), false);
3010 netdev_dbg(netdev, " %#06x: %s\n", i, buffer);
3013 netdev_dbg(netdev, "\n************** SKB dump ****************\n");