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