1 /* $Id: sungem.c,v 1.44.2.22 2002/03/13 01:18:12 davem Exp $
2 * sungem.c: Sun GEM ethernet driver.
4 * Copyright (C) 2000, 2001, 2002, 2003 David S. Miller (davem@redhat.com)
6 * 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.
10 * NAPI and NETPOLL support
11 * (C) 2004 by Eric Lemoine (eric.lemoine@gmail.com)
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #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>
24 #include <linux/sched.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
27 #include <linux/errno.h>
28 #include <linux/pci.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/crc32.h>
36 #include <linux/random.h>
37 #include <linux/workqueue.h>
38 #include <linux/if_vlan.h>
39 #include <linux/bitops.h>
41 #include <linux/gfp.h>
44 #include <asm/byteorder.h>
45 #include <linux/uaccess.h>
49 #include <asm/idprom.h>
53 #ifdef CONFIG_PPC_PMAC
55 #include <asm/machdep.h>
56 #include <asm/pmac_feature.h>
59 #include <linux/sungem_phy.h>
62 /* Stripping FCS is causing problems, disabled for now */
65 #define DEFAULT_MSG (NETIF_MSG_DRV | \
69 #define ADVERTISE_MASK (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
70 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
71 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full | \
72 SUPPORTED_Pause | SUPPORTED_Autoneg)
74 #define DRV_NAME "sungem"
75 #define DRV_VERSION "1.0"
76 #define DRV_AUTHOR "David S. Miller <davem@redhat.com>"
78 static char version
[] =
79 DRV_NAME
".c:v" DRV_VERSION
" " DRV_AUTHOR
"\n";
81 MODULE_AUTHOR(DRV_AUTHOR
);
82 MODULE_DESCRIPTION("Sun GEM Gbit ethernet driver");
83 MODULE_LICENSE("GPL");
85 #define GEM_MODULE_NAME "gem"
87 static const struct pci_device_id gem_pci_tbl
[] = {
88 { PCI_VENDOR_ID_SUN
, PCI_DEVICE_ID_SUN_GEM
,
89 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
91 /* These models only differ from the original GEM in
92 * that their tx/rx fifos are of a different size and
93 * they only support 10/100 speeds. -DaveM
95 * Apple's GMAC does support gigabit on machines with
96 * the BCM54xx PHYs. -BenH
98 { PCI_VENDOR_ID_SUN
, PCI_DEVICE_ID_SUN_RIO_GEM
,
99 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
100 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_UNI_N_GMAC
,
101 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
102 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_UNI_N_GMACP
,
103 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
104 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_UNI_N_GMAC2
,
105 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
106 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_K2_GMAC
,
107 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
108 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_SH_SUNGEM
,
109 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
110 { PCI_VENDOR_ID_APPLE
, PCI_DEVICE_ID_APPLE_IPID2_GMAC
,
111 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
115 MODULE_DEVICE_TABLE(pci
, gem_pci_tbl
);
117 static u16
__sungem_phy_read(struct gem
*gp
, int phy_addr
, int reg
)
124 cmd
|= (phy_addr
<< 23) & MIF_FRAME_PHYAD
;
125 cmd
|= (reg
<< 18) & MIF_FRAME_REGAD
;
126 cmd
|= (MIF_FRAME_TAMSB
);
127 writel(cmd
, gp
->regs
+ MIF_FRAME
);
130 cmd
= readl(gp
->regs
+ MIF_FRAME
);
131 if (cmd
& MIF_FRAME_TALSB
)
140 return cmd
& MIF_FRAME_DATA
;
143 static inline int _sungem_phy_read(struct net_device
*dev
, int mii_id
, int reg
)
145 struct gem
*gp
= netdev_priv(dev
);
146 return __sungem_phy_read(gp
, mii_id
, reg
);
149 static inline u16
sungem_phy_read(struct gem
*gp
, int reg
)
151 return __sungem_phy_read(gp
, gp
->mii_phy_addr
, reg
);
154 static void __sungem_phy_write(struct gem
*gp
, int phy_addr
, int reg
, u16 val
)
161 cmd
|= (phy_addr
<< 23) & MIF_FRAME_PHYAD
;
162 cmd
|= (reg
<< 18) & MIF_FRAME_REGAD
;
163 cmd
|= (MIF_FRAME_TAMSB
);
164 cmd
|= (val
& MIF_FRAME_DATA
);
165 writel(cmd
, gp
->regs
+ MIF_FRAME
);
168 cmd
= readl(gp
->regs
+ MIF_FRAME
);
169 if (cmd
& MIF_FRAME_TALSB
)
176 static inline void _sungem_phy_write(struct net_device
*dev
, int mii_id
, int reg
, int val
)
178 struct gem
*gp
= netdev_priv(dev
);
179 __sungem_phy_write(gp
, mii_id
, reg
, val
& 0xffff);
182 static inline void sungem_phy_write(struct gem
*gp
, int reg
, u16 val
)
184 __sungem_phy_write(gp
, gp
->mii_phy_addr
, reg
, val
);
187 static inline void gem_enable_ints(struct gem
*gp
)
189 /* Enable all interrupts but TXDONE */
190 writel(GREG_STAT_TXDONE
, gp
->regs
+ GREG_IMASK
);
193 static inline void gem_disable_ints(struct gem
*gp
)
195 /* Disable all interrupts, including TXDONE */
196 writel(GREG_STAT_NAPI
| GREG_STAT_TXDONE
, gp
->regs
+ GREG_IMASK
);
197 (void)readl(gp
->regs
+ GREG_IMASK
); /* write posting */
200 static void gem_get_cell(struct gem
*gp
)
202 BUG_ON(gp
->cell_enabled
< 0);
204 #ifdef CONFIG_PPC_PMAC
205 if (gp
->cell_enabled
== 1) {
207 pmac_call_feature(PMAC_FTR_GMAC_ENABLE
, gp
->of_node
, 0, 1);
210 #endif /* CONFIG_PPC_PMAC */
213 /* Turn off the chip's clock */
214 static void gem_put_cell(struct gem
*gp
)
216 BUG_ON(gp
->cell_enabled
<= 0);
218 #ifdef CONFIG_PPC_PMAC
219 if (gp
->cell_enabled
== 0) {
221 pmac_call_feature(PMAC_FTR_GMAC_ENABLE
, gp
->of_node
, 0, 0);
224 #endif /* CONFIG_PPC_PMAC */
227 static inline void gem_netif_stop(struct gem
*gp
)
229 netif_trans_update(gp
->dev
); /* prevent tx timeout */
230 napi_disable(&gp
->napi
);
231 netif_tx_disable(gp
->dev
);
234 static inline void gem_netif_start(struct gem
*gp
)
236 /* NOTE: unconditional netif_wake_queue is only
237 * appropriate so long as all callers are assured to
238 * have free tx slots.
240 netif_wake_queue(gp
->dev
);
241 napi_enable(&gp
->napi
);
244 static void gem_schedule_reset(struct gem
*gp
)
246 gp
->reset_task_pending
= 1;
247 schedule_work(&gp
->reset_task
);
250 static void gem_handle_mif_event(struct gem
*gp
, u32 reg_val
, u32 changed_bits
)
252 if (netif_msg_intr(gp
))
253 printk(KERN_DEBUG
"%s: mif interrupt\n", gp
->dev
->name
);
256 static int gem_pcs_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
258 u32 pcs_istat
= readl(gp
->regs
+ PCS_ISTAT
);
261 if (netif_msg_intr(gp
))
262 printk(KERN_DEBUG
"%s: pcs interrupt, pcs_istat: 0x%x\n",
263 gp
->dev
->name
, pcs_istat
);
265 if (!(pcs_istat
& PCS_ISTAT_LSC
)) {
266 netdev_err(dev
, "PCS irq but no link status change???\n");
270 /* The link status bit latches on zero, so you must
271 * read it twice in such a case to see a transition
272 * to the link being up.
274 pcs_miistat
= readl(gp
->regs
+ PCS_MIISTAT
);
275 if (!(pcs_miistat
& PCS_MIISTAT_LS
))
277 (readl(gp
->regs
+ PCS_MIISTAT
) &
280 if (pcs_miistat
& PCS_MIISTAT_ANC
) {
281 /* The remote-fault indication is only valid
282 * when autoneg has completed.
284 if (pcs_miistat
& PCS_MIISTAT_RF
)
285 netdev_info(dev
, "PCS AutoNEG complete, RemoteFault\n");
287 netdev_info(dev
, "PCS AutoNEG complete\n");
290 if (pcs_miistat
& PCS_MIISTAT_LS
) {
291 netdev_info(dev
, "PCS link is now up\n");
292 netif_carrier_on(gp
->dev
);
294 netdev_info(dev
, "PCS link is now down\n");
295 netif_carrier_off(gp
->dev
);
296 /* If this happens and the link timer is not running,
297 * reset so we re-negotiate.
299 if (!timer_pending(&gp
->link_timer
))
306 static int gem_txmac_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
308 u32 txmac_stat
= readl(gp
->regs
+ MAC_TXSTAT
);
310 if (netif_msg_intr(gp
))
311 printk(KERN_DEBUG
"%s: txmac interrupt, txmac_stat: 0x%x\n",
312 gp
->dev
->name
, txmac_stat
);
314 /* Defer timer expiration is quite normal,
315 * don't even log the event.
317 if ((txmac_stat
& MAC_TXSTAT_DTE
) &&
318 !(txmac_stat
& ~MAC_TXSTAT_DTE
))
321 if (txmac_stat
& MAC_TXSTAT_URUN
) {
322 netdev_err(dev
, "TX MAC xmit underrun\n");
323 dev
->stats
.tx_fifo_errors
++;
326 if (txmac_stat
& MAC_TXSTAT_MPE
) {
327 netdev_err(dev
, "TX MAC max packet size error\n");
328 dev
->stats
.tx_errors
++;
331 /* The rest are all cases of one of the 16-bit TX
334 if (txmac_stat
& MAC_TXSTAT_NCE
)
335 dev
->stats
.collisions
+= 0x10000;
337 if (txmac_stat
& MAC_TXSTAT_ECE
) {
338 dev
->stats
.tx_aborted_errors
+= 0x10000;
339 dev
->stats
.collisions
+= 0x10000;
342 if (txmac_stat
& MAC_TXSTAT_LCE
) {
343 dev
->stats
.tx_aborted_errors
+= 0x10000;
344 dev
->stats
.collisions
+= 0x10000;
347 /* We do not keep track of MAC_TXSTAT_FCE and
348 * MAC_TXSTAT_PCE events.
353 /* When we get a RX fifo overflow, the RX unit in GEM is probably hung
354 * so we do the following.
356 * If any part of the reset goes wrong, we return 1 and that causes the
357 * whole chip to be reset.
359 static int gem_rxmac_reset(struct gem
*gp
)
361 struct net_device
*dev
= gp
->dev
;
366 /* First, reset & disable MAC RX. */
367 writel(MAC_RXRST_CMD
, gp
->regs
+ MAC_RXRST
);
368 for (limit
= 0; limit
< 5000; limit
++) {
369 if (!(readl(gp
->regs
+ MAC_RXRST
) & MAC_RXRST_CMD
))
374 netdev_err(dev
, "RX MAC will not reset, resetting whole chip\n");
378 writel(gp
->mac_rx_cfg
& ~MAC_RXCFG_ENAB
,
379 gp
->regs
+ MAC_RXCFG
);
380 for (limit
= 0; limit
< 5000; limit
++) {
381 if (!(readl(gp
->regs
+ MAC_RXCFG
) & MAC_RXCFG_ENAB
))
386 netdev_err(dev
, "RX MAC will not disable, resetting whole chip\n");
390 /* Second, disable RX DMA. */
391 writel(0, gp
->regs
+ RXDMA_CFG
);
392 for (limit
= 0; limit
< 5000; limit
++) {
393 if (!(readl(gp
->regs
+ RXDMA_CFG
) & RXDMA_CFG_ENABLE
))
398 netdev_err(dev
, "RX DMA will not disable, resetting whole chip\n");
404 /* Execute RX reset command. */
405 writel(gp
->swrst_base
| GREG_SWRST_RXRST
,
406 gp
->regs
+ GREG_SWRST
);
407 for (limit
= 0; limit
< 5000; limit
++) {
408 if (!(readl(gp
->regs
+ GREG_SWRST
) & GREG_SWRST_RXRST
))
413 netdev_err(dev
, "RX reset command will not execute, resetting whole chip\n");
417 /* Refresh the RX ring. */
418 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
419 struct gem_rxd
*rxd
= &gp
->init_block
->rxd
[i
];
421 if (gp
->rx_skbs
[i
] == NULL
) {
422 netdev_err(dev
, "Parts of RX ring empty, resetting whole chip\n");
426 rxd
->status_word
= cpu_to_le64(RXDCTRL_FRESH(gp
));
428 gp
->rx_new
= gp
->rx_old
= 0;
430 /* Now we must reprogram the rest of RX unit. */
431 desc_dma
= (u64
) gp
->gblock_dvma
;
432 desc_dma
+= (INIT_BLOCK_TX_RING_SIZE
* sizeof(struct gem_txd
));
433 writel(desc_dma
>> 32, gp
->regs
+ RXDMA_DBHI
);
434 writel(desc_dma
& 0xffffffff, gp
->regs
+ RXDMA_DBLOW
);
435 writel(RX_RING_SIZE
- 4, gp
->regs
+ RXDMA_KICK
);
436 val
= (RXDMA_CFG_BASE
| (RX_OFFSET
<< 10) |
437 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128
);
438 writel(val
, gp
->regs
+ RXDMA_CFG
);
439 if (readl(gp
->regs
+ GREG_BIFCFG
) & GREG_BIFCFG_M66EN
)
440 writel(((5 & RXDMA_BLANK_IPKTS
) |
441 ((8 << 12) & RXDMA_BLANK_ITIME
)),
442 gp
->regs
+ RXDMA_BLANK
);
444 writel(((5 & RXDMA_BLANK_IPKTS
) |
445 ((4 << 12) & RXDMA_BLANK_ITIME
)),
446 gp
->regs
+ RXDMA_BLANK
);
447 val
= (((gp
->rx_pause_off
/ 64) << 0) & RXDMA_PTHRESH_OFF
);
448 val
|= (((gp
->rx_pause_on
/ 64) << 12) & RXDMA_PTHRESH_ON
);
449 writel(val
, gp
->regs
+ RXDMA_PTHRESH
);
450 val
= readl(gp
->regs
+ RXDMA_CFG
);
451 writel(val
| RXDMA_CFG_ENABLE
, gp
->regs
+ RXDMA_CFG
);
452 writel(MAC_RXSTAT_RCV
, gp
->regs
+ MAC_RXMASK
);
453 val
= readl(gp
->regs
+ MAC_RXCFG
);
454 writel(val
| MAC_RXCFG_ENAB
, gp
->regs
+ MAC_RXCFG
);
459 static int gem_rxmac_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
461 u32 rxmac_stat
= readl(gp
->regs
+ MAC_RXSTAT
);
464 if (netif_msg_intr(gp
))
465 printk(KERN_DEBUG
"%s: rxmac interrupt, rxmac_stat: 0x%x\n",
466 gp
->dev
->name
, rxmac_stat
);
468 if (rxmac_stat
& MAC_RXSTAT_OFLW
) {
469 u32 smac
= readl(gp
->regs
+ MAC_SMACHINE
);
471 netdev_err(dev
, "RX MAC fifo overflow smac[%08x]\n", smac
);
472 dev
->stats
.rx_over_errors
++;
473 dev
->stats
.rx_fifo_errors
++;
475 ret
= gem_rxmac_reset(gp
);
478 if (rxmac_stat
& MAC_RXSTAT_ACE
)
479 dev
->stats
.rx_frame_errors
+= 0x10000;
481 if (rxmac_stat
& MAC_RXSTAT_CCE
)
482 dev
->stats
.rx_crc_errors
+= 0x10000;
484 if (rxmac_stat
& MAC_RXSTAT_LCE
)
485 dev
->stats
.rx_length_errors
+= 0x10000;
487 /* We do not track MAC_RXSTAT_FCE and MAC_RXSTAT_VCE
493 static int gem_mac_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
495 u32 mac_cstat
= readl(gp
->regs
+ MAC_CSTAT
);
497 if (netif_msg_intr(gp
))
498 printk(KERN_DEBUG
"%s: mac interrupt, mac_cstat: 0x%x\n",
499 gp
->dev
->name
, mac_cstat
);
501 /* This interrupt is just for pause frame and pause
502 * tracking. It is useful for diagnostics and debug
503 * but probably by default we will mask these events.
505 if (mac_cstat
& MAC_CSTAT_PS
)
508 if (mac_cstat
& MAC_CSTAT_PRCV
)
509 gp
->pause_last_time_recvd
= (mac_cstat
>> 16);
514 static int gem_mif_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
516 u32 mif_status
= readl(gp
->regs
+ MIF_STATUS
);
517 u32 reg_val
, changed_bits
;
519 reg_val
= (mif_status
& MIF_STATUS_DATA
) >> 16;
520 changed_bits
= (mif_status
& MIF_STATUS_STAT
);
522 gem_handle_mif_event(gp
, reg_val
, changed_bits
);
527 static int gem_pci_interrupt(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
529 u32 pci_estat
= readl(gp
->regs
+ GREG_PCIESTAT
);
531 if (gp
->pdev
->vendor
== PCI_VENDOR_ID_SUN
&&
532 gp
->pdev
->device
== PCI_DEVICE_ID_SUN_GEM
) {
533 netdev_err(dev
, "PCI error [%04x]", pci_estat
);
535 if (pci_estat
& GREG_PCIESTAT_BADACK
)
536 pr_cont(" <No ACK64# during ABS64 cycle>");
537 if (pci_estat
& GREG_PCIESTAT_DTRTO
)
538 pr_cont(" <Delayed transaction timeout>");
539 if (pci_estat
& GREG_PCIESTAT_OTHER
)
543 pci_estat
|= GREG_PCIESTAT_OTHER
;
544 netdev_err(dev
, "PCI error\n");
547 if (pci_estat
& GREG_PCIESTAT_OTHER
) {
550 /* Interrogate PCI config space for the
553 pci_read_config_word(gp
->pdev
, PCI_STATUS
,
555 netdev_err(dev
, "Read PCI cfg space status [%04x]\n",
557 if (pci_cfg_stat
& PCI_STATUS_PARITY
)
558 netdev_err(dev
, "PCI parity error detected\n");
559 if (pci_cfg_stat
& PCI_STATUS_SIG_TARGET_ABORT
)
560 netdev_err(dev
, "PCI target abort\n");
561 if (pci_cfg_stat
& PCI_STATUS_REC_TARGET_ABORT
)
562 netdev_err(dev
, "PCI master acks target abort\n");
563 if (pci_cfg_stat
& PCI_STATUS_REC_MASTER_ABORT
)
564 netdev_err(dev
, "PCI master abort\n");
565 if (pci_cfg_stat
& PCI_STATUS_SIG_SYSTEM_ERROR
)
566 netdev_err(dev
, "PCI system error SERR#\n");
567 if (pci_cfg_stat
& PCI_STATUS_DETECTED_PARITY
)
568 netdev_err(dev
, "PCI parity error\n");
570 /* Write the error bits back to clear them. */
571 pci_cfg_stat
&= (PCI_STATUS_PARITY
|
572 PCI_STATUS_SIG_TARGET_ABORT
|
573 PCI_STATUS_REC_TARGET_ABORT
|
574 PCI_STATUS_REC_MASTER_ABORT
|
575 PCI_STATUS_SIG_SYSTEM_ERROR
|
576 PCI_STATUS_DETECTED_PARITY
);
577 pci_write_config_word(gp
->pdev
,
578 PCI_STATUS
, pci_cfg_stat
);
581 /* For all PCI errors, we should reset the chip. */
585 /* All non-normal interrupt conditions get serviced here.
586 * Returns non-zero if we should just exit the interrupt
587 * handler right now (ie. if we reset the card which invalidates
588 * all of the other original irq status bits).
590 static int gem_abnormal_irq(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
592 if (gem_status
& GREG_STAT_RXNOBUF
) {
593 /* Frame arrived, no free RX buffers available. */
594 if (netif_msg_rx_err(gp
))
595 printk(KERN_DEBUG
"%s: no buffer for rx frame\n",
597 dev
->stats
.rx_dropped
++;
600 if (gem_status
& GREG_STAT_RXTAGERR
) {
601 /* corrupt RX tag framing */
602 if (netif_msg_rx_err(gp
))
603 printk(KERN_DEBUG
"%s: corrupt rx tag framing\n",
605 dev
->stats
.rx_errors
++;
610 if (gem_status
& GREG_STAT_PCS
) {
611 if (gem_pcs_interrupt(dev
, gp
, gem_status
))
615 if (gem_status
& GREG_STAT_TXMAC
) {
616 if (gem_txmac_interrupt(dev
, gp
, gem_status
))
620 if (gem_status
& GREG_STAT_RXMAC
) {
621 if (gem_rxmac_interrupt(dev
, gp
, gem_status
))
625 if (gem_status
& GREG_STAT_MAC
) {
626 if (gem_mac_interrupt(dev
, gp
, gem_status
))
630 if (gem_status
& GREG_STAT_MIF
) {
631 if (gem_mif_interrupt(dev
, gp
, gem_status
))
635 if (gem_status
& GREG_STAT_PCIERR
) {
636 if (gem_pci_interrupt(dev
, gp
, gem_status
))
643 static __inline__
void gem_tx(struct net_device
*dev
, struct gem
*gp
, u32 gem_status
)
648 limit
= ((gem_status
& GREG_STAT_TXNR
) >> GREG_STAT_TXNR_SHIFT
);
649 while (entry
!= limit
) {
656 if (netif_msg_tx_done(gp
))
657 printk(KERN_DEBUG
"%s: tx done, slot %d\n",
658 gp
->dev
->name
, entry
);
659 skb
= gp
->tx_skbs
[entry
];
660 if (skb_shinfo(skb
)->nr_frags
) {
661 int last
= entry
+ skb_shinfo(skb
)->nr_frags
;
665 last
&= (TX_RING_SIZE
- 1);
667 walk
= NEXT_TX(walk
);
676 gp
->tx_skbs
[entry
] = NULL
;
677 dev
->stats
.tx_bytes
+= skb
->len
;
679 for (frag
= 0; frag
<= skb_shinfo(skb
)->nr_frags
; frag
++) {
680 txd
= &gp
->init_block
->txd
[entry
];
682 dma_addr
= le64_to_cpu(txd
->buffer
);
683 dma_len
= le64_to_cpu(txd
->control_word
) & TXDCTRL_BUFSZ
;
685 pci_unmap_page(gp
->pdev
, dma_addr
, dma_len
, PCI_DMA_TODEVICE
);
686 entry
= NEXT_TX(entry
);
689 dev
->stats
.tx_packets
++;
690 dev_consume_skb_any(skb
);
694 /* Need to make the tx_old update visible to gem_start_xmit()
695 * before checking for netif_queue_stopped(). Without the
696 * memory barrier, there is a small possibility that gem_start_xmit()
697 * will miss it and cause the queue to be stopped forever.
701 if (unlikely(netif_queue_stopped(dev
) &&
702 TX_BUFFS_AVAIL(gp
) > (MAX_SKB_FRAGS
+ 1))) {
703 struct netdev_queue
*txq
= netdev_get_tx_queue(dev
, 0);
705 __netif_tx_lock(txq
, smp_processor_id());
706 if (netif_queue_stopped(dev
) &&
707 TX_BUFFS_AVAIL(gp
) > (MAX_SKB_FRAGS
+ 1))
708 netif_wake_queue(dev
);
709 __netif_tx_unlock(txq
);
713 static __inline__
void gem_post_rxds(struct gem
*gp
, int limit
)
715 int cluster_start
, curr
, count
, kick
;
717 cluster_start
= curr
= (gp
->rx_new
& ~(4 - 1));
721 while (curr
!= limit
) {
722 curr
= NEXT_RX(curr
);
724 struct gem_rxd
*rxd
=
725 &gp
->init_block
->rxd
[cluster_start
];
727 rxd
->status_word
= cpu_to_le64(RXDCTRL_FRESH(gp
));
729 cluster_start
= NEXT_RX(cluster_start
);
730 if (cluster_start
== curr
)
739 writel(kick
, gp
->regs
+ RXDMA_KICK
);
743 #define ALIGNED_RX_SKB_ADDR(addr) \
744 ((((unsigned long)(addr) + (64UL - 1UL)) & ~(64UL - 1UL)) - (unsigned long)(addr))
745 static __inline__
struct sk_buff
*gem_alloc_skb(struct net_device
*dev
, int size
,
748 struct sk_buff
*skb
= alloc_skb(size
+ 64, gfp_flags
);
751 unsigned long offset
= ALIGNED_RX_SKB_ADDR(skb
->data
);
752 skb_reserve(skb
, offset
);
757 static int gem_rx(struct gem
*gp
, int work_to_do
)
759 struct net_device
*dev
= gp
->dev
;
760 int entry
, drops
, work_done
= 0;
764 if (netif_msg_rx_status(gp
))
765 printk(KERN_DEBUG
"%s: rx interrupt, done: %d, rx_new: %d\n",
766 gp
->dev
->name
, readl(gp
->regs
+ RXDMA_DONE
), gp
->rx_new
);
770 done
= readl(gp
->regs
+ RXDMA_DONE
);
772 struct gem_rxd
*rxd
= &gp
->init_block
->rxd
[entry
];
774 u64 status
= le64_to_cpu(rxd
->status_word
);
778 if ((status
& RXDCTRL_OWN
) != 0)
781 if (work_done
>= RX_RING_SIZE
|| work_done
>= work_to_do
)
784 /* When writing back RX descriptor, GEM writes status
785 * then buffer address, possibly in separate transactions.
786 * If we don't wait for the chip to write both, we could
787 * post a new buffer to this descriptor then have GEM spam
788 * on the buffer address. We sync on the RX completion
789 * register to prevent this from happening.
792 done
= readl(gp
->regs
+ RXDMA_DONE
);
797 /* We can now account for the work we're about to do */
800 skb
= gp
->rx_skbs
[entry
];
802 len
= (status
& RXDCTRL_BUFSZ
) >> 16;
803 if ((len
< ETH_ZLEN
) || (status
& RXDCTRL_BAD
)) {
804 dev
->stats
.rx_errors
++;
806 dev
->stats
.rx_length_errors
++;
807 if (len
& RXDCTRL_BAD
)
808 dev
->stats
.rx_crc_errors
++;
810 /* We'll just return it to GEM. */
812 dev
->stats
.rx_dropped
++;
816 dma_addr
= le64_to_cpu(rxd
->buffer
);
817 if (len
> RX_COPY_THRESHOLD
) {
818 struct sk_buff
*new_skb
;
820 new_skb
= gem_alloc_skb(dev
, RX_BUF_ALLOC_SIZE(gp
), GFP_ATOMIC
);
821 if (new_skb
== NULL
) {
825 pci_unmap_page(gp
->pdev
, dma_addr
,
826 RX_BUF_ALLOC_SIZE(gp
),
828 gp
->rx_skbs
[entry
] = new_skb
;
829 skb_put(new_skb
, (gp
->rx_buf_sz
+ RX_OFFSET
));
830 rxd
->buffer
= cpu_to_le64(pci_map_page(gp
->pdev
,
831 virt_to_page(new_skb
->data
),
832 offset_in_page(new_skb
->data
),
833 RX_BUF_ALLOC_SIZE(gp
),
834 PCI_DMA_FROMDEVICE
));
835 skb_reserve(new_skb
, RX_OFFSET
);
837 /* Trim the original skb for the netif. */
840 struct sk_buff
*copy_skb
= netdev_alloc_skb(dev
, len
+ 2);
842 if (copy_skb
== NULL
) {
847 skb_reserve(copy_skb
, 2);
848 skb_put(copy_skb
, len
);
849 pci_dma_sync_single_for_cpu(gp
->pdev
, dma_addr
, len
, PCI_DMA_FROMDEVICE
);
850 skb_copy_from_linear_data(skb
, copy_skb
->data
, len
);
851 pci_dma_sync_single_for_device(gp
->pdev
, dma_addr
, len
, PCI_DMA_FROMDEVICE
);
853 /* We'll reuse the original ring buffer. */
857 csum
= (__force __sum16
)htons((status
& RXDCTRL_TCPCSUM
) ^ 0xffff);
858 skb
->csum
= csum_unfold(csum
);
859 skb
->ip_summed
= CHECKSUM_COMPLETE
;
860 skb
->protocol
= eth_type_trans(skb
, gp
->dev
);
862 napi_gro_receive(&gp
->napi
, skb
);
864 dev
->stats
.rx_packets
++;
865 dev
->stats
.rx_bytes
+= len
;
868 entry
= NEXT_RX(entry
);
871 gem_post_rxds(gp
, entry
);
876 netdev_info(gp
->dev
, "Memory squeeze, deferring packet\n");
881 static int gem_poll(struct napi_struct
*napi
, int budget
)
883 struct gem
*gp
= container_of(napi
, struct gem
, napi
);
884 struct net_device
*dev
= gp
->dev
;
889 /* Handle anomalies */
890 if (unlikely(gp
->status
& GREG_STAT_ABNORMAL
)) {
891 struct netdev_queue
*txq
= netdev_get_tx_queue(dev
, 0);
894 /* We run the abnormal interrupt handling code with
895 * the Tx lock. It only resets the Rx portion of the
896 * chip, but we need to guard it against DMA being
897 * restarted by the link poll timer
899 __netif_tx_lock(txq
, smp_processor_id());
900 reset
= gem_abnormal_irq(dev
, gp
, gp
->status
);
901 __netif_tx_unlock(txq
);
903 gem_schedule_reset(gp
);
909 /* Run TX completion thread */
910 gem_tx(dev
, gp
, gp
->status
);
912 /* Run RX thread. We don't use any locking here,
913 * code willing to do bad things - like cleaning the
914 * rx ring - must call napi_disable(), which
915 * schedule_timeout()'s if polling is already disabled.
917 work_done
+= gem_rx(gp
, budget
- work_done
);
919 if (work_done
>= budget
)
922 gp
->status
= readl(gp
->regs
+ GREG_STAT
);
923 } while (gp
->status
& GREG_STAT_NAPI
);
925 napi_complete_done(napi
, work_done
);
931 static irqreturn_t
gem_interrupt(int irq
, void *dev_id
)
933 struct net_device
*dev
= dev_id
;
934 struct gem
*gp
= netdev_priv(dev
);
936 if (napi_schedule_prep(&gp
->napi
)) {
937 u32 gem_status
= readl(gp
->regs
+ GREG_STAT
);
939 if (unlikely(gem_status
== 0)) {
940 napi_enable(&gp
->napi
);
943 if (netif_msg_intr(gp
))
944 printk(KERN_DEBUG
"%s: gem_interrupt() gem_status: 0x%x\n",
945 gp
->dev
->name
, gem_status
);
947 gp
->status
= gem_status
;
948 gem_disable_ints(gp
);
949 __napi_schedule(&gp
->napi
);
952 /* If polling was disabled at the time we received that
953 * interrupt, we may return IRQ_HANDLED here while we
954 * should return IRQ_NONE. No big deal...
959 #ifdef CONFIG_NET_POLL_CONTROLLER
960 static void gem_poll_controller(struct net_device
*dev
)
962 struct gem
*gp
= netdev_priv(dev
);
964 disable_irq(gp
->pdev
->irq
);
965 gem_interrupt(gp
->pdev
->irq
, dev
);
966 enable_irq(gp
->pdev
->irq
);
970 static void gem_tx_timeout(struct net_device
*dev
)
972 struct gem
*gp
= netdev_priv(dev
);
974 netdev_err(dev
, "transmit timed out, resetting\n");
976 netdev_err(dev
, "TX_STATE[%08x:%08x:%08x]\n",
977 readl(gp
->regs
+ TXDMA_CFG
),
978 readl(gp
->regs
+ MAC_TXSTAT
),
979 readl(gp
->regs
+ MAC_TXCFG
));
980 netdev_err(dev
, "RX_STATE[%08x:%08x:%08x]\n",
981 readl(gp
->regs
+ RXDMA_CFG
),
982 readl(gp
->regs
+ MAC_RXSTAT
),
983 readl(gp
->regs
+ MAC_RXCFG
));
985 gem_schedule_reset(gp
);
988 static __inline__
int gem_intme(int entry
)
990 /* Algorithm: IRQ every 1/2 of descriptors. */
991 if (!(entry
& ((TX_RING_SIZE
>>1)-1)))
997 static netdev_tx_t
gem_start_xmit(struct sk_buff
*skb
,
998 struct net_device
*dev
)
1000 struct gem
*gp
= netdev_priv(dev
);
1005 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1006 const u64 csum_start_off
= skb_checksum_start_offset(skb
);
1007 const u64 csum_stuff_off
= csum_start_off
+ skb
->csum_offset
;
1009 ctrl
= (TXDCTRL_CENAB
|
1010 (csum_start_off
<< 15) |
1011 (csum_stuff_off
<< 21));
1014 if (unlikely(TX_BUFFS_AVAIL(gp
) <= (skb_shinfo(skb
)->nr_frags
+ 1))) {
1015 /* This is a hard error, log it. */
1016 if (!netif_queue_stopped(dev
)) {
1017 netif_stop_queue(dev
);
1018 netdev_err(dev
, "BUG! Tx Ring full when queue awake!\n");
1020 return NETDEV_TX_BUSY
;
1024 gp
->tx_skbs
[entry
] = skb
;
1026 if (skb_shinfo(skb
)->nr_frags
== 0) {
1027 struct gem_txd
*txd
= &gp
->init_block
->txd
[entry
];
1032 mapping
= pci_map_page(gp
->pdev
,
1033 virt_to_page(skb
->data
),
1034 offset_in_page(skb
->data
),
1035 len
, PCI_DMA_TODEVICE
);
1036 ctrl
|= TXDCTRL_SOF
| TXDCTRL_EOF
| len
;
1037 if (gem_intme(entry
))
1038 ctrl
|= TXDCTRL_INTME
;
1039 txd
->buffer
= cpu_to_le64(mapping
);
1041 txd
->control_word
= cpu_to_le64(ctrl
);
1042 entry
= NEXT_TX(entry
);
1044 struct gem_txd
*txd
;
1047 dma_addr_t first_mapping
;
1048 int frag
, first_entry
= entry
;
1051 if (gem_intme(entry
))
1052 intme
|= TXDCTRL_INTME
;
1054 /* We must give this initial chunk to the device last.
1055 * Otherwise we could race with the device.
1057 first_len
= skb_headlen(skb
);
1058 first_mapping
= pci_map_page(gp
->pdev
, virt_to_page(skb
->data
),
1059 offset_in_page(skb
->data
),
1060 first_len
, PCI_DMA_TODEVICE
);
1061 entry
= NEXT_TX(entry
);
1063 for (frag
= 0; frag
< skb_shinfo(skb
)->nr_frags
; frag
++) {
1064 const skb_frag_t
*this_frag
= &skb_shinfo(skb
)->frags
[frag
];
1069 len
= skb_frag_size(this_frag
);
1070 mapping
= skb_frag_dma_map(&gp
->pdev
->dev
, this_frag
,
1071 0, len
, DMA_TO_DEVICE
);
1073 if (frag
== skb_shinfo(skb
)->nr_frags
- 1)
1074 this_ctrl
|= TXDCTRL_EOF
;
1076 txd
= &gp
->init_block
->txd
[entry
];
1077 txd
->buffer
= cpu_to_le64(mapping
);
1079 txd
->control_word
= cpu_to_le64(this_ctrl
| len
);
1081 if (gem_intme(entry
))
1082 intme
|= TXDCTRL_INTME
;
1084 entry
= NEXT_TX(entry
);
1086 txd
= &gp
->init_block
->txd
[first_entry
];
1087 txd
->buffer
= cpu_to_le64(first_mapping
);
1090 cpu_to_le64(ctrl
| TXDCTRL_SOF
| intme
| first_len
);
1094 if (unlikely(TX_BUFFS_AVAIL(gp
) <= (MAX_SKB_FRAGS
+ 1))) {
1095 netif_stop_queue(dev
);
1097 /* netif_stop_queue() must be done before checking
1098 * checking tx index in TX_BUFFS_AVAIL() below, because
1099 * in gem_tx(), we update tx_old before checking for
1100 * netif_queue_stopped().
1103 if (TX_BUFFS_AVAIL(gp
) > (MAX_SKB_FRAGS
+ 1))
1104 netif_wake_queue(dev
);
1106 if (netif_msg_tx_queued(gp
))
1107 printk(KERN_DEBUG
"%s: tx queued, slot %d, skblen %d\n",
1108 dev
->name
, entry
, skb
->len
);
1110 writel(gp
->tx_new
, gp
->regs
+ TXDMA_KICK
);
1112 return NETDEV_TX_OK
;
1115 static void gem_pcs_reset(struct gem
*gp
)
1120 /* Reset PCS unit. */
1121 val
= readl(gp
->regs
+ PCS_MIICTRL
);
1122 val
|= PCS_MIICTRL_RST
;
1123 writel(val
, gp
->regs
+ PCS_MIICTRL
);
1126 while (readl(gp
->regs
+ PCS_MIICTRL
) & PCS_MIICTRL_RST
) {
1132 netdev_warn(gp
->dev
, "PCS reset bit would not clear\n");
1135 static void gem_pcs_reinit_adv(struct gem
*gp
)
1139 /* Make sure PCS is disabled while changing advertisement
1142 val
= readl(gp
->regs
+ PCS_CFG
);
1143 val
&= ~(PCS_CFG_ENABLE
| PCS_CFG_TO
);
1144 writel(val
, gp
->regs
+ PCS_CFG
);
1146 /* Advertise all capabilities except asymmetric
1149 val
= readl(gp
->regs
+ PCS_MIIADV
);
1150 val
|= (PCS_MIIADV_FD
| PCS_MIIADV_HD
|
1151 PCS_MIIADV_SP
| PCS_MIIADV_AP
);
1152 writel(val
, gp
->regs
+ PCS_MIIADV
);
1154 /* Enable and restart auto-negotiation, disable wrapback/loopback,
1155 * and re-enable PCS.
1157 val
= readl(gp
->regs
+ PCS_MIICTRL
);
1158 val
|= (PCS_MIICTRL_RAN
| PCS_MIICTRL_ANE
);
1159 val
&= ~PCS_MIICTRL_WB
;
1160 writel(val
, gp
->regs
+ PCS_MIICTRL
);
1162 val
= readl(gp
->regs
+ PCS_CFG
);
1163 val
|= PCS_CFG_ENABLE
;
1164 writel(val
, gp
->regs
+ PCS_CFG
);
1166 /* Make sure serialink loopback is off. The meaning
1167 * of this bit is logically inverted based upon whether
1168 * you are in Serialink or SERDES mode.
1170 val
= readl(gp
->regs
+ PCS_SCTRL
);
1171 if (gp
->phy_type
== phy_serialink
)
1172 val
&= ~PCS_SCTRL_LOOP
;
1174 val
|= PCS_SCTRL_LOOP
;
1175 writel(val
, gp
->regs
+ PCS_SCTRL
);
1178 #define STOP_TRIES 32
1180 static void gem_reset(struct gem
*gp
)
1185 /* Make sure we won't get any more interrupts */
1186 writel(0xffffffff, gp
->regs
+ GREG_IMASK
);
1188 /* Reset the chip */
1189 writel(gp
->swrst_base
| GREG_SWRST_TXRST
| GREG_SWRST_RXRST
,
1190 gp
->regs
+ GREG_SWRST
);
1196 val
= readl(gp
->regs
+ GREG_SWRST
);
1199 } while (val
& (GREG_SWRST_TXRST
| GREG_SWRST_RXRST
));
1202 netdev_err(gp
->dev
, "SW reset is ghetto\n");
1204 if (gp
->phy_type
== phy_serialink
|| gp
->phy_type
== phy_serdes
)
1205 gem_pcs_reinit_adv(gp
);
1208 static void gem_start_dma(struct gem
*gp
)
1212 /* We are ready to rock, turn everything on. */
1213 val
= readl(gp
->regs
+ TXDMA_CFG
);
1214 writel(val
| TXDMA_CFG_ENABLE
, gp
->regs
+ TXDMA_CFG
);
1215 val
= readl(gp
->regs
+ RXDMA_CFG
);
1216 writel(val
| RXDMA_CFG_ENABLE
, gp
->regs
+ RXDMA_CFG
);
1217 val
= readl(gp
->regs
+ MAC_TXCFG
);
1218 writel(val
| MAC_TXCFG_ENAB
, gp
->regs
+ MAC_TXCFG
);
1219 val
= readl(gp
->regs
+ MAC_RXCFG
);
1220 writel(val
| MAC_RXCFG_ENAB
, gp
->regs
+ MAC_RXCFG
);
1222 (void) readl(gp
->regs
+ MAC_RXCFG
);
1225 gem_enable_ints(gp
);
1227 writel(RX_RING_SIZE
- 4, gp
->regs
+ RXDMA_KICK
);
1230 /* DMA won't be actually stopped before about 4ms tho ...
1232 static void gem_stop_dma(struct gem
*gp
)
1236 /* We are done rocking, turn everything off. */
1237 val
= readl(gp
->regs
+ TXDMA_CFG
);
1238 writel(val
& ~TXDMA_CFG_ENABLE
, gp
->regs
+ TXDMA_CFG
);
1239 val
= readl(gp
->regs
+ RXDMA_CFG
);
1240 writel(val
& ~RXDMA_CFG_ENABLE
, gp
->regs
+ RXDMA_CFG
);
1241 val
= readl(gp
->regs
+ MAC_TXCFG
);
1242 writel(val
& ~MAC_TXCFG_ENAB
, gp
->regs
+ MAC_TXCFG
);
1243 val
= readl(gp
->regs
+ MAC_RXCFG
);
1244 writel(val
& ~MAC_RXCFG_ENAB
, gp
->regs
+ MAC_RXCFG
);
1246 (void) readl(gp
->regs
+ MAC_RXCFG
);
1248 /* Need to wait a bit ... done by the caller */
1252 // XXX dbl check what that function should do when called on PCS PHY
1253 static void gem_begin_auto_negotiation(struct gem
*gp
,
1254 const struct ethtool_link_ksettings
*ep
)
1256 u32 advertise
, features
;
1263 ethtool_convert_link_mode_to_legacy_u32(
1264 &advertising
, ep
->link_modes
.advertising
);
1266 if (gp
->phy_type
!= phy_mii_mdio0
&&
1267 gp
->phy_type
!= phy_mii_mdio1
)
1270 /* Setup advertise */
1271 if (found_mii_phy(gp
))
1272 features
= gp
->phy_mii
.def
->features
;
1276 advertise
= features
& ADVERTISE_MASK
;
1277 if (gp
->phy_mii
.advertising
!= 0)
1278 advertise
&= gp
->phy_mii
.advertising
;
1280 autoneg
= gp
->want_autoneg
;
1281 speed
= gp
->phy_mii
.speed
;
1282 duplex
= gp
->phy_mii
.duplex
;
1284 /* Setup link parameters */
1287 if (ep
->base
.autoneg
== AUTONEG_ENABLE
) {
1288 advertise
= advertising
;
1292 speed
= ep
->base
.speed
;
1293 duplex
= ep
->base
.duplex
;
1297 /* Sanitize settings based on PHY capabilities */
1298 if ((features
& SUPPORTED_Autoneg
) == 0)
1300 if (speed
== SPEED_1000
&&
1301 !(features
& (SUPPORTED_1000baseT_Half
| SUPPORTED_1000baseT_Full
)))
1303 if (speed
== SPEED_100
&&
1304 !(features
& (SUPPORTED_100baseT_Half
| SUPPORTED_100baseT_Full
)))
1306 if (duplex
== DUPLEX_FULL
&&
1307 !(features
& (SUPPORTED_1000baseT_Full
|
1308 SUPPORTED_100baseT_Full
|
1309 SUPPORTED_10baseT_Full
)))
1310 duplex
= DUPLEX_HALF
;
1314 /* If we are asleep, we don't try to actually setup the PHY, we
1315 * just store the settings
1317 if (!netif_device_present(gp
->dev
)) {
1318 gp
->phy_mii
.autoneg
= gp
->want_autoneg
= autoneg
;
1319 gp
->phy_mii
.speed
= speed
;
1320 gp
->phy_mii
.duplex
= duplex
;
1324 /* Configure PHY & start aneg */
1325 gp
->want_autoneg
= autoneg
;
1327 if (found_mii_phy(gp
))
1328 gp
->phy_mii
.def
->ops
->setup_aneg(&gp
->phy_mii
, advertise
);
1329 gp
->lstate
= link_aneg
;
1331 if (found_mii_phy(gp
))
1332 gp
->phy_mii
.def
->ops
->setup_forced(&gp
->phy_mii
, speed
, duplex
);
1333 gp
->lstate
= link_force_ok
;
1337 gp
->timer_ticks
= 0;
1338 mod_timer(&gp
->link_timer
, jiffies
+ ((12 * HZ
) / 10));
1341 /* A link-up condition has occurred, initialize and enable the
1344 static int gem_set_link_modes(struct gem
*gp
)
1346 struct netdev_queue
*txq
= netdev_get_tx_queue(gp
->dev
, 0);
1347 int full_duplex
, speed
, pause
;
1354 if (found_mii_phy(gp
)) {
1355 if (gp
->phy_mii
.def
->ops
->read_link(&gp
->phy_mii
))
1357 full_duplex
= (gp
->phy_mii
.duplex
== DUPLEX_FULL
);
1358 speed
= gp
->phy_mii
.speed
;
1359 pause
= gp
->phy_mii
.pause
;
1360 } else if (gp
->phy_type
== phy_serialink
||
1361 gp
->phy_type
== phy_serdes
) {
1362 u32 pcs_lpa
= readl(gp
->regs
+ PCS_MIILP
);
1364 if ((pcs_lpa
& PCS_MIIADV_FD
) || gp
->phy_type
== phy_serdes
)
1369 netif_info(gp
, link
, gp
->dev
, "Link is up at %d Mbps, %s-duplex\n",
1370 speed
, (full_duplex
? "full" : "half"));
1373 /* We take the tx queue lock to avoid collisions between
1374 * this code, the tx path and the NAPI-driven error path
1376 __netif_tx_lock(txq
, smp_processor_id());
1378 val
= (MAC_TXCFG_EIPG0
| MAC_TXCFG_NGU
);
1380 val
|= (MAC_TXCFG_ICS
| MAC_TXCFG_ICOLL
);
1382 /* MAC_TXCFG_NBO must be zero. */
1384 writel(val
, gp
->regs
+ MAC_TXCFG
);
1386 val
= (MAC_XIFCFG_OE
| MAC_XIFCFG_LLED
);
1388 (gp
->phy_type
== phy_mii_mdio0
||
1389 gp
->phy_type
== phy_mii_mdio1
)) {
1390 val
|= MAC_XIFCFG_DISE
;
1391 } else if (full_duplex
) {
1392 val
|= MAC_XIFCFG_FLED
;
1395 if (speed
== SPEED_1000
)
1396 val
|= (MAC_XIFCFG_GMII
);
1398 writel(val
, gp
->regs
+ MAC_XIFCFG
);
1400 /* If gigabit and half-duplex, enable carrier extension
1401 * mode. Else, disable it.
1403 if (speed
== SPEED_1000
&& !full_duplex
) {
1404 val
= readl(gp
->regs
+ MAC_TXCFG
);
1405 writel(val
| MAC_TXCFG_TCE
, gp
->regs
+ MAC_TXCFG
);
1407 val
= readl(gp
->regs
+ MAC_RXCFG
);
1408 writel(val
| MAC_RXCFG_RCE
, gp
->regs
+ MAC_RXCFG
);
1410 val
= readl(gp
->regs
+ MAC_TXCFG
);
1411 writel(val
& ~MAC_TXCFG_TCE
, gp
->regs
+ MAC_TXCFG
);
1413 val
= readl(gp
->regs
+ MAC_RXCFG
);
1414 writel(val
& ~MAC_RXCFG_RCE
, gp
->regs
+ MAC_RXCFG
);
1417 if (gp
->phy_type
== phy_serialink
||
1418 gp
->phy_type
== phy_serdes
) {
1419 u32 pcs_lpa
= readl(gp
->regs
+ PCS_MIILP
);
1421 if (pcs_lpa
& (PCS_MIIADV_SP
| PCS_MIIADV_AP
))
1426 writel(512, gp
->regs
+ MAC_STIME
);
1428 writel(64, gp
->regs
+ MAC_STIME
);
1429 val
= readl(gp
->regs
+ MAC_MCCFG
);
1431 val
|= (MAC_MCCFG_SPE
| MAC_MCCFG_RPE
);
1433 val
&= ~(MAC_MCCFG_SPE
| MAC_MCCFG_RPE
);
1434 writel(val
, gp
->regs
+ MAC_MCCFG
);
1438 __netif_tx_unlock(txq
);
1440 if (netif_msg_link(gp
)) {
1442 netdev_info(gp
->dev
,
1443 "Pause is enabled (rxfifo: %d off: %d on: %d)\n",
1448 netdev_info(gp
->dev
, "Pause is disabled\n");
1455 static int gem_mdio_link_not_up(struct gem
*gp
)
1457 switch (gp
->lstate
) {
1458 case link_force_ret
:
1459 netif_info(gp
, link
, gp
->dev
,
1460 "Autoneg failed again, keeping forced mode\n");
1461 gp
->phy_mii
.def
->ops
->setup_forced(&gp
->phy_mii
,
1462 gp
->last_forced_speed
, DUPLEX_HALF
);
1463 gp
->timer_ticks
= 5;
1464 gp
->lstate
= link_force_ok
;
1467 /* We try forced modes after a failed aneg only on PHYs that don't
1468 * have "magic_aneg" bit set, which means they internally do the
1469 * while forced-mode thingy. On these, we just restart aneg
1471 if (gp
->phy_mii
.def
->magic_aneg
)
1473 netif_info(gp
, link
, gp
->dev
, "switching to forced 100bt\n");
1474 /* Try forced modes. */
1475 gp
->phy_mii
.def
->ops
->setup_forced(&gp
->phy_mii
, SPEED_100
,
1477 gp
->timer_ticks
= 5;
1478 gp
->lstate
= link_force_try
;
1480 case link_force_try
:
1481 /* Downgrade from 100 to 10 Mbps if necessary.
1482 * If already at 10Mbps, warn user about the
1483 * situation every 10 ticks.
1485 if (gp
->phy_mii
.speed
== SPEED_100
) {
1486 gp
->phy_mii
.def
->ops
->setup_forced(&gp
->phy_mii
, SPEED_10
,
1488 gp
->timer_ticks
= 5;
1489 netif_info(gp
, link
, gp
->dev
,
1490 "switching to forced 10bt\n");
1499 static void gem_link_timer(struct timer_list
*t
)
1501 struct gem
*gp
= from_timer(gp
, t
, link_timer
);
1502 struct net_device
*dev
= gp
->dev
;
1503 int restart_aneg
= 0;
1505 /* There's no point doing anything if we're going to be reset */
1506 if (gp
->reset_task_pending
)
1509 if (gp
->phy_type
== phy_serialink
||
1510 gp
->phy_type
== phy_serdes
) {
1511 u32 val
= readl(gp
->regs
+ PCS_MIISTAT
);
1513 if (!(val
& PCS_MIISTAT_LS
))
1514 val
= readl(gp
->regs
+ PCS_MIISTAT
);
1516 if ((val
& PCS_MIISTAT_LS
) != 0) {
1517 if (gp
->lstate
== link_up
)
1520 gp
->lstate
= link_up
;
1521 netif_carrier_on(dev
);
1522 (void)gem_set_link_modes(gp
);
1526 if (found_mii_phy(gp
) && gp
->phy_mii
.def
->ops
->poll_link(&gp
->phy_mii
)) {
1527 /* Ok, here we got a link. If we had it due to a forced
1528 * fallback, and we were configured for autoneg, we do
1529 * retry a short autoneg pass. If you know your hub is
1530 * broken, use ethtool ;)
1532 if (gp
->lstate
== link_force_try
&& gp
->want_autoneg
) {
1533 gp
->lstate
= link_force_ret
;
1534 gp
->last_forced_speed
= gp
->phy_mii
.speed
;
1535 gp
->timer_ticks
= 5;
1536 if (netif_msg_link(gp
))
1538 "Got link after fallback, retrying autoneg once...\n");
1539 gp
->phy_mii
.def
->ops
->setup_aneg(&gp
->phy_mii
, gp
->phy_mii
.advertising
);
1540 } else if (gp
->lstate
!= link_up
) {
1541 gp
->lstate
= link_up
;
1542 netif_carrier_on(dev
);
1543 if (gem_set_link_modes(gp
))
1547 /* If the link was previously up, we restart the
1550 if (gp
->lstate
== link_up
) {
1551 gp
->lstate
= link_down
;
1552 netif_info(gp
, link
, dev
, "Link down\n");
1553 netif_carrier_off(dev
);
1554 gem_schedule_reset(gp
);
1555 /* The reset task will restart the timer */
1557 } else if (++gp
->timer_ticks
> 10) {
1558 if (found_mii_phy(gp
))
1559 restart_aneg
= gem_mdio_link_not_up(gp
);
1565 gem_begin_auto_negotiation(gp
, NULL
);
1569 mod_timer(&gp
->link_timer
, jiffies
+ ((12 * HZ
) / 10));
1572 static void gem_clean_rings(struct gem
*gp
)
1574 struct gem_init_block
*gb
= gp
->init_block
;
1575 struct sk_buff
*skb
;
1577 dma_addr_t dma_addr
;
1579 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1580 struct gem_rxd
*rxd
;
1583 if (gp
->rx_skbs
[i
] != NULL
) {
1584 skb
= gp
->rx_skbs
[i
];
1585 dma_addr
= le64_to_cpu(rxd
->buffer
);
1586 pci_unmap_page(gp
->pdev
, dma_addr
,
1587 RX_BUF_ALLOC_SIZE(gp
),
1588 PCI_DMA_FROMDEVICE
);
1589 dev_kfree_skb_any(skb
);
1590 gp
->rx_skbs
[i
] = NULL
;
1592 rxd
->status_word
= 0;
1597 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1598 if (gp
->tx_skbs
[i
] != NULL
) {
1599 struct gem_txd
*txd
;
1602 skb
= gp
->tx_skbs
[i
];
1603 gp
->tx_skbs
[i
] = NULL
;
1605 for (frag
= 0; frag
<= skb_shinfo(skb
)->nr_frags
; frag
++) {
1606 int ent
= i
& (TX_RING_SIZE
- 1);
1608 txd
= &gb
->txd
[ent
];
1609 dma_addr
= le64_to_cpu(txd
->buffer
);
1610 pci_unmap_page(gp
->pdev
, dma_addr
,
1611 le64_to_cpu(txd
->control_word
) &
1612 TXDCTRL_BUFSZ
, PCI_DMA_TODEVICE
);
1614 if (frag
!= skb_shinfo(skb
)->nr_frags
)
1617 dev_kfree_skb_any(skb
);
1622 static void gem_init_rings(struct gem
*gp
)
1624 struct gem_init_block
*gb
= gp
->init_block
;
1625 struct net_device
*dev
= gp
->dev
;
1627 dma_addr_t dma_addr
;
1629 gp
->rx_new
= gp
->rx_old
= gp
->tx_new
= gp
->tx_old
= 0;
1631 gem_clean_rings(gp
);
1633 gp
->rx_buf_sz
= max(dev
->mtu
+ ETH_HLEN
+ VLAN_HLEN
,
1634 (unsigned)VLAN_ETH_FRAME_LEN
);
1636 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1637 struct sk_buff
*skb
;
1638 struct gem_rxd
*rxd
= &gb
->rxd
[i
];
1640 skb
= gem_alloc_skb(dev
, RX_BUF_ALLOC_SIZE(gp
), GFP_KERNEL
);
1643 rxd
->status_word
= 0;
1647 gp
->rx_skbs
[i
] = skb
;
1648 skb_put(skb
, (gp
->rx_buf_sz
+ RX_OFFSET
));
1649 dma_addr
= pci_map_page(gp
->pdev
,
1650 virt_to_page(skb
->data
),
1651 offset_in_page(skb
->data
),
1652 RX_BUF_ALLOC_SIZE(gp
),
1653 PCI_DMA_FROMDEVICE
);
1654 rxd
->buffer
= cpu_to_le64(dma_addr
);
1656 rxd
->status_word
= cpu_to_le64(RXDCTRL_FRESH(gp
));
1657 skb_reserve(skb
, RX_OFFSET
);
1660 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1661 struct gem_txd
*txd
= &gb
->txd
[i
];
1663 txd
->control_word
= 0;
1670 /* Init PHY interface and start link poll state machine */
1671 static void gem_init_phy(struct gem
*gp
)
1675 /* Revert MIF CFG setting done on stop_phy */
1676 mifcfg
= readl(gp
->regs
+ MIF_CFG
);
1677 mifcfg
&= ~MIF_CFG_BBMODE
;
1678 writel(mifcfg
, gp
->regs
+ MIF_CFG
);
1680 if (gp
->pdev
->vendor
== PCI_VENDOR_ID_APPLE
) {
1683 /* Those delay sucks, the HW seem to love them though, I'll
1684 * serisouly consider breaking some locks here to be able
1685 * to schedule instead
1687 for (i
= 0; i
< 3; i
++) {
1688 #ifdef CONFIG_PPC_PMAC
1689 pmac_call_feature(PMAC_FTR_GMAC_PHY_RESET
, gp
->of_node
, 0, 0);
1692 /* Some PHYs used by apple have problem getting back to us,
1693 * we do an additional reset here
1695 sungem_phy_write(gp
, MII_BMCR
, BMCR_RESET
);
1697 if (sungem_phy_read(gp
, MII_BMCR
) != 0xffff)
1700 netdev_warn(gp
->dev
, "GMAC PHY not responding !\n");
1704 if (gp
->pdev
->vendor
== PCI_VENDOR_ID_SUN
&&
1705 gp
->pdev
->device
== PCI_DEVICE_ID_SUN_GEM
) {
1708 /* Init datapath mode register. */
1709 if (gp
->phy_type
== phy_mii_mdio0
||
1710 gp
->phy_type
== phy_mii_mdio1
) {
1711 val
= PCS_DMODE_MGM
;
1712 } else if (gp
->phy_type
== phy_serialink
) {
1713 val
= PCS_DMODE_SM
| PCS_DMODE_GMOE
;
1715 val
= PCS_DMODE_ESM
;
1718 writel(val
, gp
->regs
+ PCS_DMODE
);
1721 if (gp
->phy_type
== phy_mii_mdio0
||
1722 gp
->phy_type
== phy_mii_mdio1
) {
1723 /* Reset and detect MII PHY */
1724 sungem_phy_probe(&gp
->phy_mii
, gp
->mii_phy_addr
);
1727 if (gp
->phy_mii
.def
&& gp
->phy_mii
.def
->ops
->init
)
1728 gp
->phy_mii
.def
->ops
->init(&gp
->phy_mii
);
1731 gem_pcs_reinit_adv(gp
);
1734 /* Default aneg parameters */
1735 gp
->timer_ticks
= 0;
1736 gp
->lstate
= link_down
;
1737 netif_carrier_off(gp
->dev
);
1739 /* Print things out */
1740 if (gp
->phy_type
== phy_mii_mdio0
||
1741 gp
->phy_type
== phy_mii_mdio1
)
1742 netdev_info(gp
->dev
, "Found %s PHY\n",
1743 gp
->phy_mii
.def
? gp
->phy_mii
.def
->name
: "no");
1745 gem_begin_auto_negotiation(gp
, NULL
);
1748 static void gem_init_dma(struct gem
*gp
)
1750 u64 desc_dma
= (u64
) gp
->gblock_dvma
;
1753 val
= (TXDMA_CFG_BASE
| (0x7ff << 10) | TXDMA_CFG_PMODE
);
1754 writel(val
, gp
->regs
+ TXDMA_CFG
);
1756 writel(desc_dma
>> 32, gp
->regs
+ TXDMA_DBHI
);
1757 writel(desc_dma
& 0xffffffff, gp
->regs
+ TXDMA_DBLOW
);
1758 desc_dma
+= (INIT_BLOCK_TX_RING_SIZE
* sizeof(struct gem_txd
));
1760 writel(0, gp
->regs
+ TXDMA_KICK
);
1762 val
= (RXDMA_CFG_BASE
| (RX_OFFSET
<< 10) |
1763 ((14 / 2) << 13) | RXDMA_CFG_FTHRESH_128
);
1764 writel(val
, gp
->regs
+ RXDMA_CFG
);
1766 writel(desc_dma
>> 32, gp
->regs
+ RXDMA_DBHI
);
1767 writel(desc_dma
& 0xffffffff, gp
->regs
+ RXDMA_DBLOW
);
1769 writel(RX_RING_SIZE
- 4, gp
->regs
+ RXDMA_KICK
);
1771 val
= (((gp
->rx_pause_off
/ 64) << 0) & RXDMA_PTHRESH_OFF
);
1772 val
|= (((gp
->rx_pause_on
/ 64) << 12) & RXDMA_PTHRESH_ON
);
1773 writel(val
, gp
->regs
+ RXDMA_PTHRESH
);
1775 if (readl(gp
->regs
+ GREG_BIFCFG
) & GREG_BIFCFG_M66EN
)
1776 writel(((5 & RXDMA_BLANK_IPKTS
) |
1777 ((8 << 12) & RXDMA_BLANK_ITIME
)),
1778 gp
->regs
+ RXDMA_BLANK
);
1780 writel(((5 & RXDMA_BLANK_IPKTS
) |
1781 ((4 << 12) & RXDMA_BLANK_ITIME
)),
1782 gp
->regs
+ RXDMA_BLANK
);
1785 static u32
gem_setup_multicast(struct gem
*gp
)
1790 if ((gp
->dev
->flags
& IFF_ALLMULTI
) ||
1791 (netdev_mc_count(gp
->dev
) > 256)) {
1792 for (i
=0; i
<16; i
++)
1793 writel(0xffff, gp
->regs
+ MAC_HASH0
+ (i
<< 2));
1794 rxcfg
|= MAC_RXCFG_HFE
;
1795 } else if (gp
->dev
->flags
& IFF_PROMISC
) {
1796 rxcfg
|= MAC_RXCFG_PROM
;
1800 struct netdev_hw_addr
*ha
;
1803 memset(hash_table
, 0, sizeof(hash_table
));
1804 netdev_for_each_mc_addr(ha
, gp
->dev
) {
1805 crc
= ether_crc_le(6, ha
->addr
);
1807 hash_table
[crc
>> 4] |= 1 << (15 - (crc
& 0xf));
1809 for (i
=0; i
<16; i
++)
1810 writel(hash_table
[i
], gp
->regs
+ MAC_HASH0
+ (i
<< 2));
1811 rxcfg
|= MAC_RXCFG_HFE
;
1817 static void gem_init_mac(struct gem
*gp
)
1819 unsigned char *e
= &gp
->dev
->dev_addr
[0];
1821 writel(0x1bf0, gp
->regs
+ MAC_SNDPAUSE
);
1823 writel(0x00, gp
->regs
+ MAC_IPG0
);
1824 writel(0x08, gp
->regs
+ MAC_IPG1
);
1825 writel(0x04, gp
->regs
+ MAC_IPG2
);
1826 writel(0x40, gp
->regs
+ MAC_STIME
);
1827 writel(0x40, gp
->regs
+ MAC_MINFSZ
);
1829 /* Ethernet payload + header + FCS + optional VLAN tag. */
1830 writel(0x20000000 | (gp
->rx_buf_sz
+ 4), gp
->regs
+ MAC_MAXFSZ
);
1832 writel(0x07, gp
->regs
+ MAC_PASIZE
);
1833 writel(0x04, gp
->regs
+ MAC_JAMSIZE
);
1834 writel(0x10, gp
->regs
+ MAC_ATTLIM
);
1835 writel(0x8808, gp
->regs
+ MAC_MCTYPE
);
1837 writel((e
[5] | (e
[4] << 8)) & 0x3ff, gp
->regs
+ MAC_RANDSEED
);
1839 writel((e
[4] << 8) | e
[5], gp
->regs
+ MAC_ADDR0
);
1840 writel((e
[2] << 8) | e
[3], gp
->regs
+ MAC_ADDR1
);
1841 writel((e
[0] << 8) | e
[1], gp
->regs
+ MAC_ADDR2
);
1843 writel(0, gp
->regs
+ MAC_ADDR3
);
1844 writel(0, gp
->regs
+ MAC_ADDR4
);
1845 writel(0, gp
->regs
+ MAC_ADDR5
);
1847 writel(0x0001, gp
->regs
+ MAC_ADDR6
);
1848 writel(0xc200, gp
->regs
+ MAC_ADDR7
);
1849 writel(0x0180, gp
->regs
+ MAC_ADDR8
);
1851 writel(0, gp
->regs
+ MAC_AFILT0
);
1852 writel(0, gp
->regs
+ MAC_AFILT1
);
1853 writel(0, gp
->regs
+ MAC_AFILT2
);
1854 writel(0, gp
->regs
+ MAC_AF21MSK
);
1855 writel(0, gp
->regs
+ MAC_AF0MSK
);
1857 gp
->mac_rx_cfg
= gem_setup_multicast(gp
);
1859 gp
->mac_rx_cfg
|= MAC_RXCFG_SFCS
;
1861 writel(0, gp
->regs
+ MAC_NCOLL
);
1862 writel(0, gp
->regs
+ MAC_FASUCC
);
1863 writel(0, gp
->regs
+ MAC_ECOLL
);
1864 writel(0, gp
->regs
+ MAC_LCOLL
);
1865 writel(0, gp
->regs
+ MAC_DTIMER
);
1866 writel(0, gp
->regs
+ MAC_PATMPS
);
1867 writel(0, gp
->regs
+ MAC_RFCTR
);
1868 writel(0, gp
->regs
+ MAC_LERR
);
1869 writel(0, gp
->regs
+ MAC_AERR
);
1870 writel(0, gp
->regs
+ MAC_FCSERR
);
1871 writel(0, gp
->regs
+ MAC_RXCVERR
);
1873 /* Clear RX/TX/MAC/XIF config, we will set these up and enable
1874 * them once a link is established.
1876 writel(0, gp
->regs
+ MAC_TXCFG
);
1877 writel(gp
->mac_rx_cfg
, gp
->regs
+ MAC_RXCFG
);
1878 writel(0, gp
->regs
+ MAC_MCCFG
);
1879 writel(0, gp
->regs
+ MAC_XIFCFG
);
1881 /* Setup MAC interrupts. We want to get all of the interesting
1882 * counter expiration events, but we do not want to hear about
1883 * normal rx/tx as the DMA engine tells us that.
1885 writel(MAC_TXSTAT_XMIT
, gp
->regs
+ MAC_TXMASK
);
1886 writel(MAC_RXSTAT_RCV
, gp
->regs
+ MAC_RXMASK
);
1888 /* Don't enable even the PAUSE interrupts for now, we
1889 * make no use of those events other than to record them.
1891 writel(0xffffffff, gp
->regs
+ MAC_MCMASK
);
1893 /* Don't enable GEM's WOL in normal operations
1896 writel(0, gp
->regs
+ WOL_WAKECSR
);
1899 static void gem_init_pause_thresholds(struct gem
*gp
)
1903 /* Calculate pause thresholds. Setting the OFF threshold to the
1904 * full RX fifo size effectively disables PAUSE generation which
1905 * is what we do for 10/100 only GEMs which have FIFOs too small
1906 * to make real gains from PAUSE.
1908 if (gp
->rx_fifo_sz
<= (2 * 1024)) {
1909 gp
->rx_pause_off
= gp
->rx_pause_on
= gp
->rx_fifo_sz
;
1911 int max_frame
= (gp
->rx_buf_sz
+ 4 + 64) & ~63;
1912 int off
= (gp
->rx_fifo_sz
- (max_frame
* 2));
1913 int on
= off
- max_frame
;
1915 gp
->rx_pause_off
= off
;
1916 gp
->rx_pause_on
= on
;
1920 /* Configure the chip "burst" DMA mode & enable some
1921 * HW bug fixes on Apple version
1924 if (gp
->pdev
->vendor
== PCI_VENDOR_ID_APPLE
)
1925 cfg
|= GREG_CFG_RONPAULBIT
| GREG_CFG_ENBUG2FIX
;
1926 #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
1927 cfg
|= GREG_CFG_IBURST
;
1929 cfg
|= ((31 << 1) & GREG_CFG_TXDMALIM
);
1930 cfg
|= ((31 << 6) & GREG_CFG_RXDMALIM
);
1931 writel(cfg
, gp
->regs
+ GREG_CFG
);
1933 /* If Infinite Burst didn't stick, then use different
1934 * thresholds (and Apple bug fixes don't exist)
1936 if (!(readl(gp
->regs
+ GREG_CFG
) & GREG_CFG_IBURST
)) {
1937 cfg
= ((2 << 1) & GREG_CFG_TXDMALIM
);
1938 cfg
|= ((8 << 6) & GREG_CFG_RXDMALIM
);
1939 writel(cfg
, gp
->regs
+ GREG_CFG
);
1943 static int gem_check_invariants(struct gem
*gp
)
1945 struct pci_dev
*pdev
= gp
->pdev
;
1948 /* On Apple's sungem, we can't rely on registers as the chip
1949 * was been powered down by the firmware. The PHY is looked
1952 if (pdev
->vendor
== PCI_VENDOR_ID_APPLE
) {
1953 gp
->phy_type
= phy_mii_mdio0
;
1954 gp
->tx_fifo_sz
= readl(gp
->regs
+ TXDMA_FSZ
) * 64;
1955 gp
->rx_fifo_sz
= readl(gp
->regs
+ RXDMA_FSZ
) * 64;
1958 mif_cfg
= readl(gp
->regs
+ MIF_CFG
);
1959 mif_cfg
&= ~(MIF_CFG_PSELECT
|MIF_CFG_POLL
|MIF_CFG_BBMODE
|MIF_CFG_MDI1
);
1960 mif_cfg
|= MIF_CFG_MDI0
;
1961 writel(mif_cfg
, gp
->regs
+ MIF_CFG
);
1962 writel(PCS_DMODE_MGM
, gp
->regs
+ PCS_DMODE
);
1963 writel(MAC_XIFCFG_OE
, gp
->regs
+ MAC_XIFCFG
);
1965 /* We hard-code the PHY address so we can properly bring it out of
1966 * reset later on, we can't really probe it at this point, though
1967 * that isn't an issue.
1969 if (gp
->pdev
->device
== PCI_DEVICE_ID_APPLE_K2_GMAC
)
1970 gp
->mii_phy_addr
= 1;
1972 gp
->mii_phy_addr
= 0;
1977 mif_cfg
= readl(gp
->regs
+ MIF_CFG
);
1979 if (pdev
->vendor
== PCI_VENDOR_ID_SUN
&&
1980 pdev
->device
== PCI_DEVICE_ID_SUN_RIO_GEM
) {
1981 /* One of the MII PHYs _must_ be present
1982 * as this chip has no gigabit PHY.
1984 if ((mif_cfg
& (MIF_CFG_MDI0
| MIF_CFG_MDI1
)) == 0) {
1985 pr_err("RIO GEM lacks MII phy, mif_cfg[%08x]\n",
1991 /* Determine initial PHY interface type guess. MDIO1 is the
1992 * external PHY and thus takes precedence over MDIO0.
1995 if (mif_cfg
& MIF_CFG_MDI1
) {
1996 gp
->phy_type
= phy_mii_mdio1
;
1997 mif_cfg
|= MIF_CFG_PSELECT
;
1998 writel(mif_cfg
, gp
->regs
+ MIF_CFG
);
1999 } else if (mif_cfg
& MIF_CFG_MDI0
) {
2000 gp
->phy_type
= phy_mii_mdio0
;
2001 mif_cfg
&= ~MIF_CFG_PSELECT
;
2002 writel(mif_cfg
, gp
->regs
+ MIF_CFG
);
2007 p
= of_get_property(gp
->of_node
, "shared-pins", NULL
);
2008 if (p
&& !strcmp(p
, "serdes"))
2009 gp
->phy_type
= phy_serdes
;
2012 gp
->phy_type
= phy_serialink
;
2014 if (gp
->phy_type
== phy_mii_mdio1
||
2015 gp
->phy_type
== phy_mii_mdio0
) {
2018 for (i
= 0; i
< 32; i
++) {
2019 gp
->mii_phy_addr
= i
;
2020 if (sungem_phy_read(gp
, MII_BMCR
) != 0xffff)
2024 if (pdev
->device
!= PCI_DEVICE_ID_SUN_GEM
) {
2025 pr_err("RIO MII phy will not respond\n");
2028 gp
->phy_type
= phy_serdes
;
2032 /* Fetch the FIFO configurations now too. */
2033 gp
->tx_fifo_sz
= readl(gp
->regs
+ TXDMA_FSZ
) * 64;
2034 gp
->rx_fifo_sz
= readl(gp
->regs
+ RXDMA_FSZ
) * 64;
2036 if (pdev
->vendor
== PCI_VENDOR_ID_SUN
) {
2037 if (pdev
->device
== PCI_DEVICE_ID_SUN_GEM
) {
2038 if (gp
->tx_fifo_sz
!= (9 * 1024) ||
2039 gp
->rx_fifo_sz
!= (20 * 1024)) {
2040 pr_err("GEM has bogus fifo sizes tx(%d) rx(%d)\n",
2041 gp
->tx_fifo_sz
, gp
->rx_fifo_sz
);
2046 if (gp
->tx_fifo_sz
!= (2 * 1024) ||
2047 gp
->rx_fifo_sz
!= (2 * 1024)) {
2048 pr_err("RIO GEM has bogus fifo sizes tx(%d) rx(%d)\n",
2049 gp
->tx_fifo_sz
, gp
->rx_fifo_sz
);
2052 gp
->swrst_base
= (64 / 4) << GREG_SWRST_CACHE_SHIFT
;
2059 static void gem_reinit_chip(struct gem
*gp
)
2061 /* Reset the chip */
2064 /* Make sure ints are disabled */
2065 gem_disable_ints(gp
);
2067 /* Allocate & setup ring buffers */
2070 /* Configure pause thresholds */
2071 gem_init_pause_thresholds(gp
);
2073 /* Init DMA & MAC engines */
2079 static void gem_stop_phy(struct gem
*gp
, int wol
)
2083 /* Let the chip settle down a bit, it seems that helps
2084 * for sleep mode on some models
2088 /* Make sure we aren't polling PHY status change. We
2089 * don't currently use that feature though
2091 mifcfg
= readl(gp
->regs
+ MIF_CFG
);
2092 mifcfg
&= ~MIF_CFG_POLL
;
2093 writel(mifcfg
, gp
->regs
+ MIF_CFG
);
2095 if (wol
&& gp
->has_wol
) {
2096 unsigned char *e
= &gp
->dev
->dev_addr
[0];
2099 /* Setup wake-on-lan for MAGIC packet */
2100 writel(MAC_RXCFG_HFE
| MAC_RXCFG_SFCS
| MAC_RXCFG_ENAB
,
2101 gp
->regs
+ MAC_RXCFG
);
2102 writel((e
[4] << 8) | e
[5], gp
->regs
+ WOL_MATCH0
);
2103 writel((e
[2] << 8) | e
[3], gp
->regs
+ WOL_MATCH1
);
2104 writel((e
[0] << 8) | e
[1], gp
->regs
+ WOL_MATCH2
);
2106 writel(WOL_MCOUNT_N
| WOL_MCOUNT_M
, gp
->regs
+ WOL_MCOUNT
);
2107 csr
= WOL_WAKECSR_ENABLE
;
2108 if ((readl(gp
->regs
+ MAC_XIFCFG
) & MAC_XIFCFG_GMII
) == 0)
2109 csr
|= WOL_WAKECSR_MII
;
2110 writel(csr
, gp
->regs
+ WOL_WAKECSR
);
2112 writel(0, gp
->regs
+ MAC_RXCFG
);
2113 (void)readl(gp
->regs
+ MAC_RXCFG
);
2114 /* Machine sleep will die in strange ways if we
2115 * dont wait a bit here, looks like the chip takes
2116 * some time to really shut down
2121 writel(0, gp
->regs
+ MAC_TXCFG
);
2122 writel(0, gp
->regs
+ MAC_XIFCFG
);
2123 writel(0, gp
->regs
+ TXDMA_CFG
);
2124 writel(0, gp
->regs
+ RXDMA_CFG
);
2128 writel(MAC_TXRST_CMD
, gp
->regs
+ MAC_TXRST
);
2129 writel(MAC_RXRST_CMD
, gp
->regs
+ MAC_RXRST
);
2131 if (found_mii_phy(gp
) && gp
->phy_mii
.def
->ops
->suspend
)
2132 gp
->phy_mii
.def
->ops
->suspend(&gp
->phy_mii
);
2134 /* According to Apple, we must set the MDIO pins to this begnign
2135 * state or we may 1) eat more current, 2) damage some PHYs
2137 writel(mifcfg
| MIF_CFG_BBMODE
, gp
->regs
+ MIF_CFG
);
2138 writel(0, gp
->regs
+ MIF_BBCLK
);
2139 writel(0, gp
->regs
+ MIF_BBDATA
);
2140 writel(0, gp
->regs
+ MIF_BBOENAB
);
2141 writel(MAC_XIFCFG_GMII
| MAC_XIFCFG_LBCK
, gp
->regs
+ MAC_XIFCFG
);
2142 (void) readl(gp
->regs
+ MAC_XIFCFG
);
2146 static int gem_do_start(struct net_device
*dev
)
2148 struct gem
*gp
= netdev_priv(dev
);
2151 /* Enable the cell */
2154 /* Make sure PCI access and bus master are enabled */
2155 rc
= pci_enable_device(gp
->pdev
);
2157 netdev_err(dev
, "Failed to enable chip on PCI bus !\n");
2159 /* Put cell and forget it for now, it will be considered as
2160 * still asleep, a new sleep cycle may bring it back
2165 pci_set_master(gp
->pdev
);
2167 /* Init & setup chip hardware */
2168 gem_reinit_chip(gp
);
2170 /* An interrupt might come in handy */
2171 rc
= request_irq(gp
->pdev
->irq
, gem_interrupt
,
2172 IRQF_SHARED
, dev
->name
, (void *)dev
);
2174 netdev_err(dev
, "failed to request irq !\n");
2177 gem_clean_rings(gp
);
2182 /* Mark us as attached again if we come from resume(), this has
2183 * no effect if we weren't detached and needs to be done now.
2185 netif_device_attach(dev
);
2187 /* Restart NAPI & queues */
2188 gem_netif_start(gp
);
2190 /* Detect & init PHY, start autoneg etc... this will
2191 * eventually result in starting DMA operations when
2199 static void gem_do_stop(struct net_device
*dev
, int wol
)
2201 struct gem
*gp
= netdev_priv(dev
);
2203 /* Stop NAPI and stop tx queue */
2206 /* Make sure ints are disabled. We don't care about
2207 * synchronizing as NAPI is disabled, thus a stray
2208 * interrupt will do nothing bad (our irq handler
2209 * just schedules NAPI)
2211 gem_disable_ints(gp
);
2213 /* Stop the link timer */
2214 del_timer_sync(&gp
->link_timer
);
2216 /* We cannot cancel the reset task while holding the
2217 * rtnl lock, we'd get an A->B / B->A deadlock stituation
2218 * if we did. This is not an issue however as the reset
2219 * task is synchronized vs. us (rtnl_lock) and will do
2220 * nothing if the device is down or suspended. We do
2221 * still clear reset_task_pending to avoid a spurrious
2222 * reset later on in case we do resume before it gets
2225 gp
->reset_task_pending
= 0;
2227 /* If we are going to sleep with WOL */
2234 /* Get rid of rings */
2235 gem_clean_rings(gp
);
2237 /* No irq needed anymore */
2238 free_irq(gp
->pdev
->irq
, (void *) dev
);
2240 /* Shut the PHY down eventually and setup WOL */
2241 gem_stop_phy(gp
, wol
);
2243 /* Make sure bus master is disabled */
2244 pci_disable_device(gp
->pdev
);
2246 /* Cell not needed neither if no WOL */
2251 static void gem_reset_task(struct work_struct
*work
)
2253 struct gem
*gp
= container_of(work
, struct gem
, reset_task
);
2255 /* Lock out the network stack (essentially shield ourselves
2256 * against a racing open, close, control call, or suspend
2260 /* Skip the reset task if suspended or closed, or if it's
2261 * been cancelled by gem_do_stop (see comment there)
2263 if (!netif_device_present(gp
->dev
) ||
2264 !netif_running(gp
->dev
) ||
2265 !gp
->reset_task_pending
) {
2270 /* Stop the link timer */
2271 del_timer_sync(&gp
->link_timer
);
2273 /* Stop NAPI and tx */
2276 /* Reset the chip & rings */
2277 gem_reinit_chip(gp
);
2278 if (gp
->lstate
== link_up
)
2279 gem_set_link_modes(gp
);
2281 /* Restart NAPI and Tx */
2282 gem_netif_start(gp
);
2285 gp
->reset_task_pending
= 0;
2287 /* If the link is not up, restart autoneg, else restart the
2290 if (gp
->lstate
!= link_up
)
2291 gem_begin_auto_negotiation(gp
, NULL
);
2293 mod_timer(&gp
->link_timer
, jiffies
+ ((12 * HZ
) / 10));
2298 static int gem_open(struct net_device
*dev
)
2300 /* We allow open while suspended, we just do nothing,
2301 * the chip will be initialized in resume()
2303 if (netif_device_present(dev
))
2304 return gem_do_start(dev
);
2308 static int gem_close(struct net_device
*dev
)
2310 if (netif_device_present(dev
))
2311 gem_do_stop(dev
, 0);
2317 static int gem_suspend(struct pci_dev
*pdev
, pm_message_t state
)
2319 struct net_device
*dev
= pci_get_drvdata(pdev
);
2320 struct gem
*gp
= netdev_priv(dev
);
2322 /* Lock the network stack first to avoid racing with open/close,
2323 * reset task and setting calls
2327 /* Not running, mark ourselves non-present, no need for
2330 if (!netif_running(dev
)) {
2331 netif_device_detach(dev
);
2335 netdev_info(dev
, "suspending, WakeOnLan %s\n",
2336 (gp
->wake_on_lan
&& netif_running(dev
)) ?
2337 "enabled" : "disabled");
2339 /* Tell the network stack we're gone. gem_do_stop() below will
2340 * synchronize with TX, stop NAPI etc...
2342 netif_device_detach(dev
);
2344 /* Switch off chip, remember WOL setting */
2345 gp
->asleep_wol
= !!gp
->wake_on_lan
;
2346 gem_do_stop(dev
, gp
->asleep_wol
);
2348 /* Unlock the network stack */
2354 static int gem_resume(struct pci_dev
*pdev
)
2356 struct net_device
*dev
= pci_get_drvdata(pdev
);
2357 struct gem
*gp
= netdev_priv(dev
);
2359 /* See locking comment in gem_suspend */
2362 /* Not running, mark ourselves present, no need for
2365 if (!netif_running(dev
)) {
2366 netif_device_attach(dev
);
2371 /* Restart chip. If that fails there isn't much we can do, we
2372 * leave things stopped.
2376 /* If we had WOL enabled, the cell clock was never turned off during
2377 * sleep, so we end up beeing unbalanced. Fix that here
2382 /* Unlock the network stack */
2387 #endif /* CONFIG_PM */
2389 static struct net_device_stats
*gem_get_stats(struct net_device
*dev
)
2391 struct gem
*gp
= netdev_priv(dev
);
2393 /* I have seen this being called while the PM was in progress,
2394 * so we shield against this. Let's also not poke at registers
2395 * while the reset task is going on.
2397 * TODO: Move stats collection elsewhere (link timer ?) and
2398 * make this a nop to avoid all those synchro issues
2400 if (!netif_device_present(dev
) || !netif_running(dev
))
2403 /* Better safe than sorry... */
2404 if (WARN_ON(!gp
->cell_enabled
))
2407 dev
->stats
.rx_crc_errors
+= readl(gp
->regs
+ MAC_FCSERR
);
2408 writel(0, gp
->regs
+ MAC_FCSERR
);
2410 dev
->stats
.rx_frame_errors
+= readl(gp
->regs
+ MAC_AERR
);
2411 writel(0, gp
->regs
+ MAC_AERR
);
2413 dev
->stats
.rx_length_errors
+= readl(gp
->regs
+ MAC_LERR
);
2414 writel(0, gp
->regs
+ MAC_LERR
);
2416 dev
->stats
.tx_aborted_errors
+= readl(gp
->regs
+ MAC_ECOLL
);
2417 dev
->stats
.collisions
+=
2418 (readl(gp
->regs
+ MAC_ECOLL
) + readl(gp
->regs
+ MAC_LCOLL
));
2419 writel(0, gp
->regs
+ MAC_ECOLL
);
2420 writel(0, gp
->regs
+ MAC_LCOLL
);
2425 static int gem_set_mac_address(struct net_device
*dev
, void *addr
)
2427 struct sockaddr
*macaddr
= (struct sockaddr
*) addr
;
2428 struct gem
*gp
= netdev_priv(dev
);
2429 unsigned char *e
= &dev
->dev_addr
[0];
2431 if (!is_valid_ether_addr(macaddr
->sa_data
))
2432 return -EADDRNOTAVAIL
;
2434 memcpy(dev
->dev_addr
, macaddr
->sa_data
, dev
->addr_len
);
2436 /* We'll just catch it later when the device is up'd or resumed */
2437 if (!netif_running(dev
) || !netif_device_present(dev
))
2440 /* Better safe than sorry... */
2441 if (WARN_ON(!gp
->cell_enabled
))
2444 writel((e
[4] << 8) | e
[5], gp
->regs
+ MAC_ADDR0
);
2445 writel((e
[2] << 8) | e
[3], gp
->regs
+ MAC_ADDR1
);
2446 writel((e
[0] << 8) | e
[1], gp
->regs
+ MAC_ADDR2
);
2451 static void gem_set_multicast(struct net_device
*dev
)
2453 struct gem
*gp
= netdev_priv(dev
);
2454 u32 rxcfg
, rxcfg_new
;
2457 if (!netif_running(dev
) || !netif_device_present(dev
))
2460 /* Better safe than sorry... */
2461 if (gp
->reset_task_pending
|| WARN_ON(!gp
->cell_enabled
))
2464 rxcfg
= readl(gp
->regs
+ MAC_RXCFG
);
2465 rxcfg_new
= gem_setup_multicast(gp
);
2467 rxcfg_new
|= MAC_RXCFG_SFCS
;
2469 gp
->mac_rx_cfg
= rxcfg_new
;
2471 writel(rxcfg
& ~MAC_RXCFG_ENAB
, gp
->regs
+ MAC_RXCFG
);
2472 while (readl(gp
->regs
+ MAC_RXCFG
) & MAC_RXCFG_ENAB
) {
2478 rxcfg
&= ~(MAC_RXCFG_PROM
| MAC_RXCFG_HFE
);
2481 writel(rxcfg
, gp
->regs
+ MAC_RXCFG
);
2484 /* Jumbo-grams don't seem to work :-( */
2485 #define GEM_MIN_MTU ETH_MIN_MTU
2487 #define GEM_MAX_MTU ETH_DATA_LEN
2489 #define GEM_MAX_MTU 9000
2492 static int gem_change_mtu(struct net_device
*dev
, int new_mtu
)
2494 struct gem
*gp
= netdev_priv(dev
);
2498 /* We'll just catch it later when the device is up'd or resumed */
2499 if (!netif_running(dev
) || !netif_device_present(dev
))
2502 /* Better safe than sorry... */
2503 if (WARN_ON(!gp
->cell_enabled
))
2507 gem_reinit_chip(gp
);
2508 if (gp
->lstate
== link_up
)
2509 gem_set_link_modes(gp
);
2510 gem_netif_start(gp
);
2515 static void gem_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
2517 struct gem
*gp
= netdev_priv(dev
);
2519 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
2520 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
2521 strlcpy(info
->bus_info
, pci_name(gp
->pdev
), sizeof(info
->bus_info
));
2524 static int gem_get_link_ksettings(struct net_device
*dev
,
2525 struct ethtool_link_ksettings
*cmd
)
2527 struct gem
*gp
= netdev_priv(dev
);
2528 u32 supported
, advertising
;
2530 if (gp
->phy_type
== phy_mii_mdio0
||
2531 gp
->phy_type
== phy_mii_mdio1
) {
2532 if (gp
->phy_mii
.def
)
2533 supported
= gp
->phy_mii
.def
->features
;
2535 supported
= (SUPPORTED_10baseT_Half
|
2536 SUPPORTED_10baseT_Full
);
2538 /* XXX hardcoded stuff for now */
2539 cmd
->base
.port
= PORT_MII
;
2540 cmd
->base
.phy_address
= 0; /* XXX fixed PHYAD */
2542 /* Return current PHY settings */
2543 cmd
->base
.autoneg
= gp
->want_autoneg
;
2544 cmd
->base
.speed
= gp
->phy_mii
.speed
;
2545 cmd
->base
.duplex
= gp
->phy_mii
.duplex
;
2546 advertising
= gp
->phy_mii
.advertising
;
2548 /* If we started with a forced mode, we don't have a default
2549 * advertise set, we need to return something sensible so
2550 * userland can re-enable autoneg properly.
2552 if (advertising
== 0)
2553 advertising
= supported
;
2554 } else { // XXX PCS ?
2556 (SUPPORTED_10baseT_Half
| SUPPORTED_10baseT_Full
|
2557 SUPPORTED_100baseT_Half
| SUPPORTED_100baseT_Full
|
2559 advertising
= supported
;
2560 cmd
->base
.speed
= 0;
2561 cmd
->base
.duplex
= 0;
2563 cmd
->base
.phy_address
= 0;
2564 cmd
->base
.autoneg
= 0;
2566 /* serdes means usually a Fibre connector, with most fixed */
2567 if (gp
->phy_type
== phy_serdes
) {
2568 cmd
->base
.port
= PORT_FIBRE
;
2569 supported
= (SUPPORTED_1000baseT_Half
|
2570 SUPPORTED_1000baseT_Full
|
2571 SUPPORTED_FIBRE
| SUPPORTED_Autoneg
|
2572 SUPPORTED_Pause
| SUPPORTED_Asym_Pause
);
2573 advertising
= supported
;
2574 if (gp
->lstate
== link_up
)
2575 cmd
->base
.speed
= SPEED_1000
;
2576 cmd
->base
.duplex
= DUPLEX_FULL
;
2577 cmd
->base
.autoneg
= 1;
2581 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.supported
,
2583 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.advertising
,
2589 static int gem_set_link_ksettings(struct net_device
*dev
,
2590 const struct ethtool_link_ksettings
*cmd
)
2592 struct gem
*gp
= netdev_priv(dev
);
2593 u32 speed
= cmd
->base
.speed
;
2596 ethtool_convert_link_mode_to_legacy_u32(&advertising
,
2597 cmd
->link_modes
.advertising
);
2599 /* Verify the settings we care about. */
2600 if (cmd
->base
.autoneg
!= AUTONEG_ENABLE
&&
2601 cmd
->base
.autoneg
!= AUTONEG_DISABLE
)
2604 if (cmd
->base
.autoneg
== AUTONEG_ENABLE
&&
2608 if (cmd
->base
.autoneg
== AUTONEG_DISABLE
&&
2609 ((speed
!= SPEED_1000
&&
2610 speed
!= SPEED_100
&&
2611 speed
!= SPEED_10
) ||
2612 (cmd
->base
.duplex
!= DUPLEX_HALF
&&
2613 cmd
->base
.duplex
!= DUPLEX_FULL
)))
2616 /* Apply settings and restart link process. */
2617 if (netif_device_present(gp
->dev
)) {
2618 del_timer_sync(&gp
->link_timer
);
2619 gem_begin_auto_negotiation(gp
, cmd
);
2625 static int gem_nway_reset(struct net_device
*dev
)
2627 struct gem
*gp
= netdev_priv(dev
);
2629 if (!gp
->want_autoneg
)
2632 /* Restart link process */
2633 if (netif_device_present(gp
->dev
)) {
2634 del_timer_sync(&gp
->link_timer
);
2635 gem_begin_auto_negotiation(gp
, NULL
);
2641 static u32
gem_get_msglevel(struct net_device
*dev
)
2643 struct gem
*gp
= netdev_priv(dev
);
2644 return gp
->msg_enable
;
2647 static void gem_set_msglevel(struct net_device
*dev
, u32 value
)
2649 struct gem
*gp
= netdev_priv(dev
);
2650 gp
->msg_enable
= value
;
2654 /* Add more when I understand how to program the chip */
2655 /* like WAKE_UCAST | WAKE_MCAST | WAKE_BCAST */
2657 #define WOL_SUPPORTED_MASK (WAKE_MAGIC)
2659 static void gem_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
2661 struct gem
*gp
= netdev_priv(dev
);
2663 /* Add more when I understand how to program the chip */
2665 wol
->supported
= WOL_SUPPORTED_MASK
;
2666 wol
->wolopts
= gp
->wake_on_lan
;
2673 static int gem_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
2675 struct gem
*gp
= netdev_priv(dev
);
2679 gp
->wake_on_lan
= wol
->wolopts
& WOL_SUPPORTED_MASK
;
2683 static const struct ethtool_ops gem_ethtool_ops
= {
2684 .get_drvinfo
= gem_get_drvinfo
,
2685 .get_link
= ethtool_op_get_link
,
2686 .nway_reset
= gem_nway_reset
,
2687 .get_msglevel
= gem_get_msglevel
,
2688 .set_msglevel
= gem_set_msglevel
,
2689 .get_wol
= gem_get_wol
,
2690 .set_wol
= gem_set_wol
,
2691 .get_link_ksettings
= gem_get_link_ksettings
,
2692 .set_link_ksettings
= gem_set_link_ksettings
,
2695 static int gem_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
2697 struct gem
*gp
= netdev_priv(dev
);
2698 struct mii_ioctl_data
*data
= if_mii(ifr
);
2699 int rc
= -EOPNOTSUPP
;
2701 /* For SIOCGMIIREG and SIOCSMIIREG the core checks for us that
2702 * netif_device_present() is true and holds rtnl_lock for us
2703 * so we have nothing to worry about
2707 case SIOCGMIIPHY
: /* Get address of MII PHY in use. */
2708 data
->phy_id
= gp
->mii_phy_addr
;
2709 /* Fallthrough... */
2711 case SIOCGMIIREG
: /* Read MII PHY register. */
2712 data
->val_out
= __sungem_phy_read(gp
, data
->phy_id
& 0x1f,
2713 data
->reg_num
& 0x1f);
2717 case SIOCSMIIREG
: /* Write MII PHY register. */
2718 __sungem_phy_write(gp
, data
->phy_id
& 0x1f, data
->reg_num
& 0x1f,
2726 #if (!defined(CONFIG_SPARC) && !defined(CONFIG_PPC_PMAC))
2727 /* Fetch MAC address from vital product data of PCI ROM. */
2728 static int find_eth_addr_in_vpd(void __iomem
*rom_base
, int len
, unsigned char *dev_addr
)
2732 for (this_offset
= 0x20; this_offset
< len
; this_offset
++) {
2733 void __iomem
*p
= rom_base
+ this_offset
;
2736 if (readb(p
+ 0) != 0x90 ||
2737 readb(p
+ 1) != 0x00 ||
2738 readb(p
+ 2) != 0x09 ||
2739 readb(p
+ 3) != 0x4e ||
2740 readb(p
+ 4) != 0x41 ||
2741 readb(p
+ 5) != 0x06)
2747 for (i
= 0; i
< 6; i
++)
2748 dev_addr
[i
] = readb(p
+ i
);
2754 static void get_gem_mac_nonobp(struct pci_dev
*pdev
, unsigned char *dev_addr
)
2757 void __iomem
*p
= pci_map_rom(pdev
, &size
);
2762 found
= readb(p
) == 0x55 &&
2763 readb(p
+ 1) == 0xaa &&
2764 find_eth_addr_in_vpd(p
, (64 * 1024), dev_addr
);
2765 pci_unmap_rom(pdev
, p
);
2770 /* Sun MAC prefix then 3 random bytes. */
2774 get_random_bytes(dev_addr
+ 3, 3);
2776 #endif /* not Sparc and not PPC */
2778 static int gem_get_device_address(struct gem
*gp
)
2780 #if defined(CONFIG_SPARC) || defined(CONFIG_PPC_PMAC)
2781 struct net_device
*dev
= gp
->dev
;
2782 const unsigned char *addr
;
2784 addr
= of_get_property(gp
->of_node
, "local-mac-address", NULL
);
2787 addr
= idprom
->id_ethaddr
;
2790 pr_err("%s: can't get mac-address\n", dev
->name
);
2794 memcpy(dev
->dev_addr
, addr
, ETH_ALEN
);
2796 get_gem_mac_nonobp(gp
->pdev
, gp
->dev
->dev_addr
);
2801 static void gem_remove_one(struct pci_dev
*pdev
)
2803 struct net_device
*dev
= pci_get_drvdata(pdev
);
2806 struct gem
*gp
= netdev_priv(dev
);
2808 unregister_netdev(dev
);
2810 /* Ensure reset task is truly gone */
2811 cancel_work_sync(&gp
->reset_task
);
2813 /* Free resources */
2814 pci_free_consistent(pdev
,
2815 sizeof(struct gem_init_block
),
2819 pci_release_regions(pdev
);
2824 static const struct net_device_ops gem_netdev_ops
= {
2825 .ndo_open
= gem_open
,
2826 .ndo_stop
= gem_close
,
2827 .ndo_start_xmit
= gem_start_xmit
,
2828 .ndo_get_stats
= gem_get_stats
,
2829 .ndo_set_rx_mode
= gem_set_multicast
,
2830 .ndo_do_ioctl
= gem_ioctl
,
2831 .ndo_tx_timeout
= gem_tx_timeout
,
2832 .ndo_change_mtu
= gem_change_mtu
,
2833 .ndo_validate_addr
= eth_validate_addr
,
2834 .ndo_set_mac_address
= gem_set_mac_address
,
2835 #ifdef CONFIG_NET_POLL_CONTROLLER
2836 .ndo_poll_controller
= gem_poll_controller
,
2840 static int gem_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
2842 unsigned long gemreg_base
, gemreg_len
;
2843 struct net_device
*dev
;
2845 int err
, pci_using_dac
;
2847 printk_once(KERN_INFO
"%s", version
);
2849 /* Apple gmac note: during probe, the chip is powered up by
2850 * the arch code to allow the code below to work (and to let
2851 * the chip be probed on the config space. It won't stay powered
2852 * up until the interface is brought up however, so we can't rely
2853 * on register configuration done at this point.
2855 err
= pci_enable_device(pdev
);
2857 pr_err("Cannot enable MMIO operation, aborting\n");
2860 pci_set_master(pdev
);
2862 /* Configure DMA attributes. */
2864 /* All of the GEM documentation states that 64-bit DMA addressing
2865 * is fully supported and should work just fine. However the
2866 * front end for RIO based GEMs is different and only supports
2867 * 32-bit addressing.
2869 * For now we assume the various PPC GEMs are 32-bit only as well.
2871 if (pdev
->vendor
== PCI_VENDOR_ID_SUN
&&
2872 pdev
->device
== PCI_DEVICE_ID_SUN_GEM
&&
2873 !pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
2876 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
2878 pr_err("No usable DMA configuration, aborting\n");
2879 goto err_disable_device
;
2884 gemreg_base
= pci_resource_start(pdev
, 0);
2885 gemreg_len
= pci_resource_len(pdev
, 0);
2887 if ((pci_resource_flags(pdev
, 0) & IORESOURCE_IO
) != 0) {
2888 pr_err("Cannot find proper PCI device base address, aborting\n");
2890 goto err_disable_device
;
2893 dev
= alloc_etherdev(sizeof(*gp
));
2896 goto err_disable_device
;
2898 SET_NETDEV_DEV(dev
, &pdev
->dev
);
2900 gp
= netdev_priv(dev
);
2902 err
= pci_request_regions(pdev
, DRV_NAME
);
2904 pr_err("Cannot obtain PCI resources, aborting\n");
2905 goto err_out_free_netdev
;
2911 gp
->msg_enable
= DEFAULT_MSG
;
2913 timer_setup(&gp
->link_timer
, gem_link_timer
, 0);
2915 INIT_WORK(&gp
->reset_task
, gem_reset_task
);
2917 gp
->lstate
= link_down
;
2918 gp
->timer_ticks
= 0;
2919 netif_carrier_off(dev
);
2921 gp
->regs
= ioremap(gemreg_base
, gemreg_len
);
2923 pr_err("Cannot map device registers, aborting\n");
2925 goto err_out_free_res
;
2928 /* On Apple, we want a reference to the Open Firmware device-tree
2929 * node. We use it for clock control.
2931 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_SPARC)
2932 gp
->of_node
= pci_device_to_OF_node(pdev
);
2935 /* Only Apple version supports WOL afaik */
2936 if (pdev
->vendor
== PCI_VENDOR_ID_APPLE
)
2939 /* Make sure cell is enabled */
2942 /* Make sure everything is stopped and in init state */
2945 /* Fill up the mii_phy structure (even if we won't use it) */
2946 gp
->phy_mii
.dev
= dev
;
2947 gp
->phy_mii
.mdio_read
= _sungem_phy_read
;
2948 gp
->phy_mii
.mdio_write
= _sungem_phy_write
;
2949 #ifdef CONFIG_PPC_PMAC
2950 gp
->phy_mii
.platform_data
= gp
->of_node
;
2952 /* By default, we start with autoneg */
2953 gp
->want_autoneg
= 1;
2955 /* Check fifo sizes, PHY type, etc... */
2956 if (gem_check_invariants(gp
)) {
2958 goto err_out_iounmap
;
2961 /* It is guaranteed that the returned buffer will be at least
2962 * PAGE_SIZE aligned.
2964 gp
->init_block
= (struct gem_init_block
*)
2965 pci_alloc_consistent(pdev
, sizeof(struct gem_init_block
),
2967 if (!gp
->init_block
) {
2968 pr_err("Cannot allocate init block, aborting\n");
2970 goto err_out_iounmap
;
2973 err
= gem_get_device_address(gp
);
2975 goto err_out_free_consistent
;
2977 dev
->netdev_ops
= &gem_netdev_ops
;
2978 netif_napi_add(dev
, &gp
->napi
, gem_poll
, 64);
2979 dev
->ethtool_ops
= &gem_ethtool_ops
;
2980 dev
->watchdog_timeo
= 5 * HZ
;
2983 /* Set that now, in case PM kicks in now */
2984 pci_set_drvdata(pdev
, dev
);
2986 /* We can do scatter/gather and HW checksum */
2987 dev
->hw_features
= NETIF_F_SG
| NETIF_F_HW_CSUM
;
2988 dev
->features
|= dev
->hw_features
| NETIF_F_RXCSUM
;
2990 dev
->features
|= NETIF_F_HIGHDMA
;
2992 /* MTU range: 68 - 1500 (Jumbo mode is broken) */
2993 dev
->min_mtu
= GEM_MIN_MTU
;
2994 dev
->max_mtu
= GEM_MAX_MTU
;
2996 /* Register with kernel */
2997 if (register_netdev(dev
)) {
2998 pr_err("Cannot register net device, aborting\n");
3000 goto err_out_free_consistent
;
3003 /* Undo the get_cell with appropriate locking (we could use
3004 * ndo_init/uninit but that would be even more clumsy imho)
3010 netdev_info(dev
, "Sun GEM (PCI) 10/100/1000BaseT Ethernet %pM\n",
3014 err_out_free_consistent
:
3015 gem_remove_one(pdev
);
3021 pci_release_regions(pdev
);
3023 err_out_free_netdev
:
3026 pci_disable_device(pdev
);
3032 static struct pci_driver gem_driver
= {
3033 .name
= GEM_MODULE_NAME
,
3034 .id_table
= gem_pci_tbl
,
3035 .probe
= gem_init_one
,
3036 .remove
= gem_remove_one
,
3038 .suspend
= gem_suspend
,
3039 .resume
= gem_resume
,
3040 #endif /* CONFIG_PM */
3043 module_pci_driver(gem_driver
);