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