blob: 71068d7d930dd2b51ebee496beeec208d50469b7 [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>
25#include <net/ip.h>
26#include <net/ipv6.h>
27
28#include "bcmsysport.h"
29
30/* I/O accessors register helpers */
31#define BCM_SYSPORT_IO_MACRO(name, offset) \
32static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
33{ \
34 u32 reg = __raw_readl(priv->base + offset + off); \
35 return reg; \
36} \
37static inline void name##_writel(struct bcm_sysport_priv *priv, \
38 u32 val, u32 off) \
39{ \
40 __raw_writel(val, priv->base + offset + off); \
41} \
42
43BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
44BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
45BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
46BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
47BCM_SYSPORT_IO_MACRO(rdma, SYS_PORT_RDMA_OFFSET);
48BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
49BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
50BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
51BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
52BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
53
54/* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
55 * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
56 */
57#define BCM_SYSPORT_INTR_L2(which) \
58static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
59 u32 mask) \
60{ \
61 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
62 priv->irq##which##_mask &= ~(mask); \
63} \
64static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
65 u32 mask) \
66{ \
67 intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
68 priv->irq##which##_mask |= (mask); \
69} \
70
71BCM_SYSPORT_INTR_L2(0)
72BCM_SYSPORT_INTR_L2(1)
73
74/* Register accesses to GISB/RBUS registers are expensive (few hundred
75 * nanoseconds), so keep the check for 64-bits explicit here to save
76 * one register write per-packet on 32-bits platforms.
77 */
78static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
79 void __iomem *d,
80 dma_addr_t addr)
81{
82#ifdef CONFIG_PHYS_ADDR_T_64BIT
83 __raw_writel(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
Florian Fainelli23acb2f2014-07-09 17:36:46 -070084 d + DESC_ADDR_HI_STATUS_LEN);
Florian Fainelli80105be2014-04-24 18:08:57 -070085#endif
86 __raw_writel(lower_32_bits(addr), d + DESC_ADDR_LO);
87}
88
89static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -070090 struct dma_desc *desc,
91 unsigned int port)
Florian Fainelli80105be2014-04-24 18:08:57 -070092{
93 /* Ports are latched, so write upper address first */
94 tdma_writel(priv, desc->addr_status_len, TDMA_WRITE_PORT_HI(port));
95 tdma_writel(priv, desc->addr_lo, TDMA_WRITE_PORT_LO(port));
96}
97
98/* Ethtool operations */
99static int bcm_sysport_set_settings(struct net_device *dev,
100 struct ethtool_cmd *cmd)
101{
102 struct bcm_sysport_priv *priv = netdev_priv(dev);
103
104 if (!netif_running(dev))
105 return -EINVAL;
106
107 return phy_ethtool_sset(priv->phydev, cmd);
108}
109
110static int bcm_sysport_get_settings(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700111 struct ethtool_cmd *cmd)
Florian Fainelli80105be2014-04-24 18:08:57 -0700112{
113 struct bcm_sysport_priv *priv = netdev_priv(dev);
114
115 if (!netif_running(dev))
116 return -EINVAL;
117
118 return phy_ethtool_gset(priv->phydev, cmd);
119}
120
121static int bcm_sysport_set_rx_csum(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700122 netdev_features_t wanted)
Florian Fainelli80105be2014-04-24 18:08:57 -0700123{
124 struct bcm_sysport_priv *priv = netdev_priv(dev);
125 u32 reg;
126
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700127 priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
Florian Fainelli80105be2014-04-24 18:08:57 -0700128 reg = rxchk_readl(priv, RXCHK_CONTROL);
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700129 if (priv->rx_chk_en)
Florian Fainelli80105be2014-04-24 18:08:57 -0700130 reg |= RXCHK_EN;
131 else
132 reg &= ~RXCHK_EN;
133
134 /* If UniMAC forwards CRC, we need to skip over it to get
135 * a valid CHK bit to be set in the per-packet status word
136 */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -0700137 if (priv->rx_chk_en && priv->crc_fwd)
Florian Fainelli80105be2014-04-24 18:08:57 -0700138 reg |= RXCHK_SKIP_FCS;
139 else
140 reg &= ~RXCHK_SKIP_FCS;
141
142 rxchk_writel(priv, reg, RXCHK_CONTROL);
143
144 return 0;
145}
146
147static int bcm_sysport_set_tx_csum(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700148 netdev_features_t wanted)
Florian Fainelli80105be2014-04-24 18:08:57 -0700149{
150 struct bcm_sysport_priv *priv = netdev_priv(dev);
151 u32 reg;
152
153 /* Hardware transmit checksum requires us to enable the Transmit status
154 * block prepended to the packet contents
155 */
156 priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
157 reg = tdma_readl(priv, TDMA_CONTROL);
158 if (priv->tsb_en)
159 reg |= TSB_EN;
160 else
161 reg &= ~TSB_EN;
162 tdma_writel(priv, reg, TDMA_CONTROL);
163
164 return 0;
165}
166
167static int bcm_sysport_set_features(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700168 netdev_features_t features)
Florian Fainelli80105be2014-04-24 18:08:57 -0700169{
170 netdev_features_t changed = features ^ dev->features;
171 netdev_features_t wanted = dev->wanted_features;
172 int ret = 0;
173
174 if (changed & NETIF_F_RXCSUM)
175 ret = bcm_sysport_set_rx_csum(dev, wanted);
176 if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
177 ret = bcm_sysport_set_tx_csum(dev, wanted);
178
179 return ret;
180}
181
182/* Hardware counters must be kept in sync because the order/offset
183 * is important here (order in structure declaration = order in hardware)
184 */
185static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
186 /* general stats */
187 STAT_NETDEV(rx_packets),
188 STAT_NETDEV(tx_packets),
189 STAT_NETDEV(rx_bytes),
190 STAT_NETDEV(tx_bytes),
191 STAT_NETDEV(rx_errors),
192 STAT_NETDEV(tx_errors),
193 STAT_NETDEV(rx_dropped),
194 STAT_NETDEV(tx_dropped),
195 STAT_NETDEV(multicast),
196 /* UniMAC RSV counters */
197 STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
198 STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
199 STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
200 STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
201 STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
202 STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
203 STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
204 STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
205 STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
206 STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
207 STAT_MIB_RX("rx_pkts", mib.rx.pkt),
208 STAT_MIB_RX("rx_bytes", mib.rx.bytes),
209 STAT_MIB_RX("rx_multicast", mib.rx.mca),
210 STAT_MIB_RX("rx_broadcast", mib.rx.bca),
211 STAT_MIB_RX("rx_fcs", mib.rx.fcs),
212 STAT_MIB_RX("rx_control", mib.rx.cf),
213 STAT_MIB_RX("rx_pause", mib.rx.pf),
214 STAT_MIB_RX("rx_unknown", mib.rx.uo),
215 STAT_MIB_RX("rx_align", mib.rx.aln),
216 STAT_MIB_RX("rx_outrange", mib.rx.flr),
217 STAT_MIB_RX("rx_code", mib.rx.cde),
218 STAT_MIB_RX("rx_carrier", mib.rx.fcr),
219 STAT_MIB_RX("rx_oversize", mib.rx.ovr),
220 STAT_MIB_RX("rx_jabber", mib.rx.jbr),
221 STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
222 STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
223 STAT_MIB_RX("rx_unicast", mib.rx.uc),
224 STAT_MIB_RX("rx_ppp", mib.rx.ppp),
225 STAT_MIB_RX("rx_crc", mib.rx.rcrc),
226 /* UniMAC TSV counters */
227 STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
228 STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
229 STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
230 STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
231 STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
232 STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
233 STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
234 STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
235 STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
236 STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
237 STAT_MIB_TX("tx_pkts", mib.tx.pkts),
238 STAT_MIB_TX("tx_multicast", mib.tx.mca),
239 STAT_MIB_TX("tx_broadcast", mib.tx.bca),
240 STAT_MIB_TX("tx_pause", mib.tx.pf),
241 STAT_MIB_TX("tx_control", mib.tx.cf),
242 STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
243 STAT_MIB_TX("tx_oversize", mib.tx.ovr),
244 STAT_MIB_TX("tx_defer", mib.tx.drf),
245 STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
246 STAT_MIB_TX("tx_single_col", mib.tx.scl),
247 STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
248 STAT_MIB_TX("tx_late_col", mib.tx.lcl),
249 STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
250 STAT_MIB_TX("tx_frags", mib.tx.frg),
251 STAT_MIB_TX("tx_total_col", mib.tx.ncl),
252 STAT_MIB_TX("tx_jabber", mib.tx.jbr),
253 STAT_MIB_TX("tx_bytes", mib.tx.bytes),
254 STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
255 STAT_MIB_TX("tx_unicast", mib.tx.uc),
256 /* UniMAC RUNT counters */
257 STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
258 STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
259 STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
260 STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
261 /* RXCHK misc statistics */
262 STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
263 STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700264 RXCHK_OTHER_DISC_CNTR),
Florian Fainelli80105be2014-04-24 18:08:57 -0700265 /* RBUF misc statistics */
266 STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
267 STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
268};
269
270#define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
271
272static void bcm_sysport_get_drvinfo(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700273 struct ethtool_drvinfo *info)
Florian Fainelli80105be2014-04-24 18:08:57 -0700274{
275 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
276 strlcpy(info->version, "0.1", sizeof(info->version));
277 strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
278 info->n_stats = BCM_SYSPORT_STATS_LEN;
279}
280
281static u32 bcm_sysport_get_msglvl(struct net_device *dev)
282{
283 struct bcm_sysport_priv *priv = netdev_priv(dev);
284
285 return priv->msg_enable;
286}
287
288static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
289{
290 struct bcm_sysport_priv *priv = netdev_priv(dev);
291
292 priv->msg_enable = enable;
293}
294
295static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
296{
297 switch (string_set) {
298 case ETH_SS_STATS:
299 return BCM_SYSPORT_STATS_LEN;
300 default:
301 return -EOPNOTSUPP;
302 }
303}
304
305static void bcm_sysport_get_strings(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700306 u32 stringset, u8 *data)
Florian Fainelli80105be2014-04-24 18:08:57 -0700307{
308 int i;
309
310 switch (stringset) {
311 case ETH_SS_STATS:
312 for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
313 memcpy(data + i * ETH_GSTRING_LEN,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700314 bcm_sysport_gstrings_stats[i].stat_string,
315 ETH_GSTRING_LEN);
Florian Fainelli80105be2014-04-24 18:08:57 -0700316 }
317 break;
318 default:
319 break;
320 }
321}
322
323static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
324{
325 int i, j = 0;
326
327 for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
328 const struct bcm_sysport_stats *s;
329 u8 offset = 0;
330 u32 val = 0;
331 char *p;
332
333 s = &bcm_sysport_gstrings_stats[i];
334 switch (s->type) {
335 case BCM_SYSPORT_STAT_NETDEV:
336 continue;
337 case BCM_SYSPORT_STAT_MIB_RX:
338 case BCM_SYSPORT_STAT_MIB_TX:
339 case BCM_SYSPORT_STAT_RUNT:
340 if (s->type != BCM_SYSPORT_STAT_MIB_RX)
341 offset = UMAC_MIB_STAT_OFFSET;
342 val = umac_readl(priv, UMAC_MIB_START + j + offset);
343 break;
344 case BCM_SYSPORT_STAT_RXCHK:
345 val = rxchk_readl(priv, s->reg_offset);
346 if (val == ~0)
347 rxchk_writel(priv, 0, s->reg_offset);
348 break;
349 case BCM_SYSPORT_STAT_RBUF:
350 val = rbuf_readl(priv, s->reg_offset);
351 if (val == ~0)
352 rbuf_writel(priv, 0, s->reg_offset);
353 break;
354 }
355
356 j += s->stat_sizeof;
357 p = (char *)priv + s->stat_offset;
358 *(u32 *)p = val;
359 }
360
361 netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
362}
363
364static void bcm_sysport_get_stats(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700365 struct ethtool_stats *stats, u64 *data)
Florian Fainelli80105be2014-04-24 18:08:57 -0700366{
367 struct bcm_sysport_priv *priv = netdev_priv(dev);
368 int i;
369
370 if (netif_running(dev))
371 bcm_sysport_update_mib_counters(priv);
372
373 for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
374 const struct bcm_sysport_stats *s;
375 char *p;
376
377 s = &bcm_sysport_gstrings_stats[i];
378 if (s->type == BCM_SYSPORT_STAT_NETDEV)
379 p = (char *)&dev->stats;
380 else
381 p = (char *)priv;
382 p += s->stat_offset;
383 data[i] = *(u32 *)p;
384 }
385}
386
Florian Fainelli83e82f42014-07-01 21:08:40 -0700387static void bcm_sysport_get_wol(struct net_device *dev,
388 struct ethtool_wolinfo *wol)
389{
390 struct bcm_sysport_priv *priv = netdev_priv(dev);
391 u32 reg;
392
393 wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
394 wol->wolopts = priv->wolopts;
395
396 if (!(priv->wolopts & WAKE_MAGICSECURE))
397 return;
398
399 /* Return the programmed SecureOn password */
400 reg = umac_readl(priv, UMAC_PSW_MS);
401 put_unaligned_be16(reg, &wol->sopass[0]);
402 reg = umac_readl(priv, UMAC_PSW_LS);
403 put_unaligned_be32(reg, &wol->sopass[2]);
404}
405
406static int bcm_sysport_set_wol(struct net_device *dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700407 struct ethtool_wolinfo *wol)
Florian Fainelli83e82f42014-07-01 21:08:40 -0700408{
409 struct bcm_sysport_priv *priv = netdev_priv(dev);
410 struct device *kdev = &priv->pdev->dev;
411 u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE;
412
413 if (!device_can_wakeup(kdev))
414 return -ENOTSUPP;
415
416 if (wol->wolopts & ~supported)
417 return -EINVAL;
418
419 /* Program the SecureOn password */
420 if (wol->wolopts & WAKE_MAGICSECURE) {
421 umac_writel(priv, get_unaligned_be16(&wol->sopass[0]),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700422 UMAC_PSW_MS);
Florian Fainelli83e82f42014-07-01 21:08:40 -0700423 umac_writel(priv, get_unaligned_be32(&wol->sopass[2]),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700424 UMAC_PSW_LS);
Florian Fainelli83e82f42014-07-01 21:08:40 -0700425 }
426
427 /* Flag the device and relevant IRQ as wakeup capable */
428 if (wol->wolopts) {
429 device_set_wakeup_enable(kdev, 1);
430 enable_irq_wake(priv->wol_irq);
431 priv->wol_irq_disabled = 0;
432 } else {
433 device_set_wakeup_enable(kdev, 0);
434 /* Avoid unbalanced disable_irq_wake calls */
435 if (!priv->wol_irq_disabled)
436 disable_irq_wake(priv->wol_irq);
437 priv->wol_irq_disabled = 1;
438 }
439
440 priv->wolopts = wol->wolopts;
441
442 return 0;
443}
444
Florian Fainelli80105be2014-04-24 18:08:57 -0700445static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
446{
447 dev_kfree_skb_any(cb->skb);
448 cb->skb = NULL;
449 dma_unmap_addr_set(cb, dma_addr, 0);
450}
451
452static int bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
453 struct bcm_sysport_cb *cb)
454{
455 struct device *kdev = &priv->pdev->dev;
456 struct net_device *ndev = priv->netdev;
457 dma_addr_t mapping;
458 int ret;
459
460 cb->skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
461 if (!cb->skb) {
462 netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
463 return -ENOMEM;
464 }
465
466 mapping = dma_map_single(kdev, cb->skb->data,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700467 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700468 ret = dma_mapping_error(kdev, mapping);
469 if (ret) {
470 bcm_sysport_free_cb(cb);
471 netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
472 return ret;
473 }
474
475 dma_unmap_addr_set(cb, dma_addr, mapping);
476 dma_desc_set_addr(priv, priv->rx_bd_assign_ptr, mapping);
477
478 priv->rx_bd_assign_index++;
479 priv->rx_bd_assign_index &= (priv->num_rx_bds - 1);
480 priv->rx_bd_assign_ptr = priv->rx_bds +
481 (priv->rx_bd_assign_index * DESC_SIZE);
482
483 netif_dbg(priv, rx_status, ndev, "RX refill\n");
484
485 return 0;
486}
487
488static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
489{
490 struct bcm_sysport_cb *cb;
491 int ret = 0;
492 unsigned int i;
493
494 for (i = 0; i < priv->num_rx_bds; i++) {
495 cb = &priv->rx_cbs[priv->rx_bd_assign_index];
496 if (cb->skb)
497 continue;
498
499 ret = bcm_sysport_rx_refill(priv, cb);
500 if (ret)
501 break;
502 }
503
504 return ret;
505}
506
507/* Poll the hardware for up to budget packets to process */
508static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
509 unsigned int budget)
510{
511 struct device *kdev = &priv->pdev->dev;
512 struct net_device *ndev = priv->netdev;
513 unsigned int processed = 0, to_process;
514 struct bcm_sysport_cb *cb;
515 struct sk_buff *skb;
516 unsigned int p_index;
517 u16 len, status;
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400518 struct bcm_rsb *rsb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700519
520 /* Determine how much we should process since last call */
521 p_index = rdma_readl(priv, RDMA_PROD_INDEX);
522 p_index &= RDMA_PROD_INDEX_MASK;
523
524 if (p_index < priv->rx_c_index)
525 to_process = (RDMA_CONS_INDEX_MASK + 1) -
526 priv->rx_c_index + p_index;
527 else
528 to_process = p_index - priv->rx_c_index;
529
530 netif_dbg(priv, rx_status, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700531 "p_index=%d rx_c_index=%d to_process=%d\n",
532 p_index, priv->rx_c_index, to_process);
Florian Fainelli80105be2014-04-24 18:08:57 -0700533
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700534 while ((processed < to_process) && (processed < budget)) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700535 cb = &priv->rx_cbs[priv->rx_read_ptr];
536 skb = cb->skb;
Florian Fainellife24ba02014-09-08 11:37:51 -0700537
538 processed++;
539 priv->rx_read_ptr++;
540
541 if (priv->rx_read_ptr == priv->num_rx_bds)
542 priv->rx_read_ptr = 0;
543
544 /* We do not have a backing SKB, so we do not a corresponding
545 * DMA mapping for this incoming packet since
546 * bcm_sysport_rx_refill always either has both skb and mapping
547 * or none.
548 */
549 if (unlikely(!skb)) {
550 netif_err(priv, rx_err, ndev, "out of memory!\n");
551 ndev->stats.rx_dropped++;
552 ndev->stats.rx_errors++;
553 goto refill;
554 }
555
Florian Fainelli80105be2014-04-24 18:08:57 -0700556 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700557 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700558
559 /* Extract the Receive Status Block prepended */
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400560 rsb = (struct bcm_rsb *)skb->data;
Florian Fainelli80105be2014-04-24 18:08:57 -0700561 len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
562 status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700563 DESC_STATUS_MASK;
Florian Fainelli80105be2014-04-24 18:08:57 -0700564
Florian Fainelli80105be2014-04-24 18:08:57 -0700565 netif_dbg(priv, rx_status, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700566 "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
567 p_index, priv->rx_c_index, priv->rx_read_ptr,
568 len, status);
Florian Fainelli80105be2014-04-24 18:08:57 -0700569
Florian Fainelli80105be2014-04-24 18:08:57 -0700570 if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
571 netif_err(priv, rx_status, ndev, "fragmented packet!\n");
572 ndev->stats.rx_dropped++;
573 ndev->stats.rx_errors++;
574 bcm_sysport_free_cb(cb);
575 goto refill;
576 }
577
578 if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
579 netif_err(priv, rx_err, ndev, "error packet\n");
Florian Fainelliad51c612014-06-05 10:22:16 -0700580 if (status & RX_STATUS_OVFLOW)
Florian Fainelli80105be2014-04-24 18:08:57 -0700581 ndev->stats.rx_over_errors++;
582 ndev->stats.rx_dropped++;
583 ndev->stats.rx_errors++;
584 bcm_sysport_free_cb(cb);
585 goto refill;
586 }
587
588 skb_put(skb, len);
589
590 /* Hardware validated our checksum */
591 if (likely(status & DESC_L4_CSUM))
592 skb->ip_summed = CHECKSUM_UNNECESSARY;
593
Florian Fainellie0ea05d2014-06-05 10:22:17 -0700594 /* Hardware pre-pends packets with 2bytes before Ethernet
595 * header plus we have the Receive Status Block, strip off all
596 * of this from the SKB.
Florian Fainelli80105be2014-04-24 18:08:57 -0700597 */
598 skb_pull(skb, sizeof(*rsb) + 2);
599 len -= (sizeof(*rsb) + 2);
600
601 /* UniMAC may forward CRC */
602 if (priv->crc_fwd) {
603 skb_trim(skb, len - ETH_FCS_LEN);
604 len -= ETH_FCS_LEN;
605 }
606
607 skb->protocol = eth_type_trans(skb, ndev);
608 ndev->stats.rx_packets++;
609 ndev->stats.rx_bytes += len;
610
611 napi_gro_receive(&priv->napi, skb);
612refill:
613 bcm_sysport_rx_refill(priv, cb);
614 }
615
616 return processed;
617}
618
619static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700620 struct bcm_sysport_cb *cb,
621 unsigned int *bytes_compl,
622 unsigned int *pkts_compl)
Florian Fainelli80105be2014-04-24 18:08:57 -0700623{
624 struct device *kdev = &priv->pdev->dev;
625 struct net_device *ndev = priv->netdev;
626
627 if (cb->skb) {
628 ndev->stats.tx_bytes += cb->skb->len;
629 *bytes_compl += cb->skb->len;
630 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700631 dma_unmap_len(cb, dma_len),
632 DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700633 ndev->stats.tx_packets++;
634 (*pkts_compl)++;
635 bcm_sysport_free_cb(cb);
636 /* SKB fragment */
637 } else if (dma_unmap_addr(cb, dma_addr)) {
638 ndev->stats.tx_bytes += dma_unmap_len(cb, dma_len);
639 dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700640 dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700641 dma_unmap_addr_set(cb, dma_addr, 0);
642 }
643}
644
645/* Reclaim queued SKBs for transmission completion, lockless version */
646static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
647 struct bcm_sysport_tx_ring *ring)
648{
649 struct net_device *ndev = priv->netdev;
650 unsigned int c_index, last_c_index, last_tx_cn, num_tx_cbs;
651 unsigned int pkts_compl = 0, bytes_compl = 0;
652 struct bcm_sysport_cb *cb;
653 struct netdev_queue *txq;
654 u32 hw_ind;
655
656 txq = netdev_get_tx_queue(ndev, ring->index);
657
658 /* Compute how many descriptors have been processed since last call */
659 hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
660 c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
661 ring->p_index = (hw_ind & RING_PROD_INDEX_MASK);
662
663 last_c_index = ring->c_index;
664 num_tx_cbs = ring->size;
665
666 c_index &= (num_tx_cbs - 1);
667
668 if (c_index >= last_c_index)
669 last_tx_cn = c_index - last_c_index;
670 else
671 last_tx_cn = num_tx_cbs - last_c_index + c_index;
672
673 netif_dbg(priv, tx_done, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700674 "ring=%d c_index=%d last_tx_cn=%d last_c_index=%d\n",
675 ring->index, c_index, last_tx_cn, last_c_index);
Florian Fainelli80105be2014-04-24 18:08:57 -0700676
677 while (last_tx_cn-- > 0) {
678 cb = ring->cbs + last_c_index;
679 bcm_sysport_tx_reclaim_one(priv, cb, &bytes_compl, &pkts_compl);
680
681 ring->desc_count++;
682 last_c_index++;
683 last_c_index &= (num_tx_cbs - 1);
684 }
685
686 ring->c_index = c_index;
687
688 if (netif_tx_queue_stopped(txq) && pkts_compl)
689 netif_tx_wake_queue(txq);
690
691 netif_dbg(priv, tx_done, ndev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700692 "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
693 ring->index, ring->c_index, pkts_compl, bytes_compl);
Florian Fainelli80105be2014-04-24 18:08:57 -0700694
695 return pkts_compl;
696}
697
698/* Locked version of the per-ring TX reclaim routine */
699static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
700 struct bcm_sysport_tx_ring *ring)
701{
702 unsigned int released;
Florian Fainellid8498082014-06-05 10:22:15 -0700703 unsigned long flags;
Florian Fainelli80105be2014-04-24 18:08:57 -0700704
Florian Fainellid8498082014-06-05 10:22:15 -0700705 spin_lock_irqsave(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -0700706 released = __bcm_sysport_tx_reclaim(priv, ring);
Florian Fainellid8498082014-06-05 10:22:15 -0700707 spin_unlock_irqrestore(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -0700708
709 return released;
710}
711
712static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
713{
714 struct bcm_sysport_tx_ring *ring =
715 container_of(napi, struct bcm_sysport_tx_ring, napi);
716 unsigned int work_done = 0;
717
718 work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
719
Florian Fainelli16f62d92014-06-26 10:06:46 -0700720 if (work_done == 0) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700721 napi_complete(napi);
722 /* re-enable TX interrupt */
723 intrl2_1_mask_clear(ring->priv, BIT(ring->index));
724 }
725
Florian Fainelli16f62d92014-06-26 10:06:46 -0700726 return 0;
Florian Fainelli80105be2014-04-24 18:08:57 -0700727}
728
729static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
730{
731 unsigned int q;
732
733 for (q = 0; q < priv->netdev->num_tx_queues; q++)
734 bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
735}
736
737static int bcm_sysport_poll(struct napi_struct *napi, int budget)
738{
739 struct bcm_sysport_priv *priv =
740 container_of(napi, struct bcm_sysport_priv, napi);
741 unsigned int work_done = 0;
742
743 work_done = bcm_sysport_desc_rx(priv, budget);
744
745 priv->rx_c_index += work_done;
746 priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
747 rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
748
749 if (work_done < budget) {
750 napi_complete(napi);
751 /* re-enable RX interrupts */
752 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
753 }
754
755 return work_done;
756}
757
Florian Fainelli83e82f42014-07-01 21:08:40 -0700758static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
759{
760 u32 reg;
761
762 /* Stop monitoring MPD interrupt */
763 intrl2_0_mask_set(priv, INTRL2_0_MPD);
764
765 /* Clear the MagicPacket detection logic */
766 reg = umac_readl(priv, UMAC_MPD_CTRL);
767 reg &= ~MPD_EN;
768 umac_writel(priv, reg, UMAC_MPD_CTRL);
769
770 netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
771}
Florian Fainelli80105be2014-04-24 18:08:57 -0700772
773/* RX and misc interrupt routine */
774static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
775{
776 struct net_device *dev = dev_id;
777 struct bcm_sysport_priv *priv = netdev_priv(dev);
778
779 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
780 ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
781 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
782
783 if (unlikely(priv->irq0_stat == 0)) {
784 netdev_warn(priv->netdev, "spurious RX interrupt\n");
785 return IRQ_NONE;
786 }
787
788 if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
789 if (likely(napi_schedule_prep(&priv->napi))) {
790 /* disable RX interrupts */
791 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
792 __napi_schedule(&priv->napi);
793 }
794 }
795
796 /* TX ring is full, perform a full reclaim since we do not know
797 * which one would trigger this interrupt
798 */
799 if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
800 bcm_sysport_tx_reclaim_all(priv);
801
Florian Fainelli83e82f42014-07-01 21:08:40 -0700802 if (priv->irq0_stat & INTRL2_0_MPD) {
803 netdev_info(priv->netdev, "Wake-on-LAN interrupt!\n");
804 bcm_sysport_resume_from_wol(priv);
805 }
806
Florian Fainelli80105be2014-04-24 18:08:57 -0700807 return IRQ_HANDLED;
808}
809
810/* TX interrupt service routine */
811static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
812{
813 struct net_device *dev = dev_id;
814 struct bcm_sysport_priv *priv = netdev_priv(dev);
815 struct bcm_sysport_tx_ring *txr;
816 unsigned int ring;
817
818 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
819 ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
820 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
821
822 if (unlikely(priv->irq1_stat == 0)) {
823 netdev_warn(priv->netdev, "spurious TX interrupt\n");
824 return IRQ_NONE;
825 }
826
827 for (ring = 0; ring < dev->num_tx_queues; ring++) {
828 if (!(priv->irq1_stat & BIT(ring)))
829 continue;
830
831 txr = &priv->tx_rings[ring];
832
833 if (likely(napi_schedule_prep(&txr->napi))) {
834 intrl2_1_mask_set(priv, BIT(ring));
835 __napi_schedule(&txr->napi);
836 }
837 }
838
839 return IRQ_HANDLED;
840}
841
Florian Fainelli83e82f42014-07-01 21:08:40 -0700842static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
843{
844 struct bcm_sysport_priv *priv = dev_id;
845
846 pm_wakeup_event(&priv->pdev->dev, 0);
847
848 return IRQ_HANDLED;
849}
850
Florian Fainellie87474a2014-10-02 09:43:16 -0700851static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
852 struct net_device *dev)
Florian Fainelli80105be2014-04-24 18:08:57 -0700853{
854 struct sk_buff *nskb;
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400855 struct bcm_tsb *tsb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700856 u32 csum_info;
857 u8 ip_proto;
858 u16 csum_start;
859 u16 ip_ver;
860
861 /* Re-allocate SKB if needed */
862 if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
863 nskb = skb_realloc_headroom(skb, sizeof(*tsb));
864 dev_kfree_skb(skb);
865 if (!nskb) {
866 dev->stats.tx_errors++;
867 dev->stats.tx_dropped++;
Florian Fainellie87474a2014-10-02 09:43:16 -0700868 return NULL;
Florian Fainelli80105be2014-04-24 18:08:57 -0700869 }
870 skb = nskb;
871 }
872
Paul Gortmaker3afc5572014-05-30 15:39:30 -0400873 tsb = (struct bcm_tsb *)skb_push(skb, sizeof(*tsb));
Florian Fainelli80105be2014-04-24 18:08:57 -0700874 /* Zero-out TSB by default */
875 memset(tsb, 0, sizeof(*tsb));
876
877 if (skb->ip_summed == CHECKSUM_PARTIAL) {
878 ip_ver = htons(skb->protocol);
879 switch (ip_ver) {
880 case ETH_P_IP:
881 ip_proto = ip_hdr(skb)->protocol;
882 break;
883 case ETH_P_IPV6:
884 ip_proto = ipv6_hdr(skb)->nexthdr;
885 break;
886 default:
Florian Fainellie87474a2014-10-02 09:43:16 -0700887 return skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700888 }
889
890 /* Get the checksum offset and the L4 (transport) offset */
891 csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
892 csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
893 csum_info |= (csum_start << L4_PTR_SHIFT);
894
895 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
896 csum_info |= L4_LENGTH_VALID;
897 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
898 csum_info |= L4_UDP;
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700899 } else {
Florian Fainelli80105be2014-04-24 18:08:57 -0700900 csum_info = 0;
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700901 }
Florian Fainelli80105be2014-04-24 18:08:57 -0700902
903 tsb->l4_ptr_dest_map = csum_info;
904 }
905
Florian Fainellie87474a2014-10-02 09:43:16 -0700906 return skb;
Florian Fainelli80105be2014-04-24 18:08:57 -0700907}
908
909static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
910 struct net_device *dev)
911{
912 struct bcm_sysport_priv *priv = netdev_priv(dev);
913 struct device *kdev = &priv->pdev->dev;
914 struct bcm_sysport_tx_ring *ring;
915 struct bcm_sysport_cb *cb;
916 struct netdev_queue *txq;
917 struct dma_desc *desc;
Florian Fainellidab531b2014-05-14 19:32:14 -0700918 unsigned int skb_len;
Florian Fainellid8498082014-06-05 10:22:15 -0700919 unsigned long flags;
Florian Fainelli80105be2014-04-24 18:08:57 -0700920 dma_addr_t mapping;
921 u32 len_status;
922 u16 queue;
923 int ret;
924
925 queue = skb_get_queue_mapping(skb);
926 txq = netdev_get_tx_queue(dev, queue);
927 ring = &priv->tx_rings[queue];
928
Florian Fainellid8498082014-06-05 10:22:15 -0700929 /* lock against tx reclaim in BH context and TX ring full interrupt */
930 spin_lock_irqsave(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -0700931 if (unlikely(ring->desc_count == 0)) {
932 netif_tx_stop_queue(txq);
933 netdev_err(dev, "queue %d awake and ring full!\n", queue);
934 ret = NETDEV_TX_BUSY;
935 goto out;
936 }
937
938 /* Insert TSB and checksum infos */
939 if (priv->tsb_en) {
Florian Fainellie87474a2014-10-02 09:43:16 -0700940 skb = bcm_sysport_insert_tsb(skb, dev);
941 if (!skb) {
Florian Fainelli80105be2014-04-24 18:08:57 -0700942 ret = NETDEV_TX_OK;
943 goto out;
944 }
945 }
946
Florian Fainellidab531b2014-05-14 19:32:14 -0700947 /* The Ethernet switch we are interfaced with needs packets to be at
948 * least 64 bytes (including FCS) otherwise they will be discarded when
949 * they enter the switch port logic. When Broadcom tags are enabled, we
950 * need to make sure that packets are at least 68 bytes
951 * (including FCS and tag) because the length verification is done after
952 * the Broadcom tag is stripped off the ingress packet.
953 */
954 if (skb_padto(skb, ETH_ZLEN + ENET_BRCM_TAG_LEN)) {
955 ret = NETDEV_TX_OK;
956 goto out;
957 }
958
959 skb_len = skb->len < ETH_ZLEN + ENET_BRCM_TAG_LEN ?
960 ETH_ZLEN + ENET_BRCM_TAG_LEN : skb->len;
961
962 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700963 if (dma_mapping_error(kdev, mapping)) {
964 netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700965 skb->data, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -0700966 ret = NETDEV_TX_OK;
967 goto out;
968 }
969
970 /* Remember the SKB for future freeing */
971 cb = &ring->cbs[ring->curr_desc];
972 cb->skb = skb;
973 dma_unmap_addr_set(cb, dma_addr, mapping);
Florian Fainellidab531b2014-05-14 19:32:14 -0700974 dma_unmap_len_set(cb, dma_len, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -0700975
976 /* Fetch a descriptor entry from our pool */
977 desc = ring->desc_cpu;
978
979 desc->addr_lo = lower_32_bits(mapping);
980 len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
Florian Fainellidab531b2014-05-14 19:32:14 -0700981 len_status |= (skb_len << DESC_LEN_SHIFT);
Florian Fainelli80105be2014-04-24 18:08:57 -0700982 len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
Florian Fainelli23acb2f2014-07-09 17:36:46 -0700983 DESC_STATUS_SHIFT;
Florian Fainelli80105be2014-04-24 18:08:57 -0700984 if (skb->ip_summed == CHECKSUM_PARTIAL)
985 len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
986
987 ring->curr_desc++;
988 if (ring->curr_desc == ring->size)
989 ring->curr_desc = 0;
990 ring->desc_count--;
991
992 /* Ensure write completion of the descriptor status/length
993 * in DRAM before the System Port WRITE_PORT register latches
994 * the value
995 */
996 wmb();
997 desc->addr_status_len = len_status;
998 wmb();
999
1000 /* Write this descriptor address to the RING write port */
1001 tdma_port_write_desc_addr(priv, desc, ring->index);
1002
1003 /* Check ring space and update SW control flow */
1004 if (ring->desc_count == 0)
1005 netif_tx_stop_queue(txq);
1006
1007 netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001008 ring->index, ring->desc_count, ring->curr_desc);
Florian Fainelli80105be2014-04-24 18:08:57 -07001009
1010 ret = NETDEV_TX_OK;
1011out:
Florian Fainellid8498082014-06-05 10:22:15 -07001012 spin_unlock_irqrestore(&ring->lock, flags);
Florian Fainelli80105be2014-04-24 18:08:57 -07001013 return ret;
1014}
1015
1016static void bcm_sysport_tx_timeout(struct net_device *dev)
1017{
1018 netdev_warn(dev, "transmit timeout!\n");
1019
1020 dev->trans_start = jiffies;
1021 dev->stats.tx_errors++;
1022
1023 netif_tx_wake_all_queues(dev);
1024}
1025
1026/* phylib adjust link callback */
1027static void bcm_sysport_adj_link(struct net_device *dev)
1028{
1029 struct bcm_sysport_priv *priv = netdev_priv(dev);
1030 struct phy_device *phydev = priv->phydev;
1031 unsigned int changed = 0;
1032 u32 cmd_bits = 0, reg;
1033
1034 if (priv->old_link != phydev->link) {
1035 changed = 1;
1036 priv->old_link = phydev->link;
1037 }
1038
1039 if (priv->old_duplex != phydev->duplex) {
1040 changed = 1;
1041 priv->old_duplex = phydev->duplex;
1042 }
1043
1044 switch (phydev->speed) {
1045 case SPEED_2500:
1046 cmd_bits = CMD_SPEED_2500;
1047 break;
1048 case SPEED_1000:
1049 cmd_bits = CMD_SPEED_1000;
1050 break;
1051 case SPEED_100:
1052 cmd_bits = CMD_SPEED_100;
1053 break;
1054 case SPEED_10:
1055 cmd_bits = CMD_SPEED_10;
1056 break;
1057 default:
1058 break;
1059 }
1060 cmd_bits <<= CMD_SPEED_SHIFT;
1061
1062 if (phydev->duplex == DUPLEX_HALF)
1063 cmd_bits |= CMD_HD_EN;
1064
1065 if (priv->old_pause != phydev->pause) {
1066 changed = 1;
1067 priv->old_pause = phydev->pause;
1068 }
1069
1070 if (!phydev->pause)
1071 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
1072
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001073 if (changed) {
1074 reg = umac_readl(priv, UMAC_CMD);
1075 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
Florian Fainelli80105be2014-04-24 18:08:57 -07001076 CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
1077 CMD_TX_PAUSE_IGNORE);
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001078 reg |= cmd_bits;
1079 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli80105be2014-04-24 18:08:57 -07001080
Florian Fainelli80105be2014-04-24 18:08:57 -07001081 phy_print_status(priv->phydev);
Florian Fainellid5e32cc2014-05-14 19:32:13 -07001082 }
Florian Fainelli80105be2014-04-24 18:08:57 -07001083}
1084
1085static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
1086 unsigned int index)
1087{
1088 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1089 struct device *kdev = &priv->pdev->dev;
1090 size_t size;
1091 void *p;
1092 u32 reg;
1093
1094 /* Simple descriptors partitioning for now */
1095 size = 256;
1096
1097 /* We just need one DMA descriptor which is DMA-able, since writing to
1098 * the port will allocate a new descriptor in its internal linked-list
1099 */
1100 p = dma_zalloc_coherent(kdev, 1, &ring->desc_dma, GFP_KERNEL);
1101 if (!p) {
1102 netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
1103 return -ENOMEM;
1104 }
1105
Florian Fainelli40a8a312014-07-09 17:36:47 -07001106 ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
Florian Fainelli80105be2014-04-24 18:08:57 -07001107 if (!ring->cbs) {
1108 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1109 return -ENOMEM;
1110 }
1111
1112 /* Initialize SW view of the ring */
1113 spin_lock_init(&ring->lock);
1114 ring->priv = priv;
1115 netif_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
1116 ring->index = index;
1117 ring->size = size;
1118 ring->alloc_size = ring->size;
1119 ring->desc_cpu = p;
1120 ring->desc_count = ring->size;
1121 ring->curr_desc = 0;
1122
1123 /* Initialize HW ring */
1124 tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1125 tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1126 tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1127 tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1128 tdma_writel(priv, RING_IGNORE_STATUS, TDMA_DESC_RING_MAPPING(index));
1129 tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
1130
1131 /* Program the number of descriptors as MAX_THRESHOLD and half of
1132 * its size for the hysteresis trigger
1133 */
1134 tdma_writel(priv, ring->size |
1135 1 << RING_HYST_THRESH_SHIFT,
1136 TDMA_DESC_RING_MAX_HYST(index));
1137
1138 /* Enable the ring queue in the arbiter */
1139 reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1140 reg |= (1 << index);
1141 tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1142
1143 napi_enable(&ring->napi);
1144
1145 netif_dbg(priv, hw, priv->netdev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001146 "TDMA cfg, size=%d, desc_cpu=%p\n",
1147 ring->size, ring->desc_cpu);
Florian Fainelli80105be2014-04-24 18:08:57 -07001148
1149 return 0;
1150}
1151
1152static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001153 unsigned int index)
Florian Fainelli80105be2014-04-24 18:08:57 -07001154{
1155 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1156 struct device *kdev = &priv->pdev->dev;
1157 u32 reg;
1158
1159 /* Caller should stop the TDMA engine */
1160 reg = tdma_readl(priv, TDMA_STATUS);
1161 if (!(reg & TDMA_DISABLED))
1162 netdev_warn(priv->netdev, "TDMA not stopped!\n");
1163
1164 napi_disable(&ring->napi);
1165 netif_napi_del(&ring->napi);
1166
1167 bcm_sysport_tx_reclaim(priv, ring);
1168
1169 kfree(ring->cbs);
1170 ring->cbs = NULL;
1171
1172 if (ring->desc_dma) {
1173 dma_free_coherent(kdev, 1, ring->desc_cpu, ring->desc_dma);
1174 ring->desc_dma = 0;
1175 }
1176 ring->size = 0;
1177 ring->alloc_size = 0;
1178
1179 netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1180}
1181
1182/* RDMA helper */
1183static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001184 unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001185{
1186 unsigned int timeout = 1000;
1187 u32 reg;
1188
1189 reg = rdma_readl(priv, RDMA_CONTROL);
1190 if (enable)
1191 reg |= RDMA_EN;
1192 else
1193 reg &= ~RDMA_EN;
1194 rdma_writel(priv, reg, RDMA_CONTROL);
1195
1196 /* Poll for RMDA disabling completion */
1197 do {
1198 reg = rdma_readl(priv, RDMA_STATUS);
1199 if (!!(reg & RDMA_DISABLED) == !enable)
1200 return 0;
1201 usleep_range(1000, 2000);
1202 } while (timeout-- > 0);
1203
1204 netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1205
1206 return -ETIMEDOUT;
1207}
1208
1209/* TDMA helper */
1210static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001211 unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001212{
1213 unsigned int timeout = 1000;
1214 u32 reg;
1215
1216 reg = tdma_readl(priv, TDMA_CONTROL);
1217 if (enable)
1218 reg |= TDMA_EN;
1219 else
1220 reg &= ~TDMA_EN;
1221 tdma_writel(priv, reg, TDMA_CONTROL);
1222
1223 /* Poll for TMDA disabling completion */
1224 do {
1225 reg = tdma_readl(priv, TDMA_STATUS);
1226 if (!!(reg & TDMA_DISABLED) == !enable)
1227 return 0;
1228
1229 usleep_range(1000, 2000);
1230 } while (timeout-- > 0);
1231
1232 netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1233
1234 return -ETIMEDOUT;
1235}
1236
1237static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1238{
1239 u32 reg;
1240 int ret;
1241
1242 /* Initialize SW view of the RX ring */
1243 priv->num_rx_bds = NUM_RX_DESC;
1244 priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
1245 priv->rx_bd_assign_ptr = priv->rx_bds;
1246 priv->rx_bd_assign_index = 0;
1247 priv->rx_c_index = 0;
1248 priv->rx_read_ptr = 0;
Florian Fainelli40a8a312014-07-09 17:36:47 -07001249 priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
1250 GFP_KERNEL);
Florian Fainelli80105be2014-04-24 18:08:57 -07001251 if (!priv->rx_cbs) {
1252 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1253 return -ENOMEM;
1254 }
1255
1256 ret = bcm_sysport_alloc_rx_bufs(priv);
1257 if (ret) {
1258 netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1259 return ret;
1260 }
1261
1262 /* Initialize HW, ensure RDMA is disabled */
1263 reg = rdma_readl(priv, RDMA_STATUS);
1264 if (!(reg & RDMA_DISABLED))
1265 rdma_enable_set(priv, 0);
1266
1267 rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1268 rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1269 rdma_writel(priv, 0, RDMA_PROD_INDEX);
1270 rdma_writel(priv, 0, RDMA_CONS_INDEX);
1271 rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1272 RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1273 /* Operate the queue in ring mode */
1274 rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1275 rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1276 rdma_writel(priv, 0, RDMA_END_ADDR_HI);
1277 rdma_writel(priv, NUM_HW_RX_DESC_WORDS - 1, RDMA_END_ADDR_LO);
1278
1279 rdma_writel(priv, 1, RDMA_MBDONE_INTR);
1280
1281 netif_dbg(priv, hw, priv->netdev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001282 "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1283 priv->num_rx_bds, priv->rx_bds);
Florian Fainelli80105be2014-04-24 18:08:57 -07001284
1285 return 0;
1286}
1287
1288static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1289{
1290 struct bcm_sysport_cb *cb;
1291 unsigned int i;
1292 u32 reg;
1293
1294 /* Caller should ensure RDMA is disabled */
1295 reg = rdma_readl(priv, RDMA_STATUS);
1296 if (!(reg & RDMA_DISABLED))
1297 netdev_warn(priv->netdev, "RDMA not stopped!\n");
1298
1299 for (i = 0; i < priv->num_rx_bds; i++) {
1300 cb = &priv->rx_cbs[i];
1301 if (dma_unmap_addr(cb, dma_addr))
1302 dma_unmap_single(&priv->pdev->dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001303 dma_unmap_addr(cb, dma_addr),
1304 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -07001305 bcm_sysport_free_cb(cb);
1306 }
1307
1308 kfree(priv->rx_cbs);
1309 priv->rx_cbs = NULL;
1310
1311 netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1312}
1313
1314static void bcm_sysport_set_rx_mode(struct net_device *dev)
1315{
1316 struct bcm_sysport_priv *priv = netdev_priv(dev);
1317 u32 reg;
1318
1319 reg = umac_readl(priv, UMAC_CMD);
1320 if (dev->flags & IFF_PROMISC)
1321 reg |= CMD_PROMISC;
1322 else
1323 reg &= ~CMD_PROMISC;
1324 umac_writel(priv, reg, UMAC_CMD);
1325
1326 /* No support for ALLMULTI */
1327 if (dev->flags & IFF_ALLMULTI)
1328 return;
1329}
1330
1331static inline void umac_enable_set(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001332 u32 mask, unsigned int enable)
Florian Fainelli80105be2014-04-24 18:08:57 -07001333{
1334 u32 reg;
1335
1336 reg = umac_readl(priv, UMAC_CMD);
1337 if (enable)
Florian Fainelli18e21b02014-07-01 21:08:36 -07001338 reg |= mask;
Florian Fainelli80105be2014-04-24 18:08:57 -07001339 else
Florian Fainelli18e21b02014-07-01 21:08:36 -07001340 reg &= ~mask;
Florian Fainelli80105be2014-04-24 18:08:57 -07001341 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli00b91c62014-05-15 14:33:53 -07001342
1343 /* UniMAC stops on a packet boundary, wait for a full-sized packet
1344 * to be processed (1 msec).
1345 */
1346 if (enable == 0)
1347 usleep_range(1000, 2000);
Florian Fainelli80105be2014-04-24 18:08:57 -07001348}
1349
Florian Fainelli412bce82014-06-26 10:06:45 -07001350static inline void umac_reset(struct bcm_sysport_priv *priv)
Florian Fainelli80105be2014-04-24 18:08:57 -07001351{
Florian Fainelli80105be2014-04-24 18:08:57 -07001352 u32 reg;
Florian Fainelli80105be2014-04-24 18:08:57 -07001353
Florian Fainelli412bce82014-06-26 10:06:45 -07001354 reg = umac_readl(priv, UMAC_CMD);
1355 reg |= CMD_SW_RESET;
1356 umac_writel(priv, reg, UMAC_CMD);
1357 udelay(10);
1358 reg = umac_readl(priv, UMAC_CMD);
1359 reg &= ~CMD_SW_RESET;
1360 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli80105be2014-04-24 18:08:57 -07001361}
1362
1363static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001364 unsigned char *addr)
Florian Fainelli80105be2014-04-24 18:08:57 -07001365{
1366 umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
1367 (addr[2] << 8) | addr[3], UMAC_MAC0);
1368 umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
1369}
1370
1371static void topctrl_flush(struct bcm_sysport_priv *priv)
1372{
1373 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1374 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1375 mdelay(1);
1376 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1377 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1378}
1379
Florian Fainellib02e6d92014-07-01 21:08:37 -07001380static void bcm_sysport_netif_start(struct net_device *dev)
1381{
1382 struct bcm_sysport_priv *priv = netdev_priv(dev);
1383
1384 /* Enable NAPI */
1385 napi_enable(&priv->napi);
1386
1387 phy_start(priv->phydev);
1388
1389 /* Enable TX interrupts for the 32 TXQs */
1390 intrl2_1_mask_clear(priv, 0xffffffff);
1391
1392 /* Last call before we start the real business */
1393 netif_tx_start_all_queues(dev);
1394}
1395
Florian Fainelli40755a02014-07-01 21:08:38 -07001396static void rbuf_init(struct bcm_sysport_priv *priv)
1397{
1398 u32 reg;
1399
1400 reg = rbuf_readl(priv, RBUF_CONTROL);
1401 reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
1402 rbuf_writel(priv, reg, RBUF_CONTROL);
1403}
1404
Florian Fainelli80105be2014-04-24 18:08:57 -07001405static int bcm_sysport_open(struct net_device *dev)
1406{
1407 struct bcm_sysport_priv *priv = netdev_priv(dev);
1408 unsigned int i;
Florian Fainelli80105be2014-04-24 18:08:57 -07001409 int ret;
1410
1411 /* Reset UniMAC */
Florian Fainelli412bce82014-06-26 10:06:45 -07001412 umac_reset(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001413
1414 /* Flush TX and RX FIFOs at TOPCTRL level */
1415 topctrl_flush(priv);
1416
1417 /* Disable the UniMAC RX/TX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001418 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001419
1420 /* Enable RBUF 2bytes alignment and Receive Status Block */
Florian Fainelli40755a02014-07-01 21:08:38 -07001421 rbuf_init(priv);
Florian Fainelli80105be2014-04-24 18:08:57 -07001422
1423 /* Set maximum frame length */
1424 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1425
1426 /* Set MAC address */
1427 umac_set_hw_addr(priv, dev->dev_addr);
1428
1429 /* Read CRC forward */
1430 priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
1431
Florian Fainelli186534a2014-05-22 09:47:46 -07001432 priv->phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1433 0, priv->phy_interface);
Florian Fainelli80105be2014-04-24 18:08:57 -07001434 if (!priv->phydev) {
1435 netdev_err(dev, "could not attach to PHY\n");
1436 return -ENODEV;
1437 }
1438
1439 /* Reset house keeping link status */
1440 priv->old_duplex = -1;
1441 priv->old_link = -1;
1442 priv->old_pause = -1;
1443
1444 /* mask all interrupts and request them */
1445 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
1446 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1447 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1448 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
1449 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1450 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1451
1452 ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1453 if (ret) {
1454 netdev_err(dev, "failed to request RX interrupt\n");
1455 goto out_phy_disconnect;
1456 }
1457
1458 ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0, dev->name, dev);
1459 if (ret) {
1460 netdev_err(dev, "failed to request TX interrupt\n");
1461 goto out_free_irq0;
1462 }
1463
1464 /* Initialize both hardware and software ring */
1465 for (i = 0; i < dev->num_tx_queues; i++) {
1466 ret = bcm_sysport_init_tx_ring(priv, i);
1467 if (ret) {
1468 netdev_err(dev, "failed to initialize TX ring %d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001469 i);
Florian Fainelli80105be2014-04-24 18:08:57 -07001470 goto out_free_tx_ring;
1471 }
1472 }
1473
1474 /* Initialize linked-list */
1475 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1476
1477 /* Initialize RX ring */
1478 ret = bcm_sysport_init_rx_ring(priv);
1479 if (ret) {
1480 netdev_err(dev, "failed to initialize RX ring\n");
1481 goto out_free_rx_ring;
1482 }
1483
1484 /* Turn on RDMA */
1485 ret = rdma_enable_set(priv, 1);
1486 if (ret)
1487 goto out_free_rx_ring;
1488
1489 /* Enable RX interrupt and TX ring full interrupt */
1490 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1491
1492 /* Turn on TDMA */
1493 ret = tdma_enable_set(priv, 1);
1494 if (ret)
1495 goto out_clear_rx_int;
1496
Florian Fainelli80105be2014-04-24 18:08:57 -07001497 /* Turn on UniMAC TX/RX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001498 umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
Florian Fainelli80105be2014-04-24 18:08:57 -07001499
Florian Fainellib02e6d92014-07-01 21:08:37 -07001500 bcm_sysport_netif_start(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001501
1502 return 0;
1503
1504out_clear_rx_int:
1505 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1506out_free_rx_ring:
1507 bcm_sysport_fini_rx_ring(priv);
1508out_free_tx_ring:
1509 for (i = 0; i < dev->num_tx_queues; i++)
1510 bcm_sysport_fini_tx_ring(priv, i);
1511 free_irq(priv->irq1, dev);
1512out_free_irq0:
1513 free_irq(priv->irq0, dev);
1514out_phy_disconnect:
1515 phy_disconnect(priv->phydev);
1516 return ret;
1517}
1518
Florian Fainellib02e6d92014-07-01 21:08:37 -07001519static void bcm_sysport_netif_stop(struct net_device *dev)
Florian Fainelli80105be2014-04-24 18:08:57 -07001520{
1521 struct bcm_sysport_priv *priv = netdev_priv(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001522
1523 /* stop all software from updating hardware */
1524 netif_tx_stop_all_queues(dev);
1525 napi_disable(&priv->napi);
1526 phy_stop(priv->phydev);
1527
1528 /* mask all interrupts */
1529 intrl2_0_mask_set(priv, 0xffffffff);
1530 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1531 intrl2_1_mask_set(priv, 0xffffffff);
1532 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellib02e6d92014-07-01 21:08:37 -07001533}
1534
1535static int bcm_sysport_stop(struct net_device *dev)
1536{
1537 struct bcm_sysport_priv *priv = netdev_priv(dev);
1538 unsigned int i;
1539 int ret;
1540
1541 bcm_sysport_netif_stop(dev);
Florian Fainelli80105be2014-04-24 18:08:57 -07001542
1543 /* Disable UniMAC RX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001544 umac_enable_set(priv, CMD_RX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001545
1546 ret = tdma_enable_set(priv, 0);
1547 if (ret) {
1548 netdev_err(dev, "timeout disabling RDMA\n");
1549 return ret;
1550 }
1551
1552 /* Wait for a maximum packet size to be drained */
1553 usleep_range(2000, 3000);
1554
1555 ret = rdma_enable_set(priv, 0);
1556 if (ret) {
1557 netdev_err(dev, "timeout disabling TDMA\n");
1558 return ret;
1559 }
1560
1561 /* Disable UniMAC TX */
Florian Fainelli18e21b02014-07-01 21:08:36 -07001562 umac_enable_set(priv, CMD_TX_EN, 0);
Florian Fainelli80105be2014-04-24 18:08:57 -07001563
1564 /* Free RX/TX rings SW structures */
1565 for (i = 0; i < dev->num_tx_queues; i++)
1566 bcm_sysport_fini_tx_ring(priv, i);
1567 bcm_sysport_fini_rx_ring(priv);
1568
1569 free_irq(priv->irq0, dev);
1570 free_irq(priv->irq1, dev);
1571
1572 /* Disconnect from PHY */
1573 phy_disconnect(priv->phydev);
1574
1575 return 0;
1576}
1577
1578static struct ethtool_ops bcm_sysport_ethtool_ops = {
1579 .get_settings = bcm_sysport_get_settings,
1580 .set_settings = bcm_sysport_set_settings,
1581 .get_drvinfo = bcm_sysport_get_drvinfo,
1582 .get_msglevel = bcm_sysport_get_msglvl,
1583 .set_msglevel = bcm_sysport_set_msglvl,
1584 .get_link = ethtool_op_get_link,
1585 .get_strings = bcm_sysport_get_strings,
1586 .get_ethtool_stats = bcm_sysport_get_stats,
1587 .get_sset_count = bcm_sysport_get_sset_count,
Florian Fainelli83e82f42014-07-01 21:08:40 -07001588 .get_wol = bcm_sysport_get_wol,
1589 .set_wol = bcm_sysport_set_wol,
Florian Fainelli80105be2014-04-24 18:08:57 -07001590};
1591
1592static const struct net_device_ops bcm_sysport_netdev_ops = {
1593 .ndo_start_xmit = bcm_sysport_xmit,
1594 .ndo_tx_timeout = bcm_sysport_tx_timeout,
1595 .ndo_open = bcm_sysport_open,
1596 .ndo_stop = bcm_sysport_stop,
1597 .ndo_set_features = bcm_sysport_set_features,
1598 .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
1599};
1600
1601#define REV_FMT "v%2x.%02x"
1602
1603static int bcm_sysport_probe(struct platform_device *pdev)
1604{
1605 struct bcm_sysport_priv *priv;
1606 struct device_node *dn;
1607 struct net_device *dev;
1608 const void *macaddr;
1609 struct resource *r;
1610 u32 txq, rxq;
1611 int ret;
1612
1613 dn = pdev->dev.of_node;
1614 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1615
1616 /* Read the Transmit/Receive Queue properties */
1617 if (of_property_read_u32(dn, "systemport,num-txq", &txq))
1618 txq = TDMA_NUM_RINGS;
1619 if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
1620 rxq = 1;
1621
1622 dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
1623 if (!dev)
1624 return -ENOMEM;
1625
1626 /* Initialize private members */
1627 priv = netdev_priv(dev);
1628
1629 priv->irq0 = platform_get_irq(pdev, 0);
1630 priv->irq1 = platform_get_irq(pdev, 1);
Florian Fainelli83e82f42014-07-01 21:08:40 -07001631 priv->wol_irq = platform_get_irq(pdev, 2);
Florian Fainelli80105be2014-04-24 18:08:57 -07001632 if (priv->irq0 <= 0 || priv->irq1 <= 0) {
1633 dev_err(&pdev->dev, "invalid interrupts\n");
1634 ret = -EINVAL;
1635 goto err;
1636 }
1637
Jingoo Han126e6122014-05-14 12:15:42 +09001638 priv->base = devm_ioremap_resource(&pdev->dev, r);
1639 if (IS_ERR(priv->base)) {
1640 ret = PTR_ERR(priv->base);
Florian Fainelli80105be2014-04-24 18:08:57 -07001641 goto err;
1642 }
1643
1644 priv->netdev = dev;
1645 priv->pdev = pdev;
1646
1647 priv->phy_interface = of_get_phy_mode(dn);
1648 /* Default to GMII interface mode */
1649 if (priv->phy_interface < 0)
1650 priv->phy_interface = PHY_INTERFACE_MODE_GMII;
1651
Florian Fainelli186534a2014-05-22 09:47:46 -07001652 /* In the case of a fixed PHY, the DT node associated
1653 * to the PHY is the Ethernet MAC DT node.
1654 */
1655 if (of_phy_is_fixed_link(dn)) {
1656 ret = of_phy_register_fixed_link(dn);
1657 if (ret) {
1658 dev_err(&pdev->dev, "failed to register fixed PHY\n");
1659 goto err;
1660 }
1661
1662 priv->phy_dn = dn;
1663 }
1664
Florian Fainelli80105be2014-04-24 18:08:57 -07001665 /* Initialize netdevice members */
1666 macaddr = of_get_mac_address(dn);
1667 if (!macaddr || !is_valid_ether_addr(macaddr)) {
1668 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
1669 random_ether_addr(dev->dev_addr);
1670 } else {
1671 ether_addr_copy(dev->dev_addr, macaddr);
1672 }
1673
1674 SET_NETDEV_DEV(dev, &pdev->dev);
1675 dev_set_drvdata(&pdev->dev, dev);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001676 dev->ethtool_ops = &bcm_sysport_ethtool_ops;
Florian Fainelli80105be2014-04-24 18:08:57 -07001677 dev->netdev_ops = &bcm_sysport_netdev_ops;
1678 netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
1679
1680 /* HW supported features, none enabled by default */
1681 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
1682 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1683
Florian Fainelli83e82f42014-07-01 21:08:40 -07001684 /* Request the WOL interrupt and advertise suspend if available */
1685 priv->wol_irq_disabled = 1;
1686 ret = devm_request_irq(&pdev->dev, priv->wol_irq,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001687 bcm_sysport_wol_isr, 0, dev->name, priv);
Florian Fainelli83e82f42014-07-01 21:08:40 -07001688 if (!ret)
1689 device_set_wakeup_capable(&pdev->dev, 1);
1690
Florian Fainelli80105be2014-04-24 18:08:57 -07001691 /* Set the needed headroom once and for all */
Paul Gortmaker3afc5572014-05-30 15:39:30 -04001692 BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
1693 dev->needed_headroom += sizeof(struct bcm_tsb);
Florian Fainelli80105be2014-04-24 18:08:57 -07001694
Florian Fainellif532e742014-06-05 10:22:18 -07001695 /* libphy will adjust the link state accordingly */
1696 netif_carrier_off(dev);
1697
Florian Fainelli80105be2014-04-24 18:08:57 -07001698 ret = register_netdev(dev);
1699 if (ret) {
1700 dev_err(&pdev->dev, "failed to register net_device\n");
1701 goto err;
1702 }
1703
1704 priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
1705 dev_info(&pdev->dev,
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001706 "Broadcom SYSTEMPORT" REV_FMT
1707 " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
1708 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
1709 priv->base, priv->irq0, priv->irq1, txq, rxq);
Florian Fainelli80105be2014-04-24 18:08:57 -07001710
1711 return 0;
1712err:
1713 free_netdev(dev);
1714 return ret;
1715}
1716
1717static int bcm_sysport_remove(struct platform_device *pdev)
1718{
1719 struct net_device *dev = dev_get_drvdata(&pdev->dev);
1720
1721 /* Not much to do, ndo_close has been called
1722 * and we use managed allocations
1723 */
1724 unregister_netdev(dev);
1725 free_netdev(dev);
1726 dev_set_drvdata(&pdev->dev, NULL);
1727
1728 return 0;
1729}
1730
Florian Fainelli40755a02014-07-01 21:08:38 -07001731#ifdef CONFIG_PM_SLEEP
Florian Fainelli83e82f42014-07-01 21:08:40 -07001732static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
1733{
1734 struct net_device *ndev = priv->netdev;
1735 unsigned int timeout = 1000;
1736 u32 reg;
1737
1738 /* Password has already been programmed */
1739 reg = umac_readl(priv, UMAC_MPD_CTRL);
1740 reg |= MPD_EN;
1741 reg &= ~PSW_EN;
1742 if (priv->wolopts & WAKE_MAGICSECURE)
1743 reg |= PSW_EN;
1744 umac_writel(priv, reg, UMAC_MPD_CTRL);
1745
1746 /* Make sure RBUF entered WoL mode as result */
1747 do {
1748 reg = rbuf_readl(priv, RBUF_STATUS);
1749 if (reg & RBUF_WOL_MODE)
1750 break;
1751
1752 udelay(10);
1753 } while (timeout-- > 0);
1754
1755 /* Do not leave the UniMAC RBUF matching only MPD packets */
1756 if (!timeout) {
1757 reg = umac_readl(priv, UMAC_MPD_CTRL);
1758 reg &= ~MPD_EN;
1759 umac_writel(priv, reg, UMAC_MPD_CTRL);
1760 netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
1761 return -ETIMEDOUT;
1762 }
1763
1764 /* UniMAC receive needs to be turned on */
1765 umac_enable_set(priv, CMD_RX_EN, 1);
1766
1767 /* Enable the interrupt wake-up source */
1768 intrl2_0_mask_clear(priv, INTRL2_0_MPD);
1769
1770 netif_dbg(priv, wol, ndev, "entered WOL mode\n");
1771
1772 return 0;
1773}
1774
Florian Fainelli40755a02014-07-01 21:08:38 -07001775static int bcm_sysport_suspend(struct device *d)
1776{
1777 struct net_device *dev = dev_get_drvdata(d);
1778 struct bcm_sysport_priv *priv = netdev_priv(dev);
1779 unsigned int i;
Florian Fainelli83e82f42014-07-01 21:08:40 -07001780 int ret = 0;
Florian Fainelli40755a02014-07-01 21:08:38 -07001781 u32 reg;
1782
1783 if (!netif_running(dev))
1784 return 0;
1785
1786 bcm_sysport_netif_stop(dev);
1787
1788 phy_suspend(priv->phydev);
1789
1790 netif_device_detach(dev);
1791
1792 /* Disable UniMAC RX */
1793 umac_enable_set(priv, CMD_RX_EN, 0);
1794
1795 ret = rdma_enable_set(priv, 0);
1796 if (ret) {
1797 netdev_err(dev, "RDMA timeout!\n");
1798 return ret;
1799 }
1800
1801 /* Disable RXCHK if enabled */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -07001802 if (priv->rx_chk_en) {
Florian Fainelli40755a02014-07-01 21:08:38 -07001803 reg = rxchk_readl(priv, RXCHK_CONTROL);
1804 reg &= ~RXCHK_EN;
1805 rxchk_writel(priv, reg, RXCHK_CONTROL);
1806 }
1807
1808 /* Flush RX pipe */
Florian Fainelli83e82f42014-07-01 21:08:40 -07001809 if (!priv->wolopts)
1810 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
Florian Fainelli40755a02014-07-01 21:08:38 -07001811
1812 ret = tdma_enable_set(priv, 0);
1813 if (ret) {
1814 netdev_err(dev, "TDMA timeout!\n");
1815 return ret;
1816 }
1817
1818 /* Wait for a packet boundary */
1819 usleep_range(2000, 3000);
1820
1821 umac_enable_set(priv, CMD_TX_EN, 0);
1822
1823 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1824
1825 /* Free RX/TX rings SW structures */
1826 for (i = 0; i < dev->num_tx_queues; i++)
1827 bcm_sysport_fini_tx_ring(priv, i);
1828 bcm_sysport_fini_rx_ring(priv);
1829
Florian Fainelli83e82f42014-07-01 21:08:40 -07001830 /* Get prepared for Wake-on-LAN */
1831 if (device_may_wakeup(d) && priv->wolopts)
1832 ret = bcm_sysport_suspend_to_wol(priv);
1833
1834 return ret;
Florian Fainelli40755a02014-07-01 21:08:38 -07001835}
1836
1837static int bcm_sysport_resume(struct device *d)
1838{
1839 struct net_device *dev = dev_get_drvdata(d);
1840 struct bcm_sysport_priv *priv = netdev_priv(dev);
1841 unsigned int i;
1842 u32 reg;
1843 int ret;
1844
1845 if (!netif_running(dev))
1846 return 0;
1847
Florian Fainelli83e82f42014-07-01 21:08:40 -07001848 /* We may have been suspended and never received a WOL event that
1849 * would turn off MPD detection, take care of that now
1850 */
1851 bcm_sysport_resume_from_wol(priv);
1852
Florian Fainelli40755a02014-07-01 21:08:38 -07001853 /* Initialize both hardware and software ring */
1854 for (i = 0; i < dev->num_tx_queues; i++) {
1855 ret = bcm_sysport_init_tx_ring(priv, i);
1856 if (ret) {
1857 netdev_err(dev, "failed to initialize TX ring %d\n",
Florian Fainelli23acb2f2014-07-09 17:36:46 -07001858 i);
Florian Fainelli40755a02014-07-01 21:08:38 -07001859 goto out_free_tx_rings;
1860 }
1861 }
1862
1863 /* Initialize linked-list */
1864 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1865
1866 /* Initialize RX ring */
1867 ret = bcm_sysport_init_rx_ring(priv);
1868 if (ret) {
1869 netdev_err(dev, "failed to initialize RX ring\n");
1870 goto out_free_rx_ring;
1871 }
1872
1873 netif_device_attach(dev);
1874
1875 /* Enable RX interrupt and TX ring full interrupt */
1876 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1877
1878 /* RX pipe enable */
1879 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1880
1881 ret = rdma_enable_set(priv, 1);
1882 if (ret) {
1883 netdev_err(dev, "failed to enable RDMA\n");
1884 goto out_free_rx_ring;
1885 }
1886
1887 /* Enable rxhck */
Florian Fainelli9d34c1c2014-07-01 21:08:39 -07001888 if (priv->rx_chk_en) {
Florian Fainelli40755a02014-07-01 21:08:38 -07001889 reg = rxchk_readl(priv, RXCHK_CONTROL);
1890 reg |= RXCHK_EN;
1891 rxchk_writel(priv, reg, RXCHK_CONTROL);
1892 }
1893
1894 rbuf_init(priv);
1895
1896 /* Set maximum frame length */
1897 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1898
1899 /* Set MAC address */
1900 umac_set_hw_addr(priv, dev->dev_addr);
1901
1902 umac_enable_set(priv, CMD_RX_EN, 1);
1903
1904 /* TX pipe enable */
1905 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1906
1907 umac_enable_set(priv, CMD_TX_EN, 1);
1908
1909 ret = tdma_enable_set(priv, 1);
1910 if (ret) {
1911 netdev_err(dev, "TDMA timeout!\n");
1912 goto out_free_rx_ring;
1913 }
1914
1915 phy_resume(priv->phydev);
1916
1917 bcm_sysport_netif_start(dev);
1918
1919 return 0;
1920
1921out_free_rx_ring:
1922 bcm_sysport_fini_rx_ring(priv);
1923out_free_tx_rings:
1924 for (i = 0; i < dev->num_tx_queues; i++)
1925 bcm_sysport_fini_tx_ring(priv, i);
1926 return ret;
1927}
1928#endif
1929
1930static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
1931 bcm_sysport_suspend, bcm_sysport_resume);
1932
Florian Fainelli80105be2014-04-24 18:08:57 -07001933static const struct of_device_id bcm_sysport_of_match[] = {
1934 { .compatible = "brcm,systemport-v1.00" },
1935 { .compatible = "brcm,systemport" },
1936 { /* sentinel */ }
1937};
1938
1939static struct platform_driver bcm_sysport_driver = {
1940 .probe = bcm_sysport_probe,
1941 .remove = bcm_sysport_remove,
1942 .driver = {
1943 .name = "brcm-systemport",
1944 .owner = THIS_MODULE,
1945 .of_match_table = bcm_sysport_of_match,
Florian Fainelli40755a02014-07-01 21:08:38 -07001946 .pm = &bcm_sysport_pm_ops,
Florian Fainelli80105be2014-04-24 18:08:57 -07001947 },
1948};
1949module_platform_driver(bcm_sysport_driver);
1950
1951MODULE_AUTHOR("Broadcom Corporation");
1952MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
1953MODULE_ALIAS("platform:brcm-systemport");
1954MODULE_LICENSE("GPL");