blob: 12dd18cbdba34d75edcfed14f83b0fbc2998b456 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Cadence MACB/GEM Ethernet Controller driver
4 *
5 * Copyright (C) 2004-2006 Atmel Corporation
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/clk.h>
10#include <linux/clk-provider.h>
11#include <linux/crc32.h>
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/kernel.h>
15#include <linux/types.h>
16#include <linux/circ_buf.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/gpio.h>
21#include <linux/gpio/consumer.h>
22#include <linux/interrupt.h>
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/dma-mapping.h>
26#include <linux/platform_data/macb.h>
27#include <linux/platform_device.h>
28#include <linux/phy.h>
29#include <linux/of.h>
30#include <linux/of_device.h>
31#include <linux/of_gpio.h>
32#include <linux/of_mdio.h>
33#include <linux/of_net.h>
34#include <linux/ip.h>
35#include <linux/udp.h>
36#include <linux/tcp.h>
37#include <linux/iopoll.h>
38#include <linux/pm_runtime.h>
39#include "macb.h"
40
41/* This structure is only used for MACB on SiFive FU540 devices */
42struct sifive_fu540_macb_mgmt {
43 void __iomem *reg;
44 unsigned long rate;
45 struct clk_hw hw;
46};
47
48#define MACB_RX_BUFFER_SIZE 128
49#define RX_BUFFER_MULTIPLE 64 /* bytes */
50
51#define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
52#define MIN_RX_RING_SIZE 64
53#define MAX_RX_RING_SIZE 8192
54#define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
55 * (bp)->rx_ring_size)
56
57#define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
58#define MIN_TX_RING_SIZE 64
59#define MAX_TX_RING_SIZE 4096
60#define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
61 * (bp)->tx_ring_size)
62
63/* level of occupied TX descriptors under which we wake up TX process */
64#define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
65
66#define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
67#define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
68 | MACB_BIT(ISR_RLE) \
69 | MACB_BIT(TXERR))
70#define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
71 | MACB_BIT(TXUBR))
72
73/* Max length of transmit frame must be a multiple of 8 bytes */
74#define MACB_TX_LEN_ALIGN 8
75#define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
76/* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
77 * false amba_error in TX path from the DMA assuming there is not enough
78 * space in the SRAM (16KB) even when there is.
79 */
80#define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
81
82#define GEM_MTU_MIN_SIZE ETH_MIN_MTU
83#define MACB_NETIF_LSO NETIF_F_TSO
84
85#define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
86#define MACB_WOL_ENABLED (0x1 << 1)
87
88/* Graceful stop timeouts in us. We should allow up to
89 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
90 */
91#define MACB_HALT_TIMEOUT 1230
92
93#define MACB_PM_TIMEOUT 100 /* ms */
94
95#define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
96
97/* DMA buffer descriptor might be different size
98 * depends on hardware configuration:
99 *
100 * 1. dma address width 32 bits:
101 * word 1: 32 bit address of Data Buffer
102 * word 2: control
103 *
104 * 2. dma address width 64 bits:
105 * word 1: 32 bit address of Data Buffer
106 * word 2: control
107 * word 3: upper 32 bit address of Data Buffer
108 * word 4: unused
109 *
110 * 3. dma address width 32 bits with hardware timestamping:
111 * word 1: 32 bit address of Data Buffer
112 * word 2: control
113 * word 3: timestamp word 1
114 * word 4: timestamp word 2
115 *
116 * 4. dma address width 64 bits with hardware timestamping:
117 * word 1: 32 bit address of Data Buffer
118 * word 2: control
119 * word 3: upper 32 bit address of Data Buffer
120 * word 4: unused
121 * word 5: timestamp word 1
122 * word 6: timestamp word 2
123 */
124static unsigned int macb_dma_desc_get_size(struct macb *bp)
125{
126#ifdef MACB_EXT_DESC
127 unsigned int desc_size;
128
129 switch (bp->hw_dma_cap) {
130 case HW_DMA_CAP_64B:
131 desc_size = sizeof(struct macb_dma_desc)
132 + sizeof(struct macb_dma_desc_64);
133 break;
134 case HW_DMA_CAP_PTP:
135 desc_size = sizeof(struct macb_dma_desc)
136 + sizeof(struct macb_dma_desc_ptp);
137 break;
138 case HW_DMA_CAP_64B_PTP:
139 desc_size = sizeof(struct macb_dma_desc)
140 + sizeof(struct macb_dma_desc_64)
141 + sizeof(struct macb_dma_desc_ptp);
142 break;
143 default:
144 desc_size = sizeof(struct macb_dma_desc);
145 }
146 return desc_size;
147#endif
148 return sizeof(struct macb_dma_desc);
149}
150
151static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
152{
153#ifdef MACB_EXT_DESC
154 switch (bp->hw_dma_cap) {
155 case HW_DMA_CAP_64B:
156 case HW_DMA_CAP_PTP:
157 desc_idx <<= 1;
158 break;
159 case HW_DMA_CAP_64B_PTP:
160 desc_idx *= 3;
161 break;
162 default:
163 break;
164 }
165#endif
166 return desc_idx;
167}
168
169#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
170static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
171{
172 return (struct macb_dma_desc_64 *)((void *)desc
173 + sizeof(struct macb_dma_desc));
174}
175#endif
176
177/* Ring buffer accessors */
178static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
179{
180 return index & (bp->tx_ring_size - 1);
181}
182
183static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
184 unsigned int index)
185{
186 index = macb_tx_ring_wrap(queue->bp, index);
187 index = macb_adj_dma_desc_idx(queue->bp, index);
188 return &queue->tx_ring[index];
189}
190
191static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
192 unsigned int index)
193{
194 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
195}
196
197static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
198{
199 dma_addr_t offset;
200
201 offset = macb_tx_ring_wrap(queue->bp, index) *
202 macb_dma_desc_get_size(queue->bp);
203
204 return queue->tx_ring_dma + offset;
205}
206
207static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
208{
209 return index & (bp->rx_ring_size - 1);
210}
211
212static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
213{
214 index = macb_rx_ring_wrap(queue->bp, index);
215 index = macb_adj_dma_desc_idx(queue->bp, index);
216 return &queue->rx_ring[index];
217}
218
219static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
220{
221 return queue->rx_buffers + queue->bp->rx_buffer_size *
222 macb_rx_ring_wrap(queue->bp, index);
223}
224
225/* I/O accessors */
226static u32 hw_readl_native(struct macb *bp, int offset)
227{
228 return __raw_readl(bp->regs + offset);
229}
230
231static void hw_writel_native(struct macb *bp, int offset, u32 value)
232{
233 __raw_writel(value, bp->regs + offset);
234}
235
236static u32 hw_readl(struct macb *bp, int offset)
237{
238 return readl_relaxed(bp->regs + offset);
239}
240
241static void hw_writel(struct macb *bp, int offset, u32 value)
242{
243 writel_relaxed(value, bp->regs + offset);
244}
245
246/* Find the CPU endianness by using the loopback bit of NCR register. When the
247 * CPU is in big endian we need to program swapped mode for management
248 * descriptor access.
249 */
250static bool hw_is_native_io(void __iomem *addr)
251{
252 u32 value = MACB_BIT(LLB);
253
254 __raw_writel(value, addr + MACB_NCR);
255 value = __raw_readl(addr + MACB_NCR);
256
257 /* Write 0 back to disable everything */
258 __raw_writel(0, addr + MACB_NCR);
259
260 return value == MACB_BIT(LLB);
261}
262
263static bool hw_is_gem(void __iomem *addr, bool native_io)
264{
265 u32 id;
266
267 if (native_io)
268 id = __raw_readl(addr + MACB_MID);
269 else
270 id = readl_relaxed(addr + MACB_MID);
271
272 return MACB_BFEXT(IDNUM, id) >= 0x2;
273}
274
275static void macb_set_hwaddr(struct macb *bp)
276{
277 u32 bottom;
278 u16 top;
279
280 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
281 macb_or_gem_writel(bp, SA1B, bottom);
282 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
283 macb_or_gem_writel(bp, SA1T, top);
284
285 /* Clear unused address register sets */
286 macb_or_gem_writel(bp, SA2B, 0);
287 macb_or_gem_writel(bp, SA2T, 0);
288 macb_or_gem_writel(bp, SA3B, 0);
289 macb_or_gem_writel(bp, SA3T, 0);
290 macb_or_gem_writel(bp, SA4B, 0);
291 macb_or_gem_writel(bp, SA4T, 0);
292}
293
294static void macb_get_hwaddr(struct macb *bp)
295{
296 u32 bottom;
297 u16 top;
298 u8 addr[6];
299 int i;
300
301 /* Check all 4 address register for valid address */
302 for (i = 0; i < 4; i++) {
303 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
304 top = macb_or_gem_readl(bp, SA1T + i * 8);
305
306 addr[0] = bottom & 0xff;
307 addr[1] = (bottom >> 8) & 0xff;
308 addr[2] = (bottom >> 16) & 0xff;
309 addr[3] = (bottom >> 24) & 0xff;
310 addr[4] = top & 0xff;
311 addr[5] = (top >> 8) & 0xff;
312
313 if (is_valid_ether_addr(addr)) {
314 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
315 return;
316 }
317 }
318
319 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
320 eth_hw_addr_random(bp->dev);
321}
322
323static int macb_mdio_wait_for_idle(struct macb *bp)
324{
325 u32 val;
326
327 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
328 1, MACB_MDIO_TIMEOUT);
329}
330
331static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
332{
333 struct macb *bp = bus->priv;
334 int status;
335
336 status = pm_runtime_get_sync(&bp->pdev->dev);
337 if (status < 0) {
338 pm_runtime_put_noidle(&bp->pdev->dev);
339 goto mdio_pm_exit;
340 }
341
342 status = macb_mdio_wait_for_idle(bp);
343 if (status < 0)
344 goto mdio_read_exit;
345
346 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
347 | MACB_BF(RW, MACB_MAN_READ)
348 | MACB_BF(PHYA, mii_id)
349 | MACB_BF(REGA, regnum)
350 | MACB_BF(CODE, MACB_MAN_CODE)));
351
352 status = macb_mdio_wait_for_idle(bp);
353 if (status < 0)
354 goto mdio_read_exit;
355
356 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
357
358mdio_read_exit:
359 pm_runtime_mark_last_busy(&bp->pdev->dev);
360 pm_runtime_put_autosuspend(&bp->pdev->dev);
361mdio_pm_exit:
362 return status;
363}
364
365static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
366 u16 value)
367{
368 struct macb *bp = bus->priv;
369 int status;
370
371 status = pm_runtime_get_sync(&bp->pdev->dev);
372 if (status < 0) {
373 pm_runtime_put_noidle(&bp->pdev->dev);
374 goto mdio_pm_exit;
375 }
376
377 status = macb_mdio_wait_for_idle(bp);
378 if (status < 0)
379 goto mdio_write_exit;
380
381 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
382 | MACB_BF(RW, MACB_MAN_WRITE)
383 | MACB_BF(PHYA, mii_id)
384 | MACB_BF(REGA, regnum)
385 | MACB_BF(CODE, MACB_MAN_CODE)
386 | MACB_BF(DATA, value)));
387
388 status = macb_mdio_wait_for_idle(bp);
389 if (status < 0)
390 goto mdio_write_exit;
391
392mdio_write_exit:
393 pm_runtime_mark_last_busy(&bp->pdev->dev);
394 pm_runtime_put_autosuspend(&bp->pdev->dev);
395mdio_pm_exit:
396 return status;
397}
398
399/**
400 * macb_set_tx_clk() - Set a clock to a new frequency
401 * @clk Pointer to the clock to change
402 * @rate New frequency in Hz
403 * @dev Pointer to the struct net_device
404 */
405static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
406{
407 long ferr, rate, rate_rounded;
408
409 if (!clk)
410 return;
411
412 switch (speed) {
413 case SPEED_10:
414 rate = 2500000;
415 break;
416 case SPEED_100:
417 rate = 25000000;
418 break;
419 case SPEED_1000:
420 rate = 125000000;
421 break;
422 default:
423 return;
424 }
425
426 rate_rounded = clk_round_rate(clk, rate);
427 if (rate_rounded < 0)
428 return;
429
430 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
431 * is not satisfied.
432 */
433 ferr = abs(rate_rounded - rate);
434 ferr = DIV_ROUND_UP(ferr, rate / 100000);
435 if (ferr > 5)
436 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
437 rate);
438
439 if (clk_set_rate(clk, rate_rounded))
440 netdev_err(dev, "adjusting tx_clk failed.\n");
441}
442
443static void macb_handle_link_change(struct net_device *dev)
444{
445 struct macb *bp = netdev_priv(dev);
446 struct phy_device *phydev = dev->phydev;
447 unsigned long flags;
448 int status_change = 0;
449
450 spin_lock_irqsave(&bp->lock, flags);
451
452 if (phydev->link) {
453 if ((bp->speed != phydev->speed) ||
454 (bp->duplex != phydev->duplex)) {
455 u32 reg;
456
457 reg = macb_readl(bp, NCFGR);
458 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
459 if (macb_is_gem(bp))
460 reg &= ~GEM_BIT(GBE);
461
462 if (phydev->duplex)
463 reg |= MACB_BIT(FD);
464 if (phydev->speed == SPEED_100)
465 reg |= MACB_BIT(SPD);
466 if (phydev->speed == SPEED_1000 &&
467 bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
468 reg |= GEM_BIT(GBE);
469
470 macb_or_gem_writel(bp, NCFGR, reg);
471
472 bp->speed = phydev->speed;
473 bp->duplex = phydev->duplex;
474 status_change = 1;
475 }
476 }
477
478 if (phydev->link != bp->link) {
479 if (!phydev->link) {
480 bp->speed = 0;
481 bp->duplex = -1;
482 }
483 bp->link = phydev->link;
484
485 status_change = 1;
486 }
487
488 spin_unlock_irqrestore(&bp->lock, flags);
489
490 if (status_change) {
491 if (phydev->link) {
492 /* Update the TX clock rate if and only if the link is
493 * up and there has been a link change.
494 */
495 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
496
497 netif_carrier_on(dev);
498 netdev_info(dev, "link up (%d/%s)\n",
499 phydev->speed,
500 phydev->duplex == DUPLEX_FULL ?
501 "Full" : "Half");
502 } else {
503 netif_carrier_off(dev);
504 netdev_info(dev, "link down\n");
505 }
506 }
507}
508
509/* based on au1000_eth. c*/
510static int macb_mii_probe(struct net_device *dev)
511{
512 struct macb *bp = netdev_priv(dev);
513 struct phy_device *phydev;
514 struct device_node *np;
515 int ret, i;
516
517 np = bp->pdev->dev.of_node;
518 ret = 0;
519
520 if (np) {
521 if (of_phy_is_fixed_link(np)) {
522 bp->phy_node = of_node_get(np);
523 } else {
524 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
525 /* fallback to standard phy registration if no
526 * phy-handle was found nor any phy found during
527 * dt phy registration
528 */
529 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
530 for (i = 0; i < PHY_MAX_ADDR; i++) {
531 phydev = mdiobus_scan(bp->mii_bus, i);
532 if (IS_ERR(phydev) &&
533 PTR_ERR(phydev) != -ENODEV) {
534 ret = PTR_ERR(phydev);
535 break;
536 }
537 }
538
539 if (ret)
540 return -ENODEV;
541 }
542 }
543 }
544
545 if (bp->phy_node) {
546 phydev = of_phy_connect(dev, bp->phy_node,
547 &macb_handle_link_change, 0,
548 bp->phy_interface);
549 if (!phydev)
550 return -ENODEV;
551 } else {
552 phydev = phy_find_first(bp->mii_bus);
553 if (!phydev) {
554 netdev_err(dev, "no PHY found\n");
555 return -ENXIO;
556 }
557
558 /* attach the mac to the phy */
559 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
560 bp->phy_interface);
561 if (ret) {
562 netdev_err(dev, "Could not attach to PHY\n");
563 return ret;
564 }
565 }
566
567 /* mask with MAC supported features */
568 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
569 phy_set_max_speed(phydev, SPEED_1000);
570 else
571 phy_set_max_speed(phydev, SPEED_100);
572
573 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
574 phy_remove_link_mode(phydev,
575 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
576
577 bp->link = 0;
578 bp->speed = 0;
579 bp->duplex = -1;
580
581 return 0;
582}
583
584static int macb_mii_init(struct macb *bp)
585{
586 struct device_node *np;
587 int err = -ENXIO;
588
589 /* Enable management port */
590 macb_writel(bp, NCR, MACB_BIT(MPE));
591
592 bp->mii_bus = mdiobus_alloc();
593 if (!bp->mii_bus) {
594 err = -ENOMEM;
595 goto err_out;
596 }
597
598 bp->mii_bus->name = "MACB_mii_bus";
599 bp->mii_bus->read = &macb_mdio_read;
600 bp->mii_bus->write = &macb_mdio_write;
601 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
602 bp->pdev->name, bp->pdev->id);
603 bp->mii_bus->priv = bp;
604 bp->mii_bus->parent = &bp->pdev->dev;
605
606 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
607
608 np = bp->pdev->dev.of_node;
609 if (np && of_phy_is_fixed_link(np)) {
610 if (of_phy_register_fixed_link(np) < 0) {
611 dev_err(&bp->pdev->dev,
612 "broken fixed-link specification %pOF\n", np);
613 goto err_out_free_mdiobus;
614 }
615
616 err = mdiobus_register(bp->mii_bus);
617 } else {
618 err = of_mdiobus_register(bp->mii_bus, np);
619 }
620
621 if (err)
622 goto err_out_free_fixed_link;
623
624 err = macb_mii_probe(bp->dev);
625 if (err)
626 goto err_out_unregister_bus;
627
628 return 0;
629
630err_out_unregister_bus:
631 mdiobus_unregister(bp->mii_bus);
632err_out_free_fixed_link:
633 if (np && of_phy_is_fixed_link(np))
634 of_phy_deregister_fixed_link(np);
635err_out_free_mdiobus:
636 of_node_put(bp->phy_node);
637 mdiobus_free(bp->mii_bus);
638err_out:
639 return err;
640}
641
642static void macb_update_stats(struct macb *bp)
643{
644 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
645 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
646 int offset = MACB_PFR;
647
648 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
649
650 for (; p < end; p++, offset += 4)
651 *p += bp->macb_reg_readl(bp, offset);
652}
653
654static int macb_halt_tx(struct macb *bp)
655{
656 unsigned long halt_time, timeout;
657 u32 status;
658
659 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
660
661 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
662 do {
663 halt_time = jiffies;
664 status = macb_readl(bp, TSR);
665 if (!(status & MACB_BIT(TGO)))
666 return 0;
667
668 udelay(250);
669 } while (time_before(halt_time, timeout));
670
671 return -ETIMEDOUT;
672}
673
674static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
675{
676 if (tx_skb->mapping) {
677 if (tx_skb->mapped_as_page)
678 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
679 tx_skb->size, DMA_TO_DEVICE);
680 else
681 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
682 tx_skb->size, DMA_TO_DEVICE);
683 tx_skb->mapping = 0;
684 }
685
686 if (tx_skb->skb) {
687 dev_kfree_skb_any(tx_skb->skb);
688 tx_skb->skb = NULL;
689 }
690}
691
692static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
693{
694#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
695 struct macb_dma_desc_64 *desc_64;
696
697 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
698 desc_64 = macb_64b_desc(bp, desc);
699 desc_64->addrh = upper_32_bits(addr);
700 /* The low bits of RX address contain the RX_USED bit, clearing
701 * of which allows packet RX. Make sure the high bits are also
702 * visible to HW at that point.
703 */
704 dma_wmb();
705 }
706#endif
707 desc->addr = lower_32_bits(addr);
708}
709
710static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
711{
712 dma_addr_t addr = 0;
713#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
714 struct macb_dma_desc_64 *desc_64;
715
716 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
717 desc_64 = macb_64b_desc(bp, desc);
718 addr = ((u64)(desc_64->addrh) << 32);
719 }
720#endif
721 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
722#ifdef CONFIG_MACB_USE_HWSTAMP
723 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
724 addr &= ~GEM_BIT(DMA_RXVALID);
725#endif
726 return addr;
727}
728
729static void macb_tx_error_task(struct work_struct *work)
730{
731 struct macb_queue *queue = container_of(work, struct macb_queue,
732 tx_error_task);
733 struct macb *bp = queue->bp;
734 struct macb_tx_skb *tx_skb;
735 struct macb_dma_desc *desc;
736 struct sk_buff *skb;
737 unsigned int tail;
738 unsigned long flags;
739
740 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
741 (unsigned int)(queue - bp->queues),
742 queue->tx_tail, queue->tx_head);
743
744 /* Prevent the queue IRQ handlers from running: each of them may call
745 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
746 * As explained below, we have to halt the transmission before updating
747 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
748 * network engine about the macb/gem being halted.
749 */
750 spin_lock_irqsave(&bp->lock, flags);
751
752 /* Make sure nobody is trying to queue up new packets */
753 netif_tx_stop_all_queues(bp->dev);
754
755 /* Stop transmission now
756 * (in case we have just queued new packets)
757 * macb/gem must be halted to write TBQP register
758 */
759 if (macb_halt_tx(bp))
760 /* Just complain for now, reinitializing TX path can be good */
761 netdev_err(bp->dev, "BUG: halt tx timed out\n");
762
763 /* Treat frames in TX queue including the ones that caused the error.
764 * Free transmit buffers in upper layer.
765 */
766 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
767 u32 ctrl;
768
769 desc = macb_tx_desc(queue, tail);
770 ctrl = desc->ctrl;
771 tx_skb = macb_tx_skb(queue, tail);
772 skb = tx_skb->skb;
773
774 if (ctrl & MACB_BIT(TX_USED)) {
775 /* skb is set for the last buffer of the frame */
776 while (!skb) {
777 macb_tx_unmap(bp, tx_skb);
778 tail++;
779 tx_skb = macb_tx_skb(queue, tail);
780 skb = tx_skb->skb;
781 }
782
783 /* ctrl still refers to the first buffer descriptor
784 * since it's the only one written back by the hardware
785 */
786 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
787 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
788 macb_tx_ring_wrap(bp, tail),
789 skb->data);
790 bp->dev->stats.tx_packets++;
791 queue->stats.tx_packets++;
792 bp->dev->stats.tx_bytes += skb->len;
793 queue->stats.tx_bytes += skb->len;
794 }
795 } else {
796 /* "Buffers exhausted mid-frame" errors may only happen
797 * if the driver is buggy, so complain loudly about
798 * those. Statistics are updated by hardware.
799 */
800 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
801 netdev_err(bp->dev,
802 "BUG: TX buffers exhausted mid-frame\n");
803
804 desc->ctrl = ctrl | MACB_BIT(TX_USED);
805 }
806
807 macb_tx_unmap(bp, tx_skb);
808 }
809
810 /* Set end of TX queue */
811 desc = macb_tx_desc(queue, 0);
812 macb_set_addr(bp, desc, 0);
813 desc->ctrl = MACB_BIT(TX_USED);
814
815 /* Make descriptor updates visible to hardware */
816 wmb();
817
818 /* Reinitialize the TX desc queue */
819 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
820#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
821 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
822 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
823#endif
824 /* Make TX ring reflect state of hardware */
825 queue->tx_head = 0;
826 queue->tx_tail = 0;
827
828 /* Housework before enabling TX IRQ */
829 macb_writel(bp, TSR, macb_readl(bp, TSR));
830 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
831
832 /* Now we are ready to start transmission again */
833 netif_tx_start_all_queues(bp->dev);
834 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
835
836 spin_unlock_irqrestore(&bp->lock, flags);
837}
838
839static void macb_tx_interrupt(struct macb_queue *queue)
840{
841 unsigned int tail;
842 unsigned int head;
843 u32 status;
844 struct macb *bp = queue->bp;
845 u16 queue_index = queue - bp->queues;
846
847 status = macb_readl(bp, TSR);
848 macb_writel(bp, TSR, status);
849
850 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
851 queue_writel(queue, ISR, MACB_BIT(TCOMP));
852
853 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
854 (unsigned long)status);
855
856 head = queue->tx_head;
857 for (tail = queue->tx_tail; tail != head; tail++) {
858 struct macb_tx_skb *tx_skb;
859 struct sk_buff *skb;
860 struct macb_dma_desc *desc;
861 u32 ctrl;
862
863 desc = macb_tx_desc(queue, tail);
864
865 /* Make hw descriptor updates visible to CPU */
866 rmb();
867
868 ctrl = desc->ctrl;
869
870 /* TX_USED bit is only set by hardware on the very first buffer
871 * descriptor of the transmitted frame.
872 */
873 if (!(ctrl & MACB_BIT(TX_USED)))
874 break;
875
876 /* Process all buffers of the current transmitted frame */
877 for (;; tail++) {
878 tx_skb = macb_tx_skb(queue, tail);
879 skb = tx_skb->skb;
880
881 /* First, update TX stats if needed */
882 if (skb) {
883 if (unlikely(skb_shinfo(skb)->tx_flags &
884 SKBTX_HW_TSTAMP) &&
885 gem_ptp_do_txstamp(queue, skb, desc) == 0) {
886 /* skb now belongs to timestamp buffer
887 * and will be removed later
888 */
889 tx_skb->skb = NULL;
890 }
891 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
892 macb_tx_ring_wrap(bp, tail),
893 skb->data);
894 bp->dev->stats.tx_packets++;
895 queue->stats.tx_packets++;
896 bp->dev->stats.tx_bytes += skb->len;
897 queue->stats.tx_bytes += skb->len;
898 }
899
900 /* Now we can safely release resources */
901 macb_tx_unmap(bp, tx_skb);
902
903 /* skb is set only for the last buffer of the frame.
904 * WARNING: at this point skb has been freed by
905 * macb_tx_unmap().
906 */
907 if (skb)
908 break;
909 }
910 }
911
912 queue->tx_tail = tail;
913 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
914 CIRC_CNT(queue->tx_head, queue->tx_tail,
915 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
916 netif_wake_subqueue(bp->dev, queue_index);
917}
918
919static void gem_rx_refill(struct macb_queue *queue)
920{
921 unsigned int entry;
922 struct sk_buff *skb;
923 dma_addr_t paddr;
924 struct macb *bp = queue->bp;
925 struct macb_dma_desc *desc;
926
927 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
928 bp->rx_ring_size) > 0) {
929 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
930
931 /* Make hw descriptor updates visible to CPU */
932 rmb();
933
934 desc = macb_rx_desc(queue, entry);
935
936 if (!queue->rx_skbuff[entry]) {
937 /* allocate sk_buff for this free entry in ring */
938 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
939 if (unlikely(!skb)) {
940 netdev_err(bp->dev,
941 "Unable to allocate sk_buff\n");
942 break;
943 }
944
945 /* now fill corresponding descriptor entry */
946 paddr = dma_map_single(&bp->pdev->dev, skb->data,
947 bp->rx_buffer_size,
948 DMA_FROM_DEVICE);
949 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
950 dev_kfree_skb(skb);
951 break;
952 }
953
954 queue->rx_skbuff[entry] = skb;
955
956 if (entry == bp->rx_ring_size - 1)
957 paddr |= MACB_BIT(RX_WRAP);
958 desc->ctrl = 0;
959 /* Setting addr clears RX_USED and allows reception,
960 * make sure ctrl is cleared first to avoid a race.
961 */
962 dma_wmb();
963 macb_set_addr(bp, desc, paddr);
964
965 /* properly align Ethernet header */
966 skb_reserve(skb, NET_IP_ALIGN);
967 } else {
968 desc->ctrl = 0;
969 dma_wmb();
970 desc->addr &= ~MACB_BIT(RX_USED);
971 }
972 queue->rx_prepared_head++;
973 }
974
975 /* Make descriptor updates visible to hardware */
976 wmb();
977
978 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
979 queue, queue->rx_prepared_head, queue->rx_tail);
980}
981
982/* Mark DMA descriptors from begin up to and not including end as unused */
983static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
984 unsigned int end)
985{
986 unsigned int frag;
987
988 for (frag = begin; frag != end; frag++) {
989 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
990
991 desc->addr &= ~MACB_BIT(RX_USED);
992 }
993
994 /* Make descriptor updates visible to hardware */
995 wmb();
996
997 /* When this happens, the hardware stats registers for
998 * whatever caused this is updated, so we don't have to record
999 * anything.
1000 */
1001}
1002
1003static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
1004 int budget)
1005{
1006 struct macb *bp = queue->bp;
1007 unsigned int len;
1008 unsigned int entry;
1009 struct sk_buff *skb;
1010 struct macb_dma_desc *desc;
1011 int count = 0;
1012
1013 while (count < budget) {
1014 u32 ctrl;
1015 dma_addr_t addr;
1016 bool rxused;
1017
1018 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1019 desc = macb_rx_desc(queue, entry);
1020
1021 /* Make hw descriptor updates visible to CPU */
1022 rmb();
1023
1024 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
1025 addr = macb_get_addr(bp, desc);
1026
1027 if (!rxused)
1028 break;
1029
1030 /* Ensure ctrl is at least as up-to-date as rxused */
1031 dma_rmb();
1032
1033 ctrl = desc->ctrl;
1034
1035 queue->rx_tail++;
1036 count++;
1037
1038 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1039 netdev_err(bp->dev,
1040 "not whole frame pointed by descriptor\n");
1041 bp->dev->stats.rx_dropped++;
1042 queue->stats.rx_dropped++;
1043 break;
1044 }
1045 skb = queue->rx_skbuff[entry];
1046 if (unlikely(!skb)) {
1047 netdev_err(bp->dev,
1048 "inconsistent Rx descriptor chain\n");
1049 bp->dev->stats.rx_dropped++;
1050 queue->stats.rx_dropped++;
1051 break;
1052 }
1053 /* now everything is ready for receiving packet */
1054 queue->rx_skbuff[entry] = NULL;
1055 len = ctrl & bp->rx_frm_len_mask;
1056
1057 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1058
1059 skb_put(skb, len);
1060 dma_unmap_single(&bp->pdev->dev, addr,
1061 bp->rx_buffer_size, DMA_FROM_DEVICE);
1062
1063 skb->protocol = eth_type_trans(skb, bp->dev);
1064 skb_checksum_none_assert(skb);
1065 if (bp->dev->features & NETIF_F_RXCSUM &&
1066 !(bp->dev->flags & IFF_PROMISC) &&
1067 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1068 skb->ip_summed = CHECKSUM_UNNECESSARY;
1069
1070 bp->dev->stats.rx_packets++;
1071 queue->stats.rx_packets++;
1072 bp->dev->stats.rx_bytes += skb->len;
1073 queue->stats.rx_bytes += skb->len;
1074
1075 gem_ptp_do_rxstamp(bp, skb, desc);
1076
1077#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1078 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1079 skb->len, skb->csum);
1080 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
1081 skb_mac_header(skb), 16, true);
1082 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1083 skb->data, 32, true);
1084#endif
1085
1086 napi_gro_receive(napi, skb);
1087 }
1088
1089 gem_rx_refill(queue);
1090
1091 return count;
1092}
1093
1094static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
1095 unsigned int first_frag, unsigned int last_frag)
1096{
1097 unsigned int len;
1098 unsigned int frag;
1099 unsigned int offset;
1100 struct sk_buff *skb;
1101 struct macb_dma_desc *desc;
1102 struct macb *bp = queue->bp;
1103
1104 desc = macb_rx_desc(queue, last_frag);
1105 len = desc->ctrl & bp->rx_frm_len_mask;
1106
1107 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
1108 macb_rx_ring_wrap(bp, first_frag),
1109 macb_rx_ring_wrap(bp, last_frag), len);
1110
1111 /* The ethernet header starts NET_IP_ALIGN bytes into the
1112 * first buffer. Since the header is 14 bytes, this makes the
1113 * payload word-aligned.
1114 *
1115 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1116 * the two padding bytes into the skb so that we avoid hitting
1117 * the slowpath in memcpy(), and pull them off afterwards.
1118 */
1119 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
1120 if (!skb) {
1121 bp->dev->stats.rx_dropped++;
1122 for (frag = first_frag; ; frag++) {
1123 desc = macb_rx_desc(queue, frag);
1124 desc->addr &= ~MACB_BIT(RX_USED);
1125 if (frag == last_frag)
1126 break;
1127 }
1128
1129 /* Make descriptor updates visible to hardware */
1130 wmb();
1131
1132 return 1;
1133 }
1134
1135 offset = 0;
1136 len += NET_IP_ALIGN;
1137 skb_checksum_none_assert(skb);
1138 skb_put(skb, len);
1139
1140 for (frag = first_frag; ; frag++) {
1141 unsigned int frag_len = bp->rx_buffer_size;
1142
1143 if (offset + frag_len > len) {
1144 if (unlikely(frag != last_frag)) {
1145 dev_kfree_skb_any(skb);
1146 return -1;
1147 }
1148 frag_len = len - offset;
1149 }
1150 skb_copy_to_linear_data_offset(skb, offset,
1151 macb_rx_buffer(queue, frag),
1152 frag_len);
1153 offset += bp->rx_buffer_size;
1154 desc = macb_rx_desc(queue, frag);
1155 desc->addr &= ~MACB_BIT(RX_USED);
1156
1157 if (frag == last_frag)
1158 break;
1159 }
1160
1161 /* Make descriptor updates visible to hardware */
1162 wmb();
1163
1164 __skb_pull(skb, NET_IP_ALIGN);
1165 skb->protocol = eth_type_trans(skb, bp->dev);
1166
1167 bp->dev->stats.rx_packets++;
1168 bp->dev->stats.rx_bytes += skb->len;
1169 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1170 skb->len, skb->csum);
1171 napi_gro_receive(napi, skb);
1172
1173 return 0;
1174}
1175
1176static inline void macb_init_rx_ring(struct macb_queue *queue)
1177{
1178 struct macb *bp = queue->bp;
1179 dma_addr_t addr;
1180 struct macb_dma_desc *desc = NULL;
1181 int i;
1182
1183 addr = queue->rx_buffers_dma;
1184 for (i = 0; i < bp->rx_ring_size; i++) {
1185 desc = macb_rx_desc(queue, i);
1186 macb_set_addr(bp, desc, addr);
1187 desc->ctrl = 0;
1188 addr += bp->rx_buffer_size;
1189 }
1190 desc->addr |= MACB_BIT(RX_WRAP);
1191 queue->rx_tail = 0;
1192}
1193
1194static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
1195 int budget)
1196{
1197 struct macb *bp = queue->bp;
1198 bool reset_rx_queue = false;
1199 int received = 0;
1200 unsigned int tail;
1201 int first_frag = -1;
1202
1203 for (tail = queue->rx_tail; budget > 0; tail++) {
1204 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
1205 u32 ctrl;
1206
1207 /* Make hw descriptor updates visible to CPU */
1208 rmb();
1209
1210 if (!(desc->addr & MACB_BIT(RX_USED)))
1211 break;
1212
1213 /* Ensure ctrl is at least as up-to-date as addr */
1214 dma_rmb();
1215
1216 ctrl = desc->ctrl;
1217
1218 if (ctrl & MACB_BIT(RX_SOF)) {
1219 if (first_frag != -1)
1220 discard_partial_frame(queue, first_frag, tail);
1221 first_frag = tail;
1222 }
1223
1224 if (ctrl & MACB_BIT(RX_EOF)) {
1225 int dropped;
1226
1227 if (unlikely(first_frag == -1)) {
1228 reset_rx_queue = true;
1229 continue;
1230 }
1231
1232 dropped = macb_rx_frame(queue, napi, first_frag, tail);
1233 first_frag = -1;
1234 if (unlikely(dropped < 0)) {
1235 reset_rx_queue = true;
1236 continue;
1237 }
1238 if (!dropped) {
1239 received++;
1240 budget--;
1241 }
1242 }
1243 }
1244
1245 if (unlikely(reset_rx_queue)) {
1246 unsigned long flags;
1247 u32 ctrl;
1248
1249 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1250
1251 spin_lock_irqsave(&bp->lock, flags);
1252
1253 ctrl = macb_readl(bp, NCR);
1254 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1255
1256 macb_init_rx_ring(queue);
1257 queue_writel(queue, RBQP, queue->rx_ring_dma);
1258
1259 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1260
1261 spin_unlock_irqrestore(&bp->lock, flags);
1262 return received;
1263 }
1264
1265 if (first_frag != -1)
1266 queue->rx_tail = first_frag;
1267 else
1268 queue->rx_tail = tail;
1269
1270 return received;
1271}
1272
1273static int macb_poll(struct napi_struct *napi, int budget)
1274{
1275 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1276 struct macb *bp = queue->bp;
1277 int work_done;
1278 u32 status;
1279
1280 status = macb_readl(bp, RSR);
1281 macb_writel(bp, RSR, status);
1282
1283 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
1284 (unsigned long)status, budget);
1285
1286 work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
1287 if (work_done < budget) {
1288 napi_complete_done(napi, work_done);
1289
1290 /* RSR bits only seem to propagate to raise interrupts when
1291 * interrupts are enabled at the time, so if bits are already
1292 * set due to packets received while interrupts were disabled,
1293 * they will not cause another interrupt to be generated when
1294 * interrupts are re-enabled.
1295 * Check for this case here. This has been seen to happen
1296 * around 30% of the time under heavy network load.
1297 */
1298 status = macb_readl(bp, RSR);
1299 if (status) {
1300 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1301 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1302 napi_reschedule(napi);
1303 } else {
1304 queue_writel(queue, IER, bp->rx_intr_mask);
1305
1306 /* In rare cases, packets could have been received in
1307 * the window between the check above and re-enabling
1308 * interrupts. Therefore, a double-check is required
1309 * to avoid losing a wakeup. This can potentially race
1310 * with the interrupt handler doing the same actions
1311 * if an interrupt is raised just after enabling them,
1312 * but this should be harmless.
1313 */
1314 status = macb_readl(bp, RSR);
1315 if (unlikely(status)) {
1316 queue_writel(queue, IDR, bp->rx_intr_mask);
1317 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1318 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1319 napi_schedule(napi);
1320 }
1321 }
1322 }
1323
1324 /* TODO: Handle errors */
1325
1326 return work_done;
1327}
1328
1329static void macb_hresp_error_task(unsigned long data)
1330{
1331 struct macb *bp = (struct macb *)data;
1332 struct net_device *dev = bp->dev;
1333 struct macb_queue *queue = bp->queues;
1334 unsigned int q;
1335 u32 ctrl;
1336
1337 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1338 queue_writel(queue, IDR, bp->rx_intr_mask |
1339 MACB_TX_INT_FLAGS |
1340 MACB_BIT(HRESP));
1341 }
1342 ctrl = macb_readl(bp, NCR);
1343 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1344 macb_writel(bp, NCR, ctrl);
1345
1346 netif_tx_stop_all_queues(dev);
1347 netif_carrier_off(dev);
1348
1349 bp->macbgem_ops.mog_init_rings(bp);
1350
1351 /* Initialize TX and RX buffers */
1352 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1353 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1354#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1355 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1356 queue_writel(queue, RBQPH,
1357 upper_32_bits(queue->rx_ring_dma));
1358#endif
1359 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1360#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1361 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1362 queue_writel(queue, TBQPH,
1363 upper_32_bits(queue->tx_ring_dma));
1364#endif
1365
1366 /* Enable interrupts */
1367 queue_writel(queue, IER,
1368 bp->rx_intr_mask |
1369 MACB_TX_INT_FLAGS |
1370 MACB_BIT(HRESP));
1371 }
1372
1373 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1374 macb_writel(bp, NCR, ctrl);
1375
1376 netif_carrier_on(dev);
1377 netif_tx_start_all_queues(dev);
1378}
1379
1380static void macb_tx_restart(struct macb_queue *queue)
1381{
1382 unsigned int head = queue->tx_head;
1383 unsigned int tail = queue->tx_tail;
1384 struct macb *bp = queue->bp;
1385 unsigned int head_idx, tbqp;
1386
1387 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1388 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1389
1390 if (head == tail)
1391 return;
1392
1393 tbqp = queue_readl(queue, TBQP) / macb_dma_desc_get_size(bp);
1394 tbqp = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, tbqp));
1395 head_idx = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, head));
1396
1397 if (tbqp == head_idx)
1398 return;
1399
1400 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1401}
1402
1403static irqreturn_t macb_interrupt(int irq, void *dev_id)
1404{
1405 struct macb_queue *queue = dev_id;
1406 struct macb *bp = queue->bp;
1407 struct net_device *dev = bp->dev;
1408 u32 status, ctrl;
1409
1410 status = queue_readl(queue, ISR);
1411
1412 if (unlikely(!status))
1413 return IRQ_NONE;
1414
1415 spin_lock(&bp->lock);
1416
1417 while (status) {
1418 /* close possible race with dev_close */
1419 if (unlikely(!netif_running(dev))) {
1420 queue_writel(queue, IDR, -1);
1421 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1422 queue_writel(queue, ISR, -1);
1423 break;
1424 }
1425
1426 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1427 (unsigned int)(queue - bp->queues),
1428 (unsigned long)status);
1429
1430 if (status & bp->rx_intr_mask) {
1431 /* There's no point taking any more interrupts
1432 * until we have processed the buffers. The
1433 * scheduling call may fail if the poll routine
1434 * is already scheduled, so disable interrupts
1435 * now.
1436 */
1437 queue_writel(queue, IDR, bp->rx_intr_mask);
1438 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1439 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1440
1441 if (napi_schedule_prep(&queue->napi)) {
1442 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1443 __napi_schedule(&queue->napi);
1444 }
1445 }
1446
1447 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1448 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1449 schedule_work(&queue->tx_error_task);
1450
1451 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1452 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1453
1454 break;
1455 }
1456
1457 if (status & MACB_BIT(TCOMP))
1458 macb_tx_interrupt(queue);
1459
1460 if (status & MACB_BIT(TXUBR))
1461 macb_tx_restart(queue);
1462
1463 /* Link change detection isn't possible with RMII, so we'll
1464 * add that if/when we get our hands on a full-blown MII PHY.
1465 */
1466
1467 /* There is a hardware issue under heavy load where DMA can
1468 * stop, this causes endless "used buffer descriptor read"
1469 * interrupts but it can be cleared by re-enabling RX. See
1470 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1471 * section 16.7.4 for details. RXUBR is only enabled for
1472 * these two versions.
1473 */
1474 if (status & MACB_BIT(RXUBR)) {
1475 ctrl = macb_readl(bp, NCR);
1476 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1477 wmb();
1478 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1479
1480 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1481 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1482 }
1483
1484 if (status & MACB_BIT(ISR_ROVR)) {
1485 /* We missed at least one packet */
1486 if (macb_is_gem(bp))
1487 bp->hw_stats.gem.rx_overruns++;
1488 else
1489 bp->hw_stats.macb.rx_overruns++;
1490
1491 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1492 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1493 }
1494
1495 if (status & MACB_BIT(HRESP)) {
1496 tasklet_schedule(&bp->hresp_err_tasklet);
1497 netdev_err(dev, "DMA bus error: HRESP not OK\n");
1498
1499 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1500 queue_writel(queue, ISR, MACB_BIT(HRESP));
1501 }
1502 status = queue_readl(queue, ISR);
1503 }
1504
1505 spin_unlock(&bp->lock);
1506
1507 return IRQ_HANDLED;
1508}
1509
1510#ifdef CONFIG_NET_POLL_CONTROLLER
1511/* Polling receive - used by netconsole and other diagnostic tools
1512 * to allow network i/o with interrupts disabled.
1513 */
1514static void macb_poll_controller(struct net_device *dev)
1515{
1516 struct macb *bp = netdev_priv(dev);
1517 struct macb_queue *queue;
1518 unsigned long flags;
1519 unsigned int q;
1520
1521 local_irq_save(flags);
1522 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1523 macb_interrupt(dev->irq, queue);
1524 local_irq_restore(flags);
1525}
1526#endif
1527
1528static unsigned int macb_tx_map(struct macb *bp,
1529 struct macb_queue *queue,
1530 struct sk_buff *skb,
1531 unsigned int hdrlen)
1532{
1533 dma_addr_t mapping;
1534 unsigned int len, entry, i, tx_head = queue->tx_head;
1535 struct macb_tx_skb *tx_skb = NULL;
1536 struct macb_dma_desc *desc;
1537 unsigned int offset, size, count = 0;
1538 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1539 unsigned int eof = 1, mss_mfs = 0;
1540 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1541
1542 /* LSO */
1543 if (skb_shinfo(skb)->gso_size != 0) {
1544 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1545 /* UDP - UFO */
1546 lso_ctrl = MACB_LSO_UFO_ENABLE;
1547 else
1548 /* TCP - TSO */
1549 lso_ctrl = MACB_LSO_TSO_ENABLE;
1550 }
1551
1552 /* First, map non-paged data */
1553 len = skb_headlen(skb);
1554
1555 /* first buffer length */
1556 size = hdrlen;
1557
1558 offset = 0;
1559 while (len) {
1560 entry = macb_tx_ring_wrap(bp, tx_head);
1561 tx_skb = &queue->tx_skb[entry];
1562
1563 mapping = dma_map_single(&bp->pdev->dev,
1564 skb->data + offset,
1565 size, DMA_TO_DEVICE);
1566 if (dma_mapping_error(&bp->pdev->dev, mapping))
1567 goto dma_error;
1568
1569 /* Save info to properly release resources */
1570 tx_skb->skb = NULL;
1571 tx_skb->mapping = mapping;
1572 tx_skb->size = size;
1573 tx_skb->mapped_as_page = false;
1574
1575 len -= size;
1576 offset += size;
1577 count++;
1578 tx_head++;
1579
1580 size = min(len, bp->max_tx_length);
1581 }
1582
1583 /* Then, map paged data from fragments */
1584 for (f = 0; f < nr_frags; f++) {
1585 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1586
1587 len = skb_frag_size(frag);
1588 offset = 0;
1589 while (len) {
1590 size = min(len, bp->max_tx_length);
1591 entry = macb_tx_ring_wrap(bp, tx_head);
1592 tx_skb = &queue->tx_skb[entry];
1593
1594 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1595 offset, size, DMA_TO_DEVICE);
1596 if (dma_mapping_error(&bp->pdev->dev, mapping))
1597 goto dma_error;
1598
1599 /* Save info to properly release resources */
1600 tx_skb->skb = NULL;
1601 tx_skb->mapping = mapping;
1602 tx_skb->size = size;
1603 tx_skb->mapped_as_page = true;
1604
1605 len -= size;
1606 offset += size;
1607 count++;
1608 tx_head++;
1609 }
1610 }
1611
1612 /* Should never happen */
1613 if (unlikely(!tx_skb)) {
1614 netdev_err(bp->dev, "BUG! empty skb!\n");
1615 return 0;
1616 }
1617
1618 /* This is the last buffer of the frame: save socket buffer */
1619 tx_skb->skb = skb;
1620
1621 /* Update TX ring: update buffer descriptors in reverse order
1622 * to avoid race condition
1623 */
1624
1625 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1626 * to set the end of TX queue
1627 */
1628 i = tx_head;
1629 entry = macb_tx_ring_wrap(bp, i);
1630 ctrl = MACB_BIT(TX_USED);
1631 desc = macb_tx_desc(queue, entry);
1632 desc->ctrl = ctrl;
1633
1634 if (lso_ctrl) {
1635 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1636 /* include header and FCS in value given to h/w */
1637 mss_mfs = skb_shinfo(skb)->gso_size +
1638 skb_transport_offset(skb) +
1639 ETH_FCS_LEN;
1640 else /* TSO */ {
1641 mss_mfs = skb_shinfo(skb)->gso_size;
1642 /* TCP Sequence Number Source Select
1643 * can be set only for TSO
1644 */
1645 seq_ctrl = 0;
1646 }
1647 }
1648
1649 do {
1650 i--;
1651 entry = macb_tx_ring_wrap(bp, i);
1652 tx_skb = &queue->tx_skb[entry];
1653 desc = macb_tx_desc(queue, entry);
1654
1655 ctrl = (u32)tx_skb->size;
1656 if (eof) {
1657 ctrl |= MACB_BIT(TX_LAST);
1658 eof = 0;
1659 }
1660 if (unlikely(entry == (bp->tx_ring_size - 1)))
1661 ctrl |= MACB_BIT(TX_WRAP);
1662
1663 /* First descriptor is header descriptor */
1664 if (i == queue->tx_head) {
1665 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1666 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1667 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1668 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1669 ctrl |= MACB_BIT(TX_NOCRC);
1670 } else
1671 /* Only set MSS/MFS on payload descriptors
1672 * (second or later descriptor)
1673 */
1674 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1675
1676 /* Set TX buffer descriptor */
1677 macb_set_addr(bp, desc, tx_skb->mapping);
1678 /* desc->addr must be visible to hardware before clearing
1679 * 'TX_USED' bit in desc->ctrl.
1680 */
1681 wmb();
1682 desc->ctrl = ctrl;
1683 } while (i != queue->tx_head);
1684
1685 queue->tx_head = tx_head;
1686
1687 return count;
1688
1689dma_error:
1690 netdev_err(bp->dev, "TX DMA map failed\n");
1691
1692 for (i = queue->tx_head; i != tx_head; i++) {
1693 tx_skb = macb_tx_skb(queue, i);
1694
1695 macb_tx_unmap(bp, tx_skb);
1696 }
1697
1698 return 0;
1699}
1700
1701static netdev_features_t macb_features_check(struct sk_buff *skb,
1702 struct net_device *dev,
1703 netdev_features_t features)
1704{
1705 unsigned int nr_frags, f;
1706 unsigned int hdrlen;
1707
1708 /* Validate LSO compatibility */
1709
1710 /* there is only one buffer or protocol is not UDP */
1711 if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
1712 return features;
1713
1714 /* length of header */
1715 hdrlen = skb_transport_offset(skb);
1716
1717 /* For UFO only:
1718 * When software supplies two or more payload buffers all payload buffers
1719 * apart from the last must be a multiple of 8 bytes in size.
1720 */
1721 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1722 return features & ~MACB_NETIF_LSO;
1723
1724 nr_frags = skb_shinfo(skb)->nr_frags;
1725 /* No need to check last fragment */
1726 nr_frags--;
1727 for (f = 0; f < nr_frags; f++) {
1728 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1729
1730 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1731 return features & ~MACB_NETIF_LSO;
1732 }
1733 return features;
1734}
1735
1736static inline int macb_clear_csum(struct sk_buff *skb)
1737{
1738 /* no change for packets without checksum offloading */
1739 if (skb->ip_summed != CHECKSUM_PARTIAL)
1740 return 0;
1741
1742 /* make sure we can modify the header */
1743 if (unlikely(skb_cow_head(skb, 0)))
1744 return -1;
1745
1746 /* initialize checksum field
1747 * This is required - at least for Zynq, which otherwise calculates
1748 * wrong UDP header checksums for UDP packets with UDP data len <=2
1749 */
1750 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1751 return 0;
1752}
1753
1754static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1755{
1756 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
1757 skb_is_nonlinear(*skb);
1758 int padlen = ETH_ZLEN - (*skb)->len;
1759 int tailroom = skb_tailroom(*skb);
1760 struct sk_buff *nskb;
1761 u32 fcs;
1762
1763 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1764 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1765 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1766 return 0;
1767
1768 if (padlen <= 0) {
1769 /* FCS could be appeded to tailroom. */
1770 if (tailroom >= ETH_FCS_LEN)
1771 goto add_fcs;
1772 /* No room for FCS, need to reallocate skb. */
1773 else
1774 padlen = ETH_FCS_LEN;
1775 } else {
1776 /* Add room for FCS. */
1777 padlen += ETH_FCS_LEN;
1778 }
1779
1780 if (cloned || tailroom < padlen) {
1781 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1782 if (!nskb)
1783 return -ENOMEM;
1784
1785 dev_consume_skb_any(*skb);
1786 *skb = nskb;
1787 }
1788
1789 if (padlen > ETH_FCS_LEN)
1790 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1791
1792add_fcs:
1793 /* set FCS to packet */
1794 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1795 fcs = ~fcs;
1796
1797 skb_put_u8(*skb, fcs & 0xff);
1798 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1799 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1800 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1801
1802 return 0;
1803}
1804
1805static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1806{
1807 u16 queue_index = skb_get_queue_mapping(skb);
1808 struct macb *bp = netdev_priv(dev);
1809 struct macb_queue *queue = &bp->queues[queue_index];
1810 unsigned long flags;
1811 unsigned int desc_cnt, nr_frags, frag_size, f;
1812 unsigned int hdrlen;
1813 bool is_lso, is_udp = 0;
1814 netdev_tx_t ret = NETDEV_TX_OK;
1815
1816 if (macb_clear_csum(skb)) {
1817 dev_kfree_skb_any(skb);
1818 return ret;
1819 }
1820
1821 if (macb_pad_and_fcs(&skb, dev)) {
1822 dev_kfree_skb_any(skb);
1823 return ret;
1824 }
1825
1826 is_lso = (skb_shinfo(skb)->gso_size != 0);
1827
1828 if (is_lso) {
1829 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1830
1831 /* length of headers */
1832 if (is_udp)
1833 /* only queue eth + ip headers separately for UDP */
1834 hdrlen = skb_transport_offset(skb);
1835 else
1836 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1837 if (skb_headlen(skb) < hdrlen) {
1838 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1839 /* if this is required, would need to copy to single buffer */
1840 return NETDEV_TX_BUSY;
1841 }
1842 } else
1843 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
1844
1845#if defined(DEBUG) && defined(VERBOSE_DEBUG)
1846 netdev_vdbg(bp->dev,
1847 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1848 queue_index, skb->len, skb->head, skb->data,
1849 skb_tail_pointer(skb), skb_end_pointer(skb));
1850 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1851 skb->data, 16, true);
1852#endif
1853
1854 /* Count how many TX buffer descriptors are needed to send this
1855 * socket buffer: skb fragments of jumbo frames may need to be
1856 * split into many buffer descriptors.
1857 */
1858 if (is_lso && (skb_headlen(skb) > hdrlen))
1859 /* extra header descriptor if also payload in first buffer */
1860 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1861 else
1862 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
1863 nr_frags = skb_shinfo(skb)->nr_frags;
1864 for (f = 0; f < nr_frags; f++) {
1865 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1866 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
1867 }
1868
1869 spin_lock_irqsave(&bp->lock, flags);
1870
1871 /* This is a hard error, log it. */
1872 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1873 bp->tx_ring_size) < desc_cnt) {
1874 netif_stop_subqueue(dev, queue_index);
1875 spin_unlock_irqrestore(&bp->lock, flags);
1876 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1877 queue->tx_head, queue->tx_tail);
1878 return NETDEV_TX_BUSY;
1879 }
1880
1881 /* Map socket buffer for DMA transfer */
1882 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
1883 dev_kfree_skb_any(skb);
1884 goto unlock;
1885 }
1886
1887 /* Make newly initialized descriptor visible to hardware */
1888 wmb();
1889 skb_tx_timestamp(skb);
1890
1891 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1892
1893 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
1894 netif_stop_subqueue(dev, queue_index);
1895
1896unlock:
1897 spin_unlock_irqrestore(&bp->lock, flags);
1898
1899 return ret;
1900}
1901
1902static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1903{
1904 if (!macb_is_gem(bp)) {
1905 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1906 } else {
1907 bp->rx_buffer_size = size;
1908
1909 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1910 netdev_dbg(bp->dev,
1911 "RX buffer must be multiple of %d bytes, expanding\n",
1912 RX_BUFFER_MULTIPLE);
1913 bp->rx_buffer_size =
1914 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1915 }
1916 }
1917
1918 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
1919 bp->dev->mtu, bp->rx_buffer_size);
1920}
1921
1922static void gem_free_rx_buffers(struct macb *bp)
1923{
1924 struct sk_buff *skb;
1925 struct macb_dma_desc *desc;
1926 struct macb_queue *queue;
1927 dma_addr_t addr;
1928 unsigned int q;
1929 int i;
1930
1931 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1932 if (!queue->rx_skbuff)
1933 continue;
1934
1935 for (i = 0; i < bp->rx_ring_size; i++) {
1936 skb = queue->rx_skbuff[i];
1937
1938 if (!skb)
1939 continue;
1940
1941 desc = macb_rx_desc(queue, i);
1942 addr = macb_get_addr(bp, desc);
1943
1944 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1945 DMA_FROM_DEVICE);
1946 dev_kfree_skb_any(skb);
1947 skb = NULL;
1948 }
1949
1950 kfree(queue->rx_skbuff);
1951 queue->rx_skbuff = NULL;
1952 }
1953}
1954
1955static void macb_free_rx_buffers(struct macb *bp)
1956{
1957 struct macb_queue *queue = &bp->queues[0];
1958
1959 if (queue->rx_buffers) {
1960 dma_free_coherent(&bp->pdev->dev,
1961 bp->rx_ring_size * bp->rx_buffer_size,
1962 queue->rx_buffers, queue->rx_buffers_dma);
1963 queue->rx_buffers = NULL;
1964 }
1965}
1966
1967static void macb_free_consistent(struct macb *bp)
1968{
1969 struct macb_queue *queue;
1970 unsigned int q;
1971 int size;
1972
1973 bp->macbgem_ops.mog_free_rx_buffers(bp);
1974
1975 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1976 kfree(queue->tx_skb);
1977 queue->tx_skb = NULL;
1978 if (queue->tx_ring) {
1979 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1980 dma_free_coherent(&bp->pdev->dev, size,
1981 queue->tx_ring, queue->tx_ring_dma);
1982 queue->tx_ring = NULL;
1983 }
1984 if (queue->rx_ring) {
1985 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1986 dma_free_coherent(&bp->pdev->dev, size,
1987 queue->rx_ring, queue->rx_ring_dma);
1988 queue->rx_ring = NULL;
1989 }
1990 }
1991}
1992
1993static int gem_alloc_rx_buffers(struct macb *bp)
1994{
1995 struct macb_queue *queue;
1996 unsigned int q;
1997 int size;
1998
1999 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2000 size = bp->rx_ring_size * sizeof(struct sk_buff *);
2001 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
2002 if (!queue->rx_skbuff)
2003 return -ENOMEM;
2004 else
2005 netdev_dbg(bp->dev,
2006 "Allocated %d RX struct sk_buff entries at %p\n",
2007 bp->rx_ring_size, queue->rx_skbuff);
2008 }
2009 return 0;
2010}
2011
2012static int macb_alloc_rx_buffers(struct macb *bp)
2013{
2014 struct macb_queue *queue = &bp->queues[0];
2015 int size;
2016
2017 size = bp->rx_ring_size * bp->rx_buffer_size;
2018 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2019 &queue->rx_buffers_dma, GFP_KERNEL);
2020 if (!queue->rx_buffers)
2021 return -ENOMEM;
2022
2023 netdev_dbg(bp->dev,
2024 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
2025 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
2026 return 0;
2027}
2028
2029static int macb_alloc_consistent(struct macb *bp)
2030{
2031 struct macb_queue *queue;
2032 unsigned int q;
2033 int size;
2034
2035 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2036 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2037 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2038 &queue->tx_ring_dma,
2039 GFP_KERNEL);
2040 if (!queue->tx_ring)
2041 goto out_err;
2042 netdev_dbg(bp->dev,
2043 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2044 q, size, (unsigned long)queue->tx_ring_dma,
2045 queue->tx_ring);
2046
2047 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
2048 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2049 if (!queue->tx_skb)
2050 goto out_err;
2051
2052 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2053 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2054 &queue->rx_ring_dma, GFP_KERNEL);
2055 if (!queue->rx_ring)
2056 goto out_err;
2057 netdev_dbg(bp->dev,
2058 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2059 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
2060 }
2061 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2062 goto out_err;
2063
2064 return 0;
2065
2066out_err:
2067 macb_free_consistent(bp);
2068 return -ENOMEM;
2069}
2070
2071static void gem_init_rings(struct macb *bp)
2072{
2073 struct macb_queue *queue;
2074 struct macb_dma_desc *desc = NULL;
2075 unsigned int q;
2076 int i;
2077
2078 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2079 for (i = 0; i < bp->tx_ring_size; i++) {
2080 desc = macb_tx_desc(queue, i);
2081 macb_set_addr(bp, desc, 0);
2082 desc->ctrl = MACB_BIT(TX_USED);
2083 }
2084 desc->ctrl |= MACB_BIT(TX_WRAP);
2085 queue->tx_head = 0;
2086 queue->tx_tail = 0;
2087
2088 queue->rx_tail = 0;
2089 queue->rx_prepared_head = 0;
2090
2091 gem_rx_refill(queue);
2092 }
2093
2094}
2095
2096static void macb_init_rings(struct macb *bp)
2097{
2098 int i;
2099 struct macb_dma_desc *desc = NULL;
2100
2101 macb_init_rx_ring(&bp->queues[0]);
2102
2103 for (i = 0; i < bp->tx_ring_size; i++) {
2104 desc = macb_tx_desc(&bp->queues[0], i);
2105 macb_set_addr(bp, desc, 0);
2106 desc->ctrl = MACB_BIT(TX_USED);
2107 }
2108 bp->queues[0].tx_head = 0;
2109 bp->queues[0].tx_tail = 0;
2110 desc->ctrl |= MACB_BIT(TX_WRAP);
2111}
2112
2113static void macb_reset_hw(struct macb *bp)
2114{
2115 struct macb_queue *queue;
2116 unsigned int q;
2117 u32 ctrl = macb_readl(bp, NCR);
2118
2119 /* Disable RX and TX (XXX: Should we halt the transmission
2120 * more gracefully?)
2121 */
2122 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2123
2124 /* Clear the stats registers (XXX: Update stats first?) */
2125 ctrl |= MACB_BIT(CLRSTAT);
2126
2127 macb_writel(bp, NCR, ctrl);
2128
2129 /* Clear all status flags */
2130 macb_writel(bp, TSR, -1);
2131 macb_writel(bp, RSR, -1);
2132
2133 /* Disable all interrupts */
2134 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2135 queue_writel(queue, IDR, -1);
2136 queue_readl(queue, ISR);
2137 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2138 queue_writel(queue, ISR, -1);
2139 }
2140}
2141
2142static u32 gem_mdc_clk_div(struct macb *bp)
2143{
2144 u32 config;
2145 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2146
2147 if (pclk_hz <= 20000000)
2148 config = GEM_BF(CLK, GEM_CLK_DIV8);
2149 else if (pclk_hz <= 40000000)
2150 config = GEM_BF(CLK, GEM_CLK_DIV16);
2151 else if (pclk_hz <= 80000000)
2152 config = GEM_BF(CLK, GEM_CLK_DIV32);
2153 else if (pclk_hz <= 120000000)
2154 config = GEM_BF(CLK, GEM_CLK_DIV48);
2155 else if (pclk_hz <= 160000000)
2156 config = GEM_BF(CLK, GEM_CLK_DIV64);
2157 else
2158 config = GEM_BF(CLK, GEM_CLK_DIV96);
2159
2160 return config;
2161}
2162
2163static u32 macb_mdc_clk_div(struct macb *bp)
2164{
2165 u32 config;
2166 unsigned long pclk_hz;
2167
2168 if (macb_is_gem(bp))
2169 return gem_mdc_clk_div(bp);
2170
2171 pclk_hz = clk_get_rate(bp->pclk);
2172 if (pclk_hz <= 20000000)
2173 config = MACB_BF(CLK, MACB_CLK_DIV8);
2174 else if (pclk_hz <= 40000000)
2175 config = MACB_BF(CLK, MACB_CLK_DIV16);
2176 else if (pclk_hz <= 80000000)
2177 config = MACB_BF(CLK, MACB_CLK_DIV32);
2178 else
2179 config = MACB_BF(CLK, MACB_CLK_DIV64);
2180
2181 return config;
2182}
2183
2184/* Get the DMA bus width field of the network configuration register that we
2185 * should program. We find the width from decoding the design configuration
2186 * register to find the maximum supported data bus width.
2187 */
2188static u32 macb_dbw(struct macb *bp)
2189{
2190 if (!macb_is_gem(bp))
2191 return 0;
2192
2193 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2194 case 4:
2195 return GEM_BF(DBW, GEM_DBW128);
2196 case 2:
2197 return GEM_BF(DBW, GEM_DBW64);
2198 case 1:
2199 default:
2200 return GEM_BF(DBW, GEM_DBW32);
2201 }
2202}
2203
2204/* Configure the receive DMA engine
2205 * - use the correct receive buffer size
2206 * - set best burst length for DMA operations
2207 * (if not supported by FIFO, it will fallback to default)
2208 * - set both rx/tx packet buffers to full memory size
2209 * These are configurable parameters for GEM.
2210 */
2211static void macb_configure_dma(struct macb *bp)
2212{
2213 struct macb_queue *queue;
2214 u32 buffer_size;
2215 unsigned int q;
2216 u32 dmacfg;
2217
2218 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
2219 if (macb_is_gem(bp)) {
2220 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2221 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2222 if (q)
2223 queue_writel(queue, RBQS, buffer_size);
2224 else
2225 dmacfg |= GEM_BF(RXBS, buffer_size);
2226 }
2227 if (bp->dma_burst_length)
2228 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2229 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2230 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2231
2232 if (bp->native_io)
2233 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2234 else
2235 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2236
2237 if (bp->dev->features & NETIF_F_HW_CSUM)
2238 dmacfg |= GEM_BIT(TXCOEN);
2239 else
2240 dmacfg &= ~GEM_BIT(TXCOEN);
2241
2242 dmacfg &= ~GEM_BIT(ADDR64);
2243#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2244 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2245 dmacfg |= GEM_BIT(ADDR64);
2246#endif
2247#ifdef CONFIG_MACB_USE_HWSTAMP
2248 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2249 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2250#endif
2251 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2252 dmacfg);
2253 gem_writel(bp, DMACFG, dmacfg);
2254 }
2255}
2256
2257static void macb_init_hw(struct macb *bp)
2258{
2259 struct macb_queue *queue;
2260 unsigned int q;
2261
2262 u32 config;
2263
2264 macb_reset_hw(bp);
2265 macb_set_hwaddr(bp);
2266
2267 config = macb_mdc_clk_div(bp);
2268 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2269 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
2270 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
2271 config |= MACB_BIT(PAE); /* PAuse Enable */
2272 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
2273 if (bp->caps & MACB_CAPS_JUMBO)
2274 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2275 else
2276 config |= MACB_BIT(BIG); /* Receive oversized frames */
2277 if (bp->dev->flags & IFF_PROMISC)
2278 config |= MACB_BIT(CAF); /* Copy All Frames */
2279 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2280 config |= GEM_BIT(RXCOEN);
2281 if (!(bp->dev->flags & IFF_BROADCAST))
2282 config |= MACB_BIT(NBC); /* No BroadCast */
2283 config |= macb_dbw(bp);
2284 macb_writel(bp, NCFGR, config);
2285 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2286 gem_writel(bp, JML, bp->jumbo_max_len);
2287 bp->speed = SPEED_10;
2288 bp->duplex = DUPLEX_HALF;
2289 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
2290 if (bp->caps & MACB_CAPS_JUMBO)
2291 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
2292
2293 macb_configure_dma(bp);
2294
2295 /* Initialize TX and RX buffers */
2296 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2297 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2298#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2299 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2300 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2301#endif
2302 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
2303#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2304 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2305 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
2306#endif
2307
2308 /* Enable interrupts */
2309 queue_writel(queue, IER,
2310 bp->rx_intr_mask |
2311 MACB_TX_INT_FLAGS |
2312 MACB_BIT(HRESP));
2313 }
2314
2315 /* Enable TX and RX */
2316 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
2317}
2318
2319/* The hash address register is 64 bits long and takes up two
2320 * locations in the memory map. The least significant bits are stored
2321 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2322 *
2323 * The unicast hash enable and the multicast hash enable bits in the
2324 * network configuration register enable the reception of hash matched
2325 * frames. The destination address is reduced to a 6 bit index into
2326 * the 64 bit hash register using the following hash function. The
2327 * hash function is an exclusive or of every sixth bit of the
2328 * destination address.
2329 *
2330 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2331 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2332 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2333 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2334 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2335 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2336 *
2337 * da[0] represents the least significant bit of the first byte
2338 * received, that is, the multicast/unicast indicator, and da[47]
2339 * represents the most significant bit of the last byte received. If
2340 * the hash index, hi[n], points to a bit that is set in the hash
2341 * register then the frame will be matched according to whether the
2342 * frame is multicast or unicast. A multicast match will be signalled
2343 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2344 * index points to a bit set in the hash register. A unicast match
2345 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2346 * and the hash index points to a bit set in the hash register. To
2347 * receive all multicast frames, the hash register should be set with
2348 * all ones and the multicast hash enable bit should be set in the
2349 * network configuration register.
2350 */
2351
2352static inline int hash_bit_value(int bitnr, __u8 *addr)
2353{
2354 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2355 return 1;
2356 return 0;
2357}
2358
2359/* Return the hash index value for the specified address. */
2360static int hash_get_index(__u8 *addr)
2361{
2362 int i, j, bitval;
2363 int hash_index = 0;
2364
2365 for (j = 0; j < 6; j++) {
2366 for (i = 0, bitval = 0; i < 8; i++)
2367 bitval ^= hash_bit_value(i * 6 + j, addr);
2368
2369 hash_index |= (bitval << j);
2370 }
2371
2372 return hash_index;
2373}
2374
2375/* Add multicast addresses to the internal multicast-hash table. */
2376static void macb_sethashtable(struct net_device *dev)
2377{
2378 struct netdev_hw_addr *ha;
2379 unsigned long mc_filter[2];
2380 unsigned int bitnr;
2381 struct macb *bp = netdev_priv(dev);
2382
2383 mc_filter[0] = 0;
2384 mc_filter[1] = 0;
2385
2386 netdev_for_each_mc_addr(ha, dev) {
2387 bitnr = hash_get_index(ha->addr);
2388 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2389 }
2390
2391 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2392 macb_or_gem_writel(bp, HRT, mc_filter[1]);
2393}
2394
2395/* Enable/Disable promiscuous and multicast modes. */
2396static void macb_set_rx_mode(struct net_device *dev)
2397{
2398 unsigned long cfg;
2399 struct macb *bp = netdev_priv(dev);
2400
2401 cfg = macb_readl(bp, NCFGR);
2402
2403 if (dev->flags & IFF_PROMISC) {
2404 /* Enable promiscuous mode */
2405 cfg |= MACB_BIT(CAF);
2406
2407 /* Disable RX checksum offload */
2408 if (macb_is_gem(bp))
2409 cfg &= ~GEM_BIT(RXCOEN);
2410 } else {
2411 /* Disable promiscuous mode */
2412 cfg &= ~MACB_BIT(CAF);
2413
2414 /* Enable RX checksum offload only if requested */
2415 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2416 cfg |= GEM_BIT(RXCOEN);
2417 }
2418
2419 if (dev->flags & IFF_ALLMULTI) {
2420 /* Enable all multicast mode */
2421 macb_or_gem_writel(bp, HRB, -1);
2422 macb_or_gem_writel(bp, HRT, -1);
2423 cfg |= MACB_BIT(NCFGR_MTI);
2424 } else if (!netdev_mc_empty(dev)) {
2425 /* Enable specific multicasts */
2426 macb_sethashtable(dev);
2427 cfg |= MACB_BIT(NCFGR_MTI);
2428 } else if (dev->flags & (~IFF_ALLMULTI)) {
2429 /* Disable all multicast mode */
2430 macb_or_gem_writel(bp, HRB, 0);
2431 macb_or_gem_writel(bp, HRT, 0);
2432 cfg &= ~MACB_BIT(NCFGR_MTI);
2433 }
2434
2435 macb_writel(bp, NCFGR, cfg);
2436}
2437
2438static int macb_open(struct net_device *dev)
2439{
2440 struct macb *bp = netdev_priv(dev);
2441 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
2442 struct macb_queue *queue;
2443 unsigned int q;
2444 int err;
2445
2446 netdev_dbg(bp->dev, "open\n");
2447
2448 err = pm_runtime_get_sync(&bp->pdev->dev);
2449 if (err < 0)
2450 goto pm_exit;
2451
2452 /* carrier starts down */
2453 netif_carrier_off(dev);
2454
2455 /* if the phy is not yet register, retry later*/
2456 if (!dev->phydev) {
2457 err = -EAGAIN;
2458 goto pm_exit;
2459 }
2460
2461 /* RX buffers initialization */
2462 macb_init_rx_buffer_size(bp, bufsz);
2463
2464 err = macb_alloc_consistent(bp);
2465 if (err) {
2466 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2467 err);
2468 goto pm_exit;
2469 }
2470
2471 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2472 napi_enable(&queue->napi);
2473
2474 bp->macbgem_ops.mog_init_rings(bp);
2475 macb_init_hw(bp);
2476
2477 /* schedule a link state check */
2478 phy_start(dev->phydev);
2479
2480 netif_tx_start_all_queues(dev);
2481
2482 if (bp->ptp_info)
2483 bp->ptp_info->ptp_init(dev);
2484
2485pm_exit:
2486 if (err) {
2487 pm_runtime_put_sync(&bp->pdev->dev);
2488 return err;
2489 }
2490 return 0;
2491}
2492
2493static int macb_close(struct net_device *dev)
2494{
2495 struct macb *bp = netdev_priv(dev);
2496 struct macb_queue *queue;
2497 unsigned long flags;
2498 unsigned int q;
2499
2500 netif_tx_stop_all_queues(dev);
2501
2502 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2503 napi_disable(&queue->napi);
2504
2505 if (dev->phydev)
2506 phy_stop(dev->phydev);
2507
2508 spin_lock_irqsave(&bp->lock, flags);
2509 macb_reset_hw(bp);
2510 netif_carrier_off(dev);
2511 spin_unlock_irqrestore(&bp->lock, flags);
2512
2513 macb_free_consistent(bp);
2514
2515 if (bp->ptp_info)
2516 bp->ptp_info->ptp_remove(dev);
2517
2518 pm_runtime_put(&bp->pdev->dev);
2519
2520 return 0;
2521}
2522
2523static int macb_change_mtu(struct net_device *dev, int new_mtu)
2524{
2525 if (netif_running(dev))
2526 return -EBUSY;
2527
2528 dev->mtu = new_mtu;
2529
2530 return 0;
2531}
2532
2533static void gem_update_stats(struct macb *bp)
2534{
2535 struct macb_queue *queue;
2536 unsigned int i, q, idx;
2537 unsigned long *stat;
2538
2539 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2540
2541 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2542 u32 offset = gem_statistics[i].offset;
2543 u64 val = bp->macb_reg_readl(bp, offset);
2544
2545 bp->ethtool_stats[i] += val;
2546 *p += val;
2547
2548 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2549 /* Add GEM_OCTTXH, GEM_OCTRXH */
2550 val = bp->macb_reg_readl(bp, offset + 4);
2551 bp->ethtool_stats[i] += ((u64)val) << 32;
2552 *(++p) += val;
2553 }
2554 }
2555
2556 idx = GEM_STATS_LEN;
2557 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2558 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2559 bp->ethtool_stats[idx++] = *stat;
2560}
2561
2562static struct net_device_stats *gem_get_stats(struct macb *bp)
2563{
2564 struct gem_stats *hwstat = &bp->hw_stats.gem;
2565 struct net_device_stats *nstat = &bp->dev->stats;
2566
2567 if (!netif_running(bp->dev))
2568 return nstat;
2569
2570 gem_update_stats(bp);
2571
2572 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2573 hwstat->rx_alignment_errors +
2574 hwstat->rx_resource_errors +
2575 hwstat->rx_overruns +
2576 hwstat->rx_oversize_frames +
2577 hwstat->rx_jabbers +
2578 hwstat->rx_undersized_frames +
2579 hwstat->rx_length_field_frame_errors);
2580 nstat->tx_errors = (hwstat->tx_late_collisions +
2581 hwstat->tx_excessive_collisions +
2582 hwstat->tx_underrun +
2583 hwstat->tx_carrier_sense_errors);
2584 nstat->multicast = hwstat->rx_multicast_frames;
2585 nstat->collisions = (hwstat->tx_single_collision_frames +
2586 hwstat->tx_multiple_collision_frames +
2587 hwstat->tx_excessive_collisions);
2588 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2589 hwstat->rx_jabbers +
2590 hwstat->rx_undersized_frames +
2591 hwstat->rx_length_field_frame_errors);
2592 nstat->rx_over_errors = hwstat->rx_resource_errors;
2593 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2594 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2595 nstat->rx_fifo_errors = hwstat->rx_overruns;
2596 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2597 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2598 nstat->tx_fifo_errors = hwstat->tx_underrun;
2599
2600 return nstat;
2601}
2602
2603static void gem_get_ethtool_stats(struct net_device *dev,
2604 struct ethtool_stats *stats, u64 *data)
2605{
2606 struct macb *bp;
2607
2608 bp = netdev_priv(dev);
2609 gem_update_stats(bp);
2610 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2611 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
2612}
2613
2614static int gem_get_sset_count(struct net_device *dev, int sset)
2615{
2616 struct macb *bp = netdev_priv(dev);
2617
2618 switch (sset) {
2619 case ETH_SS_STATS:
2620 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
2621 default:
2622 return -EOPNOTSUPP;
2623 }
2624}
2625
2626static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2627{
2628 char stat_string[ETH_GSTRING_LEN];
2629 struct macb *bp = netdev_priv(dev);
2630 struct macb_queue *queue;
2631 unsigned int i;
2632 unsigned int q;
2633
2634 switch (sset) {
2635 case ETH_SS_STATS:
2636 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2637 memcpy(p, gem_statistics[i].stat_string,
2638 ETH_GSTRING_LEN);
2639
2640 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2641 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2642 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2643 q, queue_statistics[i].stat_string);
2644 memcpy(p, stat_string, ETH_GSTRING_LEN);
2645 }
2646 }
2647 break;
2648 }
2649}
2650
2651static struct net_device_stats *macb_get_stats(struct net_device *dev)
2652{
2653 struct macb *bp = netdev_priv(dev);
2654 struct net_device_stats *nstat = &bp->dev->stats;
2655 struct macb_stats *hwstat = &bp->hw_stats.macb;
2656
2657 if (macb_is_gem(bp))
2658 return gem_get_stats(bp);
2659
2660 /* read stats from hardware */
2661 macb_update_stats(bp);
2662
2663 /* Convert HW stats into netdevice stats */
2664 nstat->rx_errors = (hwstat->rx_fcs_errors +
2665 hwstat->rx_align_errors +
2666 hwstat->rx_resource_errors +
2667 hwstat->rx_overruns +
2668 hwstat->rx_oversize_pkts +
2669 hwstat->rx_jabbers +
2670 hwstat->rx_undersize_pkts +
2671 hwstat->rx_length_mismatch);
2672 nstat->tx_errors = (hwstat->tx_late_cols +
2673 hwstat->tx_excessive_cols +
2674 hwstat->tx_underruns +
2675 hwstat->tx_carrier_errors +
2676 hwstat->sqe_test_errors);
2677 nstat->collisions = (hwstat->tx_single_cols +
2678 hwstat->tx_multiple_cols +
2679 hwstat->tx_excessive_cols);
2680 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2681 hwstat->rx_jabbers +
2682 hwstat->rx_undersize_pkts +
2683 hwstat->rx_length_mismatch);
2684 nstat->rx_over_errors = hwstat->rx_resource_errors +
2685 hwstat->rx_overruns;
2686 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2687 nstat->rx_frame_errors = hwstat->rx_align_errors;
2688 nstat->rx_fifo_errors = hwstat->rx_overruns;
2689 /* XXX: What does "missed" mean? */
2690 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2691 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2692 nstat->tx_fifo_errors = hwstat->tx_underruns;
2693 /* Don't know about heartbeat or window errors... */
2694
2695 return nstat;
2696}
2697
2698static int macb_get_regs_len(struct net_device *netdev)
2699{
2700 return MACB_GREGS_NBR * sizeof(u32);
2701}
2702
2703static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2704 void *p)
2705{
2706 struct macb *bp = netdev_priv(dev);
2707 unsigned int tail, head;
2708 u32 *regs_buff = p;
2709
2710 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2711 | MACB_GREGS_VERSION;
2712
2713 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2714 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
2715
2716 regs_buff[0] = macb_readl(bp, NCR);
2717 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2718 regs_buff[2] = macb_readl(bp, NSR);
2719 regs_buff[3] = macb_readl(bp, TSR);
2720 regs_buff[4] = macb_readl(bp, RBQP);
2721 regs_buff[5] = macb_readl(bp, TBQP);
2722 regs_buff[6] = macb_readl(bp, RSR);
2723 regs_buff[7] = macb_readl(bp, IMR);
2724
2725 regs_buff[8] = tail;
2726 regs_buff[9] = head;
2727 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2728 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
2729
2730 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2731 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
2732 if (macb_is_gem(bp))
2733 regs_buff[13] = gem_readl(bp, DMACFG);
2734}
2735
2736static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2737{
2738 struct macb *bp = netdev_priv(netdev);
2739
2740 wol->supported = 0;
2741 wol->wolopts = 0;
2742
2743 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2744 wol->supported = WAKE_MAGIC;
2745
2746 if (bp->wol & MACB_WOL_ENABLED)
2747 wol->wolopts |= WAKE_MAGIC;
2748 }
2749}
2750
2751static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2752{
2753 struct macb *bp = netdev_priv(netdev);
2754
2755 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2756 (wol->wolopts & ~WAKE_MAGIC))
2757 return -EOPNOTSUPP;
2758
2759 if (wol->wolopts & WAKE_MAGIC)
2760 bp->wol |= MACB_WOL_ENABLED;
2761 else
2762 bp->wol &= ~MACB_WOL_ENABLED;
2763
2764 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2765
2766 return 0;
2767}
2768
2769static void macb_get_ringparam(struct net_device *netdev,
2770 struct ethtool_ringparam *ring)
2771{
2772 struct macb *bp = netdev_priv(netdev);
2773
2774 ring->rx_max_pending = MAX_RX_RING_SIZE;
2775 ring->tx_max_pending = MAX_TX_RING_SIZE;
2776
2777 ring->rx_pending = bp->rx_ring_size;
2778 ring->tx_pending = bp->tx_ring_size;
2779}
2780
2781static int macb_set_ringparam(struct net_device *netdev,
2782 struct ethtool_ringparam *ring)
2783{
2784 struct macb *bp = netdev_priv(netdev);
2785 u32 new_rx_size, new_tx_size;
2786 unsigned int reset = 0;
2787
2788 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2789 return -EINVAL;
2790
2791 new_rx_size = clamp_t(u32, ring->rx_pending,
2792 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2793 new_rx_size = roundup_pow_of_two(new_rx_size);
2794
2795 new_tx_size = clamp_t(u32, ring->tx_pending,
2796 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2797 new_tx_size = roundup_pow_of_two(new_tx_size);
2798
2799 if ((new_tx_size == bp->tx_ring_size) &&
2800 (new_rx_size == bp->rx_ring_size)) {
2801 /* nothing to do */
2802 return 0;
2803 }
2804
2805 if (netif_running(bp->dev)) {
2806 reset = 1;
2807 macb_close(bp->dev);
2808 }
2809
2810 bp->rx_ring_size = new_rx_size;
2811 bp->tx_ring_size = new_tx_size;
2812
2813 if (reset)
2814 macb_open(bp->dev);
2815
2816 return 0;
2817}
2818
2819#ifdef CONFIG_MACB_USE_HWSTAMP
2820static unsigned int gem_get_tsu_rate(struct macb *bp)
2821{
2822 struct clk *tsu_clk;
2823 unsigned int tsu_rate;
2824
2825 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2826 if (!IS_ERR(tsu_clk))
2827 tsu_rate = clk_get_rate(tsu_clk);
2828 /* try pclk instead */
2829 else if (!IS_ERR(bp->pclk)) {
2830 tsu_clk = bp->pclk;
2831 tsu_rate = clk_get_rate(tsu_clk);
2832 } else
2833 return -ENOTSUPP;
2834 return tsu_rate;
2835}
2836
2837static s32 gem_get_ptp_max_adj(void)
2838{
2839 return 64000000;
2840}
2841
2842static int gem_get_ts_info(struct net_device *dev,
2843 struct ethtool_ts_info *info)
2844{
2845 struct macb *bp = netdev_priv(dev);
2846
2847 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2848 ethtool_op_get_ts_info(dev, info);
2849 return 0;
2850 }
2851
2852 info->so_timestamping =
2853 SOF_TIMESTAMPING_TX_SOFTWARE |
2854 SOF_TIMESTAMPING_RX_SOFTWARE |
2855 SOF_TIMESTAMPING_SOFTWARE |
2856 SOF_TIMESTAMPING_TX_HARDWARE |
2857 SOF_TIMESTAMPING_RX_HARDWARE |
2858 SOF_TIMESTAMPING_RAW_HARDWARE;
2859 info->tx_types =
2860 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2861 (1 << HWTSTAMP_TX_OFF) |
2862 (1 << HWTSTAMP_TX_ON);
2863 info->rx_filters =
2864 (1 << HWTSTAMP_FILTER_NONE) |
2865 (1 << HWTSTAMP_FILTER_ALL);
2866
2867 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2868
2869 return 0;
2870}
2871
2872static struct macb_ptp_info gem_ptp_info = {
2873 .ptp_init = gem_ptp_init,
2874 .ptp_remove = gem_ptp_remove,
2875 .get_ptp_max_adj = gem_get_ptp_max_adj,
2876 .get_tsu_rate = gem_get_tsu_rate,
2877 .get_ts_info = gem_get_ts_info,
2878 .get_hwtst = gem_get_hwtst,
2879 .set_hwtst = gem_set_hwtst,
2880};
2881#endif
2882
2883static int macb_get_ts_info(struct net_device *netdev,
2884 struct ethtool_ts_info *info)
2885{
2886 struct macb *bp = netdev_priv(netdev);
2887
2888 if (bp->ptp_info)
2889 return bp->ptp_info->get_ts_info(netdev, info);
2890
2891 return ethtool_op_get_ts_info(netdev, info);
2892}
2893
2894static void gem_enable_flow_filters(struct macb *bp, bool enable)
2895{
2896 struct net_device *netdev = bp->dev;
2897 struct ethtool_rx_fs_item *item;
2898 u32 t2_scr;
2899 int num_t2_scr;
2900
2901 if (!(netdev->features & NETIF_F_NTUPLE))
2902 return;
2903
2904 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2905
2906 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2907 struct ethtool_rx_flow_spec *fs = &item->fs;
2908 struct ethtool_tcpip4_spec *tp4sp_m;
2909
2910 if (fs->location >= num_t2_scr)
2911 continue;
2912
2913 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2914
2915 /* enable/disable screener regs for the flow entry */
2916 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2917
2918 /* only enable fields with no masking */
2919 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2920
2921 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2922 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2923 else
2924 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2925
2926 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2927 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2928 else
2929 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2930
2931 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2932 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2933 else
2934 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2935
2936 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2937 }
2938}
2939
2940static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2941{
2942 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2943 uint16_t index = fs->location;
2944 u32 w0, w1, t2_scr;
2945 bool cmp_a = false;
2946 bool cmp_b = false;
2947 bool cmp_c = false;
2948
2949 if (!macb_is_gem(bp))
2950 return;
2951
2952 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2953 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2954
2955 /* ignore field if any masking set */
2956 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2957 /* 1st compare reg - IP source address */
2958 w0 = 0;
2959 w1 = 0;
2960 w0 = tp4sp_v->ip4src;
2961 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2962 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2963 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2964 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2965 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2966 cmp_a = true;
2967 }
2968
2969 /* ignore field if any masking set */
2970 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2971 /* 2nd compare reg - IP destination address */
2972 w0 = 0;
2973 w1 = 0;
2974 w0 = tp4sp_v->ip4dst;
2975 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2976 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2977 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2978 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2979 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2980 cmp_b = true;
2981 }
2982
2983 /* ignore both port fields if masking set in both */
2984 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2985 /* 3rd compare reg - source port, destination port */
2986 w0 = 0;
2987 w1 = 0;
2988 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2989 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2990 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2991 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2992 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2993 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2994 } else {
2995 /* only one port definition */
2996 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2997 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2998 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2999 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
3000 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
3001 } else { /* dst port */
3002 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
3003 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
3004 }
3005 }
3006 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
3007 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
3008 cmp_c = true;
3009 }
3010
3011 t2_scr = 0;
3012 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
3013 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
3014 if (cmp_a)
3015 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
3016 if (cmp_b)
3017 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3018 if (cmp_c)
3019 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3020 gem_writel_n(bp, SCRT2, index, t2_scr);
3021}
3022
3023static int gem_add_flow_filter(struct net_device *netdev,
3024 struct ethtool_rxnfc *cmd)
3025{
3026 struct macb *bp = netdev_priv(netdev);
3027 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3028 struct ethtool_rx_fs_item *item, *newfs;
3029 unsigned long flags;
3030 int ret = -EINVAL;
3031 bool added = false;
3032
3033 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
3034 if (newfs == NULL)
3035 return -ENOMEM;
3036 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3037
3038 netdev_dbg(netdev,
3039 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3040 fs->flow_type, (int)fs->ring_cookie, fs->location,
3041 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3042 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3043 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3044
3045 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3046
3047 /* find correct place to add in list */
3048 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3049 if (item->fs.location > newfs->fs.location) {
3050 list_add_tail(&newfs->list, &item->list);
3051 added = true;
3052 break;
3053 } else if (item->fs.location == fs->location) {
3054 netdev_err(netdev, "Rule not added: location %d not free!\n",
3055 fs->location);
3056 ret = -EBUSY;
3057 goto err;
3058 }
3059 }
3060 if (!added)
3061 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
3062
3063 gem_prog_cmp_regs(bp, fs);
3064 bp->rx_fs_list.count++;
3065 /* enable filtering if NTUPLE on */
3066 gem_enable_flow_filters(bp, 1);
3067
3068 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3069 return 0;
3070
3071err:
3072 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3073 kfree(newfs);
3074 return ret;
3075}
3076
3077static int gem_del_flow_filter(struct net_device *netdev,
3078 struct ethtool_rxnfc *cmd)
3079{
3080 struct macb *bp = netdev_priv(netdev);
3081 struct ethtool_rx_fs_item *item;
3082 struct ethtool_rx_flow_spec *fs;
3083 unsigned long flags;
3084
3085 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3086
3087 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3088 if (item->fs.location == cmd->fs.location) {
3089 /* disable screener regs for the flow entry */
3090 fs = &(item->fs);
3091 netdev_dbg(netdev,
3092 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3093 fs->flow_type, (int)fs->ring_cookie, fs->location,
3094 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3095 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3096 htons(fs->h_u.tcp_ip4_spec.psrc),
3097 htons(fs->h_u.tcp_ip4_spec.pdst));
3098
3099 gem_writel_n(bp, SCRT2, fs->location, 0);
3100
3101 list_del(&item->list);
3102 bp->rx_fs_list.count--;
3103 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3104 kfree(item);
3105 return 0;
3106 }
3107 }
3108
3109 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3110 return -EINVAL;
3111}
3112
3113static int gem_get_flow_entry(struct net_device *netdev,
3114 struct ethtool_rxnfc *cmd)
3115{
3116 struct macb *bp = netdev_priv(netdev);
3117 struct ethtool_rx_fs_item *item;
3118
3119 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3120 if (item->fs.location == cmd->fs.location) {
3121 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3122 return 0;
3123 }
3124 }
3125 return -EINVAL;
3126}
3127
3128static int gem_get_all_flow_entries(struct net_device *netdev,
3129 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3130{
3131 struct macb *bp = netdev_priv(netdev);
3132 struct ethtool_rx_fs_item *item;
3133 uint32_t cnt = 0;
3134
3135 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3136 if (cnt == cmd->rule_cnt)
3137 return -EMSGSIZE;
3138 rule_locs[cnt] = item->fs.location;
3139 cnt++;
3140 }
3141 cmd->data = bp->max_tuples;
3142 cmd->rule_cnt = cnt;
3143
3144 return 0;
3145}
3146
3147static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3148 u32 *rule_locs)
3149{
3150 struct macb *bp = netdev_priv(netdev);
3151 int ret = 0;
3152
3153 switch (cmd->cmd) {
3154 case ETHTOOL_GRXRINGS:
3155 cmd->data = bp->num_queues;
3156 break;
3157 case ETHTOOL_GRXCLSRLCNT:
3158 cmd->rule_cnt = bp->rx_fs_list.count;
3159 break;
3160 case ETHTOOL_GRXCLSRULE:
3161 ret = gem_get_flow_entry(netdev, cmd);
3162 break;
3163 case ETHTOOL_GRXCLSRLALL:
3164 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3165 break;
3166 default:
3167 netdev_err(netdev,
3168 "Command parameter %d is not supported\n", cmd->cmd);
3169 ret = -EOPNOTSUPP;
3170 }
3171
3172 return ret;
3173}
3174
3175static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3176{
3177 struct macb *bp = netdev_priv(netdev);
3178 int ret;
3179
3180 switch (cmd->cmd) {
3181 case ETHTOOL_SRXCLSRLINS:
3182 if ((cmd->fs.location >= bp->max_tuples)
3183 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3184 ret = -EINVAL;
3185 break;
3186 }
3187 ret = gem_add_flow_filter(netdev, cmd);
3188 break;
3189 case ETHTOOL_SRXCLSRLDEL:
3190 ret = gem_del_flow_filter(netdev, cmd);
3191 break;
3192 default:
3193 netdev_err(netdev,
3194 "Command parameter %d is not supported\n", cmd->cmd);
3195 ret = -EOPNOTSUPP;
3196 }
3197
3198 return ret;
3199}
3200
3201static const struct ethtool_ops macb_ethtool_ops = {
3202 .get_regs_len = macb_get_regs_len,
3203 .get_regs = macb_get_regs,
3204 .get_link = ethtool_op_get_link,
3205 .get_ts_info = ethtool_op_get_ts_info,
3206 .get_wol = macb_get_wol,
3207 .set_wol = macb_set_wol,
3208 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3209 .set_link_ksettings = phy_ethtool_set_link_ksettings,
3210 .get_ringparam = macb_get_ringparam,
3211 .set_ringparam = macb_set_ringparam,
3212};
3213
3214static const struct ethtool_ops gem_ethtool_ops = {
3215 .get_regs_len = macb_get_regs_len,
3216 .get_regs = macb_get_regs,
3217 .get_link = ethtool_op_get_link,
3218 .get_ts_info = macb_get_ts_info,
3219 .get_ethtool_stats = gem_get_ethtool_stats,
3220 .get_strings = gem_get_ethtool_strings,
3221 .get_sset_count = gem_get_sset_count,
3222 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3223 .set_link_ksettings = phy_ethtool_set_link_ksettings,
3224 .get_ringparam = macb_get_ringparam,
3225 .set_ringparam = macb_set_ringparam,
3226 .get_rxnfc = gem_get_rxnfc,
3227 .set_rxnfc = gem_set_rxnfc,
3228};
3229
3230static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3231{
3232 struct phy_device *phydev = dev->phydev;
3233 struct macb *bp = netdev_priv(dev);
3234
3235 if (!netif_running(dev))
3236 return -EINVAL;
3237
3238 if (!phydev)
3239 return -ENODEV;
3240
3241 if (!bp->ptp_info)
3242 return phy_mii_ioctl(phydev, rq, cmd);
3243
3244 switch (cmd) {
3245 case SIOCSHWTSTAMP:
3246 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3247 case SIOCGHWTSTAMP:
3248 return bp->ptp_info->get_hwtst(dev, rq);
3249 default:
3250 return phy_mii_ioctl(phydev, rq, cmd);
3251 }
3252}
3253
3254static inline void macb_set_txcsum_feature(struct macb *bp,
3255 netdev_features_t features)
3256{
3257 u32 val;
3258
3259 if (!macb_is_gem(bp))
3260 return;
3261
3262 val = gem_readl(bp, DMACFG);
3263 if (features & NETIF_F_HW_CSUM)
3264 val |= GEM_BIT(TXCOEN);
3265 else
3266 val &= ~GEM_BIT(TXCOEN);
3267
3268 gem_writel(bp, DMACFG, val);
3269}
3270
3271static inline void macb_set_rxcsum_feature(struct macb *bp,
3272 netdev_features_t features)
3273{
3274 struct net_device *netdev = bp->dev;
3275 u32 val;
3276
3277 if (!macb_is_gem(bp))
3278 return;
3279
3280 val = gem_readl(bp, NCFGR);
3281 if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
3282 val |= GEM_BIT(RXCOEN);
3283 else
3284 val &= ~GEM_BIT(RXCOEN);
3285
3286 gem_writel(bp, NCFGR, val);
3287}
3288
3289static inline void macb_set_rxflow_feature(struct macb *bp,
3290 netdev_features_t features)
3291{
3292 if (!macb_is_gem(bp))
3293 return;
3294
3295 gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
3296}
3297
3298static int macb_set_features(struct net_device *netdev,
3299 netdev_features_t features)
3300{
3301 struct macb *bp = netdev_priv(netdev);
3302 netdev_features_t changed = features ^ netdev->features;
3303
3304 /* TX checksum offload */
3305 if (changed & NETIF_F_HW_CSUM)
3306 macb_set_txcsum_feature(bp, features);
3307
3308 /* RX checksum offload */
3309 if (changed & NETIF_F_RXCSUM)
3310 macb_set_rxcsum_feature(bp, features);
3311
3312 /* RX Flow Filters */
3313 if (changed & NETIF_F_NTUPLE)
3314 macb_set_rxflow_feature(bp, features);
3315
3316 return 0;
3317}
3318
3319static void macb_restore_features(struct macb *bp)
3320{
3321 struct net_device *netdev = bp->dev;
3322 netdev_features_t features = netdev->features;
3323 struct ethtool_rx_fs_item *item;
3324
3325 /* TX checksum offload */
3326 macb_set_txcsum_feature(bp, features);
3327
3328 /* RX checksum offload */
3329 macb_set_rxcsum_feature(bp, features);
3330
3331 /* RX Flow Filters */
3332 list_for_each_entry(item, &bp->rx_fs_list.list, list)
3333 gem_prog_cmp_regs(bp, &item->fs);
3334
3335 macb_set_rxflow_feature(bp, features);
3336}
3337
3338static const struct net_device_ops macb_netdev_ops = {
3339 .ndo_open = macb_open,
3340 .ndo_stop = macb_close,
3341 .ndo_start_xmit = macb_start_xmit,
3342 .ndo_set_rx_mode = macb_set_rx_mode,
3343 .ndo_get_stats = macb_get_stats,
3344 .ndo_do_ioctl = macb_ioctl,
3345 .ndo_validate_addr = eth_validate_addr,
3346 .ndo_change_mtu = macb_change_mtu,
3347 .ndo_set_mac_address = eth_mac_addr,
3348#ifdef CONFIG_NET_POLL_CONTROLLER
3349 .ndo_poll_controller = macb_poll_controller,
3350#endif
3351 .ndo_set_features = macb_set_features,
3352 .ndo_features_check = macb_features_check,
3353};
3354
3355/* Configure peripheral capabilities according to device tree
3356 * and integration options used
3357 */
3358static void macb_configure_caps(struct macb *bp,
3359 const struct macb_config *dt_conf)
3360{
3361 u32 dcfg;
3362
3363 if (dt_conf)
3364 bp->caps = dt_conf->caps;
3365
3366 if (hw_is_gem(bp->regs, bp->native_io)) {
3367 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3368
3369 dcfg = gem_readl(bp, DCFG1);
3370 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3371 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3372 dcfg = gem_readl(bp, DCFG2);
3373 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3374 bp->caps |= MACB_CAPS_FIFO_MODE;
3375#ifdef CONFIG_MACB_USE_HWSTAMP
3376 if (gem_has_ptp(bp)) {
3377 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3378 pr_err("GEM doesn't support hardware ptp.\n");
3379 else {
3380 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
3381 bp->ptp_info = &gem_ptp_info;
3382 }
3383 }
3384#endif
3385 }
3386
3387 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
3388}
3389
3390static void macb_probe_queues(void __iomem *mem,
3391 bool native_io,
3392 unsigned int *queue_mask,
3393 unsigned int *num_queues)
3394{
3395 unsigned int hw_q;
3396
3397 *queue_mask = 0x1;
3398 *num_queues = 1;
3399
3400 /* is it macb or gem ?
3401 *
3402 * We need to read directly from the hardware here because
3403 * we are early in the probe process and don't have the
3404 * MACB_CAPS_MACB_IS_GEM flag positioned
3405 */
3406 if (!hw_is_gem(mem, native_io))
3407 return;
3408
3409 /* bit 0 is never set but queue 0 always exists */
3410 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3411
3412 *queue_mask |= 0x1;
3413
3414 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3415 if (*queue_mask & (1 << hw_q))
3416 (*num_queues)++;
3417}
3418
3419static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
3420 struct clk **hclk, struct clk **tx_clk,
3421 struct clk **rx_clk, struct clk **tsu_clk)
3422{
3423 struct macb_platform_data *pdata;
3424 int err;
3425
3426 pdata = dev_get_platdata(&pdev->dev);
3427 if (pdata) {
3428 *pclk = pdata->pclk;
3429 *hclk = pdata->hclk;
3430 } else {
3431 *pclk = devm_clk_get(&pdev->dev, "pclk");
3432 *hclk = devm_clk_get(&pdev->dev, "hclk");
3433 }
3434
3435 if (IS_ERR_OR_NULL(*pclk)) {
3436 err = PTR_ERR(*pclk);
3437 if (!err)
3438 err = -ENODEV;
3439
3440 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
3441 return err;
3442 }
3443
3444 if (IS_ERR_OR_NULL(*hclk)) {
3445 err = PTR_ERR(*hclk);
3446 if (!err)
3447 err = -ENODEV;
3448
3449 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
3450 return err;
3451 }
3452
3453 *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
3454 if (IS_ERR(*tx_clk))
3455 return PTR_ERR(*tx_clk);
3456
3457 *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
3458 if (IS_ERR(*rx_clk))
3459 return PTR_ERR(*rx_clk);
3460
3461 *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
3462 if (IS_ERR(*tsu_clk))
3463 return PTR_ERR(*tsu_clk);
3464
3465 err = clk_prepare_enable(*pclk);
3466 if (err) {
3467 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
3468 return err;
3469 }
3470
3471 err = clk_prepare_enable(*hclk);
3472 if (err) {
3473 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
3474 goto err_disable_pclk;
3475 }
3476
3477 err = clk_prepare_enable(*tx_clk);
3478 if (err) {
3479 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
3480 goto err_disable_hclk;
3481 }
3482
3483 err = clk_prepare_enable(*rx_clk);
3484 if (err) {
3485 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
3486 goto err_disable_txclk;
3487 }
3488
3489 err = clk_prepare_enable(*tsu_clk);
3490 if (err) {
3491 dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
3492 goto err_disable_rxclk;
3493 }
3494
3495 return 0;
3496
3497err_disable_rxclk:
3498 clk_disable_unprepare(*rx_clk);
3499
3500err_disable_txclk:
3501 clk_disable_unprepare(*tx_clk);
3502
3503err_disable_hclk:
3504 clk_disable_unprepare(*hclk);
3505
3506err_disable_pclk:
3507 clk_disable_unprepare(*pclk);
3508
3509 return err;
3510}
3511
3512static int macb_init(struct platform_device *pdev)
3513{
3514 struct net_device *dev = platform_get_drvdata(pdev);
3515 unsigned int hw_q, q;
3516 struct macb *bp = netdev_priv(dev);
3517 struct macb_queue *queue;
3518 int err;
3519 u32 val, reg;
3520
3521 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3522 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3523
3524 /* set the queue register mapping once for all: queue0 has a special
3525 * register mapping but we don't want to test the queue index then
3526 * compute the corresponding register offset at run time.
3527 */
3528 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
3529 if (!(bp->queue_mask & (1 << hw_q)))
3530 continue;
3531
3532 queue = &bp->queues[q];
3533 queue->bp = bp;
3534 netif_napi_add(dev, &queue->napi, macb_poll, NAPI_POLL_WEIGHT);
3535 if (hw_q) {
3536 queue->ISR = GEM_ISR(hw_q - 1);
3537 queue->IER = GEM_IER(hw_q - 1);
3538 queue->IDR = GEM_IDR(hw_q - 1);
3539 queue->IMR = GEM_IMR(hw_q - 1);
3540 queue->TBQP = GEM_TBQP(hw_q - 1);
3541 queue->RBQP = GEM_RBQP(hw_q - 1);
3542 queue->RBQS = GEM_RBQS(hw_q - 1);
3543#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3544 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3545 queue->TBQPH = GEM_TBQPH(hw_q - 1);
3546 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3547 }
3548#endif
3549 } else {
3550 /* queue0 uses legacy registers */
3551 queue->ISR = MACB_ISR;
3552 queue->IER = MACB_IER;
3553 queue->IDR = MACB_IDR;
3554 queue->IMR = MACB_IMR;
3555 queue->TBQP = MACB_TBQP;
3556 queue->RBQP = MACB_RBQP;
3557#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3558 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3559 queue->TBQPH = MACB_TBQPH;
3560 queue->RBQPH = MACB_RBQPH;
3561 }
3562#endif
3563 }
3564
3565 /* get irq: here we use the linux queue index, not the hardware
3566 * queue index. the queue irq definitions in the device tree
3567 * must remove the optional gaps that could exist in the
3568 * hardware queue mask.
3569 */
3570 queue->irq = platform_get_irq(pdev, q);
3571 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
3572 IRQF_SHARED, dev->name, queue);
3573 if (err) {
3574 dev_err(&pdev->dev,
3575 "Unable to request IRQ %d (error %d)\n",
3576 queue->irq, err);
3577 return err;
3578 }
3579
3580 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
3581 q++;
3582 }
3583
3584 dev->netdev_ops = &macb_netdev_ops;
3585
3586 /* setup appropriated routines according to adapter type */
3587 if (macb_is_gem(bp)) {
3588 bp->max_tx_length = GEM_MAX_TX_LEN;
3589 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3590 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3591 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3592 bp->macbgem_ops.mog_rx = gem_rx;
3593 dev->ethtool_ops = &gem_ethtool_ops;
3594 } else {
3595 bp->max_tx_length = MACB_MAX_TX_LEN;
3596 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3597 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3598 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3599 bp->macbgem_ops.mog_rx = macb_rx;
3600 dev->ethtool_ops = &macb_ethtool_ops;
3601 }
3602
3603 /* Set features */
3604 dev->hw_features = NETIF_F_SG;
3605
3606 /* Check LSO capability */
3607 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3608 dev->hw_features |= MACB_NETIF_LSO;
3609
3610 /* Checksum offload is only available on gem with packet buffer */
3611 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
3612 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3613 if (bp->caps & MACB_CAPS_SG_DISABLED)
3614 dev->hw_features &= ~NETIF_F_SG;
3615 dev->features = dev->hw_features;
3616
3617 /* Check RX Flow Filters support.
3618 * Max Rx flows set by availability of screeners & compare regs:
3619 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3620 */
3621 reg = gem_readl(bp, DCFG8);
3622 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3623 GEM_BFEXT(T2SCR, reg));
3624 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3625 if (bp->max_tuples > 0) {
3626 /* also needs one ethtype match to check IPv4 */
3627 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3628 /* program this reg now */
3629 reg = 0;
3630 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3631 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3632 /* Filtering is supported in hw but don't enable it in kernel now */
3633 dev->hw_features |= NETIF_F_NTUPLE;
3634 /* init Rx flow definitions */
3635 bp->rx_fs_list.count = 0;
3636 spin_lock_init(&bp->rx_fs_lock);
3637 } else
3638 bp->max_tuples = 0;
3639 }
3640
3641 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3642 val = 0;
3643 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3644 val = GEM_BIT(RGMII);
3645 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
3646 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3647 val = MACB_BIT(RMII);
3648 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3649 val = MACB_BIT(MII);
3650
3651 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3652 val |= MACB_BIT(CLKEN);
3653
3654 macb_or_gem_writel(bp, USRIO, val);
3655 }
3656
3657 /* Set MII management clock divider */
3658 val = macb_mdc_clk_div(bp);
3659 val |= macb_dbw(bp);
3660 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3661 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
3662 macb_writel(bp, NCFGR, val);
3663
3664 return 0;
3665}
3666
3667#if defined(CONFIG_OF)
3668/* 1518 rounded up */
3669#define AT91ETHER_MAX_RBUFF_SZ 0x600
3670/* max number of receive buffers */
3671#define AT91ETHER_MAX_RX_DESCR 9
3672
3673static struct sifive_fu540_macb_mgmt *mgmt;
3674
3675/* Initialize and start the Receiver and Transmit subsystems */
3676static int at91ether_start(struct net_device *dev)
3677{
3678 struct macb *lp = netdev_priv(dev);
3679 struct macb_queue *q = &lp->queues[0];
3680 struct macb_dma_desc *desc;
3681 dma_addr_t addr;
3682 u32 ctl;
3683 int i;
3684
3685 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
3686 (AT91ETHER_MAX_RX_DESCR *
3687 macb_dma_desc_get_size(lp)),
3688 &q->rx_ring_dma, GFP_KERNEL);
3689 if (!q->rx_ring)
3690 return -ENOMEM;
3691
3692 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
3693 AT91ETHER_MAX_RX_DESCR *
3694 AT91ETHER_MAX_RBUFF_SZ,
3695 &q->rx_buffers_dma, GFP_KERNEL);
3696 if (!q->rx_buffers) {
3697 dma_free_coherent(&lp->pdev->dev,
3698 AT91ETHER_MAX_RX_DESCR *
3699 macb_dma_desc_get_size(lp),
3700 q->rx_ring, q->rx_ring_dma);
3701 q->rx_ring = NULL;
3702 return -ENOMEM;
3703 }
3704
3705 addr = q->rx_buffers_dma;
3706 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
3707 desc = macb_rx_desc(q, i);
3708 macb_set_addr(lp, desc, addr);
3709 desc->ctrl = 0;
3710 addr += AT91ETHER_MAX_RBUFF_SZ;
3711 }
3712
3713 /* Set the Wrap bit on the last descriptor */
3714 desc->addr |= MACB_BIT(RX_WRAP);
3715
3716 /* Reset buffer index */
3717 q->rx_tail = 0;
3718
3719 /* Program address of descriptor list in Rx Buffer Queue register */
3720 macb_writel(lp, RBQP, q->rx_ring_dma);
3721
3722 /* Enable Receive and Transmit */
3723 ctl = macb_readl(lp, NCR);
3724 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3725
3726 return 0;
3727}
3728
3729/* Open the ethernet interface */
3730static int at91ether_open(struct net_device *dev)
3731{
3732 struct macb *lp = netdev_priv(dev);
3733 u32 ctl;
3734 int ret;
3735
3736 ret = pm_runtime_get_sync(&lp->pdev->dev);
3737 if (ret < 0) {
3738 pm_runtime_put_noidle(&lp->pdev->dev);
3739 return ret;
3740 }
3741
3742 /* Clear internal statistics */
3743 ctl = macb_readl(lp, NCR);
3744 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3745
3746 macb_set_hwaddr(lp);
3747
3748 ret = at91ether_start(dev);
3749 if (ret)
3750 goto pm_exit;
3751
3752 /* Enable MAC interrupts */
3753 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3754 MACB_BIT(RXUBR) |
3755 MACB_BIT(ISR_TUND) |
3756 MACB_BIT(ISR_RLE) |
3757 MACB_BIT(TCOMP) |
3758 MACB_BIT(ISR_ROVR) |
3759 MACB_BIT(HRESP));
3760
3761 /* schedule a link state check */
3762 phy_start(dev->phydev);
3763
3764 netif_start_queue(dev);
3765
3766 return 0;
3767
3768pm_exit:
3769 pm_runtime_put_sync(&lp->pdev->dev);
3770 return ret;
3771}
3772
3773/* Close the interface */
3774static int at91ether_close(struct net_device *dev)
3775{
3776 struct macb *lp = netdev_priv(dev);
3777 struct macb_queue *q = &lp->queues[0];
3778 u32 ctl;
3779
3780 /* Disable Receiver and Transmitter */
3781 ctl = macb_readl(lp, NCR);
3782 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3783
3784 /* Disable MAC interrupts */
3785 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3786 MACB_BIT(RXUBR) |
3787 MACB_BIT(ISR_TUND) |
3788 MACB_BIT(ISR_RLE) |
3789 MACB_BIT(TCOMP) |
3790 MACB_BIT(ISR_ROVR) |
3791 MACB_BIT(HRESP));
3792
3793 netif_stop_queue(dev);
3794
3795 dma_free_coherent(&lp->pdev->dev,
3796 AT91ETHER_MAX_RX_DESCR *
3797 macb_dma_desc_get_size(lp),
3798 q->rx_ring, q->rx_ring_dma);
3799 q->rx_ring = NULL;
3800
3801 dma_free_coherent(&lp->pdev->dev,
3802 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3803 q->rx_buffers, q->rx_buffers_dma);
3804 q->rx_buffers = NULL;
3805
3806 return pm_runtime_put(&lp->pdev->dev);
3807}
3808
3809/* Transmit packet */
3810static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3811 struct net_device *dev)
3812{
3813 struct macb *lp = netdev_priv(dev);
3814
3815 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3816 netif_stop_queue(dev);
3817
3818 /* Store packet information (to free when Tx completed) */
3819 lp->skb = skb;
3820 lp->skb_length = skb->len;
3821 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3822 skb->len, DMA_TO_DEVICE);
3823 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
3824 dev_kfree_skb_any(skb);
3825 dev->stats.tx_dropped++;
3826 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3827 return NETDEV_TX_OK;
3828 }
3829
3830 /* Set address of the data in the Transmit Address register */
3831 macb_writel(lp, TAR, lp->skb_physaddr);
3832 /* Set length of the packet in the Transmit Control register */
3833 macb_writel(lp, TCR, skb->len);
3834
3835 } else {
3836 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3837 return NETDEV_TX_BUSY;
3838 }
3839
3840 return NETDEV_TX_OK;
3841}
3842
3843/* Extract received frame from buffer descriptors and sent to upper layers.
3844 * (Called from interrupt context)
3845 */
3846static void at91ether_rx(struct net_device *dev)
3847{
3848 struct macb *lp = netdev_priv(dev);
3849 struct macb_queue *q = &lp->queues[0];
3850 struct macb_dma_desc *desc;
3851 unsigned char *p_recv;
3852 struct sk_buff *skb;
3853 unsigned int pktlen;
3854
3855 desc = macb_rx_desc(q, q->rx_tail);
3856 while (desc->addr & MACB_BIT(RX_USED)) {
3857 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
3858 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
3859 skb = netdev_alloc_skb(dev, pktlen + 2);
3860 if (skb) {
3861 skb_reserve(skb, 2);
3862 skb_put_data(skb, p_recv, pktlen);
3863
3864 skb->protocol = eth_type_trans(skb, dev);
3865 dev->stats.rx_packets++;
3866 dev->stats.rx_bytes += pktlen;
3867 netif_rx(skb);
3868 } else {
3869 dev->stats.rx_dropped++;
3870 }
3871
3872 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
3873 dev->stats.multicast++;
3874
3875 /* reset ownership bit */
3876 desc->addr &= ~MACB_BIT(RX_USED);
3877
3878 /* wrap after last buffer */
3879 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3880 q->rx_tail = 0;
3881 else
3882 q->rx_tail++;
3883
3884 desc = macb_rx_desc(q, q->rx_tail);
3885 }
3886}
3887
3888/* MAC interrupt handler */
3889static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3890{
3891 struct net_device *dev = dev_id;
3892 struct macb *lp = netdev_priv(dev);
3893 u32 intstatus, ctl;
3894
3895 /* MAC Interrupt Status register indicates what interrupts are pending.
3896 * It is automatically cleared once read.
3897 */
3898 intstatus = macb_readl(lp, ISR);
3899
3900 /* Receive complete */
3901 if (intstatus & MACB_BIT(RCOMP))
3902 at91ether_rx(dev);
3903
3904 /* Transmit complete */
3905 if (intstatus & MACB_BIT(TCOMP)) {
3906 /* The TCOM bit is set even if the transmission failed */
3907 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
3908 dev->stats.tx_errors++;
3909
3910 if (lp->skb) {
3911 dev_consume_skb_irq(lp->skb);
3912 lp->skb = NULL;
3913 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
3914 lp->skb_length, DMA_TO_DEVICE);
3915 dev->stats.tx_packets++;
3916 dev->stats.tx_bytes += lp->skb_length;
3917 }
3918 netif_wake_queue(dev);
3919 }
3920
3921 /* Work-around for EMAC Errata section 41.3.1 */
3922 if (intstatus & MACB_BIT(RXUBR)) {
3923 ctl = macb_readl(lp, NCR);
3924 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
3925 wmb();
3926 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3927 }
3928
3929 if (intstatus & MACB_BIT(ISR_ROVR))
3930 netdev_err(dev, "ROVR error\n");
3931
3932 return IRQ_HANDLED;
3933}
3934
3935#ifdef CONFIG_NET_POLL_CONTROLLER
3936static void at91ether_poll_controller(struct net_device *dev)
3937{
3938 unsigned long flags;
3939
3940 local_irq_save(flags);
3941 at91ether_interrupt(dev->irq, dev);
3942 local_irq_restore(flags);
3943}
3944#endif
3945
3946static const struct net_device_ops at91ether_netdev_ops = {
3947 .ndo_open = at91ether_open,
3948 .ndo_stop = at91ether_close,
3949 .ndo_start_xmit = at91ether_start_xmit,
3950 .ndo_get_stats = macb_get_stats,
3951 .ndo_set_rx_mode = macb_set_rx_mode,
3952 .ndo_set_mac_address = eth_mac_addr,
3953 .ndo_do_ioctl = macb_ioctl,
3954 .ndo_validate_addr = eth_validate_addr,
3955#ifdef CONFIG_NET_POLL_CONTROLLER
3956 .ndo_poll_controller = at91ether_poll_controller,
3957#endif
3958};
3959
3960static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
3961 struct clk **hclk, struct clk **tx_clk,
3962 struct clk **rx_clk, struct clk **tsu_clk)
3963{
3964 int err;
3965
3966 *hclk = NULL;
3967 *tx_clk = NULL;
3968 *rx_clk = NULL;
3969 *tsu_clk = NULL;
3970
3971 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3972 if (IS_ERR(*pclk))
3973 return PTR_ERR(*pclk);
3974
3975 err = clk_prepare_enable(*pclk);
3976 if (err) {
3977 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
3978 return err;
3979 }
3980
3981 return 0;
3982}
3983
3984static int at91ether_init(struct platform_device *pdev)
3985{
3986 struct net_device *dev = platform_get_drvdata(pdev);
3987 struct macb *bp = netdev_priv(dev);
3988 int err;
3989 u32 reg;
3990
3991 bp->queues[0].bp = bp;
3992
3993 dev->netdev_ops = &at91ether_netdev_ops;
3994 dev->ethtool_ops = &macb_ethtool_ops;
3995
3996 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3997 0, dev->name, dev);
3998 if (err)
3999 return err;
4000
4001 macb_writel(bp, NCR, 0);
4002
4003 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
4004 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
4005 reg |= MACB_BIT(RM9200_RMII);
4006
4007 macb_writel(bp, NCFGR, reg);
4008
4009 return 0;
4010}
4011
4012static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
4013 unsigned long parent_rate)
4014{
4015 return mgmt->rate;
4016}
4017
4018static long fu540_macb_tx_round_rate(struct clk_hw *hw, unsigned long rate,
4019 unsigned long *parent_rate)
4020{
4021 if (WARN_ON(rate < 2500000))
4022 return 2500000;
4023 else if (rate == 2500000)
4024 return 2500000;
4025 else if (WARN_ON(rate < 13750000))
4026 return 2500000;
4027 else if (WARN_ON(rate < 25000000))
4028 return 25000000;
4029 else if (rate == 25000000)
4030 return 25000000;
4031 else if (WARN_ON(rate < 75000000))
4032 return 25000000;
4033 else if (WARN_ON(rate < 125000000))
4034 return 125000000;
4035 else if (rate == 125000000)
4036 return 125000000;
4037
4038 WARN_ON(rate > 125000000);
4039
4040 return 125000000;
4041}
4042
4043static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
4044 unsigned long parent_rate)
4045{
4046 rate = fu540_macb_tx_round_rate(hw, rate, &parent_rate);
4047 if (rate != 125000000)
4048 iowrite32(1, mgmt->reg);
4049 else
4050 iowrite32(0, mgmt->reg);
4051 mgmt->rate = rate;
4052
4053 return 0;
4054}
4055
4056static const struct clk_ops fu540_c000_ops = {
4057 .recalc_rate = fu540_macb_tx_recalc_rate,
4058 .round_rate = fu540_macb_tx_round_rate,
4059 .set_rate = fu540_macb_tx_set_rate,
4060};
4061
4062static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
4063 struct clk **hclk, struct clk **tx_clk,
4064 struct clk **rx_clk, struct clk **tsu_clk)
4065{
4066 struct clk_init_data init;
4067 int err = 0;
4068
4069 err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
4070 if (err)
4071 return err;
4072
4073 mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
4074 if (!mgmt)
4075 return -ENOMEM;
4076
4077 init.name = "sifive-gemgxl-mgmt";
4078 init.ops = &fu540_c000_ops;
4079 init.flags = 0;
4080 init.num_parents = 0;
4081
4082 mgmt->rate = 0;
4083 mgmt->hw.init = &init;
4084
4085 *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
4086 if (IS_ERR(*tx_clk))
4087 return PTR_ERR(*tx_clk);
4088
4089 err = clk_prepare_enable(*tx_clk);
4090 if (err)
4091 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
4092 else
4093 dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
4094
4095 return 0;
4096}
4097
4098static int fu540_c000_init(struct platform_device *pdev)
4099{
4100 mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
4101 if (IS_ERR(mgmt->reg))
4102 return PTR_ERR(mgmt->reg);
4103
4104 return macb_init(pdev);
4105}
4106
4107static const struct macb_config fu540_c000_config = {
4108 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
4109 MACB_CAPS_GEM_HAS_PTP,
4110 .dma_burst_length = 16,
4111 .clk_init = fu540_c000_clk_init,
4112 .init = fu540_c000_init,
4113 .jumbo_max_len = 10240,
4114};
4115
4116static const struct macb_config at91sam9260_config = {
4117 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4118 .clk_init = macb_clk_init,
4119 .init = macb_init,
4120};
4121
4122static const struct macb_config sama5d3macb_config = {
4123 .caps = MACB_CAPS_SG_DISABLED
4124 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4125 .clk_init = macb_clk_init,
4126 .init = macb_init,
4127};
4128
4129static const struct macb_config pc302gem_config = {
4130 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
4131 .dma_burst_length = 16,
4132 .clk_init = macb_clk_init,
4133 .init = macb_init,
4134};
4135
4136static const struct macb_config sama5d2_config = {
4137 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4138 .dma_burst_length = 16,
4139 .clk_init = macb_clk_init,
4140 .init = macb_init,
4141};
4142
4143static const struct macb_config sama5d3_config = {
4144 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
4145 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
4146 .dma_burst_length = 16,
4147 .clk_init = macb_clk_init,
4148 .init = macb_init,
4149 .jumbo_max_len = 10240,
4150};
4151
4152static const struct macb_config sama5d4_config = {
4153 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
4154 .dma_burst_length = 4,
4155 .clk_init = macb_clk_init,
4156 .init = macb_init,
4157};
4158
4159static const struct macb_config emac_config = {
4160 .caps = MACB_CAPS_NEEDS_RSTONUBR,
4161 .clk_init = at91ether_clk_init,
4162 .init = at91ether_init,
4163};
4164
4165static const struct macb_config np4_config = {
4166 .caps = MACB_CAPS_USRIO_DISABLED,
4167 .clk_init = macb_clk_init,
4168 .init = macb_init,
4169};
4170
4171static const struct macb_config zynqmp_config = {
4172 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4173 MACB_CAPS_JUMBO |
4174 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
4175 .dma_burst_length = 16,
4176 .clk_init = macb_clk_init,
4177 .init = macb_init,
4178 .jumbo_max_len = 10240,
4179};
4180
4181static const struct macb_config zynq_config = {
4182 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4183 MACB_CAPS_NEEDS_RSTONUBR,
4184 .dma_burst_length = 16,
4185 .clk_init = macb_clk_init,
4186 .init = macb_init,
4187};
4188
4189static const struct of_device_id macb_dt_ids[] = {
4190 { .compatible = "cdns,at32ap7000-macb" },
4191 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4192 { .compatible = "cdns,macb" },
4193 { .compatible = "cdns,np4-macb", .data = &np4_config },
4194 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4195 { .compatible = "cdns,gem", .data = &pc302gem_config },
4196 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
4197 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
4198 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
4199 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
4200 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4201 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4202 { .compatible = "cdns,emac", .data = &emac_config },
4203 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
4204 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
4205 { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
4206 { /* sentinel */ }
4207};
4208MODULE_DEVICE_TABLE(of, macb_dt_ids);
4209#endif /* CONFIG_OF */
4210
4211static const struct macb_config default_gem_config = {
4212 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4213 MACB_CAPS_JUMBO |
4214 MACB_CAPS_GEM_HAS_PTP,
4215 .dma_burst_length = 16,
4216 .clk_init = macb_clk_init,
4217 .init = macb_init,
4218 .jumbo_max_len = 10240,
4219};
4220
4221static int macb_probe(struct platform_device *pdev)
4222{
4223 const struct macb_config *macb_config = &default_gem_config;
4224 int (*clk_init)(struct platform_device *, struct clk **,
4225 struct clk **, struct clk **, struct clk **,
4226 struct clk **) = macb_config->clk_init;
4227 int (*init)(struct platform_device *) = macb_config->init;
4228 struct device_node *np = pdev->dev.of_node;
4229 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
4230 struct clk *tsu_clk = NULL;
4231 unsigned int queue_mask, num_queues;
4232 bool native_io;
4233 struct phy_device *phydev;
4234 struct net_device *dev;
4235 struct resource *regs;
4236 void __iomem *mem;
4237 const char *mac;
4238 struct macb *bp;
4239 int err, val;
4240
4241 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4242 mem = devm_ioremap_resource(&pdev->dev, regs);
4243 if (IS_ERR(mem))
4244 return PTR_ERR(mem);
4245
4246 if (np) {
4247 const struct of_device_id *match;
4248
4249 match = of_match_node(macb_dt_ids, np);
4250 if (match && match->data) {
4251 macb_config = match->data;
4252 clk_init = macb_config->clk_init;
4253 init = macb_config->init;
4254 }
4255 }
4256
4257 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
4258 if (err)
4259 return err;
4260
4261 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4262 pm_runtime_use_autosuspend(&pdev->dev);
4263 pm_runtime_get_noresume(&pdev->dev);
4264 pm_runtime_set_active(&pdev->dev);
4265 pm_runtime_enable(&pdev->dev);
4266 native_io = hw_is_native_io(mem);
4267
4268 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
4269 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
4270 if (!dev) {
4271 err = -ENOMEM;
4272 goto err_disable_clocks;
4273 }
4274
4275 dev->base_addr = regs->start;
4276
4277 SET_NETDEV_DEV(dev, &pdev->dev);
4278
4279 bp = netdev_priv(dev);
4280 bp->pdev = pdev;
4281 bp->dev = dev;
4282 bp->regs = mem;
4283 bp->native_io = native_io;
4284 if (native_io) {
4285 bp->macb_reg_readl = hw_readl_native;
4286 bp->macb_reg_writel = hw_writel_native;
4287 } else {
4288 bp->macb_reg_readl = hw_readl;
4289 bp->macb_reg_writel = hw_writel;
4290 }
4291 bp->num_queues = num_queues;
4292 bp->queue_mask = queue_mask;
4293 if (macb_config)
4294 bp->dma_burst_length = macb_config->dma_burst_length;
4295 bp->pclk = pclk;
4296 bp->hclk = hclk;
4297 bp->tx_clk = tx_clk;
4298 bp->rx_clk = rx_clk;
4299 bp->tsu_clk = tsu_clk;
4300 if (macb_config)
4301 bp->jumbo_max_len = macb_config->jumbo_max_len;
4302
4303 bp->wol = 0;
4304 if (of_get_property(np, "magic-packet", NULL))
4305 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4306 device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4307
4308 spin_lock_init(&bp->lock);
4309
4310 /* setup capabilities */
4311 macb_configure_caps(bp, macb_config);
4312
4313#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4314 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4315 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
4316 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4317 }
4318#endif
4319 platform_set_drvdata(pdev, dev);
4320
4321 dev->irq = platform_get_irq(pdev, 0);
4322 if (dev->irq < 0) {
4323 err = dev->irq;
4324 goto err_out_free_netdev;
4325 }
4326
4327 /* MTU range: 68 - 1500 or 10240 */
4328 dev->min_mtu = GEM_MTU_MIN_SIZE;
4329 if (bp->caps & MACB_CAPS_JUMBO)
4330 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4331 else
4332 dev->max_mtu = ETH_DATA_LEN;
4333
4334 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4335 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4336 if (val)
4337 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4338 macb_dma_desc_get_size(bp);
4339
4340 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4341 if (val)
4342 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4343 macb_dma_desc_get_size(bp);
4344 }
4345
4346 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4347 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4348 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4349
4350 mac = of_get_mac_address(np);
4351 if (PTR_ERR(mac) == -EPROBE_DEFER) {
4352 err = -EPROBE_DEFER;
4353 goto err_out_free_netdev;
4354 } else if (!IS_ERR_OR_NULL(mac)) {
4355 ether_addr_copy(bp->dev->dev_addr, mac);
4356 } else {
4357 macb_get_hwaddr(bp);
4358 }
4359
4360 err = of_get_phy_mode(np);
4361 if (err < 0)
4362 /* not found in DT, MII by default */
4363 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4364 else
4365 bp->phy_interface = err;
4366
4367 /* IP specific init */
4368 err = init(pdev);
4369 if (err)
4370 goto err_out_free_netdev;
4371
4372 err = macb_mii_init(bp);
4373 if (err)
4374 goto err_out_free_netdev;
4375
4376 phydev = dev->phydev;
4377
4378 netif_carrier_off(dev);
4379
4380 err = register_netdev(dev);
4381 if (err) {
4382 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
4383 goto err_out_unregister_mdio;
4384 }
4385
4386 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4387 (unsigned long)bp);
4388
4389 phy_attached_info(phydev);
4390
4391 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4392 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4393 dev->base_addr, dev->irq, dev->dev_addr);
4394
4395 pm_runtime_mark_last_busy(&bp->pdev->dev);
4396 pm_runtime_put_autosuspend(&bp->pdev->dev);
4397
4398 return 0;
4399
4400err_out_unregister_mdio:
4401 phy_disconnect(dev->phydev);
4402 mdiobus_unregister(bp->mii_bus);
4403 of_node_put(bp->phy_node);
4404 if (np && of_phy_is_fixed_link(np))
4405 of_phy_deregister_fixed_link(np);
4406 mdiobus_free(bp->mii_bus);
4407
4408err_out_free_netdev:
4409 free_netdev(dev);
4410
4411err_disable_clocks:
4412 clk_disable_unprepare(tx_clk);
4413 clk_disable_unprepare(hclk);
4414 clk_disable_unprepare(pclk);
4415 clk_disable_unprepare(rx_clk);
4416 clk_disable_unprepare(tsu_clk);
4417 pm_runtime_disable(&pdev->dev);
4418 pm_runtime_set_suspended(&pdev->dev);
4419 pm_runtime_dont_use_autosuspend(&pdev->dev);
4420
4421 return err;
4422}
4423
4424static int macb_remove(struct platform_device *pdev)
4425{
4426 struct net_device *dev;
4427 struct macb *bp;
4428 struct device_node *np = pdev->dev.of_node;
4429
4430 dev = platform_get_drvdata(pdev);
4431
4432 if (dev) {
4433 bp = netdev_priv(dev);
4434 if (dev->phydev)
4435 phy_disconnect(dev->phydev);
4436 mdiobus_unregister(bp->mii_bus);
4437 if (np && of_phy_is_fixed_link(np))
4438 of_phy_deregister_fixed_link(np);
4439 dev->phydev = NULL;
4440 mdiobus_free(bp->mii_bus);
4441
4442 unregister_netdev(dev);
4443 tasklet_kill(&bp->hresp_err_tasklet);
4444 pm_runtime_disable(&pdev->dev);
4445 pm_runtime_dont_use_autosuspend(&pdev->dev);
4446 if (!pm_runtime_suspended(&pdev->dev)) {
4447 clk_disable_unprepare(bp->tx_clk);
4448 clk_disable_unprepare(bp->hclk);
4449 clk_disable_unprepare(bp->pclk);
4450 clk_disable_unprepare(bp->rx_clk);
4451 clk_disable_unprepare(bp->tsu_clk);
4452 pm_runtime_set_suspended(&pdev->dev);
4453 }
4454 of_node_put(bp->phy_node);
4455 free_netdev(dev);
4456 }
4457
4458 return 0;
4459}
4460
4461static int __maybe_unused macb_suspend(struct device *dev)
4462{
4463 struct net_device *netdev = dev_get_drvdata(dev);
4464 struct macb *bp = netdev_priv(netdev);
4465 struct macb_queue *queue = bp->queues;
4466 unsigned long flags;
4467 unsigned int q;
4468
4469 if (!netif_running(netdev))
4470 return 0;
4471
4472
4473 if (bp->wol & MACB_WOL_ENABLED) {
4474 macb_writel(bp, IER, MACB_BIT(WOL));
4475 macb_writel(bp, WOL, MACB_BIT(MAG));
4476 enable_irq_wake(bp->queues[0].irq);
4477 netif_device_detach(netdev);
4478 } else {
4479 netif_device_detach(netdev);
4480 for (q = 0, queue = bp->queues; q < bp->num_queues;
4481 ++q, ++queue)
4482 napi_disable(&queue->napi);
4483 phy_stop(netdev->phydev);
4484 phy_suspend(netdev->phydev);
4485 spin_lock_irqsave(&bp->lock, flags);
4486 macb_reset_hw(bp);
4487 spin_unlock_irqrestore(&bp->lock, flags);
4488
4489 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4490 bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
4491
4492 if (netdev->hw_features & NETIF_F_NTUPLE)
4493 bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
4494 }
4495
4496 netif_carrier_off(netdev);
4497 if (bp->ptp_info)
4498 bp->ptp_info->ptp_remove(netdev);
4499 if (!device_may_wakeup(dev))
4500 pm_runtime_force_suspend(dev);
4501
4502 return 0;
4503}
4504
4505static int __maybe_unused macb_resume(struct device *dev)
4506{
4507 struct net_device *netdev = dev_get_drvdata(dev);
4508 struct macb *bp = netdev_priv(netdev);
4509 struct macb_queue *queue = bp->queues;
4510 unsigned int q;
4511
4512 if (!netif_running(netdev))
4513 return 0;
4514
4515 if (!device_may_wakeup(dev))
4516 pm_runtime_force_resume(dev);
4517
4518 if (bp->wol & MACB_WOL_ENABLED) {
4519 macb_writel(bp, IDR, MACB_BIT(WOL));
4520 macb_writel(bp, WOL, 0);
4521 disable_irq_wake(bp->queues[0].irq);
4522 } else {
4523 macb_writel(bp, NCR, MACB_BIT(MPE));
4524
4525 if (netdev->hw_features & NETIF_F_NTUPLE)
4526 gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
4527
4528 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
4529 macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
4530
4531 for (q = 0, queue = bp->queues; q < bp->num_queues;
4532 ++q, ++queue)
4533 napi_enable(&queue->napi);
4534 phy_resume(netdev->phydev);
4535 phy_init_hw(netdev->phydev);
4536 phy_start(netdev->phydev);
4537 }
4538
4539 bp->macbgem_ops.mog_init_rings(bp);
4540 macb_init_hw(bp);
4541 macb_set_rx_mode(netdev);
4542 macb_restore_features(bp);
4543 netif_device_attach(netdev);
4544 if (bp->ptp_info)
4545 bp->ptp_info->ptp_init(netdev);
4546
4547 return 0;
4548}
4549
4550static int __maybe_unused macb_runtime_suspend(struct device *dev)
4551{
4552 struct net_device *netdev = dev_get_drvdata(dev);
4553 struct macb *bp = netdev_priv(netdev);
4554
4555 if (!(device_may_wakeup(dev))) {
4556 clk_disable_unprepare(bp->tx_clk);
4557 clk_disable_unprepare(bp->hclk);
4558 clk_disable_unprepare(bp->pclk);
4559 clk_disable_unprepare(bp->rx_clk);
4560 }
4561 clk_disable_unprepare(bp->tsu_clk);
4562
4563 return 0;
4564}
4565
4566static int __maybe_unused macb_runtime_resume(struct device *dev)
4567{
4568 struct net_device *netdev = dev_get_drvdata(dev);
4569 struct macb *bp = netdev_priv(netdev);
4570
4571 if (!(device_may_wakeup(dev))) {
4572 clk_prepare_enable(bp->pclk);
4573 clk_prepare_enable(bp->hclk);
4574 clk_prepare_enable(bp->tx_clk);
4575 clk_prepare_enable(bp->rx_clk);
4576 }
4577 clk_prepare_enable(bp->tsu_clk);
4578
4579 return 0;
4580}
4581
4582static const struct dev_pm_ops macb_pm_ops = {
4583 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4584 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4585};
4586
4587static struct platform_driver macb_driver = {
4588 .probe = macb_probe,
4589 .remove = macb_remove,
4590 .driver = {
4591 .name = "macb",
4592 .of_match_table = of_match_ptr(macb_dt_ids),
4593 .pm = &macb_pm_ops,
4594 },
4595};
4596
4597module_platform_driver(macb_driver);
4598
4599MODULE_LICENSE("GPL");
4600MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
4601MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4602MODULE_ALIAS("platform:macb");