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