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