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