xref: /linux/drivers/net/ethernet/broadcom/bcmsysport.c (revision d5a821f66047c2effd06147b84775f9b2ce0f4aa)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom BCM7xxx System Port Ethernet MAC driver
4  *
5  * Copyright (C) 2014 Broadcom Corporation
6  */
7 
8 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
9 
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/dsa/brcm.h>
16 #include <linux/etherdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/of.h>
19 #include <linux/of_net.h>
20 #include <linux/of_mdio.h>
21 #include <linux/phy.h>
22 #include <linux/phy_fixed.h>
23 #include <net/dsa.h>
24 #include <linux/clk.h>
25 #include <net/ip.h>
26 #include <net/ipv6.h>
27 
28 #include "bcmsysport.h"
29 
30 /* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
31  * same layout, except it has been moved by 4 bytes up, *sigh*
32  */
rdma_readl(struct bcm_sysport_priv * priv,u32 off)33 static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
34 {
35 	if (priv->is_lite && off >= RDMA_STATUS)
36 		off += 4;
37 	return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
38 }
39 
rdma_writel(struct bcm_sysport_priv * priv,u32 val,u32 off)40 static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
41 {
42 	if (priv->is_lite && off >= RDMA_STATUS)
43 		off += 4;
44 	writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
45 }
46 
tdma_control_bit(struct bcm_sysport_priv * priv,u32 bit)47 static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
48 {
49 	if (!priv->is_lite) {
50 		return BIT(bit);
51 	} else {
52 		if (bit >= ACB_ALGO)
53 			return BIT(bit + 1);
54 		else
55 			return BIT(bit);
56 	}
57 }
58 
59 /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
60  * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
61   */
62 #define BCM_SYSPORT_INTR_L2(which)	\
63 static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
64 						u32 mask)		\
65 {									\
66 	priv->irq##which##_mask &= ~(mask);				\
67 	intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR);	\
68 }									\
69 static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
70 						u32 mask)		\
71 {									\
72 	intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET);	\
73 	priv->irq##which##_mask |= (mask);				\
74 }									\
75 
76 BCM_SYSPORT_INTR_L2(0)
77 BCM_SYSPORT_INTR_L2(1)
78 
79 /* Register accesses to GISB/RBUS registers are expensive (few hundred
80  * nanoseconds), so keep the check for 64-bits explicit here to save
81  * one register write per-packet on 32-bits platforms.
82  */
dma_desc_set_addr(struct bcm_sysport_priv * priv,void __iomem * d,dma_addr_t addr)83 static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
84 				     void __iomem *d,
85 				     dma_addr_t addr)
86 {
87 #ifdef CONFIG_PHYS_ADDR_T_64BIT
88 	writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
89 		     d + DESC_ADDR_HI_STATUS_LEN);
90 #endif
91 	writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
92 }
93 
94 /* Ethtool operations */
bcm_sysport_set_rx_csum(struct net_device * dev,netdev_features_t wanted)95 static void bcm_sysport_set_rx_csum(struct net_device *dev,
96 				    netdev_features_t wanted)
97 {
98 	struct bcm_sysport_priv *priv = netdev_priv(dev);
99 	u32 reg;
100 
101 	priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
102 	reg = rxchk_readl(priv, RXCHK_CONTROL);
103 	/* Clear L2 header checks, which would prevent BPDUs
104 	 * from being received.
105 	 */
106 	reg &= ~RXCHK_L2_HDR_DIS;
107 	if (priv->rx_chk_en)
108 		reg |= RXCHK_EN;
109 	else
110 		reg &= ~RXCHK_EN;
111 
112 	/* If UniMAC forwards CRC, we need to skip over it to get
113 	 * a valid CHK bit to be set in the per-packet status word
114 	 */
115 	if (priv->rx_chk_en && priv->crc_fwd)
116 		reg |= RXCHK_SKIP_FCS;
117 	else
118 		reg &= ~RXCHK_SKIP_FCS;
119 
120 	/* If Broadcom tags are enabled (e.g: using a switch), make
121 	 * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
122 	 * tag after the Ethernet MAC Source Address.
123 	 */
124 	if (netdev_uses_dsa(dev))
125 		reg |= RXCHK_BRCM_TAG_EN;
126 	else
127 		reg &= ~RXCHK_BRCM_TAG_EN;
128 
129 	rxchk_writel(priv, reg, RXCHK_CONTROL);
130 }
131 
bcm_sysport_set_tx_csum(struct net_device * dev,netdev_features_t wanted)132 static void bcm_sysport_set_tx_csum(struct net_device *dev,
133 				    netdev_features_t wanted)
134 {
135 	struct bcm_sysport_priv *priv = netdev_priv(dev);
136 	u32 reg;
137 
138 	/* Hardware transmit checksum requires us to enable the Transmit status
139 	 * block prepended to the packet contents
140 	 */
141 	priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
142 				    NETIF_F_HW_VLAN_CTAG_TX));
143 	reg = tdma_readl(priv, TDMA_CONTROL);
144 	if (priv->tsb_en)
145 		reg |= tdma_control_bit(priv, TSB_EN);
146 	else
147 		reg &= ~tdma_control_bit(priv, TSB_EN);
148 	/* Indicating that software inserts Broadcom tags is needed for the TX
149 	 * checksum to be computed correctly when using VLAN HW acceleration,
150 	 * else it has no effect, so it can always be turned on.
151 	 */
152 	if (netdev_uses_dsa(dev))
153 		reg |= tdma_control_bit(priv, SW_BRCM_TAG);
154 	else
155 		reg &= ~tdma_control_bit(priv, SW_BRCM_TAG);
156 	tdma_writel(priv, reg, TDMA_CONTROL);
157 
158 	/* Default TPID is ETH_P_8021AD, change to ETH_P_8021Q */
159 	if (wanted & NETIF_F_HW_VLAN_CTAG_TX)
160 		tdma_writel(priv, ETH_P_8021Q, TDMA_TPID);
161 }
162 
bcm_sysport_set_features(struct net_device * dev,netdev_features_t features)163 static int bcm_sysport_set_features(struct net_device *dev,
164 				    netdev_features_t features)
165 {
166 	struct bcm_sysport_priv *priv = netdev_priv(dev);
167 	int ret;
168 
169 	ret = clk_prepare_enable(priv->clk);
170 	if (ret)
171 		return ret;
172 
173 	/* Read CRC forward */
174 	if (!priv->is_lite)
175 		priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
176 	else
177 		priv->crc_fwd = !((gib_readl(priv, GIB_CONTROL) &
178 				  GIB_FCS_STRIP) >> GIB_FCS_STRIP_SHIFT);
179 
180 	bcm_sysport_set_rx_csum(dev, features);
181 	bcm_sysport_set_tx_csum(dev, features);
182 
183 	clk_disable_unprepare(priv->clk);
184 
185 	return 0;
186 }
187 
188 /* Hardware counters must be kept in sync because the order/offset
189  * is important here (order in structure declaration = order in hardware)
190  */
191 static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
192 	/* general stats */
193 	STAT_NETDEV64(rx_packets),
194 	STAT_NETDEV64(tx_packets),
195 	STAT_NETDEV64(rx_bytes),
196 	STAT_NETDEV64(tx_bytes),
197 	STAT_NETDEV(rx_errors),
198 	STAT_NETDEV(tx_errors),
199 	STAT_NETDEV(rx_dropped),
200 	STAT_NETDEV(tx_dropped),
201 	STAT_NETDEV(multicast),
202 	/* UniMAC RSV counters */
203 	STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
204 	STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
205 	STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
206 	STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
207 	STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
208 	STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
209 	STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
210 	STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
211 	STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
212 	STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
213 	STAT_MIB_RX("rx_pkts", mib.rx.pkt),
214 	STAT_MIB_RX("rx_bytes", mib.rx.bytes),
215 	STAT_MIB_RX("rx_multicast", mib.rx.mca),
216 	STAT_MIB_RX("rx_broadcast", mib.rx.bca),
217 	STAT_MIB_RX("rx_fcs", mib.rx.fcs),
218 	STAT_MIB_RX("rx_control", mib.rx.cf),
219 	STAT_MIB_RX("rx_pause", mib.rx.pf),
220 	STAT_MIB_RX("rx_unknown", mib.rx.uo),
221 	STAT_MIB_RX("rx_align", mib.rx.aln),
222 	STAT_MIB_RX("rx_outrange", mib.rx.flr),
223 	STAT_MIB_RX("rx_code", mib.rx.cde),
224 	STAT_MIB_RX("rx_carrier", mib.rx.fcr),
225 	STAT_MIB_RX("rx_oversize", mib.rx.ovr),
226 	STAT_MIB_RX("rx_jabber", mib.rx.jbr),
227 	STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
228 	STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
229 	STAT_MIB_RX("rx_unicast", mib.rx.uc),
230 	STAT_MIB_RX("rx_ppp", mib.rx.ppp),
231 	STAT_MIB_RX("rx_crc", mib.rx.rcrc),
232 	/* UniMAC TSV counters */
233 	STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
234 	STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
235 	STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
236 	STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
237 	STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
238 	STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
239 	STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
240 	STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
241 	STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
242 	STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
243 	STAT_MIB_TX("tx_pkts", mib.tx.pkts),
244 	STAT_MIB_TX("tx_multicast", mib.tx.mca),
245 	STAT_MIB_TX("tx_broadcast", mib.tx.bca),
246 	STAT_MIB_TX("tx_pause", mib.tx.pf),
247 	STAT_MIB_TX("tx_control", mib.tx.cf),
248 	STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
249 	STAT_MIB_TX("tx_oversize", mib.tx.ovr),
250 	STAT_MIB_TX("tx_defer", mib.tx.drf),
251 	STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
252 	STAT_MIB_TX("tx_single_col", mib.tx.scl),
253 	STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
254 	STAT_MIB_TX("tx_late_col", mib.tx.lcl),
255 	STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
256 	STAT_MIB_TX("tx_frags", mib.tx.frg),
257 	STAT_MIB_TX("tx_total_col", mib.tx.ncl),
258 	STAT_MIB_TX("tx_jabber", mib.tx.jbr),
259 	STAT_MIB_TX("tx_bytes", mib.tx.bytes),
260 	STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
261 	STAT_MIB_TX("tx_unicast", mib.tx.uc),
262 	/* UniMAC RUNT counters */
263 	STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
264 	STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
265 	STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
266 	STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
267 	/* RXCHK misc statistics */
268 	STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
269 	STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
270 		   RXCHK_OTHER_DISC_CNTR),
271 	/* RBUF misc statistics */
272 	STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
273 	STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
274 	/* RDMA misc statistics */
275 	STAT_RDMA("rdma_ovflow_cnt", mib.rdma_ovflow_cnt, RDMA_OVFL_DISC_CNTR),
276 	STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
277 	STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
278 	STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
279 	STAT_MIB_SOFT("tx_realloc_tsb", mib.tx_realloc_tsb),
280 	STAT_MIB_SOFT("tx_realloc_tsb_failed", mib.tx_realloc_tsb_failed),
281 	/* Per TX-queue statistics are dynamically appended */
282 };
283 
284 #define BCM_SYSPORT_STATS_LEN	ARRAY_SIZE(bcm_sysport_gstrings_stats)
285 
bcm_sysport_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)286 static void bcm_sysport_get_drvinfo(struct net_device *dev,
287 				    struct ethtool_drvinfo *info)
288 {
289 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
290 	strscpy(info->bus_info, "platform", sizeof(info->bus_info));
291 }
292 
bcm_sysport_get_msglvl(struct net_device * dev)293 static u32 bcm_sysport_get_msglvl(struct net_device *dev)
294 {
295 	struct bcm_sysport_priv *priv = netdev_priv(dev);
296 
297 	return priv->msg_enable;
298 }
299 
bcm_sysport_set_msglvl(struct net_device * dev,u32 enable)300 static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
301 {
302 	struct bcm_sysport_priv *priv = netdev_priv(dev);
303 
304 	priv->msg_enable = enable;
305 }
306 
bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)307 static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
308 {
309 	switch (type) {
310 	case BCM_SYSPORT_STAT_NETDEV:
311 	case BCM_SYSPORT_STAT_NETDEV64:
312 	case BCM_SYSPORT_STAT_RXCHK:
313 	case BCM_SYSPORT_STAT_RBUF:
314 	case BCM_SYSPORT_STAT_RDMA:
315 	case BCM_SYSPORT_STAT_SOFT:
316 		return true;
317 	default:
318 		return false;
319 	}
320 }
321 
bcm_sysport_get_sset_count(struct net_device * dev,int string_set)322 static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
323 {
324 	struct bcm_sysport_priv *priv = netdev_priv(dev);
325 	const struct bcm_sysport_stats *s;
326 	unsigned int i, j;
327 
328 	switch (string_set) {
329 	case ETH_SS_STATS:
330 		for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
331 			s = &bcm_sysport_gstrings_stats[i];
332 			if (priv->is_lite &&
333 			    !bcm_sysport_lite_stat_valid(s->type))
334 				continue;
335 			j++;
336 		}
337 		/* Include per-queue statistics */
338 		return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
339 	default:
340 		return -EOPNOTSUPP;
341 	}
342 }
343 
bcm_sysport_get_strings(struct net_device * dev,u32 stringset,u8 * data)344 static void bcm_sysport_get_strings(struct net_device *dev,
345 				    u32 stringset, u8 *data)
346 {
347 	struct bcm_sysport_priv *priv = netdev_priv(dev);
348 	const struct bcm_sysport_stats *s;
349 	int i;
350 
351 	switch (stringset) {
352 	case ETH_SS_STATS:
353 		for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
354 			s = &bcm_sysport_gstrings_stats[i];
355 			if (priv->is_lite &&
356 			    !bcm_sysport_lite_stat_valid(s->type))
357 				continue;
358 
359 			ethtool_puts(&data, s->stat_string);
360 		}
361 
362 		for (i = 0; i < dev->num_tx_queues; i++) {
363 			ethtool_sprintf(&data, "txq%d_packets", i);
364 			ethtool_sprintf(&data, "txq%d_bytes", i);
365 		}
366 		break;
367 	default:
368 		break;
369 	}
370 }
371 
bcm_sysport_update_mib_counters(struct bcm_sysport_priv * priv)372 static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
373 {
374 	int i, j = 0;
375 
376 	for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
377 		const struct bcm_sysport_stats *s;
378 		u8 offset = 0;
379 		u32 val = 0;
380 		char *p;
381 
382 		s = &bcm_sysport_gstrings_stats[i];
383 		switch (s->type) {
384 		case BCM_SYSPORT_STAT_NETDEV:
385 		case BCM_SYSPORT_STAT_NETDEV64:
386 		case BCM_SYSPORT_STAT_SOFT:
387 			continue;
388 		case BCM_SYSPORT_STAT_MIB_RX:
389 		case BCM_SYSPORT_STAT_MIB_TX:
390 		case BCM_SYSPORT_STAT_RUNT:
391 			if (priv->is_lite)
392 				continue;
393 
394 			if (s->type == BCM_SYSPORT_STAT_MIB_TX)
395 				offset = UMAC_MIB_STAT_OFFSET;
396 			else if (s->type == BCM_SYSPORT_STAT_RUNT)
397 				offset = 2 * UMAC_MIB_STAT_OFFSET;
398 			val = umac_readl(priv, UMAC_MIB_START + j + offset);
399 			break;
400 		case BCM_SYSPORT_STAT_RXCHK:
401 			val = rxchk_readl(priv, s->reg_offset);
402 			if (val == ~0)
403 				rxchk_writel(priv, 0, s->reg_offset);
404 			break;
405 		case BCM_SYSPORT_STAT_RBUF:
406 			val = rbuf_readl(priv, s->reg_offset);
407 			if (val == ~0)
408 				rbuf_writel(priv, 0, s->reg_offset);
409 			break;
410 		case BCM_SYSPORT_STAT_RDMA:
411 			if (!priv->is_lite)
412 				continue;
413 
414 			val = rdma_readl(priv, s->reg_offset);
415 			if (val == ~0)
416 				rdma_writel(priv, 0, s->reg_offset);
417 			break;
418 		}
419 
420 		j += s->stat_sizeof;
421 		p = (char *)priv + s->stat_offset;
422 		*(u32 *)p = val;
423 	}
424 
425 	netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
426 }
427 
bcm_sysport_update_tx_stats(struct bcm_sysport_priv * priv,u64 * tx_bytes,u64 * tx_packets)428 static void bcm_sysport_update_tx_stats(struct bcm_sysport_priv *priv,
429 					u64 *tx_bytes, u64 *tx_packets)
430 {
431 	struct bcm_sysport_tx_ring *ring;
432 	u64 bytes = 0, packets = 0;
433 	unsigned int start;
434 	unsigned int q;
435 
436 	for (q = 0; q < priv->netdev->num_tx_queues; q++) {
437 		ring = &priv->tx_rings[q];
438 		do {
439 			start = u64_stats_fetch_begin(&priv->syncp);
440 			bytes = ring->bytes;
441 			packets = ring->packets;
442 		} while (u64_stats_fetch_retry(&priv->syncp, start));
443 
444 		*tx_bytes += bytes;
445 		*tx_packets += packets;
446 	}
447 }
448 
bcm_sysport_get_stats(struct net_device * dev,struct ethtool_stats * stats,u64 * data)449 static void bcm_sysport_get_stats(struct net_device *dev,
450 				  struct ethtool_stats *stats, u64 *data)
451 {
452 	struct bcm_sysport_priv *priv = netdev_priv(dev);
453 	struct bcm_sysport_stats64 *stats64 = &priv->stats64;
454 	struct u64_stats_sync *syncp = &priv->syncp;
455 	struct bcm_sysport_tx_ring *ring;
456 	u64 tx_bytes = 0, tx_packets = 0;
457 	unsigned int start;
458 	int i, j;
459 
460 	if (netif_running(dev)) {
461 		bcm_sysport_update_mib_counters(priv);
462 		bcm_sysport_update_tx_stats(priv, &tx_bytes, &tx_packets);
463 		stats64->tx_bytes = tx_bytes;
464 		stats64->tx_packets = tx_packets;
465 	}
466 
467 	for (i =  0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
468 		const struct bcm_sysport_stats *s;
469 		char *p;
470 
471 		s = &bcm_sysport_gstrings_stats[i];
472 		if (s->type == BCM_SYSPORT_STAT_NETDEV)
473 			p = (char *)&dev->stats;
474 		else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
475 			p = (char *)stats64;
476 		else
477 			p = (char *)priv;
478 
479 		if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
480 			continue;
481 		p += s->stat_offset;
482 
483 		if (s->stat_sizeof == sizeof(u64) &&
484 		    s->type == BCM_SYSPORT_STAT_NETDEV64) {
485 			do {
486 				start = u64_stats_fetch_begin(syncp);
487 				data[j] = *(u64 *)p;
488 			} while (u64_stats_fetch_retry(syncp, start));
489 		} else
490 			data[j] = *(u32 *)p;
491 		j++;
492 	}
493 
494 	/* For SYSTEMPORT Lite since we have holes in our statistics, j would
495 	 * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
496 	 * needs to point to how many total statistics we have minus the
497 	 * number of per TX queue statistics
498 	 */
499 	j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
500 	    dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
501 
502 	for (i = 0; i < dev->num_tx_queues; i++) {
503 		ring = &priv->tx_rings[i];
504 		data[j] = ring->packets;
505 		j++;
506 		data[j] = ring->bytes;
507 		j++;
508 	}
509 }
510 
bcm_sysport_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)511 static void bcm_sysport_get_wol(struct net_device *dev,
512 				struct ethtool_wolinfo *wol)
513 {
514 	struct bcm_sysport_priv *priv = netdev_priv(dev);
515 
516 	wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
517 	wol->wolopts = priv->wolopts;
518 
519 	if (!(priv->wolopts & WAKE_MAGICSECURE))
520 		return;
521 
522 	memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
523 }
524 
bcm_sysport_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)525 static int bcm_sysport_set_wol(struct net_device *dev,
526 			       struct ethtool_wolinfo *wol)
527 {
528 	struct bcm_sysport_priv *priv = netdev_priv(dev);
529 	struct device *kdev = &priv->pdev->dev;
530 	u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
531 
532 	if (!device_can_wakeup(kdev))
533 		return -ENOTSUPP;
534 
535 	if (wol->wolopts & ~supported)
536 		return -EINVAL;
537 
538 	if (wol->wolopts & WAKE_MAGICSECURE)
539 		memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
540 
541 	/* Flag the device and relevant IRQ as wakeup capable */
542 	if (wol->wolopts) {
543 		device_set_wakeup_enable(kdev, 1);
544 		if (priv->wol_irq_disabled)
545 			enable_irq_wake(priv->wol_irq);
546 		priv->wol_irq_disabled = 0;
547 	} else {
548 		device_set_wakeup_enable(kdev, 0);
549 		/* Avoid unbalanced disable_irq_wake calls */
550 		if (!priv->wol_irq_disabled)
551 			disable_irq_wake(priv->wol_irq);
552 		priv->wol_irq_disabled = 1;
553 	}
554 
555 	priv->wolopts = wol->wolopts;
556 
557 	return 0;
558 }
559 
bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv * priv,u32 usecs,u32 pkts)560 static void bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv *priv,
561 					u32 usecs, u32 pkts)
562 {
563 	u32 reg;
564 
565 	reg = rdma_readl(priv, RDMA_MBDONE_INTR);
566 	reg &= ~(RDMA_INTR_THRESH_MASK |
567 		 RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
568 	reg |= pkts;
569 	reg |= DIV_ROUND_UP(usecs * 1000, 8192) << RDMA_TIMEOUT_SHIFT;
570 	rdma_writel(priv, reg, RDMA_MBDONE_INTR);
571 }
572 
bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring * ring,struct ethtool_coalesce * ec)573 static void bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring *ring,
574 					struct ethtool_coalesce *ec)
575 {
576 	struct bcm_sysport_priv *priv = ring->priv;
577 	u32 reg;
578 
579 	reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(ring->index));
580 	reg &= ~(RING_INTR_THRESH_MASK |
581 		 RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
582 	reg |= ec->tx_max_coalesced_frames;
583 	reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
584 			    RING_TIMEOUT_SHIFT;
585 	tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(ring->index));
586 }
587 
bcm_sysport_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)588 static int bcm_sysport_get_coalesce(struct net_device *dev,
589 				    struct ethtool_coalesce *ec,
590 				    struct kernel_ethtool_coalesce *kernel_coal,
591 				    struct netlink_ext_ack *extack)
592 {
593 	struct bcm_sysport_priv *priv = netdev_priv(dev);
594 	u32 reg;
595 
596 	reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
597 
598 	ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
599 	ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
600 
601 	reg = rdma_readl(priv, RDMA_MBDONE_INTR);
602 
603 	ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
604 	ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
605 	ec->use_adaptive_rx_coalesce = priv->dim.use_dim;
606 
607 	return 0;
608 }
609 
bcm_sysport_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)610 static int bcm_sysport_set_coalesce(struct net_device *dev,
611 				    struct ethtool_coalesce *ec,
612 				    struct kernel_ethtool_coalesce *kernel_coal,
613 				    struct netlink_ext_ack *extack)
614 {
615 	struct bcm_sysport_priv *priv = netdev_priv(dev);
616 	struct dim_cq_moder moder;
617 	u32 usecs, pkts;
618 	unsigned int i;
619 
620 	/* Base system clock is 125Mhz, DMA timeout is this reference clock
621 	 * divided by 1024, which yield roughly 8.192 us, our maximum value has
622 	 * to fit in the RING_TIMEOUT_MASK (16 bits).
623 	 */
624 	if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
625 	    ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
626 	    ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
627 	    ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
628 		return -EINVAL;
629 
630 	if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
631 	    (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0))
632 		return -EINVAL;
633 
634 	for (i = 0; i < dev->num_tx_queues; i++)
635 		bcm_sysport_set_tx_coalesce(&priv->tx_rings[i], ec);
636 
637 	priv->rx_coalesce_usecs = ec->rx_coalesce_usecs;
638 	priv->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
639 	usecs = priv->rx_coalesce_usecs;
640 	pkts = priv->rx_max_coalesced_frames;
641 
642 	if (ec->use_adaptive_rx_coalesce && !priv->dim.use_dim) {
643 		moder = net_dim_get_def_rx_moderation(priv->dim.dim.mode);
644 		usecs = moder.usec;
645 		pkts = moder.pkts;
646 	}
647 
648 	priv->dim.use_dim = ec->use_adaptive_rx_coalesce;
649 
650 	/* Apply desired coalescing parameters */
651 	bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
652 
653 	return 0;
654 }
655 
bcm_sysport_free_cb(struct bcm_sysport_cb * cb)656 static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
657 {
658 	dev_consume_skb_any(cb->skb);
659 	cb->skb = NULL;
660 	dma_unmap_addr_set(cb, dma_addr, 0);
661 }
662 
bcm_sysport_rx_refill(struct bcm_sysport_priv * priv,struct bcm_sysport_cb * cb)663 static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
664 					     struct bcm_sysport_cb *cb)
665 {
666 	struct device *kdev = &priv->pdev->dev;
667 	struct net_device *ndev = priv->netdev;
668 	struct sk_buff *skb, *rx_skb;
669 	dma_addr_t mapping;
670 
671 	/* Allocate a new SKB for a new packet */
672 	skb = __netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH,
673 				 GFP_ATOMIC | __GFP_NOWARN);
674 	if (!skb) {
675 		priv->mib.alloc_rx_buff_failed++;
676 		netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
677 		return NULL;
678 	}
679 
680 	mapping = dma_map_single(kdev, skb->data,
681 				 RX_BUF_LENGTH, DMA_FROM_DEVICE);
682 	if (dma_mapping_error(kdev, mapping)) {
683 		priv->mib.rx_dma_failed++;
684 		dev_kfree_skb_any(skb);
685 		netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
686 		return NULL;
687 	}
688 
689 	/* Grab the current SKB on the ring */
690 	rx_skb = cb->skb;
691 	if (likely(rx_skb))
692 		dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
693 				 RX_BUF_LENGTH, DMA_FROM_DEVICE);
694 
695 	/* Put the new SKB on the ring */
696 	cb->skb = skb;
697 	dma_unmap_addr_set(cb, dma_addr, mapping);
698 	dma_desc_set_addr(priv, cb->bd_addr, mapping);
699 
700 	netif_dbg(priv, rx_status, ndev, "RX refill\n");
701 
702 	/* Return the current SKB to the caller */
703 	return rx_skb;
704 }
705 
bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv * priv)706 static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
707 {
708 	struct bcm_sysport_cb *cb;
709 	struct sk_buff *skb;
710 	unsigned int i;
711 
712 	for (i = 0; i < priv->num_rx_bds; i++) {
713 		cb = &priv->rx_cbs[i];
714 		skb = bcm_sysport_rx_refill(priv, cb);
715 		dev_kfree_skb(skb);
716 		if (!cb->skb)
717 			return -ENOMEM;
718 	}
719 
720 	return 0;
721 }
722 
723 /* Poll the hardware for up to budget packets to process */
bcm_sysport_desc_rx(struct bcm_sysport_priv * priv,unsigned int budget)724 static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
725 					unsigned int budget)
726 {
727 	struct bcm_sysport_stats64 *stats64 = &priv->stats64;
728 	struct net_device *ndev = priv->netdev;
729 	unsigned int processed = 0, to_process;
730 	unsigned int processed_bytes = 0;
731 	struct bcm_sysport_cb *cb;
732 	struct sk_buff *skb;
733 	unsigned int p_index;
734 	u16 len, status;
735 	struct bcm_rsb *rsb;
736 
737 	/* Clear status before servicing to reduce spurious interrupts */
738 	intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
739 
740 	/* Determine how much we should process since last call, SYSTEMPORT Lite
741 	 * groups the producer and consumer indexes into the same 32-bit
742 	 * which we access using RDMA_CONS_INDEX
743 	 */
744 	if (!priv->is_lite)
745 		p_index = rdma_readl(priv, RDMA_PROD_INDEX);
746 	else
747 		p_index = rdma_readl(priv, RDMA_CONS_INDEX);
748 	p_index &= RDMA_PROD_INDEX_MASK;
749 
750 	to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
751 
752 	netif_dbg(priv, rx_status, ndev,
753 		  "p_index=%d rx_c_index=%d to_process=%d\n",
754 		  p_index, priv->rx_c_index, to_process);
755 
756 	while ((processed < to_process) && (processed < budget)) {
757 		cb = &priv->rx_cbs[priv->rx_read_ptr];
758 		skb = bcm_sysport_rx_refill(priv, cb);
759 
760 
761 		/* We do not have a backing SKB, so we do not a corresponding
762 		 * DMA mapping for this incoming packet since
763 		 * bcm_sysport_rx_refill always either has both skb and mapping
764 		 * or none.
765 		 */
766 		if (unlikely(!skb)) {
767 			netif_err(priv, rx_err, ndev, "out of memory!\n");
768 			ndev->stats.rx_dropped++;
769 			ndev->stats.rx_errors++;
770 			goto next;
771 		}
772 
773 		/* Extract the Receive Status Block prepended */
774 		rsb = (struct bcm_rsb *)skb->data;
775 		len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
776 		status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
777 			  DESC_STATUS_MASK;
778 
779 		netif_dbg(priv, rx_status, ndev,
780 			  "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
781 			  p_index, priv->rx_c_index, priv->rx_read_ptr,
782 			  len, status);
783 
784 		if (unlikely(len > RX_BUF_LENGTH)) {
785 			netif_err(priv, rx_status, ndev, "oversized packet\n");
786 			ndev->stats.rx_length_errors++;
787 			ndev->stats.rx_errors++;
788 			dev_kfree_skb_any(skb);
789 			goto next;
790 		}
791 
792 		if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
793 			netif_err(priv, rx_status, ndev, "fragmented packet!\n");
794 			ndev->stats.rx_dropped++;
795 			ndev->stats.rx_errors++;
796 			dev_kfree_skb_any(skb);
797 			goto next;
798 		}
799 
800 		if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
801 			netif_err(priv, rx_err, ndev, "error packet\n");
802 			if (status & RX_STATUS_OVFLOW)
803 				ndev->stats.rx_over_errors++;
804 			ndev->stats.rx_dropped++;
805 			ndev->stats.rx_errors++;
806 			dev_kfree_skb_any(skb);
807 			goto next;
808 		}
809 
810 		skb_put(skb, len);
811 
812 		/* Hardware validated our checksum */
813 		if (likely(status & DESC_L4_CSUM))
814 			skb->ip_summed = CHECKSUM_UNNECESSARY;
815 
816 		/* Hardware pre-pends packets with 2bytes before Ethernet
817 		 * header plus we have the Receive Status Block, strip off all
818 		 * of this from the SKB.
819 		 */
820 		skb_pull(skb, sizeof(*rsb) + 2);
821 		len -= (sizeof(*rsb) + 2);
822 		processed_bytes += len;
823 
824 		/* UniMAC may forward CRC */
825 		if (priv->crc_fwd) {
826 			skb_trim(skb, len - ETH_FCS_LEN);
827 			len -= ETH_FCS_LEN;
828 		}
829 
830 		skb->protocol = eth_type_trans(skb, ndev);
831 		ndev->stats.rx_packets++;
832 		ndev->stats.rx_bytes += len;
833 		u64_stats_update_begin(&priv->syncp);
834 		stats64->rx_packets++;
835 		stats64->rx_bytes += len;
836 		u64_stats_update_end(&priv->syncp);
837 
838 		napi_gro_receive(&priv->napi, skb);
839 next:
840 		processed++;
841 		priv->rx_read_ptr++;
842 
843 		if (priv->rx_read_ptr == priv->num_rx_bds)
844 			priv->rx_read_ptr = 0;
845 	}
846 
847 	priv->dim.packets = processed;
848 	priv->dim.bytes = processed_bytes;
849 
850 	return processed;
851 }
852 
bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring * ring,struct bcm_sysport_cb * cb,unsigned int * bytes_compl,unsigned int * pkts_compl)853 static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
854 				       struct bcm_sysport_cb *cb,
855 				       unsigned int *bytes_compl,
856 				       unsigned int *pkts_compl)
857 {
858 	struct bcm_sysport_priv *priv = ring->priv;
859 	struct device *kdev = &priv->pdev->dev;
860 
861 	if (cb->skb) {
862 		*bytes_compl += cb->skb->len;
863 		dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
864 				 dma_unmap_len(cb, dma_len),
865 				 DMA_TO_DEVICE);
866 		(*pkts_compl)++;
867 		bcm_sysport_free_cb(cb);
868 	/* SKB fragment */
869 	} else if (dma_unmap_addr(cb, dma_addr)) {
870 		*bytes_compl += dma_unmap_len(cb, dma_len);
871 		dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
872 			       dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
873 		dma_unmap_addr_set(cb, dma_addr, 0);
874 	}
875 }
876 
877 /* Reclaim queued SKBs for transmission completion, lockless version */
__bcm_sysport_tx_reclaim(struct bcm_sysport_priv * priv,struct bcm_sysport_tx_ring * ring)878 static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
879 					     struct bcm_sysport_tx_ring *ring)
880 {
881 	unsigned int pkts_compl = 0, bytes_compl = 0;
882 	struct net_device *ndev = priv->netdev;
883 	unsigned int txbds_processed = 0;
884 	struct bcm_sysport_cb *cb;
885 	unsigned int txbds_ready;
886 	unsigned int c_index;
887 	u32 hw_ind;
888 
889 	/* Clear status before servicing to reduce spurious interrupts */
890 	if (!ring->priv->is_lite)
891 		intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
892 	else
893 		intrl2_0_writel(ring->priv, BIT(ring->index +
894 				INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
895 
896 	/* Compute how many descriptors have been processed since last call */
897 	hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
898 	c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
899 	txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
900 
901 	netif_dbg(priv, tx_done, ndev,
902 		  "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
903 		  ring->index, ring->c_index, c_index, txbds_ready);
904 
905 	while (txbds_processed < txbds_ready) {
906 		cb = &ring->cbs[ring->clean_index];
907 		bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
908 
909 		ring->desc_count++;
910 		txbds_processed++;
911 
912 		if (likely(ring->clean_index < ring->size - 1))
913 			ring->clean_index++;
914 		else
915 			ring->clean_index = 0;
916 	}
917 
918 	u64_stats_update_begin(&priv->syncp);
919 	ring->packets += pkts_compl;
920 	ring->bytes += bytes_compl;
921 	u64_stats_update_end(&priv->syncp);
922 
923 	ring->c_index = c_index;
924 
925 	netif_dbg(priv, tx_done, ndev,
926 		  "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
927 		  ring->index, ring->c_index, pkts_compl, bytes_compl);
928 
929 	return pkts_compl;
930 }
931 
932 /* Locked version of the per-ring TX reclaim routine */
bcm_sysport_tx_reclaim(struct bcm_sysport_priv * priv,struct bcm_sysport_tx_ring * ring)933 static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
934 					   struct bcm_sysport_tx_ring *ring)
935 {
936 	struct netdev_queue *txq;
937 	unsigned int released;
938 	unsigned long flags;
939 
940 	txq = netdev_get_tx_queue(priv->netdev, ring->index);
941 
942 	spin_lock_irqsave(&ring->lock, flags);
943 	released = __bcm_sysport_tx_reclaim(priv, ring);
944 	if (released)
945 		netif_tx_wake_queue(txq);
946 
947 	spin_unlock_irqrestore(&ring->lock, flags);
948 
949 	return released;
950 }
951 
952 /* Locked version of the per-ring TX reclaim, but does not wake the queue */
bcm_sysport_tx_clean(struct bcm_sysport_priv * priv,struct bcm_sysport_tx_ring * ring)953 static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
954 				 struct bcm_sysport_tx_ring *ring)
955 {
956 	unsigned long flags;
957 
958 	spin_lock_irqsave(&ring->lock, flags);
959 	__bcm_sysport_tx_reclaim(priv, ring);
960 	spin_unlock_irqrestore(&ring->lock, flags);
961 }
962 
bcm_sysport_tx_poll(struct napi_struct * napi,int budget)963 static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
964 {
965 	struct bcm_sysport_tx_ring *ring =
966 		container_of(napi, struct bcm_sysport_tx_ring, napi);
967 	unsigned int work_done = 0;
968 
969 	work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
970 
971 	if (work_done == 0) {
972 		napi_complete(napi);
973 		/* re-enable TX interrupt */
974 		if (!ring->priv->is_lite)
975 			intrl2_1_mask_clear(ring->priv, BIT(ring->index));
976 		else
977 			intrl2_0_mask_clear(ring->priv, BIT(ring->index +
978 					    INTRL2_0_TDMA_MBDONE_SHIFT));
979 
980 		return 0;
981 	}
982 
983 	return budget;
984 }
985 
bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv * priv)986 static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
987 {
988 	unsigned int q;
989 
990 	for (q = 0; q < priv->netdev->num_tx_queues; q++)
991 		bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
992 }
993 
bcm_sysport_poll(struct napi_struct * napi,int budget)994 static int bcm_sysport_poll(struct napi_struct *napi, int budget)
995 {
996 	struct bcm_sysport_priv *priv =
997 		container_of(napi, struct bcm_sysport_priv, napi);
998 	struct dim_sample dim_sample = {};
999 	unsigned int work_done = 0;
1000 
1001 	work_done = bcm_sysport_desc_rx(priv, budget);
1002 
1003 	priv->rx_c_index += work_done;
1004 	priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
1005 
1006 	/* SYSTEMPORT Lite groups the producer/consumer index, producer is
1007 	 * maintained by HW, but writes to it will be ignore while RDMA
1008 	 * is active
1009 	 */
1010 	if (!priv->is_lite)
1011 		rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
1012 	else
1013 		rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
1014 
1015 	if (work_done < budget) {
1016 		napi_complete_done(napi, work_done);
1017 		/* re-enable RX interrupts */
1018 		intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
1019 	}
1020 
1021 	if (priv->dim.use_dim) {
1022 		dim_update_sample(priv->dim.event_ctr, priv->dim.packets,
1023 				  priv->dim.bytes, &dim_sample);
1024 		net_dim(&priv->dim.dim, &dim_sample);
1025 	}
1026 
1027 	return work_done;
1028 }
1029 
mpd_enable_set(struct bcm_sysport_priv * priv,bool enable)1030 static void mpd_enable_set(struct bcm_sysport_priv *priv, bool enable)
1031 {
1032 	u32 reg, bit;
1033 
1034 	reg = umac_readl(priv, UMAC_MPD_CTRL);
1035 	if (enable)
1036 		reg |= MPD_EN;
1037 	else
1038 		reg &= ~MPD_EN;
1039 	umac_writel(priv, reg, UMAC_MPD_CTRL);
1040 
1041 	if (priv->is_lite)
1042 		bit = RBUF_ACPI_EN_LITE;
1043 	else
1044 		bit = RBUF_ACPI_EN;
1045 
1046 	reg = rbuf_readl(priv, RBUF_CONTROL);
1047 	if (enable)
1048 		reg |= bit;
1049 	else
1050 		reg &= ~bit;
1051 	rbuf_writel(priv, reg, RBUF_CONTROL);
1052 }
1053 
bcm_sysport_resume_from_wol(struct bcm_sysport_priv * priv)1054 static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
1055 {
1056 	unsigned int index;
1057 	u32 reg;
1058 
1059 	/* Disable RXCHK, active filters and Broadcom tag matching */
1060 	reg = rxchk_readl(priv, RXCHK_CONTROL);
1061 	reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
1062 		 RXCHK_BRCM_TAG_MATCH_SHIFT | RXCHK_EN | RXCHK_BRCM_TAG_EN);
1063 	rxchk_writel(priv, reg, RXCHK_CONTROL);
1064 
1065 	/* Make sure we restore correct CID index in case HW lost
1066 	 * its context during deep idle state
1067 	 */
1068 	for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
1069 		rxchk_writel(priv, priv->filters_loc[index] <<
1070 			     RXCHK_BRCM_TAG_CID_SHIFT, RXCHK_BRCM_TAG(index));
1071 		rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
1072 	}
1073 
1074 	/* Clear the MagicPacket detection logic */
1075 	mpd_enable_set(priv, false);
1076 
1077 	reg = intrl2_0_readl(priv, INTRL2_CPU_STATUS);
1078 	if (reg & INTRL2_0_MPD)
1079 		netdev_info(priv->netdev, "Wake-on-LAN (MPD) interrupt!\n");
1080 
1081 	if (reg & INTRL2_0_BRCM_MATCH_TAG) {
1082 		reg = rxchk_readl(priv, RXCHK_BRCM_TAG_MATCH_STATUS) &
1083 				  RXCHK_BRCM_TAG_MATCH_MASK;
1084 		netdev_info(priv->netdev,
1085 			    "Wake-on-LAN (filters 0x%02x) interrupt!\n", reg);
1086 	}
1087 
1088 	netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
1089 }
1090 
bcm_sysport_dim_work(struct work_struct * work)1091 static void bcm_sysport_dim_work(struct work_struct *work)
1092 {
1093 	struct dim *dim = container_of(work, struct dim, work);
1094 	struct bcm_sysport_net_dim *ndim =
1095 			container_of(dim, struct bcm_sysport_net_dim, dim);
1096 	struct bcm_sysport_priv *priv =
1097 			container_of(ndim, struct bcm_sysport_priv, dim);
1098 	struct dim_cq_moder cur_profile = net_dim_get_rx_moderation(dim->mode,
1099 								    dim->profile_ix);
1100 
1101 	bcm_sysport_set_rx_coalesce(priv, cur_profile.usec, cur_profile.pkts);
1102 	dim->state = DIM_START_MEASURE;
1103 }
1104 
1105 /* RX and misc interrupt routine */
bcm_sysport_rx_isr(int irq,void * dev_id)1106 static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
1107 {
1108 	struct net_device *dev = dev_id;
1109 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1110 	struct bcm_sysport_tx_ring *txr;
1111 	unsigned int ring, ring_bit;
1112 
1113 	priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
1114 			  ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1115 	intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1116 
1117 	if (unlikely(priv->irq0_stat == 0)) {
1118 		netdev_warn(priv->netdev, "spurious RX interrupt\n");
1119 		return IRQ_NONE;
1120 	}
1121 
1122 	if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
1123 		priv->dim.event_ctr++;
1124 		if (likely(napi_schedule_prep(&priv->napi))) {
1125 			/* disable RX interrupts */
1126 			intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
1127 			__napi_schedule_irqoff(&priv->napi);
1128 		}
1129 	}
1130 
1131 	/* TX ring is full, perform a full reclaim since we do not know
1132 	 * which one would trigger this interrupt
1133 	 */
1134 	if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
1135 		bcm_sysport_tx_reclaim_all(priv);
1136 
1137 	if (!priv->is_lite)
1138 		goto out;
1139 
1140 	for (ring = 0; ring < dev->num_tx_queues; ring++) {
1141 		ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
1142 		if (!(priv->irq0_stat & ring_bit))
1143 			continue;
1144 
1145 		txr = &priv->tx_rings[ring];
1146 
1147 		if (likely(napi_schedule_prep(&txr->napi))) {
1148 			intrl2_0_mask_set(priv, ring_bit);
1149 			__napi_schedule(&txr->napi);
1150 		}
1151 	}
1152 out:
1153 	return IRQ_HANDLED;
1154 }
1155 
1156 /* TX interrupt service routine */
bcm_sysport_tx_isr(int irq,void * dev_id)1157 static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
1158 {
1159 	struct net_device *dev = dev_id;
1160 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1161 	struct bcm_sysport_tx_ring *txr;
1162 	unsigned int ring;
1163 
1164 	priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
1165 				~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
1166 	intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1167 
1168 	if (unlikely(priv->irq1_stat == 0)) {
1169 		netdev_warn(priv->netdev, "spurious TX interrupt\n");
1170 		return IRQ_NONE;
1171 	}
1172 
1173 	for (ring = 0; ring < dev->num_tx_queues; ring++) {
1174 		if (!(priv->irq1_stat & BIT(ring)))
1175 			continue;
1176 
1177 		txr = &priv->tx_rings[ring];
1178 
1179 		if (likely(napi_schedule_prep(&txr->napi))) {
1180 			intrl2_1_mask_set(priv, BIT(ring));
1181 			__napi_schedule_irqoff(&txr->napi);
1182 		}
1183 	}
1184 
1185 	return IRQ_HANDLED;
1186 }
1187 
bcm_sysport_wol_isr(int irq,void * dev_id)1188 static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
1189 {
1190 	struct bcm_sysport_priv *priv = dev_id;
1191 
1192 	pm_wakeup_event(&priv->pdev->dev, 0);
1193 
1194 	return IRQ_HANDLED;
1195 }
1196 
1197 #ifdef CONFIG_NET_POLL_CONTROLLER
bcm_sysport_poll_controller(struct net_device * dev)1198 static void bcm_sysport_poll_controller(struct net_device *dev)
1199 {
1200 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1201 
1202 	disable_irq(priv->irq0);
1203 	bcm_sysport_rx_isr(priv->irq0, priv);
1204 	enable_irq(priv->irq0);
1205 
1206 	if (!priv->is_lite) {
1207 		disable_irq(priv->irq1);
1208 		bcm_sysport_tx_isr(priv->irq1, priv);
1209 		enable_irq(priv->irq1);
1210 	}
1211 }
1212 #endif
1213 
bcm_sysport_insert_tsb(struct sk_buff * skb,struct net_device * dev)1214 static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
1215 					      struct net_device *dev)
1216 {
1217 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1218 	struct sk_buff *nskb;
1219 	struct bcm_tsb *tsb;
1220 	u32 csum_info;
1221 	u8 ip_proto;
1222 	u16 csum_start;
1223 	__be16 ip_ver;
1224 
1225 	/* Re-allocate SKB if needed */
1226 	if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
1227 		nskb = skb_realloc_headroom(skb, sizeof(*tsb));
1228 		if (!nskb) {
1229 			dev_kfree_skb_any(skb);
1230 			priv->mib.tx_realloc_tsb_failed++;
1231 			dev->stats.tx_errors++;
1232 			dev->stats.tx_dropped++;
1233 			return NULL;
1234 		}
1235 		dev_consume_skb_any(skb);
1236 		skb = nskb;
1237 		priv->mib.tx_realloc_tsb++;
1238 	}
1239 
1240 	tsb = skb_push(skb, sizeof(*tsb));
1241 	/* Zero-out TSB by default */
1242 	memset(tsb, 0, sizeof(*tsb));
1243 
1244 	if (skb_vlan_tag_present(skb)) {
1245 		tsb->pcp_dei_vid = skb_vlan_tag_get_prio(skb) & PCP_DEI_MASK;
1246 		tsb->pcp_dei_vid |= (u32)skb_vlan_tag_get_id(skb) << VID_SHIFT;
1247 	}
1248 
1249 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1250 		ip_ver = skb->protocol;
1251 		switch (ip_ver) {
1252 		case htons(ETH_P_IP):
1253 			ip_proto = ip_hdr(skb)->protocol;
1254 			break;
1255 		case htons(ETH_P_IPV6):
1256 			ip_proto = ipv6_hdr(skb)->nexthdr;
1257 			break;
1258 		default:
1259 			return skb;
1260 		}
1261 
1262 		/* Get the checksum offset and the L4 (transport) offset */
1263 		csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
1264 		/* Account for the HW inserted VLAN tag */
1265 		if (skb_vlan_tag_present(skb))
1266 			csum_start += VLAN_HLEN;
1267 		csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
1268 		csum_info |= (csum_start << L4_PTR_SHIFT);
1269 
1270 		if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1271 			csum_info |= L4_LENGTH_VALID;
1272 			if (ip_proto == IPPROTO_UDP &&
1273 			    ip_ver == htons(ETH_P_IP))
1274 				csum_info |= L4_UDP;
1275 		} else {
1276 			csum_info = 0;
1277 		}
1278 
1279 		tsb->l4_ptr_dest_map = csum_info;
1280 	}
1281 
1282 	return skb;
1283 }
1284 
bcm_sysport_xmit(struct sk_buff * skb,struct net_device * dev)1285 static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
1286 				    struct net_device *dev)
1287 {
1288 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1289 	struct device *kdev = &priv->pdev->dev;
1290 	struct bcm_sysport_tx_ring *ring;
1291 	unsigned long flags, desc_flags;
1292 	struct bcm_sysport_cb *cb;
1293 	struct netdev_queue *txq;
1294 	u32 len_status, addr_lo;
1295 	unsigned int skb_len;
1296 	dma_addr_t mapping;
1297 	u16 queue;
1298 	int ret;
1299 
1300 	queue = skb_get_queue_mapping(skb);
1301 	txq = netdev_get_tx_queue(dev, queue);
1302 	ring = &priv->tx_rings[queue];
1303 
1304 	/* lock against tx reclaim in BH context and TX ring full interrupt */
1305 	spin_lock_irqsave(&ring->lock, flags);
1306 	if (unlikely(ring->desc_count == 0)) {
1307 		netif_tx_stop_queue(txq);
1308 		netdev_err(dev, "queue %d awake and ring full!\n", queue);
1309 		ret = NETDEV_TX_BUSY;
1310 		goto out;
1311 	}
1312 
1313 	/* Insert TSB and checksum infos */
1314 	if (priv->tsb_en) {
1315 		skb = bcm_sysport_insert_tsb(skb, dev);
1316 		if (!skb) {
1317 			ret = NETDEV_TX_OK;
1318 			goto out;
1319 		}
1320 	}
1321 
1322 	skb_len = skb->len;
1323 
1324 	mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
1325 	if (dma_mapping_error(kdev, mapping)) {
1326 		priv->mib.tx_dma_failed++;
1327 		netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
1328 			  skb->data, skb_len);
1329 		ret = NETDEV_TX_OK;
1330 		dev_kfree_skb_any(skb);
1331 		goto out;
1332 	}
1333 
1334 	/* Remember the SKB for future freeing */
1335 	cb = &ring->cbs[ring->curr_desc];
1336 	cb->skb = skb;
1337 	dma_unmap_addr_set(cb, dma_addr, mapping);
1338 	dma_unmap_len_set(cb, dma_len, skb_len);
1339 
1340 	addr_lo = lower_32_bits(mapping);
1341 	len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
1342 	len_status |= (skb_len << DESC_LEN_SHIFT);
1343 	len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
1344 		       DESC_STATUS_SHIFT;
1345 	if (skb->ip_summed == CHECKSUM_PARTIAL)
1346 		len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
1347 	if (skb_vlan_tag_present(skb))
1348 		len_status |= (TX_STATUS_VLAN_VID_TSB << DESC_STATUS_SHIFT);
1349 
1350 	ring->curr_desc++;
1351 	if (ring->curr_desc == ring->size)
1352 		ring->curr_desc = 0;
1353 	ring->desc_count--;
1354 
1355 	/* Ports are latched, so write upper address first */
1356 	spin_lock_irqsave(&priv->desc_lock, desc_flags);
1357 	tdma_writel(priv, len_status, TDMA_WRITE_PORT_HI(ring->index));
1358 	tdma_writel(priv, addr_lo, TDMA_WRITE_PORT_LO(ring->index));
1359 	spin_unlock_irqrestore(&priv->desc_lock, desc_flags);
1360 
1361 	/* Check ring space and update SW control flow */
1362 	if (ring->desc_count == 0)
1363 		netif_tx_stop_queue(txq);
1364 
1365 	netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
1366 		  ring->index, ring->desc_count, ring->curr_desc);
1367 
1368 	ret = NETDEV_TX_OK;
1369 out:
1370 	spin_unlock_irqrestore(&ring->lock, flags);
1371 	return ret;
1372 }
1373 
bcm_sysport_tx_timeout(struct net_device * dev,unsigned int txqueue)1374 static void bcm_sysport_tx_timeout(struct net_device *dev, unsigned int txqueue)
1375 {
1376 	netdev_warn(dev, "transmit timeout!\n");
1377 
1378 	netif_trans_update(dev);
1379 	dev->stats.tx_errors++;
1380 
1381 	netif_tx_wake_all_queues(dev);
1382 }
1383 
1384 /* phylib adjust link callback */
bcm_sysport_adj_link(struct net_device * dev)1385 static void bcm_sysport_adj_link(struct net_device *dev)
1386 {
1387 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1388 	struct phy_device *phydev = dev->phydev;
1389 	unsigned int changed = 0;
1390 	u32 cmd_bits = 0, reg;
1391 
1392 	if (priv->old_link != phydev->link) {
1393 		changed = 1;
1394 		priv->old_link = phydev->link;
1395 	}
1396 
1397 	if (priv->old_duplex != phydev->duplex) {
1398 		changed = 1;
1399 		priv->old_duplex = phydev->duplex;
1400 	}
1401 
1402 	if (priv->is_lite)
1403 		goto out;
1404 
1405 	switch (phydev->speed) {
1406 	case SPEED_2500:
1407 		cmd_bits = CMD_SPEED_2500;
1408 		break;
1409 	case SPEED_1000:
1410 		cmd_bits = CMD_SPEED_1000;
1411 		break;
1412 	case SPEED_100:
1413 		cmd_bits = CMD_SPEED_100;
1414 		break;
1415 	case SPEED_10:
1416 		cmd_bits = CMD_SPEED_10;
1417 		break;
1418 	default:
1419 		break;
1420 	}
1421 	cmd_bits <<= CMD_SPEED_SHIFT;
1422 
1423 	if (phydev->duplex == DUPLEX_HALF)
1424 		cmd_bits |= CMD_HD_EN;
1425 
1426 	if (priv->old_pause != phydev->pause) {
1427 		changed = 1;
1428 		priv->old_pause = phydev->pause;
1429 	}
1430 
1431 	if (!phydev->pause)
1432 		cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
1433 
1434 	if (!changed)
1435 		return;
1436 
1437 	if (phydev->link) {
1438 		reg = umac_readl(priv, UMAC_CMD);
1439 		reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
1440 			CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
1441 			CMD_TX_PAUSE_IGNORE);
1442 		reg |= cmd_bits;
1443 		umac_writel(priv, reg, UMAC_CMD);
1444 	}
1445 out:
1446 	if (changed)
1447 		phy_print_status(phydev);
1448 }
1449 
bcm_sysport_init_dim(struct bcm_sysport_priv * priv,void (* cb)(struct work_struct * work))1450 static void bcm_sysport_init_dim(struct bcm_sysport_priv *priv,
1451 				 void (*cb)(struct work_struct *work))
1452 {
1453 	struct bcm_sysport_net_dim *dim = &priv->dim;
1454 
1455 	INIT_WORK(&dim->dim.work, cb);
1456 	dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
1457 	dim->event_ctr = 0;
1458 	dim->packets = 0;
1459 	dim->bytes = 0;
1460 }
1461 
bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv * priv)1462 static void bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv *priv)
1463 {
1464 	struct bcm_sysport_net_dim *dim = &priv->dim;
1465 	struct dim_cq_moder moder;
1466 	u32 usecs, pkts;
1467 
1468 	usecs = priv->rx_coalesce_usecs;
1469 	pkts = priv->rx_max_coalesced_frames;
1470 
1471 	/* If DIM was enabled, re-apply default parameters */
1472 	if (dim->use_dim) {
1473 		moder = net_dim_get_def_rx_moderation(dim->dim.mode);
1474 		usecs = moder.usec;
1475 		pkts = moder.pkts;
1476 	}
1477 
1478 	bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
1479 }
1480 
bcm_sysport_init_tx_ring(struct bcm_sysport_priv * priv,unsigned int index)1481 static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
1482 				    unsigned int index)
1483 {
1484 	struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1485 	size_t size;
1486 	u32 reg;
1487 
1488 	/* Simple descriptors partitioning for now */
1489 	size = 256;
1490 
1491 	ring->cbs = kzalloc_objs(struct bcm_sysport_cb, size);
1492 	if (!ring->cbs) {
1493 		netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1494 		return -ENOMEM;
1495 	}
1496 
1497 	/* Initialize SW view of the ring */
1498 	spin_lock_init(&ring->lock);
1499 	ring->priv = priv;
1500 	netif_napi_add_tx(priv->netdev, &ring->napi, bcm_sysport_tx_poll);
1501 	ring->index = index;
1502 	ring->size = size;
1503 	ring->clean_index = 0;
1504 	ring->alloc_size = ring->size;
1505 	ring->desc_count = ring->size;
1506 	ring->curr_desc = 0;
1507 
1508 	/* Initialize HW ring */
1509 	tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1510 	tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1511 	tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1512 	tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1513 
1514 	/* Configure QID and port mapping */
1515 	reg = tdma_readl(priv, TDMA_DESC_RING_MAPPING(index));
1516 	reg &= ~(RING_QID_MASK | RING_PORT_ID_MASK << RING_PORT_ID_SHIFT);
1517 	if (ring->inspect) {
1518 		reg |= ring->switch_queue & RING_QID_MASK;
1519 		reg |= ring->switch_port << RING_PORT_ID_SHIFT;
1520 	} else {
1521 		reg |= RING_IGNORE_STATUS;
1522 	}
1523 	tdma_writel(priv, reg, TDMA_DESC_RING_MAPPING(index));
1524 	reg = 0;
1525 	/* Adjust the packet size calculations if SYSTEMPORT is responsible
1526 	 * for HW insertion of VLAN tags
1527 	 */
1528 	if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
1529 		reg = VLAN_HLEN << RING_PKT_SIZE_ADJ_SHIFT;
1530 	tdma_writel(priv, reg, TDMA_DESC_RING_PCP_DEI_VID(index));
1531 
1532 	/* Enable ACB algorithm 2 */
1533 	reg = tdma_readl(priv, TDMA_CONTROL);
1534 	reg |= tdma_control_bit(priv, ACB_ALGO);
1535 	tdma_writel(priv, reg, TDMA_CONTROL);
1536 
1537 	/* Do not use tdma_control_bit() here because TSB_SWAP1 collides
1538 	 * with the original definition of ACB_ALGO
1539 	 */
1540 	reg = tdma_readl(priv, TDMA_CONTROL);
1541 	if (priv->is_lite)
1542 		reg &= ~BIT(TSB_SWAP1);
1543 	/* Set a correct TSB format based on host endian */
1544 	if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1545 		reg |= tdma_control_bit(priv, TSB_SWAP0);
1546 	else
1547 		reg &= ~tdma_control_bit(priv, TSB_SWAP0);
1548 	tdma_writel(priv, reg, TDMA_CONTROL);
1549 
1550 	/* Program the number of descriptors as MAX_THRESHOLD and half of
1551 	 * its size for the hysteresis trigger
1552 	 */
1553 	tdma_writel(priv, ring->size |
1554 			1 << RING_HYST_THRESH_SHIFT,
1555 			TDMA_DESC_RING_MAX_HYST(index));
1556 
1557 	/* Enable the ring queue in the arbiter */
1558 	reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1559 	reg |= (1 << index);
1560 	tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1561 
1562 	napi_enable(&ring->napi);
1563 
1564 	netif_dbg(priv, hw, priv->netdev,
1565 		  "TDMA cfg, size=%d, switch q=%d,port=%d\n",
1566 		  ring->size, ring->switch_queue,
1567 		  ring->switch_port);
1568 
1569 	return 0;
1570 }
1571 
bcm_sysport_fini_tx_ring(struct bcm_sysport_priv * priv,unsigned int index)1572 static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
1573 				     unsigned int index)
1574 {
1575 	struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1576 	u32 reg;
1577 
1578 	/* Caller should stop the TDMA engine */
1579 	reg = tdma_readl(priv, TDMA_STATUS);
1580 	if (!(reg & TDMA_DISABLED))
1581 		netdev_warn(priv->netdev, "TDMA not stopped!\n");
1582 
1583 	/* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
1584 	 * fail, so by checking this pointer we know whether the TX ring was
1585 	 * fully initialized or not.
1586 	 */
1587 	if (!ring->cbs)
1588 		return;
1589 
1590 	napi_disable(&ring->napi);
1591 	netif_napi_del(&ring->napi);
1592 
1593 	bcm_sysport_tx_clean(priv, ring);
1594 
1595 	kfree(ring->cbs);
1596 	ring->cbs = NULL;
1597 	ring->size = 0;
1598 	ring->alloc_size = 0;
1599 
1600 	netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1601 }
1602 
1603 /* RDMA helper */
rdma_enable_set(struct bcm_sysport_priv * priv,unsigned int enable)1604 static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
1605 				  unsigned int enable)
1606 {
1607 	unsigned int timeout = 1000;
1608 	u32 reg;
1609 
1610 	reg = rdma_readl(priv, RDMA_CONTROL);
1611 	if (enable)
1612 		reg |= RDMA_EN;
1613 	else
1614 		reg &= ~RDMA_EN;
1615 	rdma_writel(priv, reg, RDMA_CONTROL);
1616 
1617 	/* Poll for RMDA disabling completion */
1618 	do {
1619 		reg = rdma_readl(priv, RDMA_STATUS);
1620 		if (!!(reg & RDMA_DISABLED) == !enable)
1621 			return 0;
1622 		usleep_range(1000, 2000);
1623 	} while (timeout-- > 0);
1624 
1625 	netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1626 
1627 	return -ETIMEDOUT;
1628 }
1629 
1630 /* TDMA helper */
tdma_enable_set(struct bcm_sysport_priv * priv,unsigned int enable)1631 static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
1632 				  unsigned int enable)
1633 {
1634 	unsigned int timeout = 1000;
1635 	u32 reg;
1636 
1637 	reg = tdma_readl(priv, TDMA_CONTROL);
1638 	if (enable)
1639 		reg |= tdma_control_bit(priv, TDMA_EN);
1640 	else
1641 		reg &= ~tdma_control_bit(priv, TDMA_EN);
1642 	tdma_writel(priv, reg, TDMA_CONTROL);
1643 
1644 	/* Poll for TMDA disabling completion */
1645 	do {
1646 		reg = tdma_readl(priv, TDMA_STATUS);
1647 		if (!!(reg & TDMA_DISABLED) == !enable)
1648 			return 0;
1649 
1650 		usleep_range(1000, 2000);
1651 	} while (timeout-- > 0);
1652 
1653 	netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1654 
1655 	return -ETIMEDOUT;
1656 }
1657 
bcm_sysport_init_rx_ring(struct bcm_sysport_priv * priv)1658 static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1659 {
1660 	struct bcm_sysport_cb *cb;
1661 	u32 reg;
1662 	int ret;
1663 	int i;
1664 
1665 	/* Initialize SW view of the RX ring */
1666 	priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
1667 	priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
1668 	priv->rx_c_index = 0;
1669 	priv->rx_read_ptr = 0;
1670 	priv->rx_cbs = kzalloc_objs(struct bcm_sysport_cb, priv->num_rx_bds);
1671 	if (!priv->rx_cbs) {
1672 		netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1673 		return -ENOMEM;
1674 	}
1675 
1676 	for (i = 0; i < priv->num_rx_bds; i++) {
1677 		cb = priv->rx_cbs + i;
1678 		cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
1679 	}
1680 
1681 	ret = bcm_sysport_alloc_rx_bufs(priv);
1682 	if (ret) {
1683 		netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1684 		return ret;
1685 	}
1686 
1687 	/* Initialize HW, ensure RDMA is disabled */
1688 	reg = rdma_readl(priv, RDMA_STATUS);
1689 	if (!(reg & RDMA_DISABLED))
1690 		rdma_enable_set(priv, 0);
1691 
1692 	rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1693 	rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1694 	rdma_writel(priv, 0, RDMA_PROD_INDEX);
1695 	rdma_writel(priv, 0, RDMA_CONS_INDEX);
1696 	rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1697 			  RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1698 	/* Operate the queue in ring mode */
1699 	rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1700 	rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1701 	rdma_writel(priv, 0, RDMA_END_ADDR_HI);
1702 	rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
1703 
1704 	netif_dbg(priv, hw, priv->netdev,
1705 		  "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1706 		  priv->num_rx_bds, priv->rx_bds);
1707 
1708 	return 0;
1709 }
1710 
bcm_sysport_fini_rx_ring(struct bcm_sysport_priv * priv)1711 static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1712 {
1713 	struct bcm_sysport_cb *cb;
1714 	unsigned int i;
1715 	u32 reg;
1716 
1717 	/* Caller should ensure RDMA is disabled */
1718 	reg = rdma_readl(priv, RDMA_STATUS);
1719 	if (!(reg & RDMA_DISABLED))
1720 		netdev_warn(priv->netdev, "RDMA not stopped!\n");
1721 
1722 	for (i = 0; i < priv->num_rx_bds; i++) {
1723 		cb = &priv->rx_cbs[i];
1724 		if (dma_unmap_addr(cb, dma_addr))
1725 			dma_unmap_single(&priv->pdev->dev,
1726 					 dma_unmap_addr(cb, dma_addr),
1727 					 RX_BUF_LENGTH, DMA_FROM_DEVICE);
1728 		bcm_sysport_free_cb(cb);
1729 	}
1730 
1731 	kfree(priv->rx_cbs);
1732 	priv->rx_cbs = NULL;
1733 
1734 	netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1735 }
1736 
bcm_sysport_set_rx_mode(struct net_device * dev)1737 static void bcm_sysport_set_rx_mode(struct net_device *dev)
1738 {
1739 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1740 	u32 reg;
1741 
1742 	if (priv->is_lite)
1743 		return;
1744 
1745 	reg = umac_readl(priv, UMAC_CMD);
1746 	if (dev->flags & IFF_PROMISC)
1747 		reg |= CMD_PROMISC;
1748 	else
1749 		reg &= ~CMD_PROMISC;
1750 	umac_writel(priv, reg, UMAC_CMD);
1751 
1752 	/* No support for ALLMULTI */
1753 	if (dev->flags & IFF_ALLMULTI)
1754 		return;
1755 }
1756 
umac_enable_set(struct bcm_sysport_priv * priv,u32 mask,unsigned int enable)1757 static inline void umac_enable_set(struct bcm_sysport_priv *priv,
1758 				   u32 mask, unsigned int enable)
1759 {
1760 	u32 reg;
1761 
1762 	if (!priv->is_lite) {
1763 		reg = umac_readl(priv, UMAC_CMD);
1764 		if (enable)
1765 			reg |= mask;
1766 		else
1767 			reg &= ~mask;
1768 		umac_writel(priv, reg, UMAC_CMD);
1769 	} else {
1770 		reg = gib_readl(priv, GIB_CONTROL);
1771 		if (enable)
1772 			reg |= mask;
1773 		else
1774 			reg &= ~mask;
1775 		gib_writel(priv, reg, GIB_CONTROL);
1776 	}
1777 
1778 	/* UniMAC stops on a packet boundary, wait for a full-sized packet
1779 	 * to be processed (1 msec).
1780 	 */
1781 	if (enable == 0)
1782 		usleep_range(1000, 2000);
1783 }
1784 
umac_reset(struct bcm_sysport_priv * priv)1785 static inline void umac_reset(struct bcm_sysport_priv *priv)
1786 {
1787 	u32 reg;
1788 
1789 	if (priv->is_lite)
1790 		return;
1791 
1792 	reg = umac_readl(priv, UMAC_CMD);
1793 	reg |= CMD_SW_RESET;
1794 	umac_writel(priv, reg, UMAC_CMD);
1795 	udelay(10);
1796 	reg = umac_readl(priv, UMAC_CMD);
1797 	reg &= ~CMD_SW_RESET;
1798 	umac_writel(priv, reg, UMAC_CMD);
1799 }
1800 
umac_set_hw_addr(struct bcm_sysport_priv * priv,const unsigned char * addr)1801 static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
1802 			     const unsigned char *addr)
1803 {
1804 	u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1805 		    addr[3];
1806 	u32 mac1 = (addr[4] << 8) | addr[5];
1807 
1808 	if (!priv->is_lite) {
1809 		umac_writel(priv, mac0, UMAC_MAC0);
1810 		umac_writel(priv, mac1, UMAC_MAC1);
1811 	} else {
1812 		gib_writel(priv, mac0, GIB_MAC0);
1813 		gib_writel(priv, mac1, GIB_MAC1);
1814 	}
1815 }
1816 
topctrl_flush(struct bcm_sysport_priv * priv)1817 static void topctrl_flush(struct bcm_sysport_priv *priv)
1818 {
1819 	topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1820 	topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1821 	mdelay(1);
1822 	topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1823 	topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1824 }
1825 
bcm_sysport_change_mac(struct net_device * dev,void * p)1826 static int bcm_sysport_change_mac(struct net_device *dev, void *p)
1827 {
1828 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1829 	struct sockaddr *addr = p;
1830 
1831 	if (!is_valid_ether_addr(addr->sa_data))
1832 		return -EINVAL;
1833 
1834 	eth_hw_addr_set(dev, addr->sa_data);
1835 
1836 	/* interface is disabled, changes to MAC will be reflected on next
1837 	 * open call
1838 	 */
1839 	if (!netif_running(dev))
1840 		return 0;
1841 
1842 	umac_set_hw_addr(priv, dev->dev_addr);
1843 
1844 	return 0;
1845 }
1846 
bcm_sysport_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)1847 static void bcm_sysport_get_stats64(struct net_device *dev,
1848 				    struct rtnl_link_stats64 *stats)
1849 {
1850 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1851 	struct bcm_sysport_stats64 *stats64 = &priv->stats64;
1852 	unsigned int start;
1853 
1854 	netdev_stats_to_stats64(stats, &dev->stats);
1855 
1856 	bcm_sysport_update_tx_stats(priv, &stats->tx_bytes,
1857 				    &stats->tx_packets);
1858 
1859 	do {
1860 		start = u64_stats_fetch_begin(&priv->syncp);
1861 		stats->rx_packets = stats64->rx_packets;
1862 		stats->rx_bytes = stats64->rx_bytes;
1863 	} while (u64_stats_fetch_retry(&priv->syncp, start));
1864 }
1865 
bcm_sysport_netif_start(struct net_device * dev)1866 static void bcm_sysport_netif_start(struct net_device *dev)
1867 {
1868 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1869 
1870 	/* Enable NAPI */
1871 	bcm_sysport_init_dim(priv, bcm_sysport_dim_work);
1872 	bcm_sysport_init_rx_coalesce(priv);
1873 	napi_enable(&priv->napi);
1874 
1875 	/* Enable RX interrupt and TX ring full interrupt */
1876 	intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1877 
1878 	phy_start(dev->phydev);
1879 
1880 	/* Enable TX interrupts for the TXQs */
1881 	if (!priv->is_lite)
1882 		intrl2_1_mask_clear(priv, 0xffffffff);
1883 	else
1884 		intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
1885 }
1886 
rbuf_init(struct bcm_sysport_priv * priv)1887 static void rbuf_init(struct bcm_sysport_priv *priv)
1888 {
1889 	u32 reg;
1890 
1891 	reg = rbuf_readl(priv, RBUF_CONTROL);
1892 	reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
1893 	/* Set a correct RSB format on SYSTEMPORT Lite */
1894 	if (priv->is_lite)
1895 		reg &= ~RBUF_RSB_SWAP1;
1896 
1897 	/* Set a correct RSB format based on host endian */
1898 	if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
1899 		reg |= RBUF_RSB_SWAP0;
1900 	else
1901 		reg &= ~RBUF_RSB_SWAP0;
1902 	rbuf_writel(priv, reg, RBUF_CONTROL);
1903 }
1904 
bcm_sysport_mask_all_intrs(struct bcm_sysport_priv * priv)1905 static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
1906 {
1907 	intrl2_0_mask_set(priv, 0xffffffff);
1908 	intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1909 	if (!priv->is_lite) {
1910 		intrl2_1_mask_set(priv, 0xffffffff);
1911 		intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1912 	}
1913 }
1914 
gib_set_pad_extension(struct bcm_sysport_priv * priv)1915 static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
1916 {
1917 	u32 reg;
1918 
1919 	reg = gib_readl(priv, GIB_CONTROL);
1920 	/* Include Broadcom tag in pad extension and fix up IPG_LENGTH */
1921 	if (netdev_uses_dsa(priv->netdev)) {
1922 		reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
1923 		reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
1924 	}
1925 	reg &= ~(GIB_IPG_LEN_MASK << GIB_IPG_LEN_SHIFT);
1926 	reg |= 12 << GIB_IPG_LEN_SHIFT;
1927 	gib_writel(priv, reg, GIB_CONTROL);
1928 }
1929 
bcm_sysport_open(struct net_device * dev)1930 static int bcm_sysport_open(struct net_device *dev)
1931 {
1932 	struct bcm_sysport_priv *priv = netdev_priv(dev);
1933 	struct phy_device *phydev;
1934 	unsigned int i;
1935 	int ret;
1936 
1937 	ret = clk_prepare_enable(priv->clk);
1938 	if (ret) {
1939 		netdev_err(dev, "could not enable priv clock\n");
1940 		return ret;
1941 	}
1942 
1943 	/* Reset UniMAC */
1944 	umac_reset(priv);
1945 
1946 	/* Flush TX and RX FIFOs at TOPCTRL level */
1947 	topctrl_flush(priv);
1948 
1949 	/* Disable the UniMAC RX/TX */
1950 	umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
1951 
1952 	/* Enable RBUF 2bytes alignment and Receive Status Block */
1953 	rbuf_init(priv);
1954 
1955 	/* Set maximum frame length */
1956 	if (!priv->is_lite)
1957 		umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1958 	else
1959 		gib_set_pad_extension(priv);
1960 
1961 	/* Apply features again in case we changed them while interface was
1962 	 * down
1963 	 */
1964 	bcm_sysport_set_features(dev, dev->features);
1965 
1966 	/* Set MAC address */
1967 	umac_set_hw_addr(priv, dev->dev_addr);
1968 
1969 	phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1970 				0, priv->phy_interface);
1971 	if (!phydev) {
1972 		netdev_err(dev, "could not attach to PHY\n");
1973 		ret = -ENODEV;
1974 		goto out_clk_disable;
1975 	}
1976 
1977 	/* Indicate that the MAC is responsible for PHY PM */
1978 	phydev->mac_managed_pm = true;
1979 
1980 	/* Reset house keeping link status */
1981 	priv->old_duplex = -1;
1982 	priv->old_link = -1;
1983 	priv->old_pause = -1;
1984 
1985 	/* mask all interrupts and request them */
1986 	bcm_sysport_mask_all_intrs(priv);
1987 
1988 	ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1989 	if (ret) {
1990 		netdev_err(dev, "failed to request RX interrupt\n");
1991 		goto out_phy_disconnect;
1992 	}
1993 
1994 	if (!priv->is_lite) {
1995 		ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
1996 				  dev->name, dev);
1997 		if (ret) {
1998 			netdev_err(dev, "failed to request TX interrupt\n");
1999 			goto out_free_irq0;
2000 		}
2001 	}
2002 
2003 	/* Initialize both hardware and software ring */
2004 	spin_lock_init(&priv->desc_lock);
2005 	for (i = 0; i < dev->num_tx_queues; i++) {
2006 		ret = bcm_sysport_init_tx_ring(priv, i);
2007 		if (ret) {
2008 			netdev_err(dev, "failed to initialize TX ring %d\n",
2009 				   i);
2010 			goto out_free_tx_ring;
2011 		}
2012 	}
2013 
2014 	/* Initialize linked-list */
2015 	tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
2016 
2017 	/* Initialize RX ring */
2018 	ret = bcm_sysport_init_rx_ring(priv);
2019 	if (ret) {
2020 		netdev_err(dev, "failed to initialize RX ring\n");
2021 		goto out_free_rx_ring;
2022 	}
2023 
2024 	/* Turn on RDMA */
2025 	ret = rdma_enable_set(priv, 1);
2026 	if (ret)
2027 		goto out_free_rx_ring;
2028 
2029 	/* Turn on TDMA */
2030 	ret = tdma_enable_set(priv, 1);
2031 	if (ret)
2032 		goto out_clear_rx_int;
2033 
2034 	/* Turn on UniMAC TX/RX */
2035 	umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
2036 
2037 	bcm_sysport_netif_start(dev);
2038 
2039 	netif_tx_start_all_queues(dev);
2040 
2041 	return 0;
2042 
2043 out_clear_rx_int:
2044 	intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
2045 out_free_rx_ring:
2046 	bcm_sysport_fini_rx_ring(priv);
2047 out_free_tx_ring:
2048 	for (i = 0; i < dev->num_tx_queues; i++)
2049 		bcm_sysport_fini_tx_ring(priv, i);
2050 	if (!priv->is_lite)
2051 		free_irq(priv->irq1, dev);
2052 out_free_irq0:
2053 	free_irq(priv->irq0, dev);
2054 out_phy_disconnect:
2055 	phy_disconnect(phydev);
2056 out_clk_disable:
2057 	clk_disable_unprepare(priv->clk);
2058 	return ret;
2059 }
2060 
bcm_sysport_netif_stop(struct net_device * dev)2061 static void bcm_sysport_netif_stop(struct net_device *dev)
2062 {
2063 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2064 
2065 	/* stop all software from updating hardware */
2066 	netif_tx_disable(dev);
2067 	napi_disable(&priv->napi);
2068 	cancel_work_sync(&priv->dim.dim.work);
2069 	phy_stop(dev->phydev);
2070 
2071 	/* mask all interrupts */
2072 	bcm_sysport_mask_all_intrs(priv);
2073 }
2074 
bcm_sysport_stop(struct net_device * dev)2075 static int bcm_sysport_stop(struct net_device *dev)
2076 {
2077 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2078 	unsigned int i;
2079 	int ret;
2080 
2081 	bcm_sysport_netif_stop(dev);
2082 
2083 	/* Disable UniMAC RX */
2084 	umac_enable_set(priv, CMD_RX_EN, 0);
2085 
2086 	ret = tdma_enable_set(priv, 0);
2087 	if (ret) {
2088 		netdev_err(dev, "timeout disabling RDMA\n");
2089 		return ret;
2090 	}
2091 
2092 	/* Wait for a maximum packet size to be drained */
2093 	usleep_range(2000, 3000);
2094 
2095 	ret = rdma_enable_set(priv, 0);
2096 	if (ret) {
2097 		netdev_err(dev, "timeout disabling TDMA\n");
2098 		return ret;
2099 	}
2100 
2101 	/* Disable UniMAC TX */
2102 	umac_enable_set(priv, CMD_TX_EN, 0);
2103 
2104 	/* Free RX/TX rings SW structures */
2105 	for (i = 0; i < dev->num_tx_queues; i++)
2106 		bcm_sysport_fini_tx_ring(priv, i);
2107 	bcm_sysport_fini_rx_ring(priv);
2108 
2109 	free_irq(priv->irq0, dev);
2110 	if (!priv->is_lite)
2111 		free_irq(priv->irq1, dev);
2112 
2113 	/* Disconnect from PHY */
2114 	phy_disconnect(dev->phydev);
2115 
2116 	clk_disable_unprepare(priv->clk);
2117 
2118 	return 0;
2119 }
2120 
bcm_sysport_rule_find(struct bcm_sysport_priv * priv,u64 location)2121 static int bcm_sysport_rule_find(struct bcm_sysport_priv *priv,
2122 				 u64 location)
2123 {
2124 	unsigned int index;
2125 	u32 reg;
2126 
2127 	for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
2128 		reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
2129 		reg >>= RXCHK_BRCM_TAG_CID_SHIFT;
2130 		reg &= RXCHK_BRCM_TAG_CID_MASK;
2131 		if (reg == location)
2132 			return index;
2133 	}
2134 
2135 	return -EINVAL;
2136 }
2137 
bcm_sysport_rule_get(struct bcm_sysport_priv * priv,struct ethtool_rxnfc * nfc)2138 static int bcm_sysport_rule_get(struct bcm_sysport_priv *priv,
2139 				struct ethtool_rxnfc *nfc)
2140 {
2141 	int index;
2142 
2143 	/* This is not a rule that we know about */
2144 	index = bcm_sysport_rule_find(priv, nfc->fs.location);
2145 	if (index < 0)
2146 		return -EOPNOTSUPP;
2147 
2148 	nfc->fs.ring_cookie = RX_CLS_FLOW_WAKE;
2149 
2150 	return 0;
2151 }
2152 
bcm_sysport_rule_set(struct bcm_sysport_priv * priv,struct ethtool_rxnfc * nfc)2153 static int bcm_sysport_rule_set(struct bcm_sysport_priv *priv,
2154 				struct ethtool_rxnfc *nfc)
2155 {
2156 	unsigned int index;
2157 	u32 reg;
2158 
2159 	/* We cannot match locations greater than what the classification ID
2160 	 * permits (256 entries)
2161 	 */
2162 	if (nfc->fs.location > RXCHK_BRCM_TAG_CID_MASK)
2163 		return -E2BIG;
2164 
2165 	/* We cannot support flows that are not destined for a wake-up */
2166 	if (nfc->fs.ring_cookie != RX_CLS_FLOW_WAKE)
2167 		return -EOPNOTSUPP;
2168 
2169 	index = find_first_zero_bit(priv->filters, RXCHK_BRCM_TAG_MAX);
2170 	if (index >= RXCHK_BRCM_TAG_MAX)
2171 		/* All filters are already in use, we cannot match more rules */
2172 		return -ENOSPC;
2173 
2174 	/* Location is the classification ID, and index is the position
2175 	 * within one of our 8 possible filters to be programmed
2176 	 */
2177 	reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
2178 	reg &= ~(RXCHK_BRCM_TAG_CID_MASK << RXCHK_BRCM_TAG_CID_SHIFT);
2179 	reg |= nfc->fs.location << RXCHK_BRCM_TAG_CID_SHIFT;
2180 	rxchk_writel(priv, reg, RXCHK_BRCM_TAG(index));
2181 	rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
2182 
2183 	priv->filters_loc[index] = nfc->fs.location;
2184 	set_bit(index, priv->filters);
2185 
2186 	return 0;
2187 }
2188 
bcm_sysport_rule_del(struct bcm_sysport_priv * priv,u64 location)2189 static int bcm_sysport_rule_del(struct bcm_sysport_priv *priv,
2190 				u64 location)
2191 {
2192 	int index;
2193 
2194 	/* This is not a rule that we know about */
2195 	index = bcm_sysport_rule_find(priv, location);
2196 	if (index < 0)
2197 		return -EOPNOTSUPP;
2198 
2199 	/* No need to disable this filter if it was enabled, this will
2200 	 * be taken care of during suspend time by bcm_sysport_suspend_to_wol
2201 	 */
2202 	clear_bit(index, priv->filters);
2203 	priv->filters_loc[index] = 0;
2204 
2205 	return 0;
2206 }
2207 
bcm_sysport_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc,u32 * rule_locs)2208 static int bcm_sysport_get_rxnfc(struct net_device *dev,
2209 				 struct ethtool_rxnfc *nfc, u32 *rule_locs)
2210 {
2211 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2212 	int ret = -EOPNOTSUPP;
2213 
2214 	switch (nfc->cmd) {
2215 	case ETHTOOL_GRXCLSRULE:
2216 		ret = bcm_sysport_rule_get(priv, nfc);
2217 		break;
2218 	default:
2219 		break;
2220 	}
2221 
2222 	return ret;
2223 }
2224 
bcm_sysport_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc)2225 static int bcm_sysport_set_rxnfc(struct net_device *dev,
2226 				 struct ethtool_rxnfc *nfc)
2227 {
2228 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2229 	int ret = -EOPNOTSUPP;
2230 
2231 	switch (nfc->cmd) {
2232 	case ETHTOOL_SRXCLSRLINS:
2233 		ret = bcm_sysport_rule_set(priv, nfc);
2234 		break;
2235 	case ETHTOOL_SRXCLSRLDEL:
2236 		ret = bcm_sysport_rule_del(priv, nfc->fs.location);
2237 		break;
2238 	default:
2239 		break;
2240 	}
2241 
2242 	return ret;
2243 }
2244 
2245 static const struct ethtool_ops bcm_sysport_ethtool_ops = {
2246 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2247 				     ETHTOOL_COALESCE_MAX_FRAMES |
2248 				     ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
2249 	.get_drvinfo		= bcm_sysport_get_drvinfo,
2250 	.get_msglevel		= bcm_sysport_get_msglvl,
2251 	.set_msglevel		= bcm_sysport_set_msglvl,
2252 	.get_link		= ethtool_op_get_link,
2253 	.get_strings		= bcm_sysport_get_strings,
2254 	.get_ethtool_stats	= bcm_sysport_get_stats,
2255 	.get_sset_count		= bcm_sysport_get_sset_count,
2256 	.get_wol		= bcm_sysport_get_wol,
2257 	.set_wol		= bcm_sysport_set_wol,
2258 	.get_coalesce		= bcm_sysport_get_coalesce,
2259 	.set_coalesce		= bcm_sysport_set_coalesce,
2260 	.get_link_ksettings     = phy_ethtool_get_link_ksettings,
2261 	.set_link_ksettings     = phy_ethtool_set_link_ksettings,
2262 	.get_rxnfc		= bcm_sysport_get_rxnfc,
2263 	.set_rxnfc		= bcm_sysport_set_rxnfc,
2264 };
2265 
bcm_sysport_select_queue(struct net_device * dev,struct sk_buff * skb,struct net_device * sb_dev)2266 static u16 bcm_sysport_select_queue(struct net_device *dev, struct sk_buff *skb,
2267 				    struct net_device *sb_dev)
2268 {
2269 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2270 	u16 queue = skb_get_queue_mapping(skb);
2271 	struct bcm_sysport_tx_ring *tx_ring;
2272 	unsigned int q, port;
2273 
2274 	if (!netdev_uses_dsa(dev))
2275 		return netdev_pick_tx(dev, skb, NULL);
2276 
2277 	/* DSA tagging layer will have configured the correct queue */
2278 	q = BRCM_TAG_GET_QUEUE(queue);
2279 	port = BRCM_TAG_GET_PORT(queue);
2280 	tx_ring = priv->ring_map[q + port * priv->per_port_num_tx_queues];
2281 
2282 	if (unlikely(!tx_ring))
2283 		return netdev_pick_tx(dev, skb, NULL);
2284 
2285 	return tx_ring->index;
2286 }
2287 
2288 static const struct net_device_ops bcm_sysport_netdev_ops = {
2289 	.ndo_start_xmit		= bcm_sysport_xmit,
2290 	.ndo_tx_timeout		= bcm_sysport_tx_timeout,
2291 	.ndo_open		= bcm_sysport_open,
2292 	.ndo_stop		= bcm_sysport_stop,
2293 	.ndo_set_features	= bcm_sysport_set_features,
2294 	.ndo_set_rx_mode	= bcm_sysport_set_rx_mode,
2295 	.ndo_set_mac_address	= bcm_sysport_change_mac,
2296 #ifdef CONFIG_NET_POLL_CONTROLLER
2297 	.ndo_poll_controller	= bcm_sysport_poll_controller,
2298 #endif
2299 	.ndo_get_stats64	= bcm_sysport_get_stats64,
2300 	.ndo_select_queue	= bcm_sysport_select_queue,
2301 };
2302 
bcm_sysport_map_queues(struct net_device * dev,struct net_device * slave_dev)2303 static int bcm_sysport_map_queues(struct net_device *dev,
2304 				  struct net_device *slave_dev)
2305 {
2306 	struct dsa_port *dp = dsa_port_from_netdev(slave_dev);
2307 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2308 	struct bcm_sysport_tx_ring *ring;
2309 	unsigned int num_tx_queues;
2310 	unsigned int q, qp, port;
2311 
2312 	/* We can't be setting up queue inspection for non directly attached
2313 	 * switches
2314 	 */
2315 	if (dp->ds->index)
2316 		return 0;
2317 
2318 	port = dp->index;
2319 
2320 	/* On SYSTEMPORT Lite we have twice as less queues, so we cannot do a
2321 	 * 1:1 mapping, we can only do a 2:1 mapping. By reducing the number of
2322 	 * per-port (slave_dev) network devices queue, we achieve just that.
2323 	 * This need to happen now before any slave network device is used such
2324 	 * it accurately reflects the number of real TX queues.
2325 	 */
2326 	if (priv->is_lite)
2327 		netif_set_real_num_tx_queues(slave_dev,
2328 					     slave_dev->num_tx_queues / 2);
2329 
2330 	num_tx_queues = slave_dev->real_num_tx_queues;
2331 
2332 	if (priv->per_port_num_tx_queues &&
2333 	    priv->per_port_num_tx_queues != num_tx_queues)
2334 		netdev_warn(slave_dev, "asymmetric number of per-port queues\n");
2335 
2336 	priv->per_port_num_tx_queues = num_tx_queues;
2337 
2338 	for (q = 0, qp = 0; q < dev->num_tx_queues && qp < num_tx_queues;
2339 	     q++) {
2340 		ring = &priv->tx_rings[q];
2341 
2342 		if (ring->inspect)
2343 			continue;
2344 
2345 		/* Just remember the mapping actual programming done
2346 		 * during bcm_sysport_init_tx_ring
2347 		 */
2348 		ring->switch_queue = qp;
2349 		ring->switch_port = port;
2350 		ring->inspect = true;
2351 		priv->ring_map[qp + port * num_tx_queues] = ring;
2352 		qp++;
2353 	}
2354 
2355 	return 0;
2356 }
2357 
bcm_sysport_unmap_queues(struct net_device * dev,struct net_device * slave_dev)2358 static int bcm_sysport_unmap_queues(struct net_device *dev,
2359 				    struct net_device *slave_dev)
2360 {
2361 	struct dsa_port *dp = dsa_port_from_netdev(slave_dev);
2362 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2363 	struct bcm_sysport_tx_ring *ring;
2364 	unsigned int num_tx_queues;
2365 	unsigned int q, qp, port;
2366 
2367 	port = dp->index;
2368 
2369 	num_tx_queues = slave_dev->real_num_tx_queues;
2370 
2371 	for (q = 0; q < dev->num_tx_queues; q++) {
2372 		ring = &priv->tx_rings[q];
2373 
2374 		if (ring->switch_port != port)
2375 			continue;
2376 
2377 		if (!ring->inspect)
2378 			continue;
2379 
2380 		ring->inspect = false;
2381 		qp = ring->switch_queue;
2382 		priv->ring_map[qp + port * num_tx_queues] = NULL;
2383 	}
2384 
2385 	return 0;
2386 }
2387 
bcm_sysport_netdevice_event(struct notifier_block * nb,unsigned long event,void * ptr)2388 static int bcm_sysport_netdevice_event(struct notifier_block *nb,
2389 				       unsigned long event, void *ptr)
2390 {
2391 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2392 	struct netdev_notifier_changeupper_info *info = ptr;
2393 	struct bcm_sysport_priv *priv;
2394 	int ret = 0;
2395 
2396 	priv = container_of(nb, struct bcm_sysport_priv, netdev_notifier);
2397 	if (priv->netdev != dev)
2398 		return NOTIFY_DONE;
2399 
2400 	switch (event) {
2401 	case NETDEV_CHANGEUPPER:
2402 		if (dev->netdev_ops != &bcm_sysport_netdev_ops)
2403 			return NOTIFY_DONE;
2404 
2405 		if (!dsa_user_dev_check(info->upper_dev))
2406 			return NOTIFY_DONE;
2407 
2408 		if (info->linking)
2409 			ret = bcm_sysport_map_queues(dev, info->upper_dev);
2410 		else
2411 			ret = bcm_sysport_unmap_queues(dev, info->upper_dev);
2412 		break;
2413 	}
2414 
2415 	return notifier_from_errno(ret);
2416 }
2417 
2418 #define REV_FMT	"v%2x.%02x"
2419 
2420 static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
2421 	[SYSTEMPORT] = {
2422 		.is_lite = false,
2423 		.num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
2424 	},
2425 	[SYSTEMPORT_LITE] = {
2426 		.is_lite = true,
2427 		.num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
2428 	},
2429 };
2430 
2431 static const struct of_device_id bcm_sysport_of_match[] = {
2432 	{ .compatible = "brcm,systemportlite-v1.00",
2433 	  .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
2434 	{ .compatible = "brcm,systemport-v1.00",
2435 	  .data = &bcm_sysport_params[SYSTEMPORT] },
2436 	{ .compatible = "brcm,systemport",
2437 	  .data = &bcm_sysport_params[SYSTEMPORT] },
2438 	{ /* sentinel */ }
2439 };
2440 MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
2441 
bcm_sysport_probe(struct platform_device * pdev)2442 static int bcm_sysport_probe(struct platform_device *pdev)
2443 {
2444 	const struct bcm_sysport_hw_params *params;
2445 	const struct of_device_id *of_id = NULL;
2446 	struct bcm_sysport_priv *priv;
2447 	struct device_node *dn;
2448 	struct net_device *dev;
2449 	u32 txq, rxq;
2450 	int ret;
2451 
2452 	dn = pdev->dev.of_node;
2453 	of_id = of_match_node(bcm_sysport_of_match, dn);
2454 	if (!of_id || !of_id->data)
2455 		return -EINVAL;
2456 
2457 	ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
2458 	if (ret)
2459 		ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2460 	if (ret) {
2461 		dev_err(&pdev->dev, "unable to set DMA mask: %d\n", ret);
2462 		return ret;
2463 	}
2464 
2465 	/* Fairly quickly we need to know the type of adapter we have */
2466 	params = of_id->data;
2467 
2468 	/* Read the Transmit/Receive Queue properties */
2469 	if (of_property_read_u32(dn, "systemport,num-txq", &txq))
2470 		txq = TDMA_NUM_RINGS;
2471 	if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
2472 		rxq = 1;
2473 
2474 	/* Sanity check the number of transmit queues */
2475 	if (!txq || txq > TDMA_NUM_RINGS)
2476 		return -EINVAL;
2477 
2478 	dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
2479 	if (!dev)
2480 		return -ENOMEM;
2481 
2482 	/* Initialize private members */
2483 	priv = netdev_priv(dev);
2484 
2485 	priv->clk = devm_clk_get_optional(&pdev->dev, "sw_sysport");
2486 	if (IS_ERR(priv->clk)) {
2487 		ret = PTR_ERR(priv->clk);
2488 		goto err_free_netdev;
2489 	}
2490 
2491 	/* Allocate number of TX rings */
2492 	priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
2493 				      sizeof(struct bcm_sysport_tx_ring),
2494 				      GFP_KERNEL);
2495 	if (!priv->tx_rings) {
2496 		ret = -ENOMEM;
2497 		goto err_free_netdev;
2498 	}
2499 
2500 	priv->is_lite = params->is_lite;
2501 	priv->num_rx_desc_words = params->num_rx_desc_words;
2502 
2503 	priv->irq0 = platform_get_irq(pdev, 0);
2504 	if (!priv->is_lite) {
2505 		priv->irq1 = platform_get_irq(pdev, 1);
2506 		priv->wol_irq = platform_get_irq_optional(pdev, 2);
2507 	} else {
2508 		priv->wol_irq = platform_get_irq_optional(pdev, 1);
2509 	}
2510 	if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
2511 		ret = -EINVAL;
2512 		goto err_free_netdev;
2513 	}
2514 
2515 	priv->base = devm_platform_ioremap_resource(pdev, 0);
2516 	if (IS_ERR(priv->base)) {
2517 		ret = PTR_ERR(priv->base);
2518 		goto err_free_netdev;
2519 	}
2520 
2521 	priv->netdev = dev;
2522 	priv->pdev = pdev;
2523 
2524 	ret = of_get_phy_mode(dn, &priv->phy_interface);
2525 	/* Default to GMII interface mode */
2526 	if (ret)
2527 		priv->phy_interface = PHY_INTERFACE_MODE_GMII;
2528 
2529 	/* In the case of a fixed PHY, the DT node associated
2530 	 * to the PHY is the Ethernet MAC DT node.
2531 	 */
2532 	if (of_phy_is_fixed_link(dn)) {
2533 		ret = of_phy_register_fixed_link(dn);
2534 		if (ret) {
2535 			dev_err(&pdev->dev, "failed to register fixed PHY\n");
2536 			goto err_free_netdev;
2537 		}
2538 
2539 		priv->phy_dn = dn;
2540 	}
2541 
2542 	/* Initialize netdevice members */
2543 	ret = of_get_ethdev_address(dn, dev);
2544 	if (ret) {
2545 		dev_warn(&pdev->dev, "using random Ethernet MAC\n");
2546 		eth_hw_addr_random(dev);
2547 	}
2548 
2549 	SET_NETDEV_DEV(dev, &pdev->dev);
2550 	dev_set_drvdata(&pdev->dev, dev);
2551 	dev->ethtool_ops = &bcm_sysport_ethtool_ops;
2552 	dev->netdev_ops = &bcm_sysport_netdev_ops;
2553 	netif_napi_add(dev, &priv->napi, bcm_sysport_poll);
2554 
2555 	dev->features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
2556 			 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2557 			 NETIF_F_HW_VLAN_CTAG_TX;
2558 	dev->hw_features |= dev->features;
2559 	dev->vlan_features |= dev->features;
2560 	dev->max_mtu = UMAC_MAX_MTU_SIZE;
2561 
2562 	/* Request the WOL interrupt and advertise suspend if available */
2563 	priv->wol_irq_disabled = 1;
2564 	ret = devm_request_irq(&pdev->dev, priv->wol_irq,
2565 			       bcm_sysport_wol_isr, 0, dev->name, priv);
2566 	if (!ret)
2567 		device_set_wakeup_capable(&pdev->dev, 1);
2568 
2569 	priv->wol_clk = devm_clk_get_optional(&pdev->dev, "sw_sysportwol");
2570 	if (IS_ERR(priv->wol_clk)) {
2571 		ret = PTR_ERR(priv->wol_clk);
2572 		goto err_deregister_fixed_link;
2573 	}
2574 
2575 	/* Set the needed headroom once and for all */
2576 	BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
2577 	dev->needed_headroom += sizeof(struct bcm_tsb);
2578 
2579 	/* libphy will adjust the link state accordingly */
2580 	netif_carrier_off(dev);
2581 
2582 	priv->rx_max_coalesced_frames = 1;
2583 	u64_stats_init(&priv->syncp);
2584 
2585 	priv->netdev_notifier.notifier_call = bcm_sysport_netdevice_event;
2586 
2587 	ret = register_netdevice_notifier(&priv->netdev_notifier);
2588 	if (ret) {
2589 		dev_err(&pdev->dev, "failed to register DSA notifier\n");
2590 		goto err_deregister_fixed_link;
2591 	}
2592 
2593 	ret = register_netdev(dev);
2594 	if (ret) {
2595 		dev_err(&pdev->dev, "failed to register net_device\n");
2596 		goto err_deregister_notifier;
2597 	}
2598 
2599 	ret = clk_prepare_enable(priv->clk);
2600 	if (ret) {
2601 		dev_err(&pdev->dev, "could not enable priv clock\n");
2602 		goto err_deregister_netdev;
2603 	}
2604 
2605 	priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
2606 	dev_info(&pdev->dev,
2607 		 "Broadcom SYSTEMPORT%s " REV_FMT
2608 		 " (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
2609 		 priv->is_lite ? " Lite" : "",
2610 		 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
2611 		 priv->irq0, priv->irq1, txq, rxq);
2612 
2613 	clk_disable_unprepare(priv->clk);
2614 
2615 	return 0;
2616 
2617 err_deregister_netdev:
2618 	unregister_netdev(dev);
2619 err_deregister_notifier:
2620 	unregister_netdevice_notifier(&priv->netdev_notifier);
2621 err_deregister_fixed_link:
2622 	if (of_phy_is_fixed_link(dn))
2623 		of_phy_deregister_fixed_link(dn);
2624 err_free_netdev:
2625 	free_netdev(dev);
2626 	return ret;
2627 }
2628 
bcm_sysport_remove(struct platform_device * pdev)2629 static void bcm_sysport_remove(struct platform_device *pdev)
2630 {
2631 	struct net_device *dev = dev_get_drvdata(&pdev->dev);
2632 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2633 	struct device_node *dn = pdev->dev.of_node;
2634 
2635 	/* Not much to do, ndo_close has been called
2636 	 * and we use managed allocations
2637 	 */
2638 	unregister_netdevice_notifier(&priv->netdev_notifier);
2639 	unregister_netdev(dev);
2640 	if (of_phy_is_fixed_link(dn))
2641 		of_phy_deregister_fixed_link(dn);
2642 	free_netdev(dev);
2643 	dev_set_drvdata(&pdev->dev, NULL);
2644 }
2645 
bcm_sysport_suspend_to_wol(struct bcm_sysport_priv * priv)2646 static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
2647 {
2648 	struct net_device *ndev = priv->netdev;
2649 	unsigned int timeout = 1000;
2650 	unsigned int index, i = 0;
2651 	u32 reg;
2652 
2653 	reg = umac_readl(priv, UMAC_MPD_CTRL);
2654 	if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE))
2655 		reg |= MPD_EN;
2656 	reg &= ~PSW_EN;
2657 	if (priv->wolopts & WAKE_MAGICSECURE) {
2658 		/* Program the SecureOn password */
2659 		umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
2660 			    UMAC_PSW_MS);
2661 		umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
2662 			    UMAC_PSW_LS);
2663 		reg |= PSW_EN;
2664 	}
2665 	umac_writel(priv, reg, UMAC_MPD_CTRL);
2666 
2667 	if (priv->wolopts & WAKE_FILTER) {
2668 		/* Turn on ACPI matching to steal packets from RBUF */
2669 		reg = rbuf_readl(priv, RBUF_CONTROL);
2670 		if (priv->is_lite)
2671 			reg |= RBUF_ACPI_EN_LITE;
2672 		else
2673 			reg |= RBUF_ACPI_EN;
2674 		rbuf_writel(priv, reg, RBUF_CONTROL);
2675 
2676 		/* Enable RXCHK, active filters and Broadcom tag matching */
2677 		reg = rxchk_readl(priv, RXCHK_CONTROL);
2678 		reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
2679 			 RXCHK_BRCM_TAG_MATCH_SHIFT);
2680 		for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
2681 			reg |= BIT(RXCHK_BRCM_TAG_MATCH_SHIFT + i);
2682 			i++;
2683 		}
2684 		reg |= RXCHK_EN | RXCHK_BRCM_TAG_EN;
2685 		rxchk_writel(priv, reg, RXCHK_CONTROL);
2686 	}
2687 
2688 	/* Make sure RBUF entered WoL mode as result */
2689 	do {
2690 		reg = rbuf_readl(priv, RBUF_STATUS);
2691 		if (reg & RBUF_WOL_MODE)
2692 			break;
2693 
2694 		udelay(10);
2695 	} while (timeout-- > 0);
2696 
2697 	/* Do not leave the UniMAC RBUF matching only MPD packets */
2698 	if (!timeout) {
2699 		mpd_enable_set(priv, false);
2700 		netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
2701 		return -ETIMEDOUT;
2702 	}
2703 
2704 	/* UniMAC receive needs to be turned on */
2705 	umac_enable_set(priv, CMD_RX_EN, 1);
2706 
2707 	netif_dbg(priv, wol, ndev, "entered WOL mode\n");
2708 
2709 	return 0;
2710 }
2711 
bcm_sysport_suspend(struct device * d)2712 static int __maybe_unused bcm_sysport_suspend(struct device *d)
2713 {
2714 	struct net_device *dev = dev_get_drvdata(d);
2715 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2716 	unsigned int i;
2717 	int ret = 0;
2718 	u32 reg;
2719 
2720 	if (!netif_running(dev))
2721 		return 0;
2722 
2723 	netif_device_detach(dev);
2724 
2725 	bcm_sysport_netif_stop(dev);
2726 
2727 	phy_suspend(dev->phydev);
2728 
2729 	/* Disable UniMAC RX */
2730 	umac_enable_set(priv, CMD_RX_EN, 0);
2731 
2732 	ret = rdma_enable_set(priv, 0);
2733 	if (ret) {
2734 		netdev_err(dev, "RDMA timeout!\n");
2735 		return ret;
2736 	}
2737 
2738 	/* Disable RXCHK if enabled */
2739 	if (priv->rx_chk_en) {
2740 		reg = rxchk_readl(priv, RXCHK_CONTROL);
2741 		reg &= ~RXCHK_EN;
2742 		rxchk_writel(priv, reg, RXCHK_CONTROL);
2743 	}
2744 
2745 	/* Flush RX pipe */
2746 	if (!priv->wolopts)
2747 		topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
2748 
2749 	ret = tdma_enable_set(priv, 0);
2750 	if (ret) {
2751 		netdev_err(dev, "TDMA timeout!\n");
2752 		return ret;
2753 	}
2754 
2755 	/* Wait for a packet boundary */
2756 	usleep_range(2000, 3000);
2757 
2758 	umac_enable_set(priv, CMD_TX_EN, 0);
2759 
2760 	topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
2761 
2762 	/* Free RX/TX rings SW structures */
2763 	for (i = 0; i < dev->num_tx_queues; i++)
2764 		bcm_sysport_fini_tx_ring(priv, i);
2765 	bcm_sysport_fini_rx_ring(priv);
2766 
2767 	/* Get prepared for Wake-on-LAN */
2768 	if (device_may_wakeup(d) && priv->wolopts) {
2769 		clk_prepare_enable(priv->wol_clk);
2770 		ret = bcm_sysport_suspend_to_wol(priv);
2771 	}
2772 
2773 	clk_disable_unprepare(priv->clk);
2774 
2775 	return ret;
2776 }
2777 
bcm_sysport_resume(struct device * d)2778 static int __maybe_unused bcm_sysport_resume(struct device *d)
2779 {
2780 	struct net_device *dev = dev_get_drvdata(d);
2781 	struct bcm_sysport_priv *priv = netdev_priv(dev);
2782 	unsigned int i;
2783 	int ret;
2784 
2785 	if (!netif_running(dev))
2786 		return 0;
2787 
2788 	ret = clk_prepare_enable(priv->clk);
2789 	if (ret) {
2790 		netdev_err(dev, "could not enable priv clock\n");
2791 		return ret;
2792 	}
2793 
2794 	if (priv->wolopts)
2795 		clk_disable_unprepare(priv->wol_clk);
2796 
2797 	umac_reset(priv);
2798 
2799 	/* Disable the UniMAC RX/TX */
2800 	umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
2801 
2802 	/* We may have been suspended and never received a WOL event that
2803 	 * would turn off MPD detection, take care of that now
2804 	 */
2805 	bcm_sysport_resume_from_wol(priv);
2806 
2807 	/* Initialize both hardware and software ring */
2808 	for (i = 0; i < dev->num_tx_queues; i++) {
2809 		ret = bcm_sysport_init_tx_ring(priv, i);
2810 		if (ret) {
2811 			netdev_err(dev, "failed to initialize TX ring %d\n",
2812 				   i);
2813 			goto out_free_tx_rings;
2814 		}
2815 	}
2816 
2817 	/* Initialize linked-list */
2818 	tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
2819 
2820 	/* Initialize RX ring */
2821 	ret = bcm_sysport_init_rx_ring(priv);
2822 	if (ret) {
2823 		netdev_err(dev, "failed to initialize RX ring\n");
2824 		goto out_free_rx_ring;
2825 	}
2826 
2827 	/* RX pipe enable */
2828 	topctrl_writel(priv, 0, RX_FLUSH_CNTL);
2829 
2830 	ret = rdma_enable_set(priv, 1);
2831 	if (ret) {
2832 		netdev_err(dev, "failed to enable RDMA\n");
2833 		goto out_free_rx_ring;
2834 	}
2835 
2836 	/* Restore enabled features */
2837 	bcm_sysport_set_features(dev, dev->features);
2838 
2839 	rbuf_init(priv);
2840 
2841 	/* Set maximum frame length */
2842 	if (!priv->is_lite)
2843 		umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2844 	else
2845 		gib_set_pad_extension(priv);
2846 
2847 	/* Set MAC address */
2848 	umac_set_hw_addr(priv, dev->dev_addr);
2849 
2850 	umac_enable_set(priv, CMD_RX_EN, 1);
2851 
2852 	/* TX pipe enable */
2853 	topctrl_writel(priv, 0, TX_FLUSH_CNTL);
2854 
2855 	umac_enable_set(priv, CMD_TX_EN, 1);
2856 
2857 	ret = tdma_enable_set(priv, 1);
2858 	if (ret) {
2859 		netdev_err(dev, "TDMA timeout!\n");
2860 		goto out_free_rx_ring;
2861 	}
2862 
2863 	phy_resume(dev->phydev);
2864 
2865 	bcm_sysport_netif_start(dev);
2866 
2867 	netif_device_attach(dev);
2868 
2869 	return 0;
2870 
2871 out_free_rx_ring:
2872 	bcm_sysport_fini_rx_ring(priv);
2873 out_free_tx_rings:
2874 	for (i = 0; i < dev->num_tx_queues; i++)
2875 		bcm_sysport_fini_tx_ring(priv, i);
2876 	clk_disable_unprepare(priv->clk);
2877 	return ret;
2878 }
2879 
2880 static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
2881 		bcm_sysport_suspend, bcm_sysport_resume);
2882 
2883 static struct platform_driver bcm_sysport_driver = {
2884 	.probe	= bcm_sysport_probe,
2885 	.remove = bcm_sysport_remove,
2886 	.driver =  {
2887 		.name = "brcm-systemport",
2888 		.of_match_table = bcm_sysport_of_match,
2889 		.pm = &bcm_sysport_pm_ops,
2890 	},
2891 };
2892 module_platform_driver(bcm_sysport_driver);
2893 
2894 MODULE_AUTHOR("Broadcom Corporation");
2895 MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
2896 MODULE_ALIAS("platform:brcm-systemport");
2897 MODULE_LICENSE("GPL");
2898