GNU Linux-libre 4.19.264-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                 desc = macb_rx_desc(queue, entry);
919
920                 if (!queue->rx_skbuff[entry]) {
921                         /* allocate sk_buff for this free entry in ring */
922                         skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
923                         if (unlikely(!skb)) {
924                                 netdev_err(bp->dev,
925                                            "Unable to allocate sk_buff\n");
926                                 break;
927                         }
928
929                         /* now fill corresponding descriptor entry */
930                         paddr = dma_map_single(&bp->pdev->dev, skb->data,
931                                                bp->rx_buffer_size,
932                                                DMA_FROM_DEVICE);
933                         if (dma_mapping_error(&bp->pdev->dev, paddr)) {
934                                 dev_kfree_skb(skb);
935                                 break;
936                         }
937
938                         queue->rx_skbuff[entry] = skb;
939
940                         if (entry == bp->rx_ring_size - 1)
941                                 paddr |= MACB_BIT(RX_WRAP);
942                         desc->ctrl = 0;
943                         /* Setting addr clears RX_USED and allows reception,
944                          * make sure ctrl is cleared first to avoid a race.
945                          */
946                         dma_wmb();
947                         macb_set_addr(bp, desc, paddr);
948
949                         /* properly align Ethernet header */
950                         skb_reserve(skb, NET_IP_ALIGN);
951                 } else {
952                         desc->ctrl = 0;
953                         dma_wmb();
954                         desc->addr &= ~MACB_BIT(RX_USED);
955                 }
956                 queue->rx_prepared_head++;
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                 /* RSR bits only seem to propagate to raise interrupts when
1273                  * interrupts are enabled at the time, so if bits are already
1274                  * set due to packets received while interrupts were disabled,
1275                  * they will not cause another interrupt to be generated when
1276                  * interrupts are re-enabled.
1277                  * Check for this case here. This has been seen to happen
1278                  * around 30% of the time under heavy network load.
1279                  */
1280                 status = macb_readl(bp, RSR);
1281                 if (status) {
1282                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1283                                 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1284                         napi_reschedule(napi);
1285                 } else {
1286                         queue_writel(queue, IER, bp->rx_intr_mask);
1287
1288                         /* In rare cases, packets could have been received in
1289                          * the window between the check above and re-enabling
1290                          * interrupts. Therefore, a double-check is required
1291                          * to avoid losing a wakeup. This can potentially race
1292                          * with the interrupt handler doing the same actions
1293                          * if an interrupt is raised just after enabling them,
1294                          * but this should be harmless.
1295                          */
1296                         status = macb_readl(bp, RSR);
1297                         if (unlikely(status)) {
1298                                 queue_writel(queue, IDR, bp->rx_intr_mask);
1299                                 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1300                                         queue_writel(queue, ISR, MACB_BIT(RCOMP));
1301                                 napi_schedule(napi);
1302                         }
1303                 }
1304         }
1305
1306         /* TODO: Handle errors */
1307
1308         return work_done;
1309 }
1310
1311 static void macb_hresp_error_task(unsigned long data)
1312 {
1313         struct macb *bp = (struct macb *)data;
1314         struct net_device *dev = bp->dev;
1315         struct macb_queue *queue = bp->queues;
1316         unsigned int q;
1317         u32 ctrl;
1318
1319         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1320                 queue_writel(queue, IDR, bp->rx_intr_mask |
1321                                          MACB_TX_INT_FLAGS |
1322                                          MACB_BIT(HRESP));
1323         }
1324         ctrl = macb_readl(bp, NCR);
1325         ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
1326         macb_writel(bp, NCR, ctrl);
1327
1328         netif_tx_stop_all_queues(dev);
1329         netif_carrier_off(dev);
1330
1331         bp->macbgem_ops.mog_init_rings(bp);
1332
1333         /* Initialize TX and RX buffers */
1334         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1335                 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
1336 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1337                 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1338                         queue_writel(queue, RBQPH,
1339                                      upper_32_bits(queue->rx_ring_dma));
1340 #endif
1341                 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
1342 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1343                 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
1344                         queue_writel(queue, TBQPH,
1345                                      upper_32_bits(queue->tx_ring_dma));
1346 #endif
1347
1348                 /* Enable interrupts */
1349                 queue_writel(queue, IER,
1350                              bp->rx_intr_mask |
1351                              MACB_TX_INT_FLAGS |
1352                              MACB_BIT(HRESP));
1353         }
1354
1355         ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
1356         macb_writel(bp, NCR, ctrl);
1357
1358         netif_carrier_on(dev);
1359         netif_tx_start_all_queues(dev);
1360 }
1361
1362 static void macb_tx_restart(struct macb_queue *queue)
1363 {
1364         unsigned int head = queue->tx_head;
1365         unsigned int tail = queue->tx_tail;
1366         struct macb *bp = queue->bp;
1367         unsigned int head_idx, tbqp;
1368
1369         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1370                 queue_writel(queue, ISR, MACB_BIT(TXUBR));
1371
1372         if (head == tail)
1373                 return;
1374
1375         tbqp = queue_readl(queue, TBQP) / macb_dma_desc_get_size(bp);
1376         tbqp = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, tbqp));
1377         head_idx = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, head));
1378
1379         if (tbqp == head_idx)
1380                 return;
1381
1382         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1383 }
1384
1385 static irqreturn_t macb_interrupt(int irq, void *dev_id)
1386 {
1387         struct macb_queue *queue = dev_id;
1388         struct macb *bp = queue->bp;
1389         struct net_device *dev = bp->dev;
1390         u32 status, ctrl;
1391
1392         status = queue_readl(queue, ISR);
1393
1394         if (unlikely(!status))
1395                 return IRQ_NONE;
1396
1397         spin_lock(&bp->lock);
1398
1399         while (status) {
1400                 /* close possible race with dev_close */
1401                 if (unlikely(!netif_running(dev))) {
1402                         queue_writel(queue, IDR, -1);
1403                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1404                                 queue_writel(queue, ISR, -1);
1405                         break;
1406                 }
1407
1408                 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1409                             (unsigned int)(queue - bp->queues),
1410                             (unsigned long)status);
1411
1412                 if (status & bp->rx_intr_mask) {
1413                         /* There's no point taking any more interrupts
1414                          * until we have processed the buffers. The
1415                          * scheduling call may fail if the poll routine
1416                          * is already scheduled, so disable interrupts
1417                          * now.
1418                          */
1419                         queue_writel(queue, IDR, bp->rx_intr_mask);
1420                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1421                                 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1422
1423                         if (napi_schedule_prep(&queue->napi)) {
1424                                 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1425                                 __napi_schedule(&queue->napi);
1426                         }
1427                 }
1428
1429                 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1430                         queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1431                         schedule_work(&queue->tx_error_task);
1432
1433                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1434                                 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1435
1436                         break;
1437                 }
1438
1439                 if (status & MACB_BIT(TCOMP))
1440                         macb_tx_interrupt(queue);
1441
1442                 if (status & MACB_BIT(TXUBR))
1443                         macb_tx_restart(queue);
1444
1445                 /* Link change detection isn't possible with RMII, so we'll
1446                  * add that if/when we get our hands on a full-blown MII PHY.
1447                  */
1448
1449                 /* There is a hardware issue under heavy load where DMA can
1450                  * stop, this causes endless "used buffer descriptor read"
1451                  * interrupts but it can be cleared by re-enabling RX. See
1452                  * the at91rm9200 manual, section 41.3.1 or the Zynq manual
1453                  * section 16.7.4 for details. RXUBR is only enabled for
1454                  * these two versions.
1455                  */
1456                 if (status & MACB_BIT(RXUBR)) {
1457                         ctrl = macb_readl(bp, NCR);
1458                         macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1459                         wmb();
1460                         macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1461
1462                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1463                                 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1464                 }
1465
1466                 if (status & MACB_BIT(ISR_ROVR)) {
1467                         /* We missed at least one packet */
1468                         if (macb_is_gem(bp))
1469                                 bp->hw_stats.gem.rx_overruns++;
1470                         else
1471                                 bp->hw_stats.macb.rx_overruns++;
1472
1473                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1474                                 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1475                 }
1476
1477                 if (status & MACB_BIT(HRESP)) {
1478                         tasklet_schedule(&bp->hresp_err_tasklet);
1479                         netdev_err(dev, "DMA bus error: HRESP not OK\n");
1480
1481                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1482                                 queue_writel(queue, ISR, MACB_BIT(HRESP));
1483                 }
1484                 status = queue_readl(queue, ISR);
1485         }
1486
1487         spin_unlock(&bp->lock);
1488
1489         return IRQ_HANDLED;
1490 }
1491
1492 #ifdef CONFIG_NET_POLL_CONTROLLER
1493 /* Polling receive - used by netconsole and other diagnostic tools
1494  * to allow network i/o with interrupts disabled.
1495  */
1496 static void macb_poll_controller(struct net_device *dev)
1497 {
1498         struct macb *bp = netdev_priv(dev);
1499         struct macb_queue *queue;
1500         unsigned long flags;
1501         unsigned int q;
1502
1503         local_irq_save(flags);
1504         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1505                 macb_interrupt(dev->irq, queue);
1506         local_irq_restore(flags);
1507 }
1508 #endif
1509
1510 static unsigned int macb_tx_map(struct macb *bp,
1511                                 struct macb_queue *queue,
1512                                 struct sk_buff *skb,
1513                                 unsigned int hdrlen)
1514 {
1515         dma_addr_t mapping;
1516         unsigned int len, entry, i, tx_head = queue->tx_head;
1517         struct macb_tx_skb *tx_skb = NULL;
1518         struct macb_dma_desc *desc;
1519         unsigned int offset, size, count = 0;
1520         unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1521         unsigned int eof = 1, mss_mfs = 0;
1522         u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
1523
1524         /* LSO */
1525         if (skb_shinfo(skb)->gso_size != 0) {
1526                 if (ip_hdr(skb)->protocol == IPPROTO_UDP)
1527                         /* UDP - UFO */
1528                         lso_ctrl = MACB_LSO_UFO_ENABLE;
1529                 else
1530                         /* TCP - TSO */
1531                         lso_ctrl = MACB_LSO_TSO_ENABLE;
1532         }
1533
1534         /* First, map non-paged data */
1535         len = skb_headlen(skb);
1536
1537         /* first buffer length */
1538         size = hdrlen;
1539
1540         offset = 0;
1541         while (len) {
1542                 entry = macb_tx_ring_wrap(bp, tx_head);
1543                 tx_skb = &queue->tx_skb[entry];
1544
1545                 mapping = dma_map_single(&bp->pdev->dev,
1546                                          skb->data + offset,
1547                                          size, DMA_TO_DEVICE);
1548                 if (dma_mapping_error(&bp->pdev->dev, mapping))
1549                         goto dma_error;
1550
1551                 /* Save info to properly release resources */
1552                 tx_skb->skb = NULL;
1553                 tx_skb->mapping = mapping;
1554                 tx_skb->size = size;
1555                 tx_skb->mapped_as_page = false;
1556
1557                 len -= size;
1558                 offset += size;
1559                 count++;
1560                 tx_head++;
1561
1562                 size = min(len, bp->max_tx_length);
1563         }
1564
1565         /* Then, map paged data from fragments */
1566         for (f = 0; f < nr_frags; f++) {
1567                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1568
1569                 len = skb_frag_size(frag);
1570                 offset = 0;
1571                 while (len) {
1572                         size = min(len, bp->max_tx_length);
1573                         entry = macb_tx_ring_wrap(bp, tx_head);
1574                         tx_skb = &queue->tx_skb[entry];
1575
1576                         mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1577                                                    offset, size, DMA_TO_DEVICE);
1578                         if (dma_mapping_error(&bp->pdev->dev, mapping))
1579                                 goto dma_error;
1580
1581                         /* Save info to properly release resources */
1582                         tx_skb->skb = NULL;
1583                         tx_skb->mapping = mapping;
1584                         tx_skb->size = size;
1585                         tx_skb->mapped_as_page = true;
1586
1587                         len -= size;
1588                         offset += size;
1589                         count++;
1590                         tx_head++;
1591                 }
1592         }
1593
1594         /* Should never happen */
1595         if (unlikely(!tx_skb)) {
1596                 netdev_err(bp->dev, "BUG! empty skb!\n");
1597                 return 0;
1598         }
1599
1600         /* This is the last buffer of the frame: save socket buffer */
1601         tx_skb->skb = skb;
1602
1603         /* Update TX ring: update buffer descriptors in reverse order
1604          * to avoid race condition
1605          */
1606
1607         /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1608          * to set the end of TX queue
1609          */
1610         i = tx_head;
1611         entry = macb_tx_ring_wrap(bp, i);
1612         ctrl = MACB_BIT(TX_USED);
1613         desc = macb_tx_desc(queue, entry);
1614         desc->ctrl = ctrl;
1615
1616         if (lso_ctrl) {
1617                 if (lso_ctrl == MACB_LSO_UFO_ENABLE)
1618                         /* include header and FCS in value given to h/w */
1619                         mss_mfs = skb_shinfo(skb)->gso_size +
1620                                         skb_transport_offset(skb) +
1621                                         ETH_FCS_LEN;
1622                 else /* TSO */ {
1623                         mss_mfs = skb_shinfo(skb)->gso_size;
1624                         /* TCP Sequence Number Source Select
1625                          * can be set only for TSO
1626                          */
1627                         seq_ctrl = 0;
1628                 }
1629         }
1630
1631         do {
1632                 i--;
1633                 entry = macb_tx_ring_wrap(bp, i);
1634                 tx_skb = &queue->tx_skb[entry];
1635                 desc = macb_tx_desc(queue, entry);
1636
1637                 ctrl = (u32)tx_skb->size;
1638                 if (eof) {
1639                         ctrl |= MACB_BIT(TX_LAST);
1640                         eof = 0;
1641                 }
1642                 if (unlikely(entry == (bp->tx_ring_size - 1)))
1643                         ctrl |= MACB_BIT(TX_WRAP);
1644
1645                 /* First descriptor is header descriptor */
1646                 if (i == queue->tx_head) {
1647                         ctrl |= MACB_BF(TX_LSO, lso_ctrl);
1648                         ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
1649                         if ((bp->dev->features & NETIF_F_HW_CSUM) &&
1650                             skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl)
1651                                 ctrl |= MACB_BIT(TX_NOCRC);
1652                 } else
1653                         /* Only set MSS/MFS on payload descriptors
1654                          * (second or later descriptor)
1655                          */
1656                         ctrl |= MACB_BF(MSS_MFS, mss_mfs);
1657
1658                 /* Set TX buffer descriptor */
1659                 macb_set_addr(bp, desc, tx_skb->mapping);
1660                 /* desc->addr must be visible to hardware before clearing
1661                  * 'TX_USED' bit in desc->ctrl.
1662                  */
1663                 wmb();
1664                 desc->ctrl = ctrl;
1665         } while (i != queue->tx_head);
1666
1667         queue->tx_head = tx_head;
1668
1669         return count;
1670
1671 dma_error:
1672         netdev_err(bp->dev, "TX DMA map failed\n");
1673
1674         for (i = queue->tx_head; i != tx_head; i++) {
1675                 tx_skb = macb_tx_skb(queue, i);
1676
1677                 macb_tx_unmap(bp, tx_skb);
1678         }
1679
1680         return 0;
1681 }
1682
1683 static netdev_features_t macb_features_check(struct sk_buff *skb,
1684                                              struct net_device *dev,
1685                                              netdev_features_t features)
1686 {
1687         unsigned int nr_frags, f;
1688         unsigned int hdrlen;
1689
1690         /* Validate LSO compatibility */
1691
1692         /* there is only one buffer or protocol is not UDP */
1693         if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
1694                 return features;
1695
1696         /* length of header */
1697         hdrlen = skb_transport_offset(skb);
1698
1699         /* For UFO only:
1700          * When software supplies two or more payload buffers all payload buffers
1701          * apart from the last must be a multiple of 8 bytes in size.
1702          */
1703         if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
1704                 return features & ~MACB_NETIF_LSO;
1705
1706         nr_frags = skb_shinfo(skb)->nr_frags;
1707         /* No need to check last fragment */
1708         nr_frags--;
1709         for (f = 0; f < nr_frags; f++) {
1710                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1711
1712                 if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
1713                         return features & ~MACB_NETIF_LSO;
1714         }
1715         return features;
1716 }
1717
1718 static inline int macb_clear_csum(struct sk_buff *skb)
1719 {
1720         /* no change for packets without checksum offloading */
1721         if (skb->ip_summed != CHECKSUM_PARTIAL)
1722                 return 0;
1723
1724         /* make sure we can modify the header */
1725         if (unlikely(skb_cow_head(skb, 0)))
1726                 return -1;
1727
1728         /* initialize checksum field
1729          * This is required - at least for Zynq, which otherwise calculates
1730          * wrong UDP header checksums for UDP packets with UDP data len <=2
1731          */
1732         *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1733         return 0;
1734 }
1735
1736 static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
1737 {
1738         bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
1739                       skb_is_nonlinear(*skb);
1740         int padlen = ETH_ZLEN - (*skb)->len;
1741         int headroom = skb_headroom(*skb);
1742         int tailroom = skb_tailroom(*skb);
1743         struct sk_buff *nskb;
1744         u32 fcs;
1745
1746         if (!(ndev->features & NETIF_F_HW_CSUM) ||
1747             !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
1748             skb_shinfo(*skb)->gso_size) /* Not available for GSO */
1749                 return 0;
1750
1751         if (padlen <= 0) {
1752                 /* FCS could be appeded to tailroom. */
1753                 if (tailroom >= ETH_FCS_LEN)
1754                         goto add_fcs;
1755                 /* FCS could be appeded by moving data to headroom. */
1756                 else if (!cloned && headroom + tailroom >= ETH_FCS_LEN)
1757                         padlen = 0;
1758                 /* No room for FCS, need to reallocate skb. */
1759                 else
1760                         padlen = ETH_FCS_LEN;
1761         } else {
1762                 /* Add room for FCS. */
1763                 padlen += ETH_FCS_LEN;
1764         }
1765
1766         if (!cloned && headroom + tailroom >= padlen) {
1767                 (*skb)->data = memmove((*skb)->head, (*skb)->data, (*skb)->len);
1768                 skb_set_tail_pointer(*skb, (*skb)->len);
1769         } else {
1770                 nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
1771                 if (!nskb)
1772                         return -ENOMEM;
1773
1774                 dev_kfree_skb_any(*skb);
1775                 *skb = nskb;
1776         }
1777
1778         if (padlen) {
1779                 if (padlen >= ETH_FCS_LEN)
1780                         skb_put_zero(*skb, padlen - ETH_FCS_LEN);
1781                 else
1782                         skb_trim(*skb, ETH_FCS_LEN - padlen);
1783         }
1784
1785 add_fcs:
1786         /* set FCS to packet */
1787         fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
1788         fcs = ~fcs;
1789
1790         skb_put_u8(*skb, fcs            & 0xff);
1791         skb_put_u8(*skb, (fcs >> 8)     & 0xff);
1792         skb_put_u8(*skb, (fcs >> 16)    & 0xff);
1793         skb_put_u8(*skb, (fcs >> 24)    & 0xff);
1794
1795         return 0;
1796 }
1797
1798 static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1799 {
1800         u16 queue_index = skb_get_queue_mapping(skb);
1801         struct macb *bp = netdev_priv(dev);
1802         struct macb_queue *queue = &bp->queues[queue_index];
1803         unsigned long flags;
1804         unsigned int desc_cnt, nr_frags, frag_size, f;
1805         unsigned int hdrlen;
1806         bool is_lso, is_udp = 0;
1807         netdev_tx_t ret = NETDEV_TX_OK;
1808
1809         if (macb_clear_csum(skb)) {
1810                 dev_kfree_skb_any(skb);
1811                 return ret;
1812         }
1813
1814         if (macb_pad_and_fcs(&skb, dev)) {
1815                 dev_kfree_skb_any(skb);
1816                 return ret;
1817         }
1818
1819         is_lso = (skb_shinfo(skb)->gso_size != 0);
1820
1821         if (is_lso) {
1822                 is_udp = !!(ip_hdr(skb)->protocol == IPPROTO_UDP);
1823
1824                 /* length of headers */
1825                 if (is_udp)
1826                         /* only queue eth + ip headers separately for UDP */
1827                         hdrlen = skb_transport_offset(skb);
1828                 else
1829                         hdrlen = skb_transport_offset(skb) + tcp_hdrlen(skb);
1830                 if (skb_headlen(skb) < hdrlen) {
1831                         netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
1832                         /* if this is required, would need to copy to single buffer */
1833                         return NETDEV_TX_BUSY;
1834                 }
1835         } else
1836                 hdrlen = min(skb_headlen(skb), bp->max_tx_length);
1837
1838 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1839         netdev_vdbg(bp->dev,
1840                     "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1841                     queue_index, skb->len, skb->head, skb->data,
1842                     skb_tail_pointer(skb), skb_end_pointer(skb));
1843         print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1844                        skb->data, 16, true);
1845 #endif
1846
1847         /* Count how many TX buffer descriptors are needed to send this
1848          * socket buffer: skb fragments of jumbo frames may need to be
1849          * split into many buffer descriptors.
1850          */
1851         if (is_lso && (skb_headlen(skb) > hdrlen))
1852                 /* extra header descriptor if also payload in first buffer */
1853                 desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
1854         else
1855                 desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
1856         nr_frags = skb_shinfo(skb)->nr_frags;
1857         for (f = 0; f < nr_frags; f++) {
1858                 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1859                 desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
1860         }
1861
1862         spin_lock_irqsave(&bp->lock, flags);
1863
1864         /* This is a hard error, log it. */
1865         if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
1866                        bp->tx_ring_size) < desc_cnt) {
1867                 netif_stop_subqueue(dev, queue_index);
1868                 spin_unlock_irqrestore(&bp->lock, flags);
1869                 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1870                            queue->tx_head, queue->tx_tail);
1871                 return NETDEV_TX_BUSY;
1872         }
1873
1874         /* Map socket buffer for DMA transfer */
1875         if (!macb_tx_map(bp, queue, skb, hdrlen)) {
1876                 dev_kfree_skb_any(skb);
1877                 goto unlock;
1878         }
1879
1880         /* Make newly initialized descriptor visible to hardware */
1881         wmb();
1882         skb_tx_timestamp(skb);
1883
1884         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1885
1886         if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
1887                 netif_stop_subqueue(dev, queue_index);
1888
1889 unlock:
1890         spin_unlock_irqrestore(&bp->lock, flags);
1891
1892         return ret;
1893 }
1894
1895 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1896 {
1897         if (!macb_is_gem(bp)) {
1898                 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1899         } else {
1900                 bp->rx_buffer_size = size;
1901
1902                 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1903                         netdev_dbg(bp->dev,
1904                                    "RX buffer must be multiple of %d bytes, expanding\n",
1905                                    RX_BUFFER_MULTIPLE);
1906                         bp->rx_buffer_size =
1907                                 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1908                 }
1909         }
1910
1911         netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
1912                    bp->dev->mtu, bp->rx_buffer_size);
1913 }
1914
1915 static void gem_free_rx_buffers(struct macb *bp)
1916 {
1917         struct sk_buff          *skb;
1918         struct macb_dma_desc    *desc;
1919         struct macb_queue *queue;
1920         dma_addr_t              addr;
1921         unsigned int q;
1922         int i;
1923
1924         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1925                 if (!queue->rx_skbuff)
1926                         continue;
1927
1928                 for (i = 0; i < bp->rx_ring_size; i++) {
1929                         skb = queue->rx_skbuff[i];
1930
1931                         if (!skb)
1932                                 continue;
1933
1934                         desc = macb_rx_desc(queue, i);
1935                         addr = macb_get_addr(bp, desc);
1936
1937                         dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1938                                         DMA_FROM_DEVICE);
1939                         dev_kfree_skb_any(skb);
1940                         skb = NULL;
1941                 }
1942
1943                 kfree(queue->rx_skbuff);
1944                 queue->rx_skbuff = NULL;
1945         }
1946 }
1947
1948 static void macb_free_rx_buffers(struct macb *bp)
1949 {
1950         struct macb_queue *queue = &bp->queues[0];
1951
1952         if (queue->rx_buffers) {
1953                 dma_free_coherent(&bp->pdev->dev,
1954                                   bp->rx_ring_size * bp->rx_buffer_size,
1955                                   queue->rx_buffers, queue->rx_buffers_dma);
1956                 queue->rx_buffers = NULL;
1957         }
1958 }
1959
1960 static void macb_free_consistent(struct macb *bp)
1961 {
1962         struct macb_queue *queue;
1963         unsigned int q;
1964         int size;
1965
1966         bp->macbgem_ops.mog_free_rx_buffers(bp);
1967
1968         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1969                 kfree(queue->tx_skb);
1970                 queue->tx_skb = NULL;
1971                 if (queue->tx_ring) {
1972                         size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
1973                         dma_free_coherent(&bp->pdev->dev, size,
1974                                           queue->tx_ring, queue->tx_ring_dma);
1975                         queue->tx_ring = NULL;
1976                 }
1977                 if (queue->rx_ring) {
1978                         size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
1979                         dma_free_coherent(&bp->pdev->dev, size,
1980                                           queue->rx_ring, queue->rx_ring_dma);
1981                         queue->rx_ring = NULL;
1982                 }
1983         }
1984 }
1985
1986 static int gem_alloc_rx_buffers(struct macb *bp)
1987 {
1988         struct macb_queue *queue;
1989         unsigned int q;
1990         int size;
1991
1992         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1993                 size = bp->rx_ring_size * sizeof(struct sk_buff *);
1994                 queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
1995                 if (!queue->rx_skbuff)
1996                         return -ENOMEM;
1997                 else
1998                         netdev_dbg(bp->dev,
1999                                    "Allocated %d RX struct sk_buff entries at %p\n",
2000                                    bp->rx_ring_size, queue->rx_skbuff);
2001         }
2002         return 0;
2003 }
2004
2005 static int macb_alloc_rx_buffers(struct macb *bp)
2006 {
2007         struct macb_queue *queue = &bp->queues[0];
2008         int size;
2009
2010         size = bp->rx_ring_size * bp->rx_buffer_size;
2011         queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
2012                                             &queue->rx_buffers_dma, GFP_KERNEL);
2013         if (!queue->rx_buffers)
2014                 return -ENOMEM;
2015
2016         netdev_dbg(bp->dev,
2017                    "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
2018                    size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
2019         return 0;
2020 }
2021
2022 static int macb_alloc_consistent(struct macb *bp)
2023 {
2024         struct macb_queue *queue;
2025         unsigned int q;
2026         int size;
2027
2028         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2029                 size = TX_RING_BYTES(bp) + bp->tx_bd_rd_prefetch;
2030                 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2031                                                     &queue->tx_ring_dma,
2032                                                     GFP_KERNEL);
2033                 if (!queue->tx_ring)
2034                         goto out_err;
2035                 netdev_dbg(bp->dev,
2036                            "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
2037                            q, size, (unsigned long)queue->tx_ring_dma,
2038                            queue->tx_ring);
2039
2040                 size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
2041                 queue->tx_skb = kmalloc(size, GFP_KERNEL);
2042                 if (!queue->tx_skb)
2043                         goto out_err;
2044
2045                 size = RX_RING_BYTES(bp) + bp->rx_bd_rd_prefetch;
2046                 queue->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
2047                                                  &queue->rx_ring_dma, GFP_KERNEL);
2048                 if (!queue->rx_ring)
2049                         goto out_err;
2050                 netdev_dbg(bp->dev,
2051                            "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
2052                            size, (unsigned long)queue->rx_ring_dma, queue->rx_ring);
2053         }
2054         if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
2055                 goto out_err;
2056
2057         return 0;
2058
2059 out_err:
2060         macb_free_consistent(bp);
2061         return -ENOMEM;
2062 }
2063
2064 static void gem_init_rings(struct macb *bp)
2065 {
2066         struct macb_queue *queue;
2067         struct macb_dma_desc *desc = NULL;
2068         unsigned int q;
2069         int i;
2070
2071         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2072                 for (i = 0; i < bp->tx_ring_size; i++) {
2073                         desc = macb_tx_desc(queue, i);
2074                         macb_set_addr(bp, desc, 0);
2075                         desc->ctrl = MACB_BIT(TX_USED);
2076                 }
2077                 desc->ctrl |= MACB_BIT(TX_WRAP);
2078                 queue->tx_head = 0;
2079                 queue->tx_tail = 0;
2080
2081                 queue->rx_tail = 0;
2082                 queue->rx_prepared_head = 0;
2083
2084                 gem_rx_refill(queue);
2085         }
2086
2087 }
2088
2089 static void macb_init_rings(struct macb *bp)
2090 {
2091         int i;
2092         struct macb_dma_desc *desc = NULL;
2093
2094         macb_init_rx_ring(&bp->queues[0]);
2095
2096         for (i = 0; i < bp->tx_ring_size; i++) {
2097                 desc = macb_tx_desc(&bp->queues[0], i);
2098                 macb_set_addr(bp, desc, 0);
2099                 desc->ctrl = MACB_BIT(TX_USED);
2100         }
2101         bp->queues[0].tx_head = 0;
2102         bp->queues[0].tx_tail = 0;
2103         desc->ctrl |= MACB_BIT(TX_WRAP);
2104 }
2105
2106 static void macb_reset_hw(struct macb *bp)
2107 {
2108         struct macb_queue *queue;
2109         unsigned int q;
2110         u32 ctrl = macb_readl(bp, NCR);
2111
2112         /* Disable RX and TX (XXX: Should we halt the transmission
2113          * more gracefully?)
2114          */
2115         ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
2116
2117         /* Clear the stats registers (XXX: Update stats first?) */
2118         ctrl |= MACB_BIT(CLRSTAT);
2119
2120         macb_writel(bp, NCR, ctrl);
2121
2122         /* Clear all status flags */
2123         macb_writel(bp, TSR, -1);
2124         macb_writel(bp, RSR, -1);
2125
2126         /* Disable all interrupts */
2127         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2128                 queue_writel(queue, IDR, -1);
2129                 queue_readl(queue, ISR);
2130                 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
2131                         queue_writel(queue, ISR, -1);
2132         }
2133 }
2134
2135 static u32 gem_mdc_clk_div(struct macb *bp)
2136 {
2137         u32 config;
2138         unsigned long pclk_hz = clk_get_rate(bp->pclk);
2139
2140         if (pclk_hz <= 20000000)
2141                 config = GEM_BF(CLK, GEM_CLK_DIV8);
2142         else if (pclk_hz <= 40000000)
2143                 config = GEM_BF(CLK, GEM_CLK_DIV16);
2144         else if (pclk_hz <= 80000000)
2145                 config = GEM_BF(CLK, GEM_CLK_DIV32);
2146         else if (pclk_hz <= 120000000)
2147                 config = GEM_BF(CLK, GEM_CLK_DIV48);
2148         else if (pclk_hz <= 160000000)
2149                 config = GEM_BF(CLK, GEM_CLK_DIV64);
2150         else
2151                 config = GEM_BF(CLK, GEM_CLK_DIV96);
2152
2153         return config;
2154 }
2155
2156 static u32 macb_mdc_clk_div(struct macb *bp)
2157 {
2158         u32 config;
2159         unsigned long pclk_hz;
2160
2161         if (macb_is_gem(bp))
2162                 return gem_mdc_clk_div(bp);
2163
2164         pclk_hz = clk_get_rate(bp->pclk);
2165         if (pclk_hz <= 20000000)
2166                 config = MACB_BF(CLK, MACB_CLK_DIV8);
2167         else if (pclk_hz <= 40000000)
2168                 config = MACB_BF(CLK, MACB_CLK_DIV16);
2169         else if (pclk_hz <= 80000000)
2170                 config = MACB_BF(CLK, MACB_CLK_DIV32);
2171         else
2172                 config = MACB_BF(CLK, MACB_CLK_DIV64);
2173
2174         return config;
2175 }
2176
2177 /* Get the DMA bus width field of the network configuration register that we
2178  * should program.  We find the width from decoding the design configuration
2179  * register to find the maximum supported data bus width.
2180  */
2181 static u32 macb_dbw(struct macb *bp)
2182 {
2183         if (!macb_is_gem(bp))
2184                 return 0;
2185
2186         switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
2187         case 4:
2188                 return GEM_BF(DBW, GEM_DBW128);
2189         case 2:
2190                 return GEM_BF(DBW, GEM_DBW64);
2191         case 1:
2192         default:
2193                 return GEM_BF(DBW, GEM_DBW32);
2194         }
2195 }
2196
2197 /* Configure the receive DMA engine
2198  * - use the correct receive buffer size
2199  * - set best burst length for DMA operations
2200  *   (if not supported by FIFO, it will fallback to default)
2201  * - set both rx/tx packet buffers to full memory size
2202  * These are configurable parameters for GEM.
2203  */
2204 static void macb_configure_dma(struct macb *bp)
2205 {
2206         struct macb_queue *queue;
2207         u32 buffer_size;
2208         unsigned int q;
2209         u32 dmacfg;
2210
2211         buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
2212         if (macb_is_gem(bp)) {
2213                 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
2214                 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2215                         if (q)
2216                                 queue_writel(queue, RBQS, buffer_size);
2217                         else
2218                                 dmacfg |= GEM_BF(RXBS, buffer_size);
2219                 }
2220                 if (bp->dma_burst_length)
2221                         dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
2222                 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
2223                 dmacfg &= ~GEM_BIT(ENDIA_PKT);
2224
2225                 if (bp->native_io)
2226                         dmacfg &= ~GEM_BIT(ENDIA_DESC);
2227                 else
2228                         dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
2229
2230                 if (bp->dev->features & NETIF_F_HW_CSUM)
2231                         dmacfg |= GEM_BIT(TXCOEN);
2232                 else
2233                         dmacfg &= ~GEM_BIT(TXCOEN);
2234
2235                 dmacfg &= ~GEM_BIT(ADDR64);
2236 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2237                 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2238                         dmacfg |= GEM_BIT(ADDR64);
2239 #endif
2240 #ifdef CONFIG_MACB_USE_HWSTAMP
2241                 if (bp->hw_dma_cap & HW_DMA_CAP_PTP)
2242                         dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
2243 #endif
2244                 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
2245                            dmacfg);
2246                 gem_writel(bp, DMACFG, dmacfg);
2247         }
2248 }
2249
2250 static void macb_init_hw(struct macb *bp)
2251 {
2252         struct macb_queue *queue;
2253         unsigned int q;
2254
2255         u32 config;
2256
2257         macb_reset_hw(bp);
2258         macb_set_hwaddr(bp);
2259
2260         config = macb_mdc_clk_div(bp);
2261         if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2262                 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
2263         config |= MACB_BF(RBOF, NET_IP_ALIGN);  /* Make eth data aligned */
2264         config |= MACB_BIT(PAE);                /* PAuse Enable */
2265         config |= MACB_BIT(DRFCS);              /* Discard Rx FCS */
2266         if (bp->caps & MACB_CAPS_JUMBO)
2267                 config |= MACB_BIT(JFRAME);     /* Enable jumbo frames */
2268         else
2269                 config |= MACB_BIT(BIG);        /* Receive oversized frames */
2270         if (bp->dev->flags & IFF_PROMISC)
2271                 config |= MACB_BIT(CAF);        /* Copy All Frames */
2272         else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
2273                 config |= GEM_BIT(RXCOEN);
2274         if (!(bp->dev->flags & IFF_BROADCAST))
2275                 config |= MACB_BIT(NBC);        /* No BroadCast */
2276         config |= macb_dbw(bp);
2277         macb_writel(bp, NCFGR, config);
2278         if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
2279                 gem_writel(bp, JML, bp->jumbo_max_len);
2280         bp->speed = SPEED_10;
2281         bp->duplex = DUPLEX_HALF;
2282         bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
2283         if (bp->caps & MACB_CAPS_JUMBO)
2284                 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
2285
2286         macb_configure_dma(bp);
2287
2288         /* Initialize TX and RX buffers */
2289         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2290                 queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
2291 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2292                 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2293                         queue_writel(queue, RBQPH, upper_32_bits(queue->rx_ring_dma));
2294 #endif
2295                 queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
2296 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2297                 if (bp->hw_dma_cap & HW_DMA_CAP_64B)
2298                         queue_writel(queue, TBQPH, upper_32_bits(queue->tx_ring_dma));
2299 #endif
2300
2301                 /* Enable interrupts */
2302                 queue_writel(queue, IER,
2303                              bp->rx_intr_mask |
2304                              MACB_TX_INT_FLAGS |
2305                              MACB_BIT(HRESP));
2306         }
2307
2308         /* Enable TX and RX */
2309         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(RE) | MACB_BIT(TE));
2310 }
2311
2312 /* The hash address register is 64 bits long and takes up two
2313  * locations in the memory map.  The least significant bits are stored
2314  * in EMAC_HSL and the most significant bits in EMAC_HSH.
2315  *
2316  * The unicast hash enable and the multicast hash enable bits in the
2317  * network configuration register enable the reception of hash matched
2318  * frames. The destination address is reduced to a 6 bit index into
2319  * the 64 bit hash register using the following hash function.  The
2320  * hash function is an exclusive or of every sixth bit of the
2321  * destination address.
2322  *
2323  * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
2324  * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
2325  * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
2326  * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
2327  * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
2328  * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
2329  *
2330  * da[0] represents the least significant bit of the first byte
2331  * received, that is, the multicast/unicast indicator, and da[47]
2332  * represents the most significant bit of the last byte received.  If
2333  * the hash index, hi[n], points to a bit that is set in the hash
2334  * register then the frame will be matched according to whether the
2335  * frame is multicast or unicast.  A multicast match will be signalled
2336  * if the multicast hash enable bit is set, da[0] is 1 and the hash
2337  * index points to a bit set in the hash register.  A unicast match
2338  * will be signalled if the unicast hash enable bit is set, da[0] is 0
2339  * and the hash index points to a bit set in the hash register.  To
2340  * receive all multicast frames, the hash register should be set with
2341  * all ones and the multicast hash enable bit should be set in the
2342  * network configuration register.
2343  */
2344
2345 static inline int hash_bit_value(int bitnr, __u8 *addr)
2346 {
2347         if (addr[bitnr / 8] & (1 << (bitnr % 8)))
2348                 return 1;
2349         return 0;
2350 }
2351
2352 /* Return the hash index value for the specified address. */
2353 static int hash_get_index(__u8 *addr)
2354 {
2355         int i, j, bitval;
2356         int hash_index = 0;
2357
2358         for (j = 0; j < 6; j++) {
2359                 for (i = 0, bitval = 0; i < 8; i++)
2360                         bitval ^= hash_bit_value(i * 6 + j, addr);
2361
2362                 hash_index |= (bitval << j);
2363         }
2364
2365         return hash_index;
2366 }
2367
2368 /* Add multicast addresses to the internal multicast-hash table. */
2369 static void macb_sethashtable(struct net_device *dev)
2370 {
2371         struct netdev_hw_addr *ha;
2372         unsigned long mc_filter[2];
2373         unsigned int bitnr;
2374         struct macb *bp = netdev_priv(dev);
2375
2376         mc_filter[0] = 0;
2377         mc_filter[1] = 0;
2378
2379         netdev_for_each_mc_addr(ha, dev) {
2380                 bitnr = hash_get_index(ha->addr);
2381                 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
2382         }
2383
2384         macb_or_gem_writel(bp, HRB, mc_filter[0]);
2385         macb_or_gem_writel(bp, HRT, mc_filter[1]);
2386 }
2387
2388 /* Enable/Disable promiscuous and multicast modes. */
2389 static void macb_set_rx_mode(struct net_device *dev)
2390 {
2391         unsigned long cfg;
2392         struct macb *bp = netdev_priv(dev);
2393
2394         cfg = macb_readl(bp, NCFGR);
2395
2396         if (dev->flags & IFF_PROMISC) {
2397                 /* Enable promiscuous mode */
2398                 cfg |= MACB_BIT(CAF);
2399
2400                 /* Disable RX checksum offload */
2401                 if (macb_is_gem(bp))
2402                         cfg &= ~GEM_BIT(RXCOEN);
2403         } else {
2404                 /* Disable promiscuous mode */
2405                 cfg &= ~MACB_BIT(CAF);
2406
2407                 /* Enable RX checksum offload only if requested */
2408                 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
2409                         cfg |= GEM_BIT(RXCOEN);
2410         }
2411
2412         if (dev->flags & IFF_ALLMULTI) {
2413                 /* Enable all multicast mode */
2414                 macb_or_gem_writel(bp, HRB, -1);
2415                 macb_or_gem_writel(bp, HRT, -1);
2416                 cfg |= MACB_BIT(NCFGR_MTI);
2417         } else if (!netdev_mc_empty(dev)) {
2418                 /* Enable specific multicasts */
2419                 macb_sethashtable(dev);
2420                 cfg |= MACB_BIT(NCFGR_MTI);
2421         } else if (dev->flags & (~IFF_ALLMULTI)) {
2422                 /* Disable all multicast mode */
2423                 macb_or_gem_writel(bp, HRB, 0);
2424                 macb_or_gem_writel(bp, HRT, 0);
2425                 cfg &= ~MACB_BIT(NCFGR_MTI);
2426         }
2427
2428         macb_writel(bp, NCFGR, cfg);
2429 }
2430
2431 static int macb_open(struct net_device *dev)
2432 {
2433         struct macb *bp = netdev_priv(dev);
2434         size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
2435         struct macb_queue *queue;
2436         unsigned int q;
2437         int err;
2438
2439         netdev_dbg(bp->dev, "open\n");
2440
2441         /* carrier starts down */
2442         netif_carrier_off(dev);
2443
2444         /* if the phy is not yet register, retry later*/
2445         if (!dev->phydev)
2446                 return -EAGAIN;
2447
2448         /* RX buffers initialization */
2449         macb_init_rx_buffer_size(bp, bufsz);
2450
2451         err = macb_alloc_consistent(bp);
2452         if (err) {
2453                 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
2454                            err);
2455                 return err;
2456         }
2457
2458         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2459                 napi_enable(&queue->napi);
2460
2461         bp->macbgem_ops.mog_init_rings(bp);
2462         macb_init_hw(bp);
2463
2464         /* schedule a link state check */
2465         phy_start(dev->phydev);
2466
2467         netif_tx_start_all_queues(dev);
2468
2469         if (bp->ptp_info)
2470                 bp->ptp_info->ptp_init(dev);
2471
2472         return 0;
2473 }
2474
2475 static int macb_close(struct net_device *dev)
2476 {
2477         struct macb *bp = netdev_priv(dev);
2478         struct macb_queue *queue;
2479         unsigned long flags;
2480         unsigned int q;
2481
2482         netif_tx_stop_all_queues(dev);
2483
2484         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2485                 napi_disable(&queue->napi);
2486
2487         if (dev->phydev)
2488                 phy_stop(dev->phydev);
2489
2490         spin_lock_irqsave(&bp->lock, flags);
2491         macb_reset_hw(bp);
2492         netif_carrier_off(dev);
2493         spin_unlock_irqrestore(&bp->lock, flags);
2494
2495         macb_free_consistent(bp);
2496
2497         if (bp->ptp_info)
2498                 bp->ptp_info->ptp_remove(dev);
2499
2500         return 0;
2501 }
2502
2503 static int macb_change_mtu(struct net_device *dev, int new_mtu)
2504 {
2505         if (netif_running(dev))
2506                 return -EBUSY;
2507
2508         dev->mtu = new_mtu;
2509
2510         return 0;
2511 }
2512
2513 static void gem_update_stats(struct macb *bp)
2514 {
2515         struct macb_queue *queue;
2516         unsigned int i, q, idx;
2517         unsigned long *stat;
2518
2519         u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2520
2521         for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2522                 u32 offset = gem_statistics[i].offset;
2523                 u64 val = bp->macb_reg_readl(bp, offset);
2524
2525                 bp->ethtool_stats[i] += val;
2526                 *p += val;
2527
2528                 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2529                         /* Add GEM_OCTTXH, GEM_OCTRXH */
2530                         val = bp->macb_reg_readl(bp, offset + 4);
2531                         bp->ethtool_stats[i] += ((u64)val) << 32;
2532                         *(++p) += val;
2533                 }
2534         }
2535
2536         idx = GEM_STATS_LEN;
2537         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
2538                 for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
2539                         bp->ethtool_stats[idx++] = *stat;
2540 }
2541
2542 static struct net_device_stats *gem_get_stats(struct macb *bp)
2543 {
2544         struct gem_stats *hwstat = &bp->hw_stats.gem;
2545         struct net_device_stats *nstat = &bp->dev->stats;
2546
2547         if (!netif_running(bp->dev))
2548                 return nstat;
2549
2550         gem_update_stats(bp);
2551
2552         nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2553                             hwstat->rx_alignment_errors +
2554                             hwstat->rx_resource_errors +
2555                             hwstat->rx_overruns +
2556                             hwstat->rx_oversize_frames +
2557                             hwstat->rx_jabbers +
2558                             hwstat->rx_undersized_frames +
2559                             hwstat->rx_length_field_frame_errors);
2560         nstat->tx_errors = (hwstat->tx_late_collisions +
2561                             hwstat->tx_excessive_collisions +
2562                             hwstat->tx_underrun +
2563                             hwstat->tx_carrier_sense_errors);
2564         nstat->multicast = hwstat->rx_multicast_frames;
2565         nstat->collisions = (hwstat->tx_single_collision_frames +
2566                              hwstat->tx_multiple_collision_frames +
2567                              hwstat->tx_excessive_collisions);
2568         nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2569                                    hwstat->rx_jabbers +
2570                                    hwstat->rx_undersized_frames +
2571                                    hwstat->rx_length_field_frame_errors);
2572         nstat->rx_over_errors = hwstat->rx_resource_errors;
2573         nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2574         nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2575         nstat->rx_fifo_errors = hwstat->rx_overruns;
2576         nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2577         nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2578         nstat->tx_fifo_errors = hwstat->tx_underrun;
2579
2580         return nstat;
2581 }
2582
2583 static void gem_get_ethtool_stats(struct net_device *dev,
2584                                   struct ethtool_stats *stats, u64 *data)
2585 {
2586         struct macb *bp;
2587
2588         bp = netdev_priv(dev);
2589         gem_update_stats(bp);
2590         memcpy(data, &bp->ethtool_stats, sizeof(u64)
2591                         * (GEM_STATS_LEN + QUEUE_STATS_LEN * MACB_MAX_QUEUES));
2592 }
2593
2594 static int gem_get_sset_count(struct net_device *dev, int sset)
2595 {
2596         struct macb *bp = netdev_priv(dev);
2597
2598         switch (sset) {
2599         case ETH_SS_STATS:
2600                 return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
2601         default:
2602                 return -EOPNOTSUPP;
2603         }
2604 }
2605
2606 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2607 {
2608         char stat_string[ETH_GSTRING_LEN];
2609         struct macb *bp = netdev_priv(dev);
2610         struct macb_queue *queue;
2611         unsigned int i;
2612         unsigned int q;
2613
2614         switch (sset) {
2615         case ETH_SS_STATS:
2616                 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2617                         memcpy(p, gem_statistics[i].stat_string,
2618                                ETH_GSTRING_LEN);
2619
2620                 for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
2621                         for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
2622                                 snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
2623                                                 q, queue_statistics[i].stat_string);
2624                                 memcpy(p, stat_string, ETH_GSTRING_LEN);
2625                         }
2626                 }
2627                 break;
2628         }
2629 }
2630
2631 static struct net_device_stats *macb_get_stats(struct net_device *dev)
2632 {
2633         struct macb *bp = netdev_priv(dev);
2634         struct net_device_stats *nstat = &bp->dev->stats;
2635         struct macb_stats *hwstat = &bp->hw_stats.macb;
2636
2637         if (macb_is_gem(bp))
2638                 return gem_get_stats(bp);
2639
2640         /* read stats from hardware */
2641         macb_update_stats(bp);
2642
2643         /* Convert HW stats into netdevice stats */
2644         nstat->rx_errors = (hwstat->rx_fcs_errors +
2645                             hwstat->rx_align_errors +
2646                             hwstat->rx_resource_errors +
2647                             hwstat->rx_overruns +
2648                             hwstat->rx_oversize_pkts +
2649                             hwstat->rx_jabbers +
2650                             hwstat->rx_undersize_pkts +
2651                             hwstat->rx_length_mismatch);
2652         nstat->tx_errors = (hwstat->tx_late_cols +
2653                             hwstat->tx_excessive_cols +
2654                             hwstat->tx_underruns +
2655                             hwstat->tx_carrier_errors +
2656                             hwstat->sqe_test_errors);
2657         nstat->collisions = (hwstat->tx_single_cols +
2658                              hwstat->tx_multiple_cols +
2659                              hwstat->tx_excessive_cols);
2660         nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2661                                    hwstat->rx_jabbers +
2662                                    hwstat->rx_undersize_pkts +
2663                                    hwstat->rx_length_mismatch);
2664         nstat->rx_over_errors = hwstat->rx_resource_errors +
2665                                    hwstat->rx_overruns;
2666         nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2667         nstat->rx_frame_errors = hwstat->rx_align_errors;
2668         nstat->rx_fifo_errors = hwstat->rx_overruns;
2669         /* XXX: What does "missed" mean? */
2670         nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2671         nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2672         nstat->tx_fifo_errors = hwstat->tx_underruns;
2673         /* Don't know about heartbeat or window errors... */
2674
2675         return nstat;
2676 }
2677
2678 static int macb_get_regs_len(struct net_device *netdev)
2679 {
2680         return MACB_GREGS_NBR * sizeof(u32);
2681 }
2682
2683 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2684                           void *p)
2685 {
2686         struct macb *bp = netdev_priv(dev);
2687         unsigned int tail, head;
2688         u32 *regs_buff = p;
2689
2690         regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2691                         | MACB_GREGS_VERSION;
2692
2693         tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
2694         head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
2695
2696         regs_buff[0]  = macb_readl(bp, NCR);
2697         regs_buff[1]  = macb_or_gem_readl(bp, NCFGR);
2698         regs_buff[2]  = macb_readl(bp, NSR);
2699         regs_buff[3]  = macb_readl(bp, TSR);
2700         regs_buff[4]  = macb_readl(bp, RBQP);
2701         regs_buff[5]  = macb_readl(bp, TBQP);
2702         regs_buff[6]  = macb_readl(bp, RSR);
2703         regs_buff[7]  = macb_readl(bp, IMR);
2704
2705         regs_buff[8]  = tail;
2706         regs_buff[9]  = head;
2707         regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2708         regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
2709
2710         if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2711                 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
2712         if (macb_is_gem(bp))
2713                 regs_buff[13] = gem_readl(bp, DMACFG);
2714 }
2715
2716 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2717 {
2718         struct macb *bp = netdev_priv(netdev);
2719
2720         wol->supported = 0;
2721         wol->wolopts = 0;
2722
2723         if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2724                 wol->supported = WAKE_MAGIC;
2725
2726                 if (bp->wol & MACB_WOL_ENABLED)
2727                         wol->wolopts |= WAKE_MAGIC;
2728         }
2729 }
2730
2731 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2732 {
2733         struct macb *bp = netdev_priv(netdev);
2734
2735         if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2736             (wol->wolopts & ~WAKE_MAGIC))
2737                 return -EOPNOTSUPP;
2738
2739         if (wol->wolopts & WAKE_MAGIC)
2740                 bp->wol |= MACB_WOL_ENABLED;
2741         else
2742                 bp->wol &= ~MACB_WOL_ENABLED;
2743
2744         device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2745
2746         return 0;
2747 }
2748
2749 static void macb_get_ringparam(struct net_device *netdev,
2750                                struct ethtool_ringparam *ring)
2751 {
2752         struct macb *bp = netdev_priv(netdev);
2753
2754         ring->rx_max_pending = MAX_RX_RING_SIZE;
2755         ring->tx_max_pending = MAX_TX_RING_SIZE;
2756
2757         ring->rx_pending = bp->rx_ring_size;
2758         ring->tx_pending = bp->tx_ring_size;
2759 }
2760
2761 static int macb_set_ringparam(struct net_device *netdev,
2762                               struct ethtool_ringparam *ring)
2763 {
2764         struct macb *bp = netdev_priv(netdev);
2765         u32 new_rx_size, new_tx_size;
2766         unsigned int reset = 0;
2767
2768         if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
2769                 return -EINVAL;
2770
2771         new_rx_size = clamp_t(u32, ring->rx_pending,
2772                               MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
2773         new_rx_size = roundup_pow_of_two(new_rx_size);
2774
2775         new_tx_size = clamp_t(u32, ring->tx_pending,
2776                               MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
2777         new_tx_size = roundup_pow_of_two(new_tx_size);
2778
2779         if ((new_tx_size == bp->tx_ring_size) &&
2780             (new_rx_size == bp->rx_ring_size)) {
2781                 /* nothing to do */
2782                 return 0;
2783         }
2784
2785         if (netif_running(bp->dev)) {
2786                 reset = 1;
2787                 macb_close(bp->dev);
2788         }
2789
2790         bp->rx_ring_size = new_rx_size;
2791         bp->tx_ring_size = new_tx_size;
2792
2793         if (reset)
2794                 macb_open(bp->dev);
2795
2796         return 0;
2797 }
2798
2799 #ifdef CONFIG_MACB_USE_HWSTAMP
2800 static unsigned int gem_get_tsu_rate(struct macb *bp)
2801 {
2802         struct clk *tsu_clk;
2803         unsigned int tsu_rate;
2804
2805         tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
2806         if (!IS_ERR(tsu_clk))
2807                 tsu_rate = clk_get_rate(tsu_clk);
2808         /* try pclk instead */
2809         else if (!IS_ERR(bp->pclk)) {
2810                 tsu_clk = bp->pclk;
2811                 tsu_rate = clk_get_rate(tsu_clk);
2812         } else
2813                 return -ENOTSUPP;
2814         return tsu_rate;
2815 }
2816
2817 static s32 gem_get_ptp_max_adj(void)
2818 {
2819         return 64000000;
2820 }
2821
2822 static int gem_get_ts_info(struct net_device *dev,
2823                            struct ethtool_ts_info *info)
2824 {
2825         struct macb *bp = netdev_priv(dev);
2826
2827         if ((bp->hw_dma_cap & HW_DMA_CAP_PTP) == 0) {
2828                 ethtool_op_get_ts_info(dev, info);
2829                 return 0;
2830         }
2831
2832         info->so_timestamping =
2833                 SOF_TIMESTAMPING_TX_SOFTWARE |
2834                 SOF_TIMESTAMPING_RX_SOFTWARE |
2835                 SOF_TIMESTAMPING_SOFTWARE |
2836                 SOF_TIMESTAMPING_TX_HARDWARE |
2837                 SOF_TIMESTAMPING_RX_HARDWARE |
2838                 SOF_TIMESTAMPING_RAW_HARDWARE;
2839         info->tx_types =
2840                 (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
2841                 (1 << HWTSTAMP_TX_OFF) |
2842                 (1 << HWTSTAMP_TX_ON);
2843         info->rx_filters =
2844                 (1 << HWTSTAMP_FILTER_NONE) |
2845                 (1 << HWTSTAMP_FILTER_ALL);
2846
2847         info->phc_index = bp->ptp_clock ? ptp_clock_index(bp->ptp_clock) : -1;
2848
2849         return 0;
2850 }
2851
2852 static struct macb_ptp_info gem_ptp_info = {
2853         .ptp_init        = gem_ptp_init,
2854         .ptp_remove      = gem_ptp_remove,
2855         .get_ptp_max_adj = gem_get_ptp_max_adj,
2856         .get_tsu_rate    = gem_get_tsu_rate,
2857         .get_ts_info     = gem_get_ts_info,
2858         .get_hwtst       = gem_get_hwtst,
2859         .set_hwtst       = gem_set_hwtst,
2860 };
2861 #endif
2862
2863 static int macb_get_ts_info(struct net_device *netdev,
2864                             struct ethtool_ts_info *info)
2865 {
2866         struct macb *bp = netdev_priv(netdev);
2867
2868         if (bp->ptp_info)
2869                 return bp->ptp_info->get_ts_info(netdev, info);
2870
2871         return ethtool_op_get_ts_info(netdev, info);
2872 }
2873
2874 static void gem_enable_flow_filters(struct macb *bp, bool enable)
2875 {
2876         struct ethtool_rx_fs_item *item;
2877         u32 t2_scr;
2878         int num_t2_scr;
2879
2880         num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
2881
2882         list_for_each_entry(item, &bp->rx_fs_list.list, list) {
2883                 struct ethtool_rx_flow_spec *fs = &item->fs;
2884                 struct ethtool_tcpip4_spec *tp4sp_m;
2885
2886                 if (fs->location >= num_t2_scr)
2887                         continue;
2888
2889                 t2_scr = gem_readl_n(bp, SCRT2, fs->location);
2890
2891                 /* enable/disable screener regs for the flow entry */
2892                 t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
2893
2894                 /* only enable fields with no masking */
2895                 tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2896
2897                 if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
2898                         t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
2899                 else
2900                         t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
2901
2902                 if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
2903                         t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
2904                 else
2905                         t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
2906
2907                 if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
2908                         t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
2909                 else
2910                         t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
2911
2912                 gem_writel_n(bp, SCRT2, fs->location, t2_scr);
2913         }
2914 }
2915
2916 static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
2917 {
2918         struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
2919         uint16_t index = fs->location;
2920         u32 w0, w1, t2_scr;
2921         bool cmp_a = false;
2922         bool cmp_b = false;
2923         bool cmp_c = false;
2924
2925         tp4sp_v = &(fs->h_u.tcp_ip4_spec);
2926         tp4sp_m = &(fs->m_u.tcp_ip4_spec);
2927
2928         /* ignore field if any masking set */
2929         if (tp4sp_m->ip4src == 0xFFFFFFFF) {
2930                 /* 1st compare reg - IP source address */
2931                 w0 = 0;
2932                 w1 = 0;
2933                 w0 = tp4sp_v->ip4src;
2934                 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2935                 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2936                 w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
2937                 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
2938                 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
2939                 cmp_a = true;
2940         }
2941
2942         /* ignore field if any masking set */
2943         if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
2944                 /* 2nd compare reg - IP destination address */
2945                 w0 = 0;
2946                 w1 = 0;
2947                 w0 = tp4sp_v->ip4dst;
2948                 w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2949                 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
2950                 w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
2951                 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
2952                 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
2953                 cmp_b = true;
2954         }
2955
2956         /* ignore both port fields if masking set in both */
2957         if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
2958                 /* 3rd compare reg - source port, destination port */
2959                 w0 = 0;
2960                 w1 = 0;
2961                 w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
2962                 if (tp4sp_m->psrc == tp4sp_m->pdst) {
2963                         w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
2964                         w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2965                         w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
2966                         w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2967                 } else {
2968                         /* only one port definition */
2969                         w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
2970                         w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
2971                         if (tp4sp_m->psrc == 0xFFFF) { /* src port */
2972                                 w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
2973                                 w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
2974                         } else { /* dst port */
2975                                 w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
2976                                 w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
2977                         }
2978                 }
2979                 gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
2980                 gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
2981                 cmp_c = true;
2982         }
2983
2984         t2_scr = 0;
2985         t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
2986         t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
2987         if (cmp_a)
2988                 t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
2989         if (cmp_b)
2990                 t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
2991         if (cmp_c)
2992                 t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
2993         gem_writel_n(bp, SCRT2, index, t2_scr);
2994 }
2995
2996 static int gem_add_flow_filter(struct net_device *netdev,
2997                 struct ethtool_rxnfc *cmd)
2998 {
2999         struct macb *bp = netdev_priv(netdev);
3000         struct ethtool_rx_flow_spec *fs = &cmd->fs;
3001         struct ethtool_rx_fs_item *item, *newfs;
3002         unsigned long flags;
3003         int ret = -EINVAL;
3004         bool added = false;
3005
3006         newfs = kmalloc(sizeof(*newfs), GFP_KERNEL);
3007         if (newfs == NULL)
3008                 return -ENOMEM;
3009         memcpy(&newfs->fs, fs, sizeof(newfs->fs));
3010
3011         netdev_dbg(netdev,
3012                         "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3013                         fs->flow_type, (int)fs->ring_cookie, fs->location,
3014                         htonl(fs->h_u.tcp_ip4_spec.ip4src),
3015                         htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3016                         htons(fs->h_u.tcp_ip4_spec.psrc), htons(fs->h_u.tcp_ip4_spec.pdst));
3017
3018         spin_lock_irqsave(&bp->rx_fs_lock, flags);
3019
3020         /* find correct place to add in list */
3021         list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3022                 if (item->fs.location > newfs->fs.location) {
3023                         list_add_tail(&newfs->list, &item->list);
3024                         added = true;
3025                         break;
3026                 } else if (item->fs.location == fs->location) {
3027                         netdev_err(netdev, "Rule not added: location %d not free!\n",
3028                                         fs->location);
3029                         ret = -EBUSY;
3030                         goto err;
3031                 }
3032         }
3033         if (!added)
3034                 list_add_tail(&newfs->list, &bp->rx_fs_list.list);
3035
3036         gem_prog_cmp_regs(bp, fs);
3037         bp->rx_fs_list.count++;
3038         /* enable filtering if NTUPLE on */
3039         if (netdev->features & NETIF_F_NTUPLE)
3040                 gem_enable_flow_filters(bp, 1);
3041
3042         spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3043         return 0;
3044
3045 err:
3046         spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3047         kfree(newfs);
3048         return ret;
3049 }
3050
3051 static int gem_del_flow_filter(struct net_device *netdev,
3052                 struct ethtool_rxnfc *cmd)
3053 {
3054         struct macb *bp = netdev_priv(netdev);
3055         struct ethtool_rx_fs_item *item;
3056         struct ethtool_rx_flow_spec *fs;
3057         unsigned long flags;
3058
3059         spin_lock_irqsave(&bp->rx_fs_lock, flags);
3060
3061         list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3062                 if (item->fs.location == cmd->fs.location) {
3063                         /* disable screener regs for the flow entry */
3064                         fs = &(item->fs);
3065                         netdev_dbg(netdev,
3066                                         "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
3067                                         fs->flow_type, (int)fs->ring_cookie, fs->location,
3068                                         htonl(fs->h_u.tcp_ip4_spec.ip4src),
3069                                         htonl(fs->h_u.tcp_ip4_spec.ip4dst),
3070                                         htons(fs->h_u.tcp_ip4_spec.psrc),
3071                                         htons(fs->h_u.tcp_ip4_spec.pdst));
3072
3073                         gem_writel_n(bp, SCRT2, fs->location, 0);
3074
3075                         list_del(&item->list);
3076                         bp->rx_fs_list.count--;
3077                         spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3078                         kfree(item);
3079                         return 0;
3080                 }
3081         }
3082
3083         spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
3084         return -EINVAL;
3085 }
3086
3087 static int gem_get_flow_entry(struct net_device *netdev,
3088                 struct ethtool_rxnfc *cmd)
3089 {
3090         struct macb *bp = netdev_priv(netdev);
3091         struct ethtool_rx_fs_item *item;
3092
3093         list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3094                 if (item->fs.location == cmd->fs.location) {
3095                         memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
3096                         return 0;
3097                 }
3098         }
3099         return -EINVAL;
3100 }
3101
3102 static int gem_get_all_flow_entries(struct net_device *netdev,
3103                 struct ethtool_rxnfc *cmd, u32 *rule_locs)
3104 {
3105         struct macb *bp = netdev_priv(netdev);
3106         struct ethtool_rx_fs_item *item;
3107         uint32_t cnt = 0;
3108
3109         list_for_each_entry(item, &bp->rx_fs_list.list, list) {
3110                 if (cnt == cmd->rule_cnt)
3111                         return -EMSGSIZE;
3112                 rule_locs[cnt] = item->fs.location;
3113                 cnt++;
3114         }
3115         cmd->data = bp->max_tuples;
3116         cmd->rule_cnt = cnt;
3117
3118         return 0;
3119 }
3120
3121 static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
3122                 u32 *rule_locs)
3123 {
3124         struct macb *bp = netdev_priv(netdev);
3125         int ret = 0;
3126
3127         switch (cmd->cmd) {
3128         case ETHTOOL_GRXRINGS:
3129                 cmd->data = bp->num_queues;
3130                 break;
3131         case ETHTOOL_GRXCLSRLCNT:
3132                 cmd->rule_cnt = bp->rx_fs_list.count;
3133                 break;
3134         case ETHTOOL_GRXCLSRULE:
3135                 ret = gem_get_flow_entry(netdev, cmd);
3136                 break;
3137         case ETHTOOL_GRXCLSRLALL:
3138                 ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
3139                 break;
3140         default:
3141                 netdev_err(netdev,
3142                           "Command parameter %d is not supported\n", cmd->cmd);
3143                 ret = -EOPNOTSUPP;
3144         }
3145
3146         return ret;
3147 }
3148
3149 static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
3150 {
3151         struct macb *bp = netdev_priv(netdev);
3152         int ret;
3153
3154         switch (cmd->cmd) {
3155         case ETHTOOL_SRXCLSRLINS:
3156                 if ((cmd->fs.location >= bp->max_tuples)
3157                                 || (cmd->fs.ring_cookie >= bp->num_queues)) {
3158                         ret = -EINVAL;
3159                         break;
3160                 }
3161                 ret = gem_add_flow_filter(netdev, cmd);
3162                 break;
3163         case ETHTOOL_SRXCLSRLDEL:
3164                 ret = gem_del_flow_filter(netdev, cmd);
3165                 break;
3166         default:
3167                 netdev_err(netdev,
3168                           "Command parameter %d is not supported\n", cmd->cmd);
3169                 ret = -EOPNOTSUPP;
3170         }
3171
3172         return ret;
3173 }
3174
3175 static const struct ethtool_ops macb_ethtool_ops = {
3176         .get_regs_len           = macb_get_regs_len,
3177         .get_regs               = macb_get_regs,
3178         .get_link               = ethtool_op_get_link,
3179         .get_ts_info            = ethtool_op_get_ts_info,
3180         .get_wol                = macb_get_wol,
3181         .set_wol                = macb_set_wol,
3182         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
3183         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
3184         .get_ringparam          = macb_get_ringparam,
3185         .set_ringparam          = macb_set_ringparam,
3186 };
3187
3188 static const struct ethtool_ops gem_ethtool_ops = {
3189         .get_regs_len           = macb_get_regs_len,
3190         .get_regs               = macb_get_regs,
3191         .get_link               = ethtool_op_get_link,
3192         .get_ts_info            = macb_get_ts_info,
3193         .get_ethtool_stats      = gem_get_ethtool_stats,
3194         .get_strings            = gem_get_ethtool_strings,
3195         .get_sset_count         = gem_get_sset_count,
3196         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
3197         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
3198         .get_ringparam          = macb_get_ringparam,
3199         .set_ringparam          = macb_set_ringparam,
3200         .get_rxnfc                      = gem_get_rxnfc,
3201         .set_rxnfc                      = gem_set_rxnfc,
3202 };
3203
3204 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3205 {
3206         struct phy_device *phydev = dev->phydev;
3207         struct macb *bp = netdev_priv(dev);
3208
3209         if (!netif_running(dev))
3210                 return -EINVAL;
3211
3212         if (!phydev)
3213                 return -ENODEV;
3214
3215         if (!bp->ptp_info)
3216                 return phy_mii_ioctl(phydev, rq, cmd);
3217
3218         switch (cmd) {
3219         case SIOCSHWTSTAMP:
3220                 return bp->ptp_info->set_hwtst(dev, rq, cmd);
3221         case SIOCGHWTSTAMP:
3222                 return bp->ptp_info->get_hwtst(dev, rq);
3223         default:
3224                 return phy_mii_ioctl(phydev, rq, cmd);
3225         }
3226 }
3227
3228 static int macb_set_features(struct net_device *netdev,
3229                              netdev_features_t features)
3230 {
3231         struct macb *bp = netdev_priv(netdev);
3232         netdev_features_t changed = features ^ netdev->features;
3233
3234         /* TX checksum offload */
3235         if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
3236                 u32 dmacfg;
3237
3238                 dmacfg = gem_readl(bp, DMACFG);
3239                 if (features & NETIF_F_HW_CSUM)
3240                         dmacfg |= GEM_BIT(TXCOEN);
3241                 else
3242                         dmacfg &= ~GEM_BIT(TXCOEN);
3243                 gem_writel(bp, DMACFG, dmacfg);
3244         }
3245
3246         /* RX checksum offload */
3247         if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
3248                 u32 netcfg;
3249
3250                 netcfg = gem_readl(bp, NCFGR);
3251                 if (features & NETIF_F_RXCSUM &&
3252                     !(netdev->flags & IFF_PROMISC))
3253                         netcfg |= GEM_BIT(RXCOEN);
3254                 else
3255                         netcfg &= ~GEM_BIT(RXCOEN);
3256                 gem_writel(bp, NCFGR, netcfg);
3257         }
3258
3259         /* RX Flow Filters */
3260         if ((changed & NETIF_F_NTUPLE) && macb_is_gem(bp)) {
3261                 bool turn_on = features & NETIF_F_NTUPLE;
3262
3263                 gem_enable_flow_filters(bp, turn_on);
3264         }
3265         return 0;
3266 }
3267
3268 static const struct net_device_ops macb_netdev_ops = {
3269         .ndo_open               = macb_open,
3270         .ndo_stop               = macb_close,
3271         .ndo_start_xmit         = macb_start_xmit,
3272         .ndo_set_rx_mode        = macb_set_rx_mode,
3273         .ndo_get_stats          = macb_get_stats,
3274         .ndo_do_ioctl           = macb_ioctl,
3275         .ndo_validate_addr      = eth_validate_addr,
3276         .ndo_change_mtu         = macb_change_mtu,
3277         .ndo_set_mac_address    = eth_mac_addr,
3278 #ifdef CONFIG_NET_POLL_CONTROLLER
3279         .ndo_poll_controller    = macb_poll_controller,
3280 #endif
3281         .ndo_set_features       = macb_set_features,
3282         .ndo_features_check     = macb_features_check,
3283 };
3284
3285 /* Configure peripheral capabilities according to device tree
3286  * and integration options used
3287  */
3288 static void macb_configure_caps(struct macb *bp,
3289                                 const struct macb_config *dt_conf)
3290 {
3291         u32 dcfg;
3292
3293         if (dt_conf)
3294                 bp->caps = dt_conf->caps;
3295
3296         if (hw_is_gem(bp->regs, bp->native_io)) {
3297                 bp->caps |= MACB_CAPS_MACB_IS_GEM;
3298
3299                 dcfg = gem_readl(bp, DCFG1);
3300                 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
3301                         bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
3302                 dcfg = gem_readl(bp, DCFG2);
3303                 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
3304                         bp->caps |= MACB_CAPS_FIFO_MODE;
3305 #ifdef CONFIG_MACB_USE_HWSTAMP
3306                 if (gem_has_ptp(bp)) {
3307                         if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
3308                                 pr_err("GEM doesn't support hardware ptp.\n");
3309                         else {
3310                                 bp->hw_dma_cap |= HW_DMA_CAP_PTP;
3311                                 bp->ptp_info = &gem_ptp_info;
3312                         }
3313                 }
3314 #endif
3315         }
3316
3317         dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
3318 }
3319
3320 static void macb_probe_queues(void __iomem *mem,
3321                               bool native_io,
3322                               unsigned int *queue_mask,
3323                               unsigned int *num_queues)
3324 {
3325         unsigned int hw_q;
3326
3327         *queue_mask = 0x1;
3328         *num_queues = 1;
3329
3330         /* is it macb or gem ?
3331          *
3332          * We need to read directly from the hardware here because
3333          * we are early in the probe process and don't have the
3334          * MACB_CAPS_MACB_IS_GEM flag positioned
3335          */
3336         if (!hw_is_gem(mem, native_io))
3337                 return;
3338
3339         /* bit 0 is never set but queue 0 always exists */
3340         *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
3341
3342         *queue_mask |= 0x1;
3343
3344         for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
3345                 if (*queue_mask & (1 << hw_q))
3346                         (*num_queues)++;
3347 }
3348
3349 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
3350                          struct clk **hclk, struct clk **tx_clk,
3351                          struct clk **rx_clk)
3352 {
3353         struct macb_platform_data *pdata;
3354         int err;
3355
3356         pdata = dev_get_platdata(&pdev->dev);
3357         if (pdata) {
3358                 *pclk = pdata->pclk;
3359                 *hclk = pdata->hclk;
3360         } else {
3361                 *pclk = devm_clk_get(&pdev->dev, "pclk");
3362                 *hclk = devm_clk_get(&pdev->dev, "hclk");
3363         }
3364
3365         if (IS_ERR_OR_NULL(*pclk)) {
3366                 err = PTR_ERR(*pclk);
3367                 if (!err)
3368                         err = -ENODEV;
3369
3370                 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
3371                 return err;
3372         }
3373
3374         if (IS_ERR_OR_NULL(*hclk)) {
3375                 err = PTR_ERR(*hclk);
3376                 if (!err)
3377                         err = -ENODEV;
3378
3379                 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
3380                 return err;
3381         }
3382
3383         *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
3384         if (IS_ERR(*tx_clk))
3385                 *tx_clk = NULL;
3386
3387         *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
3388         if (IS_ERR(*rx_clk))
3389                 *rx_clk = NULL;
3390
3391         err = clk_prepare_enable(*pclk);
3392         if (err) {
3393                 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
3394                 return err;
3395         }
3396
3397         err = clk_prepare_enable(*hclk);
3398         if (err) {
3399                 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
3400                 goto err_disable_pclk;
3401         }
3402
3403         err = clk_prepare_enable(*tx_clk);
3404         if (err) {
3405                 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
3406                 goto err_disable_hclk;
3407         }
3408
3409         err = clk_prepare_enable(*rx_clk);
3410         if (err) {
3411                 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
3412                 goto err_disable_txclk;
3413         }
3414
3415         return 0;
3416
3417 err_disable_txclk:
3418         clk_disable_unprepare(*tx_clk);
3419
3420 err_disable_hclk:
3421         clk_disable_unprepare(*hclk);
3422
3423 err_disable_pclk:
3424         clk_disable_unprepare(*pclk);
3425
3426         return err;
3427 }
3428
3429 static int macb_init(struct platform_device *pdev)
3430 {
3431         struct net_device *dev = platform_get_drvdata(pdev);
3432         unsigned int hw_q, q;
3433         struct macb *bp = netdev_priv(dev);
3434         struct macb_queue *queue;
3435         int err;
3436         u32 val, reg;
3437
3438         bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
3439         bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
3440
3441         /* set the queue register mapping once for all: queue0 has a special
3442          * register mapping but we don't want to test the queue index then
3443          * compute the corresponding register offset at run time.
3444          */
3445         for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
3446                 if (!(bp->queue_mask & (1 << hw_q)))
3447                         continue;
3448
3449                 queue = &bp->queues[q];
3450                 queue->bp = bp;
3451                 netif_napi_add(dev, &queue->napi, macb_poll, 64);
3452                 if (hw_q) {
3453                         queue->ISR  = GEM_ISR(hw_q - 1);
3454                         queue->IER  = GEM_IER(hw_q - 1);
3455                         queue->IDR  = GEM_IDR(hw_q - 1);
3456                         queue->IMR  = GEM_IMR(hw_q - 1);
3457                         queue->TBQP = GEM_TBQP(hw_q - 1);
3458                         queue->RBQP = GEM_RBQP(hw_q - 1);
3459                         queue->RBQS = GEM_RBQS(hw_q - 1);
3460 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3461                         if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3462                                 queue->TBQPH = GEM_TBQPH(hw_q - 1);
3463                                 queue->RBQPH = GEM_RBQPH(hw_q - 1);
3464                         }
3465 #endif
3466                 } else {
3467                         /* queue0 uses legacy registers */
3468                         queue->ISR  = MACB_ISR;
3469                         queue->IER  = MACB_IER;
3470                         queue->IDR  = MACB_IDR;
3471                         queue->IMR  = MACB_IMR;
3472                         queue->TBQP = MACB_TBQP;
3473                         queue->RBQP = MACB_RBQP;
3474 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3475                         if (bp->hw_dma_cap & HW_DMA_CAP_64B) {
3476                                 queue->TBQPH = MACB_TBQPH;
3477                                 queue->RBQPH = MACB_RBQPH;
3478                         }
3479 #endif
3480                 }
3481
3482                 /* get irq: here we use the linux queue index, not the hardware
3483                  * queue index. the queue irq definitions in the device tree
3484                  * must remove the optional gaps that could exist in the
3485                  * hardware queue mask.
3486                  */
3487                 queue->irq = platform_get_irq(pdev, q);
3488                 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
3489                                        IRQF_SHARED, dev->name, queue);
3490                 if (err) {
3491                         dev_err(&pdev->dev,
3492                                 "Unable to request IRQ %d (error %d)\n",
3493                                 queue->irq, err);
3494                         return err;
3495                 }
3496
3497                 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
3498                 q++;
3499         }
3500
3501         dev->netdev_ops = &macb_netdev_ops;
3502
3503         /* setup appropriated routines according to adapter type */
3504         if (macb_is_gem(bp)) {
3505                 bp->max_tx_length = GEM_MAX_TX_LEN;
3506                 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
3507                 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
3508                 bp->macbgem_ops.mog_init_rings = gem_init_rings;
3509                 bp->macbgem_ops.mog_rx = gem_rx;
3510                 dev->ethtool_ops = &gem_ethtool_ops;
3511         } else {
3512                 bp->max_tx_length = MACB_MAX_TX_LEN;
3513                 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
3514                 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
3515                 bp->macbgem_ops.mog_init_rings = macb_init_rings;
3516                 bp->macbgem_ops.mog_rx = macb_rx;
3517                 dev->ethtool_ops = &macb_ethtool_ops;
3518         }
3519
3520         /* Set features */
3521         dev->hw_features = NETIF_F_SG;
3522
3523         /* Check LSO capability */
3524         if (GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
3525                 dev->hw_features |= MACB_NETIF_LSO;
3526
3527         /* Checksum offload is only available on gem with packet buffer */
3528         if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
3529                 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
3530         if (bp->caps & MACB_CAPS_SG_DISABLED)
3531                 dev->hw_features &= ~NETIF_F_SG;
3532         dev->features = dev->hw_features;
3533
3534         /* Check RX Flow Filters support.
3535          * Max Rx flows set by availability of screeners & compare regs:
3536          * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
3537          */
3538         reg = gem_readl(bp, DCFG8);
3539         bp->max_tuples = min((GEM_BFEXT(SCR2CMP, reg) / 3),
3540                         GEM_BFEXT(T2SCR, reg));
3541         if (bp->max_tuples > 0) {
3542                 /* also needs one ethtype match to check IPv4 */
3543                 if (GEM_BFEXT(SCR2ETH, reg) > 0) {
3544                         /* program this reg now */
3545                         reg = 0;
3546                         reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
3547                         gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
3548                         /* Filtering is supported in hw but don't enable it in kernel now */
3549                         dev->hw_features |= NETIF_F_NTUPLE;
3550                         /* init Rx flow definitions */
3551                         INIT_LIST_HEAD(&bp->rx_fs_list.list);
3552                         bp->rx_fs_list.count = 0;
3553                         spin_lock_init(&bp->rx_fs_lock);
3554                 } else
3555                         bp->max_tuples = 0;
3556         }
3557
3558         if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
3559                 val = 0;
3560                 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
3561                         val = GEM_BIT(RGMII);
3562                 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
3563                          (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3564                         val = MACB_BIT(RMII);
3565                 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
3566                         val = MACB_BIT(MII);
3567
3568                 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
3569                         val |= MACB_BIT(CLKEN);
3570
3571                 macb_or_gem_writel(bp, USRIO, val);
3572         }
3573
3574         /* Set MII management clock divider */
3575         val = macb_mdc_clk_div(bp);
3576         val |= macb_dbw(bp);
3577         if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
3578                 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
3579         macb_writel(bp, NCFGR, val);
3580
3581         return 0;
3582 }
3583
3584 #if defined(CONFIG_OF)
3585 /* 1518 rounded up */
3586 #define AT91ETHER_MAX_RBUFF_SZ  0x600
3587 /* max number of receive buffers */
3588 #define AT91ETHER_MAX_RX_DESCR  9
3589
3590 /* Initialize and start the Receiver and Transmit subsystems */
3591 static int at91ether_start(struct net_device *dev)
3592 {
3593         struct macb *lp = netdev_priv(dev);
3594         struct macb_queue *q = &lp->queues[0];
3595         struct macb_dma_desc *desc;
3596         dma_addr_t addr;
3597         u32 ctl;
3598         int i;
3599
3600         q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
3601                                          (AT91ETHER_MAX_RX_DESCR *
3602                                           macb_dma_desc_get_size(lp)),
3603                                          &q->rx_ring_dma, GFP_KERNEL);
3604         if (!q->rx_ring)
3605                 return -ENOMEM;
3606
3607         q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
3608                                             AT91ETHER_MAX_RX_DESCR *
3609                                             AT91ETHER_MAX_RBUFF_SZ,
3610                                             &q->rx_buffers_dma, GFP_KERNEL);
3611         if (!q->rx_buffers) {
3612                 dma_free_coherent(&lp->pdev->dev,
3613                                   AT91ETHER_MAX_RX_DESCR *
3614                                   macb_dma_desc_get_size(lp),
3615                                   q->rx_ring, q->rx_ring_dma);
3616                 q->rx_ring = NULL;
3617                 return -ENOMEM;
3618         }
3619
3620         addr = q->rx_buffers_dma;
3621         for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
3622                 desc = macb_rx_desc(q, i);
3623                 macb_set_addr(lp, desc, addr);
3624                 desc->ctrl = 0;
3625                 addr += AT91ETHER_MAX_RBUFF_SZ;
3626         }
3627
3628         /* Set the Wrap bit on the last descriptor */
3629         desc->addr |= MACB_BIT(RX_WRAP);
3630
3631         /* Reset buffer index */
3632         q->rx_tail = 0;
3633
3634         /* Program address of descriptor list in Rx Buffer Queue register */
3635         macb_writel(lp, RBQP, q->rx_ring_dma);
3636
3637         /* Enable Receive and Transmit */
3638         ctl = macb_readl(lp, NCR);
3639         macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
3640
3641         return 0;
3642 }
3643
3644 /* Open the ethernet interface */
3645 static int at91ether_open(struct net_device *dev)
3646 {
3647         struct macb *lp = netdev_priv(dev);
3648         u32 ctl;
3649         int ret;
3650
3651         /* Clear internal statistics */
3652         ctl = macb_readl(lp, NCR);
3653         macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
3654
3655         macb_set_hwaddr(lp);
3656
3657         ret = at91ether_start(dev);
3658         if (ret)
3659                 return ret;
3660
3661         /* Enable MAC interrupts */
3662         macb_writel(lp, IER, MACB_BIT(RCOMP)    |
3663                              MACB_BIT(RXUBR)    |
3664                              MACB_BIT(ISR_TUND) |
3665                              MACB_BIT(ISR_RLE)  |
3666                              MACB_BIT(TCOMP)    |
3667                              MACB_BIT(ISR_ROVR) |
3668                              MACB_BIT(HRESP));
3669
3670         /* schedule a link state check */
3671         phy_start(dev->phydev);
3672
3673         netif_start_queue(dev);
3674
3675         return 0;
3676 }
3677
3678 /* Close the interface */
3679 static int at91ether_close(struct net_device *dev)
3680 {
3681         struct macb *lp = netdev_priv(dev);
3682         struct macb_queue *q = &lp->queues[0];
3683         u32 ctl;
3684
3685         /* Disable Receiver and Transmitter */
3686         ctl = macb_readl(lp, NCR);
3687         macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
3688
3689         /* Disable MAC interrupts */
3690         macb_writel(lp, IDR, MACB_BIT(RCOMP)    |
3691                              MACB_BIT(RXUBR)    |
3692                              MACB_BIT(ISR_TUND) |
3693                              MACB_BIT(ISR_RLE)  |
3694                              MACB_BIT(TCOMP)    |
3695                              MACB_BIT(ISR_ROVR) |
3696                              MACB_BIT(HRESP));
3697
3698         netif_stop_queue(dev);
3699
3700         dma_free_coherent(&lp->pdev->dev,
3701                           AT91ETHER_MAX_RX_DESCR *
3702                           macb_dma_desc_get_size(lp),
3703                           q->rx_ring, q->rx_ring_dma);
3704         q->rx_ring = NULL;
3705
3706         dma_free_coherent(&lp->pdev->dev,
3707                           AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
3708                           q->rx_buffers, q->rx_buffers_dma);
3709         q->rx_buffers = NULL;
3710
3711         return 0;
3712 }
3713
3714 /* Transmit packet */
3715 static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
3716                                         struct net_device *dev)
3717 {
3718         struct macb *lp = netdev_priv(dev);
3719
3720         if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
3721                 netif_stop_queue(dev);
3722
3723                 /* Store packet information (to free when Tx completed) */
3724                 lp->skb = skb;
3725                 lp->skb_length = skb->len;
3726                 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
3727                                                         DMA_TO_DEVICE);
3728                 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
3729                         dev_kfree_skb_any(skb);
3730                         dev->stats.tx_dropped++;
3731                         netdev_err(dev, "%s: DMA mapping error\n", __func__);
3732                         return NETDEV_TX_OK;
3733                 }
3734
3735                 /* Set address of the data in the Transmit Address register */
3736                 macb_writel(lp, TAR, lp->skb_physaddr);
3737                 /* Set length of the packet in the Transmit Control register */
3738                 macb_writel(lp, TCR, skb->len);
3739
3740         } else {
3741                 netdev_err(dev, "%s called, but device is busy!\n", __func__);
3742                 return NETDEV_TX_BUSY;
3743         }
3744
3745         return NETDEV_TX_OK;
3746 }
3747
3748 /* Extract received frame from buffer descriptors and sent to upper layers.
3749  * (Called from interrupt context)
3750  */
3751 static void at91ether_rx(struct net_device *dev)
3752 {
3753         struct macb *lp = netdev_priv(dev);
3754         struct macb_queue *q = &lp->queues[0];
3755         struct macb_dma_desc *desc;
3756         unsigned char *p_recv;
3757         struct sk_buff *skb;
3758         unsigned int pktlen;
3759
3760         desc = macb_rx_desc(q, q->rx_tail);
3761         while (desc->addr & MACB_BIT(RX_USED)) {
3762                 p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
3763                 pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
3764                 skb = netdev_alloc_skb(dev, pktlen + 2);
3765                 if (skb) {
3766                         skb_reserve(skb, 2);
3767                         skb_put_data(skb, p_recv, pktlen);
3768
3769                         skb->protocol = eth_type_trans(skb, dev);
3770                         dev->stats.rx_packets++;
3771                         dev->stats.rx_bytes += pktlen;
3772                         netif_rx(skb);
3773                 } else {
3774                         dev->stats.rx_dropped++;
3775                 }
3776
3777                 if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
3778                         dev->stats.multicast++;
3779
3780                 /* reset ownership bit */
3781                 desc->addr &= ~MACB_BIT(RX_USED);
3782
3783                 /* wrap after last buffer */
3784                 if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
3785                         q->rx_tail = 0;
3786                 else
3787                         q->rx_tail++;
3788
3789                 desc = macb_rx_desc(q, q->rx_tail);
3790         }
3791 }
3792
3793 /* MAC interrupt handler */
3794 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
3795 {
3796         struct net_device *dev = dev_id;
3797         struct macb *lp = netdev_priv(dev);
3798         u32 intstatus, ctl;
3799
3800         /* MAC Interrupt Status register indicates what interrupts are pending.
3801          * It is automatically cleared once read.
3802          */
3803         intstatus = macb_readl(lp, ISR);
3804
3805         /* Receive complete */
3806         if (intstatus & MACB_BIT(RCOMP))
3807                 at91ether_rx(dev);
3808
3809         /* Transmit complete */
3810         if (intstatus & MACB_BIT(TCOMP)) {
3811                 /* The TCOM bit is set even if the transmission failed */
3812                 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
3813                         dev->stats.tx_errors++;
3814
3815                 if (lp->skb) {
3816                         dev_kfree_skb_irq(lp->skb);
3817                         lp->skb = NULL;
3818                         dma_unmap_single(NULL, lp->skb_physaddr,
3819                                          lp->skb_length, DMA_TO_DEVICE);
3820                         dev->stats.tx_packets++;
3821                         dev->stats.tx_bytes += lp->skb_length;
3822                 }
3823                 netif_wake_queue(dev);
3824         }
3825
3826         /* Work-around for EMAC Errata section 41.3.1 */
3827         if (intstatus & MACB_BIT(RXUBR)) {
3828                 ctl = macb_readl(lp, NCR);
3829                 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
3830                 wmb();
3831                 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
3832         }
3833
3834         if (intstatus & MACB_BIT(ISR_ROVR))
3835                 netdev_err(dev, "ROVR error\n");
3836
3837         return IRQ_HANDLED;
3838 }
3839
3840 #ifdef CONFIG_NET_POLL_CONTROLLER
3841 static void at91ether_poll_controller(struct net_device *dev)
3842 {
3843         unsigned long flags;
3844
3845         local_irq_save(flags);
3846         at91ether_interrupt(dev->irq, dev);
3847         local_irq_restore(flags);
3848 }
3849 #endif
3850
3851 static const struct net_device_ops at91ether_netdev_ops = {
3852         .ndo_open               = at91ether_open,
3853         .ndo_stop               = at91ether_close,
3854         .ndo_start_xmit         = at91ether_start_xmit,
3855         .ndo_get_stats          = macb_get_stats,
3856         .ndo_set_rx_mode        = macb_set_rx_mode,
3857         .ndo_set_mac_address    = eth_mac_addr,
3858         .ndo_do_ioctl           = macb_ioctl,
3859         .ndo_validate_addr      = eth_validate_addr,
3860 #ifdef CONFIG_NET_POLL_CONTROLLER
3861         .ndo_poll_controller    = at91ether_poll_controller,
3862 #endif
3863 };
3864
3865 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
3866                               struct clk **hclk, struct clk **tx_clk,
3867                               struct clk **rx_clk)
3868 {
3869         int err;
3870
3871         *hclk = NULL;
3872         *tx_clk = NULL;
3873         *rx_clk = NULL;
3874
3875         *pclk = devm_clk_get(&pdev->dev, "ether_clk");
3876         if (IS_ERR(*pclk))
3877                 return PTR_ERR(*pclk);
3878
3879         err = clk_prepare_enable(*pclk);
3880         if (err) {
3881                 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
3882                 return err;
3883         }
3884
3885         return 0;
3886 }
3887
3888 static int at91ether_init(struct platform_device *pdev)
3889 {
3890         struct net_device *dev = platform_get_drvdata(pdev);
3891         struct macb *bp = netdev_priv(dev);
3892         int err;
3893         u32 reg;
3894
3895         bp->queues[0].bp = bp;
3896
3897         dev->netdev_ops = &at91ether_netdev_ops;
3898         dev->ethtool_ops = &macb_ethtool_ops;
3899
3900         err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
3901                                0, dev->name, dev);
3902         if (err)
3903                 return err;
3904
3905         macb_writel(bp, NCR, 0);
3906
3907         reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
3908         if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
3909                 reg |= MACB_BIT(RM9200_RMII);
3910
3911         macb_writel(bp, NCFGR, reg);
3912
3913         return 0;
3914 }
3915
3916 static const struct macb_config at91sam9260_config = {
3917         .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3918         .clk_init = macb_clk_init,
3919         .init = macb_init,
3920 };
3921
3922 static const struct macb_config sama5d3macb_config = {
3923         .caps = MACB_CAPS_SG_DISABLED
3924               | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3925         .clk_init = macb_clk_init,
3926         .init = macb_init,
3927 };
3928
3929 static const struct macb_config pc302gem_config = {
3930         .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
3931         .dma_burst_length = 16,
3932         .clk_init = macb_clk_init,
3933         .init = macb_init,
3934 };
3935
3936 static const struct macb_config sama5d2_config = {
3937         .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3938         .dma_burst_length = 16,
3939         .clk_init = macb_clk_init,
3940         .init = macb_init,
3941 };
3942
3943 static const struct macb_config sama5d3_config = {
3944         .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
3945               | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
3946         .dma_burst_length = 16,
3947         .clk_init = macb_clk_init,
3948         .init = macb_init,
3949         .jumbo_max_len = 10240,
3950 };
3951
3952 static const struct macb_config sama5d4_config = {
3953         .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
3954         .dma_burst_length = 4,
3955         .clk_init = macb_clk_init,
3956         .init = macb_init,
3957 };
3958
3959 static const struct macb_config emac_config = {
3960         .caps = MACB_CAPS_NEEDS_RSTONUBR,
3961         .clk_init = at91ether_clk_init,
3962         .init = at91ether_init,
3963 };
3964
3965 static const struct macb_config np4_config = {
3966         .caps = MACB_CAPS_USRIO_DISABLED,
3967         .clk_init = macb_clk_init,
3968         .init = macb_init,
3969 };
3970
3971 static const struct macb_config zynqmp_config = {
3972         .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
3973                         MACB_CAPS_JUMBO |
3974                         MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
3975         .dma_burst_length = 16,
3976         .clk_init = macb_clk_init,
3977         .init = macb_init,
3978         .jumbo_max_len = 10240,
3979 };
3980
3981 static const struct macb_config zynq_config = {
3982         .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
3983                 MACB_CAPS_NEEDS_RSTONUBR,
3984         .dma_burst_length = 16,
3985         .clk_init = macb_clk_init,
3986         .init = macb_init,
3987 };
3988
3989 static const struct of_device_id macb_dt_ids[] = {
3990         { .compatible = "cdns,at32ap7000-macb" },
3991         { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
3992         { .compatible = "cdns,macb" },
3993         { .compatible = "cdns,np4-macb", .data = &np4_config },
3994         { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
3995         { .compatible = "cdns,gem", .data = &pc302gem_config },
3996         { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
3997         { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
3998         { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
3999         { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
4000         { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
4001         { .compatible = "cdns,emac", .data = &emac_config },
4002         { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
4003         { .compatible = "cdns,zynq-gem", .data = &zynq_config },
4004         { /* sentinel */ }
4005 };
4006 MODULE_DEVICE_TABLE(of, macb_dt_ids);
4007 #endif /* CONFIG_OF */
4008
4009 static const struct macb_config default_gem_config = {
4010         .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
4011                         MACB_CAPS_JUMBO |
4012                         MACB_CAPS_GEM_HAS_PTP,
4013         .dma_burst_length = 16,
4014         .clk_init = macb_clk_init,
4015         .init = macb_init,
4016         .jumbo_max_len = 10240,
4017 };
4018
4019 static int macb_probe(struct platform_device *pdev)
4020 {
4021         const struct macb_config *macb_config = &default_gem_config;
4022         int (*clk_init)(struct platform_device *, struct clk **,
4023                         struct clk **, struct clk **,  struct clk **)
4024                                               = macb_config->clk_init;
4025         int (*init)(struct platform_device *) = macb_config->init;
4026         struct device_node *np = pdev->dev.of_node;
4027         struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
4028         unsigned int queue_mask, num_queues;
4029         struct macb_platform_data *pdata;
4030         bool native_io;
4031         struct phy_device *phydev;
4032         struct net_device *dev;
4033         struct resource *regs;
4034         void __iomem *mem;
4035         const char *mac;
4036         struct macb *bp;
4037         int err, val;
4038
4039         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4040         mem = devm_ioremap_resource(&pdev->dev, regs);
4041         if (IS_ERR(mem))
4042                 return PTR_ERR(mem);
4043
4044         if (np) {
4045                 const struct of_device_id *match;
4046
4047                 match = of_match_node(macb_dt_ids, np);
4048                 if (match && match->data) {
4049                         macb_config = match->data;
4050                         clk_init = macb_config->clk_init;
4051                         init = macb_config->init;
4052                 }
4053         }
4054
4055         err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
4056         if (err)
4057                 return err;
4058
4059         native_io = hw_is_native_io(mem);
4060
4061         macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
4062         dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
4063         if (!dev) {
4064                 err = -ENOMEM;
4065                 goto err_disable_clocks;
4066         }
4067
4068         dev->base_addr = regs->start;
4069
4070         SET_NETDEV_DEV(dev, &pdev->dev);
4071
4072         bp = netdev_priv(dev);
4073         bp->pdev = pdev;
4074         bp->dev = dev;
4075         bp->regs = mem;
4076         bp->native_io = native_io;
4077         if (native_io) {
4078                 bp->macb_reg_readl = hw_readl_native;
4079                 bp->macb_reg_writel = hw_writel_native;
4080         } else {
4081                 bp->macb_reg_readl = hw_readl;
4082                 bp->macb_reg_writel = hw_writel;
4083         }
4084         bp->num_queues = num_queues;
4085         bp->queue_mask = queue_mask;
4086         if (macb_config)
4087                 bp->dma_burst_length = macb_config->dma_burst_length;
4088         bp->pclk = pclk;
4089         bp->hclk = hclk;
4090         bp->tx_clk = tx_clk;
4091         bp->rx_clk = rx_clk;
4092         if (macb_config)
4093                 bp->jumbo_max_len = macb_config->jumbo_max_len;
4094
4095         bp->wol = 0;
4096         if (of_get_property(np, "magic-packet", NULL))
4097                 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
4098         device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
4099
4100         spin_lock_init(&bp->lock);
4101
4102         /* setup capabilities */
4103         macb_configure_caps(bp, macb_config);
4104
4105 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
4106         if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
4107                 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
4108                 bp->hw_dma_cap |= HW_DMA_CAP_64B;
4109         }
4110 #endif
4111         platform_set_drvdata(pdev, dev);
4112
4113         dev->irq = platform_get_irq(pdev, 0);
4114         if (dev->irq < 0) {
4115                 err = dev->irq;
4116                 goto err_out_free_netdev;
4117         }
4118
4119         /* MTU range: 68 - 1500 or 10240 */
4120         dev->min_mtu = GEM_MTU_MIN_SIZE;
4121         if (bp->caps & MACB_CAPS_JUMBO)
4122                 dev->max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
4123         else
4124                 dev->max_mtu = ETH_DATA_LEN;
4125
4126         if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
4127                 val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
4128                 if (val)
4129                         bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
4130                                                 macb_dma_desc_get_size(bp);
4131
4132                 val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
4133                 if (val)
4134                         bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
4135                                                 macb_dma_desc_get_size(bp);
4136         }
4137
4138         bp->rx_intr_mask = MACB_RX_INT_FLAGS;
4139         if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
4140                 bp->rx_intr_mask |= MACB_BIT(RXUBR);
4141
4142         mac = of_get_mac_address(np);
4143         if (mac) {
4144                 ether_addr_copy(bp->dev->dev_addr, mac);
4145         } else {
4146                 err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
4147                 if (err) {
4148                         if (err == -EPROBE_DEFER)
4149                                 goto err_out_free_netdev;
4150                         macb_get_hwaddr(bp);
4151                 }
4152         }
4153
4154         err = of_get_phy_mode(np);
4155         if (err < 0) {
4156                 pdata = dev_get_platdata(&pdev->dev);
4157                 if (pdata && pdata->is_rmii)
4158                         bp->phy_interface = PHY_INTERFACE_MODE_RMII;
4159                 else
4160                         bp->phy_interface = PHY_INTERFACE_MODE_MII;
4161         } else {
4162                 bp->phy_interface = err;
4163         }
4164
4165         /* IP specific init */
4166         err = init(pdev);
4167         if (err)
4168                 goto err_out_free_netdev;
4169
4170         err = macb_mii_init(bp);
4171         if (err)
4172                 goto err_out_free_netdev;
4173
4174         phydev = dev->phydev;
4175
4176         netif_carrier_off(dev);
4177
4178         err = register_netdev(dev);
4179         if (err) {
4180                 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
4181                 goto err_out_unregister_mdio;
4182         }
4183
4184         tasklet_init(&bp->hresp_err_tasklet, macb_hresp_error_task,
4185                      (unsigned long)bp);
4186
4187         phy_attached_info(phydev);
4188
4189         netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
4190                     macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
4191                     dev->base_addr, dev->irq, dev->dev_addr);
4192
4193         return 0;
4194
4195 err_out_unregister_mdio:
4196         phy_disconnect(dev->phydev);
4197         mdiobus_unregister(bp->mii_bus);
4198         of_node_put(bp->phy_node);
4199         if (np && of_phy_is_fixed_link(np))
4200                 of_phy_deregister_fixed_link(np);
4201         mdiobus_free(bp->mii_bus);
4202
4203 err_out_free_netdev:
4204         free_netdev(dev);
4205
4206 err_disable_clocks:
4207         clk_disable_unprepare(tx_clk);
4208         clk_disable_unprepare(hclk);
4209         clk_disable_unprepare(pclk);
4210         clk_disable_unprepare(rx_clk);
4211
4212         return err;
4213 }
4214
4215 static int macb_remove(struct platform_device *pdev)
4216 {
4217         struct net_device *dev;
4218         struct macb *bp;
4219         struct device_node *np = pdev->dev.of_node;
4220
4221         dev = platform_get_drvdata(pdev);
4222
4223         if (dev) {
4224                 bp = netdev_priv(dev);
4225                 if (dev->phydev)
4226                         phy_disconnect(dev->phydev);
4227                 mdiobus_unregister(bp->mii_bus);
4228                 if (np && of_phy_is_fixed_link(np))
4229                         of_phy_deregister_fixed_link(np);
4230                 dev->phydev = NULL;
4231                 mdiobus_free(bp->mii_bus);
4232
4233                 unregister_netdev(dev);
4234                 tasklet_kill(&bp->hresp_err_tasklet);
4235                 clk_disable_unprepare(bp->tx_clk);
4236                 clk_disable_unprepare(bp->hclk);
4237                 clk_disable_unprepare(bp->pclk);
4238                 clk_disable_unprepare(bp->rx_clk);
4239                 of_node_put(bp->phy_node);
4240                 free_netdev(dev);
4241         }
4242
4243         return 0;
4244 }
4245
4246 static int __maybe_unused macb_suspend(struct device *dev)
4247 {
4248         struct platform_device *pdev = to_platform_device(dev);
4249         struct net_device *netdev = platform_get_drvdata(pdev);
4250         struct macb *bp = netdev_priv(netdev);
4251
4252         netif_carrier_off(netdev);
4253         netif_device_detach(netdev);
4254
4255         if (bp->wol & MACB_WOL_ENABLED) {
4256                 macb_writel(bp, IER, MACB_BIT(WOL));
4257                 macb_writel(bp, WOL, MACB_BIT(MAG));
4258                 enable_irq_wake(bp->queues[0].irq);
4259         } else {
4260                 clk_disable_unprepare(bp->tx_clk);
4261                 clk_disable_unprepare(bp->hclk);
4262                 clk_disable_unprepare(bp->pclk);
4263                 clk_disable_unprepare(bp->rx_clk);
4264         }
4265
4266         return 0;
4267 }
4268
4269 static int __maybe_unused macb_resume(struct device *dev)
4270 {
4271         struct platform_device *pdev = to_platform_device(dev);
4272         struct net_device *netdev = platform_get_drvdata(pdev);
4273         struct macb *bp = netdev_priv(netdev);
4274
4275         if (bp->wol & MACB_WOL_ENABLED) {
4276                 macb_writel(bp, IDR, MACB_BIT(WOL));
4277                 macb_writel(bp, WOL, 0);
4278                 disable_irq_wake(bp->queues[0].irq);
4279         } else {
4280                 clk_prepare_enable(bp->pclk);
4281                 clk_prepare_enable(bp->hclk);
4282                 clk_prepare_enable(bp->tx_clk);
4283                 clk_prepare_enable(bp->rx_clk);
4284         }
4285
4286         netif_device_attach(netdev);
4287
4288         return 0;
4289 }
4290
4291 static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
4292
4293 static struct platform_driver macb_driver = {
4294         .probe          = macb_probe,
4295         .remove         = macb_remove,
4296         .driver         = {
4297                 .name           = "macb",
4298                 .of_match_table = of_match_ptr(macb_dt_ids),
4299                 .pm     = &macb_pm_ops,
4300         },
4301 };
4302
4303 module_platform_driver(macb_driver);
4304
4305 MODULE_LICENSE("GPL");
4306 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
4307 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
4308 MODULE_ALIAS("platform:macb");