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