1 /* b44.c: Broadcom 4400 device driver.
3 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
4 * Fixed by Pekka Pietikainen (pp@ee.oulu.fi)
6 * Distribute under GPL.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/types.h>
13 #include <linux/netdevice.h>
14 #include <linux/ethtool.h>
15 #include <linux/mii.h>
16 #include <linux/if_ether.h>
17 #include <linux/etherdevice.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/version.h>
22 #include <linux/dma-mapping.h>
24 #include <asm/uaccess.h>
30 #define DRV_MODULE_NAME "b44"
31 #define PFX DRV_MODULE_NAME ": "
32 #define DRV_MODULE_VERSION "0.95"
33 #define DRV_MODULE_RELDATE "Aug 3, 2004"
35 #define B44_DEF_MSG_ENABLE \
45 /* length of time before we decide the hardware is borked,
46 * and dev->tx_timeout() should be called to fix the problem
48 #define B44_TX_TIMEOUT (5 * HZ)
50 /* hardware minimum and maximum for a single frame's data payload */
51 #define B44_MIN_MTU 60
52 #define B44_MAX_MTU 1500
54 #define B44_RX_RING_SIZE 512
55 #define B44_DEF_RX_RING_PENDING 200
56 #define B44_RX_RING_BYTES (sizeof(struct dma_desc) * \
58 #define B44_TX_RING_SIZE 512
59 #define B44_DEF_TX_RING_PENDING (B44_TX_RING_SIZE - 1)
60 #define B44_TX_RING_BYTES (sizeof(struct dma_desc) * \
62 #define B44_DMA_MASK 0x3fffffff
64 #define TX_RING_GAP(BP) \
65 (B44_TX_RING_SIZE - (BP)->tx_pending)
66 #define TX_BUFFS_AVAIL(BP) \
67 (((BP)->tx_cons <= (BP)->tx_prod) ? \
68 (BP)->tx_cons + (BP)->tx_pending - (BP)->tx_prod : \
69 (BP)->tx_cons - (BP)->tx_prod - TX_RING_GAP(BP))
70 #define NEXT_TX(N) (((N) + 1) & (B44_TX_RING_SIZE - 1))
72 #define RX_PKT_BUF_SZ (1536 + bp->rx_offset + 64)
73 #define TX_PKT_BUF_SZ (B44_MAX_MTU + ETH_HLEN + 8)
75 /* minimum number of free TX descriptors required to wake up TX process */
76 #define B44_TX_WAKEUP_THRESH (B44_TX_RING_SIZE / 4)
78 static char version
[] __devinitdata
=
79 DRV_MODULE_NAME
".c:v" DRV_MODULE_VERSION
" (" DRV_MODULE_RELDATE
")\n";
81 MODULE_AUTHOR("Florian Schirmer, Pekka Pietikainen, David S. Miller");
82 MODULE_DESCRIPTION("Broadcom 4400 10/100 PCI ethernet driver");
83 MODULE_LICENSE("GPL");
84 MODULE_VERSION(DRV_MODULE_VERSION
);
86 static int b44_debug
= -1; /* -1 == use B44_DEF_MSG_ENABLE as value */
87 module_param(b44_debug
, int, 0);
88 MODULE_PARM_DESC(b44_debug
, "B44 bitmapped debugging message enable value");
90 static struct pci_device_id b44_pci_tbl
[] = {
91 { PCI_VENDOR_ID_BROADCOM
, PCI_DEVICE_ID_BCM4401
,
92 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
93 { PCI_VENDOR_ID_BROADCOM
, PCI_DEVICE_ID_BCM4401B0
,
94 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
95 { PCI_VENDOR_ID_BROADCOM
, PCI_DEVICE_ID_BCM4401B1
,
96 PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0UL },
97 { } /* terminate list with empty entry */
100 MODULE_DEVICE_TABLE(pci
, b44_pci_tbl
);
102 static void b44_halt(struct b44
*);
103 static void b44_init_rings(struct b44
*);
104 static void b44_init_hw(struct b44
*);
106 static int dma_desc_align_mask
;
107 static int dma_desc_sync_size
;
109 static inline void b44_sync_dma_desc_for_device(struct pci_dev
*pdev
,
111 unsigned long offset
,
112 enum dma_data_direction dir
)
114 dma_sync_single_range_for_device(&pdev
->dev
, dma_base
,
115 offset
& dma_desc_align_mask
,
116 dma_desc_sync_size
, dir
);
119 static inline void b44_sync_dma_desc_for_cpu(struct pci_dev
*pdev
,
121 unsigned long offset
,
122 enum dma_data_direction dir
)
124 dma_sync_single_range_for_cpu(&pdev
->dev
, dma_base
,
125 offset
& dma_desc_align_mask
,
126 dma_desc_sync_size
, dir
);
129 static inline unsigned long br32(const struct b44
*bp
, unsigned long reg
)
131 return readl(bp
->regs
+ reg
);
134 static inline void bw32(const struct b44
*bp
,
135 unsigned long reg
, unsigned long val
)
137 writel(val
, bp
->regs
+ reg
);
140 static int b44_wait_bit(struct b44
*bp
, unsigned long reg
,
141 u32 bit
, unsigned long timeout
, const int clear
)
145 for (i
= 0; i
< timeout
; i
++) {
146 u32 val
= br32(bp
, reg
);
148 if (clear
&& !(val
& bit
))
150 if (!clear
&& (val
& bit
))
155 printk(KERN_ERR PFX
"%s: BUG! Timeout waiting for bit %08x of register "
159 (clear
? "clear" : "set"));
165 /* Sonics SiliconBackplane support routines. ROFL, you should see all the
166 * buzz words used on this company's website :-)
168 * All of these routines must be invoked with bp->lock held and
169 * interrupts disabled.
172 #define SB_PCI_DMA 0x40000000 /* Client Mode PCI memory access space (1 GB) */
173 #define BCM4400_PCI_CORE_ADDR 0x18002000 /* Address of PCI core on BCM4400 cards */
175 static u32
ssb_get_core_rev(struct b44
*bp
)
177 return (br32(bp
, B44_SBIDHIGH
) & SBIDHIGH_RC_MASK
);
180 static u32
ssb_pci_setup(struct b44
*bp
, u32 cores
)
182 u32 bar_orig
, pci_rev
, val
;
184 pci_read_config_dword(bp
->pdev
, SSB_BAR0_WIN
, &bar_orig
);
185 pci_write_config_dword(bp
->pdev
, SSB_BAR0_WIN
, BCM4400_PCI_CORE_ADDR
);
186 pci_rev
= ssb_get_core_rev(bp
);
188 val
= br32(bp
, B44_SBINTVEC
);
190 bw32(bp
, B44_SBINTVEC
, val
);
192 val
= br32(bp
, SSB_PCI_TRANS_2
);
193 val
|= SSB_PCI_PREF
| SSB_PCI_BURST
;
194 bw32(bp
, SSB_PCI_TRANS_2
, val
);
196 pci_write_config_dword(bp
->pdev
, SSB_BAR0_WIN
, bar_orig
);
201 static void ssb_core_disable(struct b44
*bp
)
203 if (br32(bp
, B44_SBTMSLOW
) & SBTMSLOW_RESET
)
206 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_REJECT
| SBTMSLOW_CLOCK
));
207 b44_wait_bit(bp
, B44_SBTMSLOW
, SBTMSLOW_REJECT
, 100000, 0);
208 b44_wait_bit(bp
, B44_SBTMSHIGH
, SBTMSHIGH_BUSY
, 100000, 1);
209 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_FGC
| SBTMSLOW_CLOCK
|
210 SBTMSLOW_REJECT
| SBTMSLOW_RESET
));
211 br32(bp
, B44_SBTMSLOW
);
213 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_REJECT
| SBTMSLOW_RESET
));
214 br32(bp
, B44_SBTMSLOW
);
218 static void ssb_core_reset(struct b44
*bp
)
222 ssb_core_disable(bp
);
223 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_RESET
| SBTMSLOW_CLOCK
| SBTMSLOW_FGC
));
224 br32(bp
, B44_SBTMSLOW
);
227 /* Clear SERR if set, this is a hw bug workaround. */
228 if (br32(bp
, B44_SBTMSHIGH
) & SBTMSHIGH_SERR
)
229 bw32(bp
, B44_SBTMSHIGH
, 0);
231 val
= br32(bp
, B44_SBIMSTATE
);
232 if (val
& (SBIMSTATE_IBE
| SBIMSTATE_TO
))
233 bw32(bp
, B44_SBIMSTATE
, val
& ~(SBIMSTATE_IBE
| SBIMSTATE_TO
));
235 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_CLOCK
| SBTMSLOW_FGC
));
236 br32(bp
, B44_SBTMSLOW
);
239 bw32(bp
, B44_SBTMSLOW
, (SBTMSLOW_CLOCK
));
240 br32(bp
, B44_SBTMSLOW
);
244 static int ssb_core_unit(struct b44
*bp
)
247 u32 val
= br32(bp
, B44_SBADMATCH0
);
250 type
= val
& SBADMATCH0_TYPE_MASK
;
253 base
= val
& SBADMATCH0_BS0_MASK
;
257 base
= val
& SBADMATCH0_BS1_MASK
;
262 base
= val
& SBADMATCH0_BS2_MASK
;
269 static int ssb_is_core_up(struct b44
*bp
)
271 return ((br32(bp
, B44_SBTMSLOW
) & (SBTMSLOW_RESET
| SBTMSLOW_REJECT
| SBTMSLOW_CLOCK
))
275 static void __b44_cam_write(struct b44
*bp
, unsigned char *data
, int index
)
279 val
= ((u32
) data
[2]) << 24;
280 val
|= ((u32
) data
[3]) << 16;
281 val
|= ((u32
) data
[4]) << 8;
282 val
|= ((u32
) data
[5]) << 0;
283 bw32(bp
, B44_CAM_DATA_LO
, val
);
284 val
= (CAM_DATA_HI_VALID
|
285 (((u32
) data
[0]) << 8) |
286 (((u32
) data
[1]) << 0));
287 bw32(bp
, B44_CAM_DATA_HI
, val
);
288 bw32(bp
, B44_CAM_CTRL
, (CAM_CTRL_WRITE
|
289 (index
<< CAM_CTRL_INDEX_SHIFT
)));
290 b44_wait_bit(bp
, B44_CAM_CTRL
, CAM_CTRL_BUSY
, 100, 1);
293 static inline void __b44_disable_ints(struct b44
*bp
)
295 bw32(bp
, B44_IMASK
, 0);
298 static void b44_disable_ints(struct b44
*bp
)
300 __b44_disable_ints(bp
);
302 /* Flush posted writes. */
306 static void b44_enable_ints(struct b44
*bp
)
308 bw32(bp
, B44_IMASK
, bp
->imask
);
311 static int b44_readphy(struct b44
*bp
, int reg
, u32
*val
)
315 bw32(bp
, B44_EMAC_ISTAT
, EMAC_INT_MII
);
316 bw32(bp
, B44_MDIO_DATA
, (MDIO_DATA_SB_START
|
317 (MDIO_OP_READ
<< MDIO_DATA_OP_SHIFT
) |
318 (bp
->phy_addr
<< MDIO_DATA_PMD_SHIFT
) |
319 (reg
<< MDIO_DATA_RA_SHIFT
) |
320 (MDIO_TA_VALID
<< MDIO_DATA_TA_SHIFT
)));
321 err
= b44_wait_bit(bp
, B44_EMAC_ISTAT
, EMAC_INT_MII
, 100, 0);
322 *val
= br32(bp
, B44_MDIO_DATA
) & MDIO_DATA_DATA
;
327 static int b44_writephy(struct b44
*bp
, int reg
, u32 val
)
329 bw32(bp
, B44_EMAC_ISTAT
, EMAC_INT_MII
);
330 bw32(bp
, B44_MDIO_DATA
, (MDIO_DATA_SB_START
|
331 (MDIO_OP_WRITE
<< MDIO_DATA_OP_SHIFT
) |
332 (bp
->phy_addr
<< MDIO_DATA_PMD_SHIFT
) |
333 (reg
<< MDIO_DATA_RA_SHIFT
) |
334 (MDIO_TA_VALID
<< MDIO_DATA_TA_SHIFT
) |
335 (val
& MDIO_DATA_DATA
)));
336 return b44_wait_bit(bp
, B44_EMAC_ISTAT
, EMAC_INT_MII
, 100, 0);
339 /* miilib interface */
340 /* FIXME FIXME: phy_id is ignored, bp->phy_addr use is unconditional
341 * due to code existing before miilib use was added to this driver.
342 * Someone should remove this artificial driver limitation in
343 * b44_{read,write}phy. bp->phy_addr itself is fine (and needed).
345 static int b44_mii_read(struct net_device
*dev
, int phy_id
, int location
)
348 struct b44
*bp
= netdev_priv(dev
);
349 int rc
= b44_readphy(bp
, location
, &val
);
355 static void b44_mii_write(struct net_device
*dev
, int phy_id
, int location
,
358 struct b44
*bp
= netdev_priv(dev
);
359 b44_writephy(bp
, location
, val
);
362 static int b44_phy_reset(struct b44
*bp
)
367 err
= b44_writephy(bp
, MII_BMCR
, BMCR_RESET
);
371 err
= b44_readphy(bp
, MII_BMCR
, &val
);
373 if (val
& BMCR_RESET
) {
374 printk(KERN_ERR PFX
"%s: PHY Reset would not complete.\n",
383 static void __b44_set_flow_ctrl(struct b44
*bp
, u32 pause_flags
)
387 bp
->flags
&= ~(B44_FLAG_TX_PAUSE
| B44_FLAG_RX_PAUSE
);
388 bp
->flags
|= pause_flags
;
390 val
= br32(bp
, B44_RXCONFIG
);
391 if (pause_flags
& B44_FLAG_RX_PAUSE
)
392 val
|= RXCONFIG_FLOW
;
394 val
&= ~RXCONFIG_FLOW
;
395 bw32(bp
, B44_RXCONFIG
, val
);
397 val
= br32(bp
, B44_MAC_FLOW
);
398 if (pause_flags
& B44_FLAG_TX_PAUSE
)
399 val
|= (MAC_FLOW_PAUSE_ENAB
|
400 (0xc0 & MAC_FLOW_RX_HI_WATER
));
402 val
&= ~MAC_FLOW_PAUSE_ENAB
;
403 bw32(bp
, B44_MAC_FLOW
, val
);
406 static void b44_set_flow_ctrl(struct b44
*bp
, u32 local
, u32 remote
)
408 u32 pause_enab
= bp
->flags
& (B44_FLAG_TX_PAUSE
|
411 if (local
& ADVERTISE_PAUSE_CAP
) {
412 if (local
& ADVERTISE_PAUSE_ASYM
) {
413 if (remote
& LPA_PAUSE_CAP
)
414 pause_enab
|= (B44_FLAG_TX_PAUSE
|
416 else if (remote
& LPA_PAUSE_ASYM
)
417 pause_enab
|= B44_FLAG_RX_PAUSE
;
419 if (remote
& LPA_PAUSE_CAP
)
420 pause_enab
|= (B44_FLAG_TX_PAUSE
|
423 } else if (local
& ADVERTISE_PAUSE_ASYM
) {
424 if ((remote
& LPA_PAUSE_CAP
) &&
425 (remote
& LPA_PAUSE_ASYM
))
426 pause_enab
|= B44_FLAG_TX_PAUSE
;
429 __b44_set_flow_ctrl(bp
, pause_enab
);
432 static int b44_setup_phy(struct b44
*bp
)
437 if ((err
= b44_readphy(bp
, B44_MII_ALEDCTRL
, &val
)) != 0)
439 if ((err
= b44_writephy(bp
, B44_MII_ALEDCTRL
,
440 val
& MII_ALEDCTRL_ALLMSK
)) != 0)
442 if ((err
= b44_readphy(bp
, B44_MII_TLEDCTRL
, &val
)) != 0)
444 if ((err
= b44_writephy(bp
, B44_MII_TLEDCTRL
,
445 val
| MII_TLEDCTRL_ENABLE
)) != 0)
448 if (!(bp
->flags
& B44_FLAG_FORCE_LINK
)) {
449 u32 adv
= ADVERTISE_CSMA
;
451 if (bp
->flags
& B44_FLAG_ADV_10HALF
)
452 adv
|= ADVERTISE_10HALF
;
453 if (bp
->flags
& B44_FLAG_ADV_10FULL
)
454 adv
|= ADVERTISE_10FULL
;
455 if (bp
->flags
& B44_FLAG_ADV_100HALF
)
456 adv
|= ADVERTISE_100HALF
;
457 if (bp
->flags
& B44_FLAG_ADV_100FULL
)
458 adv
|= ADVERTISE_100FULL
;
460 if (bp
->flags
& B44_FLAG_PAUSE_AUTO
)
461 adv
|= ADVERTISE_PAUSE_CAP
| ADVERTISE_PAUSE_ASYM
;
463 if ((err
= b44_writephy(bp
, MII_ADVERTISE
, adv
)) != 0)
465 if ((err
= b44_writephy(bp
, MII_BMCR
, (BMCR_ANENABLE
|
466 BMCR_ANRESTART
))) != 0)
471 if ((err
= b44_readphy(bp
, MII_BMCR
, &bmcr
)) != 0)
473 bmcr
&= ~(BMCR_FULLDPLX
| BMCR_ANENABLE
| BMCR_SPEED100
);
474 if (bp
->flags
& B44_FLAG_100_BASE_T
)
475 bmcr
|= BMCR_SPEED100
;
476 if (bp
->flags
& B44_FLAG_FULL_DUPLEX
)
477 bmcr
|= BMCR_FULLDPLX
;
478 if ((err
= b44_writephy(bp
, MII_BMCR
, bmcr
)) != 0)
481 /* Since we will not be negotiating there is no safe way
482 * to determine if the link partner supports flow control
483 * or not. So just disable it completely in this case.
485 b44_set_flow_ctrl(bp
, 0, 0);
492 static void b44_stats_update(struct b44
*bp
)
497 val
= &bp
->hw_stats
.tx_good_octets
;
498 for (reg
= B44_TX_GOOD_O
; reg
<= B44_TX_PAUSE
; reg
+= 4UL) {
499 *val
++ += br32(bp
, reg
);
501 val
= &bp
->hw_stats
.rx_good_octets
;
502 for (reg
= B44_RX_GOOD_O
; reg
<= B44_RX_NPAUSE
; reg
+= 4UL) {
503 *val
++ += br32(bp
, reg
);
507 static void b44_link_report(struct b44
*bp
)
509 if (!netif_carrier_ok(bp
->dev
)) {
510 printk(KERN_INFO PFX
"%s: Link is down.\n", bp
->dev
->name
);
512 printk(KERN_INFO PFX
"%s: Link is up at %d Mbps, %s duplex.\n",
514 (bp
->flags
& B44_FLAG_100_BASE_T
) ? 100 : 10,
515 (bp
->flags
& B44_FLAG_FULL_DUPLEX
) ? "full" : "half");
517 printk(KERN_INFO PFX
"%s: Flow control is %s for TX and "
520 (bp
->flags
& B44_FLAG_TX_PAUSE
) ? "on" : "off",
521 (bp
->flags
& B44_FLAG_RX_PAUSE
) ? "on" : "off");
525 static void b44_check_phy(struct b44
*bp
)
529 if (!b44_readphy(bp
, MII_BMSR
, &bmsr
) &&
530 !b44_readphy(bp
, B44_MII_AUXCTRL
, &aux
) &&
532 if (aux
& MII_AUXCTRL_SPEED
)
533 bp
->flags
|= B44_FLAG_100_BASE_T
;
535 bp
->flags
&= ~B44_FLAG_100_BASE_T
;
536 if (aux
& MII_AUXCTRL_DUPLEX
)
537 bp
->flags
|= B44_FLAG_FULL_DUPLEX
;
539 bp
->flags
&= ~B44_FLAG_FULL_DUPLEX
;
541 if (!netif_carrier_ok(bp
->dev
) &&
542 (bmsr
& BMSR_LSTATUS
)) {
543 u32 val
= br32(bp
, B44_TX_CTRL
);
544 u32 local_adv
, remote_adv
;
546 if (bp
->flags
& B44_FLAG_FULL_DUPLEX
)
547 val
|= TX_CTRL_DUPLEX
;
549 val
&= ~TX_CTRL_DUPLEX
;
550 bw32(bp
, B44_TX_CTRL
, val
);
552 if (!(bp
->flags
& B44_FLAG_FORCE_LINK
) &&
553 !b44_readphy(bp
, MII_ADVERTISE
, &local_adv
) &&
554 !b44_readphy(bp
, MII_LPA
, &remote_adv
))
555 b44_set_flow_ctrl(bp
, local_adv
, remote_adv
);
558 netif_carrier_on(bp
->dev
);
560 } else if (netif_carrier_ok(bp
->dev
) && !(bmsr
& BMSR_LSTATUS
)) {
562 netif_carrier_off(bp
->dev
);
566 if (bmsr
& BMSR_RFAULT
)
567 printk(KERN_WARNING PFX
"%s: Remote fault detected in PHY\n",
570 printk(KERN_WARNING PFX
"%s: Jabber detected in PHY\n",
575 static void b44_timer(unsigned long __opaque
)
577 struct b44
*bp
= (struct b44
*) __opaque
;
579 spin_lock_irq(&bp
->lock
);
583 b44_stats_update(bp
);
585 spin_unlock_irq(&bp
->lock
);
587 bp
->timer
.expires
= jiffies
+ HZ
;
588 add_timer(&bp
->timer
);
591 static void b44_tx(struct b44
*bp
)
595 cur
= br32(bp
, B44_DMATX_STAT
) & DMATX_STAT_CDMASK
;
596 cur
/= sizeof(struct dma_desc
);
598 /* XXX needs updating when NETIF_F_SG is supported */
599 for (cons
= bp
->tx_cons
; cons
!= cur
; cons
= NEXT_TX(cons
)) {
600 struct ring_info
*rp
= &bp
->tx_buffers
[cons
];
601 struct sk_buff
*skb
= rp
->skb
;
603 if (unlikely(skb
== NULL
))
606 pci_unmap_single(bp
->pdev
,
607 pci_unmap_addr(rp
, mapping
),
611 dev_kfree_skb_irq(skb
);
615 if (netif_queue_stopped(bp
->dev
) &&
616 TX_BUFFS_AVAIL(bp
) > B44_TX_WAKEUP_THRESH
)
617 netif_wake_queue(bp
->dev
);
619 bw32(bp
, B44_GPTIMER
, 0);
622 /* Works like this. This chip writes a 'struct rx_header" 30 bytes
623 * before the DMA address you give it. So we allocate 30 more bytes
624 * for the RX buffer, DMA map all of it, skb_reserve the 30 bytes, then
625 * point the chip at 30 bytes past where the rx_header will go.
627 static int b44_alloc_rx_skb(struct b44
*bp
, int src_idx
, u32 dest_idx_unmasked
)
630 struct ring_info
*src_map
, *map
;
631 struct rx_header
*rh
;
639 src_map
= &bp
->rx_buffers
[src_idx
];
640 dest_idx
= dest_idx_unmasked
& (B44_RX_RING_SIZE
- 1);
641 map
= &bp
->rx_buffers
[dest_idx
];
642 skb
= dev_alloc_skb(RX_PKT_BUF_SZ
);
646 mapping
= pci_map_single(bp
->pdev
, skb
->data
,
650 /* Hardware bug work-around, the chip is unable to do PCI DMA
651 to/from anything above 1GB :-( */
652 if (mapping
+ RX_PKT_BUF_SZ
> B44_DMA_MASK
) {
654 pci_unmap_single(bp
->pdev
, mapping
, RX_PKT_BUF_SZ
,PCI_DMA_FROMDEVICE
);
655 dev_kfree_skb_any(skb
);
656 skb
= __dev_alloc_skb(RX_PKT_BUF_SZ
,GFP_DMA
);
659 mapping
= pci_map_single(bp
->pdev
, skb
->data
,
662 if (mapping
+ RX_PKT_BUF_SZ
> B44_DMA_MASK
) {
663 pci_unmap_single(bp
->pdev
, mapping
, RX_PKT_BUF_SZ
,PCI_DMA_FROMDEVICE
);
664 dev_kfree_skb_any(skb
);
670 skb_reserve(skb
, bp
->rx_offset
);
672 rh
= (struct rx_header
*)
673 (skb
->data
- bp
->rx_offset
);
678 pci_unmap_addr_set(map
, mapping
, mapping
);
683 ctrl
= (DESC_CTRL_LEN
& (RX_PKT_BUF_SZ
- bp
->rx_offset
));
684 if (dest_idx
== (B44_RX_RING_SIZE
- 1))
685 ctrl
|= DESC_CTRL_EOT
;
687 dp
= &bp
->rx_ring
[dest_idx
];
688 dp
->ctrl
= cpu_to_le32(ctrl
);
689 dp
->addr
= cpu_to_le32((u32
) mapping
+ bp
->rx_offset
+ bp
->dma_offset
);
691 if (bp
->flags
& B44_FLAG_RX_RING_HACK
)
692 b44_sync_dma_desc_for_device(bp
->pdev
, bp
->rx_ring_dma
,
693 dest_idx
* sizeof(dp
),
696 return RX_PKT_BUF_SZ
;
699 static void b44_recycle_rx(struct b44
*bp
, int src_idx
, u32 dest_idx_unmasked
)
701 struct dma_desc
*src_desc
, *dest_desc
;
702 struct ring_info
*src_map
, *dest_map
;
703 struct rx_header
*rh
;
707 dest_idx
= dest_idx_unmasked
& (B44_RX_RING_SIZE
- 1);
708 dest_desc
= &bp
->rx_ring
[dest_idx
];
709 dest_map
= &bp
->rx_buffers
[dest_idx
];
710 src_desc
= &bp
->rx_ring
[src_idx
];
711 src_map
= &bp
->rx_buffers
[src_idx
];
713 dest_map
->skb
= src_map
->skb
;
714 rh
= (struct rx_header
*) src_map
->skb
->data
;
717 pci_unmap_addr_set(dest_map
, mapping
,
718 pci_unmap_addr(src_map
, mapping
));
720 if (bp
->flags
& B44_FLAG_RX_RING_HACK
)
721 b44_sync_dma_desc_for_cpu(bp
->pdev
, bp
->rx_ring_dma
,
722 src_idx
* sizeof(src_desc
),
725 ctrl
= src_desc
->ctrl
;
726 if (dest_idx
== (B44_RX_RING_SIZE
- 1))
727 ctrl
|= cpu_to_le32(DESC_CTRL_EOT
);
729 ctrl
&= cpu_to_le32(~DESC_CTRL_EOT
);
731 dest_desc
->ctrl
= ctrl
;
732 dest_desc
->addr
= src_desc
->addr
;
736 if (bp
->flags
& B44_FLAG_RX_RING_HACK
)
737 b44_sync_dma_desc_for_device(bp
->pdev
, bp
->rx_ring_dma
,
738 dest_idx
* sizeof(dest_desc
),
741 pci_dma_sync_single_for_device(bp
->pdev
, src_desc
->addr
,
746 static int b44_rx(struct b44
*bp
, int budget
)
752 prod
= br32(bp
, B44_DMARX_STAT
) & DMARX_STAT_CDMASK
;
753 prod
/= sizeof(struct dma_desc
);
756 while (cons
!= prod
&& budget
> 0) {
757 struct ring_info
*rp
= &bp
->rx_buffers
[cons
];
758 struct sk_buff
*skb
= rp
->skb
;
759 dma_addr_t map
= pci_unmap_addr(rp
, mapping
);
760 struct rx_header
*rh
;
763 pci_dma_sync_single_for_cpu(bp
->pdev
, map
,
766 rh
= (struct rx_header
*) skb
->data
;
767 len
= cpu_to_le16(rh
->len
);
768 if ((len
> (RX_PKT_BUF_SZ
- bp
->rx_offset
)) ||
769 (rh
->flags
& cpu_to_le16(RX_FLAG_ERRORS
))) {
771 b44_recycle_rx(bp
, cons
, bp
->rx_prod
);
773 bp
->stats
.rx_dropped
++;
783 len
= cpu_to_le16(rh
->len
);
784 } while (len
== 0 && i
++ < 5);
792 if (len
> RX_COPY_THRESHOLD
) {
794 skb_size
= b44_alloc_rx_skb(bp
, cons
, bp
->rx_prod
);
797 pci_unmap_single(bp
->pdev
, map
,
798 skb_size
, PCI_DMA_FROMDEVICE
);
799 /* Leave out rx_header */
800 skb_put(skb
, len
+bp
->rx_offset
);
801 skb_pull(skb
,bp
->rx_offset
);
803 struct sk_buff
*copy_skb
;
805 b44_recycle_rx(bp
, cons
, bp
->rx_prod
);
806 copy_skb
= dev_alloc_skb(len
+ 2);
807 if (copy_skb
== NULL
)
808 goto drop_it_no_recycle
;
810 copy_skb
->dev
= bp
->dev
;
811 skb_reserve(copy_skb
, 2);
812 skb_put(copy_skb
, len
);
813 /* DMA sync done above, copy just the actual packet */
814 memcpy(copy_skb
->data
, skb
->data
+bp
->rx_offset
, len
);
818 skb
->ip_summed
= CHECKSUM_NONE
;
819 skb
->protocol
= eth_type_trans(skb
, bp
->dev
);
820 netif_receive_skb(skb
);
821 bp
->dev
->last_rx
= jiffies
;
825 bp
->rx_prod
= (bp
->rx_prod
+ 1) &
826 (B44_RX_RING_SIZE
- 1);
827 cons
= (cons
+ 1) & (B44_RX_RING_SIZE
- 1);
831 bw32(bp
, B44_DMARX_PTR
, cons
* sizeof(struct dma_desc
));
836 static int b44_poll(struct net_device
*netdev
, int *budget
)
838 struct b44
*bp
= netdev_priv(netdev
);
841 spin_lock_irq(&bp
->lock
);
843 if (bp
->istat
& (ISTAT_TX
| ISTAT_TO
)) {
844 /* spin_lock(&bp->tx_lock); */
846 /* spin_unlock(&bp->tx_lock); */
848 spin_unlock_irq(&bp
->lock
);
851 if (bp
->istat
& ISTAT_RX
) {
852 int orig_budget
= *budget
;
855 if (orig_budget
> netdev
->quota
)
856 orig_budget
= netdev
->quota
;
858 work_done
= b44_rx(bp
, orig_budget
);
860 *budget
-= work_done
;
861 netdev
->quota
-= work_done
;
863 if (work_done
>= orig_budget
)
867 if (bp
->istat
& ISTAT_ERRORS
) {
868 spin_lock_irq(&bp
->lock
);
872 netif_wake_queue(bp
->dev
);
873 spin_unlock_irq(&bp
->lock
);
878 netif_rx_complete(netdev
);
882 return (done
? 0 : 1);
885 static irqreturn_t
b44_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
887 struct net_device
*dev
= dev_id
;
888 struct b44
*bp
= netdev_priv(dev
);
893 spin_lock_irqsave(&bp
->lock
, flags
);
895 istat
= br32(bp
, B44_ISTAT
);
896 imask
= br32(bp
, B44_IMASK
);
898 /* ??? What the fuck is the purpose of the interrupt mask
899 * ??? register if we have to mask it out by hand anyways?
904 if (netif_rx_schedule_prep(dev
)) {
905 /* NOTE: These writes are posted by the readback of
906 * the ISTAT register below.
909 __b44_disable_ints(bp
);
910 __netif_rx_schedule(dev
);
912 printk(KERN_ERR PFX
"%s: Error, poll already scheduled\n",
916 bw32(bp
, B44_ISTAT
, istat
);
919 spin_unlock_irqrestore(&bp
->lock
, flags
);
920 return IRQ_RETVAL(handled
);
923 static void b44_tx_timeout(struct net_device
*dev
)
925 struct b44
*bp
= netdev_priv(dev
);
927 printk(KERN_ERR PFX
"%s: transmit timed out, resetting\n",
930 spin_lock_irq(&bp
->lock
);
936 spin_unlock_irq(&bp
->lock
);
940 netif_wake_queue(dev
);
943 static int b44_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
945 struct b44
*bp
= netdev_priv(dev
);
946 struct sk_buff
*bounce_skb
;
947 int rc
= NETDEV_TX_OK
;
949 u32 len
, entry
, ctrl
;
952 spin_lock_irq(&bp
->lock
);
954 /* This is a hard error, log it. */
955 if (unlikely(TX_BUFFS_AVAIL(bp
) < 1)) {
956 netif_stop_queue(dev
);
957 printk(KERN_ERR PFX
"%s: BUG! Tx Ring full when queue awake!\n",
962 mapping
= pci_map_single(bp
->pdev
, skb
->data
, len
, PCI_DMA_TODEVICE
);
963 if (mapping
+ len
> B44_DMA_MASK
) {
964 /* Chip can't handle DMA to/from >1GB, use bounce buffer */
965 pci_unmap_single(bp
->pdev
, mapping
, len
, PCI_DMA_TODEVICE
);
967 bounce_skb
= __dev_alloc_skb(TX_PKT_BUF_SZ
,
972 mapping
= pci_map_single(bp
->pdev
, bounce_skb
->data
,
973 len
, PCI_DMA_TODEVICE
);
974 if (mapping
+ len
> B44_DMA_MASK
) {
975 pci_unmap_single(bp
->pdev
, mapping
,
976 len
, PCI_DMA_TODEVICE
);
977 dev_kfree_skb_any(bounce_skb
);
981 memcpy(skb_put(bounce_skb
, len
), skb
->data
, skb
->len
);
982 dev_kfree_skb_any(skb
);
987 bp
->tx_buffers
[entry
].skb
= skb
;
988 pci_unmap_addr_set(&bp
->tx_buffers
[entry
], mapping
, mapping
);
990 ctrl
= (len
& DESC_CTRL_LEN
);
991 ctrl
|= DESC_CTRL_IOC
| DESC_CTRL_SOF
| DESC_CTRL_EOF
;
992 if (entry
== (B44_TX_RING_SIZE
- 1))
993 ctrl
|= DESC_CTRL_EOT
;
995 bp
->tx_ring
[entry
].ctrl
= cpu_to_le32(ctrl
);
996 bp
->tx_ring
[entry
].addr
= cpu_to_le32((u32
) mapping
+bp
->dma_offset
);
998 if (bp
->flags
& B44_FLAG_TX_RING_HACK
)
999 b44_sync_dma_desc_for_device(bp
->pdev
, bp
->tx_ring_dma
,
1000 entry
* sizeof(bp
->tx_ring
[0]),
1003 entry
= NEXT_TX(entry
);
1005 bp
->tx_prod
= entry
;
1009 bw32(bp
, B44_DMATX_PTR
, entry
* sizeof(struct dma_desc
));
1010 if (bp
->flags
& B44_FLAG_BUGGY_TXPTR
)
1011 bw32(bp
, B44_DMATX_PTR
, entry
* sizeof(struct dma_desc
));
1012 if (bp
->flags
& B44_FLAG_REORDER_BUG
)
1013 br32(bp
, B44_DMATX_PTR
);
1015 if (TX_BUFFS_AVAIL(bp
) < 1)
1016 netif_stop_queue(dev
);
1018 dev
->trans_start
= jiffies
;
1021 spin_unlock_irq(&bp
->lock
);
1026 rc
= NETDEV_TX_BUSY
;
1030 static int b44_change_mtu(struct net_device
*dev
, int new_mtu
)
1032 struct b44
*bp
= netdev_priv(dev
);
1034 if (new_mtu
< B44_MIN_MTU
|| new_mtu
> B44_MAX_MTU
)
1037 if (!netif_running(dev
)) {
1038 /* We'll just catch it later when the
1045 spin_lock_irq(&bp
->lock
);
1050 spin_unlock_irq(&bp
->lock
);
1052 b44_enable_ints(bp
);
1057 /* Free up pending packets in all rx/tx rings.
1059 * The chip has been shut down and the driver detached from
1060 * the networking, so no interrupts or new tx packets will
1061 * end up in the driver. bp->lock is not held and we are not
1062 * in an interrupt context and thus may sleep.
1064 static void b44_free_rings(struct b44
*bp
)
1066 struct ring_info
*rp
;
1069 for (i
= 0; i
< B44_RX_RING_SIZE
; i
++) {
1070 rp
= &bp
->rx_buffers
[i
];
1072 if (rp
->skb
== NULL
)
1074 pci_unmap_single(bp
->pdev
,
1075 pci_unmap_addr(rp
, mapping
),
1077 PCI_DMA_FROMDEVICE
);
1078 dev_kfree_skb_any(rp
->skb
);
1082 /* XXX needs changes once NETIF_F_SG is set... */
1083 for (i
= 0; i
< B44_TX_RING_SIZE
; i
++) {
1084 rp
= &bp
->tx_buffers
[i
];
1086 if (rp
->skb
== NULL
)
1088 pci_unmap_single(bp
->pdev
,
1089 pci_unmap_addr(rp
, mapping
),
1092 dev_kfree_skb_any(rp
->skb
);
1097 /* Initialize tx/rx rings for packet processing.
1099 * The chip has been shut down and the driver detached from
1100 * the networking, so no interrupts or new tx packets will
1101 * end up in the driver.
1103 static void b44_init_rings(struct b44
*bp
)
1109 memset(bp
->rx_ring
, 0, B44_RX_RING_BYTES
);
1110 memset(bp
->tx_ring
, 0, B44_TX_RING_BYTES
);
1112 if (bp
->flags
& B44_FLAG_RX_RING_HACK
)
1113 dma_sync_single_for_device(&bp
->pdev
->dev
, bp
->rx_ring_dma
,
1115 PCI_DMA_BIDIRECTIONAL
);
1117 if (bp
->flags
& B44_FLAG_TX_RING_HACK
)
1118 dma_sync_single_for_device(&bp
->pdev
->dev
, bp
->tx_ring_dma
,
1122 for (i
= 0; i
< bp
->rx_pending
; i
++) {
1123 if (b44_alloc_rx_skb(bp
, -1, i
) < 0)
1129 * Must not be invoked with interrupt sources disabled and
1130 * the hardware shutdown down.
1132 static void b44_free_consistent(struct b44
*bp
)
1134 kfree(bp
->rx_buffers
);
1135 bp
->rx_buffers
= NULL
;
1136 kfree(bp
->tx_buffers
);
1137 bp
->tx_buffers
= NULL
;
1139 if (bp
->flags
& B44_FLAG_RX_RING_HACK
) {
1140 dma_unmap_single(&bp
->pdev
->dev
, bp
->rx_ring_dma
,
1145 pci_free_consistent(bp
->pdev
, DMA_TABLE_BYTES
,
1146 bp
->rx_ring
, bp
->rx_ring_dma
);
1148 bp
->flags
&= ~B44_FLAG_RX_RING_HACK
;
1151 if (bp
->flags
& B44_FLAG_TX_RING_HACK
) {
1152 dma_unmap_single(&bp
->pdev
->dev
, bp
->tx_ring_dma
,
1157 pci_free_consistent(bp
->pdev
, DMA_TABLE_BYTES
,
1158 bp
->tx_ring
, bp
->tx_ring_dma
);
1160 bp
->flags
&= ~B44_FLAG_TX_RING_HACK
;
1165 * Must not be invoked with interrupt sources disabled and
1166 * the hardware shutdown down. Can sleep.
1168 static int b44_alloc_consistent(struct b44
*bp
)
1172 size
= B44_RX_RING_SIZE
* sizeof(struct ring_info
);
1173 bp
->rx_buffers
= kzalloc(size
, GFP_KERNEL
);
1174 if (!bp
->rx_buffers
)
1177 size
= B44_TX_RING_SIZE
* sizeof(struct ring_info
);
1178 bp
->tx_buffers
= kzalloc(size
, GFP_KERNEL
);
1179 if (!bp
->tx_buffers
)
1182 size
= DMA_TABLE_BYTES
;
1183 bp
->rx_ring
= pci_alloc_consistent(bp
->pdev
, size
, &bp
->rx_ring_dma
);
1185 /* Allocation may have failed due to pci_alloc_consistent
1186 insisting on use of GFP_DMA, which is more restrictive
1187 than necessary... */
1188 struct dma_desc
*rx_ring
;
1189 dma_addr_t rx_ring_dma
;
1191 rx_ring
= kzalloc(size
, GFP_KERNEL
);
1195 rx_ring_dma
= dma_map_single(&bp
->pdev
->dev
, rx_ring
,
1199 if (rx_ring_dma
+ size
> B44_DMA_MASK
) {
1204 bp
->rx_ring
= rx_ring
;
1205 bp
->rx_ring_dma
= rx_ring_dma
;
1206 bp
->flags
|= B44_FLAG_RX_RING_HACK
;
1209 bp
->tx_ring
= pci_alloc_consistent(bp
->pdev
, size
, &bp
->tx_ring_dma
);
1211 /* Allocation may have failed due to pci_alloc_consistent
1212 insisting on use of GFP_DMA, which is more restrictive
1213 than necessary... */
1214 struct dma_desc
*tx_ring
;
1215 dma_addr_t tx_ring_dma
;
1217 tx_ring
= kzalloc(size
, GFP_KERNEL
);
1221 tx_ring_dma
= dma_map_single(&bp
->pdev
->dev
, tx_ring
,
1225 if (tx_ring_dma
+ size
> B44_DMA_MASK
) {
1230 bp
->tx_ring
= tx_ring
;
1231 bp
->tx_ring_dma
= tx_ring_dma
;
1232 bp
->flags
|= B44_FLAG_TX_RING_HACK
;
1238 b44_free_consistent(bp
);
1242 /* bp->lock is held. */
1243 static void b44_clear_stats(struct b44
*bp
)
1247 bw32(bp
, B44_MIB_CTRL
, MIB_CTRL_CLR_ON_READ
);
1248 for (reg
= B44_TX_GOOD_O
; reg
<= B44_TX_PAUSE
; reg
+= 4UL)
1250 for (reg
= B44_RX_GOOD_O
; reg
<= B44_RX_NPAUSE
; reg
+= 4UL)
1254 /* bp->lock is held. */
1255 static void b44_chip_reset(struct b44
*bp
)
1257 if (ssb_is_core_up(bp
)) {
1258 bw32(bp
, B44_RCV_LAZY
, 0);
1259 bw32(bp
, B44_ENET_CTRL
, ENET_CTRL_DISABLE
);
1260 b44_wait_bit(bp
, B44_ENET_CTRL
, ENET_CTRL_DISABLE
, 100, 1);
1261 bw32(bp
, B44_DMATX_CTRL
, 0);
1262 bp
->tx_prod
= bp
->tx_cons
= 0;
1263 if (br32(bp
, B44_DMARX_STAT
) & DMARX_STAT_EMASK
) {
1264 b44_wait_bit(bp
, B44_DMARX_STAT
, DMARX_STAT_SIDLE
,
1267 bw32(bp
, B44_DMARX_CTRL
, 0);
1268 bp
->rx_prod
= bp
->rx_cons
= 0;
1270 ssb_pci_setup(bp
, (bp
->core_unit
== 0 ?
1277 b44_clear_stats(bp
);
1279 /* Make PHY accessible. */
1280 bw32(bp
, B44_MDIO_CTRL
, (MDIO_CTRL_PREAMBLE
|
1281 (0x0d & MDIO_CTRL_MAXF_MASK
)));
1282 br32(bp
, B44_MDIO_CTRL
);
1284 if (!(br32(bp
, B44_DEVCTRL
) & DEVCTRL_IPP
)) {
1285 bw32(bp
, B44_ENET_CTRL
, ENET_CTRL_EPSEL
);
1286 br32(bp
, B44_ENET_CTRL
);
1287 bp
->flags
&= ~B44_FLAG_INTERNAL_PHY
;
1289 u32 val
= br32(bp
, B44_DEVCTRL
);
1291 if (val
& DEVCTRL_EPR
) {
1292 bw32(bp
, B44_DEVCTRL
, (val
& ~DEVCTRL_EPR
));
1293 br32(bp
, B44_DEVCTRL
);
1296 bp
->flags
|= B44_FLAG_INTERNAL_PHY
;
1300 /* bp->lock is held. */
1301 static void b44_halt(struct b44
*bp
)
1303 b44_disable_ints(bp
);
1307 /* bp->lock is held. */
1308 static void __b44_set_mac_addr(struct b44
*bp
)
1310 bw32(bp
, B44_CAM_CTRL
, 0);
1311 if (!(bp
->dev
->flags
& IFF_PROMISC
)) {
1314 __b44_cam_write(bp
, bp
->dev
->dev_addr
, 0);
1315 val
= br32(bp
, B44_CAM_CTRL
);
1316 bw32(bp
, B44_CAM_CTRL
, val
| CAM_CTRL_ENABLE
);
1320 static int b44_set_mac_addr(struct net_device
*dev
, void *p
)
1322 struct b44
*bp
= netdev_priv(dev
);
1323 struct sockaddr
*addr
= p
;
1325 if (netif_running(dev
))
1328 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
1330 spin_lock_irq(&bp
->lock
);
1331 __b44_set_mac_addr(bp
);
1332 spin_unlock_irq(&bp
->lock
);
1337 /* Called at device open time to get the chip ready for
1338 * packet processing. Invoked with bp->lock held.
1340 static void __b44_set_rx_mode(struct net_device
*);
1341 static void b44_init_hw(struct b44
*bp
)
1349 /* Enable CRC32, set proper LED modes and power on PHY */
1350 bw32(bp
, B44_MAC_CTRL
, MAC_CTRL_CRC32_ENAB
| MAC_CTRL_PHY_LEDCTRL
);
1351 bw32(bp
, B44_RCV_LAZY
, (1 << RCV_LAZY_FC_SHIFT
));
1353 /* This sets the MAC address too. */
1354 __b44_set_rx_mode(bp
->dev
);
1356 /* MTU + eth header + possible VLAN tag + struct rx_header */
1357 bw32(bp
, B44_RXMAXLEN
, bp
->dev
->mtu
+ ETH_HLEN
+ 8 + RX_HEADER_LEN
);
1358 bw32(bp
, B44_TXMAXLEN
, bp
->dev
->mtu
+ ETH_HLEN
+ 8 + RX_HEADER_LEN
);
1360 bw32(bp
, B44_TX_WMARK
, 56); /* XXX magic */
1361 bw32(bp
, B44_DMATX_CTRL
, DMATX_CTRL_ENABLE
);
1362 bw32(bp
, B44_DMATX_ADDR
, bp
->tx_ring_dma
+ bp
->dma_offset
);
1363 bw32(bp
, B44_DMARX_CTRL
, (DMARX_CTRL_ENABLE
|
1364 (bp
->rx_offset
<< DMARX_CTRL_ROSHIFT
)));
1365 bw32(bp
, B44_DMARX_ADDR
, bp
->rx_ring_dma
+ bp
->dma_offset
);
1367 bw32(bp
, B44_DMARX_PTR
, bp
->rx_pending
);
1368 bp
->rx_prod
= bp
->rx_pending
;
1370 bw32(bp
, B44_MIB_CTRL
, MIB_CTRL_CLR_ON_READ
);
1372 val
= br32(bp
, B44_ENET_CTRL
);
1373 bw32(bp
, B44_ENET_CTRL
, (val
| ENET_CTRL_ENABLE
));
1376 static int b44_open(struct net_device
*dev
)
1378 struct b44
*bp
= netdev_priv(dev
);
1381 err
= b44_alloc_consistent(bp
);
1385 err
= request_irq(dev
->irq
, b44_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
1389 spin_lock_irq(&bp
->lock
);
1393 bp
->flags
|= B44_FLAG_INIT_COMPLETE
;
1395 netif_carrier_off(dev
);
1398 spin_unlock_irq(&bp
->lock
);
1400 init_timer(&bp
->timer
);
1401 bp
->timer
.expires
= jiffies
+ HZ
;
1402 bp
->timer
.data
= (unsigned long) bp
;
1403 bp
->timer
.function
= b44_timer
;
1404 add_timer(&bp
->timer
);
1406 b44_enable_ints(bp
);
1411 b44_free_consistent(bp
);
1416 /*static*/ void b44_dump_state(struct b44
*bp
)
1418 u32 val32
, val32_2
, val32_3
, val32_4
, val32_5
;
1421 pci_read_config_word(bp
->pdev
, PCI_STATUS
, &val16
);
1422 printk("DEBUG: PCI status [%04x] \n", val16
);
1427 #ifdef CONFIG_NET_POLL_CONTROLLER
1429 * Polling receive - used by netconsole and other diagnostic tools
1430 * to allow network i/o with interrupts disabled.
1432 static void b44_poll_controller(struct net_device
*dev
)
1434 disable_irq(dev
->irq
);
1435 b44_interrupt(dev
->irq
, dev
, NULL
);
1436 enable_irq(dev
->irq
);
1440 static int b44_close(struct net_device
*dev
)
1442 struct b44
*bp
= netdev_priv(dev
);
1444 netif_stop_queue(dev
);
1446 del_timer_sync(&bp
->timer
);
1448 spin_lock_irq(&bp
->lock
);
1455 bp
->flags
&= ~B44_FLAG_INIT_COMPLETE
;
1456 netif_carrier_off(bp
->dev
);
1458 spin_unlock_irq(&bp
->lock
);
1460 free_irq(dev
->irq
, dev
);
1462 b44_free_consistent(bp
);
1467 static struct net_device_stats
*b44_get_stats(struct net_device
*dev
)
1469 struct b44
*bp
= netdev_priv(dev
);
1470 struct net_device_stats
*nstat
= &bp
->stats
;
1471 struct b44_hw_stats
*hwstat
= &bp
->hw_stats
;
1473 /* Convert HW stats into netdevice stats. */
1474 nstat
->rx_packets
= hwstat
->rx_pkts
;
1475 nstat
->tx_packets
= hwstat
->tx_pkts
;
1476 nstat
->rx_bytes
= hwstat
->rx_octets
;
1477 nstat
->tx_bytes
= hwstat
->tx_octets
;
1478 nstat
->tx_errors
= (hwstat
->tx_jabber_pkts
+
1479 hwstat
->tx_oversize_pkts
+
1480 hwstat
->tx_underruns
+
1481 hwstat
->tx_excessive_cols
+
1482 hwstat
->tx_late_cols
);
1483 nstat
->multicast
= hwstat
->tx_multicast_pkts
;
1484 nstat
->collisions
= hwstat
->tx_total_cols
;
1486 nstat
->rx_length_errors
= (hwstat
->rx_oversize_pkts
+
1487 hwstat
->rx_undersize
);
1488 nstat
->rx_over_errors
= hwstat
->rx_missed_pkts
;
1489 nstat
->rx_frame_errors
= hwstat
->rx_align_errs
;
1490 nstat
->rx_crc_errors
= hwstat
->rx_crc_errs
;
1491 nstat
->rx_errors
= (hwstat
->rx_jabber_pkts
+
1492 hwstat
->rx_oversize_pkts
+
1493 hwstat
->rx_missed_pkts
+
1494 hwstat
->rx_crc_align_errs
+
1495 hwstat
->rx_undersize
+
1496 hwstat
->rx_crc_errs
+
1497 hwstat
->rx_align_errs
+
1498 hwstat
->rx_symbol_errs
);
1500 nstat
->tx_aborted_errors
= hwstat
->tx_underruns
;
1502 /* Carrier lost counter seems to be broken for some devices */
1503 nstat
->tx_carrier_errors
= hwstat
->tx_carrier_lost
;
1509 static int __b44_load_mcast(struct b44
*bp
, struct net_device
*dev
)
1511 struct dev_mc_list
*mclist
;
1514 num_ents
= min_t(int, dev
->mc_count
, B44_MCAST_TABLE_SIZE
);
1515 mclist
= dev
->mc_list
;
1516 for (i
= 0; mclist
&& i
< num_ents
; i
++, mclist
= mclist
->next
) {
1517 __b44_cam_write(bp
, mclist
->dmi_addr
, i
+ 1);
1522 static void __b44_set_rx_mode(struct net_device
*dev
)
1524 struct b44
*bp
= netdev_priv(dev
);
1527 val
= br32(bp
, B44_RXCONFIG
);
1528 val
&= ~(RXCONFIG_PROMISC
| RXCONFIG_ALLMULTI
);
1529 if (dev
->flags
& IFF_PROMISC
) {
1530 val
|= RXCONFIG_PROMISC
;
1531 bw32(bp
, B44_RXCONFIG
, val
);
1533 unsigned char zero
[6] = {0, 0, 0, 0, 0, 0};
1536 __b44_set_mac_addr(bp
);
1538 if (dev
->flags
& IFF_ALLMULTI
)
1539 val
|= RXCONFIG_ALLMULTI
;
1541 i
= __b44_load_mcast(bp
, dev
);
1543 for (; i
< 64; i
++) {
1544 __b44_cam_write(bp
, zero
, i
);
1546 bw32(bp
, B44_RXCONFIG
, val
);
1547 val
= br32(bp
, B44_CAM_CTRL
);
1548 bw32(bp
, B44_CAM_CTRL
, val
| CAM_CTRL_ENABLE
);
1552 static void b44_set_rx_mode(struct net_device
*dev
)
1554 struct b44
*bp
= netdev_priv(dev
);
1556 spin_lock_irq(&bp
->lock
);
1557 __b44_set_rx_mode(dev
);
1558 spin_unlock_irq(&bp
->lock
);
1561 static u32
b44_get_msglevel(struct net_device
*dev
)
1563 struct b44
*bp
= netdev_priv(dev
);
1564 return bp
->msg_enable
;
1567 static void b44_set_msglevel(struct net_device
*dev
, u32 value
)
1569 struct b44
*bp
= netdev_priv(dev
);
1570 bp
->msg_enable
= value
;
1573 static void b44_get_drvinfo (struct net_device
*dev
, struct ethtool_drvinfo
*info
)
1575 struct b44
*bp
= netdev_priv(dev
);
1576 struct pci_dev
*pci_dev
= bp
->pdev
;
1578 strcpy (info
->driver
, DRV_MODULE_NAME
);
1579 strcpy (info
->version
, DRV_MODULE_VERSION
);
1580 strcpy (info
->bus_info
, pci_name(pci_dev
));
1583 static int b44_nway_reset(struct net_device
*dev
)
1585 struct b44
*bp
= netdev_priv(dev
);
1589 spin_lock_irq(&bp
->lock
);
1590 b44_readphy(bp
, MII_BMCR
, &bmcr
);
1591 b44_readphy(bp
, MII_BMCR
, &bmcr
);
1593 if (bmcr
& BMCR_ANENABLE
) {
1594 b44_writephy(bp
, MII_BMCR
,
1595 bmcr
| BMCR_ANRESTART
);
1598 spin_unlock_irq(&bp
->lock
);
1603 static int b44_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1605 struct b44
*bp
= netdev_priv(dev
);
1607 if (!(bp
->flags
& B44_FLAG_INIT_COMPLETE
))
1609 cmd
->supported
= (SUPPORTED_Autoneg
);
1610 cmd
->supported
|= (SUPPORTED_100baseT_Half
|
1611 SUPPORTED_100baseT_Full
|
1612 SUPPORTED_10baseT_Half
|
1613 SUPPORTED_10baseT_Full
|
1616 cmd
->advertising
= 0;
1617 if (bp
->flags
& B44_FLAG_ADV_10HALF
)
1618 cmd
->advertising
|= ADVERTISED_10baseT_Half
;
1619 if (bp
->flags
& B44_FLAG_ADV_10FULL
)
1620 cmd
->advertising
|= ADVERTISED_10baseT_Full
;
1621 if (bp
->flags
& B44_FLAG_ADV_100HALF
)
1622 cmd
->advertising
|= ADVERTISED_100baseT_Half
;
1623 if (bp
->flags
& B44_FLAG_ADV_100FULL
)
1624 cmd
->advertising
|= ADVERTISED_100baseT_Full
;
1625 cmd
->advertising
|= ADVERTISED_Pause
| ADVERTISED_Asym_Pause
;
1626 cmd
->speed
= (bp
->flags
& B44_FLAG_100_BASE_T
) ?
1627 SPEED_100
: SPEED_10
;
1628 cmd
->duplex
= (bp
->flags
& B44_FLAG_FULL_DUPLEX
) ?
1629 DUPLEX_FULL
: DUPLEX_HALF
;
1631 cmd
->phy_address
= bp
->phy_addr
;
1632 cmd
->transceiver
= (bp
->flags
& B44_FLAG_INTERNAL_PHY
) ?
1633 XCVR_INTERNAL
: XCVR_EXTERNAL
;
1634 cmd
->autoneg
= (bp
->flags
& B44_FLAG_FORCE_LINK
) ?
1635 AUTONEG_DISABLE
: AUTONEG_ENABLE
;
1641 static int b44_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1643 struct b44
*bp
= netdev_priv(dev
);
1645 if (!(bp
->flags
& B44_FLAG_INIT_COMPLETE
))
1648 /* We do not support gigabit. */
1649 if (cmd
->autoneg
== AUTONEG_ENABLE
) {
1650 if (cmd
->advertising
&
1651 (ADVERTISED_1000baseT_Half
|
1652 ADVERTISED_1000baseT_Full
))
1654 } else if ((cmd
->speed
!= SPEED_100
&&
1655 cmd
->speed
!= SPEED_10
) ||
1656 (cmd
->duplex
!= DUPLEX_HALF
&&
1657 cmd
->duplex
!= DUPLEX_FULL
)) {
1661 spin_lock_irq(&bp
->lock
);
1663 if (cmd
->autoneg
== AUTONEG_ENABLE
) {
1664 bp
->flags
&= ~B44_FLAG_FORCE_LINK
;
1665 bp
->flags
&= ~(B44_FLAG_ADV_10HALF
|
1666 B44_FLAG_ADV_10FULL
|
1667 B44_FLAG_ADV_100HALF
|
1668 B44_FLAG_ADV_100FULL
);
1669 if (cmd
->advertising
& ADVERTISE_10HALF
)
1670 bp
->flags
|= B44_FLAG_ADV_10HALF
;
1671 if (cmd
->advertising
& ADVERTISE_10FULL
)
1672 bp
->flags
|= B44_FLAG_ADV_10FULL
;
1673 if (cmd
->advertising
& ADVERTISE_100HALF
)
1674 bp
->flags
|= B44_FLAG_ADV_100HALF
;
1675 if (cmd
->advertising
& ADVERTISE_100FULL
)
1676 bp
->flags
|= B44_FLAG_ADV_100FULL
;
1678 bp
->flags
|= B44_FLAG_FORCE_LINK
;
1679 if (cmd
->speed
== SPEED_100
)
1680 bp
->flags
|= B44_FLAG_100_BASE_T
;
1681 if (cmd
->duplex
== DUPLEX_FULL
)
1682 bp
->flags
|= B44_FLAG_FULL_DUPLEX
;
1687 spin_unlock_irq(&bp
->lock
);
1692 static void b44_get_ringparam(struct net_device
*dev
,
1693 struct ethtool_ringparam
*ering
)
1695 struct b44
*bp
= netdev_priv(dev
);
1697 ering
->rx_max_pending
= B44_RX_RING_SIZE
- 1;
1698 ering
->rx_pending
= bp
->rx_pending
;
1700 /* XXX ethtool lacks a tx_max_pending, oops... */
1703 static int b44_set_ringparam(struct net_device
*dev
,
1704 struct ethtool_ringparam
*ering
)
1706 struct b44
*bp
= netdev_priv(dev
);
1708 if ((ering
->rx_pending
> B44_RX_RING_SIZE
- 1) ||
1709 (ering
->rx_mini_pending
!= 0) ||
1710 (ering
->rx_jumbo_pending
!= 0) ||
1711 (ering
->tx_pending
> B44_TX_RING_SIZE
- 1))
1714 spin_lock_irq(&bp
->lock
);
1716 bp
->rx_pending
= ering
->rx_pending
;
1717 bp
->tx_pending
= ering
->tx_pending
;
1722 netif_wake_queue(bp
->dev
);
1723 spin_unlock_irq(&bp
->lock
);
1725 b44_enable_ints(bp
);
1730 static void b44_get_pauseparam(struct net_device
*dev
,
1731 struct ethtool_pauseparam
*epause
)
1733 struct b44
*bp
= netdev_priv(dev
);
1736 (bp
->flags
& B44_FLAG_PAUSE_AUTO
) != 0;
1738 (bp
->flags
& B44_FLAG_RX_PAUSE
) != 0;
1740 (bp
->flags
& B44_FLAG_TX_PAUSE
) != 0;
1743 static int b44_set_pauseparam(struct net_device
*dev
,
1744 struct ethtool_pauseparam
*epause
)
1746 struct b44
*bp
= netdev_priv(dev
);
1748 spin_lock_irq(&bp
->lock
);
1749 if (epause
->autoneg
)
1750 bp
->flags
|= B44_FLAG_PAUSE_AUTO
;
1752 bp
->flags
&= ~B44_FLAG_PAUSE_AUTO
;
1753 if (epause
->rx_pause
)
1754 bp
->flags
|= B44_FLAG_RX_PAUSE
;
1756 bp
->flags
&= ~B44_FLAG_RX_PAUSE
;
1757 if (epause
->tx_pause
)
1758 bp
->flags
|= B44_FLAG_TX_PAUSE
;
1760 bp
->flags
&= ~B44_FLAG_TX_PAUSE
;
1761 if (bp
->flags
& B44_FLAG_PAUSE_AUTO
) {
1766 __b44_set_flow_ctrl(bp
, bp
->flags
);
1768 spin_unlock_irq(&bp
->lock
);
1770 b44_enable_ints(bp
);
1775 static struct ethtool_ops b44_ethtool_ops
= {
1776 .get_drvinfo
= b44_get_drvinfo
,
1777 .get_settings
= b44_get_settings
,
1778 .set_settings
= b44_set_settings
,
1779 .nway_reset
= b44_nway_reset
,
1780 .get_link
= ethtool_op_get_link
,
1781 .get_ringparam
= b44_get_ringparam
,
1782 .set_ringparam
= b44_set_ringparam
,
1783 .get_pauseparam
= b44_get_pauseparam
,
1784 .set_pauseparam
= b44_set_pauseparam
,
1785 .get_msglevel
= b44_get_msglevel
,
1786 .set_msglevel
= b44_set_msglevel
,
1787 .get_perm_addr
= ethtool_op_get_perm_addr
,
1790 static int b44_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1792 struct mii_ioctl_data
*data
= if_mii(ifr
);
1793 struct b44
*bp
= netdev_priv(dev
);
1796 spin_lock_irq(&bp
->lock
);
1797 err
= generic_mii_ioctl(&bp
->mii_if
, data
, cmd
, NULL
);
1798 spin_unlock_irq(&bp
->lock
);
1803 /* Read 128-bytes of EEPROM. */
1804 static int b44_read_eeprom(struct b44
*bp
, u8
*data
)
1807 u16
*ptr
= (u16
*) data
;
1809 for (i
= 0; i
< 128; i
+= 2)
1810 ptr
[i
/ 2] = readw(bp
->regs
+ 4096 + i
);
1815 static int __devinit
b44_get_invariants(struct b44
*bp
)
1820 err
= b44_read_eeprom(bp
, &eeprom
[0]);
1824 bp
->dev
->dev_addr
[0] = eeprom
[79];
1825 bp
->dev
->dev_addr
[1] = eeprom
[78];
1826 bp
->dev
->dev_addr
[2] = eeprom
[81];
1827 bp
->dev
->dev_addr
[3] = eeprom
[80];
1828 bp
->dev
->dev_addr
[4] = eeprom
[83];
1829 bp
->dev
->dev_addr
[5] = eeprom
[82];
1830 memcpy(bp
->dev
->perm_addr
, bp
->dev
->dev_addr
, bp
->dev
->addr_len
);
1832 bp
->phy_addr
= eeprom
[90] & 0x1f;
1834 /* With this, plus the rx_header prepended to the data by the
1835 * hardware, we'll land the ethernet header on a 2-byte boundary.
1839 bp
->imask
= IMASK_DEF
;
1841 bp
->core_unit
= ssb_core_unit(bp
);
1842 bp
->dma_offset
= SB_PCI_DMA
;
1844 /* XXX - really required?
1845 bp->flags |= B44_FLAG_BUGGY_TXPTR;
1851 static int __devinit
b44_init_one(struct pci_dev
*pdev
,
1852 const struct pci_device_id
*ent
)
1854 static int b44_version_printed
= 0;
1855 unsigned long b44reg_base
, b44reg_len
;
1856 struct net_device
*dev
;
1860 if (b44_version_printed
++ == 0)
1861 printk(KERN_INFO
"%s", version
);
1863 err
= pci_enable_device(pdev
);
1865 printk(KERN_ERR PFX
"Cannot enable PCI device, "
1870 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
1871 printk(KERN_ERR PFX
"Cannot find proper PCI device "
1872 "base address, aborting.\n");
1874 goto err_out_disable_pdev
;
1877 err
= pci_request_regions(pdev
, DRV_MODULE_NAME
);
1879 printk(KERN_ERR PFX
"Cannot obtain PCI resources, "
1881 goto err_out_disable_pdev
;
1884 pci_set_master(pdev
);
1886 err
= pci_set_dma_mask(pdev
, (u64
) B44_DMA_MASK
);
1888 printk(KERN_ERR PFX
"No usable DMA configuration, "
1890 goto err_out_free_res
;
1893 err
= pci_set_consistent_dma_mask(pdev
, (u64
) B44_DMA_MASK
);
1895 printk(KERN_ERR PFX
"No usable DMA configuration, "
1897 goto err_out_free_res
;
1900 b44reg_base
= pci_resource_start(pdev
, 0);
1901 b44reg_len
= pci_resource_len(pdev
, 0);
1903 dev
= alloc_etherdev(sizeof(*bp
));
1905 printk(KERN_ERR PFX
"Etherdev alloc failed, aborting.\n");
1907 goto err_out_free_res
;
1910 SET_MODULE_OWNER(dev
);
1911 SET_NETDEV_DEV(dev
,&pdev
->dev
);
1913 /* No interesting netdevice features in this card... */
1916 bp
= netdev_priv(dev
);
1920 bp
->msg_enable
= netif_msg_init(b44_debug
, B44_DEF_MSG_ENABLE
);
1922 spin_lock_init(&bp
->lock
);
1924 bp
->regs
= ioremap(b44reg_base
, b44reg_len
);
1925 if (bp
->regs
== 0UL) {
1926 printk(KERN_ERR PFX
"Cannot map device registers, "
1929 goto err_out_free_dev
;
1932 bp
->rx_pending
= B44_DEF_RX_RING_PENDING
;
1933 bp
->tx_pending
= B44_DEF_TX_RING_PENDING
;
1935 dev
->open
= b44_open
;
1936 dev
->stop
= b44_close
;
1937 dev
->hard_start_xmit
= b44_start_xmit
;
1938 dev
->get_stats
= b44_get_stats
;
1939 dev
->set_multicast_list
= b44_set_rx_mode
;
1940 dev
->set_mac_address
= b44_set_mac_addr
;
1941 dev
->do_ioctl
= b44_ioctl
;
1942 dev
->tx_timeout
= b44_tx_timeout
;
1943 dev
->poll
= b44_poll
;
1945 dev
->watchdog_timeo
= B44_TX_TIMEOUT
;
1946 #ifdef CONFIG_NET_POLL_CONTROLLER
1947 dev
->poll_controller
= b44_poll_controller
;
1949 dev
->change_mtu
= b44_change_mtu
;
1950 dev
->irq
= pdev
->irq
;
1951 SET_ETHTOOL_OPS(dev
, &b44_ethtool_ops
);
1953 err
= b44_get_invariants(bp
);
1955 printk(KERN_ERR PFX
"Problem fetching invariants of chip, "
1957 goto err_out_iounmap
;
1960 bp
->mii_if
.dev
= dev
;
1961 bp
->mii_if
.mdio_read
= b44_mii_read
;
1962 bp
->mii_if
.mdio_write
= b44_mii_write
;
1963 bp
->mii_if
.phy_id
= bp
->phy_addr
;
1964 bp
->mii_if
.phy_id_mask
= 0x1f;
1965 bp
->mii_if
.reg_num_mask
= 0x1f;
1967 /* By default, advertise all speed/duplex settings. */
1968 bp
->flags
|= (B44_FLAG_ADV_10HALF
| B44_FLAG_ADV_10FULL
|
1969 B44_FLAG_ADV_100HALF
| B44_FLAG_ADV_100FULL
);
1971 /* By default, auto-negotiate PAUSE. */
1972 bp
->flags
|= B44_FLAG_PAUSE_AUTO
;
1974 err
= register_netdev(dev
);
1976 printk(KERN_ERR PFX
"Cannot register net device, "
1978 goto err_out_iounmap
;
1981 pci_set_drvdata(pdev
, dev
);
1983 pci_save_state(bp
->pdev
);
1985 printk(KERN_INFO
"%s: Broadcom 4400 10/100BaseT Ethernet ", dev
->name
);
1986 for (i
= 0; i
< 6; i
++)
1987 printk("%2.2x%c", dev
->dev_addr
[i
],
1988 i
== 5 ? '\n' : ':');
1999 pci_release_regions(pdev
);
2001 err_out_disable_pdev
:
2002 pci_disable_device(pdev
);
2003 pci_set_drvdata(pdev
, NULL
);
2007 static void __devexit
b44_remove_one(struct pci_dev
*pdev
)
2009 struct net_device
*dev
= pci_get_drvdata(pdev
);
2010 struct b44
*bp
= netdev_priv(dev
);
2012 unregister_netdev(dev
);
2015 pci_release_regions(pdev
);
2016 pci_disable_device(pdev
);
2017 pci_set_drvdata(pdev
, NULL
);
2020 static int b44_suspend(struct pci_dev
*pdev
, pm_message_t state
)
2022 struct net_device
*dev
= pci_get_drvdata(pdev
);
2023 struct b44
*bp
= netdev_priv(dev
);
2025 if (!netif_running(dev
))
2028 del_timer_sync(&bp
->timer
);
2030 spin_lock_irq(&bp
->lock
);
2033 netif_carrier_off(bp
->dev
);
2034 netif_device_detach(bp
->dev
);
2037 spin_unlock_irq(&bp
->lock
);
2039 free_irq(dev
->irq
, dev
);
2040 pci_disable_device(pdev
);
2044 static int b44_resume(struct pci_dev
*pdev
)
2046 struct net_device
*dev
= pci_get_drvdata(pdev
);
2047 struct b44
*bp
= netdev_priv(dev
);
2049 pci_restore_state(pdev
);
2050 pci_enable_device(pdev
);
2051 pci_set_master(pdev
);
2053 if (!netif_running(dev
))
2056 if (request_irq(dev
->irq
, b44_interrupt
, SA_SHIRQ
, dev
->name
, dev
))
2057 printk(KERN_ERR PFX
"%s: request_irq failed\n", dev
->name
);
2059 spin_lock_irq(&bp
->lock
);
2063 netif_device_attach(bp
->dev
);
2064 spin_unlock_irq(&bp
->lock
);
2066 bp
->timer
.expires
= jiffies
+ HZ
;
2067 add_timer(&bp
->timer
);
2069 b44_enable_ints(bp
);
2073 static struct pci_driver b44_driver
= {
2074 .name
= DRV_MODULE_NAME
,
2075 .id_table
= b44_pci_tbl
,
2076 .probe
= b44_init_one
,
2077 .remove
= __devexit_p(b44_remove_one
),
2078 .suspend
= b44_suspend
,
2079 .resume
= b44_resume
,
2082 static int __init
b44_init(void)
2084 unsigned int dma_desc_align_size
= dma_get_cache_alignment();
2086 /* Setup paramaters for syncing RX/TX DMA descriptors */
2087 dma_desc_align_mask
= ~(dma_desc_align_size
- 1);
2088 dma_desc_sync_size
= max(dma_desc_align_size
, sizeof(struct dma_desc
));
2090 return pci_module_init(&b44_driver
);
2093 static void __exit
b44_cleanup(void)
2095 pci_unregister_driver(&b44_driver
);
2098 module_init(b44_init
);
2099 module_exit(b44_cleanup
);