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