MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / ni65.c
blobc43c4cf9e837b7b320116786b7e22ff7b8a1fb3c
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/module.h>
77 #include <asm/bitops.h>
78 #include <asm/io.h>
79 #include <asm/dma.h>
81 #include "ni65.h"
84 * the current setting allows an acceptable performance
85 * for 'RCV_PARANOIA_CHECK' read the 'known problems' part in
86 * the header of this file
87 * 'invert' the defines for max. performance. This may cause DMA problems
88 * on some boards (e.g on my ASUS SP3G)
90 #undef XMT_VIA_SKB
91 #undef RCV_VIA_SKB
92 #define RCV_PARANOIA_CHECK
94 #define MID_PERFORMANCE
96 #if defined( LOW_PERFORMANCE )
97 static int isa0=7,isa1=7,csr80=0x0c10;
98 #elif defined( MID_PERFORMANCE )
99 static int isa0=5,isa1=5,csr80=0x2810;
100 #else /* high performance */
101 static int isa0=4,isa1=4,csr80=0x0017;
102 #endif
105 * a few card/vendor specific defines
107 #define NI65_ID0 0x00
108 #define NI65_ID1 0x55
109 #define NI65_EB_ID0 0x52
110 #define NI65_EB_ID1 0x44
111 #define NE2100_ID0 0x57
112 #define NE2100_ID1 0x57
114 #define PORT p->cmdr_addr
117 * buffer configuration
119 #if 1
120 #define RMDNUM 16
121 #define RMDNUMMASK 0x80000000
122 #else
123 #define RMDNUM 8
124 #define RMDNUMMASK 0x60000000 /* log2(RMDNUM)<<29 */
125 #endif
127 #if 0
128 #define TMDNUM 1
129 #define TMDNUMMASK 0x00000000
130 #else
131 #define TMDNUM 4
132 #define TMDNUMMASK 0x40000000 /* log2(TMDNUM)<<29 */
133 #endif
135 /* slightly oversized */
136 #define R_BUF_SIZE 1544
137 #define T_BUF_SIZE 1544
140 * lance register defines
142 #define L_DATAREG 0x00
143 #define L_ADDRREG 0x02
144 #define L_RESET 0x04
145 #define L_CONFIG 0x05
146 #define L_BUSIF 0x06
149 * to access the lance/am7990-regs, you have to write
150 * reg-number into L_ADDRREG, then you can access it using L_DATAREG
152 #define CSR0 0x00
153 #define CSR1 0x01
154 #define CSR2 0x02
155 #define CSR3 0x03
157 #define INIT_RING_BEFORE_START 0x1
158 #define FULL_RESET_ON_ERROR 0x2
160 #if 0
161 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);inw(PORT+L_ADDRREG); \
162 outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
163 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_ADDRREG),\
164 inw(PORT+L_DATAREG))
165 #if 0
166 #define writedatareg(val) {outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
167 #else
168 #define writedatareg(val) { writereg(val,CSR0); }
169 #endif
170 #else
171 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);outw(val,PORT+L_DATAREG);}
172 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_DATAREG))
173 #define writedatareg(val) { writereg(val,CSR0); }
174 #endif
176 static unsigned char ni_vendor[] = { 0x02,0x07,0x01 };
178 static struct card {
179 unsigned char id0,id1;
180 short id_offset;
181 short total_size;
182 short cmd_offset;
183 short addr_offset;
184 unsigned char *vendor_id;
185 char *cardname;
186 long config;
187 } cards[] = {
189 .id0 = NI65_ID0,
190 .id1 = NI65_ID1,
191 .id_offset = 0x0e,
192 .total_size = 0x10,
193 .cmd_offset = 0x0,
194 .addr_offset = 0x8,
195 .vendor_id = ni_vendor,
196 .cardname = "ni6510",
197 .config = 0x1,
200 .id0 = NI65_EB_ID0,
201 .id1 = NI65_EB_ID1,
202 .id_offset = 0x0e,
203 .total_size = 0x18,
204 .cmd_offset = 0x10,
205 .addr_offset = 0x0,
206 .vendor_id = ni_vendor,
207 .cardname = "ni6510 EtherBlaster",
208 .config = 0x2,
211 .id0 = NE2100_ID0,
212 .id1 = NE2100_ID1,
213 .id_offset = 0x0e,
214 .total_size = 0x18,
215 .cmd_offset = 0x10,
216 .addr_offset = 0x0,
217 .vendor_id = NULL,
218 .cardname = "generic NE2100",
219 .config = 0x0,
222 #define NUM_CARDS 3
224 struct priv
226 struct rmd rmdhead[RMDNUM];
227 struct tmd tmdhead[TMDNUM];
228 struct init_block ib;
229 int rmdnum;
230 int tmdnum,tmdlast;
231 #ifdef RCV_VIA_SKB
232 struct sk_buff *recv_skb[RMDNUM];
233 #else
234 void *recvbounce[RMDNUM];
235 #endif
236 #ifdef XMT_VIA_SKB
237 struct sk_buff *tmd_skb[TMDNUM];
238 #endif
239 void *tmdbounce[TMDNUM];
240 int tmdbouncenum;
241 int lock,xmit_queued;
242 struct net_device_stats stats;
243 void *self;
244 int cmdr_addr;
245 int cardno;
246 int features;
247 spinlock_t ring_lock;
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(KERN_ERR "%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;
346 static void cleanup_card(struct net_device *dev)
348 struct priv *p = (struct priv *) dev->priv;
349 disable_dma(dev->dma);
350 free_dma(dev->dma);
351 release_region(dev->base_addr, cards[p->cardno].total_size);
352 ni65_free_buffer(p);
355 /* set: io,irq,dma or set it when calling insmod */
356 static int irq;
357 static int io;
358 static int dma;
361 * Probe The Card (not the lance-chip)
363 struct net_device * __init ni65_probe(int unit)
365 struct net_device *dev = alloc_etherdev(0);
366 static int ports[] = {0x360,0x300,0x320,0x340, 0};
367 int *port;
368 int err = 0;
370 if (!dev)
371 return ERR_PTR(-ENOMEM);
373 if (unit >= 0) {
374 sprintf(dev->name, "eth%d", unit);
375 netdev_boot_setup_check(dev);
376 irq = dev->irq;
377 dma = dev->dma;
378 } else {
379 dev->base_addr = io;
382 if (dev->base_addr > 0x1ff) { /* Check a single specified location. */
383 err = ni65_probe1(dev, dev->base_addr);
384 } else if (dev->base_addr > 0) { /* Don't probe at all. */
385 err = -ENXIO;
386 } else {
387 for (port = ports; *port && ni65_probe1(dev, *port); port++)
389 if (!*port)
390 err = -ENODEV;
392 if (err)
393 goto out;
395 err = register_netdev(dev);
396 if (err)
397 goto out1;
398 return dev;
399 out1:
400 cleanup_card(dev);
401 out:
402 free_netdev(dev);
403 return ERR_PTR(err);
407 * this is the real card probe ..
409 static int __init ni65_probe1(struct net_device *dev,int ioaddr)
411 int i,j;
412 struct priv *p;
413 unsigned long flags;
415 dev->irq = irq;
416 dev->dma = dma;
418 for(i=0;i<NUM_CARDS;i++) {
419 if(!request_region(ioaddr, cards[i].total_size, cards[i].cardname))
420 continue;
421 if(cards[i].id_offset >= 0) {
422 if(inb(ioaddr+cards[i].id_offset+0) != cards[i].id0 ||
423 inb(ioaddr+cards[i].id_offset+1) != cards[i].id1) {
424 release_region(ioaddr, cards[i].total_size);
425 continue;
428 if(cards[i].vendor_id) {
429 for(j=0;j<3;j++)
430 if(inb(ioaddr+cards[i].addr_offset+j) != cards[i].vendor_id[j]) {
431 release_region(ioaddr, cards[i].total_size);
432 continue;
435 break;
437 if(i == NUM_CARDS)
438 return -ENODEV;
440 for(j=0;j<6;j++)
441 dev->dev_addr[j] = inb(ioaddr+cards[i].addr_offset+j);
443 if( (j=ni65_alloc_buffer(dev)) < 0) {
444 release_region(ioaddr, cards[i].total_size);
445 return j;
447 p = (struct priv *) dev->priv;
448 p->cmdr_addr = ioaddr + cards[i].cmd_offset;
449 p->cardno = i;
450 spin_lock_init(&p->ring_lock);
452 printk(KERN_INFO "%s: %s found at %#3x, ", dev->name, cards[p->cardno].cardname , ioaddr);
454 outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
455 if( (j=readreg(CSR0)) != 0x4) {
456 printk("failed.\n");
457 printk(KERN_ERR "%s: Can't RESET card: %04x\n", dev->name, j);
458 ni65_free_buffer(p);
459 release_region(ioaddr, cards[p->cardno].total_size);
460 return -EAGAIN;
463 outw(88,PORT+L_ADDRREG);
464 if(inw(PORT+L_ADDRREG) == 88) {
465 unsigned long v;
466 v = inw(PORT+L_DATAREG);
467 v <<= 16;
468 outw(89,PORT+L_ADDRREG);
469 v |= inw(PORT+L_DATAREG);
470 printk("Version %#08lx, ",v);
471 p->features = INIT_RING_BEFORE_START;
473 else {
474 printk("ancient LANCE, ");
475 p->features = 0x0;
478 if(test_bit(0,&cards[i].config)) {
479 dev->irq = irqtab[(inw(ioaddr+L_CONFIG)>>2)&3];
480 dev->dma = dmatab[inw(ioaddr+L_CONFIG)&3];
481 printk("IRQ %d (from card), DMA %d (from card).\n",dev->irq,dev->dma);
483 else {
484 if(dev->dma == 0) {
485 /* 'stuck test' from lance.c */
486 long dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) |
487 (inb(DMA2_STAT_REG) & 0xf0);
488 for(i=1;i<5;i++) {
489 int dma = dmatab[i];
490 if(test_bit(dma,&dma_channels) || request_dma(dma,"ni6510"))
491 continue;
493 flags=claim_dma_lock();
494 disable_dma(dma);
495 set_dma_mode(dma,DMA_MODE_CASCADE);
496 enable_dma(dma);
497 release_dma_lock(flags);
499 ni65_init_lance(p,dev->dev_addr,0,0); /* trigger memory access */
501 flags=claim_dma_lock();
502 disable_dma(dma);
503 free_dma(dma);
504 release_dma_lock(flags);
506 if(readreg(CSR0) & CSR0_IDON)
507 break;
509 if(i == 5) {
510 printk("failed.\n");
511 printk(KERN_ERR "%s: Can't detect DMA channel!\n", dev->name);
512 ni65_free_buffer(p);
513 release_region(ioaddr, cards[p->cardno].total_size);
514 return -EAGAIN;
516 dev->dma = dmatab[i];
517 printk("DMA %d (autodetected), ",dev->dma);
519 else
520 printk("DMA %d (assigned), ",dev->dma);
522 if(dev->irq < 2)
524 unsigned long irq_mask;
526 ni65_init_lance(p,dev->dev_addr,0,0);
527 irq_mask = probe_irq_on();
528 writereg(CSR0_INIT|CSR0_INEA,CSR0); /* trigger interrupt */
529 set_current_state(TASK_UNINTERRUPTIBLE);
530 schedule_timeout(HZ/50);
531 dev->irq = probe_irq_off(irq_mask);
532 if(!dev->irq)
534 printk("Failed to detect IRQ line!\n");
535 ni65_free_buffer(p);
536 release_region(ioaddr, cards[p->cardno].total_size);
537 return -EAGAIN;
539 printk("IRQ %d (autodetected).\n",dev->irq);
541 else
542 printk("IRQ %d (assigned).\n",dev->irq);
545 if(request_dma(dev->dma, cards[p->cardno].cardname ) != 0)
547 printk(KERN_ERR "%s: Can't request dma-channel %d\n",dev->name,(int) dev->dma);
548 ni65_free_buffer(p);
549 release_region(ioaddr, cards[p->cardno].total_size);
550 return -EAGAIN;
553 dev->base_addr = ioaddr;
554 SET_MODULE_OWNER(dev);
555 dev->open = ni65_open;
556 dev->stop = ni65_close;
557 dev->hard_start_xmit = ni65_send_packet;
558 dev->tx_timeout = ni65_timeout;
559 dev->watchdog_timeo = HZ/2;
560 dev->get_stats = ni65_get_stats;
561 dev->set_multicast_list = set_multicast_list;
562 return 0; /* everything is OK */
566 * set lance register and trigger init
568 static void ni65_init_lance(struct priv *p,unsigned char *daddr,int filter,int mode)
570 int i;
571 u32 pib;
573 writereg(CSR0_CLRALL|CSR0_STOP,CSR0);
575 for(i=0;i<6;i++)
576 p->ib.eaddr[i] = daddr[i];
578 for(i=0;i<8;i++)
579 p->ib.filter[i] = filter;
580 p->ib.mode = mode;
582 p->ib.trp = (u32) isa_virt_to_bus(p->tmdhead) | TMDNUMMASK;
583 p->ib.rrp = (u32) isa_virt_to_bus(p->rmdhead) | RMDNUMMASK;
584 writereg(0,CSR3); /* busmaster/no word-swap */
585 pib = (u32) isa_virt_to_bus(&p->ib);
586 writereg(pib & 0xffff,CSR1);
587 writereg(pib >> 16,CSR2);
589 writereg(CSR0_INIT,CSR0); /* this changes L_ADDRREG to CSR0 */
591 for(i=0;i<32;i++)
593 mdelay(4);
594 if(inw(PORT+L_DATAREG) & (CSR0_IDON | CSR0_MERR) )
595 break; /* init ok ? */
600 * allocate memory area and check the 16MB border
602 static void *ni65_alloc_mem(struct net_device *dev,char *what,int size,int type)
604 struct sk_buff *skb=NULL;
605 unsigned char *ptr;
606 void *ret;
608 if(type) {
609 ret = skb = alloc_skb(2+16+size,GFP_KERNEL|GFP_DMA);
610 if(!skb) {
611 printk(KERN_WARNING "%s: unable to allocate %s memory.\n",dev->name,what);
612 return NULL;
614 skb->dev = dev;
615 skb_reserve(skb,2+16);
616 skb_put(skb,R_BUF_SIZE); /* grab the whole space .. (not necessary) */
617 ptr = skb->data;
619 else {
620 ret = ptr = kmalloc(T_BUF_SIZE,GFP_KERNEL | GFP_DMA);
621 if(!ret) {
622 printk(KERN_WARNING "%s: unable to allocate %s memory.\n",dev->name,what);
623 return NULL;
626 if( (u32) virt_to_phys(ptr+size) > 0x1000000) {
627 printk(KERN_WARNING "%s: unable to allocate %s memory in lower 16MB!\n",dev->name,what);
628 if(type)
629 kfree_skb(skb);
630 else
631 kfree(ptr);
632 return NULL;
634 return ret;
638 * allocate all memory structures .. send/recv buffers etc ...
640 static int ni65_alloc_buffer(struct net_device *dev)
642 unsigned char *ptr;
643 struct priv *p;
644 int i;
647 * we need 8-aligned memory ..
649 ptr = ni65_alloc_mem(dev,"BUFFER",sizeof(struct priv)+8,0);
650 if(!ptr)
651 return -ENOMEM;
653 p = dev->priv = (struct priv *) (((unsigned long) ptr + 7) & ~0x7);
654 memset((char *) dev->priv,0,sizeof(struct priv));
655 p->self = ptr;
657 for(i=0;i<TMDNUM;i++)
659 #ifdef XMT_VIA_SKB
660 p->tmd_skb[i] = NULL;
661 #endif
662 p->tmdbounce[i] = ni65_alloc_mem(dev,"XMIT",T_BUF_SIZE,0);
663 if(!p->tmdbounce[i]) {
664 ni65_free_buffer(p);
665 return -ENOMEM;
669 for(i=0;i<RMDNUM;i++)
671 #ifdef RCV_VIA_SKB
672 p->recv_skb[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,1);
673 if(!p->recv_skb[i]) {
674 ni65_free_buffer(p);
675 return -ENOMEM;
677 #else
678 p->recvbounce[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,0);
679 if(!p->recvbounce[i]) {
680 ni65_free_buffer(p);
681 return -ENOMEM;
683 #endif
686 return 0; /* everything is OK */
690 * free buffers and private struct
692 static void ni65_free_buffer(struct priv *p)
694 int i;
696 if(!p)
697 return;
699 for(i=0;i<TMDNUM;i++) {
700 if(p->tmdbounce[i])
701 kfree(p->tmdbounce[i]);
702 #ifdef XMT_VIA_SKB
703 if(p->tmd_skb[i])
704 dev_kfree_skb(p->tmd_skb[i]);
705 #endif
708 for(i=0;i<RMDNUM;i++)
710 #ifdef RCV_VIA_SKB
711 if(p->recv_skb[i])
712 dev_kfree_skb(p->recv_skb[i]);
713 #else
714 if(p->recvbounce[i])
715 kfree(p->recvbounce[i]);
716 #endif
718 if(p->self)
719 kfree(p->self);
724 * stop and (re)start lance .. e.g after an error
726 static void ni65_stop_start(struct net_device *dev,struct priv *p)
728 int csr0 = CSR0_INEA;
730 writedatareg(CSR0_STOP);
732 if(debuglevel > 1)
733 printk(KERN_DEBUG "ni65_stop_start\n");
735 if(p->features & INIT_RING_BEFORE_START) {
736 int i;
737 #ifdef XMT_VIA_SKB
738 struct sk_buff *skb_save[TMDNUM];
739 #endif
740 unsigned long buffer[TMDNUM];
741 short blen[TMDNUM];
743 if(p->xmit_queued) {
744 while(1) {
745 if((p->tmdhead[p->tmdlast].u.s.status & XMIT_OWN))
746 break;
747 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
748 if(p->tmdlast == p->tmdnum)
749 break;
753 for(i=0;i<TMDNUM;i++) {
754 struct tmd *tmdp = p->tmdhead + i;
755 #ifdef XMT_VIA_SKB
756 skb_save[i] = p->tmd_skb[i];
757 #endif
758 buffer[i] = (u32) isa_bus_to_virt(tmdp->u.buffer);
759 blen[i] = tmdp->blen;
760 tmdp->u.s.status = 0x0;
763 for(i=0;i<RMDNUM;i++) {
764 struct rmd *rmdp = p->rmdhead + i;
765 rmdp->u.s.status = RCV_OWN;
767 p->tmdnum = p->xmit_queued = 0;
768 writedatareg(CSR0_STRT | csr0);
770 for(i=0;i<TMDNUM;i++) {
771 int num = (i + p->tmdlast) & (TMDNUM-1);
772 p->tmdhead[i].u.buffer = (u32) isa_virt_to_bus((char *)buffer[num]); /* status is part of buffer field */
773 p->tmdhead[i].blen = blen[num];
774 if(p->tmdhead[i].u.s.status & XMIT_OWN) {
775 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
776 p->xmit_queued = 1;
777 writedatareg(CSR0_TDMD | CSR0_INEA | csr0);
779 #ifdef XMT_VIA_SKB
780 p->tmd_skb[i] = skb_save[num];
781 #endif
783 p->rmdnum = p->tmdlast = 0;
784 if(!p->lock)
785 if (p->tmdnum || !p->xmit_queued)
786 netif_wake_queue(dev);
787 dev->trans_start = jiffies;
789 else
790 writedatareg(CSR0_STRT | csr0);
794 * init lance (write init-values .. init-buffers) (open-helper)
796 static int ni65_lance_reinit(struct net_device *dev)
798 int i;
799 struct priv *p = (struct priv *) dev->priv;
800 unsigned long flags;
802 p->lock = 0;
803 p->xmit_queued = 0;
805 flags=claim_dma_lock();
806 disable_dma(dev->dma); /* I've never worked with dma, but we do it like the packetdriver */
807 set_dma_mode(dev->dma,DMA_MODE_CASCADE);
808 enable_dma(dev->dma);
809 release_dma_lock(flags);
811 outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
812 if( (i=readreg(CSR0) ) != 0x4)
814 printk(KERN_ERR "%s: can't RESET %s card: %04x\n",dev->name,
815 cards[p->cardno].cardname,(int) i);
816 flags=claim_dma_lock();
817 disable_dma(dev->dma);
818 release_dma_lock(flags);
819 return 0;
822 p->rmdnum = p->tmdnum = p->tmdlast = p->tmdbouncenum = 0;
823 for(i=0;i<TMDNUM;i++)
825 struct tmd *tmdp = p->tmdhead + i;
826 #ifdef XMT_VIA_SKB
827 if(p->tmd_skb[i]) {
828 dev_kfree_skb(p->tmd_skb[i]);
829 p->tmd_skb[i] = NULL;
831 #endif
832 tmdp->u.buffer = 0x0;
833 tmdp->u.s.status = XMIT_START | XMIT_END;
834 tmdp->blen = tmdp->status2 = 0;
837 for(i=0;i<RMDNUM;i++)
839 struct rmd *rmdp = p->rmdhead + i;
840 #ifdef RCV_VIA_SKB
841 rmdp->u.buffer = (u32) isa_virt_to_bus(p->recv_skb[i]->data);
842 #else
843 rmdp->u.buffer = (u32) isa_virt_to_bus(p->recvbounce[i]);
844 #endif
845 rmdp->blen = -(R_BUF_SIZE-8);
846 rmdp->mlen = 0;
847 rmdp->u.s.status = RCV_OWN;
850 if(dev->flags & IFF_PROMISC)
851 ni65_init_lance(p,dev->dev_addr,0x00,M_PROM);
852 else if(dev->mc_count || dev->flags & IFF_ALLMULTI)
853 ni65_init_lance(p,dev->dev_addr,0xff,0x0);
854 else
855 ni65_init_lance(p,dev->dev_addr,0x00,0x00);
858 * ni65_set_lance_mem() sets L_ADDRREG to CSR0
859 * NOW, WE WILL NEVER CHANGE THE L_ADDRREG, CSR0 IS ALWAYS SELECTED
862 if(inw(PORT+L_DATAREG) & CSR0_IDON) {
863 ni65_set_performance(p);
864 /* init OK: start lance , enable interrupts */
865 writedatareg(CSR0_CLRALL | CSR0_INEA | CSR0_STRT);
866 return 1; /* ->OK */
868 printk(KERN_ERR "%s: can't init lance, status: %04x\n",dev->name,(int) inw(PORT+L_DATAREG));
869 flags=claim_dma_lock();
870 disable_dma(dev->dma);
871 release_dma_lock(flags);
872 return 0; /* ->Error */
876 * interrupt handler
878 static irqreturn_t ni65_interrupt(int irq, void * dev_id, struct pt_regs * regs)
880 int csr0 = 0;
881 struct net_device *dev = dev_id;
882 struct priv *p;
883 int bcnt = 32;
885 p = (struct priv *) dev->priv;
887 spin_lock(&p->ring_lock);
889 while(--bcnt) {
890 csr0 = inw(PORT+L_DATAREG);
892 #if 0
893 writedatareg( (csr0 & CSR0_CLRALL) ); /* ack interrupts, disable int. */
894 #else
895 writedatareg( (csr0 & CSR0_CLRALL) | CSR0_INEA ); /* ack interrupts, interrupts enabled */
896 #endif
898 if(!(csr0 & (CSR0_ERR | CSR0_RINT | CSR0_TINT)))
899 break;
901 if(csr0 & CSR0_RINT) /* RECV-int? */
902 ni65_recv_intr(dev,csr0);
903 if(csr0 & CSR0_TINT) /* XMIT-int? */
904 ni65_xmit_intr(dev,csr0);
906 if(csr0 & CSR0_ERR)
908 struct priv *p = (struct priv *) dev->priv;
909 if(debuglevel > 1)
910 printk(KERN_ERR "%s: general error: %04x.\n",dev->name,csr0);
911 if(csr0 & CSR0_BABL)
912 p->stats.tx_errors++;
913 if(csr0 & CSR0_MISS) {
914 int i;
915 for(i=0;i<RMDNUM;i++)
916 printk("%02x ",p->rmdhead[i].u.s.status);
917 printk("\n");
918 p->stats.rx_errors++;
920 if(csr0 & CSR0_MERR) {
921 if(debuglevel > 1)
922 printk(KERN_ERR "%s: Ooops .. memory error: %04x.\n",dev->name,csr0);
923 ni65_stop_start(dev,p);
928 #ifdef RCV_PARANOIA_CHECK
930 int j;
931 for(j=0;j<RMDNUM;j++)
933 struct priv *p = (struct priv *) dev->priv;
934 int i,k,num1,num2;
935 for(i=RMDNUM-1;i>0;i--) {
936 num2 = (p->rmdnum + i) & (RMDNUM-1);
937 if(!(p->rmdhead[num2].u.s.status & RCV_OWN))
938 break;
941 if(i) {
942 for(k=0;k<RMDNUM;k++) {
943 num1 = (p->rmdnum + k) & (RMDNUM-1);
944 if(!(p->rmdhead[num1].u.s.status & RCV_OWN))
945 break;
947 if(!k)
948 break;
950 if(debuglevel > 0)
952 char buf[256],*buf1;
953 int k;
954 buf1 = buf;
955 for(k=0;k<RMDNUM;k++) {
956 sprintf(buf1,"%02x ",(p->rmdhead[k].u.s.status)); /* & RCV_OWN) ); */
957 buf1 += 3;
959 *buf1 = 0;
960 printk(KERN_ERR "%s: Ooops, receive ring corrupted %2d %2d | %s\n",dev->name,p->rmdnum,i,buf);
963 p->rmdnum = num1;
964 ni65_recv_intr(dev,csr0);
965 if((p->rmdhead[num2].u.s.status & RCV_OWN))
966 break; /* ok, we are 'in sync' again */
968 else
969 break;
972 #endif
974 if( (csr0 & (CSR0_RXON | CSR0_TXON)) != (CSR0_RXON | CSR0_TXON) ) {
975 printk(KERN_DEBUG "%s: RX or TX was offline -> restart\n",dev->name);
976 ni65_stop_start(dev,p);
978 else
979 writedatareg(CSR0_INEA);
981 spin_unlock(&p->ring_lock);
982 return IRQ_HANDLED;
986 * We have received an Xmit-Interrupt ..
987 * send a new packet if necessary
989 static void ni65_xmit_intr(struct net_device *dev,int csr0)
991 struct priv *p = (struct priv *) dev->priv;
993 while(p->xmit_queued)
995 struct tmd *tmdp = p->tmdhead + p->tmdlast;
996 int tmdstat = tmdp->u.s.status;
998 if(tmdstat & XMIT_OWN)
999 break;
1001 if(tmdstat & XMIT_ERR)
1003 #if 0
1004 if(tmdp->status2 & XMIT_TDRMASK && debuglevel > 3)
1005 printk(KERN_ERR "%s: tdr-problems (e.g. no resistor)\n",dev->name);
1006 #endif
1007 /* checking some errors */
1008 if(tmdp->status2 & XMIT_RTRY)
1009 p->stats.tx_aborted_errors++;
1010 if(tmdp->status2 & XMIT_LCAR)
1011 p->stats.tx_carrier_errors++;
1012 if(tmdp->status2 & (XMIT_BUFF | XMIT_UFLO )) {
1013 /* this stops the xmitter */
1014 p->stats.tx_fifo_errors++;
1015 if(debuglevel > 0)
1016 printk(KERN_ERR "%s: Xmit FIFO/BUFF error\n",dev->name);
1017 if(p->features & INIT_RING_BEFORE_START) {
1018 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END; /* test: resend this frame */
1019 ni65_stop_start(dev,p);
1020 break; /* no more Xmit processing .. */
1022 else
1023 ni65_stop_start(dev,p);
1025 if(debuglevel > 2)
1026 printk(KERN_ERR "%s: xmit-error: %04x %02x-%04x\n",dev->name,csr0,(int) tmdstat,(int) tmdp->status2);
1027 if(!(csr0 & CSR0_BABL)) /* don't count errors twice */
1028 p->stats.tx_errors++;
1029 tmdp->status2 = 0;
1031 else {
1032 p->stats.tx_bytes -= (short)(tmdp->blen);
1033 p->stats.tx_packets++;
1036 #ifdef XMT_VIA_SKB
1037 if(p->tmd_skb[p->tmdlast]) {
1038 dev_kfree_skb_irq(p->tmd_skb[p->tmdlast]);
1039 p->tmd_skb[p->tmdlast] = NULL;
1041 #endif
1043 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
1044 if(p->tmdlast == p->tmdnum)
1045 p->xmit_queued = 0;
1047 netif_wake_queue(dev);
1051 * We have received a packet
1053 static void ni65_recv_intr(struct net_device *dev,int csr0)
1055 struct rmd *rmdp;
1056 int rmdstat,len;
1057 int cnt=0;
1058 struct priv *p = (struct priv *) dev->priv;
1060 rmdp = p->rmdhead + p->rmdnum;
1061 while(!( (rmdstat = rmdp->u.s.status) & RCV_OWN))
1063 cnt++;
1064 if( (rmdstat & (RCV_START | RCV_END | RCV_ERR)) != (RCV_START | RCV_END) ) /* error or oversized? */
1066 if(!(rmdstat & RCV_ERR)) {
1067 if(rmdstat & RCV_START)
1069 p->stats.rx_length_errors++;
1070 printk(KERN_ERR "%s: recv, packet too long: %d\n",dev->name,rmdp->mlen & 0x0fff);
1073 else {
1074 if(debuglevel > 2)
1075 printk(KERN_ERR "%s: receive-error: %04x, lance-status: %04x/%04x\n",
1076 dev->name,(int) rmdstat,csr0,(int) inw(PORT+L_DATAREG) );
1077 if(rmdstat & RCV_FRAM)
1078 p->stats.rx_frame_errors++;
1079 if(rmdstat & RCV_OFLO)
1080 p->stats.rx_over_errors++;
1081 if(rmdstat & RCV_CRC)
1082 p->stats.rx_crc_errors++;
1083 if(rmdstat & RCV_BUF_ERR)
1084 p->stats.rx_fifo_errors++;
1086 if(!(csr0 & CSR0_MISS)) /* don't count errors twice */
1087 p->stats.rx_errors++;
1089 else if( (len = (rmdp->mlen & 0x0fff) - 4) >= 60)
1091 #ifdef RCV_VIA_SKB
1092 struct sk_buff *skb = alloc_skb(R_BUF_SIZE+2+16,GFP_ATOMIC);
1093 if (skb)
1094 skb_reserve(skb,16);
1095 #else
1096 struct sk_buff *skb = dev_alloc_skb(len+2);
1097 #endif
1098 if(skb)
1100 skb_reserve(skb,2);
1101 skb->dev = dev;
1102 #ifdef RCV_VIA_SKB
1103 if( (unsigned long) (skb->data + R_BUF_SIZE) > 0x1000000) {
1104 skb_put(skb,len);
1105 eth_copy_and_sum(skb, (unsigned char *)(p->recv_skb[p->rmdnum]->data),len,0);
1107 else {
1108 struct sk_buff *skb1 = p->recv_skb[p->rmdnum];
1109 skb_put(skb,R_BUF_SIZE);
1110 p->recv_skb[p->rmdnum] = skb;
1111 rmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1112 skb = skb1;
1113 skb_trim(skb,len);
1115 #else
1116 skb_put(skb,len);
1117 eth_copy_and_sum(skb, (unsigned char *) p->recvbounce[p->rmdnum],len,0);
1118 #endif
1119 p->stats.rx_packets++;
1120 p->stats.rx_bytes += len;
1121 skb->protocol=eth_type_trans(skb,dev);
1122 netif_rx(skb);
1123 dev->last_rx = jiffies;
1125 else
1127 printk(KERN_ERR "%s: can't alloc new sk_buff\n",dev->name);
1128 p->stats.rx_dropped++;
1131 else {
1132 printk(KERN_INFO "%s: received runt packet\n",dev->name);
1133 p->stats.rx_errors++;
1135 rmdp->blen = -(R_BUF_SIZE-8);
1136 rmdp->mlen = 0;
1137 rmdp->u.s.status = RCV_OWN; /* change owner */
1138 p->rmdnum = (p->rmdnum + 1) & (RMDNUM-1);
1139 rmdp = p->rmdhead + p->rmdnum;
1144 * kick xmitter ..
1147 static void ni65_timeout(struct net_device *dev)
1149 int i;
1150 struct priv *p = (struct priv *) dev->priv;
1152 printk(KERN_ERR "%s: xmitter timed out, try to restart!\n",dev->name);
1153 for(i=0;i<TMDNUM;i++)
1154 printk("%02x ",p->tmdhead[i].u.s.status);
1155 printk("\n");
1156 ni65_lance_reinit(dev);
1157 dev->trans_start = jiffies;
1158 netif_wake_queue(dev);
1162 * Send a packet
1165 static int ni65_send_packet(struct sk_buff *skb, struct net_device *dev)
1167 struct priv *p = (struct priv *) dev->priv;
1169 netif_stop_queue(dev);
1171 if (test_and_set_bit(0, (void*)&p->lock)) {
1172 printk(KERN_ERR "%s: Queue was locked.\n", dev->name);
1173 return 1;
1177 short len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
1178 struct tmd *tmdp;
1179 unsigned long flags;
1181 #ifdef XMT_VIA_SKB
1182 if( (unsigned long) (skb->data + skb->len) > 0x1000000) {
1183 #endif
1185 memcpy((char *) p->tmdbounce[p->tmdbouncenum] ,(char *)skb->data,
1186 (skb->len > T_BUF_SIZE) ? T_BUF_SIZE : skb->len);
1187 if (len > skb->len)
1188 memset((char *)p->tmdbounce[p->tmdbouncenum]+skb->len, 0, len-skb->len);
1189 dev_kfree_skb (skb);
1191 spin_lock_irqsave(&p->ring_lock, flags);
1192 tmdp = p->tmdhead + p->tmdnum;
1193 tmdp->u.buffer = (u32) isa_virt_to_bus(p->tmdbounce[p->tmdbouncenum]);
1194 p->tmdbouncenum = (p->tmdbouncenum + 1) & (TMDNUM - 1);
1196 #ifdef XMT_VIA_SKB
1198 else {
1199 spin_lock_irqsave(&p->ring_lock, flags);
1201 tmdp = p->tmdhead + p->tmdnum;
1202 tmdp->u.buffer = (u32) isa_virt_to_bus(skb->data);
1203 p->tmd_skb[p->tmdnum] = skb;
1205 #endif
1206 tmdp->blen = -len;
1208 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;
1209 writedatareg(CSR0_TDMD | CSR0_INEA); /* enable xmit & interrupt */
1211 p->xmit_queued = 1;
1212 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
1214 if(p->tmdnum != p->tmdlast)
1215 netif_wake_queue(dev);
1217 p->lock = 0;
1218 dev->trans_start = jiffies;
1220 spin_unlock_irqrestore(&p->ring_lock, flags);
1223 return 0;
1226 static struct net_device_stats *ni65_get_stats(struct net_device *dev)
1229 #if 0
1230 int i;
1231 struct priv *p = (struct priv *) dev->priv;
1232 for(i=0;i<RMDNUM;i++)
1234 struct rmd *rmdp = p->rmdhead + ((p->rmdnum + i) & (RMDNUM-1));
1235 printk("%02x ",rmdp->u.s.status);
1237 printk("\n");
1238 #endif
1240 return &((struct priv *) dev->priv)->stats;
1243 static void set_multicast_list(struct net_device *dev)
1245 if(!ni65_lance_reinit(dev))
1246 printk(KERN_ERR "%s: Can't switch card into MC mode!\n",dev->name);
1247 netif_wake_queue(dev);
1250 #ifdef MODULE
1251 static struct net_device *dev_ni65;
1253 MODULE_PARM(irq, "i");
1254 MODULE_PARM(io, "i");
1255 MODULE_PARM(dma, "i");
1256 MODULE_PARM_DESC(irq, "ni6510 IRQ number (ignored for some cards)");
1257 MODULE_PARM_DESC(io, "ni6510 I/O base address");
1258 MODULE_PARM_DESC(dma, "ni6510 ISA DMA channel (ignored for some cards)");
1260 int init_module(void)
1262 dev_ni65 = ni65_probe(-1);
1263 return IS_ERR(dev_ni65) ? PTR_ERR(dev_ni65) : 0;
1266 void cleanup_module(void)
1268 unregister_netdev(dev_ni65);
1269 cleanup_card(dev_ni65);
1270 free_netdev(dev_ni65);
1272 #endif /* MODULE */
1274 MODULE_LICENSE("GPL");
1277 * END of ni65.c