[PATCH] m68knommu: allow for SDRAM and GPIO differences on 5270/1 and 5274/5 processors
[linux/fpc-iii.git] / drivers / net / b44.c
blob94939f570f78988e7c54c9dbcab685ca59385253
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.
7 */
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>
23 #include <asm/uaccess.h>
24 #include <asm/io.h>
25 #include <asm/irq.h>
27 #include "b44.h"
29 #define DRV_MODULE_NAME "b44"
30 #define PFX DRV_MODULE_NAME ": "
31 #define DRV_MODULE_VERSION "0.95"
32 #define DRV_MODULE_RELDATE "Aug 3, 2004"
34 #define B44_DEF_MSG_ENABLE \
35 (NETIF_MSG_DRV | \
36 NETIF_MSG_PROBE | \
37 NETIF_MSG_LINK | \
38 NETIF_MSG_TIMER | \
39 NETIF_MSG_IFDOWN | \
40 NETIF_MSG_IFUP | \
41 NETIF_MSG_RX_ERR | \
42 NETIF_MSG_TX_ERR)
44 /* length of time before we decide the hardware is borked,
45 * and dev->tx_timeout() should be called to fix the problem
47 #define B44_TX_TIMEOUT (5 * HZ)
49 /* hardware minimum and maximum for a single frame's data payload */
50 #define B44_MIN_MTU 60
51 #define B44_MAX_MTU 1500
53 #define B44_RX_RING_SIZE 512
54 #define B44_DEF_RX_RING_PENDING 200
55 #define B44_RX_RING_BYTES (sizeof(struct dma_desc) * \
56 B44_RX_RING_SIZE)
57 #define B44_TX_RING_SIZE 512
58 #define B44_DEF_TX_RING_PENDING (B44_TX_RING_SIZE - 1)
59 #define B44_TX_RING_BYTES (sizeof(struct dma_desc) * \
60 B44_TX_RING_SIZE)
61 #define B44_DMA_MASK 0x3fffffff
63 #define TX_RING_GAP(BP) \
64 (B44_TX_RING_SIZE - (BP)->tx_pending)
65 #define TX_BUFFS_AVAIL(BP) \
66 (((BP)->tx_cons <= (BP)->tx_prod) ? \
67 (BP)->tx_cons + (BP)->tx_pending - (BP)->tx_prod : \
68 (BP)->tx_cons - (BP)->tx_prod - TX_RING_GAP(BP))
69 #define NEXT_TX(N) (((N) + 1) & (B44_TX_RING_SIZE - 1))
71 #define RX_PKT_BUF_SZ (1536 + bp->rx_offset + 64)
72 #define TX_PKT_BUF_SZ (B44_MAX_MTU + ETH_HLEN + 8)
74 /* minimum number of free TX descriptors required to wake up TX process */
75 #define B44_TX_WAKEUP_THRESH (B44_TX_RING_SIZE / 4)
77 static char version[] __devinitdata =
78 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
80 MODULE_AUTHOR("Florian Schirmer, Pekka Pietikainen, David S. Miller");
81 MODULE_DESCRIPTION("Broadcom 4400 10/100 PCI ethernet driver");
82 MODULE_LICENSE("GPL");
83 MODULE_VERSION(DRV_MODULE_VERSION);
85 static int b44_debug = -1; /* -1 == use B44_DEF_MSG_ENABLE as value */
86 module_param(b44_debug, int, 0);
87 MODULE_PARM_DESC(b44_debug, "B44 bitmapped debugging message enable value");
89 static struct pci_device_id b44_pci_tbl[] = {
90 { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401,
91 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
92 { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B0,
93 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
94 { PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_BCM4401B1,
95 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
96 { } /* terminate list with empty entry */
99 MODULE_DEVICE_TABLE(pci, b44_pci_tbl);
101 static void b44_halt(struct b44 *);
102 static void b44_init_rings(struct b44 *);
103 static void b44_init_hw(struct b44 *);
104 static int b44_poll(struct net_device *dev, int *budget);
105 #ifdef CONFIG_NET_POLL_CONTROLLER
106 static void b44_poll_controller(struct net_device *dev);
107 #endif
109 static inline unsigned long br32(const struct b44 *bp, unsigned long reg)
111 return readl(bp->regs + reg);
114 static inline void bw32(const struct b44 *bp,
115 unsigned long reg, unsigned long val)
117 writel(val, bp->regs + reg);
120 static int b44_wait_bit(struct b44 *bp, unsigned long reg,
121 u32 bit, unsigned long timeout, const int clear)
123 unsigned long i;
125 for (i = 0; i < timeout; i++) {
126 u32 val = br32(bp, reg);
128 if (clear && !(val & bit))
129 break;
130 if (!clear && (val & bit))
131 break;
132 udelay(10);
134 if (i == timeout) {
135 printk(KERN_ERR PFX "%s: BUG! Timeout waiting for bit %08x of register "
136 "%lx to %s.\n",
137 bp->dev->name,
138 bit, reg,
139 (clear ? "clear" : "set"));
140 return -ENODEV;
142 return 0;
145 /* Sonics SiliconBackplane support routines. ROFL, you should see all the
146 * buzz words used on this company's website :-)
148 * All of these routines must be invoked with bp->lock held and
149 * interrupts disabled.
152 #define SB_PCI_DMA 0x40000000 /* Client Mode PCI memory access space (1 GB) */
153 #define BCM4400_PCI_CORE_ADDR 0x18002000 /* Address of PCI core on BCM4400 cards */
155 static u32 ssb_get_core_rev(struct b44 *bp)
157 return (br32(bp, B44_SBIDHIGH) & SBIDHIGH_RC_MASK);
160 static u32 ssb_pci_setup(struct b44 *bp, u32 cores)
162 u32 bar_orig, pci_rev, val;
164 pci_read_config_dword(bp->pdev, SSB_BAR0_WIN, &bar_orig);
165 pci_write_config_dword(bp->pdev, SSB_BAR0_WIN, BCM4400_PCI_CORE_ADDR);
166 pci_rev = ssb_get_core_rev(bp);
168 val = br32(bp, B44_SBINTVEC);
169 val |= cores;
170 bw32(bp, B44_SBINTVEC, val);
172 val = br32(bp, SSB_PCI_TRANS_2);
173 val |= SSB_PCI_PREF | SSB_PCI_BURST;
174 bw32(bp, SSB_PCI_TRANS_2, val);
176 pci_write_config_dword(bp->pdev, SSB_BAR0_WIN, bar_orig);
178 return pci_rev;
181 static void ssb_core_disable(struct b44 *bp)
183 if (br32(bp, B44_SBTMSLOW) & SBTMSLOW_RESET)
184 return;
186 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_REJECT | SBTMSLOW_CLOCK));
187 b44_wait_bit(bp, B44_SBTMSLOW, SBTMSLOW_REJECT, 100000, 0);
188 b44_wait_bit(bp, B44_SBTMSHIGH, SBTMSHIGH_BUSY, 100000, 1);
189 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_FGC | SBTMSLOW_CLOCK |
190 SBTMSLOW_REJECT | SBTMSLOW_RESET));
191 br32(bp, B44_SBTMSLOW);
192 udelay(1);
193 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_REJECT | SBTMSLOW_RESET));
194 br32(bp, B44_SBTMSLOW);
195 udelay(1);
198 static void ssb_core_reset(struct b44 *bp)
200 u32 val;
202 ssb_core_disable(bp);
203 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_RESET | SBTMSLOW_CLOCK | SBTMSLOW_FGC));
204 br32(bp, B44_SBTMSLOW);
205 udelay(1);
207 /* Clear SERR if set, this is a hw bug workaround. */
208 if (br32(bp, B44_SBTMSHIGH) & SBTMSHIGH_SERR)
209 bw32(bp, B44_SBTMSHIGH, 0);
211 val = br32(bp, B44_SBIMSTATE);
212 if (val & (SBIMSTATE_IBE | SBIMSTATE_TO))
213 bw32(bp, B44_SBIMSTATE, val & ~(SBIMSTATE_IBE | SBIMSTATE_TO));
215 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_CLOCK | SBTMSLOW_FGC));
216 br32(bp, B44_SBTMSLOW);
217 udelay(1);
219 bw32(bp, B44_SBTMSLOW, (SBTMSLOW_CLOCK));
220 br32(bp, B44_SBTMSLOW);
221 udelay(1);
224 static int ssb_core_unit(struct b44 *bp)
226 #if 0
227 u32 val = br32(bp, B44_SBADMATCH0);
228 u32 base;
230 type = val & SBADMATCH0_TYPE_MASK;
231 switch (type) {
232 case 0:
233 base = val & SBADMATCH0_BS0_MASK;
234 break;
236 case 1:
237 base = val & SBADMATCH0_BS1_MASK;
238 break;
240 case 2:
241 default:
242 base = val & SBADMATCH0_BS2_MASK;
243 break;
245 #endif
246 return 0;
249 static int ssb_is_core_up(struct b44 *bp)
251 return ((br32(bp, B44_SBTMSLOW) & (SBTMSLOW_RESET | SBTMSLOW_REJECT | SBTMSLOW_CLOCK))
252 == SBTMSLOW_CLOCK);
255 static void __b44_cam_write(struct b44 *bp, unsigned char *data, int index)
257 u32 val;
259 val = ((u32) data[2]) << 24;
260 val |= ((u32) data[3]) << 16;
261 val |= ((u32) data[4]) << 8;
262 val |= ((u32) data[5]) << 0;
263 bw32(bp, B44_CAM_DATA_LO, val);
264 val = (CAM_DATA_HI_VALID |
265 (((u32) data[0]) << 8) |
266 (((u32) data[1]) << 0));
267 bw32(bp, B44_CAM_DATA_HI, val);
268 bw32(bp, B44_CAM_CTRL, (CAM_CTRL_WRITE |
269 (index << CAM_CTRL_INDEX_SHIFT)));
270 b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
273 static inline void __b44_disable_ints(struct b44 *bp)
275 bw32(bp, B44_IMASK, 0);
278 static void b44_disable_ints(struct b44 *bp)
280 __b44_disable_ints(bp);
282 /* Flush posted writes. */
283 br32(bp, B44_IMASK);
286 static void b44_enable_ints(struct b44 *bp)
288 bw32(bp, B44_IMASK, bp->imask);
291 static int b44_readphy(struct b44 *bp, int reg, u32 *val)
293 int err;
295 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
296 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
297 (MDIO_OP_READ << MDIO_DATA_OP_SHIFT) |
298 (bp->phy_addr << MDIO_DATA_PMD_SHIFT) |
299 (reg << MDIO_DATA_RA_SHIFT) |
300 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT)));
301 err = b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
302 *val = br32(bp, B44_MDIO_DATA) & MDIO_DATA_DATA;
304 return err;
307 static int b44_writephy(struct b44 *bp, int reg, u32 val)
309 bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
310 bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START |
311 (MDIO_OP_WRITE << MDIO_DATA_OP_SHIFT) |
312 (bp->phy_addr << MDIO_DATA_PMD_SHIFT) |
313 (reg << MDIO_DATA_RA_SHIFT) |
314 (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT) |
315 (val & MDIO_DATA_DATA)));
316 return b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
319 /* miilib interface */
320 /* FIXME FIXME: phy_id is ignored, bp->phy_addr use is unconditional
321 * due to code existing before miilib use was added to this driver.
322 * Someone should remove this artificial driver limitation in
323 * b44_{read,write}phy. bp->phy_addr itself is fine (and needed).
325 static int b44_mii_read(struct net_device *dev, int phy_id, int location)
327 u32 val;
328 struct b44 *bp = netdev_priv(dev);
329 int rc = b44_readphy(bp, location, &val);
330 if (rc)
331 return 0xffffffff;
332 return val;
335 static void b44_mii_write(struct net_device *dev, int phy_id, int location,
336 int val)
338 struct b44 *bp = netdev_priv(dev);
339 b44_writephy(bp, location, val);
342 static int b44_phy_reset(struct b44 *bp)
344 u32 val;
345 int err;
347 err = b44_writephy(bp, MII_BMCR, BMCR_RESET);
348 if (err)
349 return err;
350 udelay(100);
351 err = b44_readphy(bp, MII_BMCR, &val);
352 if (!err) {
353 if (val & BMCR_RESET) {
354 printk(KERN_ERR PFX "%s: PHY Reset would not complete.\n",
355 bp->dev->name);
356 err = -ENODEV;
360 return 0;
363 static void __b44_set_flow_ctrl(struct b44 *bp, u32 pause_flags)
365 u32 val;
367 bp->flags &= ~(B44_FLAG_TX_PAUSE | B44_FLAG_RX_PAUSE);
368 bp->flags |= pause_flags;
370 val = br32(bp, B44_RXCONFIG);
371 if (pause_flags & B44_FLAG_RX_PAUSE)
372 val |= RXCONFIG_FLOW;
373 else
374 val &= ~RXCONFIG_FLOW;
375 bw32(bp, B44_RXCONFIG, val);
377 val = br32(bp, B44_MAC_FLOW);
378 if (pause_flags & B44_FLAG_TX_PAUSE)
379 val |= (MAC_FLOW_PAUSE_ENAB |
380 (0xc0 & MAC_FLOW_RX_HI_WATER));
381 else
382 val &= ~MAC_FLOW_PAUSE_ENAB;
383 bw32(bp, B44_MAC_FLOW, val);
386 static void b44_set_flow_ctrl(struct b44 *bp, u32 local, u32 remote)
388 u32 pause_enab = bp->flags & (B44_FLAG_TX_PAUSE |
389 B44_FLAG_RX_PAUSE);
391 if (local & ADVERTISE_PAUSE_CAP) {
392 if (local & ADVERTISE_PAUSE_ASYM) {
393 if (remote & LPA_PAUSE_CAP)
394 pause_enab |= (B44_FLAG_TX_PAUSE |
395 B44_FLAG_RX_PAUSE);
396 else if (remote & LPA_PAUSE_ASYM)
397 pause_enab |= B44_FLAG_RX_PAUSE;
398 } else {
399 if (remote & LPA_PAUSE_CAP)
400 pause_enab |= (B44_FLAG_TX_PAUSE |
401 B44_FLAG_RX_PAUSE);
403 } else if (local & ADVERTISE_PAUSE_ASYM) {
404 if ((remote & LPA_PAUSE_CAP) &&
405 (remote & LPA_PAUSE_ASYM))
406 pause_enab |= B44_FLAG_TX_PAUSE;
409 __b44_set_flow_ctrl(bp, pause_enab);
412 static int b44_setup_phy(struct b44 *bp)
414 u32 val;
415 int err;
417 if ((err = b44_readphy(bp, B44_MII_ALEDCTRL, &val)) != 0)
418 goto out;
419 if ((err = b44_writephy(bp, B44_MII_ALEDCTRL,
420 val & MII_ALEDCTRL_ALLMSK)) != 0)
421 goto out;
422 if ((err = b44_readphy(bp, B44_MII_TLEDCTRL, &val)) != 0)
423 goto out;
424 if ((err = b44_writephy(bp, B44_MII_TLEDCTRL,
425 val | MII_TLEDCTRL_ENABLE)) != 0)
426 goto out;
428 if (!(bp->flags & B44_FLAG_FORCE_LINK)) {
429 u32 adv = ADVERTISE_CSMA;
431 if (bp->flags & B44_FLAG_ADV_10HALF)
432 adv |= ADVERTISE_10HALF;
433 if (bp->flags & B44_FLAG_ADV_10FULL)
434 adv |= ADVERTISE_10FULL;
435 if (bp->flags & B44_FLAG_ADV_100HALF)
436 adv |= ADVERTISE_100HALF;
437 if (bp->flags & B44_FLAG_ADV_100FULL)
438 adv |= ADVERTISE_100FULL;
440 if (bp->flags & B44_FLAG_PAUSE_AUTO)
441 adv |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
443 if ((err = b44_writephy(bp, MII_ADVERTISE, adv)) != 0)
444 goto out;
445 if ((err = b44_writephy(bp, MII_BMCR, (BMCR_ANENABLE |
446 BMCR_ANRESTART))) != 0)
447 goto out;
448 } else {
449 u32 bmcr;
451 if ((err = b44_readphy(bp, MII_BMCR, &bmcr)) != 0)
452 goto out;
453 bmcr &= ~(BMCR_FULLDPLX | BMCR_ANENABLE | BMCR_SPEED100);
454 if (bp->flags & B44_FLAG_100_BASE_T)
455 bmcr |= BMCR_SPEED100;
456 if (bp->flags & B44_FLAG_FULL_DUPLEX)
457 bmcr |= BMCR_FULLDPLX;
458 if ((err = b44_writephy(bp, MII_BMCR, bmcr)) != 0)
459 goto out;
461 /* Since we will not be negotiating there is no safe way
462 * to determine if the link partner supports flow control
463 * or not. So just disable it completely in this case.
465 b44_set_flow_ctrl(bp, 0, 0);
468 out:
469 return err;
472 static void b44_stats_update(struct b44 *bp)
474 unsigned long reg;
475 u32 *val;
477 val = &bp->hw_stats.tx_good_octets;
478 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL) {
479 *val++ += br32(bp, reg);
481 val = &bp->hw_stats.rx_good_octets;
482 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL) {
483 *val++ += br32(bp, reg);
487 static void b44_link_report(struct b44 *bp)
489 if (!netif_carrier_ok(bp->dev)) {
490 printk(KERN_INFO PFX "%s: Link is down.\n", bp->dev->name);
491 } else {
492 printk(KERN_INFO PFX "%s: Link is up at %d Mbps, %s duplex.\n",
493 bp->dev->name,
494 (bp->flags & B44_FLAG_100_BASE_T) ? 100 : 10,
495 (bp->flags & B44_FLAG_FULL_DUPLEX) ? "full" : "half");
497 printk(KERN_INFO PFX "%s: Flow control is %s for TX and "
498 "%s for RX.\n",
499 bp->dev->name,
500 (bp->flags & B44_FLAG_TX_PAUSE) ? "on" : "off",
501 (bp->flags & B44_FLAG_RX_PAUSE) ? "on" : "off");
505 static void b44_check_phy(struct b44 *bp)
507 u32 bmsr, aux;
509 if (!b44_readphy(bp, MII_BMSR, &bmsr) &&
510 !b44_readphy(bp, B44_MII_AUXCTRL, &aux) &&
511 (bmsr != 0xffff)) {
512 if (aux & MII_AUXCTRL_SPEED)
513 bp->flags |= B44_FLAG_100_BASE_T;
514 else
515 bp->flags &= ~B44_FLAG_100_BASE_T;
516 if (aux & MII_AUXCTRL_DUPLEX)
517 bp->flags |= B44_FLAG_FULL_DUPLEX;
518 else
519 bp->flags &= ~B44_FLAG_FULL_DUPLEX;
521 if (!netif_carrier_ok(bp->dev) &&
522 (bmsr & BMSR_LSTATUS)) {
523 u32 val = br32(bp, B44_TX_CTRL);
524 u32 local_adv, remote_adv;
526 if (bp->flags & B44_FLAG_FULL_DUPLEX)
527 val |= TX_CTRL_DUPLEX;
528 else
529 val &= ~TX_CTRL_DUPLEX;
530 bw32(bp, B44_TX_CTRL, val);
532 if (!(bp->flags & B44_FLAG_FORCE_LINK) &&
533 !b44_readphy(bp, MII_ADVERTISE, &local_adv) &&
534 !b44_readphy(bp, MII_LPA, &remote_adv))
535 b44_set_flow_ctrl(bp, local_adv, remote_adv);
537 /* Link now up */
538 netif_carrier_on(bp->dev);
539 b44_link_report(bp);
540 } else if (netif_carrier_ok(bp->dev) && !(bmsr & BMSR_LSTATUS)) {
541 /* Link now down */
542 netif_carrier_off(bp->dev);
543 b44_link_report(bp);
546 if (bmsr & BMSR_RFAULT)
547 printk(KERN_WARNING PFX "%s: Remote fault detected in PHY\n",
548 bp->dev->name);
549 if (bmsr & BMSR_JCD)
550 printk(KERN_WARNING PFX "%s: Jabber detected in PHY\n",
551 bp->dev->name);
555 static void b44_timer(unsigned long __opaque)
557 struct b44 *bp = (struct b44 *) __opaque;
559 spin_lock_irq(&bp->lock);
561 b44_check_phy(bp);
563 b44_stats_update(bp);
565 spin_unlock_irq(&bp->lock);
567 bp->timer.expires = jiffies + HZ;
568 add_timer(&bp->timer);
571 static void b44_tx(struct b44 *bp)
573 u32 cur, cons;
575 cur = br32(bp, B44_DMATX_STAT) & DMATX_STAT_CDMASK;
576 cur /= sizeof(struct dma_desc);
578 /* XXX needs updating when NETIF_F_SG is supported */
579 for (cons = bp->tx_cons; cons != cur; cons = NEXT_TX(cons)) {
580 struct ring_info *rp = &bp->tx_buffers[cons];
581 struct sk_buff *skb = rp->skb;
583 if (unlikely(skb == NULL))
584 BUG();
586 pci_unmap_single(bp->pdev,
587 pci_unmap_addr(rp, mapping),
588 skb->len,
589 PCI_DMA_TODEVICE);
590 rp->skb = NULL;
591 dev_kfree_skb_irq(skb);
594 bp->tx_cons = cons;
595 if (netif_queue_stopped(bp->dev) &&
596 TX_BUFFS_AVAIL(bp) > B44_TX_WAKEUP_THRESH)
597 netif_wake_queue(bp->dev);
599 bw32(bp, B44_GPTIMER, 0);
602 /* Works like this. This chip writes a 'struct rx_header" 30 bytes
603 * before the DMA address you give it. So we allocate 30 more bytes
604 * for the RX buffer, DMA map all of it, skb_reserve the 30 bytes, then
605 * point the chip at 30 bytes past where the rx_header will go.
607 static int b44_alloc_rx_skb(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
609 struct dma_desc *dp;
610 struct ring_info *src_map, *map;
611 struct rx_header *rh;
612 struct sk_buff *skb;
613 dma_addr_t mapping;
614 int dest_idx;
615 u32 ctrl;
617 src_map = NULL;
618 if (src_idx >= 0)
619 src_map = &bp->rx_buffers[src_idx];
620 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
621 map = &bp->rx_buffers[dest_idx];
622 skb = dev_alloc_skb(RX_PKT_BUF_SZ);
623 if (skb == NULL)
624 return -ENOMEM;
626 mapping = pci_map_single(bp->pdev, skb->data,
627 RX_PKT_BUF_SZ,
628 PCI_DMA_FROMDEVICE);
630 /* Hardware bug work-around, the chip is unable to do PCI DMA
631 to/from anything above 1GB :-( */
632 if(mapping+RX_PKT_BUF_SZ > B44_DMA_MASK) {
633 /* Sigh... */
634 pci_unmap_single(bp->pdev, mapping, RX_PKT_BUF_SZ,PCI_DMA_FROMDEVICE);
635 dev_kfree_skb_any(skb);
636 skb = __dev_alloc_skb(RX_PKT_BUF_SZ,GFP_DMA);
637 if (skb == NULL)
638 return -ENOMEM;
639 mapping = pci_map_single(bp->pdev, skb->data,
640 RX_PKT_BUF_SZ,
641 PCI_DMA_FROMDEVICE);
642 if(mapping+RX_PKT_BUF_SZ > B44_DMA_MASK) {
643 pci_unmap_single(bp->pdev, mapping, RX_PKT_BUF_SZ,PCI_DMA_FROMDEVICE);
644 dev_kfree_skb_any(skb);
645 return -ENOMEM;
649 skb->dev = bp->dev;
650 skb_reserve(skb, bp->rx_offset);
652 rh = (struct rx_header *)
653 (skb->data - bp->rx_offset);
654 rh->len = 0;
655 rh->flags = 0;
657 map->skb = skb;
658 pci_unmap_addr_set(map, mapping, mapping);
660 if (src_map != NULL)
661 src_map->skb = NULL;
663 ctrl = (DESC_CTRL_LEN & (RX_PKT_BUF_SZ - bp->rx_offset));
664 if (dest_idx == (B44_RX_RING_SIZE - 1))
665 ctrl |= DESC_CTRL_EOT;
667 dp = &bp->rx_ring[dest_idx];
668 dp->ctrl = cpu_to_le32(ctrl);
669 dp->addr = cpu_to_le32((u32) mapping + bp->rx_offset + bp->dma_offset);
671 return RX_PKT_BUF_SZ;
674 static void b44_recycle_rx(struct b44 *bp, int src_idx, u32 dest_idx_unmasked)
676 struct dma_desc *src_desc, *dest_desc;
677 struct ring_info *src_map, *dest_map;
678 struct rx_header *rh;
679 int dest_idx;
680 u32 ctrl;
682 dest_idx = dest_idx_unmasked & (B44_RX_RING_SIZE - 1);
683 dest_desc = &bp->rx_ring[dest_idx];
684 dest_map = &bp->rx_buffers[dest_idx];
685 src_desc = &bp->rx_ring[src_idx];
686 src_map = &bp->rx_buffers[src_idx];
688 dest_map->skb = src_map->skb;
689 rh = (struct rx_header *) src_map->skb->data;
690 rh->len = 0;
691 rh->flags = 0;
692 pci_unmap_addr_set(dest_map, mapping,
693 pci_unmap_addr(src_map, mapping));
695 ctrl = src_desc->ctrl;
696 if (dest_idx == (B44_RX_RING_SIZE - 1))
697 ctrl |= cpu_to_le32(DESC_CTRL_EOT);
698 else
699 ctrl &= cpu_to_le32(~DESC_CTRL_EOT);
701 dest_desc->ctrl = ctrl;
702 dest_desc->addr = src_desc->addr;
703 src_map->skb = NULL;
705 pci_dma_sync_single_for_device(bp->pdev, src_desc->addr,
706 RX_PKT_BUF_SZ,
707 PCI_DMA_FROMDEVICE);
710 static int b44_rx(struct b44 *bp, int budget)
712 int received;
713 u32 cons, prod;
715 received = 0;
716 prod = br32(bp, B44_DMARX_STAT) & DMARX_STAT_CDMASK;
717 prod /= sizeof(struct dma_desc);
718 cons = bp->rx_cons;
720 while (cons != prod && budget > 0) {
721 struct ring_info *rp = &bp->rx_buffers[cons];
722 struct sk_buff *skb = rp->skb;
723 dma_addr_t map = pci_unmap_addr(rp, mapping);
724 struct rx_header *rh;
725 u16 len;
727 pci_dma_sync_single_for_cpu(bp->pdev, map,
728 RX_PKT_BUF_SZ,
729 PCI_DMA_FROMDEVICE);
730 rh = (struct rx_header *) skb->data;
731 len = cpu_to_le16(rh->len);
732 if ((len > (RX_PKT_BUF_SZ - bp->rx_offset)) ||
733 (rh->flags & cpu_to_le16(RX_FLAG_ERRORS))) {
734 drop_it:
735 b44_recycle_rx(bp, cons, bp->rx_prod);
736 drop_it_no_recycle:
737 bp->stats.rx_dropped++;
738 goto next_pkt;
741 if (len == 0) {
742 int i = 0;
744 do {
745 udelay(2);
746 barrier();
747 len = cpu_to_le16(rh->len);
748 } while (len == 0 && i++ < 5);
749 if (len == 0)
750 goto drop_it;
753 /* Omit CRC. */
754 len -= 4;
756 if (len > RX_COPY_THRESHOLD) {
757 int skb_size;
758 skb_size = b44_alloc_rx_skb(bp, cons, bp->rx_prod);
759 if (skb_size < 0)
760 goto drop_it;
761 pci_unmap_single(bp->pdev, map,
762 skb_size, PCI_DMA_FROMDEVICE);
763 /* Leave out rx_header */
764 skb_put(skb, len+bp->rx_offset);
765 skb_pull(skb,bp->rx_offset);
766 } else {
767 struct sk_buff *copy_skb;
769 b44_recycle_rx(bp, cons, bp->rx_prod);
770 copy_skb = dev_alloc_skb(len + 2);
771 if (copy_skb == NULL)
772 goto drop_it_no_recycle;
774 copy_skb->dev = bp->dev;
775 skb_reserve(copy_skb, 2);
776 skb_put(copy_skb, len);
777 /* DMA sync done above, copy just the actual packet */
778 memcpy(copy_skb->data, skb->data+bp->rx_offset, len);
780 skb = copy_skb;
782 skb->ip_summed = CHECKSUM_NONE;
783 skb->protocol = eth_type_trans(skb, bp->dev);
784 netif_receive_skb(skb);
785 bp->dev->last_rx = jiffies;
786 received++;
787 budget--;
788 next_pkt:
789 bp->rx_prod = (bp->rx_prod + 1) &
790 (B44_RX_RING_SIZE - 1);
791 cons = (cons + 1) & (B44_RX_RING_SIZE - 1);
794 bp->rx_cons = cons;
795 bw32(bp, B44_DMARX_PTR, cons * sizeof(struct dma_desc));
797 return received;
800 static int b44_poll(struct net_device *netdev, int *budget)
802 struct b44 *bp = netdev_priv(netdev);
803 int done;
805 spin_lock_irq(&bp->lock);
807 if (bp->istat & (ISTAT_TX | ISTAT_TO)) {
808 /* spin_lock(&bp->tx_lock); */
809 b44_tx(bp);
810 /* spin_unlock(&bp->tx_lock); */
812 spin_unlock_irq(&bp->lock);
814 done = 1;
815 if (bp->istat & ISTAT_RX) {
816 int orig_budget = *budget;
817 int work_done;
819 if (orig_budget > netdev->quota)
820 orig_budget = netdev->quota;
822 work_done = b44_rx(bp, orig_budget);
824 *budget -= work_done;
825 netdev->quota -= work_done;
827 if (work_done >= orig_budget)
828 done = 0;
831 if (bp->istat & ISTAT_ERRORS) {
832 spin_lock_irq(&bp->lock);
833 b44_halt(bp);
834 b44_init_rings(bp);
835 b44_init_hw(bp);
836 netif_wake_queue(bp->dev);
837 spin_unlock_irq(&bp->lock);
838 done = 1;
841 if (done) {
842 netif_rx_complete(netdev);
843 b44_enable_ints(bp);
846 return (done ? 0 : 1);
849 static irqreturn_t b44_interrupt(int irq, void *dev_id, struct pt_regs *regs)
851 struct net_device *dev = dev_id;
852 struct b44 *bp = netdev_priv(dev);
853 unsigned long flags;
854 u32 istat, imask;
855 int handled = 0;
857 spin_lock_irqsave(&bp->lock, flags);
859 istat = br32(bp, B44_ISTAT);
860 imask = br32(bp, B44_IMASK);
862 /* ??? What the fuck is the purpose of the interrupt mask
863 * ??? register if we have to mask it out by hand anyways?
865 istat &= imask;
866 if (istat) {
867 handled = 1;
868 if (netif_rx_schedule_prep(dev)) {
869 /* NOTE: These writes are posted by the readback of
870 * the ISTAT register below.
872 bp->istat = istat;
873 __b44_disable_ints(bp);
874 __netif_rx_schedule(dev);
875 } else {
876 printk(KERN_ERR PFX "%s: Error, poll already scheduled\n",
877 dev->name);
880 bw32(bp, B44_ISTAT, istat);
881 br32(bp, B44_ISTAT);
883 spin_unlock_irqrestore(&bp->lock, flags);
884 return IRQ_RETVAL(handled);
887 static void b44_tx_timeout(struct net_device *dev)
889 struct b44 *bp = netdev_priv(dev);
891 printk(KERN_ERR PFX "%s: transmit timed out, resetting\n",
892 dev->name);
894 spin_lock_irq(&bp->lock);
896 b44_halt(bp);
897 b44_init_rings(bp);
898 b44_init_hw(bp);
900 spin_unlock_irq(&bp->lock);
902 b44_enable_ints(bp);
904 netif_wake_queue(dev);
907 static int b44_start_xmit(struct sk_buff *skb, struct net_device *dev)
909 struct b44 *bp = netdev_priv(dev);
910 struct sk_buff *bounce_skb;
911 dma_addr_t mapping;
912 u32 len, entry, ctrl;
914 len = skb->len;
915 spin_lock_irq(&bp->lock);
917 /* This is a hard error, log it. */
918 if (unlikely(TX_BUFFS_AVAIL(bp) < 1)) {
919 netif_stop_queue(dev);
920 spin_unlock_irq(&bp->lock);
921 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
922 dev->name);
923 return 1;
926 mapping = pci_map_single(bp->pdev, skb->data, len, PCI_DMA_TODEVICE);
927 if(mapping+len > B44_DMA_MASK) {
928 /* Chip can't handle DMA to/from >1GB, use bounce buffer */
929 pci_unmap_single(bp->pdev, mapping, len, PCI_DMA_TODEVICE);
931 bounce_skb = __dev_alloc_skb(TX_PKT_BUF_SZ,
932 GFP_ATOMIC|GFP_DMA);
933 if (!bounce_skb)
934 return NETDEV_TX_BUSY;
936 mapping = pci_map_single(bp->pdev, bounce_skb->data,
937 len, PCI_DMA_TODEVICE);
938 if(mapping+len > B44_DMA_MASK) {
939 pci_unmap_single(bp->pdev, mapping,
940 len, PCI_DMA_TODEVICE);
941 dev_kfree_skb_any(bounce_skb);
942 return NETDEV_TX_BUSY;
945 memcpy(skb_put(bounce_skb, len), skb->data, skb->len);
946 dev_kfree_skb_any(skb);
947 skb = bounce_skb;
950 entry = bp->tx_prod;
951 bp->tx_buffers[entry].skb = skb;
952 pci_unmap_addr_set(&bp->tx_buffers[entry], mapping, mapping);
954 ctrl = (len & DESC_CTRL_LEN);
955 ctrl |= DESC_CTRL_IOC | DESC_CTRL_SOF | DESC_CTRL_EOF;
956 if (entry == (B44_TX_RING_SIZE - 1))
957 ctrl |= DESC_CTRL_EOT;
959 bp->tx_ring[entry].ctrl = cpu_to_le32(ctrl);
960 bp->tx_ring[entry].addr = cpu_to_le32((u32) mapping+bp->dma_offset);
962 entry = NEXT_TX(entry);
964 bp->tx_prod = entry;
966 wmb();
968 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
969 if (bp->flags & B44_FLAG_BUGGY_TXPTR)
970 bw32(bp, B44_DMATX_PTR, entry * sizeof(struct dma_desc));
971 if (bp->flags & B44_FLAG_REORDER_BUG)
972 br32(bp, B44_DMATX_PTR);
974 if (TX_BUFFS_AVAIL(bp) < 1)
975 netif_stop_queue(dev);
977 spin_unlock_irq(&bp->lock);
979 dev->trans_start = jiffies;
981 return 0;
984 static int b44_change_mtu(struct net_device *dev, int new_mtu)
986 struct b44 *bp = netdev_priv(dev);
988 if (new_mtu < B44_MIN_MTU || new_mtu > B44_MAX_MTU)
989 return -EINVAL;
991 if (!netif_running(dev)) {
992 /* We'll just catch it later when the
993 * device is up'd.
995 dev->mtu = new_mtu;
996 return 0;
999 spin_lock_irq(&bp->lock);
1000 b44_halt(bp);
1001 dev->mtu = new_mtu;
1002 b44_init_rings(bp);
1003 b44_init_hw(bp);
1004 spin_unlock_irq(&bp->lock);
1006 b44_enable_ints(bp);
1008 return 0;
1011 /* Free up pending packets in all rx/tx rings.
1013 * The chip has been shut down and the driver detached from
1014 * the networking, so no interrupts or new tx packets will
1015 * end up in the driver. bp->lock is not held and we are not
1016 * in an interrupt context and thus may sleep.
1018 static void b44_free_rings(struct b44 *bp)
1020 struct ring_info *rp;
1021 int i;
1023 for (i = 0; i < B44_RX_RING_SIZE; i++) {
1024 rp = &bp->rx_buffers[i];
1026 if (rp->skb == NULL)
1027 continue;
1028 pci_unmap_single(bp->pdev,
1029 pci_unmap_addr(rp, mapping),
1030 RX_PKT_BUF_SZ,
1031 PCI_DMA_FROMDEVICE);
1032 dev_kfree_skb_any(rp->skb);
1033 rp->skb = NULL;
1036 /* XXX needs changes once NETIF_F_SG is set... */
1037 for (i = 0; i < B44_TX_RING_SIZE; i++) {
1038 rp = &bp->tx_buffers[i];
1040 if (rp->skb == NULL)
1041 continue;
1042 pci_unmap_single(bp->pdev,
1043 pci_unmap_addr(rp, mapping),
1044 rp->skb->len,
1045 PCI_DMA_TODEVICE);
1046 dev_kfree_skb_any(rp->skb);
1047 rp->skb = NULL;
1051 /* Initialize tx/rx rings for packet processing.
1053 * The chip has been shut down and the driver detached from
1054 * the networking, so no interrupts or new tx packets will
1055 * end up in the driver. bp->lock is not held and we are not
1056 * in an interrupt context and thus may sleep.
1058 static void b44_init_rings(struct b44 *bp)
1060 int i;
1062 b44_free_rings(bp);
1064 memset(bp->rx_ring, 0, B44_RX_RING_BYTES);
1065 memset(bp->tx_ring, 0, B44_TX_RING_BYTES);
1067 for (i = 0; i < bp->rx_pending; i++) {
1068 if (b44_alloc_rx_skb(bp, -1, i) < 0)
1069 break;
1074 * Must not be invoked with interrupt sources disabled and
1075 * the hardware shutdown down.
1077 static void b44_free_consistent(struct b44 *bp)
1079 if (bp->rx_buffers) {
1080 kfree(bp->rx_buffers);
1081 bp->rx_buffers = NULL;
1083 if (bp->tx_buffers) {
1084 kfree(bp->tx_buffers);
1085 bp->tx_buffers = NULL;
1087 if (bp->rx_ring) {
1088 pci_free_consistent(bp->pdev, DMA_TABLE_BYTES,
1089 bp->rx_ring, bp->rx_ring_dma);
1090 bp->rx_ring = NULL;
1092 if (bp->tx_ring) {
1093 pci_free_consistent(bp->pdev, DMA_TABLE_BYTES,
1094 bp->tx_ring, bp->tx_ring_dma);
1095 bp->tx_ring = NULL;
1100 * Must not be invoked with interrupt sources disabled and
1101 * the hardware shutdown down. Can sleep.
1103 static int b44_alloc_consistent(struct b44 *bp)
1105 int size;
1107 size = B44_RX_RING_SIZE * sizeof(struct ring_info);
1108 bp->rx_buffers = kmalloc(size, GFP_KERNEL);
1109 if (!bp->rx_buffers)
1110 goto out_err;
1111 memset(bp->rx_buffers, 0, size);
1113 size = B44_TX_RING_SIZE * sizeof(struct ring_info);
1114 bp->tx_buffers = kmalloc(size, GFP_KERNEL);
1115 if (!bp->tx_buffers)
1116 goto out_err;
1117 memset(bp->tx_buffers, 0, size);
1119 size = DMA_TABLE_BYTES;
1120 bp->rx_ring = pci_alloc_consistent(bp->pdev, size, &bp->rx_ring_dma);
1121 if (!bp->rx_ring)
1122 goto out_err;
1124 bp->tx_ring = pci_alloc_consistent(bp->pdev, size, &bp->tx_ring_dma);
1125 if (!bp->tx_ring)
1126 goto out_err;
1128 return 0;
1130 out_err:
1131 b44_free_consistent(bp);
1132 return -ENOMEM;
1135 /* bp->lock is held. */
1136 static void b44_clear_stats(struct b44 *bp)
1138 unsigned long reg;
1140 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
1141 for (reg = B44_TX_GOOD_O; reg <= B44_TX_PAUSE; reg += 4UL)
1142 br32(bp, reg);
1143 for (reg = B44_RX_GOOD_O; reg <= B44_RX_NPAUSE; reg += 4UL)
1144 br32(bp, reg);
1147 /* bp->lock is held. */
1148 static void b44_chip_reset(struct b44 *bp)
1150 if (ssb_is_core_up(bp)) {
1151 bw32(bp, B44_RCV_LAZY, 0);
1152 bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
1153 b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 100, 1);
1154 bw32(bp, B44_DMATX_CTRL, 0);
1155 bp->tx_prod = bp->tx_cons = 0;
1156 if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK) {
1157 b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
1158 100, 0);
1160 bw32(bp, B44_DMARX_CTRL, 0);
1161 bp->rx_prod = bp->rx_cons = 0;
1162 } else {
1163 ssb_pci_setup(bp, (bp->core_unit == 0 ?
1164 SBINTVEC_ENET0 :
1165 SBINTVEC_ENET1));
1168 ssb_core_reset(bp);
1170 b44_clear_stats(bp);
1172 /* Make PHY accessible. */
1173 bw32(bp, B44_MDIO_CTRL, (MDIO_CTRL_PREAMBLE |
1174 (0x0d & MDIO_CTRL_MAXF_MASK)));
1175 br32(bp, B44_MDIO_CTRL);
1177 if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
1178 bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
1179 br32(bp, B44_ENET_CTRL);
1180 bp->flags &= ~B44_FLAG_INTERNAL_PHY;
1181 } else {
1182 u32 val = br32(bp, B44_DEVCTRL);
1184 if (val & DEVCTRL_EPR) {
1185 bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
1186 br32(bp, B44_DEVCTRL);
1187 udelay(100);
1189 bp->flags |= B44_FLAG_INTERNAL_PHY;
1193 /* bp->lock is held. */
1194 static void b44_halt(struct b44 *bp)
1196 b44_disable_ints(bp);
1197 b44_chip_reset(bp);
1200 /* bp->lock is held. */
1201 static void __b44_set_mac_addr(struct b44 *bp)
1203 bw32(bp, B44_CAM_CTRL, 0);
1204 if (!(bp->dev->flags & IFF_PROMISC)) {
1205 u32 val;
1207 __b44_cam_write(bp, bp->dev->dev_addr, 0);
1208 val = br32(bp, B44_CAM_CTRL);
1209 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1213 static int b44_set_mac_addr(struct net_device *dev, void *p)
1215 struct b44 *bp = netdev_priv(dev);
1216 struct sockaddr *addr = p;
1218 if (netif_running(dev))
1219 return -EBUSY;
1221 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1223 spin_lock_irq(&bp->lock);
1224 __b44_set_mac_addr(bp);
1225 spin_unlock_irq(&bp->lock);
1227 return 0;
1230 /* Called at device open time to get the chip ready for
1231 * packet processing. Invoked with bp->lock held.
1233 static void __b44_set_rx_mode(struct net_device *);
1234 static void b44_init_hw(struct b44 *bp)
1236 u32 val;
1238 b44_chip_reset(bp);
1239 b44_phy_reset(bp);
1240 b44_setup_phy(bp);
1242 /* Enable CRC32, set proper LED modes and power on PHY */
1243 bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
1244 bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
1246 /* This sets the MAC address too. */
1247 __b44_set_rx_mode(bp->dev);
1249 /* MTU + eth header + possible VLAN tag + struct rx_header */
1250 bw32(bp, B44_RXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1251 bw32(bp, B44_TXMAXLEN, bp->dev->mtu + ETH_HLEN + 8 + RX_HEADER_LEN);
1253 bw32(bp, B44_TX_WMARK, 56); /* XXX magic */
1254 bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
1255 bw32(bp, B44_DMATX_ADDR, bp->tx_ring_dma + bp->dma_offset);
1256 bw32(bp, B44_DMARX_CTRL, (DMARX_CTRL_ENABLE |
1257 (bp->rx_offset << DMARX_CTRL_ROSHIFT)));
1258 bw32(bp, B44_DMARX_ADDR, bp->rx_ring_dma + bp->dma_offset);
1260 bw32(bp, B44_DMARX_PTR, bp->rx_pending);
1261 bp->rx_prod = bp->rx_pending;
1263 bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
1265 val = br32(bp, B44_ENET_CTRL);
1266 bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
1269 static int b44_open(struct net_device *dev)
1271 struct b44 *bp = netdev_priv(dev);
1272 int err;
1274 err = b44_alloc_consistent(bp);
1275 if (err)
1276 return err;
1278 err = request_irq(dev->irq, b44_interrupt, SA_SHIRQ, dev->name, dev);
1279 if (err)
1280 goto err_out_free;
1282 spin_lock_irq(&bp->lock);
1284 b44_init_rings(bp);
1285 b44_init_hw(bp);
1286 bp->flags |= B44_FLAG_INIT_COMPLETE;
1288 netif_carrier_off(dev);
1289 b44_check_phy(bp);
1291 spin_unlock_irq(&bp->lock);
1293 init_timer(&bp->timer);
1294 bp->timer.expires = jiffies + HZ;
1295 bp->timer.data = (unsigned long) bp;
1296 bp->timer.function = b44_timer;
1297 add_timer(&bp->timer);
1299 b44_enable_ints(bp);
1301 return 0;
1303 err_out_free:
1304 b44_free_consistent(bp);
1305 return err;
1308 #if 0
1309 /*static*/ void b44_dump_state(struct b44 *bp)
1311 u32 val32, val32_2, val32_3, val32_4, val32_5;
1312 u16 val16;
1314 pci_read_config_word(bp->pdev, PCI_STATUS, &val16);
1315 printk("DEBUG: PCI status [%04x] \n", val16);
1318 #endif
1320 #ifdef CONFIG_NET_POLL_CONTROLLER
1322 * Polling receive - used by netconsole and other diagnostic tools
1323 * to allow network i/o with interrupts disabled.
1325 static void b44_poll_controller(struct net_device *dev)
1327 disable_irq(dev->irq);
1328 b44_interrupt(dev->irq, dev, NULL);
1329 enable_irq(dev->irq);
1331 #endif
1333 static int b44_close(struct net_device *dev)
1335 struct b44 *bp = netdev_priv(dev);
1337 netif_stop_queue(dev);
1339 del_timer_sync(&bp->timer);
1341 spin_lock_irq(&bp->lock);
1343 #if 0
1344 b44_dump_state(bp);
1345 #endif
1346 b44_halt(bp);
1347 b44_free_rings(bp);
1348 bp->flags &= ~B44_FLAG_INIT_COMPLETE;
1349 netif_carrier_off(bp->dev);
1351 spin_unlock_irq(&bp->lock);
1353 free_irq(dev->irq, dev);
1355 b44_free_consistent(bp);
1357 return 0;
1360 static struct net_device_stats *b44_get_stats(struct net_device *dev)
1362 struct b44 *bp = netdev_priv(dev);
1363 struct net_device_stats *nstat = &bp->stats;
1364 struct b44_hw_stats *hwstat = &bp->hw_stats;
1366 /* Convert HW stats into netdevice stats. */
1367 nstat->rx_packets = hwstat->rx_pkts;
1368 nstat->tx_packets = hwstat->tx_pkts;
1369 nstat->rx_bytes = hwstat->rx_octets;
1370 nstat->tx_bytes = hwstat->tx_octets;
1371 nstat->tx_errors = (hwstat->tx_jabber_pkts +
1372 hwstat->tx_oversize_pkts +
1373 hwstat->tx_underruns +
1374 hwstat->tx_excessive_cols +
1375 hwstat->tx_late_cols);
1376 nstat->multicast = hwstat->tx_multicast_pkts;
1377 nstat->collisions = hwstat->tx_total_cols;
1379 nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1380 hwstat->rx_undersize);
1381 nstat->rx_over_errors = hwstat->rx_missed_pkts;
1382 nstat->rx_frame_errors = hwstat->rx_align_errs;
1383 nstat->rx_crc_errors = hwstat->rx_crc_errs;
1384 nstat->rx_errors = (hwstat->rx_jabber_pkts +
1385 hwstat->rx_oversize_pkts +
1386 hwstat->rx_missed_pkts +
1387 hwstat->rx_crc_align_errs +
1388 hwstat->rx_undersize +
1389 hwstat->rx_crc_errs +
1390 hwstat->rx_align_errs +
1391 hwstat->rx_symbol_errs);
1393 nstat->tx_aborted_errors = hwstat->tx_underruns;
1394 #if 0
1395 /* Carrier lost counter seems to be broken for some devices */
1396 nstat->tx_carrier_errors = hwstat->tx_carrier_lost;
1397 #endif
1399 return nstat;
1402 static int __b44_load_mcast(struct b44 *bp, struct net_device *dev)
1404 struct dev_mc_list *mclist;
1405 int i, num_ents;
1407 num_ents = min_t(int, dev->mc_count, B44_MCAST_TABLE_SIZE);
1408 mclist = dev->mc_list;
1409 for (i = 0; mclist && i < num_ents; i++, mclist = mclist->next) {
1410 __b44_cam_write(bp, mclist->dmi_addr, i + 1);
1412 return i+1;
1415 static void __b44_set_rx_mode(struct net_device *dev)
1417 struct b44 *bp = netdev_priv(dev);
1418 u32 val;
1419 int i=0;
1420 unsigned char zero[6] = {0,0,0,0,0,0};
1422 val = br32(bp, B44_RXCONFIG);
1423 val &= ~(RXCONFIG_PROMISC | RXCONFIG_ALLMULTI);
1424 if (dev->flags & IFF_PROMISC) {
1425 val |= RXCONFIG_PROMISC;
1426 bw32(bp, B44_RXCONFIG, val);
1427 } else {
1428 __b44_set_mac_addr(bp);
1430 if (dev->flags & IFF_ALLMULTI)
1431 val |= RXCONFIG_ALLMULTI;
1432 else
1433 i=__b44_load_mcast(bp, dev);
1435 for(;i<64;i++) {
1436 __b44_cam_write(bp, zero, i);
1438 bw32(bp, B44_RXCONFIG, val);
1439 val = br32(bp, B44_CAM_CTRL);
1440 bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
1444 static void b44_set_rx_mode(struct net_device *dev)
1446 struct b44 *bp = netdev_priv(dev);
1448 spin_lock_irq(&bp->lock);
1449 __b44_set_rx_mode(dev);
1450 spin_unlock_irq(&bp->lock);
1453 static u32 b44_get_msglevel(struct net_device *dev)
1455 struct b44 *bp = netdev_priv(dev);
1456 return bp->msg_enable;
1459 static void b44_set_msglevel(struct net_device *dev, u32 value)
1461 struct b44 *bp = netdev_priv(dev);
1462 bp->msg_enable = value;
1465 static void b44_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1467 struct b44 *bp = netdev_priv(dev);
1468 struct pci_dev *pci_dev = bp->pdev;
1470 strcpy (info->driver, DRV_MODULE_NAME);
1471 strcpy (info->version, DRV_MODULE_VERSION);
1472 strcpy (info->bus_info, pci_name(pci_dev));
1475 static int b44_nway_reset(struct net_device *dev)
1477 struct b44 *bp = netdev_priv(dev);
1478 u32 bmcr;
1479 int r;
1481 spin_lock_irq(&bp->lock);
1482 b44_readphy(bp, MII_BMCR, &bmcr);
1483 b44_readphy(bp, MII_BMCR, &bmcr);
1484 r = -EINVAL;
1485 if (bmcr & BMCR_ANENABLE) {
1486 b44_writephy(bp, MII_BMCR,
1487 bmcr | BMCR_ANRESTART);
1488 r = 0;
1490 spin_unlock_irq(&bp->lock);
1492 return r;
1495 static int b44_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1497 struct b44 *bp = netdev_priv(dev);
1499 if (!(bp->flags & B44_FLAG_INIT_COMPLETE))
1500 return -EAGAIN;
1501 cmd->supported = (SUPPORTED_Autoneg);
1502 cmd->supported |= (SUPPORTED_100baseT_Half |
1503 SUPPORTED_100baseT_Full |
1504 SUPPORTED_10baseT_Half |
1505 SUPPORTED_10baseT_Full |
1506 SUPPORTED_MII);
1508 cmd->advertising = 0;
1509 if (bp->flags & B44_FLAG_ADV_10HALF)
1510 cmd->advertising |= ADVERTISE_10HALF;
1511 if (bp->flags & B44_FLAG_ADV_10FULL)
1512 cmd->advertising |= ADVERTISE_10FULL;
1513 if (bp->flags & B44_FLAG_ADV_100HALF)
1514 cmd->advertising |= ADVERTISE_100HALF;
1515 if (bp->flags & B44_FLAG_ADV_100FULL)
1516 cmd->advertising |= ADVERTISE_100FULL;
1517 cmd->advertising |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1518 cmd->speed = (bp->flags & B44_FLAG_100_BASE_T) ?
1519 SPEED_100 : SPEED_10;
1520 cmd->duplex = (bp->flags & B44_FLAG_FULL_DUPLEX) ?
1521 DUPLEX_FULL : DUPLEX_HALF;
1522 cmd->port = 0;
1523 cmd->phy_address = bp->phy_addr;
1524 cmd->transceiver = (bp->flags & B44_FLAG_INTERNAL_PHY) ?
1525 XCVR_INTERNAL : XCVR_EXTERNAL;
1526 cmd->autoneg = (bp->flags & B44_FLAG_FORCE_LINK) ?
1527 AUTONEG_DISABLE : AUTONEG_ENABLE;
1528 cmd->maxtxpkt = 0;
1529 cmd->maxrxpkt = 0;
1530 return 0;
1533 static int b44_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1535 struct b44 *bp = netdev_priv(dev);
1537 if (!(bp->flags & B44_FLAG_INIT_COMPLETE))
1538 return -EAGAIN;
1540 /* We do not support gigabit. */
1541 if (cmd->autoneg == AUTONEG_ENABLE) {
1542 if (cmd->advertising &
1543 (ADVERTISED_1000baseT_Half |
1544 ADVERTISED_1000baseT_Full))
1545 return -EINVAL;
1546 } else if ((cmd->speed != SPEED_100 &&
1547 cmd->speed != SPEED_10) ||
1548 (cmd->duplex != DUPLEX_HALF &&
1549 cmd->duplex != DUPLEX_FULL)) {
1550 return -EINVAL;
1553 spin_lock_irq(&bp->lock);
1555 if (cmd->autoneg == AUTONEG_ENABLE) {
1556 bp->flags &= ~B44_FLAG_FORCE_LINK;
1557 bp->flags &= ~(B44_FLAG_ADV_10HALF |
1558 B44_FLAG_ADV_10FULL |
1559 B44_FLAG_ADV_100HALF |
1560 B44_FLAG_ADV_100FULL);
1561 if (cmd->advertising & ADVERTISE_10HALF)
1562 bp->flags |= B44_FLAG_ADV_10HALF;
1563 if (cmd->advertising & ADVERTISE_10FULL)
1564 bp->flags |= B44_FLAG_ADV_10FULL;
1565 if (cmd->advertising & ADVERTISE_100HALF)
1566 bp->flags |= B44_FLAG_ADV_100HALF;
1567 if (cmd->advertising & ADVERTISE_100FULL)
1568 bp->flags |= B44_FLAG_ADV_100FULL;
1569 } else {
1570 bp->flags |= B44_FLAG_FORCE_LINK;
1571 if (cmd->speed == SPEED_100)
1572 bp->flags |= B44_FLAG_100_BASE_T;
1573 if (cmd->duplex == DUPLEX_FULL)
1574 bp->flags |= B44_FLAG_FULL_DUPLEX;
1577 b44_setup_phy(bp);
1579 spin_unlock_irq(&bp->lock);
1581 return 0;
1584 static void b44_get_ringparam(struct net_device *dev,
1585 struct ethtool_ringparam *ering)
1587 struct b44 *bp = netdev_priv(dev);
1589 ering->rx_max_pending = B44_RX_RING_SIZE - 1;
1590 ering->rx_pending = bp->rx_pending;
1592 /* XXX ethtool lacks a tx_max_pending, oops... */
1595 static int b44_set_ringparam(struct net_device *dev,
1596 struct ethtool_ringparam *ering)
1598 struct b44 *bp = netdev_priv(dev);
1600 if ((ering->rx_pending > B44_RX_RING_SIZE - 1) ||
1601 (ering->rx_mini_pending != 0) ||
1602 (ering->rx_jumbo_pending != 0) ||
1603 (ering->tx_pending > B44_TX_RING_SIZE - 1))
1604 return -EINVAL;
1606 spin_lock_irq(&bp->lock);
1608 bp->rx_pending = ering->rx_pending;
1609 bp->tx_pending = ering->tx_pending;
1611 b44_halt(bp);
1612 b44_init_rings(bp);
1613 b44_init_hw(bp);
1614 netif_wake_queue(bp->dev);
1615 spin_unlock_irq(&bp->lock);
1617 b44_enable_ints(bp);
1619 return 0;
1622 static void b44_get_pauseparam(struct net_device *dev,
1623 struct ethtool_pauseparam *epause)
1625 struct b44 *bp = netdev_priv(dev);
1627 epause->autoneg =
1628 (bp->flags & B44_FLAG_PAUSE_AUTO) != 0;
1629 epause->rx_pause =
1630 (bp->flags & B44_FLAG_RX_PAUSE) != 0;
1631 epause->tx_pause =
1632 (bp->flags & B44_FLAG_TX_PAUSE) != 0;
1635 static int b44_set_pauseparam(struct net_device *dev,
1636 struct ethtool_pauseparam *epause)
1638 struct b44 *bp = netdev_priv(dev);
1640 spin_lock_irq(&bp->lock);
1641 if (epause->autoneg)
1642 bp->flags |= B44_FLAG_PAUSE_AUTO;
1643 else
1644 bp->flags &= ~B44_FLAG_PAUSE_AUTO;
1645 if (epause->rx_pause)
1646 bp->flags |= B44_FLAG_RX_PAUSE;
1647 else
1648 bp->flags &= ~B44_FLAG_RX_PAUSE;
1649 if (epause->tx_pause)
1650 bp->flags |= B44_FLAG_TX_PAUSE;
1651 else
1652 bp->flags &= ~B44_FLAG_TX_PAUSE;
1653 if (bp->flags & B44_FLAG_PAUSE_AUTO) {
1654 b44_halt(bp);
1655 b44_init_rings(bp);
1656 b44_init_hw(bp);
1657 } else {
1658 __b44_set_flow_ctrl(bp, bp->flags);
1660 spin_unlock_irq(&bp->lock);
1662 b44_enable_ints(bp);
1664 return 0;
1667 static struct ethtool_ops b44_ethtool_ops = {
1668 .get_drvinfo = b44_get_drvinfo,
1669 .get_settings = b44_get_settings,
1670 .set_settings = b44_set_settings,
1671 .nway_reset = b44_nway_reset,
1672 .get_link = ethtool_op_get_link,
1673 .get_ringparam = b44_get_ringparam,
1674 .set_ringparam = b44_set_ringparam,
1675 .get_pauseparam = b44_get_pauseparam,
1676 .set_pauseparam = b44_set_pauseparam,
1677 .get_msglevel = b44_get_msglevel,
1678 .set_msglevel = b44_set_msglevel,
1681 static int b44_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1683 struct mii_ioctl_data *data = if_mii(ifr);
1684 struct b44 *bp = netdev_priv(dev);
1685 int err;
1687 spin_lock_irq(&bp->lock);
1688 err = generic_mii_ioctl(&bp->mii_if, data, cmd, NULL);
1689 spin_unlock_irq(&bp->lock);
1691 return err;
1694 /* Read 128-bytes of EEPROM. */
1695 static int b44_read_eeprom(struct b44 *bp, u8 *data)
1697 long i;
1698 u16 *ptr = (u16 *) data;
1700 for (i = 0; i < 128; i += 2)
1701 ptr[i / 2] = readw(bp->regs + 4096 + i);
1703 return 0;
1706 static int __devinit b44_get_invariants(struct b44 *bp)
1708 u8 eeprom[128];
1709 int err;
1711 err = b44_read_eeprom(bp, &eeprom[0]);
1712 if (err)
1713 goto out;
1715 bp->dev->dev_addr[0] = eeprom[79];
1716 bp->dev->dev_addr[1] = eeprom[78];
1717 bp->dev->dev_addr[2] = eeprom[81];
1718 bp->dev->dev_addr[3] = eeprom[80];
1719 bp->dev->dev_addr[4] = eeprom[83];
1720 bp->dev->dev_addr[5] = eeprom[82];
1722 bp->phy_addr = eeprom[90] & 0x1f;
1724 /* With this, plus the rx_header prepended to the data by the
1725 * hardware, we'll land the ethernet header on a 2-byte boundary.
1727 bp->rx_offset = 30;
1729 bp->imask = IMASK_DEF;
1731 bp->core_unit = ssb_core_unit(bp);
1732 bp->dma_offset = SB_PCI_DMA;
1734 /* XXX - really required?
1735 bp->flags |= B44_FLAG_BUGGY_TXPTR;
1737 out:
1738 return err;
1741 static int __devinit b44_init_one(struct pci_dev *pdev,
1742 const struct pci_device_id *ent)
1744 static int b44_version_printed = 0;
1745 unsigned long b44reg_base, b44reg_len;
1746 struct net_device *dev;
1747 struct b44 *bp;
1748 int err, i;
1750 if (b44_version_printed++ == 0)
1751 printk(KERN_INFO "%s", version);
1753 err = pci_enable_device(pdev);
1754 if (err) {
1755 printk(KERN_ERR PFX "Cannot enable PCI device, "
1756 "aborting.\n");
1757 return err;
1760 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
1761 printk(KERN_ERR PFX "Cannot find proper PCI device "
1762 "base address, aborting.\n");
1763 err = -ENODEV;
1764 goto err_out_disable_pdev;
1767 err = pci_request_regions(pdev, DRV_MODULE_NAME);
1768 if (err) {
1769 printk(KERN_ERR PFX "Cannot obtain PCI resources, "
1770 "aborting.\n");
1771 goto err_out_disable_pdev;
1774 pci_set_master(pdev);
1776 err = pci_set_dma_mask(pdev, (u64) B44_DMA_MASK);
1777 if (err) {
1778 printk(KERN_ERR PFX "No usable DMA configuration, "
1779 "aborting.\n");
1780 goto err_out_free_res;
1783 err = pci_set_consistent_dma_mask(pdev, (u64) B44_DMA_MASK);
1784 if (err) {
1785 printk(KERN_ERR PFX "No usable DMA configuration, "
1786 "aborting.\n");
1787 goto err_out_free_res;
1790 b44reg_base = pci_resource_start(pdev, 0);
1791 b44reg_len = pci_resource_len(pdev, 0);
1793 dev = alloc_etherdev(sizeof(*bp));
1794 if (!dev) {
1795 printk(KERN_ERR PFX "Etherdev alloc failed, aborting.\n");
1796 err = -ENOMEM;
1797 goto err_out_free_res;
1800 SET_MODULE_OWNER(dev);
1801 SET_NETDEV_DEV(dev,&pdev->dev);
1803 /* No interesting netdevice features in this card... */
1804 dev->features |= 0;
1806 bp = netdev_priv(dev);
1807 bp->pdev = pdev;
1808 bp->dev = dev;
1809 if (b44_debug >= 0)
1810 bp->msg_enable = (1 << b44_debug) - 1;
1811 else
1812 bp->msg_enable = B44_DEF_MSG_ENABLE;
1814 spin_lock_init(&bp->lock);
1816 bp->regs = ioremap(b44reg_base, b44reg_len);
1817 if (bp->regs == 0UL) {
1818 printk(KERN_ERR PFX "Cannot map device registers, "
1819 "aborting.\n");
1820 err = -ENOMEM;
1821 goto err_out_free_dev;
1824 bp->rx_pending = B44_DEF_RX_RING_PENDING;
1825 bp->tx_pending = B44_DEF_TX_RING_PENDING;
1827 dev->open = b44_open;
1828 dev->stop = b44_close;
1829 dev->hard_start_xmit = b44_start_xmit;
1830 dev->get_stats = b44_get_stats;
1831 dev->set_multicast_list = b44_set_rx_mode;
1832 dev->set_mac_address = b44_set_mac_addr;
1833 dev->do_ioctl = b44_ioctl;
1834 dev->tx_timeout = b44_tx_timeout;
1835 dev->poll = b44_poll;
1836 dev->weight = 64;
1837 dev->watchdog_timeo = B44_TX_TIMEOUT;
1838 #ifdef CONFIG_NET_POLL_CONTROLLER
1839 dev->poll_controller = b44_poll_controller;
1840 #endif
1841 dev->change_mtu = b44_change_mtu;
1842 dev->irq = pdev->irq;
1843 SET_ETHTOOL_OPS(dev, &b44_ethtool_ops);
1845 err = b44_get_invariants(bp);
1846 if (err) {
1847 printk(KERN_ERR PFX "Problem fetching invariants of chip, "
1848 "aborting.\n");
1849 goto err_out_iounmap;
1852 bp->mii_if.dev = dev;
1853 bp->mii_if.mdio_read = b44_mii_read;
1854 bp->mii_if.mdio_write = b44_mii_write;
1855 bp->mii_if.phy_id = bp->phy_addr;
1856 bp->mii_if.phy_id_mask = 0x1f;
1857 bp->mii_if.reg_num_mask = 0x1f;
1859 /* By default, advertise all speed/duplex settings. */
1860 bp->flags |= (B44_FLAG_ADV_10HALF | B44_FLAG_ADV_10FULL |
1861 B44_FLAG_ADV_100HALF | B44_FLAG_ADV_100FULL);
1863 /* By default, auto-negotiate PAUSE. */
1864 bp->flags |= B44_FLAG_PAUSE_AUTO;
1866 err = register_netdev(dev);
1867 if (err) {
1868 printk(KERN_ERR PFX "Cannot register net device, "
1869 "aborting.\n");
1870 goto err_out_iounmap;
1873 pci_set_drvdata(pdev, dev);
1875 pci_save_state(bp->pdev);
1877 printk(KERN_INFO "%s: Broadcom 4400 10/100BaseT Ethernet ", dev->name);
1878 for (i = 0; i < 6; i++)
1879 printk("%2.2x%c", dev->dev_addr[i],
1880 i == 5 ? '\n' : ':');
1882 return 0;
1884 err_out_iounmap:
1885 iounmap(bp->regs);
1887 err_out_free_dev:
1888 free_netdev(dev);
1890 err_out_free_res:
1891 pci_release_regions(pdev);
1893 err_out_disable_pdev:
1894 pci_disable_device(pdev);
1895 pci_set_drvdata(pdev, NULL);
1896 return err;
1899 static void __devexit b44_remove_one(struct pci_dev *pdev)
1901 struct net_device *dev = pci_get_drvdata(pdev);
1903 if (dev) {
1904 struct b44 *bp = netdev_priv(dev);
1906 unregister_netdev(dev);
1907 iounmap(bp->regs);
1908 free_netdev(dev);
1909 pci_release_regions(pdev);
1910 pci_disable_device(pdev);
1911 pci_set_drvdata(pdev, NULL);
1915 static int b44_suspend(struct pci_dev *pdev, pm_message_t state)
1917 struct net_device *dev = pci_get_drvdata(pdev);
1918 struct b44 *bp = netdev_priv(dev);
1920 if (!netif_running(dev))
1921 return 0;
1923 del_timer_sync(&bp->timer);
1925 spin_lock_irq(&bp->lock);
1927 b44_halt(bp);
1928 netif_carrier_off(bp->dev);
1929 netif_device_detach(bp->dev);
1930 b44_free_rings(bp);
1932 spin_unlock_irq(&bp->lock);
1933 pci_disable_device(pdev);
1934 return 0;
1937 static int b44_resume(struct pci_dev *pdev)
1939 struct net_device *dev = pci_get_drvdata(pdev);
1940 struct b44 *bp = netdev_priv(dev);
1942 pci_restore_state(pdev);
1943 pci_enable_device(pdev);
1944 pci_set_master(pdev);
1946 if (!netif_running(dev))
1947 return 0;
1949 spin_lock_irq(&bp->lock);
1951 b44_init_rings(bp);
1952 b44_init_hw(bp);
1953 netif_device_attach(bp->dev);
1954 spin_unlock_irq(&bp->lock);
1956 bp->timer.expires = jiffies + HZ;
1957 add_timer(&bp->timer);
1959 b44_enable_ints(bp);
1960 return 0;
1963 static struct pci_driver b44_driver = {
1964 .name = DRV_MODULE_NAME,
1965 .id_table = b44_pci_tbl,
1966 .probe = b44_init_one,
1967 .remove = __devexit_p(b44_remove_one),
1968 .suspend = b44_suspend,
1969 .resume = b44_resume,
1972 static int __init b44_init(void)
1974 return pci_module_init(&b44_driver);
1977 static void __exit b44_cleanup(void)
1979 pci_unregister_driver(&b44_driver);
1982 module_init(b44_init);
1983 module_exit(b44_cleanup);