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