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/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_one_poll(struct napi_struct
*, int);
130 static int xgbe_all_poll(struct napi_struct
*, int);
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
;
139 count
= max_t(unsigned int, pdata
->tx_ring_count
, pdata
->rx_ring_count
);
141 channel_mem
= kcalloc(count
, sizeof(struct xgbe_channel
), GFP_KERNEL
);
145 tx_ring
= kcalloc(pdata
->tx_ring_count
, sizeof(struct xgbe_ring
),
150 rx_ring
= kcalloc(pdata
->rx_ring_count
, sizeof(struct xgbe_ring
),
155 for (i
= 0, channel
= channel_mem
; i
< count
; i
++, channel
++) {
156 snprintf(channel
->name
, sizeof(channel
->name
), "channel-%u", i
);
157 channel
->pdata
= pdata
;
158 channel
->queue_index
= i
;
159 channel
->dma_regs
= pdata
->xgmac_regs
+ DMA_CH_BASE
+
162 if (pdata
->per_channel_irq
)
163 channel
->dma_irq
= pdata
->channel_irq
[i
];
165 if (i
< pdata
->tx_ring_count
) {
166 spin_lock_init(&tx_ring
->lock
);
167 channel
->tx_ring
= tx_ring
++;
170 if (i
< pdata
->rx_ring_count
) {
171 spin_lock_init(&rx_ring
->lock
);
172 channel
->rx_ring
= rx_ring
++;
175 netif_dbg(pdata
, drv
, pdata
->netdev
,
176 "%s: dma_regs=%p, dma_irq=%d, tx=%p, rx=%p\n",
177 channel
->name
, channel
->dma_regs
, channel
->dma_irq
,
178 channel
->tx_ring
, channel
->rx_ring
);
181 pdata
->channel
= channel_mem
;
182 pdata
->channel_count
= count
;
196 static void xgbe_free_channels(struct xgbe_prv_data
*pdata
)
201 kfree(pdata
->channel
->rx_ring
);
202 kfree(pdata
->channel
->tx_ring
);
203 kfree(pdata
->channel
);
205 pdata
->channel
= NULL
;
206 pdata
->channel_count
= 0;
209 static inline unsigned int xgbe_tx_avail_desc(struct xgbe_ring
*ring
)
211 return (ring
->rdesc_count
- (ring
->cur
- ring
->dirty
));
214 static inline unsigned int xgbe_rx_dirty_desc(struct xgbe_ring
*ring
)
216 return (ring
->cur
- ring
->dirty
);
219 static int xgbe_maybe_stop_tx_queue(struct xgbe_channel
*channel
,
220 struct xgbe_ring
*ring
, unsigned int count
)
222 struct xgbe_prv_data
*pdata
= channel
->pdata
;
224 if (count
> xgbe_tx_avail_desc(ring
)) {
225 netif_info(pdata
, drv
, pdata
->netdev
,
226 "Tx queue stopped, not enough descriptors available\n");
227 netif_stop_subqueue(pdata
->netdev
, channel
->queue_index
);
228 ring
->tx
.queue_stopped
= 1;
230 /* If we haven't notified the hardware because of xmit_more
231 * support, tell it now
233 if (ring
->tx
.xmit_more
)
234 pdata
->hw_if
.tx_start_xmit(channel
, ring
);
236 return NETDEV_TX_BUSY
;
242 static int xgbe_calc_rx_buf_size(struct net_device
*netdev
, unsigned int mtu
)
244 unsigned int rx_buf_size
;
246 rx_buf_size
= mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
247 rx_buf_size
= clamp_val(rx_buf_size
, XGBE_RX_MIN_BUF_SIZE
, PAGE_SIZE
);
249 rx_buf_size
= (rx_buf_size
+ XGBE_RX_BUF_ALIGN
- 1) &
250 ~(XGBE_RX_BUF_ALIGN
- 1);
255 static void xgbe_enable_rx_tx_ints(struct xgbe_prv_data
*pdata
)
257 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
258 struct xgbe_channel
*channel
;
259 enum xgbe_int int_id
;
262 channel
= pdata
->channel
;
263 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
264 if (channel
->tx_ring
&& channel
->rx_ring
)
265 int_id
= XGMAC_INT_DMA_CH_SR_TI_RI
;
266 else if (channel
->tx_ring
)
267 int_id
= XGMAC_INT_DMA_CH_SR_TI
;
268 else if (channel
->rx_ring
)
269 int_id
= XGMAC_INT_DMA_CH_SR_RI
;
273 hw_if
->enable_int(channel
, int_id
);
277 static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data
*pdata
)
279 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
280 struct xgbe_channel
*channel
;
281 enum xgbe_int int_id
;
284 channel
= pdata
->channel
;
285 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
286 if (channel
->tx_ring
&& channel
->rx_ring
)
287 int_id
= XGMAC_INT_DMA_CH_SR_TI_RI
;
288 else if (channel
->tx_ring
)
289 int_id
= XGMAC_INT_DMA_CH_SR_TI
;
290 else if (channel
->rx_ring
)
291 int_id
= XGMAC_INT_DMA_CH_SR_RI
;
295 hw_if
->disable_int(channel
, int_id
);
299 static irqreturn_t
xgbe_isr(int irq
, void *data
)
301 struct xgbe_prv_data
*pdata
= data
;
302 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
303 struct xgbe_channel
*channel
;
304 unsigned int dma_isr
, dma_ch_isr
;
305 unsigned int mac_isr
, mac_tssr
;
308 /* The DMA interrupt status register also reports MAC and MTL
309 * interrupts. So for polling mode, we just need to check for
310 * this register to be non-zero
312 dma_isr
= XGMAC_IOREAD(pdata
, DMA_ISR
);
316 netif_dbg(pdata
, intr
, pdata
->netdev
, "DMA_ISR=%#010x\n", dma_isr
);
318 for (i
= 0; i
< pdata
->channel_count
; i
++) {
319 if (!(dma_isr
& (1 << i
)))
322 channel
= pdata
->channel
+ i
;
324 dma_ch_isr
= XGMAC_DMA_IOREAD(channel
, DMA_CH_SR
);
325 netif_dbg(pdata
, intr
, pdata
->netdev
, "DMA_CH%u_ISR=%#010x\n",
328 /* The TI or RI interrupt bits may still be set even if using
329 * per channel DMA interrupts. Check to be sure those are not
330 * enabled before using the private data napi structure.
332 if (!pdata
->per_channel_irq
&&
333 (XGMAC_GET_BITS(dma_ch_isr
, DMA_CH_SR
, TI
) ||
334 XGMAC_GET_BITS(dma_ch_isr
, DMA_CH_SR
, RI
))) {
335 if (napi_schedule_prep(&pdata
->napi
)) {
336 /* Disable Tx and Rx interrupts */
337 xgbe_disable_rx_tx_ints(pdata
);
339 /* Turn on polling */
340 __napi_schedule_irqoff(&pdata
->napi
);
344 if (XGMAC_GET_BITS(dma_ch_isr
, DMA_CH_SR
, RBU
))
345 pdata
->ext_stats
.rx_buffer_unavailable
++;
347 /* Restart the device on a Fatal Bus Error */
348 if (XGMAC_GET_BITS(dma_ch_isr
, DMA_CH_SR
, FBE
))
349 schedule_work(&pdata
->restart_work
);
351 /* Clear all interrupt signals */
352 XGMAC_DMA_IOWRITE(channel
, DMA_CH_SR
, dma_ch_isr
);
355 if (XGMAC_GET_BITS(dma_isr
, DMA_ISR
, MACIS
)) {
356 mac_isr
= XGMAC_IOREAD(pdata
, MAC_ISR
);
358 if (XGMAC_GET_BITS(mac_isr
, MAC_ISR
, MMCTXIS
))
359 hw_if
->tx_mmc_int(pdata
);
361 if (XGMAC_GET_BITS(mac_isr
, MAC_ISR
, MMCRXIS
))
362 hw_if
->rx_mmc_int(pdata
);
364 if (XGMAC_GET_BITS(mac_isr
, MAC_ISR
, TSIS
)) {
365 mac_tssr
= XGMAC_IOREAD(pdata
, MAC_TSSR
);
367 if (XGMAC_GET_BITS(mac_tssr
, MAC_TSSR
, TXTSC
)) {
368 /* Read Tx Timestamp to clear interrupt */
370 hw_if
->get_tx_tstamp(pdata
);
371 queue_work(pdata
->dev_workqueue
,
372 &pdata
->tx_tstamp_work
);
381 static irqreturn_t
xgbe_dma_isr(int irq
, void *data
)
383 struct xgbe_channel
*channel
= data
;
385 /* Per channel DMA interrupts are enabled, so we use the per
386 * channel napi structure and not the private data napi structure
388 if (napi_schedule_prep(&channel
->napi
)) {
389 /* Disable Tx and Rx interrupts */
390 disable_irq_nosync(channel
->dma_irq
);
392 /* Turn on polling */
393 __napi_schedule_irqoff(&channel
->napi
);
399 static void xgbe_tx_timer(unsigned long data
)
401 struct xgbe_channel
*channel
= (struct xgbe_channel
*)data
;
402 struct xgbe_prv_data
*pdata
= channel
->pdata
;
403 struct napi_struct
*napi
;
405 DBGPR("-->xgbe_tx_timer\n");
407 napi
= (pdata
->per_channel_irq
) ? &channel
->napi
: &pdata
->napi
;
409 if (napi_schedule_prep(napi
)) {
410 /* Disable Tx and Rx interrupts */
411 if (pdata
->per_channel_irq
)
412 disable_irq_nosync(channel
->dma_irq
);
414 xgbe_disable_rx_tx_ints(pdata
);
416 /* Turn on polling */
417 __napi_schedule(napi
);
420 channel
->tx_timer_active
= 0;
422 DBGPR("<--xgbe_tx_timer\n");
425 static void xgbe_service(struct work_struct
*work
)
427 struct xgbe_prv_data
*pdata
= container_of(work
,
428 struct xgbe_prv_data
,
431 pdata
->phy_if
.phy_status(pdata
);
434 static void xgbe_service_timer(unsigned long data
)
436 struct xgbe_prv_data
*pdata
= (struct xgbe_prv_data
*)data
;
438 queue_work(pdata
->dev_workqueue
, &pdata
->service_work
);
440 mod_timer(&pdata
->service_timer
, jiffies
+ HZ
);
443 static void xgbe_init_timers(struct xgbe_prv_data
*pdata
)
445 struct xgbe_channel
*channel
;
448 setup_timer(&pdata
->service_timer
, xgbe_service_timer
,
449 (unsigned long)pdata
);
451 channel
= pdata
->channel
;
452 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
453 if (!channel
->tx_ring
)
456 setup_timer(&channel
->tx_timer
, xgbe_tx_timer
,
457 (unsigned long)channel
);
461 static void xgbe_start_timers(struct xgbe_prv_data
*pdata
)
463 mod_timer(&pdata
->service_timer
, jiffies
+ HZ
);
466 static void xgbe_stop_timers(struct xgbe_prv_data
*pdata
)
468 struct xgbe_channel
*channel
;
471 del_timer_sync(&pdata
->service_timer
);
473 channel
= pdata
->channel
;
474 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
475 if (!channel
->tx_ring
)
478 del_timer_sync(&channel
->tx_timer
);
482 void xgbe_get_all_hw_features(struct xgbe_prv_data
*pdata
)
484 unsigned int mac_hfr0
, mac_hfr1
, mac_hfr2
;
485 struct xgbe_hw_features
*hw_feat
= &pdata
->hw_feat
;
487 DBGPR("-->xgbe_get_all_hw_features\n");
489 mac_hfr0
= XGMAC_IOREAD(pdata
, MAC_HWF0R
);
490 mac_hfr1
= XGMAC_IOREAD(pdata
, MAC_HWF1R
);
491 mac_hfr2
= XGMAC_IOREAD(pdata
, MAC_HWF2R
);
493 memset(hw_feat
, 0, sizeof(*hw_feat
));
495 hw_feat
->version
= XGMAC_IOREAD(pdata
, MAC_VR
);
497 /* Hardware feature register 0 */
498 hw_feat
->gmii
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, GMIISEL
);
499 hw_feat
->vlhash
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, VLHASH
);
500 hw_feat
->sma
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, SMASEL
);
501 hw_feat
->rwk
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, RWKSEL
);
502 hw_feat
->mgk
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, MGKSEL
);
503 hw_feat
->mmc
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, MMCSEL
);
504 hw_feat
->aoe
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, ARPOFFSEL
);
505 hw_feat
->ts
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, TSSEL
);
506 hw_feat
->eee
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, EEESEL
);
507 hw_feat
->tx_coe
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, TXCOESEL
);
508 hw_feat
->rx_coe
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, RXCOESEL
);
509 hw_feat
->addn_mac
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
,
511 hw_feat
->ts_src
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, TSSTSSEL
);
512 hw_feat
->sa_vlan_ins
= XGMAC_GET_BITS(mac_hfr0
, MAC_HWF0R
, SAVLANINS
);
514 /* Hardware feature register 1 */
515 hw_feat
->rx_fifo_size
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
,
517 hw_feat
->tx_fifo_size
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
,
519 hw_feat
->adv_ts_hi
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, ADVTHWORD
);
520 hw_feat
->dma_width
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, ADDR64
);
521 hw_feat
->dcb
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, DCBEN
);
522 hw_feat
->sph
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, SPHEN
);
523 hw_feat
->tso
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, TSOEN
);
524 hw_feat
->dma_debug
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, DBGMEMA
);
525 hw_feat
->rss
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, RSSEN
);
526 hw_feat
->tc_cnt
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
, NUMTC
);
527 hw_feat
->hash_table_size
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
,
529 hw_feat
->l3l4_filter_num
= XGMAC_GET_BITS(mac_hfr1
, MAC_HWF1R
,
532 /* Hardware feature register 2 */
533 hw_feat
->rx_q_cnt
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, RXQCNT
);
534 hw_feat
->tx_q_cnt
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, TXQCNT
);
535 hw_feat
->rx_ch_cnt
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, RXCHCNT
);
536 hw_feat
->tx_ch_cnt
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, TXCHCNT
);
537 hw_feat
->pps_out_num
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, PPSOUTNUM
);
538 hw_feat
->aux_snap_num
= XGMAC_GET_BITS(mac_hfr2
, MAC_HWF2R
, AUXSNAPNUM
);
540 /* Translate the Hash Table size into actual number */
541 switch (hw_feat
->hash_table_size
) {
545 hw_feat
->hash_table_size
= 64;
548 hw_feat
->hash_table_size
= 128;
551 hw_feat
->hash_table_size
= 256;
555 /* Translate the address width setting into actual number */
556 switch (hw_feat
->dma_width
) {
558 hw_feat
->dma_width
= 32;
561 hw_feat
->dma_width
= 40;
564 hw_feat
->dma_width
= 48;
567 hw_feat
->dma_width
= 32;
570 /* The Queue, Channel and TC counts are zero based so increment them
571 * to get the actual number
575 hw_feat
->rx_ch_cnt
++;
576 hw_feat
->tx_ch_cnt
++;
579 /* Translate the fifo sizes into actual numbers */
580 hw_feat
->rx_fifo_size
= 1 << (hw_feat
->rx_fifo_size
+ 7);
581 hw_feat
->tx_fifo_size
= 1 << (hw_feat
->tx_fifo_size
+ 7);
583 DBGPR("<--xgbe_get_all_hw_features\n");
586 static void xgbe_napi_enable(struct xgbe_prv_data
*pdata
, unsigned int add
)
588 struct xgbe_channel
*channel
;
591 if (pdata
->per_channel_irq
) {
592 channel
= pdata
->channel
;
593 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
595 netif_napi_add(pdata
->netdev
, &channel
->napi
,
596 xgbe_one_poll
, NAPI_POLL_WEIGHT
);
598 napi_enable(&channel
->napi
);
602 netif_napi_add(pdata
->netdev
, &pdata
->napi
,
603 xgbe_all_poll
, NAPI_POLL_WEIGHT
);
605 napi_enable(&pdata
->napi
);
609 static void xgbe_napi_disable(struct xgbe_prv_data
*pdata
, unsigned int del
)
611 struct xgbe_channel
*channel
;
614 if (pdata
->per_channel_irq
) {
615 channel
= pdata
->channel
;
616 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
617 napi_disable(&channel
->napi
);
620 netif_napi_del(&channel
->napi
);
623 napi_disable(&pdata
->napi
);
626 netif_napi_del(&pdata
->napi
);
630 static int xgbe_request_irqs(struct xgbe_prv_data
*pdata
)
632 struct xgbe_channel
*channel
;
633 struct net_device
*netdev
= pdata
->netdev
;
637 ret
= devm_request_irq(pdata
->dev
, pdata
->dev_irq
, xgbe_isr
, 0,
638 netdev
->name
, pdata
);
640 netdev_alert(netdev
, "error requesting irq %d\n",
645 if (!pdata
->per_channel_irq
)
648 channel
= pdata
->channel
;
649 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
650 snprintf(channel
->dma_irq_name
,
651 sizeof(channel
->dma_irq_name
) - 1,
652 "%s-TxRx-%u", netdev_name(netdev
),
653 channel
->queue_index
);
655 ret
= devm_request_irq(pdata
->dev
, channel
->dma_irq
,
657 channel
->dma_irq_name
, channel
);
659 netdev_alert(netdev
, "error requesting irq %d\n",
668 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
669 for (i
--, channel
--; i
< pdata
->channel_count
; i
--, channel
--)
670 devm_free_irq(pdata
->dev
, channel
->dma_irq
, channel
);
672 devm_free_irq(pdata
->dev
, pdata
->dev_irq
, pdata
);
677 static void xgbe_free_irqs(struct xgbe_prv_data
*pdata
)
679 struct xgbe_channel
*channel
;
682 devm_free_irq(pdata
->dev
, pdata
->dev_irq
, pdata
);
684 if (!pdata
->per_channel_irq
)
687 channel
= pdata
->channel
;
688 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++)
689 devm_free_irq(pdata
->dev
, channel
->dma_irq
, channel
);
692 void xgbe_init_tx_coalesce(struct xgbe_prv_data
*pdata
)
694 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
696 DBGPR("-->xgbe_init_tx_coalesce\n");
698 pdata
->tx_usecs
= XGMAC_INIT_DMA_TX_USECS
;
699 pdata
->tx_frames
= XGMAC_INIT_DMA_TX_FRAMES
;
701 hw_if
->config_tx_coalesce(pdata
);
703 DBGPR("<--xgbe_init_tx_coalesce\n");
706 void xgbe_init_rx_coalesce(struct xgbe_prv_data
*pdata
)
708 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
710 DBGPR("-->xgbe_init_rx_coalesce\n");
712 pdata
->rx_riwt
= hw_if
->usec_to_riwt(pdata
, XGMAC_INIT_DMA_RX_USECS
);
713 pdata
->rx_usecs
= XGMAC_INIT_DMA_RX_USECS
;
714 pdata
->rx_frames
= XGMAC_INIT_DMA_RX_FRAMES
;
716 hw_if
->config_rx_coalesce(pdata
);
718 DBGPR("<--xgbe_init_rx_coalesce\n");
721 static void xgbe_free_tx_data(struct xgbe_prv_data
*pdata
)
723 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
724 struct xgbe_channel
*channel
;
725 struct xgbe_ring
*ring
;
726 struct xgbe_ring_data
*rdata
;
729 DBGPR("-->xgbe_free_tx_data\n");
731 channel
= pdata
->channel
;
732 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
733 ring
= channel
->tx_ring
;
737 for (j
= 0; j
< ring
->rdesc_count
; j
++) {
738 rdata
= XGBE_GET_DESC_DATA(ring
, j
);
739 desc_if
->unmap_rdata(pdata
, rdata
);
743 DBGPR("<--xgbe_free_tx_data\n");
746 static void xgbe_free_rx_data(struct xgbe_prv_data
*pdata
)
748 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
749 struct xgbe_channel
*channel
;
750 struct xgbe_ring
*ring
;
751 struct xgbe_ring_data
*rdata
;
754 DBGPR("-->xgbe_free_rx_data\n");
756 channel
= pdata
->channel
;
757 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
758 ring
= channel
->rx_ring
;
762 for (j
= 0; j
< ring
->rdesc_count
; j
++) {
763 rdata
= XGBE_GET_DESC_DATA(ring
, j
);
764 desc_if
->unmap_rdata(pdata
, rdata
);
768 DBGPR("<--xgbe_free_rx_data\n");
771 static int xgbe_phy_reset(struct xgbe_prv_data
*pdata
)
773 pdata
->phy_link
= -1;
774 pdata
->phy_speed
= SPEED_UNKNOWN
;
776 return pdata
->phy_if
.phy_reset(pdata
);
779 int xgbe_powerdown(struct net_device
*netdev
, unsigned int caller
)
781 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
782 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
785 DBGPR("-->xgbe_powerdown\n");
787 if (!netif_running(netdev
) ||
788 (caller
== XGMAC_IOCTL_CONTEXT
&& pdata
->power_down
)) {
789 netdev_alert(netdev
, "Device is already powered down\n");
790 DBGPR("<--xgbe_powerdown\n");
794 spin_lock_irqsave(&pdata
->lock
, flags
);
796 if (caller
== XGMAC_DRIVER_CONTEXT
)
797 netif_device_detach(netdev
);
799 netif_tx_stop_all_queues(netdev
);
801 xgbe_stop_timers(pdata
);
802 flush_workqueue(pdata
->dev_workqueue
);
804 hw_if
->powerdown_tx(pdata
);
805 hw_if
->powerdown_rx(pdata
);
807 xgbe_napi_disable(pdata
, 0);
809 pdata
->power_down
= 1;
811 spin_unlock_irqrestore(&pdata
->lock
, flags
);
813 DBGPR("<--xgbe_powerdown\n");
818 int xgbe_powerup(struct net_device
*netdev
, unsigned int caller
)
820 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
821 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
824 DBGPR("-->xgbe_powerup\n");
826 if (!netif_running(netdev
) ||
827 (caller
== XGMAC_IOCTL_CONTEXT
&& !pdata
->power_down
)) {
828 netdev_alert(netdev
, "Device is already powered up\n");
829 DBGPR("<--xgbe_powerup\n");
833 spin_lock_irqsave(&pdata
->lock
, flags
);
835 pdata
->power_down
= 0;
837 xgbe_napi_enable(pdata
, 0);
839 hw_if
->powerup_tx(pdata
);
840 hw_if
->powerup_rx(pdata
);
842 if (caller
== XGMAC_DRIVER_CONTEXT
)
843 netif_device_attach(netdev
);
845 netif_tx_start_all_queues(netdev
);
847 xgbe_start_timers(pdata
);
849 spin_unlock_irqrestore(&pdata
->lock
, flags
);
851 DBGPR("<--xgbe_powerup\n");
856 static int xgbe_start(struct xgbe_prv_data
*pdata
)
858 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
859 struct xgbe_phy_if
*phy_if
= &pdata
->phy_if
;
860 struct net_device
*netdev
= pdata
->netdev
;
863 DBGPR("-->xgbe_start\n");
867 ret
= phy_if
->phy_start(pdata
);
871 xgbe_napi_enable(pdata
, 1);
873 ret
= xgbe_request_irqs(pdata
);
877 hw_if
->enable_tx(pdata
);
878 hw_if
->enable_rx(pdata
);
880 netif_tx_start_all_queues(netdev
);
882 xgbe_start_timers(pdata
);
883 queue_work(pdata
->dev_workqueue
, &pdata
->service_work
);
885 DBGPR("<--xgbe_start\n");
890 xgbe_napi_disable(pdata
, 1);
892 phy_if
->phy_stop(pdata
);
900 static void xgbe_stop(struct xgbe_prv_data
*pdata
)
902 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
903 struct xgbe_phy_if
*phy_if
= &pdata
->phy_if
;
904 struct xgbe_channel
*channel
;
905 struct net_device
*netdev
= pdata
->netdev
;
906 struct netdev_queue
*txq
;
909 DBGPR("-->xgbe_stop\n");
911 netif_tx_stop_all_queues(netdev
);
913 xgbe_stop_timers(pdata
);
914 flush_workqueue(pdata
->dev_workqueue
);
916 hw_if
->disable_tx(pdata
);
917 hw_if
->disable_rx(pdata
);
919 xgbe_free_irqs(pdata
);
921 xgbe_napi_disable(pdata
, 1);
923 phy_if
->phy_stop(pdata
);
927 channel
= pdata
->channel
;
928 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
929 if (!channel
->tx_ring
)
932 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
933 netdev_tx_reset_queue(txq
);
936 DBGPR("<--xgbe_stop\n");
939 static void xgbe_restart_dev(struct xgbe_prv_data
*pdata
)
941 DBGPR("-->xgbe_restart_dev\n");
943 /* If not running, "restart" will happen on open */
944 if (!netif_running(pdata
->netdev
))
949 xgbe_free_tx_data(pdata
);
950 xgbe_free_rx_data(pdata
);
954 DBGPR("<--xgbe_restart_dev\n");
957 static void xgbe_restart(struct work_struct
*work
)
959 struct xgbe_prv_data
*pdata
= container_of(work
,
960 struct xgbe_prv_data
,
965 xgbe_restart_dev(pdata
);
970 static void xgbe_tx_tstamp(struct work_struct
*work
)
972 struct xgbe_prv_data
*pdata
= container_of(work
,
973 struct xgbe_prv_data
,
975 struct skb_shared_hwtstamps hwtstamps
;
979 if (pdata
->tx_tstamp
) {
980 nsec
= timecounter_cyc2time(&pdata
->tstamp_tc
,
983 memset(&hwtstamps
, 0, sizeof(hwtstamps
));
984 hwtstamps
.hwtstamp
= ns_to_ktime(nsec
);
985 skb_tstamp_tx(pdata
->tx_tstamp_skb
, &hwtstamps
);
988 dev_kfree_skb_any(pdata
->tx_tstamp_skb
);
990 spin_lock_irqsave(&pdata
->tstamp_lock
, flags
);
991 pdata
->tx_tstamp_skb
= NULL
;
992 spin_unlock_irqrestore(&pdata
->tstamp_lock
, flags
);
995 static int xgbe_get_hwtstamp_settings(struct xgbe_prv_data
*pdata
,
998 if (copy_to_user(ifreq
->ifr_data
, &pdata
->tstamp_config
,
999 sizeof(pdata
->tstamp_config
)))
1005 static int xgbe_set_hwtstamp_settings(struct xgbe_prv_data
*pdata
,
1006 struct ifreq
*ifreq
)
1008 struct hwtstamp_config config
;
1009 unsigned int mac_tscr
;
1011 if (copy_from_user(&config
, ifreq
->ifr_data
, sizeof(config
)))
1019 switch (config
.tx_type
) {
1020 case HWTSTAMP_TX_OFF
:
1023 case HWTSTAMP_TX_ON
:
1024 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1031 switch (config
.rx_filter
) {
1032 case HWTSTAMP_FILTER_NONE
:
1035 case HWTSTAMP_FILTER_ALL
:
1036 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENALL
, 1);
1037 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1040 /* PTP v2, UDP, any kind of event packet */
1041 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1042 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1043 /* PTP v1, UDP, any kind of event packet */
1044 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1045 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1046 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1047 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, SNAPTYPSEL
, 1);
1048 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1051 /* PTP v2, UDP, Sync packet */
1052 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1053 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1054 /* PTP v1, UDP, Sync packet */
1055 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1056 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1057 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1058 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1059 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1062 /* PTP v2, UDP, Delay_req packet */
1063 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1064 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1065 /* PTP v1, UDP, Delay_req packet */
1066 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1067 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1068 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1069 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1070 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSMSTRENA
, 1);
1071 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1074 /* 802.AS1, Ethernet, any kind of event packet */
1075 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1076 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, AV8021ASMEN
, 1);
1077 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, SNAPTYPSEL
, 1);
1078 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1081 /* 802.AS1, Ethernet, Sync packet */
1082 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1083 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, AV8021ASMEN
, 1);
1084 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1085 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1088 /* 802.AS1, Ethernet, Delay_req packet */
1089 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1090 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, AV8021ASMEN
, 1);
1091 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSMSTRENA
, 1);
1092 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1093 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1096 /* PTP v2/802.AS1, any layer, any kind of event packet */
1097 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1098 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1099 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPENA
, 1);
1100 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1101 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1102 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, SNAPTYPSEL
, 1);
1103 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1106 /* PTP v2/802.AS1, any layer, Sync packet */
1107 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1108 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1109 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPENA
, 1);
1110 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1111 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1112 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1113 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1116 /* PTP v2/802.AS1, any layer, Delay_req packet */
1117 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1118 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSVER2ENA
, 1);
1119 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPENA
, 1);
1120 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV4ENA
, 1);
1121 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSIPV6ENA
, 1);
1122 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSMSTRENA
, 1);
1123 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSEVNTENA
, 1);
1124 XGMAC_SET_BITS(mac_tscr
, MAC_TSCR
, TSENA
, 1);
1131 pdata
->hw_if
.config_tstamp(pdata
, mac_tscr
);
1133 memcpy(&pdata
->tstamp_config
, &config
, sizeof(config
));
1138 static void xgbe_prep_tx_tstamp(struct xgbe_prv_data
*pdata
,
1139 struct sk_buff
*skb
,
1140 struct xgbe_packet_data
*packet
)
1142 unsigned long flags
;
1144 if (XGMAC_GET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
, PTP
)) {
1145 spin_lock_irqsave(&pdata
->tstamp_lock
, flags
);
1146 if (pdata
->tx_tstamp_skb
) {
1147 /* Another timestamp in progress, ignore this one */
1148 XGMAC_SET_BITS(packet
->attributes
,
1149 TX_PACKET_ATTRIBUTES
, PTP
, 0);
1151 pdata
->tx_tstamp_skb
= skb_get(skb
);
1152 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
1154 spin_unlock_irqrestore(&pdata
->tstamp_lock
, flags
);
1157 if (!XGMAC_GET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
, PTP
))
1158 skb_tx_timestamp(skb
);
1161 static void xgbe_prep_vlan(struct sk_buff
*skb
, struct xgbe_packet_data
*packet
)
1163 if (skb_vlan_tag_present(skb
))
1164 packet
->vlan_ctag
= skb_vlan_tag_get(skb
);
1167 static int xgbe_prep_tso(struct sk_buff
*skb
, struct xgbe_packet_data
*packet
)
1171 if (!XGMAC_GET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1175 ret
= skb_cow_head(skb
, 0);
1179 packet
->header_len
= skb_transport_offset(skb
) + tcp_hdrlen(skb
);
1180 packet
->tcp_header_len
= tcp_hdrlen(skb
);
1181 packet
->tcp_payload_len
= skb
->len
- packet
->header_len
;
1182 packet
->mss
= skb_shinfo(skb
)->gso_size
;
1183 DBGPR(" packet->header_len=%u\n", packet
->header_len
);
1184 DBGPR(" packet->tcp_header_len=%u, packet->tcp_payload_len=%u\n",
1185 packet
->tcp_header_len
, packet
->tcp_payload_len
);
1186 DBGPR(" packet->mss=%u\n", packet
->mss
);
1188 /* Update the number of packets that will ultimately be transmitted
1189 * along with the extra bytes for each extra packet
1191 packet
->tx_packets
= skb_shinfo(skb
)->gso_segs
;
1192 packet
->tx_bytes
+= (packet
->tx_packets
- 1) * packet
->header_len
;
1197 static int xgbe_is_tso(struct sk_buff
*skb
)
1199 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
1202 if (!skb_is_gso(skb
))
1205 DBGPR(" TSO packet to be processed\n");
1210 static void xgbe_packet_info(struct xgbe_prv_data
*pdata
,
1211 struct xgbe_ring
*ring
, struct sk_buff
*skb
,
1212 struct xgbe_packet_data
*packet
)
1214 struct skb_frag_struct
*frag
;
1215 unsigned int context_desc
;
1222 packet
->rdesc_count
= 0;
1224 packet
->tx_packets
= 1;
1225 packet
->tx_bytes
= skb
->len
;
1227 if (xgbe_is_tso(skb
)) {
1228 /* TSO requires an extra descriptor if mss is different */
1229 if (skb_shinfo(skb
)->gso_size
!= ring
->tx
.cur_mss
) {
1231 packet
->rdesc_count
++;
1234 /* TSO requires an extra descriptor for TSO header */
1235 packet
->rdesc_count
++;
1237 XGMAC_SET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1239 XGMAC_SET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1241 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
1242 XGMAC_SET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1245 if (skb_vlan_tag_present(skb
)) {
1246 /* VLAN requires an extra descriptor if tag is different */
1247 if (skb_vlan_tag_get(skb
) != ring
->tx
.cur_vlan_ctag
)
1248 /* We can share with the TSO context descriptor */
1249 if (!context_desc
) {
1251 packet
->rdesc_count
++;
1254 XGMAC_SET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1258 if ((skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) &&
1259 (pdata
->tstamp_config
.tx_type
== HWTSTAMP_TX_ON
))
1260 XGMAC_SET_BITS(packet
->attributes
, TX_PACKET_ATTRIBUTES
,
1263 for (len
= skb_headlen(skb
); len
;) {
1264 packet
->rdesc_count
++;
1265 len
-= min_t(unsigned int, len
, XGBE_TX_MAX_BUF_SIZE
);
1268 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
1269 frag
= &skb_shinfo(skb
)->frags
[i
];
1270 for (len
= skb_frag_size(frag
); len
; ) {
1271 packet
->rdesc_count
++;
1272 len
-= min_t(unsigned int, len
, XGBE_TX_MAX_BUF_SIZE
);
1277 static int xgbe_open(struct net_device
*netdev
)
1279 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1280 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
1283 DBGPR("-->xgbe_open\n");
1285 /* Reset the phy settings */
1286 ret
= xgbe_phy_reset(pdata
);
1290 /* Enable the clocks */
1291 ret
= clk_prepare_enable(pdata
->sysclk
);
1293 netdev_alert(netdev
, "dma clk_prepare_enable failed\n");
1297 ret
= clk_prepare_enable(pdata
->ptpclk
);
1299 netdev_alert(netdev
, "ptp clk_prepare_enable failed\n");
1303 /* Calculate the Rx buffer size before allocating rings */
1304 ret
= xgbe_calc_rx_buf_size(netdev
, netdev
->mtu
);
1307 pdata
->rx_buf_size
= ret
;
1309 /* Allocate the channel and ring structures */
1310 ret
= xgbe_alloc_channels(pdata
);
1314 /* Allocate the ring descriptors and buffers */
1315 ret
= desc_if
->alloc_ring_resources(pdata
);
1319 INIT_WORK(&pdata
->service_work
, xgbe_service
);
1320 INIT_WORK(&pdata
->restart_work
, xgbe_restart
);
1321 INIT_WORK(&pdata
->tx_tstamp_work
, xgbe_tx_tstamp
);
1322 xgbe_init_timers(pdata
);
1324 ret
= xgbe_start(pdata
);
1328 clear_bit(XGBE_DOWN
, &pdata
->dev_state
);
1330 DBGPR("<--xgbe_open\n");
1335 desc_if
->free_ring_resources(pdata
);
1338 xgbe_free_channels(pdata
);
1341 clk_disable_unprepare(pdata
->ptpclk
);
1344 clk_disable_unprepare(pdata
->sysclk
);
1349 static int xgbe_close(struct net_device
*netdev
)
1351 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1352 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
1354 DBGPR("-->xgbe_close\n");
1356 /* Stop the device */
1359 /* Free the ring descriptors and buffers */
1360 desc_if
->free_ring_resources(pdata
);
1362 /* Free the channel and ring structures */
1363 xgbe_free_channels(pdata
);
1365 /* Disable the clocks */
1366 clk_disable_unprepare(pdata
->ptpclk
);
1367 clk_disable_unprepare(pdata
->sysclk
);
1369 set_bit(XGBE_DOWN
, &pdata
->dev_state
);
1371 DBGPR("<--xgbe_close\n");
1376 static int xgbe_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
1378 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1379 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1380 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
1381 struct xgbe_channel
*channel
;
1382 struct xgbe_ring
*ring
;
1383 struct xgbe_packet_data
*packet
;
1384 struct netdev_queue
*txq
;
1387 DBGPR("-->xgbe_xmit: skb->len = %d\n", skb
->len
);
1389 channel
= pdata
->channel
+ skb
->queue_mapping
;
1390 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
1391 ring
= channel
->tx_ring
;
1392 packet
= &ring
->packet_data
;
1396 if (skb
->len
== 0) {
1397 netif_err(pdata
, tx_err
, netdev
,
1398 "empty skb received from stack\n");
1399 dev_kfree_skb_any(skb
);
1400 goto tx_netdev_return
;
1403 /* Calculate preliminary packet info */
1404 memset(packet
, 0, sizeof(*packet
));
1405 xgbe_packet_info(pdata
, ring
, skb
, packet
);
1407 /* Check that there are enough descriptors available */
1408 ret
= xgbe_maybe_stop_tx_queue(channel
, ring
, packet
->rdesc_count
);
1410 goto tx_netdev_return
;
1412 ret
= xgbe_prep_tso(skb
, packet
);
1414 netif_err(pdata
, tx_err
, netdev
,
1415 "error processing TSO packet\n");
1416 dev_kfree_skb_any(skb
);
1417 goto tx_netdev_return
;
1419 xgbe_prep_vlan(skb
, packet
);
1421 if (!desc_if
->map_tx_skb(channel
, skb
)) {
1422 dev_kfree_skb_any(skb
);
1423 goto tx_netdev_return
;
1426 xgbe_prep_tx_tstamp(pdata
, skb
, packet
);
1428 /* Report on the actual number of bytes (to be) sent */
1429 netdev_tx_sent_queue(txq
, packet
->tx_bytes
);
1431 /* Configure required descriptor fields for transmission */
1432 hw_if
->dev_xmit(channel
);
1434 if (netif_msg_pktdata(pdata
))
1435 xgbe_print_pkt(netdev
, skb
, true);
1437 /* Stop the queue in advance if there may not be enough descriptors */
1438 xgbe_maybe_stop_tx_queue(channel
, ring
, XGBE_TX_MAX_DESCS
);
1446 static void xgbe_set_rx_mode(struct net_device
*netdev
)
1448 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1449 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1451 DBGPR("-->xgbe_set_rx_mode\n");
1453 hw_if
->config_rx_mode(pdata
);
1455 DBGPR("<--xgbe_set_rx_mode\n");
1458 static int xgbe_set_mac_address(struct net_device
*netdev
, void *addr
)
1460 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1461 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1462 struct sockaddr
*saddr
= addr
;
1464 DBGPR("-->xgbe_set_mac_address\n");
1466 if (!is_valid_ether_addr(saddr
->sa_data
))
1467 return -EADDRNOTAVAIL
;
1469 memcpy(netdev
->dev_addr
, saddr
->sa_data
, netdev
->addr_len
);
1471 hw_if
->set_mac_address(pdata
, netdev
->dev_addr
);
1473 DBGPR("<--xgbe_set_mac_address\n");
1478 static int xgbe_ioctl(struct net_device
*netdev
, struct ifreq
*ifreq
, int cmd
)
1480 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1485 ret
= xgbe_get_hwtstamp_settings(pdata
, ifreq
);
1489 ret
= xgbe_set_hwtstamp_settings(pdata
, ifreq
);
1499 static int xgbe_change_mtu(struct net_device
*netdev
, int mtu
)
1501 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1504 DBGPR("-->xgbe_change_mtu\n");
1506 ret
= xgbe_calc_rx_buf_size(netdev
, mtu
);
1510 pdata
->rx_buf_size
= ret
;
1513 xgbe_restart_dev(pdata
);
1515 DBGPR("<--xgbe_change_mtu\n");
1520 static void xgbe_tx_timeout(struct net_device
*netdev
)
1522 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1524 netdev_warn(netdev
, "tx timeout, device restarting\n");
1525 schedule_work(&pdata
->restart_work
);
1528 static struct rtnl_link_stats64
*xgbe_get_stats64(struct net_device
*netdev
,
1529 struct rtnl_link_stats64
*s
)
1531 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1532 struct xgbe_mmc_stats
*pstats
= &pdata
->mmc_stats
;
1534 DBGPR("-->%s\n", __func__
);
1536 pdata
->hw_if
.read_mmc_stats(pdata
);
1538 s
->rx_packets
= pstats
->rxframecount_gb
;
1539 s
->rx_bytes
= pstats
->rxoctetcount_gb
;
1540 s
->rx_errors
= pstats
->rxframecount_gb
-
1541 pstats
->rxbroadcastframes_g
-
1542 pstats
->rxmulticastframes_g
-
1543 pstats
->rxunicastframes_g
;
1544 s
->multicast
= pstats
->rxmulticastframes_g
;
1545 s
->rx_length_errors
= pstats
->rxlengtherror
;
1546 s
->rx_crc_errors
= pstats
->rxcrcerror
;
1547 s
->rx_fifo_errors
= pstats
->rxfifooverflow
;
1549 s
->tx_packets
= pstats
->txframecount_gb
;
1550 s
->tx_bytes
= pstats
->txoctetcount_gb
;
1551 s
->tx_errors
= pstats
->txframecount_gb
- pstats
->txframecount_g
;
1552 s
->tx_dropped
= netdev
->stats
.tx_dropped
;
1554 DBGPR("<--%s\n", __func__
);
1559 static int xgbe_vlan_rx_add_vid(struct net_device
*netdev
, __be16 proto
,
1562 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1563 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1565 DBGPR("-->%s\n", __func__
);
1567 set_bit(vid
, pdata
->active_vlans
);
1568 hw_if
->update_vlan_hash_table(pdata
);
1570 DBGPR("<--%s\n", __func__
);
1575 static int xgbe_vlan_rx_kill_vid(struct net_device
*netdev
, __be16 proto
,
1578 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1579 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1581 DBGPR("-->%s\n", __func__
);
1583 clear_bit(vid
, pdata
->active_vlans
);
1584 hw_if
->update_vlan_hash_table(pdata
);
1586 DBGPR("<--%s\n", __func__
);
1591 #ifdef CONFIG_NET_POLL_CONTROLLER
1592 static void xgbe_poll_controller(struct net_device
*netdev
)
1594 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1595 struct xgbe_channel
*channel
;
1598 DBGPR("-->xgbe_poll_controller\n");
1600 if (pdata
->per_channel_irq
) {
1601 channel
= pdata
->channel
;
1602 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++)
1603 xgbe_dma_isr(channel
->dma_irq
, channel
);
1605 disable_irq(pdata
->dev_irq
);
1606 xgbe_isr(pdata
->dev_irq
, pdata
);
1607 enable_irq(pdata
->dev_irq
);
1610 DBGPR("<--xgbe_poll_controller\n");
1612 #endif /* End CONFIG_NET_POLL_CONTROLLER */
1614 static int xgbe_setup_tc(struct net_device
*netdev
, u32 handle
, __be16 proto
,
1615 struct tc_to_netdev
*tc_to_netdev
)
1617 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1620 if (tc_to_netdev
->type
!= TC_SETUP_MQPRIO
)
1623 tc
= tc_to_netdev
->tc
;
1625 if (tc
> pdata
->hw_feat
.tc_cnt
)
1628 pdata
->num_tcs
= tc
;
1629 pdata
->hw_if
.config_tc(pdata
);
1634 static int xgbe_set_features(struct net_device
*netdev
,
1635 netdev_features_t features
)
1637 struct xgbe_prv_data
*pdata
= netdev_priv(netdev
);
1638 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1639 netdev_features_t rxhash
, rxcsum
, rxvlan
, rxvlan_filter
;
1642 rxhash
= pdata
->netdev_features
& NETIF_F_RXHASH
;
1643 rxcsum
= pdata
->netdev_features
& NETIF_F_RXCSUM
;
1644 rxvlan
= pdata
->netdev_features
& NETIF_F_HW_VLAN_CTAG_RX
;
1645 rxvlan_filter
= pdata
->netdev_features
& NETIF_F_HW_VLAN_CTAG_FILTER
;
1647 if ((features
& NETIF_F_RXHASH
) && !rxhash
)
1648 ret
= hw_if
->enable_rss(pdata
);
1649 else if (!(features
& NETIF_F_RXHASH
) && rxhash
)
1650 ret
= hw_if
->disable_rss(pdata
);
1654 if ((features
& NETIF_F_RXCSUM
) && !rxcsum
)
1655 hw_if
->enable_rx_csum(pdata
);
1656 else if (!(features
& NETIF_F_RXCSUM
) && rxcsum
)
1657 hw_if
->disable_rx_csum(pdata
);
1659 if ((features
& NETIF_F_HW_VLAN_CTAG_RX
) && !rxvlan
)
1660 hw_if
->enable_rx_vlan_stripping(pdata
);
1661 else if (!(features
& NETIF_F_HW_VLAN_CTAG_RX
) && rxvlan
)
1662 hw_if
->disable_rx_vlan_stripping(pdata
);
1664 if ((features
& NETIF_F_HW_VLAN_CTAG_FILTER
) && !rxvlan_filter
)
1665 hw_if
->enable_rx_vlan_filtering(pdata
);
1666 else if (!(features
& NETIF_F_HW_VLAN_CTAG_FILTER
) && rxvlan_filter
)
1667 hw_if
->disable_rx_vlan_filtering(pdata
);
1669 pdata
->netdev_features
= features
;
1671 DBGPR("<--xgbe_set_features\n");
1676 static const struct net_device_ops xgbe_netdev_ops
= {
1677 .ndo_open
= xgbe_open
,
1678 .ndo_stop
= xgbe_close
,
1679 .ndo_start_xmit
= xgbe_xmit
,
1680 .ndo_set_rx_mode
= xgbe_set_rx_mode
,
1681 .ndo_set_mac_address
= xgbe_set_mac_address
,
1682 .ndo_validate_addr
= eth_validate_addr
,
1683 .ndo_do_ioctl
= xgbe_ioctl
,
1684 .ndo_change_mtu
= xgbe_change_mtu
,
1685 .ndo_tx_timeout
= xgbe_tx_timeout
,
1686 .ndo_get_stats64
= xgbe_get_stats64
,
1687 .ndo_vlan_rx_add_vid
= xgbe_vlan_rx_add_vid
,
1688 .ndo_vlan_rx_kill_vid
= xgbe_vlan_rx_kill_vid
,
1689 #ifdef CONFIG_NET_POLL_CONTROLLER
1690 .ndo_poll_controller
= xgbe_poll_controller
,
1692 .ndo_setup_tc
= xgbe_setup_tc
,
1693 .ndo_set_features
= xgbe_set_features
,
1696 const struct net_device_ops
*xgbe_get_netdev_ops(void)
1698 return &xgbe_netdev_ops
;
1701 static void xgbe_rx_refresh(struct xgbe_channel
*channel
)
1703 struct xgbe_prv_data
*pdata
= channel
->pdata
;
1704 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1705 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
1706 struct xgbe_ring
*ring
= channel
->rx_ring
;
1707 struct xgbe_ring_data
*rdata
;
1709 while (ring
->dirty
!= ring
->cur
) {
1710 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->dirty
);
1712 /* Reset rdata values */
1713 desc_if
->unmap_rdata(pdata
, rdata
);
1715 if (desc_if
->map_rx_buffer(pdata
, ring
, rdata
))
1718 hw_if
->rx_desc_reset(pdata
, rdata
, ring
->dirty
);
1723 /* Make sure everything is written before the register write */
1726 /* Update the Rx Tail Pointer Register with address of
1727 * the last cleaned entry */
1728 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->dirty
- 1);
1729 XGMAC_DMA_IOWRITE(channel
, DMA_CH_RDTR_LO
,
1730 lower_32_bits(rdata
->rdesc_dma
));
1733 static struct sk_buff
*xgbe_create_skb(struct xgbe_prv_data
*pdata
,
1734 struct napi_struct
*napi
,
1735 struct xgbe_ring_data
*rdata
,
1738 struct sk_buff
*skb
;
1740 unsigned int copy_len
;
1742 skb
= napi_alloc_skb(napi
, rdata
->rx
.hdr
.dma_len
);
1746 /* Start with the header buffer which may contain just the header
1747 * or the header plus data
1749 dma_sync_single_range_for_cpu(pdata
->dev
, rdata
->rx
.hdr
.dma_base
,
1750 rdata
->rx
.hdr
.dma_off
,
1751 rdata
->rx
.hdr
.dma_len
, DMA_FROM_DEVICE
);
1753 packet
= page_address(rdata
->rx
.hdr
.pa
.pages
) +
1754 rdata
->rx
.hdr
.pa
.pages_offset
;
1755 copy_len
= (rdata
->rx
.hdr_len
) ? rdata
->rx
.hdr_len
: len
;
1756 copy_len
= min(rdata
->rx
.hdr
.dma_len
, copy_len
);
1757 skb_copy_to_linear_data(skb
, packet
, copy_len
);
1758 skb_put(skb
, copy_len
);
1762 /* Add the remaining data as a frag */
1763 dma_sync_single_range_for_cpu(pdata
->dev
,
1764 rdata
->rx
.buf
.dma_base
,
1765 rdata
->rx
.buf
.dma_off
,
1766 rdata
->rx
.buf
.dma_len
,
1769 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
1770 rdata
->rx
.buf
.pa
.pages
,
1771 rdata
->rx
.buf
.pa
.pages_offset
,
1772 len
, rdata
->rx
.buf
.dma_len
);
1773 rdata
->rx
.buf
.pa
.pages
= NULL
;
1779 static int xgbe_tx_poll(struct xgbe_channel
*channel
)
1781 struct xgbe_prv_data
*pdata
= channel
->pdata
;
1782 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1783 struct xgbe_desc_if
*desc_if
= &pdata
->desc_if
;
1784 struct xgbe_ring
*ring
= channel
->tx_ring
;
1785 struct xgbe_ring_data
*rdata
;
1786 struct xgbe_ring_desc
*rdesc
;
1787 struct net_device
*netdev
= pdata
->netdev
;
1788 struct netdev_queue
*txq
;
1790 unsigned int tx_packets
= 0, tx_bytes
= 0;
1793 DBGPR("-->xgbe_tx_poll\n");
1795 /* Nothing to do if there isn't a Tx ring for this channel */
1801 /* Be sure we get ring->cur before accessing descriptor data */
1804 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
1806 while ((processed
< XGBE_TX_DESC_MAX_PROC
) &&
1807 (ring
->dirty
!= cur
)) {
1808 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->dirty
);
1809 rdesc
= rdata
->rdesc
;
1811 if (!hw_if
->tx_complete(rdesc
))
1814 /* Make sure descriptor fields are read after reading the OWN
1818 if (netif_msg_tx_done(pdata
))
1819 xgbe_dump_tx_desc(pdata
, ring
, ring
->dirty
, 1, 0);
1821 if (hw_if
->is_last_desc(rdesc
)) {
1822 tx_packets
+= rdata
->tx
.packets
;
1823 tx_bytes
+= rdata
->tx
.bytes
;
1826 /* Free the SKB and reset the descriptor for re-use */
1827 desc_if
->unmap_rdata(pdata
, rdata
);
1828 hw_if
->tx_desc_reset(rdata
);
1837 netdev_tx_completed_queue(txq
, tx_packets
, tx_bytes
);
1839 if ((ring
->tx
.queue_stopped
== 1) &&
1840 (xgbe_tx_avail_desc(ring
) > XGBE_TX_DESC_MIN_FREE
)) {
1841 ring
->tx
.queue_stopped
= 0;
1842 netif_tx_wake_queue(txq
);
1845 DBGPR("<--xgbe_tx_poll: processed=%d\n", processed
);
1850 static int xgbe_rx_poll(struct xgbe_channel
*channel
, int budget
)
1852 struct xgbe_prv_data
*pdata
= channel
->pdata
;
1853 struct xgbe_hw_if
*hw_if
= &pdata
->hw_if
;
1854 struct xgbe_ring
*ring
= channel
->rx_ring
;
1855 struct xgbe_ring_data
*rdata
;
1856 struct xgbe_packet_data
*packet
;
1857 struct net_device
*netdev
= pdata
->netdev
;
1858 struct napi_struct
*napi
;
1859 struct sk_buff
*skb
;
1860 struct skb_shared_hwtstamps
*hwtstamps
;
1861 unsigned int incomplete
, error
, context_next
, context
;
1862 unsigned int len
, rdesc_len
, max_len
;
1863 unsigned int received
= 0;
1864 int packet_count
= 0;
1866 DBGPR("-->xgbe_rx_poll: budget=%d\n", budget
);
1868 /* Nothing to do if there isn't a Rx ring for this channel */
1875 napi
= (pdata
->per_channel_irq
) ? &channel
->napi
: &pdata
->napi
;
1877 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->cur
);
1878 packet
= &ring
->packet_data
;
1879 while (packet_count
< budget
) {
1880 DBGPR(" cur = %d\n", ring
->cur
);
1882 /* First time in loop see if we need to restore state */
1883 if (!received
&& rdata
->state_saved
) {
1884 skb
= rdata
->state
.skb
;
1885 error
= rdata
->state
.error
;
1886 len
= rdata
->state
.len
;
1888 memset(packet
, 0, sizeof(*packet
));
1895 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->cur
);
1897 if (xgbe_rx_dirty_desc(ring
) > (XGBE_RX_DESC_CNT
>> 3))
1898 xgbe_rx_refresh(channel
);
1900 if (hw_if
->dev_read(channel
))
1906 incomplete
= XGMAC_GET_BITS(packet
->attributes
,
1907 RX_PACKET_ATTRIBUTES
,
1909 context_next
= XGMAC_GET_BITS(packet
->attributes
,
1910 RX_PACKET_ATTRIBUTES
,
1912 context
= XGMAC_GET_BITS(packet
->attributes
,
1913 RX_PACKET_ATTRIBUTES
,
1916 /* Earlier error, just drain the remaining data */
1917 if ((incomplete
|| context_next
) && error
)
1920 if (error
|| packet
->errors
) {
1922 netif_err(pdata
, rx_err
, netdev
,
1923 "error in received packet\n");
1929 /* Length is cumulative, get this descriptor's length */
1930 rdesc_len
= rdata
->rx
.len
- len
;
1933 if (rdesc_len
&& !skb
) {
1934 skb
= xgbe_create_skb(pdata
, napi
, rdata
,
1938 } else if (rdesc_len
) {
1939 dma_sync_single_range_for_cpu(pdata
->dev
,
1940 rdata
->rx
.buf
.dma_base
,
1941 rdata
->rx
.buf
.dma_off
,
1942 rdata
->rx
.buf
.dma_len
,
1945 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
1946 rdata
->rx
.buf
.pa
.pages
,
1947 rdata
->rx
.buf
.pa
.pages_offset
,
1949 rdata
->rx
.buf
.dma_len
);
1950 rdata
->rx
.buf
.pa
.pages
= NULL
;
1954 if (incomplete
|| context_next
)
1960 /* Be sure we don't exceed the configured MTU */
1961 max_len
= netdev
->mtu
+ ETH_HLEN
;
1962 if (!(netdev
->features
& NETIF_F_HW_VLAN_CTAG_RX
) &&
1963 (skb
->protocol
== htons(ETH_P_8021Q
)))
1964 max_len
+= VLAN_HLEN
;
1966 if (skb
->len
> max_len
) {
1967 netif_err(pdata
, rx_err
, netdev
,
1968 "packet length exceeds configured MTU\n");
1973 if (netif_msg_pktdata(pdata
))
1974 xgbe_print_pkt(netdev
, skb
, false);
1976 skb_checksum_none_assert(skb
);
1977 if (XGMAC_GET_BITS(packet
->attributes
,
1978 RX_PACKET_ATTRIBUTES
, CSUM_DONE
))
1979 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1981 if (XGMAC_GET_BITS(packet
->attributes
,
1982 RX_PACKET_ATTRIBUTES
, VLAN_CTAG
))
1983 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
),
1986 if (XGMAC_GET_BITS(packet
->attributes
,
1987 RX_PACKET_ATTRIBUTES
, RX_TSTAMP
)) {
1990 nsec
= timecounter_cyc2time(&pdata
->tstamp_tc
,
1992 hwtstamps
= skb_hwtstamps(skb
);
1993 hwtstamps
->hwtstamp
= ns_to_ktime(nsec
);
1996 if (XGMAC_GET_BITS(packet
->attributes
,
1997 RX_PACKET_ATTRIBUTES
, RSS_HASH
))
1998 skb_set_hash(skb
, packet
->rss_hash
,
1999 packet
->rss_hash_type
);
2002 skb
->protocol
= eth_type_trans(skb
, netdev
);
2003 skb_record_rx_queue(skb
, channel
->queue_index
);
2005 napi_gro_receive(napi
, skb
);
2011 /* Check if we need to save state before leaving */
2012 if (received
&& (incomplete
|| context_next
)) {
2013 rdata
= XGBE_GET_DESC_DATA(ring
, ring
->cur
);
2014 rdata
->state_saved
= 1;
2015 rdata
->state
.skb
= skb
;
2016 rdata
->state
.len
= len
;
2017 rdata
->state
.error
= error
;
2020 DBGPR("<--xgbe_rx_poll: packet_count = %d\n", packet_count
);
2022 return packet_count
;
2025 static int xgbe_one_poll(struct napi_struct
*napi
, int budget
)
2027 struct xgbe_channel
*channel
= container_of(napi
, struct xgbe_channel
,
2031 DBGPR("-->xgbe_one_poll: budget=%d\n", budget
);
2033 /* Cleanup Tx ring first */
2034 xgbe_tx_poll(channel
);
2036 /* Process Rx ring next */
2037 processed
= xgbe_rx_poll(channel
, budget
);
2039 /* If we processed everything, we are done */
2040 if (processed
< budget
) {
2041 /* Turn off polling */
2042 napi_complete_done(napi
, processed
);
2044 /* Enable Tx and Rx interrupts */
2045 enable_irq(channel
->dma_irq
);
2048 DBGPR("<--xgbe_one_poll: received = %d\n", processed
);
2053 static int xgbe_all_poll(struct napi_struct
*napi
, int budget
)
2055 struct xgbe_prv_data
*pdata
= container_of(napi
, struct xgbe_prv_data
,
2057 struct xgbe_channel
*channel
;
2059 int processed
, last_processed
;
2062 DBGPR("-->xgbe_all_poll: budget=%d\n", budget
);
2065 ring_budget
= budget
/ pdata
->rx_ring_count
;
2067 last_processed
= processed
;
2069 channel
= pdata
->channel
;
2070 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
2071 /* Cleanup Tx ring first */
2072 xgbe_tx_poll(channel
);
2074 /* Process Rx ring next */
2075 if (ring_budget
> (budget
- processed
))
2076 ring_budget
= budget
- processed
;
2077 processed
+= xgbe_rx_poll(channel
, ring_budget
);
2079 } while ((processed
< budget
) && (processed
!= last_processed
));
2081 /* If we processed everything, we are done */
2082 if (processed
< budget
) {
2083 /* Turn off polling */
2084 napi_complete_done(napi
, processed
);
2086 /* Enable Tx and Rx interrupts */
2087 xgbe_enable_rx_tx_ints(pdata
);
2090 DBGPR("<--xgbe_all_poll: received = %d\n", processed
);
2095 void xgbe_dump_tx_desc(struct xgbe_prv_data
*pdata
, struct xgbe_ring
*ring
,
2096 unsigned int idx
, unsigned int count
, unsigned int flag
)
2098 struct xgbe_ring_data
*rdata
;
2099 struct xgbe_ring_desc
*rdesc
;
2102 rdata
= XGBE_GET_DESC_DATA(ring
, idx
);
2103 rdesc
= rdata
->rdesc
;
2104 netdev_dbg(pdata
->netdev
,
2105 "TX_NORMAL_DESC[%d %s] = %08x:%08x:%08x:%08x\n", idx
,
2106 (flag
== 1) ? "QUEUED FOR TX" : "TX BY DEVICE",
2107 le32_to_cpu(rdesc
->desc0
),
2108 le32_to_cpu(rdesc
->desc1
),
2109 le32_to_cpu(rdesc
->desc2
),
2110 le32_to_cpu(rdesc
->desc3
));
2115 void xgbe_dump_rx_desc(struct xgbe_prv_data
*pdata
, struct xgbe_ring
*ring
,
2118 struct xgbe_ring_data
*rdata
;
2119 struct xgbe_ring_desc
*rdesc
;
2121 rdata
= XGBE_GET_DESC_DATA(ring
, idx
);
2122 rdesc
= rdata
->rdesc
;
2123 netdev_dbg(pdata
->netdev
,
2124 "RX_NORMAL_DESC[%d RX BY DEVICE] = %08x:%08x:%08x:%08x\n",
2125 idx
, le32_to_cpu(rdesc
->desc0
), le32_to_cpu(rdesc
->desc1
),
2126 le32_to_cpu(rdesc
->desc2
), le32_to_cpu(rdesc
->desc3
));
2129 void xgbe_print_pkt(struct net_device
*netdev
, struct sk_buff
*skb
, bool tx_rx
)
2131 struct ethhdr
*eth
= (struct ethhdr
*)skb
->data
;
2132 unsigned char *buf
= skb
->data
;
2133 unsigned char buffer
[128];
2136 netdev_dbg(netdev
, "\n************** SKB dump ****************\n");
2138 netdev_dbg(netdev
, "%s packet of %d bytes\n",
2139 (tx_rx
? "TX" : "RX"), skb
->len
);
2141 netdev_dbg(netdev
, "Dst MAC addr: %pM\n", eth
->h_dest
);
2142 netdev_dbg(netdev
, "Src MAC addr: %pM\n", eth
->h_source
);
2143 netdev_dbg(netdev
, "Protocol: %#06hx\n", ntohs(eth
->h_proto
));
2145 for (i
= 0, j
= 0; i
< skb
->len
;) {
2146 j
+= snprintf(buffer
+ j
, sizeof(buffer
) - j
, "%02hhx",
2149 if ((i
% 32) == 0) {
2150 netdev_dbg(netdev
, " %#06x: %s\n", i
- 32, buffer
);
2152 } else if ((i
% 16) == 0) {
2155 } else if ((i
% 4) == 0) {
2160 netdev_dbg(netdev
, " %#06x: %s\n", i
- (i
% 32), buffer
);
2162 netdev_dbg(netdev
, "\n************** SKB dump ****************\n");