blob: dc708a888f802ccc8e910bfdd46944fa4a8f78c5 [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,
84 d + DESC_ADDR_HI_STATUS_LEN);
85#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,
90 struct dma_desc *desc,
91 unsigned int port)
92{
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,
111 struct ethtool_cmd *cmd)
112{
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,
122 netdev_features_t wanted)
123{
124 struct bcm_sysport_priv *priv = netdev_priv(dev);
125 u32 reg;
126
127 priv->rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
128 reg = rxchk_readl(priv, RXCHK_CONTROL);
129 if (priv->rx_csum_en)
130 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 */
137 if (priv->rx_csum_en && priv->crc_fwd)
138 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,
148 netdev_features_t wanted)
149{
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,
168 netdev_features_t features)
169{
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,
264 RXCHK_OTHER_DISC_CNTR),
265 /* 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,
273 struct ethtool_drvinfo *info)
274{
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,
306 u32 stringset, u8 *data)
307{
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,
314 bcm_sysport_gstrings_stats[i].stat_string,
315 ETH_GSTRING_LEN);
316 }
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,
365 struct ethtool_stats *stats, u64 *data)
366{
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
387static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
388{
389 dev_kfree_skb_any(cb->skb);
390 cb->skb = NULL;
391 dma_unmap_addr_set(cb, dma_addr, 0);
392}
393
394static int bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
395 struct bcm_sysport_cb *cb)
396{
397 struct device *kdev = &priv->pdev->dev;
398 struct net_device *ndev = priv->netdev;
399 dma_addr_t mapping;
400 int ret;
401
402 cb->skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
403 if (!cb->skb) {
404 netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
405 return -ENOMEM;
406 }
407
408 mapping = dma_map_single(kdev, cb->skb->data,
409 RX_BUF_LENGTH, DMA_FROM_DEVICE);
410 ret = dma_mapping_error(kdev, mapping);
411 if (ret) {
412 bcm_sysport_free_cb(cb);
413 netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
414 return ret;
415 }
416
417 dma_unmap_addr_set(cb, dma_addr, mapping);
418 dma_desc_set_addr(priv, priv->rx_bd_assign_ptr, mapping);
419
420 priv->rx_bd_assign_index++;
421 priv->rx_bd_assign_index &= (priv->num_rx_bds - 1);
422 priv->rx_bd_assign_ptr = priv->rx_bds +
423 (priv->rx_bd_assign_index * DESC_SIZE);
424
425 netif_dbg(priv, rx_status, ndev, "RX refill\n");
426
427 return 0;
428}
429
430static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
431{
432 struct bcm_sysport_cb *cb;
433 int ret = 0;
434 unsigned int i;
435
436 for (i = 0; i < priv->num_rx_bds; i++) {
437 cb = &priv->rx_cbs[priv->rx_bd_assign_index];
438 if (cb->skb)
439 continue;
440
441 ret = bcm_sysport_rx_refill(priv, cb);
442 if (ret)
443 break;
444 }
445
446 return ret;
447}
448
449/* Poll the hardware for up to budget packets to process */
450static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
451 unsigned int budget)
452{
453 struct device *kdev = &priv->pdev->dev;
454 struct net_device *ndev = priv->netdev;
455 unsigned int processed = 0, to_process;
456 struct bcm_sysport_cb *cb;
457 struct sk_buff *skb;
458 unsigned int p_index;
459 u16 len, status;
460 struct rsb *rsb;
461
462 /* Determine how much we should process since last call */
463 p_index = rdma_readl(priv, RDMA_PROD_INDEX);
464 p_index &= RDMA_PROD_INDEX_MASK;
465
466 if (p_index < priv->rx_c_index)
467 to_process = (RDMA_CONS_INDEX_MASK + 1) -
468 priv->rx_c_index + p_index;
469 else
470 to_process = p_index - priv->rx_c_index;
471
472 netif_dbg(priv, rx_status, ndev,
473 "p_index=%d rx_c_index=%d to_process=%d\n",
474 p_index, priv->rx_c_index, to_process);
475
476 while ((processed < to_process) &&
477 (processed < budget)) {
478
479 cb = &priv->rx_cbs[priv->rx_read_ptr];
480 skb = cb->skb;
481 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
Florian Fainellib1ff53e2014-05-15 14:33:52 -0700482 RX_BUF_LENGTH, DMA_FROM_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700483
484 /* Extract the Receive Status Block prepended */
485 rsb = (struct rsb *)skb->data;
486 len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
487 status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
488 DESC_STATUS_MASK;
489
490 processed++;
491 priv->rx_read_ptr++;
492 if (priv->rx_read_ptr == priv->num_rx_bds)
493 priv->rx_read_ptr = 0;
494
495 netif_dbg(priv, rx_status, ndev,
496 "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
497 p_index, priv->rx_c_index, priv->rx_read_ptr,
498 len, status);
499
500 if (unlikely(!skb)) {
501 netif_err(priv, rx_err, ndev, "out of memory!\n");
502 ndev->stats.rx_dropped++;
503 ndev->stats.rx_errors++;
504 goto refill;
505 }
506
507 if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
508 netif_err(priv, rx_status, ndev, "fragmented packet!\n");
509 ndev->stats.rx_dropped++;
510 ndev->stats.rx_errors++;
511 bcm_sysport_free_cb(cb);
512 goto refill;
513 }
514
515 if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
516 netif_err(priv, rx_err, ndev, "error packet\n");
517 if (RX_STATUS_OVFLOW)
518 ndev->stats.rx_over_errors++;
519 ndev->stats.rx_dropped++;
520 ndev->stats.rx_errors++;
521 bcm_sysport_free_cb(cb);
522 goto refill;
523 }
524
525 skb_put(skb, len);
526
527 /* Hardware validated our checksum */
528 if (likely(status & DESC_L4_CSUM))
529 skb->ip_summed = CHECKSUM_UNNECESSARY;
530
531 /* Hardware pre-pends packets with 2bytes between Ethernet
532 * and IP header plus we have the Receive Status Block, strip
533 * off all of this from the SKB.
534 */
535 skb_pull(skb, sizeof(*rsb) + 2);
536 len -= (sizeof(*rsb) + 2);
537
538 /* UniMAC may forward CRC */
539 if (priv->crc_fwd) {
540 skb_trim(skb, len - ETH_FCS_LEN);
541 len -= ETH_FCS_LEN;
542 }
543
544 skb->protocol = eth_type_trans(skb, ndev);
545 ndev->stats.rx_packets++;
546 ndev->stats.rx_bytes += len;
547
548 napi_gro_receive(&priv->napi, skb);
549refill:
550 bcm_sysport_rx_refill(priv, cb);
551 }
552
553 return processed;
554}
555
556static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_priv *priv,
557 struct bcm_sysport_cb *cb,
558 unsigned int *bytes_compl,
559 unsigned int *pkts_compl)
560{
561 struct device *kdev = &priv->pdev->dev;
562 struct net_device *ndev = priv->netdev;
563
564 if (cb->skb) {
565 ndev->stats.tx_bytes += cb->skb->len;
566 *bytes_compl += cb->skb->len;
567 dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
568 dma_unmap_len(cb, dma_len),
569 DMA_TO_DEVICE);
570 ndev->stats.tx_packets++;
571 (*pkts_compl)++;
572 bcm_sysport_free_cb(cb);
573 /* SKB fragment */
574 } else if (dma_unmap_addr(cb, dma_addr)) {
575 ndev->stats.tx_bytes += dma_unmap_len(cb, dma_len);
576 dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
577 dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
578 dma_unmap_addr_set(cb, dma_addr, 0);
579 }
580}
581
582/* Reclaim queued SKBs for transmission completion, lockless version */
583static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
584 struct bcm_sysport_tx_ring *ring)
585{
586 struct net_device *ndev = priv->netdev;
587 unsigned int c_index, last_c_index, last_tx_cn, num_tx_cbs;
588 unsigned int pkts_compl = 0, bytes_compl = 0;
589 struct bcm_sysport_cb *cb;
590 struct netdev_queue *txq;
591 u32 hw_ind;
592
593 txq = netdev_get_tx_queue(ndev, ring->index);
594
595 /* Compute how many descriptors have been processed since last call */
596 hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
597 c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
598 ring->p_index = (hw_ind & RING_PROD_INDEX_MASK);
599
600 last_c_index = ring->c_index;
601 num_tx_cbs = ring->size;
602
603 c_index &= (num_tx_cbs - 1);
604
605 if (c_index >= last_c_index)
606 last_tx_cn = c_index - last_c_index;
607 else
608 last_tx_cn = num_tx_cbs - last_c_index + c_index;
609
610 netif_dbg(priv, tx_done, ndev,
611 "ring=%d c_index=%d last_tx_cn=%d last_c_index=%d\n",
612 ring->index, c_index, last_tx_cn, last_c_index);
613
614 while (last_tx_cn-- > 0) {
615 cb = ring->cbs + last_c_index;
616 bcm_sysport_tx_reclaim_one(priv, cb, &bytes_compl, &pkts_compl);
617
618 ring->desc_count++;
619 last_c_index++;
620 last_c_index &= (num_tx_cbs - 1);
621 }
622
623 ring->c_index = c_index;
624
625 if (netif_tx_queue_stopped(txq) && pkts_compl)
626 netif_tx_wake_queue(txq);
627
628 netif_dbg(priv, tx_done, ndev,
629 "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
630 ring->index, ring->c_index, pkts_compl, bytes_compl);
631
632 return pkts_compl;
633}
634
635/* Locked version of the per-ring TX reclaim routine */
636static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
637 struct bcm_sysport_tx_ring *ring)
638{
639 unsigned int released;
640
641 spin_lock(&ring->lock);
642 released = __bcm_sysport_tx_reclaim(priv, ring);
643 spin_unlock(&ring->lock);
644
645 return released;
646}
647
648static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
649{
650 struct bcm_sysport_tx_ring *ring =
651 container_of(napi, struct bcm_sysport_tx_ring, napi);
652 unsigned int work_done = 0;
653
654 work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
655
656 if (work_done < budget) {
657 napi_complete(napi);
658 /* re-enable TX interrupt */
659 intrl2_1_mask_clear(ring->priv, BIT(ring->index));
660 }
661
662 return work_done;
663}
664
665static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
666{
667 unsigned int q;
668
669 for (q = 0; q < priv->netdev->num_tx_queues; q++)
670 bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
671}
672
673static int bcm_sysport_poll(struct napi_struct *napi, int budget)
674{
675 struct bcm_sysport_priv *priv =
676 container_of(napi, struct bcm_sysport_priv, napi);
677 unsigned int work_done = 0;
678
679 work_done = bcm_sysport_desc_rx(priv, budget);
680
681 priv->rx_c_index += work_done;
682 priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
683 rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
684
685 if (work_done < budget) {
686 napi_complete(napi);
687 /* re-enable RX interrupts */
688 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
689 }
690
691 return work_done;
692}
693
694
695/* RX and misc interrupt routine */
696static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
697{
698 struct net_device *dev = dev_id;
699 struct bcm_sysport_priv *priv = netdev_priv(dev);
700
701 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
702 ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
703 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
704
705 if (unlikely(priv->irq0_stat == 0)) {
706 netdev_warn(priv->netdev, "spurious RX interrupt\n");
707 return IRQ_NONE;
708 }
709
710 if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
711 if (likely(napi_schedule_prep(&priv->napi))) {
712 /* disable RX interrupts */
713 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
714 __napi_schedule(&priv->napi);
715 }
716 }
717
718 /* TX ring is full, perform a full reclaim since we do not know
719 * which one would trigger this interrupt
720 */
721 if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
722 bcm_sysport_tx_reclaim_all(priv);
723
724 return IRQ_HANDLED;
725}
726
727/* TX interrupt service routine */
728static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
729{
730 struct net_device *dev = dev_id;
731 struct bcm_sysport_priv *priv = netdev_priv(dev);
732 struct bcm_sysport_tx_ring *txr;
733 unsigned int ring;
734
735 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
736 ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
737 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
738
739 if (unlikely(priv->irq1_stat == 0)) {
740 netdev_warn(priv->netdev, "spurious TX interrupt\n");
741 return IRQ_NONE;
742 }
743
744 for (ring = 0; ring < dev->num_tx_queues; ring++) {
745 if (!(priv->irq1_stat & BIT(ring)))
746 continue;
747
748 txr = &priv->tx_rings[ring];
749
750 if (likely(napi_schedule_prep(&txr->napi))) {
751 intrl2_1_mask_set(priv, BIT(ring));
752 __napi_schedule(&txr->napi);
753 }
754 }
755
756 return IRQ_HANDLED;
757}
758
759static int bcm_sysport_insert_tsb(struct sk_buff *skb, struct net_device *dev)
760{
761 struct sk_buff *nskb;
762 struct tsb *tsb;
763 u32 csum_info;
764 u8 ip_proto;
765 u16 csum_start;
766 u16 ip_ver;
767
768 /* Re-allocate SKB if needed */
769 if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
770 nskb = skb_realloc_headroom(skb, sizeof(*tsb));
771 dev_kfree_skb(skb);
772 if (!nskb) {
773 dev->stats.tx_errors++;
774 dev->stats.tx_dropped++;
775 return -ENOMEM;
776 }
777 skb = nskb;
778 }
779
780 tsb = (struct tsb *)skb_push(skb, sizeof(*tsb));
781 /* Zero-out TSB by default */
782 memset(tsb, 0, sizeof(*tsb));
783
784 if (skb->ip_summed == CHECKSUM_PARTIAL) {
785 ip_ver = htons(skb->protocol);
786 switch (ip_ver) {
787 case ETH_P_IP:
788 ip_proto = ip_hdr(skb)->protocol;
789 break;
790 case ETH_P_IPV6:
791 ip_proto = ipv6_hdr(skb)->nexthdr;
792 break;
793 default:
794 return 0;
795 }
796
797 /* Get the checksum offset and the L4 (transport) offset */
798 csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
799 csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
800 csum_info |= (csum_start << L4_PTR_SHIFT);
801
802 if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
803 csum_info |= L4_LENGTH_VALID;
804 if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
805 csum_info |= L4_UDP;
806 } else
807 csum_info = 0;
808
809 tsb->l4_ptr_dest_map = csum_info;
810 }
811
812 return 0;
813}
814
815static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
816 struct net_device *dev)
817{
818 struct bcm_sysport_priv *priv = netdev_priv(dev);
819 struct device *kdev = &priv->pdev->dev;
820 struct bcm_sysport_tx_ring *ring;
821 struct bcm_sysport_cb *cb;
822 struct netdev_queue *txq;
823 struct dma_desc *desc;
Florian Fainellidab531b2014-05-14 19:32:14 -0700824 unsigned int skb_len;
Florian Fainelli80105be2014-04-24 18:08:57 -0700825 dma_addr_t mapping;
826 u32 len_status;
827 u16 queue;
828 int ret;
829
830 queue = skb_get_queue_mapping(skb);
831 txq = netdev_get_tx_queue(dev, queue);
832 ring = &priv->tx_rings[queue];
833
834 /* lock against tx reclaim in BH context */
835 spin_lock(&ring->lock);
836 if (unlikely(ring->desc_count == 0)) {
837 netif_tx_stop_queue(txq);
838 netdev_err(dev, "queue %d awake and ring full!\n", queue);
839 ret = NETDEV_TX_BUSY;
840 goto out;
841 }
842
843 /* Insert TSB and checksum infos */
844 if (priv->tsb_en) {
845 ret = bcm_sysport_insert_tsb(skb, dev);
846 if (ret) {
847 ret = NETDEV_TX_OK;
848 goto out;
849 }
850 }
851
Florian Fainellidab531b2014-05-14 19:32:14 -0700852 /* The Ethernet switch we are interfaced with needs packets to be at
853 * least 64 bytes (including FCS) otherwise they will be discarded when
854 * they enter the switch port logic. When Broadcom tags are enabled, we
855 * need to make sure that packets are at least 68 bytes
856 * (including FCS and tag) because the length verification is done after
857 * the Broadcom tag is stripped off the ingress packet.
858 */
859 if (skb_padto(skb, ETH_ZLEN + ENET_BRCM_TAG_LEN)) {
860 ret = NETDEV_TX_OK;
861 goto out;
862 }
863
864 skb_len = skb->len < ETH_ZLEN + ENET_BRCM_TAG_LEN ?
865 ETH_ZLEN + ENET_BRCM_TAG_LEN : skb->len;
866
867 mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
Florian Fainelli80105be2014-04-24 18:08:57 -0700868 if (dma_mapping_error(kdev, mapping)) {
869 netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
Florian Fainellidab531b2014-05-14 19:32:14 -0700870 skb->data, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -0700871 ret = NETDEV_TX_OK;
872 goto out;
873 }
874
875 /* Remember the SKB for future freeing */
876 cb = &ring->cbs[ring->curr_desc];
877 cb->skb = skb;
878 dma_unmap_addr_set(cb, dma_addr, mapping);
Florian Fainellidab531b2014-05-14 19:32:14 -0700879 dma_unmap_len_set(cb, dma_len, skb_len);
Florian Fainelli80105be2014-04-24 18:08:57 -0700880
881 /* Fetch a descriptor entry from our pool */
882 desc = ring->desc_cpu;
883
884 desc->addr_lo = lower_32_bits(mapping);
885 len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
Florian Fainellidab531b2014-05-14 19:32:14 -0700886 len_status |= (skb_len << DESC_LEN_SHIFT);
Florian Fainelli80105be2014-04-24 18:08:57 -0700887 len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
888 DESC_STATUS_SHIFT;
889 if (skb->ip_summed == CHECKSUM_PARTIAL)
890 len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
891
892 ring->curr_desc++;
893 if (ring->curr_desc == ring->size)
894 ring->curr_desc = 0;
895 ring->desc_count--;
896
897 /* Ensure write completion of the descriptor status/length
898 * in DRAM before the System Port WRITE_PORT register latches
899 * the value
900 */
901 wmb();
902 desc->addr_status_len = len_status;
903 wmb();
904
905 /* Write this descriptor address to the RING write port */
906 tdma_port_write_desc_addr(priv, desc, ring->index);
907
908 /* Check ring space and update SW control flow */
909 if (ring->desc_count == 0)
910 netif_tx_stop_queue(txq);
911
912 netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
913 ring->index, ring->desc_count, ring->curr_desc);
914
915 ret = NETDEV_TX_OK;
916out:
917 spin_unlock(&ring->lock);
918 return ret;
919}
920
921static void bcm_sysport_tx_timeout(struct net_device *dev)
922{
923 netdev_warn(dev, "transmit timeout!\n");
924
925 dev->trans_start = jiffies;
926 dev->stats.tx_errors++;
927
928 netif_tx_wake_all_queues(dev);
929}
930
931/* phylib adjust link callback */
932static void bcm_sysport_adj_link(struct net_device *dev)
933{
934 struct bcm_sysport_priv *priv = netdev_priv(dev);
935 struct phy_device *phydev = priv->phydev;
936 unsigned int changed = 0;
937 u32 cmd_bits = 0, reg;
938
939 if (priv->old_link != phydev->link) {
940 changed = 1;
941 priv->old_link = phydev->link;
942 }
943
944 if (priv->old_duplex != phydev->duplex) {
945 changed = 1;
946 priv->old_duplex = phydev->duplex;
947 }
948
949 switch (phydev->speed) {
950 case SPEED_2500:
951 cmd_bits = CMD_SPEED_2500;
952 break;
953 case SPEED_1000:
954 cmd_bits = CMD_SPEED_1000;
955 break;
956 case SPEED_100:
957 cmd_bits = CMD_SPEED_100;
958 break;
959 case SPEED_10:
960 cmd_bits = CMD_SPEED_10;
961 break;
962 default:
963 break;
964 }
965 cmd_bits <<= CMD_SPEED_SHIFT;
966
967 if (phydev->duplex == DUPLEX_HALF)
968 cmd_bits |= CMD_HD_EN;
969
970 if (priv->old_pause != phydev->pause) {
971 changed = 1;
972 priv->old_pause = phydev->pause;
973 }
974
975 if (!phydev->pause)
976 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
977
Florian Fainellid5e32cc2014-05-14 19:32:13 -0700978 if (changed) {
979 reg = umac_readl(priv, UMAC_CMD);
980 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
Florian Fainelli80105be2014-04-24 18:08:57 -0700981 CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
982 CMD_TX_PAUSE_IGNORE);
Florian Fainellid5e32cc2014-05-14 19:32:13 -0700983 reg |= cmd_bits;
984 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli80105be2014-04-24 18:08:57 -0700985
Florian Fainelli80105be2014-04-24 18:08:57 -0700986 phy_print_status(priv->phydev);
Florian Fainellid5e32cc2014-05-14 19:32:13 -0700987 }
Florian Fainelli80105be2014-04-24 18:08:57 -0700988}
989
990static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
991 unsigned int index)
992{
993 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
994 struct device *kdev = &priv->pdev->dev;
995 size_t size;
996 void *p;
997 u32 reg;
998
999 /* Simple descriptors partitioning for now */
1000 size = 256;
1001
1002 /* We just need one DMA descriptor which is DMA-able, since writing to
1003 * the port will allocate a new descriptor in its internal linked-list
1004 */
1005 p = dma_zalloc_coherent(kdev, 1, &ring->desc_dma, GFP_KERNEL);
1006 if (!p) {
1007 netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
1008 return -ENOMEM;
1009 }
1010
1011 ring->cbs = kzalloc(sizeof(struct bcm_sysport_cb) * size, GFP_KERNEL);
1012 if (!ring->cbs) {
1013 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1014 return -ENOMEM;
1015 }
1016
1017 /* Initialize SW view of the ring */
1018 spin_lock_init(&ring->lock);
1019 ring->priv = priv;
1020 netif_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
1021 ring->index = index;
1022 ring->size = size;
1023 ring->alloc_size = ring->size;
1024 ring->desc_cpu = p;
1025 ring->desc_count = ring->size;
1026 ring->curr_desc = 0;
1027
1028 /* Initialize HW ring */
1029 tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
1030 tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
1031 tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
1032 tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
1033 tdma_writel(priv, RING_IGNORE_STATUS, TDMA_DESC_RING_MAPPING(index));
1034 tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
1035
1036 /* Program the number of descriptors as MAX_THRESHOLD and half of
1037 * its size for the hysteresis trigger
1038 */
1039 tdma_writel(priv, ring->size |
1040 1 << RING_HYST_THRESH_SHIFT,
1041 TDMA_DESC_RING_MAX_HYST(index));
1042
1043 /* Enable the ring queue in the arbiter */
1044 reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
1045 reg |= (1 << index);
1046 tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
1047
1048 napi_enable(&ring->napi);
1049
1050 netif_dbg(priv, hw, priv->netdev,
1051 "TDMA cfg, size=%d, desc_cpu=%p\n",
1052 ring->size, ring->desc_cpu);
1053
1054 return 0;
1055}
1056
1057static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
1058 unsigned int index)
1059{
1060 struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
1061 struct device *kdev = &priv->pdev->dev;
1062 u32 reg;
1063
1064 /* Caller should stop the TDMA engine */
1065 reg = tdma_readl(priv, TDMA_STATUS);
1066 if (!(reg & TDMA_DISABLED))
1067 netdev_warn(priv->netdev, "TDMA not stopped!\n");
1068
1069 napi_disable(&ring->napi);
1070 netif_napi_del(&ring->napi);
1071
1072 bcm_sysport_tx_reclaim(priv, ring);
1073
1074 kfree(ring->cbs);
1075 ring->cbs = NULL;
1076
1077 if (ring->desc_dma) {
1078 dma_free_coherent(kdev, 1, ring->desc_cpu, ring->desc_dma);
1079 ring->desc_dma = 0;
1080 }
1081 ring->size = 0;
1082 ring->alloc_size = 0;
1083
1084 netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
1085}
1086
1087/* RDMA helper */
1088static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
1089 unsigned int enable)
1090{
1091 unsigned int timeout = 1000;
1092 u32 reg;
1093
1094 reg = rdma_readl(priv, RDMA_CONTROL);
1095 if (enable)
1096 reg |= RDMA_EN;
1097 else
1098 reg &= ~RDMA_EN;
1099 rdma_writel(priv, reg, RDMA_CONTROL);
1100
1101 /* Poll for RMDA disabling completion */
1102 do {
1103 reg = rdma_readl(priv, RDMA_STATUS);
1104 if (!!(reg & RDMA_DISABLED) == !enable)
1105 return 0;
1106 usleep_range(1000, 2000);
1107 } while (timeout-- > 0);
1108
1109 netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
1110
1111 return -ETIMEDOUT;
1112}
1113
1114/* TDMA helper */
1115static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
1116 unsigned int enable)
1117{
1118 unsigned int timeout = 1000;
1119 u32 reg;
1120
1121 reg = tdma_readl(priv, TDMA_CONTROL);
1122 if (enable)
1123 reg |= TDMA_EN;
1124 else
1125 reg &= ~TDMA_EN;
1126 tdma_writel(priv, reg, TDMA_CONTROL);
1127
1128 /* Poll for TMDA disabling completion */
1129 do {
1130 reg = tdma_readl(priv, TDMA_STATUS);
1131 if (!!(reg & TDMA_DISABLED) == !enable)
1132 return 0;
1133
1134 usleep_range(1000, 2000);
1135 } while (timeout-- > 0);
1136
1137 netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
1138
1139 return -ETIMEDOUT;
1140}
1141
1142static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
1143{
1144 u32 reg;
1145 int ret;
1146
1147 /* Initialize SW view of the RX ring */
1148 priv->num_rx_bds = NUM_RX_DESC;
1149 priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
1150 priv->rx_bd_assign_ptr = priv->rx_bds;
1151 priv->rx_bd_assign_index = 0;
1152 priv->rx_c_index = 0;
1153 priv->rx_read_ptr = 0;
1154 priv->rx_cbs = kzalloc(priv->num_rx_bds *
1155 sizeof(struct bcm_sysport_cb), GFP_KERNEL);
1156 if (!priv->rx_cbs) {
1157 netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
1158 return -ENOMEM;
1159 }
1160
1161 ret = bcm_sysport_alloc_rx_bufs(priv);
1162 if (ret) {
1163 netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
1164 return ret;
1165 }
1166
1167 /* Initialize HW, ensure RDMA is disabled */
1168 reg = rdma_readl(priv, RDMA_STATUS);
1169 if (!(reg & RDMA_DISABLED))
1170 rdma_enable_set(priv, 0);
1171
1172 rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
1173 rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
1174 rdma_writel(priv, 0, RDMA_PROD_INDEX);
1175 rdma_writel(priv, 0, RDMA_CONS_INDEX);
1176 rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
1177 RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
1178 /* Operate the queue in ring mode */
1179 rdma_writel(priv, 0, RDMA_START_ADDR_HI);
1180 rdma_writel(priv, 0, RDMA_START_ADDR_LO);
1181 rdma_writel(priv, 0, RDMA_END_ADDR_HI);
1182 rdma_writel(priv, NUM_HW_RX_DESC_WORDS - 1, RDMA_END_ADDR_LO);
1183
1184 rdma_writel(priv, 1, RDMA_MBDONE_INTR);
1185
1186 netif_dbg(priv, hw, priv->netdev,
1187 "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
1188 priv->num_rx_bds, priv->rx_bds);
1189
1190 return 0;
1191}
1192
1193static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
1194{
1195 struct bcm_sysport_cb *cb;
1196 unsigned int i;
1197 u32 reg;
1198
1199 /* Caller should ensure RDMA is disabled */
1200 reg = rdma_readl(priv, RDMA_STATUS);
1201 if (!(reg & RDMA_DISABLED))
1202 netdev_warn(priv->netdev, "RDMA not stopped!\n");
1203
1204 for (i = 0; i < priv->num_rx_bds; i++) {
1205 cb = &priv->rx_cbs[i];
1206 if (dma_unmap_addr(cb, dma_addr))
1207 dma_unmap_single(&priv->pdev->dev,
1208 dma_unmap_addr(cb, dma_addr),
1209 RX_BUF_LENGTH, DMA_FROM_DEVICE);
1210 bcm_sysport_free_cb(cb);
1211 }
1212
1213 kfree(priv->rx_cbs);
1214 priv->rx_cbs = NULL;
1215
1216 netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
1217}
1218
1219static void bcm_sysport_set_rx_mode(struct net_device *dev)
1220{
1221 struct bcm_sysport_priv *priv = netdev_priv(dev);
1222 u32 reg;
1223
1224 reg = umac_readl(priv, UMAC_CMD);
1225 if (dev->flags & IFF_PROMISC)
1226 reg |= CMD_PROMISC;
1227 else
1228 reg &= ~CMD_PROMISC;
1229 umac_writel(priv, reg, UMAC_CMD);
1230
1231 /* No support for ALLMULTI */
1232 if (dev->flags & IFF_ALLMULTI)
1233 return;
1234}
1235
1236static inline void umac_enable_set(struct bcm_sysport_priv *priv,
1237 unsigned int enable)
1238{
1239 u32 reg;
1240
1241 reg = umac_readl(priv, UMAC_CMD);
1242 if (enable)
1243 reg |= CMD_RX_EN | CMD_TX_EN;
1244 else
1245 reg &= ~(CMD_RX_EN | CMD_TX_EN);
1246 umac_writel(priv, reg, UMAC_CMD);
Florian Fainelli00b91c62014-05-15 14:33:53 -07001247
1248 /* UniMAC stops on a packet boundary, wait for a full-sized packet
1249 * to be processed (1 msec).
1250 */
1251 if (enable == 0)
1252 usleep_range(1000, 2000);
Florian Fainelli80105be2014-04-24 18:08:57 -07001253}
1254
1255static inline int umac_reset(struct bcm_sysport_priv *priv)
1256{
1257 unsigned int timeout = 0;
1258 u32 reg;
1259 int ret = 0;
1260
1261 umac_writel(priv, 0, UMAC_CMD);
1262 while (timeout++ < 1000) {
1263 reg = umac_readl(priv, UMAC_CMD);
1264 if (!(reg & CMD_SW_RESET))
1265 break;
1266
1267 udelay(1);
1268 }
1269
1270 if (timeout == 1000) {
1271 dev_err(&priv->pdev->dev,
1272 "timeout waiting for MAC to come out of reset\n");
1273 ret = -ETIMEDOUT;
1274 }
1275
1276 return ret;
1277}
1278
1279static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
1280 unsigned char *addr)
1281{
1282 umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
1283 (addr[2] << 8) | addr[3], UMAC_MAC0);
1284 umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
1285}
1286
1287static void topctrl_flush(struct bcm_sysport_priv *priv)
1288{
1289 topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
1290 topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
1291 mdelay(1);
1292 topctrl_writel(priv, 0, RX_FLUSH_CNTL);
1293 topctrl_writel(priv, 0, TX_FLUSH_CNTL);
1294}
1295
1296static int bcm_sysport_open(struct net_device *dev)
1297{
1298 struct bcm_sysport_priv *priv = netdev_priv(dev);
1299 unsigned int i;
1300 u32 reg;
1301 int ret;
1302
1303 /* Reset UniMAC */
1304 ret = umac_reset(priv);
1305 if (ret) {
1306 netdev_err(dev, "UniMAC reset failed\n");
1307 return ret;
1308 }
1309
1310 /* Flush TX and RX FIFOs at TOPCTRL level */
1311 topctrl_flush(priv);
1312
1313 /* Disable the UniMAC RX/TX */
1314 umac_enable_set(priv, 0);
1315
1316 /* Enable RBUF 2bytes alignment and Receive Status Block */
1317 reg = rbuf_readl(priv, RBUF_CONTROL);
1318 reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
1319 rbuf_writel(priv, reg, RBUF_CONTROL);
1320
1321 /* Set maximum frame length */
1322 umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
1323
1324 /* Set MAC address */
1325 umac_set_hw_addr(priv, dev->dev_addr);
1326
1327 /* Read CRC forward */
1328 priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
1329
Florian Fainelli186534a2014-05-22 09:47:46 -07001330 priv->phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
1331 0, priv->phy_interface);
Florian Fainelli80105be2014-04-24 18:08:57 -07001332 if (!priv->phydev) {
1333 netdev_err(dev, "could not attach to PHY\n");
1334 return -ENODEV;
1335 }
1336
1337 /* Reset house keeping link status */
1338 priv->old_duplex = -1;
1339 priv->old_link = -1;
1340 priv->old_pause = -1;
1341
1342 /* mask all interrupts and request them */
1343 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
1344 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1345 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1346 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
1347 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1348 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
1349
1350 ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
1351 if (ret) {
1352 netdev_err(dev, "failed to request RX interrupt\n");
1353 goto out_phy_disconnect;
1354 }
1355
1356 ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0, dev->name, dev);
1357 if (ret) {
1358 netdev_err(dev, "failed to request TX interrupt\n");
1359 goto out_free_irq0;
1360 }
1361
1362 /* Initialize both hardware and software ring */
1363 for (i = 0; i < dev->num_tx_queues; i++) {
1364 ret = bcm_sysport_init_tx_ring(priv, i);
1365 if (ret) {
1366 netdev_err(dev, "failed to initialize TX ring %d\n",
1367 i);
1368 goto out_free_tx_ring;
1369 }
1370 }
1371
1372 /* Initialize linked-list */
1373 tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
1374
1375 /* Initialize RX ring */
1376 ret = bcm_sysport_init_rx_ring(priv);
1377 if (ret) {
1378 netdev_err(dev, "failed to initialize RX ring\n");
1379 goto out_free_rx_ring;
1380 }
1381
1382 /* Turn on RDMA */
1383 ret = rdma_enable_set(priv, 1);
1384 if (ret)
1385 goto out_free_rx_ring;
1386
1387 /* Enable RX interrupt and TX ring full interrupt */
1388 intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1389
1390 /* Turn on TDMA */
1391 ret = tdma_enable_set(priv, 1);
1392 if (ret)
1393 goto out_clear_rx_int;
1394
1395 /* Enable NAPI */
1396 napi_enable(&priv->napi);
1397
1398 /* Turn on UniMAC TX/RX */
1399 umac_enable_set(priv, 1);
1400
1401 phy_start(priv->phydev);
1402
1403 /* Enable TX interrupts for the 32 TXQs */
1404 intrl2_1_mask_clear(priv, 0xffffffff);
1405
1406 /* Last call before we start the real business */
1407 netif_tx_start_all_queues(dev);
1408
1409 return 0;
1410
1411out_clear_rx_int:
1412 intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
1413out_free_rx_ring:
1414 bcm_sysport_fini_rx_ring(priv);
1415out_free_tx_ring:
1416 for (i = 0; i < dev->num_tx_queues; i++)
1417 bcm_sysport_fini_tx_ring(priv, i);
1418 free_irq(priv->irq1, dev);
1419out_free_irq0:
1420 free_irq(priv->irq0, dev);
1421out_phy_disconnect:
1422 phy_disconnect(priv->phydev);
1423 return ret;
1424}
1425
1426static int bcm_sysport_stop(struct net_device *dev)
1427{
1428 struct bcm_sysport_priv *priv = netdev_priv(dev);
1429 unsigned int i;
1430 u32 reg;
1431 int ret;
1432
1433 /* stop all software from updating hardware */
1434 netif_tx_stop_all_queues(dev);
1435 napi_disable(&priv->napi);
1436 phy_stop(priv->phydev);
1437
1438 /* mask all interrupts */
1439 intrl2_0_mask_set(priv, 0xffffffff);
1440 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1441 intrl2_1_mask_set(priv, 0xffffffff);
1442 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
1443
1444 /* Disable UniMAC RX */
1445 reg = umac_readl(priv, UMAC_CMD);
1446 reg &= ~CMD_RX_EN;
1447 umac_writel(priv, reg, UMAC_CMD);
1448
1449 ret = tdma_enable_set(priv, 0);
1450 if (ret) {
1451 netdev_err(dev, "timeout disabling RDMA\n");
1452 return ret;
1453 }
1454
1455 /* Wait for a maximum packet size to be drained */
1456 usleep_range(2000, 3000);
1457
1458 ret = rdma_enable_set(priv, 0);
1459 if (ret) {
1460 netdev_err(dev, "timeout disabling TDMA\n");
1461 return ret;
1462 }
1463
1464 /* Disable UniMAC TX */
1465 reg = umac_readl(priv, UMAC_CMD);
1466 reg &= ~CMD_TX_EN;
1467 umac_writel(priv, reg, UMAC_CMD);
1468
1469 /* Free RX/TX rings SW structures */
1470 for (i = 0; i < dev->num_tx_queues; i++)
1471 bcm_sysport_fini_tx_ring(priv, i);
1472 bcm_sysport_fini_rx_ring(priv);
1473
1474 free_irq(priv->irq0, dev);
1475 free_irq(priv->irq1, dev);
1476
1477 /* Disconnect from PHY */
1478 phy_disconnect(priv->phydev);
1479
1480 return 0;
1481}
1482
1483static struct ethtool_ops bcm_sysport_ethtool_ops = {
1484 .get_settings = bcm_sysport_get_settings,
1485 .set_settings = bcm_sysport_set_settings,
1486 .get_drvinfo = bcm_sysport_get_drvinfo,
1487 .get_msglevel = bcm_sysport_get_msglvl,
1488 .set_msglevel = bcm_sysport_set_msglvl,
1489 .get_link = ethtool_op_get_link,
1490 .get_strings = bcm_sysport_get_strings,
1491 .get_ethtool_stats = bcm_sysport_get_stats,
1492 .get_sset_count = bcm_sysport_get_sset_count,
1493};
1494
1495static const struct net_device_ops bcm_sysport_netdev_ops = {
1496 .ndo_start_xmit = bcm_sysport_xmit,
1497 .ndo_tx_timeout = bcm_sysport_tx_timeout,
1498 .ndo_open = bcm_sysport_open,
1499 .ndo_stop = bcm_sysport_stop,
1500 .ndo_set_features = bcm_sysport_set_features,
1501 .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
1502};
1503
1504#define REV_FMT "v%2x.%02x"
1505
1506static int bcm_sysport_probe(struct platform_device *pdev)
1507{
1508 struct bcm_sysport_priv *priv;
1509 struct device_node *dn;
1510 struct net_device *dev;
1511 const void *macaddr;
1512 struct resource *r;
1513 u32 txq, rxq;
1514 int ret;
1515
1516 dn = pdev->dev.of_node;
1517 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1518
1519 /* Read the Transmit/Receive Queue properties */
1520 if (of_property_read_u32(dn, "systemport,num-txq", &txq))
1521 txq = TDMA_NUM_RINGS;
1522 if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
1523 rxq = 1;
1524
1525 dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
1526 if (!dev)
1527 return -ENOMEM;
1528
1529 /* Initialize private members */
1530 priv = netdev_priv(dev);
1531
1532 priv->irq0 = platform_get_irq(pdev, 0);
1533 priv->irq1 = platform_get_irq(pdev, 1);
1534 if (priv->irq0 <= 0 || priv->irq1 <= 0) {
1535 dev_err(&pdev->dev, "invalid interrupts\n");
1536 ret = -EINVAL;
1537 goto err;
1538 }
1539
Jingoo Han126e6122014-05-14 12:15:42 +09001540 priv->base = devm_ioremap_resource(&pdev->dev, r);
1541 if (IS_ERR(priv->base)) {
1542 ret = PTR_ERR(priv->base);
Florian Fainelli80105be2014-04-24 18:08:57 -07001543 goto err;
1544 }
1545
1546 priv->netdev = dev;
1547 priv->pdev = pdev;
1548
1549 priv->phy_interface = of_get_phy_mode(dn);
1550 /* Default to GMII interface mode */
1551 if (priv->phy_interface < 0)
1552 priv->phy_interface = PHY_INTERFACE_MODE_GMII;
1553
Florian Fainelli186534a2014-05-22 09:47:46 -07001554 /* In the case of a fixed PHY, the DT node associated
1555 * to the PHY is the Ethernet MAC DT node.
1556 */
1557 if (of_phy_is_fixed_link(dn)) {
1558 ret = of_phy_register_fixed_link(dn);
1559 if (ret) {
1560 dev_err(&pdev->dev, "failed to register fixed PHY\n");
1561 goto err;
1562 }
1563
1564 priv->phy_dn = dn;
1565 }
1566
Florian Fainelli80105be2014-04-24 18:08:57 -07001567 /* Initialize netdevice members */
1568 macaddr = of_get_mac_address(dn);
1569 if (!macaddr || !is_valid_ether_addr(macaddr)) {
1570 dev_warn(&pdev->dev, "using random Ethernet MAC\n");
1571 random_ether_addr(dev->dev_addr);
1572 } else {
1573 ether_addr_copy(dev->dev_addr, macaddr);
1574 }
1575
1576 SET_NETDEV_DEV(dev, &pdev->dev);
1577 dev_set_drvdata(&pdev->dev, dev);
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001578 dev->ethtool_ops = &bcm_sysport_ethtool_ops;
Florian Fainelli80105be2014-04-24 18:08:57 -07001579 dev->netdev_ops = &bcm_sysport_netdev_ops;
1580 netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
1581
1582 /* HW supported features, none enabled by default */
1583 dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
1584 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1585
1586 /* Set the needed headroom once and for all */
1587 BUILD_BUG_ON(sizeof(struct tsb) != 8);
1588 dev->needed_headroom += sizeof(struct tsb);
1589
1590 /* We are interfaced to a switch which handles the multicast
1591 * filtering for us, so we do not support programming any
1592 * multicast hash table in this Ethernet MAC.
1593 */
1594 dev->flags &= ~IFF_MULTICAST;
1595
1596 ret = register_netdev(dev);
1597 if (ret) {
1598 dev_err(&pdev->dev, "failed to register net_device\n");
1599 goto err;
1600 }
1601
1602 priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
1603 dev_info(&pdev->dev,
1604 "Broadcom SYSTEMPORT" REV_FMT
1605 " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
1606 (priv->rev >> 8) & 0xff, priv->rev & 0xff,
1607 priv->base, priv->irq0, priv->irq1, txq, rxq);
1608
1609 return 0;
1610err:
1611 free_netdev(dev);
1612 return ret;
1613}
1614
1615static int bcm_sysport_remove(struct platform_device *pdev)
1616{
1617 struct net_device *dev = dev_get_drvdata(&pdev->dev);
1618
1619 /* Not much to do, ndo_close has been called
1620 * and we use managed allocations
1621 */
1622 unregister_netdev(dev);
1623 free_netdev(dev);
1624 dev_set_drvdata(&pdev->dev, NULL);
1625
1626 return 0;
1627}
1628
1629static const struct of_device_id bcm_sysport_of_match[] = {
1630 { .compatible = "brcm,systemport-v1.00" },
1631 { .compatible = "brcm,systemport" },
1632 { /* sentinel */ }
1633};
1634
1635static struct platform_driver bcm_sysport_driver = {
1636 .probe = bcm_sysport_probe,
1637 .remove = bcm_sysport_remove,
1638 .driver = {
1639 .name = "brcm-systemport",
1640 .owner = THIS_MODULE,
1641 .of_match_table = bcm_sysport_of_match,
1642 },
1643};
1644module_platform_driver(bcm_sysport_driver);
1645
1646MODULE_AUTHOR("Broadcom Corporation");
1647MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
1648MODULE_ALIAS("platform:brcm-systemport");
1649MODULE_LICENSE("GPL");