ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / ni52.c
1 /*
2  * net-3-driver for the NI5210 card (i82586 Ethernet chip)
3  *
4  * This is an extension to the Linux operating system, and is covered by the
5  * same GNU General Public License that covers that work.
6  *
7  * Alphacode 0.82 (96/09/29) for Linux 2.0.0 (or later)
8  * Copyrights (c) 1994,1995,1996 by M.Hipp (hippm@informatik.uni-tuebingen.de)
9  *    [feel free to mail ....]
10  *
11  * when using as module: (no autoprobing!)
12  *   compile with:
13  *       gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ -DMODULE -c ni52.c
14  *   run with e.g:
15  *       insmod ni52.o io=0x360 irq=9 memstart=0xd0000 memend=0xd4000
16  *
17  * CAN YOU PLEASE REPORT ME YOUR PERFORMANCE EXPERIENCES !!.
18  *
19  * If you find a bug, please report me:
20  *   The kernel panic output and any kmsg from the ni52 driver
21  *   the ni5210-driver-version and the linux-kernel version
22  *   how many shared memory (memsize) on the netcard,
23  *   bootprom: yes/no, base_addr, mem_start
24  *   maybe the ni5210-card revision and the i82586 version
25  *
26  * autoprobe for: base_addr: 0x300,0x280,0x360,0x320,0x340
27  *                mem_start: 0xd0000,0xd2000,0xc8000,0xca000,0xd4000,0xd6000,
28  *                           0xd8000,0xcc000,0xce000,0xda000,0xdc000
29  *
30  * sources:
31  *   skeleton.c from Donald Becker
32  *
33  * I have also done a look in the following sources: (mail me if you need them)
34  *   crynwr-packet-driver by Russ Nelson
35  *   Garret A. Wollman's (fourth) i82586-driver for BSD
36  *   (before getting an i82596 (yes 596 not 586) manual, the existing drivers helped
37  *    me a lot to understand this tricky chip.)
38  *
39  * Known Problems:
40  *   The internal sysbus seems to be slow. So we often lose packets because of
41  *   overruns while receiving from a fast remote host.
42  *   This can slow down TCP connections. Maybe the newer ni5210 cards are better.
43  *   my experience is, that if a machine sends with more than about 500-600K/s
44  *   the fifo/sysbus overflows.
45  *
46  * IMPORTANT NOTE:
47  *   On fast networks, it's a (very) good idea to have 16K shared memory. With
48  *   8K, we can store only 4 receive frames, so it can (easily) happen that a remote
49  *   machine 'overruns' our system.
50  *
51  * Known i82586/card problems (I'm sure, there are many more!):
52  *   Running the NOP-mode, the i82586 sometimes seems to forget to report
53  *   every xmit-interrupt until we restart the CU.
54  *   Another MAJOR bug is, that the RU sometimes seems to ignore the EL-Bit
55  *   in the RBD-Struct which indicates an end of the RBD queue.
56  *   Instead, the RU fetches another (randomly selected and
57  *   usually used) RBD and begins to fill it. (Maybe, this happens only if
58  *   the last buffer from the previous RFD fits exact into the queue and
59  *   the next RFD can't fetch an initial RBD. Anyone knows more? )
60  *
61  * results from ftp performance tests with Linux 1.2.5
62  *   send and receive about 350-400 KByte/s (peak up to 460 kbytes/s)
63  *   sending in NOP-mode: peak performance up to 530K/s (but better don't run this mode)
64  */
65
66 /*
67  * 29.Sept.96: virt_to_bus changes for new memory scheme
68  * 19.Feb.96: more Mcast changes, module support (MH)
69  *
70  * 18.Nov.95: Mcast changes (AC).
71  *
72  * 23.April.95: fixed(?) receiving problems by configuring a RFD more
73  *              than the number of RBD's. Can maybe cause other problems.
74  * 18.April.95: Added MODULE support (MH)
75  * 17.April.95: MC related changes in init586() and set_multicast_list().
76  *              removed use of 'jiffies' in init586() (MH)
77  *
78  * 19.Sep.94: Added Multicast support (not tested yet) (MH)
79  *
80  * 18.Sep.94: Workaround for 'EL-Bug'. Removed flexible RBD-handling.
81  *            Now, every RFD has exact one RBD. (MH)
82  *
83  * 14.Sep.94: added promiscuous mode, a few cleanups (MH)
84  *
85  * 19.Aug.94: changed request_irq() parameter (MH)
86  *
87  * 20.July.94: removed cleanup bugs, removed a 16K-mem-probe-bug (MH)
88  *
89  * 19.July.94: lotsa cleanups .. (MH)
90  *
91  * 17.July.94: some patches ... verified to run with 1.1.29 (MH)
92  *
93  * 4.July.94: patches for Linux 1.1.24  (MH)
94  *
95  * 26.March.94: patches for Linux 1.0 and iomem-auto-probe (MH)
96  *
97  * 30.Sep.93: Added nop-chain .. driver now runs with only one Xmit-Buff, too (MH)
98  *
99  * < 30.Sep.93: first versions
100  */
101
102 static int debuglevel;  /* debug-printk 0: off 1: a few 2: more */
103 static int automatic_resume; /* experimental .. better should be zero */
104 static int rfdadd;      /* rfdadd=1 may be better for 8K MEM cards */
105 static int fifo=0x8;    /* don't change */
106
107 /* #define REALLY_SLOW_IO */
108
109 #include <linux/module.h>
110 #include <linux/kernel.h>
111 #include <linux/string.h>
112 #include <linux/errno.h>
113 #include <linux/ioport.h>
114 #include <linux/slab.h>
115 #include <linux/interrupt.h>
116 #include <linux/delay.h>
117 #include <linux/init.h>
118 #include <asm/bitops.h>
119 #include <asm/io.h>
120
121 #include <linux/netdevice.h>
122 #include <linux/etherdevice.h>
123 #include <linux/skbuff.h>
124
125 #include "ni52.h"
126
127 #define DEBUG       /* debug on */
128 #define SYSBUSVAL 1 /* 8 Bit */
129
130 #define ni_attn586()  {outb(0,dev->base_addr+NI52_ATTENTION);}
131 #define ni_reset586() {outb(0,dev->base_addr+NI52_RESET);}
132 #define ni_disint()   {outb(0,dev->base_addr+NI52_INTDIS);}
133 #define ni_enaint()   {outb(0,dev->base_addr+NI52_INTENA);}
134
135 #define make32(ptr16) (p->memtop + (short) (ptr16) )
136 #define make24(ptr32) ( ((char *) (ptr32)) - p->base)
137 #define make16(ptr32) ((unsigned short) ((unsigned long)(ptr32) - (unsigned long) p->memtop ))
138
139 /******************* how to calculate the buffers *****************************
140
141   * IMPORTANT NOTE: if you configure only one NUM_XMIT_BUFFS, the driver works
142   * --------------- in a different (more stable?) mode. Only in this mode it's
143   *                 possible to configure the driver with 'NO_NOPCOMMANDS'
144
145 sizeof(scp)=12; sizeof(scb)=16; sizeof(iscp)=8;
146 sizeof(scp)+sizeof(iscp)+sizeof(scb) = 36 = INIT
147 sizeof(rfd) = 24; sizeof(rbd) = 12;
148 sizeof(tbd) = 8; sizeof(transmit_cmd) = 16;
149 sizeof(nop_cmd) = 8;
150
151   * if you don't know the driver, better do not change these values: */
152
153 #define RECV_BUFF_SIZE 1524 /* slightly oversized */
154 #define XMIT_BUFF_SIZE 1524 /* slightly oversized */
155 #define NUM_XMIT_BUFFS 1    /* config for both, 8K and 16K shmem */
156 #define NUM_RECV_BUFFS_8  4 /* config for 8K shared mem */
157 #define NUM_RECV_BUFFS_16 9 /* config for 16K shared mem */
158 #define NO_NOPCOMMANDS      /* only possible with NUM_XMIT_BUFFS=1 */
159
160 /**************************************************************************/
161
162 /* different DELAYs */
163 #define DELAY(x) mdelay(32 * x);
164 #define DELAY_16(); { udelay(16); }
165 #define DELAY_18(); { udelay(4); }
166
167 /* wait for command with timeout: */
168 #define WAIT_4_SCB_CMD() \
169 { int i; \
170   for(i=0;i<16384;i++) { \
171     if(!p->scb->cmd_cuc) break; \
172     DELAY_18(); \
173     if(i == 16383) { \
174       printk("%s: scb_cmd timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_cuc,p->scb->cus); \
175        if(!p->reseted) { p->reseted = 1; ni_reset586(); } } } }
176
177 #define WAIT_4_SCB_CMD_RUC() { int i; \
178   for(i=0;i<16384;i++) { \
179     if(!p->scb->cmd_ruc) break; \
180     DELAY_18(); \
181     if(i == 16383) { \
182       printk("%s: scb_cmd (ruc) timed out: %04x,%04x .. disabling i82586!!\n",dev->name,p->scb->cmd_ruc,p->scb->rus); \
183        if(!p->reseted) { p->reseted = 1; ni_reset586(); } } } }
184
185 #define WAIT_4_STAT_COMPL(addr) { int i; \
186    for(i=0;i<32767;i++) { \
187      if((addr)->cmd_status & STAT_COMPL) break; \
188      DELAY_16(); DELAY_16(); } }
189
190 #define NI52_TOTAL_SIZE 16
191 #define NI52_ADDR0 0x02
192 #define NI52_ADDR1 0x07
193 #define NI52_ADDR2 0x01
194
195 static int     ni52_probe1(struct net_device *dev,int ioaddr);
196 static irqreturn_t ni52_interrupt(int irq,void *dev_id,struct pt_regs *reg_ptr);
197 static int     ni52_open(struct net_device *dev);
198 static int     ni52_close(struct net_device *dev);
199 static int     ni52_send_packet(struct sk_buff *,struct net_device *);
200 static struct  net_device_stats *ni52_get_stats(struct net_device *dev);
201 static void    set_multicast_list(struct net_device *dev);
202 static void    ni52_timeout(struct net_device *dev);
203 #if 0
204 static void    ni52_dump(struct net_device *,void *);
205 #endif
206
207 /* helper-functions */
208 static int     init586(struct net_device *dev);
209 static int     check586(struct net_device *dev,char *where,unsigned size);
210 static void    alloc586(struct net_device *dev);
211 static void    startrecv586(struct net_device *dev);
212 static void   *alloc_rfa(struct net_device *dev,void *ptr);
213 static void    ni52_rcv_int(struct net_device *dev);
214 static void    ni52_xmt_int(struct net_device *dev);
215 static void    ni52_rnr_int(struct net_device *dev);
216
217 struct priv
218 {
219         struct net_device_stats stats;
220         unsigned long base;
221         char *memtop;
222         long int lock;
223         int reseted;
224         volatile struct rfd_struct      *rfd_last,*rfd_top,*rfd_first;
225         volatile struct scp_struct      *scp;   /* volatile is important */
226         volatile struct iscp_struct     *iscp;  /* volatile is important */
227         volatile struct scb_struct      *scb;   /* volatile is important */
228         volatile struct tbd_struct      *xmit_buffs[NUM_XMIT_BUFFS];
229 #if (NUM_XMIT_BUFFS == 1)
230         volatile struct transmit_cmd_struct *xmit_cmds[2];
231         volatile struct nop_cmd_struct *nop_cmds[2];
232 #else
233         volatile struct transmit_cmd_struct *xmit_cmds[NUM_XMIT_BUFFS];
234         volatile struct nop_cmd_struct *nop_cmds[NUM_XMIT_BUFFS];
235 #endif
236         volatile int            nop_point,num_recv_buffs;
237         volatile char           *xmit_cbuffs[NUM_XMIT_BUFFS];
238         volatile int            xmit_count,xmit_last;
239 };
240
241 /**********************************************
242  * close device
243  */
244 static int ni52_close(struct net_device *dev)
245 {
246         free_irq(dev->irq, dev);
247
248         ni_reset586(); /* the hard way to stop the receiver */
249
250         netif_stop_queue(dev);
251
252         return 0;
253 }
254
255 /**********************************************
256  * open device
257  */
258 static int ni52_open(struct net_device *dev)
259 {
260         int ret;
261
262         ni_disint();
263         alloc586(dev);
264         init586(dev);
265         startrecv586(dev);
266         ni_enaint();
267
268         ret = request_irq(dev->irq, &ni52_interrupt,0,dev->name,dev);
269         if (ret)
270         {
271                 ni_reset586();
272                 return ret;
273         }
274
275         netif_start_queue(dev);
276
277         return 0; /* most done by init */
278 }
279
280 /**********************************************
281  * Check to see if there's an 82586 out there.
282  */
283 static int check586(struct net_device *dev,char *where,unsigned size)
284 {
285         struct priv pb;
286         struct priv *p = /* (struct priv *) dev->priv*/ &pb;
287         char *iscp_addrs[2];
288         int i;
289
290         p->base = (unsigned long) isa_bus_to_virt((unsigned long)where) + size - 0x01000000;
291         p->memtop = isa_bus_to_virt((unsigned long)where) + size;
292         p->scp = (struct scp_struct *)(p->base + SCP_DEFAULT_ADDRESS);
293         memset((char *)p->scp,0, sizeof(struct scp_struct));
294         for(i=0;i<sizeof(struct scp_struct);i++) /* memory was writeable? */
295                 if(((char *)p->scp)[i])
296                         return 0;
297         p->scp->sysbus = SYSBUSVAL;                             /* 1 = 8Bit-Bus, 0 = 16 Bit */
298         if(p->scp->sysbus != SYSBUSVAL)
299                 return 0;
300
301         iscp_addrs[0] = isa_bus_to_virt((unsigned long)where);
302         iscp_addrs[1]= (char *) p->scp - sizeof(struct iscp_struct);
303
304         for(i=0;i<2;i++)
305         {
306                 p->iscp = (struct iscp_struct *) iscp_addrs[i];
307                 memset((char *)p->iscp,0, sizeof(struct iscp_struct));
308
309                 p->scp->iscp = make24(p->iscp);
310                 p->iscp->busy = 1;
311
312                 ni_reset586();
313                 ni_attn586();
314                 DELAY(1);       /* wait a while... */
315
316                 if(p->iscp->busy) /* i82586 clears 'busy' after successful init */
317                         return 0;
318         }
319         return 1;
320 }
321
322 /******************************************************************
323  * set iscp at the right place, called by ni52_probe1 and open586.
324  */
325 static void alloc586(struct net_device *dev)
326 {
327         struct priv *p =        (struct priv *) dev->priv;
328
329         ni_reset586();
330         DELAY(1);
331
332         p->scp  = (struct scp_struct *) (p->base + SCP_DEFAULT_ADDRESS);
333         p->scb  = (struct scb_struct *) isa_bus_to_virt(dev->mem_start);
334         p->iscp = (struct iscp_struct *) ((char *)p->scp - sizeof(struct iscp_struct));
335
336         memset((char *) p->iscp,0,sizeof(struct iscp_struct));
337         memset((char *) p->scp ,0,sizeof(struct scp_struct));
338
339         p->scp->iscp = make24(p->iscp);
340         p->scp->sysbus = SYSBUSVAL;
341         p->iscp->scb_offset = make16(p->scb);
342
343         p->iscp->busy = 1;
344         ni_reset586();
345         ni_attn586();
346
347         DELAY(1);
348
349         if(p->iscp->busy)
350                 printk("%s: Init-Problems (alloc).\n",dev->name);
351
352         p->reseted = 0;
353
354         memset((char *)p->scb,0,sizeof(struct scb_struct));
355 }
356
357 /* set: io,irq,memstart,memend or set it when calling insmod */
358 static int irq=9;
359 static int io=0x300;
360 static long memstart;   /* e.g 0xd0000 */
361 static long memend;     /* e.g 0xd4000 */
362
363 /**********************************************
364  * probe the ni5210-card
365  */
366 struct net_device * __init ni52_probe(int unit)
367 {
368         struct net_device *dev = alloc_etherdev(sizeof(struct priv));
369         static int ports[] = {0x300, 0x280, 0x360 , 0x320 , 0x340, 0};
370         int *port;
371         int err = 0;
372
373         if (!dev)
374                 return ERR_PTR(-ENOMEM);
375
376         if (unit >= 0) {
377                 sprintf(dev->name, "eth%d", unit);
378                 netdev_boot_setup_check(dev);
379                 io = dev->base_addr;
380                 irq = dev->irq;
381                 memstart = dev->mem_start;
382                 memend = dev->mem_end;
383         }
384
385         SET_MODULE_OWNER(dev);
386
387         if (io > 0x1ff) {       /* Check a single specified location. */
388                 err = ni52_probe1(dev, io);
389         } else if (io > 0) {            /* Don't probe at all. */
390                 err = -ENXIO;
391         } else {
392                 for (port = ports; *port && ni52_probe1(dev, *port) ; port++)
393                         ;
394                 if (*port)
395                         goto got_it;
396 #ifdef FULL_IO_PROBE
397                 for (io = 0x200; io < 0x400 && ni52_probe1(dev, io); io += 8)
398                         ;
399                 if (io < 0x400)
400                         goto got_it;
401 #endif
402                 err = -ENODEV;
403         }
404         if (err)
405                 goto out;
406 got_it:
407         err = register_netdev(dev);
408         if (err)
409                 goto out1;
410         return dev;
411 out1:
412         release_region(dev->base_addr, NI52_TOTAL_SIZE);
413 out:
414         free_netdev(dev);
415         return ERR_PTR(err);
416 }
417
418 static int __init ni52_probe1(struct net_device *dev,int ioaddr)
419 {
420         int i, size, retval;
421
422         dev->base_addr = ioaddr;
423         dev->irq = irq;
424         dev->mem_start = memstart;
425         dev->mem_end = memend;
426
427         if (!request_region(ioaddr, NI52_TOTAL_SIZE, dev->name))
428                 return -EBUSY;
429
430         if( !(inb(ioaddr+NI52_MAGIC1) == NI52_MAGICVAL1) ||
431             !(inb(ioaddr+NI52_MAGIC2) == NI52_MAGICVAL2)) {
432                 retval = -ENODEV;
433                 goto out;
434         }
435
436         for(i=0;i<ETH_ALEN;i++)
437                 dev->dev_addr[i] = inb(dev->base_addr+i);
438
439         if(dev->dev_addr[0] != NI52_ADDR0 || dev->dev_addr[1] != NI52_ADDR1
440                  || dev->dev_addr[2] != NI52_ADDR2) {
441                 retval = -ENODEV;
442                 goto out;
443         }
444
445         printk(KERN_INFO "%s: NI5210 found at %#3lx, ",dev->name,dev->base_addr);
446
447         /*
448          * check (or search) IO-Memory, 8K and 16K
449          */
450 #ifdef MODULE
451         size = dev->mem_end - dev->mem_start;
452         if(size != 0x2000 && size != 0x4000) {
453                 printk("\n%s: Illegal memory size %d. Allowed is 0x2000 or 0x4000 bytes.\n",dev->name,size);
454                 retval = -ENODEV;
455                 goto out;
456         }
457         if(!check586(dev,(char *) dev->mem_start,size)) {
458                 printk("?memcheck, Can't find memory at 0x%lx with size %d!\n",dev->mem_start,size);
459                 retval = -ENODEV;
460                 goto out;
461         }
462 #else
463         if(dev->mem_start != 0) /* no auto-mem-probe */
464         {
465                 size = 0x4000; /* check for 16K mem */
466                 if(!check586(dev,(char *) dev->mem_start,size)) {
467                         size = 0x2000; /* check for 8K mem */
468                         if(!check586(dev,(char *) dev->mem_start,size)) {
469                                 printk("?memprobe, Can't find memory at 0x%lx!\n",dev->mem_start);
470                                 retval = -ENODEV;
471                                 goto out;
472                         }
473                 }
474         }
475         else
476         {
477                 static long memaddrs[] = { 0xc8000,0xca000,0xcc000,0xce000,0xd0000,0xd2000,
478                                         0xd4000,0xd6000,0xd8000,0xda000,0xdc000, 0 };
479                 for(i=0;;i++)
480                 {
481                         if(!memaddrs[i]) {
482                                 printk("?memprobe, Can't find io-memory!\n");
483                                 retval = -ENODEV;
484                                 goto out;
485                         }
486                         dev->mem_start = memaddrs[i];
487                         size = 0x2000; /* check for 8K mem */
488                         if(check586(dev,(char *)dev->mem_start,size)) /* 8K-check */
489                                 break;
490                         size = 0x4000; /* check for 16K mem */
491                         if(check586(dev,(char *)dev->mem_start,size)) /* 16K-check */
492                                 break;
493                 }
494         }
495         dev->mem_end = dev->mem_start + size; /* set mem_end showed by 'ifconfig' */
496 #endif
497
498         memset((char *) dev->priv,0,sizeof(struct priv));
499
500         ((struct priv *) (dev->priv))->memtop = isa_bus_to_virt(dev->mem_start) + size;
501         ((struct priv *) (dev->priv))->base =   (unsigned long) isa_bus_to_virt(dev->mem_start) + size - 0x01000000;
502         alloc586(dev);
503
504         /* set number of receive-buffs according to memsize */
505         if(size == 0x2000)
506                 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_8;
507         else
508                 ((struct priv *) dev->priv)->num_recv_buffs = NUM_RECV_BUFFS_16;
509
510         printk("Memaddr: 0x%lx, Memsize: %d, ",dev->mem_start,size);
511
512         if(dev->irq < 2)
513         {
514                 unsigned long irq_mask;
515
516                 irq_mask = probe_irq_on();
517                 ni_reset586();
518                 ni_attn586();
519
520                 mdelay(20);
521                 dev->irq = probe_irq_off(irq_mask);
522                 if(!dev->irq)
523                 {
524                         printk("?autoirq, Failed to detect IRQ line!\n");
525                         retval = -EAGAIN;
526                         goto out;
527                 }
528                 printk("IRQ %d (autodetected).\n",dev->irq);
529         }
530         else    {
531                 if(dev->irq == 2)
532                         dev->irq = 9;
533                 printk("IRQ %d (assigned and not checked!).\n",dev->irq);
534         }
535
536         dev->open               = ni52_open;
537         dev->stop               = ni52_close;
538         dev->get_stats          = ni52_get_stats;
539         dev->tx_timeout         = ni52_timeout;
540         dev->watchdog_timeo     = HZ/20;
541         dev->hard_start_xmit    = ni52_send_packet;
542         dev->set_multicast_list = set_multicast_list;
543
544         dev->if_port            = 0;
545
546         return 0;
547 out:
548         release_region(ioaddr, NI52_TOTAL_SIZE);
549         return retval;
550 }
551
552 /**********************************************
553  * init the chip (ni52-interrupt should be disabled?!)
554  * needs a correct 'allocated' memory
555  */
556
557 static int init586(struct net_device *dev)
558 {
559         void *ptr;
560         int i,result=0;
561         struct priv *p = (struct priv *) dev->priv;
562         volatile struct configure_cmd_struct    *cfg_cmd;
563         volatile struct iasetup_cmd_struct *ias_cmd;
564         volatile struct tdr_cmd_struct *tdr_cmd;
565         volatile struct mcsetup_cmd_struct *mc_cmd;
566         struct dev_mc_list *dmi=dev->mc_list;
567         int num_addrs=dev->mc_count;
568
569         ptr = (void *) ((char *)p->scb + sizeof(struct scb_struct));
570
571         cfg_cmd = (struct configure_cmd_struct *)ptr; /* configure-command */
572         cfg_cmd->cmd_status     = 0;
573         cfg_cmd->cmd_cmd        = CMD_CONFIGURE | CMD_LAST;
574         cfg_cmd->cmd_link       = 0xffff;
575
576         cfg_cmd->byte_cnt       = 0x0a; /* number of cfg bytes */
577         cfg_cmd->fifo           = fifo; /* fifo-limit (8=tx:32/rx:64) */
578         cfg_cmd->sav_bf         = 0x40; /* hold or discard bad recv frames (bit 7) */
579         cfg_cmd->adr_len        = 0x2e; /* addr_len |!src_insert |pre-len |loopback */
580         cfg_cmd->priority       = 0x00;
581         cfg_cmd->ifs            = 0x60;
582         cfg_cmd->time_low       = 0x00;
583         cfg_cmd->time_high      = 0xf2;
584         cfg_cmd->promisc        = 0;
585         if(dev->flags & IFF_ALLMULTI) {
586                 int len = ((char *) p->iscp - (char *) ptr - 8) / 6;
587                 if(num_addrs > len)     {
588                         printk("%s: switching to promisc. mode\n",dev->name);
589                         dev->flags|=IFF_PROMISC;
590                 }
591         }
592         if(dev->flags&IFF_PROMISC)
593         {
594                          cfg_cmd->promisc=1;
595                          dev->flags|=IFF_PROMISC;
596         }
597         cfg_cmd->carr_coll      = 0x00;
598
599         p->scb->cbl_offset      = make16(cfg_cmd);
600         p->scb->cmd_ruc         = 0;
601
602         p->scb->cmd_cuc         = CUC_START; /* cmd.-unit start */
603         ni_attn586();
604
605         WAIT_4_STAT_COMPL(cfg_cmd);
606
607         if((cfg_cmd->cmd_status & (STAT_OK|STAT_COMPL)) != (STAT_COMPL|STAT_OK))
608         {
609                 printk("%s: configure command failed: %x\n",dev->name,cfg_cmd->cmd_status);
610                 return 1;
611         }
612
613         /*
614          * individual address setup
615          */
616
617         ias_cmd = (struct iasetup_cmd_struct *)ptr;
618
619         ias_cmd->cmd_status     = 0;
620         ias_cmd->cmd_cmd        = CMD_IASETUP | CMD_LAST;
621         ias_cmd->cmd_link       = 0xffff;
622
623         memcpy((char *)&ias_cmd->iaddr,(char *) dev->dev_addr,ETH_ALEN);
624
625         p->scb->cbl_offset = make16(ias_cmd);
626
627         p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
628         ni_attn586();
629
630         WAIT_4_STAT_COMPL(ias_cmd);
631
632         if((ias_cmd->cmd_status & (STAT_OK|STAT_COMPL)) != (STAT_OK|STAT_COMPL)) {
633                 printk("%s (ni52): individual address setup command failed: %04x\n",dev->name,ias_cmd->cmd_status);
634                 return 1;
635         }
636
637         /*
638          * TDR, wire check .. e.g. no resistor e.t.c
639          */
640          
641         tdr_cmd = (struct tdr_cmd_struct *)ptr;
642
643         tdr_cmd->cmd_status     = 0;
644         tdr_cmd->cmd_cmd        = CMD_TDR | CMD_LAST;
645         tdr_cmd->cmd_link       = 0xffff;
646         tdr_cmd->status         = 0;
647
648         p->scb->cbl_offset = make16(tdr_cmd);
649         p->scb->cmd_cuc = CUC_START; /* cmd.-unit start */
650         ni_attn586();
651
652         WAIT_4_STAT_COMPL(tdr_cmd);
653
654         if(!(tdr_cmd->cmd_status & STAT_COMPL))
655         {
656                 printk("%s: Problems while running the TDR.\n",dev->name);
657         }
658         else
659         {
660                 DELAY_16(); /* wait for result */
661                 result = tdr_cmd->status;
662
663                 p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
664                 ni_attn586(); /* ack the interrupts */
665
666                 if(result & TDR_LNK_OK)
667                         ;
668                 else if(result & TDR_XCVR_PRB)
669                         printk("%s: TDR: Transceiver problem. Check the cable(s)!\n",dev->name);
670                 else if(result & TDR_ET_OPN)
671                         printk("%s: TDR: No correct termination %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
672                 else if(result & TDR_ET_SRT)
673                 {
674                         if (result & TDR_TIMEMASK) /* time == 0 -> strange :-) */
675                                 printk("%s: TDR: Detected a short circuit %d clocks away.\n",dev->name,result & TDR_TIMEMASK);
676                 }
677                 else
678                         printk("%s: TDR: Unknown status %04x\n",dev->name,result);
679         }
680
681         /*
682          * Multicast setup
683          */
684         if(num_addrs && !(dev->flags & IFF_PROMISC) )
685         {
686                 mc_cmd = (struct mcsetup_cmd_struct *) ptr;
687                 mc_cmd->cmd_status = 0;
688                 mc_cmd->cmd_cmd = CMD_MCSETUP | CMD_LAST;
689                 mc_cmd->cmd_link = 0xffff;
690                 mc_cmd->mc_cnt = num_addrs * 6;
691
692                 for(i=0;i<num_addrs;i++,dmi=dmi->next)
693                         memcpy((char *) mc_cmd->mc_list[i], dmi->dmi_addr,6);
694
695                 p->scb->cbl_offset = make16(mc_cmd);
696                 p->scb->cmd_cuc = CUC_START;
697                 ni_attn586();
698
699                 WAIT_4_STAT_COMPL(mc_cmd);
700
701                 if( (mc_cmd->cmd_status & (STAT_COMPL|STAT_OK)) != (STAT_COMPL|STAT_OK) )
702                         printk("%s: Can't apply multicast-address-list.\n",dev->name);
703         }
704
705         /*
706          * alloc nop/xmit-cmds
707          */
708 #if (NUM_XMIT_BUFFS == 1)
709         for(i=0;i<2;i++)
710         {
711                 p->nop_cmds[i]                  = (struct nop_cmd_struct *)ptr;
712                 p->nop_cmds[i]->cmd_cmd         = CMD_NOP;
713                 p->nop_cmds[i]->cmd_status      = 0;
714                 p->nop_cmds[i]->cmd_link        = make16((p->nop_cmds[i]));
715                 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
716         }
717 #else
718         for(i=0;i<NUM_XMIT_BUFFS;i++)
719         {
720                 p->nop_cmds[i]                  = (struct nop_cmd_struct *)ptr;
721                 p->nop_cmds[i]->cmd_cmd         = CMD_NOP;
722                 p->nop_cmds[i]->cmd_status      = 0;
723                 p->nop_cmds[i]->cmd_link        = make16((p->nop_cmds[i]));
724                 ptr = (char *) ptr + sizeof(struct nop_cmd_struct);
725         }
726 #endif
727
728         ptr = alloc_rfa(dev,(void *)ptr); /* init receive-frame-area */
729
730         /*
731          * alloc xmit-buffs / init xmit_cmds
732          */
733         for(i=0;i<NUM_XMIT_BUFFS;i++)
734         {
735                 p->xmit_cmds[i] = (struct transmit_cmd_struct *)ptr; /*transmit cmd/buff 0*/
736                 ptr = (char *) ptr + sizeof(struct transmit_cmd_struct);
737                 p->xmit_cbuffs[i] = (char *)ptr; /* char-buffs */
738                 ptr = (char *) ptr + XMIT_BUFF_SIZE;
739                 p->xmit_buffs[i] = (struct tbd_struct *)ptr; /* TBD */
740                 ptr = (char *) ptr + sizeof(struct tbd_struct);
741                 if((void *)ptr > (void *)p->iscp)
742                 {
743                         printk("%s: not enough shared-mem for your configuration!\n",dev->name);
744                         return 1;
745                 }
746                 memset((char *)(p->xmit_cmds[i]) ,0, sizeof(struct transmit_cmd_struct));
747                 memset((char *)(p->xmit_buffs[i]),0, sizeof(struct tbd_struct));
748                 p->xmit_cmds[i]->cmd_link = make16(p->nop_cmds[(i+1)%NUM_XMIT_BUFFS]);
749                 p->xmit_cmds[i]->cmd_status = STAT_COMPL;
750                 p->xmit_cmds[i]->cmd_cmd = CMD_XMIT | CMD_INT;
751                 p->xmit_cmds[i]->tbd_offset = make16((p->xmit_buffs[i]));
752                 p->xmit_buffs[i]->next = 0xffff;
753                 p->xmit_buffs[i]->buffer = make24((p->xmit_cbuffs[i]));
754         }
755
756         p->xmit_count = 0;
757         p->xmit_last    = 0;
758 #ifndef NO_NOPCOMMANDS
759         p->nop_point    = 0;
760 #endif
761
762          /*
763                 * 'start transmitter'
764                 */
765 #ifndef NO_NOPCOMMANDS
766         p->scb->cbl_offset = make16(p->nop_cmds[0]);
767         p->scb->cmd_cuc = CUC_START;
768         ni_attn586();
769         WAIT_4_SCB_CMD();
770 #else
771         p->xmit_cmds[0]->cmd_link = make16(p->xmit_cmds[0]);
772         p->xmit_cmds[0]->cmd_cmd        = CMD_XMIT | CMD_SUSPEND | CMD_INT;
773 #endif
774
775         /*
776          * ack. interrupts
777          */
778         p->scb->cmd_cuc = p->scb->cus & STAT_MASK;
779         ni_attn586();
780         DELAY_16();
781
782         ni_enaint();
783
784         return 0;
785 }
786
787 /******************************************************
788  * This is a helper routine for ni52_rnr_int() and init586().
789  * It sets up the Receive Frame Area (RFA).
790  */
791
792 static void *alloc_rfa(struct net_device *dev,void *ptr)
793 {
794         volatile struct rfd_struct *rfd = (struct rfd_struct *)ptr;
795         volatile struct rbd_struct *rbd;
796         int i;
797         struct priv *p = (struct priv *) dev->priv;
798
799         memset((char *) rfd,0,sizeof(struct rfd_struct)*(p->num_recv_buffs+rfdadd));
800         p->rfd_first = rfd;
801
802         for(i = 0; i < (p->num_recv_buffs+rfdadd); i++) {
803                 rfd[i].next = make16(rfd + (i+1) % (p->num_recv_buffs+rfdadd) );
804                 rfd[i].rbd_offset = 0xffff;
805         }
806         rfd[p->num_recv_buffs-1+rfdadd].last = RFD_SUSP;         /* RU suspend */
807
808         ptr = (void *) (rfd + (p->num_recv_buffs + rfdadd) );
809
810         rbd = (struct rbd_struct *) ptr;
811         ptr = (void *) (rbd + p->num_recv_buffs);
812
813          /* clr descriptors */
814         memset((char *) rbd,0,sizeof(struct rbd_struct)*(p->num_recv_buffs));
815
816         for(i=0;i<p->num_recv_buffs;i++)
817         {
818                 rbd[i].next = make16((rbd + (i+1) % p->num_recv_buffs));
819                 rbd[i].size = RECV_BUFF_SIZE;
820                 rbd[i].buffer = make24(ptr);
821                 ptr = (char *) ptr + RECV_BUFF_SIZE;
822         }
823
824         p->rfd_top      = p->rfd_first;
825         p->rfd_last = p->rfd_first + (p->num_recv_buffs - 1 + rfdadd);
826
827         p->scb->rfa_offset              = make16(p->rfd_first);
828         p->rfd_first->rbd_offset        = make16(rbd);
829
830         return ptr;
831 }
832
833
834 /**************************************************
835  * Interrupt Handler ...
836  */
837
838 static irqreturn_t ni52_interrupt(int irq,void *dev_id,struct pt_regs *reg_ptr)
839 {
840         struct net_device *dev = dev_id;
841         unsigned short stat;
842         int cnt=0;
843         struct priv *p;
844
845         if (!dev) {
846                 printk ("ni5210-interrupt: irq %d for unknown device.\n",irq);
847                 return IRQ_NONE;
848         }
849         p = (struct priv *) dev->priv;
850
851         if(debuglevel > 1)
852                 printk("I");
853
854         WAIT_4_SCB_CMD(); /* wait for last command      */
855
856         while((stat=p->scb->cus & STAT_MASK))
857         {
858                 p->scb->cmd_cuc = stat;
859                 ni_attn586();
860
861                 if(stat & STAT_FR)       /* received a frame */
862                         ni52_rcv_int(dev);
863
864                 if(stat & STAT_RNR) /* RU went 'not ready' */
865                 {
866                         printk("(R)");
867                         if(p->scb->rus & RU_SUSPEND) /* special case: RU_SUSPEND */
868                         {
869                                 WAIT_4_SCB_CMD();
870                                 p->scb->cmd_ruc = RUC_RESUME;
871                                 ni_attn586();
872                                 WAIT_4_SCB_CMD_RUC();
873                         }
874                         else
875                         {
876                                 printk("%s: Receiver-Unit went 'NOT READY': %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->rus);
877                                 ni52_rnr_int(dev);
878                         }
879                 }
880
881                 if(stat & STAT_CX)              /* command with I-bit set complete */
882                          ni52_xmt_int(dev);
883
884 #ifndef NO_NOPCOMMANDS
885                 if(stat & STAT_CNA)     /* CU went 'not ready' */
886                 {
887                         if(netif_running(dev))
888                                 printk("%s: oops! CU has left active state. stat: %04x/%02x.\n",dev->name,(int) stat,(int) p->scb->cus);
889                 }
890 #endif
891
892                 if(debuglevel > 1)
893                         printk("%d",cnt++);
894
895                 WAIT_4_SCB_CMD(); /* wait for ack. (ni52_xmt_int can be faster than ack!!) */
896                 if(p->scb->cmd_cuc)      /* timed out? */
897                 {
898                         printk("%s: Acknowledge timed out.\n",dev->name);
899                         ni_disint();
900                         break;
901                 }
902         }
903
904         if(debuglevel > 1)
905                 printk("i");
906         return IRQ_HANDLED;
907 }
908
909 /*******************************************************
910  * receive-interrupt
911  */
912
913 static void ni52_rcv_int(struct net_device *dev)
914 {
915         int status,cnt=0;
916         unsigned short totlen;
917         struct sk_buff *skb;
918         struct rbd_struct *rbd;
919         struct priv *p = (struct priv *) dev->priv;
920
921         if(debuglevel > 0)
922                 printk("R");
923
924         for(;(status = p->rfd_top->stat_high) & RFD_COMPL;)
925         {
926                         rbd = (struct rbd_struct *) make32(p->rfd_top->rbd_offset);
927
928                         if(status & RFD_OK) /* frame received without error? */
929                         {
930                                 if( (totlen = rbd->status) & RBD_LAST) /* the first and the last buffer? */
931                                 {
932                                         totlen &= RBD_MASK; /* length of this frame */
933                                         rbd->status = 0;
934                                         skb = (struct sk_buff *) dev_alloc_skb(totlen+2);
935                                         if(skb != NULL)
936                                         {
937                                                 skb->dev = dev;
938                                                 skb_reserve(skb,2);
939                                                 skb_put(skb,totlen);
940                                                 eth_copy_and_sum(skb,(char *) p->base+(unsigned long) rbd->buffer,totlen,0);
941                                                 skb->protocol=eth_type_trans(skb,dev);
942                                                 netif_rx(skb);
943                                                 dev->last_rx = jiffies;
944                                                 p->stats.rx_packets++;
945                                                 p->stats.rx_bytes += totlen;
946                                         }
947                                         else
948                                                 p->stats.rx_dropped++;
949                                 }
950                                 else
951                                 {
952                                         int rstat;
953                                                  /* free all RBD's until RBD_LAST is set */
954                                         totlen = 0;
955                                         while(!((rstat=rbd->status) & RBD_LAST))
956                                         {
957                                                 totlen += rstat & RBD_MASK;
958                                                 if(!rstat)
959                                                 {
960                                                         printk("%s: Whoops .. no end mark in RBD list\n",dev->name);
961                                                         break;
962                                                 }
963                                                 rbd->status = 0;
964                                                 rbd = (struct rbd_struct *) make32(rbd->next);
965                                         }
966                                         totlen += rstat & RBD_MASK;
967                                         rbd->status = 0;
968                                         printk("%s: received oversized frame! length: %d\n",dev->name,totlen);
969                                         p->stats.rx_dropped++;
970                          }
971                 }
972                 else /* frame !(ok), only with 'save-bad-frames' */
973                 {
974                         printk("%s: oops! rfd-error-status: %04x\n",dev->name,status);
975                         p->stats.rx_errors++;
976                 }
977                 p->rfd_top->stat_high = 0;
978                 p->rfd_top->last = RFD_SUSP; /* maybe exchange by RFD_LAST */
979                 p->rfd_top->rbd_offset = 0xffff;
980                 p->rfd_last->last = 0;                          /* delete RFD_SUSP      */
981                 p->rfd_last = p->rfd_top;
982                 p->rfd_top = (struct rfd_struct *) make32(p->rfd_top->next); /* step to next RFD */
983                 p->scb->rfa_offset = make16(p->rfd_top);
984
985                 if(debuglevel > 0)
986                         printk("%d",cnt++);
987         }
988
989         if(automatic_resume)
990         {
991                 WAIT_4_SCB_CMD();
992                 p->scb->cmd_ruc = RUC_RESUME;
993                 ni_attn586();
994                 WAIT_4_SCB_CMD_RUC();
995         }
996
997 #ifdef WAIT_4_BUSY
998         {
999                 int i;
1000                 for(i=0;i<1024;i++)
1001                 {
1002                         if(p->rfd_top->status)
1003                                 break;
1004                         DELAY_16();
1005                         if(i == 1023)
1006                                 printk("%s: RU hasn't fetched next RFD (not busy/complete)\n",dev->name);
1007                 }
1008         }
1009 #endif
1010
1011 #if 0
1012         if(!at_least_one)
1013         {
1014                 int i;
1015                 volatile struct rfd_struct *rfds=p->rfd_top;
1016                 volatile struct rbd_struct *rbds;
1017                 printk("%s: received a FC intr. without having a frame: %04x %d\n",dev->name,status,old_at_least);
1018                 for(i=0;i< (p->num_recv_buffs+4);i++)
1019                 {
1020                         rbds = (struct rbd_struct *) make32(rfds->rbd_offset);
1021                         printk("%04x:%04x ",rfds->status,rbds->status);
1022                         rfds = (struct rfd_struct *) make32(rfds->next);
1023                 }
1024                 printk("\nerrs: %04x %04x stat: %04x\n",(int)p->scb->rsc_errs,(int)p->scb->ovrn_errs,(int)p->scb->status);
1025                 printk("\nerrs: %04x %04x rus: %02x, cus: %02x\n",(int)p->scb->rsc_errs,(int)p->scb->ovrn_errs,(int)p->scb->rus,(int)p->scb->cus);
1026         }
1027         old_at_least = at_least_one;
1028 #endif
1029
1030         if(debuglevel > 0)
1031                 printk("r");
1032 }
1033
1034 /**********************************************************
1035  * handle 'Receiver went not ready'.
1036  */
1037
1038 static void ni52_rnr_int(struct net_device *dev)
1039 {
1040         struct priv *p = (struct priv *) dev->priv;
1041
1042         p->stats.rx_errors++;
1043
1044         WAIT_4_SCB_CMD();               /* wait for the last cmd, WAIT_4_FULLSTAT?? */
1045         p->scb->cmd_ruc = RUC_ABORT; /* usually the RU is in the 'no resource'-state .. abort it now. */
1046         ni_attn586();
1047         WAIT_4_SCB_CMD_RUC();           /* wait for accept cmd. */
1048
1049         alloc_rfa(dev,(char *)p->rfd_first);
1050 /* maybe add a check here, before restarting the RU */
1051         startrecv586(dev); /* restart RU */
1052
1053         printk("%s: Receive-Unit restarted. Status: %04x\n",dev->name,p->scb->rus);
1054
1055 }
1056
1057 /**********************************************************
1058  * handle xmit - interrupt
1059  */
1060
1061 static void ni52_xmt_int(struct net_device *dev)
1062 {
1063         int status;
1064         struct priv *p = (struct priv *) dev->priv;
1065
1066         if(debuglevel > 0)
1067                 printk("X");
1068
1069         status = p->xmit_cmds[p->xmit_last]->cmd_status;
1070         if(!(status & STAT_COMPL))
1071                 printk("%s: strange .. xmit-int without a 'COMPLETE'\n",dev->name);
1072
1073         if(status & STAT_OK)
1074         {
1075                 p->stats.tx_packets++;
1076                 p->stats.collisions += (status & TCMD_MAXCOLLMASK);
1077         }
1078         else
1079         {
1080                 p->stats.tx_errors++;
1081                 if(status & TCMD_LATECOLL) {
1082                         printk("%s: late collision detected.\n",dev->name);
1083                         p->stats.collisions++;
1084                 }
1085                 else if(status & TCMD_NOCARRIER) {
1086                         p->stats.tx_carrier_errors++;
1087                         printk("%s: no carrier detected.\n",dev->name);
1088                 }
1089                 else if(status & TCMD_LOSTCTS)
1090                         printk("%s: loss of CTS detected.\n",dev->name);
1091                 else if(status & TCMD_UNDERRUN) {
1092                         p->stats.tx_fifo_errors++;
1093                         printk("%s: DMA underrun detected.\n",dev->name);
1094                 }
1095                 else if(status & TCMD_MAXCOLL) {
1096                         printk("%s: Max. collisions exceeded.\n",dev->name);
1097                         p->stats.collisions += 16;
1098                 }
1099         }
1100
1101 #if (NUM_XMIT_BUFFS > 1)
1102         if( (++p->xmit_last) == NUM_XMIT_BUFFS)
1103                 p->xmit_last = 0;
1104 #endif
1105         netif_wake_queue(dev);
1106 }
1107
1108 /***********************************************************
1109  * (re)start the receiver
1110  */
1111
1112 static void startrecv586(struct net_device *dev)
1113 {
1114         struct priv *p = (struct priv *) dev->priv;
1115
1116         WAIT_4_SCB_CMD();
1117         WAIT_4_SCB_CMD_RUC();
1118         p->scb->rfa_offset = make16(p->rfd_first);
1119         p->scb->cmd_ruc = RUC_START;
1120         ni_attn586();           /* start cmd. */
1121         WAIT_4_SCB_CMD_RUC();   /* wait for accept cmd. (no timeout!!) */
1122 }
1123
1124 static void ni52_timeout(struct net_device *dev)
1125 {
1126         struct priv *p = (struct priv *) dev->priv;
1127 #ifndef NO_NOPCOMMANDS
1128         if(p->scb->cus & CU_ACTIVE) /* COMMAND-UNIT active? */
1129         {
1130                 netif_wake_queue(dev);
1131 #ifdef DEBUG
1132                 printk("%s: strange ... timeout with CU active?!?\n",dev->name);
1133                 printk("%s: X0: %04x N0: %04x N1: %04x %d\n",dev->name,(int)p->xmit_cmds[0]->cmd_status,(int)p->nop_cmds[0]->cmd_status,(int)p->nop_cmds[1]->cmd_status,(int)p->nop_point);
1134 #endif
1135                 p->scb->cmd_cuc = CUC_ABORT;
1136                 ni_attn586();
1137                 WAIT_4_SCB_CMD();
1138                 p->scb->cbl_offset = make16(p->nop_cmds[p->nop_point]);
1139                 p->scb->cmd_cuc = CUC_START;
1140                 ni_attn586();
1141                 WAIT_4_SCB_CMD();
1142                 dev->trans_start = jiffies;
1143                 return 0;
1144         }
1145 #endif
1146         {
1147 #ifdef DEBUG
1148                 printk("%s: xmitter timed out, try to restart! stat: %02x\n",dev->name,p->scb->cus);
1149                 printk("%s: command-stats: %04x %04x\n",dev->name,p->xmit_cmds[0]->cmd_status,p->xmit_cmds[1]->cmd_status);
1150                 printk("%s: check, whether you set the right interrupt number!\n",dev->name);
1151 #endif
1152                 ni52_close(dev);
1153                 ni52_open(dev);
1154         }
1155         dev->trans_start = jiffies;
1156 }
1157
1158 /******************************************************
1159  * send frame
1160  */
1161
1162 static int ni52_send_packet(struct sk_buff *skb, struct net_device *dev)
1163 {
1164         int len,i;
1165 #ifndef NO_NOPCOMMANDS
1166         int next_nop;
1167 #endif
1168         struct priv *p = (struct priv *) dev->priv;
1169
1170         if(skb->len > XMIT_BUFF_SIZE)
1171         {
1172                 printk("%s: Sorry, max. framelength is %d bytes. The length of your frame is %d bytes.\n",dev->name,XMIT_BUFF_SIZE,skb->len);
1173                 return 0;
1174         }
1175
1176         netif_stop_queue(dev);
1177
1178 #if(NUM_XMIT_BUFFS > 1)
1179         if(test_and_set_bit(0,(void *) &p->lock)) {
1180                 printk("%s: Queue was locked\n",dev->name);
1181                 return 1;
1182         }
1183         else
1184 #endif
1185         {
1186                 memcpy((char *)p->xmit_cbuffs[p->xmit_count],(char *)(skb->data),skb->len);
1187                 len = skb->len;
1188                 if (len < ETH_ZLEN) {
1189                         len = ETH_ZLEN;
1190                         memset((char *)p->xmit_cbuffs[p->xmit_count]+skb->len, 0, len - skb->len);
1191                 }
1192
1193 #if (NUM_XMIT_BUFFS == 1)
1194 #       ifdef NO_NOPCOMMANDS
1195
1196 #ifdef DEBUG
1197                 if(p->scb->cus & CU_ACTIVE)
1198                 {
1199                         printk("%s: Hmmm .. CU is still running and we wanna send a new packet.\n",dev->name);
1200                         printk("%s: stat: %04x %04x\n",dev->name,p->scb->cus,p->xmit_cmds[0]->cmd_status);
1201                 }
1202 #endif
1203
1204                 p->xmit_buffs[0]->size = TBD_LAST | len;
1205                 for(i=0;i<16;i++)
1206                 {
1207                         p->xmit_cmds[0]->cmd_status = 0;
1208                         WAIT_4_SCB_CMD();
1209                         if( (p->scb->cus & CU_STATUS) == CU_SUSPEND)
1210                                 p->scb->cmd_cuc = CUC_RESUME;
1211                         else
1212                         {
1213                                 p->scb->cbl_offset = make16(p->xmit_cmds[0]);
1214                                 p->scb->cmd_cuc = CUC_START;
1215                         }
1216
1217                         ni_attn586();
1218                         dev->trans_start = jiffies;
1219                         if(!i)
1220                                 dev_kfree_skb(skb);
1221                         WAIT_4_SCB_CMD();
1222                         if( (p->scb->cus & CU_ACTIVE)) /* test it, because CU sometimes doesn't start immediately */
1223                                 break;
1224                         if(p->xmit_cmds[0]->cmd_status)
1225                                 break;
1226                         if(i==15)
1227                                 printk("%s: Can't start transmit-command.\n",dev->name);
1228                 }
1229 #       else
1230                 next_nop = (p->nop_point + 1) & 0x1;
1231                 p->xmit_buffs[0]->size = TBD_LAST | len;
1232
1233                 p->xmit_cmds[0]->cmd_link        = p->nop_cmds[next_nop]->cmd_link
1234                                                                                                                                 = make16((p->nop_cmds[next_nop]));
1235                 p->xmit_cmds[0]->cmd_status = p->nop_cmds[next_nop]->cmd_status = 0;
1236
1237                 p->nop_cmds[p->nop_point]->cmd_link = make16((p->xmit_cmds[0]));
1238                 dev->trans_start = jiffies;
1239                 p->nop_point = next_nop;
1240                 dev_kfree_skb(skb);
1241 #       endif
1242 #else
1243                 p->xmit_buffs[p->xmit_count]->size = TBD_LAST | len;
1244                 if( (next_nop = p->xmit_count + 1) == NUM_XMIT_BUFFS )
1245                         next_nop = 0;
1246
1247                 p->xmit_cmds[p->xmit_count]->cmd_status = 0;
1248                 /* linkpointer of xmit-command already points to next nop cmd */
1249                 p->nop_cmds[next_nop]->cmd_link = make16((p->nop_cmds[next_nop]));
1250                 p->nop_cmds[next_nop]->cmd_status = 0;
1251
1252                 p->nop_cmds[p->xmit_count]->cmd_link = make16((p->xmit_cmds[p->xmit_count]));
1253                 dev->trans_start = jiffies;
1254                 p->xmit_count = next_nop;
1255
1256                 {
1257                         unsigned long flags;
1258                         save_flags(flags);
1259                         cli();
1260                         if(p->xmit_count != p->xmit_last)
1261                                 netif_wake_queue(dev);
1262                         p->lock = 0;
1263                         restore_flags(flags);
1264                 }
1265                 dev_kfree_skb(skb);
1266 #endif
1267         }
1268         return 0;
1269 }
1270
1271 /*******************************************
1272  * Someone wanna have the statistics
1273  */
1274
1275 static struct net_device_stats *ni52_get_stats(struct net_device *dev)
1276 {
1277         struct priv *p = (struct priv *) dev->priv;
1278         unsigned short crc,aln,rsc,ovrn;
1279
1280         crc = p->scb->crc_errs; /* get error-statistic from the ni82586 */
1281         p->scb->crc_errs = 0;
1282         aln = p->scb->aln_errs;
1283         p->scb->aln_errs = 0;
1284         rsc = p->scb->rsc_errs;
1285         p->scb->rsc_errs = 0;
1286         ovrn = p->scb->ovrn_errs;
1287         p->scb->ovrn_errs = 0;
1288
1289         p->stats.rx_crc_errors += crc;
1290         p->stats.rx_fifo_errors += ovrn;
1291         p->stats.rx_frame_errors += aln;
1292         p->stats.rx_dropped += rsc;
1293
1294         return &p->stats;
1295 }
1296
1297 /********************************************************
1298  * Set MC list ..
1299  */
1300
1301 static void set_multicast_list(struct net_device *dev)
1302 {
1303         netif_stop_queue(dev);
1304         ni_disint();
1305         alloc586(dev);
1306         init586(dev);
1307         startrecv586(dev);
1308         ni_enaint();
1309         netif_wake_queue(dev);
1310 }
1311
1312 #ifdef MODULE
1313 static struct net_device *dev_ni52;
1314
1315 MODULE_PARM(io, "i");
1316 MODULE_PARM(irq, "i");
1317 MODULE_PARM(memstart, "l");
1318 MODULE_PARM(memend, "l");
1319 MODULE_PARM_DESC(io, "NI5210 I/O base address,required");
1320 MODULE_PARM_DESC(irq, "NI5210 IRQ number,required");
1321 MODULE_PARM_DESC(memstart, "NI5210 memory base address,required");
1322 MODULE_PARM_DESC(memend, "NI5210 memory end address,required");
1323
1324 int init_module(void)
1325 {
1326         if(io <= 0x0 || !memend || !memstart || irq < 2) {
1327                 printk("ni52: Autoprobing not allowed for modules.\nni52: Set symbols 'io' 'irq' 'memstart' and 'memend'\n");
1328                 return -ENODEV;
1329         }
1330         dev_ni52 = ni52_probe(-1);
1331         if (IS_ERR(dev_ni52))
1332                 return PTR_ERR(dev_ni52);
1333         return 0;
1334 }
1335
1336 void cleanup_module(void)
1337 {
1338         unregister_netdev(dev_ni52);
1339         release_region(dev_ni52->base_addr, NI52_TOTAL_SIZE);
1340         free_netdev(dev_ni52);
1341 }
1342 #endif /* MODULE */
1343
1344 #if 0
1345 /*
1346  * DUMP .. we expect a not running CMD unit and enough space
1347  */
1348 void ni52_dump(struct net_device *dev,void *ptr)
1349 {
1350         struct priv *p = (struct priv *) dev->priv;
1351         struct dump_cmd_struct *dump_cmd = (struct dump_cmd_struct *) ptr;
1352         int i;
1353
1354         p->scb->cmd_cuc = CUC_ABORT;
1355         ni_attn586();
1356         WAIT_4_SCB_CMD();
1357         WAIT_4_SCB_CMD_RUC();
1358
1359         dump_cmd->cmd_status = 0;
1360         dump_cmd->cmd_cmd = CMD_DUMP | CMD_LAST;
1361         dump_cmd->dump_offset = make16((dump_cmd + 1));
1362         dump_cmd->cmd_link = 0xffff;
1363
1364         p->scb->cbl_offset = make16(dump_cmd);
1365         p->scb->cmd_cuc = CUC_START;
1366         ni_attn586();
1367         WAIT_4_STAT_COMPL(dump_cmd);
1368
1369         if( (dump_cmd->cmd_status & (STAT_COMPL|STAT_OK)) != (STAT_COMPL|STAT_OK) )
1370                                 printk("%s: Can't get dump information.\n",dev->name);
1371
1372         for(i=0;i<170;i++) {
1373                 printk("%02x ",(int) ((unsigned char *) (dump_cmd + 1))[i]);
1374                 if(i % 24 == 23)
1375                         printk("\n");
1376         }
1377         printk("\n");
1378 }
1379 #endif
1380 MODULE_LICENSE("GPL");
1381
1382 /*
1383  * END: linux/drivers/net/ni52.c
1384  */