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