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