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