2 * AMD 10Gb Ethernet driver
4 * This file is available to you under your choice of the following two
9 * Copyright (c) 2014 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 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/spinlock.h>
118 #include <linux/tcp.h>
119 #include <linux/if_vlan.h>
120 #include <net/busy_poll.h>
121 #include <linux/clk.h>
122 #include <linux/if_ether.h>
123 #include <linux/net_tstamp.h>
124 #include <linux/phy.h>
127 #include "xgbe-common.h"
129 static int xgbe_poll(struct napi_struct *, int);
130 static void xgbe_set_rx_mode(struct net_device *);
132 static int xgbe_alloc_channels(struct xgbe_prv_data *pdata)
134 struct xgbe_channel *channel_mem, *channel;
135 struct xgbe_ring *tx_ring, *rx_ring;
136 unsigned int count, i;
138 count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
140 channel_mem = kcalloc(count, sizeof(struct xgbe_channel), GFP_KERNEL);
144 tx_ring = kcalloc(pdata->tx_ring_count, sizeof(struct xgbe_ring),
149 rx_ring = kcalloc(pdata->rx_ring_count, sizeof(struct xgbe_ring),
154 for (i = 0, channel = channel_mem; i < count; i++, channel++) {
155 snprintf(channel->name, sizeof(channel->name), "channel-%d", i);
156 channel->pdata = pdata;
157 channel->queue_index = i;
158 channel->dma_regs = pdata->xgmac_regs + DMA_CH_BASE +
161 if (i < pdata->tx_ring_count) {
162 spin_lock_init(&tx_ring->lock);
163 channel->tx_ring = tx_ring++;
166 if (i < pdata->rx_ring_count) {
167 spin_lock_init(&rx_ring->lock);
168 channel->rx_ring = rx_ring++;
171 DBGPR(" %s - queue_index=%u, dma_regs=%p, tx=%p, rx=%p\n",
172 channel->name, channel->queue_index, channel->dma_regs,
173 channel->tx_ring, channel->rx_ring);
176 pdata->channel = channel_mem;
177 pdata->channel_count = count;
188 netdev_err(pdata->netdev, "channel allocation failed\n");
193 static void xgbe_free_channels(struct xgbe_prv_data *pdata)
198 kfree(pdata->channel->rx_ring);
199 kfree(pdata->channel->tx_ring);
200 kfree(pdata->channel);
202 pdata->channel = NULL;
203 pdata->channel_count = 0;
206 static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring *ring)
208 return (ring->rdesc_count - (ring->cur - ring->dirty));
211 static int xgbe_calc_rx_buf_size(struct net_device *netdev, unsigned int mtu)
213 unsigned int rx_buf_size;
215 if (mtu > XGMAC_JUMBO_PACKET_MTU) {
216 netdev_alert(netdev, "MTU exceeds maximum supported value\n");
220 rx_buf_size = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
221 rx_buf_size = clamp_val(rx_buf_size, XGBE_RX_MIN_BUF_SIZE, PAGE_SIZE);
223 rx_buf_size = (rx_buf_size + XGBE_RX_BUF_ALIGN - 1) &
224 ~(XGBE_RX_BUF_ALIGN - 1);
229 static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data *pdata)
231 struct xgbe_hw_if *hw_if = &pdata->hw_if;
232 struct xgbe_channel *channel;
233 enum xgbe_int int_id;
236 channel = pdata->channel;
237 for (i = 0; i < pdata->channel_count; i++, channel++) {
238 if (channel->tx_ring && channel->rx_ring)
239 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
240 else if (channel->tx_ring)
241 int_id = XGMAC_INT_DMA_CH_SR_TI;
242 else if (channel->rx_ring)
243 int_id = XGMAC_INT_DMA_CH_SR_RI;
247 hw_if->enable_int(channel, int_id);
251 static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
253 struct xgbe_hw_if *hw_if = &pdata->hw_if;
254 struct xgbe_channel *channel;
255 enum xgbe_int int_id;
258 channel = pdata->channel;
259 for (i = 0; i < pdata->channel_count; i++, channel++) {
260 if (channel->tx_ring && channel->rx_ring)
261 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
262 else if (channel->tx_ring)
263 int_id = XGMAC_INT_DMA_CH_SR_TI;
264 else if (channel->rx_ring)
265 int_id = XGMAC_INT_DMA_CH_SR_RI;
269 hw_if->disable_int(channel, int_id);
273 static irqreturn_t xgbe_isr(int irq, void *data)
275 struct xgbe_prv_data *pdata = data;
276 struct xgbe_hw_if *hw_if = &pdata->hw_if;
277 struct xgbe_channel *channel;
278 unsigned int dma_isr, dma_ch_isr;
279 unsigned int mac_isr, mac_tssr;
282 /* The DMA interrupt status register also reports MAC and MTL
283 * interrupts. So for polling mode, we just need to check for
284 * this register to be non-zero
286 dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
290 DBGPR("-->xgbe_isr\n");
292 DBGPR(" DMA_ISR = %08x\n", dma_isr);
293 DBGPR(" DMA_DS0 = %08x\n", XGMAC_IOREAD(pdata, DMA_DSR0));
294 DBGPR(" DMA_DS1 = %08x\n", XGMAC_IOREAD(pdata, DMA_DSR1));
296 for (i = 0; i < pdata->channel_count; i++) {
297 if (!(dma_isr & (1 << i)))
300 channel = pdata->channel + i;
302 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
303 DBGPR(" DMA_CH%u_ISR = %08x\n", i, dma_ch_isr);
305 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
306 XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI)) {
307 if (napi_schedule_prep(&pdata->napi)) {
308 /* Disable Tx and Rx interrupts */
309 xgbe_disable_rx_tx_ints(pdata);
311 /* Turn on polling */
312 __napi_schedule(&pdata->napi);
316 /* Restart the device on a Fatal Bus Error */
317 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
318 schedule_work(&pdata->restart_work);
320 /* Clear all interrupt signals */
321 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
324 if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
325 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
327 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
328 hw_if->tx_mmc_int(pdata);
330 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
331 hw_if->rx_mmc_int(pdata);
333 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, TSIS)) {
334 mac_tssr = XGMAC_IOREAD(pdata, MAC_TSSR);
336 if (XGMAC_GET_BITS(mac_tssr, MAC_TSSR, TXTSC)) {
337 /* Read Tx Timestamp to clear interrupt */
339 hw_if->get_tx_tstamp(pdata);
340 schedule_work(&pdata->tx_tstamp_work);
345 DBGPR(" DMA_ISR = %08x\n", XGMAC_IOREAD(pdata, DMA_ISR));
347 DBGPR("<--xgbe_isr\n");
353 static enum hrtimer_restart xgbe_tx_timer(struct hrtimer *timer)
355 struct xgbe_channel *channel = container_of(timer,
358 struct xgbe_ring *ring = channel->tx_ring;
359 struct xgbe_prv_data *pdata = channel->pdata;
362 DBGPR("-->xgbe_tx_timer\n");
364 spin_lock_irqsave(&ring->lock, flags);
366 if (napi_schedule_prep(&pdata->napi)) {
367 /* Disable Tx and Rx interrupts */
368 xgbe_disable_rx_tx_ints(pdata);
370 /* Turn on polling */
371 __napi_schedule(&pdata->napi);
374 channel->tx_timer_active = 0;
376 spin_unlock_irqrestore(&ring->lock, flags);
378 DBGPR("<--xgbe_tx_timer\n");
380 return HRTIMER_NORESTART;
383 static void xgbe_init_tx_timers(struct xgbe_prv_data *pdata)
385 struct xgbe_channel *channel;
388 DBGPR("-->xgbe_init_tx_timers\n");
390 channel = pdata->channel;
391 for (i = 0; i < pdata->channel_count; i++, channel++) {
392 if (!channel->tx_ring)
395 DBGPR(" %s adding tx timer\n", channel->name);
396 hrtimer_init(&channel->tx_timer, CLOCK_MONOTONIC,
398 channel->tx_timer.function = xgbe_tx_timer;
401 DBGPR("<--xgbe_init_tx_timers\n");
404 static void xgbe_stop_tx_timers(struct xgbe_prv_data *pdata)
406 struct xgbe_channel *channel;
409 DBGPR("-->xgbe_stop_tx_timers\n");
411 channel = pdata->channel;
412 for (i = 0; i < pdata->channel_count; i++, channel++) {
413 if (!channel->tx_ring)
416 DBGPR(" %s deleting tx timer\n", channel->name);
417 channel->tx_timer_active = 0;
418 hrtimer_cancel(&channel->tx_timer);
421 DBGPR("<--xgbe_stop_tx_timers\n");
424 void xgbe_get_all_hw_features(struct xgbe_prv_data *pdata)
426 unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
427 struct xgbe_hw_features *hw_feat = &pdata->hw_feat;
429 DBGPR("-->xgbe_get_all_hw_features\n");
431 mac_hfr0 = XGMAC_IOREAD(pdata, MAC_HWF0R);
432 mac_hfr1 = XGMAC_IOREAD(pdata, MAC_HWF1R);
433 mac_hfr2 = XGMAC_IOREAD(pdata, MAC_HWF2R);
435 memset(hw_feat, 0, sizeof(*hw_feat));
437 hw_feat->version = XGMAC_IOREAD(pdata, MAC_VR);
439 /* Hardware feature register 0 */
440 hw_feat->gmii = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, GMIISEL);
441 hw_feat->vlhash = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, VLHASH);
442 hw_feat->sma = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SMASEL);
443 hw_feat->rwk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RWKSEL);
444 hw_feat->mgk = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MGKSEL);
445 hw_feat->mmc = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, MMCSEL);
446 hw_feat->aoe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, ARPOFFSEL);
447 hw_feat->ts = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSEL);
448 hw_feat->eee = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, EEESEL);
449 hw_feat->tx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TXCOESEL);
450 hw_feat->rx_coe = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, RXCOESEL);
451 hw_feat->addn_mac = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R,
453 hw_feat->ts_src = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, TSSTSSEL);
454 hw_feat->sa_vlan_ins = XGMAC_GET_BITS(mac_hfr0, MAC_HWF0R, SAVLANINS);
456 /* Hardware feature register 1 */
457 hw_feat->rx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
459 hw_feat->tx_fifo_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
461 hw_feat->dcb = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DCBEN);
462 hw_feat->sph = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, SPHEN);
463 hw_feat->tso = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, TSOEN);
464 hw_feat->dma_debug = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, DBGMEMA);
465 hw_feat->tc_cnt = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R, NUMTC);
466 hw_feat->hash_table_size = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
468 hw_feat->l3l4_filter_num = XGMAC_GET_BITS(mac_hfr1, MAC_HWF1R,
471 /* Hardware feature register 2 */
472 hw_feat->rx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXQCNT);
473 hw_feat->tx_q_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXQCNT);
474 hw_feat->rx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, RXCHCNT);
475 hw_feat->tx_ch_cnt = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, TXCHCNT);
476 hw_feat->pps_out_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, PPSOUTNUM);
477 hw_feat->aux_snap_num = XGMAC_GET_BITS(mac_hfr2, MAC_HWF2R, AUXSNAPNUM);
479 /* Translate the Hash Table size into actual number */
480 switch (hw_feat->hash_table_size) {
484 hw_feat->hash_table_size = 64;
487 hw_feat->hash_table_size = 128;
490 hw_feat->hash_table_size = 256;
494 /* The Queue and Channel counts are zero based so increment them
495 * to get the actual number
499 hw_feat->rx_ch_cnt++;
500 hw_feat->tx_ch_cnt++;
502 DBGPR("<--xgbe_get_all_hw_features\n");
505 static void xgbe_napi_enable(struct xgbe_prv_data *pdata, unsigned int add)
508 netif_napi_add(pdata->netdev, &pdata->napi, xgbe_poll,
510 napi_enable(&pdata->napi);
513 static void xgbe_napi_disable(struct xgbe_prv_data *pdata, unsigned int del)
515 napi_disable(&pdata->napi);
518 netif_napi_del(&pdata->napi);
521 void xgbe_init_tx_coalesce(struct xgbe_prv_data *pdata)
523 struct xgbe_hw_if *hw_if = &pdata->hw_if;
525 DBGPR("-->xgbe_init_tx_coalesce\n");
527 pdata->tx_usecs = XGMAC_INIT_DMA_TX_USECS;
528 pdata->tx_frames = XGMAC_INIT_DMA_TX_FRAMES;
530 hw_if->config_tx_coalesce(pdata);
532 DBGPR("<--xgbe_init_tx_coalesce\n");
535 void xgbe_init_rx_coalesce(struct xgbe_prv_data *pdata)
537 struct xgbe_hw_if *hw_if = &pdata->hw_if;
539 DBGPR("-->xgbe_init_rx_coalesce\n");
541 pdata->rx_riwt = hw_if->usec_to_riwt(pdata, XGMAC_INIT_DMA_RX_USECS);
542 pdata->rx_frames = XGMAC_INIT_DMA_RX_FRAMES;
544 hw_if->config_rx_coalesce(pdata);
546 DBGPR("<--xgbe_init_rx_coalesce\n");
549 static void xgbe_free_tx_data(struct xgbe_prv_data *pdata)
551 struct xgbe_desc_if *desc_if = &pdata->desc_if;
552 struct xgbe_channel *channel;
553 struct xgbe_ring *ring;
554 struct xgbe_ring_data *rdata;
557 DBGPR("-->xgbe_free_tx_data\n");
559 channel = pdata->channel;
560 for (i = 0; i < pdata->channel_count; i++, channel++) {
561 ring = channel->tx_ring;
565 for (j = 0; j < ring->rdesc_count; j++) {
566 rdata = XGBE_GET_DESC_DATA(ring, j);
567 desc_if->unmap_rdata(pdata, rdata);
571 DBGPR("<--xgbe_free_tx_data\n");
574 static void xgbe_free_rx_data(struct xgbe_prv_data *pdata)
576 struct xgbe_desc_if *desc_if = &pdata->desc_if;
577 struct xgbe_channel *channel;
578 struct xgbe_ring *ring;
579 struct xgbe_ring_data *rdata;
582 DBGPR("-->xgbe_free_rx_data\n");
584 channel = pdata->channel;
585 for (i = 0; i < pdata->channel_count; i++, channel++) {
586 ring = channel->rx_ring;
590 for (j = 0; j < ring->rdesc_count; j++) {
591 rdata = XGBE_GET_DESC_DATA(ring, j);
592 desc_if->unmap_rdata(pdata, rdata);
596 DBGPR("<--xgbe_free_rx_data\n");
599 static void xgbe_adjust_link(struct net_device *netdev)
601 struct xgbe_prv_data *pdata = netdev_priv(netdev);
602 struct xgbe_hw_if *hw_if = &pdata->hw_if;
603 struct phy_device *phydev = pdata->phydev;
610 /* Flow control support */
611 if (pdata->pause_autoneg) {
612 if (phydev->pause || phydev->asym_pause) {
621 if (pdata->tx_pause != pdata->phy_tx_pause) {
622 hw_if->config_tx_flow_control(pdata);
623 pdata->phy_tx_pause = pdata->tx_pause;
626 if (pdata->rx_pause != pdata->phy_rx_pause) {
627 hw_if->config_rx_flow_control(pdata);
628 pdata->phy_rx_pause = pdata->rx_pause;
632 if (phydev->speed != pdata->phy_speed) {
635 switch (phydev->speed) {
637 hw_if->set_xgmii_speed(pdata);
641 hw_if->set_gmii_2500_speed(pdata);
645 hw_if->set_gmii_speed(pdata);
648 pdata->phy_speed = phydev->speed;
651 if (phydev->link != pdata->phy_link) {
655 } else if (pdata->phy_link) {
658 pdata->phy_speed = SPEED_UNKNOWN;
662 phy_print_status(phydev);
665 static int xgbe_phy_init(struct xgbe_prv_data *pdata)
667 struct net_device *netdev = pdata->netdev;
668 struct phy_device *phydev = pdata->phydev;
671 pdata->phy_link = -1;
672 pdata->phy_speed = SPEED_UNKNOWN;
673 pdata->phy_tx_pause = pdata->tx_pause;
674 pdata->phy_rx_pause = pdata->rx_pause;
676 ret = phy_connect_direct(netdev, phydev, &xgbe_adjust_link,
679 netdev_err(netdev, "phy_connect_direct failed\n");
683 if (!phydev->drv || (phydev->drv->phy_id == 0)) {
684 netdev_err(netdev, "phy_id not valid\n");
686 goto err_phy_connect;
688 DBGPR(" phy_connect_direct succeeded for PHY %s, link=%d\n",
689 dev_name(&phydev->dev), phydev->link);
694 phy_disconnect(phydev);
699 static void xgbe_phy_exit(struct xgbe_prv_data *pdata)
704 phy_disconnect(pdata->phydev);
707 int xgbe_powerdown(struct net_device *netdev, unsigned int caller)
709 struct xgbe_prv_data *pdata = netdev_priv(netdev);
710 struct xgbe_hw_if *hw_if = &pdata->hw_if;
713 DBGPR("-->xgbe_powerdown\n");
715 if (!netif_running(netdev) ||
716 (caller == XGMAC_IOCTL_CONTEXT && pdata->power_down)) {
717 netdev_alert(netdev, "Device is already powered down\n");
718 DBGPR("<--xgbe_powerdown\n");
722 phy_stop(pdata->phydev);
724 spin_lock_irqsave(&pdata->lock, flags);
726 if (caller == XGMAC_DRIVER_CONTEXT)
727 netif_device_detach(netdev);
729 netif_tx_stop_all_queues(netdev);
730 xgbe_napi_disable(pdata, 0);
732 /* Powerdown Tx/Rx */
733 hw_if->powerdown_tx(pdata);
734 hw_if->powerdown_rx(pdata);
736 pdata->power_down = 1;
738 spin_unlock_irqrestore(&pdata->lock, flags);
740 DBGPR("<--xgbe_powerdown\n");
745 int xgbe_powerup(struct net_device *netdev, unsigned int caller)
747 struct xgbe_prv_data *pdata = netdev_priv(netdev);
748 struct xgbe_hw_if *hw_if = &pdata->hw_if;
751 DBGPR("-->xgbe_powerup\n");
753 if (!netif_running(netdev) ||
754 (caller == XGMAC_IOCTL_CONTEXT && !pdata->power_down)) {
755 netdev_alert(netdev, "Device is already powered up\n");
756 DBGPR("<--xgbe_powerup\n");
760 spin_lock_irqsave(&pdata->lock, flags);
762 pdata->power_down = 0;
764 phy_start(pdata->phydev);
767 hw_if->powerup_tx(pdata);
768 hw_if->powerup_rx(pdata);
770 if (caller == XGMAC_DRIVER_CONTEXT)
771 netif_device_attach(netdev);
773 xgbe_napi_enable(pdata, 0);
774 netif_tx_start_all_queues(netdev);
776 spin_unlock_irqrestore(&pdata->lock, flags);
778 DBGPR("<--xgbe_powerup\n");
783 static int xgbe_start(struct xgbe_prv_data *pdata)
785 struct xgbe_hw_if *hw_if = &pdata->hw_if;
786 struct net_device *netdev = pdata->netdev;
788 DBGPR("-->xgbe_start\n");
790 xgbe_set_rx_mode(netdev);
794 phy_start(pdata->phydev);
796 hw_if->enable_tx(pdata);
797 hw_if->enable_rx(pdata);
799 xgbe_init_tx_timers(pdata);
801 xgbe_napi_enable(pdata, 1);
802 netif_tx_start_all_queues(netdev);
804 DBGPR("<--xgbe_start\n");
809 static void xgbe_stop(struct xgbe_prv_data *pdata)
811 struct xgbe_hw_if *hw_if = &pdata->hw_if;
812 struct net_device *netdev = pdata->netdev;
814 DBGPR("-->xgbe_stop\n");
816 phy_stop(pdata->phydev);
818 netif_tx_stop_all_queues(netdev);
819 xgbe_napi_disable(pdata, 1);
821 xgbe_stop_tx_timers(pdata);
823 hw_if->disable_tx(pdata);
824 hw_if->disable_rx(pdata);
826 DBGPR("<--xgbe_stop\n");
829 static void xgbe_restart_dev(struct xgbe_prv_data *pdata, unsigned int reset)
831 struct xgbe_hw_if *hw_if = &pdata->hw_if;
833 DBGPR("-->xgbe_restart_dev\n");
835 /* If not running, "restart" will happen on open */
836 if (!netif_running(pdata->netdev))
840 synchronize_irq(pdata->irq_number);
842 xgbe_free_tx_data(pdata);
843 xgbe_free_rx_data(pdata);
845 /* Issue software reset to device if requested */
851 DBGPR("<--xgbe_restart_dev\n");
854 static void xgbe_restart(struct work_struct *work)
856 struct xgbe_prv_data *pdata = container_of(work,
857 struct xgbe_prv_data,
862 xgbe_restart_dev(pdata, 1);
867 static void xgbe_tx_tstamp(struct work_struct *work)
869 struct xgbe_prv_data *pdata = container_of(work,
870 struct xgbe_prv_data,
872 struct skb_shared_hwtstamps hwtstamps;
876 if (pdata->tx_tstamp) {
877 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
880 memset(&hwtstamps, 0, sizeof(hwtstamps));
881 hwtstamps.hwtstamp = ns_to_ktime(nsec);
882 skb_tstamp_tx(pdata->tx_tstamp_skb, &hwtstamps);
885 dev_kfree_skb_any(pdata->tx_tstamp_skb);
887 spin_lock_irqsave(&pdata->tstamp_lock, flags);
888 pdata->tx_tstamp_skb = NULL;
889 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
892 static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data *pdata,
895 if (copy_to_user(ifreq->ifr_data, &pdata->tstamp_config,
896 sizeof(pdata->tstamp_config)))
902 static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data *pdata,
905 struct hwtstamp_config config;
906 unsigned int mac_tscr;
908 if (copy_from_user(&config, ifreq->ifr_data, sizeof(config)))
916 switch (config.tx_type) {
917 case HWTSTAMP_TX_OFF:
921 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
928 switch (config.rx_filter) {
929 case HWTSTAMP_FILTER_NONE:
932 case HWTSTAMP_FILTER_ALL:
933 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENALL, 1);
934 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
937 /* PTP v2, UDP, any kind of event packet */
938 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
939 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
940 /* PTP v1, UDP, any kind of event packet */
941 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
942 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
943 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
944 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
945 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
948 /* PTP v2, UDP, Sync packet */
949 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
950 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
951 /* PTP v1, UDP, Sync packet */
952 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
953 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
954 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
955 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
956 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
959 /* PTP v2, UDP, Delay_req packet */
960 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
961 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
962 /* PTP v1, UDP, Delay_req packet */
963 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
964 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
965 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
966 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
967 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
968 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
971 /* 802.AS1, Ethernet, any kind of event packet */
972 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
973 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
974 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
975 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
978 /* 802.AS1, Ethernet, Sync packet */
979 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
980 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
981 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
982 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
985 /* 802.AS1, Ethernet, Delay_req packet */
986 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
987 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, AV8021ASMEN, 1);
988 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
989 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
990 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
993 /* PTP v2/802.AS1, any layer, any kind of event packet */
994 case HWTSTAMP_FILTER_PTP_V2_EVENT:
995 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
996 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
997 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
998 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
999 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, SNAPTYPSEL, 1);
1000 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1003 /* PTP v2/802.AS1, any layer, Sync packet */
1004 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1005 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1006 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1007 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1008 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1009 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1010 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1013 /* PTP v2/802.AS1, any layer, Delay_req packet */
1014 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1015 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSVER2ENA, 1);
1016 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPENA, 1);
1017 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV4ENA, 1);
1018 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSIPV6ENA, 1);
1019 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSMSTRENA, 1);
1020 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSEVNTENA, 1);
1021 XGMAC_SET_BITS(mac_tscr, MAC_TSCR, TSENA, 1);
1028 pdata->hw_if.config_tstamp(pdata, mac_tscr);
1030 memcpy(&pdata->tstamp_config, &config, sizeof(config));
1035 static void xgbe_prep_tx_tstamp(struct xgbe_prv_data *pdata,
1036 struct sk_buff *skb,
1037 struct xgbe_packet_data *packet)
1039 unsigned long flags;
1041 if (XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP)) {
1042 spin_lock_irqsave(&pdata->tstamp_lock, flags);
1043 if (pdata->tx_tstamp_skb) {
1044 /* Another timestamp in progress, ignore this one */
1045 XGMAC_SET_BITS(packet->attributes,
1046 TX_PACKET_ATTRIBUTES, PTP, 0);
1048 pdata->tx_tstamp_skb = skb_get(skb);
1049 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1051 spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1054 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES, PTP))
1055 skb_tx_timestamp(skb);
1058 static void xgbe_prep_vlan(struct sk_buff *skb, struct xgbe_packet_data *packet)
1060 if (vlan_tx_tag_present(skb))
1061 packet->vlan_ctag = vlan_tx_tag_get(skb);
1064 static int xgbe_prep_tso(struct sk_buff *skb, struct xgbe_packet_data *packet)
1068 if (!XGMAC_GET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1072 ret = skb_cow_head(skb, 0);
1076 packet->header_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
1077 packet->tcp_header_len = tcp_hdrlen(skb);
1078 packet->tcp_payload_len = skb->len - packet->header_len;
1079 packet->mss = skb_shinfo(skb)->gso_size;
1080 DBGPR(" packet->header_len=%u\n", packet->header_len);
1081 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1082 packet->tcp_header_len, packet->tcp_payload_len);
1083 DBGPR(" packet->mss=%u\n", packet->mss);
1088 static int xgbe_is_tso(struct sk_buff *skb)
1090 if (skb->ip_summed != CHECKSUM_PARTIAL)
1093 if (!skb_is_gso(skb))
1096 DBGPR(" TSO packet to be processed\n");
1101 static void xgbe_packet_info(struct xgbe_prv_data *pdata,
1102 struct xgbe_ring *ring, struct sk_buff *skb,
1103 struct xgbe_packet_data *packet)
1105 struct skb_frag_struct *frag;
1106 unsigned int context_desc;
1111 packet->rdesc_count = 0;
1113 if (xgbe_is_tso(skb)) {
1114 /* TSO requires an extra desriptor if mss is different */
1115 if (skb_shinfo(skb)->gso_size != ring->tx.cur_mss) {
1117 packet->rdesc_count++;
1120 /* TSO requires an extra desriptor for TSO header */
1121 packet->rdesc_count++;
1123 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1125 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1127 } else if (skb->ip_summed == CHECKSUM_PARTIAL)
1128 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1131 if (vlan_tx_tag_present(skb)) {
1132 /* VLAN requires an extra descriptor if tag is different */
1133 if (vlan_tx_tag_get(skb) != ring->tx.cur_vlan_ctag)
1134 /* We can share with the TSO context descriptor */
1135 if (!context_desc) {
1137 packet->rdesc_count++;
1140 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1144 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1145 (pdata->tstamp_config.tx_type == HWTSTAMP_TX_ON))
1146 XGMAC_SET_BITS(packet->attributes, TX_PACKET_ATTRIBUTES,
1149 for (len = skb_headlen(skb); len;) {
1150 packet->rdesc_count++;
1151 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 frag = &skb_shinfo(skb)->frags[i];
1156 for (len = skb_frag_size(frag); len; ) {
1157 packet->rdesc_count++;
1158 len -= min_t(unsigned int, len, XGBE_TX_MAX_BUF_SIZE);
1163 static int xgbe_open(struct net_device *netdev)
1165 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1166 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1167 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1170 DBGPR("-->xgbe_open\n");
1172 /* Initialize the phy */
1173 ret = xgbe_phy_init(pdata);
1177 /* Enable the clocks */
1178 ret = clk_prepare_enable(pdata->sysclk);
1180 netdev_alert(netdev, "dma clk_prepare_enable failed\n");
1184 ret = clk_prepare_enable(pdata->ptpclk);
1186 netdev_alert(netdev, "ptp clk_prepare_enable failed\n");
1190 /* Calculate the Rx buffer size before allocating rings */
1191 ret = xgbe_calc_rx_buf_size(netdev, netdev->mtu);
1194 pdata->rx_buf_size = ret;
1196 /* Allocate the channel and ring structures */
1197 ret = xgbe_alloc_channels(pdata);
1201 /* Allocate the ring descriptors and buffers */
1202 ret = desc_if->alloc_ring_resources(pdata);
1206 /* Initialize the device restart and Tx timestamp work struct */
1207 INIT_WORK(&pdata->restart_work, xgbe_restart);
1208 INIT_WORK(&pdata->tx_tstamp_work, xgbe_tx_tstamp);
1210 /* Request interrupts */
1211 ret = devm_request_irq(pdata->dev, netdev->irq, xgbe_isr, 0,
1212 netdev->name, pdata);
1214 netdev_alert(netdev, "error requesting irq %d\n",
1218 pdata->irq_number = netdev->irq;
1220 ret = xgbe_start(pdata);
1224 DBGPR("<--xgbe_open\n");
1231 devm_free_irq(pdata->dev, pdata->irq_number, pdata);
1232 pdata->irq_number = 0;
1235 desc_if->free_ring_resources(pdata);
1238 xgbe_free_channels(pdata);
1241 clk_disable_unprepare(pdata->ptpclk);
1244 clk_disable_unprepare(pdata->sysclk);
1247 xgbe_phy_exit(pdata);
1252 static int xgbe_close(struct net_device *netdev)
1254 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1255 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1256 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1258 DBGPR("-->xgbe_close\n");
1260 /* Stop the device */
1263 /* Issue software reset to device */
1266 /* Free the ring descriptors and buffers */
1267 desc_if->free_ring_resources(pdata);
1269 /* Free the channel and ring structures */
1270 xgbe_free_channels(pdata);
1272 /* Release the interrupt */
1273 if (pdata->irq_number != 0) {
1274 devm_free_irq(pdata->dev, pdata->irq_number, pdata);
1275 pdata->irq_number = 0;
1278 /* Disable the clocks */
1279 clk_disable_unprepare(pdata->ptpclk);
1280 clk_disable_unprepare(pdata->sysclk);
1282 /* Release the phy */
1283 xgbe_phy_exit(pdata);
1285 DBGPR("<--xgbe_close\n");
1290 static int xgbe_xmit(struct sk_buff *skb, struct net_device *netdev)
1292 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1293 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1294 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1295 struct xgbe_channel *channel;
1296 struct xgbe_ring *ring;
1297 struct xgbe_packet_data *packet;
1298 unsigned long flags;
1301 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb->len);
1303 channel = pdata->channel + skb->queue_mapping;
1304 ring = channel->tx_ring;
1305 packet = &ring->packet_data;
1309 spin_lock_irqsave(&ring->lock, flags);
1311 if (skb->len == 0) {
1312 netdev_err(netdev, "empty skb received from stack\n");
1313 dev_kfree_skb_any(skb);
1314 goto tx_netdev_return;
1317 /* Calculate preliminary packet info */
1318 memset(packet, 0, sizeof(*packet));
1319 xgbe_packet_info(pdata, ring, skb, packet);
1321 /* Check that there are enough descriptors available */
1322 if (packet->rdesc_count > xgbe_tx_avail_desc(ring)) {
1323 DBGPR(" Tx queue stopped, not enough descriptors available\n");
1324 netif_stop_subqueue(netdev, channel->queue_index);
1325 ring->tx.queue_stopped = 1;
1326 ret = NETDEV_TX_BUSY;
1327 goto tx_netdev_return;
1330 ret = xgbe_prep_tso(skb, packet);
1332 netdev_err(netdev, "error processing TSO packet\n");
1333 dev_kfree_skb_any(skb);
1334 goto tx_netdev_return;
1336 xgbe_prep_vlan(skb, packet);
1338 if (!desc_if->map_tx_skb(channel, skb)) {
1339 dev_kfree_skb_any(skb);
1340 goto tx_netdev_return;
1343 xgbe_prep_tx_tstamp(pdata, skb, packet);
1345 /* Configure required descriptor fields for transmission */
1346 hw_if->dev_xmit(channel);
1348 #ifdef XGMAC_ENABLE_TX_PKT_DUMP
1349 xgbe_print_pkt(netdev, skb, true);
1353 spin_unlock_irqrestore(&ring->lock, flags);
1355 DBGPR("<--xgbe_xmit\n");
1360 static void xgbe_set_rx_mode(struct net_device *netdev)
1362 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1363 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1364 unsigned int pr_mode, am_mode;
1366 DBGPR("-->xgbe_set_rx_mode\n");
1368 pr_mode = ((netdev->flags & IFF_PROMISC) != 0);
1369 am_mode = ((netdev->flags & IFF_ALLMULTI) != 0);
1371 hw_if->set_promiscuous_mode(pdata, pr_mode);
1372 hw_if->set_all_multicast_mode(pdata, am_mode);
1374 hw_if->add_mac_addresses(pdata);
1376 DBGPR("<--xgbe_set_rx_mode\n");
1379 static int xgbe_set_mac_address(struct net_device *netdev, void *addr)
1381 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1382 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1383 struct sockaddr *saddr = addr;
1385 DBGPR("-->xgbe_set_mac_address\n");
1387 if (!is_valid_ether_addr(saddr->sa_data))
1388 return -EADDRNOTAVAIL;
1390 memcpy(netdev->dev_addr, saddr->sa_data, netdev->addr_len);
1392 hw_if->set_mac_address(pdata, netdev->dev_addr);
1394 DBGPR("<--xgbe_set_mac_address\n");
1399 static int xgbe_ioctl(struct net_device *netdev, struct ifreq *ifreq, int cmd)
1401 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1406 ret = xgbe_get_hwtstamp_settings(pdata, ifreq);
1410 ret = xgbe_set_hwtstamp_settings(pdata, ifreq);
1420 static int xgbe_change_mtu(struct net_device *netdev, int mtu)
1422 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1425 DBGPR("-->xgbe_change_mtu\n");
1427 ret = xgbe_calc_rx_buf_size(netdev, mtu);
1431 pdata->rx_buf_size = ret;
1434 xgbe_restart_dev(pdata, 0);
1436 DBGPR("<--xgbe_change_mtu\n");
1441 static struct rtnl_link_stats64 *xgbe_get_stats64(struct net_device *netdev,
1442 struct rtnl_link_stats64 *s)
1444 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1445 struct xgbe_mmc_stats *pstats = &pdata->mmc_stats;
1447 DBGPR("-->%s\n", __func__);
1449 pdata->hw_if.read_mmc_stats(pdata);
1451 s->rx_packets = pstats->rxframecount_gb;
1452 s->rx_bytes = pstats->rxoctetcount_gb;
1453 s->rx_errors = pstats->rxframecount_gb -
1454 pstats->rxbroadcastframes_g -
1455 pstats->rxmulticastframes_g -
1456 pstats->rxunicastframes_g;
1457 s->multicast = pstats->rxmulticastframes_g;
1458 s->rx_length_errors = pstats->rxlengtherror;
1459 s->rx_crc_errors = pstats->rxcrcerror;
1460 s->rx_fifo_errors = pstats->rxfifooverflow;
1462 s->tx_packets = pstats->txframecount_gb;
1463 s->tx_bytes = pstats->txoctetcount_gb;
1464 s->tx_errors = pstats->txframecount_gb - pstats->txframecount_g;
1465 s->tx_dropped = netdev->stats.tx_dropped;
1467 DBGPR("<--%s\n", __func__);
1472 static int xgbe_vlan_rx_add_vid(struct net_device *netdev, __be16 proto,
1475 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1476 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1478 DBGPR("-->%s\n", __func__);
1480 set_bit(vid, pdata->active_vlans);
1481 hw_if->update_vlan_hash_table(pdata);
1483 DBGPR("<--%s\n", __func__);
1488 static int xgbe_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto,
1491 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1492 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1494 DBGPR("-->%s\n", __func__);
1496 clear_bit(vid, pdata->active_vlans);
1497 hw_if->update_vlan_hash_table(pdata);
1499 DBGPR("<--%s\n", __func__);
1504 #ifdef CONFIG_NET_POLL_CONTROLLER
1505 static void xgbe_poll_controller(struct net_device *netdev)
1507 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1509 DBGPR("-->xgbe_poll_controller\n");
1511 disable_irq(pdata->irq_number);
1513 xgbe_isr(pdata->irq_number, pdata);
1515 enable_irq(pdata->irq_number);
1517 DBGPR("<--xgbe_poll_controller\n");
1519 #endif /* End CONFIG_NET_POLL_CONTROLLER */
1521 static int xgbe_setup_tc(struct net_device *netdev, u8 tc)
1523 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1524 unsigned int offset, queue;
1527 if (tc && (tc != pdata->hw_feat.tc_cnt))
1531 netdev_set_num_tc(netdev, tc);
1532 for (i = 0, queue = 0, offset = 0; i < tc; i++) {
1533 while ((queue < pdata->tx_q_count) &&
1534 (pdata->q2tc_map[queue] == i))
1537 DBGPR(" TC%u using TXq%u-%u\n", i, offset, queue - 1);
1538 netdev_set_tc_queue(netdev, i, queue - offset, offset);
1542 netdev_reset_tc(netdev);
1548 static int xgbe_set_features(struct net_device *netdev,
1549 netdev_features_t features)
1551 struct xgbe_prv_data *pdata = netdev_priv(netdev);
1552 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1553 netdev_features_t rxcsum, rxvlan, rxvlan_filter;
1555 rxcsum = pdata->netdev_features & NETIF_F_RXCSUM;
1556 rxvlan = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_RX;
1557 rxvlan_filter = pdata->netdev_features & NETIF_F_HW_VLAN_CTAG_FILTER;
1559 if ((features & NETIF_F_RXCSUM) && !rxcsum)
1560 hw_if->enable_rx_csum(pdata);
1561 else if (!(features & NETIF_F_RXCSUM) && rxcsum)
1562 hw_if->disable_rx_csum(pdata);
1564 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && !rxvlan)
1565 hw_if->enable_rx_vlan_stripping(pdata);
1566 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && rxvlan)
1567 hw_if->disable_rx_vlan_stripping(pdata);
1569 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && !rxvlan_filter)
1570 hw_if->enable_rx_vlan_filtering(pdata);
1571 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && rxvlan_filter)
1572 hw_if->disable_rx_vlan_filtering(pdata);
1574 pdata->netdev_features = features;
1576 DBGPR("<--xgbe_set_features\n");
1581 static const struct net_device_ops xgbe_netdev_ops = {
1582 .ndo_open = xgbe_open,
1583 .ndo_stop = xgbe_close,
1584 .ndo_start_xmit = xgbe_xmit,
1585 .ndo_set_rx_mode = xgbe_set_rx_mode,
1586 .ndo_set_mac_address = xgbe_set_mac_address,
1587 .ndo_validate_addr = eth_validate_addr,
1588 .ndo_do_ioctl = xgbe_ioctl,
1589 .ndo_change_mtu = xgbe_change_mtu,
1590 .ndo_get_stats64 = xgbe_get_stats64,
1591 .ndo_vlan_rx_add_vid = xgbe_vlan_rx_add_vid,
1592 .ndo_vlan_rx_kill_vid = xgbe_vlan_rx_kill_vid,
1593 #ifdef CONFIG_NET_POLL_CONTROLLER
1594 .ndo_poll_controller = xgbe_poll_controller,
1596 .ndo_setup_tc = xgbe_setup_tc,
1597 .ndo_set_features = xgbe_set_features,
1600 struct net_device_ops *xgbe_get_netdev_ops(void)
1602 return (struct net_device_ops *)&xgbe_netdev_ops;
1605 static void xgbe_rx_refresh(struct xgbe_channel *channel)
1607 struct xgbe_prv_data *pdata = channel->pdata;
1608 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1609 struct xgbe_ring *ring = channel->rx_ring;
1610 struct xgbe_ring_data *rdata;
1612 desc_if->realloc_rx_buffer(channel);
1614 /* Update the Rx Tail Pointer Register with address of
1615 * the last cleaned entry */
1616 rdata = XGBE_GET_DESC_DATA(ring, ring->rx.realloc_index - 1);
1617 XGMAC_DMA_IOWRITE(channel, DMA_CH_RDTR_LO,
1618 lower_32_bits(rdata->rdesc_dma));
1621 static struct sk_buff *xgbe_create_skb(struct xgbe_prv_data *pdata,
1622 struct xgbe_ring_data *rdata,
1625 struct net_device *netdev = pdata->netdev;
1626 struct sk_buff *skb;
1628 unsigned int copy_len;
1630 skb = netdev_alloc_skb_ip_align(netdev, rdata->rx_hdr.dma_len);
1634 packet = page_address(rdata->rx_hdr.pa.pages) +
1635 rdata->rx_hdr.pa.pages_offset;
1636 copy_len = (rdata->hdr_len) ? rdata->hdr_len : *len;
1637 copy_len = min(rdata->rx_hdr.dma_len, copy_len);
1638 skb_copy_to_linear_data(skb, packet, copy_len);
1639 skb_put(skb, copy_len);
1646 static int xgbe_tx_poll(struct xgbe_channel *channel)
1648 struct xgbe_prv_data *pdata = channel->pdata;
1649 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1650 struct xgbe_desc_if *desc_if = &pdata->desc_if;
1651 struct xgbe_ring *ring = channel->tx_ring;
1652 struct xgbe_ring_data *rdata;
1653 struct xgbe_ring_desc *rdesc;
1654 struct net_device *netdev = pdata->netdev;
1655 unsigned long flags;
1658 DBGPR("-->xgbe_tx_poll\n");
1660 /* Nothing to do if there isn't a Tx ring for this channel */
1664 spin_lock_irqsave(&ring->lock, flags);
1666 while ((processed < XGBE_TX_DESC_MAX_PROC) &&
1667 (ring->dirty < ring->cur)) {
1668 rdata = XGBE_GET_DESC_DATA(ring, ring->dirty);
1669 rdesc = rdata->rdesc;
1671 if (!hw_if->tx_complete(rdesc))
1674 #ifdef XGMAC_ENABLE_TX_DESC_DUMP
1675 xgbe_dump_tx_desc(ring, ring->dirty, 1, 0);
1678 /* Free the SKB and reset the descriptor for re-use */
1679 desc_if->unmap_rdata(pdata, rdata);
1680 hw_if->tx_desc_reset(rdata);
1686 if ((ring->tx.queue_stopped == 1) &&
1687 (xgbe_tx_avail_desc(ring) > XGBE_TX_DESC_MIN_FREE)) {
1688 ring->tx.queue_stopped = 0;
1689 netif_wake_subqueue(netdev, channel->queue_index);
1692 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed);
1694 spin_unlock_irqrestore(&ring->lock, flags);
1699 static int xgbe_rx_poll(struct xgbe_channel *channel, int budget)
1701 struct xgbe_prv_data *pdata = channel->pdata;
1702 struct xgbe_hw_if *hw_if = &pdata->hw_if;
1703 struct xgbe_ring *ring = channel->rx_ring;
1704 struct xgbe_ring_data *rdata;
1705 struct xgbe_packet_data *packet;
1706 struct net_device *netdev = pdata->netdev;
1707 struct sk_buff *skb;
1708 struct skb_shared_hwtstamps *hwtstamps;
1709 unsigned int incomplete, error, context_next, context;
1710 unsigned int len, put_len, max_len;
1711 unsigned int received = 0;
1712 int packet_count = 0;
1714 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget);
1716 /* Nothing to do if there isn't a Rx ring for this channel */
1720 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1721 packet = &ring->packet_data;
1722 while (packet_count < budget) {
1723 DBGPR(" cur = %d\n", ring->cur);
1725 /* First time in loop see if we need to restore state */
1726 if (!received && rdata->state_saved) {
1727 incomplete = rdata->state.incomplete;
1728 context_next = rdata->state.context_next;
1729 skb = rdata->state.skb;
1730 error = rdata->state.error;
1731 len = rdata->state.len;
1733 memset(packet, 0, sizeof(*packet));
1742 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1744 if (ring->dirty > (XGBE_RX_DESC_CNT >> 3))
1745 xgbe_rx_refresh(channel);
1747 if (hw_if->dev_read(channel))
1754 incomplete = XGMAC_GET_BITS(packet->attributes,
1755 RX_PACKET_ATTRIBUTES,
1757 context_next = XGMAC_GET_BITS(packet->attributes,
1758 RX_PACKET_ATTRIBUTES,
1760 context = XGMAC_GET_BITS(packet->attributes,
1761 RX_PACKET_ATTRIBUTES,
1764 /* Earlier error, just drain the remaining data */
1765 if ((incomplete || context_next) && error)
1768 if (error || packet->errors) {
1770 DBGPR("Error in received packet\n");
1776 put_len = rdata->len - len;
1780 dma_sync_single_for_cpu(pdata->dev,
1782 rdata->rx_hdr.dma_len,
1785 skb = xgbe_create_skb(pdata, rdata, &put_len);
1793 dma_sync_single_for_cpu(pdata->dev,
1795 rdata->rx_buf.dma_len,
1798 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
1799 rdata->rx_buf.pa.pages,
1800 rdata->rx_buf.pa.pages_offset,
1801 put_len, rdata->rx_buf.dma_len);
1802 rdata->rx_buf.pa.pages = NULL;
1806 if (incomplete || context_next)
1809 /* Stray Context Descriptor? */
1813 /* Be sure we don't exceed the configured MTU */
1814 max_len = netdev->mtu + ETH_HLEN;
1815 if (!(netdev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
1816 (skb->protocol == htons(ETH_P_8021Q)))
1817 max_len += VLAN_HLEN;
1819 if (skb->len > max_len) {
1820 DBGPR("packet length exceeds configured MTU\n");
1825 #ifdef XGMAC_ENABLE_RX_PKT_DUMP
1826 xgbe_print_pkt(netdev, skb, false);
1829 skb_checksum_none_assert(skb);
1830 if (XGMAC_GET_BITS(packet->attributes,
1831 RX_PACKET_ATTRIBUTES, CSUM_DONE))
1832 skb->ip_summed = CHECKSUM_UNNECESSARY;
1834 if (XGMAC_GET_BITS(packet->attributes,
1835 RX_PACKET_ATTRIBUTES, VLAN_CTAG))
1836 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1839 if (XGMAC_GET_BITS(packet->attributes,
1840 RX_PACKET_ATTRIBUTES, RX_TSTAMP)) {
1843 nsec = timecounter_cyc2time(&pdata->tstamp_tc,
1845 hwtstamps = skb_hwtstamps(skb);
1846 hwtstamps->hwtstamp = ns_to_ktime(nsec);
1850 skb->protocol = eth_type_trans(skb, netdev);
1851 skb_record_rx_queue(skb, channel->queue_index);
1852 skb_mark_napi_id(skb, &pdata->napi);
1854 netdev->last_rx = jiffies;
1855 napi_gro_receive(&pdata->napi, skb);
1861 /* Check if we need to save state before leaving */
1862 if (received && (incomplete || context_next)) {
1863 rdata = XGBE_GET_DESC_DATA(ring, ring->cur);
1864 rdata->state_saved = 1;
1865 rdata->state.incomplete = incomplete;
1866 rdata->state.context_next = context_next;
1867 rdata->state.skb = skb;
1868 rdata->state.len = len;
1869 rdata->state.error = error;
1872 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count);
1874 return packet_count;
1877 static int xgbe_poll(struct napi_struct *napi, int budget)
1879 struct xgbe_prv_data *pdata = container_of(napi, struct xgbe_prv_data,
1881 struct xgbe_channel *channel;
1883 int processed, last_processed;
1886 DBGPR("-->xgbe_poll: budget=%d\n", budget);
1889 ring_budget = budget / pdata->rx_ring_count;
1891 last_processed = processed;
1893 channel = pdata->channel;
1894 for (i = 0; i < pdata->channel_count; i++, channel++) {
1895 /* Cleanup Tx ring first */
1896 xgbe_tx_poll(channel);
1898 /* Process Rx ring next */
1899 if (ring_budget > (budget - processed))
1900 ring_budget = budget - processed;
1901 processed += xgbe_rx_poll(channel, ring_budget);
1903 } while ((processed < budget) && (processed != last_processed));
1905 /* If we processed everything, we are done */
1906 if (processed < budget) {
1907 /* Turn off polling */
1908 napi_complete(napi);
1910 /* Enable Tx and Rx interrupts */
1911 xgbe_enable_rx_tx_ints(pdata);
1914 DBGPR("<--xgbe_poll: received = %d\n", processed);
1919 void xgbe_dump_tx_desc(struct xgbe_ring *ring, unsigned int idx,
1920 unsigned int count, unsigned int flag)
1922 struct xgbe_ring_data *rdata;
1923 struct xgbe_ring_desc *rdesc;
1926 rdata = XGBE_GET_DESC_DATA(ring, idx);
1927 rdesc = rdata->rdesc;
1928 pr_alert("TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx,
1929 (flag == 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
1930 le32_to_cpu(rdesc->desc0), le32_to_cpu(rdesc->desc1),
1931 le32_to_cpu(rdesc->desc2), le32_to_cpu(rdesc->desc3));
1936 void xgbe_dump_rx_desc(struct xgbe_ring *ring, struct xgbe_ring_desc *desc,
1939 pr_alert("RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n", idx,
1940 le32_to_cpu(desc->desc0), le32_to_cpu(desc->desc1),
1941 le32_to_cpu(desc->desc2), le32_to_cpu(desc->desc3));
1944 void xgbe_print_pkt(struct net_device *netdev, struct sk_buff *skb, bool tx_rx)
1946 struct ethhdr *eth = (struct ethhdr *)skb->data;
1947 unsigned char *buf = skb->data;
1948 unsigned char buffer[128];
1951 netdev_alert(netdev, "\n************** SKB dump ****************\n");
1953 netdev_alert(netdev, "%s packet of %d bytes\n",
1954 (tx_rx ? "TX" : "RX"), skb->len);
1956 netdev_alert(netdev, "Dst MAC addr: %pM\n", eth->h_dest);
1957 netdev_alert(netdev, "Src MAC addr: %pM\n", eth->h_source);
1958 netdev_alert(netdev, "Protocol: 0x%04hx\n", ntohs(eth->h_proto));
1960 for (i = 0, j = 0; i < skb->len;) {
1961 j += snprintf(buffer + j, sizeof(buffer) - j, "%02hhx",
1964 if ((i % 32) == 0) {
1965 netdev_alert(netdev, " 0x%04x: %s\n", i - 32, buffer);
1967 } else if ((i % 16) == 0) {
1970 } else if ((i % 4) == 0) {
1975 netdev_alert(netdev, " 0x%04x: %s\n", i - (i % 32), buffer);
1977 netdev_alert(netdev, "\n************** SKB dump ****************\n");