qlge: bugfix: Increase filter on inbound csum.
[linux-2.6] / drivers / net / ni65.c
1 /*
2  * ni6510 (am7990 'lance' chip) driver for Linux-net-3
3  * BETAcode v0.71 (96/09/29) for 2.0.0 (or later)
4  * copyrights (c) 1994,1995,1996 by M.Hipp
5  *
6  * This driver can handle the old ni6510 board and the newer ni6510
7  * EtherBlaster. (probably it also works with every full NE2100
8  * compatible card)
9  *
10  * driver probes: io: 0x360,0x300,0x320,0x340 / dma: 3,5,6,7
11  *
12  * This is an extension to the Linux operating system, and is covered by the
13  * same GNU General Public License that covers the Linux-kernel.
14  *
15  * comments/bugs/suggestions can be sent to:
16  *   Michael Hipp
17  *   email: hippm@informatik.uni-tuebingen.de
18  *
19  * sources:
20  *   some things are from the 'ni6510-packet-driver for dos by Russ Nelson'
21  *   and from the original drivers by D.Becker
22  *
23  * known problems:
24  *   - on some PCI boards (including my own) the card/board/ISA-bridge has
25  *     problems with bus master DMA. This results in lotsa overruns.
26  *     It may help to '#define RCV_PARANOIA_CHECK' or try to #undef
27  *     the XMT and RCV_VIA_SKB option .. this reduces driver performance.
28  *     Or just play with your BIOS options to optimize ISA-DMA access.
29  *     Maybe you also wanna play with the LOW_PERFORAMCE and MID_PERFORMANCE
30  *     defines -> please report me your experience then
31  *   - Harald reported for ASUS SP3G mainboards, that you should use
32  *     the 'optimal settings' from the user's manual on page 3-12!
33  *
34  * credits:
35  *   thanx to Jason Sullivan for sending me a ni6510 card!
36  *   lot of debug runs with ASUS SP3G Boards (Intel Saturn) by Harald Koenig
37  *
38  * simple performance test: (486DX-33/Ni6510-EB receives from 486DX4-100/Ni6510-EB)
39  *    average: FTP -> 8384421 bytes received in 8.5 seconds
40  *           (no RCV_VIA_SKB,no XMT_VIA_SKB,PARANOIA_CHECK,4 XMIT BUFS, 8 RCV_BUFFS)
41  *    peak: FTP -> 8384421 bytes received in 7.5 seconds
42  *           (RCV_VIA_SKB,XMT_VIA_SKB,no PARANOIA_CHECK,1(!) XMIT BUF, 16 RCV BUFFS)
43  */
44
45 /*
46  * 99.Jun.8: added support for /proc/net/dev byte count for xosview (HK)
47  * 96.Sept.29: virt_to_bus stuff added for new memory modell
48  * 96.April.29: Added Harald Koenig's Patches (MH)
49  * 96.April.13: enhanced error handling .. more tests (MH)
50  * 96.April.5/6: a lot of performance tests. Got it stable now (hopefully) (MH)
51  * 96.April.1: (no joke ;) .. added EtherBlaster and Module support (MH)
52  * 96.Feb.19: fixed a few bugs .. cleanups .. tested for 1.3.66 (MH)
53  *            hopefully no more 16MB limit
54  *
55  * 95.Nov.18: multicast tweaked (AC).
56  *
57  * 94.Aug.22: changes in xmit_intr (ack more than one xmitted-packet), ni65_send_packet (p->lock) (MH)
58  *
59  * 94.July.16: fixed bugs in recv_skb and skb-alloc stuff  (MH)
60  */
61
62 #include <linux/kernel.h>
63 #include <linux/string.h>
64 #include <linux/errno.h>
65 #include <linux/ioport.h>
66 #include <linux/slab.h>
67 #include <linux/interrupt.h>
68 #include <linux/delay.h>
69 #include <linux/init.h>
70 #include <linux/netdevice.h>
71 #include <linux/etherdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/module.h>
74 #include <linux/bitops.h>
75
76 #include <asm/io.h>
77 #include <asm/dma.h>
78
79 #include "ni65.h"
80
81 /*
82  * the current setting allows an acceptable performance
83  * for 'RCV_PARANOIA_CHECK' read the 'known problems' part in
84  * the header of this file
85  * 'invert' the defines for max. performance. This may cause DMA problems
86  * on some boards (e.g on my ASUS SP3G)
87  */
88 #undef XMT_VIA_SKB
89 #undef RCV_VIA_SKB
90 #define RCV_PARANOIA_CHECK
91
92 #define MID_PERFORMANCE
93
94 #if   defined( LOW_PERFORMANCE )
95  static int isa0=7,isa1=7,csr80=0x0c10;
96 #elif defined( MID_PERFORMANCE )
97  static int isa0=5,isa1=5,csr80=0x2810;
98 #else   /* high performance */
99  static int isa0=4,isa1=4,csr80=0x0017;
100 #endif
101
102 /*
103  * a few card/vendor specific defines
104  */
105 #define NI65_ID0    0x00
106 #define NI65_ID1    0x55
107 #define NI65_EB_ID0 0x52
108 #define NI65_EB_ID1 0x44
109 #define NE2100_ID0  0x57
110 #define NE2100_ID1  0x57
111
112 #define PORT p->cmdr_addr
113
114 /*
115  * buffer configuration
116  */
117 #if 1
118 #define RMDNUM 16
119 #define RMDNUMMASK 0x80000000
120 #else
121 #define RMDNUM 8
122 #define RMDNUMMASK 0x60000000 /* log2(RMDNUM)<<29 */
123 #endif
124
125 #if 0
126 #define TMDNUM 1
127 #define TMDNUMMASK 0x00000000
128 #else
129 #define TMDNUM 4
130 #define TMDNUMMASK 0x40000000 /* log2(TMDNUM)<<29 */
131 #endif
132
133 /* slightly oversized */
134 #define R_BUF_SIZE 1544
135 #define T_BUF_SIZE 1544
136
137 /*
138  * lance register defines
139  */
140 #define L_DATAREG 0x00
141 #define L_ADDRREG 0x02
142 #define L_RESET   0x04
143 #define L_CONFIG  0x05
144 #define L_BUSIF   0x06
145
146 /*
147  * to access the lance/am7990-regs, you have to write
148  * reg-number into L_ADDRREG, then you can access it using L_DATAREG
149  */
150 #define CSR0  0x00
151 #define CSR1  0x01
152 #define CSR2  0x02
153 #define CSR3  0x03
154
155 #define INIT_RING_BEFORE_START  0x1
156 #define FULL_RESET_ON_ERROR     0x2
157
158 #if 0
159 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);inw(PORT+L_ADDRREG); \
160                            outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
161 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_ADDRREG),\
162                        inw(PORT+L_DATAREG))
163 #if 0
164 #define writedatareg(val) {outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
165 #else
166 #define writedatareg(val) {  writereg(val,CSR0); }
167 #endif
168 #else
169 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);outw(val,PORT+L_DATAREG);}
170 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_DATAREG))
171 #define writedatareg(val) { writereg(val,CSR0); }
172 #endif
173
174 static unsigned char ni_vendor[] = { 0x02,0x07,0x01 };
175
176 static struct card {
177         unsigned char id0,id1;
178         short id_offset;
179         short total_size;
180         short cmd_offset;
181         short addr_offset;
182         unsigned char *vendor_id;
183         char *cardname;
184         unsigned long config;
185 } cards[] = {
186         {
187                 .id0         = NI65_ID0,
188                 .id1         = NI65_ID1,
189                 .id_offset   = 0x0e,
190                 .total_size  = 0x10,
191                 .cmd_offset  = 0x0,
192                 .addr_offset = 0x8,
193                 .vendor_id   = ni_vendor,
194                 .cardname    = "ni6510",
195                 .config      = 0x1,
196         },
197         {
198                 .id0         = NI65_EB_ID0,
199                 .id1         = NI65_EB_ID1,
200                 .id_offset   = 0x0e,
201                 .total_size  = 0x18,
202                 .cmd_offset  = 0x10,
203                 .addr_offset = 0x0,
204                 .vendor_id   = ni_vendor,
205                 .cardname    = "ni6510 EtherBlaster",
206                 .config      = 0x2,
207         },
208         {
209                 .id0         = NE2100_ID0,
210                 .id1         = NE2100_ID1,
211                 .id_offset   = 0x0e,
212                 .total_size  = 0x18,
213                 .cmd_offset  = 0x10,
214                 .addr_offset = 0x0,
215                 .vendor_id   = NULL,
216                 .cardname    = "generic NE2100",
217                 .config      = 0x0,
218         },
219 };
220 #define NUM_CARDS 3
221
222 struct priv
223 {
224         struct rmd rmdhead[RMDNUM];
225         struct tmd tmdhead[TMDNUM];
226         struct init_block ib;
227         int rmdnum;
228         int tmdnum,tmdlast;
229 #ifdef RCV_VIA_SKB
230         struct sk_buff *recv_skb[RMDNUM];
231 #else
232         void *recvbounce[RMDNUM];
233 #endif
234 #ifdef XMT_VIA_SKB
235         struct sk_buff *tmd_skb[TMDNUM];
236 #endif
237         void *tmdbounce[TMDNUM];
238         int tmdbouncenum;
239         int lock,xmit_queued;
240         struct net_device_stats stats;
241         void *self;
242         int cmdr_addr;
243         int cardno;
244         int features;
245         spinlock_t ring_lock;
246 };
247
248 static int  ni65_probe1(struct net_device *dev,int);
249 static irqreturn_t ni65_interrupt(int irq, void * dev_id);
250 static void ni65_recv_intr(struct net_device *dev,int);
251 static void ni65_xmit_intr(struct net_device *dev,int);
252 static int  ni65_open(struct net_device *dev);
253 static int  ni65_lance_reinit(struct net_device *dev);
254 static void ni65_init_lance(struct priv *p,unsigned char*,int,int);
255 static int  ni65_send_packet(struct sk_buff *skb, struct net_device *dev);
256 static void  ni65_timeout(struct net_device *dev);
257 static int  ni65_close(struct net_device *dev);
258 static int  ni65_alloc_buffer(struct net_device *dev);
259 static void ni65_free_buffer(struct priv *p);
260 static struct net_device_stats *ni65_get_stats(struct net_device *);
261 static void set_multicast_list(struct net_device *dev);
262
263 static int irqtab[] __initdata = { 9,12,15,5 }; /* irq config-translate */
264 static int dmatab[] __initdata = { 0,3,5,6,7 }; /* dma config-translate and autodetect */
265
266 static int debuglevel = 1;
267
268 /*
269  * set 'performance' registers .. we must STOP lance for that
270  */
271 static void ni65_set_performance(struct priv *p)
272 {
273         writereg(CSR0_STOP | CSR0_CLRALL,CSR0); /* STOP */
274
275         if( !(cards[p->cardno].config & 0x02) )
276                 return;
277
278         outw(80,PORT+L_ADDRREG);
279         if(inw(PORT+L_ADDRREG) != 80)
280                 return;
281
282         writereg( (csr80 & 0x3fff) ,80); /* FIFO watermarks */
283         outw(0,PORT+L_ADDRREG);
284         outw((short)isa0,PORT+L_BUSIF); /* write ISA 0: DMA_R : isa0 * 50ns */
285         outw(1,PORT+L_ADDRREG);
286         outw((short)isa1,PORT+L_BUSIF); /* write ISA 1: DMA_W : isa1 * 50ns     */
287
288         outw(CSR0,PORT+L_ADDRREG);      /* switch back to CSR0 */
289 }
290
291 /*
292  * open interface (up)
293  */
294 static int ni65_open(struct net_device *dev)
295 {
296         struct priv *p = dev->ml_priv;
297         int irqval = request_irq(dev->irq, &ni65_interrupt,0,
298                         cards[p->cardno].cardname,dev);
299         if (irqval) {
300                 printk(KERN_ERR "%s: unable to get IRQ %d (irqval=%d).\n",
301                           dev->name,dev->irq, irqval);
302                 return -EAGAIN;
303         }
304
305         if(ni65_lance_reinit(dev))
306         {
307                 netif_start_queue(dev);
308                 return 0;
309         }
310         else
311         {
312                 free_irq(dev->irq,dev);
313                 return -EAGAIN;
314         }
315 }
316
317 /*
318  * close interface (down)
319  */
320 static int ni65_close(struct net_device *dev)
321 {
322         struct priv *p = dev->ml_priv;
323
324         netif_stop_queue(dev);
325
326         outw(inw(PORT+L_RESET),PORT+L_RESET); /* that's the hard way */
327
328 #ifdef XMT_VIA_SKB
329         {
330                 int i;
331                 for(i=0;i<TMDNUM;i++)
332                 {
333                         if(p->tmd_skb[i]) {
334                                 dev_kfree_skb(p->tmd_skb[i]);
335                                 p->tmd_skb[i] = NULL;
336                         }
337                 }
338         }
339 #endif
340         free_irq(dev->irq,dev);
341         return 0;
342 }
343
344 static void cleanup_card(struct net_device *dev)
345 {
346         struct priv *p = dev->ml_priv;
347         disable_dma(dev->dma);
348         free_dma(dev->dma);
349         release_region(dev->base_addr, cards[p->cardno].total_size);
350         ni65_free_buffer(p);
351 }
352
353 /* set: io,irq,dma or set it when calling insmod */
354 static int irq;
355 static int io;
356 static int dma;
357
358 /*
359  * Probe The Card (not the lance-chip)
360  */
361 struct net_device * __init ni65_probe(int unit)
362 {
363         struct net_device *dev = alloc_etherdev(0);
364         static int ports[] = {0x360,0x300,0x320,0x340, 0};
365         int *port;
366         int err = 0;
367
368         if (!dev)
369                 return ERR_PTR(-ENOMEM);
370
371         if (unit >= 0) {
372                 sprintf(dev->name, "eth%d", unit);
373                 netdev_boot_setup_check(dev);
374                 irq = dev->irq;
375                 dma = dev->dma;
376         } else {
377                 dev->base_addr = io;
378         }
379
380         if (dev->base_addr > 0x1ff) { /* Check a single specified location. */
381                 err = ni65_probe1(dev, dev->base_addr);
382         } else if (dev->base_addr > 0) { /* Don't probe at all. */
383                 err = -ENXIO;
384         } else {
385                 for (port = ports; *port && ni65_probe1(dev, *port); port++)
386                         ;
387                 if (!*port)
388                         err = -ENODEV;
389         }
390         if (err)
391                 goto out;
392
393         err = register_netdev(dev);
394         if (err)
395                 goto out1;
396         return dev;
397 out1:
398         cleanup_card(dev);
399 out:
400         free_netdev(dev);
401         return ERR_PTR(err);
402 }
403
404 /*
405  * this is the real card probe ..
406  */
407 static int __init ni65_probe1(struct net_device *dev,int ioaddr)
408 {
409         int i,j;
410         struct priv *p;
411         unsigned long flags;
412
413         dev->irq = irq;
414         dev->dma = dma;
415
416         for(i=0;i<NUM_CARDS;i++) {
417                 if(!request_region(ioaddr, cards[i].total_size, cards[i].cardname))
418                         continue;
419                 if(cards[i].id_offset >= 0) {
420                         if(inb(ioaddr+cards[i].id_offset+0) != cards[i].id0 ||
421                                  inb(ioaddr+cards[i].id_offset+1) != cards[i].id1) {
422                                  release_region(ioaddr, cards[i].total_size);
423                                  continue;
424                         }
425                 }
426                 if(cards[i].vendor_id) {
427                         for(j=0;j<3;j++)
428                                 if(inb(ioaddr+cards[i].addr_offset+j) != cards[i].vendor_id[j]) {
429                                         release_region(ioaddr, cards[i].total_size);
430                                         continue;
431                           }
432                 }
433                 break;
434         }
435         if(i == NUM_CARDS)
436                 return -ENODEV;
437
438         for(j=0;j<6;j++)
439                 dev->dev_addr[j] = inb(ioaddr+cards[i].addr_offset+j);
440
441         if( (j=ni65_alloc_buffer(dev)) < 0) {
442                 release_region(ioaddr, cards[i].total_size);
443                 return j;
444         }
445         p = dev->ml_priv;
446         p->cmdr_addr = ioaddr + cards[i].cmd_offset;
447         p->cardno = i;
448         spin_lock_init(&p->ring_lock);
449
450         printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cards[p->cardno].cardname , ioaddr);
451
452         outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
453         if( (j=readreg(CSR0)) != 0x4) {
454                  printk("failed.\n");
455                  printk(KERN_ERR "%s: Can't RESET card: %04x\n", dev->name, j);
456                  ni65_free_buffer(p);
457                  release_region(ioaddr, cards[p->cardno].total_size);
458                  return -EAGAIN;
459         }
460
461         outw(88,PORT+L_ADDRREG);
462         if(inw(PORT+L_ADDRREG) == 88) {
463                 unsigned long v;
464                 v = inw(PORT+L_DATAREG);
465                 v <<= 16;
466                 outw(89,PORT+L_ADDRREG);
467                 v |= inw(PORT+L_DATAREG);
468                 printk("Version %#08lx, ",v);
469                 p->features = INIT_RING_BEFORE_START;
470         }
471         else {
472                 printk("ancient LANCE, ");
473                 p->features = 0x0;
474         }
475
476         if(test_bit(0,&cards[i].config)) {
477                 dev->irq = irqtab[(inw(ioaddr+L_CONFIG)>>2)&3];
478                 dev->dma = dmatab[inw(ioaddr+L_CONFIG)&3];
479                 printk("IRQ %d (from card), DMA %d (from card).\n",dev->irq,dev->dma);
480         }
481         else {
482                 if(dev->dma == 0) {
483                 /* 'stuck test' from lance.c */
484                         long dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
485                                             (inb(DMA2_STAT_REG) & 0xf0);
486                         for(i=1;i<5;i++) {
487                                 int dma = dmatab[i];
488                                 if(test_bit(dma,&dma_channels) || request_dma(dma,"ni6510"))
489                                         continue;
490
491                                 flags=claim_dma_lock();
492                                 disable_dma(dma);
493                                 set_dma_mode(dma,DMA_MODE_CASCADE);
494                                 enable_dma(dma);
495                                 release_dma_lock(flags);
496
497                                 ni65_init_lance(p,dev->dev_addr,0,0); /* trigger memory access */
498
499                                 flags=claim_dma_lock();
500                                 disable_dma(dma);
501                                 free_dma(dma);
502                                 release_dma_lock(flags);
503
504                                 if(readreg(CSR0) & CSR0_IDON)
505                                         break;
506                         }
507                         if(i == 5) {
508                                 printk("failed.\n");
509                                 printk(KERN_ERR "%s: Can't detect DMA channel!\n", dev->name);
510                                 ni65_free_buffer(p);
511                                 release_region(ioaddr, cards[p->cardno].total_size);
512                                 return -EAGAIN;
513                         }
514                         dev->dma = dmatab[i];
515                         printk("DMA %d (autodetected), ",dev->dma);
516                 }
517                 else
518                         printk("DMA %d (assigned), ",dev->dma);
519
520                 if(dev->irq < 2)
521                 {
522                         unsigned long irq_mask;
523
524                         ni65_init_lance(p,dev->dev_addr,0,0);
525                         irq_mask = probe_irq_on();
526                         writereg(CSR0_INIT|CSR0_INEA,CSR0); /* trigger interrupt */
527                         msleep(20);
528                         dev->irq = probe_irq_off(irq_mask);
529                         if(!dev->irq)
530                         {
531                                 printk("Failed to detect IRQ line!\n");
532                                 ni65_free_buffer(p);
533                                 release_region(ioaddr, cards[p->cardno].total_size);
534                                 return -EAGAIN;
535                         }
536                         printk("IRQ %d (autodetected).\n",dev->irq);
537                 }
538                 else
539                         printk("IRQ %d (assigned).\n",dev->irq);
540         }
541
542         if(request_dma(dev->dma, cards[p->cardno].cardname ) != 0)
543         {
544                 printk(KERN_ERR "%s: Can't request dma-channel %d\n",dev->name,(int) dev->dma);
545                 ni65_free_buffer(p);
546                 release_region(ioaddr, cards[p->cardno].total_size);
547                 return -EAGAIN;
548         }
549
550         dev->base_addr = ioaddr;
551         dev->open               = ni65_open;
552         dev->stop               = ni65_close;
553         dev->hard_start_xmit    = ni65_send_packet;
554         dev->tx_timeout         = ni65_timeout;
555         dev->watchdog_timeo     = HZ/2;
556         dev->get_stats          = ni65_get_stats;
557         dev->set_multicast_list = set_multicast_list;
558         return 0; /* everything is OK */
559 }
560
561 /*
562  * set lance register and trigger init
563  */
564 static void ni65_init_lance(struct priv *p,unsigned char *daddr,int filter,int mode)
565 {
566         int i;
567         u32 pib;
568
569         writereg(CSR0_CLRALL|CSR0_STOP,CSR0);
570
571         for(i=0;i<6;i++)
572                 p->ib.eaddr[i] = daddr[i];
573
574         for(i=0;i<8;i++)
575                 p->ib.filter[i] = filter;
576         p->ib.mode = mode;
577
578         p->ib.trp = (u32) isa_virt_to_bus(p->tmdhead) | TMDNUMMASK;
579         p->ib.rrp = (u32) isa_virt_to_bus(p->rmdhead) | RMDNUMMASK;
580         writereg(0,CSR3);       /* busmaster/no word-swap */
581         pib = (u32) isa_virt_to_bus(&p->ib);
582         writereg(pib & 0xffff,CSR1);
583         writereg(pib >> 16,CSR2);
584
585         writereg(CSR0_INIT,CSR0); /* this changes L_ADDRREG to CSR0 */
586
587         for(i=0;i<32;i++)
588         {
589                 mdelay(4);
590                 if(inw(PORT+L_DATAREG) & (CSR0_IDON | CSR0_MERR) )
591                         break; /* init ok ? */
592         }
593 }
594
595 /*
596  * allocate memory area and check the 16MB border
597  */
598 static void *ni65_alloc_mem(struct net_device *dev,char *what,int size,int type)
599 {
600         struct sk_buff *skb=NULL;
601         unsigned char *ptr;
602         void *ret;
603
604         if(type) {
605                 ret = skb = alloc_skb(2+16+size,GFP_KERNEL|GFP_DMA);
606                 if(!skb) {
607                         printk(KERN_WARNING "%s: unable to allocate %s memory.\n",dev->name,what);
608                         return NULL;
609                 }
610                 skb_reserve(skb,2+16);
611                 skb_put(skb,R_BUF_SIZE);         /* grab the whole space .. (not necessary) */
612                 ptr = skb->data;
613         }
614         else {
615                 ret = ptr = kmalloc(T_BUF_SIZE,GFP_KERNEL | GFP_DMA);
616                 if(!ret) {
617                         printk(KERN_WARNING "%s: unable to allocate %s memory.\n",dev->name,what);
618                         return NULL;
619                 }
620         }
621         if( (u32) virt_to_phys(ptr+size) > 0x1000000) {
622                 printk(KERN_WARNING "%s: unable to allocate %s memory in lower 16MB!\n",dev->name,what);
623                 if(type)
624                         kfree_skb(skb);
625                 else
626                         kfree(ptr);
627                 return NULL;
628         }
629         return ret;
630 }
631
632 /*
633  * allocate all memory structures .. send/recv buffers etc ...
634  */
635 static int ni65_alloc_buffer(struct net_device *dev)
636 {
637         unsigned char *ptr;
638         struct priv *p;
639         int i;
640
641         /*
642          * we need 8-aligned memory ..
643          */
644         ptr = ni65_alloc_mem(dev,"BUFFER",sizeof(struct priv)+8,0);
645         if(!ptr)
646                 return -ENOMEM;
647
648         p = dev->ml_priv = (struct priv *) (((unsigned long) ptr + 7) & ~0x7);
649         memset((char *)p, 0, sizeof(struct priv));
650         p->self = ptr;
651
652         for(i=0;i<TMDNUM;i++)
653         {
654 #ifdef XMT_VIA_SKB
655                 p->tmd_skb[i] = NULL;
656 #endif
657                 p->tmdbounce[i] = ni65_alloc_mem(dev,"XMIT",T_BUF_SIZE,0);
658                 if(!p->tmdbounce[i]) {
659                         ni65_free_buffer(p);
660                         return -ENOMEM;
661                 }
662         }
663
664         for(i=0;i<RMDNUM;i++)
665         {
666 #ifdef RCV_VIA_SKB
667                 p->recv_skb[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,1);
668                 if(!p->recv_skb[i]) {
669                         ni65_free_buffer(p);
670                         return -ENOMEM;
671                 }
672 #else
673                 p->recvbounce[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,0);
674                 if(!p->recvbounce[i]) {
675                         ni65_free_buffer(p);
676                         return -ENOMEM;
677                 }
678 #endif
679         }
680
681         return 0; /* everything is OK */
682 }
683
684 /*
685  * free buffers and private struct
686  */
687 static void ni65_free_buffer(struct priv *p)
688 {
689         int i;
690
691         if(!p)
692                 return;
693
694         for(i=0;i<TMDNUM;i++) {
695                 kfree(p->tmdbounce[i]);
696 #ifdef XMT_VIA_SKB
697                 if(p->tmd_skb[i])
698                         dev_kfree_skb(p->tmd_skb[i]);
699 #endif
700         }
701
702         for(i=0;i<RMDNUM;i++)
703         {
704 #ifdef RCV_VIA_SKB
705                 if(p->recv_skb[i])
706                         dev_kfree_skb(p->recv_skb[i]);
707 #else
708                 kfree(p->recvbounce[i]);
709 #endif
710         }
711         kfree(p->self);
712 }
713
714
715 /*
716  * stop and (re)start lance .. e.g after an error
717  */
718 static void ni65_stop_start(struct net_device *dev,struct priv *p)
719 {
720         int csr0 = CSR0_INEA;
721
722         writedatareg(CSR0_STOP);
723
724         if(debuglevel > 1)
725                 printk(KERN_DEBUG "ni65_stop_start\n");
726
727         if(p->features & INIT_RING_BEFORE_START) {
728                 int i;
729 #ifdef XMT_VIA_SKB
730                 struct sk_buff *skb_save[TMDNUM];
731 #endif
732                 unsigned long buffer[TMDNUM];
733                 short blen[TMDNUM];
734
735                 if(p->xmit_queued) {
736                         while(1) {
737                                 if((p->tmdhead[p->tmdlast].u.s.status & XMIT_OWN))
738                                         break;
739                                 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
740                                 if(p->tmdlast == p->tmdnum)
741                                         break;
742                         }
743                 }
744
745                 for(i=0;i<TMDNUM;i++) {
746                         struct tmd *tmdp = p->tmdhead + i;
747 #ifdef XMT_VIA_SKB
748                         skb_save[i] = p->tmd_skb[i];
749 #endif
750                         buffer[i] = (u32) isa_bus_to_virt(tmdp->u.buffer);
751                         blen[i] = tmdp->blen;
752                         tmdp->u.s.status = 0x0;
753                 }
754
755                 for(i=0;i<RMDNUM;i++) {
756                         struct rmd *rmdp = p->rmdhead + i;
757                         rmdp->u.s.status = RCV_OWN;
758                 }
759                 p->tmdnum = p->xmit_queued = 0;
760                 writedatareg(CSR0_STRT | csr0);
761
762                 for(i=0;i<TMDNUM;i++) {
763                         int num = (i + p->tmdlast) & (TMDNUM-1);
764                         p->tmdhead[i].u.buffer = (u32) isa_virt_to_bus((char *)buffer[num]); /* status is part of buffer field */
765                         p->tmdhead[i].blen = blen[num];
766                         if(p->tmdhead[i].u.s.status & XMIT_OWN) {
767                                  p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
768                                  p->xmit_queued = 1;
769          writedatareg(CSR0_TDMD | CSR0_INEA | csr0);
770                         }
771 #ifdef XMT_VIA_SKB
772                         p->tmd_skb[i] = skb_save[num];
773 #endif
774                 }
775                 p->rmdnum = p->tmdlast = 0;
776                 if(!p->lock)
777                         if (p->tmdnum || !p->xmit_queued)
778                                 netif_wake_queue(dev);
779                 dev->trans_start = jiffies;
780         }
781         else
782                 writedatareg(CSR0_STRT | csr0);
783 }
784
785 /*
786  * init lance (write init-values .. init-buffers) (open-helper)
787  */
788 static int ni65_lance_reinit(struct net_device *dev)
789 {
790          int i;
791          struct priv *p = dev->ml_priv;
792          unsigned long flags;
793
794          p->lock = 0;
795          p->xmit_queued = 0;
796
797          flags=claim_dma_lock();
798          disable_dma(dev->dma); /* I've never worked with dma, but we do it like the packetdriver */
799          set_dma_mode(dev->dma,DMA_MODE_CASCADE);
800          enable_dma(dev->dma);
801          release_dma_lock(flags);
802
803          outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
804          if( (i=readreg(CSR0) ) != 0x4)
805          {
806                  printk(KERN_ERR "%s: can't RESET %s card: %04x\n",dev->name,
807                                                         cards[p->cardno].cardname,(int) i);
808                  flags=claim_dma_lock();
809                  disable_dma(dev->dma);
810                  release_dma_lock(flags);
811                  return 0;
812          }
813
814          p->rmdnum = p->tmdnum = p->tmdlast = p->tmdbouncenum = 0;
815          for(i=0;i<TMDNUM;i++)
816          {
817                  struct tmd *tmdp = p->tmdhead + i;
818 #ifdef XMT_VIA_SKB
819                  if(p->tmd_skb[i]) {
820                          dev_kfree_skb(p->tmd_skb[i]);
821                          p->tmd_skb[i] = NULL;
822                  }
823 #endif
824                  tmdp->u.buffer = 0x0;
825                  tmdp->u.s.status = XMIT_START | XMIT_END;
826                  tmdp->blen = tmdp->status2 = 0;
827          }
828
829          for(i=0;i<RMDNUM;i++)
830          {
831                  struct rmd *rmdp = p->rmdhead + i;
832 #ifdef RCV_VIA_SKB
833                  rmdp->u.buffer = (u32) isa_virt_to_bus(p->recv_skb[i]->data);
834 #else
835                  rmdp->u.buffer = (u32) isa_virt_to_bus(p->recvbounce[i]);
836 #endif
837                  rmdp->blen = -(R_BUF_SIZE-8);
838                  rmdp->mlen = 0;
839                  rmdp->u.s.status = RCV_OWN;
840          }
841
842          if(dev->flags & IFF_PROMISC)
843                  ni65_init_lance(p,dev->dev_addr,0x00,M_PROM);
844          else if(dev->mc_count || dev->flags & IFF_ALLMULTI)
845                  ni65_init_lance(p,dev->dev_addr,0xff,0x0);
846          else
847                  ni65_init_lance(p,dev->dev_addr,0x00,0x00);
848
849         /*
850          * ni65_set_lance_mem() sets L_ADDRREG to CSR0
851          * NOW, WE WILL NEVER CHANGE THE L_ADDRREG, CSR0 IS ALWAYS SELECTED
852          */
853
854          if(inw(PORT+L_DATAREG) & CSR0_IDON)    {
855                  ni65_set_performance(p);
856                                          /* init OK: start lance , enable interrupts */
857                  writedatareg(CSR0_CLRALL | CSR0_INEA | CSR0_STRT);
858                  return 1; /* ->OK */
859          }
860          printk(KERN_ERR "%s: can't init lance, status: %04x\n",dev->name,(int) inw(PORT+L_DATAREG));
861          flags=claim_dma_lock();
862          disable_dma(dev->dma);
863          release_dma_lock(flags);
864          return 0; /* ->Error */
865 }
866
867 /*
868  * interrupt handler
869  */
870 static irqreturn_t ni65_interrupt(int irq, void * dev_id)
871 {
872         int csr0 = 0;
873         struct net_device *dev = dev_id;
874         struct priv *p;
875         int bcnt = 32;
876
877         p = dev->ml_priv;
878
879         spin_lock(&p->ring_lock);
880
881         while(--bcnt) {
882                 csr0 = inw(PORT+L_DATAREG);
883
884 #if 0
885                 writedatareg( (csr0 & CSR0_CLRALL) ); /* ack interrupts, disable int. */
886 #else
887                 writedatareg( (csr0 & CSR0_CLRALL) | CSR0_INEA ); /* ack interrupts, interrupts enabled */
888 #endif
889
890                 if(!(csr0 & (CSR0_ERR | CSR0_RINT | CSR0_TINT)))
891                         break;
892
893                 if(csr0 & CSR0_RINT) /* RECV-int? */
894                         ni65_recv_intr(dev,csr0);
895                 if(csr0 & CSR0_TINT) /* XMIT-int? */
896                         ni65_xmit_intr(dev,csr0);
897
898                 if(csr0 & CSR0_ERR)
899                 {
900                         struct priv *p = dev->ml_priv;
901                         if(debuglevel > 1)
902                                 printk(KERN_ERR "%s: general error: %04x.\n",dev->name,csr0);
903                         if(csr0 & CSR0_BABL)
904                                 p->stats.tx_errors++;
905                         if(csr0 & CSR0_MISS) {
906                                 int i;
907                                 for(i=0;i<RMDNUM;i++)
908                                         printk("%02x ",p->rmdhead[i].u.s.status);
909                                 printk("\n");
910                                 p->stats.rx_errors++;
911                         }
912                         if(csr0 & CSR0_MERR) {
913                                 if(debuglevel > 1)
914                                         printk(KERN_ERR "%s: Ooops .. memory error: %04x.\n",dev->name,csr0);
915                                 ni65_stop_start(dev,p);
916                         }
917                 }
918         }
919
920 #ifdef RCV_PARANOIA_CHECK
921 {
922  int j;
923  for(j=0;j<RMDNUM;j++)
924  {
925         struct priv *p = dev->ml_priv;
926         int i,k,num1,num2;
927         for(i=RMDNUM-1;i>0;i--) {
928                  num2 = (p->rmdnum + i) & (RMDNUM-1);
929                  if(!(p->rmdhead[num2].u.s.status & RCV_OWN))
930                                 break;
931         }
932
933         if(i) {
934                 for(k=0;k<RMDNUM;k++) {
935                         num1 = (p->rmdnum + k) & (RMDNUM-1);
936                         if(!(p->rmdhead[num1].u.s.status & RCV_OWN))
937                                 break;
938                 }
939                 if(!k)
940                         break;
941
942                 if(debuglevel > 0)
943                 {
944                         char buf[256],*buf1;
945                         int k;
946                         buf1 = buf;
947                         for(k=0;k<RMDNUM;k++) {
948                                 sprintf(buf1,"%02x ",(p->rmdhead[k].u.s.status)); /* & RCV_OWN) ); */
949                                 buf1 += 3;
950                         }
951                         *buf1 = 0;
952                         printk(KERN_ERR "%s: Ooops, receive ring corrupted %2d %2d | %s\n",dev->name,p->rmdnum,i,buf);
953                 }
954
955                 p->rmdnum = num1;
956                 ni65_recv_intr(dev,csr0);
957                 if((p->rmdhead[num2].u.s.status & RCV_OWN))
958                         break;  /* ok, we are 'in sync' again */
959         }
960         else
961                 break;
962  }
963 }
964 #endif
965
966         if( (csr0 & (CSR0_RXON | CSR0_TXON)) != (CSR0_RXON | CSR0_TXON) ) {
967                 printk(KERN_DEBUG "%s: RX or TX was offline -> restart\n",dev->name);
968                 ni65_stop_start(dev,p);
969         }
970         else
971                 writedatareg(CSR0_INEA);
972
973         spin_unlock(&p->ring_lock);
974         return IRQ_HANDLED;
975 }
976
977 /*
978  * We have received an Xmit-Interrupt ..
979  * send a new packet if necessary
980  */
981 static void ni65_xmit_intr(struct net_device *dev,int csr0)
982 {
983         struct priv *p = dev->ml_priv;
984
985         while(p->xmit_queued)
986         {
987                 struct tmd *tmdp = p->tmdhead + p->tmdlast;
988                 int tmdstat = tmdp->u.s.status;
989
990                 if(tmdstat & XMIT_OWN)
991                         break;
992
993                 if(tmdstat & XMIT_ERR)
994                 {
995 #if 0
996                         if(tmdp->status2 & XMIT_TDRMASK && debuglevel > 3)
997                                 printk(KERN_ERR "%s: tdr-problems (e.g. no resistor)\n",dev->name);
998 #endif
999                  /* checking some errors */
1000                         if(tmdp->status2 & XMIT_RTRY)
1001                                 p->stats.tx_aborted_errors++;
1002                         if(tmdp->status2 & XMIT_LCAR)
1003                                 p->stats.tx_carrier_errors++;
1004                         if(tmdp->status2 & (XMIT_BUFF | XMIT_UFLO )) {
1005                 /* this stops the xmitter */
1006                                 p->stats.tx_fifo_errors++;
1007                                 if(debuglevel > 0)
1008                                         printk(KERN_ERR "%s: Xmit FIFO/BUFF error\n",dev->name);
1009                                 if(p->features & INIT_RING_BEFORE_START) {
1010                                         tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;    /* test: resend this frame */
1011                                         ni65_stop_start(dev,p);
1012                                         break;  /* no more Xmit processing .. */
1013                                 }
1014                                 else
1015                                  ni65_stop_start(dev,p);
1016                         }
1017                         if(debuglevel > 2)
1018                                 printk(KERN_ERR "%s: xmit-error: %04x %02x-%04x\n",dev->name,csr0,(int) tmdstat,(int) tmdp->status2);
1019                         if(!(csr0 & CSR0_BABL)) /* don't count errors twice */
1020                                 p->stats.tx_errors++;
1021                         tmdp->status2 = 0;
1022                 }
1023                 else {
1024                         p->stats.tx_bytes -= (short)(tmdp->blen);
1025                         p->stats.tx_packets++;
1026                 }
1027
1028 #ifdef XMT_VIA_SKB
1029                 if(p->tmd_skb[p->tmdlast]) {
1030                          dev_kfree_skb_irq(p->tmd_skb[p->tmdlast]);
1031                          p->tmd_skb[p->tmdlast] = NULL;
1032                 }
1033 #endif
1034
1035                 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
1036                 if(p->tmdlast == p->tmdnum)
1037                         p->xmit_queued = 0;
1038         }
1039         netif_wake_queue(dev);
1040 }
1041
1042 /*
1043  * We have received a packet
1044  */
1045 static void ni65_recv_intr(struct net_device *dev,int csr0)
1046 {
1047         struct rmd *rmdp;
1048         int rmdstat,len;
1049         int cnt=0;
1050         struct priv *p = dev->ml_priv;
1051
1052         rmdp = p->rmdhead + p->rmdnum;
1053         while(!( (rmdstat = rmdp->u.s.status) & RCV_OWN))
1054         {
1055                 cnt++;
1056                 if( (rmdstat & (RCV_START | RCV_END | RCV_ERR)) != (RCV_START | RCV_END) ) /* error or oversized? */
1057                 {
1058                         if(!(rmdstat & RCV_ERR)) {
1059                                 if(rmdstat & RCV_START)
1060                                 {
1061                                         p->stats.rx_length_errors++;
1062                                         printk(KERN_ERR "%s: recv, packet too long: %d\n",dev->name,rmdp->mlen & 0x0fff);
1063                                 }
1064                         }
1065                         else {
1066                                 if(debuglevel > 2)
1067                                         printk(KERN_ERR "%s: receive-error: %04x, lance-status: %04x/%04x\n",
1068                                                                         dev->name,(int) rmdstat,csr0,(int) inw(PORT+L_DATAREG) );
1069                                 if(rmdstat & RCV_FRAM)
1070                                         p->stats.rx_frame_errors++;
1071                                 if(rmdstat & RCV_OFLO)
1072                                         p->stats.rx_over_errors++;
1073                                 if(rmdstat & RCV_CRC)
1074                                         p->stats.rx_crc_errors++;
1075                                 if(rmdstat & RCV_BUF_ERR)
1076                                         p->stats.rx_fifo_errors++;
1077                         }
1078                         if(!(csr0 & CSR0_MISS)) /* don't count errors twice */
1079                                 p->stats.rx_errors++;
1080                 }
1081                 else if( (len = (rmdp->mlen & 0x0fff) - 4) >= 60)
1082                 {
1083 #ifdef RCV_VIA_SKB
1084                         struct sk_buff *skb = alloc_skb(R_BUF_SIZE+2+16,GFP_ATOMIC);
1085                         if (skb)
1086                                 skb_reserve(skb,16);
1087 #else
1088                         struct sk_buff *skb = dev_alloc_skb(len+2);
1089 #endif
1090                         if(skb)
1091                         {
1092                                 skb_reserve(skb,2);
1093 #ifdef RCV_VIA_SKB
1094                                 if( (unsigned long) (skb->data + R_BUF_SIZE) > 0x1000000) {
1095                                         skb_put(skb,len);
1096                                         skb_copy_to_linear_data(skb, (unsigned char *)(p->recv_skb[p->rmdnum]->data),len);
1097                                 }
1098                                 else {
1099                                         struct sk_buff *skb1 = p->recv_skb[p->rmdnum];
1100                                         skb_put(skb,R_BUF_SIZE);
1101                                         p->recv_skb[p->rmdnum] = skb;
1102                                         rmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1103                                         skb = skb1;
1104                                         skb_trim(skb,len);
1105                                 }
1106 #else
1107                                 skb_put(skb,len);
1108                                 skb_copy_to_linear_data(skb, (unsigned char *) p->recvbounce[p->rmdnum],len);
1109 #endif
1110                                 p->stats.rx_packets++;
1111                                 p->stats.rx_bytes += len;
1112                                 skb->protocol=eth_type_trans(skb,dev);
1113                                 netif_rx(skb);
1114                         }
1115                         else
1116                         {
1117                                 printk(KERN_ERR "%s: can't alloc new sk_buff\n",dev->name);
1118                                 p->stats.rx_dropped++;
1119                         }
1120                 }
1121                 else {
1122                         printk(KERN_INFO "%s: received runt packet\n",dev->name);
1123                         p->stats.rx_errors++;
1124                 }
1125                 rmdp->blen = -(R_BUF_SIZE-8);
1126                 rmdp->mlen = 0;
1127                 rmdp->u.s.status = RCV_OWN; /* change owner */
1128                 p->rmdnum = (p->rmdnum + 1) & (RMDNUM-1);
1129                 rmdp = p->rmdhead + p->rmdnum;
1130         }
1131 }
1132
1133 /*
1134  * kick xmitter ..
1135  */
1136
1137 static void ni65_timeout(struct net_device *dev)
1138 {
1139         int i;
1140         struct priv *p = dev->ml_priv;
1141
1142         printk(KERN_ERR "%s: xmitter timed out, try to restart!\n",dev->name);
1143         for(i=0;i<TMDNUM;i++)
1144                 printk("%02x ",p->tmdhead[i].u.s.status);
1145         printk("\n");
1146         ni65_lance_reinit(dev);
1147         dev->trans_start = jiffies;
1148         netif_wake_queue(dev);
1149 }
1150
1151 /*
1152  *      Send a packet
1153  */
1154
1155 static int ni65_send_packet(struct sk_buff *skb, struct net_device *dev)
1156 {
1157         struct priv *p = dev->ml_priv;
1158
1159         netif_stop_queue(dev);
1160
1161         if (test_and_set_bit(0, (void*)&p->lock)) {
1162                 printk(KERN_ERR "%s: Queue was locked.\n", dev->name);
1163                 return 1;
1164         }
1165
1166         {
1167                 short len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1168                 struct tmd *tmdp;
1169                 unsigned long flags;
1170
1171 #ifdef XMT_VIA_SKB
1172                 if( (unsigned long) (skb->data + skb->len) > 0x1000000) {
1173 #endif
1174
1175                         skb_copy_from_linear_data(skb, p->tmdbounce[p->tmdbouncenum],
1176                                       skb->len > T_BUF_SIZE ? T_BUF_SIZE :
1177                                                               skb->len);
1178                         if (len > skb->len)
1179                                 memset((char *)p->tmdbounce[p->tmdbouncenum]+skb->len, 0, len-skb->len);
1180                         dev_kfree_skb (skb);
1181
1182                         spin_lock_irqsave(&p->ring_lock, flags);
1183                         tmdp = p->tmdhead + p->tmdnum;
1184                         tmdp->u.buffer = (u32) isa_virt_to_bus(p->tmdbounce[p->tmdbouncenum]);
1185                         p->tmdbouncenum = (p->tmdbouncenum + 1) & (TMDNUM - 1);
1186
1187 #ifdef XMT_VIA_SKB
1188                 }
1189                 else {
1190                         spin_lock_irqsave(&p->ring_lock, flags);
1191
1192                         tmdp = p->tmdhead + p->tmdnum;
1193                         tmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1194                         p->tmd_skb[p->tmdnum] = skb;
1195                 }
1196 #endif
1197                 tmdp->blen = -len;
1198
1199                 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;
1200                 writedatareg(CSR0_TDMD | CSR0_INEA); /* enable xmit & interrupt */
1201
1202                 p->xmit_queued = 1;
1203                 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
1204
1205                 if(p->tmdnum != p->tmdlast)
1206                         netif_wake_queue(dev);
1207
1208                 p->lock = 0;
1209                 dev->trans_start = jiffies;
1210
1211                 spin_unlock_irqrestore(&p->ring_lock, flags);
1212         }
1213
1214         return 0;
1215 }
1216
1217 static struct net_device_stats *ni65_get_stats(struct net_device *dev)
1218 {
1219
1220 #if 0
1221         int i;
1222         struct priv *p = dev->ml_priv;
1223         for(i=0;i<RMDNUM;i++)
1224         {
1225                 struct rmd *rmdp = p->rmdhead + ((p->rmdnum + i) & (RMDNUM-1));
1226                 printk("%02x ",rmdp->u.s.status);
1227         }
1228         printk("\n");
1229 #endif
1230
1231         return &((struct priv *)dev->ml_priv)->stats;
1232 }
1233
1234 static void set_multicast_list(struct net_device *dev)
1235 {
1236         if(!ni65_lance_reinit(dev))
1237                 printk(KERN_ERR "%s: Can't switch card into MC mode!\n",dev->name);
1238         netif_wake_queue(dev);
1239 }
1240
1241 #ifdef MODULE
1242 static struct net_device *dev_ni65;
1243
1244 module_param(irq, int, 0);
1245 module_param(io, int, 0);
1246 module_param(dma, int, 0);
1247 MODULE_PARM_DESC(irq, "ni6510 IRQ number (ignored for some cards)");
1248 MODULE_PARM_DESC(io, "ni6510 I/O base address");
1249 MODULE_PARM_DESC(dma, "ni6510 ISA DMA channel (ignored for some cards)");
1250
1251 int __init init_module(void)
1252 {
1253         dev_ni65 = ni65_probe(-1);
1254         return IS_ERR(dev_ni65) ? PTR_ERR(dev_ni65) : 0;
1255 }
1256
1257 void __exit cleanup_module(void)
1258 {
1259         unregister_netdev(dev_ni65);
1260         cleanup_card(dev_ni65);
1261         free_netdev(dev_ni65);
1262 }
1263 #endif /* MODULE */
1264
1265 MODULE_LICENSE("GPL");