blob: b5655b79bd3b25bcd2e90376fd18e67fb78e1f93 [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>
27#include <linux/init.h>
28#include <linux/errno.h>
29#include <linux/pci.h>
Domen Puncer1e7f0bd2005-06-26 18:22:14 -040030#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/mii.h>
35#include <linux/ethtool.h>
36#include <linux/crc32.h>
37#include <linux/random.h>
38#include <linux/workqueue.h>
39#include <linux/if_vlan.h>
40#include <linux/bitops.h>
Al Virod7fe0f22006-12-03 23:15:30 -050041#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/io.h>
45#include <asm/byteorder.h>
46#include <asm/uaccess.h>
47#include <asm/irq.h>
48
David S. Millerdadb8302007-02-28 15:42:50 -080049#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/idprom.h>
Jiri Kosina59034172011-07-29 17:31:16 +020051#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
53
54#ifdef CONFIG_PPC_PMAC
55#include <asm/pci-bridge.h>
Jiri Kosina59034172011-07-29 17:31:16 +020056#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/machdep.h>
58#include <asm/pmac_feature.h>
59#endif
60
David S. Miller2bb69842011-08-14 22:52:04 -070061#include <linux/sungem_phy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "sungem.h"
63
64/* Stripping FCS is causing problems, disabled for now */
65#undef STRIP_FCS
66
67#define DEFAULT_MSG (NETIF_MSG_DRV | \
68 NETIF_MSG_PROBE | \
69 NETIF_MSG_LINK)
70
71#define ADVERTISE_MASK (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
72 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
Benjamin Herrenschmidt63ea9982007-01-03 18:54:43 -080073 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full | \
74 SUPPORTED_Pause | SUPPORTED_Autoneg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define DRV_NAME "sungem"
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100077#define DRV_VERSION "1.0"
78#define DRV_AUTHOR "David S. Miller <davem@redhat.com>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Bill Pembertonf73d12b2012-12-03 09:24:02 -050080static char version[] =
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +100081 DRV_NAME ".c:v" DRV_VERSION " " DRV_AUTHOR "\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83MODULE_AUTHOR(DRV_AUTHOR);
84MODULE_DESCRIPTION("Sun GEM Gbit ethernet driver");
85MODULE_LICENSE("GPL");
86
87#define GEM_MODULE_NAME "gem"
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000089static DEFINE_PCI_DEVICE_TABLE(gem_pci_tbl) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_GEM,
91 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
92
93 /* These models only differ from the original GEM in
94 * that their tx/rx fifos are of a different size and
95 * they only support 10/100 speeds. -DaveM
Jeff Garzik6aa20a22006-09-13 13:24:59 -040096 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * Apple's GMAC does support gigabit on machines with
98 * the BCM54xx PHYs. -BenH
99 */
100 { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_RIO_GEM,
101 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
102 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC,
103 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
104 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMACP,
105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
106 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_GMAC2,
107 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
108 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_K2_GMAC,
109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
110 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_SH_SUNGEM,
111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Olof Johansson7fce2602005-11-13 16:06:48 -0800112 { PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_IPID2_GMAC,
113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 {0, }
115};
116
117MODULE_DEVICE_TABLE(pci, gem_pci_tbl);
118
119static u16 __phy_read(struct gem *gp, int phy_addr, int reg)
120{
121 u32 cmd;
122 int limit = 10000;
123
124 cmd = (1 << 30);
125 cmd |= (2 << 28);
126 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
127 cmd |= (reg << 18) & MIF_FRAME_REGAD;
128 cmd |= (MIF_FRAME_TAMSB);
129 writel(cmd, gp->regs + MIF_FRAME);
130
Roel Kluin46578a692009-02-02 21:39:02 -0800131 while (--limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 cmd = readl(gp->regs + MIF_FRAME);
133 if (cmd & MIF_FRAME_TALSB)
134 break;
135
136 udelay(10);
137 }
138
139 if (!limit)
140 cmd = 0xffff;
141
142 return cmd & MIF_FRAME_DATA;
143}
144
145static inline int _phy_read(struct net_device *dev, int mii_id, int reg)
146{
Wang Chen8f15ea42008-11-12 23:38:36 -0800147 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return __phy_read(gp, mii_id, reg);
149}
150
151static inline u16 phy_read(struct gem *gp, int reg)
152{
153 return __phy_read(gp, gp->mii_phy_addr, reg);
154}
155
156static void __phy_write(struct gem *gp, int phy_addr, int reg, u16 val)
157{
158 u32 cmd;
159 int limit = 10000;
160
161 cmd = (1 << 30);
162 cmd |= (1 << 28);
163 cmd |= (phy_addr << 23) & MIF_FRAME_PHYAD;
164 cmd |= (reg << 18) & MIF_FRAME_REGAD;
165 cmd |= (MIF_FRAME_TAMSB);
166 cmd |= (val & MIF_FRAME_DATA);
167 writel(cmd, gp->regs + MIF_FRAME);
168
169 while (limit--) {
170 cmd = readl(gp->regs + MIF_FRAME);
171 if (cmd & MIF_FRAME_TALSB)
172 break;
173
174 udelay(10);
175 }
176}
177
178static inline void _phy_write(struct net_device *dev, int mii_id, int reg, int val)
179{
Wang Chen8f15ea42008-11-12 23:38:36 -0800180 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 __phy_write(gp, mii_id, reg, val & 0xffff);
182}
183
184static inline void phy_write(struct gem *gp, int reg, u16 val)
185{
186 __phy_write(gp, gp->mii_phy_addr, reg, val);
187}
188
189static inline void gem_enable_ints(struct gem *gp)
190{
191 /* Enable all interrupts but TXDONE */
192 writel(GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
193}
194
195static inline void gem_disable_ints(struct gem *gp)
196{
197 /* Disable all interrupts, including TXDONE */
198 writel(GREG_STAT_NAPI | GREG_STAT_TXDONE, gp->regs + GREG_IMASK);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000199 (void)readl(gp->regs + GREG_IMASK); /* write posting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202static void gem_get_cell(struct gem *gp)
203{
204 BUG_ON(gp->cell_enabled < 0);
205 gp->cell_enabled++;
206#ifdef CONFIG_PPC_PMAC
207 if (gp->cell_enabled == 1) {
208 mb();
209 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 1);
210 udelay(10);
211 }
212#endif /* CONFIG_PPC_PMAC */
213}
214
215/* Turn off the chip's clock */
216static void gem_put_cell(struct gem *gp)
217{
218 BUG_ON(gp->cell_enabled <= 0);
219 gp->cell_enabled--;
220#ifdef CONFIG_PPC_PMAC
221 if (gp->cell_enabled == 0) {
222 mb();
223 pmac_call_feature(PMAC_FTR_GMAC_ENABLE, gp->of_node, 0, 0);
224 udelay(10);
225 }
226#endif /* CONFIG_PPC_PMAC */
227}
228
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000229static inline void gem_netif_stop(struct gem *gp)
230{
231 gp->dev->trans_start = jiffies; /* prevent tx timeout */
232 napi_disable(&gp->napi);
233 netif_tx_disable(gp->dev);
234}
235
236static inline void gem_netif_start(struct gem *gp)
237{
238 /* NOTE: unconditional netif_wake_queue is only
239 * appropriate so long as all callers are assured to
240 * have free tx slots.
241 */
242 netif_wake_queue(gp->dev);
243 napi_enable(&gp->napi);
244}
245
246static void gem_schedule_reset(struct gem *gp)
247{
248 gp->reset_task_pending = 1;
249 schedule_work(&gp->reset_task);
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static void gem_handle_mif_event(struct gem *gp, u32 reg_val, u32 changed_bits)
253{
254 if (netif_msg_intr(gp))
255 printk(KERN_DEBUG "%s: mif interrupt\n", gp->dev->name);
256}
257
258static int gem_pcs_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
259{
260 u32 pcs_istat = readl(gp->regs + PCS_ISTAT);
261 u32 pcs_miistat;
262
263 if (netif_msg_intr(gp))
264 printk(KERN_DEBUG "%s: pcs interrupt, pcs_istat: 0x%x\n",
265 gp->dev->name, pcs_istat);
266
267 if (!(pcs_istat & PCS_ISTAT_LSC)) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000268 netdev_err(dev, "PCS irq but no link status change???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270 }
271
272 /* The link status bit latches on zero, so you must
273 * read it twice in such a case to see a transition
274 * to the link being up.
275 */
276 pcs_miistat = readl(gp->regs + PCS_MIISTAT);
277 if (!(pcs_miistat & PCS_MIISTAT_LS))
278 pcs_miistat |=
279 (readl(gp->regs + PCS_MIISTAT) &
280 PCS_MIISTAT_LS);
281
282 if (pcs_miistat & PCS_MIISTAT_ANC) {
283 /* The remote-fault indication is only valid
284 * when autoneg has completed.
285 */
286 if (pcs_miistat & PCS_MIISTAT_RF)
Joe Perchesc6c75982010-08-17 07:55:04 +0000287 netdev_info(dev, "PCS AutoNEG complete, RemoteFault\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 else
Joe Perchesc6c75982010-08-17 07:55:04 +0000289 netdev_info(dev, "PCS AutoNEG complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292 if (pcs_miistat & PCS_MIISTAT_LS) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000293 netdev_info(dev, "PCS link is now up\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 netif_carrier_on(gp->dev);
295 } else {
Joe Perchesc6c75982010-08-17 07:55:04 +0000296 netdev_info(dev, "PCS link is now down\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 netif_carrier_off(gp->dev);
298 /* If this happens and the link timer is not running,
299 * reset so we re-negotiate.
300 */
301 if (!timer_pending(&gp->link_timer))
302 return 1;
303 }
304
305 return 0;
306}
307
308static int gem_txmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
309{
310 u32 txmac_stat = readl(gp->regs + MAC_TXSTAT);
311
312 if (netif_msg_intr(gp))
313 printk(KERN_DEBUG "%s: txmac interrupt, txmac_stat: 0x%x\n",
314 gp->dev->name, txmac_stat);
315
316 /* Defer timer expiration is quite normal,
317 * don't even log the event.
318 */
319 if ((txmac_stat & MAC_TXSTAT_DTE) &&
320 !(txmac_stat & ~MAC_TXSTAT_DTE))
321 return 0;
322
323 if (txmac_stat & MAC_TXSTAT_URUN) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000324 netdev_err(dev, "TX MAC xmit underrun\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000325 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 if (txmac_stat & MAC_TXSTAT_MPE) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000329 netdev_err(dev, "TX MAC max packet size error\n");
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000330 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 /* The rest are all cases of one of the 16-bit TX
334 * counters expiring.
335 */
336 if (txmac_stat & MAC_TXSTAT_NCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000337 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 if (txmac_stat & MAC_TXSTAT_ECE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000340 dev->stats.tx_aborted_errors += 0x10000;
341 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 if (txmac_stat & MAC_TXSTAT_LCE) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000345 dev->stats.tx_aborted_errors += 0x10000;
346 dev->stats.collisions += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* We do not keep track of MAC_TXSTAT_FCE and
350 * MAC_TXSTAT_PCE events.
351 */
352 return 0;
353}
354
355/* When we get a RX fifo overflow, the RX unit in GEM is probably hung
356 * so we do the following.
357 *
358 * If any part of the reset goes wrong, we return 1 and that causes the
359 * whole chip to be reset.
360 */
361static int gem_rxmac_reset(struct gem *gp)
362{
363 struct net_device *dev = gp->dev;
364 int limit, i;
365 u64 desc_dma;
366 u32 val;
367
368 /* First, reset & disable MAC RX. */
369 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
370 for (limit = 0; limit < 5000; limit++) {
371 if (!(readl(gp->regs + MAC_RXRST) & MAC_RXRST_CMD))
372 break;
373 udelay(10);
374 }
375 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000376 netdev_err(dev, "RX MAC will not reset, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 1;
378 }
379
380 writel(gp->mac_rx_cfg & ~MAC_RXCFG_ENAB,
381 gp->regs + MAC_RXCFG);
382 for (limit = 0; limit < 5000; limit++) {
383 if (!(readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB))
384 break;
385 udelay(10);
386 }
387 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000388 netdev_err(dev, "RX MAC will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390 }
391
392 /* Second, disable RX DMA. */
393 writel(0, gp->regs + RXDMA_CFG);
394 for (limit = 0; limit < 5000; limit++) {
395 if (!(readl(gp->regs + RXDMA_CFG) & RXDMA_CFG_ENABLE))
396 break;
397 udelay(10);
398 }
399 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000400 netdev_err(dev, "RX DMA will not disable, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 1;
402 }
403
Arnd Bergmann3a22d5d2012-04-20 10:56:15 +0000404 mdelay(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* Execute RX reset command. */
407 writel(gp->swrst_base | GREG_SWRST_RXRST,
408 gp->regs + GREG_SWRST);
409 for (limit = 0; limit < 5000; limit++) {
410 if (!(readl(gp->regs + GREG_SWRST) & GREG_SWRST_RXRST))
411 break;
412 udelay(10);
413 }
414 if (limit == 5000) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000415 netdev_err(dev, "RX reset command will not execute, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return 1;
417 }
418
419 /* Refresh the RX ring. */
420 for (i = 0; i < RX_RING_SIZE; i++) {
421 struct gem_rxd *rxd = &gp->init_block->rxd[i];
422
423 if (gp->rx_skbs[i] == NULL) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000424 netdev_err(dev, "Parts of RX ring empty, resetting whole chip\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 1;
426 }
427
428 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
429 }
430 gp->rx_new = gp->rx_old = 0;
431
432 /* Now we must reprogram the rest of RX unit. */
433 desc_dma = (u64) gp->gblock_dvma;
434 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
435 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
436 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
437 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
438 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
439 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
440 writel(val, gp->regs + RXDMA_CFG);
441 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
442 writel(((5 & RXDMA_BLANK_IPKTS) |
443 ((8 << 12) & RXDMA_BLANK_ITIME)),
444 gp->regs + RXDMA_BLANK);
445 else
446 writel(((5 & RXDMA_BLANK_IPKTS) |
447 ((4 << 12) & RXDMA_BLANK_ITIME)),
448 gp->regs + RXDMA_BLANK);
449 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
450 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
451 writel(val, gp->regs + RXDMA_PTHRESH);
452 val = readl(gp->regs + RXDMA_CFG);
453 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
454 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
455 val = readl(gp->regs + MAC_RXCFG);
456 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
457
458 return 0;
459}
460
461static int gem_rxmac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
462{
463 u32 rxmac_stat = readl(gp->regs + MAC_RXSTAT);
464 int ret = 0;
465
466 if (netif_msg_intr(gp))
467 printk(KERN_DEBUG "%s: rxmac interrupt, rxmac_stat: 0x%x\n",
468 gp->dev->name, rxmac_stat);
469
470 if (rxmac_stat & MAC_RXSTAT_OFLW) {
471 u32 smac = readl(gp->regs + MAC_SMACHINE);
472
Joe Perchesc6c75982010-08-17 07:55:04 +0000473 netdev_err(dev, "RX MAC fifo overflow smac[%08x]\n", smac);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000474 dev->stats.rx_over_errors++;
475 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 ret = gem_rxmac_reset(gp);
478 }
479
480 if (rxmac_stat & MAC_RXSTAT_ACE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000481 dev->stats.rx_frame_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (rxmac_stat & MAC_RXSTAT_CCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000484 dev->stats.rx_crc_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (rxmac_stat & MAC_RXSTAT_LCE)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000487 dev->stats.rx_length_errors += 0x10000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 /* We do not track MAC_RXSTAT_FCE and MAC_RXSTAT_VCE
490 * events.
491 */
492 return ret;
493}
494
495static int gem_mac_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
496{
497 u32 mac_cstat = readl(gp->regs + MAC_CSTAT);
498
499 if (netif_msg_intr(gp))
500 printk(KERN_DEBUG "%s: mac interrupt, mac_cstat: 0x%x\n",
501 gp->dev->name, mac_cstat);
502
503 /* This interrupt is just for pause frame and pause
504 * tracking. It is useful for diagnostics and debug
505 * but probably by default we will mask these events.
506 */
507 if (mac_cstat & MAC_CSTAT_PS)
508 gp->pause_entered++;
509
510 if (mac_cstat & MAC_CSTAT_PRCV)
511 gp->pause_last_time_recvd = (mac_cstat >> 16);
512
513 return 0;
514}
515
516static int gem_mif_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
517{
518 u32 mif_status = readl(gp->regs + MIF_STATUS);
519 u32 reg_val, changed_bits;
520
521 reg_val = (mif_status & MIF_STATUS_DATA) >> 16;
522 changed_bits = (mif_status & MIF_STATUS_STAT);
523
524 gem_handle_mif_event(gp, reg_val, changed_bits);
525
526 return 0;
527}
528
529static int gem_pci_interrupt(struct net_device *dev, struct gem *gp, u32 gem_status)
530{
531 u32 pci_estat = readl(gp->regs + GREG_PCIESTAT);
532
533 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
534 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +0000535 netdev_err(dev, "PCI error [%04x]", pci_estat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (pci_estat & GREG_PCIESTAT_BADACK)
Joe Perchesc6c75982010-08-17 07:55:04 +0000538 pr_cont(" <No ACK64# during ABS64 cycle>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (pci_estat & GREG_PCIESTAT_DTRTO)
Joe Perchesc6c75982010-08-17 07:55:04 +0000540 pr_cont(" <Delayed transaction timeout>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (pci_estat & GREG_PCIESTAT_OTHER)
Joe Perchesc6c75982010-08-17 07:55:04 +0000542 pr_cont(" <other>");
543 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
545 pci_estat |= GREG_PCIESTAT_OTHER;
Joe Perchesc6c75982010-08-17 07:55:04 +0000546 netdev_err(dev, "PCI error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 if (pci_estat & GREG_PCIESTAT_OTHER) {
550 u16 pci_cfg_stat;
551
552 /* Interrogate PCI config space for the
553 * true cause.
554 */
555 pci_read_config_word(gp->pdev, PCI_STATUS,
556 &pci_cfg_stat);
Joe Perchesc6c75982010-08-17 07:55:04 +0000557 netdev_err(dev, "Read PCI cfg space status [%04x]\n",
558 pci_cfg_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (pci_cfg_stat & PCI_STATUS_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000560 netdev_err(dev, "PCI parity error detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (pci_cfg_stat & PCI_STATUS_SIG_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000562 netdev_err(dev, "PCI target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (pci_cfg_stat & PCI_STATUS_REC_TARGET_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000564 netdev_err(dev, "PCI master acks target abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (pci_cfg_stat & PCI_STATUS_REC_MASTER_ABORT)
Joe Perchesc6c75982010-08-17 07:55:04 +0000566 netdev_err(dev, "PCI master abort\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (pci_cfg_stat & PCI_STATUS_SIG_SYSTEM_ERROR)
Joe Perchesc6c75982010-08-17 07:55:04 +0000568 netdev_err(dev, "PCI system error SERR#\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (pci_cfg_stat & PCI_STATUS_DETECTED_PARITY)
Joe Perchesc6c75982010-08-17 07:55:04 +0000570 netdev_err(dev, "PCI parity error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* Write the error bits back to clear them. */
573 pci_cfg_stat &= (PCI_STATUS_PARITY |
574 PCI_STATUS_SIG_TARGET_ABORT |
575 PCI_STATUS_REC_TARGET_ABORT |
576 PCI_STATUS_REC_MASTER_ABORT |
577 PCI_STATUS_SIG_SYSTEM_ERROR |
578 PCI_STATUS_DETECTED_PARITY);
579 pci_write_config_word(gp->pdev,
580 PCI_STATUS, pci_cfg_stat);
581 }
582
583 /* For all PCI errors, we should reset the chip. */
584 return 1;
585}
586
587/* All non-normal interrupt conditions get serviced here.
588 * Returns non-zero if we should just exit the interrupt
589 * handler right now (ie. if we reset the card which invalidates
590 * all of the other original irq status bits).
591 */
592static int gem_abnormal_irq(struct net_device *dev, struct gem *gp, u32 gem_status)
593{
594 if (gem_status & GREG_STAT_RXNOBUF) {
595 /* Frame arrived, no free RX buffers available. */
596 if (netif_msg_rx_err(gp))
597 printk(KERN_DEBUG "%s: no buffer for rx frame\n",
598 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000599 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
602 if (gem_status & GREG_STAT_RXTAGERR) {
603 /* corrupt RX tag framing */
604 if (netif_msg_rx_err(gp))
605 printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
606 gp->dev->name);
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000607 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000609 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 if (gem_status & GREG_STAT_PCS) {
613 if (gem_pcs_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000614 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 if (gem_status & GREG_STAT_TXMAC) {
618 if (gem_txmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000619 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
622 if (gem_status & GREG_STAT_RXMAC) {
623 if (gem_rxmac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000624 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 if (gem_status & GREG_STAT_MAC) {
628 if (gem_mac_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000629 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 if (gem_status & GREG_STAT_MIF) {
633 if (gem_mif_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000634 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 if (gem_status & GREG_STAT_PCIERR) {
638 if (gem_pci_interrupt(dev, gp, gem_status))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000639 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645static __inline__ void gem_tx(struct net_device *dev, struct gem *gp, u32 gem_status)
646{
647 int entry, limit;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 entry = gp->tx_old;
650 limit = ((gem_status & GREG_STAT_TXNR) >> GREG_STAT_TXNR_SHIFT);
651 while (entry != limit) {
652 struct sk_buff *skb;
653 struct gem_txd *txd;
654 dma_addr_t dma_addr;
655 u32 dma_len;
656 int frag;
657
658 if (netif_msg_tx_done(gp))
659 printk(KERN_DEBUG "%s: tx done, slot %d\n",
660 gp->dev->name, entry);
661 skb = gp->tx_skbs[entry];
662 if (skb_shinfo(skb)->nr_frags) {
663 int last = entry + skb_shinfo(skb)->nr_frags;
664 int walk = entry;
665 int incomplete = 0;
666
667 last &= (TX_RING_SIZE - 1);
668 for (;;) {
669 walk = NEXT_TX(walk);
670 if (walk == limit)
671 incomplete = 1;
672 if (walk == last)
673 break;
674 }
675 if (incomplete)
676 break;
677 }
678 gp->tx_skbs[entry] = NULL;
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000679 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
682 txd = &gp->init_block->txd[entry];
683
684 dma_addr = le64_to_cpu(txd->buffer);
685 dma_len = le64_to_cpu(txd->control_word) & TXDCTRL_BUFSZ;
686
687 pci_unmap_page(gp->pdev, dma_addr, dma_len, PCI_DMA_TODEVICE);
688 entry = NEXT_TX(entry);
689 }
690
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000691 dev->stats.tx_packets++;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000692 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 gp->tx_old = entry;
695
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000696 /* Need to make the tx_old update visible to gem_start_xmit()
697 * before checking for netif_queue_stopped(). Without the
698 * memory barrier, there is a small possibility that gem_start_xmit()
699 * will miss it and cause the queue to be stopped forever.
700 */
701 smp_mb();
702
703 if (unlikely(netif_queue_stopped(dev) &&
704 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))) {
705 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
706
707 __netif_tx_lock(txq, smp_processor_id());
708 if (netif_queue_stopped(dev) &&
709 TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
710 netif_wake_queue(dev);
711 __netif_tx_unlock(txq);
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static __inline__ void gem_post_rxds(struct gem *gp, int limit)
716{
717 int cluster_start, curr, count, kick;
718
719 cluster_start = curr = (gp->rx_new & ~(4 - 1));
720 count = 0;
721 kick = -1;
722 wmb();
723 while (curr != limit) {
724 curr = NEXT_RX(curr);
725 if (++count == 4) {
726 struct gem_rxd *rxd =
727 &gp->init_block->rxd[cluster_start];
728 for (;;) {
729 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
730 rxd++;
731 cluster_start = NEXT_RX(cluster_start);
732 if (cluster_start == curr)
733 break;
734 }
735 kick = curr;
736 count = 0;
737 }
738 }
739 if (kick >= 0) {
740 mb();
741 writel(kick, gp->regs + RXDMA_KICK);
742 }
743}
744
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000745#define ALIGNED_RX_SKB_ADDR(addr) \
746 ((((unsigned long)(addr) + (64UL - 1UL)) & ~(64UL - 1UL)) - (unsigned long)(addr))
747static __inline__ struct sk_buff *gem_alloc_skb(struct net_device *dev, int size,
748 gfp_t gfp_flags)
749{
750 struct sk_buff *skb = alloc_skb(size + 64, gfp_flags);
751
752 if (likely(skb)) {
753 unsigned long offset = ALIGNED_RX_SKB_ADDR(skb->data);
754 skb_reserve(skb, offset);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000755 }
756 return skb;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static int gem_rx(struct gem *gp, int work_to_do)
760{
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000761 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int entry, drops, work_done = 0;
763 u32 done;
Al Viro439104b2007-12-17 06:48:04 +0000764 __sum16 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (netif_msg_rx_status(gp))
767 printk(KERN_DEBUG "%s: rx interrupt, done: %d, rx_new: %d\n",
768 gp->dev->name, readl(gp->regs + RXDMA_DONE), gp->rx_new);
769
770 entry = gp->rx_new;
771 drops = 0;
772 done = readl(gp->regs + RXDMA_DONE);
773 for (;;) {
774 struct gem_rxd *rxd = &gp->init_block->rxd[entry];
775 struct sk_buff *skb;
Al Viro439104b2007-12-17 06:48:04 +0000776 u64 status = le64_to_cpu(rxd->status_word);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dma_addr_t dma_addr;
778 int len;
779
780 if ((status & RXDCTRL_OWN) != 0)
781 break;
782
783 if (work_done >= RX_RING_SIZE || work_done >= work_to_do)
784 break;
785
786 /* When writing back RX descriptor, GEM writes status
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800787 * then buffer address, possibly in separate transactions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 * If we don't wait for the chip to write both, we could
789 * post a new buffer to this descriptor then have GEM spam
790 * on the buffer address. We sync on the RX completion
791 * register to prevent this from happening.
792 */
793 if (entry == done) {
794 done = readl(gp->regs + RXDMA_DONE);
795 if (entry == done)
796 break;
797 }
798
799 /* We can now account for the work we're about to do */
800 work_done++;
801
802 skb = gp->rx_skbs[entry];
803
804 len = (status & RXDCTRL_BUFSZ) >> 16;
805 if ((len < ETH_ZLEN) || (status & RXDCTRL_BAD)) {
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000806 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (len < ETH_ZLEN)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000808 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (len & RXDCTRL_BAD)
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000810 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* We'll just return it to GEM. */
813 drop_it:
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000814 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto next;
816 }
817
Al Viro439104b2007-12-17 06:48:04 +0000818 dma_addr = le64_to_cpu(rxd->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (len > RX_COPY_THRESHOLD) {
820 struct sk_buff *new_skb;
821
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000822 new_skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (new_skb == NULL) {
824 drops++;
825 goto drop_it;
826 }
827 pci_unmap_page(gp->pdev, dma_addr,
828 RX_BUF_ALLOC_SIZE(gp),
829 PCI_DMA_FROMDEVICE);
830 gp->rx_skbs[entry] = new_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 skb_put(new_skb, (gp->rx_buf_sz + RX_OFFSET));
832 rxd->buffer = cpu_to_le64(pci_map_page(gp->pdev,
833 virt_to_page(new_skb->data),
834 offset_in_page(new_skb->data),
835 RX_BUF_ALLOC_SIZE(gp),
836 PCI_DMA_FROMDEVICE));
837 skb_reserve(new_skb, RX_OFFSET);
838
839 /* Trim the original skb for the netif. */
840 skb_trim(skb, len);
841 } else {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000842 struct sk_buff *copy_skb = netdev_alloc_skb(dev, len + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (copy_skb == NULL) {
845 drops++;
846 goto drop_it;
847 }
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 skb_reserve(copy_skb, 2);
850 skb_put(copy_skb, len);
851 pci_dma_sync_single_for_cpu(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300852 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 pci_dma_sync_single_for_device(gp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
854
855 /* We'll reuse the original ring buffer. */
856 skb = copy_skb;
857 }
858
Al Viro439104b2007-12-17 06:48:04 +0000859 csum = (__force __sum16)htons((status & RXDCTRL_TCPCSUM) ^ 0xffff);
860 skb->csum = csum_unfold(csum);
Patrick McHardy84fa7932006-08-29 16:44:56 -0700861 skb->ip_summed = CHECKSUM_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 skb->protocol = eth_type_trans(skb, gp->dev);
863
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000864 napi_gro_receive(&gp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Denis Kirjanovaae7c472011-01-27 09:54:12 +0000866 dev->stats.rx_packets++;
867 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 next:
870 entry = NEXT_RX(entry);
871 }
872
873 gem_post_rxds(gp, entry);
874
875 gp->rx_new = entry;
876
877 if (drops)
Joe Perchesc6c75982010-08-17 07:55:04 +0000878 netdev_info(gp->dev, "Memory squeeze, deferring packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 return work_done;
881}
882
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700883static int gem_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885 struct gem *gp = container_of(napi, struct gem, napi);
886 struct net_device *dev = gp->dev;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700887 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700889 work_done = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /* Handle anomalies */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000892 if (unlikely(gp->status & GREG_STAT_ABNORMAL)) {
893 struct netdev_queue *txq = netdev_get_tx_queue(dev, 0);
894 int reset;
895
896 /* We run the abnormal interrupt handling code with
897 * the Tx lock. It only resets the Rx portion of the
898 * chip, but we need to guard it against DMA being
899 * restarted by the link poll timer
900 */
901 __netif_tx_lock(txq, smp_processor_id());
902 reset = gem_abnormal_irq(dev, gp, gp->status);
903 __netif_tx_unlock(txq);
904 if (reset) {
905 gem_schedule_reset(gp);
906 napi_complete(napi);
907 return work_done;
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
911 /* Run TX completion thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 gem_tx(dev, gp, gp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400914 /* Run RX thread. We don't use any locking here,
915 * code willing to do bad things - like cleaning the
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700916 * rx ring - must call napi_disable(), which
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * schedule_timeout()'s if polling is already disabled.
918 */
David S. Millerda990a22008-03-23 03:35:12 -0700919 work_done += gem_rx(gp, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700921 if (work_done >= budget)
922 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 gp->status = readl(gp->regs + GREG_STAT);
925 } while (gp->status & GREG_STAT_NAPI);
926
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000927 napi_complete(napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 gem_enable_ints(gp);
929
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700930 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
David Howells7d12e782006-10-05 14:55:46 +0100933static irqreturn_t gem_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 struct net_device *dev = dev_id;
Wang Chen8f15ea42008-11-12 23:38:36 -0800936 struct gem *gp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400937
Ben Hutchings288379f2009-01-19 16:43:59 -0800938 if (napi_schedule_prep(&gp->napi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 u32 gem_status = readl(gp->regs + GREG_STAT);
940
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000941 if (unlikely(gem_status == 0)) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700942 napi_enable(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return IRQ_NONE;
944 }
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000945 if (netif_msg_intr(gp))
946 printk(KERN_DEBUG "%s: gem_interrupt() gem_status: 0x%x\n",
947 gp->dev->name, gem_status);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 gp->status = gem_status;
950 gem_disable_ints(gp);
Ben Hutchings288379f2009-01-19 16:43:59 -0800951 __napi_schedule(&gp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* If polling was disabled at the time we received that
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400955 * interrupt, we may return IRQ_HANDLED here while we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * should return IRQ_NONE. No big deal...
957 */
958 return IRQ_HANDLED;
959}
960
961#ifdef CONFIG_NET_POLL_CONTROLLER
962static void gem_poll_controller(struct net_device *dev)
963{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000964 struct gem *gp = netdev_priv(dev);
965
966 disable_irq(gp->pdev->irq);
967 gem_interrupt(gp->pdev->irq, dev);
968 enable_irq(gp->pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970#endif
971
972static void gem_tx_timeout(struct net_device *dev)
973{
Wang Chen8f15ea42008-11-12 23:38:36 -0800974 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Joe Perchesc6c75982010-08-17 07:55:04 +0000976 netdev_err(dev, "transmit timed out, resetting\n");
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000977
Joe Perchesc6c75982010-08-17 07:55:04 +0000978 netdev_err(dev, "TX_STATE[%08x:%08x:%08x]\n",
979 readl(gp->regs + TXDMA_CFG),
980 readl(gp->regs + MAC_TXSTAT),
981 readl(gp->regs + MAC_TXCFG));
982 netdev_err(dev, "RX_STATE[%08x:%08x:%08x]\n",
983 readl(gp->regs + RXDMA_CFG),
984 readl(gp->regs + MAC_RXSTAT),
985 readl(gp->regs + MAC_RXCFG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +1000987 gem_schedule_reset(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990static __inline__ int gem_intme(int entry)
991{
992 /* Algorithm: IRQ every 1/2 of descriptors. */
993 if (!(entry & ((TX_RING_SIZE>>1)-1)))
994 return 1;
995
996 return 0;
997}
998
Stephen Hemminger613573252009-08-31 19:50:58 +0000999static netdev_tx_t gem_start_xmit(struct sk_buff *skb,
1000 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Wang Chen8f15ea42008-11-12 23:38:36 -08001002 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 int entry;
1004 u64 ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 ctrl = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07001007 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michał Mirosław0d0b1672010-12-14 15:24:08 +00001008 const u64 csum_start_off = skb_checksum_start_offset(skb);
Arnaldo Carvalho de Meloea2ae172007-04-25 17:55:53 -07001009 const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 ctrl = (TXDCTRL_CENAB |
1012 (csum_start_off << 15) |
1013 (csum_stuff_off << 21));
1014 }
1015
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001016 if (unlikely(TX_BUFFS_AVAIL(gp) <= (skb_shinfo(skb)->nr_frags + 1))) {
1017 /* This is a hard error, log it. */
1018 if (!netif_queue_stopped(dev)) {
1019 netif_stop_queue(dev);
1020 netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
1021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return NETDEV_TX_BUSY;
1023 }
1024
1025 entry = gp->tx_new;
1026 gp->tx_skbs[entry] = skb;
1027
1028 if (skb_shinfo(skb)->nr_frags == 0) {
1029 struct gem_txd *txd = &gp->init_block->txd[entry];
1030 dma_addr_t mapping;
1031 u32 len;
1032
1033 len = skb->len;
1034 mapping = pci_map_page(gp->pdev,
1035 virt_to_page(skb->data),
1036 offset_in_page(skb->data),
1037 len, PCI_DMA_TODEVICE);
1038 ctrl |= TXDCTRL_SOF | TXDCTRL_EOF | len;
1039 if (gem_intme(entry))
1040 ctrl |= TXDCTRL_INTME;
1041 txd->buffer = cpu_to_le64(mapping);
1042 wmb();
1043 txd->control_word = cpu_to_le64(ctrl);
1044 entry = NEXT_TX(entry);
1045 } else {
1046 struct gem_txd *txd;
1047 u32 first_len;
1048 u64 intme;
1049 dma_addr_t first_mapping;
1050 int frag, first_entry = entry;
1051
1052 intme = 0;
1053 if (gem_intme(entry))
1054 intme |= TXDCTRL_INTME;
1055
1056 /* We must give this initial chunk to the device last.
1057 * Otherwise we could race with the device.
1058 */
1059 first_len = skb_headlen(skb);
1060 first_mapping = pci_map_page(gp->pdev, virt_to_page(skb->data),
1061 offset_in_page(skb->data),
1062 first_len, PCI_DMA_TODEVICE);
1063 entry = NEXT_TX(entry);
1064
1065 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001066 const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 u32 len;
1068 dma_addr_t mapping;
1069 u64 this_ctrl;
1070
Eric Dumazet9e903e02011-10-18 21:00:24 +00001071 len = skb_frag_size(this_frag);
Ian Campbell4fee78b2011-09-21 21:53:21 +00001072 mapping = skb_frag_dma_map(&gp->pdev->dev, this_frag,
Ian Campbell5d6bcdf2011-10-06 11:10:48 +01001073 0, len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 this_ctrl = ctrl;
1075 if (frag == skb_shinfo(skb)->nr_frags - 1)
1076 this_ctrl |= TXDCTRL_EOF;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 txd = &gp->init_block->txd[entry];
1079 txd->buffer = cpu_to_le64(mapping);
1080 wmb();
1081 txd->control_word = cpu_to_le64(this_ctrl | len);
1082
1083 if (gem_intme(entry))
1084 intme |= TXDCTRL_INTME;
1085
1086 entry = NEXT_TX(entry);
1087 }
1088 txd = &gp->init_block->txd[first_entry];
1089 txd->buffer = cpu_to_le64(first_mapping);
1090 wmb();
1091 txd->control_word =
1092 cpu_to_le64(ctrl | TXDCTRL_SOF | intme | first_len);
1093 }
1094
1095 gp->tx_new = entry;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001096 if (unlikely(TX_BUFFS_AVAIL(gp) <= (MAX_SKB_FRAGS + 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 netif_stop_queue(dev);
1098
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001099 /* netif_stop_queue() must be done before checking
1100 * checking tx index in TX_BUFFS_AVAIL() below, because
1101 * in gem_tx(), we update tx_old before checking for
1102 * netif_queue_stopped().
1103 */
1104 smp_mb();
1105 if (TX_BUFFS_AVAIL(gp) > (MAX_SKB_FRAGS + 1))
1106 netif_wake_queue(dev);
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (netif_msg_tx_queued(gp))
1109 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
1110 dev->name, entry, skb->len);
1111 mb();
1112 writel(gp->tx_new, gp->regs + TXDMA_KICK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return NETDEV_TX_OK;
1115}
1116
David S. Miller8c83f802008-12-09 00:10:11 -08001117static void gem_pcs_reset(struct gem *gp)
1118{
1119 int limit;
1120 u32 val;
1121
1122 /* Reset PCS unit. */
1123 val = readl(gp->regs + PCS_MIICTRL);
1124 val |= PCS_MIICTRL_RST;
1125 writel(val, gp->regs + PCS_MIICTRL);
1126
1127 limit = 32;
1128 while (readl(gp->regs + PCS_MIICTRL) & PCS_MIICTRL_RST) {
1129 udelay(100);
1130 if (limit-- <= 0)
1131 break;
1132 }
roel kluind13c11f2009-02-16 04:02:04 +00001133 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001134 netdev_warn(gp->dev, "PCS reset bit would not clear\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001135}
1136
1137static void gem_pcs_reinit_adv(struct gem *gp)
1138{
1139 u32 val;
1140
1141 /* Make sure PCS is disabled while changing advertisement
1142 * configuration.
1143 */
1144 val = readl(gp->regs + PCS_CFG);
1145 val &= ~(PCS_CFG_ENABLE | PCS_CFG_TO);
1146 writel(val, gp->regs + PCS_CFG);
1147
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001148 /* Advertise all capabilities except asymmetric
David S. Miller8c83f802008-12-09 00:10:11 -08001149 * pause.
1150 */
1151 val = readl(gp->regs + PCS_MIIADV);
1152 val |= (PCS_MIIADV_FD | PCS_MIIADV_HD |
1153 PCS_MIIADV_SP | PCS_MIIADV_AP);
1154 writel(val, gp->regs + PCS_MIIADV);
1155
1156 /* Enable and restart auto-negotiation, disable wrapback/loopback,
1157 * and re-enable PCS.
1158 */
1159 val = readl(gp->regs + PCS_MIICTRL);
1160 val |= (PCS_MIICTRL_RAN | PCS_MIICTRL_ANE);
1161 val &= ~PCS_MIICTRL_WB;
1162 writel(val, gp->regs + PCS_MIICTRL);
1163
1164 val = readl(gp->regs + PCS_CFG);
1165 val |= PCS_CFG_ENABLE;
1166 writel(val, gp->regs + PCS_CFG);
1167
1168 /* Make sure serialink loopback is off. The meaning
1169 * of this bit is logically inverted based upon whether
1170 * you are in Serialink or SERDES mode.
1171 */
1172 val = readl(gp->regs + PCS_SCTRL);
1173 if (gp->phy_type == phy_serialink)
1174 val &= ~PCS_SCTRL_LOOP;
1175 else
1176 val |= PCS_SCTRL_LOOP;
1177 writel(val, gp->regs + PCS_SCTRL);
1178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180#define STOP_TRIES 32
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182static void gem_reset(struct gem *gp)
1183{
1184 int limit;
1185 u32 val;
1186
1187 /* Make sure we won't get any more interrupts */
1188 writel(0xffffffff, gp->regs + GREG_IMASK);
1189
1190 /* Reset the chip */
1191 writel(gp->swrst_base | GREG_SWRST_TXRST | GREG_SWRST_RXRST,
1192 gp->regs + GREG_SWRST);
1193
1194 limit = STOP_TRIES;
1195
1196 do {
1197 udelay(20);
1198 val = readl(gp->regs + GREG_SWRST);
1199 if (limit-- <= 0)
1200 break;
1201 } while (val & (GREG_SWRST_TXRST | GREG_SWRST_RXRST));
1202
Roel Kluin4a8fd2c2009-03-04 00:08:39 -08001203 if (limit < 0)
Joe Perchesc6c75982010-08-17 07:55:04 +00001204 netdev_err(gp->dev, "SW reset is ghetto\n");
David S. Miller8c83f802008-12-09 00:10:11 -08001205
1206 if (gp->phy_type == phy_serialink || gp->phy_type == phy_serdes)
1207 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static void gem_start_dma(struct gem *gp)
1211{
1212 u32 val;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /* We are ready to rock, turn everything on. */
1215 val = readl(gp->regs + TXDMA_CFG);
1216 writel(val | TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1217 val = readl(gp->regs + RXDMA_CFG);
1218 writel(val | RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1219 val = readl(gp->regs + MAC_TXCFG);
1220 writel(val | MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1221 val = readl(gp->regs + MAC_RXCFG);
1222 writel(val | MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1223
1224 (void) readl(gp->regs + MAC_RXCFG);
1225 udelay(100);
1226
1227 gem_enable_ints(gp);
1228
1229 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1230}
1231
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001232/* DMA won't be actually stopped before about 4ms tho ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
1234static void gem_stop_dma(struct gem *gp)
1235{
1236 u32 val;
1237
1238 /* We are done rocking, turn everything off. */
1239 val = readl(gp->regs + TXDMA_CFG);
1240 writel(val & ~TXDMA_CFG_ENABLE, gp->regs + TXDMA_CFG);
1241 val = readl(gp->regs + RXDMA_CFG);
1242 writel(val & ~RXDMA_CFG_ENABLE, gp->regs + RXDMA_CFG);
1243 val = readl(gp->regs + MAC_TXCFG);
1244 writel(val & ~MAC_TXCFG_ENAB, gp->regs + MAC_TXCFG);
1245 val = readl(gp->regs + MAC_RXCFG);
1246 writel(val & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
1247
1248 (void) readl(gp->regs + MAC_RXCFG);
1249
1250 /* Need to wait a bit ... done by the caller */
1251}
1252
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254// XXX dbl check what that function should do when called on PCS PHY
1255static void gem_begin_auto_negotiation(struct gem *gp, struct ethtool_cmd *ep)
1256{
1257 u32 advertise, features;
1258 int autoneg;
1259 int speed;
1260 int duplex;
1261
1262 if (gp->phy_type != phy_mii_mdio0 &&
1263 gp->phy_type != phy_mii_mdio1)
1264 goto non_mii;
1265
1266 /* Setup advertise */
1267 if (found_mii_phy(gp))
1268 features = gp->phy_mii.def->features;
1269 else
1270 features = 0;
1271
1272 advertise = features & ADVERTISE_MASK;
1273 if (gp->phy_mii.advertising != 0)
1274 advertise &= gp->phy_mii.advertising;
1275
1276 autoneg = gp->want_autoneg;
1277 speed = gp->phy_mii.speed;
1278 duplex = gp->phy_mii.duplex;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 /* Setup link parameters */
1281 if (!ep)
1282 goto start_aneg;
1283 if (ep->autoneg == AUTONEG_ENABLE) {
1284 advertise = ep->advertising;
1285 autoneg = 1;
1286 } else {
1287 autoneg = 0;
David Decotigny25db0332011-04-27 18:32:39 +00001288 speed = ethtool_cmd_speed(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 duplex = ep->duplex;
1290 }
1291
1292start_aneg:
1293 /* Sanitize settings based on PHY capabilities */
1294 if ((features & SUPPORTED_Autoneg) == 0)
1295 autoneg = 0;
1296 if (speed == SPEED_1000 &&
1297 !(features & (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)))
1298 speed = SPEED_100;
1299 if (speed == SPEED_100 &&
1300 !(features & (SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full)))
1301 speed = SPEED_10;
1302 if (duplex == DUPLEX_FULL &&
1303 !(features & (SUPPORTED_1000baseT_Full |
1304 SUPPORTED_100baseT_Full |
1305 SUPPORTED_10baseT_Full)))
1306 duplex = DUPLEX_HALF;
1307 if (speed == 0)
1308 speed = SPEED_10;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /* If we are asleep, we don't try to actually setup the PHY, we
1311 * just store the settings
1312 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001313 if (!netif_device_present(gp->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 gp->phy_mii.autoneg = gp->want_autoneg = autoneg;
1315 gp->phy_mii.speed = speed;
1316 gp->phy_mii.duplex = duplex;
1317 return;
1318 }
1319
1320 /* Configure PHY & start aneg */
1321 gp->want_autoneg = autoneg;
1322 if (autoneg) {
1323 if (found_mii_phy(gp))
1324 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, advertise);
1325 gp->lstate = link_aneg;
1326 } else {
1327 if (found_mii_phy(gp))
1328 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, speed, duplex);
1329 gp->lstate = link_force_ok;
1330 }
1331
1332non_mii:
1333 gp->timer_ticks = 0;
1334 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
1335}
1336
1337/* A link-up condition has occurred, initialize and enable the
1338 * rest of the chip.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
1340static int gem_set_link_modes(struct gem *gp)
1341{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001342 struct netdev_queue *txq = netdev_get_tx_queue(gp->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int full_duplex, speed, pause;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001344 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 full_duplex = 0;
1347 speed = SPEED_10;
1348 pause = 0;
1349
1350 if (found_mii_phy(gp)) {
1351 if (gp->phy_mii.def->ops->read_link(&gp->phy_mii))
1352 return 1;
1353 full_duplex = (gp->phy_mii.duplex == DUPLEX_FULL);
1354 speed = gp->phy_mii.speed;
1355 pause = gp->phy_mii.pause;
1356 } else if (gp->phy_type == phy_serialink ||
1357 gp->phy_type == phy_serdes) {
1358 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1359
David S. Miller8c83f802008-12-09 00:10:11 -08001360 if ((pcs_lpa & PCS_MIIADV_FD) || gp->phy_type == phy_serdes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 full_duplex = 1;
1362 speed = SPEED_1000;
1363 }
1364
Joe Perchesc6c75982010-08-17 07:55:04 +00001365 netif_info(gp, link, gp->dev, "Link is up at %d Mbps, %s-duplex\n",
1366 speed, (full_duplex ? "full" : "half"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001368
1369 /* We take the tx queue lock to avoid collisions between
1370 * this code, the tx path and the NAPI-driven error path
1371 */
1372 __netif_tx_lock(txq, smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 val = (MAC_TXCFG_EIPG0 | MAC_TXCFG_NGU);
1375 if (full_duplex) {
1376 val |= (MAC_TXCFG_ICS | MAC_TXCFG_ICOLL);
1377 } else {
1378 /* MAC_TXCFG_NBO must be zero. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 writel(val, gp->regs + MAC_TXCFG);
1381
1382 val = (MAC_XIFCFG_OE | MAC_XIFCFG_LLED);
1383 if (!full_duplex &&
1384 (gp->phy_type == phy_mii_mdio0 ||
1385 gp->phy_type == phy_mii_mdio1)) {
1386 val |= MAC_XIFCFG_DISE;
1387 } else if (full_duplex) {
1388 val |= MAC_XIFCFG_FLED;
1389 }
1390
1391 if (speed == SPEED_1000)
1392 val |= (MAC_XIFCFG_GMII);
1393
1394 writel(val, gp->regs + MAC_XIFCFG);
1395
1396 /* If gigabit and half-duplex, enable carrier extension
1397 * mode. Else, disable it.
1398 */
1399 if (speed == SPEED_1000 && !full_duplex) {
1400 val = readl(gp->regs + MAC_TXCFG);
1401 writel(val | MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1402
1403 val = readl(gp->regs + MAC_RXCFG);
1404 writel(val | MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1405 } else {
1406 val = readl(gp->regs + MAC_TXCFG);
1407 writel(val & ~MAC_TXCFG_TCE, gp->regs + MAC_TXCFG);
1408
1409 val = readl(gp->regs + MAC_RXCFG);
1410 writel(val & ~MAC_RXCFG_RCE, gp->regs + MAC_RXCFG);
1411 }
1412
1413 if (gp->phy_type == phy_serialink ||
1414 gp->phy_type == phy_serdes) {
1415 u32 pcs_lpa = readl(gp->regs + PCS_MIILP);
1416
1417 if (pcs_lpa & (PCS_MIIADV_SP | PCS_MIIADV_AP))
1418 pause = 1;
1419 }
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!full_duplex)
1422 writel(512, gp->regs + MAC_STIME);
1423 else
1424 writel(64, gp->regs + MAC_STIME);
1425 val = readl(gp->regs + MAC_MCCFG);
1426 if (pause)
1427 val |= (MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1428 else
1429 val &= ~(MAC_MCCFG_SPE | MAC_MCCFG_RPE);
1430 writel(val, gp->regs + MAC_MCCFG);
1431
1432 gem_start_dma(gp);
1433
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001434 __netif_tx_unlock(txq);
1435
1436 if (netif_msg_link(gp)) {
1437 if (pause) {
1438 netdev_info(gp->dev,
1439 "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
1440 gp->rx_fifo_sz,
1441 gp->rx_pause_off,
1442 gp->rx_pause_on);
1443 } else {
1444 netdev_info(gp->dev, "Pause is disabled\n");
1445 }
1446 }
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return 0;
1449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451static int gem_mdio_link_not_up(struct gem *gp)
1452{
1453 switch (gp->lstate) {
1454 case link_force_ret:
Joe Perchesc6c75982010-08-17 07:55:04 +00001455 netif_info(gp, link, gp->dev,
1456 "Autoneg failed again, keeping forced mode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii,
1458 gp->last_forced_speed, DUPLEX_HALF);
1459 gp->timer_ticks = 5;
1460 gp->lstate = link_force_ok;
1461 return 0;
1462 case link_aneg:
1463 /* We try forced modes after a failed aneg only on PHYs that don't
1464 * have "magic_aneg" bit set, which means they internally do the
1465 * while forced-mode thingy. On these, we just restart aneg
1466 */
1467 if (gp->phy_mii.def->magic_aneg)
1468 return 1;
Joe Perchesc6c75982010-08-17 07:55:04 +00001469 netif_info(gp, link, gp->dev, "switching to forced 100bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Try forced modes. */
1471 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_100,
1472 DUPLEX_HALF);
1473 gp->timer_ticks = 5;
1474 gp->lstate = link_force_try;
1475 return 0;
1476 case link_force_try:
1477 /* Downgrade from 100 to 10 Mbps if necessary.
1478 * If already at 10Mbps, warn user about the
1479 * situation every 10 ticks.
1480 */
1481 if (gp->phy_mii.speed == SPEED_100) {
1482 gp->phy_mii.def->ops->setup_forced(&gp->phy_mii, SPEED_10,
1483 DUPLEX_HALF);
1484 gp->timer_ticks = 5;
Joe Perchesc6c75982010-08-17 07:55:04 +00001485 netif_info(gp, link, gp->dev,
1486 "switching to forced 10bt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488 } else
1489 return 1;
1490 default:
1491 return 0;
1492 }
1493}
1494
1495static void gem_link_timer(unsigned long data)
1496{
1497 struct gem *gp = (struct gem *) data;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001498 struct net_device *dev = gp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 int restart_aneg = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001500
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001501 /* There's no point doing anything if we're going to be reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (gp->reset_task_pending)
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001503 return;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (gp->phy_type == phy_serialink ||
1506 gp->phy_type == phy_serdes) {
1507 u32 val = readl(gp->regs + PCS_MIISTAT);
1508
1509 if (!(val & PCS_MIISTAT_LS))
1510 val = readl(gp->regs + PCS_MIISTAT);
1511
1512 if ((val & PCS_MIISTAT_LS) != 0) {
David S. Miller8c83f802008-12-09 00:10:11 -08001513 if (gp->lstate == link_up)
1514 goto restart;
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001517 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 (void)gem_set_link_modes(gp);
1519 }
1520 goto restart;
1521 }
1522 if (found_mii_phy(gp) && gp->phy_mii.def->ops->poll_link(&gp->phy_mii)) {
1523 /* Ok, here we got a link. If we had it due to a forced
1524 * fallback, and we were configured for autoneg, we do
1525 * retry a short autoneg pass. If you know your hub is
1526 * broken, use ethtool ;)
1527 */
1528 if (gp->lstate == link_force_try && gp->want_autoneg) {
1529 gp->lstate = link_force_ret;
1530 gp->last_forced_speed = gp->phy_mii.speed;
1531 gp->timer_ticks = 5;
1532 if (netif_msg_link(gp))
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001533 netdev_info(dev,
Joe Perchesc6c75982010-08-17 07:55:04 +00001534 "Got link after fallback, retrying autoneg once...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 gp->phy_mii.def->ops->setup_aneg(&gp->phy_mii, gp->phy_mii.advertising);
1536 } else if (gp->lstate != link_up) {
1537 gp->lstate = link_up;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001538 netif_carrier_on(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (gem_set_link_modes(gp))
1540 restart_aneg = 1;
1541 }
1542 } else {
1543 /* If the link was previously up, we restart the
1544 * whole process
1545 */
1546 if (gp->lstate == link_up) {
1547 gp->lstate = link_down;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001548 netif_info(gp, link, dev, "Link down\n");
1549 netif_carrier_off(dev);
1550 gem_schedule_reset(gp);
1551 /* The reset task will restart the timer */
1552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 } else if (++gp->timer_ticks > 10) {
1554 if (found_mii_phy(gp))
1555 restart_aneg = gem_mdio_link_not_up(gp);
1556 else
1557 restart_aneg = 1;
1558 }
1559 }
1560 if (restart_aneg) {
1561 gem_begin_auto_negotiation(gp, NULL);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564restart:
1565 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static void gem_clean_rings(struct gem *gp)
1569{
1570 struct gem_init_block *gb = gp->init_block;
1571 struct sk_buff *skb;
1572 int i;
1573 dma_addr_t dma_addr;
1574
1575 for (i = 0; i < RX_RING_SIZE; i++) {
1576 struct gem_rxd *rxd;
1577
1578 rxd = &gb->rxd[i];
1579 if (gp->rx_skbs[i] != NULL) {
1580 skb = gp->rx_skbs[i];
1581 dma_addr = le64_to_cpu(rxd->buffer);
1582 pci_unmap_page(gp->pdev, dma_addr,
1583 RX_BUF_ALLOC_SIZE(gp),
1584 PCI_DMA_FROMDEVICE);
1585 dev_kfree_skb_any(skb);
1586 gp->rx_skbs[i] = NULL;
1587 }
1588 rxd->status_word = 0;
1589 wmb();
1590 rxd->buffer = 0;
1591 }
1592
1593 for (i = 0; i < TX_RING_SIZE; i++) {
1594 if (gp->tx_skbs[i] != NULL) {
1595 struct gem_txd *txd;
1596 int frag;
1597
1598 skb = gp->tx_skbs[i];
1599 gp->tx_skbs[i] = NULL;
1600
1601 for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
1602 int ent = i & (TX_RING_SIZE - 1);
1603
1604 txd = &gb->txd[ent];
1605 dma_addr = le64_to_cpu(txd->buffer);
1606 pci_unmap_page(gp->pdev, dma_addr,
1607 le64_to_cpu(txd->control_word) &
1608 TXDCTRL_BUFSZ, PCI_DMA_TODEVICE);
1609
1610 if (frag != skb_shinfo(skb)->nr_frags)
1611 i++;
1612 }
1613 dev_kfree_skb_any(skb);
1614 }
1615 }
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618static void gem_init_rings(struct gem *gp)
1619{
1620 struct gem_init_block *gb = gp->init_block;
1621 struct net_device *dev = gp->dev;
1622 int i;
1623 dma_addr_t dma_addr;
1624
1625 gp->rx_new = gp->rx_old = gp->tx_new = gp->tx_old = 0;
1626
1627 gem_clean_rings(gp);
1628
1629 gp->rx_buf_sz = max(dev->mtu + ETH_HLEN + VLAN_HLEN,
1630 (unsigned)VLAN_ETH_FRAME_LEN);
1631
1632 for (i = 0; i < RX_RING_SIZE; i++) {
1633 struct sk_buff *skb;
1634 struct gem_rxd *rxd = &gb->rxd[i];
1635
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001636 skb = gem_alloc_skb(dev, RX_BUF_ALLOC_SIZE(gp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!skb) {
1638 rxd->buffer = 0;
1639 rxd->status_word = 0;
1640 continue;
1641 }
1642
1643 gp->rx_skbs[i] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 skb_put(skb, (gp->rx_buf_sz + RX_OFFSET));
1645 dma_addr = pci_map_page(gp->pdev,
1646 virt_to_page(skb->data),
1647 offset_in_page(skb->data),
1648 RX_BUF_ALLOC_SIZE(gp),
1649 PCI_DMA_FROMDEVICE);
1650 rxd->buffer = cpu_to_le64(dma_addr);
1651 wmb();
1652 rxd->status_word = cpu_to_le64(RXDCTRL_FRESH(gp));
1653 skb_reserve(skb, RX_OFFSET);
1654 }
1655
1656 for (i = 0; i < TX_RING_SIZE; i++) {
1657 struct gem_txd *txd = &gb->txd[i];
1658
1659 txd->control_word = 0;
1660 wmb();
1661 txd->buffer = 0;
1662 }
1663 wmb();
1664}
1665
1666/* Init PHY interface and start link poll state machine */
1667static void gem_init_phy(struct gem *gp)
1668{
David S. Miller7fb76aa2006-01-31 17:09:20 -08001669 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 /* Revert MIF CFG setting done on stop_phy */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001672 mifcfg = readl(gp->regs + MIF_CFG);
1673 mifcfg &= ~MIF_CFG_BBMODE;
1674 writel(mifcfg, gp->regs + MIF_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE) {
1677 int i;
1678
David S. Miller7fb76aa2006-01-31 17:09:20 -08001679 /* Those delay sucks, the HW seem to love them though, I'll
1680 * serisouly consider breaking some locks here to be able
1681 * to schedule instead
Benjamin Herrenschmidt40727192006-01-23 16:30:04 -08001682 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08001683 for (i = 0; i < 3; i++) {
1684#ifdef CONFIG_PPC_PMAC
1685 pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET, gp->of_node, 0, 0);
1686 msleep(20);
1687#endif
1688 /* Some PHYs used by apple have problem getting back to us,
1689 * we do an additional reset here
1690 */
1691 phy_write(gp, MII_BMCR, BMCR_RESET);
1692 msleep(20);
1693 if (phy_read(gp, MII_BMCR) != 0xffff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
David S. Miller7fb76aa2006-01-31 17:09:20 -08001695 if (i == 2)
Joe Perchesc6c75982010-08-17 07:55:04 +00001696 netdev_warn(gp->dev, "GMAC PHY not responding !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698 }
1699
1700 if (gp->pdev->vendor == PCI_VENDOR_ID_SUN &&
1701 gp->pdev->device == PCI_DEVICE_ID_SUN_GEM) {
1702 u32 val;
1703
1704 /* Init datapath mode register. */
1705 if (gp->phy_type == phy_mii_mdio0 ||
1706 gp->phy_type == phy_mii_mdio1) {
1707 val = PCS_DMODE_MGM;
1708 } else if (gp->phy_type == phy_serialink) {
1709 val = PCS_DMODE_SM | PCS_DMODE_GMOE;
1710 } else {
1711 val = PCS_DMODE_ESM;
1712 }
1713
1714 writel(val, gp->regs + PCS_DMODE);
1715 }
1716
1717 if (gp->phy_type == phy_mii_mdio0 ||
1718 gp->phy_type == phy_mii_mdio1) {
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001719 /* Reset and detect MII PHY */
David S. Miller19e2f6f2011-08-15 23:10:39 -07001720 sungem_phy_probe(&gp->phy_mii, gp->mii_phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /* Init PHY */
1723 if (gp->phy_mii.def && gp->phy_mii.def->ops->init)
1724 gp->phy_mii.def->ops->init(&gp->phy_mii);
1725 } else {
David S. Miller8c83f802008-12-09 00:10:11 -08001726 gem_pcs_reset(gp);
1727 gem_pcs_reinit_adv(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 /* Default aneg parameters */
1731 gp->timer_ticks = 0;
1732 gp->lstate = link_down;
1733 netif_carrier_off(gp->dev);
1734
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10001735 /* Print things out */
1736 if (gp->phy_type == phy_mii_mdio0 ||
1737 gp->phy_type == phy_mii_mdio1)
1738 netdev_info(gp->dev, "Found %s PHY\n",
1739 gp->phy_mii.def ? gp->phy_mii.def->name : "no");
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 gem_begin_auto_negotiation(gp, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static void gem_init_dma(struct gem *gp)
1745{
1746 u64 desc_dma = (u64) gp->gblock_dvma;
1747 u32 val;
1748
1749 val = (TXDMA_CFG_BASE | (0x7ff << 10) | TXDMA_CFG_PMODE);
1750 writel(val, gp->regs + TXDMA_CFG);
1751
1752 writel(desc_dma >> 32, gp->regs + TXDMA_DBHI);
1753 writel(desc_dma & 0xffffffff, gp->regs + TXDMA_DBLOW);
1754 desc_dma += (INIT_BLOCK_TX_RING_SIZE * sizeof(struct gem_txd));
1755
1756 writel(0, gp->regs + TXDMA_KICK);
1757
1758 val = (RXDMA_CFG_BASE | (RX_OFFSET << 10) |
1759 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128);
1760 writel(val, gp->regs + RXDMA_CFG);
1761
1762 writel(desc_dma >> 32, gp->regs + RXDMA_DBHI);
1763 writel(desc_dma & 0xffffffff, gp->regs + RXDMA_DBLOW);
1764
1765 writel(RX_RING_SIZE - 4, gp->regs + RXDMA_KICK);
1766
1767 val = (((gp->rx_pause_off / 64) << 0) & RXDMA_PTHRESH_OFF);
1768 val |= (((gp->rx_pause_on / 64) << 12) & RXDMA_PTHRESH_ON);
1769 writel(val, gp->regs + RXDMA_PTHRESH);
1770
1771 if (readl(gp->regs + GREG_BIFCFG) & GREG_BIFCFG_M66EN)
1772 writel(((5 & RXDMA_BLANK_IPKTS) |
1773 ((8 << 12) & RXDMA_BLANK_ITIME)),
1774 gp->regs + RXDMA_BLANK);
1775 else
1776 writel(((5 & RXDMA_BLANK_IPKTS) |
1777 ((4 << 12) & RXDMA_BLANK_ITIME)),
1778 gp->regs + RXDMA_BLANK);
1779}
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781static u32 gem_setup_multicast(struct gem *gp)
1782{
1783 u32 rxcfg = 0;
1784 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if ((gp->dev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001787 (netdev_mc_count(gp->dev) > 256)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 for (i=0; i<16; i++)
1789 writel(0xffff, gp->regs + MAC_HASH0 + (i << 2));
1790 rxcfg |= MAC_RXCFG_HFE;
1791 } else if (gp->dev->flags & IFF_PROMISC) {
1792 rxcfg |= MAC_RXCFG_PROM;
1793 } else {
1794 u16 hash_table[16];
1795 u32 crc;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001796 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 int i;
1798
Jiri Pirko55085902010-02-18 00:42:54 +00001799 memset(hash_table, 0, sizeof(hash_table));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001800 netdev_for_each_mc_addr(ha, gp->dev) {
Tobias Klauser498d8e22011-07-07 22:06:26 +00001801 crc = ether_crc_le(6, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 crc >>= 24;
1803 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
1804 }
1805 for (i=0; i<16; i++)
1806 writel(hash_table[i], gp->regs + MAC_HASH0 + (i << 2));
1807 rxcfg |= MAC_RXCFG_HFE;
1808 }
1809
1810 return rxcfg;
1811}
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813static void gem_init_mac(struct gem *gp)
1814{
1815 unsigned char *e = &gp->dev->dev_addr[0];
1816
1817 writel(0x1bf0, gp->regs + MAC_SNDPAUSE);
1818
1819 writel(0x00, gp->regs + MAC_IPG0);
1820 writel(0x08, gp->regs + MAC_IPG1);
1821 writel(0x04, gp->regs + MAC_IPG2);
1822 writel(0x40, gp->regs + MAC_STIME);
1823 writel(0x40, gp->regs + MAC_MINFSZ);
1824
1825 /* Ethernet payload + header + FCS + optional VLAN tag. */
1826 writel(0x20000000 | (gp->rx_buf_sz + 4), gp->regs + MAC_MAXFSZ);
1827
1828 writel(0x07, gp->regs + MAC_PASIZE);
1829 writel(0x04, gp->regs + MAC_JAMSIZE);
1830 writel(0x10, gp->regs + MAC_ATTLIM);
1831 writel(0x8808, gp->regs + MAC_MCTYPE);
1832
1833 writel((e[5] | (e[4] << 8)) & 0x3ff, gp->regs + MAC_RANDSEED);
1834
1835 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
1836 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
1837 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
1838
1839 writel(0, gp->regs + MAC_ADDR3);
1840 writel(0, gp->regs + MAC_ADDR4);
1841 writel(0, gp->regs + MAC_ADDR5);
1842
1843 writel(0x0001, gp->regs + MAC_ADDR6);
1844 writel(0xc200, gp->regs + MAC_ADDR7);
1845 writel(0x0180, gp->regs + MAC_ADDR8);
1846
1847 writel(0, gp->regs + MAC_AFILT0);
1848 writel(0, gp->regs + MAC_AFILT1);
1849 writel(0, gp->regs + MAC_AFILT2);
1850 writel(0, gp->regs + MAC_AF21MSK);
1851 writel(0, gp->regs + MAC_AF0MSK);
1852
1853 gp->mac_rx_cfg = gem_setup_multicast(gp);
1854#ifdef STRIP_FCS
1855 gp->mac_rx_cfg |= MAC_RXCFG_SFCS;
1856#endif
1857 writel(0, gp->regs + MAC_NCOLL);
1858 writel(0, gp->regs + MAC_FASUCC);
1859 writel(0, gp->regs + MAC_ECOLL);
1860 writel(0, gp->regs + MAC_LCOLL);
1861 writel(0, gp->regs + MAC_DTIMER);
1862 writel(0, gp->regs + MAC_PATMPS);
1863 writel(0, gp->regs + MAC_RFCTR);
1864 writel(0, gp->regs + MAC_LERR);
1865 writel(0, gp->regs + MAC_AERR);
1866 writel(0, gp->regs + MAC_FCSERR);
1867 writel(0, gp->regs + MAC_RXCVERR);
1868
1869 /* Clear RX/TX/MAC/XIF config, we will set these up and enable
1870 * them once a link is established.
1871 */
1872 writel(0, gp->regs + MAC_TXCFG);
1873 writel(gp->mac_rx_cfg, gp->regs + MAC_RXCFG);
1874 writel(0, gp->regs + MAC_MCCFG);
1875 writel(0, gp->regs + MAC_XIFCFG);
1876
1877 /* Setup MAC interrupts. We want to get all of the interesting
1878 * counter expiration events, but we do not want to hear about
1879 * normal rx/tx as the DMA engine tells us that.
1880 */
1881 writel(MAC_TXSTAT_XMIT, gp->regs + MAC_TXMASK);
1882 writel(MAC_RXSTAT_RCV, gp->regs + MAC_RXMASK);
1883
1884 /* Don't enable even the PAUSE interrupts for now, we
1885 * make no use of those events other than to record them.
1886 */
1887 writel(0xffffffff, gp->regs + MAC_MCMASK);
1888
1889 /* Don't enable GEM's WOL in normal operations
1890 */
1891 if (gp->has_wol)
1892 writel(0, gp->regs + WOL_WAKECSR);
1893}
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895static void gem_init_pause_thresholds(struct gem *gp)
1896{
1897 u32 cfg;
1898
1899 /* Calculate pause thresholds. Setting the OFF threshold to the
1900 * full RX fifo size effectively disables PAUSE generation which
1901 * is what we do for 10/100 only GEMs which have FIFOs too small
1902 * to make real gains from PAUSE.
1903 */
1904 if (gp->rx_fifo_sz <= (2 * 1024)) {
1905 gp->rx_pause_off = gp->rx_pause_on = gp->rx_fifo_sz;
1906 } else {
1907 int max_frame = (gp->rx_buf_sz + 4 + 64) & ~63;
1908 int off = (gp->rx_fifo_sz - (max_frame * 2));
1909 int on = off - max_frame;
1910
1911 gp->rx_pause_off = off;
1912 gp->rx_pause_on = on;
1913 }
1914
1915
1916 /* Configure the chip "burst" DMA mode & enable some
1917 * HW bug fixes on Apple version
1918 */
1919 cfg = 0;
1920 if (gp->pdev->vendor == PCI_VENDOR_ID_APPLE)
1921 cfg |= GREG_CFG_RONPAULBIT | GREG_CFG_ENBUG2FIX;
1922#if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
1923 cfg |= GREG_CFG_IBURST;
1924#endif
1925 cfg |= ((31 << 1) & GREG_CFG_TXDMALIM);
1926 cfg |= ((31 << 6) & GREG_CFG_RXDMALIM);
1927 writel(cfg, gp->regs + GREG_CFG);
1928
1929 /* If Infinite Burst didn't stick, then use different
1930 * thresholds (and Apple bug fixes don't exist)
1931 */
1932 if (!(readl(gp->regs + GREG_CFG) & GREG_CFG_IBURST)) {
1933 cfg = ((2 << 1) & GREG_CFG_TXDMALIM);
1934 cfg |= ((8 << 6) & GREG_CFG_RXDMALIM);
1935 writel(cfg, gp->regs + GREG_CFG);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939static int gem_check_invariants(struct gem *gp)
1940{
1941 struct pci_dev *pdev = gp->pdev;
1942 u32 mif_cfg;
1943
1944 /* On Apple's sungem, we can't rely on registers as the chip
1945 * was been powered down by the firmware. The PHY is looked
1946 * up later on.
1947 */
1948 if (pdev->vendor == PCI_VENDOR_ID_APPLE) {
1949 gp->phy_type = phy_mii_mdio0;
1950 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
1951 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
1952 gp->swrst_base = 0;
1953
1954 mif_cfg = readl(gp->regs + MIF_CFG);
1955 mif_cfg &= ~(MIF_CFG_PSELECT|MIF_CFG_POLL|MIF_CFG_BBMODE|MIF_CFG_MDI1);
1956 mif_cfg |= MIF_CFG_MDI0;
1957 writel(mif_cfg, gp->regs + MIF_CFG);
1958 writel(PCS_DMODE_MGM, gp->regs + PCS_DMODE);
1959 writel(MAC_XIFCFG_OE, gp->regs + MAC_XIFCFG);
1960
1961 /* We hard-code the PHY address so we can properly bring it out of
1962 * reset later on, we can't really probe it at this point, though
1963 * that isn't an issue.
1964 */
1965 if (gp->pdev->device == PCI_DEVICE_ID_APPLE_K2_GMAC)
1966 gp->mii_phy_addr = 1;
1967 else
1968 gp->mii_phy_addr = 0;
1969
1970 return 0;
1971 }
1972
1973 mif_cfg = readl(gp->regs + MIF_CFG);
1974
1975 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
1976 pdev->device == PCI_DEVICE_ID_SUN_RIO_GEM) {
1977 /* One of the MII PHYs _must_ be present
1978 * as this chip has no gigabit PHY.
1979 */
1980 if ((mif_cfg & (MIF_CFG_MDI0 | MIF_CFG_MDI1)) == 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00001981 pr_err("RIO GEM lacks MII phy, mif_cfg[%08x]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 mif_cfg);
1983 return -1;
1984 }
1985 }
1986
1987 /* Determine initial PHY interface type guess. MDIO1 is the
1988 * external PHY and thus takes precedence over MDIO0.
1989 */
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (mif_cfg & MIF_CFG_MDI1) {
1992 gp->phy_type = phy_mii_mdio1;
1993 mif_cfg |= MIF_CFG_PSELECT;
1994 writel(mif_cfg, gp->regs + MIF_CFG);
1995 } else if (mif_cfg & MIF_CFG_MDI0) {
1996 gp->phy_type = phy_mii_mdio0;
1997 mif_cfg &= ~MIF_CFG_PSELECT;
1998 writel(mif_cfg, gp->regs + MIF_CFG);
1999 } else {
David S. Millere54d5512009-11-17 04:46:18 -08002000#ifdef CONFIG_SPARC
2001 const char *p;
2002
2003 p = of_get_property(gp->of_node, "shared-pins", NULL);
2004 if (p && !strcmp(p, "serdes"))
2005 gp->phy_type = phy_serdes;
2006 else
2007#endif
2008 gp->phy_type = phy_serialink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 if (gp->phy_type == phy_mii_mdio1 ||
2011 gp->phy_type == phy_mii_mdio0) {
2012 int i;
2013
2014 for (i = 0; i < 32; i++) {
2015 gp->mii_phy_addr = i;
2016 if (phy_read(gp, MII_BMCR) != 0xffff)
2017 break;
2018 }
2019 if (i == 32) {
2020 if (pdev->device != PCI_DEVICE_ID_SUN_GEM) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002021 pr_err("RIO MII phy will not respond\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return -1;
2023 }
2024 gp->phy_type = phy_serdes;
2025 }
2026 }
2027
2028 /* Fetch the FIFO configurations now too. */
2029 gp->tx_fifo_sz = readl(gp->regs + TXDMA_FSZ) * 64;
2030 gp->rx_fifo_sz = readl(gp->regs + RXDMA_FSZ) * 64;
2031
2032 if (pdev->vendor == PCI_VENDOR_ID_SUN) {
2033 if (pdev->device == PCI_DEVICE_ID_SUN_GEM) {
2034 if (gp->tx_fifo_sz != (9 * 1024) ||
2035 gp->rx_fifo_sz != (20 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002036 pr_err("GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 gp->tx_fifo_sz, gp->rx_fifo_sz);
2038 return -1;
2039 }
2040 gp->swrst_base = 0;
2041 } else {
2042 if (gp->tx_fifo_sz != (2 * 1024) ||
2043 gp->rx_fifo_sz != (2 * 1024)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002044 pr_err("RIO GEM has bogus fifo sizes tx(%d) rx(%d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 gp->tx_fifo_sz, gp->rx_fifo_sz);
2046 return -1;
2047 }
2048 gp->swrst_base = (64 / 4) << GREG_SWRST_CACHE_SHIFT;
2049 }
2050 }
2051
2052 return 0;
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055static void gem_reinit_chip(struct gem *gp)
2056{
2057 /* Reset the chip */
2058 gem_reset(gp);
2059
2060 /* Make sure ints are disabled */
2061 gem_disable_ints(gp);
2062
2063 /* Allocate & setup ring buffers */
2064 gem_init_rings(gp);
2065
2066 /* Configure pause thresholds */
2067 gem_init_pause_thresholds(gp);
2068
2069 /* Init DMA & MAC engines */
2070 gem_init_dma(gp);
2071 gem_init_mac(gp);
2072}
2073
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075static void gem_stop_phy(struct gem *gp, int wol)
2076{
David S. Miller7fb76aa2006-01-31 17:09:20 -08002077 u32 mifcfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 /* Let the chip settle down a bit, it seems that helps
2080 * for sleep mode on some models
2081 */
2082 msleep(10);
2083
2084 /* Make sure we aren't polling PHY status change. We
2085 * don't currently use that feature though
2086 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002087 mifcfg = readl(gp->regs + MIF_CFG);
2088 mifcfg &= ~MIF_CFG_POLL;
2089 writel(mifcfg, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (wol && gp->has_wol) {
2092 unsigned char *e = &gp->dev->dev_addr[0];
2093 u32 csr;
2094
2095 /* Setup wake-on-lan for MAGIC packet */
2096 writel(MAC_RXCFG_HFE | MAC_RXCFG_SFCS | MAC_RXCFG_ENAB,
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002097 gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 writel((e[4] << 8) | e[5], gp->regs + WOL_MATCH0);
2099 writel((e[2] << 8) | e[3], gp->regs + WOL_MATCH1);
2100 writel((e[0] << 8) | e[1], gp->regs + WOL_MATCH2);
2101
2102 writel(WOL_MCOUNT_N | WOL_MCOUNT_M, gp->regs + WOL_MCOUNT);
2103 csr = WOL_WAKECSR_ENABLE;
2104 if ((readl(gp->regs + MAC_XIFCFG) & MAC_XIFCFG_GMII) == 0)
2105 csr |= WOL_WAKECSR_MII;
2106 writel(csr, gp->regs + WOL_WAKECSR);
2107 } else {
2108 writel(0, gp->regs + MAC_RXCFG);
2109 (void)readl(gp->regs + MAC_RXCFG);
2110 /* Machine sleep will die in strange ways if we
2111 * dont wait a bit here, looks like the chip takes
2112 * some time to really shut down
2113 */
2114 msleep(10);
2115 }
2116
2117 writel(0, gp->regs + MAC_TXCFG);
2118 writel(0, gp->regs + MAC_XIFCFG);
2119 writel(0, gp->regs + TXDMA_CFG);
2120 writel(0, gp->regs + RXDMA_CFG);
2121
2122 if (!wol) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 gem_reset(gp);
2124 writel(MAC_TXRST_CMD, gp->regs + MAC_TXRST);
2125 writel(MAC_RXRST_CMD, gp->regs + MAC_RXRST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 if (found_mii_phy(gp) && gp->phy_mii.def->ops->suspend)
2128 gp->phy_mii.def->ops->suspend(&gp->phy_mii);
2129
2130 /* According to Apple, we must set the MDIO pins to this begnign
2131 * state or we may 1) eat more current, 2) damage some PHYs
2132 */
David S. Miller7fb76aa2006-01-31 17:09:20 -08002133 writel(mifcfg | MIF_CFG_BBMODE, gp->regs + MIF_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 writel(0, gp->regs + MIF_BBCLK);
2135 writel(0, gp->regs + MIF_BBDATA);
2136 writel(0, gp->regs + MIF_BBOENAB);
2137 writel(MAC_XIFCFG_GMII | MAC_XIFCFG_LBCK, gp->regs + MAC_XIFCFG);
2138 (void) readl(gp->regs + MAC_XIFCFG);
2139 }
2140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142static int gem_do_start(struct net_device *dev)
2143{
Wang Chen8f15ea42008-11-12 23:38:36 -08002144 struct gem *gp = netdev_priv(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002145 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 /* Enable the cell */
2148 gem_get_cell(gp);
2149
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002150 /* Make sure PCI access and bus master are enabled */
2151 rc = pci_enable_device(gp->pdev);
2152 if (rc) {
2153 netdev_err(dev, "Failed to enable chip on PCI bus !\n");
2154
2155 /* Put cell and forget it for now, it will be considered as
2156 * still asleep, a new sleep cycle may bring it back
2157 */
2158 gem_put_cell(gp);
2159 return -ENXIO;
2160 }
2161 pci_set_master(gp->pdev);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /* Init & setup chip hardware */
2164 gem_reinit_chip(gp);
2165
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002166 /* An interrupt might come in handy */
2167 rc = request_irq(gp->pdev->irq, gem_interrupt,
2168 IRQF_SHARED, dev->name, (void *)dev);
2169 if (rc) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002170 netdev_err(dev, "failed to request irq !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 gem_reset(gp);
2173 gem_clean_rings(gp);
2174 gem_put_cell(gp);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002175 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002178 /* Mark us as attached again if we come from resume(), this has
2179 * no effect if we weren't detatched and needs to be done now.
2180 */
2181 netif_device_attach(dev);
2182
2183 /* Restart NAPI & queues */
2184 gem_netif_start(gp);
2185
2186 /* Detect & init PHY, start autoneg etc... this will
2187 * eventually result in starting DMA operations when
2188 * the link is up
2189 */
2190 gem_init_phy(gp);
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
2194
2195static void gem_do_stop(struct net_device *dev, int wol)
2196{
Wang Chen8f15ea42008-11-12 23:38:36 -08002197 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002199 /* Stop NAPI and stop tx queue */
2200 gem_netif_stop(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002202 /* Make sure ints are disabled. We don't care about
2203 * synchronizing as NAPI is disabled, thus a stray
2204 * interrupt will do nothing bad (our irq handler
2205 * just schedules NAPI)
2206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 gem_disable_ints(gp);
2208
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002209 /* Stop the link timer */
2210 del_timer_sync(&gp->link_timer);
2211
2212 /* We cannot cancel the reset task while holding the
2213 * rtnl lock, we'd get an A->B / B->A deadlock stituation
2214 * if we did. This is not an issue however as the reset
2215 * task is synchronized vs. us (rtnl_lock) and will do
2216 * nothing if the device is down or suspended. We do
2217 * still clear reset_task_pending to avoid a spurrious
2218 * reset later on in case we do resume before it gets
2219 * scheduled.
2220 */
2221 gp->reset_task_pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 /* If we are going to sleep with WOL */
2224 gem_stop_dma(gp);
2225 msleep(10);
2226 if (!wol)
2227 gem_reset(gp);
2228 msleep(10);
2229
2230 /* Get rid of rings */
2231 gem_clean_rings(gp);
2232
2233 /* No irq needed anymore */
2234 free_irq(gp->pdev->irq, (void *) dev);
2235
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002236 /* Shut the PHY down eventually and setup WOL */
2237 gem_stop_phy(gp, wol);
2238
2239 /* Make sure bus master is disabled */
2240 pci_disable_device(gp->pdev);
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 /* Cell not needed neither if no WOL */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002243 if (!wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 gem_put_cell(gp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
David Howellsc4028952006-11-22 14:57:56 +00002247static void gem_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
David Howellsc4028952006-11-22 14:57:56 +00002249 struct gem *gp = container_of(work, struct gem, reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002251 /* Lock out the network stack (essentially shield ourselves
2252 * against a racing open, close, control call, or suspend
2253 */
2254 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002256 /* Skip the reset task if suspended or closed, or if it's
2257 * been cancelled by gem_do_stop (see comment there)
2258 */
2259 if (!netif_device_present(gp->dev) ||
2260 !netif_running(gp->dev) ||
2261 !gp->reset_task_pending) {
2262 rtnl_unlock();
2263 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Johannes Bergdde655c2007-11-29 21:51:36 +11002265
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002266 /* Stop the link timer */
2267 del_timer_sync(&gp->link_timer);
2268
2269 /* Stop NAPI and tx */
2270 gem_netif_stop(gp);
2271
2272 /* Reset the chip & rings */
2273 gem_reinit_chip(gp);
2274 if (gp->lstate == link_up)
2275 gem_set_link_modes(gp);
2276
2277 /* Restart NAPI and Tx */
2278 gem_netif_start(gp);
2279
2280 /* We are back ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 gp->reset_task_pending = 0;
2282
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002283 /* If the link is not up, restart autoneg, else restart the
2284 * polling timer
2285 */
2286 if (gp->lstate != link_up)
2287 gem_begin_auto_negotiation(gp, NULL);
2288 else
2289 mod_timer(&gp->link_timer, jiffies + ((12 * HZ) / 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002291 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294static int gem_open(struct net_device *dev)
2295{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002296 /* We allow open while suspended, we just do nothing,
2297 * the chip will be initialized in resume()
2298 */
2299 if (netif_device_present(dev))
2300 return gem_do_start(dev);
2301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304static int gem_close(struct net_device *dev)
2305{
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002306 if (netif_device_present(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 gem_do_stop(dev, 0);
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310}
2311
2312#ifdef CONFIG_PM
2313static int gem_suspend(struct pci_dev *pdev, pm_message_t state)
2314{
2315 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002316 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002318 /* Lock the network stack first to avoid racing with open/close,
2319 * reset task and setting calls
2320 */
2321 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002323 /* Not running, mark ourselves non-present, no need for
2324 * a lock here
2325 */
2326 if (!netif_running(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 netif_device_detach(dev);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002328 rtnl_unlock();
2329 return 0;
2330 }
2331 netdev_info(dev, "suspending, WakeOnLan %s\n",
2332 (gp->wake_on_lan && netif_running(dev)) ?
2333 "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002335 /* Tell the network stack we're gone. gem_do_stop() below will
2336 * synchronize with TX, stop NAPI etc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002338 netif_device_detach(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002340 /* Switch off chip, remember WOL setting */
Gerard Lledo5a8887d2012-04-28 08:52:37 +00002341 gp->asleep_wol = !!gp->wake_on_lan;
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002342 gem_do_stop(dev, gp->asleep_wol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002344 /* Unlock the network stack */
2345 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 return 0;
2348}
2349
2350static int gem_resume(struct pci_dev *pdev)
2351{
2352 struct net_device *dev = pci_get_drvdata(pdev);
Wang Chen8f15ea42008-11-12 23:38:36 -08002353 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002355 /* See locking comment in gem_suspend */
2356 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002358 /* Not running, mark ourselves present, no need for
2359 * a lock here
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002361 if (!netif_running(dev)) {
2362 netif_device_attach(dev);
2363 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 return 0;
2365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002367 /* Restart chip. If that fails there isn't much we can do, we
2368 * leave things stopped.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002370 gem_do_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /* If we had WOL enabled, the cell clock was never turned off during
2373 * sleep, so we end up beeing unbalanced. Fix that here
2374 */
2375 if (gp->asleep_wol)
2376 gem_put_cell(gp);
2377
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002378 /* Unlock the network stack */
2379 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 return 0;
2382}
2383#endif /* CONFIG_PM */
2384
2385static struct net_device_stats *gem_get_stats(struct net_device *dev)
2386{
Wang Chen8f15ea42008-11-12 23:38:36 -08002387 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /* I have seen this being called while the PM was in progress,
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002390 * so we shield against this. Let's also not poke at registers
2391 * while the reset task is going on.
2392 *
2393 * TODO: Move stats collection elsewhere (link timer ?) and
2394 * make this a nop to avoid all those synchro issues
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002396 if (!netif_device_present(dev) || !netif_running(dev))
2397 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002399 /* Better safe than sorry... */
2400 if (WARN_ON(!gp->cell_enabled))
2401 goto bail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002403 dev->stats.rx_crc_errors += readl(gp->regs + MAC_FCSERR);
2404 writel(0, gp->regs + MAC_FCSERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002406 dev->stats.rx_frame_errors += readl(gp->regs + MAC_AERR);
2407 writel(0, gp->regs + MAC_AERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002409 dev->stats.rx_length_errors += readl(gp->regs + MAC_LERR);
2410 writel(0, gp->regs + MAC_LERR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002412 dev->stats.tx_aborted_errors += readl(gp->regs + MAC_ECOLL);
2413 dev->stats.collisions +=
2414 (readl(gp->regs + MAC_ECOLL) + readl(gp->regs + MAC_LCOLL));
2415 writel(0, gp->regs + MAC_ECOLL);
2416 writel(0, gp->regs + MAC_LCOLL);
2417 bail:
Denis Kirjanovaae7c472011-01-27 09:54:12 +00002418 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002421static int gem_set_mac_address(struct net_device *dev, void *addr)
2422{
2423 struct sockaddr *macaddr = (struct sockaddr *) addr;
Wang Chen8f15ea42008-11-12 23:38:36 -08002424 struct gem *gp = netdev_priv(dev);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002425 unsigned char *e = &dev->dev_addr[0];
2426
2427 if (!is_valid_ether_addr(macaddr->sa_data))
2428 return -EADDRNOTAVAIL;
2429
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002430 memcpy(dev->dev_addr, macaddr->sa_data, dev->addr_len);
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002431
2432 /* We'll just catch it later when the device is up'd or resumed */
2433 if (!netif_running(dev) || !netif_device_present(dev))
2434 return 0;
2435
2436 /* Better safe than sorry... */
2437 if (WARN_ON(!gp->cell_enabled))
2438 return 0;
2439
2440 writel((e[4] << 8) | e[5], gp->regs + MAC_ADDR0);
2441 writel((e[2] << 8) | e[3], gp->regs + MAC_ADDR1);
2442 writel((e[0] << 8) | e[1], gp->regs + MAC_ADDR2);
Ruben Vandeginste09c72ec2007-03-26 14:43:49 -07002443
2444 return 0;
2445}
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447static void gem_set_multicast(struct net_device *dev)
2448{
Wang Chen8f15ea42008-11-12 23:38:36 -08002449 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 u32 rxcfg, rxcfg_new;
2451 int limit = 10000;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002452
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002453 if (!netif_running(dev) || !netif_device_present(dev))
2454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002456 /* Better safe than sorry... */
2457 if (gp->reset_task_pending || WARN_ON(!gp->cell_enabled))
2458 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 rxcfg = readl(gp->regs + MAC_RXCFG);
2461 rxcfg_new = gem_setup_multicast(gp);
2462#ifdef STRIP_FCS
2463 rxcfg_new |= MAC_RXCFG_SFCS;
2464#endif
2465 gp->mac_rx_cfg = rxcfg_new;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 writel(rxcfg & ~MAC_RXCFG_ENAB, gp->regs + MAC_RXCFG);
2468 while (readl(gp->regs + MAC_RXCFG) & MAC_RXCFG_ENAB) {
2469 if (!limit--)
2470 break;
2471 udelay(10);
2472 }
2473
2474 rxcfg &= ~(MAC_RXCFG_PROM | MAC_RXCFG_HFE);
2475 rxcfg |= rxcfg_new;
2476
2477 writel(rxcfg, gp->regs + MAC_RXCFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
2480/* Jumbo-grams don't seem to work :-( */
2481#define GEM_MIN_MTU 68
2482#if 1
2483#define GEM_MAX_MTU 1500
2484#else
2485#define GEM_MAX_MTU 9000
2486#endif
2487
2488static int gem_change_mtu(struct net_device *dev, int new_mtu)
2489{
Wang Chen8f15ea42008-11-12 23:38:36 -08002490 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
2492 if (new_mtu < GEM_MIN_MTU || new_mtu > GEM_MAX_MTU)
2493 return -EINVAL;
2494
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523static int gem_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2524{
Wang Chen8f15ea42008-11-12 23:38:36 -08002525 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 if (gp->phy_type == phy_mii_mdio0 ||
2528 gp->phy_type == phy_mii_mdio1) {
2529 if (gp->phy_mii.def)
2530 cmd->supported = gp->phy_mii.def->features;
2531 else
2532 cmd->supported = (SUPPORTED_10baseT_Half |
2533 SUPPORTED_10baseT_Full);
2534
2535 /* XXX hardcoded stuff for now */
2536 cmd->port = PORT_MII;
2537 cmd->transceiver = XCVR_EXTERNAL;
2538 cmd->phy_address = 0; /* XXX fixed PHYAD */
2539
2540 /* Return current PHY settings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 cmd->autoneg = gp->want_autoneg;
David Decotigny70739492011-04-27 18:32:40 +00002542 ethtool_cmd_speed_set(cmd, gp->phy_mii.speed);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002543 cmd->duplex = gp->phy_mii.duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 cmd->advertising = gp->phy_mii.advertising;
2545
2546 /* If we started with a forced mode, we don't have a default
2547 * advertise set, we need to return something sensible so
2548 * userland can re-enable autoneg properly.
2549 */
2550 if (cmd->advertising == 0)
2551 cmd->advertising = cmd->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 } else { // XXX PCS ?
2553 cmd->supported =
2554 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
2555 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
2556 SUPPORTED_Autoneg);
2557 cmd->advertising = cmd->supported;
David Decotigny70739492011-04-27 18:32:40 +00002558 ethtool_cmd_speed_set(cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 cmd->duplex = cmd->port = cmd->phy_address =
2560 cmd->transceiver = cmd->autoneg = 0;
Hermann Lauerfbf02292008-12-09 15:39:14 -08002561
2562 /* serdes means usually a Fibre connector, with most fixed */
2563 if (gp->phy_type == phy_serdes) {
2564 cmd->port = PORT_FIBRE;
2565 cmd->supported = (SUPPORTED_1000baseT_Half |
2566 SUPPORTED_1000baseT_Full |
2567 SUPPORTED_FIBRE | SUPPORTED_Autoneg |
2568 SUPPORTED_Pause | SUPPORTED_Asym_Pause);
2569 cmd->advertising = cmd->supported;
2570 cmd->transceiver = XCVR_INTERNAL;
2571 if (gp->lstate == link_up)
David Decotigny70739492011-04-27 18:32:40 +00002572 ethtool_cmd_speed_set(cmd, SPEED_1000);
Hermann Lauerfbf02292008-12-09 15:39:14 -08002573 cmd->duplex = DUPLEX_FULL;
2574 cmd->autoneg = 1;
2575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 cmd->maxtxpkt = cmd->maxrxpkt = 0;
2578
2579 return 0;
2580}
2581
2582static int gem_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2583{
Wang Chen8f15ea42008-11-12 23:38:36 -08002584 struct gem *gp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00002585 u32 speed = ethtool_cmd_speed(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /* Verify the settings we care about. */
2588 if (cmd->autoneg != AUTONEG_ENABLE &&
2589 cmd->autoneg != AUTONEG_DISABLE)
2590 return -EINVAL;
2591
2592 if (cmd->autoneg == AUTONEG_ENABLE &&
2593 cmd->advertising == 0)
2594 return -EINVAL;
2595
2596 if (cmd->autoneg == AUTONEG_DISABLE &&
David Decotigny25db0332011-04-27 18:32:39 +00002597 ((speed != SPEED_1000 &&
2598 speed != SPEED_100 &&
2599 speed != SPEED_10) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 (cmd->duplex != DUPLEX_HALF &&
2601 cmd->duplex != DUPLEX_FULL)))
2602 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* Apply settings and restart link process. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002605 if (netif_device_present(gp->dev)) {
2606 del_timer_sync(&gp->link_timer);
2607 gem_begin_auto_negotiation(gp, cmd);
2608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 return 0;
2611}
2612
2613static int gem_nway_reset(struct net_device *dev)
2614{
Wang Chen8f15ea42008-11-12 23:38:36 -08002615 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 if (!gp->want_autoneg)
2618 return -EINVAL;
2619
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002620 /* Restart link process */
2621 if (netif_device_present(gp->dev)) {
2622 del_timer_sync(&gp->link_timer);
2623 gem_begin_auto_negotiation(gp, NULL);
2624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 return 0;
2627}
2628
2629static u32 gem_get_msglevel(struct net_device *dev)
2630{
Wang Chen8f15ea42008-11-12 23:38:36 -08002631 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return gp->msg_enable;
2633}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635static void gem_set_msglevel(struct net_device *dev, u32 value)
2636{
Wang Chen8f15ea42008-11-12 23:38:36 -08002637 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 gp->msg_enable = value;
2639}
2640
2641
2642/* Add more when I understand how to program the chip */
2643/* like WAKE_UCAST | WAKE_MCAST | WAKE_BCAST */
2644
2645#define WOL_SUPPORTED_MASK (WAKE_MAGIC)
2646
2647static void gem_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2648{
Wang Chen8f15ea42008-11-12 23:38:36 -08002649 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* Add more when I understand how to program the chip */
2652 if (gp->has_wol) {
2653 wol->supported = WOL_SUPPORTED_MASK;
2654 wol->wolopts = gp->wake_on_lan;
2655 } else {
2656 wol->supported = 0;
2657 wol->wolopts = 0;
2658 }
2659}
2660
2661static int gem_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
2662{
Wang Chen8f15ea42008-11-12 23:38:36 -08002663 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 if (!gp->has_wol)
2666 return -EOPNOTSUPP;
2667 gp->wake_on_lan = wol->wolopts & WOL_SUPPORTED_MASK;
2668 return 0;
2669}
2670
Jeff Garzik7282d492006-09-13 14:30:00 -04002671static const struct ethtool_ops gem_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .get_drvinfo = gem_get_drvinfo,
2673 .get_link = ethtool_op_get_link,
2674 .get_settings = gem_get_settings,
2675 .set_settings = gem_set_settings,
2676 .nway_reset = gem_nway_reset,
2677 .get_msglevel = gem_get_msglevel,
2678 .set_msglevel = gem_set_msglevel,
2679 .get_wol = gem_get_wol,
2680 .set_wol = gem_set_wol,
2681};
2682
2683static int gem_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2684{
Wang Chen8f15ea42008-11-12 23:38:36 -08002685 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct mii_ioctl_data *data = if_mii(ifr);
2687 int rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002689 /* For SIOCGMIIREG and SIOCSMIIREG the core checks for us that
2690 * netif_device_present() is true and holds rtnl_lock for us
2691 * so we have nothing to worry about
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 switch (cmd) {
2695 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
2696 data->phy_id = gp->mii_phy_addr;
2697 /* Fallthrough... */
2698
2699 case SIOCGMIIREG: /* Read MII PHY register. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002700 data->val_out = __phy_read(gp, data->phy_id & 0x1f,
2701 data->reg_num & 0x1f);
2702 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 break;
2704
2705 case SIOCSMIIREG: /* Write MII PHY register. */
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002706 __phy_write(gp, data->phy_id & 0x1f, data->reg_num & 0x1f,
2707 data->val_in);
2708 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 break;
Joe Perches6403eab2011-06-03 11:51:20 +00002710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return rc;
2712}
2713
David S. Millerdadb8302007-02-28 15:42:50 -08002714#if (!defined(CONFIG_SPARC) && !defined(CONFIG_PPC_PMAC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715/* Fetch MAC address from vital product data of PCI ROM. */
Linus Torvalds4120b022005-09-11 09:26:20 -07002716static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, unsigned char *dev_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 int this_offset;
2719
2720 for (this_offset = 0x20; this_offset < len; this_offset++) {
2721 void __iomem *p = rom_base + this_offset;
2722 int i;
2723
2724 if (readb(p + 0) != 0x90 ||
2725 readb(p + 1) != 0x00 ||
2726 readb(p + 2) != 0x09 ||
2727 readb(p + 3) != 0x4e ||
2728 readb(p + 4) != 0x41 ||
2729 readb(p + 5) != 0x06)
2730 continue;
2731
2732 this_offset += 6;
2733 p += 6;
2734
2735 for (i = 0; i < 6; i++)
2736 dev_addr[i] = readb(p + i);
Linus Torvalds4120b022005-09-11 09:26:20 -07002737 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
Linus Torvalds4120b022005-09-11 09:26:20 -07002739 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
2742static void get_gem_mac_nonobp(struct pci_dev *pdev, unsigned char *dev_addr)
2743{
Linus Torvalds4120b022005-09-11 09:26:20 -07002744 size_t size;
2745 void __iomem *p = pci_map_rom(pdev, &size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Linus Torvalds4120b022005-09-11 09:26:20 -07002747 if (p) {
2748 int found;
2749
2750 found = readb(p) == 0x55 &&
2751 readb(p + 1) == 0xaa &&
2752 find_eth_addr_in_vpd(p, (64 * 1024), dev_addr);
2753 pci_unmap_rom(pdev, p);
2754 if (found)
2755 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /* Sun MAC prefix then 3 random bytes. */
2759 dev_addr[0] = 0x08;
2760 dev_addr[1] = 0x00;
2761 dev_addr[2] = 0x20;
2762 get_random_bytes(dev_addr + 3, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764#endif /* not Sparc and not PPC */
2765
Bill Pembertonf73d12b2012-12-03 09:24:02 -05002766static int gem_get_device_address(struct gem *gp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
David S. Millerdadb8302007-02-28 15:42:50 -08002768#if defined(CONFIG_SPARC) || defined(CONFIG_PPC_PMAC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 struct net_device *dev = gp->dev;
Jeremy Kerr1a2509c2006-07-12 15:41:03 +10002770 const unsigned char *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Stephen Rothwell40cd3a42007-05-01 13:54:02 +10002772 addr = of_get_property(gp->of_node, "local-mac-address", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (addr == NULL) {
David S. Miller457e1a82007-03-29 01:36:44 -07002774#ifdef CONFIG_SPARC
2775 addr = idprom->id_ethaddr;
2776#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 printk("\n");
Joe Perchesc6c75982010-08-17 07:55:04 +00002778 pr_err("%s: can't get mac-address\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return -1;
David S. Miller457e1a82007-03-29 01:36:44 -07002780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Joe Perchesd458cdf2013-10-01 19:04:40 -07002782 memcpy(dev->dev_addr, addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783#else
2784 get_gem_mac_nonobp(gp->pdev, gp->dev->dev_addr);
2785#endif
2786 return 0;
2787}
2788
Adrian Bunk14904392005-12-21 18:50:12 -08002789static void gem_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
2791 struct net_device *dev = pci_get_drvdata(pdev);
2792
2793 if (dev) {
Wang Chen8f15ea42008-11-12 23:38:36 -08002794 struct gem *gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 unregister_netdev(dev);
2797
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002798 /* Ensure reset task is truely gone */
Tejun Heofe8998c2010-12-12 16:45:14 +01002799 cancel_work_sync(&gp->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /* Free resources */
2802 pci_free_consistent(pdev,
2803 sizeof(struct gem_init_block),
2804 gp->init_block,
2805 gp->gblock_dvma);
2806 iounmap(gp->regs);
2807 pci_release_regions(pdev);
2808 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810}
2811
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002812static const struct net_device_ops gem_netdev_ops = {
2813 .ndo_open = gem_open,
2814 .ndo_stop = gem_close,
2815 .ndo_start_xmit = gem_start_xmit,
2816 .ndo_get_stats = gem_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002817 .ndo_set_rx_mode = gem_set_multicast,
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002818 .ndo_do_ioctl = gem_ioctl,
2819 .ndo_tx_timeout = gem_tx_timeout,
2820 .ndo_change_mtu = gem_change_mtu,
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002821 .ndo_validate_addr = eth_validate_addr,
Stephen Hemminger5ed01022009-03-19 23:58:01 -07002822 .ndo_set_mac_address = gem_set_mac_address,
2823#ifdef CONFIG_NET_POLL_CONTROLLER
2824 .ndo_poll_controller = gem_poll_controller,
2825#endif
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002826};
2827
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002828static int gem_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 unsigned long gemreg_base, gemreg_len;
2831 struct net_device *dev;
2832 struct gem *gp;
Joe Perches0795af52007-10-03 17:59:30 -07002833 int err, pci_using_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Joe Perchesc6c75982010-08-17 07:55:04 +00002835 printk_once(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 /* Apple gmac note: during probe, the chip is powered up by
2838 * the arch code to allow the code below to work (and to let
2839 * the chip be probed on the config space. It won't stay powered
2840 * up until the interface is brought up however, so we can't rely
2841 * on register configuration done at this point.
2842 */
2843 err = pci_enable_device(pdev);
2844 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002845 pr_err("Cannot enable MMIO operation, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return err;
2847 }
2848 pci_set_master(pdev);
2849
2850 /* Configure DMA attributes. */
2851
2852 /* All of the GEM documentation states that 64-bit DMA addressing
2853 * is fully supported and should work just fine. However the
2854 * front end for RIO based GEMs is different and only supports
2855 * 32-bit addressing.
2856 *
2857 * For now we assume the various PPC GEMs are 32-bit only as well.
2858 */
2859 if (pdev->vendor == PCI_VENDOR_ID_SUN &&
2860 pdev->device == PCI_DEVICE_ID_SUN_GEM &&
Yang Hongyang6a355282009-04-06 19:01:13 -07002861 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 pci_using_dac = 1;
2863 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002864 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002866 pr_err("No usable DMA configuration, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 goto err_disable_device;
2868 }
2869 pci_using_dac = 0;
2870 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 gemreg_base = pci_resource_start(pdev, 0);
2873 gemreg_len = pci_resource_len(pdev, 0);
2874
2875 if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002876 pr_err("Cannot find proper PCI device base address, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 err = -ENODEV;
2878 goto err_disable_device;
2879 }
2880
2881 dev = alloc_etherdev(sizeof(*gp));
2882 if (!dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 err = -ENOMEM;
2884 goto err_disable_device;
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 SET_NETDEV_DEV(dev, &pdev->dev);
2887
Wang Chen8f15ea42008-11-12 23:38:36 -08002888 gp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 err = pci_request_regions(pdev, DRV_NAME);
2891 if (err) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002892 pr_err("Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 goto err_out_free_netdev;
2894 }
2895
2896 gp->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 gp->dev = dev;
2898
2899 gp->msg_enable = DEFAULT_MSG;
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 init_timer(&gp->link_timer);
2902 gp->link_timer.function = gem_link_timer;
2903 gp->link_timer.data = (unsigned long) gp;
2904
David Howellsc4028952006-11-22 14:57:56 +00002905 INIT_WORK(&gp->reset_task, gem_reset_task);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 gp->lstate = link_down;
2908 gp->timer_ticks = 0;
2909 netif_carrier_off(dev);
2910
2911 gp->regs = ioremap(gemreg_base, gemreg_len);
Al Viro79ea13c2008-01-24 02:06:46 -08002912 if (!gp->regs) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002913 pr_err("Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 err = -EIO;
2915 goto err_out_free_res;
2916 }
2917
2918 /* On Apple, we want a reference to the Open Firmware device-tree
2919 * node. We use it for clock control.
2920 */
David S. Miller457e1a82007-03-29 01:36:44 -07002921#if defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 gp->of_node = pci_device_to_OF_node(pdev);
2923#endif
2924
2925 /* Only Apple version supports WOL afaik */
2926 if (pdev->vendor == PCI_VENDOR_ID_APPLE)
2927 gp->has_wol = 1;
2928
2929 /* Make sure cell is enabled */
2930 gem_get_cell(gp);
2931
2932 /* Make sure everything is stopped and in init state */
2933 gem_reset(gp);
2934
2935 /* Fill up the mii_phy structure (even if we won't use it) */
2936 gp->phy_mii.dev = dev;
2937 gp->phy_mii.mdio_read = _phy_read;
2938 gp->phy_mii.mdio_write = _phy_write;
Benjamin Herrenschmidt3c326fe2005-07-07 17:56:09 -07002939#ifdef CONFIG_PPC_PMAC
2940 gp->phy_mii.platform_data = gp->of_node;
2941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /* By default, we start with autoneg */
2943 gp->want_autoneg = 1;
2944
2945 /* Check fifo sizes, PHY type, etc... */
2946 if (gem_check_invariants(gp)) {
2947 err = -ENODEV;
2948 goto err_out_iounmap;
2949 }
2950
2951 /* It is guaranteed that the returned buffer will be at least
2952 * PAGE_SIZE aligned.
2953 */
2954 gp->init_block = (struct gem_init_block *)
2955 pci_alloc_consistent(pdev, sizeof(struct gem_init_block),
2956 &gp->gblock_dvma);
2957 if (!gp->init_block) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002958 pr_err("Cannot allocate init block, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 err = -ENOMEM;
2960 goto err_out_iounmap;
2961 }
2962
Peter Senna Tschudin4df12832012-10-05 12:40:54 +00002963 err = gem_get_device_address(gp);
2964 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 goto err_out_free_consistent;
2966
Stephen Hemmingerd9a811d2009-01-07 17:28:54 -08002967 dev->netdev_ops = &gem_netdev_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002968 netif_napi_add(dev, &gp->napi, gem_poll, 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 dev->ethtool_ops = &gem_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 dev->watchdog_timeo = 5 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 dev->dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 /* Set that now, in case PM kicks in now */
2974 pci_set_drvdata(pdev, dev);
2975
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002976 /* We can do scatter/gather and HW checksum */
2977 dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
2978 dev->features |= dev->hw_features | NETIF_F_RXCSUM;
2979 if (pci_using_dac)
2980 dev->features |= NETIF_F_HIGHDMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 /* Register with kernel */
2983 if (register_netdev(dev)) {
Joe Perchesc6c75982010-08-17 07:55:04 +00002984 pr_err("Cannot register net device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 err = -ENOMEM;
2986 goto err_out_free_consistent;
2987 }
2988
Benjamin Herrenschmidtfe09bb62011-06-01 17:17:10 +10002989 /* Undo the get_cell with appropriate locking (we could use
2990 * ndo_init/uninit but that would be even more clumsy imho)
2991 */
2992 rtnl_lock();
2993 gem_put_cell(gp);
2994 rtnl_unlock();
2995
Joe Perchesc6c75982010-08-17 07:55:04 +00002996 netdev_info(dev, "Sun GEM (PCI) 10/100/1000BaseT Ethernet %pM\n",
2997 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return 0;
2999
3000err_out_free_consistent:
3001 gem_remove_one(pdev);
3002err_out_iounmap:
3003 gem_put_cell(gp);
3004 iounmap(gp->regs);
3005
3006err_out_free_res:
3007 pci_release_regions(pdev);
3008
3009err_out_free_netdev:
3010 free_netdev(dev);
3011err_disable_device:
3012 pci_disable_device(pdev);
3013 return err;
3014
3015}
3016
3017
3018static struct pci_driver gem_driver = {
3019 .name = GEM_MODULE_NAME,
3020 .id_table = gem_pci_tbl,
3021 .probe = gem_init_one,
Adrian Bunk14904392005-12-21 18:50:12 -08003022 .remove = gem_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023#ifdef CONFIG_PM
3024 .suspend = gem_suspend,
3025 .resume = gem_resume,
3026#endif /* CONFIG_PM */
3027};
3028
Peter Hüwe5119ad02013-05-21 12:58:09 +00003029module_pci_driver(gem_driver);