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