blob: dbfca04667603af7f6f5bff36ba4372b09b81833 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: sungem.c,v 1.44.2.22 2002/03/13 01:18:12 davem Exp $
2 * sungem.c: Sun GEM ethernet driver.
3 *
4 * Copyright (C) 2000, 2001, 2002, 2003 David S. Miller (davem@redhat.com)
Jeff Garzik6aa20a22006-09-13 13:24:59 -04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Support for Apple GMAC and assorted PHYs, WOL, Power Management
7 * (C) 2001,2002,2003 Benjamin Herrenscmidt (benh@kernel.crashing.org)
8 * (C) 2004,2005 Benjamin Herrenscmidt, IBM Corp.
9 *
10 * NAPI and NETPOLL support
11 * (C) 2004 by Eric Lemoine (eric.lemoine@gmail.com)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Joe Perchesc6c75982010-08-17 07:55:04 +000015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/types.h>
20#include <linux/fcntl.h>
21#include <linux/interrupt.h>
22#include <linux/ioport.h>
23#include <linux/in.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040024#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/string.h>
26#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/pci.h>
Domen Puncer1e7f0bd2005-06-26 18:22:14 -040029#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/netdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/crc32.h>
36#include <linux/random.h>
37#include <linux/workqueue.h>
38#include <linux/if_vlan.h>
39#include <linux/bitops.h>
Al Virod7fe0f22006-12-03 23:15:30 -050040#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
44#include <asm/byteorder.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080045#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/irq.h>
47
David S. Millerdadb8302007-02-28 15:42:50 -080048#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <asm/idprom.h>
Jiri Kosina59034172011-07-29 17:31:16 +020050#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#endif
52
53#ifdef CONFIG_PPC_PMAC
Jiri Kosina59034172011-07-29 17:31:16 +020054#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/machdep.h>
56#include <asm/pmac_feature.h>
57#endif
58
David S. Miller2bb69842011-08-14 22:52:04 -070059#include <linux/sungem_phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "sungem.h"
61
62/* Stripping FCS is causing problems, disabled for now */
63#undef STRIP_FCS
64
65#define DEFAULT_MSG (NETIF_MSG_DRV | \
66 NETIF_MSG_PROBE | \
67 NETIF_MSG_LINK)
68
69#define ADVERTISE_MASK (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
70 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
Benjamin Herrenschmidt63ea9982007-01-03 18:54:43 -080071 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full | \
72 SUPPORTED_Pause | SUPPORTED_Autoneg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74#define DRV_NAME "sungem"
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100075#define DRV_VERSION "1.0"
76#define DRV_AUTHOR "David S. Miller <davem@redhat.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Bill Pembertonf73d12b2012-12-03 09:24:02 -050078static char version[] =
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100079 DRV_NAME ".c:v" DRV_VERSION " " DRV_AUTHOR "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81MODULE_AUTHOR(DRV_AUTHOR);
82MODULE_DESCRIPTION("Sun GEM Gbit ethernet driver");
83MODULE_LICENSE("GPL");
84
85#define GEM_MODULE_NAME "gem"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Benoit Taine9baa3c32014-08-08 15:56:03 +020087static const struct pci_device_id gem_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_GEM,
89 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
90
91 /* These models only differ from the original GEM in
92 * that their tx/rx fifos are of a different size and
93 * they only support 10/100 speeds. -DaveM
Jeff Garzik6aa20a22006-09-13 13:24:59 -040094 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * Apple's GMAC does support gigabit on machines with
96 * the BCM54xx PHYs. -BenH
97 */
98 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_RIO_GEM,
99 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
100 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC,
101 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
102 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMACP,
103 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
104 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC2,
105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
106 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_K2_GMAC,
107 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
108 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_SH_SUNGEM,
109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Olof Johansson7fce2602005-11-13 16:06:48 -0800110 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID2_GMAC,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 {0, }
113};
114
115MODULE_DEVICE_TABLE(pci, gem_pci_tbl);
116
David S. Millerabc4da42014-08-27 22:59:26 -0700117static u16 __sungem_phy_read(struct gem *gp, int phy_addr, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 u32 cmd;
120 int limit = 10000;
121
122 cmd = (1 << 30);
123 cmd |= (2 << 28);
124 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
125 cmd |= (reg << 18) & MIF_FRAME_REGAD;
126 cmd |= (MIF_FRAME_TAMSB);
127 writel(cmd, gp->regs + MIF_FRAME);
128
Roel Kluin46578a692009-02-02 21:39:02 -0800129 while (--limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 cmd = readl(gp->regs + MIF_FRAME);
131 if (cmd & MIF_FRAME_TALSB)
132 break;
133
134 udelay(10);
135 }
136
137 if (!limit)
138 cmd = 0xffff;
139
140 return cmd & MIF_FRAME_DATA;
141}
142
David S. Millerabc4da42014-08-27 22:59:26 -0700143static inline int _sungem_phy_read(struct net_device *dev, int mii_id, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Wang Chen8f15ea42008-11-12 23:38:36 -0800145 struct gem *gp = netdev_priv(dev);
David S. Millerabc4da42014-08-27 22:59:26 -0700146 return __sungem_phy_read(gp, mii_id, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
David S. Millerabc4da42014-08-27 22:59:26 -0700149static inline u16 sungem_phy_read(struct gem *gp, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
David S. Millerabc4da42014-08-27 22:59:26 -0700151 return __sungem_phy_read(gp, gp->mii_phy_addr, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
David S. Millerabc4da42014-08-27 22:59:26 -0700154static void __sungem_phy_write(struct gem *gp, int phy_addr, int reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 u32 cmd;
157 int limit = 10000;
158
159 cmd = (1 << 30);
160 cmd |= (1 << 28);
161 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
162 cmd |= (reg << 18) & MIF_FRAME_REGAD;
163 cmd |= (MIF_FRAME_TAMSB);
164 cmd |= (val & MIF_FRAME_DATA);
165 writel(cmd, gp->regs + MIF_FRAME);
166
167 while (limit--) {
168 cmd = readl(gp->regs + MIF_FRAME);
169 if (cmd & MIF_FRAME_TALSB)
170 break;
171
172 udelay(10);
173 }
174}
175
David S. Millerabc4da42014-08-27 22:59:26 -0700176static inline void _sungem_phy_write(struct net_device *dev, int mii_id, int reg, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Wang Chen8f15ea42008-11-12 23:38:36 -0800178 struct gem *gp = netdev_priv(dev);
David S. Millerabc4da42014-08-27 22:59:26 -0700179 __sungem_phy_write(gp, mii_id, reg, val & 0xffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
David S. Millerabc4da42014-08-27 22:59:26 -0700182static inline void sungem_phy_write(struct gem *gp, int reg, u16 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
David S. Millerabc4da42014-08-27 22:59:26 -0700184 __sungem_phy_write(gp, gp->mii_phy_addr, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static inline void gem_enable_ints(struct gem *gp)
188{
189 /* Enable all interrupts but TXDONE */
190 writel(GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
191}
192
193static inline void gem_disable_ints(struct gem *gp)
194{
195 /* Disable all interrupts, including TXDONE */
196 writel(GREG_STAT_NAPI | GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000197 (void)readl(gp->regs + GREG_IMASK); /* write posting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static void gem_get_cell(struct gem *gp)
201{
202 BUG_ON(gp->cell_enabled < 0);
203 gp->cell_enabled++;
204#ifdef CONFIG_PPC_PMAC
205 if (gp->cell_enabled == 1) {
206 mb();
207 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 1);
208 udelay(10);
209 }
210#endif /* CONFIG_PPC_PMAC */
211}
212
213/* Turn off the chip's clock */
214static void gem_put_cell(struct gem *gp)
215{
216 BUG_ON(gp->cell_enabled <= 0);
217 gp->cell_enabled--;
218#ifdef CONFIG_PPC_PMAC
219 if (gp->cell_enabled == 0) {
220 mb();
221 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 0);
222 udelay(10);
223 }
224#endif /* CONFIG_PPC_PMAC */
225}
226
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000227static inline void gem_netif_stop(struct gem *gp)
228{
Florian Westphal860e9532016-05-03 16:33:13 +0200229 netif_trans_update(gp->dev); /* prevent tx timeout */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000230 napi_disable(&gp->napi);
231 netif_tx_disable(gp->dev);
232}
233
234static inline void gem_netif_start(struct gem *gp)
235{
236 /* NOTE: unconditional netif_wake_queue is only
237 * appropriate so long as all callers are assured to
238 * have free tx slots.
239 */
240 netif_wake_queue(gp->dev);
241 napi_enable(&gp->napi);
242}
243
244static void gem_schedule_reset(struct gem *gp)
245{
246 gp->reset_task_pending = 1;
247 schedule_work(&gp->reset_task);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static void gem_handle_mif_event(struct gem *gp, u32 reg_val, u32 changed_bits)
251{
252 if (netif_msg_intr(gp))
253 printk(KERN_DEBUG "%s: mif interrupt\n", gp->dev->name);
254}
255
256static int gem_pcs_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
257{
258 u32 pcs_istat = readl(gp->regs + PCS_ISTAT);
259 u32 pcs_miistat;
260
261 if (netif_msg_intr(gp))
262 printk(KERN_DEBUG "%s: pcs interrupt, pcs_istat: 0x%x\n",
263 gp->dev->name, pcs_istat);
264
265 if (!(pcs_istat & PCS_ISTAT_LSC)) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000266 netdev_err(dev, "PCS irq but no link status change???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return 0;
268 }
269
270 /* The link status bit latches on zero, so you must
271 * read it twice in such a case to see a transition
272 * to the link being up.
273 */
274 pcs_miistat = readl(gp->regs + PCS_MIISTAT);
275 if (!(pcs_miistat & PCS_MIISTAT_LS))
276 pcs_miistat |=
277 (readl(gp->regs + PCS_MIISTAT) &
278 PCS_MIISTAT_LS);
279
280 if (pcs_miistat & PCS_MIISTAT_ANC) {
281 /* The remote-fault indication is only valid
282 * when autoneg has completed.
283 */
284 if (pcs_miistat & PCS_MIISTAT_RF)
Joe Perchesc6c75982010-08-17 07:55:04 +0000285 netdev_info(dev, "PCS AutoNEG complete, RemoteFault\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 else
Joe Perchesc6c75982010-08-17 07:55:04 +0000287 netdev_info(dev, "PCS AutoNEG complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 if (pcs_miistat & PCS_MIISTAT_LS) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000291 netdev_info(dev, "PCS link is now up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 netif_carrier_on(gp->dev);
293 } else {
Joe Perchesc6c75982010-08-17 07:55:04 +0000294 netdev_info(dev, "PCS link is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 netif_carrier_off(gp->dev);
296 /* If this happens and the link timer is not running,
297 * reset so we re-negotiate.
298 */
299 if (!timer_pending(&gp->link_timer))
300 return 1;
301 }
302
303 return 0;
304}
305
306static int gem_txmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
307{
308 u32 txmac_stat = readl(gp->regs + MAC_TXSTAT);
309
310 if (netif_msg_intr(gp))
311 printk(KERN_DEBUG "%s: txmac interrupt, txmac_stat: 0x%x\n",
312 gp->dev->name, txmac_stat);
313
314 /* Defer timer expiration is quite normal,
315 * don't even log the event.
316 */
317 if ((txmac_stat & MAC_TXSTAT_DTE) &&
318 !(txmac_stat & ~MAC_TXSTAT_DTE))
319 return 0;
320
321 if (txmac_stat & MAC_TXSTAT_URUN) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000322 netdev_err(dev, "TX MAC xmit underrun\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000323 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 if (txmac_stat & MAC_TXSTAT_MPE) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000327 netdev_err(dev, "TX MAC max packet size error\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000328 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 /* The rest are all cases of one of the 16-bit TX
332 * counters expiring.
333 */
334 if (txmac_stat & MAC_TXSTAT_NCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000335 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (txmac_stat & MAC_TXSTAT_ECE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000338 dev->stats.tx_aborted_errors += 0x10000;
339 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
342 if (txmac_stat & MAC_TXSTAT_LCE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000343 dev->stats.tx_aborted_errors += 0x10000;
344 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 /* We do not keep track of MAC_TXSTAT_FCE and
348 * MAC_TXSTAT_PCE events.
349 */
350 return 0;
351}
352
353/* When we get a RX fifo overflow, the RX unit in GEM is probably hung
354 * so we do the following.
355 *
356 * If any part of the reset goes wrong, we return 1 and that causes the
357 * whole chip to be reset.
358 */
359static int gem_rxmac_reset(struct gem *gp)
360{
361 struct net_device *dev = gp->dev;
362 int limit, i;
363 u64 desc_dma;
364 u32 val;
365
366 /* First, reset & disable MAC RX. */
367 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
368 for (limit = 0; limit < 5000; limit++) {
369 if (!(readl(gp->regs + MAC_RXRST) & MAC_RXRST_CMD))
370 break;
371 udelay(10);
372 }
373 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000374 netdev_err(dev, "RX MAC will not reset, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 1;
376 }
377
378 writel(gp->mac_rx_cfg & ~MAC_RXCFG_ENAB,
379 gp->regs + MAC_RXCFG);
380 for (limit = 0; limit < 5000; limit++) {
381 if (!(readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB))
382 break;
383 udelay(10);
384 }
385 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000386 netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 1;
388 }
389
390 /* Second, disable RX DMA. */
391 writel(0, gp->regs + RXDMA_CFG);
392 for (limit = 0; limit < 5000; limit++) {
393 if (!(readl(gp->regs + RXDMA_CFG) & RXDMA_CFG_ENABLE))
394 break;
395 udelay(10);
396 }
397 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000398 netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return 1;
400 }
401
Arnd Bergmann3a22d5d2012-04-20 10:56:15 +0000402 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* Execute RX reset command. */
405 writel(gp->swrst_base | GREG_SWRST_RXRST,
406 gp->regs + GREG_SWRST);
407 for (limit = 0; limit < 5000; limit++) {
408 if (!(readl(gp->regs + GREG_SWRST) & GREG_SWRST_RXRST))
409 break;
410 udelay(10);
411 }
412 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000413 netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 1;
415 }
416
417 /* Refresh the RX ring. */
418 for (i = 0; i < RX_RING_SIZE; i++) {
419 struct gem_rxd *rxd = &gp->init_block->rxd[i];
420
421 if (gp->rx_skbs[i] == NULL) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000422 netdev_err(dev, "Parts of RX ring empty, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 1;
424 }
425
426 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
427 }
428 gp->rx_new = gp->rx_old = 0;
429
430 /* Now we must reprogram the rest of RX unit. */
431 desc_dma = (u64) gp->gblock_dvma;
432 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
433 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
434 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
435 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
436 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
437 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
438 writel(val, gp->regs + RXDMA_CFG);
439 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
440 writel(((5 & RXDMA_BLANK_IPKTS) |
441 ((8 << 12) & RXDMA_BLANK_ITIME)),
442 gp->regs + RXDMA_BLANK);
443 else
444 writel(((5 & RXDMA_BLANK_IPKTS) |
445 ((4 << 12) & RXDMA_BLANK_ITIME)),
446 gp->regs + RXDMA_BLANK);
447 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
448 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
449 writel(val, gp->regs + RXDMA_PTHRESH);
450 val = readl(gp->regs + RXDMA_CFG);
451 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
452 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
453 val = readl(gp->regs + MAC_RXCFG);
454 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
455
456 return 0;
457}
458
459static int gem_rxmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
460{
461 u32 rxmac_stat = readl(gp->regs + MAC_RXSTAT);
462 int ret = 0;
463
464 if (netif_msg_intr(gp))
465 printk(KERN_DEBUG "%s: rxmac interrupt, rxmac_stat: 0x%x\n",
466 gp->dev->name, rxmac_stat);
467
468 if (rxmac_stat & MAC_RXSTAT_OFLW) {
469 u32 smac = readl(gp->regs + MAC_SMACHINE);
470
Joe Perchesc6c75982010-08-17 07:55:04 +0000471 netdev_err(dev, "RX MAC fifo overflow smac[%08x]\n", smac);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000472 dev->stats.rx_over_errors++;
473 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 ret = gem_rxmac_reset(gp);
476 }
477
478 if (rxmac_stat & MAC_RXSTAT_ACE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000479 dev->stats.rx_frame_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (rxmac_stat & MAC_RXSTAT_CCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000482 dev->stats.rx_crc_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (rxmac_stat & MAC_RXSTAT_LCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000485 dev->stats.rx_length_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* We do not track MAC_RXSTAT_FCE and MAC_RXSTAT_VCE
488 * events.
489 */
490 return ret;
491}
492
493static int gem_mac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
494{
495 u32 mac_cstat = readl(gp->regs + MAC_CSTAT);
496
497 if (netif_msg_intr(gp))
498 printk(KERN_DEBUG "%s: mac interrupt, mac_cstat: 0x%x\n",
499 gp->dev->name, mac_cstat);
500
501 /* This interrupt is just for pause frame and pause
502 * tracking. It is useful for diagnostics and debug
503 * but probably by default we will mask these events.
504 */
505 if (mac_cstat & MAC_CSTAT_PS)
506 gp->pause_entered++;
507
508 if (mac_cstat & MAC_CSTAT_PRCV)
509 gp->pause_last_time_recvd = (mac_cstat >> 16);
510
511 return 0;
512}
513
514static int gem_mif_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
515{
516 u32 mif_status = readl(gp->regs + MIF_STATUS);
517 u32 reg_val, changed_bits;
518
519 reg_val = (mif_status & MIF_STATUS_DATA) >> 16;
520 changed_bits = (mif_status & MIF_STATUS_STAT);
521
522 gem_handle_mif_event(gp, reg_val, changed_bits);
523
524 return 0;
525}
526
527static int gem_pci_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
528{
529 u32 pci_estat = readl(gp->regs + GREG_PCIESTAT);
530
531 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
532 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000533 netdev_err(dev, "PCI error [%04x]", pci_estat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (pci_estat & GREG_PCIESTAT_BADACK)
Joe Perchesc6c75982010-08-17 07:55:04 +0000536 pr_cont(" <No ACK64# during ABS64 cycle>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (pci_estat & GREG_PCIESTAT_DTRTO)
Joe Perchesc6c75982010-08-17 07:55:04 +0000538 pr_cont(" <Delayed transaction timeout>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (pci_estat & GREG_PCIESTAT_OTHER)
Joe Perchesc6c75982010-08-17 07:55:04 +0000540 pr_cont(" <other>");
541 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
543 pci_estat |= GREG_PCIESTAT_OTHER;
Joe Perchesc6c75982010-08-17 07:55:04 +0000544 netdev_err(dev, "PCI error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546
547 if (pci_estat & GREG_PCIESTAT_OTHER) {
548 u16 pci_cfg_stat;
549
550 /* Interrogate PCI config space for the
551 * true cause.
552 */
553 pci_read_config_word(gp->pdev, PCI_STATUS,
554 &pci_cfg_stat);
Joe Perchesc6c75982010-08-17 07:55:04 +0000555 netdev_err(dev, "Read PCI cfg space status [%04x]\n",
556 pci_cfg_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (pci_cfg_stat & PCI_STATUS_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000558 netdev_err(dev, "PCI parity error detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (pci_cfg_stat & PCI_STATUS_SIG_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000560 netdev_err(dev, "PCI target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (pci_cfg_stat & PCI_STATUS_REC_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000562 netdev_err(dev, "PCI master acks target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (pci_cfg_stat & PCI_STATUS_REC_MASTER_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000564 netdev_err(dev, "PCI master abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (pci_cfg_stat & PCI_STATUS_SIG_SYSTEM_ERROR)
Joe Perchesc6c75982010-08-17 07:55:04 +0000566 netdev_err(dev, "PCI system error SERR#\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (pci_cfg_stat & PCI_STATUS_DETECTED_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000568 netdev_err(dev, "PCI parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Write the error bits back to clear them. */
571 pci_cfg_stat &= (PCI_STATUS_PARITY |
572 PCI_STATUS_SIG_TARGET_ABORT |
573 PCI_STATUS_REC_TARGET_ABORT |
574 PCI_STATUS_REC_MASTER_ABORT |
575 PCI_STATUS_SIG_SYSTEM_ERROR |
576 PCI_STATUS_DETECTED_PARITY);
577 pci_write_config_word(gp->pdev,
578 PCI_STATUS, pci_cfg_stat);
579 }
580
581 /* For all PCI errors, we should reset the chip. */
582 return 1;
583}
584
585/* All non-normal interrupt conditions get serviced here.
586 * Returns non-zero if we should just exit the interrupt
587 * handler right now (ie. if we reset the card which invalidates
588 * all of the other original irq status bits).
589 */
590static int gem_abnormal_irq(struct net_device *dev, struct gem *gp, u32 gem_status)
591{
592 if (gem_status & GREG_STAT_RXNOBUF) {
593 /* Frame arrived, no free RX buffers available. */
594 if (netif_msg_rx_err(gp))
595 printk(KERN_DEBUG "%s: no buffer for rx frame\n",
596 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000597 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 if (gem_status & GREG_STAT_RXTAGERR) {
601 /* corrupt RX tag framing */
602 if (netif_msg_rx_err(gp))
603 printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
604 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000605 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000607 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 if (gem_status & GREG_STAT_PCS) {
611 if (gem_pcs_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000612 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 if (gem_status & GREG_STAT_TXMAC) {
616 if (gem_txmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000617 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
620 if (gem_status & GREG_STAT_RXMAC) {
621 if (gem_rxmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000622 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624
625 if (gem_status & GREG_STAT_MAC) {
626 if (gem_mac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000627 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629
630 if (gem_status & GREG_STAT_MIF) {
631 if (gem_mif_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000632 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 if (gem_status & GREG_STAT_PCIERR) {
636 if (gem_pci_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000637 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static __inline__ void gem_tx(struct net_device *dev, struct gem *gp, u32 gem_status)
644{
645 int entry, limit;
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 entry = gp->tx_old;
648 limit = ((gem_status & GREG_STAT_TXNR) >> GREG_STAT_TXNR_SHIFT);
649 while (entry != limit) {
650 struct sk_buff *skb;
651 struct gem_txd *txd;
652 dma_addr_t dma_addr;
653 u32 dma_len;
654 int frag;
655
656 if (netif_msg_tx_done(gp))
657 printk(KERN_DEBUG "%s: tx done, slot %d\n",
658 gp->dev->name, entry);
659 skb = gp->tx_skbs[entry];
660 if (skb_shinfo(skb)->nr_frags) {
661 int last = entry + skb_shinfo(skb)->nr_frags;
662 int walk = entry;
663 int incomplete = 0;
664
665 last &= (TX_RING_SIZE - 1);
666 for (;;) {
667 walk = NEXT_TX(walk);
668 if (walk == limit)
669 incomplete = 1;
670 if (walk == last)
671 break;
672 }
673 if (incomplete)
674 break;
675 }
676 gp->tx_skbs[entry] = NULL;
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000677 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
680 txd = &gp->init_block->txd[entry];
681
682 dma_addr = le64_to_cpu(txd->buffer);
683 dma_len = le64_to_cpu(txd->control_word) & TXDCTRL_BUFSZ;
684
685 pci_unmap_page(gp->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
686 entry = NEXT_TX(entry);
687 }
688
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000689 dev->stats.tx_packets++;
Eric W. Biederman241198a2014-03-15 18:13:13 -0700690 dev_consume_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692 gp->tx_old = entry;
693
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000694 /* Need to make the tx_old update visible to gem_start_xmit()
695 * before checking for netif_queue_stopped(). Without the
696 * memory barrier, there is a small possibility that gem_start_xmit()
697 * will miss it and cause the queue to be stopped forever.
698 */
699 smp_mb();
700
701 if (unlikely(netif_queue_stopped(dev) &&
702 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))) {
703 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
704
705 __netif_tx_lock(txq, smp_processor_id());
706 if (netif_queue_stopped(dev) &&
707 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
708 netif_wake_queue(dev);
709 __netif_tx_unlock(txq);
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static __inline__ void gem_post_rxds(struct gem *gp, int limit)
714{
715 int cluster_start, curr, count, kick;
716
717 cluster_start = curr = (gp->rx_new & ~(4 - 1));
718 count = 0;
719 kick = -1;
Alexander Duyckb4468cc2015-04-07 16:55:14 -0700720 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 while (curr != limit) {
722 curr = NEXT_RX(curr);
723 if (++count == 4) {
724 struct gem_rxd *rxd =
725 &gp->init_block->rxd[cluster_start];
726 for (;;) {
727 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
728 rxd++;
729 cluster_start = NEXT_RX(cluster_start);
730 if (cluster_start == curr)
731 break;
732 }
733 kick = curr;
734 count = 0;
735 }
736 }
737 if (kick >= 0) {
738 mb();
739 writel(kick, gp->regs + RXDMA_KICK);
740 }
741}
742
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000743#define ALIGNED_RX_SKB_ADDR(addr) \
744 ((((unsigned long)(addr) + (64UL - 1UL)) & ~(64UL - 1UL)) - (unsigned long)(addr))
745static __inline__ struct sk_buff *gem_alloc_skb(struct net_device *dev, int size,
746 gfp_t gfp_flags)
747{
748 struct sk_buff *skb = alloc_skb(size + 64, gfp_flags);
749
750 if (likely(skb)) {
751 unsigned long offset = ALIGNED_RX_SKB_ADDR(skb->data);
752 skb_reserve(skb, offset);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000753 }
754 return skb;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static int gem_rx(struct gem *gp, int work_to_do)
758{
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000759 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int entry, drops, work_done = 0;
761 u32 done;
Al Viro439104b2007-12-17 06:48:04 +0000762 __sum16 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (netif_msg_rx_status(gp))
765 printk(KERN_DEBUG "%s: rx interrupt, done: %d, rx_new: %d\n",
766 gp->dev->name, readl(gp->regs + RXDMA_DONE), gp->rx_new);
767
768 entry = gp->rx_new;
769 drops = 0;
770 done = readl(gp->regs + RXDMA_DONE);
771 for (;;) {
772 struct gem_rxd *rxd = &gp->init_block->rxd[entry];
773 struct sk_buff *skb;
Al Viro439104b2007-12-17 06:48:04 +0000774 u64 status = le64_to_cpu(rxd->status_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 dma_addr_t dma_addr;
776 int len;
777
778 if ((status & RXDCTRL_OWN) != 0)
779 break;
780
781 if (work_done >= RX_RING_SIZE || work_done >= work_to_do)
782 break;
783
784 /* When writing back RX descriptor, GEM writes status
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800785 * then buffer address, possibly in separate transactions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * If we don't wait for the chip to write both, we could
787 * post a new buffer to this descriptor then have GEM spam
788 * on the buffer address. We sync on the RX completion
789 * register to prevent this from happening.
790 */
791 if (entry == done) {
792 done = readl(gp->regs + RXDMA_DONE);
793 if (entry == done)
794 break;
795 }
796
797 /* We can now account for the work we're about to do */
798 work_done++;
799
800 skb = gp->rx_skbs[entry];
801
802 len = (status & RXDCTRL_BUFSZ) >> 16;
803 if ((len < ETH_ZLEN) || (status & RXDCTRL_BAD)) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000804 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (len < ETH_ZLEN)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000806 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (len & RXDCTRL_BAD)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000808 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 /* We'll just return it to GEM. */
811 drop_it:
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000812 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 goto next;
814 }
815
Al Viro439104b2007-12-17 06:48:04 +0000816 dma_addr = le64_to_cpu(rxd->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (len > RX_COPY_THRESHOLD) {
818 struct sk_buff *new_skb;
819
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000820 new_skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (new_skb == NULL) {
822 drops++;
823 goto drop_it;
824 }
825 pci_unmap_page(gp->pdev, dma_addr,
826 RX_BUF_ALLOC_SIZE(gp),
827 PCI_DMA_FROMDEVICE);
828 gp->rx_skbs[entry] = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 skb_put(new_skb, (gp->rx_buf_sz + RX_OFFSET));
830 rxd->buffer = cpu_to_le64(pci_map_page(gp->pdev,
831 virt_to_page(new_skb->data),
832 offset_in_page(new_skb->data),
833 RX_BUF_ALLOC_SIZE(gp),
834 PCI_DMA_FROMDEVICE));
835 skb_reserve(new_skb, RX_OFFSET);
836
837 /* Trim the original skb for the netif. */
838 skb_trim(skb, len);
839 } else {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000840 struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (copy_skb == NULL) {
843 drops++;
844 goto drop_it;
845 }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 skb_reserve(copy_skb, 2);
848 skb_put(copy_skb, len);
849 pci_dma_sync_single_for_cpu(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300850 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 pci_dma_sync_single_for_device(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
852
853 /* We'll reuse the original ring buffer. */
854 skb = copy_skb;
855 }
856
Al Viro439104b2007-12-17 06:48:04 +0000857 csum = (__force __sum16)htons((status & RXDCTRL_TCPCSUM) ^ 0xffff);
858 skb->csum = csum_unfold(csum);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700859 skb->ip_summed = CHECKSUM_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 skb->protocol = eth_type_trans(skb, gp->dev);
861
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000862 napi_gro_receive(&gp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000864 dev->stats.rx_packets++;
865 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 next:
868 entry = NEXT_RX(entry);
869 }
870
871 gem_post_rxds(gp, entry);
872
873 gp->rx_new = entry;
874
875 if (drops)
Joe Perchesc6c75982010-08-17 07:55:04 +0000876 netdev_info(gp->dev, "Memory squeeze, deferring packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 return work_done;
879}
880
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700881static int gem_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700883 struct gem *gp = container_of(napi, struct gem, napi);
884 struct net_device *dev = gp->dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700887 work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 /* Handle anomalies */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000890 if (unlikely(gp->status & GREG_STAT_ABNORMAL)) {
891 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
892 int reset;
893
894 /* We run the abnormal interrupt handling code with
895 * the Tx lock. It only resets the Rx portion of the
896 * chip, but we need to guard it against DMA being
897 * restarted by the link poll timer
898 */
899 __netif_tx_lock(txq, smp_processor_id());
900 reset = gem_abnormal_irq(dev, gp, gp->status);
901 __netif_tx_unlock(txq);
902 if (reset) {
903 gem_schedule_reset(gp);
904 napi_complete(napi);
905 return work_done;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 /* Run TX completion thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 gem_tx(dev, gp, gp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400912 /* Run RX thread. We don't use any locking here,
913 * code willing to do bad things - like cleaning the
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700914 * rx ring - must call napi_disable(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * schedule_timeout()'s if polling is already disabled.
916 */
David S. Millerda990a22008-03-23 03:35:12 -0700917 work_done += gem_rx(gp, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700919 if (work_done >= budget)
920 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 gp->status = readl(gp->regs + GREG_STAT);
923 } while (gp->status & GREG_STAT_NAPI);
924
Eric Dumazet6ad20162017-01-30 08:22:01 -0800925 napi_complete_done(napi, work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 gem_enable_ints(gp);
927
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700928 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
David Howells7d12e782006-10-05 14:55:46 +0100931static irqreturn_t gem_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
933 struct net_device *dev = dev_id;
Wang Chen8f15ea42008-11-12 23:38:36 -0800934 struct gem *gp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400935
Ben Hutchings288379f2009-01-19 16:43:59 -0800936 if (napi_schedule_prep(&gp->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 u32 gem_status = readl(gp->regs + GREG_STAT);
938
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000939 if (unlikely(gem_status == 0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700940 napi_enable(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return IRQ_NONE;
942 }
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000943 if (netif_msg_intr(gp))
944 printk(KERN_DEBUG "%s: gem_interrupt() gem_status: 0x%x\n",
945 gp->dev->name, gem_status);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 gp->status = gem_status;
948 gem_disable_ints(gp);
Ben Hutchings288379f2009-01-19 16:43:59 -0800949 __napi_schedule(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* If polling was disabled at the time we received that
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400953 * interrupt, we may return IRQ_HANDLED here while we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * should return IRQ_NONE. No big deal...
955 */
956 return IRQ_HANDLED;
957}
958
959#ifdef CONFIG_NET_POLL_CONTROLLER
960static void gem_poll_controller(struct net_device *dev)
961{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000962 struct gem *gp = netdev_priv(dev);
963
964 disable_irq(gp->pdev->irq);
965 gem_interrupt(gp->pdev->irq, dev);
966 enable_irq(gp->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968#endif
969
970static void gem_tx_timeout(struct net_device *dev)
971{
Wang Chen8f15ea42008-11-12 23:38:36 -0800972 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Joe Perchesc6c75982010-08-17 07:55:04 +0000974 netdev_err(dev, "transmit timed out, resetting\n");
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000975
Joe Perchesc6c75982010-08-17 07:55:04 +0000976 netdev_err(dev, "TX_STATE[%08x:%08x:%08x]\n",
977 readl(gp->regs + TXDMA_CFG),
978 readl(gp->regs + MAC_TXSTAT),
979 readl(gp->regs + MAC_TXCFG));
980 netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
981 readl(gp->regs + RXDMA_CFG),
982 readl(gp->regs + MAC_RXSTAT),
983 readl(gp->regs + MAC_RXCFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000985 gem_schedule_reset(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static __inline__ int gem_intme(int entry)
989{
990 /* Algorithm: IRQ every 1/2 of descriptors. */
991 if (!(entry & ((TX_RING_SIZE>>1)-1)))
992 return 1;
993
994 return 0;
995}
996
Stephen Hemminger613573252009-08-31 19:50:58 +0000997static netdev_tx_t gem_start_xmit(struct sk_buff *skb,
998 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Wang Chen8f15ea42008-11-12 23:38:36 -08001000 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int entry;
1002 u64 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 ctrl = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001005 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +00001006 const u64 csum_start_off = skb_checksum_start_offset(skb);
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001007 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 ctrl = (TXDCTRL_CENAB |
1010 (csum_start_off << 15) |
1011 (csum_stuff_off << 21));
1012 }
1013
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001014 if (unlikely(TX_BUFFS_AVAIL(gp) <= (skb_shinfo(skb)->nr_frags + 1))) {
1015 /* This is a hard error, log it. */
1016 if (!netif_queue_stopped(dev)) {
1017 netif_stop_queue(dev);
1018 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return NETDEV_TX_BUSY;
1021 }
1022
1023 entry = gp->tx_new;
1024 gp->tx_skbs[entry] = skb;
1025
1026 if (skb_shinfo(skb)->nr_frags == 0) {
1027 struct gem_txd *txd = &gp->init_block->txd[entry];
1028 dma_addr_t mapping;
1029 u32 len;
1030
1031 len = skb->len;
1032 mapping = pci_map_page(gp->pdev,
1033 virt_to_page(skb->data),
1034 offset_in_page(skb->data),
1035 len, PCI_DMA_TODEVICE);
1036 ctrl |= TXDCTRL_SOF | TXDCTRL_EOF | len;
1037 if (gem_intme(entry))
1038 ctrl |= TXDCTRL_INTME;
1039 txd->buffer = cpu_to_le64(mapping);
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001040 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 txd->control_word = cpu_to_le64(ctrl);
1042 entry = NEXT_TX(entry);
1043 } else {
1044 struct gem_txd *txd;
1045 u32 first_len;
1046 u64 intme;
1047 dma_addr_t first_mapping;
1048 int frag, first_entry = entry;
1049
1050 intme = 0;
1051 if (gem_intme(entry))
1052 intme |= TXDCTRL_INTME;
1053
1054 /* We must give this initial chunk to the device last.
1055 * Otherwise we could race with the device.
1056 */
1057 first_len = skb_headlen(skb);
1058 first_mapping = pci_map_page(gp->pdev, virt_to_page(skb->data),
1059 offset_in_page(skb->data),
1060 first_len, PCI_DMA_TODEVICE);
1061 entry = NEXT_TX(entry);
1062
1063 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001064 const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 u32 len;
1066 dma_addr_t mapping;
1067 u64 this_ctrl;
1068
Eric Dumazet9e903e02011-10-18 21:00:24 +00001069 len = skb_frag_size(this_frag);
Ian Campbell4fee78b2011-09-21 21:53:21 +00001070 mapping = skb_frag_dma_map(&gp->pdev->dev, this_frag,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001071 0, len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 this_ctrl = ctrl;
1073 if (frag == skb_shinfo(skb)->nr_frags - 1)
1074 this_ctrl |= TXDCTRL_EOF;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 txd = &gp->init_block->txd[entry];
1077 txd->buffer = cpu_to_le64(mapping);
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001078 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 txd->control_word = cpu_to_le64(this_ctrl | len);
1080
1081 if (gem_intme(entry))
1082 intme |= TXDCTRL_INTME;
1083
1084 entry = NEXT_TX(entry);
1085 }
1086 txd = &gp->init_block->txd[first_entry];
1087 txd->buffer = cpu_to_le64(first_mapping);
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001088 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 txd->control_word =
1090 cpu_to_le64(ctrl | TXDCTRL_SOF | intme | first_len);
1091 }
1092
1093 gp->tx_new = entry;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001094 if (unlikely(TX_BUFFS_AVAIL(gp) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 netif_stop_queue(dev);
1096
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001097 /* netif_stop_queue() must be done before checking
1098 * checking tx index in TX_BUFFS_AVAIL() below, because
1099 * in gem_tx(), we update tx_old before checking for
1100 * netif_queue_stopped().
1101 */
1102 smp_mb();
1103 if (TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
1104 netif_wake_queue(dev);
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (netif_msg_tx_queued(gp))
1107 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
1108 dev->name, entry, skb->len);
1109 mb();
1110 writel(gp->tx_new, gp->regs + TXDMA_KICK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 return NETDEV_TX_OK;
1113}
1114
David S. Miller8c83f802008-12-09 00:10:11 -08001115static void gem_pcs_reset(struct gem *gp)
1116{
1117 int limit;
1118 u32 val;
1119
1120 /* Reset PCS unit. */
1121 val = readl(gp->regs + PCS_MIICTRL);
1122 val |= PCS_MIICTRL_RST;
1123 writel(val, gp->regs + PCS_MIICTRL);
1124
1125 limit = 32;
1126 while (readl(gp->regs + PCS_MIICTRL) & PCS_MIICTRL_RST) {
1127 udelay(100);
1128 if (limit-- <= 0)
1129 break;
1130 }
roel kluind13c11f2009-02-16 04:02:04 +00001131 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001132 netdev_warn(gp->dev, "PCS reset bit would not clear\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001133}
1134
1135static void gem_pcs_reinit_adv(struct gem *gp)
1136{
1137 u32 val;
1138
1139 /* Make sure PCS is disabled while changing advertisement
1140 * configuration.
1141 */
1142 val = readl(gp->regs + PCS_CFG);
1143 val &= ~(PCS_CFG_ENABLE | PCS_CFG_TO);
1144 writel(val, gp->regs + PCS_CFG);
1145
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001146 /* Advertise all capabilities except asymmetric
David S. Miller8c83f802008-12-09 00:10:11 -08001147 * pause.
1148 */
1149 val = readl(gp->regs + PCS_MIIADV);
1150 val |= (PCS_MIIADV_FD | PCS_MIIADV_HD |
1151 PCS_MIIADV_SP | PCS_MIIADV_AP);
1152 writel(val, gp->regs + PCS_MIIADV);
1153
1154 /* Enable and restart auto-negotiation, disable wrapback/loopback,
1155 * and re-enable PCS.
1156 */
1157 val = readl(gp->regs + PCS_MIICTRL);
1158 val |= (PCS_MIICTRL_RAN | PCS_MIICTRL_ANE);
1159 val &= ~PCS_MIICTRL_WB;
1160 writel(val, gp->regs + PCS_MIICTRL);
1161
1162 val = readl(gp->regs + PCS_CFG);
1163 val |= PCS_CFG_ENABLE;
1164 writel(val, gp->regs + PCS_CFG);
1165
1166 /* Make sure serialink loopback is off. The meaning
1167 * of this bit is logically inverted based upon whether
1168 * you are in Serialink or SERDES mode.
1169 */
1170 val = readl(gp->regs + PCS_SCTRL);
1171 if (gp->phy_type == phy_serialink)
1172 val &= ~PCS_SCTRL_LOOP;
1173 else
1174 val |= PCS_SCTRL_LOOP;
1175 writel(val, gp->regs + PCS_SCTRL);
1176}
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178#define STOP_TRIES 32
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180static void gem_reset(struct gem *gp)
1181{
1182 int limit;
1183 u32 val;
1184
1185 /* Make sure we won't get any more interrupts */
1186 writel(0xffffffff, gp->regs + GREG_IMASK);
1187
1188 /* Reset the chip */
1189 writel(gp->swrst_base | GREG_SWRST_TXRST | GREG_SWRST_RXRST,
1190 gp->regs + GREG_SWRST);
1191
1192 limit = STOP_TRIES;
1193
1194 do {
1195 udelay(20);
1196 val = readl(gp->regs + GREG_SWRST);
1197 if (limit-- <= 0)
1198 break;
1199 } while (val & (GREG_SWRST_TXRST | GREG_SWRST_RXRST));
1200
Roel Kluin4a8fd2c2009-03-04 00:08:39 -08001201 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001202 netdev_err(gp->dev, "SW reset is ghetto\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001203
1204 if (gp->phy_type == phy_serialink || gp->phy_type == phy_serdes)
1205 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208static void gem_start_dma(struct gem *gp)
1209{
1210 u32 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 /* We are ready to rock, turn everything on. */
1213 val = readl(gp->regs + TXDMA_CFG);
1214 writel(val | TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1215 val = readl(gp->regs + RXDMA_CFG);
1216 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1217 val = readl(gp->regs + MAC_TXCFG);
1218 writel(val | MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1219 val = readl(gp->regs + MAC_RXCFG);
1220 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1221
1222 (void) readl(gp->regs + MAC_RXCFG);
1223 udelay(100);
1224
1225 gem_enable_ints(gp);
1226
1227 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1228}
1229
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001230/* DMA won't be actually stopped before about 4ms tho ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 */
1232static void gem_stop_dma(struct gem *gp)
1233{
1234 u32 val;
1235
1236 /* We are done rocking, turn everything off. */
1237 val = readl(gp->regs + TXDMA_CFG);
1238 writel(val & ~TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1239 val = readl(gp->regs + RXDMA_CFG);
1240 writel(val & ~RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1241 val = readl(gp->regs + MAC_TXCFG);
1242 writel(val & ~MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1243 val = readl(gp->regs + MAC_RXCFG);
1244 writel(val & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1245
1246 (void) readl(gp->regs + MAC_RXCFG);
1247
1248 /* Need to wait a bit ... done by the caller */
1249}
1250
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252// XXX dbl check what that function should do when called on PCS PHY
Philippe Reynes92552fd2017-03-05 00:04:18 +01001253static void gem_begin_auto_negotiation(struct gem *gp,
1254 const struct ethtool_link_ksettings *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 u32 advertise, features;
1257 int autoneg;
1258 int speed;
1259 int duplex;
Philippe Reynes92552fd2017-03-05 00:04:18 +01001260 u32 advertising;
1261
1262 ethtool_convert_link_mode_to_legacy_u32(&advertising,
1263 ep->link_modes.advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (gp->phy_type != phy_mii_mdio0 &&
1266 gp->phy_type != phy_mii_mdio1)
1267 goto non_mii;
1268
1269 /* Setup advertise */
1270 if (found_mii_phy(gp))
1271 features = gp->phy_mii.def->features;
1272 else
1273 features = 0;
1274
1275 advertise = features & ADVERTISE_MASK;
1276 if (gp->phy_mii.advertising != 0)
1277 advertise &= gp->phy_mii.advertising;
1278
1279 autoneg = gp->want_autoneg;
1280 speed = gp->phy_mii.speed;
1281 duplex = gp->phy_mii.duplex;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 /* Setup link parameters */
1284 if (!ep)
1285 goto start_aneg;
Philippe Reynes92552fd2017-03-05 00:04:18 +01001286 if (ep->base.autoneg == AUTONEG_ENABLE) {
1287 advertise = advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 autoneg = 1;
1289 } else {
1290 autoneg = 0;
Philippe Reynes92552fd2017-03-05 00:04:18 +01001291 speed = ep->base.speed;
1292 duplex = ep->base.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
1295start_aneg:
1296 /* Sanitize settings based on PHY capabilities */
1297 if ((features & SUPPORTED_Autoneg) == 0)
1298 autoneg = 0;
1299 if (speed == SPEED_1000 &&
1300 !(features & (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)))
1301 speed = SPEED_100;
1302 if (speed == SPEED_100 &&
1303 !(features & (SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full)))
1304 speed = SPEED_10;
1305 if (duplex == DUPLEX_FULL &&
1306 !(features & (SUPPORTED_1000baseT_Full |
1307 SUPPORTED_100baseT_Full |
1308 SUPPORTED_10baseT_Full)))
1309 duplex = DUPLEX_HALF;
1310 if (speed == 0)
1311 speed = SPEED_10;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* If we are asleep, we don't try to actually setup the PHY, we
1314 * just store the settings
1315 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001316 if (!netif_device_present(gp->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 gp->phy_mii.autoneg = gp->want_autoneg = autoneg;
1318 gp->phy_mii.speed = speed;
1319 gp->phy_mii.duplex = duplex;
1320 return;
1321 }
1322
1323 /* Configure PHY & start aneg */
1324 gp->want_autoneg = autoneg;
1325 if (autoneg) {
1326 if (found_mii_phy(gp))
1327 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, advertise);
1328 gp->lstate = link_aneg;
1329 } else {
1330 if (found_mii_phy(gp))
1331 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, speed, duplex);
1332 gp->lstate = link_force_ok;
1333 }
1334
1335non_mii:
1336 gp->timer_ticks = 0;
1337 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
1338}
1339
1340/* A link-up condition has occurred, initialize and enable the
1341 * rest of the chip.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
1343static int gem_set_link_modes(struct gem *gp)
1344{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001345 struct netdev_queue *txq = netdev_get_tx_queue(gp->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int full_duplex, speed, pause;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001347 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 full_duplex = 0;
1350 speed = SPEED_10;
1351 pause = 0;
1352
1353 if (found_mii_phy(gp)) {
1354 if (gp->phy_mii.def->ops->read_link(&gp->phy_mii))
1355 return 1;
1356 full_duplex = (gp->phy_mii.duplex == DUPLEX_FULL);
1357 speed = gp->phy_mii.speed;
1358 pause = gp->phy_mii.pause;
1359 } else if (gp->phy_type == phy_serialink ||
1360 gp->phy_type == phy_serdes) {
1361 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1362
David S. Miller8c83f802008-12-09 00:10:11 -08001363 if ((pcs_lpa & PCS_MIIADV_FD) || gp->phy_type == phy_serdes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 full_duplex = 1;
1365 speed = SPEED_1000;
1366 }
1367
Joe Perchesc6c75982010-08-17 07:55:04 +00001368 netif_info(gp, link, gp->dev, "Link is up at %d Mbps, %s-duplex\n",
1369 speed, (full_duplex ? "full" : "half"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001371
1372 /* We take the tx queue lock to avoid collisions between
1373 * this code, the tx path and the NAPI-driven error path
1374 */
1375 __netif_tx_lock(txq, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 val = (MAC_TXCFG_EIPG0 | MAC_TXCFG_NGU);
1378 if (full_duplex) {
1379 val |= (MAC_TXCFG_ICS | MAC_TXCFG_ICOLL);
1380 } else {
1381 /* MAC_TXCFG_NBO must be zero. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 writel(val, gp->regs + MAC_TXCFG);
1384
1385 val = (MAC_XIFCFG_OE | MAC_XIFCFG_LLED);
1386 if (!full_duplex &&
1387 (gp->phy_type == phy_mii_mdio0 ||
1388 gp->phy_type == phy_mii_mdio1)) {
1389 val |= MAC_XIFCFG_DISE;
1390 } else if (full_duplex) {
1391 val |= MAC_XIFCFG_FLED;
1392 }
1393
1394 if (speed == SPEED_1000)
1395 val |= (MAC_XIFCFG_GMII);
1396
1397 writel(val, gp->regs + MAC_XIFCFG);
1398
1399 /* If gigabit and half-duplex, enable carrier extension
1400 * mode. Else, disable it.
1401 */
1402 if (speed == SPEED_1000 && !full_duplex) {
1403 val = readl(gp->regs + MAC_TXCFG);
1404 writel(val | MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1405
1406 val = readl(gp->regs + MAC_RXCFG);
1407 writel(val | MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1408 } else {
1409 val = readl(gp->regs + MAC_TXCFG);
1410 writel(val & ~MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1411
1412 val = readl(gp->regs + MAC_RXCFG);
1413 writel(val & ~MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1414 }
1415
1416 if (gp->phy_type == phy_serialink ||
1417 gp->phy_type == phy_serdes) {
1418 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1419
1420 if (pcs_lpa & (PCS_MIIADV_SP | PCS_MIIADV_AP))
1421 pause = 1;
1422 }
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (!full_duplex)
1425 writel(512, gp->regs + MAC_STIME);
1426 else
1427 writel(64, gp->regs + MAC_STIME);
1428 val = readl(gp->regs + MAC_MCCFG);
1429 if (pause)
1430 val |= (MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1431 else
1432 val &= ~(MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1433 writel(val, gp->regs + MAC_MCCFG);
1434
1435 gem_start_dma(gp);
1436
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001437 __netif_tx_unlock(txq);
1438
1439 if (netif_msg_link(gp)) {
1440 if (pause) {
1441 netdev_info(gp->dev,
1442 "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
1443 gp->rx_fifo_sz,
1444 gp->rx_pause_off,
1445 gp->rx_pause_on);
1446 } else {
1447 netdev_info(gp->dev, "Pause is disabled\n");
1448 }
1449 }
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return 0;
1452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static int gem_mdio_link_not_up(struct gem *gp)
1455{
1456 switch (gp->lstate) {
1457 case link_force_ret:
Joe Perchesc6c75982010-08-17 07:55:04 +00001458 netif_info(gp, link, gp->dev,
1459 "Autoneg failed again, keeping forced mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii,
1461 gp->last_forced_speed, DUPLEX_HALF);
1462 gp->timer_ticks = 5;
1463 gp->lstate = link_force_ok;
1464 return 0;
1465 case link_aneg:
1466 /* We try forced modes after a failed aneg only on PHYs that don't
1467 * have "magic_aneg" bit set, which means they internally do the
1468 * while forced-mode thingy. On these, we just restart aneg
1469 */
1470 if (gp->phy_mii.def->magic_aneg)
1471 return 1;
Joe Perchesc6c75982010-08-17 07:55:04 +00001472 netif_info(gp, link, gp->dev, "switching to forced 100bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 /* Try forced modes. */
1474 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_100,
1475 DUPLEX_HALF);
1476 gp->timer_ticks = 5;
1477 gp->lstate = link_force_try;
1478 return 0;
1479 case link_force_try:
1480 /* Downgrade from 100 to 10 Mbps if necessary.
1481 * If already at 10Mbps, warn user about the
1482 * situation every 10 ticks.
1483 */
1484 if (gp->phy_mii.speed == SPEED_100) {
1485 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_10,
1486 DUPLEX_HALF);
1487 gp->timer_ticks = 5;
Joe Perchesc6c75982010-08-17 07:55:04 +00001488 netif_info(gp, link, gp->dev,
1489 "switching to forced 10bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
1491 } else
1492 return 1;
1493 default:
1494 return 0;
1495 }
1496}
1497
1498static void gem_link_timer(unsigned long data)
1499{
1500 struct gem *gp = (struct gem *) data;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001501 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 int restart_aneg = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001503
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001504 /* There's no point doing anything if we're going to be reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (gp->reset_task_pending)
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001506 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (gp->phy_type == phy_serialink ||
1509 gp->phy_type == phy_serdes) {
1510 u32 val = readl(gp->regs + PCS_MIISTAT);
1511
1512 if (!(val & PCS_MIISTAT_LS))
1513 val = readl(gp->regs + PCS_MIISTAT);
1514
1515 if ((val & PCS_MIISTAT_LS) != 0) {
David S. Miller8c83f802008-12-09 00:10:11 -08001516 if (gp->lstate == link_up)
1517 goto restart;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001520 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 (void)gem_set_link_modes(gp);
1522 }
1523 goto restart;
1524 }
1525 if (found_mii_phy(gp) && gp->phy_mii.def->ops->poll_link(&gp->phy_mii)) {
1526 /* Ok, here we got a link. If we had it due to a forced
1527 * fallback, and we were configured for autoneg, we do
1528 * retry a short autoneg pass. If you know your hub is
1529 * broken, use ethtool ;)
1530 */
1531 if (gp->lstate == link_force_try && gp->want_autoneg) {
1532 gp->lstate = link_force_ret;
1533 gp->last_forced_speed = gp->phy_mii.speed;
1534 gp->timer_ticks = 5;
1535 if (netif_msg_link(gp))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001536 netdev_info(dev,
Joe Perchesc6c75982010-08-17 07:55:04 +00001537 "Got link after fallback, retrying autoneg once...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, gp->phy_mii.advertising);
1539 } else if (gp->lstate != link_up) {
1540 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001541 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (gem_set_link_modes(gp))
1543 restart_aneg = 1;
1544 }
1545 } else {
1546 /* If the link was previously up, we restart the
1547 * whole process
1548 */
1549 if (gp->lstate == link_up) {
1550 gp->lstate = link_down;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001551 netif_info(gp, link, dev, "Link down\n");
1552 netif_carrier_off(dev);
1553 gem_schedule_reset(gp);
1554 /* The reset task will restart the timer */
1555 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else if (++gp->timer_ticks > 10) {
1557 if (found_mii_phy(gp))
1558 restart_aneg = gem_mdio_link_not_up(gp);
1559 else
1560 restart_aneg = 1;
1561 }
1562 }
1563 if (restart_aneg) {
1564 gem_begin_auto_negotiation(gp, NULL);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001565 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567restart:
1568 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static void gem_clean_rings(struct gem *gp)
1572{
1573 struct gem_init_block *gb = gp->init_block;
1574 struct sk_buff *skb;
1575 int i;
1576 dma_addr_t dma_addr;
1577
1578 for (i = 0; i < RX_RING_SIZE; i++) {
1579 struct gem_rxd *rxd;
1580
1581 rxd = &gb->rxd[i];
1582 if (gp->rx_skbs[i] != NULL) {
1583 skb = gp->rx_skbs[i];
1584 dma_addr = le64_to_cpu(rxd->buffer);
1585 pci_unmap_page(gp->pdev, dma_addr,
1586 RX_BUF_ALLOC_SIZE(gp),
1587 PCI_DMA_FROMDEVICE);
1588 dev_kfree_skb_any(skb);
1589 gp->rx_skbs[i] = NULL;
1590 }
1591 rxd->status_word = 0;
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001592 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 rxd->buffer = 0;
1594 }
1595
1596 for (i = 0; i < TX_RING_SIZE; i++) {
1597 if (gp->tx_skbs[i] != NULL) {
1598 struct gem_txd *txd;
1599 int frag;
1600
1601 skb = gp->tx_skbs[i];
1602 gp->tx_skbs[i] = NULL;
1603
1604 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1605 int ent = i & (TX_RING_SIZE - 1);
1606
1607 txd = &gb->txd[ent];
1608 dma_addr = le64_to_cpu(txd->buffer);
1609 pci_unmap_page(gp->pdev, dma_addr,
1610 le64_to_cpu(txd->control_word) &
1611 TXDCTRL_BUFSZ, PCI_DMA_TODEVICE);
1612
1613 if (frag != skb_shinfo(skb)->nr_frags)
1614 i++;
1615 }
1616 dev_kfree_skb_any(skb);
1617 }
1618 }
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static void gem_init_rings(struct gem *gp)
1622{
1623 struct gem_init_block *gb = gp->init_block;
1624 struct net_device *dev = gp->dev;
1625 int i;
1626 dma_addr_t dma_addr;
1627
1628 gp->rx_new = gp->rx_old = gp->tx_new = gp->tx_old = 0;
1629
1630 gem_clean_rings(gp);
1631
1632 gp->rx_buf_sz = max(dev->mtu + ETH_HLEN + VLAN_HLEN,
1633 (unsigned)VLAN_ETH_FRAME_LEN);
1634
1635 for (i = 0; i < RX_RING_SIZE; i++) {
1636 struct sk_buff *skb;
1637 struct gem_rxd *rxd = &gb->rxd[i];
1638
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001639 skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!skb) {
1641 rxd->buffer = 0;
1642 rxd->status_word = 0;
1643 continue;
1644 }
1645
1646 gp->rx_skbs[i] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 skb_put(skb, (gp->rx_buf_sz + RX_OFFSET));
1648 dma_addr = pci_map_page(gp->pdev,
1649 virt_to_page(skb->data),
1650 offset_in_page(skb->data),
1651 RX_BUF_ALLOC_SIZE(gp),
1652 PCI_DMA_FROMDEVICE);
1653 rxd->buffer = cpu_to_le64(dma_addr);
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001654 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
1656 skb_reserve(skb, RX_OFFSET);
1657 }
1658
1659 for (i = 0; i < TX_RING_SIZE; i++) {
1660 struct gem_txd *txd = &gb->txd[i];
1661
1662 txd->control_word = 0;
Alexander Duyckb4468cc2015-04-07 16:55:14 -07001663 dma_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 txd->buffer = 0;
1665 }
1666 wmb();
1667}
1668
1669/* Init PHY interface and start link poll state machine */
1670static void gem_init_phy(struct gem *gp)
1671{
David S. Miller7fb76aa2006-01-31 17:09:20 -08001672 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 /* Revert MIF CFG setting done on stop_phy */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001675 mifcfg = readl(gp->regs + MIF_CFG);
1676 mifcfg &= ~MIF_CFG_BBMODE;
1677 writel(mifcfg, gp->regs + MIF_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE) {
1680 int i;
1681
David S. Miller7fb76aa2006-01-31 17:09:20 -08001682 /* Those delay sucks, the HW seem to love them though, I'll
1683 * serisouly consider breaking some locks here to be able
1684 * to schedule instead
Benjamin Herrenschmidt40727192006-01-23 16:30:04 -08001685 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001686 for (i = 0; i < 3; i++) {
1687#ifdef CONFIG_PPC_PMAC
1688 pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET, gp->of_node, 0, 0);
1689 msleep(20);
1690#endif
1691 /* Some PHYs used by apple have problem getting back to us,
1692 * we do an additional reset here
1693 */
David S. Millerabc4da42014-08-27 22:59:26 -07001694 sungem_phy_write(gp, MII_BMCR, BMCR_RESET);
David S. Miller7fb76aa2006-01-31 17:09:20 -08001695 msleep(20);
David S. Millerabc4da42014-08-27 22:59:26 -07001696 if (sungem_phy_read(gp, MII_BMCR) != 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 break;
David S. Miller7fb76aa2006-01-31 17:09:20 -08001698 if (i == 2)
Joe Perchesc6c75982010-08-17 07:55:04 +00001699 netdev_warn(gp->dev, "GMAC PHY not responding !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702
1703 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
1704 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
1705 u32 val;
1706
1707 /* Init datapath mode register. */
1708 if (gp->phy_type == phy_mii_mdio0 ||
1709 gp->phy_type == phy_mii_mdio1) {
1710 val = PCS_DMODE_MGM;
1711 } else if (gp->phy_type == phy_serialink) {
1712 val = PCS_DMODE_SM | PCS_DMODE_GMOE;
1713 } else {
1714 val = PCS_DMODE_ESM;
1715 }
1716
1717 writel(val, gp->regs + PCS_DMODE);
1718 }
1719
1720 if (gp->phy_type == phy_mii_mdio0 ||
1721 gp->phy_type == phy_mii_mdio1) {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001722 /* Reset and detect MII PHY */
David S. Miller19e2f6f2011-08-15 23:10:39 -07001723 sungem_phy_probe(&gp->phy_mii, gp->mii_phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 /* Init PHY */
1726 if (gp->phy_mii.def && gp->phy_mii.def->ops->init)
1727 gp->phy_mii.def->ops->init(&gp->phy_mii);
1728 } else {
David S. Miller8c83f802008-12-09 00:10:11 -08001729 gem_pcs_reset(gp);
1730 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
1733 /* Default aneg parameters */
1734 gp->timer_ticks = 0;
1735 gp->lstate = link_down;
1736 netif_carrier_off(gp->dev);
1737
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001738 /* Print things out */
1739 if (gp->phy_type == phy_mii_mdio0 ||
1740 gp->phy_type == phy_mii_mdio1)
1741 netdev_info(gp->dev, "Found %s PHY\n",
1742 gp->phy_mii.def ? gp->phy_mii.def->name : "no");
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 gem_begin_auto_negotiation(gp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static void gem_init_dma(struct gem *gp)
1748{
1749 u64 desc_dma = (u64) gp->gblock_dvma;
1750 u32 val;
1751
1752 val = (TXDMA_CFG_BASE | (0x7ff << 10) | TXDMA_CFG_PMODE);
1753 writel(val, gp->regs + TXDMA_CFG);
1754
1755 writel(desc_dma >> 32, gp->regs + TXDMA_DBHI);
1756 writel(desc_dma & 0xffffffff, gp->regs + TXDMA_DBLOW);
1757 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
1758
1759 writel(0, gp->regs + TXDMA_KICK);
1760
1761 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
1762 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
1763 writel(val, gp->regs + RXDMA_CFG);
1764
1765 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
1766 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
1767
1768 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1769
1770 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
1771 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
1772 writel(val, gp->regs + RXDMA_PTHRESH);
1773
1774 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
1775 writel(((5 & RXDMA_BLANK_IPKTS) |
1776 ((8 << 12) & RXDMA_BLANK_ITIME)),
1777 gp->regs + RXDMA_BLANK);
1778 else
1779 writel(((5 & RXDMA_BLANK_IPKTS) |
1780 ((4 << 12) & RXDMA_BLANK_ITIME)),
1781 gp->regs + RXDMA_BLANK);
1782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784static u32 gem_setup_multicast(struct gem *gp)
1785{
1786 u32 rxcfg = 0;
1787 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if ((gp->dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001790 (netdev_mc_count(gp->dev) > 256)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 for (i=0; i<16; i++)
1792 writel(0xffff, gp->regs + MAC_HASH0 + (i << 2));
1793 rxcfg |= MAC_RXCFG_HFE;
1794 } else if (gp->dev->flags & IFF_PROMISC) {
1795 rxcfg |= MAC_RXCFG_PROM;
1796 } else {
1797 u16 hash_table[16];
1798 u32 crc;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001799 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 int i;
1801
Jiri Pirko55085902010-02-18 00:42:54 +00001802 memset(hash_table, 0, sizeof(hash_table));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001803 netdev_for_each_mc_addr(ha, gp->dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +00001804 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 crc >>= 24;
1806 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
1807 }
1808 for (i=0; i<16; i++)
1809 writel(hash_table[i], gp->regs + MAC_HASH0 + (i << 2));
1810 rxcfg |= MAC_RXCFG_HFE;
1811 }
1812
1813 return rxcfg;
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816static void gem_init_mac(struct gem *gp)
1817{
1818 unsigned char *e = &gp->dev->dev_addr[0];
1819
1820 writel(0x1bf0, gp->regs + MAC_SNDPAUSE);
1821
1822 writel(0x00, gp->regs + MAC_IPG0);
1823 writel(0x08, gp->regs + MAC_IPG1);
1824 writel(0x04, gp->regs + MAC_IPG2);
1825 writel(0x40, gp->regs + MAC_STIME);
1826 writel(0x40, gp->regs + MAC_MINFSZ);
1827
1828 /* Ethernet payload + header + FCS + optional VLAN tag. */
1829 writel(0x20000000 | (gp->rx_buf_sz + 4), gp->regs + MAC_MAXFSZ);
1830
1831 writel(0x07, gp->regs + MAC_PASIZE);
1832 writel(0x04, gp->regs + MAC_JAMSIZE);
1833 writel(0x10, gp->regs + MAC_ATTLIM);
1834 writel(0x8808, gp->regs + MAC_MCTYPE);
1835
1836 writel((e[5] | (e[4] << 8)) & 0x3ff, gp->regs + MAC_RANDSEED);
1837
1838 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
1839 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
1840 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
1841
1842 writel(0, gp->regs + MAC_ADDR3);
1843 writel(0, gp->regs + MAC_ADDR4);
1844 writel(0, gp->regs + MAC_ADDR5);
1845
1846 writel(0x0001, gp->regs + MAC_ADDR6);
1847 writel(0xc200, gp->regs + MAC_ADDR7);
1848 writel(0x0180, gp->regs + MAC_ADDR8);
1849
1850 writel(0, gp->regs + MAC_AFILT0);
1851 writel(0, gp->regs + MAC_AFILT1);
1852 writel(0, gp->regs + MAC_AFILT2);
1853 writel(0, gp->regs + MAC_AF21MSK);
1854 writel(0, gp->regs + MAC_AF0MSK);
1855
1856 gp->mac_rx_cfg = gem_setup_multicast(gp);
1857#ifdef STRIP_FCS
1858 gp->mac_rx_cfg |= MAC_RXCFG_SFCS;
1859#endif
1860 writel(0, gp->regs + MAC_NCOLL);
1861 writel(0, gp->regs + MAC_FASUCC);
1862 writel(0, gp->regs + MAC_ECOLL);
1863 writel(0, gp->regs + MAC_LCOLL);
1864 writel(0, gp->regs + MAC_DTIMER);
1865 writel(0, gp->regs + MAC_PATMPS);
1866 writel(0, gp->regs + MAC_RFCTR);
1867 writel(0, gp->regs + MAC_LERR);
1868 writel(0, gp->regs + MAC_AERR);
1869 writel(0, gp->regs + MAC_FCSERR);
1870 writel(0, gp->regs + MAC_RXCVERR);
1871
1872 /* Clear RX/TX/MAC/XIF config, we will set these up and enable
1873 * them once a link is established.
1874 */
1875 writel(0, gp->regs + MAC_TXCFG);
1876 writel(gp->mac_rx_cfg, gp->regs + MAC_RXCFG);
1877 writel(0, gp->regs + MAC_MCCFG);
1878 writel(0, gp->regs + MAC_XIFCFG);
1879
1880 /* Setup MAC interrupts. We want to get all of the interesting
1881 * counter expiration events, but we do not want to hear about
1882 * normal rx/tx as the DMA engine tells us that.
1883 */
1884 writel(MAC_TXSTAT_XMIT, gp->regs + MAC_TXMASK);
1885 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
1886
1887 /* Don't enable even the PAUSE interrupts for now, we
1888 * make no use of those events other than to record them.
1889 */
1890 writel(0xffffffff, gp->regs + MAC_MCMASK);
1891
1892 /* Don't enable GEM's WOL in normal operations
1893 */
1894 if (gp->has_wol)
1895 writel(0, gp->regs + WOL_WAKECSR);
1896}
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898static void gem_init_pause_thresholds(struct gem *gp)
1899{
1900 u32 cfg;
1901
1902 /* Calculate pause thresholds. Setting the OFF threshold to the
1903 * full RX fifo size effectively disables PAUSE generation which
1904 * is what we do for 10/100 only GEMs which have FIFOs too small
1905 * to make real gains from PAUSE.
1906 */
1907 if (gp->rx_fifo_sz <= (2 * 1024)) {
1908 gp->rx_pause_off = gp->rx_pause_on = gp->rx_fifo_sz;
1909 } else {
1910 int max_frame = (gp->rx_buf_sz + 4 + 64) & ~63;
1911 int off = (gp->rx_fifo_sz - (max_frame * 2));
1912 int on = off - max_frame;
1913
1914 gp->rx_pause_off = off;
1915 gp->rx_pause_on = on;
1916 }
1917
1918
1919 /* Configure the chip "burst" DMA mode & enable some
1920 * HW bug fixes on Apple version
1921 */
1922 cfg = 0;
1923 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE)
1924 cfg |= GREG_CFG_RONPAULBIT | GREG_CFG_ENBUG2FIX;
1925#if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
1926 cfg |= GREG_CFG_IBURST;
1927#endif
1928 cfg |= ((31 << 1) & GREG_CFG_TXDMALIM);
1929 cfg |= ((31 << 6) & GREG_CFG_RXDMALIM);
1930 writel(cfg, gp->regs + GREG_CFG);
1931
1932 /* If Infinite Burst didn't stick, then use different
1933 * thresholds (and Apple bug fixes don't exist)
1934 */
1935 if (!(readl(gp->regs + GREG_CFG) & GREG_CFG_IBURST)) {
1936 cfg = ((2 << 1) & GREG_CFG_TXDMALIM);
1937 cfg |= ((8 << 6) & GREG_CFG_RXDMALIM);
1938 writel(cfg, gp->regs + GREG_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
1942static int gem_check_invariants(struct gem *gp)
1943{
1944 struct pci_dev *pdev = gp->pdev;
1945 u32 mif_cfg;
1946
1947 /* On Apple's sungem, we can't rely on registers as the chip
1948 * was been powered down by the firmware. The PHY is looked
1949 * up later on.
1950 */
1951 if (pdev->vendor == PCI_VENDOR_ID_APPLE) {
1952 gp->phy_type = phy_mii_mdio0;
1953 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
1954 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
1955 gp->swrst_base = 0;
1956
1957 mif_cfg = readl(gp->regs + MIF_CFG);
1958 mif_cfg &= ~(MIF_CFG_PSELECT|MIF_CFG_POLL|MIF_CFG_BBMODE|MIF_CFG_MDI1);
1959 mif_cfg |= MIF_CFG_MDI0;
1960 writel(mif_cfg, gp->regs + MIF_CFG);
1961 writel(PCS_DMODE_MGM, gp->regs + PCS_DMODE);
1962 writel(MAC_XIFCFG_OE, gp->regs + MAC_XIFCFG);
1963
1964 /* We hard-code the PHY address so we can properly bring it out of
1965 * reset later on, we can't really probe it at this point, though
1966 * that isn't an issue.
1967 */
1968 if (gp->pdev->device == PCI_DEVICE_ID_APPLE_K2_GMAC)
1969 gp->mii_phy_addr = 1;
1970 else
1971 gp->mii_phy_addr = 0;
1972
1973 return 0;
1974 }
1975
1976 mif_cfg = readl(gp->regs + MIF_CFG);
1977
1978 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
1979 pdev->device == PCI_DEVICE_ID_SUN_RIO_GEM) {
1980 /* One of the MII PHYs _must_ be present
1981 * as this chip has no gigabit PHY.
1982 */
1983 if ((mif_cfg & (MIF_CFG_MDI0 | MIF_CFG_MDI1)) == 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00001984 pr_err("RIO GEM lacks MII phy, mif_cfg[%08x]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 mif_cfg);
1986 return -1;
1987 }
1988 }
1989
1990 /* Determine initial PHY interface type guess. MDIO1 is the
1991 * external PHY and thus takes precedence over MDIO0.
1992 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (mif_cfg & MIF_CFG_MDI1) {
1995 gp->phy_type = phy_mii_mdio1;
1996 mif_cfg |= MIF_CFG_PSELECT;
1997 writel(mif_cfg, gp->regs + MIF_CFG);
1998 } else if (mif_cfg & MIF_CFG_MDI0) {
1999 gp->phy_type = phy_mii_mdio0;
2000 mif_cfg &= ~MIF_CFG_PSELECT;
2001 writel(mif_cfg, gp->regs + MIF_CFG);
2002 } else {
David S. Millere54d5512009-11-17 04:46:18 -08002003#ifdef CONFIG_SPARC
2004 const char *p;
2005
2006 p = of_get_property(gp->of_node, "shared-pins", NULL);
2007 if (p && !strcmp(p, "serdes"))
2008 gp->phy_type = phy_serdes;
2009 else
2010#endif
2011 gp->phy_type = phy_serialink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 if (gp->phy_type == phy_mii_mdio1 ||
2014 gp->phy_type == phy_mii_mdio0) {
2015 int i;
2016
2017 for (i = 0; i < 32; i++) {
2018 gp->mii_phy_addr = i;
David S. Millerabc4da42014-08-27 22:59:26 -07002019 if (sungem_phy_read(gp, MII_BMCR) != 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 break;
2021 }
2022 if (i == 32) {
2023 if (pdev->device != PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002024 pr_err("RIO MII phy will not respond\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return -1;
2026 }
2027 gp->phy_type = phy_serdes;
2028 }
2029 }
2030
2031 /* Fetch the FIFO configurations now too. */
2032 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
2033 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
2034
2035 if (pdev->vendor == PCI_VENDOR_ID_SUN) {
2036 if (pdev->device == PCI_DEVICE_ID_SUN_GEM) {
2037 if (gp->tx_fifo_sz != (9 * 1024) ||
2038 gp->rx_fifo_sz != (20 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002039 pr_err("GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 gp->tx_fifo_sz, gp->rx_fifo_sz);
2041 return -1;
2042 }
2043 gp->swrst_base = 0;
2044 } else {
2045 if (gp->tx_fifo_sz != (2 * 1024) ||
2046 gp->rx_fifo_sz != (2 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002047 pr_err("RIO GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 gp->tx_fifo_sz, gp->rx_fifo_sz);
2049 return -1;
2050 }
2051 gp->swrst_base = (64 / 4) << GREG_SWRST_CACHE_SHIFT;
2052 }
2053 }
2054
2055 return 0;
2056}
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058static void gem_reinit_chip(struct gem *gp)
2059{
2060 /* Reset the chip */
2061 gem_reset(gp);
2062
2063 /* Make sure ints are disabled */
2064 gem_disable_ints(gp);
2065
2066 /* Allocate & setup ring buffers */
2067 gem_init_rings(gp);
2068
2069 /* Configure pause thresholds */
2070 gem_init_pause_thresholds(gp);
2071
2072 /* Init DMA & MAC engines */
2073 gem_init_dma(gp);
2074 gem_init_mac(gp);
2075}
2076
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078static void gem_stop_phy(struct gem *gp, int wol)
2079{
David S. Miller7fb76aa2006-01-31 17:09:20 -08002080 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* Let the chip settle down a bit, it seems that helps
2083 * for sleep mode on some models
2084 */
2085 msleep(10);
2086
2087 /* Make sure we aren't polling PHY status change. We
2088 * don't currently use that feature though
2089 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002090 mifcfg = readl(gp->regs + MIF_CFG);
2091 mifcfg &= ~MIF_CFG_POLL;
2092 writel(mifcfg, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 if (wol && gp->has_wol) {
2095 unsigned char *e = &gp->dev->dev_addr[0];
2096 u32 csr;
2097
2098 /* Setup wake-on-lan for MAGIC packet */
2099 writel(MAC_RXCFG_HFE | MAC_RXCFG_SFCS | MAC_RXCFG_ENAB,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002100 gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 writel((e[4] << 8) | e[5], gp->regs + WOL_MATCH0);
2102 writel((e[2] << 8) | e[3], gp->regs + WOL_MATCH1);
2103 writel((e[0] << 8) | e[1], gp->regs + WOL_MATCH2);
2104
2105 writel(WOL_MCOUNT_N | WOL_MCOUNT_M, gp->regs + WOL_MCOUNT);
2106 csr = WOL_WAKECSR_ENABLE;
2107 if ((readl(gp->regs + MAC_XIFCFG) & MAC_XIFCFG_GMII) == 0)
2108 csr |= WOL_WAKECSR_MII;
2109 writel(csr, gp->regs + WOL_WAKECSR);
2110 } else {
2111 writel(0, gp->regs + MAC_RXCFG);
2112 (void)readl(gp->regs + MAC_RXCFG);
2113 /* Machine sleep will die in strange ways if we
2114 * dont wait a bit here, looks like the chip takes
2115 * some time to really shut down
2116 */
2117 msleep(10);
2118 }
2119
2120 writel(0, gp->regs + MAC_TXCFG);
2121 writel(0, gp->regs + MAC_XIFCFG);
2122 writel(0, gp->regs + TXDMA_CFG);
2123 writel(0, gp->regs + RXDMA_CFG);
2124
2125 if (!wol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 gem_reset(gp);
2127 writel(MAC_TXRST_CMD, gp->regs + MAC_TXRST);
2128 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 if (found_mii_phy(gp) && gp->phy_mii.def->ops->suspend)
2131 gp->phy_mii.def->ops->suspend(&gp->phy_mii);
2132
2133 /* According to Apple, we must set the MDIO pins to this begnign
2134 * state or we may 1) eat more current, 2) damage some PHYs
2135 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002136 writel(mifcfg | MIF_CFG_BBMODE, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 writel(0, gp->regs + MIF_BBCLK);
2138 writel(0, gp->regs + MIF_BBDATA);
2139 writel(0, gp->regs + MIF_BBOENAB);
2140 writel(MAC_XIFCFG_GMII | MAC_XIFCFG_LBCK, gp->regs + MAC_XIFCFG);
2141 (void) readl(gp->regs + MAC_XIFCFG);
2142 }
2143}
2144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145static int gem_do_start(struct net_device *dev)
2146{
Wang Chen8f15ea42008-11-12 23:38:36 -08002147 struct gem *gp = netdev_priv(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002148 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* Enable the cell */
2151 gem_get_cell(gp);
2152
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002153 /* Make sure PCI access and bus master are enabled */
2154 rc = pci_enable_device(gp->pdev);
2155 if (rc) {
2156 netdev_err(dev, "Failed to enable chip on PCI bus !\n");
2157
2158 /* Put cell and forget it for now, it will be considered as
2159 * still asleep, a new sleep cycle may bring it back
2160 */
2161 gem_put_cell(gp);
2162 return -ENXIO;
2163 }
2164 pci_set_master(gp->pdev);
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* Init & setup chip hardware */
2167 gem_reinit_chip(gp);
2168
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002169 /* An interrupt might come in handy */
2170 rc = request_irq(gp->pdev->irq, gem_interrupt,
2171 IRQF_SHARED, dev->name, (void *)dev);
2172 if (rc) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002173 netdev_err(dev, "failed to request irq !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 gem_reset(gp);
2176 gem_clean_rings(gp);
2177 gem_put_cell(gp);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002178 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002181 /* Mark us as attached again if we come from resume(), this has
Joe Perchesdbedd442015-03-06 20:49:12 -08002182 * no effect if we weren't detached and needs to be done now.
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002183 */
2184 netif_device_attach(dev);
2185
2186 /* Restart NAPI & queues */
2187 gem_netif_start(gp);
2188
2189 /* Detect & init PHY, start autoneg etc... this will
2190 * eventually result in starting DMA operations when
2191 * the link is up
2192 */
2193 gem_init_phy(gp);
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return 0;
2196}
2197
2198static void gem_do_stop(struct net_device *dev, int wol)
2199{
Wang Chen8f15ea42008-11-12 23:38:36 -08002200 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002202 /* Stop NAPI and stop tx queue */
2203 gem_netif_stop(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002205 /* Make sure ints are disabled. We don't care about
2206 * synchronizing as NAPI is disabled, thus a stray
2207 * interrupt will do nothing bad (our irq handler
2208 * just schedules NAPI)
2209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 gem_disable_ints(gp);
2211
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002212 /* Stop the link timer */
2213 del_timer_sync(&gp->link_timer);
2214
2215 /* We cannot cancel the reset task while holding the
2216 * rtnl lock, we'd get an A->B / B->A deadlock stituation
2217 * if we did. This is not an issue however as the reset
2218 * task is synchronized vs. us (rtnl_lock) and will do
2219 * nothing if the device is down or suspended. We do
2220 * still clear reset_task_pending to avoid a spurrious
2221 * reset later on in case we do resume before it gets
2222 * scheduled.
2223 */
2224 gp->reset_task_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 /* If we are going to sleep with WOL */
2227 gem_stop_dma(gp);
2228 msleep(10);
2229 if (!wol)
2230 gem_reset(gp);
2231 msleep(10);
2232
2233 /* Get rid of rings */
2234 gem_clean_rings(gp);
2235
2236 /* No irq needed anymore */
2237 free_irq(gp->pdev->irq, (void *) dev);
2238
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002239 /* Shut the PHY down eventually and setup WOL */
2240 gem_stop_phy(gp, wol);
2241
2242 /* Make sure bus master is disabled */
2243 pci_disable_device(gp->pdev);
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Cell not needed neither if no WOL */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002246 if (!wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 gem_put_cell(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
David Howellsc4028952006-11-22 14:57:56 +00002250static void gem_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
David Howellsc4028952006-11-22 14:57:56 +00002252 struct gem *gp = container_of(work, struct gem, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002254 /* Lock out the network stack (essentially shield ourselves
2255 * against a racing open, close, control call, or suspend
2256 */
2257 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002259 /* Skip the reset task if suspended or closed, or if it's
2260 * been cancelled by gem_do_stop (see comment there)
2261 */
2262 if (!netif_device_present(gp->dev) ||
2263 !netif_running(gp->dev) ||
2264 !gp->reset_task_pending) {
2265 rtnl_unlock();
2266 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Johannes Bergdde655c92007-11-29 21:51:36 +11002268
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002269 /* Stop the link timer */
2270 del_timer_sync(&gp->link_timer);
2271
2272 /* Stop NAPI and tx */
2273 gem_netif_stop(gp);
2274
2275 /* Reset the chip & rings */
2276 gem_reinit_chip(gp);
2277 if (gp->lstate == link_up)
2278 gem_set_link_modes(gp);
2279
2280 /* Restart NAPI and Tx */
2281 gem_netif_start(gp);
2282
2283 /* We are back ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 gp->reset_task_pending = 0;
2285
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002286 /* If the link is not up, restart autoneg, else restart the
2287 * polling timer
2288 */
2289 if (gp->lstate != link_up)
2290 gem_begin_auto_negotiation(gp, NULL);
2291 else
2292 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002294 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297static int gem_open(struct net_device *dev)
2298{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002299 /* We allow open while suspended, we just do nothing,
2300 * the chip will be initialized in resume()
2301 */
2302 if (netif_device_present(dev))
2303 return gem_do_start(dev);
2304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307static int gem_close(struct net_device *dev)
2308{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002309 if (netif_device_present(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 gem_do_stop(dev, 0);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return 0;
2313}
2314
2315#ifdef CONFIG_PM
2316static int gem_suspend(struct pci_dev *pdev, pm_message_t state)
2317{
2318 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002319 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002321 /* Lock the network stack first to avoid racing with open/close,
2322 * reset task and setting calls
2323 */
2324 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002326 /* Not running, mark ourselves non-present, no need for
2327 * a lock here
2328 */
2329 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 netif_device_detach(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002331 rtnl_unlock();
2332 return 0;
2333 }
2334 netdev_info(dev, "suspending, WakeOnLan %s\n",
2335 (gp->wake_on_lan && netif_running(dev)) ?
2336 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002338 /* Tell the network stack we're gone. gem_do_stop() below will
2339 * synchronize with TX, stop NAPI etc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002341 netif_device_detach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002343 /* Switch off chip, remember WOL setting */
Gerard Lledo5a8887d2012-04-28 08:52:37 +00002344 gp->asleep_wol = !!gp->wake_on_lan;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002345 gem_do_stop(dev, gp->asleep_wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002347 /* Unlock the network stack */
2348 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 return 0;
2351}
2352
2353static int gem_resume(struct pci_dev *pdev)
2354{
2355 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002356 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002358 /* See locking comment in gem_suspend */
2359 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002361 /* Not running, mark ourselves present, no need for
2362 * a lock here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002364 if (!netif_running(dev)) {
2365 netif_device_attach(dev);
2366 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 return 0;
2368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002370 /* Restart chip. If that fails there isn't much we can do, we
2371 * leave things stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002373 gem_do_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* If we had WOL enabled, the cell clock was never turned off during
2376 * sleep, so we end up beeing unbalanced. Fix that here
2377 */
2378 if (gp->asleep_wol)
2379 gem_put_cell(gp);
2380
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002381 /* Unlock the network stack */
2382 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 return 0;
2385}
2386#endif /* CONFIG_PM */
2387
2388static struct net_device_stats *gem_get_stats(struct net_device *dev)
2389{
Wang Chen8f15ea42008-11-12 23:38:36 -08002390 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* I have seen this being called while the PM was in progress,
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002393 * so we shield against this. Let's also not poke at registers
2394 * while the reset task is going on.
2395 *
2396 * TODO: Move stats collection elsewhere (link timer ?) and
2397 * make this a nop to avoid all those synchro issues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002399 if (!netif_device_present(dev) || !netif_running(dev))
2400 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002402 /* Better safe than sorry... */
2403 if (WARN_ON(!gp->cell_enabled))
2404 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002406 dev->stats.rx_crc_errors += readl(gp->regs + MAC_FCSERR);
2407 writel(0, gp->regs + MAC_FCSERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002409 dev->stats.rx_frame_errors += readl(gp->regs + MAC_AERR);
2410 writel(0, gp->regs + MAC_AERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002412 dev->stats.rx_length_errors += readl(gp->regs + MAC_LERR);
2413 writel(0, gp->regs + MAC_LERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002415 dev->stats.tx_aborted_errors += readl(gp->regs + MAC_ECOLL);
2416 dev->stats.collisions +=
2417 (readl(gp->regs + MAC_ECOLL) + readl(gp->regs + MAC_LCOLL));
2418 writel(0, gp->regs + MAC_ECOLL);
2419 writel(0, gp->regs + MAC_LCOLL);
2420 bail:
Denis Kirjanovaae7c472011-01-27 09:54:12 +00002421 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002424static int gem_set_mac_address(struct net_device *dev, void *addr)
2425{
2426 struct sockaddr *macaddr = (struct sockaddr *) addr;
Wang Chen8f15ea42008-11-12 23:38:36 -08002427 struct gem *gp = netdev_priv(dev);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002428 unsigned char *e = &dev->dev_addr[0];
2429
2430 if (!is_valid_ether_addr(macaddr->sa_data))
2431 return -EADDRNOTAVAIL;
2432
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002433 memcpy(dev->dev_addr, macaddr->sa_data, dev->addr_len);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002434
2435 /* We'll just catch it later when the device is up'd or resumed */
2436 if (!netif_running(dev) || !netif_device_present(dev))
2437 return 0;
2438
2439 /* Better safe than sorry... */
2440 if (WARN_ON(!gp->cell_enabled))
2441 return 0;
2442
2443 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
2444 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
2445 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002446
2447 return 0;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static void gem_set_multicast(struct net_device *dev)
2451{
Wang Chen8f15ea42008-11-12 23:38:36 -08002452 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 u32 rxcfg, rxcfg_new;
2454 int limit = 10000;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002455
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002456 if (!netif_running(dev) || !netif_device_present(dev))
2457 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002459 /* Better safe than sorry... */
2460 if (gp->reset_task_pending || WARN_ON(!gp->cell_enabled))
2461 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 rxcfg = readl(gp->regs + MAC_RXCFG);
2464 rxcfg_new = gem_setup_multicast(gp);
2465#ifdef STRIP_FCS
2466 rxcfg_new |= MAC_RXCFG_SFCS;
2467#endif
2468 gp->mac_rx_cfg = rxcfg_new;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 writel(rxcfg & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
2471 while (readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB) {
2472 if (!limit--)
2473 break;
2474 udelay(10);
2475 }
2476
2477 rxcfg &= ~(MAC_RXCFG_PROM | MAC_RXCFG_HFE);
2478 rxcfg |= rxcfg_new;
2479
2480 writel(rxcfg, gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
2483/* Jumbo-grams don't seem to work :-( */
Jarod Wilson540bfe32016-10-17 15:54:10 -04002484#define GEM_MIN_MTU ETH_MIN_MTU
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485#if 1
Jarod Wilson540bfe32016-10-17 15:54:10 -04002486#define GEM_MAX_MTU ETH_DATA_LEN
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#else
2488#define GEM_MAX_MTU 9000
2489#endif
2490
2491static int gem_change_mtu(struct net_device *dev, int new_mtu)
2492{
Wang Chen8f15ea42008-11-12 23:38:36 -08002493 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 dev->mtu = new_mtu;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002496
2497 /* We'll just catch it later when the device is up'd or resumed */
2498 if (!netif_running(dev) || !netif_device_present(dev))
2499 return 0;
2500
2501 /* Better safe than sorry... */
2502 if (WARN_ON(!gp->cell_enabled))
2503 return 0;
2504
2505 gem_netif_stop(gp);
2506 gem_reinit_chip(gp);
2507 if (gp->lstate == link_up)
2508 gem_set_link_modes(gp);
2509 gem_netif_start(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
2511 return 0;
2512}
2513
2514static void gem_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
2515{
Wang Chen8f15ea42008-11-12 23:38:36 -08002516 struct gem *gp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002517
Rick Jones23020ab2011-11-09 09:58:07 +00002518 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
2519 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
2520 strlcpy(info->bus_info, pci_name(gp->pdev), sizeof(info->bus_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002522
Philippe Reynes92552fd2017-03-05 00:04:18 +01002523static int gem_get_link_ksettings(struct net_device *dev,
2524 struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Wang Chen8f15ea42008-11-12 23:38:36 -08002526 struct gem *gp = netdev_priv(dev);
Philippe Reynes92552fd2017-03-05 00:04:18 +01002527 u32 supported, advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 if (gp->phy_type == phy_mii_mdio0 ||
2530 gp->phy_type == phy_mii_mdio1) {
2531 if (gp->phy_mii.def)
Philippe Reynes92552fd2017-03-05 00:04:18 +01002532 supported = gp->phy_mii.def->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 else
Philippe Reynes92552fd2017-03-05 00:04:18 +01002534 supported = (SUPPORTED_10baseT_Half |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 SUPPORTED_10baseT_Full);
2536
2537 /* XXX hardcoded stuff for now */
Philippe Reynes92552fd2017-03-05 00:04:18 +01002538 cmd->base.port = PORT_MII;
2539 cmd->base.phy_address = 0; /* XXX fixed PHYAD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 /* Return current PHY settings */
Philippe Reynes92552fd2017-03-05 00:04:18 +01002542 cmd->base.autoneg = gp->want_autoneg;
2543 cmd->base.speed = gp->phy_mii.speed;
2544 cmd->base.duplex = gp->phy_mii.duplex;
2545 advertising = gp->phy_mii.advertising;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 /* If we started with a forced mode, we don't have a default
2548 * advertise set, we need to return something sensible so
2549 * userland can re-enable autoneg properly.
2550 */
Philippe Reynes92552fd2017-03-05 00:04:18 +01002551 if (advertising == 0)
2552 advertising = supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 } else { // XXX PCS ?
Philippe Reynes92552fd2017-03-05 00:04:18 +01002554 supported =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2556 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2557 SUPPORTED_Autoneg);
Philippe Reynes92552fd2017-03-05 00:04:18 +01002558 advertising = supported;
2559 cmd->base.speed = 0;
2560 cmd->base.duplex = 0;
2561 cmd->base.port = 0;
2562 cmd->base.phy_address = 0;
2563 cmd->base.autoneg = 0;
Hermann Lauerfbf02292008-12-09 15:39:14 -08002564
2565 /* serdes means usually a Fibre connector, with most fixed */
2566 if (gp->phy_type == phy_serdes) {
Philippe Reynes92552fd2017-03-05 00:04:18 +01002567 cmd->base.port = PORT_FIBRE;
2568 supported = (SUPPORTED_1000baseT_Half |
Hermann Lauerfbf02292008-12-09 15:39:14 -08002569 SUPPORTED_1000baseT_Full |
2570 SUPPORTED_FIBRE | SUPPORTED_Autoneg |
2571 SUPPORTED_Pause | SUPPORTED_Asym_Pause);
Philippe Reynes92552fd2017-03-05 00:04:18 +01002572 advertising = supported;
Hermann Lauerfbf02292008-12-09 15:39:14 -08002573 if (gp->lstate == link_up)
Philippe Reynes92552fd2017-03-05 00:04:18 +01002574 cmd->base.speed = SPEED_1000;
2575 cmd->base.duplex = DUPLEX_FULL;
2576 cmd->base.autoneg = 1;
Hermann Lauerfbf02292008-12-09 15:39:14 -08002577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
Philippe Reynes92552fd2017-03-05 00:04:18 +01002579
2580 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2581 supported);
2582 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2583 advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 return 0;
2586}
2587
Philippe Reynes92552fd2017-03-05 00:04:18 +01002588static int gem_set_link_ksettings(struct net_device *dev,
2589 const struct ethtool_link_ksettings *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Wang Chen8f15ea42008-11-12 23:38:36 -08002591 struct gem *gp = netdev_priv(dev);
Philippe Reynes92552fd2017-03-05 00:04:18 +01002592 u32 speed = cmd->base.speed;
2593 u32 advertising;
2594
2595 ethtool_convert_link_mode_to_legacy_u32(&advertising,
2596 cmd->link_modes.advertising);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /* Verify the settings we care about. */
Philippe Reynes92552fd2017-03-05 00:04:18 +01002599 if (cmd->base.autoneg != AUTONEG_ENABLE &&
2600 cmd->base.autoneg != AUTONEG_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return -EINVAL;
2602
Philippe Reynes92552fd2017-03-05 00:04:18 +01002603 if (cmd->base.autoneg == AUTONEG_ENABLE &&
2604 advertising == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 return -EINVAL;
2606
Philippe Reynes92552fd2017-03-05 00:04:18 +01002607 if (cmd->base.autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +00002608 ((speed != SPEED_1000 &&
2609 speed != SPEED_100 &&
2610 speed != SPEED_10) ||
Philippe Reynes92552fd2017-03-05 00:04:18 +01002611 (cmd->base.duplex != DUPLEX_HALF &&
2612 cmd->base.duplex != DUPLEX_FULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* Apply settings and restart link process. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002616 if (netif_device_present(gp->dev)) {
2617 del_timer_sync(&gp->link_timer);
2618 gem_begin_auto_negotiation(gp, cmd);
2619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 return 0;
2622}
2623
2624static int gem_nway_reset(struct net_device *dev)
2625{
Wang Chen8f15ea42008-11-12 23:38:36 -08002626 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628 if (!gp->want_autoneg)
2629 return -EINVAL;
2630
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002631 /* Restart link process */
2632 if (netif_device_present(gp->dev)) {
2633 del_timer_sync(&gp->link_timer);
2634 gem_begin_auto_negotiation(gp, NULL);
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 return 0;
2638}
2639
2640static u32 gem_get_msglevel(struct net_device *dev)
2641{
Wang Chen8f15ea42008-11-12 23:38:36 -08002642 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return gp->msg_enable;
2644}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646static void gem_set_msglevel(struct net_device *dev, u32 value)
2647{
Wang Chen8f15ea42008-11-12 23:38:36 -08002648 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 gp->msg_enable = value;
2650}
2651
2652
2653/* Add more when I understand how to program the chip */
2654/* like WAKE_UCAST | WAKE_MCAST | WAKE_BCAST */
2655
2656#define WOL_SUPPORTED_MASK (WAKE_MAGIC)
2657
2658static void gem_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2659{
Wang Chen8f15ea42008-11-12 23:38:36 -08002660 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 /* Add more when I understand how to program the chip */
2663 if (gp->has_wol) {
2664 wol->supported = WOL_SUPPORTED_MASK;
2665 wol->wolopts = gp->wake_on_lan;
2666 } else {
2667 wol->supported = 0;
2668 wol->wolopts = 0;
2669 }
2670}
2671
2672static int gem_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2673{
Wang Chen8f15ea42008-11-12 23:38:36 -08002674 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 if (!gp->has_wol)
2677 return -EOPNOTSUPP;
2678 gp->wake_on_lan = wol->wolopts & WOL_SUPPORTED_MASK;
2679 return 0;
2680}
2681
Jeff Garzik7282d492006-09-13 14:30:00 -04002682static const struct ethtool_ops gem_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 .get_drvinfo = gem_get_drvinfo,
2684 .get_link = ethtool_op_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 .nway_reset = gem_nway_reset,
2686 .get_msglevel = gem_get_msglevel,
2687 .set_msglevel = gem_set_msglevel,
2688 .get_wol = gem_get_wol,
2689 .set_wol = gem_set_wol,
Philippe Reynes92552fd2017-03-05 00:04:18 +01002690 .get_link_ksettings = gem_get_link_ksettings,
2691 .set_link_ksettings = gem_set_link_ksettings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692};
2693
2694static int gem_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2695{
Wang Chen8f15ea42008-11-12 23:38:36 -08002696 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct mii_ioctl_data *data = if_mii(ifr);
2698 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002700 /* For SIOCGMIIREG and SIOCSMIIREG the core checks for us that
2701 * netif_device_present() is true and holds rtnl_lock for us
2702 * so we have nothing to worry about
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 switch (cmd) {
2706 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2707 data->phy_id = gp->mii_phy_addr;
2708 /* Fallthrough... */
2709
2710 case SIOCGMIIREG: /* Read MII PHY register. */
David S. Millerabc4da42014-08-27 22:59:26 -07002711 data->val_out = __sungem_phy_read(gp, data->phy_id & 0x1f,
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002712 data->reg_num & 0x1f);
2713 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 break;
2715
2716 case SIOCSMIIREG: /* Write MII PHY register. */
David S. Millerabc4da42014-08-27 22:59:26 -07002717 __sungem_phy_write(gp, data->phy_id & 0x1f, data->reg_num & 0x1f,
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002718 data->val_in);
2719 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 break;
Joe Perches6403eab2011-06-03 11:51:20 +00002721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return rc;
2723}
2724
David S. Millerdadb8302007-02-28 15:42:50 -08002725#if (!defined(CONFIG_SPARC) && !defined(CONFIG_PPC_PMAC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726/* Fetch MAC address from vital product data of PCI ROM. */
Linus Torvalds4120b022005-09-11 09:26:20 -07002727static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, unsigned char *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
2729 int this_offset;
2730
2731 for (this_offset = 0x20; this_offset < len; this_offset++) {
2732 void __iomem *p = rom_base + this_offset;
2733 int i;
2734
2735 if (readb(p + 0) != 0x90 ||
2736 readb(p + 1) != 0x00 ||
2737 readb(p + 2) != 0x09 ||
2738 readb(p + 3) != 0x4e ||
2739 readb(p + 4) != 0x41 ||
2740 readb(p + 5) != 0x06)
2741 continue;
2742
2743 this_offset += 6;
2744 p += 6;
2745
2746 for (i = 0; i < 6; i++)
2747 dev_addr[i] = readb(p + i);
Linus Torvalds4120b022005-09-11 09:26:20 -07002748 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
Linus Torvalds4120b022005-09-11 09:26:20 -07002750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753static void get_gem_mac_nonobp(struct pci_dev *pdev, unsigned char *dev_addr)
2754{
Linus Torvalds4120b022005-09-11 09:26:20 -07002755 size_t size;
2756 void __iomem *p = pci_map_rom(pdev, &size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Linus Torvalds4120b022005-09-11 09:26:20 -07002758 if (p) {
2759 int found;
2760
2761 found = readb(p) == 0x55 &&
2762 readb(p + 1) == 0xaa &&
2763 find_eth_addr_in_vpd(p, (64 * 1024), dev_addr);
2764 pci_unmap_rom(pdev, p);
2765 if (found)
2766 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /* Sun MAC prefix then 3 random bytes. */
2770 dev_addr[0] = 0x08;
2771 dev_addr[1] = 0x00;
2772 dev_addr[2] = 0x20;
2773 get_random_bytes(dev_addr + 3, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775#endif /* not Sparc and not PPC */
2776
Bill Pembertonf73d12b2012-12-03 09:24:02 -05002777static int gem_get_device_address(struct gem *gp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
David S. Millerdadb8302007-02-28 15:42:50 -08002779#if defined(CONFIG_SPARC) || defined(CONFIG_PPC_PMAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct net_device *dev = gp->dev;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002781 const unsigned char *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10002783 addr = of_get_property(gp->of_node, "local-mac-address", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (addr == NULL) {
David S. Miller457e1a82007-03-29 01:36:44 -07002785#ifdef CONFIG_SPARC
2786 addr = idprom->id_ethaddr;
2787#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 printk("\n");
Joe Perchesc6c75982010-08-17 07:55:04 +00002789 pr_err("%s: can't get mac-address\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return -1;
David S. Miller457e1a82007-03-29 01:36:44 -07002791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Joe Perchesd458cdf2013-10-01 19:04:40 -07002793 memcpy(dev->dev_addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794#else
2795 get_gem_mac_nonobp(gp->pdev, gp->dev->dev_addr);
2796#endif
2797 return 0;
2798}
2799
Adrian Bunk14904392005-12-21 18:50:12 -08002800static void gem_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct net_device *dev = pci_get_drvdata(pdev);
2803
2804 if (dev) {
Wang Chen8f15ea42008-11-12 23:38:36 -08002805 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 unregister_netdev(dev);
2808
Joe Perchesdbedd442015-03-06 20:49:12 -08002809 /* Ensure reset task is truly gone */
Tejun Heofe8998c2010-12-12 16:45:14 +01002810 cancel_work_sync(&gp->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /* Free resources */
2813 pci_free_consistent(pdev,
2814 sizeof(struct gem_init_block),
2815 gp->init_block,
2816 gp->gblock_dvma);
2817 iounmap(gp->regs);
2818 pci_release_regions(pdev);
2819 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821}
2822
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002823static const struct net_device_ops gem_netdev_ops = {
2824 .ndo_open = gem_open,
2825 .ndo_stop = gem_close,
2826 .ndo_start_xmit = gem_start_xmit,
2827 .ndo_get_stats = gem_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002828 .ndo_set_rx_mode = gem_set_multicast,
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002829 .ndo_do_ioctl = gem_ioctl,
2830 .ndo_tx_timeout = gem_tx_timeout,
2831 .ndo_change_mtu = gem_change_mtu,
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002832 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger5ed01022009-03-19 23:58:01 -07002833 .ndo_set_mac_address = gem_set_mac_address,
2834#ifdef CONFIG_NET_POLL_CONTROLLER
2835 .ndo_poll_controller = gem_poll_controller,
2836#endif
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002837};
2838
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002839static int gem_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 unsigned long gemreg_base, gemreg_len;
2842 struct net_device *dev;
2843 struct gem *gp;
Joe Perches0795af52007-10-03 17:59:30 -07002844 int err, pci_using_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Joe Perchesc6c75982010-08-17 07:55:04 +00002846 printk_once(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 /* Apple gmac note: during probe, the chip is powered up by
2849 * the arch code to allow the code below to work (and to let
2850 * the chip be probed on the config space. It won't stay powered
2851 * up until the interface is brought up however, so we can't rely
2852 * on register configuration done at this point.
2853 */
2854 err = pci_enable_device(pdev);
2855 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002856 pr_err("Cannot enable MMIO operation, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return err;
2858 }
2859 pci_set_master(pdev);
2860
2861 /* Configure DMA attributes. */
2862
2863 /* All of the GEM documentation states that 64-bit DMA addressing
2864 * is fully supported and should work just fine. However the
2865 * front end for RIO based GEMs is different and only supports
2866 * 32-bit addressing.
2867 *
2868 * For now we assume the various PPC GEMs are 32-bit only as well.
2869 */
2870 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
2871 pdev->device == PCI_DEVICE_ID_SUN_GEM &&
Yang Hongyang6a355282009-04-06 19:01:13 -07002872 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 pci_using_dac = 1;
2874 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002875 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002877 pr_err("No usable DMA configuration, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 goto err_disable_device;
2879 }
2880 pci_using_dac = 0;
2881 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 gemreg_base = pci_resource_start(pdev, 0);
2884 gemreg_len = pci_resource_len(pdev, 0);
2885
2886 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002887 pr_err("Cannot find proper PCI device base address, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 err = -ENODEV;
2889 goto err_disable_device;
2890 }
2891
2892 dev = alloc_etherdev(sizeof(*gp));
2893 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 err = -ENOMEM;
2895 goto err_disable_device;
2896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 SET_NETDEV_DEV(dev, &pdev->dev);
2898
Wang Chen8f15ea42008-11-12 23:38:36 -08002899 gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 err = pci_request_regions(pdev, DRV_NAME);
2902 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002903 pr_err("Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 goto err_out_free_netdev;
2905 }
2906
2907 gp->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 gp->dev = dev;
2909
2910 gp->msg_enable = DEFAULT_MSG;
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 init_timer(&gp->link_timer);
2913 gp->link_timer.function = gem_link_timer;
2914 gp->link_timer.data = (unsigned long) gp;
2915
David Howellsc4028952006-11-22 14:57:56 +00002916 INIT_WORK(&gp->reset_task, gem_reset_task);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 gp->lstate = link_down;
2919 gp->timer_ticks = 0;
2920 netif_carrier_off(dev);
2921
2922 gp->regs = ioremap(gemreg_base, gemreg_len);
Al Viro79ea13c2008-01-24 02:06:46 -08002923 if (!gp->regs) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002924 pr_err("Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 err = -EIO;
2926 goto err_out_free_res;
2927 }
2928
2929 /* On Apple, we want a reference to the Open Firmware device-tree
2930 * node. We use it for clock control.
2931 */
David S. Miller457e1a82007-03-29 01:36:44 -07002932#if defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 gp->of_node = pci_device_to_OF_node(pdev);
2934#endif
2935
2936 /* Only Apple version supports WOL afaik */
2937 if (pdev->vendor == PCI_VENDOR_ID_APPLE)
2938 gp->has_wol = 1;
2939
2940 /* Make sure cell is enabled */
2941 gem_get_cell(gp);
2942
2943 /* Make sure everything is stopped and in init state */
2944 gem_reset(gp);
2945
2946 /* Fill up the mii_phy structure (even if we won't use it) */
2947 gp->phy_mii.dev = dev;
David S. Millerabc4da42014-08-27 22:59:26 -07002948 gp->phy_mii.mdio_read = _sungem_phy_read;
2949 gp->phy_mii.mdio_write = _sungem_phy_write;
Benjamin Herrenschmidt3c326fe2005-07-07 17:56:09 -07002950#ifdef CONFIG_PPC_PMAC
2951 gp->phy_mii.platform_data = gp->of_node;
2952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 /* By default, we start with autoneg */
2954 gp->want_autoneg = 1;
2955
2956 /* Check fifo sizes, PHY type, etc... */
2957 if (gem_check_invariants(gp)) {
2958 err = -ENODEV;
2959 goto err_out_iounmap;
2960 }
2961
2962 /* It is guaranteed that the returned buffer will be at least
2963 * PAGE_SIZE aligned.
2964 */
2965 gp->init_block = (struct gem_init_block *)
2966 pci_alloc_consistent(pdev, sizeof(struct gem_init_block),
2967 &gp->gblock_dvma);
2968 if (!gp->init_block) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002969 pr_err("Cannot allocate init block, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 err = -ENOMEM;
2971 goto err_out_iounmap;
2972 }
2973
Peter Senna Tschudin4df12832012-10-05 12:40:54 +00002974 err = gem_get_device_address(gp);
2975 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto err_out_free_consistent;
2977
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002978 dev->netdev_ops = &gem_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002979 netif_napi_add(dev, &gp->napi, gem_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 dev->ethtool_ops = &gem_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 dev->watchdog_timeo = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 dev->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 /* Set that now, in case PM kicks in now */
2985 pci_set_drvdata(pdev, dev);
2986
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002987 /* We can do scatter/gather and HW checksum */
2988 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2989 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2990 if (pci_using_dac)
2991 dev->features |= NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Jarod Wilson540bfe32016-10-17 15:54:10 -04002993 /* MTU range: 68 - 1500 (Jumbo mode is broken) */
2994 dev->min_mtu = GEM_MIN_MTU;
2995 dev->max_mtu = GEM_MAX_MTU;
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 /* Register with kernel */
2998 if (register_netdev(dev)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002999 pr_err("Cannot register net device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 err = -ENOMEM;
3001 goto err_out_free_consistent;
3002 }
3003
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10003004 /* Undo the get_cell with appropriate locking (we could use
3005 * ndo_init/uninit but that would be even more clumsy imho)
3006 */
3007 rtnl_lock();
3008 gem_put_cell(gp);
3009 rtnl_unlock();
3010
Joe Perchesc6c75982010-08-17 07:55:04 +00003011 netdev_info(dev, "Sun GEM (PCI) 10/100/1000BaseT Ethernet %pM\n",
3012 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return 0;
3014
3015err_out_free_consistent:
3016 gem_remove_one(pdev);
3017err_out_iounmap:
3018 gem_put_cell(gp);
3019 iounmap(gp->regs);
3020
3021err_out_free_res:
3022 pci_release_regions(pdev);
3023
3024err_out_free_netdev:
3025 free_netdev(dev);
3026err_disable_device:
3027 pci_disable_device(pdev);
3028 return err;
3029
3030}
3031
3032
3033static struct pci_driver gem_driver = {
3034 .name = GEM_MODULE_NAME,
3035 .id_table = gem_pci_tbl,
3036 .probe = gem_init_one,
Adrian Bunk14904392005-12-21 18:50:12 -08003037 .remove = gem_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038#ifdef CONFIG_PM
3039 .suspend = gem_suspend,
3040 .resume = gem_resume,
3041#endif /* CONFIG_PM */
3042};
3043
Peter Hüwe5119ad02013-05-21 12:58:09 +00003044module_pci_driver(gem_driver);