2 * Cadence MACB/GEM Ethernet Controller driver
4 * Copyright (C) 2004-2006 Atmel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/clk.h>
13 #include <linux/crc32.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/circ_buf.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
22 #include <linux/gpio.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/interrupt.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/platform_data/macb.h>
29 #include <linux/platform_device.h>
30 #include <linux/phy.h>
32 #include <linux/of_device.h>
33 #include <linux/of_gpio.h>
34 #include <linux/of_mdio.h>
35 #include <linux/of_net.h>
37 #include <linux/udp.h>
38 #include <linux/tcp.h>
39 #include <linux/iopoll.h>
40 #include <linux/pm_runtime.h>
43 #define MACB_RX_BUFFER_SIZE 128
44 #define RX_BUFFER_MULTIPLE 64 /* bytes */
46 #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
47 #define MIN_RX_RING_SIZE 64
48 #define MAX_RX_RING_SIZE 8192
49 #define RX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
52 #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
53 #define MIN_TX_RING_SIZE 64
54 #define MAX_TX_RING_SIZE 4096
55 #define TX_RING_BYTES(bp) (macb_dma_desc_get_size(bp) \
58 /* level of occupied TX descriptors under which we wake up TX process */
59 #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
61 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
62 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
65 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
68 /* Max length of transmit frame must be a multiple of 8 bytes */
69 #define MACB_TX_LEN_ALIGN 8
70 #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
71 #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
73 #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
74 #define MACB_NETIF_LSO NETIF_F_TSO
76 #define MACB_WOL_HAS_MAGIC_PACKET (0x1 << 0)
77 #define MACB_WOL_ENABLED (0x1 << 1)
79 /* Graceful stop timeouts in us. We should allow up to
80 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
82 #define MACB_HALT_TIMEOUT 1230
84 #define MACB_PM_TIMEOUT 100 /* ms */
86 #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
88 /* DMA buffer descriptor might be different size
89 * depends on hardware configuration:
91 * 1. dma address width 32 bits:
92 * word 1: 32 bit address of Data Buffer
95 * 2. dma address width 64 bits:
96 * word 1: 32 bit address of Data Buffer
98 * word 3: upper 32 bit address of Data Buffer
101 * 3. dma address width 32 bits with hardware timestamping:
102 * word 1: 32 bit address of Data Buffer
104 * word 3: timestamp word 1
105 * word 4: timestamp word 2
107 * 4. dma address width 64 bits with hardware timestamping:
108 * word 1: 32 bit address of Data Buffer
110 * word 3: upper 32 bit address of Data Buffer
112 * word 5: timestamp word 1
113 * word 6: timestamp word 2
115 static unsigned int macb_dma_desc_get_size(struct macb *bp)
118 unsigned int desc_size;
120 switch (bp->hw_dma_cap) {
122 desc_size = sizeof(struct macb_dma_desc)
123 + sizeof(struct macb_dma_desc_64);
126 desc_size = sizeof(struct macb_dma_desc)
127 + sizeof(struct macb_dma_desc_ptp);
129 case HW_DMA_CAP_64B_PTP:
130 desc_size = sizeof(struct macb_dma_desc)
131 + sizeof(struct macb_dma_desc_64)
132 + sizeof(struct macb_dma_desc_ptp);
135 desc_size = sizeof(struct macb_dma_desc);
139 return sizeof(struct macb_dma_desc);
142 static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
145 switch (bp->hw_dma_cap) {
150 case HW_DMA_CAP_64B_PTP:
160 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
161 static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
163 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
164 return (struct macb_dma_desc_64 *)((void *)desc + sizeof(struct macb_dma_desc));
169 /* Ring buffer accessors */
170 static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
172 return index & (bp->tx_ring_size - 1);
175 static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
178 index = macb_tx_ring_wrap(queue->bp, index);
179 index = macb_adj_dma_desc_idx(queue->bp, index);
180 return &queue->tx_ring[index];
183 static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
186 return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
189 static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
193 offset = macb_tx_ring_wrap(queue->bp, index) *
194 macb_dma_desc_get_size(queue->bp);
196 return queue->tx_ring_dma + offset;
199 static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
201 return index & (bp->rx_ring_size - 1);
204 static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
206 index = macb_rx_ring_wrap(queue->bp, index);
207 index = macb_adj_dma_desc_idx(queue->bp, index);
208 return &queue->rx_ring[index];
211 static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
213 return queue->rx_buffers + queue->bp->rx_buffer_size *
214 macb_rx_ring_wrap(queue->bp, index);
218 static u32 hw_readl_native(struct macb *bp, int offset)
220 return __raw_readl(bp->regs + offset);
223 static void hw_writel_native(struct macb *bp, int offset, u32 value)
225 __raw_writel(value, bp->regs + offset);
228 static u32 hw_readl(struct macb *bp, int offset)
230 return readl_relaxed(bp->regs + offset);
233 static void hw_writel(struct macb *bp, int offset, u32 value)
235 writel_relaxed(value, bp->regs + offset);
238 /* Find the CPU endianness by using the loopback bit of NCR register. When the
239 * CPU is in big endian we need to program swapped mode for management
242 static bool hw_is_native_io(void __iomem *addr)
244 u32 value = MACB_BIT(LLB);
246 __raw_writel(value, addr + MACB_NCR);
247 value = __raw_readl(addr + MACB_NCR);
249 /* Write 0 back to disable everything */
250 __raw_writel(0, addr + MACB_NCR);
252 return value == MACB_BIT(LLB);
255 static bool hw_is_gem(void __iomem *addr, bool native_io)
260 id = __raw_readl(addr + MACB_MID);
262 id = readl_relaxed(addr + MACB_MID);
264 return MACB_BFEXT(IDNUM, id) >= 0x2;
267 static void macb_set_hwaddr(struct macb *bp)
272 bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
273 macb_or_gem_writel(bp, SA1B, bottom);
274 top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
275 macb_or_gem_writel(bp, SA1T, top);
277 /* Clear unused address register sets */
278 macb_or_gem_writel(bp, SA2B, 0);
279 macb_or_gem_writel(bp, SA2T, 0);
280 macb_or_gem_writel(bp, SA3B, 0);
281 macb_or_gem_writel(bp, SA3T, 0);
282 macb_or_gem_writel(bp, SA4B, 0);
283 macb_or_gem_writel(bp, SA4T, 0);
286 static void macb_get_hwaddr(struct macb *bp)
288 struct macb_platform_data *pdata;
294 pdata = dev_get_platdata(&bp->pdev->dev);
296 /* Check all 4 address register for valid address */
297 for (i = 0; i < 4; i++) {
298 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
299 top = macb_or_gem_readl(bp, SA1T + i * 8);
301 if (pdata && pdata->rev_eth_addr) {
302 addr[5] = bottom & 0xff;
303 addr[4] = (bottom >> 8) & 0xff;
304 addr[3] = (bottom >> 16) & 0xff;
305 addr[2] = (bottom >> 24) & 0xff;
306 addr[1] = top & 0xff;
307 addr[0] = (top & 0xff00) >> 8;
309 addr[0] = bottom & 0xff;
310 addr[1] = (bottom >> 8) & 0xff;
311 addr[2] = (bottom >> 16) & 0xff;
312 addr[3] = (bottom >> 24) & 0xff;
313 addr[4] = top & 0xff;
314 addr[5] = (top >> 8) & 0xff;
317 if (is_valid_ether_addr(addr)) {
318 memcpy(bp->dev->dev_addr, addr, sizeof(addr));
323 dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
324 eth_hw_addr_random(bp->dev);
327 static int macb_mdio_wait_for_idle(struct macb *bp)
331 return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
332 1, MACB_MDIO_TIMEOUT);
335 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
337 struct macb *bp = bus->priv;
340 status = pm_runtime_get_sync(&bp->pdev->dev);
344 status = macb_mdio_wait_for_idle(bp);
348 macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
349 | MACB_BF(RW, MACB_MAN_READ)
350 | MACB_BF(PHYA, mii_id)
351 | MACB_BF(REGA, regnum)
352 | MACB_BF(CODE, MACB_MAN_CODE)));
354 status = macb_mdio_wait_for_idle(bp);
358 status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
361 pm_runtime_mark_last_busy(&bp->pdev->dev);
362 pm_runtime_put_autosuspend(&bp->pdev->dev);
367 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
370 struct macb *bp = bus->priv;
373 status = pm_runtime_get_sync(&bp->pdev->dev);
377 status = macb_mdio_wait_for_idle(bp);
379 goto mdio_write_exit;
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)));
388 status = macb_mdio_wait_for_idle(bp);
390 goto mdio_write_exit;
393 pm_runtime_mark_last_busy(&bp->pdev->dev);
394 pm_runtime_put_autosuspend(&bp->pdev->dev);
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
405 static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
407 long ferr, rate, rate_rounded;
426 rate_rounded = clk_round_rate(clk, rate);
427 if (rate_rounded < 0)
430 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
433 ferr = abs(rate_rounded - rate);
434 ferr = DIV_ROUND_UP(ferr, rate / 100000);
436 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
439 if (clk_set_rate(clk, rate_rounded))
440 netdev_err(dev, "adjusting tx_clk failed.\n");
443 static void macb_handle_link_change(struct net_device *dev)
445 struct macb *bp = netdev_priv(dev);
446 struct phy_device *phydev = dev->phydev;
448 int status_change = 0;
450 spin_lock_irqsave(&bp->lock, flags);
453 if ((bp->speed != phydev->speed) ||
454 (bp->duplex != phydev->duplex)) {
457 reg = macb_readl(bp, NCFGR);
458 reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
460 reg &= ~GEM_BIT(GBE);
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)
470 macb_or_gem_writel(bp, NCFGR, reg);
472 bp->speed = phydev->speed;
473 bp->duplex = phydev->duplex;
478 if (phydev->link != bp->link) {
483 bp->link = phydev->link;
488 spin_unlock_irqrestore(&bp->lock, flags);
492 /* Update the TX clock rate if and only if the link is
493 * up and there has been a link change.
495 macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
497 netif_carrier_on(dev);
498 netdev_info(dev, "link up (%d/%s)\n",
500 phydev->duplex == DUPLEX_FULL ?
503 netif_carrier_off(dev);
504 netdev_info(dev, "link down\n");
509 /* based on au1000_eth. c*/
510 static int macb_mii_probe(struct net_device *dev)
512 struct macb *bp = netdev_priv(dev);
513 struct macb_platform_data *pdata;
514 struct phy_device *phydev;
515 struct device_node *np;
518 pdata = dev_get_platdata(&bp->pdev->dev);
519 np = bp->pdev->dev.of_node;
523 if (of_phy_is_fixed_link(np)) {
524 bp->phy_node = of_node_get(np);
526 bp->phy_node = of_parse_phandle(np, "phy-handle", 0);
527 /* fallback to standard phy registration if no
528 * phy-handle was found nor any phy found during
529 * dt phy registration
531 if (!bp->phy_node && !phy_find_first(bp->mii_bus)) {
532 for (i = 0; i < PHY_MAX_ADDR; i++) {
533 struct phy_device *phydev;
535 phydev = mdiobus_scan(bp->mii_bus, i);
536 if (IS_ERR(phydev) &&
537 PTR_ERR(phydev) != -ENODEV) {
538 ret = PTR_ERR(phydev);
550 phydev = of_phy_connect(dev, bp->phy_node,
551 &macb_handle_link_change, 0,
556 phydev = phy_find_first(bp->mii_bus);
558 netdev_err(dev, "no PHY found\n");
563 if (gpio_is_valid(pdata->phy_irq_pin)) {
564 ret = devm_gpio_request(&bp->pdev->dev,
565 pdata->phy_irq_pin, "phy int");
567 phy_irq = gpio_to_irq(pdata->phy_irq_pin);
568 phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
571 phydev->irq = PHY_POLL;
575 /* attach the mac to the phy */
576 ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
579 netdev_err(dev, "Could not attach to PHY\n");
584 /* mask with MAC supported features */
585 if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
586 phy_set_max_speed(phydev, SPEED_1000);
588 phy_set_max_speed(phydev, SPEED_100);
590 if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
591 phy_remove_link_mode(phydev,
592 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
601 static int macb_mii_init(struct macb *bp)
603 struct macb_platform_data *pdata;
604 struct device_node *np;
607 /* Enable management port */
608 macb_writel(bp, NCR, MACB_BIT(MPE));
610 bp->mii_bus = mdiobus_alloc();
616 bp->mii_bus->name = "MACB_mii_bus";
617 bp->mii_bus->read = &macb_mdio_read;
618 bp->mii_bus->write = &macb_mdio_write;
619 snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
620 bp->pdev->name, bp->pdev->id);
621 bp->mii_bus->priv = bp;
622 bp->mii_bus->parent = &bp->pdev->dev;
623 pdata = dev_get_platdata(&bp->pdev->dev);
625 dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
627 np = bp->pdev->dev.of_node;
628 if (np && of_phy_is_fixed_link(np)) {
629 if (of_phy_register_fixed_link(np) < 0) {
630 dev_err(&bp->pdev->dev,
631 "broken fixed-link specification %pOF\n", np);
632 goto err_out_free_mdiobus;
635 err = mdiobus_register(bp->mii_bus);
638 bp->mii_bus->phy_mask = pdata->phy_mask;
640 err = of_mdiobus_register(bp->mii_bus, np);
644 goto err_out_free_fixed_link;
646 err = macb_mii_probe(bp->dev);
648 goto err_out_unregister_bus;
652 err_out_unregister_bus:
653 mdiobus_unregister(bp->mii_bus);
654 err_out_free_fixed_link:
655 if (np && of_phy_is_fixed_link(np))
656 of_phy_deregister_fixed_link(np);
657 err_out_free_mdiobus:
658 of_node_put(bp->phy_node);
659 mdiobus_free(bp->mii_bus);
664 static void macb_update_stats(struct macb *bp)
666 u32 *p = &bp->hw_stats.macb.rx_pause_frames;
667 u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
668 int offset = MACB_PFR;
670 WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
672 for (; p < end; p++, offset += 4)
673 *p += bp->macb_reg_readl(bp, offset);
676 static int macb_halt_tx(struct macb *bp)
678 unsigned long halt_time, timeout;
681 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
683 timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
686 status = macb_readl(bp, TSR);
687 if (!(status & MACB_BIT(TGO)))
691 } while (time_before(halt_time, timeout));
696 static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
698 if (tx_skb->mapping) {
699 if (tx_skb->mapped_as_page)
700 dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
701 tx_skb->size, DMA_TO_DEVICE);
703 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
704 tx_skb->size, DMA_TO_DEVICE);
709 dev_kfree_skb_any(tx_skb->skb);
714 static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
716 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
717 struct macb_dma_desc_64 *desc_64;
719 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
720 desc_64 = macb_64b_desc(bp, desc);
721 desc_64->addrh = upper_32_bits(addr);
722 /* The low bits of RX address contain the RX_USED bit, clearing
723 * of which allows packet RX. Make sure the high bits are also
724 * visible to HW at that point.
729 desc->addr = lower_32_bits(addr);
732 static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
735 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
736 struct macb_dma_desc_64 *desc_64;
738 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
739 desc_64 = macb_64b_desc(bp, desc);
740 addr = ((u64)(desc_64->addrh) << 32);
743 addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
747 static void macb_tx_error_task(struct work_struct *work)
749 struct macb_queue *queue = container_of(work, struct macb_queue,
751 struct macb *bp = queue->bp;
752 struct macb_tx_skb *tx_skb;
753 struct macb_dma_desc *desc;
758 netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
759 (unsigned int)(queue - bp->queues),
760 queue->tx_tail, queue->tx_head);
762 /* Prevent the queue IRQ handlers from running: each of them may call
763 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
764 * As explained below, we have to halt the transmission before updating
765 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
766 * network engine about the macb/gem being halted.
768 spin_lock_irqsave(&bp->lock, flags);
770 /* Make sure nobody is trying to queue up new packets */
771 netif_tx_stop_all_queues(bp->dev);
773 /* Stop transmission now
774 * (in case we have just queued new packets)
775 * macb/gem must be halted to write TBQP register
777 if (macb_halt_tx(bp))
778 /* Just complain for now, reinitializing TX path can be good */
779 netdev_err(bp->dev, "BUG: halt tx timed out\n");
781 /* Treat frames in TX queue including the ones that caused the error.
782 * Free transmit buffers in upper layer.
784 for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
787 desc = macb_tx_desc(queue, tail);
789 tx_skb = macb_tx_skb(queue, tail);
792 if (ctrl & MACB_BIT(TX_USED)) {
793 /* skb is set for the last buffer of the frame */
795 macb_tx_unmap(bp, tx_skb);
797 tx_skb = macb_tx_skb(queue, tail);
801 /* ctrl still refers to the first buffer descriptor
802 * since it's the only one written back by the hardware
804 if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
805 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
806 macb_tx_ring_wrap(bp, tail),
808 bp->dev->stats.tx_packets++;
809 queue->stats.tx_packets++;
810 bp->dev->stats.tx_bytes += skb->len;
811 queue->stats.tx_bytes += skb->len;
814 /* "Buffers exhausted mid-frame" errors may only happen
815 * if the driver is buggy, so complain loudly about
816 * those. Statistics are updated by hardware.
818 if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
820 "BUG: TX buffers exhausted mid-frame\n");
822 desc->ctrl = ctrl | MACB_BIT(TX_USED);
825 macb_tx_unmap(bp, tx_skb);
828 /* Set end of TX queue */
829 desc = macb_tx_desc(queue, 0);
830 macb_set_addr(bp, desc, 0);
831 desc->ctrl = MACB_BIT(TX_USED);
833 /* Make descriptor updates visible to hardware */
836 /* Reinitialize the TX desc queue */
837 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
838 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
839 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
840 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
842 /* Make TX ring reflect state of hardware */
846 /* Housework before enabling TX IRQ */
847 macb_writel(bp, TSR, macb_readl(bp, TSR));
848 queue_writel(queue, IER, MACB_TX_INT_FLAGS);
850 /* Now we are ready to start transmission again */
851 netif_tx_start_all_queues(bp->dev);
852 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
854 spin_unlock_irqrestore(&bp->lock, flags);
857 static void macb_tx_interrupt(struct macb_queue *queue)
862 struct macb *bp = queue->bp;
863 u16 queue_index = queue - bp->queues;
865 status = macb_readl(bp, TSR);
866 macb_writel(bp, TSR, status);
868 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
869 queue_writel(queue, ISR, MACB_BIT(TCOMP));
871 netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
872 (unsigned long)status);
874 head = queue->tx_head;
875 for (tail = queue->tx_tail; tail != head; tail++) {
876 struct macb_tx_skb *tx_skb;
878 struct macb_dma_desc *desc;
881 desc = macb_tx_desc(queue, tail);
883 /* Make hw descriptor updates visible to CPU */
888 /* TX_USED bit is only set by hardware on the very first buffer
889 * descriptor of the transmitted frame.
891 if (!(ctrl & MACB_BIT(TX_USED)))
894 /* Process all buffers of the current transmitted frame */
896 tx_skb = macb_tx_skb(queue, tail);
899 /* First, update TX stats if needed */
901 if (gem_ptp_do_txstamp(queue, skb, desc) == 0) {
902 /* skb now belongs to timestamp buffer
903 * and will be removed later
907 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
908 macb_tx_ring_wrap(bp, tail),
910 bp->dev->stats.tx_packets++;
911 queue->stats.tx_packets++;
912 bp->dev->stats.tx_bytes += skb->len;
913 queue->stats.tx_bytes += skb->len;
916 /* Now we can safely release resources */
917 macb_tx_unmap(bp, tx_skb);
919 /* skb is set only for the last buffer of the frame.
920 * WARNING: at this point skb has been freed by
928 queue->tx_tail = tail;
929 if (__netif_subqueue_stopped(bp->dev, queue_index) &&
930 CIRC_CNT(queue->tx_head, queue->tx_tail,
931 bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
932 netif_wake_subqueue(bp->dev, queue_index);
935 static void gem_rx_refill(struct macb_queue *queue)
940 struct macb *bp = queue->bp;
941 struct macb_dma_desc *desc;
943 while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
944 bp->rx_ring_size) > 0) {
945 entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
947 /* Make hw descriptor updates visible to CPU */
950 queue->rx_prepared_head++;
951 desc = macb_rx_desc(queue, entry);
953 if (!queue->rx_skbuff[entry]) {
954 /* allocate sk_buff for this free entry in ring */
955 skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
956 if (unlikely(!skb)) {
958 "Unable to allocate sk_buff\n");
962 /* now fill corresponding descriptor entry */
963 paddr = dma_map_single(&bp->pdev->dev, skb->data,
966 if (dma_mapping_error(&bp->pdev->dev, paddr)) {
971 queue->rx_skbuff[entry] = skb;
973 if (entry == bp->rx_ring_size - 1)
974 paddr |= MACB_BIT(RX_WRAP);
976 /* Setting addr clears RX_USED and allows reception,
977 * make sure ctrl is cleared first to avoid a race.
980 macb_set_addr(bp, desc, paddr);
982 /* properly align Ethernet header */
983 skb_reserve(skb, NET_IP_ALIGN);
987 desc->addr &= ~MACB_BIT(RX_USED);
991 /* Make descriptor updates visible to hardware */
994 netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
995 queue, queue->rx_prepared_head, queue->rx_tail);
998 /* Mark DMA descriptors from begin up to and not including end as unused */
999 static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
1004 for (frag = begin; frag != end; frag++) {
1005 struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
1007 desc->addr &= ~MACB_BIT(RX_USED);
1010 /* Make descriptor updates visible to hardware */
1013 /* When this happens, the hardware stats registers for
1014 * whatever caused this is updated, so we don't have to record
1019 static int gem_rx(struct macb_queue *queue, int budget)
1021 struct macb *bp = queue->bp;
1024 struct sk_buff *skb;
1025 struct macb_dma_desc *desc;
1028 while (count < budget) {
1033 entry = macb_rx_ring_wrap(bp, queue->rx_tail);
1034 desc = macb_rx_desc(queue, entry);
1036 /* Make hw descriptor updates visible to CPU */
1039 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
1040 addr = macb_get_addr(bp, desc);
1045 /* Ensure ctrl is at least as up-to-date as rxused */
1053 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
1055 "not whole frame pointed by descriptor\n");
1056 bp->dev->stats.rx_dropped++;
1057 queue->stats.rx_dropped++;
1060 skb = queue->rx_skbuff[entry];
1061 if (unlikely(!skb)) {
1063 "inconsistent Rx descriptor chain\n");
1064 bp->dev->stats.rx_dropped++;
1065 queue->stats.rx_dropped++;
1068 /* now everything is ready for receiving packet */
1069 queue->rx_skbuff[entry] = NULL;
1070 len = ctrl & bp->rx_frm_len_mask;
1072 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
1075 dma_unmap_single(&bp->pdev->dev, addr,
1076 bp->rx_buffer_size, DMA_FROM_DEVICE);
1078 skb->protocol = eth_type_trans(skb, bp->dev);
1079 skb_checksum_none_assert(skb);
1080 if (bp->dev->features & NETIF_F_RXCSUM &&
1081 !(bp->dev->flags & IFF_PROMISC) &&
1082 GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
1083 skb->ip_summed = CHECKSUM_UNNECESSARY;
1085 bp->dev->stats.rx_packets++;
1086 queue->stats.rx_packets++;
1087 bp->dev->stats.rx_bytes += skb->len;
1088 queue->stats.rx_bytes += skb->len;
1090 gem_ptp_do_rxstamp(bp, skb, desc);
1092 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1093 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1094 skb->len, skb->csum);
1095 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
1096 skb_mac_header(skb), 16, true);
1097 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
1098 skb->data, 32, true);
1101 netif_receive_skb(skb);
1104 gem_rx_refill(queue);
1109 static int macb_rx_frame(struct macb_queue *queue, unsigned int first_frag,
1110 unsigned int last_frag)
1114 unsigned int offset;
1115 struct sk_buff *skb;
1116 struct macb_dma_desc *desc;
1117 struct macb *bp = queue->bp;
1119 desc = macb_rx_desc(queue, last_frag);
1120 len = desc->ctrl & bp->rx_frm_len_mask;
1122 netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
1123 macb_rx_ring_wrap(bp, first_frag),
1124 macb_rx_ring_wrap(bp, last_frag), len);
1126 /* The ethernet header starts NET_IP_ALIGN bytes into the
1127 * first buffer. Since the header is 14 bytes, this makes the
1128 * payload word-aligned.
1130 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
1131 * the two padding bytes into the skb so that we avoid hitting
1132 * the slowpath in memcpy(), and pull them off afterwards.
1134 skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
1136 bp->dev->stats.rx_dropped++;
1137 for (frag = first_frag; ; frag++) {
1138 desc = macb_rx_desc(queue, frag);
1139 desc->addr &= ~MACB_BIT(RX_USED);
1140 if (frag == last_frag)
1144 /* Make descriptor updates visible to hardware */
1151 len += NET_IP_ALIGN;
1152 skb_checksum_none_assert(skb);
1155 for (frag = first_frag; ; frag++) {
1156 unsigned int frag_len = bp->rx_buffer_size;
1158 if (offset + frag_len > len) {
1159 if (unlikely(frag != last_frag)) {
1160 dev_kfree_skb_any(skb);
1163 frag_len = len - offset;
1165 skb_copy_to_linear_data_offset(skb, offset,
1166 macb_rx_buffer(queue, frag),
1168 offset += bp->rx_buffer_size;
1169 desc = macb_rx_desc(queue, frag);
1170 desc->addr &= ~MACB_BIT(RX_USED);
1172 if (frag == last_frag)
1176 /* Make descriptor updates visible to hardware */
1179 __skb_pull(skb, NET_IP_ALIGN);
1180 skb->protocol = eth_type_trans(skb, bp->dev);
1182 bp->dev->stats.rx_packets++;
1183 bp->dev->stats.rx_bytes += skb->len;
1184 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
1185 skb->len, skb->csum);
1186 netif_receive_skb(skb);
1191 static inline void macb_init_rx_ring(struct macb_queue *queue)
1193 struct macb *bp = queue->bp;
1195 struct macb_dma_desc *desc = NULL;
1198 addr = queue->rx_buffers_dma;
1199 for (i = 0; i < bp->rx_ring_size; i++) {
1200 desc = macb_rx_desc(queue, i);
1201 macb_set_addr(bp, desc, addr);
1203 addr += bp->rx_buffer_size;
1205 desc->addr |= MACB_BIT(RX_WRAP);
1209 static int macb_rx(struct macb_queue *queue, int budget)
1211 struct macb *bp = queue->bp;
1212 bool reset_rx_queue = false;
1215 int first_frag = -1;
1217 for (tail = queue->rx_tail; budget > 0; tail++) {
1218 struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
1221 /* Make hw descriptor updates visible to CPU */
1224 if (!(desc->addr & MACB_BIT(RX_USED)))
1227 /* Ensure ctrl is at least as up-to-date as addr */
1232 if (ctrl & MACB_BIT(RX_SOF)) {
1233 if (first_frag != -1)
1234 discard_partial_frame(queue, first_frag, tail);
1238 if (ctrl & MACB_BIT(RX_EOF)) {
1241 if (unlikely(first_frag == -1)) {
1242 reset_rx_queue = true;
1246 dropped = macb_rx_frame(queue, first_frag, tail);
1248 if (unlikely(dropped < 0)) {
1249 reset_rx_queue = true;
1259 if (unlikely(reset_rx_queue)) {
1260 unsigned long flags;
1263 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1265 spin_lock_irqsave(&bp->lock, flags);
1267 ctrl = macb_readl(bp, NCR);
1268 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1270 macb_init_rx_ring(queue);
1271 queue_writel(queue, RBQP, queue->rx_ring_dma);
1273 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1275 spin_unlock_irqrestore(&bp->lock, flags);
1279 if (first_frag != -1)
1280 queue->rx_tail = first_frag;
1282 queue->rx_tail = tail;
1287 static int macb_poll(struct napi_struct *napi, int budget)
1289 struct macb_queue *queue = container_of(napi, struct macb_queue, napi);
1290 struct macb *bp = queue->bp;
1294 status = macb_readl(bp, RSR);
1295 macb_writel(bp, RSR, status);
1297 netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
1298 (unsigned long)status, budget);
1300 work_done = bp->macbgem_ops.mog_rx(queue, budget);
1301 if (work_done < budget) {
1302 napi_complete_done(napi, work_done);
1304 /* Packets received while interrupts were disabled */
1305 status = macb_readl(bp, RSR);
1307 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1308 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1309 napi_reschedule(napi);
1311 queue_writel(queue, IER, bp->rx_intr_mask);
1315 /* TODO: Handle errors */
1320 static void macb_hresp_error_task(unsigned long data)
1322 struct macb *bp = (struct macb *)data;
1323 struct net_device *dev = bp->dev;
1324 struct macb_queue *queue = bp->queues;
1328 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1329 queue_writel(queue, IDR, bp->rx_intr_mask |
1333 ctrl = macb_readl(bp, NCR);
1334 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1335 macb_writel(bp, NCR, ctrl);
1337 netif_tx_stop_all_queues(dev);
1338 netif_carrier_off(dev);
1340 bp->macbgem_ops.mog_init_rings(bp);
1342 /* Initialize TX and RX buffers */
1343 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1344 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1345 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1346 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1347 queue_writel(queue, RBQPH,
1348 upper_32_bits(queue->rx_ring_dma));
1350 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1351 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1352 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1353 queue_writel(queue, TBQPH,
1354 upper_32_bits(queue->tx_ring_dma));
1357 /* Enable interrupts */
1358 queue_writel(queue, IER,
1364 ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1365 macb_writel(bp, NCR, ctrl);
1367 netif_carrier_on(dev);
1368 netif_tx_start_all_queues(dev);
1371 static void macb_tx_restart(struct macb_queue *queue)
1373 unsigned int head = queue->tx_head;
1374 unsigned int tail = queue->tx_tail;
1375 struct macb *bp = queue->bp;
1377 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1378 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1383 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1386 static irqreturn_t macb_interrupt(int irq, void *dev_id)
1388 struct macb_queue *queue = dev_id;
1389 struct macb *bp = queue->bp;
1390 struct net_device *dev = bp->dev;
1393 status = queue_readl(queue, ISR);
1395 if (unlikely(!status))
1398 spin_lock(&bp->lock);
1401 /* close possible race with dev_close */
1402 if (unlikely(!netif_running(dev))) {
1403 queue_writel(queue, IDR, -1);
1404 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1405 queue_writel(queue, ISR, -1);
1409 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1410 (unsigned int)(queue - bp->queues),
1411 (unsigned long)status);
1413 if (status & bp->rx_intr_mask) {
1414 /* There's no point taking any more interrupts
1415 * until we have processed the buffers. The
1416 * scheduling call may fail if the poll routine
1417 * is already scheduled, so disable interrupts
1420 queue_writel(queue, IDR, bp->rx_intr_mask);
1421 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1422 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1424 if (napi_schedule_prep(&queue->napi)) {
1425 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1426 __napi_schedule(&queue->napi);
1430 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1431 queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1432 schedule_work(&queue->tx_error_task);
1434 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1435 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1440 if (status & MACB_BIT(TCOMP))
1441 macb_tx_interrupt(queue);
1443 if (status & MACB_BIT(TXUBR))
1444 macb_tx_restart(queue);
1446 /* Link change detection isn't possible with RMII, so we'll
1447 * add that if/when we get our hands on a full-blown MII PHY.
1450 /* There is a hardware issue under heavy load where DMA can
1451 * stop, this causes endless "used buffer descriptor read"
1452 * interrupts but it can be cleared by re-enabling RX. See
1453 * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1454 * section 16.7.4 for details. RXUBR is only enabled for
1455 * these two versions.
1457 if (status & MACB_BIT(RXUBR)) {
1458 ctrl = macb_readl(bp, NCR);
1459 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1461 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1463 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1464 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1467 if (status & MACB_BIT(ISR_ROVR)) {
1468 /* We missed at least one packet */
1469 if (macb_is_gem(bp))
1470 bp->hw_stats.gem.rx_overruns++;
1472 bp->hw_stats.macb.rx_overruns++;
1474 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1475 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1478 if (status & MACB_BIT(HRESP)) {
1479 tasklet_schedule(&bp->hresp_err_tasklet);
1480 netdev_err(dev, "DMA bus error: HRESP not OK\n");
1482 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1483 queue_writel(queue, ISR, MACB_BIT(HRESP));
1485 status = queue_readl(queue, ISR);
1488 spin_unlock(&bp->lock);
1493 #ifdef CONFIG_NET_POLL_CONTROLLER
1494 /* Polling receive - used by netconsole and other diagnostic tools
1495 * to allow network i/o with interrupts disabled.
1497 static void macb_poll_controller(struct net_device *dev)
1499 struct macb *bp = netdev_priv(dev);
1500 struct macb_queue *queue;
1501 unsigned long flags;
1504 local_irq_save(flags);
1505 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1506 macb_interrupt(dev->irq, queue);
1507 local_irq_restore(flags);
1511 static unsigned int macb_tx_map(struct macb *bp,
1512 struct macb_queue *queue,
1513 struct sk_buff *skb,
1514 unsigned int hdrlen)
1517 unsigned int len, entry, i, tx_head = queue->tx_head;
1518 struct macb_tx_skb *tx_skb = NULL;
1519 struct macb_dma_desc *desc;
1520 unsigned int offset, size, count = 0;
1521 unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1522 unsigned int eof = 1, mss_mfs = 0;
1523 u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1526 if (skb_shinfo(skb)->gso_size != 0) {
1527 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1529 lso_ctrl = MACB_LSO_UFO_ENABLE;
1532 lso_ctrl = MACB_LSO_TSO_ENABLE;
1535 /* First, map non-paged data */
1536 len = skb_headlen(skb);
1538 /* first buffer length */
1543 entry = macb_tx_ring_wrap(bp, tx_head);
1544 tx_skb = &queue->tx_skb[entry];
1546 mapping = dma_map_single(&bp->pdev->dev,
1548 size, DMA_TO_DEVICE);
1549 if (dma_mapping_error(&bp->pdev->dev, mapping))
1552 /* Save info to properly release resources */
1554 tx_skb->mapping = mapping;
1555 tx_skb->size = size;
1556 tx_skb->mapped_as_page = false;
1563 size = min(len, bp->max_tx_length);
1566 /* Then, map paged data from fragments */
1567 for (f = 0; f < nr_frags; f++) {
1568 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1570 len = skb_frag_size(frag);
1573 size = min(len, bp->max_tx_length);
1574 entry = macb_tx_ring_wrap(bp, tx_head);
1575 tx_skb = &queue->tx_skb[entry];
1577 mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1578 offset, size, DMA_TO_DEVICE);
1579 if (dma_mapping_error(&bp->pdev->dev, mapping))
1582 /* Save info to properly release resources */
1584 tx_skb->mapping = mapping;
1585 tx_skb->size = size;
1586 tx_skb->mapped_as_page = true;
1595 /* Should never happen */
1596 if (unlikely(!tx_skb)) {
1597 netdev_err(bp->dev, "BUG! empty skb!\n");
1601 /* This is the last buffer of the frame: save socket buffer */
1604 /* Update TX ring: update buffer descriptors in reverse order
1605 * to avoid race condition
1608 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1609 * to set the end of TX queue
1612 entry = macb_tx_ring_wrap(bp, i);
1613 ctrl = MACB_BIT(TX_USED);
1614 desc = macb_tx_desc(queue, entry);
1618 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1619 /* include header and FCS in value given to h/w */
1620 mss_mfs = skb_shinfo(skb)->gso_size +
1621 skb_transport_offset(skb) +
1624 mss_mfs = skb_shinfo(skb)->gso_size;
1625 /* TCP Sequence Number Source Select
1626 * can be set only for TSO
1634 entry = macb_tx_ring_wrap(bp, i);
1635 tx_skb = &queue->tx_skb[entry];
1636 desc = macb_tx_desc(queue, entry);
1638 ctrl = (u32)tx_skb->size;
1640 ctrl |= MACB_BIT(TX_LAST);
1643 if (unlikely(entry == (bp->tx_ring_size - 1)))
1644 ctrl |= MACB_BIT(TX_WRAP);
1646 /* First descriptor is header descriptor */
1647 if (i == queue->tx_head) {
1648 ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1649 ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1650 if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1651 skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1652 ctrl |= MACB_BIT(TX_NOCRC);
1654 /* Only set MSS/MFS on payload descriptors
1655 * (second or later descriptor)
1657 ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1659 /* Set TX buffer descriptor */
1660 macb_set_addr(bp, desc, tx_skb->mapping);
1661 /* desc->addr must be visible to hardware before clearing
1662 * 'TX_USED' bit in desc->ctrl.
1666 } while (i != queue->tx_head);
1668 queue->tx_head = tx_head;
1673 netdev_err(bp->dev, "TX DMA map failed\n");
1675 for (i = queue->tx_head; i != tx_head; i++) {
1676 tx_skb = macb_tx_skb(queue, i);
1678 macb_tx_unmap(bp, tx_skb);
1684 static netdev_features_t macb_features_check(struct sk_buff *skb,
1685 struct net_device *dev,
1686 netdev_features_t features)
1688 unsigned int nr_frags, f;
1689 unsigned int hdrlen;
1691 /* Validate LSO compatibility */
1693 /* there is only one buffer */
1694 if (!skb_is_nonlinear(skb))
1697 /* length of header */
1698 hdrlen = skb_transport_offset(skb);
1699 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
1700 hdrlen += tcp_hdrlen(skb);
1703 * When software supplies two or more payload buffers all payload buffers
1704 * apart from the last must be a multiple of 8 bytes in size.
1706 if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1707 return features & ~MACB_NETIF_LSO;
1709 nr_frags = skb_shinfo(skb)->nr_frags;
1710 /* No need to check last fragment */
1712 for (f = 0; f < nr_frags; f++) {
1713 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1715 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1716 return features & ~MACB_NETIF_LSO;
1721 static inline int macb_clear_csum(struct sk_buff *skb)
1723 /* no change for packets without checksum offloading */
1724 if (skb->ip_summed != CHECKSUM_PARTIAL)
1727 /* make sure we can modify the header */
1728 if (unlikely(skb_cow_head(skb, 0)))
1731 /* initialize checksum field
1732 * This is required - at least for Zynq, which otherwise calculates
1733 * wrong UDP header checksums for UDP packets with UDP data len <=2
1735 *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1739 static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1741 bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb);
1742 int padlen = ETH_ZLEN - (*skb)->len;
1743 int headroom = skb_headroom(*skb);
1744 int tailroom = skb_tailroom(*skb);
1745 struct sk_buff *nskb;
1748 if (!(ndev->features & NETIF_F_HW_CSUM) ||
1749 !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1750 skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1754 /* FCS could be appeded to tailroom. */
1755 if (tailroom >= ETH_FCS_LEN)
1757 /* FCS could be appeded by moving data to headroom. */
1758 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1760 /* No room for FCS, need to reallocate skb. */
1762 padlen = ETH_FCS_LEN;
1764 /* Add room for FCS. */
1765 padlen += ETH_FCS_LEN;
1768 if (!cloned && headroom + tailroom >= padlen) {
1769 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1770 skb_set_tail_pointer(*skb, (*skb)->len);
1772 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1776 dev_consume_skb_any(*skb);
1780 if (padlen > ETH_FCS_LEN)
1781 skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1784 /* set FCS to packet */
1785 fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1788 skb_put_u8(*skb, fcs & 0xff);
1789 skb_put_u8(*skb, (fcs >> 8) & 0xff);
1790 skb_put_u8(*skb, (fcs >> 16) & 0xff);
1791 skb_put_u8(*skb, (fcs >> 24) & 0xff);
1796 static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1798 u16 queue_index = skb_get_queue_mapping(skb);
1799 struct macb *bp = netdev_priv(dev);
1800 struct macb_queue *queue = &bp->queues[queue_index];
1801 unsigned long flags;
1802 unsigned int desc_cnt, nr_frags, frag_size, f;
1803 unsigned int hdrlen;
1804 bool is_lso, is_udp = 0;
1805 netdev_tx_t ret = NETDEV_TX_OK;
1807 if (macb_clear_csum(skb)) {
1808 dev_kfree_skb_any(skb);
1812 if (macb_pad_and_fcs(&skb, dev)) {
1813 dev_kfree_skb_any(skb);
1817 is_lso = (skb_shinfo(skb)->gso_size != 0);
1820 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1822 /* length of headers */
1824 /* only queue eth + ip headers separately for UDP */
1825 hdrlen = skb_transport_offset(skb);
1827 hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1828 if (skb_headlen(skb) < hdrlen) {
1829 netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1830 /* if this is required, would need to copy to single buffer */
1831 return NETDEV_TX_BUSY;
1834 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
1836 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1837 netdev_vdbg(bp->dev,
1838 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1839 queue_index, skb->len, skb->head, skb->data,
1840 skb_tail_pointer(skb), skb_end_pointer(skb));
1841 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1842 skb->data, 16, true);
1845 /* Count how many TX buffer descriptors are needed to send this
1846 * socket buffer: skb fragments of jumbo frames may need to be
1847 * split into many buffer descriptors.
1849 if (is_lso && (skb_headlen(skb) > hdrlen))
1850 /* extra header descriptor if also payload in first buffer */
1851 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1853 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
1854 nr_frags = skb_shinfo(skb)->nr_frags;
1855 for (f = 0; f < nr_frags; f++) {
1856 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1857 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
1860 spin_lock_irqsave(&bp->lock, flags);
1862 /* This is a hard error, log it. */
1863 if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1864 bp->tx_ring_size) < desc_cnt) {
1865 netif_stop_subqueue(dev, queue_index);
1866 spin_unlock_irqrestore(&bp->lock, flags);
1867 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1868 queue->tx_head, queue->tx_tail);
1869 return NETDEV_TX_BUSY;
1872 /* Map socket buffer for DMA transfer */
1873 if (!macb_tx_map(bp, queue, skb, hdrlen)) {
1874 dev_kfree_skb_any(skb);
1878 /* Make newly initialized descriptor visible to hardware */
1880 skb_tx_timestamp(skb);
1882 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1884 if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
1885 netif_stop_subqueue(dev, queue_index);
1888 spin_unlock_irqrestore(&bp->lock, flags);
1893 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1895 if (!macb_is_gem(bp)) {
1896 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1898 bp->rx_buffer_size = size;
1900 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1902 "RX buffer must be multiple of %d bytes, expanding\n",
1903 RX_BUFFER_MULTIPLE);
1904 bp->rx_buffer_size =
1905 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1909 netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
1910 bp->dev->mtu, bp->rx_buffer_size);
1913 static void gem_free_rx_buffers(struct macb *bp)
1915 struct sk_buff *skb;
1916 struct macb_dma_desc *desc;
1917 struct macb_queue *queue;
1922 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1923 if (!queue->rx_skbuff)
1926 for (i = 0; i < bp->rx_ring_size; i++) {
1927 skb = queue->rx_skbuff[i];
1932 desc = macb_rx_desc(queue, i);
1933 addr = macb_get_addr(bp, desc);
1935 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1937 dev_kfree_skb_any(skb);
1941 kfree(queue->rx_skbuff);
1942 queue->rx_skbuff = NULL;
1946 static void macb_free_rx_buffers(struct macb *bp)
1948 struct macb_queue *queue = &bp->queues[0];
1950 if (queue->rx_buffers) {
1951 dma_free_coherent(&bp->pdev->dev,
1952 bp->rx_ring_size * bp->rx_buffer_size,
1953 queue->rx_buffers, queue->rx_buffers_dma);
1954 queue->rx_buffers = NULL;
1958 static void macb_free_consistent(struct macb *bp)
1960 struct macb_queue *queue;
1964 bp->macbgem_ops.mog_free_rx_buffers(bp);
1966 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1967 kfree(queue->tx_skb);
1968 queue->tx_skb = NULL;
1969 if (queue->tx_ring) {
1970 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1971 dma_free_coherent(&bp->pdev->dev, size,
1972 queue->tx_ring, queue->tx_ring_dma);
1973 queue->tx_ring = NULL;
1975 if (queue->rx_ring) {
1976 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1977 dma_free_coherent(&bp->pdev->dev, size,
1978 queue->rx_ring, queue->rx_ring_dma);
1979 queue->rx_ring = NULL;
1984 static int gem_alloc_rx_buffers(struct macb *bp)
1986 struct macb_queue *queue;
1990 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1991 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1992 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1993 if (!queue->rx_skbuff)
1997 "Allocated %d RX struct sk_buff entries at %p\n",
1998 bp->rx_ring_size, queue->rx_skbuff);
2003 static int macb_alloc_rx_buffers(struct macb *bp)
2005 struct macb_queue *queue = &bp->queues[0];
2008 size = bp->rx_ring_size * bp->rx_buffer_size;
2009 queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2010 &queue->rx_buffers_dma, GFP_KERNEL);
2011 if (!queue->rx_buffers)
2015 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
2016 size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
2020 static int macb_alloc_consistent(struct macb *bp)
2022 struct macb_queue *queue;
2026 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2027 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2028 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2029 &queue->tx_ring_dma,
2031 if (!queue->tx_ring)
2034 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2035 q, size, (unsigned long)queue->tx_ring_dma,
2038 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
2039 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2043 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2044 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2045 &queue->rx_ring_dma, GFP_KERNEL);
2046 if (!queue->rx_ring)
2049 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2050 size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
2052 if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2058 macb_free_consistent(bp);
2062 static void gem_init_rings(struct macb *bp)
2064 struct macb_queue *queue;
2065 struct macb_dma_desc *desc = NULL;
2069 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2070 for (i = 0; i < bp->tx_ring_size; i++) {
2071 desc = macb_tx_desc(queue, i);
2072 macb_set_addr(bp, desc, 0);
2073 desc->ctrl = MACB_BIT(TX_USED);
2075 desc->ctrl |= MACB_BIT(TX_WRAP);
2080 queue->rx_prepared_head = 0;
2082 gem_rx_refill(queue);
2087 static void macb_init_rings(struct macb *bp)
2090 struct macb_dma_desc *desc = NULL;
2092 macb_init_rx_ring(&bp->queues[0]);
2094 for (i = 0; i < bp->tx_ring_size; i++) {
2095 desc = macb_tx_desc(&bp->queues[0], i);
2096 macb_set_addr(bp, desc, 0);
2097 desc->ctrl = MACB_BIT(TX_USED);
2099 bp->queues[0].tx_head = 0;
2100 bp->queues[0].tx_tail = 0;
2101 desc->ctrl |= MACB_BIT(TX_WRAP);
2104 static void macb_reset_hw(struct macb *bp)
2106 struct macb_queue *queue;
2108 u32 ctrl = macb_readl(bp, NCR);
2110 /* Disable RX and TX (XXX: Should we halt the transmission
2113 ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2115 /* Clear the stats registers (XXX: Update stats first?) */
2116 ctrl |= MACB_BIT(CLRSTAT);
2118 macb_writel(bp, NCR, ctrl);
2120 /* Clear all status flags */
2121 macb_writel(bp, TSR, -1);
2122 macb_writel(bp, RSR, -1);
2124 /* Disable all interrupts */
2125 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2126 queue_writel(queue, IDR, -1);
2127 queue_readl(queue, ISR);
2128 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2129 queue_writel(queue, ISR, -1);
2133 static u32 gem_mdc_clk_div(struct macb *bp)
2136 unsigned long pclk_hz = clk_get_rate(bp->pclk);
2138 if (pclk_hz <= 20000000)
2139 config = GEM_BF(CLK, GEM_CLK_DIV8);
2140 else if (pclk_hz <= 40000000)
2141 config = GEM_BF(CLK, GEM_CLK_DIV16);
2142 else if (pclk_hz <= 80000000)
2143 config = GEM_BF(CLK, GEM_CLK_DIV32);
2144 else if (pclk_hz <= 120000000)
2145 config = GEM_BF(CLK, GEM_CLK_DIV48);
2146 else if (pclk_hz <= 160000000)
2147 config = GEM_BF(CLK, GEM_CLK_DIV64);
2149 config = GEM_BF(CLK, GEM_CLK_DIV96);
2154 static u32 macb_mdc_clk_div(struct macb *bp)
2157 unsigned long pclk_hz;
2159 if (macb_is_gem(bp))
2160 return gem_mdc_clk_div(bp);
2162 pclk_hz = clk_get_rate(bp->pclk);
2163 if (pclk_hz <= 20000000)
2164 config = MACB_BF(CLK, MACB_CLK_DIV8);
2165 else if (pclk_hz <= 40000000)
2166 config = MACB_BF(CLK, MACB_CLK_DIV16);
2167 else if (pclk_hz <= 80000000)
2168 config = MACB_BF(CLK, MACB_CLK_DIV32);
2170 config = MACB_BF(CLK, MACB_CLK_DIV64);
2175 /* Get the DMA bus width field of the network configuration register that we
2176 * should program. We find the width from decoding the design configuration
2177 * register to find the maximum supported data bus width.
2179 static u32 macb_dbw(struct macb *bp)
2181 if (!macb_is_gem(bp))
2184 switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2186 return GEM_BF(DBW, GEM_DBW128);
2188 return GEM_BF(DBW, GEM_DBW64);
2191 return GEM_BF(DBW, GEM_DBW32);
2195 /* Configure the receive DMA engine
2196 * - use the correct receive buffer size
2197 * - set best burst length for DMA operations
2198 * (if not supported by FIFO, it will fallback to default)
2199 * - set both rx/tx packet buffers to full memory size
2200 * These are configurable parameters for GEM.
2202 static void macb_configure_dma(struct macb *bp)
2204 struct macb_queue *queue;
2209 buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
2210 if (macb_is_gem(bp)) {
2211 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2212 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2214 queue_writel(queue, RBQS, buffer_size);
2216 dmacfg |= GEM_BF(RXBS, buffer_size);
2218 if (bp->dma_burst_length)
2219 dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2220 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2221 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2224 dmacfg &= ~GEM_BIT(ENDIA_DESC);
2226 dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2228 if (bp->dev->features & NETIF_F_HW_CSUM)
2229 dmacfg |= GEM_BIT(TXCOEN);
2231 dmacfg &= ~GEM_BIT(TXCOEN);
2233 dmacfg &= ~GEM_BIT(ADDR64);
2234 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2235 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2236 dmacfg |= GEM_BIT(ADDR64);
2238 #ifdef CONFIG_MACB_USE_HWSTAMP
2239 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2240 dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2242 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2244 gem_writel(bp, DMACFG, dmacfg);
2248 static void macb_init_hw(struct macb *bp)
2250 struct macb_queue *queue;
2256 macb_set_hwaddr(bp);
2258 config = macb_mdc_clk_div(bp);
2259 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2260 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
2261 config |= MACB_BF(RBOF, NET_IP_ALIGN); /* Make eth data aligned */
2262 config |= MACB_BIT(PAE); /* PAuse Enable */
2263 config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
2264 if (bp->caps & MACB_CAPS_JUMBO)
2265 config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
2267 config |= MACB_BIT(BIG); /* Receive oversized frames */
2268 if (bp->dev->flags & IFF_PROMISC)
2269 config |= MACB_BIT(CAF); /* Copy All Frames */
2270 else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2271 config |= GEM_BIT(RXCOEN);
2272 if (!(bp->dev->flags & IFF_BROADCAST))
2273 config |= MACB_BIT(NBC); /* No BroadCast */
2274 config |= macb_dbw(bp);
2275 macb_writel(bp, NCFGR, config);
2276 if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2277 gem_writel(bp, JML, bp->jumbo_max_len);
2278 bp->speed = SPEED_10;
2279 bp->duplex = DUPLEX_HALF;
2280 bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
2281 if (bp->caps & MACB_CAPS_JUMBO)
2282 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
2284 macb_configure_dma(bp);
2286 /* Initialize TX and RX buffers */
2287 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2288 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2289 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2290 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2291 queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2293 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
2294 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2295 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2296 queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
2299 /* Enable interrupts */
2300 queue_writel(queue, IER,
2306 /* Enable TX and RX */
2307 macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
2310 /* The hash address register is 64 bits long and takes up two
2311 * locations in the memory map. The least significant bits are stored
2312 * in EMAC_HSL and the most significant bits in EMAC_HSH.
2314 * The unicast hash enable and the multicast hash enable bits in the
2315 * network configuration register enable the reception of hash matched
2316 * frames. The destination address is reduced to a 6 bit index into
2317 * the 64 bit hash register using the following hash function. The
2318 * hash function is an exclusive or of every sixth bit of the
2319 * destination address.
2321 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2322 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2323 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2324 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2325 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2326 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2328 * da[0] represents the least significant bit of the first byte
2329 * received, that is, the multicast/unicast indicator, and da[47]
2330 * represents the most significant bit of the last byte received. If
2331 * the hash index, hi[n], points to a bit that is set in the hash
2332 * register then the frame will be matched according to whether the
2333 * frame is multicast or unicast. A multicast match will be signalled
2334 * if the multicast hash enable bit is set, da[0] is 1 and the hash
2335 * index points to a bit set in the hash register. A unicast match
2336 * will be signalled if the unicast hash enable bit is set, da[0] is 0
2337 * and the hash index points to a bit set in the hash register. To
2338 * receive all multicast frames, the hash register should be set with
2339 * all ones and the multicast hash enable bit should be set in the
2340 * network configuration register.
2343 static inline int hash_bit_value(int bitnr, __u8 *addr)
2345 if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2350 /* Return the hash index value for the specified address. */
2351 static int hash_get_index(__u8 *addr)
2356 for (j = 0; j < 6; j++) {
2357 for (i = 0, bitval = 0; i < 8; i++)
2358 bitval ^= hash_bit_value(i * 6 + j, addr);
2360 hash_index |= (bitval << j);
2366 /* Add multicast addresses to the internal multicast-hash table. */
2367 static void macb_sethashtable(struct net_device *dev)
2369 struct netdev_hw_addr *ha;
2370 unsigned long mc_filter[2];
2372 struct macb *bp = netdev_priv(dev);
2377 netdev_for_each_mc_addr(ha, dev) {
2378 bitnr = hash_get_index(ha->addr);
2379 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2382 macb_or_gem_writel(bp, HRB, mc_filter[0]);
2383 macb_or_gem_writel(bp, HRT, mc_filter[1]);
2386 /* Enable/Disable promiscuous and multicast modes. */
2387 static void macb_set_rx_mode(struct net_device *dev)
2390 struct macb *bp = netdev_priv(dev);
2392 cfg = macb_readl(bp, NCFGR);
2394 if (dev->flags & IFF_PROMISC) {
2395 /* Enable promiscuous mode */
2396 cfg |= MACB_BIT(CAF);
2398 /* Disable RX checksum offload */
2399 if (macb_is_gem(bp))
2400 cfg &= ~GEM_BIT(RXCOEN);
2402 /* Disable promiscuous mode */
2403 cfg &= ~MACB_BIT(CAF);
2405 /* Enable RX checksum offload only if requested */
2406 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2407 cfg |= GEM_BIT(RXCOEN);
2410 if (dev->flags & IFF_ALLMULTI) {
2411 /* Enable all multicast mode */
2412 macb_or_gem_writel(bp, HRB, -1);
2413 macb_or_gem_writel(bp, HRT, -1);
2414 cfg |= MACB_BIT(NCFGR_MTI);
2415 } else if (!netdev_mc_empty(dev)) {
2416 /* Enable specific multicasts */
2417 macb_sethashtable(dev);
2418 cfg |= MACB_BIT(NCFGR_MTI);
2419 } else if (dev->flags & (~IFF_ALLMULTI)) {
2420 /* Disable all multicast mode */
2421 macb_or_gem_writel(bp, HRB, 0);
2422 macb_or_gem_writel(bp, HRT, 0);
2423 cfg &= ~MACB_BIT(NCFGR_MTI);
2426 macb_writel(bp, NCFGR, cfg);
2429 static int macb_open(struct net_device *dev)
2431 struct macb *bp = netdev_priv(dev);
2432 size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
2433 struct macb_queue *queue;
2437 netdev_dbg(bp->dev, "open\n");
2439 err = pm_runtime_get_sync(&bp->pdev->dev);
2443 /* carrier starts down */
2444 netif_carrier_off(dev);
2446 /* if the phy is not yet register, retry later*/
2452 /* RX buffers initialization */
2453 macb_init_rx_buffer_size(bp, bufsz);
2455 err = macb_alloc_consistent(bp);
2457 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2462 bp->macbgem_ops.mog_init_rings(bp);
2465 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2466 napi_enable(&queue->napi);
2468 /* schedule a link state check */
2469 phy_start(dev->phydev);
2471 netif_tx_start_all_queues(dev);
2474 bp->ptp_info->ptp_init(dev);
2478 pm_runtime_put_sync(&bp->pdev->dev);
2484 static int macb_close(struct net_device *dev)
2486 struct macb *bp = netdev_priv(dev);
2487 struct macb_queue *queue;
2488 unsigned long flags;
2491 netif_tx_stop_all_queues(dev);
2493 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2494 napi_disable(&queue->napi);
2497 phy_stop(dev->phydev);
2499 spin_lock_irqsave(&bp->lock, flags);
2501 netif_carrier_off(dev);
2502 spin_unlock_irqrestore(&bp->lock, flags);
2504 macb_free_consistent(bp);
2507 bp->ptp_info->ptp_remove(dev);
2509 pm_runtime_put(&bp->pdev->dev);
2514 static int macb_change_mtu(struct net_device *dev, int new_mtu)
2516 if (netif_running(dev))
2524 static void gem_update_stats(struct macb *bp)
2526 struct macb_queue *queue;
2527 unsigned int i, q, idx;
2528 unsigned long *stat;
2530 u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2532 for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2533 u32 offset = gem_statistics[i].offset;
2534 u64 val = bp->macb_reg_readl(bp, offset);
2536 bp->ethtool_stats[i] += val;
2539 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2540 /* Add GEM_OCTTXH, GEM_OCTRXH */
2541 val = bp->macb_reg_readl(bp, offset + 4);
2542 bp->ethtool_stats[i] += ((u64)val) << 32;
2547 idx = GEM_STATS_LEN;
2548 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2549 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2550 bp->ethtool_stats[idx++] = *stat;
2553 static struct net_device_stats *gem_get_stats(struct macb *bp)
2555 struct gem_stats *hwstat = &bp->hw_stats.gem;
2556 struct net_device_stats *nstat = &bp->dev->stats;
2558 gem_update_stats(bp);
2560 nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2561 hwstat->rx_alignment_errors +
2562 hwstat->rx_resource_errors +
2563 hwstat->rx_overruns +
2564 hwstat->rx_oversize_frames +
2565 hwstat->rx_jabbers +
2566 hwstat->rx_undersized_frames +
2567 hwstat->rx_length_field_frame_errors);
2568 nstat->tx_errors = (hwstat->tx_late_collisions +
2569 hwstat->tx_excessive_collisions +
2570 hwstat->tx_underrun +
2571 hwstat->tx_carrier_sense_errors);
2572 nstat->multicast = hwstat->rx_multicast_frames;
2573 nstat->collisions = (hwstat->tx_single_collision_frames +
2574 hwstat->tx_multiple_collision_frames +
2575 hwstat->tx_excessive_collisions);
2576 nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2577 hwstat->rx_jabbers +
2578 hwstat->rx_undersized_frames +
2579 hwstat->rx_length_field_frame_errors);
2580 nstat->rx_over_errors = hwstat->rx_resource_errors;
2581 nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2582 nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2583 nstat->rx_fifo_errors = hwstat->rx_overruns;
2584 nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2585 nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2586 nstat->tx_fifo_errors = hwstat->tx_underrun;
2591 static void gem_get_ethtool_stats(struct net_device *dev,
2592 struct ethtool_stats *stats, u64 *data)
2596 bp = netdev_priv(dev);
2597 gem_update_stats(bp);
2598 memcpy(data, &bp->ethtool_stats, sizeof(u64)
2599 * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
2602 static int gem_get_sset_count(struct net_device *dev, int sset)
2604 struct macb *bp = netdev_priv(dev);
2608 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
2614 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2616 char stat_string[ETH_GSTRING_LEN];
2617 struct macb *bp = netdev_priv(dev);
2618 struct macb_queue *queue;
2624 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2625 memcpy(p, gem_statistics[i].stat_string,
2628 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2629 for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2630 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2631 q, queue_statistics[i].stat_string);
2632 memcpy(p, stat_string, ETH_GSTRING_LEN);
2639 static struct net_device_stats *macb_get_stats(struct net_device *dev)
2641 struct macb *bp = netdev_priv(dev);
2642 struct net_device_stats *nstat = &bp->dev->stats;
2643 struct macb_stats *hwstat = &bp->hw_stats.macb;
2645 if (macb_is_gem(bp))
2646 return gem_get_stats(bp);
2648 /* read stats from hardware */
2649 macb_update_stats(bp);
2651 /* Convert HW stats into netdevice stats */
2652 nstat->rx_errors = (hwstat->rx_fcs_errors +
2653 hwstat->rx_align_errors +
2654 hwstat->rx_resource_errors +
2655 hwstat->rx_overruns +
2656 hwstat->rx_oversize_pkts +
2657 hwstat->rx_jabbers +
2658 hwstat->rx_undersize_pkts +
2659 hwstat->rx_length_mismatch);
2660 nstat->tx_errors = (hwstat->tx_late_cols +
2661 hwstat->tx_excessive_cols +
2662 hwstat->tx_underruns +
2663 hwstat->tx_carrier_errors +
2664 hwstat->sqe_test_errors);
2665 nstat->collisions = (hwstat->tx_single_cols +
2666 hwstat->tx_multiple_cols +
2667 hwstat->tx_excessive_cols);
2668 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2669 hwstat->rx_jabbers +
2670 hwstat->rx_undersize_pkts +
2671 hwstat->rx_length_mismatch);
2672 nstat->rx_over_errors = hwstat->rx_resource_errors +
2673 hwstat->rx_overruns;
2674 nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2675 nstat->rx_frame_errors = hwstat->rx_align_errors;
2676 nstat->rx_fifo_errors = hwstat->rx_overruns;
2677 /* XXX: What does "missed" mean? */
2678 nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2679 nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2680 nstat->tx_fifo_errors = hwstat->tx_underruns;
2681 /* Don't know about heartbeat or window errors... */
2686 static int macb_get_regs_len(struct net_device *netdev)
2688 return MACB_GREGS_NBR * sizeof(u32);
2691 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2694 struct macb *bp = netdev_priv(dev);
2695 unsigned int tail, head;
2698 regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2699 | MACB_GREGS_VERSION;
2701 tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2702 head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
2704 regs_buff[0] = macb_readl(bp, NCR);
2705 regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
2706 regs_buff[2] = macb_readl(bp, NSR);
2707 regs_buff[3] = macb_readl(bp, TSR);
2708 regs_buff[4] = macb_readl(bp, RBQP);
2709 regs_buff[5] = macb_readl(bp, TBQP);
2710 regs_buff[6] = macb_readl(bp, RSR);
2711 regs_buff[7] = macb_readl(bp, IMR);
2713 regs_buff[8] = tail;
2714 regs_buff[9] = head;
2715 regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2716 regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
2718 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2719 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
2720 if (macb_is_gem(bp))
2721 regs_buff[13] = gem_readl(bp, DMACFG);
2724 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2726 struct macb *bp = netdev_priv(netdev);
2731 if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2732 wol->supported = WAKE_MAGIC;
2734 if (bp->wol & MACB_WOL_ENABLED)
2735 wol->wolopts |= WAKE_MAGIC;
2739 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2741 struct macb *bp = netdev_priv(netdev);
2743 if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2744 (wol->wolopts & ~WAKE_MAGIC))
2747 if (wol->wolopts & WAKE_MAGIC)
2748 bp->wol |= MACB_WOL_ENABLED;
2750 bp->wol &= ~MACB_WOL_ENABLED;
2752 device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2757 static void macb_get_ringparam(struct net_device *netdev,
2758 struct ethtool_ringparam *ring)
2760 struct macb *bp = netdev_priv(netdev);
2762 ring->rx_max_pending = MAX_RX_RING_SIZE;
2763 ring->tx_max_pending = MAX_TX_RING_SIZE;
2765 ring->rx_pending = bp->rx_ring_size;
2766 ring->tx_pending = bp->tx_ring_size;
2769 static int macb_set_ringparam(struct net_device *netdev,
2770 struct ethtool_ringparam *ring)
2772 struct macb *bp = netdev_priv(netdev);
2773 u32 new_rx_size, new_tx_size;
2774 unsigned int reset = 0;
2776 if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2779 new_rx_size = clamp_t(u32, ring->rx_pending,
2780 MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2781 new_rx_size = roundup_pow_of_two(new_rx_size);
2783 new_tx_size = clamp_t(u32, ring->tx_pending,
2784 MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2785 new_tx_size = roundup_pow_of_two(new_tx_size);
2787 if ((new_tx_size == bp->tx_ring_size) &&
2788 (new_rx_size == bp->rx_ring_size)) {
2793 if (netif_running(bp->dev)) {
2795 macb_close(bp->dev);
2798 bp->rx_ring_size = new_rx_size;
2799 bp->tx_ring_size = new_tx_size;
2807 #ifdef CONFIG_MACB_USE_HWSTAMP
2808 static unsigned int gem_get_tsu_rate(struct macb *bp)
2810 struct clk *tsu_clk;
2811 unsigned int tsu_rate;
2813 tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2814 if (!IS_ERR(tsu_clk))
2815 tsu_rate = clk_get_rate(tsu_clk);
2816 /* try pclk instead */
2817 else if (!IS_ERR(bp->pclk)) {
2819 tsu_rate = clk_get_rate(tsu_clk);
2825 static s32 gem_get_ptp_max_adj(void)
2830 static int gem_get_ts_info(struct net_device *dev,
2831 struct ethtool_ts_info *info)
2833 struct macb *bp = netdev_priv(dev);
2835 if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2836 ethtool_op_get_ts_info(dev, info);
2840 info->so_timestamping =
2841 SOF_TIMESTAMPING_TX_SOFTWARE |
2842 SOF_TIMESTAMPING_RX_SOFTWARE |
2843 SOF_TIMESTAMPING_SOFTWARE |
2844 SOF_TIMESTAMPING_TX_HARDWARE |
2845 SOF_TIMESTAMPING_RX_HARDWARE |
2846 SOF_TIMESTAMPING_RAW_HARDWARE;
2848 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2849 (1 << HWTSTAMP_TX_OFF) |
2850 (1 << HWTSTAMP_TX_ON);
2852 (1 << HWTSTAMP_FILTER_NONE) |
2853 (1 << HWTSTAMP_FILTER_ALL);
2855 info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2860 static struct macb_ptp_info gem_ptp_info = {
2861 .ptp_init = gem_ptp_init,
2862 .ptp_remove = gem_ptp_remove,
2863 .get_ptp_max_adj = gem_get_ptp_max_adj,
2864 .get_tsu_rate = gem_get_tsu_rate,
2865 .get_ts_info = gem_get_ts_info,
2866 .get_hwtst = gem_get_hwtst,
2867 .set_hwtst = gem_set_hwtst,
2871 static int macb_get_ts_info(struct net_device *netdev,
2872 struct ethtool_ts_info *info)
2874 struct macb *bp = netdev_priv(netdev);
2877 return bp->ptp_info->get_ts_info(netdev, info);
2879 return ethtool_op_get_ts_info(netdev, info);
2882 static void gem_enable_flow_filters(struct macb *bp, bool enable)
2884 struct ethtool_rx_fs_item *item;
2888 num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2890 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2891 struct ethtool_rx_flow_spec *fs = &item->fs;
2892 struct ethtool_tcpip4_spec *tp4sp_m;
2894 if (fs->location >= num_t2_scr)
2897 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2899 /* enable/disable screener regs for the flow entry */
2900 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2902 /* only enable fields with no masking */
2903 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2905 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2906 t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2908 t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2910 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2911 t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2913 t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2915 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2916 t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2918 t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2920 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2924 static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2926 struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2927 uint16_t index = fs->location;
2933 tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2934 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2936 /* ignore field if any masking set */
2937 if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2938 /* 1st compare reg - IP source address */
2941 w0 = tp4sp_v->ip4src;
2942 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2943 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2944 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2945 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2946 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2950 /* ignore field if any masking set */
2951 if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2952 /* 2nd compare reg - IP destination address */
2955 w0 = tp4sp_v->ip4dst;
2956 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2957 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2958 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2959 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2960 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2964 /* ignore both port fields if masking set in both */
2965 if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2966 /* 3rd compare reg - source port, destination port */
2969 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2970 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2971 w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2972 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2973 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2974 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2976 /* only one port definition */
2977 w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2978 w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2979 if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2980 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2981 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2982 } else { /* dst port */
2983 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2984 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2987 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2988 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2993 t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2994 t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2996 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2998 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
3000 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
3001 gem_writel_n(bp, SCRT2, index, t2_scr);
3004 static int gem_add_flow_filter(struct net_device *netdev,
3005 struct ethtool_rxnfc *cmd)
3007 struct macb *bp = netdev_priv(netdev);
3008 struct ethtool_rx_flow_spec *fs = &cmd->fs;
3009 struct ethtool_rx_fs_item *item, *newfs;
3010 unsigned long flags;
3014 newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
3017 memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3020 "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3021 fs->flow_type, (int)fs->ring_cookie, fs->location,
3022 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3023 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3024 htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3026 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3028 /* find correct place to add in list */
3029 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3030 if (item->fs.location > newfs->fs.location) {
3031 list_add_tail(&newfs->list, &item->list);
3034 } else if (item->fs.location == fs->location) {
3035 netdev_err(netdev, "Rule not added: location %d not free!\n",
3042 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
3044 gem_prog_cmp_regs(bp, fs);
3045 bp->rx_fs_list.count++;
3046 /* enable filtering if NTUPLE on */
3047 if (netdev->features & NETIF_F_NTUPLE)
3048 gem_enable_flow_filters(bp, 1);
3050 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3054 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3059 static int gem_del_flow_filter(struct net_device *netdev,
3060 struct ethtool_rxnfc *cmd)
3062 struct macb *bp = netdev_priv(netdev);
3063 struct ethtool_rx_fs_item *item;
3064 struct ethtool_rx_flow_spec *fs;
3065 unsigned long flags;
3067 spin_lock_irqsave(&bp->rx_fs_lock, flags);
3069 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3070 if (item->fs.location == cmd->fs.location) {
3071 /* disable screener regs for the flow entry */
3074 "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3075 fs->flow_type, (int)fs->ring_cookie, fs->location,
3076 htonl(fs->h_u.tcp_ip4_spec.ip4src),
3077 htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3078 htons(fs->h_u.tcp_ip4_spec.psrc),
3079 htons(fs->h_u.tcp_ip4_spec.pdst));
3081 gem_writel_n(bp, SCRT2, fs->location, 0);
3083 list_del(&item->list);
3084 bp->rx_fs_list.count--;
3085 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3091 spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3095 static int gem_get_flow_entry(struct net_device *netdev,
3096 struct ethtool_rxnfc *cmd)
3098 struct macb *bp = netdev_priv(netdev);
3099 struct ethtool_rx_fs_item *item;
3101 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3102 if (item->fs.location == cmd->fs.location) {
3103 memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3110 static int gem_get_all_flow_entries(struct net_device *netdev,
3111 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3113 struct macb *bp = netdev_priv(netdev);
3114 struct ethtool_rx_fs_item *item;
3117 list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3118 if (cnt == cmd->rule_cnt)
3120 rule_locs[cnt] = item->fs.location;
3123 cmd->data = bp->max_tuples;
3124 cmd->rule_cnt = cnt;
3129 static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3132 struct macb *bp = netdev_priv(netdev);
3136 case ETHTOOL_GRXRINGS:
3137 cmd->data = bp->num_queues;
3139 case ETHTOOL_GRXCLSRLCNT:
3140 cmd->rule_cnt = bp->rx_fs_list.count;
3142 case ETHTOOL_GRXCLSRULE:
3143 ret = gem_get_flow_entry(netdev, cmd);
3145 case ETHTOOL_GRXCLSRLALL:
3146 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3150 "Command parameter %d is not supported\n", cmd->cmd);
3157 static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3159 struct macb *bp = netdev_priv(netdev);
3163 case ETHTOOL_SRXCLSRLINS:
3164 if ((cmd->fs.location >= bp->max_tuples)
3165 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3169 ret = gem_add_flow_filter(netdev, cmd);
3171 case ETHTOOL_SRXCLSRLDEL:
3172 ret = gem_del_flow_filter(netdev, cmd);
3176 "Command parameter %d is not supported\n", cmd->cmd);
3183 static const struct ethtool_ops macb_ethtool_ops = {
3184 .get_regs_len = macb_get_regs_len,
3185 .get_regs = macb_get_regs,
3186 .get_link = ethtool_op_get_link,
3187 .get_ts_info = ethtool_op_get_ts_info,
3188 .get_wol = macb_get_wol,
3189 .set_wol = macb_set_wol,
3190 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3191 .set_link_ksettings = phy_ethtool_set_link_ksettings,
3192 .get_ringparam = macb_get_ringparam,
3193 .set_ringparam = macb_set_ringparam,
3196 static const struct ethtool_ops gem_ethtool_ops = {
3197 .get_regs_len = macb_get_regs_len,
3198 .get_regs = macb_get_regs,
3199 .get_link = ethtool_op_get_link,
3200 .get_ts_info = macb_get_ts_info,
3201 .get_ethtool_stats = gem_get_ethtool_stats,
3202 .get_strings = gem_get_ethtool_strings,
3203 .get_sset_count = gem_get_sset_count,
3204 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3205 .set_link_ksettings = phy_ethtool_set_link_ksettings,
3206 .get_ringparam = macb_get_ringparam,
3207 .set_ringparam = macb_set_ringparam,
3208 .get_rxnfc = gem_get_rxnfc,
3209 .set_rxnfc = gem_set_rxnfc,
3212 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3214 struct phy_device *phydev = dev->phydev;
3215 struct macb *bp = netdev_priv(dev);
3217 if (!netif_running(dev))
3224 return phy_mii_ioctl(phydev, rq, cmd);
3228 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3230 return bp->ptp_info->get_hwtst(dev, rq);
3232 return phy_mii_ioctl(phydev, rq, cmd);
3236 static int macb_set_features(struct net_device *netdev,
3237 netdev_features_t features)
3239 struct macb *bp = netdev_priv(netdev);
3240 netdev_features_t changed = features ^ netdev->features;
3242 /* TX checksum offload */
3243 if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3246 dmacfg = gem_readl(bp, DMACFG);
3247 if (features & NETIF_F_HW_CSUM)
3248 dmacfg |= GEM_BIT(TXCOEN);
3250 dmacfg &= ~GEM_BIT(TXCOEN);
3251 gem_writel(bp, DMACFG, dmacfg);
3254 /* RX checksum offload */
3255 if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3258 netcfg = gem_readl(bp, NCFGR);
3259 if (features & NETIF_F_RXCSUM &&
3260 !(netdev->flags & IFF_PROMISC))
3261 netcfg |= GEM_BIT(RXCOEN);
3263 netcfg &= ~GEM_BIT(RXCOEN);
3264 gem_writel(bp, NCFGR, netcfg);
3267 /* RX Flow Filters */
3268 if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3269 bool turn_on = features & NETIF_F_NTUPLE;
3271 gem_enable_flow_filters(bp, turn_on);
3276 static const struct net_device_ops macb_netdev_ops = {
3277 .ndo_open = macb_open,
3278 .ndo_stop = macb_close,
3279 .ndo_start_xmit = macb_start_xmit,
3280 .ndo_set_rx_mode = macb_set_rx_mode,
3281 .ndo_get_stats = macb_get_stats,
3282 .ndo_do_ioctl = macb_ioctl,
3283 .ndo_validate_addr = eth_validate_addr,
3284 .ndo_change_mtu = macb_change_mtu,
3285 .ndo_set_mac_address = eth_mac_addr,
3286 #ifdef CONFIG_NET_POLL_CONTROLLER
3287 .ndo_poll_controller = macb_poll_controller,
3289 .ndo_set_features = macb_set_features,
3290 .ndo_features_check = macb_features_check,
3293 /* Configure peripheral capabilities according to device tree
3294 * and integration options used
3296 static void macb_configure_caps(struct macb *bp,
3297 const struct macb_config *dt_conf)
3302 bp->caps = dt_conf->caps;
3304 if (hw_is_gem(bp->regs, bp->native_io)) {
3305 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3307 dcfg = gem_readl(bp, DCFG1);
3308 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3309 bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3310 dcfg = gem_readl(bp, DCFG2);
3311 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3312 bp->caps |= MACB_CAPS_FIFO_MODE;
3313 #ifdef CONFIG_MACB_USE_HWSTAMP
3314 if (gem_has_ptp(bp)) {
3315 if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3316 pr_err("GEM doesn't support hardware ptp.\n");
3318 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
3319 bp->ptp_info = &gem_ptp_info;
3325 dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
3328 static void macb_probe_queues(void __iomem *mem,
3330 unsigned int *queue_mask,
3331 unsigned int *num_queues)
3338 /* is it macb or gem ?
3340 * We need to read directly from the hardware here because
3341 * we are early in the probe process and don't have the
3342 * MACB_CAPS_MACB_IS_GEM flag positioned
3344 if (!hw_is_gem(mem, native_io))
3347 /* bit 0 is never set but queue 0 always exists */
3348 *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3352 for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3353 if (*queue_mask & (1 << hw_q))
3357 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
3358 struct clk **hclk, struct clk **tx_clk,
3359 struct clk **rx_clk, struct clk **tsu_clk)
3361 struct macb_platform_data *pdata;
3364 pdata = dev_get_platdata(&pdev->dev);
3366 *pclk = pdata->pclk;
3367 *hclk = pdata->hclk;
3369 *pclk = devm_clk_get(&pdev->dev, "pclk");
3370 *hclk = devm_clk_get(&pdev->dev, "hclk");
3373 if (IS_ERR_OR_NULL(*pclk)) {
3374 err = PTR_ERR(*pclk);
3378 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
3382 if (IS_ERR_OR_NULL(*hclk)) {
3383 err = PTR_ERR(*hclk);
3387 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
3391 *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3392 if (IS_ERR(*tx_clk))
3395 *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3396 if (IS_ERR(*rx_clk))
3399 *tsu_clk = devm_clk_get(&pdev->dev, "tsu_clk");
3400 if (IS_ERR(*tsu_clk))
3403 err = clk_prepare_enable(*pclk);
3405 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3409 err = clk_prepare_enable(*hclk);
3411 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
3412 goto err_disable_pclk;
3415 err = clk_prepare_enable(*tx_clk);
3417 dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
3418 goto err_disable_hclk;
3421 err = clk_prepare_enable(*rx_clk);
3423 dev_err(&pdev->dev, "failed to enable rx_clk (%u)\n", err);
3424 goto err_disable_txclk;
3427 err = clk_prepare_enable(*tsu_clk);
3429 dev_err(&pdev->dev, "failed to enable tsu_clk (%u)\n", err);
3430 goto err_disable_rxclk;
3436 clk_disable_unprepare(*rx_clk);
3439 clk_disable_unprepare(*tx_clk);
3442 clk_disable_unprepare(*hclk);
3445 clk_disable_unprepare(*pclk);
3450 static int macb_init(struct platform_device *pdev)
3452 struct net_device *dev = platform_get_drvdata(pdev);
3453 unsigned int hw_q, q;
3454 struct macb *bp = netdev_priv(dev);
3455 struct macb_queue *queue;
3459 bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3460 bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3462 /* set the queue register mapping once for all: queue0 has a special
3463 * register mapping but we don't want to test the queue index then
3464 * compute the corresponding register offset at run time.
3466 for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
3467 if (!(bp->queue_mask & (1 << hw_q)))
3470 queue = &bp->queues[q];
3472 netif_napi_add(dev, &queue->napi, macb_poll, 64);
3474 queue->ISR = GEM_ISR(hw_q - 1);
3475 queue->IER = GEM_IER(hw_q - 1);
3476 queue->IDR = GEM_IDR(hw_q - 1);
3477 queue->IMR = GEM_IMR(hw_q - 1);
3478 queue->TBQP = GEM_TBQP(hw_q - 1);
3479 queue->RBQP = GEM_RBQP(hw_q - 1);
3480 queue->RBQS = GEM_RBQS(hw_q - 1);
3481 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3482 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3483 queue->TBQPH = GEM_TBQPH(hw_q - 1);
3484 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3488 /* queue0 uses legacy registers */
3489 queue->ISR = MACB_ISR;
3490 queue->IER = MACB_IER;
3491 queue->IDR = MACB_IDR;
3492 queue->IMR = MACB_IMR;
3493 queue->TBQP = MACB_TBQP;
3494 queue->RBQP = MACB_RBQP;
3495 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3496 if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3497 queue->TBQPH = MACB_TBQPH;
3498 queue->RBQPH = MACB_RBQPH;
3503 /* get irq: here we use the linux queue index, not the hardware
3504 * queue index. the queue irq definitions in the device tree
3505 * must remove the optional gaps that could exist in the
3506 * hardware queue mask.
3508 queue->irq = platform_get_irq(pdev, q);
3509 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
3510 IRQF_SHARED, dev->name, queue);
3513 "Unable to request IRQ %d (error %d)\n",
3518 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
3522 dev->netdev_ops = &macb_netdev_ops;
3524 /* setup appropriated routines according to adapter type */
3525 if (macb_is_gem(bp)) {
3526 bp->max_tx_length = GEM_MAX_TX_LEN;
3527 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3528 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3529 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3530 bp->macbgem_ops.mog_rx = gem_rx;
3531 dev->ethtool_ops = &gem_ethtool_ops;
3533 bp->max_tx_length = MACB_MAX_TX_LEN;
3534 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3535 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3536 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3537 bp->macbgem_ops.mog_rx = macb_rx;
3538 dev->ethtool_ops = &macb_ethtool_ops;
3542 dev->hw_features = NETIF_F_SG;
3544 /* Check LSO capability */
3545 if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3546 dev->hw_features |= MACB_NETIF_LSO;
3548 /* Checksum offload is only available on gem with packet buffer */
3549 if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
3550 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3551 if (bp->caps & MACB_CAPS_SG_DISABLED)
3552 dev->hw_features &= ~NETIF_F_SG;
3553 dev->features = dev->hw_features;
3555 /* Check RX Flow Filters support.
3556 * Max Rx flows set by availability of screeners & compare regs:
3557 * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3559 reg = gem_readl(bp, DCFG8);
3560 bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3561 GEM_BFEXT(T2SCR, reg));
3562 if (bp->max_tuples > 0) {
3563 /* also needs one ethtype match to check IPv4 */
3564 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3565 /* program this reg now */
3567 reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3568 gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3569 /* Filtering is supported in hw but don't enable it in kernel now */
3570 dev->hw_features |= NETIF_F_NTUPLE;
3571 /* init Rx flow definitions */
3572 INIT_LIST_HEAD(&bp->rx_fs_list.list);
3573 bp->rx_fs_list.count = 0;
3574 spin_lock_init(&bp->rx_fs_lock);
3579 if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3581 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3582 val = GEM_BIT(RGMII);
3583 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
3584 (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3585 val = MACB_BIT(RMII);
3586 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3587 val = MACB_BIT(MII);
3589 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3590 val |= MACB_BIT(CLKEN);
3592 macb_or_gem_writel(bp, USRIO, val);
3595 /* Set MII management clock divider */
3596 val = macb_mdc_clk_div(bp);
3597 val |= macb_dbw(bp);
3598 if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3599 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
3600 macb_writel(bp, NCFGR, val);
3605 #if defined(CONFIG_OF)
3606 /* 1518 rounded up */
3607 #define AT91ETHER_MAX_RBUFF_SZ 0x600
3608 /* max number of receive buffers */
3609 #define AT91ETHER_MAX_RX_DESCR 9
3611 /* Initialize and start the Receiver and Transmit subsystems */
3612 static int at91ether_start(struct net_device *dev)
3614 struct macb *lp = netdev_priv(dev);
3615 struct macb_queue *q = &lp->queues[0];
3616 struct macb_dma_desc *desc;
3621 q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
3622 (AT91ETHER_MAX_RX_DESCR *
3623 macb_dma_desc_get_size(lp)),
3624 &q->rx_ring_dma, GFP_KERNEL);
3628 q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
3629 AT91ETHER_MAX_RX_DESCR *
3630 AT91ETHER_MAX_RBUFF_SZ,
3631 &q->rx_buffers_dma, GFP_KERNEL);
3632 if (!q->rx_buffers) {
3633 dma_free_coherent(&lp->pdev->dev,
3634 AT91ETHER_MAX_RX_DESCR *
3635 macb_dma_desc_get_size(lp),
3636 q->rx_ring, q->rx_ring_dma);
3641 addr = q->rx_buffers_dma;
3642 for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
3643 desc = macb_rx_desc(q, i);
3644 macb_set_addr(lp, desc, addr);
3646 addr += AT91ETHER_MAX_RBUFF_SZ;
3649 /* Set the Wrap bit on the last descriptor */
3650 desc->addr |= MACB_BIT(RX_WRAP);
3652 /* Reset buffer index */
3655 /* Program address of descriptor list in Rx Buffer Queue register */
3656 macb_writel(lp, RBQP, q->rx_ring_dma);
3658 /* Enable Receive and Transmit */
3659 ctl = macb_readl(lp, NCR);
3660 macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3665 /* Open the ethernet interface */
3666 static int at91ether_open(struct net_device *dev)
3668 struct macb *lp = netdev_priv(dev);
3672 /* Clear internal statistics */
3673 ctl = macb_readl(lp, NCR);
3674 macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3676 macb_set_hwaddr(lp);
3678 ret = at91ether_start(dev);
3682 /* Enable MAC interrupts */
3683 macb_writel(lp, IER, MACB_BIT(RCOMP) |
3685 MACB_BIT(ISR_TUND) |
3688 MACB_BIT(ISR_ROVR) |
3691 /* schedule a link state check */
3692 phy_start(dev->phydev);
3694 netif_start_queue(dev);
3699 /* Close the interface */
3700 static int at91ether_close(struct net_device *dev)
3702 struct macb *lp = netdev_priv(dev);
3703 struct macb_queue *q = &lp->queues[0];
3706 /* Disable Receiver and Transmitter */
3707 ctl = macb_readl(lp, NCR);
3708 macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3710 /* Disable MAC interrupts */
3711 macb_writel(lp, IDR, MACB_BIT(RCOMP) |
3713 MACB_BIT(ISR_TUND) |
3716 MACB_BIT(ISR_ROVR) |
3719 netif_stop_queue(dev);
3721 dma_free_coherent(&lp->pdev->dev,
3722 AT91ETHER_MAX_RX_DESCR *
3723 macb_dma_desc_get_size(lp),
3724 q->rx_ring, q->rx_ring_dma);
3727 dma_free_coherent(&lp->pdev->dev,
3728 AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3729 q->rx_buffers, q->rx_buffers_dma);
3730 q->rx_buffers = NULL;
3735 /* Transmit packet */
3736 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3737 struct net_device *dev)
3739 struct macb *lp = netdev_priv(dev);
3741 if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3742 netif_stop_queue(dev);
3744 /* Store packet information (to free when Tx completed) */
3746 lp->skb_length = skb->len;
3747 lp->skb_physaddr = dma_map_single(&lp->pdev->dev, skb->data,
3748 skb->len, DMA_TO_DEVICE);
3749 if (dma_mapping_error(&lp->pdev->dev, lp->skb_physaddr)) {
3750 dev_kfree_skb_any(skb);
3751 dev->stats.tx_dropped++;
3752 netdev_err(dev, "%s: DMA mapping error\n", __func__);
3753 return NETDEV_TX_OK;
3756 /* Set address of the data in the Transmit Address register */
3757 macb_writel(lp, TAR, lp->skb_physaddr);
3758 /* Set length of the packet in the Transmit Control register */
3759 macb_writel(lp, TCR, skb->len);
3762 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3763 return NETDEV_TX_BUSY;
3766 return NETDEV_TX_OK;
3769 /* Extract received frame from buffer descriptors and sent to upper layers.
3770 * (Called from interrupt context)
3772 static void at91ether_rx(struct net_device *dev)
3774 struct macb *lp = netdev_priv(dev);
3775 struct macb_queue *q = &lp->queues[0];
3776 struct macb_dma_desc *desc;
3777 unsigned char *p_recv;
3778 struct sk_buff *skb;
3779 unsigned int pktlen;
3781 desc = macb_rx_desc(q, q->rx_tail);
3782 while (desc->addr & MACB_BIT(RX_USED)) {
3783 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
3784 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
3785 skb = netdev_alloc_skb(dev, pktlen + 2);
3787 skb_reserve(skb, 2);
3788 skb_put_data(skb, p_recv, pktlen);
3790 skb->protocol = eth_type_trans(skb, dev);
3791 dev->stats.rx_packets++;
3792 dev->stats.rx_bytes += pktlen;
3795 dev->stats.rx_dropped++;
3798 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
3799 dev->stats.multicast++;
3801 /* reset ownership bit */
3802 desc->addr &= ~MACB_BIT(RX_USED);
3804 /* wrap after last buffer */
3805 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3810 desc = macb_rx_desc(q, q->rx_tail);
3814 /* MAC interrupt handler */
3815 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3817 struct net_device *dev = dev_id;
3818 struct macb *lp = netdev_priv(dev);
3821 /* MAC Interrupt Status register indicates what interrupts are pending.
3822 * It is automatically cleared once read.
3824 intstatus = macb_readl(lp, ISR);
3826 /* Receive complete */
3827 if (intstatus & MACB_BIT(RCOMP))
3830 /* Transmit complete */
3831 if (intstatus & MACB_BIT(TCOMP)) {
3832 /* The TCOM bit is set even if the transmission failed */
3833 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
3834 dev->stats.tx_errors++;
3837 dev_consume_skb_irq(lp->skb);
3839 dma_unmap_single(&lp->pdev->dev, lp->skb_physaddr,
3840 lp->skb_length, DMA_TO_DEVICE);
3841 dev->stats.tx_packets++;
3842 dev->stats.tx_bytes += lp->skb_length;
3844 netif_wake_queue(dev);
3847 /* Work-around for EMAC Errata section 41.3.1 */
3848 if (intstatus & MACB_BIT(RXUBR)) {
3849 ctl = macb_readl(lp, NCR);
3850 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
3852 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3855 if (intstatus & MACB_BIT(ISR_ROVR))
3856 netdev_err(dev, "ROVR error\n");
3861 #ifdef CONFIG_NET_POLL_CONTROLLER
3862 static void at91ether_poll_controller(struct net_device *dev)
3864 unsigned long flags;
3866 local_irq_save(flags);
3867 at91ether_interrupt(dev->irq, dev);
3868 local_irq_restore(flags);
3872 static const struct net_device_ops at91ether_netdev_ops = {
3873 .ndo_open = at91ether_open,
3874 .ndo_stop = at91ether_close,
3875 .ndo_start_xmit = at91ether_start_xmit,
3876 .ndo_get_stats = macb_get_stats,
3877 .ndo_set_rx_mode = macb_set_rx_mode,
3878 .ndo_set_mac_address = eth_mac_addr,
3879 .ndo_do_ioctl = macb_ioctl,
3880 .ndo_validate_addr = eth_validate_addr,
3881 #ifdef CONFIG_NET_POLL_CONTROLLER
3882 .ndo_poll_controller = at91ether_poll_controller,
3886 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
3887 struct clk **hclk, struct clk **tx_clk,
3888 struct clk **rx_clk, struct clk **tsu_clk)
3897 *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3899 return PTR_ERR(*pclk);
3901 err = clk_prepare_enable(*pclk);
3903 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
3910 static int at91ether_init(struct platform_device *pdev)
3912 struct net_device *dev = platform_get_drvdata(pdev);
3913 struct macb *bp = netdev_priv(dev);
3917 bp->queues[0].bp = bp;
3919 dev->netdev_ops = &at91ether_netdev_ops;
3920 dev->ethtool_ops = &macb_ethtool_ops;
3922 err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3927 macb_writel(bp, NCR, 0);
3929 reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3930 if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3931 reg |= MACB_BIT(RM9200_RMII);
3933 macb_writel(bp, NCFGR, reg);
3938 static const struct macb_config at91sam9260_config = {
3939 .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3940 .clk_init = macb_clk_init,
3944 static const struct macb_config sama5d3macb_config = {
3945 .caps = MACB_CAPS_SG_DISABLED
3946 | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3947 .clk_init = macb_clk_init,
3951 static const struct macb_config pc302gem_config = {
3952 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3953 .dma_burst_length = 16,
3954 .clk_init = macb_clk_init,
3958 static const struct macb_config sama5d2_config = {
3959 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3960 .dma_burst_length = 16,
3961 .clk_init = macb_clk_init,
3965 static const struct macb_config sama5d3_config = {
3966 .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3967 | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
3968 .dma_burst_length = 16,
3969 .clk_init = macb_clk_init,
3971 .jumbo_max_len = 10240,
3974 static const struct macb_config sama5d4_config = {
3975 .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3976 .dma_burst_length = 4,
3977 .clk_init = macb_clk_init,
3981 static const struct macb_config emac_config = {
3982 .caps = MACB_CAPS_NEEDS_RSTONUBR,
3983 .clk_init = at91ether_clk_init,
3984 .init = at91ether_init,
3987 static const struct macb_config np4_config = {
3988 .caps = MACB_CAPS_USRIO_DISABLED,
3989 .clk_init = macb_clk_init,
3993 static const struct macb_config zynqmp_config = {
3994 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3996 MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
3997 .dma_burst_length = 16,
3998 .clk_init = macb_clk_init,
4000 .jumbo_max_len = 10240,
4003 static const struct macb_config zynq_config = {
4004 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
4005 MACB_CAPS_NEEDS_RSTONUBR,
4006 .dma_burst_length = 16,
4007 .clk_init = macb_clk_init,
4011 static const struct of_device_id macb_dt_ids[] = {
4012 { .compatible = "cdns,at32ap7000-macb" },
4013 { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
4014 { .compatible = "cdns,macb" },
4015 { .compatible = "cdns,np4-macb", .data = &np4_config },
4016 { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
4017 { .compatible = "cdns,gem", .data = &pc302gem_config },
4018 { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
4019 { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
4020 { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
4021 { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
4022 { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4023 { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4024 { .compatible = "cdns,emac", .data = &emac_config },
4025 { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
4026 { .compatible = "cdns,zynq-gem", .data = &zynq_config },
4029 MODULE_DEVICE_TABLE(of, macb_dt_ids);
4030 #endif /* CONFIG_OF */
4032 static const struct macb_config default_gem_config = {
4033 .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4035 MACB_CAPS_GEM_HAS_PTP,
4036 .dma_burst_length = 16,
4037 .clk_init = macb_clk_init,
4039 .jumbo_max_len = 10240,
4042 static int macb_probe(struct platform_device *pdev)
4044 const struct macb_config *macb_config = &default_gem_config;
4045 int (*clk_init)(struct platform_device *, struct clk **,
4046 struct clk **, struct clk **, struct clk **,
4047 struct clk **) = macb_config->clk_init;
4048 int (*init)(struct platform_device *) = macb_config->init;
4049 struct device_node *np = pdev->dev.of_node;
4050 struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
4051 struct clk *tsu_clk = NULL;
4052 unsigned int queue_mask, num_queues;
4053 struct macb_platform_data *pdata;
4055 struct phy_device *phydev;
4056 struct net_device *dev;
4057 struct resource *regs;
4063 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4064 mem = devm_ioremap_resource(&pdev->dev, regs);
4066 return PTR_ERR(mem);
4069 const struct of_device_id *match;
4071 match = of_match_node(macb_dt_ids, np);
4072 if (match && match->data) {
4073 macb_config = match->data;
4074 clk_init = macb_config->clk_init;
4075 init = macb_config->init;
4079 err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
4083 pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
4084 pm_runtime_use_autosuspend(&pdev->dev);
4085 pm_runtime_get_noresume(&pdev->dev);
4086 pm_runtime_set_active(&pdev->dev);
4087 pm_runtime_enable(&pdev->dev);
4088 native_io = hw_is_native_io(mem);
4090 macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
4091 dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
4094 goto err_disable_clocks;
4097 dev->base_addr = regs->start;
4099 SET_NETDEV_DEV(dev, &pdev->dev);
4101 bp = netdev_priv(dev);
4105 bp->native_io = native_io;
4107 bp->macb_reg_readl = hw_readl_native;
4108 bp->macb_reg_writel = hw_writel_native;
4110 bp->macb_reg_readl = hw_readl;
4111 bp->macb_reg_writel = hw_writel;
4113 bp->num_queues = num_queues;
4114 bp->queue_mask = queue_mask;
4116 bp->dma_burst_length = macb_config->dma_burst_length;
4119 bp->tx_clk = tx_clk;
4120 bp->rx_clk = rx_clk;
4121 bp->tsu_clk = tsu_clk;
4123 bp->jumbo_max_len = macb_config->jumbo_max_len;
4126 if (of_get_property(np, "magic-packet", NULL))
4127 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4128 device_init_wakeup(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4130 spin_lock_init(&bp->lock);
4132 /* setup capabilities */
4133 macb_configure_caps(bp, macb_config);
4135 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4136 if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4137 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
4138 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4141 platform_set_drvdata(pdev, dev);
4143 dev->irq = platform_get_irq(pdev, 0);
4146 goto err_out_free_netdev;
4149 /* MTU range: 68 - 1500 or 10240 */
4150 dev->min_mtu = GEM_MTU_MIN_SIZE;
4151 if (bp->caps & MACB_CAPS_JUMBO)
4152 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4154 dev->max_mtu = ETH_DATA_LEN;
4156 if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4157 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4159 bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4160 macb_dma_desc_get_size(bp);
4162 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4164 bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4165 macb_dma_desc_get_size(bp);
4168 bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4169 if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4170 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4172 mac = of_get_mac_address(np);
4174 ether_addr_copy(bp->dev->dev_addr, mac);
4176 err = nvmem_get_mac_address(&pdev->dev, bp->dev->dev_addr);
4178 if (err == -EPROBE_DEFER)
4179 goto err_out_free_netdev;
4180 macb_get_hwaddr(bp);
4184 err = of_get_phy_mode(np);
4186 pdata = dev_get_platdata(&pdev->dev);
4187 if (pdata && pdata->is_rmii)
4188 bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4190 bp->phy_interface = PHY_INTERFACE_MODE_MII;
4192 bp->phy_interface = err;
4195 /* IP specific init */
4198 goto err_out_free_netdev;
4200 err = macb_mii_init(bp);
4202 goto err_out_free_netdev;
4204 phydev = dev->phydev;
4206 netif_carrier_off(dev);
4208 err = register_netdev(dev);
4210 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
4211 goto err_out_unregister_mdio;
4214 tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4217 phy_attached_info(phydev);
4219 netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4220 macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4221 dev->base_addr, dev->irq, dev->dev_addr);
4223 pm_runtime_mark_last_busy(&bp->pdev->dev);
4224 pm_runtime_put_autosuspend(&bp->pdev->dev);
4228 err_out_unregister_mdio:
4229 phy_disconnect(dev->phydev);
4230 mdiobus_unregister(bp->mii_bus);
4231 of_node_put(bp->phy_node);
4232 if (np && of_phy_is_fixed_link(np))
4233 of_phy_deregister_fixed_link(np);
4234 mdiobus_free(bp->mii_bus);
4236 err_out_free_netdev:
4240 clk_disable_unprepare(tx_clk);
4241 clk_disable_unprepare(hclk);
4242 clk_disable_unprepare(pclk);
4243 clk_disable_unprepare(rx_clk);
4244 clk_disable_unprepare(tsu_clk);
4245 pm_runtime_disable(&pdev->dev);
4246 pm_runtime_set_suspended(&pdev->dev);
4247 pm_runtime_dont_use_autosuspend(&pdev->dev);
4252 static int macb_remove(struct platform_device *pdev)
4254 struct net_device *dev;
4256 struct device_node *np = pdev->dev.of_node;
4258 dev = platform_get_drvdata(pdev);
4261 bp = netdev_priv(dev);
4263 phy_disconnect(dev->phydev);
4264 mdiobus_unregister(bp->mii_bus);
4265 if (np && of_phy_is_fixed_link(np))
4266 of_phy_deregister_fixed_link(np);
4268 mdiobus_free(bp->mii_bus);
4270 unregister_netdev(dev);
4271 pm_runtime_disable(&pdev->dev);
4272 pm_runtime_dont_use_autosuspend(&pdev->dev);
4273 if (!pm_runtime_suspended(&pdev->dev)) {
4274 clk_disable_unprepare(bp->tx_clk);
4275 clk_disable_unprepare(bp->hclk);
4276 clk_disable_unprepare(bp->pclk);
4277 clk_disable_unprepare(bp->rx_clk);
4278 clk_disable_unprepare(bp->tsu_clk);
4279 pm_runtime_set_suspended(&pdev->dev);
4281 of_node_put(bp->phy_node);
4288 static int __maybe_unused macb_suspend(struct device *dev)
4290 struct net_device *netdev = dev_get_drvdata(dev);
4291 struct macb *bp = netdev_priv(netdev);
4292 struct macb_queue *queue = bp->queues;
4293 unsigned long flags;
4296 if (!netif_running(netdev))
4300 if (bp->wol & MACB_WOL_ENABLED) {
4301 macb_writel(bp, IER, MACB_BIT(WOL));
4302 macb_writel(bp, WOL, MACB_BIT(MAG));
4303 enable_irq_wake(bp->queues[0].irq);
4304 netif_device_detach(netdev);
4306 netif_device_detach(netdev);
4307 for (q = 0, queue = bp->queues; q < bp->num_queues;
4309 napi_disable(&queue->napi);
4310 phy_stop(netdev->phydev);
4311 phy_suspend(netdev->phydev);
4312 spin_lock_irqsave(&bp->lock, flags);
4314 spin_unlock_irqrestore(&bp->lock, flags);
4317 netif_carrier_off(netdev);
4319 bp->ptp_info->ptp_remove(netdev);
4320 pm_runtime_force_suspend(dev);
4325 static int __maybe_unused macb_resume(struct device *dev)
4327 struct net_device *netdev = dev_get_drvdata(dev);
4328 struct macb *bp = netdev_priv(netdev);
4329 struct macb_queue *queue = bp->queues;
4332 if (!netif_running(netdev))
4335 pm_runtime_force_resume(dev);
4337 if (bp->wol & MACB_WOL_ENABLED) {
4338 macb_writel(bp, IDR, MACB_BIT(WOL));
4339 macb_writel(bp, WOL, 0);
4340 disable_irq_wake(bp->queues[0].irq);
4342 macb_writel(bp, NCR, MACB_BIT(MPE));
4343 for (q = 0, queue = bp->queues; q < bp->num_queues;
4345 napi_enable(&queue->napi);
4346 phy_resume(netdev->phydev);
4347 phy_init_hw(netdev->phydev);
4348 phy_start(netdev->phydev);
4351 bp->macbgem_ops.mog_init_rings(bp);
4353 macb_set_rx_mode(netdev);
4354 netif_device_attach(netdev);
4356 bp->ptp_info->ptp_init(netdev);
4361 static int __maybe_unused macb_runtime_suspend(struct device *dev)
4363 struct platform_device *pdev = to_platform_device(dev);
4364 struct net_device *netdev = platform_get_drvdata(pdev);
4365 struct macb *bp = netdev_priv(netdev);
4367 if (!(device_may_wakeup(&bp->dev->dev))) {
4368 clk_disable_unprepare(bp->tx_clk);
4369 clk_disable_unprepare(bp->hclk);
4370 clk_disable_unprepare(bp->pclk);
4371 clk_disable_unprepare(bp->rx_clk);
4373 clk_disable_unprepare(bp->tsu_clk);
4378 static int __maybe_unused macb_runtime_resume(struct device *dev)
4380 struct platform_device *pdev = to_platform_device(dev);
4381 struct net_device *netdev = platform_get_drvdata(pdev);
4382 struct macb *bp = netdev_priv(netdev);
4384 if (!(device_may_wakeup(&bp->dev->dev))) {
4385 clk_prepare_enable(bp->pclk);
4386 clk_prepare_enable(bp->hclk);
4387 clk_prepare_enable(bp->tx_clk);
4388 clk_prepare_enable(bp->rx_clk);
4390 clk_prepare_enable(bp->tsu_clk);
4395 static const struct dev_pm_ops macb_pm_ops = {
4396 SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
4397 SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
4400 static struct platform_driver macb_driver = {
4401 .probe = macb_probe,
4402 .remove = macb_remove,
4405 .of_match_table = of_match_ptr(macb_dt_ids),
4410 module_platform_driver(macb_driver);
4412 MODULE_LICENSE("GPL");
4413 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
4414 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4415 MODULE_ALIAS("platform:macb");