Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / drivers / net / sunlance.c
1 /* $Id: sunlance.c,v 1.112 2002/01/15 06:48:55 davem Exp $
2  * lance.c: Linux/Sparc/Lance driver
3  *
4  *      Written 1995, 1996 by Miguel de Icaza
5  * Sources:
6  *      The Linux  depca driver
7  *      The Linux  lance driver.
8  *      The Linux  skeleton driver.
9  *      The NetBSD Sparc/Lance driver.
10  *      Theo de Raadt (deraadt@openbsd.org)
11  *      NCR92C990 Lan Controller manual
12  *
13  * 1.4:
14  *      Added support to run with a ledma on the Sun4m
15  *
16  * 1.5:
17  *      Added multiple card detection.
18  *
19  *       4/17/96: Burst sizes and tpe selection on sun4m by Eddie C. Dost
20  *                (ecd@skynet.be)
21  *
22  *       5/15/96: auto carrier detection on sun4m by Eddie C. Dost
23  *                (ecd@skynet.be)
24  *
25  *       5/17/96: lebuffer on scsi/ether cards now work David S. Miller
26  *                (davem@caip.rutgers.edu)
27  *
28  *       5/29/96: override option 'tpe-link-test?', if it is 'false', as
29  *                this disables auto carrier detection on sun4m. Eddie C. Dost
30  *                (ecd@skynet.be)
31  *
32  * 1.7:
33  *       6/26/96: Bug fix for multiple ledmas, miguel.
34  *
35  * 1.8:
36  *                Stole multicast code from depca.c, fixed lance_tx.
37  *
38  * 1.9:
39  *       8/21/96: Fixed the multicast code (Pedro Roque)
40  *
41  *       8/28/96: Send fake packet in lance_open() if auto_select is true,
42  *                so we can detect the carrier loss condition in time.
43  *                Eddie C. Dost (ecd@skynet.be)
44  *
45  *       9/15/96: Align rx_buf so that eth_copy_and_sum() won't cause an
46  *                MNA trap during chksum_partial_copy(). (ecd@skynet.be)
47  *
48  *      11/17/96: Handle LE_C0_MERR in lance_interrupt(). (ecd@skynet.be)
49  *
50  *      12/22/96: Don't loop forever in lance_rx() on incomplete packets.
51  *                This was the sun4c killer. Shit, stupid bug.
52  *                (ecd@skynet.be)
53  *
54  * 1.10:
55  *       1/26/97: Modularize driver. (ecd@skynet.be)
56  *
57  * 1.11:
58  *      12/27/97: Added sun4d support. (jj@sunsite.mff.cuni.cz)
59  *
60  * 1.12:
61  *       11/3/99: Fixed SMP race in lance_start_xmit found by davem.
62  *                Anton Blanchard (anton@progsoc.uts.edu.au)
63  * 2.00: 11/9/99: Massive overhaul and port to new SBUS driver interfaces.
64  *                David S. Miller (davem@redhat.com)
65  * 2.01:
66  *      11/08/01: Use library crc32 functions (Matt_Domsch@dell.com)
67  *
68  */
69
70 #undef DEBUG_DRIVER
71
72 static char lancestr[] = "LANCE";
73
74 #include <linux/module.h>
75 #include <linux/kernel.h>
76 #include <linux/types.h>
77 #include <linux/fcntl.h>
78 #include <linux/interrupt.h>
79 #include <linux/ioport.h>
80 #include <linux/in.h>
81 #include <linux/slab.h>
82 #include <linux/string.h>
83 #include <linux/delay.h>
84 #include <linux/init.h>
85 #include <linux/crc32.h>
86 #include <linux/errno.h>
87 #include <linux/socket.h> /* Used for the temporal inet entries and routing */
88 #include <linux/route.h>
89 #include <linux/netdevice.h>
90 #include <linux/etherdevice.h>
91 #include <linux/skbuff.h>
92 #include <linux/ethtool.h>
93 #include <linux/bitops.h>
94
95 #include <asm/system.h>
96 #include <asm/io.h>
97 #include <asm/dma.h>
98 #include <asm/pgtable.h>
99 #include <asm/byteorder.h>      /* Used by the checksum routines */
100 #include <asm/idprom.h>
101 #include <asm/sbus.h>
102 #include <asm/prom.h>
103 #include <asm/auxio.h>          /* For tpe-link-test? setting */
104 #include <asm/irq.h>
105
106 #define DRV_NAME        "sunlance"
107 #define DRV_VERSION     "2.02"
108 #define DRV_RELDATE     "8/24/03"
109 #define DRV_AUTHOR      "Miguel de Icaza (miguel@nuclecu.unam.mx)"
110
111 static char version[] =
112         DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
113
114 MODULE_VERSION(DRV_VERSION);
115 MODULE_AUTHOR(DRV_AUTHOR);
116 MODULE_DESCRIPTION("Sun Lance ethernet driver");
117 MODULE_LICENSE("GPL");
118
119 /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
120 #ifndef LANCE_LOG_TX_BUFFERS
121 #define LANCE_LOG_TX_BUFFERS 4
122 #define LANCE_LOG_RX_BUFFERS 4
123 #endif
124
125 #define LE_CSR0 0
126 #define LE_CSR1 1
127 #define LE_CSR2 2
128 #define LE_CSR3 3
129
130 #define LE_MO_PROM      0x8000  /* Enable promiscuous mode */
131
132 #define LE_C0_ERR       0x8000  /* Error: set if BAB, SQE, MISS or ME is set */
133 #define LE_C0_BABL      0x4000  /* BAB:  Babble: tx timeout. */
134 #define LE_C0_CERR      0x2000  /* SQE:  Signal quality error */
135 #define LE_C0_MISS      0x1000  /* MISS: Missed a packet */
136 #define LE_C0_MERR      0x0800  /* ME:   Memory error */
137 #define LE_C0_RINT      0x0400  /* Received interrupt */
138 #define LE_C0_TINT      0x0200  /* Transmitter Interrupt */
139 #define LE_C0_IDON      0x0100  /* IFIN: Init finished. */
140 #define LE_C0_INTR      0x0080  /* Interrupt or error */
141 #define LE_C0_INEA      0x0040  /* Interrupt enable */
142 #define LE_C0_RXON      0x0020  /* Receiver on */
143 #define LE_C0_TXON      0x0010  /* Transmitter on */
144 #define LE_C0_TDMD      0x0008  /* Transmitter demand */
145 #define LE_C0_STOP      0x0004  /* Stop the card */
146 #define LE_C0_STRT      0x0002  /* Start the card */
147 #define LE_C0_INIT      0x0001  /* Init the card */
148
149 #define LE_C3_BSWP      0x4     /* SWAP */
150 #define LE_C3_ACON      0x2     /* ALE Control */
151 #define LE_C3_BCON      0x1     /* Byte control */
152
153 /* Receive message descriptor 1 */
154 #define LE_R1_OWN       0x80    /* Who owns the entry */
155 #define LE_R1_ERR       0x40    /* Error: if FRA, OFL, CRC or BUF is set */
156 #define LE_R1_FRA       0x20    /* FRA: Frame error */
157 #define LE_R1_OFL       0x10    /* OFL: Frame overflow */
158 #define LE_R1_CRC       0x08    /* CRC error */
159 #define LE_R1_BUF       0x04    /* BUF: Buffer error */
160 #define LE_R1_SOP       0x02    /* Start of packet */
161 #define LE_R1_EOP       0x01    /* End of packet */
162 #define LE_R1_POK       0x03    /* Packet is complete: SOP + EOP */
163
164 #define LE_T1_OWN       0x80    /* Lance owns the packet */
165 #define LE_T1_ERR       0x40    /* Error summary */
166 #define LE_T1_EMORE     0x10    /* Error: more than one retry needed */
167 #define LE_T1_EONE      0x08    /* Error: one retry needed */
168 #define LE_T1_EDEF      0x04    /* Error: deferred */
169 #define LE_T1_SOP       0x02    /* Start of packet */
170 #define LE_T1_EOP       0x01    /* End of packet */
171 #define LE_T1_POK       0x03    /* Packet is complete: SOP + EOP */
172
173 #define LE_T3_BUF       0x8000  /* Buffer error */
174 #define LE_T3_UFL       0x4000  /* Error underflow */
175 #define LE_T3_LCOL      0x1000  /* Error late collision */
176 #define LE_T3_CLOS      0x0800  /* Error carrier loss */
177 #define LE_T3_RTY       0x0400  /* Error retry */
178 #define LE_T3_TDR       0x03ff  /* Time Domain Reflectometry counter */
179
180 #define TX_RING_SIZE                    (1 << (LANCE_LOG_TX_BUFFERS))
181 #define TX_RING_MOD_MASK                (TX_RING_SIZE - 1)
182 #define TX_RING_LEN_BITS                ((LANCE_LOG_TX_BUFFERS) << 29)
183 #define TX_NEXT(__x)                    (((__x)+1) & TX_RING_MOD_MASK)
184
185 #define RX_RING_SIZE                    (1 << (LANCE_LOG_RX_BUFFERS))
186 #define RX_RING_MOD_MASK                (RX_RING_SIZE - 1)
187 #define RX_RING_LEN_BITS                ((LANCE_LOG_RX_BUFFERS) << 29)
188 #define RX_NEXT(__x)                    (((__x)+1) & RX_RING_MOD_MASK)
189
190 #define PKT_BUF_SZ              1544
191 #define RX_BUFF_SIZE            PKT_BUF_SZ
192 #define TX_BUFF_SIZE            PKT_BUF_SZ
193
194 struct lance_rx_desc {
195         u16     rmd0;           /* low address of packet */
196         u8      rmd1_bits;      /* descriptor bits */
197         u8      rmd1_hadr;      /* high address of packet */
198         s16     length;         /* This length is 2s complement (negative)!
199                                  * Buffer length
200                                  */
201         u16     mblength;       /* This is the actual number of bytes received */
202 };
203
204 struct lance_tx_desc {
205         u16     tmd0;           /* low address of packet */
206         u8      tmd1_bits;      /* descriptor bits */
207         u8      tmd1_hadr;      /* high address of packet */
208         s16     length;         /* Length is 2s complement (negative)! */
209         u16     misc;
210 };
211
212 /* The LANCE initialization block, described in databook. */
213 /* On the Sparc, this block should be on a DMA region     */
214 struct lance_init_block {
215         u16     mode;           /* Pre-set mode (reg. 15) */
216         u8      phys_addr[6];   /* Physical ethernet address */
217         u32     filter[2];      /* Multicast filter. */
218
219         /* Receive and transmit ring base, along with extra bits. */
220         u16     rx_ptr;         /* receive descriptor addr */
221         u16     rx_len;         /* receive len and high addr */
222         u16     tx_ptr;         /* transmit descriptor addr */
223         u16     tx_len;         /* transmit len and high addr */
224
225         /* The Tx and Rx ring entries must aligned on 8-byte boundaries. */
226         struct lance_rx_desc brx_ring[RX_RING_SIZE];
227         struct lance_tx_desc btx_ring[TX_RING_SIZE];
228
229         u8      tx_buf [TX_RING_SIZE][TX_BUFF_SIZE];
230         u8      pad[2];         /* align rx_buf for copy_and_sum(). */
231         u8      rx_buf [RX_RING_SIZE][RX_BUFF_SIZE];
232 };
233
234 #define libdesc_offset(rt, elem) \
235 ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
236
237 #define libbuff_offset(rt, elem) \
238 ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem][0])))))
239
240 struct lance_private {
241         void __iomem    *lregs;         /* Lance RAP/RDP regs.          */
242         void __iomem    *dregs;         /* DMA controller regs.         */
243         struct lance_init_block __iomem *init_block_iomem;
244         struct lance_init_block *init_block_mem;
245
246         spinlock_t      lock;
247
248         int             rx_new, tx_new;
249         int             rx_old, tx_old;
250
251         struct sbus_dma *ledma; /* If set this points to ledma  */
252         char            tpe;            /* cable-selection is TPE       */
253         char            auto_select;    /* cable-selection by carrier   */
254         char            burst_sizes;    /* ledma SBus burst sizes       */
255         char            pio_buffer;     /* init block in PIO space?     */
256
257         unsigned short  busmaster_regval;
258
259         void (*init_ring)(struct net_device *);
260         void (*rx)(struct net_device *);
261         void (*tx)(struct net_device *);
262
263         char                   *name;
264         dma_addr_t              init_block_dvma;
265         struct net_device      *dev;              /* Backpointer        */
266         struct sbus_dev        *sdev;
267         struct timer_list       multicast_timer;
268 };
269
270 #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
271                         lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
272                         lp->tx_old - lp->tx_new-1)
273
274 /* Lance registers. */
275 #define RDP             0x00UL          /* register data port           */
276 #define RAP             0x02UL          /* register address port        */
277 #define LANCE_REG_SIZE  0x04UL
278
279 #define STOP_LANCE(__lp) \
280 do {    void __iomem *__base = (__lp)->lregs; \
281         sbus_writew(LE_CSR0,    __base + RAP); \
282         sbus_writew(LE_C0_STOP, __base + RDP); \
283 } while (0)
284
285 int sparc_lance_debug = 2;
286
287 /* The Lance uses 24 bit addresses */
288 /* On the Sun4c the DVMA will provide the remaining bytes for us */
289 /* On the Sun4m we have to instruct the ledma to provide them    */
290 /* Even worse, on scsi/ether SBUS cards, the init block and the
291  * transmit/receive buffers are addresses as offsets from absolute
292  * zero on the lebuffer PIO area. -DaveM
293  */
294
295 #define LANCE_ADDR(x) ((long)(x) & ~0xff000000)
296
297 /* Load the CSR registers */
298 static void load_csrs(struct lance_private *lp)
299 {
300         u32 leptr;
301
302         if (lp->pio_buffer)
303                 leptr = 0;
304         else
305                 leptr = LANCE_ADDR(lp->init_block_dvma);
306
307         sbus_writew(LE_CSR1,              lp->lregs + RAP);
308         sbus_writew(leptr & 0xffff,       lp->lregs + RDP);
309         sbus_writew(LE_CSR2,              lp->lregs + RAP);
310         sbus_writew(leptr >> 16,          lp->lregs + RDP);
311         sbus_writew(LE_CSR3,              lp->lregs + RAP);
312         sbus_writew(lp->busmaster_regval, lp->lregs + RDP);
313
314         /* Point back to csr0 */
315         sbus_writew(LE_CSR0, lp->lregs + RAP);
316 }
317
318 /* Setup the Lance Rx and Tx rings */
319 static void lance_init_ring_dvma(struct net_device *dev)
320 {
321         struct lance_private *lp = netdev_priv(dev);
322         struct lance_init_block *ib = lp->init_block_mem;
323         dma_addr_t aib = lp->init_block_dvma;
324         __u32 leptr;
325         int i;
326
327         /* Lock out other processes while setting up hardware */
328         netif_stop_queue(dev);
329         lp->rx_new = lp->tx_new = 0;
330         lp->rx_old = lp->tx_old = 0;
331
332         /* Copy the ethernet address to the lance init block
333          * Note that on the sparc you need to swap the ethernet address.
334          */
335         ib->phys_addr [0] = dev->dev_addr [1];
336         ib->phys_addr [1] = dev->dev_addr [0];
337         ib->phys_addr [2] = dev->dev_addr [3];
338         ib->phys_addr [3] = dev->dev_addr [2];
339         ib->phys_addr [4] = dev->dev_addr [5];
340         ib->phys_addr [5] = dev->dev_addr [4];
341
342         /* Setup the Tx ring entries */
343         for (i = 0; i <= TX_RING_SIZE; i++) {
344                 leptr = LANCE_ADDR(aib + libbuff_offset(tx_buf, i));
345                 ib->btx_ring [i].tmd0      = leptr;
346                 ib->btx_ring [i].tmd1_hadr = leptr >> 16;
347                 ib->btx_ring [i].tmd1_bits = 0;
348                 ib->btx_ring [i].length    = 0xf000; /* The ones required by tmd2 */
349                 ib->btx_ring [i].misc      = 0;
350         }
351
352         /* Setup the Rx ring entries */
353         for (i = 0; i < RX_RING_SIZE; i++) {
354                 leptr = LANCE_ADDR(aib + libbuff_offset(rx_buf, i));
355
356                 ib->brx_ring [i].rmd0      = leptr;
357                 ib->brx_ring [i].rmd1_hadr = leptr >> 16;
358                 ib->brx_ring [i].rmd1_bits = LE_R1_OWN;
359                 ib->brx_ring [i].length    = -RX_BUFF_SIZE | 0xf000;
360                 ib->brx_ring [i].mblength  = 0;
361         }
362
363         /* Setup the initialization block */
364
365         /* Setup rx descriptor pointer */
366         leptr = LANCE_ADDR(aib + libdesc_offset(brx_ring, 0));
367         ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
368         ib->rx_ptr = leptr;
369
370         /* Setup tx descriptor pointer */
371         leptr = LANCE_ADDR(aib + libdesc_offset(btx_ring, 0));
372         ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
373         ib->tx_ptr = leptr;
374 }
375
376 static void lance_init_ring_pio(struct net_device *dev)
377 {
378         struct lance_private *lp = netdev_priv(dev);
379         struct lance_init_block __iomem *ib = lp->init_block_iomem;
380         u32 leptr;
381         int i;
382
383         /* Lock out other processes while setting up hardware */
384         netif_stop_queue(dev);
385         lp->rx_new = lp->tx_new = 0;
386         lp->rx_old = lp->tx_old = 0;
387
388         /* Copy the ethernet address to the lance init block
389          * Note that on the sparc you need to swap the ethernet address.
390          */
391         sbus_writeb(dev->dev_addr[1], &ib->phys_addr[0]);
392         sbus_writeb(dev->dev_addr[0], &ib->phys_addr[1]);
393         sbus_writeb(dev->dev_addr[3], &ib->phys_addr[2]);
394         sbus_writeb(dev->dev_addr[2], &ib->phys_addr[3]);
395         sbus_writeb(dev->dev_addr[5], &ib->phys_addr[4]);
396         sbus_writeb(dev->dev_addr[4], &ib->phys_addr[5]);
397
398         /* Setup the Tx ring entries */
399         for (i = 0; i <= TX_RING_SIZE; i++) {
400                 leptr = libbuff_offset(tx_buf, i);
401                 sbus_writew(leptr,      &ib->btx_ring [i].tmd0);
402                 sbus_writeb(leptr >> 16,&ib->btx_ring [i].tmd1_hadr);
403                 sbus_writeb(0,          &ib->btx_ring [i].tmd1_bits);
404
405                 /* The ones required by tmd2 */
406                 sbus_writew(0xf000,     &ib->btx_ring [i].length);
407                 sbus_writew(0,          &ib->btx_ring [i].misc);
408         }
409
410         /* Setup the Rx ring entries */
411         for (i = 0; i < RX_RING_SIZE; i++) {
412                 leptr = libbuff_offset(rx_buf, i);
413
414                 sbus_writew(leptr,      &ib->brx_ring [i].rmd0);
415                 sbus_writeb(leptr >> 16,&ib->brx_ring [i].rmd1_hadr);
416                 sbus_writeb(LE_R1_OWN,  &ib->brx_ring [i].rmd1_bits);
417                 sbus_writew(-RX_BUFF_SIZE|0xf000,
418                             &ib->brx_ring [i].length);
419                 sbus_writew(0,          &ib->brx_ring [i].mblength);
420         }
421
422         /* Setup the initialization block */
423
424         /* Setup rx descriptor pointer */
425         leptr = libdesc_offset(brx_ring, 0);
426         sbus_writew((LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16),
427                     &ib->rx_len);
428         sbus_writew(leptr, &ib->rx_ptr);
429
430         /* Setup tx descriptor pointer */
431         leptr = libdesc_offset(btx_ring, 0);
432         sbus_writew((LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16),
433                     &ib->tx_len);
434         sbus_writew(leptr, &ib->tx_ptr);
435 }
436
437 static void init_restart_ledma(struct lance_private *lp)
438 {
439         u32 csr = sbus_readl(lp->dregs + DMA_CSR);
440
441         if (!(csr & DMA_HNDL_ERROR)) {
442                 /* E-Cache draining */
443                 while (sbus_readl(lp->dregs + DMA_CSR) & DMA_FIFO_ISDRAIN)
444                         barrier();
445         }
446
447         csr = sbus_readl(lp->dregs + DMA_CSR);
448         csr &= ~DMA_E_BURSTS;
449         if (lp->burst_sizes & DMA_BURST32)
450                 csr |= DMA_E_BURST32;
451         else
452                 csr |= DMA_E_BURST16;
453
454         csr |= (DMA_DSBL_RD_DRN | DMA_DSBL_WR_INV | DMA_FIFO_INV);
455
456         if (lp->tpe)
457                 csr |= DMA_EN_ENETAUI;
458         else
459                 csr &= ~DMA_EN_ENETAUI;
460         udelay(20);
461         sbus_writel(csr, lp->dregs + DMA_CSR);
462         udelay(200);
463 }
464
465 static int init_restart_lance(struct lance_private *lp)
466 {
467         u16 regval = 0;
468         int i;
469
470         if (lp->dregs)
471                 init_restart_ledma(lp);
472
473         sbus_writew(LE_CSR0,    lp->lregs + RAP);
474         sbus_writew(LE_C0_INIT, lp->lregs + RDP);
475
476         /* Wait for the lance to complete initialization */
477         for (i = 0; i < 100; i++) {
478                 regval = sbus_readw(lp->lregs + RDP);
479
480                 if (regval & (LE_C0_ERR | LE_C0_IDON))
481                         break;
482                 barrier();
483         }
484         if (i == 100 || (regval & LE_C0_ERR)) {
485                 printk(KERN_ERR "LANCE unopened after %d ticks, csr0=%4.4x.\n",
486                        i, regval);
487                 if (lp->dregs)
488                         printk("dcsr=%8.8x\n", sbus_readl(lp->dregs + DMA_CSR));
489                 return -1;
490         }
491
492         /* Clear IDON by writing a "1", enable interrupts and start lance */
493         sbus_writew(LE_C0_IDON,                 lp->lregs + RDP);
494         sbus_writew(LE_C0_INEA | LE_C0_STRT,    lp->lregs + RDP);
495
496         if (lp->dregs) {
497                 u32 csr = sbus_readl(lp->dregs + DMA_CSR);
498
499                 csr |= DMA_INT_ENAB;
500                 sbus_writel(csr, lp->dregs + DMA_CSR);
501         }
502
503         return 0;
504 }
505
506 static void lance_rx_dvma(struct net_device *dev)
507 {
508         struct lance_private *lp = netdev_priv(dev);
509         struct lance_init_block *ib = lp->init_block_mem;
510         struct lance_rx_desc *rd;
511         u8 bits;
512         int len, entry = lp->rx_new;
513         struct sk_buff *skb;
514
515         for (rd = &ib->brx_ring [entry];
516              !((bits = rd->rmd1_bits) & LE_R1_OWN);
517              rd = &ib->brx_ring [entry]) {
518
519                 /* We got an incomplete frame? */
520                 if ((bits & LE_R1_POK) != LE_R1_POK) {
521                         dev->stats.rx_over_errors++;
522                         dev->stats.rx_errors++;
523                 } else if (bits & LE_R1_ERR) {
524                         /* Count only the end frame as a rx error,
525                          * not the beginning
526                          */
527                         if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
528                         if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
529                         if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
530                         if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
531                         if (bits & LE_R1_EOP) dev->stats.rx_errors++;
532                 } else {
533                         len = (rd->mblength & 0xfff) - 4;
534                         skb = dev_alloc_skb(len + 2);
535
536                         if (skb == NULL) {
537                                 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
538                                        dev->name);
539                                 dev->stats.rx_dropped++;
540                                 rd->mblength = 0;
541                                 rd->rmd1_bits = LE_R1_OWN;
542                                 lp->rx_new = RX_NEXT(entry);
543                                 return;
544                         }
545
546                         dev->stats.rx_bytes += len;
547
548                         skb_reserve(skb, 2);            /* 16 byte align */
549                         skb_put(skb, len);              /* make room */
550                         skb_copy_to_linear_data(skb,
551                                          (unsigned char *)&(ib->rx_buf [entry][0]),
552                                          len);
553                         skb->protocol = eth_type_trans(skb, dev);
554                         netif_rx(skb);
555                         dev->last_rx = jiffies;
556                         dev->stats.rx_packets++;
557                 }
558
559                 /* Return the packet to the pool */
560                 rd->mblength = 0;
561                 rd->rmd1_bits = LE_R1_OWN;
562                 entry = RX_NEXT(entry);
563         }
564
565         lp->rx_new = entry;
566 }
567
568 static void lance_tx_dvma(struct net_device *dev)
569 {
570         struct lance_private *lp = netdev_priv(dev);
571         struct lance_init_block *ib = lp->init_block_mem;
572         int i, j;
573
574         spin_lock(&lp->lock);
575
576         j = lp->tx_old;
577         for (i = j; i != lp->tx_new; i = j) {
578                 struct lance_tx_desc *td = &ib->btx_ring [i];
579                 u8 bits = td->tmd1_bits;
580
581                 /* If we hit a packet not owned by us, stop */
582                 if (bits & LE_T1_OWN)
583                         break;
584
585                 if (bits & LE_T1_ERR) {
586                         u16 status = td->misc;
587
588                         dev->stats.tx_errors++;
589                         if (status & LE_T3_RTY)  dev->stats.tx_aborted_errors++;
590                         if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
591
592                         if (status & LE_T3_CLOS) {
593                                 dev->stats.tx_carrier_errors++;
594                                 if (lp->auto_select) {
595                                         lp->tpe = 1 - lp->tpe;
596                                         printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
597                                                dev->name, lp->tpe?"TPE":"AUI");
598                                         STOP_LANCE(lp);
599                                         lp->init_ring(dev);
600                                         load_csrs(lp);
601                                         init_restart_lance(lp);
602                                         goto out;
603                                 }
604                         }
605
606                         /* Buffer errors and underflows turn off the
607                          * transmitter, restart the adapter.
608                          */
609                         if (status & (LE_T3_BUF|LE_T3_UFL)) {
610                                 dev->stats.tx_fifo_errors++;
611
612                                 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
613                                        dev->name);
614                                 STOP_LANCE(lp);
615                                 lp->init_ring(dev);
616                                 load_csrs(lp);
617                                 init_restart_lance(lp);
618                                 goto out;
619                         }
620                 } else if ((bits & LE_T1_POK) == LE_T1_POK) {
621                         /*
622                          * So we don't count the packet more than once.
623                          */
624                         td->tmd1_bits = bits & ~(LE_T1_POK);
625
626                         /* One collision before packet was sent. */
627                         if (bits & LE_T1_EONE)
628                                 dev->stats.collisions++;
629
630                         /* More than one collision, be optimistic. */
631                         if (bits & LE_T1_EMORE)
632                                 dev->stats.collisions += 2;
633
634                         dev->stats.tx_packets++;
635                 }
636
637                 j = TX_NEXT(j);
638         }
639         lp->tx_old = j;
640 out:
641         if (netif_queue_stopped(dev) &&
642             TX_BUFFS_AVAIL > 0)
643                 netif_wake_queue(dev);
644
645         spin_unlock(&lp->lock);
646 }
647
648 static void lance_piocopy_to_skb(struct sk_buff *skb, void __iomem *piobuf, int len)
649 {
650         u16 *p16 = (u16 *) skb->data;
651         u32 *p32;
652         u8 *p8;
653         void __iomem *pbuf = piobuf;
654
655         /* We know here that both src and dest are on a 16bit boundary. */
656         *p16++ = sbus_readw(pbuf);
657         p32 = (u32 *) p16;
658         pbuf += 2;
659         len -= 2;
660
661         while (len >= 4) {
662                 *p32++ = sbus_readl(pbuf);
663                 pbuf += 4;
664                 len -= 4;
665         }
666         p8 = (u8 *) p32;
667         if (len >= 2) {
668                 p16 = (u16 *) p32;
669                 *p16++ = sbus_readw(pbuf);
670                 pbuf += 2;
671                 len -= 2;
672                 p8 = (u8 *) p16;
673         }
674         if (len >= 1)
675                 *p8 = sbus_readb(pbuf);
676 }
677
678 static void lance_rx_pio(struct net_device *dev)
679 {
680         struct lance_private *lp = netdev_priv(dev);
681         struct lance_init_block __iomem *ib = lp->init_block_iomem;
682         struct lance_rx_desc __iomem *rd;
683         unsigned char bits;
684         int len, entry;
685         struct sk_buff *skb;
686
687         entry = lp->rx_new;
688         for (rd = &ib->brx_ring [entry];
689              !((bits = sbus_readb(&rd->rmd1_bits)) & LE_R1_OWN);
690              rd = &ib->brx_ring [entry]) {
691
692                 /* We got an incomplete frame? */
693                 if ((bits & LE_R1_POK) != LE_R1_POK) {
694                         dev->stats.rx_over_errors++;
695                         dev->stats.rx_errors++;
696                 } else if (bits & LE_R1_ERR) {
697                         /* Count only the end frame as a rx error,
698                          * not the beginning
699                          */
700                         if (bits & LE_R1_BUF) dev->stats.rx_fifo_errors++;
701                         if (bits & LE_R1_CRC) dev->stats.rx_crc_errors++;
702                         if (bits & LE_R1_OFL) dev->stats.rx_over_errors++;
703                         if (bits & LE_R1_FRA) dev->stats.rx_frame_errors++;
704                         if (bits & LE_R1_EOP) dev->stats.rx_errors++;
705                 } else {
706                         len = (sbus_readw(&rd->mblength) & 0xfff) - 4;
707                         skb = dev_alloc_skb(len + 2);
708
709                         if (skb == NULL) {
710                                 printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
711                                        dev->name);
712                                 dev->stats.rx_dropped++;
713                                 sbus_writew(0, &rd->mblength);
714                                 sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
715                                 lp->rx_new = RX_NEXT(entry);
716                                 return;
717                         }
718
719                         dev->stats.rx_bytes += len;
720
721                         skb_reserve (skb, 2);           /* 16 byte align */
722                         skb_put(skb, len);              /* make room */
723                         lance_piocopy_to_skb(skb, &(ib->rx_buf[entry][0]), len);
724                         skb->protocol = eth_type_trans(skb, dev);
725                         netif_rx(skb);
726                         dev->last_rx = jiffies;
727                         dev->stats.rx_packets++;
728                 }
729
730                 /* Return the packet to the pool */
731                 sbus_writew(0, &rd->mblength);
732                 sbus_writeb(LE_R1_OWN, &rd->rmd1_bits);
733                 entry = RX_NEXT(entry);
734         }
735
736         lp->rx_new = entry;
737 }
738
739 static void lance_tx_pio(struct net_device *dev)
740 {
741         struct lance_private *lp = netdev_priv(dev);
742         struct lance_init_block __iomem *ib = lp->init_block_iomem;
743         int i, j;
744
745         spin_lock(&lp->lock);
746
747         j = lp->tx_old;
748         for (i = j; i != lp->tx_new; i = j) {
749                 struct lance_tx_desc __iomem *td = &ib->btx_ring [i];
750                 u8 bits = sbus_readb(&td->tmd1_bits);
751
752                 /* If we hit a packet not owned by us, stop */
753                 if (bits & LE_T1_OWN)
754                         break;
755
756                 if (bits & LE_T1_ERR) {
757                         u16 status = sbus_readw(&td->misc);
758
759                         dev->stats.tx_errors++;
760                         if (status & LE_T3_RTY)  dev->stats.tx_aborted_errors++;
761                         if (status & LE_T3_LCOL) dev->stats.tx_window_errors++;
762
763                         if (status & LE_T3_CLOS) {
764                                 dev->stats.tx_carrier_errors++;
765                                 if (lp->auto_select) {
766                                         lp->tpe = 1 - lp->tpe;
767                                         printk(KERN_NOTICE "%s: Carrier Lost, trying %s\n",
768                                                dev->name, lp->tpe?"TPE":"AUI");
769                                         STOP_LANCE(lp);
770                                         lp->init_ring(dev);
771                                         load_csrs(lp);
772                                         init_restart_lance(lp);
773                                         goto out;
774                                 }
775                         }
776
777                         /* Buffer errors and underflows turn off the
778                          * transmitter, restart the adapter.
779                          */
780                         if (status & (LE_T3_BUF|LE_T3_UFL)) {
781                                 dev->stats.tx_fifo_errors++;
782
783                                 printk(KERN_ERR "%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
784                                        dev->name);
785                                 STOP_LANCE(lp);
786                                 lp->init_ring(dev);
787                                 load_csrs(lp);
788                                 init_restart_lance(lp);
789                                 goto out;
790                         }
791                 } else if ((bits & LE_T1_POK) == LE_T1_POK) {
792                         /*
793                          * So we don't count the packet more than once.
794                          */
795                         sbus_writeb(bits & ~(LE_T1_POK), &td->tmd1_bits);
796
797                         /* One collision before packet was sent. */
798                         if (bits & LE_T1_EONE)
799                                 dev->stats.collisions++;
800
801                         /* More than one collision, be optimistic. */
802                         if (bits & LE_T1_EMORE)
803                                 dev->stats.collisions += 2;
804
805                         dev->stats.tx_packets++;
806                 }
807
808                 j = TX_NEXT(j);
809         }
810         lp->tx_old = j;
811
812         if (netif_queue_stopped(dev) &&
813             TX_BUFFS_AVAIL > 0)
814                 netif_wake_queue(dev);
815 out:
816         spin_unlock(&lp->lock);
817 }
818
819 static irqreturn_t lance_interrupt(int irq, void *dev_id)
820 {
821         struct net_device *dev = dev_id;
822         struct lance_private *lp = netdev_priv(dev);
823         int csr0;
824
825         sbus_writew(LE_CSR0, lp->lregs + RAP);
826         csr0 = sbus_readw(lp->lregs + RDP);
827
828         /* Acknowledge all the interrupt sources ASAP */
829         sbus_writew(csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT),
830                     lp->lregs + RDP);
831
832         if ((csr0 & LE_C0_ERR) != 0) {
833                 /* Clear the error condition */
834                 sbus_writew((LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
835                              LE_C0_CERR | LE_C0_MERR),
836                             lp->lregs + RDP);
837         }
838
839         if (csr0 & LE_C0_RINT)
840                 lp->rx(dev);
841
842         if (csr0 & LE_C0_TINT)
843                 lp->tx(dev);
844
845         if (csr0 & LE_C0_BABL)
846                 dev->stats.tx_errors++;
847
848         if (csr0 & LE_C0_MISS)
849                 dev->stats.rx_errors++;
850
851         if (csr0 & LE_C0_MERR) {
852                 if (lp->dregs) {
853                         u32 addr = sbus_readl(lp->dregs + DMA_ADDR);
854
855                         printk(KERN_ERR "%s: Memory error, status %04x, addr %06x\n",
856                                dev->name, csr0, addr & 0xffffff);
857                 } else {
858                         printk(KERN_ERR "%s: Memory error, status %04x\n",
859                                dev->name, csr0);
860                 }
861
862                 sbus_writew(LE_C0_STOP, lp->lregs + RDP);
863
864                 if (lp->dregs) {
865                         u32 dma_csr = sbus_readl(lp->dregs + DMA_CSR);
866
867                         dma_csr |= DMA_FIFO_INV;
868                         sbus_writel(dma_csr, lp->dregs + DMA_CSR);
869                 }
870
871                 lp->init_ring(dev);
872                 load_csrs(lp);
873                 init_restart_lance(lp);
874                 netif_wake_queue(dev);
875         }
876
877         sbus_writew(LE_C0_INEA, lp->lregs + RDP);
878
879         return IRQ_HANDLED;
880 }
881
882 /* Build a fake network packet and send it to ourselves. */
883 static void build_fake_packet(struct lance_private *lp)
884 {
885         struct net_device *dev = lp->dev;
886         int i, entry;
887
888         entry = lp->tx_new & TX_RING_MOD_MASK;
889         if (lp->pio_buffer) {
890                 struct lance_init_block __iomem *ib = lp->init_block_iomem;
891                 u16 __iomem *packet = (u16 __iomem *) &(ib->tx_buf[entry][0]);
892                 struct ethhdr __iomem *eth = (struct ethhdr __iomem *) packet;
893                 for (i = 0; i < (ETH_ZLEN / sizeof(u16)); i++)
894                         sbus_writew(0, &packet[i]);
895                 for (i = 0; i < 6; i++) {
896                         sbus_writeb(dev->dev_addr[i], &eth->h_dest[i]);
897                         sbus_writeb(dev->dev_addr[i], &eth->h_source[i]);
898                 }
899                 sbus_writew((-ETH_ZLEN) | 0xf000, &ib->btx_ring[entry].length);
900                 sbus_writew(0, &ib->btx_ring[entry].misc);
901                 sbus_writeb(LE_T1_POK|LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
902         } else {
903                 struct lance_init_block *ib = lp->init_block_mem;
904                 u16 *packet = (u16 *) &(ib->tx_buf[entry][0]);
905                 struct ethhdr *eth = (struct ethhdr *) packet;
906                 memset(packet, 0, ETH_ZLEN);
907                 for (i = 0; i < 6; i++) {
908                         eth->h_dest[i] = dev->dev_addr[i];
909                         eth->h_source[i] = dev->dev_addr[i];
910                 }
911                 ib->btx_ring[entry].length = (-ETH_ZLEN) | 0xf000;
912                 ib->btx_ring[entry].misc = 0;
913                 ib->btx_ring[entry].tmd1_bits = (LE_T1_POK|LE_T1_OWN);
914         }
915         lp->tx_new = TX_NEXT(entry);
916 }
917
918 static int lance_open(struct net_device *dev)
919 {
920         struct lance_private *lp = netdev_priv(dev);
921         int status = 0;
922
923         STOP_LANCE(lp);
924
925         if (request_irq(dev->irq, &lance_interrupt, IRQF_SHARED,
926                         lancestr, (void *) dev)) {
927                 printk(KERN_ERR "Lance: Can't get irq %d\n", dev->irq);
928                 return -EAGAIN;
929         }
930
931         /* On the 4m, setup the ledma to provide the upper bits for buffers */
932         if (lp->dregs) {
933                 u32 regval = lp->init_block_dvma & 0xff000000;
934
935                 sbus_writel(regval, lp->dregs + DMA_TEST);
936         }
937
938         /* Set mode and clear multicast filter only at device open,
939          * so that lance_init_ring() called at any error will not
940          * forget multicast filters.
941          *
942          * BTW it is common bug in all lance drivers! --ANK
943          */
944         if (lp->pio_buffer) {
945                 struct lance_init_block __iomem *ib = lp->init_block_iomem;
946                 sbus_writew(0, &ib->mode);
947                 sbus_writel(0, &ib->filter[0]);
948                 sbus_writel(0, &ib->filter[1]);
949         } else {
950                 struct lance_init_block *ib = lp->init_block_mem;
951                 ib->mode = 0;
952                 ib->filter [0] = 0;
953                 ib->filter [1] = 0;
954         }
955
956         lp->init_ring(dev);
957         load_csrs(lp);
958
959         netif_start_queue(dev);
960
961         status = init_restart_lance(lp);
962         if (!status && lp->auto_select) {
963                 build_fake_packet(lp);
964                 sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
965         }
966
967         return status;
968 }
969
970 static int lance_close(struct net_device *dev)
971 {
972         struct lance_private *lp = netdev_priv(dev);
973
974         netif_stop_queue(dev);
975         del_timer_sync(&lp->multicast_timer);
976
977         STOP_LANCE(lp);
978
979         free_irq(dev->irq, (void *) dev);
980         return 0;
981 }
982
983 static int lance_reset(struct net_device *dev)
984 {
985         struct lance_private *lp = netdev_priv(dev);
986         int status;
987
988         STOP_LANCE(lp);
989
990         /* On the 4m, reset the dma too */
991         if (lp->dregs) {
992                 u32 csr, addr;
993
994                 printk(KERN_ERR "resetting ledma\n");
995                 csr = sbus_readl(lp->dregs + DMA_CSR);
996                 sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
997                 udelay(200);
998                 sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
999
1000                 addr = lp->init_block_dvma & 0xff000000;
1001                 sbus_writel(addr, lp->dregs + DMA_TEST);
1002         }
1003         lp->init_ring(dev);
1004         load_csrs(lp);
1005         dev->trans_start = jiffies;
1006         status = init_restart_lance(lp);
1007         return status;
1008 }
1009
1010 static void lance_piocopy_from_skb(void __iomem *dest, unsigned char *src, int len)
1011 {
1012         void __iomem *piobuf = dest;
1013         u32 *p32;
1014         u16 *p16;
1015         u8 *p8;
1016
1017         switch ((unsigned long)src & 0x3) {
1018         case 0:
1019                 p32 = (u32 *) src;
1020                 while (len >= 4) {
1021                         sbus_writel(*p32, piobuf);
1022                         p32++;
1023                         piobuf += 4;
1024                         len -= 4;
1025                 }
1026                 src = (char *) p32;
1027                 break;
1028         case 1:
1029         case 3:
1030                 p8 = (u8 *) src;
1031                 while (len >= 4) {
1032                         u32 val;
1033
1034                         val  = p8[0] << 24;
1035                         val |= p8[1] << 16;
1036                         val |= p8[2] << 8;
1037                         val |= p8[3];
1038                         sbus_writel(val, piobuf);
1039                         p8 += 4;
1040                         piobuf += 4;
1041                         len -= 4;
1042                 }
1043                 src = (char *) p8;
1044                 break;
1045         case 2:
1046                 p16 = (u16 *) src;
1047                 while (len >= 4) {
1048                         u32 val = p16[0]<<16 | p16[1];
1049                         sbus_writel(val, piobuf);
1050                         p16 += 2;
1051                         piobuf += 4;
1052                         len -= 4;
1053                 }
1054                 src = (char *) p16;
1055                 break;
1056         };
1057         if (len >= 2) {
1058                 u16 val = src[0] << 8 | src[1];
1059                 sbus_writew(val, piobuf);
1060                 src += 2;
1061                 piobuf += 2;
1062                 len -= 2;
1063         }
1064         if (len >= 1)
1065                 sbus_writeb(src[0], piobuf);
1066 }
1067
1068 static void lance_piozero(void __iomem *dest, int len)
1069 {
1070         void __iomem *piobuf = dest;
1071
1072         if ((unsigned long)piobuf & 1) {
1073                 sbus_writeb(0, piobuf);
1074                 piobuf += 1;
1075                 len -= 1;
1076                 if (len == 0)
1077                         return;
1078         }
1079         if (len == 1) {
1080                 sbus_writeb(0, piobuf);
1081                 return;
1082         }
1083         if ((unsigned long)piobuf & 2) {
1084                 sbus_writew(0, piobuf);
1085                 piobuf += 2;
1086                 len -= 2;
1087                 if (len == 0)
1088                         return;
1089         }
1090         while (len >= 4) {
1091                 sbus_writel(0, piobuf);
1092                 piobuf += 4;
1093                 len -= 4;
1094         }
1095         if (len >= 2) {
1096                 sbus_writew(0, piobuf);
1097                 piobuf += 2;
1098                 len -= 2;
1099         }
1100         if (len >= 1)
1101                 sbus_writeb(0, piobuf);
1102 }
1103
1104 static void lance_tx_timeout(struct net_device *dev)
1105 {
1106         struct lance_private *lp = netdev_priv(dev);
1107
1108         printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
1109                dev->name, sbus_readw(lp->lregs + RDP));
1110         lance_reset(dev);
1111         netif_wake_queue(dev);
1112 }
1113
1114 static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
1115 {
1116         struct lance_private *lp = netdev_priv(dev);
1117         int entry, skblen, len;
1118
1119         skblen = skb->len;
1120
1121         len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
1122
1123         spin_lock_irq(&lp->lock);
1124
1125         dev->stats.tx_bytes += len;
1126
1127         entry = lp->tx_new & TX_RING_MOD_MASK;
1128         if (lp->pio_buffer) {
1129                 struct lance_init_block __iomem *ib = lp->init_block_iomem;
1130                 sbus_writew((-len) | 0xf000, &ib->btx_ring[entry].length);
1131                 sbus_writew(0, &ib->btx_ring[entry].misc);
1132                 lance_piocopy_from_skb(&ib->tx_buf[entry][0], skb->data, skblen);
1133                 if (len != skblen)
1134                         lance_piozero(&ib->tx_buf[entry][skblen], len - skblen);
1135                 sbus_writeb(LE_T1_POK | LE_T1_OWN, &ib->btx_ring[entry].tmd1_bits);
1136         } else {
1137                 struct lance_init_block *ib = lp->init_block_mem;
1138                 ib->btx_ring [entry].length = (-len) | 0xf000;
1139                 ib->btx_ring [entry].misc = 0;
1140                 skb_copy_from_linear_data(skb, &ib->tx_buf [entry][0], skblen);
1141                 if (len != skblen)
1142                         memset((char *) &ib->tx_buf [entry][skblen], 0, len - skblen);
1143                 ib->btx_ring [entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
1144         }
1145
1146         lp->tx_new = TX_NEXT(entry);
1147
1148         if (TX_BUFFS_AVAIL <= 0)
1149                 netif_stop_queue(dev);
1150
1151         /* Kick the lance: transmit now */
1152         sbus_writew(LE_C0_INEA | LE_C0_TDMD, lp->lregs + RDP);
1153
1154         /* Read back CSR to invalidate the E-Cache.
1155          * This is needed, because DMA_DSBL_WR_INV is set.
1156          */
1157         if (lp->dregs)
1158                 sbus_readw(lp->lregs + RDP);
1159
1160         spin_unlock_irq(&lp->lock);
1161
1162         dev->trans_start = jiffies;
1163         dev_kfree_skb(skb);
1164
1165         return 0;
1166 }
1167
1168 /* taken from the depca driver */
1169 static void lance_load_multicast(struct net_device *dev)
1170 {
1171         struct lance_private *lp = netdev_priv(dev);
1172         struct dev_mc_list *dmi = dev->mc_list;
1173         char *addrs;
1174         int i;
1175         u32 crc;
1176         u32 val;
1177
1178         /* set all multicast bits */
1179         if (dev->flags & IFF_ALLMULTI)
1180                 val = ~0;
1181         else
1182                 val = 0;
1183
1184         if (lp->pio_buffer) {
1185                 struct lance_init_block __iomem *ib = lp->init_block_iomem;
1186                 sbus_writel(val, &ib->filter[0]);
1187                 sbus_writel(val, &ib->filter[1]);
1188         } else {
1189                 struct lance_init_block *ib = lp->init_block_mem;
1190                 ib->filter [0] = val;
1191                 ib->filter [1] = val;
1192         }
1193
1194         if (dev->flags & IFF_ALLMULTI)
1195                 return;
1196
1197         /* Add addresses */
1198         for (i = 0; i < dev->mc_count; i++) {
1199                 addrs = dmi->dmi_addr;
1200                 dmi   = dmi->next;
1201
1202                 /* multicast address? */
1203                 if (!(*addrs & 1))
1204                         continue;
1205                 crc = ether_crc_le(6, addrs);
1206                 crc = crc >> 26;
1207                 if (lp->pio_buffer) {
1208                         struct lance_init_block __iomem *ib = lp->init_block_iomem;
1209                         u16 __iomem *mcast_table = (u16 __iomem *) &ib->filter;
1210                         u16 tmp = sbus_readw(&mcast_table[crc>>4]);
1211                         tmp |= 1 << (crc & 0xf);
1212                         sbus_writew(tmp, &mcast_table[crc>>4]);
1213                 } else {
1214                         struct lance_init_block *ib = lp->init_block_mem;
1215                         u16 *mcast_table = (u16 *) &ib->filter;
1216                         mcast_table [crc >> 4] |= 1 << (crc & 0xf);
1217                 }
1218         }
1219 }
1220
1221 static void lance_set_multicast(struct net_device *dev)
1222 {
1223         struct lance_private *lp = netdev_priv(dev);
1224         struct lance_init_block *ib_mem = lp->init_block_mem;
1225         struct lance_init_block __iomem *ib_iomem = lp->init_block_iomem;
1226         u16 mode;
1227
1228         if (!netif_running(dev))
1229                 return;
1230
1231         if (lp->tx_old != lp->tx_new) {
1232                 mod_timer(&lp->multicast_timer, jiffies + 4);
1233                 netif_wake_queue(dev);
1234                 return;
1235         }
1236
1237         netif_stop_queue(dev);
1238
1239         STOP_LANCE(lp);
1240         lp->init_ring(dev);
1241
1242         if (lp->pio_buffer)
1243                 mode = sbus_readw(&ib_iomem->mode);
1244         else
1245                 mode = ib_mem->mode;
1246         if (dev->flags & IFF_PROMISC) {
1247                 mode |= LE_MO_PROM;
1248                 if (lp->pio_buffer)
1249                         sbus_writew(mode, &ib_iomem->mode);
1250                 else
1251                         ib_mem->mode = mode;
1252         } else {
1253                 mode &= ~LE_MO_PROM;
1254                 if (lp->pio_buffer)
1255                         sbus_writew(mode, &ib_iomem->mode);
1256                 else
1257                         ib_mem->mode = mode;
1258                 lance_load_multicast(dev);
1259         }
1260         load_csrs(lp);
1261         init_restart_lance(lp);
1262         netif_wake_queue(dev);
1263 }
1264
1265 static void lance_set_multicast_retry(unsigned long _opaque)
1266 {
1267         struct net_device *dev = (struct net_device *) _opaque;
1268
1269         lance_set_multicast(dev);
1270 }
1271
1272 static void lance_free_hwresources(struct lance_private *lp)
1273 {
1274         if (lp->lregs)
1275                 sbus_iounmap(lp->lregs, LANCE_REG_SIZE);
1276         if (lp->init_block_iomem) {
1277                 sbus_iounmap(lp->init_block_iomem,
1278                              sizeof(struct lance_init_block));
1279         } else if (lp->init_block_mem) {
1280                 sbus_free_consistent(lp->sdev,
1281                                      sizeof(struct lance_init_block),
1282                                      lp->init_block_mem,
1283                                      lp->init_block_dvma);
1284         }
1285 }
1286
1287 /* Ethtool support... */
1288 static void sparc_lance_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1289 {
1290         struct lance_private *lp = netdev_priv(dev);
1291
1292         strcpy(info->driver, "sunlance");
1293         strcpy(info->version, "2.02");
1294         sprintf(info->bus_info, "SBUS:%d",
1295                 lp->sdev->slot);
1296 }
1297
1298 static u32 sparc_lance_get_link(struct net_device *dev)
1299 {
1300         /* We really do not keep track of this, but this
1301          * is better than not reporting anything at all.
1302          */
1303         return 1;
1304 }
1305
1306 static const struct ethtool_ops sparc_lance_ethtool_ops = {
1307         .get_drvinfo            = sparc_lance_get_drvinfo,
1308         .get_link               = sparc_lance_get_link,
1309 };
1310
1311 static int __devinit sparc_lance_probe_one(struct sbus_dev *sdev,
1312                                            struct sbus_dma *ledma,
1313                                            struct sbus_dev *lebuffer)
1314 {
1315         static unsigned version_printed;
1316         struct device_node *dp = sdev->ofdev.node;
1317         struct net_device *dev;
1318         struct lance_private *lp;
1319         int    i;
1320         DECLARE_MAC_BUF(mac);
1321
1322         dev = alloc_etherdev(sizeof(struct lance_private) + 8);
1323         if (!dev)
1324                 return -ENOMEM;
1325
1326         lp = netdev_priv(dev);
1327
1328         if (sparc_lance_debug && version_printed++ == 0)
1329                 printk (KERN_INFO "%s", version);
1330
1331         spin_lock_init(&lp->lock);
1332
1333         /* Copy the IDPROM ethernet address to the device structure, later we
1334          * will copy the address in the device structure to the lance
1335          * initialization block.
1336          */
1337         for (i = 0; i < 6; i++)
1338                 dev->dev_addr[i] = idprom->id_ethaddr[i];
1339
1340         /* Get the IO region */
1341         lp->lregs = sbus_ioremap(&sdev->resource[0], 0,
1342                                  LANCE_REG_SIZE, lancestr);
1343         if (!lp->lregs) {
1344                 printk(KERN_ERR "SunLance: Cannot map registers.\n");
1345                 goto fail;
1346         }
1347
1348         lp->sdev = sdev;
1349         if (lebuffer) {
1350                 /* sanity check */
1351                 if (lebuffer->resource[0].start & 7) {
1352                         printk(KERN_ERR "SunLance: ERROR: Rx and Tx rings not on even boundary.\n");
1353                         goto fail;
1354                 }
1355                 lp->init_block_iomem =
1356                         sbus_ioremap(&lebuffer->resource[0], 0,
1357                                      sizeof(struct lance_init_block), "lebuffer");
1358                 if (!lp->init_block_iomem) {
1359                         printk(KERN_ERR "SunLance: Cannot map PIO buffer.\n");
1360                         goto fail;
1361                 }
1362                 lp->init_block_dvma = 0;
1363                 lp->pio_buffer = 1;
1364                 lp->init_ring = lance_init_ring_pio;
1365                 lp->rx = lance_rx_pio;
1366                 lp->tx = lance_tx_pio;
1367         } else {
1368                 lp->init_block_mem =
1369                         sbus_alloc_consistent(sdev, sizeof(struct lance_init_block),
1370                                               &lp->init_block_dvma);
1371                 if (!lp->init_block_mem || lp->init_block_dvma == 0) {
1372                         printk(KERN_ERR "SunLance: Cannot allocate consistent DMA memory.\n");
1373                         goto fail;
1374                 }
1375                 lp->pio_buffer = 0;
1376                 lp->init_ring = lance_init_ring_dvma;
1377                 lp->rx = lance_rx_dvma;
1378                 lp->tx = lance_tx_dvma;
1379         }
1380         lp->busmaster_regval = of_getintprop_default(dp,  "busmaster-regval",
1381                                                      (LE_C3_BSWP |
1382                                                       LE_C3_ACON |
1383                                                       LE_C3_BCON));
1384
1385         lp->name = lancestr;
1386         lp->ledma = ledma;
1387
1388         lp->burst_sizes = 0;
1389         if (lp->ledma) {
1390                 struct device_node *ledma_dp = ledma->sdev->ofdev.node;
1391                 const char *prop;
1392                 unsigned int sbmask;
1393                 u32 csr;
1394
1395                 /* Find burst-size property for ledma */
1396                 lp->burst_sizes = of_getintprop_default(ledma_dp,
1397                                                         "burst-sizes", 0);
1398
1399                 /* ledma may be capable of fast bursts, but sbus may not. */
1400                 sbmask = of_getintprop_default(ledma_dp, "burst-sizes",
1401                                                DMA_BURSTBITS);
1402                 lp->burst_sizes &= sbmask;
1403
1404                 /* Get the cable-selection property */
1405                 prop = of_get_property(ledma_dp, "cable-selection", NULL);
1406                 if (!prop || prop[0] == '\0') {
1407                         struct device_node *nd;
1408
1409                         printk(KERN_INFO "SunLance: using "
1410                                "auto-carrier-detection.\n");
1411
1412                         nd = of_find_node_by_path("/options");
1413                         if (!nd)
1414                                 goto no_link_test;
1415
1416                         prop = of_get_property(nd, "tpe-link-test?", NULL);
1417                         if (!prop)
1418                                 goto no_link_test;
1419
1420                         if (strcmp(prop, "true")) {
1421                                 printk(KERN_NOTICE "SunLance: warning: overriding option "
1422                                        "'tpe-link-test?'\n");
1423                                 printk(KERN_NOTICE "SunLance: warning: mail any problems "
1424                                        "to ecd@skynet.be\n");
1425                                 auxio_set_lte(AUXIO_LTE_ON);
1426                         }
1427 no_link_test:
1428                         lp->auto_select = 1;
1429                         lp->tpe = 0;
1430                 } else if (!strcmp(prop, "aui")) {
1431                         lp->auto_select = 0;
1432                         lp->tpe = 0;
1433                 } else {
1434                         lp->auto_select = 0;
1435                         lp->tpe = 1;
1436                 }
1437
1438                 lp->dregs = ledma->regs;
1439
1440                 /* Reset ledma */
1441                 csr = sbus_readl(lp->dregs + DMA_CSR);
1442                 sbus_writel(csr | DMA_RST_ENET, lp->dregs + DMA_CSR);
1443                 udelay(200);
1444                 sbus_writel(csr & ~DMA_RST_ENET, lp->dregs + DMA_CSR);
1445         } else
1446                 lp->dregs = NULL;
1447
1448         lp->dev = dev;
1449         SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
1450         dev->open = &lance_open;
1451         dev->stop = &lance_close;
1452         dev->hard_start_xmit = &lance_start_xmit;
1453         dev->tx_timeout = &lance_tx_timeout;
1454         dev->watchdog_timeo = 5*HZ;
1455         dev->set_multicast_list = &lance_set_multicast;
1456         dev->ethtool_ops = &sparc_lance_ethtool_ops;
1457
1458         dev->irq = sdev->irqs[0];
1459
1460         dev->dma = 0;
1461
1462         /* We cannot sleep if the chip is busy during a
1463          * multicast list update event, because such events
1464          * can occur from interrupts (ex. IPv6).  So we
1465          * use a timer to try again later when necessary. -DaveM
1466          */
1467         init_timer(&lp->multicast_timer);
1468         lp->multicast_timer.data = (unsigned long) dev;
1469         lp->multicast_timer.function = &lance_set_multicast_retry;
1470
1471         if (register_netdev(dev)) {
1472                 printk(KERN_ERR "SunLance: Cannot register device.\n");
1473                 goto fail;
1474         }
1475
1476         dev_set_drvdata(&sdev->ofdev.dev, lp);
1477
1478         printk(KERN_INFO "%s: LANCE %s\n",
1479                dev->name, print_mac(mac, dev->dev_addr));
1480
1481         return 0;
1482
1483 fail:
1484         lance_free_hwresources(lp);
1485         free_netdev(dev);
1486         return -ENODEV;
1487 }
1488
1489 /* On 4m, find the associated dma for the lance chip */
1490 static struct sbus_dma * __devinit find_ledma(struct sbus_dev *sdev)
1491 {
1492         struct sbus_dma *p;
1493
1494         for_each_dvma(p) {
1495                 if (p->sdev == sdev)
1496                         return p;
1497         }
1498         return NULL;
1499 }
1500
1501 #ifdef CONFIG_SUN4
1502
1503 #include <asm/sun4paddr.h>
1504 #include <asm/machines.h>
1505
1506 /* Find all the lance cards on the system and initialize them */
1507 static struct sbus_dev sun4_sdev;
1508 static int __devinit sparc_lance_init(void)
1509 {
1510         if ((idprom->id_machtype == (SM_SUN4|SM_4_330)) ||
1511             (idprom->id_machtype == (SM_SUN4|SM_4_470))) {
1512                 memset(&sun4_sdev, 0, sizeof(struct sbus_dev));
1513                 sun4_sdev.reg_addrs[0].phys_addr = sun4_eth_physaddr;
1514                 sun4_sdev.irqs[0] = 6;
1515                 return sparc_lance_probe_one(&sun4_sdev, NULL, NULL);
1516         }
1517         return -ENODEV;
1518 }
1519
1520 static int __exit sunlance_sun4_remove(void)
1521 {
1522         struct lance_private *lp = dev_get_drvdata(&sun4_sdev.ofdev.dev);
1523         struct net_device *net_dev = lp->dev;
1524
1525         unregister_netdev(net_dev);
1526
1527         lance_free_hwresources(lp);
1528
1529         free_netdev(net_dev);
1530
1531         dev_set_drvdata(&sun4_sdev.ofdev.dev, NULL);
1532
1533         return 0;
1534 }
1535
1536 #else /* !CONFIG_SUN4 */
1537
1538 static int __devinit sunlance_sbus_probe(struct of_device *dev, const struct of_device_id *match)
1539 {
1540         struct sbus_dev *sdev = to_sbus_device(&dev->dev);
1541         int err;
1542
1543         if (sdev->parent) {
1544                 struct of_device *parent = &sdev->parent->ofdev;
1545
1546                 if (!strcmp(parent->node->name, "ledma")) {
1547                         struct sbus_dma *ledma = find_ledma(to_sbus_device(&parent->dev));
1548
1549                         err = sparc_lance_probe_one(sdev, ledma, NULL);
1550                 } else if (!strcmp(parent->node->name, "lebuffer")) {
1551                         err = sparc_lance_probe_one(sdev, NULL, to_sbus_device(&parent->dev));
1552                 } else
1553                         err = sparc_lance_probe_one(sdev, NULL, NULL);
1554         } else
1555                 err = sparc_lance_probe_one(sdev, NULL, NULL);
1556
1557         return err;
1558 }
1559
1560 static int __devexit sunlance_sbus_remove(struct of_device *dev)
1561 {
1562         struct lance_private *lp = dev_get_drvdata(&dev->dev);
1563         struct net_device *net_dev = lp->dev;
1564
1565         unregister_netdev(net_dev);
1566
1567         lance_free_hwresources(lp);
1568
1569         free_netdev(net_dev);
1570
1571         dev_set_drvdata(&dev->dev, NULL);
1572
1573         return 0;
1574 }
1575
1576 static struct of_device_id sunlance_sbus_match[] = {
1577         {
1578                 .name = "le",
1579         },
1580         {},
1581 };
1582
1583 MODULE_DEVICE_TABLE(of, sunlance_sbus_match);
1584
1585 static struct of_platform_driver sunlance_sbus_driver = {
1586         .name           = "sunlance",
1587         .match_table    = sunlance_sbus_match,
1588         .probe          = sunlance_sbus_probe,
1589         .remove         = __devexit_p(sunlance_sbus_remove),
1590 };
1591
1592
1593 /* Find all the lance cards on the system and initialize them */
1594 static int __init sparc_lance_init(void)
1595 {
1596         return of_register_driver(&sunlance_sbus_driver, &sbus_bus_type);
1597 }
1598 #endif /* !CONFIG_SUN4 */
1599
1600 static void __exit sparc_lance_exit(void)
1601 {
1602 #ifdef CONFIG_SUN4
1603         sunlance_sun4_remove();
1604 #else
1605         of_unregister_driver(&sunlance_sbus_driver);
1606 #endif
1607 }
1608
1609 module_init(sparc_lance_init);
1610 module_exit(sparc_lance_exit);