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