Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

ni65.c

Go to the documentation of this file.
00001 /*
00002  * ni6510 (am7990 'lance' chip) driver for Linux-net-3
00003  * BETAcode v0.71 (96/09/29) for 2.0.0 (or later)
00004  * copyrights (c) 1994,1995,1996 by M.Hipp
00005  *
00006  * This driver can handle the old ni6510 board and the newer ni6510
00007  * EtherBlaster. (probably it also works with every full NE2100
00008  * compatible card)
00009  *
00010  * To compile as module, type:
00011  *     gcc -O2 -fomit-frame-pointer -m486 -D__KERNEL__ -DMODULE -c ni65.c
00012  * driver probes: io: 0x360,0x300,0x320,0x340 / dma: 3,5,6,7
00013  *
00014  * This is an extension to the Linux operating system, and is covered by the
00015  * same Gnu Public License that covers the Linux-kernel.
00016  *
00017  * comments/bugs/suggestions can be sent to:
00018  *   Michael Hipp
00019  *   email: hippm@informatik.uni-tuebingen.de
00020  *
00021  * sources:
00022  *   some things are from the 'ni6510-packet-driver for dos by Russ Nelson'
00023  *   and from the original drivers by D.Becker
00024  *
00025  * known problems:
00026  *   - on some PCI boards (including my own) the card/board/ISA-bridge has
00027  *     problems with bus master DMA. This results in lotsa overruns.
00028  *     It may help to '#define RCV_PARANOIA_CHECK' or try to #undef
00029  *     the XMT and RCV_VIA_SKB option .. this reduces driver performance.
00030  *     Or just play with your BIOS options to optimize ISA-DMA access.
00031  *     Maybe you also wanna play with the LOW_PERFORAMCE and MID_PERFORMANCE
00032  *     defines -> please report me your experience then
00033  *   - Harald reported for ASUS SP3G mainboards, that you should use
00034  *     the 'optimal settings' from the user's manual on page 3-12!
00035  *
00036  * credits:
00037  *   thanx to Jason Sullivan for sending me a ni6510 card!
00038  *   lot of debug runs with ASUS SP3G Boards (Intel Saturn) by Harald Koenig
00039  *
00040  * simple performance test: (486DX-33/Ni6510-EB receives from 486DX4-100/Ni6510-EB)
00041  *    average: FTP -> 8384421 bytes received in 8.5 seconds
00042  *           (no RCV_VIA_SKB,no XMT_VIA_SKB,PARANOIA_CHECK,4 XMIT BUFS, 8 RCV_BUFFS)
00043  *    peak: FTP -> 8384421 bytes received in 7.5 seconds
00044  *           (RCV_VIA_SKB,XMT_VIA_SKB,no PARANOIA_CHECK,1(!) XMIT BUF, 16 RCV BUFFS)
00045  */
00046 
00047 /*
00048  * 99.Jun.8: added support for /proc/net/dev byte count for xosview (HK)
00049  * 96.Sept.29: virt_to_bus stuff added for new memory modell
00050  * 96.April.29: Added Harald Koenig's Patches (MH)
00051  * 96.April.13: enhanced error handling .. more tests (MH)
00052  * 96.April.5/6: a lot of performance tests. Got it stable now (hopefully) (MH)
00053  * 96.April.1: (no joke ;) .. added EtherBlaster and Module support (MH)
00054  * 96.Feb.19: fixed a few bugs .. cleanups .. tested for 1.3.66 (MH)
00055  *            hopefully no more 16MB limit
00056  *
00057  * 95.Nov.18: multicast tweaked (AC).
00058  *
00059  * 94.Aug.22: changes in xmit_intr (ack more than one xmitted-packet), ni65_send_packet (p->lock) (MH)
00060  *
00061  * 94.July.16: fixed bugs in recv_skb and skb-alloc stuff  (MH)
00062  */
00063 
00064 #include <linux/kernel.h>
00065 #include <linux/sched.h>
00066 #include <linux/string.h>
00067 #include <linux/ptrace.h>
00068 #include <linux/errno.h>
00069 #include <linux/ioport.h>
00070 #include <linux/malloc.h>
00071 #include <linux/interrupt.h>
00072 #include <linux/delay.h>
00073 #include <linux/init.h>
00074 #include <asm/bitops.h>
00075 #include <asm/io.h>
00076 #include <asm/dma.h>
00077 
00078 #include <linux/netdevice.h>
00079 #include <linux/etherdevice.h>
00080 #include <linux/skbuff.h>
00081 
00082 #include <linux/version.h>
00083 #include <linux/module.h>
00084 
00085 #include "ni65.h"
00086 
00087 /*
00088  * the current setting allows an acceptable performance
00089  * for 'RCV_PARANOIA_CHECK' read the 'known problems' part in
00090  * the header of this file
00091  * 'invert' the defines for max. performance. This may cause DMA problems
00092  * on some boards (e.g on my ASUS SP3G)
00093  */
00094 #undef XMT_VIA_SKB
00095 #undef RCV_VIA_SKB
00096 #define RCV_PARANOIA_CHECK
00097 
00098 #define MID_PERFORMANCE
00099 
00100 #if   defined( LOW_PERFORMANCE )
00101  static int isa0=7,isa1=7,csr80=0x0c10;
00102 #elif defined( MID_PERFORMANCE )
00103  static int isa0=5,isa1=5,csr80=0x2810;
00104 #else   /* high performance */
00105  static int isa0=4,isa1=4,csr80=0x0017;
00106 #endif
00107 
00108 /*
00109  * a few card/vendor specific defines
00110  */
00111 #define NI65_ID0    0x00
00112 #define NI65_ID1    0x55
00113 #define NI65_EB_ID0 0x52
00114 #define NI65_EB_ID1 0x44
00115 #define NE2100_ID0  0x57
00116 #define NE2100_ID1  0x57
00117 
00118 #define PORT p->cmdr_addr
00119 
00120 /*
00121  * buffer configuration
00122  */
00123 #if 1
00124 #define RMDNUM 16
00125 #define RMDNUMMASK 0x80000000
00126 #else
00127 #define RMDNUM 8
00128 #define RMDNUMMASK 0x60000000 /* log2(RMDNUM)<<29 */
00129 #endif
00130 
00131 #if 0
00132 #define TMDNUM 1
00133 #define TMDNUMMASK 0x00000000
00134 #else
00135 #define TMDNUM 4
00136 #define TMDNUMMASK 0x40000000 /* log2(TMDNUM)<<29 */
00137 #endif
00138 
00139 /* slightly oversized */
00140 #define R_BUF_SIZE 1544
00141 #define T_BUF_SIZE 1544
00142 
00143 /*
00144  * lance register defines
00145  */
00146 #define L_DATAREG 0x00
00147 #define L_ADDRREG 0x02
00148 #define L_RESET   0x04
00149 #define L_CONFIG  0x05
00150 #define L_BUSIF   0x06
00151 
00152 /*
00153  * to access the lance/am7990-regs, you have to write
00154  * reg-number into L_ADDRREG, then you can access it using L_DATAREG
00155  */
00156 #define CSR0  0x00
00157 #define CSR1  0x01
00158 #define CSR2  0x02
00159 #define CSR3  0x03
00160 
00161 #define INIT_RING_BEFORE_START  0x1
00162 #define FULL_RESET_ON_ERROR     0x2
00163 
00164 #if 0
00165 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);inw(PORT+L_ADDRREG); \
00166                            outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
00167 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_ADDRREG),\
00168                        inw(PORT+L_DATAREG))
00169 #if 0
00170 #define writedatareg(val) {outw(val,PORT+L_DATAREG);inw(PORT+L_DATAREG);}
00171 #else
00172 #define writedatareg(val) {  writereg(val,CSR0); }
00173 #endif
00174 #else
00175 #define writereg(val,reg) {outw(reg,PORT+L_ADDRREG);outw(val,PORT+L_DATAREG);}
00176 #define readreg(reg) (outw(reg,PORT+L_ADDRREG),inw(PORT+L_DATAREG))
00177 #define writedatareg(val) { writereg(val,CSR0); }
00178 #endif
00179 
00180 static unsigned char ni_vendor[] = { 0x02,0x07,0x01 };
00181 
00182 static struct card {
00183         unsigned char id0,id1;
00184         short id_offset;
00185         short total_size;
00186         short cmd_offset;
00187         short addr_offset;
00188         unsigned char *vendor_id;
00189         char *cardname;
00190         unsigned char config;
00191 } cards[] = {
00192         { NI65_ID0,NI65_ID1,0x0e,0x10,0x0,0x8,ni_vendor,"ni6510", 0x1 } ,
00193         { NI65_EB_ID0,NI65_EB_ID1,0x0e,0x18,0x10,0x0,ni_vendor,"ni6510 EtherBlaster", 0x2 } ,
00194         { NE2100_ID0,NE2100_ID1,0x0e,0x18,0x10,0x0,NULL,"generic NE2100", 0x0 }
00195 };
00196 #define NUM_CARDS 3
00197 
00198 struct priv
00199 {
00200         struct rmd rmdhead[RMDNUM];
00201         struct tmd tmdhead[TMDNUM];
00202         struct init_block ib;
00203         int rmdnum;
00204         int tmdnum,tmdlast;
00205 #ifdef RCV_VIA_SKB
00206         struct sk_buff *recv_skb[RMDNUM];
00207 #else
00208         void *recvbounce[RMDNUM];
00209 #endif
00210 #ifdef XMT_VIA_SKB
00211         struct sk_buff *tmd_skb[TMDNUM];
00212 #endif
00213         void *tmdbounce[TMDNUM];
00214         int tmdbouncenum;
00215         int lock,xmit_queued;
00216         struct net_device_stats stats;
00217         void *self;
00218         int cmdr_addr;
00219         int cardno;
00220         int features;
00221 };
00222 
00223 static int  ni65_probe1(struct device *dev,int);
00224 static void ni65_interrupt(int irq, void * dev_id, struct pt_regs *regs);
00225 static void ni65_recv_intr(struct device *dev,int);
00226 static void ni65_xmit_intr(struct device *dev,int);
00227 static int  ni65_open(struct device *dev);
00228 static int  ni65_lance_reinit(struct device *dev);
00229 static void ni65_init_lance(struct priv *p,unsigned char*,int,int);
00230 static int  ni65_send_packet(struct sk_buff *skb, struct device *dev);
00231 static int  ni65_close(struct device *dev);
00232 static int  ni65_alloc_buffer(struct device *dev);
00233 static void ni65_free_buffer(struct priv *p);
00234 static struct net_device_stats *ni65_get_stats(struct device *);
00235 static void set_multicast_list(struct device *dev);
00236 
00237 static int irqtab[] __initdata = { 9,12,15,5 }; /* irq config-translate */
00238 static int dmatab[] __initdata = { 0,3,5,6,7 }; /* dma config-translate and autodetect */
00239 
00240 static int debuglevel = 1;
00241 
00242 /*
00243  * set 'performance' registers .. we must STOP lance for that
00244  */
00245 static void ni65_set_performance(struct priv *p)
00246 {
00247         writereg(CSR0_STOP | CSR0_CLRALL,CSR0); /* STOP */
00248 
00249         if( !(cards[p->cardno].config & 0x02) )
00250                 return;
00251 
00252         outw(80,PORT+L_ADDRREG);
00253         if(inw(PORT+L_ADDRREG) != 80)
00254                 return;
00255 
00256         writereg( (csr80 & 0x3fff) ,80); /* FIFO watermarks */
00257         outw(0,PORT+L_ADDRREG);
00258         outw((short)isa0,PORT+L_BUSIF); /* write ISA 0: DMA_R : isa0 * 50ns */
00259         outw(1,PORT+L_ADDRREG);
00260         outw((short)isa1,PORT+L_BUSIF); /* write ISA 1: DMA_W : isa1 * 50ns     */
00261 
00262         outw(CSR0,PORT+L_ADDRREG);      /* switch back to CSR0 */
00263 }
00264 
00265 /*
00266  * open interface (up)
00267  */
00268 static int ni65_open(struct device *dev)
00269 {
00270         struct priv *p = (struct priv *) dev->priv;
00271         int irqval = request_irq(dev->irq, &ni65_interrupt,0,
00272                         cards[p->cardno].cardname,dev);
00273         if (irqval) {
00274                 printk ("%s: unable to get IRQ %d (irqval=%d).\n",
00275                           dev->name,dev->irq, irqval);
00276                 return -EAGAIN;
00277         }
00278 
00279         if(ni65_lance_reinit(dev))
00280         {
00281                 dev->tbusy     = 0;
00282                 dev->interrupt = 0;
00283                 dev->start     = 1;
00284                 MOD_INC_USE_COUNT;
00285                 return 0;
00286         }
00287         else
00288         {
00289                 free_irq(dev->irq,dev);
00290                 dev->start = 0;
00291                 return -EAGAIN;
00292         }
00293 }
00294 
00295 /*
00296  * close interface (down)
00297  */
00298 static int ni65_close(struct device *dev)
00299 {
00300         struct priv *p = (struct priv *) dev->priv;
00301 
00302         outw(inw(PORT+L_RESET),PORT+L_RESET); /* that's the hard way */
00303 
00304 #ifdef XMT_VIA_SKB
00305         {
00306                 int i;
00307                 for(i=0;i<TMDNUM;i++)
00308                 {
00309                         if(p->tmd_skb[i]) {
00310                                 dev_kfree_skb(p->tmd_skb[i]);
00311                                 p->tmd_skb[i] = NULL;
00312                         }
00313                 }
00314         }
00315 #endif
00316         free_irq(dev->irq,dev);
00317         dev->tbusy = 1;
00318         dev->start = 0;
00319         MOD_DEC_USE_COUNT;
00320         return 0;
00321 }
00322 
00323 /*
00324  * Probe The Card (not the lance-chip)
00325  */
00326 #ifdef MODULE
00327 static
00328 #endif
00329 __initfunc(int ni65_probe(struct device *dev))
00330 {
00331         int *port;
00332         static int ports[] = {0x360,0x300,0x320,0x340, 0};
00333 
00334         if (dev->base_addr > 0x1ff)          /* Check a single specified location. */
00335                  return ni65_probe1(dev, dev->base_addr);
00336         else if (dev->base_addr > 0)         /* Don't probe at all. */
00337                  return -ENXIO;
00338 
00339         for (port = ports; *port; port++)
00340         {
00341                 if (ni65_probe1(dev, *port) == 0)
00342                          return 0;
00343         }
00344 
00345         return -ENODEV;
00346 }
00347 
00348 /*
00349  * this is the real card probe ..
00350  */
00351 __initfunc(static int ni65_probe1(struct device *dev,int ioaddr))
00352 {
00353         int i,j;
00354         struct priv *p;
00355         unsigned long flags;
00356 
00357         for(i=0;i<NUM_CARDS;i++) {
00358                 if(check_region(ioaddr, cards[i].total_size))
00359                         continue;
00360                 if(cards[i].id_offset >= 0) {
00361                         if(inb(ioaddr+cards[i].id_offset+0) != cards[i].id0 ||
00362                                  inb(ioaddr+cards[i].id_offset+1) != cards[i].id1) {
00363                                  continue;
00364                         }
00365                 }
00366                 if(cards[i].vendor_id) {
00367                         for(j=0;j<3;j++)
00368                                 if(inb(ioaddr+cards[i].addr_offset+j) != cards[i].vendor_id[j])
00369                                         continue;
00370                 }
00371                 break;
00372         }
00373         if(i == NUM_CARDS)
00374                 return -ENODEV;
00375 
00376         for(j=0;j<6;j++)
00377                 dev->dev_addr[j] = inb(ioaddr+cards[i].addr_offset+j);
00378 
00379         if( (j=ni65_alloc_buffer(dev)) < 0)
00380                 return j;
00381         p = (struct priv *) dev->priv;
00382         p->cmdr_addr = ioaddr + cards[i].cmd_offset;
00383         p->cardno = i;
00384 
00385         printk("%s: %s found at %#3x, ", dev->name, cards[p->cardno].cardname , ioaddr);
00386 
00387         outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
00388         if( (j=readreg(CSR0)) != 0x4) {
00389                  printk(KERN_ERR "can't RESET card: %04x\n",j);
00390                  ni65_free_buffer(p);
00391                  return -EAGAIN;
00392         }
00393 
00394         outw(88,PORT+L_ADDRREG);
00395         if(inw(PORT+L_ADDRREG) == 88) {
00396                 unsigned long v;
00397                 v = inw(PORT+L_DATAREG);
00398                 v <<= 16;
00399                 outw(89,PORT+L_ADDRREG);
00400                 v |= inw(PORT+L_DATAREG);
00401                 printk("Version %#08lx, ",v);
00402                 p->features = INIT_RING_BEFORE_START;
00403         }
00404         else {
00405                 printk("ancient LANCE, ");
00406                 p->features = 0x0;
00407         }
00408 
00409         if(test_bit(0,&cards[i].config)) {
00410                 dev->irq = irqtab[(inw(ioaddr+L_CONFIG)>>2)&3];
00411                 dev->dma = dmatab[inw(ioaddr+L_CONFIG)&3];
00412                 printk("IRQ %d (from card), DMA %d (from card).\n",dev->irq,dev->dma);
00413         }
00414         else {
00415                 if(dev->dma == 0) {
00416                 /* 'stuck test' from lance.c */
00417                         int dma_channels = ((inb(DMA1_STAT_REG) >> 4) & 0x0f) | (inb(DMA2_STAT_REG) & 0xf0);
00418                         for(i=1;i<5;i++) {
00419                                 int dma = dmatab[i];
00420                                 if(test_bit(dma,&dma_channels) || request_dma(dma,"ni6510"))
00421                                         continue;
00422                                         
00423                                 flags=claim_dma_lock();
00424                                 disable_dma(dma);
00425                                 set_dma_mode(dma,DMA_MODE_CASCADE);
00426                                 enable_dma(dma);
00427                                 release_dma_lock(flags);
00428                                 
00429                                 ni65_init_lance(p,dev->dev_addr,0,0); /* trigger memory access */
00430                                 
00431                                 flags=claim_dma_lock();
00432                                 disable_dma(dma);
00433                                 free_dma(dma);
00434                                 release_dma_lock(flags);
00435                                 
00436                                 if(readreg(CSR0) & CSR0_IDON)
00437                                         break;
00438                         }
00439                         if(i == 5) {
00440                                 printk("Can't detect DMA channel!\n");
00441                                 ni65_free_buffer(p);
00442                                 return -EAGAIN;
00443                         }
00444                         dev->dma = dmatab[i];
00445                         printk("DMA %d (autodetected), ",dev->dma);
00446                 }
00447                 else
00448                         printk("DMA %d (assigned), ",dev->dma);
00449 
00450                 if(dev->irq < 2)
00451                 {
00452                         ni65_init_lance(p,dev->dev_addr,0,0);
00453                         autoirq_setup(0);
00454                         writereg(CSR0_INIT|CSR0_INEA,CSR0); /* trigger interrupt */
00455 
00456                         if(!(dev->irq = autoirq_report(2)))
00457                         {
00458                                 printk("Failed to detect IRQ line!\n");
00459                                 ni65_free_buffer(p);
00460                                 return -EAGAIN;
00461                         }
00462                         printk("IRQ %d (autodetected).\n",dev->irq);
00463                 }
00464                 else
00465                         printk("IRQ %d (assigned).\n",dev->irq);
00466         }
00467 
00468         if(request_dma(dev->dma, cards[p->cardno].cardname ) != 0)
00469         {
00470                 printk("%s: Can't request dma-channel %d\n",dev->name,(int) dev->dma);
00471                 ni65_free_buffer(p);
00472                 return -EAGAIN;
00473         }
00474 
00475         /*
00476          * Grab the region so we can find another board.
00477          */
00478         request_region(ioaddr,cards[p->cardno].total_size,cards[p->cardno].cardname);
00479 
00480         dev->base_addr = ioaddr;
00481 
00482         dev->open               = ni65_open;
00483         dev->stop               = ni65_close;
00484         dev->hard_start_xmit    = ni65_send_packet;
00485         dev->get_stats          = ni65_get_stats;
00486         dev->set_multicast_list = set_multicast_list;
00487 
00488         ether_setup(dev);
00489 
00490         dev->interrupt          = 0;
00491         dev->tbusy              = 0;
00492         dev->start              = 0;
00493 
00494         return 0; /* everything is OK */
00495 }
00496 
00497 /*
00498  * set lance register and trigger init
00499  */
00500 static void ni65_init_lance(struct priv *p,unsigned char *daddr,int filter,int mode)
00501 {
00502         int i;
00503         u32 pib;
00504 
00505         writereg(CSR0_CLRALL|CSR0_STOP,CSR0);
00506 
00507         for(i=0;i<6;i++)
00508                 p->ib.eaddr[i] = daddr[i];
00509 
00510         for(i=0;i<8;i++)
00511                 p->ib.filter[i] = filter;
00512         p->ib.mode = mode;
00513 
00514         p->ib.trp = (u32) virt_to_bus(p->tmdhead) | TMDNUMMASK;
00515         p->ib.rrp = (u32) virt_to_bus(p->rmdhead) | RMDNUMMASK;
00516         writereg(0,CSR3);       /* busmaster/no word-swap */
00517         pib = (u32) virt_to_bus(&p->ib);
00518         writereg(pib & 0xffff,CSR1);
00519         writereg(pib >> 16,CSR2);
00520 
00521         writereg(CSR0_INIT,CSR0); /* this changes L_ADDRREG to CSR0 */
00522 
00523         for(i=0;i<32;i++)
00524         {
00525                 mdelay(4);
00526                 if(inw(PORT+L_DATAREG) & (CSR0_IDON | CSR0_MERR) )
00527                         break; /* init ok ? */
00528         }
00529 }
00530 
00531 /*
00532  * allocate memory area and check the 16MB border
00533  */
00534 static void *ni65_alloc_mem(struct device *dev,char *what,int size,int type)
00535 {
00536         struct sk_buff *skb=NULL;
00537         unsigned char *ptr;
00538         void *ret;
00539 
00540         if(type) {
00541                 ret = skb = alloc_skb(2+16+size,GFP_KERNEL|GFP_DMA);
00542                 if(!skb) {
00543                         printk("%s: unable to allocate %s memory.\n",dev->name,what);
00544                         return NULL;
00545                 }
00546                 skb->dev = dev;
00547                 skb_reserve(skb,2+16);
00548                 skb_put(skb,R_BUF_SIZE);         /* grab the whole space .. (not necessary) */
00549                 ptr = skb->data;
00550         }
00551         else {
00552                 ret = ptr = kmalloc(T_BUF_SIZE,GFP_KERNEL | GFP_DMA);
00553                 if(!ret) {
00554                         printk("%s: unable to allocate %s memory.\n",dev->name,what);
00555                         return NULL;
00556                 }
00557         }
00558         if( (u32) virt_to_bus(ptr+size) > 0x1000000) {
00559                 printk("%s: unable to allocate %s memory in lower 16MB!\n",dev->name,what);
00560                 if(type)
00561                         kfree_skb(skb);
00562                 else
00563                         kfree(ptr);
00564                 return NULL;
00565         }
00566         return ret;
00567 }
00568 
00569 /*
00570  * allocate all memory structures .. send/recv buffers etc ...
00571  */
00572 static int ni65_alloc_buffer(struct device *dev)
00573 {
00574         unsigned char *ptr;
00575         struct priv *p;
00576         int i;
00577 
00578         /*
00579          * we need 8-aligned memory ..
00580          */
00581         ptr = ni65_alloc_mem(dev,"BUFFER",sizeof(struct priv)+8,0);
00582         if(!ptr)
00583                 return -ENOMEM;
00584 
00585         p = dev->priv = (struct priv *) (((unsigned long) ptr + 7) & ~0x7);
00586         memset((char *) dev->priv,0,sizeof(struct priv));
00587         p->self = ptr;
00588 
00589         for(i=0;i<TMDNUM;i++)
00590         {
00591 #ifdef XMT_VIA_SKB
00592                 p->tmd_skb[i] = NULL;
00593 #endif
00594                 p->tmdbounce[i] = ni65_alloc_mem(dev,"XMIT",T_BUF_SIZE,0);
00595                 if(!p->tmdbounce[i]) {
00596                         ni65_free_buffer(p);
00597                         return -ENOMEM;
00598                 }
00599         }
00600 
00601         for(i=0;i<RMDNUM;i++)
00602         {
00603 #ifdef RCV_VIA_SKB
00604                 p->recv_skb[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,1);
00605                 if(!p->recv_skb[i]) {
00606                         ni65_free_buffer(p);
00607                         return -ENOMEM;
00608                 }
00609 #else
00610                 p->recvbounce[i] = ni65_alloc_mem(dev,"RECV",R_BUF_SIZE,0);
00611                 if(!p->recvbounce[i]) {
00612                         ni65_free_buffer(p);
00613                         return -ENOMEM;
00614                 }
00615 #endif
00616         }
00617 
00618         return 0; /* everything is OK */
00619 }
00620 
00621 /*
00622  * free buffers and private struct
00623  */
00624 static void ni65_free_buffer(struct priv *p)
00625 {
00626         int i;
00627 
00628         if(!p)
00629                 return;
00630 
00631         for(i=0;i<TMDNUM;i++) {
00632                 if(p->tmdbounce[i])
00633                         kfree(p->tmdbounce[i]);
00634 #ifdef XMT_VIA_SKB
00635                 if(p->tmd_skb[i])
00636                         dev_kfree_skb(p->tmd_skb[i]);
00637 #endif
00638         }
00639 
00640         for(i=0;i<RMDNUM;i++)
00641         {
00642 #ifdef RCV_VIA_SKB
00643                 if(p->recv_skb[i])
00644                         dev_kfree_skb(p->recv_skb[i]);
00645 #else
00646                 if(p->recvbounce[i])
00647                         kfree(p->recvbounce[i]);
00648 #endif
00649         }
00650         if(p->self)
00651                 kfree(p->self);
00652 }
00653 
00654 
00655 /*
00656  * stop and (re)start lance .. e.g after an error
00657  */
00658 static void ni65_stop_start(struct device *dev,struct priv *p)
00659 {
00660         int csr0 = CSR0_INEA;
00661 
00662         writedatareg(CSR0_STOP);
00663 
00664         if(debuglevel > 1)
00665                 printk("ni65_stop_start\n");
00666 
00667         if(p->features & INIT_RING_BEFORE_START) {
00668                 int i;
00669 #ifdef XMT_VIA_SKB
00670                 struct sk_buff *skb_save[TMDNUM];
00671 #endif
00672                 unsigned long buffer[TMDNUM];
00673                 short blen[TMDNUM];
00674 
00675                 if(p->xmit_queued) {
00676                         while(1) {
00677                                 if((p->tmdhead[p->tmdlast].u.s.status & XMIT_OWN))
00678                                         break;
00679                                 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
00680                                 if(p->tmdlast == p->tmdnum)
00681                                         break;
00682                         }
00683                 }
00684 
00685                 for(i=0;i<TMDNUM;i++) {
00686                         struct tmd *tmdp = p->tmdhead + i;
00687 #ifdef XMT_VIA_SKB
00688                         skb_save[i] = p->tmd_skb[i];
00689 #endif
00690                         buffer[i] = (u32) bus_to_virt(tmdp->u.buffer);
00691                         blen[i] = tmdp->blen;
00692                         tmdp->u.s.status = 0x0;
00693                 }
00694 
00695                 for(i=0;i<RMDNUM;i++) {
00696                         struct rmd *rmdp = p->rmdhead + i;
00697                         rmdp->u.s.status = RCV_OWN;
00698                 }
00699                 p->tmdnum = p->xmit_queued = 0;
00700                 writedatareg(CSR0_STRT | csr0);
00701 
00702                 for(i=0;i<TMDNUM;i++) {
00703                         int num = (i + p->tmdlast) & (TMDNUM-1);
00704                         p->tmdhead[i].u.buffer = (u32) virt_to_bus((char *)buffer[num]); /* status is part of buffer field */
00705                         p->tmdhead[i].blen = blen[num];
00706                         if(p->tmdhead[i].u.s.status & XMIT_OWN) {
00707                                  p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
00708                                  p->xmit_queued = 1;
00709          writedatareg(CSR0_TDMD | CSR0_INEA | csr0);
00710                         }
00711 #ifdef XMT_VIA_SKB
00712                         p->tmd_skb[i] = skb_save[num];
00713 #endif
00714                 }
00715                 p->rmdnum = p->tmdlast = 0;
00716                 if(!p->lock)
00717                         dev->tbusy = (p->tmdnum || !p->xmit_queued) ? 0 : 1;
00718                 dev->trans_start = jiffies;
00719         }
00720         else
00721                 writedatareg(CSR0_STRT | csr0);
00722 }
00723 
00724 /*
00725  * init lance (write init-values .. init-buffers) (open-helper)
00726  */
00727 static int ni65_lance_reinit(struct device *dev)
00728 {
00729          int i;
00730          struct priv *p = (struct priv *) dev->priv;
00731          unsigned long flags;
00732 
00733          p->lock = 0;
00734          p->xmit_queued = 0;
00735 
00736          flags=claim_dma_lock();
00737          disable_dma(dev->dma); /* I've never worked with dma, but we do it like the packetdriver */
00738          set_dma_mode(dev->dma,DMA_MODE_CASCADE);
00739          enable_dma(dev->dma);
00740          release_dma_lock(flags);
00741 
00742          outw(inw(PORT+L_RESET),PORT+L_RESET); /* first: reset the card */
00743          if( (i=readreg(CSR0) ) != 0x4)
00744          {
00745                  printk(KERN_ERR "%s: can't RESET %s card: %04x\n",dev->name,
00746                                                         cards[p->cardno].cardname,(int) i);
00747                  flags=claim_dma_lock();
00748                  disable_dma(dev->dma);
00749                  release_dma_lock(flags);
00750                  return 0;
00751          }
00752 
00753          p->rmdnum = p->tmdnum = p->tmdlast = p->tmdbouncenum = 0;
00754          for(i=0;i<TMDNUM;i++)
00755          {
00756                  struct tmd *tmdp = p->tmdhead + i;
00757 #ifdef XMT_VIA_SKB
00758                  if(p->tmd_skb[i]) {
00759                          dev_kfree_skb(p->tmd_skb[i]);
00760                          p->tmd_skb[i] = NULL;
00761                  }
00762 #endif
00763                  tmdp->u.buffer = 0x0;
00764                  tmdp->u.s.status = XMIT_START | XMIT_END;
00765                  tmdp->blen = tmdp->status2 = 0;
00766          }
00767 
00768          for(i=0;i<RMDNUM;i++)
00769          {
00770                  struct rmd *rmdp = p->rmdhead + i;
00771 #ifdef RCV_VIA_SKB
00772                  rmdp->u.buffer = (u32) virt_to_bus(p->recv_skb[i]->data);
00773 #else
00774                  rmdp->u.buffer = (u32) virt_to_bus(p->recvbounce[i]);
00775 #endif
00776                  rmdp->blen = -(R_BUF_SIZE-8);
00777                  rmdp->mlen = 0;
00778                  rmdp->u.s.status = RCV_OWN;
00779          }
00780 
00781          if(dev->flags & IFF_PROMISC)
00782                  ni65_init_lance(p,dev->dev_addr,0x00,M_PROM);
00783          else if(dev->mc_count || dev->flags & IFF_ALLMULTI)
00784                  ni65_init_lance(p,dev->dev_addr,0xff,0x0);
00785          else
00786                  ni65_init_lance(p,dev->dev_addr,0x00,0x00);
00787 
00788         /*
00789          * ni65_set_lance_mem() sets L_ADDRREG to CSR0
00790          * NOW, WE WILL NEVER CHANGE THE L_ADDRREG, CSR0 IS ALWAYS SELECTED
00791          */
00792 
00793          if(inw(PORT+L_DATAREG) & CSR0_IDON)    {
00794                  ni65_set_performance(p);
00795                                          /* init OK: start lance , enable interrupts */
00796                  writedatareg(CSR0_CLRALL | CSR0_INEA | CSR0_STRT);
00797                  return 1; /* ->OK */
00798          }
00799          printk(KERN_ERR "%s: can't init lance, status: %04x\n",dev->name,(int) inw(PORT+L_DATAREG));
00800          flags=claim_dma_lock();
00801          disable_dma(dev->dma);
00802          release_dma_lock(flags);
00803          return 0; /* ->Error */
00804 }
00805 
00806 /*
00807  * interrupt handler
00808  */
00809 static void ni65_interrupt(int irq, void * dev_id, struct pt_regs * regs)
00810 {
00811         int csr0;
00812         struct device *dev = dev_id;
00813         struct priv *p;
00814         int bcnt = 32;
00815 
00816         if (dev == NULL) {
00817                 printk (KERN_ERR "ni65_interrupt(): irq %d for unknown device.\n", irq);
00818                 return;
00819         }
00820 
00821         if(test_and_set_bit(0,(int *) &dev->interrupt)) {
00822                 printk("ni65: oops .. interrupt while proceeding interrupt\n");
00823                 return;
00824         }
00825         p = (struct priv *) dev->priv;
00826 
00827         while(--bcnt) {
00828                 csr0 = inw(PORT+L_DATAREG);
00829 
00830 #if 0
00831                 writedatareg( (csr0 & CSR0_CLRALL) ); /* ack interrupts, disable int. */
00832 #else
00833                 writedatareg( (csr0 & CSR0_CLRALL) | CSR0_INEA ); /* ack interrupts, interrupts enabled */
00834 #endif
00835 
00836                 if(!(csr0 & (CSR0_ERR | CSR0_RINT | CSR0_TINT)))
00837                         break;
00838 
00839                 if(csr0 & CSR0_RINT) /* RECV-int? */
00840                         ni65_recv_intr(dev,csr0);
00841                 if(csr0 & CSR0_TINT) /* XMIT-int? */
00842                         ni65_xmit_intr(dev,csr0);
00843 
00844                 if(csr0 & CSR0_ERR)
00845                 {
00846                         struct priv *p = (struct priv *) dev->priv;
00847                         if(debuglevel > 1)
00848                                 printk("%s: general error: %04x.\n",dev->name,csr0);
00849                         if(csr0 & CSR0_BABL)
00850                                 p->stats.tx_errors++;
00851                         if(csr0 & CSR0_MISS) {
00852                                 int i;
00853                                 for(i=0;i<RMDNUM;i++)
00854                                         printk("%02x ",p->rmdhead[i].u.s.status);
00855                                 printk("\n");
00856                                 p->stats.rx_errors++;
00857                         }
00858                         if(csr0 & CSR0_MERR) {
00859                                 if(debuglevel > 1)
00860                                         printk("%s: Ooops .. memory error: %04x.\n",dev->name,csr0);
00861                                 ni65_stop_start(dev,p);
00862                         }
00863                 }
00864         }
00865 
00866 #ifdef RCV_PARANOIA_CHECK
00867 {
00868  int j;
00869  for(j=0;j<RMDNUM;j++)
00870  {
00871         struct priv *p = (struct priv *) dev->priv;
00872         int i,k,num1,num2;
00873         for(i=RMDNUM-1;i>0;i--) {
00874                  num2 = (p->rmdnum + i) & (RMDNUM-1);
00875                  if(!(p->rmdhead[num2].u.s.status & RCV_OWN))
00876                                 break;
00877         }
00878 
00879         if(i) {
00880                 for(k=0;k<RMDNUM;k++) {
00881                         num1 = (p->rmdnum + k) & (RMDNUM-1);
00882                         if(!(p->rmdhead[num1].u.s.status & RCV_OWN))
00883                                 break;
00884                 }
00885                 if(!k)
00886                         break;
00887 
00888                 if(debuglevel > 0)
00889                 {
00890                         char buf[256],*buf1;
00891                         int k;
00892                         buf1 = buf;
00893                         for(k=0;k<RMDNUM;k++) {
00894                                 sprintf(buf1,"%02x ",(p->rmdhead[k].u.s.status)); /* & RCV_OWN) ); */
00895                                 buf1 += 3;
00896                         }
00897                         *buf1 = 0;
00898                         printk(KERN_ERR "%s: Ooops, receive ring corrupted %2d %2d | %s\n",dev->name,p->rmdnum,i,buf);
00899                 }
00900 
00901                 p->rmdnum = num1;
00902                 ni65_recv_intr(dev,csr0);
00903                 if((p->rmdhead[num2].u.s.status & RCV_OWN))
00904                         break;  /* ok, we are 'in sync' again */
00905         }
00906         else
00907                 break;
00908  }
00909 }
00910 #endif
00911 
00912         if( (csr0 & (CSR0_RXON | CSR0_TXON)) != (CSR0_RXON | CSR0_TXON) ) {
00913                 printk("%s: RX or TX was offline -> restart\n",dev->name);
00914                 ni65_stop_start(dev,p);
00915         }
00916         else
00917                 writedatareg(CSR0_INEA);
00918 
00919         dev->interrupt = 0;
00920 
00921         return;
00922 }
00923 
00924 /*
00925  * We have received an Xmit-Interrupt ..
00926  * send a new packet if necessary
00927  */
00928 static void ni65_xmit_intr(struct device *dev,int csr0)
00929 {
00930         struct priv *p = (struct priv *) dev->priv;
00931 
00932         while(p->xmit_queued)
00933         {
00934                 struct tmd *tmdp = p->tmdhead + p->tmdlast;
00935                 int tmdstat = tmdp->u.s.status;
00936 
00937                 if(tmdstat & XMIT_OWN)
00938                         break;
00939 
00940                 if(tmdstat & XMIT_ERR)
00941                 {
00942 #if 0
00943                         if(tmdp->status2 & XMIT_TDRMASK && debuglevel > 3)
00944                                 printk(KERN_ERR "%s: tdr-problems (e.g. no resistor)\n",dev->name);
00945 #endif
00946                  /* checking some errors */
00947                         if(tmdp->status2 & XMIT_RTRY)
00948                                 p->stats.tx_aborted_errors++;
00949                         if(tmdp->status2 & XMIT_LCAR)
00950                                 p->stats.tx_carrier_errors++;
00951                         if(tmdp->status2 & (XMIT_BUFF | XMIT_UFLO )) {
00952                 /* this stops the xmitter */
00953                                 p->stats.tx_fifo_errors++;
00954                                 if(debuglevel > 0)
00955                                         printk(KERN_ERR "%s: Xmit FIFO/BUFF error\n",dev->name);
00956                                 if(p->features & INIT_RING_BEFORE_START) {
00957                                         tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;    /* test: resend this frame */
00958                                         ni65_stop_start(dev,p);
00959                                         break;  /* no more Xmit processing .. */
00960                                 }
00961                                 else
00962                                  ni65_stop_start(dev,p);
00963                         }
00964                         if(debuglevel > 2)
00965                                 printk(KERN_ERR "%s: xmit-error: %04x %02x-%04x\n",dev->name,csr0,(int) tmdstat,(int) tmdp->status2);
00966                         if(!(csr0 & CSR0_BABL)) /* don't count errors twice */
00967                                 p->stats.tx_errors++;
00968                         tmdp->status2 = 0;
00969                 }
00970                 else {
00971                         p->stats.tx_bytes -= (short)(tmdp->blen);
00972                         p->stats.tx_packets++;
00973                 }
00974 
00975 #ifdef XMT_VIA_SKB
00976                 if(p->tmd_skb[p->tmdlast]) {
00977                          dev_kfree_skb(p->tmd_skb[p->tmdlast]);
00978                          p->tmd_skb[p->tmdlast] = NULL;
00979                 }
00980 #endif
00981 
00982                 p->tmdlast = (p->tmdlast + 1) & (TMDNUM-1);
00983                 if(p->tmdlast == p->tmdnum)
00984                         p->xmit_queued = 0;
00985         }
00986         dev->tbusy = 0;
00987         mark_bh(NET_BH);
00988 }
00989 
00990 /*
00991  * We have received a packet
00992  */
00993 static void ni65_recv_intr(struct device *dev,int csr0)
00994 {
00995         struct rmd *rmdp;
00996         int rmdstat,len;
00997         int cnt=0;
00998         struct priv *p = (struct priv *) dev->priv;
00999 
01000         rmdp = p->rmdhead + p->rmdnum;
01001         while(!( (rmdstat = rmdp->u.s.status) & RCV_OWN))
01002         {
01003                 cnt++;
01004                 if( (rmdstat & (RCV_START | RCV_END | RCV_ERR)) != (RCV_START | RCV_END) ) /* error or oversized? */
01005                 {
01006                         if(!(rmdstat & RCV_ERR)) {
01007                                 if(rmdstat & RCV_START)
01008                                 {
01009                                         p->stats.rx_length_errors++;
01010                                         printk(KERN_ERR "%s: recv, packet too long: %d\n",dev->name,rmdp->mlen & 0x0fff);
01011                                 }
01012                         }
01013                         else {
01014                                 if(debuglevel > 2)
01015                                         printk(KERN_ERR "%s: receive-error: %04x, lance-status: %04x/%04x\n",
01016                                                                         dev->name,(int) rmdstat,csr0,(int) inw(PORT+L_DATAREG) );
01017                                 if(rmdstat & RCV_FRAM)
01018                                         p->stats.rx_frame_errors++;
01019                                 if(rmdstat & RCV_OFLO)
01020                                         p->stats.rx_over_errors++;
01021                                 if(rmdstat & RCV_CRC)
01022                                         p->stats.rx_crc_errors++;
01023                                 if(rmdstat & RCV_BUF_ERR)
01024                                         p->stats.rx_fifo_errors++;
01025                         }
01026                         if(!(csr0 & CSR0_MISS)) /* don't count errors twice */
01027                                 p->stats.rx_errors++;
01028                 }
01029                 else if( (len = (rmdp->mlen & 0x0fff) - 4) >= 60)
01030                 {
01031 #ifdef RCV_VIA_SKB
01032                         struct sk_buff *skb = alloc_skb(R_BUF_SIZE+2+16,GFP_ATOMIC);
01033                         if (skb)
01034                                 skb_reserve(skb,16);
01035 #else
01036                         struct sk_buff *skb = dev_alloc_skb(len+2);
01037 #endif
01038                         if(skb)
01039                         {
01040                                 skb_reserve(skb,2);
01041         skb->dev = dev;
01042 #ifdef RCV_VIA_SKB
01043                                 if( (unsigned long) (skb->data + R_BUF_SIZE) > 0x1000000) {
01044                                         skb_put(skb,len);
01045                                         eth_copy_and_sum(skb, (unsigned char *)(p->recv_skb[p->rmdnum]->data),len,0);
01046                                 }
01047                                 else {
01048                                         struct sk_buff *skb1 = p->recv_skb[p->rmdnum];
01049                                         skb_put(skb,R_BUF_SIZE);
01050                                         p->recv_skb[p->rmdnum] = skb;
01051                                         rmdp->u.buffer = (u32) virt_to_bus(skb->data);
01052                                         skb = skb1;
01053                                         skb_trim(skb,len);
01054                                 }
01055 #else
01056                                 skb_put(skb,len);
01057                                 eth_copy_and_sum(skb, (unsigned char *) p->recvbounce[p->rmdnum],len,0);
01058 #endif
01059                                 p->stats.rx_packets++;
01060                                 p->stats.rx_bytes += len;
01061                                 skb->protocol=eth_type_trans(skb,dev);
01062                                 netif_rx(skb);
01063                         }
01064                         else
01065                         {
01066                                 printk(KERN_ERR "%s: can't alloc new sk_buff\n",dev->name);
01067                                 p->stats.rx_dropped++;
01068                         }
01069                 }
01070                 else {
01071                         printk(KERN_INFO "%s: received runt packet\n",dev->name);
01072                         p->stats.rx_errors++;
01073                 }
01074                 rmdp->blen = -(R_BUF_SIZE-8);
01075                 rmdp->mlen = 0;
01076                 rmdp->u.s.status = RCV_OWN; /* change owner */
01077                 p->rmdnum = (p->rmdnum + 1) & (RMDNUM-1);
01078                 rmdp = p->rmdhead + p->rmdnum;
01079         }
01080 }
01081 
01082 /*
01083  * kick xmitter ..
01084  */
01085 static int ni65_send_packet(struct sk_buff *skb, struct device *dev)
01086 {
01087         struct priv *p = (struct priv *) dev->priv;
01088 
01089         if(dev->tbusy)
01090         {
01091                 int tickssofar = jiffies - dev->trans_start;
01092                 if (tickssofar < 50)
01093                         return 1;
01094 
01095                 printk(KERN_ERR "%s: xmitter timed out, try to restart!\n",dev->name);
01096                 {
01097                         int i;
01098                         for(i=0;i<TMDNUM;i++)
01099                                 printk("%02x ",p->tmdhead[i].u.s.status);
01100                         printk("\n");
01101                 }
01102                 ni65_lance_reinit(dev);
01103                 dev->tbusy=0;
01104                 dev->trans_start = jiffies;
01105         }
01106 
01107         if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
01108                  printk(KERN_ERR "%s: Transmitter access conflict.\n", dev->name);
01109                  return 1;
01110         }
01111         if (test_and_set_bit(0, (void*)&p->lock)) {
01112                 printk(KERN_ERR "%s: Queue was locked.\n", dev->name);
01113                 return 1;
01114         }
01115 
01116         {
01117                 short len = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
01118                 struct tmd *tmdp;
01119                 long flags;
01120 
01121 #ifdef XMT_VIA_SKB
01122                 if( (unsigned long) (skb->data + skb->len) > 0x1000000) {
01123 #endif
01124 
01125                         memcpy((char *) p->tmdbounce[p->tmdbouncenum] ,(char *)skb->data,
01126                                                          (skb->len > T_BUF_SIZE) ? T_BUF_SIZE : skb->len);
01127                         if(len > skb->len)
01128                                 memset((char *)p->tmdbounce[p->tmdbouncenum]+skb->len, 0, len-skb->len);
01129                         dev_kfree_skb (skb);
01130 
01131                         save_flags(flags);
01132                         cli();
01133 
01134                         tmdp = p->tmdhead + p->tmdnum;
01135                         tmdp->u.buffer = (u32) virt_to_bus(p->tmdbounce[p->tmdbouncenum]);
01136                         p->tmdbouncenum = (p->tmdbouncenum + 1) & (TMDNUM - 1);
01137 
01138 #ifdef XMT_VIA_SKB
01139                 }
01140                 else {
01141                         save_flags(flags);
01142                         cli();
01143 
01144                         tmdp = p->tmdhead + p->tmdnum;
01145                         tmdp->u.buffer = (u32) virt_to_bus(skb->data);
01146                         p->tmd_skb[p->tmdnum] = skb;
01147                 }
01148 #endif
01149                 tmdp->blen = -len;
01150 
01151                 tmdp->u.s.status = XMIT_OWN | XMIT_START | XMIT_END;
01152                 writedatareg(CSR0_TDMD | CSR0_INEA); /* enable xmit & interrupt */
01153 
01154                 p->xmit_queued = 1;
01155                 p->tmdnum = (p->tmdnum + 1) & (TMDNUM-1);
01156 
01157                 dev->tbusy = (p->tmdnum == p->tmdlast) ? 1 : 0;
01158                 p->lock = 0;
01159                 dev->trans_start = jiffies;
01160 
01161                 restore_flags(flags);
01162         }
01163 
01164         return 0;
01165 }
01166 
01167 static struct net_device_stats *ni65_get_stats(struct device *dev)
01168 {
01169 
01170 #if 0
01171         int i;
01172         struct priv *p = (struct priv *) dev->priv;
01173         for(i=0;i<RMDNUM;i++)
01174         {
01175                 struct rmd *rmdp = p->rmdhead + ((p->rmdnum + i) & (RMDNUM-1));
01176                 printk("%02x ",rmdp->u.s.status);
01177         }
01178         printk("\n");
01179 #endif
01180 
01181         return &((struct priv *) dev->priv)->stats;
01182 }
01183 
01184 static void set_multicast_list(struct device *dev)
01185 {
01186         if(!ni65_lance_reinit(dev))
01187                 printk(KERN_ERR