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