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