blob: 931751e4f3692ffa00dfa385a68aad34675117a0 [file] [log] [blame]
Florian Fainelli80105be2014-04-24 18:08:57 -07001/*
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>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020025#include <net/dsa.h>
Florian Fainelli80105be2014-04-24 18:08:57 -070026#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{ \
Florian Fainellif1dd1992017-08-29 13:35:15 -070035 u32 reg = readl_relaxed(priv->base + offset + off); \
Florian Fainelli80105be2014-04-24 18:08:57 -070036 return reg; \
37} \
38static inline void name##_writel(struct bcm_sysport_priv *priv, \
39 u32 val, u32 off) \
40{ \
Florian Fainellif1dd1992017-08-29 13:35:15 -070041 writel_relaxed(val, priv->base + offset + off); \
Florian Fainelli80105be2014-04-24 18:08:57 -070042} \
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);
Florian Fainelli44a45242017-01-20 11:08:27 -080047BCM_SYSPORT_IO_MACRO(gib, SYS_PORT_GIB_OFFSET);
Florian Fainelli80105be2014-04-24 18:08:57 -070048BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
Florian Fainelli80105be2014-04-24 18:08:57 -070049BCM_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
Florian Fainelli44a45242017-01-20 11:08:27 -080055/* 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;
Florian Fainellif1dd1992017-08-29 13:35:15 -070062 return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
Florian Fainelli44a45242017-01-20 11:08:27 -080063}
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;
Florian Fainellif1dd1992017-08-29 13:35:15 -070069 writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
Florian Fainelli44a45242017-01-20 11:08:27 -080070}
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
Florian Fainelli80105be2014-04-24 18:08:57 -070084/* 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{ \
Florian Fainelli80105be2014-04-24 18:08:57 -070091 priv->irq##which##_mask &= ~(mask); \
Florian Fainelli9a0a5c42016-08-24 14:21:41 -070092 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
Florian Fainelli80105be2014-04-24 18:08:57 -070093} \
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
Florian Fainellif1dd1992017-08-29 13:35:15 -0700113 writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700114 d + DESC_ADDR_HI_STATUS_LEN);
Florian Fainelli80105be2014-04-24 18:08:57 -0700115#endif
Florian Fainellif1dd1992017-08-29 13:35:15 -0700116 writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
Florian Fainelli80105be2014-04-24 18:08:57 -0700117}
118
119static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700120 struct dma_desc *desc,
121 unsigned int port)
Florian Fainelli80105be2014-04-24 18:08:57 -0700122{
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 */
Florian Fainelli80105be2014-04-24 18:08:57 -0700129static int bcm_sysport_set_rx_csum(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700130 netdev_features_t wanted)
Florian Fainelli80105be2014-04-24 18:08:57 -0700131{
132 struct bcm_sysport_priv *priv = netdev_priv(dev);
133 u32 reg;
134
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700135 priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
Florian Fainelli80105be2014-04-24 18:08:57 -0700136 reg = rxchk_readl(priv, RXCHK_CONTROL);
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700137 if (priv->rx_chk_en)
Florian Fainelli80105be2014-04-24 18:08:57 -0700138 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 */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700145 if (priv->rx_chk_en && priv->crc_fwd)
Florian Fainelli80105be2014-04-24 18:08:57 -0700146 reg |= RXCHK_SKIP_FCS;
147 else
148 reg &= ~RXCHK_SKIP_FCS;
149
Florian Fainellid09d3032014-08-28 15:11:03 -0700150 /* 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
Florian Fainelli80105be2014-04-24 18:08:57 -0700159 rxchk_writel(priv, reg, RXCHK_CONTROL);
160
161 return 0;
162}
163
164static int bcm_sysport_set_tx_csum(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700165 netdev_features_t wanted)
Florian Fainelli80105be2014-04-24 18:08:57 -0700166{
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)
Florian Fainelli44a45242017-01-20 11:08:27 -0800176 reg |= tdma_control_bit(priv, TSB_EN);
Florian Fainelli80105be2014-04-24 18:08:57 -0700177 else
Florian Fainelli44a45242017-01-20 11:08:27 -0800178 reg &= ~tdma_control_bit(priv, TSB_EN);
Florian Fainelli80105be2014-04-24 18:08:57 -0700179 tdma_writel(priv, reg, TDMA_CONTROL);
180
181 return 0;
182}
183
184static int bcm_sysport_set_features(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700185 netdev_features_t features)
Florian Fainelli80105be2014-04-24 18:08:57 -0700186{
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 */
kiki good10377ba2017-08-04 00:07:45 +0100204 STAT_NETDEV64(rx_packets),
205 STAT_NETDEV64(tx_packets),
206 STAT_NETDEV64(rx_bytes),
207 STAT_NETDEV64(tx_bytes),
Florian Fainelli80105be2014-04-24 18:08:57 -0700208 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,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700281 RXCHK_OTHER_DISC_CNTR),
Florian Fainelli80105be2014-04-24 18:08:57 -0700282 /* 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),
Florian Fainelli55ff4ea2015-02-28 18:09:17 -0800285 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),
Florian Fainelli30defeb2017-03-23 10:36:46 -0700288 /* Per TX-queue statistics are dynamically appended */
Florian Fainelli80105be2014-04-24 18:08:57 -0700289};
290
291#define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
292
293static void bcm_sysport_get_drvinfo(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700294 struct ethtool_drvinfo *info)
Florian Fainelli80105be2014-04-24 18:08:57 -0700295{
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));
Florian Fainelli80105be2014-04-24 18:08:57 -0700299}
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
Florian Fainelli44a45242017-01-20 11:08:27 -0800315static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
316{
317 switch (type) {
318 case BCM_SYSPORT_STAT_NETDEV:
kiki good10377ba2017-08-04 00:07:45 +0100319 case BCM_SYSPORT_STAT_NETDEV64:
Florian Fainelli44a45242017-01-20 11:08:27 -0800320 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
Florian Fainelli80105be2014-04-24 18:08:57 -0700329static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
330{
Florian Fainelli44a45242017-01-20 11:08:27 -0800331 struct bcm_sysport_priv *priv = netdev_priv(dev);
332 const struct bcm_sysport_stats *s;
333 unsigned int i, j;
334
Florian Fainelli80105be2014-04-24 18:08:57 -0700335 switch (string_set) {
336 case ETH_SS_STATS:
Florian Fainelli44a45242017-01-20 11:08:27 -0800337 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 }
Florian Fainelli30defeb2017-03-23 10:36:46 -0700344 /* Include per-queue statistics */
345 return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
Florian Fainelli80105be2014-04-24 18:08:57 -0700346 default:
347 return -EOPNOTSUPP;
348 }
349}
350
351static void bcm_sysport_get_strings(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700352 u32 stringset, u8 *data)
Florian Fainelli80105be2014-04-24 18:08:57 -0700353{
Florian Fainelli44a45242017-01-20 11:08:27 -0800354 struct bcm_sysport_priv *priv = netdev_priv(dev);
355 const struct bcm_sysport_stats *s;
Florian Fainelli30defeb2017-03-23 10:36:46 -0700356 char buf[128];
Florian Fainelli44a45242017-01-20 11:08:27 -0800357 int i, j;
Florian Fainelli80105be2014-04-24 18:08:57 -0700358
359 switch (stringset) {
360 case ETH_SS_STATS:
Florian Fainelli44a45242017-01-20 11:08:27 -0800361 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,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700368 ETH_GSTRING_LEN);
Florian Fainelli44a45242017-01-20 11:08:27 -0800369 j++;
Florian Fainelli80105be2014-04-24 18:08:57 -0700370 }
Florian Fainelli30defeb2017-03-23 10:36:46 -0700371
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 }
Florian Fainelli80105be2014-04-24 18:08:57 -0700383 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:
kiki good10377ba2017-08-04 00:07:45 +0100402 case BCM_SYSPORT_STAT_NETDEV64:
Florian Fainelli55ff4ea2015-02-28 18:09:17 -0800403 case BCM_SYSPORT_STAT_SOFT:
Florian Fainelli80105be2014-04-24 18:08:57 -0700404 continue;
405 case BCM_SYSPORT_STAT_MIB_RX:
406 case BCM_SYSPORT_STAT_MIB_TX:
407 case BCM_SYSPORT_STAT_RUNT:
Florian Fainelli44a45242017-01-20 11:08:27 -0800408 if (priv->is_lite)
409 continue;
410
Florian Fainelli80105be2014-04-24 18:08:57 -0700411 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_get_stats(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700436 struct ethtool_stats *stats, u64 *data)
Florian Fainelli80105be2014-04-24 18:08:57 -0700437{
438 struct bcm_sysport_priv *priv = netdev_priv(dev);
kiki good10377ba2017-08-04 00:07:45 +0100439 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
440 struct u64_stats_sync *syncp = &priv->syncp;
Florian Fainelli30defeb2017-03-23 10:36:46 -0700441 struct bcm_sysport_tx_ring *ring;
kiki good10377ba2017-08-04 00:07:45 +0100442 unsigned int start;
Florian Fainelli44a45242017-01-20 11:08:27 -0800443 int i, j;
Florian Fainelli80105be2014-04-24 18:08:57 -0700444
445 if (netif_running(dev))
446 bcm_sysport_update_mib_counters(priv);
447
Florian Fainelli44a45242017-01-20 11:08:27 -0800448 for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700449 const struct bcm_sysport_stats *s;
450 char *p;
451
452 s = &bcm_sysport_gstrings_stats[i];
453 if (s->type == BCM_SYSPORT_STAT_NETDEV)
454 p = (char *)&dev->stats;
kiki good10377ba2017-08-04 00:07:45 +0100455 else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
456 p = (char *)stats64;
Florian Fainelli80105be2014-04-24 18:08:57 -0700457 else
458 p = (char *)priv;
kiki good10377ba2017-08-04 00:07:45 +0100459
Florian Fainelli50ddfba2017-08-08 14:45:09 -0700460 if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
461 continue;
Florian Fainelli80105be2014-04-24 18:08:57 -0700462 p += s->stat_offset;
kiki good10377ba2017-08-04 00:07:45 +0100463
464 if (s->stat_sizeof == sizeof(u64))
465 do {
466 start = u64_stats_fetch_begin_irq(syncp);
467 data[i] = *(u64 *)p;
468 } while (u64_stats_fetch_retry_irq(syncp, start));
469 else
470 data[i] = *(u32 *)p;
Florian Fainelli44a45242017-01-20 11:08:27 -0800471 j++;
Florian Fainelli80105be2014-04-24 18:08:57 -0700472 }
Florian Fainelli30defeb2017-03-23 10:36:46 -0700473
474 /* For SYSTEMPORT Lite since we have holes in our statistics, j would
475 * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
476 * needs to point to how many total statistics we have minus the
477 * number of per TX queue statistics
478 */
479 j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
480 dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
481
482 for (i = 0; i < dev->num_tx_queues; i++) {
483 ring = &priv->tx_rings[i];
484 data[j] = ring->packets;
485 j++;
486 data[j] = ring->bytes;
487 j++;
488 }
Florian Fainelli80105be2014-04-24 18:08:57 -0700489}
490
Florian Fainelli83e82f42014-07-01 21:08:40 -0700491static void bcm_sysport_get_wol(struct net_device *dev,
492 struct ethtool_wolinfo *wol)
493{
494 struct bcm_sysport_priv *priv = netdev_priv(dev);
495 u32 reg;
496
497 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
498 wol->wolopts = priv->wolopts;
499
500 if (!(priv->wolopts & WAKE_MAGICSECURE))
501 return;
502
503 /* Return the programmed SecureOn password */
504 reg = umac_readl(priv, UMAC_PSW_MS);
505 put_unaligned_be16(reg, &wol->sopass[0]);
506 reg = umac_readl(priv, UMAC_PSW_LS);
507 put_unaligned_be32(reg, &wol->sopass[2]);
508}
509
510static int bcm_sysport_set_wol(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700511 struct ethtool_wolinfo *wol)
Florian Fainelli83e82f42014-07-01 21:08:40 -0700512{
513 struct bcm_sysport_priv *priv = netdev_priv(dev);
514 struct device *kdev = &priv->pdev->dev;
515 u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE;
516
517 if (!device_can_wakeup(kdev))
518 return -ENOTSUPP;
519
520 if (wol->wolopts & ~supported)
521 return -EINVAL;
522
523 /* Program the SecureOn password */
524 if (wol->wolopts & WAKE_MAGICSECURE) {
525 umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700526 UMAC_PSW_MS);
Florian Fainelli83e82f42014-07-01 21:08:40 -0700527 umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700528 UMAC_PSW_LS);
Florian Fainelli83e82f42014-07-01 21:08:40 -0700529 }
530
531 /* Flag the device and relevant IRQ as wakeup capable */
532 if (wol->wolopts) {
533 device_set_wakeup_enable(kdev, 1);
Florian Fainelli61b423a2014-10-10 10:51:54 -0700534 if (priv->wol_irq_disabled)
535 enable_irq_wake(priv->wol_irq);
Florian Fainelli83e82f42014-07-01 21:08:40 -0700536 priv->wol_irq_disabled = 0;
537 } else {
538 device_set_wakeup_enable(kdev, 0);
539 /* Avoid unbalanced disable_irq_wake calls */
540 if (!priv->wol_irq_disabled)
541 disable_irq_wake(priv->wol_irq);
542 priv->wol_irq_disabled = 1;
543 }
544
545 priv->wolopts = wol->wolopts;
546
547 return 0;
548}
549
Florian Fainellib1a15e82015-05-11 15:12:41 -0700550static int bcm_sysport_get_coalesce(struct net_device *dev,
551 struct ethtool_coalesce *ec)
552{
553 struct bcm_sysport_priv *priv = netdev_priv(dev);
554 u32 reg;
555
556 reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
557
558 ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
559 ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
560
Florian Fainellid0634862015-05-11 15:12:42 -0700561 reg = rdma_readl(priv, RDMA_MBDONE_INTR);
562
563 ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
564 ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
565
Florian Fainellib1a15e82015-05-11 15:12:41 -0700566 return 0;
567}
568
569static int bcm_sysport_set_coalesce(struct net_device *dev,
570 struct ethtool_coalesce *ec)
571{
572 struct bcm_sysport_priv *priv = netdev_priv(dev);
573 unsigned int i;
574 u32 reg;
575
Florian Fainellid0634862015-05-11 15:12:42 -0700576 /* Base system clock is 125Mhz, DMA timeout is this reference clock
577 * divided by 1024, which yield roughly 8.192 us, our maximum value has
578 * to fit in the RING_TIMEOUT_MASK (16 bits).
Florian Fainellib1a15e82015-05-11 15:12:41 -0700579 */
580 if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
Florian Fainellid0634862015-05-11 15:12:42 -0700581 ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
582 ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
583 ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
Florian Fainellib1a15e82015-05-11 15:12:41 -0700584 return -EINVAL;
585
Florian Fainellid0634862015-05-11 15:12:42 -0700586 if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
587 (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0))
Florian Fainellib1a15e82015-05-11 15:12:41 -0700588 return -EINVAL;
589
590 for (i = 0; i < dev->num_tx_queues; i++) {
591 reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(i));
592 reg &= ~(RING_INTR_THRESH_MASK |
593 RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
594 reg |= ec->tx_max_coalesced_frames;
595 reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
596 RING_TIMEOUT_SHIFT;
597 tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(i));
598 }
599
Florian Fainellid0634862015-05-11 15:12:42 -0700600 reg = rdma_readl(priv, RDMA_MBDONE_INTR);
601 reg &= ~(RDMA_INTR_THRESH_MASK |
602 RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
603 reg |= ec->rx_max_coalesced_frames;
604 reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192) <<
605 RDMA_TIMEOUT_SHIFT;
606 rdma_writel(priv, reg, RDMA_MBDONE_INTR);
607
Florian Fainellib1a15e82015-05-11 15:12:41 -0700608 return 0;
609}
610
Florian Fainelli80105be2014-04-24 18:08:57 -0700611static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
612{
613 dev_kfree_skb_any(cb->skb);
614 cb->skb = NULL;
615 dma_unmap_addr_set(cb, dma_addr, 0);
616}
617
Florian Fainellic73b0182015-05-28 15:24:43 -0700618static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
619 struct bcm_sysport_cb *cb)
Florian Fainelli80105be2014-04-24 18:08:57 -0700620{
621 struct device *kdev = &priv->pdev->dev;
622 struct net_device *ndev = priv->netdev;
Florian Fainellic73b0182015-05-28 15:24:43 -0700623 struct sk_buff *skb, *rx_skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700624 dma_addr_t mapping;
Florian Fainelli80105be2014-04-24 18:08:57 -0700625
Florian Fainellic73b0182015-05-28 15:24:43 -0700626 /* Allocate a new SKB for a new packet */
627 skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
628 if (!skb) {
629 priv->mib.alloc_rx_buff_failed++;
Florian Fainelli80105be2014-04-24 18:08:57 -0700630 netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
Florian Fainellic73b0182015-05-28 15:24:43 -0700631 return NULL;
Florian Fainelli80105be2014-04-24 18:08:57 -0700632 }
633
Florian Fainellic73b0182015-05-28 15:24:43 -0700634 mapping = dma_map_single(kdev, skb->data,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700635 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainellic73b0182015-05-28 15:24:43 -0700636 if (dma_mapping_error(kdev, mapping)) {
Florian Fainelli60b4ea12014-11-19 10:29:55 -0800637 priv->mib.rx_dma_failed++;
Florian Fainellic73b0182015-05-28 15:24:43 -0700638 dev_kfree_skb_any(skb);
Florian Fainelli80105be2014-04-24 18:08:57 -0700639 netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
Florian Fainellic73b0182015-05-28 15:24:43 -0700640 return NULL;
Florian Fainelli80105be2014-04-24 18:08:57 -0700641 }
642
Florian Fainellic73b0182015-05-28 15:24:43 -0700643 /* Grab the current SKB on the ring */
644 rx_skb = cb->skb;
645 if (likely(rx_skb))
646 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
647 RX_BUF_LENGTH, DMA_FROM_DEVICE);
648
649 /* Put the new SKB on the ring */
650 cb->skb = skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700651 dma_unmap_addr_set(cb, dma_addr, mapping);
Florian Fainellibaf387a2015-05-28 15:24:42 -0700652 dma_desc_set_addr(priv, cb->bd_addr, mapping);
Florian Fainelli80105be2014-04-24 18:08:57 -0700653
654 netif_dbg(priv, rx_status, ndev, "RX refill\n");
655
Florian Fainellic73b0182015-05-28 15:24:43 -0700656 /* Return the current SKB to the caller */
657 return rx_skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700658}
659
660static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
661{
662 struct bcm_sysport_cb *cb;
Florian Fainellic73b0182015-05-28 15:24:43 -0700663 struct sk_buff *skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700664 unsigned int i;
665
666 for (i = 0; i < priv->num_rx_bds; i++) {
Florian Fainellibaf387a2015-05-28 15:24:42 -0700667 cb = &priv->rx_cbs[i];
Florian Fainellic73b0182015-05-28 15:24:43 -0700668 skb = bcm_sysport_rx_refill(priv, cb);
669 if (skb)
670 dev_kfree_skb(skb);
671 if (!cb->skb)
672 return -ENOMEM;
Florian Fainelli80105be2014-04-24 18:08:57 -0700673 }
674
Florian Fainellic73b0182015-05-28 15:24:43 -0700675 return 0;
Florian Fainelli80105be2014-04-24 18:08:57 -0700676}
677
678/* Poll the hardware for up to budget packets to process */
679static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
680 unsigned int budget)
681{
kiki good10377ba2017-08-04 00:07:45 +0100682 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
Florian Fainelli80105be2014-04-24 18:08:57 -0700683 struct net_device *ndev = priv->netdev;
684 unsigned int processed = 0, to_process;
685 struct bcm_sysport_cb *cb;
686 struct sk_buff *skb;
687 unsigned int p_index;
688 u16 len, status;
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400689 struct bcm_rsb *rsb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700690
Florian Fainelli6baa7852017-03-23 10:36:47 -0700691 /* Clear status before servicing to reduce spurious interrupts */
692 intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
693
Florian Fainelli44a45242017-01-20 11:08:27 -0800694 /* Determine how much we should process since last call, SYSTEMPORT Lite
695 * groups the producer and consumer indexes into the same 32-bit
696 * which we access using RDMA_CONS_INDEX
697 */
698 if (!priv->is_lite)
699 p_index = rdma_readl(priv, RDMA_PROD_INDEX);
700 else
701 p_index = rdma_readl(priv, RDMA_CONS_INDEX);
Florian Fainelli80105be2014-04-24 18:08:57 -0700702 p_index &= RDMA_PROD_INDEX_MASK;
703
Florian Fainellie9d7af72017-03-23 10:36:48 -0700704 to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
Florian Fainelli80105be2014-04-24 18:08:57 -0700705
706 netif_dbg(priv, rx_status, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700707 "p_index=%d rx_c_index=%d to_process=%d\n",
708 p_index, priv->rx_c_index, to_process);
Florian Fainelli80105be2014-04-24 18:08:57 -0700709
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700710 while ((processed < to_process) && (processed < budget)) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700711 cb = &priv->rx_cbs[priv->rx_read_ptr];
Florian Fainellic73b0182015-05-28 15:24:43 -0700712 skb = bcm_sysport_rx_refill(priv, cb);
Florian Fainellife24ba02014-09-08 11:37:51 -0700713
Florian Fainellife24ba02014-09-08 11:37:51 -0700714
715 /* We do not have a backing SKB, so we do not a corresponding
716 * DMA mapping for this incoming packet since
717 * bcm_sysport_rx_refill always either has both skb and mapping
718 * or none.
719 */
720 if (unlikely(!skb)) {
721 netif_err(priv, rx_err, ndev, "out of memory!\n");
722 ndev->stats.rx_dropped++;
723 ndev->stats.rx_errors++;
Florian Fainellic73b0182015-05-28 15:24:43 -0700724 goto next;
Florian Fainellife24ba02014-09-08 11:37:51 -0700725 }
726
Florian Fainelli80105be2014-04-24 18:08:57 -0700727 /* Extract the Receive Status Block prepended */
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400728 rsb = (struct bcm_rsb *)skb->data;
Florian Fainelli80105be2014-04-24 18:08:57 -0700729 len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
730 status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700731 DESC_STATUS_MASK;
Florian Fainelli80105be2014-04-24 18:08:57 -0700732
Florian Fainelli80105be2014-04-24 18:08:57 -0700733 netif_dbg(priv, rx_status, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700734 "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
735 p_index, priv->rx_c_index, priv->rx_read_ptr,
736 len, status);
Florian Fainelli80105be2014-04-24 18:08:57 -0700737
Florian Fainelli25977ac2015-05-28 15:24:44 -0700738 if (unlikely(len > RX_BUF_LENGTH)) {
739 netif_err(priv, rx_status, ndev, "oversized packet\n");
740 ndev->stats.rx_length_errors++;
741 ndev->stats.rx_errors++;
742 dev_kfree_skb_any(skb);
743 goto next;
744 }
745
Florian Fainelli80105be2014-04-24 18:08:57 -0700746 if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
747 netif_err(priv, rx_status, ndev, "fragmented packet!\n");
748 ndev->stats.rx_dropped++;
749 ndev->stats.rx_errors++;
Florian Fainellic73b0182015-05-28 15:24:43 -0700750 dev_kfree_skb_any(skb);
751 goto next;
Florian Fainelli80105be2014-04-24 18:08:57 -0700752 }
753
754 if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
755 netif_err(priv, rx_err, ndev, "error packet\n");
Florian Fainelliad51c612014-06-05 10:22:16 -0700756 if (status & RX_STATUS_OVFLOW)
Florian Fainelli80105be2014-04-24 18:08:57 -0700757 ndev->stats.rx_over_errors++;
758 ndev->stats.rx_dropped++;
759 ndev->stats.rx_errors++;
Florian Fainellic73b0182015-05-28 15:24:43 -0700760 dev_kfree_skb_any(skb);
761 goto next;
Florian Fainelli80105be2014-04-24 18:08:57 -0700762 }
763
764 skb_put(skb, len);
765
766 /* Hardware validated our checksum */
767 if (likely(status & DESC_L4_CSUM))
768 skb->ip_summed = CHECKSUM_UNNECESSARY;
769
Florian Fainellie0ea05d2014-06-05 10:22:17 -0700770 /* Hardware pre-pends packets with 2bytes before Ethernet
771 * header plus we have the Receive Status Block, strip off all
772 * of this from the SKB.
Florian Fainelli80105be2014-04-24 18:08:57 -0700773 */
774 skb_pull(skb, sizeof(*rsb) + 2);
775 len -= (sizeof(*rsb) + 2);
776
777 /* UniMAC may forward CRC */
778 if (priv->crc_fwd) {
779 skb_trim(skb, len - ETH_FCS_LEN);
780 len -= ETH_FCS_LEN;
781 }
782
783 skb->protocol = eth_type_trans(skb, ndev);
784 ndev->stats.rx_packets++;
785 ndev->stats.rx_bytes += len;
kiki good10377ba2017-08-04 00:07:45 +0100786 u64_stats_update_begin(&priv->syncp);
787 stats64->rx_packets++;
788 stats64->rx_bytes += len;
789 u64_stats_update_end(&priv->syncp);
Florian Fainelli80105be2014-04-24 18:08:57 -0700790
791 napi_gro_receive(&priv->napi, skb);
Florian Fainellic73b0182015-05-28 15:24:43 -0700792next:
793 processed++;
794 priv->rx_read_ptr++;
795
796 if (priv->rx_read_ptr == priv->num_rx_bds)
797 priv->rx_read_ptr = 0;
Florian Fainelli80105be2014-04-24 18:08:57 -0700798 }
799
800 return processed;
801}
802
Florian Fainelli30defeb2017-03-23 10:36:46 -0700803static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700804 struct bcm_sysport_cb *cb,
805 unsigned int *bytes_compl,
806 unsigned int *pkts_compl)
Florian Fainelli80105be2014-04-24 18:08:57 -0700807{
Florian Fainelli30defeb2017-03-23 10:36:46 -0700808 struct bcm_sysport_priv *priv = ring->priv;
Florian Fainelli80105be2014-04-24 18:08:57 -0700809 struct device *kdev = &priv->pdev->dev;
Florian Fainelli80105be2014-04-24 18:08:57 -0700810
811 if (cb->skb) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700812 *bytes_compl += cb->skb->len;
813 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700814 dma_unmap_len(cb, dma_len),
815 DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700816 (*pkts_compl)++;
817 bcm_sysport_free_cb(cb);
818 /* SKB fragment */
819 } else if (dma_unmap_addr(cb, dma_addr)) {
kiki good10377ba2017-08-04 00:07:45 +0100820 *bytes_compl += dma_unmap_len(cb, dma_len);
Florian Fainelli80105be2014-04-24 18:08:57 -0700821 dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700822 dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700823 dma_unmap_addr_set(cb, dma_addr, 0);
824 }
825}
826
827/* Reclaim queued SKBs for transmission completion, lockless version */
828static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
829 struct bcm_sysport_tx_ring *ring)
830{
Florian Fainelli80105be2014-04-24 18:08:57 -0700831 unsigned int c_index, last_c_index, last_tx_cn, num_tx_cbs;
832 unsigned int pkts_compl = 0, bytes_compl = 0;
kiki good10377ba2017-08-04 00:07:45 +0100833 struct net_device *ndev = priv->netdev;
Florian Fainelli80105be2014-04-24 18:08:57 -0700834 struct bcm_sysport_cb *cb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700835 u32 hw_ind;
836
Florian Fainelli6baa7852017-03-23 10:36:47 -0700837 /* Clear status before servicing to reduce spurious interrupts */
838 if (!ring->priv->is_lite)
839 intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
840 else
841 intrl2_0_writel(ring->priv, BIT(ring->index +
842 INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
843
Florian Fainelli80105be2014-04-24 18:08:57 -0700844 /* Compute how many descriptors have been processed since last call */
845 hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
846 c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
847 ring->p_index = (hw_ind & RING_PROD_INDEX_MASK);
848
849 last_c_index = ring->c_index;
850 num_tx_cbs = ring->size;
851
852 c_index &= (num_tx_cbs - 1);
853
854 if (c_index >= last_c_index)
855 last_tx_cn = c_index - last_c_index;
856 else
857 last_tx_cn = num_tx_cbs - last_c_index + c_index;
858
859 netif_dbg(priv, tx_done, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700860 "ring=%d c_index=%d last_tx_cn=%d last_c_index=%d\n",
861 ring->index, c_index, last_tx_cn, last_c_index);
Florian Fainelli80105be2014-04-24 18:08:57 -0700862
863 while (last_tx_cn-- > 0) {
864 cb = ring->cbs + last_c_index;
Florian Fainelli30defeb2017-03-23 10:36:46 -0700865 bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
Florian Fainelli80105be2014-04-24 18:08:57 -0700866
867 ring->desc_count++;
868 last_c_index++;
869 last_c_index &= (num_tx_cbs - 1);
870 }
871
kiki good10377ba2017-08-04 00:07:45 +0100872 u64_stats_update_begin(&priv->syncp);
873 ring->packets += pkts_compl;
874 ring->bytes += bytes_compl;
875 u64_stats_update_end(&priv->syncp);
876
Florian Fainelli80105be2014-04-24 18:08:57 -0700877 ring->c_index = c_index;
878
Florian Fainelli80105be2014-04-24 18:08:57 -0700879 netif_dbg(priv, tx_done, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700880 "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
881 ring->index, ring->c_index, pkts_compl, bytes_compl);
Florian Fainelli80105be2014-04-24 18:08:57 -0700882
883 return pkts_compl;
884}
885
886/* Locked version of the per-ring TX reclaim routine */
887static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
888 struct bcm_sysport_tx_ring *ring)
889{
Florian Fainelli148d3d02017-01-12 12:09:09 -0800890 struct netdev_queue *txq;
Florian Fainelli80105be2014-04-24 18:08:57 -0700891 unsigned int released;
Florian Fainellid8498082014-06-05 10:22:15 -0700892 unsigned long flags;
Florian Fainelli80105be2014-04-24 18:08:57 -0700893
Florian Fainelli148d3d02017-01-12 12:09:09 -0800894 txq = netdev_get_tx_queue(priv->netdev, ring->index);
895
Florian Fainellid8498082014-06-05 10:22:15 -0700896 spin_lock_irqsave(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -0700897 released = __bcm_sysport_tx_reclaim(priv, ring);
Florian Fainelli148d3d02017-01-12 12:09:09 -0800898 if (released)
899 netif_tx_wake_queue(txq);
900
Florian Fainellid8498082014-06-05 10:22:15 -0700901 spin_unlock_irqrestore(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -0700902
903 return released;
904}
905
Florian Fainelli148d3d02017-01-12 12:09:09 -0800906/* Locked version of the per-ring TX reclaim, but does not wake the queue */
907static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
908 struct bcm_sysport_tx_ring *ring)
909{
910 unsigned long flags;
911
912 spin_lock_irqsave(&ring->lock, flags);
913 __bcm_sysport_tx_reclaim(priv, ring);
914 spin_unlock_irqrestore(&ring->lock, flags);
915}
916
Florian Fainelli80105be2014-04-24 18:08:57 -0700917static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
918{
919 struct bcm_sysport_tx_ring *ring =
920 container_of(napi, struct bcm_sysport_tx_ring, napi);
921 unsigned int work_done = 0;
922
923 work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
924
Florian Fainelli16f62d92014-06-26 10:06:46 -0700925 if (work_done == 0) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700926 napi_complete(napi);
927 /* re-enable TX interrupt */
Florian Fainelli44a45242017-01-20 11:08:27 -0800928 if (!ring->priv->is_lite)
929 intrl2_1_mask_clear(ring->priv, BIT(ring->index));
930 else
931 intrl2_0_mask_clear(ring->priv, BIT(ring->index +
932 INTRL2_0_TDMA_MBDONE_SHIFT));
Florian Fainelli9dfa9a22014-11-12 15:40:43 -0800933
934 return 0;
Florian Fainelli80105be2014-04-24 18:08:57 -0700935 }
936
Florian Fainelli9dfa9a22014-11-12 15:40:43 -0800937 return budget;
Florian Fainelli80105be2014-04-24 18:08:57 -0700938}
939
940static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
941{
942 unsigned int q;
943
944 for (q = 0; q < priv->netdev->num_tx_queues; q++)
945 bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
946}
947
948static int bcm_sysport_poll(struct napi_struct *napi, int budget)
949{
950 struct bcm_sysport_priv *priv =
951 container_of(napi, struct bcm_sysport_priv, napi);
952 unsigned int work_done = 0;
953
954 work_done = bcm_sysport_desc_rx(priv, budget);
955
956 priv->rx_c_index += work_done;
957 priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
Florian Fainelli44a45242017-01-20 11:08:27 -0800958
959 /* SYSTEMPORT Lite groups the producer/consumer index, producer is
960 * maintained by HW, but writes to it will be ignore while RDMA
961 * is active
962 */
963 if (!priv->is_lite)
964 rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
965 else
966 rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
Florian Fainelli80105be2014-04-24 18:08:57 -0700967
968 if (work_done < budget) {
Florian Fainellic82f47e2016-04-20 11:37:09 -0700969 napi_complete_done(napi, work_done);
Florian Fainelli80105be2014-04-24 18:08:57 -0700970 /* re-enable RX interrupts */
971 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
972 }
973
974 return work_done;
975}
976
Florian Fainelli83e82f42014-07-01 21:08:40 -0700977static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
978{
979 u32 reg;
980
981 /* Stop monitoring MPD interrupt */
982 intrl2_0_mask_set(priv, INTRL2_0_MPD);
983
984 /* Clear the MagicPacket detection logic */
985 reg = umac_readl(priv, UMAC_MPD_CTRL);
986 reg &= ~MPD_EN;
987 umac_writel(priv, reg, UMAC_MPD_CTRL);
988
989 netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
990}
Florian Fainelli80105be2014-04-24 18:08:57 -0700991
992/* RX and misc interrupt routine */
993static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
994{
995 struct net_device *dev = dev_id;
996 struct bcm_sysport_priv *priv = netdev_priv(dev);
Florian Fainelli44a45242017-01-20 11:08:27 -0800997 struct bcm_sysport_tx_ring *txr;
998 unsigned int ring, ring_bit;
Florian Fainelli80105be2014-04-24 18:08:57 -0700999
1000 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
1001 ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
1002 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
1003
1004 if (unlikely(priv->irq0_stat == 0)) {
1005 netdev_warn(priv->netdev, "spurious RX interrupt\n");
1006 return IRQ_NONE;
1007 }
1008
1009 if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
1010 if (likely(napi_schedule_prep(&priv->napi))) {
1011 /* disable RX interrupts */
1012 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
Florian Fainelliba909502016-04-20 11:37:08 -07001013 __napi_schedule_irqoff(&priv->napi);
Florian Fainelli80105be2014-04-24 18:08:57 -07001014 }
1015 }
1016
1017 /* TX ring is full, perform a full reclaim since we do not know
1018 * which one would trigger this interrupt
1019 */
1020 if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
1021 bcm_sysport_tx_reclaim_all(priv);
1022
Florian Fainelli83e82f42014-07-01 21:08:40 -07001023 if (priv->irq0_stat & INTRL2_0_MPD) {
1024 netdev_info(priv->netdev, "Wake-on-LAN interrupt!\n");
1025 bcm_sysport_resume_from_wol(priv);
1026 }
1027
Florian Fainelli44a45242017-01-20 11:08:27 -08001028 if (!priv->is_lite)
1029 goto out;
1030
1031 for (ring = 0; ring < dev->num_tx_queues; ring++) {
1032 ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
1033 if (!(priv->irq0_stat & ring_bit))
1034 continue;
1035
1036 txr = &priv->tx_rings[ring];
1037
1038 if (likely(napi_schedule_prep(&txr->napi))) {
1039 intrl2_0_mask_set(priv, ring_bit);
1040 __napi_schedule(&txr->napi);
1041 }
1042 }
1043out:
Florian Fainelli80105be2014-04-24 18:08:57 -07001044 return IRQ_HANDLED;
1045}
1046
1047/* TX interrupt service routine */
1048static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
1049{
1050 struct net_device *dev = dev_id;
1051 struct bcm_sysport_priv *priv = netdev_priv(dev);
1052 struct bcm_sysport_tx_ring *txr;
1053 unsigned int ring;
1054
1055 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
1056 ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
1057 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1058
1059 if (unlikely(priv->irq1_stat == 0)) {
1060 netdev_warn(priv->netdev, "spurious TX interrupt\n");
1061 return IRQ_NONE;
1062 }
1063
1064 for (ring = 0; ring < dev->num_tx_queues; ring++) {
1065 if (!(priv->irq1_stat & BIT(ring)))
1066 continue;
1067
1068 txr = &priv->tx_rings[ring];
1069
1070 if (likely(napi_schedule_prep(&txr->napi))) {
1071 intrl2_1_mask_set(priv, BIT(ring));
Florian Fainelliba909502016-04-20 11:37:08 -07001072 __napi_schedule_irqoff(&txr->napi);
Florian Fainelli80105be2014-04-24 18:08:57 -07001073 }
1074 }
1075
1076 return IRQ_HANDLED;
1077}
1078
Florian Fainelli83e82f42014-07-01 21:08:40 -07001079static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
1080{
1081 struct bcm_sysport_priv *priv = dev_id;
1082
1083 pm_wakeup_event(&priv->pdev->dev, 0);
1084
1085 return IRQ_HANDLED;
1086}
1087
Florian Fainelli6cec4f52015-07-31 11:42:55 -07001088#ifdef CONFIG_NET_POLL_CONTROLLER
1089static void bcm_sysport_poll_controller(struct net_device *dev)
1090{
1091 struct bcm_sysport_priv *priv = netdev_priv(dev);
1092
1093 disable_irq(priv->irq0);
1094 bcm_sysport_rx_isr(priv->irq0, priv);
1095 enable_irq(priv->irq0);
1096
Florian Fainelli44a45242017-01-20 11:08:27 -08001097 if (!priv->is_lite) {
1098 disable_irq(priv->irq1);
1099 bcm_sysport_tx_isr(priv->irq1, priv);
1100 enable_irq(priv->irq1);
1101 }
Florian Fainelli6cec4f52015-07-31 11:42:55 -07001102}
1103#endif
1104
Florian Fainellie87474a2014-10-02 09:43:16 -07001105static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
1106 struct net_device *dev)
Florian Fainelli80105be2014-04-24 18:08:57 -07001107{
1108 struct sk_buff *nskb;
Paul Gortmaker3afc5572014-05-30 15:39:30 -04001109 struct bcm_tsb *tsb;
Florian Fainelli80105be2014-04-24 18:08:57 -07001110 u32 csum_info;
1111 u8 ip_proto;
1112 u16 csum_start;
1113 u16 ip_ver;
1114
1115 /* Re-allocate SKB if needed */
1116 if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
1117 nskb = skb_realloc_headroom(skb, sizeof(*tsb));
1118 dev_kfree_skb(skb);
1119 if (!nskb) {
1120 dev->stats.tx_errors++;
1121 dev->stats.tx_dropped++;
Florian Fainellie87474a2014-10-02 09:43:16 -07001122 return NULL;
Florian Fainelli80105be2014-04-24 18:08:57 -07001123 }
1124 skb = nskb;
1125 }
1126
Johannes Bergd58ff352017-06-16 14:29:23 +02001127 tsb = skb_push(skb, sizeof(*tsb));
Florian Fainelli80105be2014-04-24 18:08:57 -07001128 /* Zero-out TSB by default */
1129 memset(tsb, 0, sizeof(*tsb));
1130
1131 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1132 ip_ver = htons(skb->protocol);
1133 switch (ip_ver) {
1134 case ETH_P_IP:
1135 ip_proto = ip_hdr(skb)->protocol;
1136 break;
1137 case ETH_P_IPV6:
1138 ip_proto = ipv6_hdr(skb)->nexthdr;
1139 break;
1140 default:
Florian Fainellie87474a2014-10-02 09:43:16 -07001141 return skb;
Florian Fainelli80105be2014-04-24 18:08:57 -07001142 }
1143
1144 /* Get the checksum offset and the L4 (transport) offset */
1145 csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
1146 csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
1147 csum_info |= (csum_start << L4_PTR_SHIFT);
1148
1149 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
1150 csum_info |= L4_LENGTH_VALID;
1151 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
1152 csum_info |= L4_UDP;
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001153 } else {
Florian Fainelli80105be2014-04-24 18:08:57 -07001154 csum_info = 0;
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001155 }
Florian Fainelli80105be2014-04-24 18:08:57 -07001156
1157 tsb->l4_ptr_dest_map = csum_info;
1158 }
1159
Florian Fainellie87474a2014-10-02 09:43:16 -07001160 return skb;
Florian Fainelli80105be2014-04-24 18:08:57 -07001161}
1162
1163static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
1164 struct net_device *dev)
1165{
1166 struct bcm_sysport_priv *priv = netdev_priv(dev);
1167 struct device *kdev = &priv->pdev->dev;
1168 struct bcm_sysport_tx_ring *ring;
1169 struct bcm_sysport_cb *cb;
1170 struct netdev_queue *txq;
1171 struct dma_desc *desc;
Florian Fainellidab531b2014-05-14 19:32:14 -07001172 unsigned int skb_len;
Florian Fainellid8498082014-06-05 10:22:15 -07001173 unsigned long flags;
Florian Fainelli80105be2014-04-24 18:08:57 -07001174 dma_addr_t mapping;
1175 u32 len_status;
1176 u16 queue;
1177 int ret;
1178
1179 queue = skb_get_queue_mapping(skb);
1180 txq = netdev_get_tx_queue(dev, queue);
1181 ring = &priv->tx_rings[queue];
1182
Florian Fainellid8498082014-06-05 10:22:15 -07001183 /* lock against tx reclaim in BH context and TX ring full interrupt */
1184 spin_lock_irqsave(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -07001185 if (unlikely(ring->desc_count == 0)) {
1186 netif_tx_stop_queue(txq);
1187 netdev_err(dev, "queue %d awake and ring full!\n", queue);
1188 ret = NETDEV_TX_BUSY;
1189 goto out;
1190 }
1191
Florian Fainellidab531b2014-05-14 19:32:14 -07001192 /* The Ethernet switch we are interfaced with needs packets to be at
1193 * least 64 bytes (including FCS) otherwise they will be discarded when
1194 * they enter the switch port logic. When Broadcom tags are enabled, we
1195 * need to make sure that packets are at least 68 bytes
1196 * (including FCS and tag) because the length verification is done after
1197 * the Broadcom tag is stripped off the ingress packet.
1198 */
Florian Fainellibb7da332017-01-03 16:34:48 -08001199 if (skb_put_padto(skb, ETH_ZLEN + ENET_BRCM_TAG_LEN)) {
Florian Fainellidab531b2014-05-14 19:32:14 -07001200 ret = NETDEV_TX_OK;
1201 goto out;
1202 }
1203
Florian Fainelli38e5a852017-01-03 16:34:49 -08001204 /* Insert TSB and checksum infos */
1205 if (priv->tsb_en) {
1206 skb = bcm_sysport_insert_tsb(skb, dev);
1207 if (!skb) {
1208 ret = NETDEV_TX_OK;
1209 goto out;
1210 }
1211 }
1212
Florian Fainellibb7da332017-01-03 16:34:48 -08001213 skb_len = skb->len;
Florian Fainellidab531b2014-05-14 19:32:14 -07001214
1215 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -07001216 if (dma_mapping_error(kdev, mapping)) {
Florian Fainelli60b4ea12014-11-19 10:29:55 -08001217 priv->mib.tx_dma_failed++;
Florian Fainelli80105be2014-04-24 18:08:57 -07001218 netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001219 skb->data, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -07001220 ret = NETDEV_TX_OK;
1221 goto out;
1222 }
1223
1224 /* Remember the SKB for future freeing */
1225 cb = &ring->cbs[ring->curr_desc];
1226 cb->skb = skb;
1227 dma_unmap_addr_set(cb, dma_addr, mapping);
Florian Fainellidab531b2014-05-14 19:32:14 -07001228 dma_unmap_len_set(cb, dma_len, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -07001229
1230 /* Fetch a descriptor entry from our pool */
1231 desc = ring->desc_cpu;
1232
1233 desc->addr_lo = lower_32_bits(mapping);
1234 len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
Florian Fainellidab531b2014-05-14 19:32:14 -07001235 len_status |= (skb_len << DESC_LEN_SHIFT);
Florian Fainelli80105be2014-04-24 18:08:57 -07001236 len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001237 DESC_STATUS_SHIFT;
Florian Fainelli80105be2014-04-24 18:08:57 -07001238 if (skb->ip_summed == CHECKSUM_PARTIAL)
1239 len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
1240
1241 ring->curr_desc++;
1242 if (ring->curr_desc == ring->size)
1243 ring->curr_desc = 0;
1244 ring->desc_count--;
1245
1246 /* Ensure write completion of the descriptor status/length
1247 * in DRAM before the System Port WRITE_PORT register latches
1248 * the value
1249 */
1250 wmb();
1251 desc->addr_status_len = len_status;
1252 wmb();
1253
1254 /* Write this descriptor address to the RING write port */
1255 tdma_port_write_desc_addr(priv, desc, ring->index);
1256
1257 /* Check ring space and update SW control flow */
1258 if (ring->desc_count == 0)
1259 netif_tx_stop_queue(txq);
1260
1261 netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001262 ring->index, ring->desc_count, ring->curr_desc);
Florian Fainelli80105be2014-04-24 18:08:57 -07001263
1264 ret = NETDEV_TX_OK;
1265out:
Florian Fainellid8498082014-06-05 10:22:15 -07001266 spin_unlock_irqrestore(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -07001267 return ret;
1268}
1269
1270static void bcm_sysport_tx_timeout(struct net_device *dev)
1271{
1272 netdev_warn(dev, "transmit timeout!\n");
1273
Florian Westphal860e9532016-05-03 16:33:13 +02001274 netif_trans_update(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001275 dev->stats.tx_errors++;
1276
1277 netif_tx_wake_all_queues(dev);
1278}
1279
1280/* phylib adjust link callback */
1281static void bcm_sysport_adj_link(struct net_device *dev)
1282{
1283 struct bcm_sysport_priv *priv = netdev_priv(dev);
Philippe Reynes715a0222016-06-19 20:39:08 +02001284 struct phy_device *phydev = dev->phydev;
Florian Fainelli80105be2014-04-24 18:08:57 -07001285 unsigned int changed = 0;
1286 u32 cmd_bits = 0, reg;
1287
1288 if (priv->old_link != phydev->link) {
1289 changed = 1;
1290 priv->old_link = phydev->link;
1291 }
1292
1293 if (priv->old_duplex != phydev->duplex) {
1294 changed = 1;
1295 priv->old_duplex = phydev->duplex;
1296 }
1297
Florian Fainelli44a45242017-01-20 11:08:27 -08001298 if (priv->is_lite)
1299 goto out;
1300
Florian Fainelli80105be2014-04-24 18:08:57 -07001301 switch (phydev->speed) {
1302 case SPEED_2500:
1303 cmd_bits = CMD_SPEED_2500;
1304 break;
1305 case SPEED_1000:
1306 cmd_bits = CMD_SPEED_1000;
1307 break;
1308 case SPEED_100:
1309 cmd_bits = CMD_SPEED_100;
1310 break;
1311 case SPEED_10:
1312 cmd_bits = CMD_SPEED_10;
1313 break;
1314 default:
1315 break;
1316 }
1317 cmd_bits <<= CMD_SPEED_SHIFT;
1318
1319 if (phydev->duplex == DUPLEX_HALF)
1320 cmd_bits |= CMD_HD_EN;
1321
1322 if (priv->old_pause != phydev->pause) {
1323 changed = 1;
1324 priv->old_pause = phydev->pause;
1325 }
1326
1327 if (!phydev->pause)
1328 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
1329
Florian Fainelli4a804c02014-09-02 11:17:07 -07001330 if (!changed)
1331 return;
1332
1333 if (phydev->link) {
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001334 reg = umac_readl(priv, UMAC_CMD);
1335 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
Florian Fainelli80105be2014-04-24 18:08:57 -07001336 CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
1337 CMD_TX_PAUSE_IGNORE);
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001338 reg |= cmd_bits;
1339 umac_writel(priv, reg, UMAC_CMD);
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001340 }
Florian Fainelli44a45242017-01-20 11:08:27 -08001341out:
1342 if (changed)
1343 phy_print_status(phydev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001344}
1345
1346static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
1347 unsigned int index)
1348{
1349 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1350 struct device *kdev = &priv->pdev->dev;
1351 size_t size;
1352 void *p;
1353 u32 reg;
1354
1355 /* Simple descriptors partitioning for now */
1356 size = 256;
1357
1358 /* We just need one DMA descriptor which is DMA-able, since writing to
1359 * the port will allocate a new descriptor in its internal linked-list
1360 */
Florian Fainelli3e8fc382014-10-31 15:51:34 -07001361 p = dma_zalloc_coherent(kdev, sizeof(struct dma_desc), &ring->desc_dma,
1362 GFP_KERNEL);
Florian Fainelli80105be2014-04-24 18:08:57 -07001363 if (!p) {
1364 netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
1365 return -ENOMEM;
1366 }
1367
Florian Fainelli40a8a312014-07-09 17:36:47 -07001368 ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
Florian Fainelli80105be2014-04-24 18:08:57 -07001369 if (!ring->cbs) {
1370 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1371 return -ENOMEM;
1372 }
1373
1374 /* Initialize SW view of the ring */
1375 spin_lock_init(&ring->lock);
1376 ring->priv = priv;
Eric Dumazetd64b5e82015-11-18 06:31:00 -08001377 netif_tx_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
Florian Fainelli80105be2014-04-24 18:08:57 -07001378 ring->index = index;
1379 ring->size = size;
1380 ring->alloc_size = ring->size;
1381 ring->desc_cpu = p;
1382 ring->desc_count = ring->size;
1383 ring->curr_desc = 0;
1384
1385 /* Initialize HW ring */
1386 tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1387 tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1388 tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1389 tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1390 tdma_writel(priv, RING_IGNORE_STATUS, TDMA_DESC_RING_MAPPING(index));
1391 tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
1392
1393 /* Program the number of descriptors as MAX_THRESHOLD and half of
1394 * its size for the hysteresis trigger
1395 */
1396 tdma_writel(priv, ring->size |
1397 1 << RING_HYST_THRESH_SHIFT,
1398 TDMA_DESC_RING_MAX_HYST(index));
1399
1400 /* Enable the ring queue in the arbiter */
1401 reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1402 reg |= (1 << index);
1403 tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1404
1405 napi_enable(&ring->napi);
1406
1407 netif_dbg(priv, hw, priv->netdev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001408 "TDMA cfg, size=%d, desc_cpu=%p\n",
1409 ring->size, ring->desc_cpu);
Florian Fainelli80105be2014-04-24 18:08:57 -07001410
1411 return 0;
1412}
1413
1414static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001415 unsigned int index)
Florian Fainelli80105be2014-04-24 18:08:57 -07001416{
1417 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1418 struct device *kdev = &priv->pdev->dev;
1419 u32 reg;
1420
1421 /* Caller should stop the TDMA engine */
1422 reg = tdma_readl(priv, TDMA_STATUS);
1423 if (!(reg & TDMA_DISABLED))
1424 netdev_warn(priv->netdev, "TDMA not stopped!\n");
1425
Florian Fainelli914adb52014-10-31 15:51:35 -07001426 /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
1427 * fail, so by checking this pointer we know whether the TX ring was
1428 * fully initialized or not.
1429 */
1430 if (!ring->cbs)
1431 return;
1432
Florian Fainelli80105be2014-04-24 18:08:57 -07001433 napi_disable(&ring->napi);
1434 netif_napi_del(&ring->napi);
1435
Florian Fainelli148d3d02017-01-12 12:09:09 -08001436 bcm_sysport_tx_clean(priv, ring);
Florian Fainelli80105be2014-04-24 18:08:57 -07001437
1438 kfree(ring->cbs);
1439 ring->cbs = NULL;
1440
1441 if (ring->desc_dma) {
Florian Fainelli3e8fc382014-10-31 15:51:34 -07001442 dma_free_coherent(kdev, sizeof(struct dma_desc),
1443 ring->desc_cpu, ring->desc_dma);
Florian Fainelli80105be2014-04-24 18:08:57 -07001444 ring->desc_dma = 0;
1445 }
1446 ring->size = 0;
1447 ring->alloc_size = 0;
1448
1449 netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1450}
1451
1452/* RDMA helper */
1453static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001454 unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001455{
1456 unsigned int timeout = 1000;
1457 u32 reg;
1458
1459 reg = rdma_readl(priv, RDMA_CONTROL);
1460 if (enable)
1461 reg |= RDMA_EN;
1462 else
1463 reg &= ~RDMA_EN;
1464 rdma_writel(priv, reg, RDMA_CONTROL);
1465
1466 /* Poll for RMDA disabling completion */
1467 do {
1468 reg = rdma_readl(priv, RDMA_STATUS);
1469 if (!!(reg & RDMA_DISABLED) == !enable)
1470 return 0;
1471 usleep_range(1000, 2000);
1472 } while (timeout-- > 0);
1473
1474 netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1475
1476 return -ETIMEDOUT;
1477}
1478
1479/* TDMA helper */
1480static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001481 unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001482{
1483 unsigned int timeout = 1000;
1484 u32 reg;
1485
1486 reg = tdma_readl(priv, TDMA_CONTROL);
1487 if (enable)
Florian Fainelli44a45242017-01-20 11:08:27 -08001488 reg |= tdma_control_bit(priv, TDMA_EN);
Florian Fainelli80105be2014-04-24 18:08:57 -07001489 else
Florian Fainelli44a45242017-01-20 11:08:27 -08001490 reg &= ~tdma_control_bit(priv, TDMA_EN);
Florian Fainelli80105be2014-04-24 18:08:57 -07001491 tdma_writel(priv, reg, TDMA_CONTROL);
1492
1493 /* Poll for TMDA disabling completion */
1494 do {
1495 reg = tdma_readl(priv, TDMA_STATUS);
1496 if (!!(reg & TDMA_DISABLED) == !enable)
1497 return 0;
1498
1499 usleep_range(1000, 2000);
1500 } while (timeout-- > 0);
1501
1502 netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1503
1504 return -ETIMEDOUT;
1505}
1506
1507static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1508{
Florian Fainellibaf387a2015-05-28 15:24:42 -07001509 struct bcm_sysport_cb *cb;
Florian Fainelli80105be2014-04-24 18:08:57 -07001510 u32 reg;
1511 int ret;
Florian Fainellibaf387a2015-05-28 15:24:42 -07001512 int i;
Florian Fainelli80105be2014-04-24 18:08:57 -07001513
1514 /* Initialize SW view of the RX ring */
Florian Fainelli44a45242017-01-20 11:08:27 -08001515 priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
Florian Fainelli80105be2014-04-24 18:08:57 -07001516 priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
Florian Fainelli80105be2014-04-24 18:08:57 -07001517 priv->rx_c_index = 0;
1518 priv->rx_read_ptr = 0;
Florian Fainelli40a8a312014-07-09 17:36:47 -07001519 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
1520 GFP_KERNEL);
Florian Fainelli80105be2014-04-24 18:08:57 -07001521 if (!priv->rx_cbs) {
1522 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1523 return -ENOMEM;
1524 }
1525
Florian Fainellibaf387a2015-05-28 15:24:42 -07001526 for (i = 0; i < priv->num_rx_bds; i++) {
1527 cb = priv->rx_cbs + i;
1528 cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
1529 }
1530
Florian Fainelli80105be2014-04-24 18:08:57 -07001531 ret = bcm_sysport_alloc_rx_bufs(priv);
1532 if (ret) {
1533 netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1534 return ret;
1535 }
1536
1537 /* Initialize HW, ensure RDMA is disabled */
1538 reg = rdma_readl(priv, RDMA_STATUS);
1539 if (!(reg & RDMA_DISABLED))
1540 rdma_enable_set(priv, 0);
1541
1542 rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1543 rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1544 rdma_writel(priv, 0, RDMA_PROD_INDEX);
1545 rdma_writel(priv, 0, RDMA_CONS_INDEX);
1546 rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1547 RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1548 /* Operate the queue in ring mode */
1549 rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1550 rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1551 rdma_writel(priv, 0, RDMA_END_ADDR_HI);
Florian Fainelli44a45242017-01-20 11:08:27 -08001552 rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
Florian Fainelli80105be2014-04-24 18:08:57 -07001553
1554 rdma_writel(priv, 1, RDMA_MBDONE_INTR);
1555
1556 netif_dbg(priv, hw, priv->netdev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001557 "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1558 priv->num_rx_bds, priv->rx_bds);
Florian Fainelli80105be2014-04-24 18:08:57 -07001559
1560 return 0;
1561}
1562
1563static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1564{
1565 struct bcm_sysport_cb *cb;
1566 unsigned int i;
1567 u32 reg;
1568
1569 /* Caller should ensure RDMA is disabled */
1570 reg = rdma_readl(priv, RDMA_STATUS);
1571 if (!(reg & RDMA_DISABLED))
1572 netdev_warn(priv->netdev, "RDMA not stopped!\n");
1573
1574 for (i = 0; i < priv->num_rx_bds; i++) {
1575 cb = &priv->rx_cbs[i];
1576 if (dma_unmap_addr(cb, dma_addr))
1577 dma_unmap_single(&priv->pdev->dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001578 dma_unmap_addr(cb, dma_addr),
1579 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -07001580 bcm_sysport_free_cb(cb);
1581 }
1582
1583 kfree(priv->rx_cbs);
1584 priv->rx_cbs = NULL;
1585
1586 netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1587}
1588
1589static void bcm_sysport_set_rx_mode(struct net_device *dev)
1590{
1591 struct bcm_sysport_priv *priv = netdev_priv(dev);
1592 u32 reg;
1593
Florian Fainelli44a45242017-01-20 11:08:27 -08001594 if (priv->is_lite)
1595 return;
1596
Florian Fainelli80105be2014-04-24 18:08:57 -07001597 reg = umac_readl(priv, UMAC_CMD);
1598 if (dev->flags & IFF_PROMISC)
1599 reg |= CMD_PROMISC;
1600 else
1601 reg &= ~CMD_PROMISC;
1602 umac_writel(priv, reg, UMAC_CMD);
1603
1604 /* No support for ALLMULTI */
1605 if (dev->flags & IFF_ALLMULTI)
1606 return;
1607}
1608
1609static inline void umac_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001610 u32 mask, unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001611{
1612 u32 reg;
1613
Florian Fainelli44a45242017-01-20 11:08:27 -08001614 if (!priv->is_lite) {
1615 reg = umac_readl(priv, UMAC_CMD);
1616 if (enable)
1617 reg |= mask;
1618 else
1619 reg &= ~mask;
1620 umac_writel(priv, reg, UMAC_CMD);
1621 } else {
1622 reg = gib_readl(priv, GIB_CONTROL);
1623 if (enable)
1624 reg |= mask;
1625 else
1626 reg &= ~mask;
1627 gib_writel(priv, reg, GIB_CONTROL);
1628 }
Florian Fainelli00b91c62014-05-15 14:33:53 -07001629
1630 /* UniMAC stops on a packet boundary, wait for a full-sized packet
1631 * to be processed (1 msec).
1632 */
1633 if (enable == 0)
1634 usleep_range(1000, 2000);
Florian Fainelli80105be2014-04-24 18:08:57 -07001635}
1636
Florian Fainelli412bce82014-06-26 10:06:45 -07001637static inline void umac_reset(struct bcm_sysport_priv *priv)
Florian Fainelli80105be2014-04-24 18:08:57 -07001638{
Florian Fainelli80105be2014-04-24 18:08:57 -07001639 u32 reg;
Florian Fainelli80105be2014-04-24 18:08:57 -07001640
Florian Fainelli44a45242017-01-20 11:08:27 -08001641 if (priv->is_lite)
1642 return;
1643
Florian Fainelli412bce82014-06-26 10:06:45 -07001644 reg = umac_readl(priv, UMAC_CMD);
1645 reg |= CMD_SW_RESET;
1646 umac_writel(priv, reg, UMAC_CMD);
1647 udelay(10);
1648 reg = umac_readl(priv, UMAC_CMD);
1649 reg &= ~CMD_SW_RESET;
1650 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli80105be2014-04-24 18:08:57 -07001651}
1652
1653static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001654 unsigned char *addr)
Florian Fainelli80105be2014-04-24 18:08:57 -07001655{
Florian Fainelli44a45242017-01-20 11:08:27 -08001656 u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1657 addr[3];
1658 u32 mac1 = (addr[4] << 8) | addr[5];
1659
1660 if (!priv->is_lite) {
1661 umac_writel(priv, mac0, UMAC_MAC0);
1662 umac_writel(priv, mac1, UMAC_MAC1);
1663 } else {
1664 gib_writel(priv, mac0, GIB_MAC0);
1665 gib_writel(priv, mac1, GIB_MAC1);
1666 }
Florian Fainelli80105be2014-04-24 18:08:57 -07001667}
1668
1669static void topctrl_flush(struct bcm_sysport_priv *priv)
1670{
1671 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1672 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1673 mdelay(1);
1674 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1675 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1676}
1677
Florian Fainellifb3b5962014-12-08 15:59:18 -08001678static int bcm_sysport_change_mac(struct net_device *dev, void *p)
1679{
1680 struct bcm_sysport_priv *priv = netdev_priv(dev);
1681 struct sockaddr *addr = p;
1682
1683 if (!is_valid_ether_addr(addr->sa_data))
1684 return -EINVAL;
1685
1686 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1687
1688 /* interface is disabled, changes to MAC will be reflected on next
1689 * open call
1690 */
1691 if (!netif_running(dev))
1692 return 0;
1693
1694 umac_set_hw_addr(priv, dev->dev_addr);
1695
1696 return 0;
1697}
1698
kiki good10377ba2017-08-04 00:07:45 +01001699static void bcm_sysport_get_stats64(struct net_device *dev,
1700 struct rtnl_link_stats64 *stats)
Florian Fainelli30defeb2017-03-23 10:36:46 -07001701{
1702 struct bcm_sysport_priv *priv = netdev_priv(dev);
kiki good10377ba2017-08-04 00:07:45 +01001703 struct bcm_sysport_stats64 *stats64 = &priv->stats64;
Florian Fainelli30defeb2017-03-23 10:36:46 -07001704 struct bcm_sysport_tx_ring *ring;
kiki good10377ba2017-08-04 00:07:45 +01001705 u64 tx_packets = 0, tx_bytes = 0;
1706 unsigned int start;
Florian Fainelli30defeb2017-03-23 10:36:46 -07001707 unsigned int q;
1708
kiki good10377ba2017-08-04 00:07:45 +01001709 netdev_stats_to_stats64(stats, &dev->stats);
1710
Florian Fainelli30defeb2017-03-23 10:36:46 -07001711 for (q = 0; q < dev->num_tx_queues; q++) {
1712 ring = &priv->tx_rings[q];
kiki good10377ba2017-08-04 00:07:45 +01001713 do {
1714 start = u64_stats_fetch_begin_irq(&priv->syncp);
1715 tx_bytes = ring->bytes;
1716 tx_packets = ring->packets;
1717 } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
1718
1719 stats->tx_bytes += tx_bytes;
1720 stats->tx_packets += tx_packets;
Florian Fainelli30defeb2017-03-23 10:36:46 -07001721 }
1722
kiki good10377ba2017-08-04 00:07:45 +01001723 /* lockless update tx_bytes and tx_packets */
1724 u64_stats_update_begin(&priv->syncp);
1725 stats64->tx_bytes = stats->tx_bytes;
1726 stats64->tx_packets = stats->tx_packets;
1727 u64_stats_update_end(&priv->syncp);
1728
1729 do {
1730 start = u64_stats_fetch_begin_irq(&priv->syncp);
1731 stats->rx_packets = stats64->rx_packets;
1732 stats->rx_bytes = stats64->rx_bytes;
1733 } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
Florian Fainelli30defeb2017-03-23 10:36:46 -07001734}
1735
Florian Fainellib02e6d92014-07-01 21:08:37 -07001736static void bcm_sysport_netif_start(struct net_device *dev)
1737{
1738 struct bcm_sysport_priv *priv = netdev_priv(dev);
1739
1740 /* Enable NAPI */
1741 napi_enable(&priv->napi);
1742
Florian Fainelli8edf0042014-10-28 11:12:00 -07001743 /* Enable RX interrupt and TX ring full interrupt */
1744 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1745
Philippe Reynes715a0222016-06-19 20:39:08 +02001746 phy_start(dev->phydev);
Florian Fainellib02e6d92014-07-01 21:08:37 -07001747
Florian Fainelli44a45242017-01-20 11:08:27 -08001748 /* Enable TX interrupts for the TXQs */
1749 if (!priv->is_lite)
1750 intrl2_1_mask_clear(priv, 0xffffffff);
1751 else
1752 intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
Florian Fainellib02e6d92014-07-01 21:08:37 -07001753
1754 /* Last call before we start the real business */
1755 netif_tx_start_all_queues(dev);
1756}
1757
Florian Fainelli40755a02014-07-01 21:08:38 -07001758static void rbuf_init(struct bcm_sysport_priv *priv)
1759{
1760 u32 reg;
1761
1762 reg = rbuf_readl(priv, RBUF_CONTROL);
1763 reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
Florian Fainelli44a45242017-01-20 11:08:27 -08001764 /* Set a correct RSB format on SYSTEMPORT Lite */
Florian Fainelli389a06b2017-08-29 13:35:17 -07001765 if (priv->is_lite)
Florian Fainelli44a45242017-01-20 11:08:27 -08001766 reg &= ~RBUF_RSB_SWAP1;
Florian Fainelli389a06b2017-08-29 13:35:17 -07001767
1768 /* Set a correct RSB format based on host endian */
1769 if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
Florian Fainelli44a45242017-01-20 11:08:27 -08001770 reg |= RBUF_RSB_SWAP0;
Florian Fainelli389a06b2017-08-29 13:35:17 -07001771 else
1772 reg &= ~RBUF_RSB_SWAP0;
Florian Fainelli40755a02014-07-01 21:08:38 -07001773 rbuf_writel(priv, reg, RBUF_CONTROL);
1774}
1775
Florian Fainelli44a45242017-01-20 11:08:27 -08001776static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
1777{
1778 intrl2_0_mask_set(priv, 0xffffffff);
1779 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1780 if (!priv->is_lite) {
1781 intrl2_1_mask_set(priv, 0xffffffff);
1782 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1783 }
1784}
1785
1786static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
1787{
1788 u32 __maybe_unused reg;
1789
1790 /* Include Broadcom tag in pad extension */
1791 if (netdev_uses_dsa(priv->netdev)) {
1792 reg = gib_readl(priv, GIB_CONTROL);
1793 reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
1794 reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
1795 gib_writel(priv, reg, GIB_CONTROL);
1796 }
1797}
1798
Florian Fainelli80105be2014-04-24 18:08:57 -07001799static int bcm_sysport_open(struct net_device *dev)
1800{
1801 struct bcm_sysport_priv *priv = netdev_priv(dev);
Philippe Reynes715a0222016-06-19 20:39:08 +02001802 struct phy_device *phydev;
Florian Fainelli80105be2014-04-24 18:08:57 -07001803 unsigned int i;
Florian Fainelli80105be2014-04-24 18:08:57 -07001804 int ret;
1805
1806 /* Reset UniMAC */
Florian Fainelli412bce82014-06-26 10:06:45 -07001807 umac_reset(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001808
1809 /* Flush TX and RX FIFOs at TOPCTRL level */
1810 topctrl_flush(priv);
1811
1812 /* Disable the UniMAC RX/TX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001813 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001814
1815 /* Enable RBUF 2bytes alignment and Receive Status Block */
Florian Fainelli40755a02014-07-01 21:08:38 -07001816 rbuf_init(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001817
1818 /* Set maximum frame length */
Florian Fainelli44a45242017-01-20 11:08:27 -08001819 if (!priv->is_lite)
1820 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1821 else
1822 gib_set_pad_extension(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001823
1824 /* Set MAC address */
1825 umac_set_hw_addr(priv, dev->dev_addr);
1826
1827 /* Read CRC forward */
Florian Fainelli44a45242017-01-20 11:08:27 -08001828 if (!priv->is_lite)
1829 priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
1830 else
1831 priv->crc_fwd = !!(gib_readl(priv, GIB_CONTROL) &
1832 GIB_FCS_STRIP);
Florian Fainelli80105be2014-04-24 18:08:57 -07001833
Philippe Reynes715a0222016-06-19 20:39:08 +02001834 phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1835 0, priv->phy_interface);
1836 if (!phydev) {
Florian Fainelli80105be2014-04-24 18:08:57 -07001837 netdev_err(dev, "could not attach to PHY\n");
1838 return -ENODEV;
1839 }
1840
1841 /* Reset house keeping link status */
1842 priv->old_duplex = -1;
1843 priv->old_link = -1;
1844 priv->old_pause = -1;
1845
1846 /* mask all interrupts and request them */
Florian Fainelli44a45242017-01-20 11:08:27 -08001847 bcm_sysport_mask_all_intrs(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001848
1849 ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1850 if (ret) {
1851 netdev_err(dev, "failed to request RX interrupt\n");
1852 goto out_phy_disconnect;
1853 }
1854
Florian Fainelli44a45242017-01-20 11:08:27 -08001855 if (!priv->is_lite) {
1856 ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
1857 dev->name, dev);
1858 if (ret) {
1859 netdev_err(dev, "failed to request TX interrupt\n");
1860 goto out_free_irq0;
1861 }
Florian Fainelli80105be2014-04-24 18:08:57 -07001862 }
1863
1864 /* Initialize both hardware and software ring */
1865 for (i = 0; i < dev->num_tx_queues; i++) {
1866 ret = bcm_sysport_init_tx_ring(priv, i);
1867 if (ret) {
1868 netdev_err(dev, "failed to initialize TX ring %d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001869 i);
Florian Fainelli80105be2014-04-24 18:08:57 -07001870 goto out_free_tx_ring;
1871 }
1872 }
1873
1874 /* Initialize linked-list */
1875 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1876
1877 /* Initialize RX ring */
1878 ret = bcm_sysport_init_rx_ring(priv);
1879 if (ret) {
1880 netdev_err(dev, "failed to initialize RX ring\n");
1881 goto out_free_rx_ring;
1882 }
1883
1884 /* Turn on RDMA */
1885 ret = rdma_enable_set(priv, 1);
1886 if (ret)
1887 goto out_free_rx_ring;
1888
Florian Fainelli80105be2014-04-24 18:08:57 -07001889 /* Turn on TDMA */
1890 ret = tdma_enable_set(priv, 1);
1891 if (ret)
1892 goto out_clear_rx_int;
1893
Florian Fainelli80105be2014-04-24 18:08:57 -07001894 /* Turn on UniMAC TX/RX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001895 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
Florian Fainelli80105be2014-04-24 18:08:57 -07001896
Florian Fainellib02e6d92014-07-01 21:08:37 -07001897 bcm_sysport_netif_start(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001898
1899 return 0;
1900
1901out_clear_rx_int:
1902 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1903out_free_rx_ring:
1904 bcm_sysport_fini_rx_ring(priv);
1905out_free_tx_ring:
1906 for (i = 0; i < dev->num_tx_queues; i++)
1907 bcm_sysport_fini_tx_ring(priv, i);
Florian Fainelli44a45242017-01-20 11:08:27 -08001908 if (!priv->is_lite)
1909 free_irq(priv->irq1, dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001910out_free_irq0:
1911 free_irq(priv->irq0, dev);
1912out_phy_disconnect:
Philippe Reynes715a0222016-06-19 20:39:08 +02001913 phy_disconnect(phydev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001914 return ret;
1915}
1916
Florian Fainellib02e6d92014-07-01 21:08:37 -07001917static void bcm_sysport_netif_stop(struct net_device *dev)
Florian Fainelli80105be2014-04-24 18:08:57 -07001918{
1919 struct bcm_sysport_priv *priv = netdev_priv(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001920
1921 /* stop all software from updating hardware */
1922 netif_tx_stop_all_queues(dev);
1923 napi_disable(&priv->napi);
Philippe Reynes715a0222016-06-19 20:39:08 +02001924 phy_stop(dev->phydev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001925
1926 /* mask all interrupts */
Florian Fainelli44a45242017-01-20 11:08:27 -08001927 bcm_sysport_mask_all_intrs(priv);
Florian Fainellib02e6d92014-07-01 21:08:37 -07001928}
1929
1930static int bcm_sysport_stop(struct net_device *dev)
1931{
1932 struct bcm_sysport_priv *priv = netdev_priv(dev);
1933 unsigned int i;
1934 int ret;
1935
1936 bcm_sysport_netif_stop(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001937
1938 /* Disable UniMAC RX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001939 umac_enable_set(priv, CMD_RX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001940
1941 ret = tdma_enable_set(priv, 0);
1942 if (ret) {
1943 netdev_err(dev, "timeout disabling RDMA\n");
1944 return ret;
1945 }
1946
1947 /* Wait for a maximum packet size to be drained */
1948 usleep_range(2000, 3000);
1949
1950 ret = rdma_enable_set(priv, 0);
1951 if (ret) {
1952 netdev_err(dev, "timeout disabling TDMA\n");
1953 return ret;
1954 }
1955
1956 /* Disable UniMAC TX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001957 umac_enable_set(priv, CMD_TX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001958
1959 /* Free RX/TX rings SW structures */
1960 for (i = 0; i < dev->num_tx_queues; i++)
1961 bcm_sysport_fini_tx_ring(priv, i);
1962 bcm_sysport_fini_rx_ring(priv);
1963
1964 free_irq(priv->irq0, dev);
Florian Fainelli44a45242017-01-20 11:08:27 -08001965 if (!priv->is_lite)
1966 free_irq(priv->irq1, dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001967
1968 /* Disconnect from PHY */
Philippe Reynes715a0222016-06-19 20:39:08 +02001969 phy_disconnect(dev->phydev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001970
1971 return 0;
1972}
1973
Julia Lawallc1ab0e92016-08-31 09:30:48 +02001974static const struct ethtool_ops bcm_sysport_ethtool_ops = {
Florian Fainelli80105be2014-04-24 18:08:57 -07001975 .get_drvinfo = bcm_sysport_get_drvinfo,
1976 .get_msglevel = bcm_sysport_get_msglvl,
1977 .set_msglevel = bcm_sysport_set_msglvl,
1978 .get_link = ethtool_op_get_link,
1979 .get_strings = bcm_sysport_get_strings,
1980 .get_ethtool_stats = bcm_sysport_get_stats,
1981 .get_sset_count = bcm_sysport_get_sset_count,
Florian Fainelli83e82f42014-07-01 21:08:40 -07001982 .get_wol = bcm_sysport_get_wol,
1983 .set_wol = bcm_sysport_set_wol,
Florian Fainellib1a15e82015-05-11 15:12:41 -07001984 .get_coalesce = bcm_sysport_get_coalesce,
1985 .set_coalesce = bcm_sysport_set_coalesce,
Philippe Reynes697666e2016-06-19 20:39:09 +02001986 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1987 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Florian Fainelli80105be2014-04-24 18:08:57 -07001988};
1989
1990static const struct net_device_ops bcm_sysport_netdev_ops = {
1991 .ndo_start_xmit = bcm_sysport_xmit,
1992 .ndo_tx_timeout = bcm_sysport_tx_timeout,
1993 .ndo_open = bcm_sysport_open,
1994 .ndo_stop = bcm_sysport_stop,
1995 .ndo_set_features = bcm_sysport_set_features,
1996 .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
Florian Fainellifb3b5962014-12-08 15:59:18 -08001997 .ndo_set_mac_address = bcm_sysport_change_mac,
Florian Fainelli6cec4f52015-07-31 11:42:55 -07001998#ifdef CONFIG_NET_POLL_CONTROLLER
1999 .ndo_poll_controller = bcm_sysport_poll_controller,
2000#endif
kiki good10377ba2017-08-04 00:07:45 +01002001 .ndo_get_stats64 = bcm_sysport_get_stats64,
Florian Fainelli80105be2014-04-24 18:08:57 -07002002};
2003
2004#define REV_FMT "v%2x.%02x"
2005
Florian Fainelli44a45242017-01-20 11:08:27 -08002006static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
2007 [SYSTEMPORT] = {
2008 .is_lite = false,
2009 .num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
2010 },
2011 [SYSTEMPORT_LITE] = {
2012 .is_lite = true,
2013 .num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
2014 },
2015};
2016
2017static const struct of_device_id bcm_sysport_of_match[] = {
2018 { .compatible = "brcm,systemportlite-v1.00",
2019 .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
2020 { .compatible = "brcm,systemport-v1.00",
2021 .data = &bcm_sysport_params[SYSTEMPORT] },
2022 { .compatible = "brcm,systemport",
2023 .data = &bcm_sysport_params[SYSTEMPORT] },
2024 { /* sentinel */ }
2025};
2026MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
2027
Florian Fainelli80105be2014-04-24 18:08:57 -07002028static int bcm_sysport_probe(struct platform_device *pdev)
2029{
Florian Fainelli44a45242017-01-20 11:08:27 -08002030 const struct bcm_sysport_hw_params *params;
2031 const struct of_device_id *of_id = NULL;
Florian Fainelli80105be2014-04-24 18:08:57 -07002032 struct bcm_sysport_priv *priv;
2033 struct device_node *dn;
2034 struct net_device *dev;
2035 const void *macaddr;
2036 struct resource *r;
2037 u32 txq, rxq;
2038 int ret;
2039
2040 dn = pdev->dev.of_node;
2041 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Florian Fainelli44a45242017-01-20 11:08:27 -08002042 of_id = of_match_node(bcm_sysport_of_match, dn);
2043 if (!of_id || !of_id->data)
2044 return -EINVAL;
2045
2046 /* Fairly quickly we need to know the type of adapter we have */
2047 params = of_id->data;
Florian Fainelli80105be2014-04-24 18:08:57 -07002048
2049 /* Read the Transmit/Receive Queue properties */
2050 if (of_property_read_u32(dn, "systemport,num-txq", &txq))
2051 txq = TDMA_NUM_RINGS;
2052 if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
2053 rxq = 1;
2054
Florian Fainelli7b78be42017-01-20 11:08:26 -08002055 /* Sanity check the number of transmit queues */
2056 if (!txq || txq > TDMA_NUM_RINGS)
2057 return -EINVAL;
2058
Florian Fainelli80105be2014-04-24 18:08:57 -07002059 dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
2060 if (!dev)
2061 return -ENOMEM;
2062
2063 /* Initialize private members */
2064 priv = netdev_priv(dev);
2065
Florian Fainelli7b78be42017-01-20 11:08:26 -08002066 /* Allocate number of TX rings */
2067 priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
2068 sizeof(struct bcm_sysport_tx_ring),
2069 GFP_KERNEL);
2070 if (!priv->tx_rings)
2071 return -ENOMEM;
2072
Florian Fainelli44a45242017-01-20 11:08:27 -08002073 priv->is_lite = params->is_lite;
2074 priv->num_rx_desc_words = params->num_rx_desc_words;
2075
Florian Fainelli80105be2014-04-24 18:08:57 -07002076 priv->irq0 = platform_get_irq(pdev, 0);
Florian Fainellid31353c2017-06-01 18:02:39 -07002077 if (!priv->is_lite) {
Florian Fainelli44a45242017-01-20 11:08:27 -08002078 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainellid31353c2017-06-01 18:02:39 -07002079 priv->wol_irq = platform_get_irq(pdev, 2);
2080 } else {
2081 priv->wol_irq = platform_get_irq(pdev, 1);
2082 }
Florian Fainelli44a45242017-01-20 11:08:27 -08002083 if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
Florian Fainelli80105be2014-04-24 18:08:57 -07002084 dev_err(&pdev->dev, "invalid interrupts\n");
2085 ret = -EINVAL;
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002086 goto err_free_netdev;
Florian Fainelli80105be2014-04-24 18:08:57 -07002087 }
2088
Jingoo Han126e6122014-05-14 12:15:42 +09002089 priv->base = devm_ioremap_resource(&pdev->dev, r);
2090 if (IS_ERR(priv->base)) {
2091 ret = PTR_ERR(priv->base);
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002092 goto err_free_netdev;
Florian Fainelli80105be2014-04-24 18:08:57 -07002093 }
2094
2095 priv->netdev = dev;
2096 priv->pdev = pdev;
2097
2098 priv->phy_interface = of_get_phy_mode(dn);
2099 /* Default to GMII interface mode */
2100 if (priv->phy_interface < 0)
2101 priv->phy_interface = PHY_INTERFACE_MODE_GMII;
2102
Florian Fainelli186534a2014-05-22 09:47:46 -07002103 /* In the case of a fixed PHY, the DT node associated
2104 * to the PHY is the Ethernet MAC DT node.
2105 */
2106 if (of_phy_is_fixed_link(dn)) {
2107 ret = of_phy_register_fixed_link(dn);
2108 if (ret) {
2109 dev_err(&pdev->dev, "failed to register fixed PHY\n");
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002110 goto err_free_netdev;
Florian Fainelli186534a2014-05-22 09:47:46 -07002111 }
2112
2113 priv->phy_dn = dn;
2114 }
2115
Florian Fainelli80105be2014-04-24 18:08:57 -07002116 /* Initialize netdevice members */
2117 macaddr = of_get_mac_address(dn);
2118 if (!macaddr || !is_valid_ether_addr(macaddr)) {
2119 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
Vaishali Thakkaradb35052015-07-08 10:49:30 +05302120 eth_hw_addr_random(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07002121 } else {
2122 ether_addr_copy(dev->dev_addr, macaddr);
2123 }
2124
2125 SET_NETDEV_DEV(dev, &pdev->dev);
2126 dev_set_drvdata(&pdev->dev, dev);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002127 dev->ethtool_ops = &bcm_sysport_ethtool_ops;
Florian Fainelli80105be2014-04-24 18:08:57 -07002128 dev->netdev_ops = &bcm_sysport_netdev_ops;
2129 netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
2130
2131 /* HW supported features, none enabled by default */
2132 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
2133 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
2134
Florian Fainelli83e82f42014-07-01 21:08:40 -07002135 /* Request the WOL interrupt and advertise suspend if available */
2136 priv->wol_irq_disabled = 1;
2137 ret = devm_request_irq(&pdev->dev, priv->wol_irq,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07002138 bcm_sysport_wol_isr, 0, dev->name, priv);
Florian Fainelli83e82f42014-07-01 21:08:40 -07002139 if (!ret)
2140 device_set_wakeup_capable(&pdev->dev, 1);
2141
Florian Fainelli80105be2014-04-24 18:08:57 -07002142 /* Set the needed headroom once and for all */
Paul Gortmaker3afc5572014-05-30 15:39:30 -04002143 BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
2144 dev->needed_headroom += sizeof(struct bcm_tsb);
Florian Fainelli80105be2014-04-24 18:08:57 -07002145
Florian Fainellif532e742014-06-05 10:22:18 -07002146 /* libphy will adjust the link state accordingly */
2147 netif_carrier_off(dev);
2148
kiki good10377ba2017-08-04 00:07:45 +01002149 u64_stats_init(&priv->syncp);
2150
Florian Fainelli80105be2014-04-24 18:08:57 -07002151 ret = register_netdev(dev);
2152 if (ret) {
2153 dev_err(&pdev->dev, "failed to register net_device\n");
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002154 goto err_deregister_fixed_link;
Florian Fainelli80105be2014-04-24 18:08:57 -07002155 }
2156
2157 priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
2158 dev_info(&pdev->dev,
Florian Fainelli44a45242017-01-20 11:08:27 -08002159 "Broadcom SYSTEMPORT%s" REV_FMT
Florian Fainelli23acb2f2014-07-09 17:36:46 -07002160 " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
Florian Fainelli44a45242017-01-20 11:08:27 -08002161 priv->is_lite ? " Lite" : "",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07002162 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
2163 priv->base, priv->irq0, priv->irq1, txq, rxq);
Florian Fainelli80105be2014-04-24 18:08:57 -07002164
2165 return 0;
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002166
2167err_deregister_fixed_link:
2168 if (of_phy_is_fixed_link(dn))
2169 of_phy_deregister_fixed_link(dn);
2170err_free_netdev:
Florian Fainelli80105be2014-04-24 18:08:57 -07002171 free_netdev(dev);
2172 return ret;
2173}
2174
2175static int bcm_sysport_remove(struct platform_device *pdev)
2176{
2177 struct net_device *dev = dev_get_drvdata(&pdev->dev);
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002178 struct device_node *dn = pdev->dev.of_node;
Florian Fainelli80105be2014-04-24 18:08:57 -07002179
2180 /* Not much to do, ndo_close has been called
2181 * and we use managed allocations
2182 */
2183 unregister_netdev(dev);
Johan Hovold39f8b0d2016-11-28 19:24:58 +01002184 if (of_phy_is_fixed_link(dn))
2185 of_phy_deregister_fixed_link(dn);
Florian Fainelli80105be2014-04-24 18:08:57 -07002186 free_netdev(dev);
2187 dev_set_drvdata(&pdev->dev, NULL);
2188
2189 return 0;
2190}
2191
Florian Fainelli40755a02014-07-01 21:08:38 -07002192#ifdef CONFIG_PM_SLEEP
Florian Fainelli83e82f42014-07-01 21:08:40 -07002193static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
2194{
2195 struct net_device *ndev = priv->netdev;
2196 unsigned int timeout = 1000;
2197 u32 reg;
2198
2199 /* Password has already been programmed */
2200 reg = umac_readl(priv, UMAC_MPD_CTRL);
2201 reg |= MPD_EN;
2202 reg &= ~PSW_EN;
2203 if (priv->wolopts & WAKE_MAGICSECURE)
2204 reg |= PSW_EN;
2205 umac_writel(priv, reg, UMAC_MPD_CTRL);
2206
2207 /* Make sure RBUF entered WoL mode as result */
2208 do {
2209 reg = rbuf_readl(priv, RBUF_STATUS);
2210 if (reg & RBUF_WOL_MODE)
2211 break;
2212
2213 udelay(10);
2214 } while (timeout-- > 0);
2215
2216 /* Do not leave the UniMAC RBUF matching only MPD packets */
2217 if (!timeout) {
2218 reg = umac_readl(priv, UMAC_MPD_CTRL);
2219 reg &= ~MPD_EN;
2220 umac_writel(priv, reg, UMAC_MPD_CTRL);
2221 netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
2222 return -ETIMEDOUT;
2223 }
2224
2225 /* UniMAC receive needs to be turned on */
2226 umac_enable_set(priv, CMD_RX_EN, 1);
2227
2228 /* Enable the interrupt wake-up source */
2229 intrl2_0_mask_clear(priv, INTRL2_0_MPD);
2230
2231 netif_dbg(priv, wol, ndev, "entered WOL mode\n");
2232
2233 return 0;
2234}
2235
Florian Fainelli40755a02014-07-01 21:08:38 -07002236static int bcm_sysport_suspend(struct device *d)
2237{
2238 struct net_device *dev = dev_get_drvdata(d);
2239 struct bcm_sysport_priv *priv = netdev_priv(dev);
2240 unsigned int i;
Florian Fainelli83e82f42014-07-01 21:08:40 -07002241 int ret = 0;
Florian Fainelli40755a02014-07-01 21:08:38 -07002242 u32 reg;
2243
2244 if (!netif_running(dev))
2245 return 0;
2246
2247 bcm_sysport_netif_stop(dev);
2248
Philippe Reynes715a0222016-06-19 20:39:08 +02002249 phy_suspend(dev->phydev);
Florian Fainelli40755a02014-07-01 21:08:38 -07002250
2251 netif_device_detach(dev);
2252
2253 /* Disable UniMAC RX */
2254 umac_enable_set(priv, CMD_RX_EN, 0);
2255
2256 ret = rdma_enable_set(priv, 0);
2257 if (ret) {
2258 netdev_err(dev, "RDMA timeout!\n");
2259 return ret;
2260 }
2261
2262 /* Disable RXCHK if enabled */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -07002263 if (priv->rx_chk_en) {
Florian Fainelli40755a02014-07-01 21:08:38 -07002264 reg = rxchk_readl(priv, RXCHK_CONTROL);
2265 reg &= ~RXCHK_EN;
2266 rxchk_writel(priv, reg, RXCHK_CONTROL);
2267 }
2268
2269 /* Flush RX pipe */
Florian Fainelli83e82f42014-07-01 21:08:40 -07002270 if (!priv->wolopts)
2271 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
Florian Fainelli40755a02014-07-01 21:08:38 -07002272
2273 ret = tdma_enable_set(priv, 0);
2274 if (ret) {
2275 netdev_err(dev, "TDMA timeout!\n");
2276 return ret;
2277 }
2278
2279 /* Wait for a packet boundary */
2280 usleep_range(2000, 3000);
2281
2282 umac_enable_set(priv, CMD_TX_EN, 0);
2283
2284 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
2285
2286 /* Free RX/TX rings SW structures */
2287 for (i = 0; i < dev->num_tx_queues; i++)
2288 bcm_sysport_fini_tx_ring(priv, i);
2289 bcm_sysport_fini_rx_ring(priv);
2290
Florian Fainelli83e82f42014-07-01 21:08:40 -07002291 /* Get prepared for Wake-on-LAN */
2292 if (device_may_wakeup(d) && priv->wolopts)
2293 ret = bcm_sysport_suspend_to_wol(priv);
2294
2295 return ret;
Florian Fainelli40755a02014-07-01 21:08:38 -07002296}
2297
2298static int bcm_sysport_resume(struct device *d)
2299{
2300 struct net_device *dev = dev_get_drvdata(d);
2301 struct bcm_sysport_priv *priv = netdev_priv(dev);
2302 unsigned int i;
2303 u32 reg;
2304 int ret;
2305
2306 if (!netif_running(dev))
2307 return 0;
2308
Florian Fainelli704d33e2014-10-28 11:12:01 -07002309 umac_reset(priv);
2310
Florian Fainelli83e82f42014-07-01 21:08:40 -07002311 /* We may have been suspended and never received a WOL event that
2312 * would turn off MPD detection, take care of that now
2313 */
2314 bcm_sysport_resume_from_wol(priv);
2315
Florian Fainelli40755a02014-07-01 21:08:38 -07002316 /* Initialize both hardware and software ring */
2317 for (i = 0; i < dev->num_tx_queues; i++) {
2318 ret = bcm_sysport_init_tx_ring(priv, i);
2319 if (ret) {
2320 netdev_err(dev, "failed to initialize TX ring %d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07002321 i);
Florian Fainelli40755a02014-07-01 21:08:38 -07002322 goto out_free_tx_rings;
2323 }
2324 }
2325
2326 /* Initialize linked-list */
2327 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
2328
2329 /* Initialize RX ring */
2330 ret = bcm_sysport_init_rx_ring(priv);
2331 if (ret) {
2332 netdev_err(dev, "failed to initialize RX ring\n");
2333 goto out_free_rx_ring;
2334 }
2335
2336 netif_device_attach(dev);
2337
Florian Fainelli40755a02014-07-01 21:08:38 -07002338 /* RX pipe enable */
2339 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
2340
2341 ret = rdma_enable_set(priv, 1);
2342 if (ret) {
2343 netdev_err(dev, "failed to enable RDMA\n");
2344 goto out_free_rx_ring;
2345 }
2346
2347 /* Enable rxhck */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -07002348 if (priv->rx_chk_en) {
Florian Fainelli40755a02014-07-01 21:08:38 -07002349 reg = rxchk_readl(priv, RXCHK_CONTROL);
2350 reg |= RXCHK_EN;
2351 rxchk_writel(priv, reg, RXCHK_CONTROL);
2352 }
2353
2354 rbuf_init(priv);
2355
2356 /* Set maximum frame length */
Florian Fainelli44a45242017-01-20 11:08:27 -08002357 if (!priv->is_lite)
2358 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
2359 else
2360 gib_set_pad_extension(priv);
Florian Fainelli40755a02014-07-01 21:08:38 -07002361
2362 /* Set MAC address */
2363 umac_set_hw_addr(priv, dev->dev_addr);
2364
2365 umac_enable_set(priv, CMD_RX_EN, 1);
2366
2367 /* TX pipe enable */
2368 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
2369
2370 umac_enable_set(priv, CMD_TX_EN, 1);
2371
2372 ret = tdma_enable_set(priv, 1);
2373 if (ret) {
2374 netdev_err(dev, "TDMA timeout!\n");
2375 goto out_free_rx_ring;
2376 }
2377
Philippe Reynes715a0222016-06-19 20:39:08 +02002378 phy_resume(dev->phydev);
Florian Fainelli40755a02014-07-01 21:08:38 -07002379
2380 bcm_sysport_netif_start(dev);
2381
2382 return 0;
2383
2384out_free_rx_ring:
2385 bcm_sysport_fini_rx_ring(priv);
2386out_free_tx_rings:
2387 for (i = 0; i < dev->num_tx_queues; i++)
2388 bcm_sysport_fini_tx_ring(priv, i);
2389 return ret;
2390}
2391#endif
2392
2393static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
2394 bcm_sysport_suspend, bcm_sysport_resume);
2395
Florian Fainelli80105be2014-04-24 18:08:57 -07002396static struct platform_driver bcm_sysport_driver = {
2397 .probe = bcm_sysport_probe,
2398 .remove = bcm_sysport_remove,
2399 .driver = {
2400 .name = "brcm-systemport",
Florian Fainelli80105be2014-04-24 18:08:57 -07002401 .of_match_table = bcm_sysport_of_match,
Florian Fainelli40755a02014-07-01 21:08:38 -07002402 .pm = &bcm_sysport_pm_ops,
Florian Fainelli80105be2014-04-24 18:08:57 -07002403 },
2404};
2405module_platform_driver(bcm_sysport_driver);
2406
2407MODULE_AUTHOR("Broadcom Corporation");
2408MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
2409MODULE_ALIAS("platform:brcm-systemport");
2410MODULE_LICENSE("GPL");