• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom GENET (Gigabit Ethernet) controller driver
4  *
5  * Copyright (c) 2014-2020 Broadcom
6  */
7 
8 #define pr_fmt(fmt)				"bcmgenet: " fmt
9 
10 #include <linux/acpi.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/interrupt.h>
17 #include <linux/string.h>
18 #include <linux/if_ether.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/pm.h>
25 #include <linux/clk.h>
26 #include <net/arp.h>
27 
28 #include <linux/mii.h>
29 #include <linux/ethtool.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/in.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/phy.h>
38 #include <linux/platform_data/bcmgenet.h>
39 
40 #include <asm/unaligned.h>
41 
42 #include "bcmgenet.h"
43 
44 /* Maximum number of hardware queues, downsized if needed */
45 #define GENET_MAX_MQ_CNT	4
46 
47 /* Default highest priority queue for multi queue support */
48 #define GENET_Q0_PRIORITY	0
49 
50 #define GENET_Q16_RX_BD_CNT	\
51 	(TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
52 #define GENET_Q16_TX_BD_CNT	\
53 	(TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
54 
55 #define RX_BUF_LENGTH		2048
56 #define SKB_ALIGNMENT		32
57 
58 /* Tx/Rx DMA register offset, skip 256 descriptors */
59 #define WORDS_PER_BD(p)		(p->hw_params->words_per_bd)
60 #define DMA_DESC_SIZE		(WORDS_PER_BD(priv) * sizeof(u32))
61 
62 #define GENET_TDMA_REG_OFF	(priv->hw_params->tdma_offset + \
63 				TOTAL_DESC * DMA_DESC_SIZE)
64 
65 #define GENET_RDMA_REG_OFF	(priv->hw_params->rdma_offset + \
66 				TOTAL_DESC * DMA_DESC_SIZE)
67 
68 /* Forward declarations */
69 static void bcmgenet_set_rx_mode(struct net_device *dev);
70 
bcmgenet_writel(u32 value,void __iomem * offset)71 static inline void bcmgenet_writel(u32 value, void __iomem *offset)
72 {
73 	/* MIPS chips strapped for BE will automagically configure the
74 	 * peripheral registers for CPU-native byte order.
75 	 */
76 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
77 		__raw_writel(value, offset);
78 	else
79 		writel_relaxed(value, offset);
80 }
81 
bcmgenet_readl(void __iomem * offset)82 static inline u32 bcmgenet_readl(void __iomem *offset)
83 {
84 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
85 		return __raw_readl(offset);
86 	else
87 		return readl_relaxed(offset);
88 }
89 
dmadesc_set_length_status(struct bcmgenet_priv * priv,void __iomem * d,u32 value)90 static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
91 					     void __iomem *d, u32 value)
92 {
93 	bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS);
94 }
95 
dmadesc_set_addr(struct bcmgenet_priv * priv,void __iomem * d,dma_addr_t addr)96 static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
97 				    void __iomem *d,
98 				    dma_addr_t addr)
99 {
100 	bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
101 
102 	/* Register writes to GISB bus can take couple hundred nanoseconds
103 	 * and are done for each packet, save these expensive writes unless
104 	 * the platform is explicitly configured for 64-bits/LPAE.
105 	 */
106 #ifdef CONFIG_PHYS_ADDR_T_64BIT
107 	if (priv->hw_params->flags & GENET_HAS_40BITS)
108 		bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
109 #endif
110 }
111 
112 /* Combined address + length/status setter */
dmadesc_set(struct bcmgenet_priv * priv,void __iomem * d,dma_addr_t addr,u32 val)113 static inline void dmadesc_set(struct bcmgenet_priv *priv,
114 			       void __iomem *d, dma_addr_t addr, u32 val)
115 {
116 	dmadesc_set_addr(priv, d, addr);
117 	dmadesc_set_length_status(priv, d, val);
118 }
119 
dmadesc_get_addr(struct bcmgenet_priv * priv,void __iomem * d)120 static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
121 					  void __iomem *d)
122 {
123 	dma_addr_t addr;
124 
125 	addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO);
126 
127 	/* Register writes to GISB bus can take couple hundred nanoseconds
128 	 * and are done for each packet, save these expensive writes unless
129 	 * the platform is explicitly configured for 64-bits/LPAE.
130 	 */
131 #ifdef CONFIG_PHYS_ADDR_T_64BIT
132 	if (priv->hw_params->flags & GENET_HAS_40BITS)
133 		addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32;
134 #endif
135 	return addr;
136 }
137 
138 #define GENET_VER_FMT	"%1d.%1d EPHY: 0x%04x"
139 
140 #define GENET_MSG_DEFAULT	(NETIF_MSG_DRV | NETIF_MSG_PROBE | \
141 				NETIF_MSG_LINK)
142 
bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv * priv)143 static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
144 {
145 	if (GENET_IS_V1(priv))
146 		return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
147 	else
148 		return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
149 }
150 
bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv * priv,u32 val)151 static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
152 {
153 	if (GENET_IS_V1(priv))
154 		bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
155 	else
156 		bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
157 }
158 
159 /* These macros are defined to deal with register map change
160  * between GENET1.1 and GENET2. Only those currently being used
161  * by driver are defined.
162  */
bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv * priv)163 static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
164 {
165 	if (GENET_IS_V1(priv))
166 		return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
167 	else
168 		return bcmgenet_readl(priv->base +
169 				      priv->hw_params->tbuf_offset + TBUF_CTRL);
170 }
171 
bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv * priv,u32 val)172 static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
173 {
174 	if (GENET_IS_V1(priv))
175 		bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
176 	else
177 		bcmgenet_writel(val, priv->base +
178 				priv->hw_params->tbuf_offset + TBUF_CTRL);
179 }
180 
bcmgenet_bp_mc_get(struct bcmgenet_priv * priv)181 static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
182 {
183 	if (GENET_IS_V1(priv))
184 		return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
185 	else
186 		return bcmgenet_readl(priv->base +
187 				      priv->hw_params->tbuf_offset + TBUF_BP_MC);
188 }
189 
bcmgenet_bp_mc_set(struct bcmgenet_priv * priv,u32 val)190 static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
191 {
192 	if (GENET_IS_V1(priv))
193 		bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
194 	else
195 		bcmgenet_writel(val, priv->base +
196 				priv->hw_params->tbuf_offset + TBUF_BP_MC);
197 }
198 
199 /* RX/TX DMA register accessors */
200 enum dma_reg {
201 	DMA_RING_CFG = 0,
202 	DMA_CTRL,
203 	DMA_STATUS,
204 	DMA_SCB_BURST_SIZE,
205 	DMA_ARB_CTRL,
206 	DMA_PRIORITY_0,
207 	DMA_PRIORITY_1,
208 	DMA_PRIORITY_2,
209 	DMA_INDEX2RING_0,
210 	DMA_INDEX2RING_1,
211 	DMA_INDEX2RING_2,
212 	DMA_INDEX2RING_3,
213 	DMA_INDEX2RING_4,
214 	DMA_INDEX2RING_5,
215 	DMA_INDEX2RING_6,
216 	DMA_INDEX2RING_7,
217 	DMA_RING0_TIMEOUT,
218 	DMA_RING1_TIMEOUT,
219 	DMA_RING2_TIMEOUT,
220 	DMA_RING3_TIMEOUT,
221 	DMA_RING4_TIMEOUT,
222 	DMA_RING5_TIMEOUT,
223 	DMA_RING6_TIMEOUT,
224 	DMA_RING7_TIMEOUT,
225 	DMA_RING8_TIMEOUT,
226 	DMA_RING9_TIMEOUT,
227 	DMA_RING10_TIMEOUT,
228 	DMA_RING11_TIMEOUT,
229 	DMA_RING12_TIMEOUT,
230 	DMA_RING13_TIMEOUT,
231 	DMA_RING14_TIMEOUT,
232 	DMA_RING15_TIMEOUT,
233 	DMA_RING16_TIMEOUT,
234 };
235 
236 static const u8 bcmgenet_dma_regs_v3plus[] = {
237 	[DMA_RING_CFG]		= 0x00,
238 	[DMA_CTRL]		= 0x04,
239 	[DMA_STATUS]		= 0x08,
240 	[DMA_SCB_BURST_SIZE]	= 0x0C,
241 	[DMA_ARB_CTRL]		= 0x2C,
242 	[DMA_PRIORITY_0]	= 0x30,
243 	[DMA_PRIORITY_1]	= 0x34,
244 	[DMA_PRIORITY_2]	= 0x38,
245 	[DMA_RING0_TIMEOUT]	= 0x2C,
246 	[DMA_RING1_TIMEOUT]	= 0x30,
247 	[DMA_RING2_TIMEOUT]	= 0x34,
248 	[DMA_RING3_TIMEOUT]	= 0x38,
249 	[DMA_RING4_TIMEOUT]	= 0x3c,
250 	[DMA_RING5_TIMEOUT]	= 0x40,
251 	[DMA_RING6_TIMEOUT]	= 0x44,
252 	[DMA_RING7_TIMEOUT]	= 0x48,
253 	[DMA_RING8_TIMEOUT]	= 0x4c,
254 	[DMA_RING9_TIMEOUT]	= 0x50,
255 	[DMA_RING10_TIMEOUT]	= 0x54,
256 	[DMA_RING11_TIMEOUT]	= 0x58,
257 	[DMA_RING12_TIMEOUT]	= 0x5c,
258 	[DMA_RING13_TIMEOUT]	= 0x60,
259 	[DMA_RING14_TIMEOUT]	= 0x64,
260 	[DMA_RING15_TIMEOUT]	= 0x68,
261 	[DMA_RING16_TIMEOUT]	= 0x6C,
262 	[DMA_INDEX2RING_0]	= 0x70,
263 	[DMA_INDEX2RING_1]	= 0x74,
264 	[DMA_INDEX2RING_2]	= 0x78,
265 	[DMA_INDEX2RING_3]	= 0x7C,
266 	[DMA_INDEX2RING_4]	= 0x80,
267 	[DMA_INDEX2RING_5]	= 0x84,
268 	[DMA_INDEX2RING_6]	= 0x88,
269 	[DMA_INDEX2RING_7]	= 0x8C,
270 };
271 
272 static const u8 bcmgenet_dma_regs_v2[] = {
273 	[DMA_RING_CFG]		= 0x00,
274 	[DMA_CTRL]		= 0x04,
275 	[DMA_STATUS]		= 0x08,
276 	[DMA_SCB_BURST_SIZE]	= 0x0C,
277 	[DMA_ARB_CTRL]		= 0x30,
278 	[DMA_PRIORITY_0]	= 0x34,
279 	[DMA_PRIORITY_1]	= 0x38,
280 	[DMA_PRIORITY_2]	= 0x3C,
281 	[DMA_RING0_TIMEOUT]	= 0x2C,
282 	[DMA_RING1_TIMEOUT]	= 0x30,
283 	[DMA_RING2_TIMEOUT]	= 0x34,
284 	[DMA_RING3_TIMEOUT]	= 0x38,
285 	[DMA_RING4_TIMEOUT]	= 0x3c,
286 	[DMA_RING5_TIMEOUT]	= 0x40,
287 	[DMA_RING6_TIMEOUT]	= 0x44,
288 	[DMA_RING7_TIMEOUT]	= 0x48,
289 	[DMA_RING8_TIMEOUT]	= 0x4c,
290 	[DMA_RING9_TIMEOUT]	= 0x50,
291 	[DMA_RING10_TIMEOUT]	= 0x54,
292 	[DMA_RING11_TIMEOUT]	= 0x58,
293 	[DMA_RING12_TIMEOUT]	= 0x5c,
294 	[DMA_RING13_TIMEOUT]	= 0x60,
295 	[DMA_RING14_TIMEOUT]	= 0x64,
296 	[DMA_RING15_TIMEOUT]	= 0x68,
297 	[DMA_RING16_TIMEOUT]	= 0x6C,
298 };
299 
300 static const u8 bcmgenet_dma_regs_v1[] = {
301 	[DMA_CTRL]		= 0x00,
302 	[DMA_STATUS]		= 0x04,
303 	[DMA_SCB_BURST_SIZE]	= 0x0C,
304 	[DMA_ARB_CTRL]		= 0x30,
305 	[DMA_PRIORITY_0]	= 0x34,
306 	[DMA_PRIORITY_1]	= 0x38,
307 	[DMA_PRIORITY_2]	= 0x3C,
308 	[DMA_RING0_TIMEOUT]	= 0x2C,
309 	[DMA_RING1_TIMEOUT]	= 0x30,
310 	[DMA_RING2_TIMEOUT]	= 0x34,
311 	[DMA_RING3_TIMEOUT]	= 0x38,
312 	[DMA_RING4_TIMEOUT]	= 0x3c,
313 	[DMA_RING5_TIMEOUT]	= 0x40,
314 	[DMA_RING6_TIMEOUT]	= 0x44,
315 	[DMA_RING7_TIMEOUT]	= 0x48,
316 	[DMA_RING8_TIMEOUT]	= 0x4c,
317 	[DMA_RING9_TIMEOUT]	= 0x50,
318 	[DMA_RING10_TIMEOUT]	= 0x54,
319 	[DMA_RING11_TIMEOUT]	= 0x58,
320 	[DMA_RING12_TIMEOUT]	= 0x5c,
321 	[DMA_RING13_TIMEOUT]	= 0x60,
322 	[DMA_RING14_TIMEOUT]	= 0x64,
323 	[DMA_RING15_TIMEOUT]	= 0x68,
324 	[DMA_RING16_TIMEOUT]	= 0x6C,
325 };
326 
327 /* Set at runtime once bcmgenet version is known */
328 static const u8 *bcmgenet_dma_regs;
329 
dev_to_priv(struct device * dev)330 static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
331 {
332 	return netdev_priv(dev_get_drvdata(dev));
333 }
334 
bcmgenet_tdma_readl(struct bcmgenet_priv * priv,enum dma_reg r)335 static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
336 				      enum dma_reg r)
337 {
338 	return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
339 			      DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
340 }
341 
bcmgenet_tdma_writel(struct bcmgenet_priv * priv,u32 val,enum dma_reg r)342 static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
343 					u32 val, enum dma_reg r)
344 {
345 	bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
346 			DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
347 }
348 
bcmgenet_rdma_readl(struct bcmgenet_priv * priv,enum dma_reg r)349 static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
350 				      enum dma_reg r)
351 {
352 	return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
353 			      DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
354 }
355 
bcmgenet_rdma_writel(struct bcmgenet_priv * priv,u32 val,enum dma_reg r)356 static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
357 					u32 val, enum dma_reg r)
358 {
359 	bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
360 			DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
361 }
362 
363 /* RDMA/TDMA ring registers and accessors
364  * we merge the common fields and just prefix with T/D the registers
365  * having different meaning depending on the direction
366  */
367 enum dma_ring_reg {
368 	TDMA_READ_PTR = 0,
369 	RDMA_WRITE_PTR = TDMA_READ_PTR,
370 	TDMA_READ_PTR_HI,
371 	RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
372 	TDMA_CONS_INDEX,
373 	RDMA_PROD_INDEX = TDMA_CONS_INDEX,
374 	TDMA_PROD_INDEX,
375 	RDMA_CONS_INDEX = TDMA_PROD_INDEX,
376 	DMA_RING_BUF_SIZE,
377 	DMA_START_ADDR,
378 	DMA_START_ADDR_HI,
379 	DMA_END_ADDR,
380 	DMA_END_ADDR_HI,
381 	DMA_MBUF_DONE_THRESH,
382 	TDMA_FLOW_PERIOD,
383 	RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
384 	TDMA_WRITE_PTR,
385 	RDMA_READ_PTR = TDMA_WRITE_PTR,
386 	TDMA_WRITE_PTR_HI,
387 	RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
388 };
389 
390 /* GENET v4 supports 40-bits pointer addressing
391  * for obvious reasons the LO and HI word parts
392  * are contiguous, but this offsets the other
393  * registers.
394  */
395 static const u8 genet_dma_ring_regs_v4[] = {
396 	[TDMA_READ_PTR]			= 0x00,
397 	[TDMA_READ_PTR_HI]		= 0x04,
398 	[TDMA_CONS_INDEX]		= 0x08,
399 	[TDMA_PROD_INDEX]		= 0x0C,
400 	[DMA_RING_BUF_SIZE]		= 0x10,
401 	[DMA_START_ADDR]		= 0x14,
402 	[DMA_START_ADDR_HI]		= 0x18,
403 	[DMA_END_ADDR]			= 0x1C,
404 	[DMA_END_ADDR_HI]		= 0x20,
405 	[DMA_MBUF_DONE_THRESH]		= 0x24,
406 	[TDMA_FLOW_PERIOD]		= 0x28,
407 	[TDMA_WRITE_PTR]		= 0x2C,
408 	[TDMA_WRITE_PTR_HI]		= 0x30,
409 };
410 
411 static const u8 genet_dma_ring_regs_v123[] = {
412 	[TDMA_READ_PTR]			= 0x00,
413 	[TDMA_CONS_INDEX]		= 0x04,
414 	[TDMA_PROD_INDEX]		= 0x08,
415 	[DMA_RING_BUF_SIZE]		= 0x0C,
416 	[DMA_START_ADDR]		= 0x10,
417 	[DMA_END_ADDR]			= 0x14,
418 	[DMA_MBUF_DONE_THRESH]		= 0x18,
419 	[TDMA_FLOW_PERIOD]		= 0x1C,
420 	[TDMA_WRITE_PTR]		= 0x20,
421 };
422 
423 /* Set at runtime once GENET version is known */
424 static const u8 *genet_dma_ring_regs;
425 
bcmgenet_tdma_ring_readl(struct bcmgenet_priv * priv,unsigned int ring,enum dma_ring_reg r)426 static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
427 					   unsigned int ring,
428 					   enum dma_ring_reg r)
429 {
430 	return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
431 			      (DMA_RING_SIZE * ring) +
432 			      genet_dma_ring_regs[r]);
433 }
434 
bcmgenet_tdma_ring_writel(struct bcmgenet_priv * priv,unsigned int ring,u32 val,enum dma_ring_reg r)435 static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
436 					     unsigned int ring, u32 val,
437 					     enum dma_ring_reg r)
438 {
439 	bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
440 			(DMA_RING_SIZE * ring) +
441 			genet_dma_ring_regs[r]);
442 }
443 
bcmgenet_rdma_ring_readl(struct bcmgenet_priv * priv,unsigned int ring,enum dma_ring_reg r)444 static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
445 					   unsigned int ring,
446 					   enum dma_ring_reg r)
447 {
448 	return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
449 			      (DMA_RING_SIZE * ring) +
450 			      genet_dma_ring_regs[r]);
451 }
452 
bcmgenet_rdma_ring_writel(struct bcmgenet_priv * priv,unsigned int ring,u32 val,enum dma_ring_reg r)453 static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
454 					     unsigned int ring, u32 val,
455 					     enum dma_ring_reg r)
456 {
457 	bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
458 			(DMA_RING_SIZE * ring) +
459 			genet_dma_ring_regs[r]);
460 }
461 
bcmgenet_hfb_enable_filter(struct bcmgenet_priv * priv,u32 f_index)462 static void bcmgenet_hfb_enable_filter(struct bcmgenet_priv *priv, u32 f_index)
463 {
464 	u32 offset;
465 	u32 reg;
466 
467 	offset = HFB_FLT_ENABLE_V3PLUS + (f_index < 32) * sizeof(u32);
468 	reg = bcmgenet_hfb_reg_readl(priv, offset);
469 	reg |= (1 << (f_index % 32));
470 	bcmgenet_hfb_reg_writel(priv, reg, offset);
471 	reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
472 	reg |= RBUF_HFB_EN;
473 	bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
474 }
475 
bcmgenet_hfb_disable_filter(struct bcmgenet_priv * priv,u32 f_index)476 static void bcmgenet_hfb_disable_filter(struct bcmgenet_priv *priv, u32 f_index)
477 {
478 	u32 offset, reg, reg1;
479 
480 	offset = HFB_FLT_ENABLE_V3PLUS;
481 	reg = bcmgenet_hfb_reg_readl(priv, offset);
482 	reg1 = bcmgenet_hfb_reg_readl(priv, offset + sizeof(u32));
483 	if  (f_index < 32) {
484 		reg1 &= ~(1 << (f_index % 32));
485 		bcmgenet_hfb_reg_writel(priv, reg1, offset + sizeof(u32));
486 	} else {
487 		reg &= ~(1 << (f_index % 32));
488 		bcmgenet_hfb_reg_writel(priv, reg, offset);
489 	}
490 	if (!reg && !reg1) {
491 		reg = bcmgenet_hfb_reg_readl(priv, HFB_CTRL);
492 		reg &= ~RBUF_HFB_EN;
493 		bcmgenet_hfb_reg_writel(priv, reg, HFB_CTRL);
494 	}
495 }
496 
bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv * priv,u32 f_index,u32 rx_queue)497 static void bcmgenet_hfb_set_filter_rx_queue_mapping(struct bcmgenet_priv *priv,
498 						     u32 f_index, u32 rx_queue)
499 {
500 	u32 offset;
501 	u32 reg;
502 
503 	offset = f_index / 8;
504 	reg = bcmgenet_rdma_readl(priv, DMA_INDEX2RING_0 + offset);
505 	reg &= ~(0xF << (4 * (f_index % 8)));
506 	reg |= ((rx_queue & 0xF) << (4 * (f_index % 8)));
507 	bcmgenet_rdma_writel(priv, reg, DMA_INDEX2RING_0 + offset);
508 }
509 
bcmgenet_hfb_set_filter_length(struct bcmgenet_priv * priv,u32 f_index,u32 f_length)510 static void bcmgenet_hfb_set_filter_length(struct bcmgenet_priv *priv,
511 					   u32 f_index, u32 f_length)
512 {
513 	u32 offset;
514 	u32 reg;
515 
516 	offset = HFB_FLT_LEN_V3PLUS +
517 		 ((priv->hw_params->hfb_filter_cnt - 1 - f_index) / 4) *
518 		 sizeof(u32);
519 	reg = bcmgenet_hfb_reg_readl(priv, offset);
520 	reg &= ~(0xFF << (8 * (f_index % 4)));
521 	reg |= ((f_length & 0xFF) << (8 * (f_index % 4)));
522 	bcmgenet_hfb_reg_writel(priv, reg, offset);
523 }
524 
bcmgenet_hfb_validate_mask(void * mask,size_t size)525 static int bcmgenet_hfb_validate_mask(void *mask, size_t size)
526 {
527 	while (size) {
528 		switch (*(unsigned char *)mask++) {
529 		case 0x00:
530 		case 0x0f:
531 		case 0xf0:
532 		case 0xff:
533 			size--;
534 			continue;
535 		default:
536 			return -EINVAL;
537 		}
538 	}
539 
540 	return 0;
541 }
542 
543 #define VALIDATE_MASK(x) \
544 	bcmgenet_hfb_validate_mask(&(x), sizeof(x))
545 
bcmgenet_hfb_insert_data(struct bcmgenet_priv * priv,u32 f_index,u32 offset,void * val,void * mask,size_t size)546 static int bcmgenet_hfb_insert_data(struct bcmgenet_priv *priv, u32 f_index,
547 				    u32 offset, void *val, void *mask,
548 				    size_t size)
549 {
550 	u32 index, tmp;
551 
552 	index = f_index * priv->hw_params->hfb_filter_size + offset / 2;
553 	tmp = bcmgenet_hfb_readl(priv, index * sizeof(u32));
554 
555 	while (size--) {
556 		if (offset++ & 1) {
557 			tmp &= ~0x300FF;
558 			tmp |= (*(unsigned char *)val++);
559 			switch ((*(unsigned char *)mask++)) {
560 			case 0xFF:
561 				tmp |= 0x30000;
562 				break;
563 			case 0xF0:
564 				tmp |= 0x20000;
565 				break;
566 			case 0x0F:
567 				tmp |= 0x10000;
568 				break;
569 			}
570 			bcmgenet_hfb_writel(priv, tmp, index++ * sizeof(u32));
571 			if (size)
572 				tmp = bcmgenet_hfb_readl(priv,
573 							 index * sizeof(u32));
574 		} else {
575 			tmp &= ~0xCFF00;
576 			tmp |= (*(unsigned char *)val++) << 8;
577 			switch ((*(unsigned char *)mask++)) {
578 			case 0xFF:
579 				tmp |= 0xC0000;
580 				break;
581 			case 0xF0:
582 				tmp |= 0x80000;
583 				break;
584 			case 0x0F:
585 				tmp |= 0x40000;
586 				break;
587 			}
588 			if (!size)
589 				bcmgenet_hfb_writel(priv, tmp, index * sizeof(u32));
590 		}
591 	}
592 
593 	return 0;
594 }
595 
bcmgenet_hfb_create_rxnfc_filter(struct bcmgenet_priv * priv,struct bcmgenet_rxnfc_rule * rule)596 static void bcmgenet_hfb_create_rxnfc_filter(struct bcmgenet_priv *priv,
597 					     struct bcmgenet_rxnfc_rule *rule)
598 {
599 	struct ethtool_rx_flow_spec *fs = &rule->fs;
600 	u32 offset = 0, f_length = 0, f;
601 	u8 val_8, mask_8;
602 	__be16 val_16;
603 	u16 mask_16;
604 	size_t size;
605 
606 	f = fs->location;
607 	if (fs->flow_type & FLOW_MAC_EXT) {
608 		bcmgenet_hfb_insert_data(priv, f, 0,
609 					 &fs->h_ext.h_dest, &fs->m_ext.h_dest,
610 					 sizeof(fs->h_ext.h_dest));
611 	}
612 
613 	if (fs->flow_type & FLOW_EXT) {
614 		if (fs->m_ext.vlan_etype ||
615 		    fs->m_ext.vlan_tci) {
616 			bcmgenet_hfb_insert_data(priv, f, 12,
617 						 &fs->h_ext.vlan_etype,
618 						 &fs->m_ext.vlan_etype,
619 						 sizeof(fs->h_ext.vlan_etype));
620 			bcmgenet_hfb_insert_data(priv, f, 14,
621 						 &fs->h_ext.vlan_tci,
622 						 &fs->m_ext.vlan_tci,
623 						 sizeof(fs->h_ext.vlan_tci));
624 			offset += VLAN_HLEN;
625 			f_length += DIV_ROUND_UP(VLAN_HLEN, 2);
626 		}
627 	}
628 
629 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
630 	case ETHER_FLOW:
631 		f_length += DIV_ROUND_UP(ETH_HLEN, 2);
632 		bcmgenet_hfb_insert_data(priv, f, 0,
633 					 &fs->h_u.ether_spec.h_dest,
634 					 &fs->m_u.ether_spec.h_dest,
635 					 sizeof(fs->h_u.ether_spec.h_dest));
636 		bcmgenet_hfb_insert_data(priv, f, ETH_ALEN,
637 					 &fs->h_u.ether_spec.h_source,
638 					 &fs->m_u.ether_spec.h_source,
639 					 sizeof(fs->h_u.ether_spec.h_source));
640 		bcmgenet_hfb_insert_data(priv, f, (2 * ETH_ALEN) + offset,
641 					 &fs->h_u.ether_spec.h_proto,
642 					 &fs->m_u.ether_spec.h_proto,
643 					 sizeof(fs->h_u.ether_spec.h_proto));
644 		break;
645 	case IP_USER_FLOW:
646 		f_length += DIV_ROUND_UP(ETH_HLEN + 20, 2);
647 		/* Specify IP Ether Type */
648 		val_16 = htons(ETH_P_IP);
649 		mask_16 = 0xFFFF;
650 		bcmgenet_hfb_insert_data(priv, f, (2 * ETH_ALEN) + offset,
651 					 &val_16, &mask_16, sizeof(val_16));
652 		bcmgenet_hfb_insert_data(priv, f, 15 + offset,
653 					 &fs->h_u.usr_ip4_spec.tos,
654 					 &fs->m_u.usr_ip4_spec.tos,
655 					 sizeof(fs->h_u.usr_ip4_spec.tos));
656 		bcmgenet_hfb_insert_data(priv, f, 23 + offset,
657 					 &fs->h_u.usr_ip4_spec.proto,
658 					 &fs->m_u.usr_ip4_spec.proto,
659 					 sizeof(fs->h_u.usr_ip4_spec.proto));
660 		bcmgenet_hfb_insert_data(priv, f, 26 + offset,
661 					 &fs->h_u.usr_ip4_spec.ip4src,
662 					 &fs->m_u.usr_ip4_spec.ip4src,
663 					 sizeof(fs->h_u.usr_ip4_spec.ip4src));
664 		bcmgenet_hfb_insert_data(priv, f, 30 + offset,
665 					 &fs->h_u.usr_ip4_spec.ip4dst,
666 					 &fs->m_u.usr_ip4_spec.ip4dst,
667 					 sizeof(fs->h_u.usr_ip4_spec.ip4dst));
668 		if (!fs->m_u.usr_ip4_spec.l4_4_bytes)
669 			break;
670 
671 		/* Only supports 20 byte IPv4 header */
672 		val_8 = 0x45;
673 		mask_8 = 0xFF;
674 		bcmgenet_hfb_insert_data(priv, f, ETH_HLEN + offset,
675 					 &val_8, &mask_8,
676 					 sizeof(val_8));
677 		size = sizeof(fs->h_u.usr_ip4_spec.l4_4_bytes);
678 		bcmgenet_hfb_insert_data(priv, f,
679 					 ETH_HLEN + 20 + offset,
680 					 &fs->h_u.usr_ip4_spec.l4_4_bytes,
681 					 &fs->m_u.usr_ip4_spec.l4_4_bytes,
682 					 size);
683 		f_length += DIV_ROUND_UP(size, 2);
684 		break;
685 	}
686 
687 	bcmgenet_hfb_set_filter_length(priv, f, 2 * f_length);
688 	if (!fs->ring_cookie || fs->ring_cookie == RX_CLS_FLOW_WAKE) {
689 		/* Ring 0 flows can be handled by the default Descriptor Ring
690 		 * We'll map them to ring 0, but don't enable the filter
691 		 */
692 		bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f, 0);
693 		rule->state = BCMGENET_RXNFC_STATE_DISABLED;
694 	} else {
695 		/* Other Rx rings are direct mapped here */
696 		bcmgenet_hfb_set_filter_rx_queue_mapping(priv, f,
697 							 fs->ring_cookie);
698 		bcmgenet_hfb_enable_filter(priv, f);
699 		rule->state = BCMGENET_RXNFC_STATE_ENABLED;
700 	}
701 }
702 
703 /* bcmgenet_hfb_clear
704  *
705  * Clear Hardware Filter Block and disable all filtering.
706  */
bcmgenet_hfb_clear_filter(struct bcmgenet_priv * priv,u32 f_index)707 static void bcmgenet_hfb_clear_filter(struct bcmgenet_priv *priv, u32 f_index)
708 {
709 	u32 base, i;
710 
711 	base = f_index * priv->hw_params->hfb_filter_size;
712 	for (i = 0; i < priv->hw_params->hfb_filter_size; i++)
713 		bcmgenet_hfb_writel(priv, 0x0, (base + i) * sizeof(u32));
714 }
715 
bcmgenet_hfb_clear(struct bcmgenet_priv * priv)716 static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
717 {
718 	u32 i;
719 
720 	if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
721 		return;
722 
723 	bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
724 	bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
725 	bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
726 
727 	for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
728 		bcmgenet_rdma_writel(priv, 0x0, i);
729 
730 	for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
731 		bcmgenet_hfb_reg_writel(priv, 0x0,
732 					HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
733 
734 	for (i = 0; i < priv->hw_params->hfb_filter_cnt; i++)
735 		bcmgenet_hfb_clear_filter(priv, i);
736 }
737 
bcmgenet_hfb_init(struct bcmgenet_priv * priv)738 static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
739 {
740 	int i;
741 
742 	INIT_LIST_HEAD(&priv->rxnfc_list);
743 	if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
744 		return;
745 
746 	for (i = 0; i < MAX_NUM_OF_FS_RULES; i++) {
747 		INIT_LIST_HEAD(&priv->rxnfc_rules[i].list);
748 		priv->rxnfc_rules[i].state = BCMGENET_RXNFC_STATE_UNUSED;
749 	}
750 
751 	bcmgenet_hfb_clear(priv);
752 }
753 
bcmgenet_begin(struct net_device * dev)754 static int bcmgenet_begin(struct net_device *dev)
755 {
756 	struct bcmgenet_priv *priv = netdev_priv(dev);
757 
758 	/* Turn on the clock */
759 	return clk_prepare_enable(priv->clk);
760 }
761 
bcmgenet_complete(struct net_device * dev)762 static void bcmgenet_complete(struct net_device *dev)
763 {
764 	struct bcmgenet_priv *priv = netdev_priv(dev);
765 
766 	/* Turn off the clock */
767 	clk_disable_unprepare(priv->clk);
768 }
769 
bcmgenet_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)770 static int bcmgenet_get_link_ksettings(struct net_device *dev,
771 				       struct ethtool_link_ksettings *cmd)
772 {
773 	if (!netif_running(dev))
774 		return -EINVAL;
775 
776 	if (!dev->phydev)
777 		return -ENODEV;
778 
779 	phy_ethtool_ksettings_get(dev->phydev, cmd);
780 
781 	return 0;
782 }
783 
bcmgenet_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)784 static int bcmgenet_set_link_ksettings(struct net_device *dev,
785 				       const struct ethtool_link_ksettings *cmd)
786 {
787 	if (!netif_running(dev))
788 		return -EINVAL;
789 
790 	if (!dev->phydev)
791 		return -ENODEV;
792 
793 	return phy_ethtool_ksettings_set(dev->phydev, cmd);
794 }
795 
bcmgenet_set_features(struct net_device * dev,netdev_features_t features)796 static int bcmgenet_set_features(struct net_device *dev,
797 				 netdev_features_t features)
798 {
799 	struct bcmgenet_priv *priv = netdev_priv(dev);
800 	u32 reg;
801 	int ret;
802 
803 	ret = clk_prepare_enable(priv->clk);
804 	if (ret)
805 		return ret;
806 
807 	/* Make sure we reflect the value of CRC_CMD_FWD */
808 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
809 	priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
810 
811 	clk_disable_unprepare(priv->clk);
812 
813 	return ret;
814 }
815 
bcmgenet_get_msglevel(struct net_device * dev)816 static u32 bcmgenet_get_msglevel(struct net_device *dev)
817 {
818 	struct bcmgenet_priv *priv = netdev_priv(dev);
819 
820 	return priv->msg_enable;
821 }
822 
bcmgenet_set_msglevel(struct net_device * dev,u32 level)823 static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
824 {
825 	struct bcmgenet_priv *priv = netdev_priv(dev);
826 
827 	priv->msg_enable = level;
828 }
829 
bcmgenet_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)830 static int bcmgenet_get_coalesce(struct net_device *dev,
831 				 struct ethtool_coalesce *ec,
832 				 struct kernel_ethtool_coalesce *kernel_coal,
833 				 struct netlink_ext_ack *extack)
834 {
835 	struct bcmgenet_priv *priv = netdev_priv(dev);
836 	struct bcmgenet_rx_ring *ring;
837 	unsigned int i;
838 
839 	ec->tx_max_coalesced_frames =
840 		bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
841 					 DMA_MBUF_DONE_THRESH);
842 	ec->rx_max_coalesced_frames =
843 		bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
844 					 DMA_MBUF_DONE_THRESH);
845 	ec->rx_coalesce_usecs =
846 		bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
847 
848 	for (i = 0; i < priv->hw_params->rx_queues; i++) {
849 		ring = &priv->rx_rings[i];
850 		ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
851 	}
852 	ring = &priv->rx_rings[DESC_INDEX];
853 	ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
854 
855 	return 0;
856 }
857 
bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring * ring,u32 usecs,u32 pkts)858 static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
859 				     u32 usecs, u32 pkts)
860 {
861 	struct bcmgenet_priv *priv = ring->priv;
862 	unsigned int i = ring->index;
863 	u32 reg;
864 
865 	bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
866 
867 	reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
868 	reg &= ~DMA_TIMEOUT_MASK;
869 	reg |= DIV_ROUND_UP(usecs * 1000, 8192);
870 	bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
871 }
872 
bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring * ring,struct ethtool_coalesce * ec)873 static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
874 					  struct ethtool_coalesce *ec)
875 {
876 	struct dim_cq_moder moder;
877 	u32 usecs, pkts;
878 
879 	ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
880 	ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
881 	usecs = ring->rx_coalesce_usecs;
882 	pkts = ring->rx_max_coalesced_frames;
883 
884 	if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
885 		moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
886 		usecs = moder.usec;
887 		pkts = moder.pkts;
888 	}
889 
890 	ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
891 	bcmgenet_set_rx_coalesce(ring, usecs, pkts);
892 }
893 
bcmgenet_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)894 static int bcmgenet_set_coalesce(struct net_device *dev,
895 				 struct ethtool_coalesce *ec,
896 				 struct kernel_ethtool_coalesce *kernel_coal,
897 				 struct netlink_ext_ack *extack)
898 {
899 	struct bcmgenet_priv *priv = netdev_priv(dev);
900 	unsigned int i;
901 
902 	/* Base system clock is 125Mhz, DMA timeout is this reference clock
903 	 * divided by 1024, which yields roughly 8.192us, our maximum value
904 	 * has to fit in the DMA_TIMEOUT_MASK (16 bits)
905 	 */
906 	if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
907 	    ec->tx_max_coalesced_frames == 0 ||
908 	    ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
909 	    ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
910 		return -EINVAL;
911 
912 	if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
913 		return -EINVAL;
914 
915 	/* GENET TDMA hardware does not support a configurable timeout, but will
916 	 * always generate an interrupt either after MBDONE packets have been
917 	 * transmitted, or when the ring is empty.
918 	 */
919 
920 	/* Program all TX queues with the same values, as there is no
921 	 * ethtool knob to do coalescing on a per-queue basis
922 	 */
923 	for (i = 0; i < priv->hw_params->tx_queues; i++)
924 		bcmgenet_tdma_ring_writel(priv, i,
925 					  ec->tx_max_coalesced_frames,
926 					  DMA_MBUF_DONE_THRESH);
927 	bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
928 				  ec->tx_max_coalesced_frames,
929 				  DMA_MBUF_DONE_THRESH);
930 
931 	for (i = 0; i < priv->hw_params->rx_queues; i++)
932 		bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
933 	bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
934 
935 	return 0;
936 }
937 
938 /* standard ethtool support functions. */
939 enum bcmgenet_stat_type {
940 	BCMGENET_STAT_NETDEV = -1,
941 	BCMGENET_STAT_MIB_RX,
942 	BCMGENET_STAT_MIB_TX,
943 	BCMGENET_STAT_RUNT,
944 	BCMGENET_STAT_MISC,
945 	BCMGENET_STAT_SOFT,
946 };
947 
948 struct bcmgenet_stats {
949 	char stat_string[ETH_GSTRING_LEN];
950 	int stat_sizeof;
951 	int stat_offset;
952 	enum bcmgenet_stat_type type;
953 	/* reg offset from UMAC base for misc counters */
954 	u16 reg_offset;
955 };
956 
957 #define STAT_NETDEV(m) { \
958 	.stat_string = __stringify(m), \
959 	.stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
960 	.stat_offset = offsetof(struct net_device_stats, m), \
961 	.type = BCMGENET_STAT_NETDEV, \
962 }
963 
964 #define STAT_GENET_MIB(str, m, _type) { \
965 	.stat_string = str, \
966 	.stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
967 	.stat_offset = offsetof(struct bcmgenet_priv, m), \
968 	.type = _type, \
969 }
970 
971 #define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
972 #define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
973 #define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
974 #define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
975 
976 #define STAT_GENET_MISC(str, m, offset) { \
977 	.stat_string = str, \
978 	.stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
979 	.stat_offset = offsetof(struct bcmgenet_priv, m), \
980 	.type = BCMGENET_STAT_MISC, \
981 	.reg_offset = offset, \
982 }
983 
984 #define STAT_GENET_Q(num) \
985 	STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
986 			tx_rings[num].packets), \
987 	STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
988 			tx_rings[num].bytes), \
989 	STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
990 			rx_rings[num].bytes),	 \
991 	STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
992 			rx_rings[num].packets), \
993 	STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
994 			rx_rings[num].errors), \
995 	STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
996 			rx_rings[num].dropped)
997 
998 /* There is a 0xC gap between the end of RX and beginning of TX stats and then
999  * between the end of TX stats and the beginning of the RX RUNT
1000  */
1001 #define BCMGENET_STAT_OFFSET	0xc
1002 
1003 /* Hardware counters must be kept in sync because the order/offset
1004  * is important here (order in structure declaration = order in hardware)
1005  */
1006 static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
1007 	/* general stats */
1008 	STAT_NETDEV(rx_packets),
1009 	STAT_NETDEV(tx_packets),
1010 	STAT_NETDEV(rx_bytes),
1011 	STAT_NETDEV(tx_bytes),
1012 	STAT_NETDEV(rx_errors),
1013 	STAT_NETDEV(tx_errors),
1014 	STAT_NETDEV(rx_dropped),
1015 	STAT_NETDEV(tx_dropped),
1016 	STAT_NETDEV(multicast),
1017 	/* UniMAC RSV counters */
1018 	STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
1019 	STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
1020 	STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
1021 	STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
1022 	STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
1023 	STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
1024 	STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
1025 	STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
1026 	STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
1027 	STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
1028 	STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
1029 	STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
1030 	STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
1031 	STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
1032 	STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
1033 	STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
1034 	STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
1035 	STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
1036 	STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
1037 	STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
1038 	STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
1039 	STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
1040 	STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
1041 	STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
1042 	STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
1043 	STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
1044 	STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
1045 	STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
1046 	STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
1047 	/* UniMAC TSV counters */
1048 	STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
1049 	STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
1050 	STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
1051 	STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
1052 	STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
1053 	STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
1054 	STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
1055 	STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
1056 	STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
1057 	STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
1058 	STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
1059 	STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
1060 	STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
1061 	STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
1062 	STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
1063 	STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
1064 	STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
1065 	STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
1066 	STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
1067 	STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
1068 	STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
1069 	STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
1070 	STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
1071 	STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
1072 	STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
1073 	STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
1074 	STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
1075 	STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
1076 	STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
1077 	/* UniMAC RUNT counters */
1078 	STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
1079 	STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
1080 	STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
1081 	STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
1082 	/* Misc UniMAC counters */
1083 	STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
1084 			UMAC_RBUF_OVFL_CNT_V1),
1085 	STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
1086 			UMAC_RBUF_ERR_CNT_V1),
1087 	STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
1088 	STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
1089 	STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
1090 	STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
1091 	STAT_GENET_SOFT_MIB("tx_realloc_tsb", mib.tx_realloc_tsb),
1092 	STAT_GENET_SOFT_MIB("tx_realloc_tsb_failed",
1093 			    mib.tx_realloc_tsb_failed),
1094 	/* Per TX queues */
1095 	STAT_GENET_Q(0),
1096 	STAT_GENET_Q(1),
1097 	STAT_GENET_Q(2),
1098 	STAT_GENET_Q(3),
1099 	STAT_GENET_Q(16),
1100 };
1101 
1102 #define BCMGENET_STATS_LEN	ARRAY_SIZE(bcmgenet_gstrings_stats)
1103 
bcmgenet_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1104 static void bcmgenet_get_drvinfo(struct net_device *dev,
1105 				 struct ethtool_drvinfo *info)
1106 {
1107 	strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
1108 }
1109 
bcmgenet_get_sset_count(struct net_device * dev,int string_set)1110 static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
1111 {
1112 	switch (string_set) {
1113 	case ETH_SS_STATS:
1114 		return BCMGENET_STATS_LEN;
1115 	default:
1116 		return -EOPNOTSUPP;
1117 	}
1118 }
1119 
bcmgenet_get_strings(struct net_device * dev,u32 stringset,u8 * data)1120 static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
1121 				 u8 *data)
1122 {
1123 	int i;
1124 
1125 	switch (stringset) {
1126 	case ETH_SS_STATS:
1127 		for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1128 			memcpy(data + i * ETH_GSTRING_LEN,
1129 			       bcmgenet_gstrings_stats[i].stat_string,
1130 			       ETH_GSTRING_LEN);
1131 		}
1132 		break;
1133 	}
1134 }
1135 
bcmgenet_update_stat_misc(struct bcmgenet_priv * priv,u16 offset)1136 static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
1137 {
1138 	u16 new_offset;
1139 	u32 val;
1140 
1141 	switch (offset) {
1142 	case UMAC_RBUF_OVFL_CNT_V1:
1143 		if (GENET_IS_V2(priv))
1144 			new_offset = RBUF_OVFL_CNT_V2;
1145 		else
1146 			new_offset = RBUF_OVFL_CNT_V3PLUS;
1147 
1148 		val = bcmgenet_rbuf_readl(priv,	new_offset);
1149 		/* clear if overflowed */
1150 		if (val == ~0)
1151 			bcmgenet_rbuf_writel(priv, 0, new_offset);
1152 		break;
1153 	case UMAC_RBUF_ERR_CNT_V1:
1154 		if (GENET_IS_V2(priv))
1155 			new_offset = RBUF_ERR_CNT_V2;
1156 		else
1157 			new_offset = RBUF_ERR_CNT_V3PLUS;
1158 
1159 		val = bcmgenet_rbuf_readl(priv,	new_offset);
1160 		/* clear if overflowed */
1161 		if (val == ~0)
1162 			bcmgenet_rbuf_writel(priv, 0, new_offset);
1163 		break;
1164 	default:
1165 		val = bcmgenet_umac_readl(priv, offset);
1166 		/* clear if overflowed */
1167 		if (val == ~0)
1168 			bcmgenet_umac_writel(priv, 0, offset);
1169 		break;
1170 	}
1171 
1172 	return val;
1173 }
1174 
bcmgenet_update_mib_counters(struct bcmgenet_priv * priv)1175 static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
1176 {
1177 	int i, j = 0;
1178 
1179 	for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1180 		const struct bcmgenet_stats *s;
1181 		u8 offset = 0;
1182 		u32 val = 0;
1183 		char *p;
1184 
1185 		s = &bcmgenet_gstrings_stats[i];
1186 		switch (s->type) {
1187 		case BCMGENET_STAT_NETDEV:
1188 		case BCMGENET_STAT_SOFT:
1189 			continue;
1190 		case BCMGENET_STAT_RUNT:
1191 			offset += BCMGENET_STAT_OFFSET;
1192 			fallthrough;
1193 		case BCMGENET_STAT_MIB_TX:
1194 			offset += BCMGENET_STAT_OFFSET;
1195 			fallthrough;
1196 		case BCMGENET_STAT_MIB_RX:
1197 			val = bcmgenet_umac_readl(priv,
1198 						  UMAC_MIB_START + j + offset);
1199 			offset = 0;	/* Reset Offset */
1200 			break;
1201 		case BCMGENET_STAT_MISC:
1202 			if (GENET_IS_V1(priv)) {
1203 				val = bcmgenet_umac_readl(priv, s->reg_offset);
1204 				/* clear if overflowed */
1205 				if (val == ~0)
1206 					bcmgenet_umac_writel(priv, 0,
1207 							     s->reg_offset);
1208 			} else {
1209 				val = bcmgenet_update_stat_misc(priv,
1210 								s->reg_offset);
1211 			}
1212 			break;
1213 		}
1214 
1215 		j += s->stat_sizeof;
1216 		p = (char *)priv + s->stat_offset;
1217 		*(u32 *)p = val;
1218 	}
1219 }
1220 
bcmgenet_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)1221 static void bcmgenet_get_ethtool_stats(struct net_device *dev,
1222 				       struct ethtool_stats *stats,
1223 				       u64 *data)
1224 {
1225 	struct bcmgenet_priv *priv = netdev_priv(dev);
1226 	int i;
1227 
1228 	if (netif_running(dev))
1229 		bcmgenet_update_mib_counters(priv);
1230 
1231 	dev->netdev_ops->ndo_get_stats(dev);
1232 
1233 	for (i = 0; i < BCMGENET_STATS_LEN; i++) {
1234 		const struct bcmgenet_stats *s;
1235 		char *p;
1236 
1237 		s = &bcmgenet_gstrings_stats[i];
1238 		if (s->type == BCMGENET_STAT_NETDEV)
1239 			p = (char *)&dev->stats;
1240 		else
1241 			p = (char *)priv;
1242 		p += s->stat_offset;
1243 		if (sizeof(unsigned long) != sizeof(u32) &&
1244 		    s->stat_sizeof == sizeof(unsigned long))
1245 			data[i] = *(unsigned long *)p;
1246 		else
1247 			data[i] = *(u32 *)p;
1248 	}
1249 }
1250 
bcmgenet_eee_enable_set(struct net_device * dev,bool enable,bool tx_lpi_enabled)1251 void bcmgenet_eee_enable_set(struct net_device *dev, bool enable,
1252 			     bool tx_lpi_enabled)
1253 {
1254 	struct bcmgenet_priv *priv = netdev_priv(dev);
1255 	u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
1256 	u32 reg;
1257 
1258 	if (enable && !priv->clk_eee_enabled) {
1259 		clk_prepare_enable(priv->clk_eee);
1260 		priv->clk_eee_enabled = true;
1261 	}
1262 
1263 	reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
1264 	if (enable)
1265 		reg |= EEE_EN;
1266 	else
1267 		reg &= ~EEE_EN;
1268 	bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
1269 
1270 	/* Enable EEE and switch to a 27Mhz clock automatically */
1271 	reg = bcmgenet_readl(priv->base + off);
1272 	if (tx_lpi_enabled)
1273 		reg |= TBUF_EEE_EN | TBUF_PM_EN;
1274 	else
1275 		reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
1276 	bcmgenet_writel(reg, priv->base + off);
1277 
1278 	/* Do the same for thing for RBUF */
1279 	reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
1280 	if (enable)
1281 		reg |= RBUF_EEE_EN | RBUF_PM_EN;
1282 	else
1283 		reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
1284 	bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
1285 
1286 	if (!enable && priv->clk_eee_enabled) {
1287 		clk_disable_unprepare(priv->clk_eee);
1288 		priv->clk_eee_enabled = false;
1289 	}
1290 
1291 	priv->eee.eee_enabled = enable;
1292 	priv->eee.eee_active = enable;
1293 	priv->eee.tx_lpi_enabled = tx_lpi_enabled;
1294 }
1295 
bcmgenet_get_eee(struct net_device * dev,struct ethtool_eee * e)1296 static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
1297 {
1298 	struct bcmgenet_priv *priv = netdev_priv(dev);
1299 	struct ethtool_eee *p = &priv->eee;
1300 
1301 	if (GENET_IS_V1(priv))
1302 		return -EOPNOTSUPP;
1303 
1304 	if (!dev->phydev)
1305 		return -ENODEV;
1306 
1307 	e->eee_enabled = p->eee_enabled;
1308 	e->eee_active = p->eee_active;
1309 	e->tx_lpi_enabled = p->tx_lpi_enabled;
1310 	e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
1311 
1312 	return phy_ethtool_get_eee(dev->phydev, e);
1313 }
1314 
bcmgenet_set_eee(struct net_device * dev,struct ethtool_eee * e)1315 static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
1316 {
1317 	struct bcmgenet_priv *priv = netdev_priv(dev);
1318 	struct ethtool_eee *p = &priv->eee;
1319 
1320 	if (GENET_IS_V1(priv))
1321 		return -EOPNOTSUPP;
1322 
1323 	if (!dev->phydev)
1324 		return -ENODEV;
1325 
1326 	p->eee_enabled = e->eee_enabled;
1327 
1328 	if (!p->eee_enabled) {
1329 		bcmgenet_eee_enable_set(dev, false, false);
1330 	} else {
1331 		p->eee_active = phy_init_eee(dev->phydev, false) >= 0;
1332 		bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
1333 		bcmgenet_eee_enable_set(dev, p->eee_active, e->tx_lpi_enabled);
1334 	}
1335 
1336 	return phy_ethtool_set_eee(dev->phydev, e);
1337 }
1338 
bcmgenet_validate_flow(struct net_device * dev,struct ethtool_rxnfc * cmd)1339 static int bcmgenet_validate_flow(struct net_device *dev,
1340 				  struct ethtool_rxnfc *cmd)
1341 {
1342 	struct ethtool_usrip4_spec *l4_mask;
1343 	struct ethhdr *eth_mask;
1344 
1345 	if (cmd->fs.location >= MAX_NUM_OF_FS_RULES) {
1346 		netdev_err(dev, "rxnfc: Invalid location (%d)\n",
1347 			   cmd->fs.location);
1348 		return -EINVAL;
1349 	}
1350 
1351 	switch (cmd->fs.flow_type & ~(FLOW_EXT | FLOW_MAC_EXT)) {
1352 	case IP_USER_FLOW:
1353 		l4_mask = &cmd->fs.m_u.usr_ip4_spec;
1354 		/* don't allow mask which isn't valid */
1355 		if (VALIDATE_MASK(l4_mask->ip4src) ||
1356 		    VALIDATE_MASK(l4_mask->ip4dst) ||
1357 		    VALIDATE_MASK(l4_mask->l4_4_bytes) ||
1358 		    VALIDATE_MASK(l4_mask->proto) ||
1359 		    VALIDATE_MASK(l4_mask->ip_ver) ||
1360 		    VALIDATE_MASK(l4_mask->tos)) {
1361 			netdev_err(dev, "rxnfc: Unsupported mask\n");
1362 			return -EINVAL;
1363 		}
1364 		break;
1365 	case ETHER_FLOW:
1366 		eth_mask = &cmd->fs.m_u.ether_spec;
1367 		/* don't allow mask which isn't valid */
1368 		if (VALIDATE_MASK(eth_mask->h_dest) ||
1369 		    VALIDATE_MASK(eth_mask->h_source) ||
1370 		    VALIDATE_MASK(eth_mask->h_proto)) {
1371 			netdev_err(dev, "rxnfc: Unsupported mask\n");
1372 			return -EINVAL;
1373 		}
1374 		break;
1375 	default:
1376 		netdev_err(dev, "rxnfc: Unsupported flow type (0x%x)\n",
1377 			   cmd->fs.flow_type);
1378 		return -EINVAL;
1379 	}
1380 
1381 	if ((cmd->fs.flow_type & FLOW_EXT)) {
1382 		/* don't allow mask which isn't valid */
1383 		if (VALIDATE_MASK(cmd->fs.m_ext.vlan_etype) ||
1384 		    VALIDATE_MASK(cmd->fs.m_ext.vlan_tci)) {
1385 			netdev_err(dev, "rxnfc: Unsupported mask\n");
1386 			return -EINVAL;
1387 		}
1388 		if (cmd->fs.m_ext.data[0] || cmd->fs.m_ext.data[1]) {
1389 			netdev_err(dev, "rxnfc: user-def not supported\n");
1390 			return -EINVAL;
1391 		}
1392 	}
1393 
1394 	if ((cmd->fs.flow_type & FLOW_MAC_EXT)) {
1395 		/* don't allow mask which isn't valid */
1396 		if (VALIDATE_MASK(cmd->fs.m_ext.h_dest)) {
1397 			netdev_err(dev, "rxnfc: Unsupported mask\n");
1398 			return -EINVAL;
1399 		}
1400 	}
1401 
1402 	return 0;
1403 }
1404 
bcmgenet_insert_flow(struct net_device * dev,struct ethtool_rxnfc * cmd)1405 static int bcmgenet_insert_flow(struct net_device *dev,
1406 				struct ethtool_rxnfc *cmd)
1407 {
1408 	struct bcmgenet_priv *priv = netdev_priv(dev);
1409 	struct bcmgenet_rxnfc_rule *loc_rule;
1410 	int err;
1411 
1412 	if (priv->hw_params->hfb_filter_size < 128) {
1413 		netdev_err(dev, "rxnfc: Not supported by this device\n");
1414 		return -EINVAL;
1415 	}
1416 
1417 	if (cmd->fs.ring_cookie > priv->hw_params->rx_queues &&
1418 	    cmd->fs.ring_cookie != RX_CLS_FLOW_WAKE) {
1419 		netdev_err(dev, "rxnfc: Unsupported action (%llu)\n",
1420 			   cmd->fs.ring_cookie);
1421 		return -EINVAL;
1422 	}
1423 
1424 	err = bcmgenet_validate_flow(dev, cmd);
1425 	if (err)
1426 		return err;
1427 
1428 	loc_rule = &priv->rxnfc_rules[cmd->fs.location];
1429 	if (loc_rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1430 		bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
1431 	if (loc_rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
1432 		list_del(&loc_rule->list);
1433 		bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1434 	}
1435 	loc_rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1436 	memcpy(&loc_rule->fs, &cmd->fs,
1437 	       sizeof(struct ethtool_rx_flow_spec));
1438 
1439 	bcmgenet_hfb_create_rxnfc_filter(priv, loc_rule);
1440 
1441 	list_add_tail(&loc_rule->list, &priv->rxnfc_list);
1442 
1443 	return 0;
1444 }
1445 
bcmgenet_delete_flow(struct net_device * dev,struct ethtool_rxnfc * cmd)1446 static int bcmgenet_delete_flow(struct net_device *dev,
1447 				struct ethtool_rxnfc *cmd)
1448 {
1449 	struct bcmgenet_priv *priv = netdev_priv(dev);
1450 	struct bcmgenet_rxnfc_rule *rule;
1451 	int err = 0;
1452 
1453 	if (cmd->fs.location >= MAX_NUM_OF_FS_RULES)
1454 		return -EINVAL;
1455 
1456 	rule = &priv->rxnfc_rules[cmd->fs.location];
1457 	if (rule->state == BCMGENET_RXNFC_STATE_UNUSED) {
1458 		err =  -ENOENT;
1459 		goto out;
1460 	}
1461 
1462 	if (rule->state == BCMGENET_RXNFC_STATE_ENABLED)
1463 		bcmgenet_hfb_disable_filter(priv, cmd->fs.location);
1464 	if (rule->state != BCMGENET_RXNFC_STATE_UNUSED) {
1465 		list_del(&rule->list);
1466 		bcmgenet_hfb_clear_filter(priv, cmd->fs.location);
1467 	}
1468 	rule->state = BCMGENET_RXNFC_STATE_UNUSED;
1469 	memset(&rule->fs, 0, sizeof(struct ethtool_rx_flow_spec));
1470 
1471 out:
1472 	return err;
1473 }
1474 
bcmgenet_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd)1475 static int bcmgenet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
1476 {
1477 	struct bcmgenet_priv *priv = netdev_priv(dev);
1478 	int err = 0;
1479 
1480 	switch (cmd->cmd) {
1481 	case ETHTOOL_SRXCLSRLINS:
1482 		err = bcmgenet_insert_flow(dev, cmd);
1483 		break;
1484 	case ETHTOOL_SRXCLSRLDEL:
1485 		err = bcmgenet_delete_flow(dev, cmd);
1486 		break;
1487 	default:
1488 		netdev_warn(priv->dev, "Unsupported ethtool command. (%d)\n",
1489 			    cmd->cmd);
1490 		return -EINVAL;
1491 	}
1492 
1493 	return err;
1494 }
1495 
bcmgenet_get_flow(struct net_device * dev,struct ethtool_rxnfc * cmd,int loc)1496 static int bcmgenet_get_flow(struct net_device *dev, struct ethtool_rxnfc *cmd,
1497 			     int loc)
1498 {
1499 	struct bcmgenet_priv *priv = netdev_priv(dev);
1500 	struct bcmgenet_rxnfc_rule *rule;
1501 	int err = 0;
1502 
1503 	if (loc < 0 || loc >= MAX_NUM_OF_FS_RULES)
1504 		return -EINVAL;
1505 
1506 	rule = &priv->rxnfc_rules[loc];
1507 	if (rule->state == BCMGENET_RXNFC_STATE_UNUSED)
1508 		err = -ENOENT;
1509 	else
1510 		memcpy(&cmd->fs, &rule->fs,
1511 		       sizeof(struct ethtool_rx_flow_spec));
1512 
1513 	return err;
1514 }
1515 
bcmgenet_get_num_flows(struct bcmgenet_priv * priv)1516 static int bcmgenet_get_num_flows(struct bcmgenet_priv *priv)
1517 {
1518 	struct list_head *pos;
1519 	int res = 0;
1520 
1521 	list_for_each(pos, &priv->rxnfc_list)
1522 		res++;
1523 
1524 	return res;
1525 }
1526 
bcmgenet_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * cmd,u32 * rule_locs)1527 static int bcmgenet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
1528 			      u32 *rule_locs)
1529 {
1530 	struct bcmgenet_priv *priv = netdev_priv(dev);
1531 	struct bcmgenet_rxnfc_rule *rule;
1532 	int err = 0;
1533 	int i = 0;
1534 
1535 	switch (cmd->cmd) {
1536 	case ETHTOOL_GRXRINGS:
1537 		cmd->data = priv->hw_params->rx_queues ?: 1;
1538 		break;
1539 	case ETHTOOL_GRXCLSRLCNT:
1540 		cmd->rule_cnt = bcmgenet_get_num_flows(priv);
1541 		cmd->data = MAX_NUM_OF_FS_RULES;
1542 		break;
1543 	case ETHTOOL_GRXCLSRULE:
1544 		err = bcmgenet_get_flow(dev, cmd, cmd->fs.location);
1545 		break;
1546 	case ETHTOOL_GRXCLSRLALL:
1547 		list_for_each_entry(rule, &priv->rxnfc_list, list)
1548 			if (i < cmd->rule_cnt)
1549 				rule_locs[i++] = rule->fs.location;
1550 		cmd->rule_cnt = i;
1551 		cmd->data = MAX_NUM_OF_FS_RULES;
1552 		break;
1553 	default:
1554 		err = -EOPNOTSUPP;
1555 		break;
1556 	}
1557 
1558 	return err;
1559 }
1560 
1561 /* standard ethtool support functions. */
1562 static const struct ethtool_ops bcmgenet_ethtool_ops = {
1563 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
1564 				     ETHTOOL_COALESCE_MAX_FRAMES |
1565 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
1566 	.begin			= bcmgenet_begin,
1567 	.complete		= bcmgenet_complete,
1568 	.get_strings		= bcmgenet_get_strings,
1569 	.get_sset_count		= bcmgenet_get_sset_count,
1570 	.get_ethtool_stats	= bcmgenet_get_ethtool_stats,
1571 	.get_drvinfo		= bcmgenet_get_drvinfo,
1572 	.get_link		= ethtool_op_get_link,
1573 	.get_msglevel		= bcmgenet_get_msglevel,
1574 	.set_msglevel		= bcmgenet_set_msglevel,
1575 	.get_wol		= bcmgenet_get_wol,
1576 	.set_wol		= bcmgenet_set_wol,
1577 	.get_eee		= bcmgenet_get_eee,
1578 	.set_eee		= bcmgenet_set_eee,
1579 	.nway_reset		= phy_ethtool_nway_reset,
1580 	.get_coalesce		= bcmgenet_get_coalesce,
1581 	.set_coalesce		= bcmgenet_set_coalesce,
1582 	.get_link_ksettings	= bcmgenet_get_link_ksettings,
1583 	.set_link_ksettings	= bcmgenet_set_link_ksettings,
1584 	.get_ts_info		= ethtool_op_get_ts_info,
1585 	.get_rxnfc		= bcmgenet_get_rxnfc,
1586 	.set_rxnfc		= bcmgenet_set_rxnfc,
1587 };
1588 
1589 /* Power down the unimac, based on mode. */
bcmgenet_power_down(struct bcmgenet_priv * priv,enum bcmgenet_power_mode mode)1590 static int bcmgenet_power_down(struct bcmgenet_priv *priv,
1591 				enum bcmgenet_power_mode mode)
1592 {
1593 	int ret = 0;
1594 	u32 reg;
1595 
1596 	switch (mode) {
1597 	case GENET_POWER_CABLE_SENSE:
1598 		phy_detach(priv->dev->phydev);
1599 		break;
1600 
1601 	case GENET_POWER_WOL_MAGIC:
1602 		ret = bcmgenet_wol_power_down_cfg(priv, mode);
1603 		break;
1604 
1605 	case GENET_POWER_PASSIVE:
1606 		/* Power down LED */
1607 		if (priv->hw_params->flags & GENET_HAS_EXT) {
1608 			reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1609 			if (GENET_IS_V5(priv))
1610 				reg |= EXT_PWR_DOWN_PHY_EN |
1611 				       EXT_PWR_DOWN_PHY_RD |
1612 				       EXT_PWR_DOWN_PHY_SD |
1613 				       EXT_PWR_DOWN_PHY_RX |
1614 				       EXT_PWR_DOWN_PHY_TX |
1615 				       EXT_IDDQ_GLBL_PWR;
1616 			else
1617 				reg |= EXT_PWR_DOWN_PHY;
1618 
1619 			reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
1620 			bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1621 
1622 			bcmgenet_phy_power_set(priv->dev, false);
1623 		}
1624 		break;
1625 	default:
1626 		break;
1627 	}
1628 
1629 	return ret;
1630 }
1631 
bcmgenet_power_up(struct bcmgenet_priv * priv,enum bcmgenet_power_mode mode)1632 static void bcmgenet_power_up(struct bcmgenet_priv *priv,
1633 			      enum bcmgenet_power_mode mode)
1634 {
1635 	u32 reg;
1636 
1637 	if (!(priv->hw_params->flags & GENET_HAS_EXT))
1638 		return;
1639 
1640 	reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
1641 
1642 	switch (mode) {
1643 	case GENET_POWER_PASSIVE:
1644 		reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS |
1645 			 EXT_ENERGY_DET_MASK);
1646 		if (GENET_IS_V5(priv)) {
1647 			reg &= ~(EXT_PWR_DOWN_PHY_EN |
1648 				 EXT_PWR_DOWN_PHY_RD |
1649 				 EXT_PWR_DOWN_PHY_SD |
1650 				 EXT_PWR_DOWN_PHY_RX |
1651 				 EXT_PWR_DOWN_PHY_TX |
1652 				 EXT_IDDQ_GLBL_PWR);
1653 			reg |=   EXT_PHY_RESET;
1654 			bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1655 			mdelay(1);
1656 
1657 			reg &=  ~EXT_PHY_RESET;
1658 		} else {
1659 			reg &= ~EXT_PWR_DOWN_PHY;
1660 			reg |= EXT_PWR_DN_EN_LD;
1661 		}
1662 		bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1663 		bcmgenet_phy_power_set(priv->dev, true);
1664 		break;
1665 
1666 	case GENET_POWER_CABLE_SENSE:
1667 		/* enable APD */
1668 		if (!GENET_IS_V5(priv)) {
1669 			reg |= EXT_PWR_DN_EN_LD;
1670 			bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
1671 		}
1672 		break;
1673 	case GENET_POWER_WOL_MAGIC:
1674 		bcmgenet_wol_power_up_cfg(priv, mode);
1675 		return;
1676 	default:
1677 		break;
1678 	}
1679 }
1680 
bcmgenet_get_txcb(struct bcmgenet_priv * priv,struct bcmgenet_tx_ring * ring)1681 static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
1682 					 struct bcmgenet_tx_ring *ring)
1683 {
1684 	struct enet_cb *tx_cb_ptr;
1685 
1686 	tx_cb_ptr = ring->cbs;
1687 	tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1688 
1689 	/* Advancing local write pointer */
1690 	if (ring->write_ptr == ring->end_ptr)
1691 		ring->write_ptr = ring->cb_ptr;
1692 	else
1693 		ring->write_ptr++;
1694 
1695 	return tx_cb_ptr;
1696 }
1697 
bcmgenet_put_txcb(struct bcmgenet_priv * priv,struct bcmgenet_tx_ring * ring)1698 static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
1699 					 struct bcmgenet_tx_ring *ring)
1700 {
1701 	struct enet_cb *tx_cb_ptr;
1702 
1703 	tx_cb_ptr = ring->cbs;
1704 	tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
1705 
1706 	/* Rewinding local write pointer */
1707 	if (ring->write_ptr == ring->cb_ptr)
1708 		ring->write_ptr = ring->end_ptr;
1709 	else
1710 		ring->write_ptr--;
1711 
1712 	return tx_cb_ptr;
1713 }
1714 
bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring * ring)1715 static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
1716 {
1717 	bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
1718 				 INTRL2_CPU_MASK_SET);
1719 }
1720 
bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring * ring)1721 static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
1722 {
1723 	bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
1724 				 INTRL2_CPU_MASK_CLEAR);
1725 }
1726 
bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring * ring)1727 static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
1728 {
1729 	bcmgenet_intrl2_1_writel(ring->priv,
1730 				 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1731 				 INTRL2_CPU_MASK_SET);
1732 }
1733 
bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring * ring)1734 static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
1735 {
1736 	bcmgenet_intrl2_1_writel(ring->priv,
1737 				 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
1738 				 INTRL2_CPU_MASK_CLEAR);
1739 }
1740 
bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring * ring)1741 static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
1742 {
1743 	bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
1744 				 INTRL2_CPU_MASK_SET);
1745 }
1746 
bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring * ring)1747 static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
1748 {
1749 	bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
1750 				 INTRL2_CPU_MASK_CLEAR);
1751 }
1752 
bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring * ring)1753 static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
1754 {
1755 	bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
1756 				 INTRL2_CPU_MASK_CLEAR);
1757 }
1758 
bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring * ring)1759 static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
1760 {
1761 	bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
1762 				 INTRL2_CPU_MASK_SET);
1763 }
1764 
1765 /* Simple helper to free a transmit control block's resources
1766  * Returns an skb when the last transmit control block associated with the
1767  * skb is freed.  The skb should be freed by the caller if necessary.
1768  */
bcmgenet_free_tx_cb(struct device * dev,struct enet_cb * cb)1769 static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
1770 					   struct enet_cb *cb)
1771 {
1772 	struct sk_buff *skb;
1773 
1774 	skb = cb->skb;
1775 
1776 	if (skb) {
1777 		cb->skb = NULL;
1778 		if (cb == GENET_CB(skb)->first_cb)
1779 			dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1780 					 dma_unmap_len(cb, dma_len),
1781 					 DMA_TO_DEVICE);
1782 		else
1783 			dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
1784 				       dma_unmap_len(cb, dma_len),
1785 				       DMA_TO_DEVICE);
1786 		dma_unmap_addr_set(cb, dma_addr, 0);
1787 
1788 		if (cb == GENET_CB(skb)->last_cb)
1789 			return skb;
1790 
1791 	} else if (dma_unmap_addr(cb, dma_addr)) {
1792 		dma_unmap_page(dev,
1793 			       dma_unmap_addr(cb, dma_addr),
1794 			       dma_unmap_len(cb, dma_len),
1795 			       DMA_TO_DEVICE);
1796 		dma_unmap_addr_set(cb, dma_addr, 0);
1797 	}
1798 
1799 	return NULL;
1800 }
1801 
1802 /* Simple helper to free a receive control block's resources */
bcmgenet_free_rx_cb(struct device * dev,struct enet_cb * cb)1803 static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
1804 					   struct enet_cb *cb)
1805 {
1806 	struct sk_buff *skb;
1807 
1808 	skb = cb->skb;
1809 	cb->skb = NULL;
1810 
1811 	if (dma_unmap_addr(cb, dma_addr)) {
1812 		dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
1813 				 dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
1814 		dma_unmap_addr_set(cb, dma_addr, 0);
1815 	}
1816 
1817 	return skb;
1818 }
1819 
1820 /* Unlocked version of the reclaim routine */
__bcmgenet_tx_reclaim(struct net_device * dev,struct bcmgenet_tx_ring * ring)1821 static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
1822 					  struct bcmgenet_tx_ring *ring)
1823 {
1824 	struct bcmgenet_priv *priv = netdev_priv(dev);
1825 	unsigned int txbds_processed = 0;
1826 	unsigned int bytes_compl = 0;
1827 	unsigned int pkts_compl = 0;
1828 	unsigned int txbds_ready;
1829 	unsigned int c_index;
1830 	struct sk_buff *skb;
1831 
1832 	/* Clear status before servicing to reduce spurious interrupts */
1833 	if (ring->index == DESC_INDEX)
1834 		bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
1835 					 INTRL2_CPU_CLEAR);
1836 	else
1837 		bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
1838 					 INTRL2_CPU_CLEAR);
1839 
1840 	/* Compute how many buffers are transmitted since last xmit call */
1841 	c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
1842 		& DMA_C_INDEX_MASK;
1843 	txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
1844 
1845 	netif_dbg(priv, tx_done, dev,
1846 		  "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
1847 		  __func__, ring->index, ring->c_index, c_index, txbds_ready);
1848 
1849 	/* Reclaim transmitted buffers */
1850 	while (txbds_processed < txbds_ready) {
1851 		skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
1852 					  &priv->tx_cbs[ring->clean_ptr]);
1853 		if (skb) {
1854 			pkts_compl++;
1855 			bytes_compl += GENET_CB(skb)->bytes_sent;
1856 			dev_consume_skb_any(skb);
1857 		}
1858 
1859 		txbds_processed++;
1860 		if (likely(ring->clean_ptr < ring->end_ptr))
1861 			ring->clean_ptr++;
1862 		else
1863 			ring->clean_ptr = ring->cb_ptr;
1864 	}
1865 
1866 	ring->free_bds += txbds_processed;
1867 	ring->c_index = c_index;
1868 
1869 	ring->packets += pkts_compl;
1870 	ring->bytes += bytes_compl;
1871 
1872 	netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
1873 				  pkts_compl, bytes_compl);
1874 
1875 	return txbds_processed;
1876 }
1877 
bcmgenet_tx_reclaim(struct net_device * dev,struct bcmgenet_tx_ring * ring)1878 static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
1879 				struct bcmgenet_tx_ring *ring)
1880 {
1881 	unsigned int released;
1882 
1883 	spin_lock_bh(&ring->lock);
1884 	released = __bcmgenet_tx_reclaim(dev, ring);
1885 	spin_unlock_bh(&ring->lock);
1886 
1887 	return released;
1888 }
1889 
bcmgenet_tx_poll(struct napi_struct * napi,int budget)1890 static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
1891 {
1892 	struct bcmgenet_tx_ring *ring =
1893 		container_of(napi, struct bcmgenet_tx_ring, napi);
1894 	unsigned int work_done = 0;
1895 	struct netdev_queue *txq;
1896 
1897 	spin_lock(&ring->lock);
1898 	work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
1899 	if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
1900 		txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
1901 		netif_tx_wake_queue(txq);
1902 	}
1903 	spin_unlock(&ring->lock);
1904 
1905 	if (work_done == 0) {
1906 		napi_complete(napi);
1907 		ring->int_enable(ring);
1908 
1909 		return 0;
1910 	}
1911 
1912 	return budget;
1913 }
1914 
bcmgenet_tx_reclaim_all(struct net_device * dev)1915 static void bcmgenet_tx_reclaim_all(struct net_device *dev)
1916 {
1917 	struct bcmgenet_priv *priv = netdev_priv(dev);
1918 	int i;
1919 
1920 	if (netif_is_multiqueue(dev)) {
1921 		for (i = 0; i < priv->hw_params->tx_queues; i++)
1922 			bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
1923 	}
1924 
1925 	bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
1926 }
1927 
1928 /* Reallocate the SKB to put enough headroom in front of it and insert
1929  * the transmit checksum offsets in the descriptors
1930  */
bcmgenet_add_tsb(struct net_device * dev,struct sk_buff * skb)1931 static struct sk_buff *bcmgenet_add_tsb(struct net_device *dev,
1932 					struct sk_buff *skb)
1933 {
1934 	struct bcmgenet_priv *priv = netdev_priv(dev);
1935 	struct status_64 *status = NULL;
1936 	struct sk_buff *new_skb;
1937 	u16 offset;
1938 	u8 ip_proto;
1939 	__be16 ip_ver;
1940 	u32 tx_csum_info;
1941 
1942 	if (unlikely(skb_headroom(skb) < sizeof(*status))) {
1943 		/* If 64 byte status block enabled, must make sure skb has
1944 		 * enough headroom for us to insert 64B status block.
1945 		 */
1946 		new_skb = skb_realloc_headroom(skb, sizeof(*status));
1947 		if (!new_skb) {
1948 			dev_kfree_skb_any(skb);
1949 			priv->mib.tx_realloc_tsb_failed++;
1950 			dev->stats.tx_dropped++;
1951 			return NULL;
1952 		}
1953 		dev_consume_skb_any(skb);
1954 		skb = new_skb;
1955 		priv->mib.tx_realloc_tsb++;
1956 	}
1957 
1958 	skb_push(skb, sizeof(*status));
1959 	status = (struct status_64 *)skb->data;
1960 
1961 	if (skb->ip_summed  == CHECKSUM_PARTIAL) {
1962 		ip_ver = skb->protocol;
1963 		switch (ip_ver) {
1964 		case htons(ETH_P_IP):
1965 			ip_proto = ip_hdr(skb)->protocol;
1966 			break;
1967 		case htons(ETH_P_IPV6):
1968 			ip_proto = ipv6_hdr(skb)->nexthdr;
1969 			break;
1970 		default:
1971 			/* don't use UDP flag */
1972 			ip_proto = 0;
1973 			break;
1974 		}
1975 
1976 		offset = skb_checksum_start_offset(skb) - sizeof(*status);
1977 		tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
1978 				(offset + skb->csum_offset) |
1979 				STATUS_TX_CSUM_LV;
1980 
1981 		/* Set the special UDP flag for UDP */
1982 		if (ip_proto == IPPROTO_UDP)
1983 			tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
1984 
1985 		status->tx_csum_info = tx_csum_info;
1986 	}
1987 
1988 	return skb;
1989 }
1990 
bcmgenet_hide_tsb(struct sk_buff * skb)1991 static void bcmgenet_hide_tsb(struct sk_buff *skb)
1992 {
1993 	__skb_pull(skb, sizeof(struct status_64));
1994 }
1995 
bcmgenet_xmit(struct sk_buff * skb,struct net_device * dev)1996 static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
1997 {
1998 	struct bcmgenet_priv *priv = netdev_priv(dev);
1999 	struct device *kdev = &priv->pdev->dev;
2000 	struct bcmgenet_tx_ring *ring = NULL;
2001 	struct enet_cb *tx_cb_ptr;
2002 	struct netdev_queue *txq;
2003 	int nr_frags, index;
2004 	dma_addr_t mapping;
2005 	unsigned int size;
2006 	skb_frag_t *frag;
2007 	u32 len_stat;
2008 	int ret;
2009 	int i;
2010 
2011 	index = skb_get_queue_mapping(skb);
2012 	/* Mapping strategy:
2013 	 * queue_mapping = 0, unclassified, packet xmited through ring16
2014 	 * queue_mapping = 1, goes to ring 0. (highest priority queue
2015 	 * queue_mapping = 2, goes to ring 1.
2016 	 * queue_mapping = 3, goes to ring 2.
2017 	 * queue_mapping = 4, goes to ring 3.
2018 	 */
2019 	if (index == 0)
2020 		index = DESC_INDEX;
2021 	else
2022 		index -= 1;
2023 
2024 	ring = &priv->tx_rings[index];
2025 	txq = netdev_get_tx_queue(dev, ring->queue);
2026 
2027 	nr_frags = skb_shinfo(skb)->nr_frags;
2028 
2029 	spin_lock(&ring->lock);
2030 	if (ring->free_bds <= (nr_frags + 1)) {
2031 		if (!netif_tx_queue_stopped(txq)) {
2032 			netif_tx_stop_queue(txq);
2033 			netdev_err(dev,
2034 				   "%s: tx ring %d full when queue %d awake\n",
2035 				   __func__, index, ring->queue);
2036 		}
2037 		ret = NETDEV_TX_BUSY;
2038 		goto out;
2039 	}
2040 
2041 	/* Retain how many bytes will be sent on the wire, without TSB inserted
2042 	 * by transmit checksum offload
2043 	 */
2044 	GENET_CB(skb)->bytes_sent = skb->len;
2045 
2046 	/* add the Transmit Status Block */
2047 	skb = bcmgenet_add_tsb(dev, skb);
2048 	if (!skb) {
2049 		ret = NETDEV_TX_OK;
2050 		goto out;
2051 	}
2052 
2053 	for (i = 0; i <= nr_frags; i++) {
2054 		tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
2055 
2056 		BUG_ON(!tx_cb_ptr);
2057 
2058 		if (!i) {
2059 			/* Transmit single SKB or head of fragment list */
2060 			GENET_CB(skb)->first_cb = tx_cb_ptr;
2061 			size = skb_headlen(skb);
2062 			mapping = dma_map_single(kdev, skb->data, size,
2063 						 DMA_TO_DEVICE);
2064 		} else {
2065 			/* xmit fragment */
2066 			frag = &skb_shinfo(skb)->frags[i - 1];
2067 			size = skb_frag_size(frag);
2068 			mapping = skb_frag_dma_map(kdev, frag, 0, size,
2069 						   DMA_TO_DEVICE);
2070 		}
2071 
2072 		ret = dma_mapping_error(kdev, mapping);
2073 		if (ret) {
2074 			priv->mib.tx_dma_failed++;
2075 			netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
2076 			ret = NETDEV_TX_OK;
2077 			goto out_unmap_frags;
2078 		}
2079 		dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
2080 		dma_unmap_len_set(tx_cb_ptr, dma_len, size);
2081 
2082 		tx_cb_ptr->skb = skb;
2083 
2084 		len_stat = (size << DMA_BUFLENGTH_SHIFT) |
2085 			   (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
2086 
2087 		/* Note: if we ever change from DMA_TX_APPEND_CRC below we
2088 		 * will need to restore software padding of "runt" packets
2089 		 */
2090 		len_stat |= DMA_TX_APPEND_CRC;
2091 
2092 		if (!i) {
2093 			len_stat |= DMA_SOP;
2094 			if (skb->ip_summed == CHECKSUM_PARTIAL)
2095 				len_stat |= DMA_TX_DO_CSUM;
2096 		}
2097 		if (i == nr_frags)
2098 			len_stat |= DMA_EOP;
2099 
2100 		dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
2101 	}
2102 
2103 	GENET_CB(skb)->last_cb = tx_cb_ptr;
2104 
2105 	bcmgenet_hide_tsb(skb);
2106 	skb_tx_timestamp(skb);
2107 
2108 	/* Decrement total BD count and advance our write pointer */
2109 	ring->free_bds -= nr_frags + 1;
2110 	ring->prod_index += nr_frags + 1;
2111 	ring->prod_index &= DMA_P_INDEX_MASK;
2112 
2113 	netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
2114 
2115 	if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
2116 		netif_tx_stop_queue(txq);
2117 
2118 	if (!netdev_xmit_more() || netif_xmit_stopped(txq))
2119 		/* Packets are ready, update producer index */
2120 		bcmgenet_tdma_ring_writel(priv, ring->index,
2121 					  ring->prod_index, TDMA_PROD_INDEX);
2122 out:
2123 	spin_unlock(&ring->lock);
2124 
2125 	return ret;
2126 
2127 out_unmap_frags:
2128 	/* Back up for failed control block mapping */
2129 	bcmgenet_put_txcb(priv, ring);
2130 
2131 	/* Unmap successfully mapped control blocks */
2132 	while (i-- > 0) {
2133 		tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
2134 		bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
2135 	}
2136 
2137 	dev_kfree_skb(skb);
2138 	goto out;
2139 }
2140 
bcmgenet_rx_refill(struct bcmgenet_priv * priv,struct enet_cb * cb)2141 static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
2142 					  struct enet_cb *cb)
2143 {
2144 	struct device *kdev = &priv->pdev->dev;
2145 	struct sk_buff *skb;
2146 	struct sk_buff *rx_skb;
2147 	dma_addr_t mapping;
2148 
2149 	/* Allocate a new Rx skb */
2150 	skb = __netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT,
2151 				 GFP_ATOMIC | __GFP_NOWARN);
2152 	if (!skb) {
2153 		priv->mib.alloc_rx_buff_failed++;
2154 		netif_err(priv, rx_err, priv->dev,
2155 			  "%s: Rx skb allocation failed\n", __func__);
2156 		return NULL;
2157 	}
2158 
2159 	/* DMA-map the new Rx skb */
2160 	mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
2161 				 DMA_FROM_DEVICE);
2162 	if (dma_mapping_error(kdev, mapping)) {
2163 		priv->mib.rx_dma_failed++;
2164 		dev_kfree_skb_any(skb);
2165 		netif_err(priv, rx_err, priv->dev,
2166 			  "%s: Rx skb DMA mapping failed\n", __func__);
2167 		return NULL;
2168 	}
2169 
2170 	/* Grab the current Rx skb from the ring and DMA-unmap it */
2171 	rx_skb = bcmgenet_free_rx_cb(kdev, cb);
2172 
2173 	/* Put the new Rx skb on the ring */
2174 	cb->skb = skb;
2175 	dma_unmap_addr_set(cb, dma_addr, mapping);
2176 	dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
2177 	dmadesc_set_addr(priv, cb->bd_addr, mapping);
2178 
2179 	/* Return the current Rx skb to caller */
2180 	return rx_skb;
2181 }
2182 
2183 /* bcmgenet_desc_rx - descriptor based rx process.
2184  * this could be called from bottom half, or from NAPI polling method.
2185  */
bcmgenet_desc_rx(struct bcmgenet_rx_ring * ring,unsigned int budget)2186 static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
2187 				     unsigned int budget)
2188 {
2189 	struct bcmgenet_priv *priv = ring->priv;
2190 	struct net_device *dev = priv->dev;
2191 	struct enet_cb *cb;
2192 	struct sk_buff *skb;
2193 	u32 dma_length_status;
2194 	unsigned long dma_flag;
2195 	int len;
2196 	unsigned int rxpktprocessed = 0, rxpkttoprocess;
2197 	unsigned int bytes_processed = 0;
2198 	unsigned int p_index, mask;
2199 	unsigned int discards;
2200 
2201 	/* Clear status before servicing to reduce spurious interrupts */
2202 	if (ring->index == DESC_INDEX) {
2203 		bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
2204 					 INTRL2_CPU_CLEAR);
2205 	} else {
2206 		mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
2207 		bcmgenet_intrl2_1_writel(priv,
2208 					 mask,
2209 					 INTRL2_CPU_CLEAR);
2210 	}
2211 
2212 	p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
2213 
2214 	discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
2215 		   DMA_P_INDEX_DISCARD_CNT_MASK;
2216 	if (discards > ring->old_discards) {
2217 		discards = discards - ring->old_discards;
2218 		ring->errors += discards;
2219 		ring->old_discards += discards;
2220 
2221 		/* Clear HW register when we reach 75% of maximum 0xFFFF */
2222 		if (ring->old_discards >= 0xC000) {
2223 			ring->old_discards = 0;
2224 			bcmgenet_rdma_ring_writel(priv, ring->index, 0,
2225 						  RDMA_PROD_INDEX);
2226 		}
2227 	}
2228 
2229 	p_index &= DMA_P_INDEX_MASK;
2230 	rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
2231 
2232 	netif_dbg(priv, rx_status, dev,
2233 		  "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
2234 
2235 	while ((rxpktprocessed < rxpkttoprocess) &&
2236 	       (rxpktprocessed < budget)) {
2237 		struct status_64 *status;
2238 		__be16 rx_csum;
2239 
2240 		cb = &priv->rx_cbs[ring->read_ptr];
2241 		skb = bcmgenet_rx_refill(priv, cb);
2242 
2243 		if (unlikely(!skb)) {
2244 			ring->dropped++;
2245 			goto next;
2246 		}
2247 
2248 		status = (struct status_64 *)skb->data;
2249 		dma_length_status = status->length_status;
2250 		if (dev->features & NETIF_F_RXCSUM) {
2251 			rx_csum = (__force __be16)(status->rx_csum & 0xffff);
2252 			if (rx_csum) {
2253 				skb->csum = (__force __wsum)ntohs(rx_csum);
2254 				skb->ip_summed = CHECKSUM_COMPLETE;
2255 			}
2256 		}
2257 
2258 		/* DMA flags and length are still valid no matter how
2259 		 * we got the Receive Status Vector (64B RSB or register)
2260 		 */
2261 		dma_flag = dma_length_status & 0xffff;
2262 		len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
2263 
2264 		netif_dbg(priv, rx_status, dev,
2265 			  "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
2266 			  __func__, p_index, ring->c_index,
2267 			  ring->read_ptr, dma_length_status);
2268 
2269 		if (unlikely(len > RX_BUF_LENGTH)) {
2270 			netif_err(priv, rx_status, dev, "oversized packet\n");
2271 			dev->stats.rx_length_errors++;
2272 			dev->stats.rx_errors++;
2273 			dev_kfree_skb_any(skb);
2274 			goto next;
2275 		}
2276 
2277 		if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
2278 			netif_err(priv, rx_status, dev,
2279 				  "dropping fragmented packet!\n");
2280 			ring->errors++;
2281 			dev_kfree_skb_any(skb);
2282 			goto next;
2283 		}
2284 
2285 		/* report errors */
2286 		if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
2287 						DMA_RX_OV |
2288 						DMA_RX_NO |
2289 						DMA_RX_LG |
2290 						DMA_RX_RXER))) {
2291 			netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
2292 				  (unsigned int)dma_flag);
2293 			if (dma_flag & DMA_RX_CRC_ERROR)
2294 				dev->stats.rx_crc_errors++;
2295 			if (dma_flag & DMA_RX_OV)
2296 				dev->stats.rx_over_errors++;
2297 			if (dma_flag & DMA_RX_NO)
2298 				dev->stats.rx_frame_errors++;
2299 			if (dma_flag & DMA_RX_LG)
2300 				dev->stats.rx_length_errors++;
2301 			dev->stats.rx_errors++;
2302 			dev_kfree_skb_any(skb);
2303 			goto next;
2304 		} /* error packet */
2305 
2306 		skb_put(skb, len);
2307 
2308 		/* remove RSB and hardware 2bytes added for IP alignment */
2309 		skb_pull(skb, 66);
2310 		len -= 66;
2311 
2312 		if (priv->crc_fwd_en) {
2313 			skb_trim(skb, len - ETH_FCS_LEN);
2314 			len -= ETH_FCS_LEN;
2315 		}
2316 
2317 		bytes_processed += len;
2318 
2319 		/*Finish setting up the received SKB and send it to the kernel*/
2320 		skb->protocol = eth_type_trans(skb, priv->dev);
2321 		ring->packets++;
2322 		ring->bytes += len;
2323 		if (dma_flag & DMA_RX_MULT)
2324 			dev->stats.multicast++;
2325 
2326 		/* Notify kernel */
2327 		napi_gro_receive(&ring->napi, skb);
2328 		netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
2329 
2330 next:
2331 		rxpktprocessed++;
2332 		if (likely(ring->read_ptr < ring->end_ptr))
2333 			ring->read_ptr++;
2334 		else
2335 			ring->read_ptr = ring->cb_ptr;
2336 
2337 		ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
2338 		bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
2339 	}
2340 
2341 	ring->dim.bytes = bytes_processed;
2342 	ring->dim.packets = rxpktprocessed;
2343 
2344 	return rxpktprocessed;
2345 }
2346 
2347 /* Rx NAPI polling method */
bcmgenet_rx_poll(struct napi_struct * napi,int budget)2348 static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
2349 {
2350 	struct bcmgenet_rx_ring *ring = container_of(napi,
2351 			struct bcmgenet_rx_ring, napi);
2352 	struct dim_sample dim_sample = {};
2353 	unsigned int work_done;
2354 
2355 	work_done = bcmgenet_desc_rx(ring, budget);
2356 
2357 	if (work_done < budget) {
2358 		napi_complete_done(napi, work_done);
2359 		ring->int_enable(ring);
2360 	}
2361 
2362 	if (ring->dim.use_dim) {
2363 		dim_update_sample(ring->dim.event_ctr, ring->dim.packets,
2364 				  ring->dim.bytes, &dim_sample);
2365 		net_dim(&ring->dim.dim, dim_sample);
2366 	}
2367 
2368 	return work_done;
2369 }
2370 
bcmgenet_dim_work(struct work_struct * work)2371 static void bcmgenet_dim_work(struct work_struct *work)
2372 {
2373 	struct dim *dim = container_of(work, struct dim, work);
2374 	struct bcmgenet_net_dim *ndim =
2375 			container_of(dim, struct bcmgenet_net_dim, dim);
2376 	struct bcmgenet_rx_ring *ring =
2377 			container_of(ndim, struct bcmgenet_rx_ring, dim);
2378 	struct dim_cq_moder cur_profile =
2379 			net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
2380 
2381 	bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
2382 	dim->state = DIM_START_MEASURE;
2383 }
2384 
2385 /* Assign skb to RX DMA descriptor. */
bcmgenet_alloc_rx_buffers(struct bcmgenet_priv * priv,struct bcmgenet_rx_ring * ring)2386 static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
2387 				     struct bcmgenet_rx_ring *ring)
2388 {
2389 	struct enet_cb *cb;
2390 	struct sk_buff *skb;
2391 	int i;
2392 
2393 	netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
2394 
2395 	/* loop here for each buffer needing assign */
2396 	for (i = 0; i < ring->size; i++) {
2397 		cb = ring->cbs + i;
2398 		skb = bcmgenet_rx_refill(priv, cb);
2399 		if (skb)
2400 			dev_consume_skb_any(skb);
2401 		if (!cb->skb)
2402 			return -ENOMEM;
2403 	}
2404 
2405 	return 0;
2406 }
2407 
bcmgenet_free_rx_buffers(struct bcmgenet_priv * priv)2408 static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
2409 {
2410 	struct sk_buff *skb;
2411 	struct enet_cb *cb;
2412 	int i;
2413 
2414 	for (i = 0; i < priv->num_rx_bds; i++) {
2415 		cb = &priv->rx_cbs[i];
2416 
2417 		skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
2418 		if (skb)
2419 			dev_consume_skb_any(skb);
2420 	}
2421 }
2422 
umac_enable_set(struct bcmgenet_priv * priv,u32 mask,bool enable)2423 static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
2424 {
2425 	u32 reg;
2426 
2427 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
2428 	if (reg & CMD_SW_RESET)
2429 		return;
2430 	if (enable)
2431 		reg |= mask;
2432 	else
2433 		reg &= ~mask;
2434 	bcmgenet_umac_writel(priv, reg, UMAC_CMD);
2435 
2436 	/* UniMAC stops on a packet boundary, wait for a full-size packet
2437 	 * to be processed
2438 	 */
2439 	if (enable == 0)
2440 		usleep_range(1000, 2000);
2441 }
2442 
reset_umac(struct bcmgenet_priv * priv)2443 static void reset_umac(struct bcmgenet_priv *priv)
2444 {
2445 	/* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
2446 	bcmgenet_rbuf_ctrl_set(priv, 0);
2447 	udelay(10);
2448 
2449 	/* issue soft reset and disable MAC while updating its registers */
2450 	bcmgenet_umac_writel(priv, CMD_SW_RESET, UMAC_CMD);
2451 	udelay(2);
2452 }
2453 
bcmgenet_intr_disable(struct bcmgenet_priv * priv)2454 static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
2455 {
2456 	/* Mask all interrupts.*/
2457 	bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2458 	bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
2459 	bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
2460 	bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
2461 }
2462 
bcmgenet_link_intr_enable(struct bcmgenet_priv * priv)2463 static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
2464 {
2465 	u32 int0_enable = 0;
2466 
2467 	/* Monitor cable plug/unplugged event for internal PHY, external PHY
2468 	 * and MoCA PHY
2469 	 */
2470 	if (priv->internal_phy) {
2471 		int0_enable |= UMAC_IRQ_LINK_EVENT;
2472 		if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
2473 			int0_enable |= UMAC_IRQ_PHY_DET_R;
2474 	} else if (priv->ext_phy) {
2475 		int0_enable |= UMAC_IRQ_LINK_EVENT;
2476 	} else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2477 		if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
2478 			int0_enable |= UMAC_IRQ_LINK_EVENT;
2479 	}
2480 	bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2481 }
2482 
init_umac(struct bcmgenet_priv * priv)2483 static void init_umac(struct bcmgenet_priv *priv)
2484 {
2485 	struct device *kdev = &priv->pdev->dev;
2486 	u32 reg;
2487 	u32 int0_enable = 0;
2488 
2489 	dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
2490 
2491 	reset_umac(priv);
2492 
2493 	/* clear tx/rx counter */
2494 	bcmgenet_umac_writel(priv,
2495 			     MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
2496 			     UMAC_MIB_CTRL);
2497 	bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
2498 
2499 	bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2500 
2501 	/* init tx registers, enable TSB */
2502 	reg = bcmgenet_tbuf_ctrl_get(priv);
2503 	reg |= TBUF_64B_EN;
2504 	bcmgenet_tbuf_ctrl_set(priv, reg);
2505 
2506 	/* init rx registers, enable ip header optimization and RSB */
2507 	reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
2508 	reg |= RBUF_ALIGN_2B | RBUF_64B_EN;
2509 	bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
2510 
2511 	/* enable rx checksumming */
2512 	reg = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
2513 	reg |= RBUF_RXCHK_EN | RBUF_L3_PARSE_DIS;
2514 	/* If UniMAC forwards CRC, we need to skip over it to get
2515 	 * a valid CHK bit to be set in the per-packet status word
2516 	 */
2517 	if (priv->crc_fwd_en)
2518 		reg |= RBUF_SKIP_FCS;
2519 	else
2520 		reg &= ~RBUF_SKIP_FCS;
2521 	bcmgenet_rbuf_writel(priv, reg, RBUF_CHK_CTRL);
2522 
2523 	if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
2524 		bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
2525 
2526 	bcmgenet_intr_disable(priv);
2527 
2528 	/* Configure backpressure vectors for MoCA */
2529 	if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
2530 		reg = bcmgenet_bp_mc_get(priv);
2531 		reg |= BIT(priv->hw_params->bp_in_en_shift);
2532 
2533 		/* bp_mask: back pressure mask */
2534 		if (netif_is_multiqueue(priv->dev))
2535 			reg |= priv->hw_params->bp_in_mask;
2536 		else
2537 			reg &= ~priv->hw_params->bp_in_mask;
2538 		bcmgenet_bp_mc_set(priv, reg);
2539 	}
2540 
2541 	/* Enable MDIO interrupts on GENET v3+ */
2542 	if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
2543 		int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
2544 
2545 	bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
2546 
2547 	dev_dbg(kdev, "done init umac\n");
2548 }
2549 
bcmgenet_init_dim(struct bcmgenet_rx_ring * ring,void (* cb)(struct work_struct * work))2550 static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
2551 			      void (*cb)(struct work_struct *work))
2552 {
2553 	struct bcmgenet_net_dim *dim = &ring->dim;
2554 
2555 	INIT_WORK(&dim->dim.work, cb);
2556 	dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2557 	dim->event_ctr = 0;
2558 	dim->packets = 0;
2559 	dim->bytes = 0;
2560 }
2561 
bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring * ring)2562 static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
2563 {
2564 	struct bcmgenet_net_dim *dim = &ring->dim;
2565 	struct dim_cq_moder moder;
2566 	u32 usecs, pkts;
2567 
2568 	usecs = ring->rx_coalesce_usecs;
2569 	pkts = ring->rx_max_coalesced_frames;
2570 
2571 	/* If DIM was enabled, re-apply default parameters */
2572 	if (dim->use_dim) {
2573 		moder = net_dim_get_def_rx_moderation(dim->dim.mode);
2574 		usecs = moder.usec;
2575 		pkts = moder.pkts;
2576 	}
2577 
2578 	bcmgenet_set_rx_coalesce(ring, usecs, pkts);
2579 }
2580 
2581 /* Initialize a Tx ring along with corresponding hardware registers */
bcmgenet_init_tx_ring(struct bcmgenet_priv * priv,unsigned int index,unsigned int size,unsigned int start_ptr,unsigned int end_ptr)2582 static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
2583 				  unsigned int index, unsigned int size,
2584 				  unsigned int start_ptr, unsigned int end_ptr)
2585 {
2586 	struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
2587 	u32 words_per_bd = WORDS_PER_BD(priv);
2588 	u32 flow_period_val = 0;
2589 
2590 	spin_lock_init(&ring->lock);
2591 	ring->priv = priv;
2592 	ring->index = index;
2593 	if (index == DESC_INDEX) {
2594 		ring->queue = 0;
2595 		ring->int_enable = bcmgenet_tx_ring16_int_enable;
2596 		ring->int_disable = bcmgenet_tx_ring16_int_disable;
2597 	} else {
2598 		ring->queue = index + 1;
2599 		ring->int_enable = bcmgenet_tx_ring_int_enable;
2600 		ring->int_disable = bcmgenet_tx_ring_int_disable;
2601 	}
2602 	ring->cbs = priv->tx_cbs + start_ptr;
2603 	ring->size = size;
2604 	ring->clean_ptr = start_ptr;
2605 	ring->c_index = 0;
2606 	ring->free_bds = size;
2607 	ring->write_ptr = start_ptr;
2608 	ring->cb_ptr = start_ptr;
2609 	ring->end_ptr = end_ptr - 1;
2610 	ring->prod_index = 0;
2611 
2612 	/* Set flow period for ring != 16 */
2613 	if (index != DESC_INDEX)
2614 		flow_period_val = ENET_MAX_MTU_SIZE << 16;
2615 
2616 	bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
2617 	bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
2618 	bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
2619 	/* Disable rate control for now */
2620 	bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
2621 				  TDMA_FLOW_PERIOD);
2622 	bcmgenet_tdma_ring_writel(priv, index,
2623 				  ((size << DMA_RING_SIZE_SHIFT) |
2624 				   RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
2625 
2626 	/* Set start and end address, read and write pointers */
2627 	bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
2628 				  DMA_START_ADDR);
2629 	bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
2630 				  TDMA_READ_PTR);
2631 	bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
2632 				  TDMA_WRITE_PTR);
2633 	bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
2634 				  DMA_END_ADDR);
2635 
2636 	/* Initialize Tx NAPI */
2637 	netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
2638 			  NAPI_POLL_WEIGHT);
2639 }
2640 
2641 /* Initialize a RDMA ring */
bcmgenet_init_rx_ring(struct bcmgenet_priv * priv,unsigned int index,unsigned int size,unsigned int start_ptr,unsigned int end_ptr)2642 static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
2643 				 unsigned int index, unsigned int size,
2644 				 unsigned int start_ptr, unsigned int end_ptr)
2645 {
2646 	struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
2647 	u32 words_per_bd = WORDS_PER_BD(priv);
2648 	int ret;
2649 
2650 	ring->priv = priv;
2651 	ring->index = index;
2652 	if (index == DESC_INDEX) {
2653 		ring->int_enable = bcmgenet_rx_ring16_int_enable;
2654 		ring->int_disable = bcmgenet_rx_ring16_int_disable;
2655 	} else {
2656 		ring->int_enable = bcmgenet_rx_ring_int_enable;
2657 		ring->int_disable = bcmgenet_rx_ring_int_disable;
2658 	}
2659 	ring->cbs = priv->rx_cbs + start_ptr;
2660 	ring->size = size;
2661 	ring->c_index = 0;
2662 	ring->read_ptr = start_ptr;
2663 	ring->cb_ptr = start_ptr;
2664 	ring->end_ptr = end_ptr - 1;
2665 
2666 	ret = bcmgenet_alloc_rx_buffers(priv, ring);
2667 	if (ret)
2668 		return ret;
2669 
2670 	bcmgenet_init_dim(ring, bcmgenet_dim_work);
2671 	bcmgenet_init_rx_coalesce(ring);
2672 
2673 	/* Initialize Rx NAPI */
2674 	netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
2675 		       NAPI_POLL_WEIGHT);
2676 
2677 	bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
2678 	bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
2679 	bcmgenet_rdma_ring_writel(priv, index,
2680 				  ((size << DMA_RING_SIZE_SHIFT) |
2681 				   RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
2682 	bcmgenet_rdma_ring_writel(priv, index,
2683 				  (DMA_FC_THRESH_LO <<
2684 				   DMA_XOFF_THRESHOLD_SHIFT) |
2685 				   DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
2686 
2687 	/* Set start and end address, read and write pointers */
2688 	bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2689 				  DMA_START_ADDR);
2690 	bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2691 				  RDMA_READ_PTR);
2692 	bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
2693 				  RDMA_WRITE_PTR);
2694 	bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
2695 				  DMA_END_ADDR);
2696 
2697 	return ret;
2698 }
2699 
bcmgenet_enable_tx_napi(struct bcmgenet_priv * priv)2700 static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
2701 {
2702 	unsigned int i;
2703 	struct bcmgenet_tx_ring *ring;
2704 
2705 	for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2706 		ring = &priv->tx_rings[i];
2707 		napi_enable(&ring->napi);
2708 		ring->int_enable(ring);
2709 	}
2710 
2711 	ring = &priv->tx_rings[DESC_INDEX];
2712 	napi_enable(&ring->napi);
2713 	ring->int_enable(ring);
2714 }
2715 
bcmgenet_disable_tx_napi(struct bcmgenet_priv * priv)2716 static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
2717 {
2718 	unsigned int i;
2719 	struct bcmgenet_tx_ring *ring;
2720 
2721 	for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2722 		ring = &priv->tx_rings[i];
2723 		napi_disable(&ring->napi);
2724 	}
2725 
2726 	ring = &priv->tx_rings[DESC_INDEX];
2727 	napi_disable(&ring->napi);
2728 }
2729 
bcmgenet_fini_tx_napi(struct bcmgenet_priv * priv)2730 static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
2731 {
2732 	unsigned int i;
2733 	struct bcmgenet_tx_ring *ring;
2734 
2735 	for (i = 0; i < priv->hw_params->tx_queues; ++i) {
2736 		ring = &priv->tx_rings[i];
2737 		netif_napi_del(&ring->napi);
2738 	}
2739 
2740 	ring = &priv->tx_rings[DESC_INDEX];
2741 	netif_napi_del(&ring->napi);
2742 }
2743 
2744 /* Initialize Tx queues
2745  *
2746  * Queues 0-3 are priority-based, each one has 32 descriptors,
2747  * with queue 0 being the highest priority queue.
2748  *
2749  * Queue 16 is the default Tx queue with
2750  * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
2751  *
2752  * The transmit control block pool is then partitioned as follows:
2753  * - Tx queue 0 uses tx_cbs[0..31]
2754  * - Tx queue 1 uses tx_cbs[32..63]
2755  * - Tx queue 2 uses tx_cbs[64..95]
2756  * - Tx queue 3 uses tx_cbs[96..127]
2757  * - Tx queue 16 uses tx_cbs[128..255]
2758  */
bcmgenet_init_tx_queues(struct net_device * dev)2759 static void bcmgenet_init_tx_queues(struct net_device *dev)
2760 {
2761 	struct bcmgenet_priv *priv = netdev_priv(dev);
2762 	u32 i, dma_enable;
2763 	u32 dma_ctrl, ring_cfg;
2764 	u32 dma_priority[3] = {0, 0, 0};
2765 
2766 	dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
2767 	dma_enable = dma_ctrl & DMA_EN;
2768 	dma_ctrl &= ~DMA_EN;
2769 	bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2770 
2771 	dma_ctrl = 0;
2772 	ring_cfg = 0;
2773 
2774 	/* Enable strict priority arbiter mode */
2775 	bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
2776 
2777 	/* Initialize Tx priority queues */
2778 	for (i = 0; i < priv->hw_params->tx_queues; i++) {
2779 		bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
2780 				      i * priv->hw_params->tx_bds_per_q,
2781 				      (i + 1) * priv->hw_params->tx_bds_per_q);
2782 		ring_cfg |= (1 << i);
2783 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2784 		dma_priority[DMA_PRIO_REG_INDEX(i)] |=
2785 			((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
2786 	}
2787 
2788 	/* Initialize Tx default queue 16 */
2789 	bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
2790 			      priv->hw_params->tx_queues *
2791 			      priv->hw_params->tx_bds_per_q,
2792 			      TOTAL_DESC);
2793 	ring_cfg |= (1 << DESC_INDEX);
2794 	dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2795 	dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
2796 		((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
2797 		 DMA_PRIO_REG_SHIFT(DESC_INDEX));
2798 
2799 	/* Set Tx queue priorities */
2800 	bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
2801 	bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
2802 	bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
2803 
2804 	/* Enable Tx queues */
2805 	bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
2806 
2807 	/* Enable Tx DMA */
2808 	if (dma_enable)
2809 		dma_ctrl |= DMA_EN;
2810 	bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
2811 }
2812 
bcmgenet_enable_rx_napi(struct bcmgenet_priv * priv)2813 static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
2814 {
2815 	unsigned int i;
2816 	struct bcmgenet_rx_ring *ring;
2817 
2818 	for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2819 		ring = &priv->rx_rings[i];
2820 		napi_enable(&ring->napi);
2821 		ring->int_enable(ring);
2822 	}
2823 
2824 	ring = &priv->rx_rings[DESC_INDEX];
2825 	napi_enable(&ring->napi);
2826 	ring->int_enable(ring);
2827 }
2828 
bcmgenet_disable_rx_napi(struct bcmgenet_priv * priv)2829 static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
2830 {
2831 	unsigned int i;
2832 	struct bcmgenet_rx_ring *ring;
2833 
2834 	for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2835 		ring = &priv->rx_rings[i];
2836 		napi_disable(&ring->napi);
2837 		cancel_work_sync(&ring->dim.dim.work);
2838 	}
2839 
2840 	ring = &priv->rx_rings[DESC_INDEX];
2841 	napi_disable(&ring->napi);
2842 	cancel_work_sync(&ring->dim.dim.work);
2843 }
2844 
bcmgenet_fini_rx_napi(struct bcmgenet_priv * priv)2845 static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
2846 {
2847 	unsigned int i;
2848 	struct bcmgenet_rx_ring *ring;
2849 
2850 	for (i = 0; i < priv->hw_params->rx_queues; ++i) {
2851 		ring = &priv->rx_rings[i];
2852 		netif_napi_del(&ring->napi);
2853 	}
2854 
2855 	ring = &priv->rx_rings[DESC_INDEX];
2856 	netif_napi_del(&ring->napi);
2857 }
2858 
2859 /* Initialize Rx queues
2860  *
2861  * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
2862  * used to direct traffic to these queues.
2863  *
2864  * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
2865  */
bcmgenet_init_rx_queues(struct net_device * dev)2866 static int bcmgenet_init_rx_queues(struct net_device *dev)
2867 {
2868 	struct bcmgenet_priv *priv = netdev_priv(dev);
2869 	u32 i;
2870 	u32 dma_enable;
2871 	u32 dma_ctrl;
2872 	u32 ring_cfg;
2873 	int ret;
2874 
2875 	dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
2876 	dma_enable = dma_ctrl & DMA_EN;
2877 	dma_ctrl &= ~DMA_EN;
2878 	bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2879 
2880 	dma_ctrl = 0;
2881 	ring_cfg = 0;
2882 
2883 	/* Initialize Rx priority queues */
2884 	for (i = 0; i < priv->hw_params->rx_queues; i++) {
2885 		ret = bcmgenet_init_rx_ring(priv, i,
2886 					    priv->hw_params->rx_bds_per_q,
2887 					    i * priv->hw_params->rx_bds_per_q,
2888 					    (i + 1) *
2889 					    priv->hw_params->rx_bds_per_q);
2890 		if (ret)
2891 			return ret;
2892 
2893 		ring_cfg |= (1 << i);
2894 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2895 	}
2896 
2897 	/* Initialize Rx default queue 16 */
2898 	ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
2899 				    priv->hw_params->rx_queues *
2900 				    priv->hw_params->rx_bds_per_q,
2901 				    TOTAL_DESC);
2902 	if (ret)
2903 		return ret;
2904 
2905 	ring_cfg |= (1 << DESC_INDEX);
2906 	dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
2907 
2908 	/* Enable rings */
2909 	bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
2910 
2911 	/* Configure ring as descriptor ring and re-enable DMA if enabled */
2912 	if (dma_enable)
2913 		dma_ctrl |= DMA_EN;
2914 	bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
2915 
2916 	return 0;
2917 }
2918 
bcmgenet_dma_teardown(struct bcmgenet_priv * priv)2919 static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
2920 {
2921 	int ret = 0;
2922 	int timeout = 0;
2923 	u32 reg;
2924 	u32 dma_ctrl;
2925 	int i;
2926 
2927 	/* Disable TDMA to stop add more frames in TX DMA */
2928 	reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2929 	reg &= ~DMA_EN;
2930 	bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2931 
2932 	/* Check TDMA status register to confirm TDMA is disabled */
2933 	while (timeout++ < DMA_TIMEOUT_VAL) {
2934 		reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
2935 		if (reg & DMA_DISABLED)
2936 			break;
2937 
2938 		udelay(1);
2939 	}
2940 
2941 	if (timeout == DMA_TIMEOUT_VAL) {
2942 		netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
2943 		ret = -ETIMEDOUT;
2944 	}
2945 
2946 	/* Wait 10ms for packet drain in both tx and rx dma */
2947 	usleep_range(10000, 20000);
2948 
2949 	/* Disable RDMA */
2950 	reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2951 	reg &= ~DMA_EN;
2952 	bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2953 
2954 	timeout = 0;
2955 	/* Check RDMA status register to confirm RDMA is disabled */
2956 	while (timeout++ < DMA_TIMEOUT_VAL) {
2957 		reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
2958 		if (reg & DMA_DISABLED)
2959 			break;
2960 
2961 		udelay(1);
2962 	}
2963 
2964 	if (timeout == DMA_TIMEOUT_VAL) {
2965 		netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
2966 		ret = -ETIMEDOUT;
2967 	}
2968 
2969 	dma_ctrl = 0;
2970 	for (i = 0; i < priv->hw_params->rx_queues; i++)
2971 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2972 	reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
2973 	reg &= ~dma_ctrl;
2974 	bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
2975 
2976 	dma_ctrl = 0;
2977 	for (i = 0; i < priv->hw_params->tx_queues; i++)
2978 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
2979 	reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
2980 	reg &= ~dma_ctrl;
2981 	bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
2982 
2983 	return ret;
2984 }
2985 
bcmgenet_fini_dma(struct bcmgenet_priv * priv)2986 static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
2987 {
2988 	struct netdev_queue *txq;
2989 	int i;
2990 
2991 	bcmgenet_fini_rx_napi(priv);
2992 	bcmgenet_fini_tx_napi(priv);
2993 
2994 	for (i = 0; i < priv->num_tx_bds; i++)
2995 		dev_kfree_skb(bcmgenet_free_tx_cb(&priv->pdev->dev,
2996 						  priv->tx_cbs + i));
2997 
2998 	for (i = 0; i < priv->hw_params->tx_queues; i++) {
2999 		txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
3000 		netdev_tx_reset_queue(txq);
3001 	}
3002 
3003 	txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
3004 	netdev_tx_reset_queue(txq);
3005 
3006 	bcmgenet_free_rx_buffers(priv);
3007 	kfree(priv->rx_cbs);
3008 	kfree(priv->tx_cbs);
3009 }
3010 
3011 /* init_edma: Initialize DMA control register */
bcmgenet_init_dma(struct bcmgenet_priv * priv)3012 static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
3013 {
3014 	int ret;
3015 	unsigned int i;
3016 	struct enet_cb *cb;
3017 
3018 	netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
3019 
3020 	/* Initialize common Rx ring structures */
3021 	priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
3022 	priv->num_rx_bds = TOTAL_DESC;
3023 	priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
3024 			       GFP_KERNEL);
3025 	if (!priv->rx_cbs)
3026 		return -ENOMEM;
3027 
3028 	for (i = 0; i < priv->num_rx_bds; i++) {
3029 		cb = priv->rx_cbs + i;
3030 		cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
3031 	}
3032 
3033 	/* Initialize common TX ring structures */
3034 	priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
3035 	priv->num_tx_bds = TOTAL_DESC;
3036 	priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
3037 			       GFP_KERNEL);
3038 	if (!priv->tx_cbs) {
3039 		kfree(priv->rx_cbs);
3040 		return -ENOMEM;
3041 	}
3042 
3043 	for (i = 0; i < priv->num_tx_bds; i++) {
3044 		cb = priv->tx_cbs + i;
3045 		cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
3046 	}
3047 
3048 	/* Init rDma */
3049 	bcmgenet_rdma_writel(priv, priv->dma_max_burst_length,
3050 			     DMA_SCB_BURST_SIZE);
3051 
3052 	/* Initialize Rx queues */
3053 	ret = bcmgenet_init_rx_queues(priv->dev);
3054 	if (ret) {
3055 		netdev_err(priv->dev, "failed to initialize Rx queues\n");
3056 		bcmgenet_free_rx_buffers(priv);
3057 		kfree(priv->rx_cbs);
3058 		kfree(priv->tx_cbs);
3059 		return ret;
3060 	}
3061 
3062 	/* Init tDma */
3063 	bcmgenet_tdma_writel(priv, priv->dma_max_burst_length,
3064 			     DMA_SCB_BURST_SIZE);
3065 
3066 	/* Initialize Tx queues */
3067 	bcmgenet_init_tx_queues(priv->dev);
3068 
3069 	return 0;
3070 }
3071 
3072 /* Interrupt bottom half */
bcmgenet_irq_task(struct work_struct * work)3073 static void bcmgenet_irq_task(struct work_struct *work)
3074 {
3075 	unsigned int status;
3076 	struct bcmgenet_priv *priv = container_of(
3077 			work, struct bcmgenet_priv, bcmgenet_irq_work);
3078 
3079 	netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
3080 
3081 	spin_lock_irq(&priv->lock);
3082 	status = priv->irq0_stat;
3083 	priv->irq0_stat = 0;
3084 	spin_unlock_irq(&priv->lock);
3085 
3086 	if (status & UMAC_IRQ_PHY_DET_R &&
3087 	    priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
3088 		phy_init_hw(priv->dev->phydev);
3089 		genphy_config_aneg(priv->dev->phydev);
3090 	}
3091 
3092 	/* Link UP/DOWN event */
3093 	if (status & UMAC_IRQ_LINK_EVENT)
3094 		phy_mac_interrupt(priv->dev->phydev);
3095 
3096 }
3097 
3098 /* bcmgenet_isr1: handle Rx and Tx priority queues */
bcmgenet_isr1(int irq,void * dev_id)3099 static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
3100 {
3101 	struct bcmgenet_priv *priv = dev_id;
3102 	struct bcmgenet_rx_ring *rx_ring;
3103 	struct bcmgenet_tx_ring *tx_ring;
3104 	unsigned int index, status;
3105 
3106 	/* Read irq status */
3107 	status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
3108 		~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3109 
3110 	/* clear interrupts */
3111 	bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
3112 
3113 	netif_dbg(priv, intr, priv->dev,
3114 		  "%s: IRQ=0x%x\n", __func__, status);
3115 
3116 	/* Check Rx priority queue interrupts */
3117 	for (index = 0; index < priv->hw_params->rx_queues; index++) {
3118 		if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
3119 			continue;
3120 
3121 		rx_ring = &priv->rx_rings[index];
3122 		rx_ring->dim.event_ctr++;
3123 
3124 		if (likely(napi_schedule_prep(&rx_ring->napi))) {
3125 			rx_ring->int_disable(rx_ring);
3126 			__napi_schedule_irqoff(&rx_ring->napi);
3127 		}
3128 	}
3129 
3130 	/* Check Tx priority queue interrupts */
3131 	for (index = 0; index < priv->hw_params->tx_queues; index++) {
3132 		if (!(status & BIT(index)))
3133 			continue;
3134 
3135 		tx_ring = &priv->tx_rings[index];
3136 
3137 		if (likely(napi_schedule_prep(&tx_ring->napi))) {
3138 			tx_ring->int_disable(tx_ring);
3139 			__napi_schedule_irqoff(&tx_ring->napi);
3140 		}
3141 	}
3142 
3143 	return IRQ_HANDLED;
3144 }
3145 
3146 /* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
bcmgenet_isr0(int irq,void * dev_id)3147 static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
3148 {
3149 	struct bcmgenet_priv *priv = dev_id;
3150 	struct bcmgenet_rx_ring *rx_ring;
3151 	struct bcmgenet_tx_ring *tx_ring;
3152 	unsigned int status;
3153 	unsigned long flags;
3154 
3155 	/* Read irq status */
3156 	status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
3157 		~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3158 
3159 	/* clear interrupts */
3160 	bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
3161 
3162 	netif_dbg(priv, intr, priv->dev,
3163 		  "IRQ=0x%x\n", status);
3164 
3165 	if (status & UMAC_IRQ_RXDMA_DONE) {
3166 		rx_ring = &priv->rx_rings[DESC_INDEX];
3167 		rx_ring->dim.event_ctr++;
3168 
3169 		if (likely(napi_schedule_prep(&rx_ring->napi))) {
3170 			rx_ring->int_disable(rx_ring);
3171 			__napi_schedule_irqoff(&rx_ring->napi);
3172 		}
3173 	}
3174 
3175 	if (status & UMAC_IRQ_TXDMA_DONE) {
3176 		tx_ring = &priv->tx_rings[DESC_INDEX];
3177 
3178 		if (likely(napi_schedule_prep(&tx_ring->napi))) {
3179 			tx_ring->int_disable(tx_ring);
3180 			__napi_schedule_irqoff(&tx_ring->napi);
3181 		}
3182 	}
3183 
3184 	if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
3185 		status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
3186 		wake_up(&priv->wq);
3187 	}
3188 
3189 	/* all other interested interrupts handled in bottom half */
3190 	status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
3191 	if (status) {
3192 		/* Save irq status for bottom-half processing. */
3193 		spin_lock_irqsave(&priv->lock, flags);
3194 		priv->irq0_stat |= status;
3195 		spin_unlock_irqrestore(&priv->lock, flags);
3196 
3197 		schedule_work(&priv->bcmgenet_irq_work);
3198 	}
3199 
3200 	return IRQ_HANDLED;
3201 }
3202 
bcmgenet_wol_isr(int irq,void * dev_id)3203 static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
3204 {
3205 	/* Acknowledge the interrupt */
3206 	return IRQ_HANDLED;
3207 }
3208 
3209 #ifdef CONFIG_NET_POLL_CONTROLLER
bcmgenet_poll_controller(struct net_device * dev)3210 static void bcmgenet_poll_controller(struct net_device *dev)
3211 {
3212 	struct bcmgenet_priv *priv = netdev_priv(dev);
3213 
3214 	/* Invoke the main RX/TX interrupt handler */
3215 	disable_irq(priv->irq0);
3216 	bcmgenet_isr0(priv->irq0, priv);
3217 	enable_irq(priv->irq0);
3218 
3219 	/* And the interrupt handler for RX/TX priority queues */
3220 	disable_irq(priv->irq1);
3221 	bcmgenet_isr1(priv->irq1, priv);
3222 	enable_irq(priv->irq1);
3223 }
3224 #endif
3225 
bcmgenet_umac_reset(struct bcmgenet_priv * priv)3226 static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
3227 {
3228 	u32 reg;
3229 
3230 	reg = bcmgenet_rbuf_ctrl_get(priv);
3231 	reg |= BIT(1);
3232 	bcmgenet_rbuf_ctrl_set(priv, reg);
3233 	udelay(10);
3234 
3235 	reg &= ~BIT(1);
3236 	bcmgenet_rbuf_ctrl_set(priv, reg);
3237 	udelay(10);
3238 }
3239 
bcmgenet_set_hw_addr(struct bcmgenet_priv * priv,const unsigned char * addr)3240 static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
3241 				 const unsigned char *addr)
3242 {
3243 	bcmgenet_umac_writel(priv, get_unaligned_be32(&addr[0]), UMAC_MAC0);
3244 	bcmgenet_umac_writel(priv, get_unaligned_be16(&addr[4]), UMAC_MAC1);
3245 }
3246 
bcmgenet_get_hw_addr(struct bcmgenet_priv * priv,unsigned char * addr)3247 static void bcmgenet_get_hw_addr(struct bcmgenet_priv *priv,
3248 				 unsigned char *addr)
3249 {
3250 	u32 addr_tmp;
3251 
3252 	addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC0);
3253 	put_unaligned_be32(addr_tmp, &addr[0]);
3254 	addr_tmp = bcmgenet_umac_readl(priv, UMAC_MAC1);
3255 	put_unaligned_be16(addr_tmp, &addr[4]);
3256 }
3257 
3258 /* Returns a reusable dma control register value */
bcmgenet_dma_disable(struct bcmgenet_priv * priv)3259 static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
3260 {
3261 	unsigned int i;
3262 	u32 reg;
3263 	u32 dma_ctrl;
3264 
3265 	/* disable DMA */
3266 	dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
3267 	for (i = 0; i < priv->hw_params->tx_queues; i++)
3268 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
3269 	reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3270 	reg &= ~dma_ctrl;
3271 	bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3272 
3273 	dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
3274 	for (i = 0; i < priv->hw_params->rx_queues; i++)
3275 		dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
3276 	reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3277 	reg &= ~dma_ctrl;
3278 	bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3279 
3280 	bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
3281 	udelay(10);
3282 	bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
3283 
3284 	return dma_ctrl;
3285 }
3286 
bcmgenet_enable_dma(struct bcmgenet_priv * priv,u32 dma_ctrl)3287 static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
3288 {
3289 	u32 reg;
3290 
3291 	reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
3292 	reg |= dma_ctrl;
3293 	bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
3294 
3295 	reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
3296 	reg |= dma_ctrl;
3297 	bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
3298 }
3299 
bcmgenet_netif_start(struct net_device * dev)3300 static void bcmgenet_netif_start(struct net_device *dev)
3301 {
3302 	struct bcmgenet_priv *priv = netdev_priv(dev);
3303 
3304 	/* Start the network engine */
3305 	bcmgenet_set_rx_mode(dev);
3306 	bcmgenet_enable_rx_napi(priv);
3307 
3308 	umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
3309 
3310 	bcmgenet_enable_tx_napi(priv);
3311 
3312 	/* Monitor link interrupts now */
3313 	bcmgenet_link_intr_enable(priv);
3314 
3315 	phy_start(dev->phydev);
3316 }
3317 
bcmgenet_open(struct net_device * dev)3318 static int bcmgenet_open(struct net_device *dev)
3319 {
3320 	struct bcmgenet_priv *priv = netdev_priv(dev);
3321 	unsigned long dma_ctrl;
3322 	int ret;
3323 
3324 	netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
3325 
3326 	/* Turn on the clock */
3327 	clk_prepare_enable(priv->clk);
3328 
3329 	/* If this is an internal GPHY, power it back on now, before UniMAC is
3330 	 * brought out of reset as absolutely no UniMAC activity is allowed
3331 	 */
3332 	if (priv->internal_phy)
3333 		bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
3334 
3335 	/* take MAC out of reset */
3336 	bcmgenet_umac_reset(priv);
3337 
3338 	init_umac(priv);
3339 
3340 	/* Apply features again in case we changed them while interface was
3341 	 * down
3342 	 */
3343 	bcmgenet_set_features(dev, dev->features);
3344 
3345 	bcmgenet_set_hw_addr(priv, dev->dev_addr);
3346 
3347 	/* Disable RX/TX DMA and flush TX queues */
3348 	dma_ctrl = bcmgenet_dma_disable(priv);
3349 
3350 	/* Reinitialize TDMA and RDMA and SW housekeeping */
3351 	ret = bcmgenet_init_dma(priv);
3352 	if (ret) {
3353 		netdev_err(dev, "failed to initialize DMA\n");
3354 		goto err_clk_disable;
3355 	}
3356 
3357 	/* Always enable ring 16 - descriptor ring */
3358 	bcmgenet_enable_dma(priv, dma_ctrl);
3359 
3360 	/* HFB init */
3361 	bcmgenet_hfb_init(priv);
3362 
3363 	ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
3364 			  dev->name, priv);
3365 	if (ret < 0) {
3366 		netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
3367 		goto err_fini_dma;
3368 	}
3369 
3370 	ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
3371 			  dev->name, priv);
3372 	if (ret < 0) {
3373 		netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
3374 		goto err_irq0;
3375 	}
3376 
3377 	ret = bcmgenet_mii_probe(dev);
3378 	if (ret) {
3379 		netdev_err(dev, "failed to connect to PHY\n");
3380 		goto err_irq1;
3381 	}
3382 
3383 	bcmgenet_netif_start(dev);
3384 
3385 	netif_tx_start_all_queues(dev);
3386 
3387 	return 0;
3388 
3389 err_irq1:
3390 	free_irq(priv->irq1, priv);
3391 err_irq0:
3392 	free_irq(priv->irq0, priv);
3393 err_fini_dma:
3394 	bcmgenet_dma_teardown(priv);
3395 	bcmgenet_fini_dma(priv);
3396 err_clk_disable:
3397 	if (priv->internal_phy)
3398 		bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3399 	clk_disable_unprepare(priv->clk);
3400 	return ret;
3401 }
3402 
bcmgenet_netif_stop(struct net_device * dev,bool stop_phy)3403 static void bcmgenet_netif_stop(struct net_device *dev, bool stop_phy)
3404 {
3405 	struct bcmgenet_priv *priv = netdev_priv(dev);
3406 
3407 	bcmgenet_disable_tx_napi(priv);
3408 	netif_tx_disable(dev);
3409 
3410 	/* Disable MAC receive */
3411 	umac_enable_set(priv, CMD_RX_EN, false);
3412 
3413 	bcmgenet_dma_teardown(priv);
3414 
3415 	/* Disable MAC transmit. TX DMA disabled must be done before this */
3416 	umac_enable_set(priv, CMD_TX_EN, false);
3417 
3418 	if (stop_phy)
3419 		phy_stop(dev->phydev);
3420 	bcmgenet_disable_rx_napi(priv);
3421 	bcmgenet_intr_disable(priv);
3422 
3423 	/* Wait for pending work items to complete. Since interrupts are
3424 	 * disabled no new work will be scheduled.
3425 	 */
3426 	cancel_work_sync(&priv->bcmgenet_irq_work);
3427 
3428 	priv->old_link = -1;
3429 	priv->old_speed = -1;
3430 	priv->old_duplex = -1;
3431 	priv->old_pause = -1;
3432 
3433 	/* tx reclaim */
3434 	bcmgenet_tx_reclaim_all(dev);
3435 	bcmgenet_fini_dma(priv);
3436 }
3437 
bcmgenet_close(struct net_device * dev)3438 static int bcmgenet_close(struct net_device *dev)
3439 {
3440 	struct bcmgenet_priv *priv = netdev_priv(dev);
3441 	int ret = 0;
3442 
3443 	netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
3444 
3445 	bcmgenet_netif_stop(dev, false);
3446 
3447 	/* Really kill the PHY state machine and disconnect from it */
3448 	phy_disconnect(dev->phydev);
3449 
3450 	free_irq(priv->irq0, priv);
3451 	free_irq(priv->irq1, priv);
3452 
3453 	if (priv->internal_phy)
3454 		ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
3455 
3456 	clk_disable_unprepare(priv->clk);
3457 
3458 	return ret;
3459 }
3460 
bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring * ring)3461 static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
3462 {
3463 	struct bcmgenet_priv *priv = ring->priv;
3464 	u32 p_index, c_index, intsts, intmsk;
3465 	struct netdev_queue *txq;
3466 	unsigned int free_bds;
3467 	bool txq_stopped;
3468 
3469 	if (!netif_msg_tx_err(priv))
3470 		return;
3471 
3472 	txq = netdev_get_tx_queue(priv->dev, ring->queue);
3473 
3474 	spin_lock(&ring->lock);
3475 	if (ring->index == DESC_INDEX) {
3476 		intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
3477 		intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
3478 	} else {
3479 		intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
3480 		intmsk = 1 << ring->index;
3481 	}
3482 	c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
3483 	p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
3484 	txq_stopped = netif_tx_queue_stopped(txq);
3485 	free_bds = ring->free_bds;
3486 	spin_unlock(&ring->lock);
3487 
3488 	netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
3489 		  "TX queue status: %s, interrupts: %s\n"
3490 		  "(sw)free_bds: %d (sw)size: %d\n"
3491 		  "(sw)p_index: %d (hw)p_index: %d\n"
3492 		  "(sw)c_index: %d (hw)c_index: %d\n"
3493 		  "(sw)clean_p: %d (sw)write_p: %d\n"
3494 		  "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
3495 		  ring->index, ring->queue,
3496 		  txq_stopped ? "stopped" : "active",
3497 		  intsts & intmsk ? "enabled" : "disabled",
3498 		  free_bds, ring->size,
3499 		  ring->prod_index, p_index & DMA_P_INDEX_MASK,
3500 		  ring->c_index, c_index & DMA_C_INDEX_MASK,
3501 		  ring->clean_ptr, ring->write_ptr,
3502 		  ring->cb_ptr, ring->end_ptr);
3503 }
3504 
bcmgenet_timeout(struct net_device * dev,unsigned int txqueue)3505 static void bcmgenet_timeout(struct net_device *dev, unsigned int txqueue)
3506 {
3507 	struct bcmgenet_priv *priv = netdev_priv(dev);
3508 	u32 int0_enable = 0;
3509 	u32 int1_enable = 0;
3510 	unsigned int q;
3511 
3512 	netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
3513 
3514 	for (q = 0; q < priv->hw_params->tx_queues; q++)
3515 		bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
3516 	bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
3517 
3518 	bcmgenet_tx_reclaim_all(dev);
3519 
3520 	for (q = 0; q < priv->hw_params->tx_queues; q++)
3521 		int1_enable |= (1 << q);
3522 
3523 	int0_enable = UMAC_IRQ_TXDMA_DONE;
3524 
3525 	/* Re-enable TX interrupts if disabled */
3526 	bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
3527 	bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
3528 
3529 	netif_trans_update(dev);
3530 
3531 	dev->stats.tx_errors++;
3532 
3533 	netif_tx_wake_all_queues(dev);
3534 }
3535 
3536 #define MAX_MDF_FILTER	17
3537 
bcmgenet_set_mdf_addr(struct bcmgenet_priv * priv,const unsigned char * addr,int * i)3538 static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
3539 					 const unsigned char *addr,
3540 					 int *i)
3541 {
3542 	bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
3543 			     UMAC_MDF_ADDR + (*i * 4));
3544 	bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
3545 			     addr[4] << 8 | addr[5],
3546 			     UMAC_MDF_ADDR + ((*i + 1) * 4));
3547 	*i += 2;
3548 }
3549 
bcmgenet_set_rx_mode(struct net_device * dev)3550 static void bcmgenet_set_rx_mode(struct net_device *dev)
3551 {
3552 	struct bcmgenet_priv *priv = netdev_priv(dev);
3553 	struct netdev_hw_addr *ha;
3554 	int i, nfilter;
3555 	u32 reg;
3556 
3557 	netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
3558 
3559 	/* Number of filters needed */
3560 	nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
3561 
3562 	/*
3563 	 * Turn on promicuous mode for three scenarios
3564 	 * 1. IFF_PROMISC flag is set
3565 	 * 2. IFF_ALLMULTI flag is set
3566 	 * 3. The number of filters needed exceeds the number filters
3567 	 *    supported by the hardware.
3568 	*/
3569 	reg = bcmgenet_umac_readl(priv, UMAC_CMD);
3570 	if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
3571 	    (nfilter > MAX_MDF_FILTER)) {
3572 		reg |= CMD_PROMISC;
3573 		bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3574 		bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
3575 		return;
3576 	} else {
3577 		reg &= ~CMD_PROMISC;
3578 		bcmgenet_umac_writel(priv, reg, UMAC_CMD);
3579 	}
3580 
3581 	/* update MDF filter */
3582 	i = 0;
3583 	/* Broadcast */
3584 	bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
3585 	/* my own address.*/
3586 	bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
3587 
3588 	/* Unicast */
3589 	netdev_for_each_uc_addr(ha, dev)
3590 		bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3591 
3592 	/* Multicast */
3593 	netdev_for_each_mc_addr(ha, dev)
3594 		bcmgenet_set_mdf_addr(priv, ha->addr, &i);
3595 
3596 	/* Enable filters */
3597 	reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
3598 	bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
3599 }
3600 
3601 /* Set the hardware MAC address. */
bcmgenet_set_mac_addr(struct net_device * dev,void * p)3602 static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
3603 {
3604 	struct sockaddr *addr = p;
3605 
3606 	/* Setting the MAC address at the hardware level is not possible
3607 	 * without disabling the UniMAC RX/TX enable bits.
3608 	 */
3609 	if (netif_running(dev))
3610 		return -EBUSY;
3611 
3612 	eth_hw_addr_set(dev, addr->sa_data);
3613 
3614 	return 0;
3615 }
3616 
bcmgenet_get_stats(struct net_device * dev)3617 static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
3618 {
3619 	struct bcmgenet_priv *priv = netdev_priv(dev);
3620 	unsigned long tx_bytes = 0, tx_packets = 0;
3621 	unsigned long rx_bytes = 0, rx_packets = 0;
3622 	unsigned long rx_errors = 0, rx_dropped = 0;
3623 	struct bcmgenet_tx_ring *tx_ring;
3624 	struct bcmgenet_rx_ring *rx_ring;
3625 	unsigned int q;
3626 
3627 	for (q = 0; q < priv->hw_params->tx_queues; q++) {
3628 		tx_ring = &priv->tx_rings[q];
3629 		tx_bytes += tx_ring->bytes;
3630 		tx_packets += tx_ring->packets;
3631 	}
3632 	tx_ring = &priv->tx_rings[DESC_INDEX];
3633 	tx_bytes += tx_ring->bytes;
3634 	tx_packets += tx_ring->packets;
3635 
3636 	for (q = 0; q < priv->hw_params->rx_queues; q++) {
3637 		rx_ring = &priv->rx_rings[q];
3638 
3639 		rx_bytes += rx_ring->bytes;
3640 		rx_packets += rx_ring->packets;
3641 		rx_errors += rx_ring->errors;
3642 		rx_dropped += rx_ring->dropped;
3643 	}
3644 	rx_ring = &priv->rx_rings[DESC_INDEX];
3645 	rx_bytes += rx_ring->bytes;
3646 	rx_packets += rx_ring->packets;
3647 	rx_errors += rx_ring->errors;
3648 	rx_dropped += rx_ring->dropped;
3649 
3650 	dev->stats.tx_bytes = tx_bytes;
3651 	dev->stats.tx_packets = tx_packets;
3652 	dev->stats.rx_bytes = rx_bytes;
3653 	dev->stats.rx_packets = rx_packets;
3654 	dev->stats.rx_errors = rx_errors;
3655 	dev->stats.rx_missed_errors = rx_errors;
3656 	dev->stats.rx_dropped = rx_dropped;
3657 	return &dev->stats;
3658 }
3659 
bcmgenet_change_carrier(struct net_device * dev,bool new_carrier)3660 static int bcmgenet_change_carrier(struct net_device *dev, bool new_carrier)
3661 {
3662 	struct bcmgenet_priv *priv = netdev_priv(dev);
3663 
3664 	if (!dev->phydev || !phy_is_pseudo_fixed_link(dev->phydev) ||
3665 	    priv->phy_interface != PHY_INTERFACE_MODE_MOCA)
3666 		return -EOPNOTSUPP;
3667 
3668 	if (new_carrier)
3669 		netif_carrier_on(dev);
3670 	else
3671 		netif_carrier_off(dev);
3672 
3673 	return 0;
3674 }
3675 
3676 static const struct net_device_ops bcmgenet_netdev_ops = {
3677 	.ndo_open		= bcmgenet_open,
3678 	.ndo_stop		= bcmgenet_close,
3679 	.ndo_start_xmit		= bcmgenet_xmit,
3680 	.ndo_tx_timeout		= bcmgenet_timeout,
3681 	.ndo_set_rx_mode	= bcmgenet_set_rx_mode,
3682 	.ndo_set_mac_address	= bcmgenet_set_mac_addr,
3683 	.ndo_eth_ioctl		= phy_do_ioctl_running,
3684 	.ndo_set_features	= bcmgenet_set_features,
3685 #ifdef CONFIG_NET_POLL_CONTROLLER
3686 	.ndo_poll_controller	= bcmgenet_poll_controller,
3687 #endif
3688 	.ndo_get_stats		= bcmgenet_get_stats,
3689 	.ndo_change_carrier	= bcmgenet_change_carrier,
3690 };
3691 
3692 /* Array of GENET hardware parameters/characteristics */
3693 static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
3694 	[GENET_V1] = {
3695 		.tx_queues = 0,
3696 		.tx_bds_per_q = 0,
3697 		.rx_queues = 0,
3698 		.rx_bds_per_q = 0,
3699 		.bp_in_en_shift = 16,
3700 		.bp_in_mask = 0xffff,
3701 		.hfb_filter_cnt = 16,
3702 		.qtag_mask = 0x1F,
3703 		.hfb_offset = 0x1000,
3704 		.rdma_offset = 0x2000,
3705 		.tdma_offset = 0x3000,
3706 		.words_per_bd = 2,
3707 	},
3708 	[GENET_V2] = {
3709 		.tx_queues = 4,
3710 		.tx_bds_per_q = 32,
3711 		.rx_queues = 0,
3712 		.rx_bds_per_q = 0,
3713 		.bp_in_en_shift = 16,
3714 		.bp_in_mask = 0xffff,
3715 		.hfb_filter_cnt = 16,
3716 		.qtag_mask = 0x1F,
3717 		.tbuf_offset = 0x0600,
3718 		.hfb_offset = 0x1000,
3719 		.hfb_reg_offset = 0x2000,
3720 		.rdma_offset = 0x3000,
3721 		.tdma_offset = 0x4000,
3722 		.words_per_bd = 2,
3723 		.flags = GENET_HAS_EXT,
3724 	},
3725 	[GENET_V3] = {
3726 		.tx_queues = 4,
3727 		.tx_bds_per_q = 32,
3728 		.rx_queues = 0,
3729 		.rx_bds_per_q = 0,
3730 		.bp_in_en_shift = 17,
3731 		.bp_in_mask = 0x1ffff,
3732 		.hfb_filter_cnt = 48,
3733 		.hfb_filter_size = 128,
3734 		.qtag_mask = 0x3F,
3735 		.tbuf_offset = 0x0600,
3736 		.hfb_offset = 0x8000,
3737 		.hfb_reg_offset = 0xfc00,
3738 		.rdma_offset = 0x10000,
3739 		.tdma_offset = 0x11000,
3740 		.words_per_bd = 2,
3741 		.flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
3742 			 GENET_HAS_MOCA_LINK_DET,
3743 	},
3744 	[GENET_V4] = {
3745 		.tx_queues = 4,
3746 		.tx_bds_per_q = 32,
3747 		.rx_queues = 0,
3748 		.rx_bds_per_q = 0,
3749 		.bp_in_en_shift = 17,
3750 		.bp_in_mask = 0x1ffff,
3751 		.hfb_filter_cnt = 48,
3752 		.hfb_filter_size = 128,
3753 		.qtag_mask = 0x3F,
3754 		.tbuf_offset = 0x0600,
3755 		.hfb_offset = 0x8000,
3756 		.hfb_reg_offset = 0xfc00,
3757 		.rdma_offset = 0x2000,
3758 		.tdma_offset = 0x4000,
3759 		.words_per_bd = 3,
3760 		.flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3761 			 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3762 	},
3763 	[GENET_V5] = {
3764 		.tx_queues = 4,
3765 		.tx_bds_per_q = 32,
3766 		.rx_queues = 0,
3767 		.rx_bds_per_q = 0,
3768 		.bp_in_en_shift = 17,
3769 		.bp_in_mask = 0x1ffff,
3770 		.hfb_filter_cnt = 48,
3771 		.hfb_filter_size = 128,
3772 		.qtag_mask = 0x3F,
3773 		.tbuf_offset = 0x0600,
3774 		.hfb_offset = 0x8000,
3775 		.hfb_reg_offset = 0xfc00,
3776 		.rdma_offset = 0x2000,
3777 		.tdma_offset = 0x4000,
3778 		.words_per_bd = 3,
3779 		.flags = GENET_HAS_40BITS | GENET_HAS_EXT |
3780 			 GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
3781 	},
3782 };
3783 
3784 /* Infer hardware parameters from the detected GENET version */
bcmgenet_set_hw_params(struct bcmgenet_priv * priv)3785 static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
3786 {
3787 	struct bcmgenet_hw_params *params;
3788 	u32 reg;
3789 	u8 major;
3790 	u16 gphy_rev;
3791 
3792 	if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
3793 		bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3794 		genet_dma_ring_regs = genet_dma_ring_regs_v4;
3795 	} else if (GENET_IS_V3(priv)) {
3796 		bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
3797 		genet_dma_ring_regs = genet_dma_ring_regs_v123;
3798 	} else if (GENET_IS_V2(priv)) {
3799 		bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
3800 		genet_dma_ring_regs = genet_dma_ring_regs_v123;
3801 	} else if (GENET_IS_V1(priv)) {
3802 		bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
3803 		genet_dma_ring_regs = genet_dma_ring_regs_v123;
3804 	}
3805 
3806 	/* enum genet_version starts at 1 */
3807 	priv->hw_params = &bcmgenet_hw_params[priv->version];
3808 	params = priv->hw_params;
3809 
3810 	/* Read GENET HW version */
3811 	reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
3812 	major = (reg >> 24 & 0x0f);
3813 	if (major == 6)
3814 		major = 5;
3815 	else if (major == 5)
3816 		major = 4;
3817 	else if (major == 0)
3818 		major = 1;
3819 	if (major != priv->version) {
3820 		dev_err(&priv->pdev->dev,
3821 			"GENET version mismatch, got: %d, configured for: %d\n",
3822 			major, priv->version);
3823 	}
3824 
3825 	/* Print the GENET core version */
3826 	dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
3827 		 major, (reg >> 16) & 0x0f, reg & 0xffff);
3828 
3829 	/* Store the integrated PHY revision for the MDIO probing function
3830 	 * to pass this information to the PHY driver. The PHY driver expects
3831 	 * to find the PHY major revision in bits 15:8 while the GENET register
3832 	 * stores that information in bits 7:0, account for that.
3833 	 *
3834 	 * On newer chips, starting with PHY revision G0, a new scheme is
3835 	 * deployed similar to the Starfighter 2 switch with GPHY major
3836 	 * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
3837 	 * is reserved as well as special value 0x01ff, we have a small
3838 	 * heuristic to check for the new GPHY revision and re-arrange things
3839 	 * so the GPHY driver is happy.
3840 	 */
3841 	gphy_rev = reg & 0xffff;
3842 
3843 	if (GENET_IS_V5(priv)) {
3844 		/* The EPHY revision should come from the MDIO registers of
3845 		 * the PHY not from GENET.
3846 		 */
3847 		if (gphy_rev != 0) {
3848 			pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
3849 				gphy_rev);
3850 		}
3851 	/* This is reserved so should require special treatment */
3852 	} else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
3853 		pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
3854 		return;
3855 	/* This is the good old scheme, just GPHY major, no minor nor patch */
3856 	} else if ((gphy_rev & 0xf0) != 0) {
3857 		priv->gphy_rev = gphy_rev << 8;
3858 	/* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
3859 	} else if ((gphy_rev & 0xff00) != 0) {
3860 		priv->gphy_rev = gphy_rev;
3861 	}
3862 
3863 #ifdef CONFIG_PHYS_ADDR_T_64BIT
3864 	if (!(params->flags & GENET_HAS_40BITS))
3865 		pr_warn("GENET does not support 40-bits PA\n");
3866 #endif
3867 
3868 	pr_debug("Configuration for version: %d\n"
3869 		"TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
3870 		"BP << en: %2d, BP msk: 0x%05x\n"
3871 		"HFB count: %2d, QTAQ msk: 0x%05x\n"
3872 		"TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
3873 		"RDMA: 0x%05x, TDMA: 0x%05x\n"
3874 		"Words/BD: %d\n",
3875 		priv->version,
3876 		params->tx_queues, params->tx_bds_per_q,
3877 		params->rx_queues, params->rx_bds_per_q,
3878 		params->bp_in_en_shift, params->bp_in_mask,
3879 		params->hfb_filter_cnt, params->qtag_mask,
3880 		params->tbuf_offset, params->hfb_offset,
3881 		params->hfb_reg_offset,
3882 		params->rdma_offset, params->tdma_offset,
3883 		params->words_per_bd);
3884 }
3885 
3886 struct bcmgenet_plat_data {
3887 	enum bcmgenet_version version;
3888 	u32 dma_max_burst_length;
3889 };
3890 
3891 static const struct bcmgenet_plat_data v1_plat_data = {
3892 	.version = GENET_V1,
3893 	.dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3894 };
3895 
3896 static const struct bcmgenet_plat_data v2_plat_data = {
3897 	.version = GENET_V2,
3898 	.dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3899 };
3900 
3901 static const struct bcmgenet_plat_data v3_plat_data = {
3902 	.version = GENET_V3,
3903 	.dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3904 };
3905 
3906 static const struct bcmgenet_plat_data v4_plat_data = {
3907 	.version = GENET_V4,
3908 	.dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3909 };
3910 
3911 static const struct bcmgenet_plat_data v5_plat_data = {
3912 	.version = GENET_V5,
3913 	.dma_max_burst_length = DMA_MAX_BURST_LENGTH,
3914 };
3915 
3916 static const struct bcmgenet_plat_data bcm2711_plat_data = {
3917 	.version = GENET_V5,
3918 	.dma_max_burst_length = 0x08,
3919 };
3920 
3921 static const struct of_device_id bcmgenet_match[] = {
3922 	{ .compatible = "brcm,genet-v1", .data = &v1_plat_data },
3923 	{ .compatible = "brcm,genet-v2", .data = &v2_plat_data },
3924 	{ .compatible = "brcm,genet-v3", .data = &v3_plat_data },
3925 	{ .compatible = "brcm,genet-v4", .data = &v4_plat_data },
3926 	{ .compatible = "brcm,genet-v5", .data = &v5_plat_data },
3927 	{ .compatible = "brcm,bcm2711-genet-v5", .data = &bcm2711_plat_data },
3928 	{ },
3929 };
3930 MODULE_DEVICE_TABLE(of, bcmgenet_match);
3931 
bcmgenet_probe(struct platform_device * pdev)3932 static int bcmgenet_probe(struct platform_device *pdev)
3933 {
3934 	struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
3935 	const struct bcmgenet_plat_data *pdata;
3936 	struct bcmgenet_priv *priv;
3937 	struct net_device *dev;
3938 	unsigned int i;
3939 	int err = -EIO;
3940 
3941 	/* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
3942 	dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
3943 				 GENET_MAX_MQ_CNT + 1);
3944 	if (!dev) {
3945 		dev_err(&pdev->dev, "can't allocate net device\n");
3946 		return -ENOMEM;
3947 	}
3948 
3949 	priv = netdev_priv(dev);
3950 	priv->irq0 = platform_get_irq(pdev, 0);
3951 	if (priv->irq0 < 0) {
3952 		err = priv->irq0;
3953 		goto err;
3954 	}
3955 	priv->irq1 = platform_get_irq(pdev, 1);
3956 	if (priv->irq1 < 0) {
3957 		err = priv->irq1;
3958 		goto err;
3959 	}
3960 	priv->wol_irq = platform_get_irq_optional(pdev, 2);
3961 	if (priv->wol_irq == -EPROBE_DEFER) {
3962 		err = priv->wol_irq;
3963 		goto err;
3964 	}
3965 
3966 	priv->base = devm_platform_ioremap_resource(pdev, 0);
3967 	if (IS_ERR(priv->base)) {
3968 		err = PTR_ERR(priv->base);
3969 		goto err;
3970 	}
3971 
3972 	spin_lock_init(&priv->lock);
3973 
3974 	SET_NETDEV_DEV(dev, &pdev->dev);
3975 	dev_set_drvdata(&pdev->dev, dev);
3976 	dev->watchdog_timeo = 2 * HZ;
3977 	dev->ethtool_ops = &bcmgenet_ethtool_ops;
3978 	dev->netdev_ops = &bcmgenet_netdev_ops;
3979 
3980 	priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
3981 
3982 	/* Set default features */
3983 	dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_HW_CSUM |
3984 			 NETIF_F_RXCSUM;
3985 	dev->hw_features |= dev->features;
3986 	dev->vlan_features |= dev->features;
3987 
3988 	/* Request the WOL interrupt and advertise suspend if available */
3989 	priv->wol_irq_disabled = true;
3990 	if (priv->wol_irq > 0) {
3991 		err = devm_request_irq(&pdev->dev, priv->wol_irq,
3992 				       bcmgenet_wol_isr, 0, dev->name, priv);
3993 		if (!err)
3994 			device_set_wakeup_capable(&pdev->dev, 1);
3995 	}
3996 
3997 	/* Set the needed headroom to account for any possible
3998 	 * features enabling/disabling at runtime
3999 	 */
4000 	dev->needed_headroom += 64;
4001 
4002 	priv->dev = dev;
4003 	priv->pdev = pdev;
4004 
4005 	pdata = device_get_match_data(&pdev->dev);
4006 	if (pdata) {
4007 		priv->version = pdata->version;
4008 		priv->dma_max_burst_length = pdata->dma_max_burst_length;
4009 	} else {
4010 		priv->version = pd->genet_version;
4011 		priv->dma_max_burst_length = DMA_MAX_BURST_LENGTH;
4012 	}
4013 
4014 	priv->clk = devm_clk_get_optional(&priv->pdev->dev, "enet");
4015 	if (IS_ERR(priv->clk)) {
4016 		dev_dbg(&priv->pdev->dev, "failed to get enet clock\n");
4017 		err = PTR_ERR(priv->clk);
4018 		goto err;
4019 	}
4020 
4021 	err = clk_prepare_enable(priv->clk);
4022 	if (err)
4023 		goto err;
4024 
4025 	bcmgenet_set_hw_params(priv);
4026 
4027 	err = -EIO;
4028 	if (priv->hw_params->flags & GENET_HAS_40BITS)
4029 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
4030 	if (err)
4031 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
4032 	if (err)
4033 		goto err_clk_disable;
4034 
4035 	/* Mii wait queue */
4036 	init_waitqueue_head(&priv->wq);
4037 	/* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
4038 	priv->rx_buf_len = RX_BUF_LENGTH;
4039 	INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
4040 
4041 	priv->clk_wol = devm_clk_get_optional(&priv->pdev->dev, "enet-wol");
4042 	if (IS_ERR(priv->clk_wol)) {
4043 		dev_dbg(&priv->pdev->dev, "failed to get enet-wol clock\n");
4044 		err = PTR_ERR(priv->clk_wol);
4045 		goto err_clk_disable;
4046 	}
4047 
4048 	priv->clk_eee = devm_clk_get_optional(&priv->pdev->dev, "enet-eee");
4049 	if (IS_ERR(priv->clk_eee)) {
4050 		dev_dbg(&priv->pdev->dev, "failed to get enet-eee clock\n");
4051 		err = PTR_ERR(priv->clk_eee);
4052 		goto err_clk_disable;
4053 	}
4054 
4055 	/* If this is an internal GPHY, power it on now, before UniMAC is
4056 	 * brought out of reset as absolutely no UniMAC activity is allowed
4057 	 */
4058 	if (device_get_phy_mode(&pdev->dev) == PHY_INTERFACE_MODE_INTERNAL)
4059 		bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4060 
4061 	if (pd && !IS_ERR_OR_NULL(pd->mac_address))
4062 		eth_hw_addr_set(dev, pd->mac_address);
4063 	else
4064 		if (!device_get_mac_address(&pdev->dev, dev->dev_addr, ETH_ALEN))
4065 			if (has_acpi_companion(&pdev->dev))
4066 				bcmgenet_get_hw_addr(priv, dev->dev_addr);
4067 
4068 	if (!is_valid_ether_addr(dev->dev_addr)) {
4069 		dev_warn(&pdev->dev, "using random Ethernet MAC\n");
4070 		eth_hw_addr_random(dev);
4071 	}
4072 
4073 	reset_umac(priv);
4074 
4075 	err = bcmgenet_mii_init(dev);
4076 	if (err)
4077 		goto err_clk_disable;
4078 
4079 	/* setup number of real queues  + 1 (GENET_V1 has 0 hardware queues
4080 	 * just the ring 16 descriptor based TX
4081 	 */
4082 	netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
4083 	netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
4084 
4085 	/* Set default coalescing parameters */
4086 	for (i = 0; i < priv->hw_params->rx_queues; i++)
4087 		priv->rx_rings[i].rx_max_coalesced_frames = 1;
4088 	priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
4089 
4090 	/* libphy will determine the link state */
4091 	netif_carrier_off(dev);
4092 
4093 	/* Turn off the main clock, WOL clock is handled separately */
4094 	clk_disable_unprepare(priv->clk);
4095 
4096 	err = register_netdev(dev);
4097 	if (err) {
4098 		bcmgenet_mii_exit(dev);
4099 		goto err;
4100 	}
4101 
4102 	return err;
4103 
4104 err_clk_disable:
4105 	clk_disable_unprepare(priv->clk);
4106 err:
4107 	free_netdev(dev);
4108 	return err;
4109 }
4110 
bcmgenet_remove(struct platform_device * pdev)4111 static int bcmgenet_remove(struct platform_device *pdev)
4112 {
4113 	struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
4114 
4115 	dev_set_drvdata(&pdev->dev, NULL);
4116 	unregister_netdev(priv->dev);
4117 	bcmgenet_mii_exit(priv->dev);
4118 	free_netdev(priv->dev);
4119 
4120 	return 0;
4121 }
4122 
bcmgenet_shutdown(struct platform_device * pdev)4123 static void bcmgenet_shutdown(struct platform_device *pdev)
4124 {
4125 	bcmgenet_remove(pdev);
4126 }
4127 
4128 #ifdef CONFIG_PM_SLEEP
bcmgenet_resume_noirq(struct device * d)4129 static int bcmgenet_resume_noirq(struct device *d)
4130 {
4131 	struct net_device *dev = dev_get_drvdata(d);
4132 	struct bcmgenet_priv *priv = netdev_priv(dev);
4133 	int ret;
4134 	u32 reg;
4135 
4136 	if (!netif_running(dev))
4137 		return 0;
4138 
4139 	/* Turn on the clock */
4140 	ret = clk_prepare_enable(priv->clk);
4141 	if (ret)
4142 		return ret;
4143 
4144 	if (device_may_wakeup(d) && priv->wolopts) {
4145 		/* Account for Wake-on-LAN events and clear those events
4146 		 * (Some devices need more time between enabling the clocks
4147 		 *  and the interrupt register reflecting the wake event so
4148 		 *  read the register twice)
4149 		 */
4150 		reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4151 		reg = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT);
4152 		if (reg & UMAC_IRQ_WAKE_EVENT)
4153 			pm_wakeup_event(&priv->pdev->dev, 0);
4154 	}
4155 
4156 	bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_WAKE_EVENT, INTRL2_CPU_CLEAR);
4157 
4158 	return 0;
4159 }
4160 
bcmgenet_resume(struct device * d)4161 static int bcmgenet_resume(struct device *d)
4162 {
4163 	struct net_device *dev = dev_get_drvdata(d);
4164 	struct bcmgenet_priv *priv = netdev_priv(dev);
4165 	struct bcmgenet_rxnfc_rule *rule;
4166 	unsigned long dma_ctrl;
4167 	int ret;
4168 
4169 	if (!netif_running(dev))
4170 		return 0;
4171 
4172 	/* From WOL-enabled suspend, switch to regular clock */
4173 	if (device_may_wakeup(d) && priv->wolopts)
4174 		bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
4175 
4176 	/* If this is an internal GPHY, power it back on now, before UniMAC is
4177 	 * brought out of reset as absolutely no UniMAC activity is allowed
4178 	 */
4179 	if (priv->internal_phy)
4180 		bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
4181 
4182 	bcmgenet_umac_reset(priv);
4183 
4184 	init_umac(priv);
4185 
4186 	phy_init_hw(dev->phydev);
4187 
4188 	/* Speed settings must be restored */
4189 	genphy_config_aneg(dev->phydev);
4190 	bcmgenet_mii_config(priv->dev, false);
4191 
4192 	/* Restore enabled features */
4193 	bcmgenet_set_features(dev, dev->features);
4194 
4195 	bcmgenet_set_hw_addr(priv, dev->dev_addr);
4196 
4197 	/* Restore hardware filters */
4198 	bcmgenet_hfb_clear(priv);
4199 	list_for_each_entry(rule, &priv->rxnfc_list, list)
4200 		if (rule->state != BCMGENET_RXNFC_STATE_UNUSED)
4201 			bcmgenet_hfb_create_rxnfc_filter(priv, rule);
4202 
4203 	/* Disable RX/TX DMA and flush TX queues */
4204 	dma_ctrl = bcmgenet_dma_disable(priv);
4205 
4206 	/* Reinitialize TDMA and RDMA and SW housekeeping */
4207 	ret = bcmgenet_init_dma(priv);
4208 	if (ret) {
4209 		netdev_err(dev, "failed to initialize DMA\n");
4210 		goto out_clk_disable;
4211 	}
4212 
4213 	/* Always enable ring 16 - descriptor ring */
4214 	bcmgenet_enable_dma(priv, dma_ctrl);
4215 
4216 	if (!device_may_wakeup(d))
4217 		phy_resume(dev->phydev);
4218 
4219 	bcmgenet_netif_start(dev);
4220 
4221 	netif_device_attach(dev);
4222 
4223 	return 0;
4224 
4225 out_clk_disable:
4226 	if (priv->internal_phy)
4227 		bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
4228 	clk_disable_unprepare(priv->clk);
4229 	return ret;
4230 }
4231 
bcmgenet_suspend(struct device * d)4232 static int bcmgenet_suspend(struct device *d)
4233 {
4234 	struct net_device *dev = dev_get_drvdata(d);
4235 	struct bcmgenet_priv *priv = netdev_priv(dev);
4236 
4237 	if (!netif_running(dev))
4238 		return 0;
4239 
4240 	netif_device_detach(dev);
4241 
4242 	bcmgenet_netif_stop(dev, true);
4243 
4244 	if (!device_may_wakeup(d))
4245 		phy_suspend(dev->phydev);
4246 
4247 	/* Disable filtering */
4248 	bcmgenet_hfb_reg_writel(priv, 0, HFB_CTRL);
4249 
4250 	return 0;
4251 }
4252 
bcmgenet_suspend_noirq(struct device * d)4253 static int bcmgenet_suspend_noirq(struct device *d)
4254 {
4255 	struct net_device *dev = dev_get_drvdata(d);
4256 	struct bcmgenet_priv *priv = netdev_priv(dev);
4257 	int ret = 0;
4258 
4259 	if (!netif_running(dev))
4260 		return 0;
4261 
4262 	/* Prepare the device for Wake-on-LAN and switch to the slow clock */
4263 	if (device_may_wakeup(d) && priv->wolopts)
4264 		ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
4265 	else if (priv->internal_phy)
4266 		ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
4267 
4268 	/* Let the framework handle resumption and leave the clocks on */
4269 	if (ret)
4270 		return ret;
4271 
4272 	/* Turn off the clocks */
4273 	clk_disable_unprepare(priv->clk);
4274 
4275 	return 0;
4276 }
4277 #else
4278 #define bcmgenet_suspend	NULL
4279 #define bcmgenet_suspend_noirq	NULL
4280 #define bcmgenet_resume		NULL
4281 #define bcmgenet_resume_noirq	NULL
4282 #endif /* CONFIG_PM_SLEEP */
4283 
4284 static const struct dev_pm_ops bcmgenet_pm_ops = {
4285 	.suspend	= bcmgenet_suspend,
4286 	.suspend_noirq	= bcmgenet_suspend_noirq,
4287 	.resume		= bcmgenet_resume,
4288 	.resume_noirq	= bcmgenet_resume_noirq,
4289 };
4290 
4291 static const struct acpi_device_id genet_acpi_match[] = {
4292 	{ "BCM6E4E", (kernel_ulong_t)&bcm2711_plat_data },
4293 	{ },
4294 };
4295 MODULE_DEVICE_TABLE(acpi, genet_acpi_match);
4296 
4297 static struct platform_driver bcmgenet_driver = {
4298 	.probe	= bcmgenet_probe,
4299 	.remove	= bcmgenet_remove,
4300 	.shutdown = bcmgenet_shutdown,
4301 	.driver	= {
4302 		.name	= "bcmgenet",
4303 		.of_match_table = bcmgenet_match,
4304 		.pm	= &bcmgenet_pm_ops,
4305 		.acpi_match_table = genet_acpi_match,
4306 	},
4307 };
4308 module_platform_driver(bcmgenet_driver);
4309 
4310 MODULE_AUTHOR("Broadcom Corporation");
4311 MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
4312 MODULE_ALIAS("platform:bcmgenet");
4313 MODULE_LICENSE("GPL");
4314 MODULE_SOFTDEP("pre: mdio-bcm-unimac");
4315