More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / ni65.c
blobd19f909c1d86f5b69b0bbb83d133771da98a7cfe
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
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)
10 * To compile as module, type:
11 * gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ -DMODULE -c ni65.c
12 * driver probes: io: 0x360,0x300,0x320,0x340 / dma: 3,5,6,7
14 * This is an extension to the Linux operating system, and is covered by the
15 * same GNU General Public License that covers the Linux-kernel.
17 * comments/bugs/suggestions can be sent to:
18 * Michael Hipp
19 * email: hippm@informatik.uni-tuebingen.de
21 * sources:
22 * some things are from the 'ni6510-packet-driver for dos by Russ Nelson'
23 * and from the original drivers by D.Becker
25 * known problems:
26 * - on some PCI boards (including my own) the card/board/ISA-bridge has
27 * problems with bus master DMA. This results in lotsa overruns.
28 * It may help to '#define RCV_PARANOIA_CHECK' or try to #undef
29 * the XMT and RCV_VIA_SKB option .. this reduces driver performance.
30 * Or just play with your BIOS options to optimize ISA-DMA access.
31 * Maybe you also wanna play with the LOW_PERFORAMCE and MID_PERFORMANCE
32 * defines -> please report me your experience then
33 * - Harald reported for ASUS SP3G mainboards, that you should use
34 * the 'optimal settings' from the user's manual on page 3-12!
36 * credits:
37 * thanx to Jason Sullivan for sending me a ni6510 card!
38 * lot of debug runs with ASUS SP3G Boards (Intel Saturn) by Harald Koenig
40 * simple performance test: (486DX-33/Ni6510-EB receives from 486DX4-100/Ni6510-EB)
41 * average: FTP -> 8384421 bytes received in 8.5 seconds
42 * (no RCV_VIA_SKB,no XMT_VIA_SKB,PARANOIA_CHECK,4 XMIT BUFS, 8 RCV_BUFFS)
43 * peak: FTP -> 8384421 bytes received in 7.5 seconds
44 * (RCV_VIA_SKB,XMT_VIA_SKB,no PARANOIA_CHECK,1(!) XMIT BUF, 16 RCV BUFFS)
48 * 99.Jun.8: added support for /proc/net/dev byte count for xosview (HK)
49 * 96.Sept.29: virt_to_bus stuff added for new memory modell
50 * 96.April.29: Added Harald Koenig's Patches (MH)
51 * 96.April.13: enhanced error handling .. more tests (MH)
52 * 96.April.5/6: a lot of performance tests. Got it stable now (hopefully) (MH)
53 * 96.April.1: (no joke ;) .. added EtherBlaster and Module support (MH)
54 * 96.Feb.19: fixed a few bugs .. cleanups .. tested for 1.3.66 (MH)
55 * hopefully no more 16MB limit
57 * 95.Nov.18: multicast tweaked (AC).
59 * 94.Aug.22: changes in xmit_intr (ack more than one xmitted-packet), ni65_send_packet (p->lock) (MH)
61 * 94.July.16: fixed bugs in recv_skb and skb-alloc stuff (MH)
64 #include <linux/kernel.h>
65 #include <linux/string.h>
66 #include <linux/errno.h>
67 #include <linux/ioport.h>
68 #include <linux/slab.h>
69 #include <linux/interrupt.h>
70 #include <linux/delay.h>
71 #include <linux/init.h>
72 #include <linux/netdevice.h>
73 #include <linux/etherdevice.h>
74 #include <linux/skbuff.h>
75 #include <linux/version.h>
76 #include <linux/module.h>
78 #include <asm/bitops.h>
79 #include <asm/io.h>
80 #include <asm/dma.h>
82 #include "ni65.h"
85 * the current setting allows an acceptable performance
86 * for 'RCV_PARANOIA_CHECK' read the 'known problems' part in
87 * the header of this file
88 * 'invert' the defines for max. performance. This may cause DMA problems
89 * on some boards (e.g on my ASUS SP3G)
91 #undef XMT_VIA_SKB
92 #undef RCV_VIA_SKB
93 #define RCV_PARANOIA_CHECK
95 #define MID_PERFORMANCE
97 #if defined( LOW_PERFORMANCE )
98 static int isa0=7,isa1=7,csr80=0x0c10;
99 #elif defined( MID_PERFORMANCE )
100 static int isa0=5,isa1=5,csr80=0x2810;
101 #else /* high performance */
102 static int isa0=4,isa1=4,csr80=0x0017;
103 #endif
106 * a few card/vendor specific defines
108 #define NI65_ID0 0x00
109 #define NI65_ID1 0x55
110 #define NI65_EB_ID0 0x52
111 #define NI65_EB_ID1 0x44
112 #define NE2100_ID0 0x57
113 #define NE2100_ID1 0x57
115 #define PORT p->cmdr_addr
118 * buffer configuration
120 #if 1
121 #define RMDNUM 16
122 #define RMDNUMMASK 0x80000000
123 #else
124 #define RMDNUM 8
125 #define RMDNUMMASK 0x60000000 /* log2(RMDNUM)<<29 */
126 #endif
128 #if 0
129 #define TMDNUM 1
130 #define TMDNUMMASK 0x00000000
131 #else
132 #define TMDNUM 4
133 #define TMDNUMMASK 0x40000000 /* log2(TMDNUM)<<29 */
134 #endif
136 /* slightly oversized */
137 #define R_BUF_SIZE 1544
138 #define T_BUF_SIZE 1544
141 * lance register defines
143 #define L_DATAREG 0x00
144 #define L_ADDRREG 0x02
145 #define L_RESET 0x04
146 #define L_CONFIG 0x05
147 #define L_BUSIF 0x06
150 * to access the lance/am7990-regs, you have to write
151 * reg-number into L_ADDRREG, then you can access it using L_DATAREG
153 #define CSR0 0x00
154 #define CSR1 0x01
155 #define CSR2 0x02
156 #define CSR3 0x03
158 #define INIT_RING_BEFORE_START 0x1
159 #define FULL_RESET_ON_ERROR 0x2
161 #if 0
162 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);inw(PORT+L_ADDRREG); \
163 outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
164 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_ADDRREG),\
165 inw(PORT+L_DATAREG))
166 #if 0
167 #define writedatareg(val) {outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
168 #else
169 #define writedatareg(val) { writereg(val,CSR0); }
170 #endif
171 #else
172 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);outw(val,PORT+L_DATAREG);}
173 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_DATAREG))
174 #define writedatareg(val) { writereg(val,CSR0); }
175 #endif
177 static unsigned char ni_vendor[] = { 0x02,0x07,0x01 };
179 static struct card {
180 unsigned char id0,id1;
181 short id_offset;
182 short total_size;
183 short cmd_offset;
184 short addr_offset;
185 unsigned char *vendor_id;
186 char *cardname;
187 long config;
188 } cards[] = {
190 .id0 = NI65_ID0,
191 .id1 = NI65_ID1,
192 .id_offset = 0x0e,
193 .total_size = 0x10,
194 .cmd_offset = 0x0,
195 .addr_offset = 0x8,
196 .vendor_id = ni_vendor,
197 .cardname = "ni6510",
198 .config = 0x1,
201 .id0 = NI65_EB_ID0,
202 .id1 = NI65_EB_ID1,
203 .id_offset = 0x0e,
204 .total_size = 0x18,
205 .cmd_offset = 0x10,
206 .addr_offset = 0x0,
207 .vendor_id = ni_vendor,
208 .cardname = "ni6510 EtherBlaster",
209 .config = 0x2,
212 .id0 = NE2100_ID0,
213 .id1 = NE2100_ID1,
214 .id_offset = 0x0e,
215 .total_size = 0x18,
216 .cmd_offset = 0x10,
217 .addr_offset = 0x0,
218 .vendor_id = NULL,
219 .cardname = "generic NE2100",
220 .config = 0x0,
223 #define NUM_CARDS 3
225 struct priv
227 struct rmd rmdhead[RMDNUM];
228 struct tmd tmdhead[TMDNUM];
229 struct init_block ib;
230 int rmdnum;
231 int tmdnum,tmdlast;
232 #ifdef RCV_VIA_SKB
233 struct sk_buff *recv_skb[RMDNUM];
234 #else
235 void *recvbounce[RMDNUM];
236 #endif
237 #ifdef XMT_VIA_SKB
238 struct sk_buff *tmd_skb[TMDNUM];
239 #endif
240 void *tmdbounce[TMDNUM];
241 int tmdbouncenum;
242 int lock,xmit_queued;
243 struct net_device_stats stats;
244 void *self;
245 int cmdr_addr;
246 int cardno;
247 int features;
250 static int ni65_probe1(struct net_device *dev,int);
251 static irqreturn_t ni65_interrupt(int irq, void * dev_id, struct pt_regs *regs);
252 static void ni65_recv_intr(struct net_device *dev,int);
253 static void ni65_xmit_intr(struct net_device *dev,int);
254 static int ni65_open(struct net_device *dev);
255 static int ni65_lance_reinit(struct net_device *dev);
256 static void ni65_init_lance(struct priv *p,unsigned char*,int,int);
257 static int ni65_send_packet(struct sk_buff *skb, struct net_device *dev);
258 static void ni65_timeout(struct net_device *dev);
259 static int ni65_close(struct net_device *dev);
260 static int ni65_alloc_buffer(struct net_device *dev);
261 static void ni65_free_buffer(struct priv *p);
262 static struct net_device_stats *ni65_get_stats(struct net_device *);
263 static void set_multicast_list(struct net_device *dev);
265 static int irqtab[] __initdata = { 9,12,15,5 }; /* irq config-translate */
266 static int dmatab[] __initdata = { 0,3,5,6,7 }; /* dma config-translate and autodetect */
268 static int debuglevel = 1;
271 * set 'performance' registers .. we must STOP lance for that
273 static void ni65_set_performance(struct priv *p)
275 writereg(CSR0_STOP | CSR0_CLRALL,CSR0); /* STOP */
277 if( !(cards[p->cardno].config & 0x02) )
278 return;
280 outw(80,PORT+L_ADDRREG);
281 if(inw(PORT+L_ADDRREG) != 80)
282 return;
284 writereg( (csr80 & 0x3fff) ,80); /* FIFO watermarks */
285 outw(0,PORT+L_ADDRREG);
286 outw((short)isa0,PORT+L_BUSIF); /* write ISA 0: DMA_R : isa0 * 50ns */
287 outw(1,PORT+L_ADDRREG);
288 outw((short)isa1,PORT+L_BUSIF); /* write ISA 1: DMA_W : isa1 * 50ns */
290 outw(CSR0,PORT+L_ADDRREG); /* switch back to CSR0 */
294 * open interface (up)
296 static int ni65_open(struct net_device *dev)
298 struct priv *p = (struct priv *) dev->priv;
299 int irqval = request_irq(dev->irq, &ni65_interrupt,0,
300 cards[p->cardno].cardname,dev);
301 if (irqval) {
302 printk ("%s: unable to get IRQ %d (irqval=%d).\n",
303 dev->name,dev->irq, irqval);
304 return -EAGAIN;
307 if(ni65_lance_reinit(dev))
309 netif_start_queue(dev);
310 return 0;
312 else
314 free_irq(dev->irq,dev);
315 return -EAGAIN;
320 * close interface (down)
322 static int ni65_close(struct net_device *dev)
324 struct priv *p = (struct priv *) dev->priv;
326 netif_stop_queue(dev);
328 outw(inw(PORT+L_RESET),PORT+L_RESET); /* that's the hard way */
330 #ifdef XMT_VIA_SKB
332 int i;
333 for(i=0;i<TMDNUM;i++)
335 if(p->tmd_skb[i]) {
336 dev_kfree_skb(p->tmd_skb[i]);
337 p->tmd_skb[i] = NULL;
341 #endif
342 free_irq(dev->irq,dev);
343 return 0;
347 * Probe The Card (not the lance-chip)
349 #ifdef MODULE
350 static
351 #endif
352 int __init ni65_probe(struct net_device *dev)
354 int *port;
355 static int ports[] = {0x360,0x300,0x320,0x340, 0};
357 if (dev->base_addr > 0x1ff) /* Check a single specified location. */
358 return ni65_probe1(dev, dev->base_addr);
359 else if (dev->base_addr > 0) /* Don't probe at all. */
360 return -ENXIO;
362 for (port = ports; *port; port++)
364 if (ni65_probe1(dev, *port) == 0)
365 return 0;
368 return -ENODEV;
372 * this is the real card probe ..
374 static int __init ni65_probe1(struct net_device *dev,int ioaddr)
376 int i,j;
377 struct priv *p;
378 unsigned long flags;
380 for(i=0;i<NUM_CARDS;i++) {
381 if(!request_region(ioaddr, cards[i].total_size, cards[i].cardname))
382 continue;
383 if(cards[i].id_offset >= 0) {
384 if(inb(ioaddr+cards[i].id_offset+0) != cards[i].id0 ||
385 inb(ioaddr+cards[i].id_offset+1) != cards[i].id1) {
386 release_region(ioaddr, cards[i].total_size);
387 continue;
390 if(cards[i].vendor_id) {
391 for(j=0;j<3;j++)
392 if(inb(ioaddr+cards[i].addr_offset+j) != cards[i].vendor_id[j]) {
393 release_region(ioaddr, cards[i].total_size);
394 continue;
397 break;
399 if(i == NUM_CARDS)
400 return -ENODEV;
402 for(j=0;j<6;j++)
403 dev->dev_addr[j] = inb(ioaddr+cards[i].addr_offset+j);
405 if( (j=ni65_alloc_buffer(dev)) < 0) {
406 release_region(ioaddr, cards[i].total_size);
407 return j;
409 p = (struct priv *) dev->priv;
410 p->cmdr_addr = ioaddr + cards[i].cmd_offset;
411 p->cardno = i;
413 printk("%s: %s found at %#3x, ", dev->name, cards[p->cardno].cardname , ioaddr);
415 outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
416 if( (j=readreg(CSR0)) != 0x4) {
417 printk(KERN_ERR "can't RESET card: %04x\n",j);
418 ni65_free_buffer(p);
419 release_region(ioaddr, cards[p->cardno].total_size);
420 return -EAGAIN;
423 outw(88,PORT+L_ADDRREG);
424 if(inw(PORT+L_ADDRREG) == 88) {
425 unsigned long v;
426 v = inw(PORT+L_DATAREG);
427 v <<= 16;
428 outw(89,PORT+L_ADDRREG);
429 v |= inw(PORT+L_DATAREG);
430 printk("Version %#08lx, ",v);
431 p->features = INIT_RING_BEFORE_START;
433 else {
434 printk("ancient LANCE, ");
435 p->features = 0x0;
438 if(test_bit(0,&cards[i].config)) {
439 dev->irq = irqtab[(inw(ioaddr+L_CONFIG)>>2)&3];
440 dev->dma = dmatab[inw(ioaddr+L_CONFIG)&3];
441 printk("IRQ %d (from card), DMA %d (from card).\n",dev->irq,dev->dma);
443 else {
444 if(dev->dma == 0) {
445 /* 'stuck test' from lance.c */
446 long dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
447 (inb(DMA2_STAT_REG) & 0xf0);
448 for(i=1;i<5;i++) {
449 int dma = dmatab[i];
450 if(test_bit(dma,&dma_channels) || request_dma(dma,"ni6510"))
451 continue;
453 flags=claim_dma_lock();
454 disable_dma(dma);
455 set_dma_mode(dma,DMA_MODE_CASCADE);
456 enable_dma(dma);
457 release_dma_lock(flags);
459 ni65_init_lance(p,dev->dev_addr,0,0); /* trigger memory access */
461 flags=claim_dma_lock();
462 disable_dma(dma);
463 free_dma(dma);
464 release_dma_lock(flags);
466 if(readreg(CSR0) & CSR0_IDON)
467 break;
469 if(i == 5) {
470 printk("Can't detect DMA channel!\n");
471 ni65_free_buffer(p);
472 release_region(ioaddr, cards[p->cardno].total_size);
473 return -EAGAIN;
475 dev->dma = dmatab[i];
476 printk("DMA %d (autodetected), ",dev->dma);
478 else
479 printk("DMA %d (assigned), ",dev->dma);
481 if(dev->irq < 2)
483 unsigned long irq_mask, delay;
485 ni65_init_lance(p,dev->dev_addr,0,0);
486 irq_mask = probe_irq_on();
487 writereg(CSR0_INIT|CSR0_INEA,CSR0); /* trigger interrupt */
488 delay = jiffies + HZ/50;
489 while (time_before(jiffies, delay)) ;
490 dev->irq = probe_irq_off(irq_mask);
491 if(!dev->irq)
493 printk("Failed to detect IRQ line!\n");
494 ni65_free_buffer(p);
495 release_region(ioaddr, cards[p->cardno].total_size);
496 return -EAGAIN;
498 printk("IRQ %d (autodetected).\n",dev->irq);
500 else
501 printk("IRQ %d (assigned).\n",dev->irq);
504 if(request_dma(dev->dma, cards[p->cardno].cardname ) != 0)
506 printk("%s: Can't request dma-channel %d\n",dev->name,(int) dev->dma);
507 ni65_free_buffer(p);
508 release_region(ioaddr, cards[p->cardno].total_size);
509 return -EAGAIN;
512 dev->base_addr = ioaddr;
513 SET_MODULE_OWNER(dev);
514 dev->open = ni65_open;
515 dev->stop = ni65_close;
516 dev->hard_start_xmit = ni65_send_packet;
517 dev->tx_timeout = ni65_timeout;
518 dev->watchdog_timeo = HZ/2;
519 dev->get_stats = ni65_get_stats;
520 dev->set_multicast_list = set_multicast_list;
522 ether_setup(dev);
524 return 0; /* everything is OK */
528 * set lance register and trigger init
530 static void ni65_init_lance(struct priv *p,unsigned char *daddr,int filter,int mode)
532 int i;
533 u32 pib;
535 writereg(CSR0_CLRALL|CSR0_STOP,CSR0);
537 for(i=0;i<6;i++)
538 p->ib.eaddr[i] = daddr[i];
540 for(i=0;i<8;i++)
541 p->ib.filter[i] = filter;
542 p->ib.mode = mode;
544 p->ib.trp = (u32) isa_virt_to_bus(p->tmdhead) | TMDNUMMASK;
545 p->ib.rrp = (u32) isa_virt_to_bus(p->rmdhead) | RMDNUMMASK;
546 writereg(0,CSR3); /* busmaster/no word-swap */
547 pib = (u32) isa_virt_to_bus(&p->ib);
548 writereg(pib & 0xffff,CSR1);
549 writereg(pib >> 16,CSR2);
551 writereg(CSR0_INIT,CSR0); /* this changes L_ADDRREG to CSR0 */
553 for(i=0;i<32;i++)
555 mdelay(4);
556 if(inw(PORT+L_DATAREG) & (CSR0_IDON | CSR0_MERR) )
557 break; /* init ok ? */
562 * allocate memory area and check the 16MB border
564 static void *ni65_alloc_mem(struct net_device *dev,char *what,int size,int type)
566 struct sk_buff *skb=NULL;
567 unsigned char *ptr;
568 void *ret;
570 if(type) {
571 ret = skb = alloc_skb(2+16+size,GFP_KERNEL|GFP_DMA);
572 if(!skb) {
573 printk("%s: unable to allocate %s memory.\n",dev->name,what);
574 return NULL;
576 skb->dev = dev;
577 skb_reserve(skb,2+16);
578 skb_put(skb,R_BUF_SIZE); /* grab the whole space .. (not necessary) */
579 ptr = skb->data;
581 else {
582 ret = ptr = kmalloc(T_BUF_SIZE,GFP_KERNEL | GFP_DMA);
583 if(!ret) {
584 printk("%s: unable to allocate %s memory.\n",dev->name,what);
585 return NULL;
588 if( (u32) virt_to_phys(ptr+size) > 0x1000000) {
589 printk("%s: unable to allocate %s memory in lower 16MB!\n",dev->name,what);
590 if(type)
591 kfree_skb(skb);
592 else
593 kfree(ptr);
594 return NULL;
596 return ret;
600 * allocate all memory structures .. send/recv buffers etc ...
602 static int ni65_alloc_buffer(struct net_device *dev)
604 unsigned char *ptr;
605 struct priv *p;
606 int i;
609 * we need 8-aligned memory ..
611 ptr = ni65_alloc_mem(dev,"BUFFER",sizeof(struct priv)+8,0);
612 if(!ptr)
613 return -ENOMEM;
615 p = dev->priv = (struct priv *) (((unsigned long) ptr + 7) & ~0x7);
616 memset((char *) dev->priv,0,sizeof(struct priv));
617 p->self = ptr;
619 for(i=0;i<TMDNUM;i++)
621 #ifdef XMT_VIA_SKB
622 p->tmd_skb[i] = NULL;
623 #endif
624 p->tmdbounce[i] = ni65_alloc_mem(dev,"XMIT",T_BUF_SIZE,0);
625 if(!p->tmdbounce[i]) {
626 ni65_free_buffer(p);
627 return -ENOMEM;
631 for(i=0;i<RMDNUM;i++)
633 #ifdef RCV_VIA_SKB
634 p->recv_skb[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,1);
635 if(!p->recv_skb[i]) {
636 ni65_free_buffer(p);
637 return -ENOMEM;
639 #else
640 p->recvbounce[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,0);
641 if(!p->recvbounce[i]) {
642 ni65_free_buffer(p);
643 return -ENOMEM;
645 #endif
648 return 0; /* everything is OK */
652 * free buffers and private struct
654 static void ni65_free_buffer(struct priv *p)
656 int i;
658 if(!p)
659 return;
661 for(i=0;i<TMDNUM;i++) {
662 if(p->tmdbounce[i])
663 kfree(p->tmdbounce[i]);
664 #ifdef XMT_VIA_SKB
665 if(p->tmd_skb[i])
666 dev_kfree_skb(p->tmd_skb[i]);
667 #endif
670 for(i=0;i<RMDNUM;i++)
672 #ifdef RCV_VIA_SKB
673 if(p->recv_skb[i])
674 dev_kfree_skb(p->recv_skb[i]);
675 #else
676 if(p->recvbounce[i])
677 kfree(p->recvbounce[i]);
678 #endif
680 if(p->self)
681 kfree(p->self);
686 * stop and (re)start lance .. e.g after an error
688 static void ni65_stop_start(struct net_device *dev,struct priv *p)
690 int csr0 = CSR0_INEA;
692 writedatareg(CSR0_STOP);
694 if(debuglevel > 1)
695 printk("ni65_stop_start\n");
697 if(p->features & INIT_RING_BEFORE_START) {
698 int i;
699 #ifdef XMT_VIA_SKB
700 struct sk_buff *skb_save[TMDNUM];
701 #endif
702 unsigned long buffer[TMDNUM];
703 short blen[TMDNUM];
705 if(p->xmit_queued) {
706 while(1) {
707 if((p->tmdhead[p->tmdlast].u.s.status & XMIT_OWN))
708 break;
709 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
710 if(p->tmdlast == p->tmdnum)
711 break;
715 for(i=0;i<TMDNUM;i++) {
716 struct tmd *tmdp = p->tmdhead + i;
717 #ifdef XMT_VIA_SKB
718 skb_save[i] = p->tmd_skb[i];
719 #endif
720 buffer[i] = (u32) isa_bus_to_virt(tmdp->u.buffer);
721 blen[i] = tmdp->blen;
722 tmdp->u.s.status = 0x0;
725 for(i=0;i<RMDNUM;i++) {
726 struct rmd *rmdp = p->rmdhead + i;
727 rmdp->u.s.status = RCV_OWN;
729 p->tmdnum = p->xmit_queued = 0;
730 writedatareg(CSR0_STRT | csr0);
732 for(i=0;i<TMDNUM;i++) {
733 int num = (i + p->tmdlast) & (TMDNUM-1);
734 p->tmdhead[i].u.buffer = (u32) isa_virt_to_bus((char *)buffer[num]); /* status is part of buffer field */
735 p->tmdhead[i].blen = blen[num];
736 if(p->tmdhead[i].u.s.status & XMIT_OWN) {
737 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
738 p->xmit_queued = 1;
739 writedatareg(CSR0_TDMD | CSR0_INEA | csr0);
741 #ifdef XMT_VIA_SKB
742 p->tmd_skb[i] = skb_save[num];
743 #endif
745 p->rmdnum = p->tmdlast = 0;
746 if(!p->lock)
747 if (p->tmdnum || !p->xmit_queued)
748 netif_wake_queue(dev);
749 dev->trans_start = jiffies;
751 else
752 writedatareg(CSR0_STRT | csr0);
756 * init lance (write init-values .. init-buffers) (open-helper)
758 static int ni65_lance_reinit(struct net_device *dev)
760 int i;
761 struct priv *p = (struct priv *) dev->priv;
762 unsigned long flags;
764 p->lock = 0;
765 p->xmit_queued = 0;
767 flags=claim_dma_lock();
768 disable_dma(dev->dma); /* I've never worked with dma, but we do it like the packetdriver */
769 set_dma_mode(dev->dma,DMA_MODE_CASCADE);
770 enable_dma(dev->dma);
771 release_dma_lock(flags);
773 outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
774 if( (i=readreg(CSR0) ) != 0x4)
776 printk(KERN_ERR "%s: can't RESET %s card: %04x\n",dev->name,
777 cards[p->cardno].cardname,(int) i);
778 flags=claim_dma_lock();
779 disable_dma(dev->dma);
780 release_dma_lock(flags);
781 return 0;
784 p->rmdnum = p->tmdnum = p->tmdlast = p->tmdbouncenum = 0;
785 for(i=0;i<TMDNUM;i++)
787 struct tmd *tmdp = p->tmdhead + i;
788 #ifdef XMT_VIA_SKB
789 if(p->tmd_skb[i]) {
790 dev_kfree_skb(p->tmd_skb[i]);
791 p->tmd_skb[i] = NULL;
793 #endif
794 tmdp->u.buffer = 0x0;
795 tmdp->u.s.status = XMIT_START | XMIT_END;
796 tmdp->blen = tmdp->status2 = 0;
799 for(i=0;i<RMDNUM;i++)
801 struct rmd *rmdp = p->rmdhead + i;
802 #ifdef RCV_VIA_SKB
803 rmdp->u.buffer = (u32) isa_virt_to_bus(p->recv_skb[i]->data);
804 #else
805 rmdp->u.buffer = (u32) isa_virt_to_bus(p->recvbounce[i]);
806 #endif
807 rmdp->blen = -(R_BUF_SIZE-8);
808 rmdp->mlen = 0;
809 rmdp->u.s.status = RCV_OWN;
812 if(dev->flags & IFF_PROMISC)
813 ni65_init_lance(p,dev->dev_addr,0x00,M_PROM);
814 else if(dev->mc_count || dev->flags & IFF_ALLMULTI)
815 ni65_init_lance(p,dev->dev_addr,0xff,0x0);
816 else
817 ni65_init_lance(p,dev->dev_addr,0x00,0x00);
820 * ni65_set_lance_mem() sets L_ADDRREG to CSR0
821 * NOW, WE WILL NEVER CHANGE THE L_ADDRREG, CSR0 IS ALWAYS SELECTED
824 if(inw(PORT+L_DATAREG) & CSR0_IDON) {
825 ni65_set_performance(p);
826 /* init OK: start lance , enable interrupts */
827 writedatareg(CSR0_CLRALL | CSR0_INEA | CSR0_STRT);
828 return 1; /* ->OK */
830 printk(KERN_ERR "%s: can't init lance, status: %04x\n",dev->name,(int) inw(PORT+L_DATAREG));
831 flags=claim_dma_lock();
832 disable_dma(dev->dma);
833 release_dma_lock(flags);
834 return 0; /* ->Error */
838 * interrupt handler
840 static irqreturn_t ni65_interrupt(int irq, void * dev_id, struct pt_regs * regs)
842 int csr0 = 0;
843 struct net_device *dev = dev_id;
844 struct priv *p;
845 int bcnt = 32;
847 p = (struct priv *) dev->priv;
849 while(--bcnt) {
850 csr0 = inw(PORT+L_DATAREG);
852 #if 0
853 writedatareg( (csr0 & CSR0_CLRALL) ); /* ack interrupts, disable int. */
854 #else
855 writedatareg( (csr0 & CSR0_CLRALL) | CSR0_INEA ); /* ack interrupts, interrupts enabled */
856 #endif
858 if(!(csr0 & (CSR0_ERR | CSR0_RINT | CSR0_TINT)))
859 break;
861 if(csr0 & CSR0_RINT) /* RECV-int? */
862 ni65_recv_intr(dev,csr0);
863 if(csr0 & CSR0_TINT) /* XMIT-int? */
864 ni65_xmit_intr(dev,csr0);
866 if(csr0 & CSR0_ERR)
868 struct priv *p = (struct priv *) dev->priv;
869 if(debuglevel > 1)
870 printk("%s: general error: %04x.\n",dev->name,csr0);
871 if(csr0 & CSR0_BABL)
872 p->stats.tx_errors++;
873 if(csr0 & CSR0_MISS) {
874 int i;
875 for(i=0;i<RMDNUM;i++)
876 printk("%02x ",p->rmdhead[i].u.s.status);
877 printk("\n");
878 p->stats.rx_errors++;
880 if(csr0 & CSR0_MERR) {
881 if(debuglevel > 1)
882 printk("%s: Ooops .. memory error: %04x.\n",dev->name,csr0);
883 ni65_stop_start(dev,p);
888 #ifdef RCV_PARANOIA_CHECK
890 int j;
891 for(j=0;j<RMDNUM;j++)
893 struct priv *p = (struct priv *) dev->priv;
894 int i,k,num1,num2;
895 for(i=RMDNUM-1;i>0;i--) {
896 num2 = (p->rmdnum + i) & (RMDNUM-1);
897 if(!(p->rmdhead[num2].u.s.status & RCV_OWN))
898 break;
901 if(i) {
902 for(k=0;k<RMDNUM;k++) {
903 num1 = (p->rmdnum + k) & (RMDNUM-1);
904 if(!(p->rmdhead[num1].u.s.status & RCV_OWN))
905 break;
907 if(!k)
908 break;
910 if(debuglevel > 0)
912 char buf[256],*buf1;
913 int k;
914 buf1 = buf;
915 for(k=0;k<RMDNUM;k++) {
916 sprintf(buf1,"%02x ",(p->rmdhead[k].u.s.status)); /* & RCV_OWN) ); */
917 buf1 += 3;
919 *buf1 = 0;
920 printk(KERN_ERR "%s: Ooops, receive ring corrupted %2d %2d | %s\n",dev->name,p->rmdnum,i,buf);
923 p->rmdnum = num1;
924 ni65_recv_intr(dev,csr0);
925 if((p->rmdhead[num2].u.s.status & RCV_OWN))
926 break; /* ok, we are 'in sync' again */
928 else
929 break;
932 #endif
934 if( (csr0 & (CSR0_RXON | CSR0_TXON)) != (CSR0_RXON | CSR0_TXON) ) {
935 printk("%s: RX or TX was offline -> restart\n",dev->name);
936 ni65_stop_start(dev,p);
938 else
939 writedatareg(CSR0_INEA);
941 return IRQ_HANDLED;
945 * We have received an Xmit-Interrupt ..
946 * send a new packet if necessary
948 static void ni65_xmit_intr(struct net_device *dev,int csr0)
950 struct priv *p = (struct priv *) dev->priv;
952 while(p->xmit_queued)
954 struct tmd *tmdp = p->tmdhead + p->tmdlast;
955 int tmdstat = tmdp->u.s.status;
957 if(tmdstat & XMIT_OWN)
958 break;
960 if(tmdstat & XMIT_ERR)
962 #if 0
963 if(tmdp->status2 & XMIT_TDRMASK && debuglevel > 3)
964 printk(KERN_ERR "%s: tdr-problems (e.g. no resistor)\n",dev->name);
965 #endif
966 /* checking some errors */
967 if(tmdp->status2 & XMIT_RTRY)
968 p->stats.tx_aborted_errors++;
969 if(tmdp->status2 & XMIT_LCAR)
970 p->stats.tx_carrier_errors++;
971 if(tmdp->status2 & (XMIT_BUFF | XMIT_UFLO )) {
972 /* this stops the xmitter */
973 p->stats.tx_fifo_errors++;
974 if(debuglevel > 0)
975 printk(KERN_ERR "%s: Xmit FIFO/BUFF error\n",dev->name);
976 if(p->features & INIT_RING_BEFORE_START) {
977 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END; /* test: resend this frame */
978 ni65_stop_start(dev,p);
979 break; /* no more Xmit processing .. */
981 else
982 ni65_stop_start(dev,p);
984 if(debuglevel > 2)
985 printk(KERN_ERR "%s: xmit-error: %04x %02x-%04x\n",dev->name,csr0,(int) tmdstat,(int) tmdp->status2);
986 if(!(csr0 & CSR0_BABL)) /* don't count errors twice */
987 p->stats.tx_errors++;
988 tmdp->status2 = 0;
990 else {
991 p->stats.tx_bytes -= (short)(tmdp->blen);
992 p->stats.tx_packets++;
995 #ifdef XMT_VIA_SKB
996 if(p->tmd_skb[p->tmdlast]) {
997 dev_kfree_skb_irq(p->tmd_skb[p->tmdlast]);
998 p->tmd_skb[p->tmdlast] = NULL;
1000 #endif
1002 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
1003 if(p->tmdlast == p->tmdnum)
1004 p->xmit_queued = 0;
1006 netif_wake_queue(dev);
1010 * We have received a packet
1012 static void ni65_recv_intr(struct net_device *dev,int csr0)
1014 struct rmd *rmdp;
1015 int rmdstat,len;
1016 int cnt=0;
1017 struct priv *p = (struct priv *) dev->priv;
1019 rmdp = p->rmdhead + p->rmdnum;
1020 while(!( (rmdstat = rmdp->u.s.status) & RCV_OWN))
1022 cnt++;
1023 if( (rmdstat & (RCV_START | RCV_END | RCV_ERR)) != (RCV_START | RCV_END) ) /* error or oversized? */
1025 if(!(rmdstat & RCV_ERR)) {
1026 if(rmdstat & RCV_START)
1028 p->stats.rx_length_errors++;
1029 printk(KERN_ERR "%s: recv, packet too long: %d\n",dev->name,rmdp->mlen & 0x0fff);
1032 else {
1033 if(debuglevel > 2)
1034 printk(KERN_ERR "%s: receive-error: %04x, lance-status: %04x/%04x\n",
1035 dev->name,(int) rmdstat,csr0,(int) inw(PORT+L_DATAREG) );
1036 if(rmdstat & RCV_FRAM)
1037 p->stats.rx_frame_errors++;
1038 if(rmdstat & RCV_OFLO)
1039 p->stats.rx_over_errors++;
1040 if(rmdstat & RCV_CRC)
1041 p->stats.rx_crc_errors++;
1042 if(rmdstat & RCV_BUF_ERR)
1043 p->stats.rx_fifo_errors++;
1045 if(!(csr0 & CSR0_MISS)) /* don't count errors twice */
1046 p->stats.rx_errors++;
1048 else if( (len = (rmdp->mlen & 0x0fff) - 4) >= 60)
1050 #ifdef RCV_VIA_SKB
1051 struct sk_buff *skb = alloc_skb(R_BUF_SIZE+2+16,GFP_ATOMIC);
1052 if (skb)
1053 skb_reserve(skb,16);
1054 #else
1055 struct sk_buff *skb = dev_alloc_skb(len+2);
1056 #endif
1057 if(skb)
1059 skb_reserve(skb,2);
1060 skb->dev = dev;
1061 #ifdef RCV_VIA_SKB
1062 if( (unsigned long) (skb->data + R_BUF_SIZE) > 0x1000000) {
1063 skb_put(skb,len);
1064 eth_copy_and_sum(skb, (unsigned char *)(p->recv_skb[p->rmdnum]->data),len,0);
1066 else {
1067 struct sk_buff *skb1 = p->recv_skb[p->rmdnum];
1068 skb_put(skb,R_BUF_SIZE);
1069 p->recv_skb[p->rmdnum] = skb;
1070 rmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1071 skb = skb1;
1072 skb_trim(skb,len);
1074 #else
1075 skb_put(skb,len);
1076 eth_copy_and_sum(skb, (unsigned char *) p->recvbounce[p->rmdnum],len,0);
1077 #endif
1078 p->stats.rx_packets++;
1079 p->stats.rx_bytes += len;
1080 skb->protocol=eth_type_trans(skb,dev);
1081 netif_rx(skb);
1082 dev->last_rx = jiffies;
1084 else
1086 printk(KERN_ERR "%s: can't alloc new sk_buff\n",dev->name);
1087 p->stats.rx_dropped++;
1090 else {
1091 printk(KERN_INFO "%s: received runt packet\n",dev->name);
1092 p->stats.rx_errors++;
1094 rmdp->blen = -(R_BUF_SIZE-8);
1095 rmdp->mlen = 0;
1096 rmdp->u.s.status = RCV_OWN; /* change owner */
1097 p->rmdnum = (p->rmdnum + 1) & (RMDNUM-1);
1098 rmdp = p->rmdhead + p->rmdnum;
1103 * kick xmitter ..
1106 static void ni65_timeout(struct net_device *dev)
1108 int i;
1109 struct priv *p = (struct priv *) dev->priv;
1111 printk(KERN_ERR "%s: xmitter timed out, try to restart!\n",dev->name);
1112 for(i=0;i<TMDNUM;i++)
1113 printk("%02x ",p->tmdhead[i].u.s.status);
1114 printk("\n");
1115 ni65_lance_reinit(dev);
1116 dev->trans_start = jiffies;
1117 netif_wake_queue(dev);
1121 * Send a packet
1124 static int ni65_send_packet(struct sk_buff *skb, struct net_device *dev)
1126 struct priv *p = (struct priv *) dev->priv;
1128 netif_stop_queue(dev);
1130 if (test_and_set_bit(0, (void*)&p->lock)) {
1131 printk(KERN_ERR "%s: Queue was locked.\n", dev->name);
1132 return 1;
1136 short len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1137 struct tmd *tmdp;
1138 unsigned long flags;
1140 #ifdef XMT_VIA_SKB
1141 if( (unsigned long) (skb->data + skb->len) > 0x1000000) {
1142 #endif
1144 memcpy((char *) p->tmdbounce[p->tmdbouncenum] ,(char *)skb->data,
1145 (skb->len > T_BUF_SIZE) ? T_BUF_SIZE : skb->len);
1146 if (len > skb->len)
1147 memset((char *)p->tmdbounce[p->tmdbouncenum]+skb->len, 0, len-skb->len);
1148 dev_kfree_skb (skb);
1150 save_flags(flags);
1151 cli();
1153 tmdp = p->tmdhead + p->tmdnum;
1154 tmdp->u.buffer = (u32) isa_virt_to_bus(p->tmdbounce[p->tmdbouncenum]);
1155 p->tmdbouncenum = (p->tmdbouncenum + 1) & (TMDNUM - 1);
1157 #ifdef XMT_VIA_SKB
1159 else {
1160 save_flags(flags);
1161 cli();
1163 tmdp = p->tmdhead + p->tmdnum;
1164 tmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1165 p->tmd_skb[p->tmdnum] = skb;
1167 #endif
1168 tmdp->blen = -len;
1170 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;
1171 writedatareg(CSR0_TDMD | CSR0_INEA); /* enable xmit & interrupt */
1173 p->xmit_queued = 1;
1174 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
1176 if(p->tmdnum != p->tmdlast)
1177 netif_wake_queue(dev);
1179 p->lock = 0;
1180 dev->trans_start = jiffies;
1182 restore_flags(flags);
1185 return 0;
1188 static struct net_device_stats *ni65_get_stats(struct net_device *dev)
1191 #if 0
1192 int i;
1193 struct priv *p = (struct priv *) dev->priv;
1194 for(i=0;i<RMDNUM;i++)
1196 struct rmd *rmdp = p->rmdhead + ((p->rmdnum + i) & (RMDNUM-1));
1197 printk("%02x ",rmdp->u.s.status);
1199 printk("\n");
1200 #endif
1202 return &((struct priv *) dev->priv)->stats;
1205 static void set_multicast_list(struct net_device *dev)
1207 if(!ni65_lance_reinit(dev))
1208 printk(KERN_ERR "%s: Can't switch card into MC mode!\n",dev->name);
1209 netif_wake_queue(dev);
1212 #ifdef MODULE
1213 static struct net_device dev_ni65 = { .base_addr = 0x360, .irq = 9, .init = ni65_probe };
1215 /* set: io,irq,dma or set it when calling insmod */
1216 static int irq;
1217 static int io;
1218 static int dma;
1220 MODULE_PARM(irq, "i");
1221 MODULE_PARM(io, "i");
1222 MODULE_PARM(dma, "i");
1223 MODULE_PARM_DESC(irq, "ni6510 IRQ number (ignored for some cards)");
1224 MODULE_PARM_DESC(io, "ni6510 I/O base address");
1225 MODULE_PARM_DESC(dma, "ni6510 ISA DMA channel (ignored for some cards)");
1227 int init_module(void)
1229 dev_ni65.irq = irq;
1230 dev_ni65.dma = dma;
1231 dev_ni65.base_addr = io;
1232 if (register_netdev(&dev_ni65) != 0)
1233 return -EIO;
1234 return 0;
1237 void cleanup_module(void)
1239 struct priv *p;
1240 p = (struct priv *) dev_ni65.priv;
1241 if(!p) {
1242 printk("Ooops .. no private struct\n");
1243 return;
1245 disable_dma(dev_ni65.dma);
1246 free_dma(dev_ni65.dma);
1247 unregister_netdev(&dev_ni65);
1248 release_region(dev_ni65.base_addr,cards[p->cardno].total_size);
1249 ni65_free_buffer(p);
1250 dev_ni65.priv = NULL;
1252 #endif /* MODULE */
1253 MODULE_LICENSE("GPL");
1256 * END of ni65.c