linux-bk/drivers/net/bagetlance.c
<<
>>
Prefs
   1/* $Id$
   2 * bagetlance.c: Ethernet driver for VME Lance cards on Baget/MIPS
   3 *      This code stealed and adopted from linux/drivers/net/atarilance.c
   4 *      See that for author info
   5 *
   6 * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
   7 */
   8
   9/* 
  10 * Driver code for Baget/Lance taken from atarilance.c, which also
  11 * works well in case of Besta. Most significant changes made here
  12 * related with 16BIT-only access to A24 space.
  13 */
  14
  15static char *version = "bagetlance.c: v1.1 11/10/98\n";
  16
  17#include <linux/module.h>
  18#include <linux/stddef.h>
  19#include <linux/kernel.h>
  20#include <linux/string.h>
  21#include <linux/errno.h>
  22#include <linux/slab.h>
  23#include <linux/interrupt.h>
  24#include <linux/init.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/skbuff.h>
  28
  29#include <asm/irq.h>
  30#include <asm/bitops.h>
  31#include <asm/io.h>
  32#include <asm/baget/baget.h>
  33
  34#define BAGET_LANCE_IRQ  BAGET_IRQ_MASK(0xdf)
  35
  36/*
  37 *  Define following if you don't need 16BIT-only access to Lance memory
  38 *  (Normally BAGET needs it)
  39 */
  40#undef NORMAL_MEM_ACCESS 
  41
  42/* Debug level:
  43 *  0 = silent, print only serious errors
  44 *  1 = normal, print error messages
  45 *  2 = debug, print debug infos
  46 *  3 = debug, print even more debug infos (packet data)
  47 */
  48
  49#define LANCE_DEBUG     1  
  50
  51#ifdef LANCE_DEBUG
  52static int lance_debug = LANCE_DEBUG;
  53#else
  54static int lance_debug = 1;
  55#endif
  56MODULE_PARM(lance_debug, "i");
  57MODULE_PARM_DESC(lance_debug, "Lance debug level (0-3)");
  58MODULE_LICENSE("GPL");
  59
  60/* Print debug messages on probing? */
  61#undef LANCE_DEBUG_PROBE
  62
  63#define DPRINTK(n,a)                                                    \
  64        do {                                                                            \
  65                if (lance_debug >= n)                                   \
  66                        printk a;                                                       \
  67        } while( 0 )
  68
  69#ifdef LANCE_DEBUG_PROBE
  70# define PROBE_PRINT(a) printk a
  71#else
  72# define PROBE_PRINT(a)
  73#endif
  74
  75/* These define the number of Rx and Tx buffers as log2. (Only powers
  76 * of two are valid)
  77 * Much more rx buffers (32) are reserved than tx buffers (8), since receiving
  78 * is more time critical then sending and packets may have to remain in the
  79 * board's memory when main memory is low.
  80 */
  81
  82/* Baget Lance has 64K on-board memory, so it looks we can't increase
  83   buffer quantity (40*1.5K is about 64K) */
  84
  85#define TX_LOG_RING_SIZE                        3
  86#define RX_LOG_RING_SIZE                        5
  87
  88/* These are the derived values */
  89
  90#define TX_RING_SIZE                    (1 << TX_LOG_RING_SIZE)
  91#define TX_RING_LEN_BITS                (TX_LOG_RING_SIZE << 5)
  92#define TX_RING_MOD_MASK                (TX_RING_SIZE - 1)
  93
  94#define RX_RING_SIZE                    (1 << RX_LOG_RING_SIZE)
  95#define RX_RING_LEN_BITS                (RX_LOG_RING_SIZE << 5)
  96#define RX_RING_MOD_MASK                (RX_RING_SIZE - 1)
  97
  98/* The LANCE Rx and Tx ring descriptors. */
  99struct lance_rx_head {
 100        volatile unsigned short base;           /* Low word of base addr */
 101#ifdef NORMAL_MEM_ACCESS
 102       /* Following two fields are joined into one short to guarantee
 103                  16BIT access to Baget lance registers */
 104        volatile unsigned char  flag;
 105        unsigned char                   base_hi;        /* High word of base addr (unused) */
 106#else
 107/* Following macros are used as replecements to 8BIT fields */
 108#define GET_FLAG(h)    (((h)->flag_base_hi >> 8) & 0xff)
 109#define SET_FLAG(h,f)  (h)->flag_base_hi = ((h)->flag_base_hi & 0xff) | \
 110                                                                (((unsigned)(f)) << 8)
 111        volatile unsigned short flag_base_hi; 
 112#endif
 113        volatile short                  buf_length;     /* This length is 2s complement! */
 114        volatile short                  msg_length;     /* This length is "normal". */
 115};
 116
 117
 118struct lance_tx_head {
 119        volatile unsigned short base;           /* Low word of base addr */
 120#ifdef NORMAL_MEM_ACCESS 
 121/* See comments above about 8BIT-access Baget A24-space problems */
 122        volatile unsigned char  flag;
 123        unsigned char                   base_hi;        /* High word of base addr (unused) */
 124#else
 125        volatile unsigned short  flag_base_hi;
 126#endif
 127        volatile short                  length;         /* Length is 2s complement! */
 128        volatile short                  misc;
 129};
 130
 131struct ringdesc {
 132        volatile unsigned short adr_lo;         /* Low 16 bits of address */
 133#ifdef NORMAL_MEM_ACCESS 
 134/* See comments above about 8BIT-access Bage A24-space problems */
 135        unsigned char   len;            /* Length bits */
 136        unsigned char   adr_hi;         /* High 8 bits of address (unused) */
 137#else
 138        volatile unsigned short  len_adr_hi;
 139#endif
 140};
 141
 142/* The LANCE initialization block, described in databook. */
 143struct lance_init_block {
 144        unsigned short  mode;           /* Pre-set mode */
 145        unsigned char   hwaddr[6];      /* Physical ethernet address */
 146        unsigned                filter[2];      /* Multicast filter (unused). */
 147        /* Receive and transmit ring base, along with length bits. */
 148        struct ringdesc rx_ring;
 149        struct ringdesc tx_ring;
 150};
 151
 152/* The whole layout of the Lance shared memory */
 153struct lance_memory {
 154        struct lance_init_block init;
 155        struct lance_tx_head    tx_head[TX_RING_SIZE];
 156        struct lance_rx_head    rx_head[RX_RING_SIZE];
 157        char                                    packet_area[0]; /* packet data follow after the
 158                                                                                         * init block and the ring
 159                                                                                         * descriptors and are located
 160                                                                                         * at runtime */
 161};
 162
 163/* RieblCard specifics:
 164 * The original TOS driver for these cards reserves the area from offset
 165 * 0xee70 to 0xeebb for storing configuration data. Of interest to us is the
 166 * Ethernet address there, and the magic for verifying the data's validity.
 167 * The reserved area isn't touch by packet buffers. Furthermore, offset 0xfffe
 168 * is reserved for the interrupt vector number.
 169 */
 170#define RIEBL_RSVD_START        0xee70
 171#define RIEBL_RSVD_END          0xeec0
 172#define RIEBL_MAGIC                     0x09051990
 173#define RIEBL_MAGIC_ADDR        ((unsigned long *)(((char *)MEM) + 0xee8a))
 174#define RIEBL_HWADDR_ADDR       ((unsigned char *)(((char *)MEM) + 0xee8e))
 175#define RIEBL_IVEC_ADDR         ((unsigned short *)(((char *)MEM) + 0xfffe))
 176
 177/* This is a default address for the old RieblCards without a battery
 178 * that have no ethernet address at boot time. 00:00:36:04 is the
 179 * prefix for Riebl cards, the 00:00 at the end is arbitrary.
 180 */
 181
 182static unsigned char OldRieblDefHwaddr[6] = {
 183        0x00, 0x00, 0x36, 0x04, 0x00, 0x00
 184};
 185
 186/* I/O registers of the Lance chip */
 187
 188struct lance_ioreg {
 189/* base+0x0 */  volatile unsigned short data;
 190/* base+0x2 */  volatile unsigned short addr;
 191                                unsigned char                   _dummy1[3];
 192/* base+0x7 */  volatile unsigned char  ivec;
 193                                unsigned char                   _dummy2[5];
 194/* base+0xd */  volatile unsigned char  eeprom;
 195                                unsigned char                   _dummy3;
 196/* base+0xf */  volatile unsigned char  mem;
 197};
 198
 199/* Types of boards this driver supports */
 200
 201enum lance_type {
 202        OLD_RIEBL,              /* old Riebl card without battery */
 203        NEW_RIEBL,              /* new Riebl card with battery */
 204        PAM_CARD                /* PAM card with EEPROM */
 205};
 206
 207static char *lance_names[] = {
 208        "Riebl-Card (without battery)",
 209        "Riebl-Card (with battery)",
 210        "PAM intern card"
 211};
 212
 213/* The driver's private device structure */
 214
 215struct lance_private {
 216        enum lance_type         cardtype;
 217        struct lance_ioreg      *iobase;
 218        struct lance_memory     *mem;
 219        int                                     cur_rx, cur_tx; /* The next free ring entry */
 220        int                                     dirty_tx;               /* Ring entries to be freed. */
 221                                                /* copy function */
 222        void                            *(*memcpy_f)( void *, const void *, size_t );
 223        struct net_device_stats stats;
 224/* These two must be longs for set_bit() */
 225        long                            tx_full;
 226        long                            lock;
 227};
 228
 229/* I/O register access macros */
 230
 231#define MEM             lp->mem
 232#define DREG    IO->data
 233#define AREG    IO->addr
 234#define REGA(a) ( AREG = (a), DREG )
 235
 236/* Definitions for packet buffer access: */
 237#define PKT_BUF_SZ              1544
 238/* Get the address of a packet buffer corresponding to a given buffer head */
 239#define PKTBUF_ADDR(head)       (((unsigned char *)(MEM)) + (head)->base)
 240
 241/* Possible memory/IO addresses for probing */
 242
 243struct lance_addr {
 244        unsigned long   memaddr;
 245        unsigned long   ioaddr;
 246        int                             slow_flag;
 247} lance_addr_list[] = {
 248        { BAGET_LANCE_MEM_BASE, BAGET_LANCE_IO_BASE, 1 }        /* Baget Lance */
 249};
 250
 251#define N_LANCE_ADDR    (sizeof(lance_addr_list)/sizeof(*lance_addr_list))
 252
 253
 254#define LANCE_HI_BASE (0xff & (BAGET_LANCE_MEM_BASE >> 16))
 255
 256/* Definitions for the Lance */
 257
 258/* tx_head flags */
 259#define TMD1_ENP                0x01    /* end of packet */
 260#define TMD1_STP                0x02    /* start of packet */
 261#define TMD1_DEF                0x04    /* deferred */
 262#define TMD1_ONE                0x08    /* one retry needed */
 263#define TMD1_MORE               0x10    /* more than one retry needed */
 264#define TMD1_ERR                0x40    /* error summary */
 265#define TMD1_OWN                0x80    /* ownership (set: chip owns) */
 266
 267#define TMD1_OWN_CHIP   TMD1_OWN
 268#define TMD1_OWN_HOST   0
 269
 270/* tx_head misc field */
 271#define TMD3_TDR                0x03FF  /* Time Domain Reflectometry counter */
 272#define TMD3_RTRY               0x0400  /* failed after 16 retries */
 273#define TMD3_LCAR               0x0800  /* carrier lost */
 274#define TMD3_LCOL               0x1000  /* late collision */
 275#define TMD3_UFLO               0x4000  /* underflow (late memory) */
 276#define TMD3_BUFF               0x8000  /* buffering error (no ENP) */
 277
 278/* rx_head flags */
 279#define RMD1_ENP                0x01    /* end of packet */
 280#define RMD1_STP                0x02    /* start of packet */
 281#define RMD1_BUFF               0x04    /* buffer error */
 282#define RMD1_CRC                0x08    /* CRC error */
 283#define RMD1_OFLO               0x10    /* overflow */
 284#define RMD1_FRAM               0x20    /* framing error */
 285#define RMD1_ERR                0x40    /* error summary */
 286#define RMD1_OWN                0x80    /* ownership (set: ship owns) */
 287
 288#define RMD1_OWN_CHIP   RMD1_OWN
 289#define RMD1_OWN_HOST   0
 290
 291/* register names */
 292#define CSR0    0               /* mode/status */
 293#define CSR1    1               /* init block addr (low) */
 294#define CSR2    2               /* init block addr (high) */
 295#define CSR3    3               /* misc */
 296#define CSR8    8               /* address filter */
 297#define CSR15   15              /* promiscuous mode */
 298
 299/* CSR0 */
 300/* (R=readable, W=writeable, S=set on write, C=clear on write) */
 301#define CSR0_INIT       0x0001          /* initialize (RS) */
 302#define CSR0_STRT       0x0002          /* start (RS) */
 303#define CSR0_STOP       0x0004          /* stop (RS) */
 304#define CSR0_TDMD       0x0008          /* transmit demand (RS) */
 305#define CSR0_TXON       0x0010          /* transmitter on (R) */
 306#define CSR0_RXON       0x0020          /* receiver on (R) */
 307#define CSR0_INEA       0x0040          /* interrupt enable (RW) */
 308#define CSR0_INTR       0x0080          /* interrupt active (R) */
 309#define CSR0_IDON       0x0100          /* initialization done (RC) */
 310#define CSR0_TINT       0x0200          /* transmitter interrupt (RC) */
 311#define CSR0_RINT       0x0400          /* receiver interrupt (RC) */
 312#define CSR0_MERR       0x0800          /* memory error (RC) */
 313#define CSR0_MISS       0x1000          /* missed frame (RC) */
 314#define CSR0_CERR       0x2000          /* carrier error (no heartbeat :-) (RC) */
 315#define CSR0_BABL       0x4000          /* babble: tx-ed too many bits (RC) */
 316#define CSR0_ERR        0x8000          /* error (RC) */
 317
 318/* CSR3 */
 319#define CSR3_BCON       0x0001          /* byte control */
 320#define CSR3_ACON       0 // fixme: 0x0002              /* ALE control */
 321#define CSR3_BSWP       0x0004          /* byte swap (1=big endian) */
 322
 323
 324
 325/***************************** Prototypes *****************************/
 326
 327static int addr_accessible( volatile void *regp, int wordflag, int
 328                            writeflag );
 329static int lance_probe1( struct net_device *dev, struct lance_addr *init_rec );
 330static int lance_open( struct net_device *dev );
 331static void lance_init_ring( struct net_device *dev );
 332static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
 333static irqreturn_t lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
 334static int lance_rx( struct net_device *dev );
 335static int lance_close( struct net_device *dev );
 336static struct net_device_stats *lance_get_stats( struct net_device *dev );
 337static void set_multicast_list( struct net_device *dev );
 338static int lance_set_mac_address( struct net_device *dev, void *addr );
 339
 340/************************* End of Prototypes **************************/
 341
 342/* Network traffic statistic (bytes) */
 343
 344int lance_stat = 0;
 345
 346static void update_lance_stat (int len) {
 347                lance_stat += len;
 348}
 349
 350/* 
 351   This function is used to access Baget/Lance memory to avoid 
 352   8/32BIT access to VAC A24 space 
 353   ALL memcpy calls was chenged to this function to avoid dbe problems
 354   Don't confuse with function name -- it stays from original code
 355*/
 356
 357void *slow_memcpy( void *dst, const void *src, size_t len )
 358
 359{       
 360        unsigned long to     = (unsigned long)dst;
 361        unsigned long from   = (unsigned long)src;
 362        unsigned long to_end = to +len;
 363        
 364        /* Unaligned flags */
 365
 366        int odd_from   = from   & 1;
 367        int odd_to     = to     & 1;
 368        int odd_to_end = to_end & 1;
 369
 370        /* Align for 16BIT-access first */
 371
 372        register unsigned short *from_a   = (unsigned short*) (from   & ~1);
 373        register unsigned short *to_a     = (unsigned short*) (to     & ~1); 
 374        register unsigned short *to_end_a = (unsigned short*) (to_end & ~1);
 375
 376        /* Caching values -- not in loop invariant */
 377
 378        register unsigned short from_v; 
 379        register unsigned short to_v;
 380
 381        /* Invariant is: from_a and to_a are pointers before or exactly to
 382           currently copying byte */
 383
 384        if (odd_to) { 
 385                        /* First byte unaligned case */
 386                        from_v = *from_a;
 387                        to_v   = *to_a;
 388
 389                        to_v &= ~0xff;
 390                        to_v |=  0xff & (from_v >> (odd_from ? 0 : 8));
 391                        *to_a++ = to_v;
 392
 393                        if (odd_from) from_a++;
 394        }
 395    if (odd_from == odd_to) {
 396                        /* Same parity */
 397                        while (to_a + 7 < to_end_a) {
 398                                        unsigned long dummy1, dummy2;
 399                                        unsigned long reg1, reg2, reg3, reg4;
 400
 401                                        __asm__ __volatile__(
 402                                        ".set\tnoreorder\n\t"
 403                                        ".set\tnoat\n\t"
 404                                        "lh\t%2,0(%1)\n\t"
 405                                        "nop\n\t"
 406                                         "lh\t%3,2(%1)\n\t"
 407                                        "sh\t%2,0(%0)\n\t"
 408                                           "lh\t%4,4(%1)\n\t"
 409                                         "sh\t%3,2(%0)\n\t"
 410                                            "lh\t%5,6(%1)\n\t"
 411                                           "sh\t%4,4(%0)\n\t"
 412                                        "lh\t%2,8(%1)\n\t"
 413                                            "sh\t%5,6(%0)\n\t"
 414                                         "lh\t%3,10(%1)\n\t"
 415                                        "sh\t%2,8(%0)\n\t"
 416                                          "lh\t%4,12(%1)\n\t"
 417                                         "sh\t%3,10(%0)\n\t"
 418                                            "lh\t%5,14(%1)\n\t"
 419                                          "sh\t%4,12(%0)\n\t"
 420                                         "nop\n\t"
 421                                            "sh\t%5,14(%0)\n\t"
 422                                        ".set\tat\n\t"
 423                                        ".set\treorder"
 424                                        :"=r" (dummy1), "=r" (dummy2),
 425                                        "=&r" (reg1), "=&r" (reg2), "=&r" (reg3), "=&r" (reg4)
 426                                        :"0" (to_a), "1" (from_a)
 427                                        :"memory");
 428
 429                                        to_a   += 8;
 430                                        from_a += 8;
 431
 432                        }
 433                        while (to_a < to_end_a) {
 434                                        *to_a++ = *from_a++;
 435                        }
 436        } else {
 437                        /* Different parity */
 438                        from_v = *from_a;
 439                        while (to_a < to_end_a) {
 440                                        unsigned short from_v_next;
 441                                        from_v_next = *++from_a;
 442                                        *to_a++ = ((from_v & 0xff)<<8) | ((from_v_next>>8) & 0xff);
 443                                        from_v = from_v_next; 
 444                        }
 445
 446        }
 447        if (odd_to_end) {
 448                        /* Last byte unaligned case */
 449                        to_v = *to_a;
 450                        from_v = *from_a;
 451
 452                        to_v &= ~0xff00;
 453                        if (odd_from == odd_to) {
 454                                        to_v |= from_v & 0xff00;
 455                        } else {
 456                                        to_v |= (from_v<<8) & 0xff00;
 457                        }
 458
 459                        *to_a = to_v;
 460        }
 461
 462        update_lance_stat( len );
 463
 464        return( dst );
 465}
 466
 467
 468int __init bagetlance_probe( struct net_device *dev )
 469
 470{       int i;
 471        static int found;
 472
 473        SET_MODULE_OWNER(dev);
 474
 475        if (found)
 476                /* Assume there's only one board possible... That seems true, since
 477                 * the Riebl/PAM board's address cannot be changed. */
 478                return( -ENODEV );
 479
 480        for( i = 0; i < N_LANCE_ADDR; ++i ) {
 481                if (lance_probe1( dev, &lance_addr_list[i] )) {
 482                        found = 1;
 483                        return( 0 );
 484                }
 485        }
 486
 487        return( -ENODEV );
 488}
 489
 490
 491
 492/* Derived from hwreg_present() in vme/config.c: */
 493
 494static int __init addr_accessible( volatile void *regp, 
 495                                   int wordflag, 
 496                                   int writeflag )
 497{       
 498                /* We have a fine function to do it */
 499                extern int try_read(unsigned long, int);
 500                return try_read((unsigned long)regp, sizeof(short)) != -1;   
 501}
 502
 503
 504
 505/* Original atari driver uses it */
 506#define IRQ_TYPE_PRIO SA_INTERRUPT
 507#define IRQ_SOURCE_TO_VECTOR(x) (x)
 508
 509static int __init lance_probe1( struct net_device *dev,
 510                                struct lance_addr *init_rec )
 511
 512{       volatile unsigned short *memaddr =
 513                (volatile unsigned short *)init_rec->memaddr;
 514        volatile unsigned short *ioaddr =
 515                (volatile unsigned short *)init_rec->ioaddr;
 516        struct lance_private    *lp;
 517        struct lance_ioreg              *IO;
 518        int                                     i;
 519        static int                              did_version;
 520        unsigned short                  save1, save2;
 521
 522        PROBE_PRINT(( "Probing for Lance card at mem %#lx io %#lx\n",
 523                                  (long)memaddr, (long)ioaddr ));
 524
 525        /* Test whether memory readable and writable */
 526        PROBE_PRINT(( "lance_probe1: testing memory to be accessible\n" ));
 527        if (!addr_accessible( memaddr, 1, 1 )) goto probe_fail;
 528
 529        if ((unsigned long)memaddr >= KSEG2) {
 530                        extern int kseg2_alloc_io (unsigned long addr, unsigned long size);
 531                        if (kseg2_alloc_io((unsigned long)memaddr, BAGET_LANCE_MEM_SIZE)) {
 532                                        printk("bagetlance: unable map lance memory\n");
 533                                        goto probe_fail;
 534                        }
 535        }
 536
 537        /* Written values should come back... */
 538        PROBE_PRINT(( "lance_probe1: testing memory to be writable (1)\n" ));
 539        save1 = *memaddr;
 540        *memaddr = 0x0001;
 541        if (*memaddr != 0x0001) goto probe_fail;
 542        PROBE_PRINT(( "lance_probe1: testing memory to be writable (2)\n" ));
 543        *memaddr = 0x0000;
 544        if (*memaddr != 0x0000) goto probe_fail;
 545        *memaddr = save1;
 546
 547        /* First port should be readable and writable */
 548        PROBE_PRINT(( "lance_probe1: testing ioport to be accessible\n" ));
 549        if (!addr_accessible( ioaddr, 1, 1 )) goto probe_fail;
 550
 551        /* and written values should be readable */
 552        PROBE_PRINT(( "lance_probe1: testing ioport to be writeable\n" ));
 553        save2 = ioaddr[1];
 554        ioaddr[1] = 0x0001;
 555        if (ioaddr[1] != 0x0001) goto probe_fail;
 556
 557        /* The CSR0_INIT bit should not be readable */
 558        PROBE_PRINT(( "lance_probe1: testing CSR0 register function (1)\n" ));
 559        save1 = ioaddr[0];
 560        ioaddr[1] = CSR0;
 561        ioaddr[0] = CSR0_INIT | CSR0_STOP;
 562        if (ioaddr[0] != CSR0_STOP) {
 563                ioaddr[0] = save1;
 564                ioaddr[1] = save2;
 565                goto probe_fail;
 566        }
 567        PROBE_PRINT(( "lance_probe1: testing CSR0 register function (2)\n" ));
 568        ioaddr[0] = CSR0_STOP;
 569        if (ioaddr[0] != CSR0_STOP) {
 570                ioaddr[0] = save1;
 571                ioaddr[1] = save2;
 572                goto probe_fail;
 573        }
 574
 575        /* Now ok... */
 576        PROBE_PRINT(( "lance_probe1: Lance card detected\n" ));
 577        goto probe_ok;
 578
 579  probe_fail:
 580        return( 0 );
 581
 582  probe_ok:
 583        init_etherdev( dev, sizeof(struct lance_private) );
 584        if (!dev->priv) {
 585                dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
 586                if (!dev->priv)
 587                        return 0;
 588        }
 589        lp = (struct lance_private *)dev->priv;
 590        MEM = (struct lance_memory *)memaddr;
 591        IO = lp->iobase = (struct lance_ioreg *)ioaddr;
 592        dev->base_addr = (unsigned long)ioaddr; /* informational only */
 593        lp->memcpy_f = init_rec->slow_flag ? slow_memcpy : memcpy;
 594
 595        REGA( CSR0 ) = CSR0_STOP;
 596
 597        /* Now test for type: If the eeprom I/O port is readable, it is a
 598         * PAM card */
 599        if (addr_accessible( &(IO->eeprom), 0, 0 )) {
 600                /* Switch back to Ram */
 601                i = IO->mem;
 602                lp->cardtype = PAM_CARD;
 603        }
 604#ifdef NORMAL_MEM_ACCESS
 605        else if (*RIEBL_MAGIC_ADDR == RIEBL_MAGIC) {
 606#else
 607        else if (({
 608                        unsigned short *a = (unsigned short*)RIEBL_MAGIC_ADDR;
 609                    (((int)a[0]) << 16) + ((int)a[1]) == RIEBL_MAGIC;
 610        })) {
 611#endif
 612                lp->cardtype = NEW_RIEBL;
 613        }
 614        else
 615                lp->cardtype = OLD_RIEBL;
 616
 617        if (lp->cardtype == PAM_CARD ||
 618                memaddr == (unsigned short *)0xffe00000) {
 619                /* PAMs card and Riebl on ST use level 5 autovector */
 620                request_irq(BAGET_LANCE_IRQ, lance_interrupt, IRQ_TYPE_PRIO,
 621                            "PAM/Riebl-ST Ethernet", dev);
 622                dev->irq = (unsigned short)BAGET_LANCE_IRQ;
 623        }
 624        else {
 625                /* For VME-RieblCards, request a free VME int;
 626                 * (This must be unsigned long, since dev->irq is short and the
 627                 * IRQ_MACHSPEC bit would be cut off...)
 628                 */
 629                unsigned long irq = BAGET_LANCE_IRQ; 
 630                if (!irq) {
 631                        printk( "Lance: request for VME interrupt failed\n" );
 632                        return( 0 );
 633                }
 634                request_irq(irq, lance_interrupt, IRQ_TYPE_PRIO,
 635                            "Riebl-VME Ethernet", dev);
 636                dev->irq = irq;
 637        }
 638
 639        printk("%s: %s at io %#lx, mem %#lx, irq %d%s, hwaddr ",
 640                   dev->name, lance_names[lp->cardtype],
 641                   (unsigned long)ioaddr,
 642                   (unsigned long)memaddr,
 643                   dev->irq,
 644                   init_rec->slow_flag ? " (slow memcpy)" : "" );
 645
 646        /* Get the ethernet address */
 647        switch( lp->cardtype ) {
 648          case OLD_RIEBL:
 649                /* No ethernet address! (Set some default address) */
 650                slow_memcpy( dev->dev_addr, OldRieblDefHwaddr, 6 );
 651                break;
 652          case NEW_RIEBL:
 653                lp->memcpy_f( dev->dev_addr, RIEBL_HWADDR_ADDR, 6 );
 654                break;
 655          case PAM_CARD:
 656                i = IO->eeprom;
 657                for( i = 0; i < 6; ++i )
 658                        dev->dev_addr[i] =
 659                                ((((unsigned short *)MEM)[i*2] & 0x0f) << 4) |
 660                                ((((unsigned short *)MEM)[i*2+1] & 0x0f));
 661                i = IO->mem;
 662                break;
 663        }
 664        for( i = 0; i < 6; ++i )
 665                printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
 666        if (lp->cardtype == OLD_RIEBL) {
 667                printk( "%s: Warning: This is a default ethernet address!\n",
 668                                dev->name );
 669                printk( "      Use \"ifconfig hw ether ...\" to set the address.\n" );
 670        }
 671
 672        MEM->init.mode = 0x0000;                /* Disable Rx and Tx. */
 673
 674        {
 675                        unsigned char hwaddr[6];
 676                        for( i = 0; i < 6; i++ ) 
 677                                        hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
 678                        slow_memcpy(MEM->init.hwaddr, hwaddr, sizeof(hwaddr));
 679        }
 680
 681        MEM->init.filter[0] = 0x00000000;
 682        MEM->init.filter[1] = 0x00000000;
 683        MEM->init.rx_ring.adr_lo = offsetof( struct lance_memory, rx_head );
 684
 685#ifdef NORMAL_MEM_ACCESS
 686        MEM->init.rx_ring.adr_hi = LANCE_HI_BASE; 
 687        MEM->init.rx_ring.len    = RX_RING_LEN_BITS;
 688#else
 689        MEM->init.rx_ring.len_adr_hi = 
 690                        ((unsigned)RX_RING_LEN_BITS << 8) | LANCE_HI_BASE;
 691#endif
 692
 693
 694        MEM->init.tx_ring.adr_lo = offsetof( struct lance_memory, tx_head );
 695
 696#ifdef NORMAL_MEM_ACCESS
 697        MEM->init.tx_ring.adr_hi = LANCE_HI_BASE; 
 698        MEM->init.tx_ring.len    = TX_RING_LEN_BITS;
 699#else
 700        MEM->init.tx_ring.len_adr_hi = 
 701                        ((unsigned)TX_RING_LEN_BITS<<8) | LANCE_HI_BASE;
 702#endif
 703
 704        if (lp->cardtype == PAM_CARD)
 705                IO->ivec = IRQ_SOURCE_TO_VECTOR(dev->irq);
 706        else
 707                *RIEBL_IVEC_ADDR = IRQ_SOURCE_TO_VECTOR(dev->irq);
 708
 709        if (did_version++ == 0)
 710                DPRINTK( 1, ( version ));
 711
 712        /* The LANCE-specific entries in the device structure. */
 713        dev->open = &lance_open;
 714        dev->hard_start_xmit = &lance_start_xmit;
 715        dev->stop = &lance_close;
 716        dev->get_stats = &lance_get_stats;
 717        dev->set_multicast_list = &set_multicast_list;
 718        dev->set_mac_address = &lance_set_mac_address;
 719        dev->start = 0;
 720
 721        memset( &lp->stats, 0, sizeof(lp->stats) );
 722
 723        return( 1 );
 724}
 725
 726
 727static int lance_open( struct net_device *dev )
 728
 729{       struct lance_private *lp = (struct lance_private *)dev->priv;
 730        struct lance_ioreg       *IO = lp->iobase;
 731        int i;
 732
 733        DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
 734
 735        lance_init_ring(dev);
 736        /* Re-initialize the LANCE, and start it when done. */
 737
 738        REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
 739        REGA( CSR2 ) = 0;
 740        REGA( CSR1 ) = 0;
 741        REGA( CSR0 ) = CSR0_INIT;
 742        /* From now on, AREG is kept to point to CSR0 */
 743
 744        i = 1000000;
 745        while (--i > 0)
 746                if (DREG & CSR0_IDON)
 747                        break;
 748        if (i < 0 || (DREG & CSR0_ERR)) {
 749                DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
 750                                          dev->name, i, DREG ));
 751                DREG = CSR0_STOP;
 752                return( -EIO );
 753        }
 754        DREG = CSR0_IDON;
 755        DREG = CSR0_STRT;
 756        DREG = CSR0_INEA;
 757
 758        dev->tbusy = 0;
 759        dev->interrupt = 0;
 760        dev->start = 1;
 761
 762        DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
 763        return( 0 );
 764}
 765
 766
 767/* Initialize the LANCE Rx and Tx rings. */
 768
 769static void lance_init_ring( struct net_device *dev )
 770
 771{       struct lance_private *lp = (struct lance_private *)dev->priv;
 772        int i;
 773        unsigned offset;
 774
 775        lp->lock = 0;
 776        lp->tx_full = 0;
 777        lp->cur_rx = lp->cur_tx = 0;
 778        lp->dirty_tx = 0;
 779
 780        offset = offsetof( struct lance_memory, packet_area );
 781
 782/* If the packet buffer at offset 'o' would conflict with the reserved area
 783 * of RieblCards, advance it */
 784#define CHECK_OFFSET(o)                                                                                                          \
 785        do {                                                                                                                                     \
 786                if (lp->cardtype == OLD_RIEBL || lp->cardtype == NEW_RIEBL) {            \
 787                        if (((o) < RIEBL_RSVD_START) ? (o)+PKT_BUF_SZ > RIEBL_RSVD_START \
 788                                                                                 : (o) < RIEBL_RSVD_END)                         \
 789                                (o) = RIEBL_RSVD_END;                                                                            \
 790                }                                                                                                                                        \
 791        } while(0)
 792
 793        for( i = 0; i < TX_RING_SIZE; i++ ) {
 794                CHECK_OFFSET(offset);
 795                MEM->tx_head[i].base = offset;
 796#ifdef NORMAL_MEM_ACCESS
 797                MEM->tx_head[i].flag = TMD1_OWN_HOST;
 798                MEM->tx_head[i].base_hi = LANCE_HI_BASE;
 799#else
 800                MEM->tx_head[i].flag_base_hi = 
 801                                (TMD1_OWN_HOST<<8) | LANCE_HI_BASE;
 802#endif
 803                MEM->tx_head[i].length = 0;
 804                MEM->tx_head[i].misc = 0;
 805                offset += PKT_BUF_SZ;
 806        }
 807
 808        for( i = 0; i < RX_RING_SIZE; i++ ) {
 809                CHECK_OFFSET(offset);
 810                MEM->rx_head[i].base = offset;
 811#ifdef NORMAL_MEM_ACCESS
 812                MEM->rx_head[i].flag = TMD1_OWN_CHIP;
 813                MEM->rx_head[i].base_hi = LANCE_HI_BASE; 
 814#else
 815                MEM->rx_head[i].flag_base_hi = 
 816                                (TMD1_OWN_CHIP<<8) | LANCE_HI_BASE;
 817#endif
 818                MEM->rx_head[i].buf_length = -PKT_BUF_SZ;
 819                MEM->rx_head[i].msg_length = 0;
 820                offset += PKT_BUF_SZ;
 821        }
 822}
 823
 824
 825static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
 826
 827{       struct lance_private *lp = (struct lance_private *)dev->priv;
 828        struct lance_ioreg       *IO = lp->iobase;
 829        int entry, len;
 830        struct lance_tx_head *head;
 831        unsigned long flags;
 832
 833        /* The old LANCE chips doesn't automatically pad buffers to min. size. */
 834        len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
 835        /* PAM-Card has a bug: Can only send packets with even number of bytes! */
 836        if (lp->cardtype == PAM_CARD && (len & 1))
 837                ++len;
 838
 839        if (len > skb->len) {
 840                skb = skb_padto(skb, len);
 841                if (skb == NULL)
 842                        return 0;
 843        }       
 844
 845        /* Transmitter timeout, serious problems. */
 846        if (dev->tbusy) {
 847                int tickssofar = jiffies - dev->trans_start;
 848                if (tickssofar < 20)
 849                        return( 1 );
 850                AREG = CSR0;
 851                DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
 852                                          dev->name, DREG ));
 853                DREG = CSR0_STOP;
 854                /*
 855                 * Always set BSWP after a STOP as STOP puts it back into
 856                 * little endian mode.
 857                 */
 858                REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
 859                lp->stats.tx_errors++;
 860#ifndef final_version
 861                {       int i;
 862                        DPRINTK( 2, ( "Ring data: dirty_tx %d cur_tx %d%s cur_rx %d\n",
 863                                                  lp->dirty_tx, lp->cur_tx,
 864                                                  lp->tx_full ? " (full)" : "",
 865                                                  lp->cur_rx ));
 866                        for( i = 0 ; i < RX_RING_SIZE; i++ )
 867                                DPRINTK( 2, ( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
 868                                                          i, MEM->rx_head[i].base,
 869                                                          -MEM->rx_head[i].buf_length,
 870                                                          MEM->rx_head[i].msg_length ));
 871                        for( i = 0 ; i < TX_RING_SIZE; i++ )
 872                                DPRINTK( 2, ( "tx #%d: base=%04x len=%04x misc=%04x\n",
 873                                                          i, MEM->tx_head[i].base,
 874                                                          -MEM->tx_head[i].length,
 875                                                          MEM->tx_head[i].misc ));
 876                }
 877#endif
 878                lance_init_ring(dev);
 879                REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
 880
 881                dev->tbusy = 0;
 882                dev->trans_start = jiffies;
 883
 884                return( 0 );
 885        }
 886
 887        DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
 888                                  dev->name, DREG ));
 889
 890        /* Block a timer-based transmit from overlapping.  This could better be
 891           done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 892        if (test_and_set_bit( 0, (void*)&dev->tbusy ) != 0) {
 893                DPRINTK( 0, ( "%s: Transmitter access conflict.\n", dev->name ));
 894                return 1;
 895        }
 896
 897        if (test_and_set_bit( 0, (void*)&lp->lock ) != 0) {
 898                DPRINTK( 0, ( "%s: tx queue lock!.\n", dev->name ));
 899                /* don't clear dev->tbusy flag. */
 900                return 1;
 901        }
 902
 903        /* Fill in a Tx ring entry */
 904        if (lance_debug >= 3) {
 905                u_char *p;
 906                int i;
 907                printk( "%s: TX pkt type 0x%04x from ", dev->name,
 908                                ((u_short *)skb->data)[6]);
 909                for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
 910                        printk("%02x%s", *p++, i != 5 ? ":" : "" );
 911                printk(" to ");
 912                for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
 913                        printk("%02x%s", *p++, i != 5 ? ":" : "" );
 914                printk(" data at 0x%08x len %d\n", (int)skb->data,
 915                           (int)skb->len );
 916        }
 917
 918        /* We're not prepared for the int until the last flags are set/reset. And
 919         * the int may happen already after setting the OWN_CHIP... */
 920        save_flags(flags);
 921        cli();
 922
 923        /* Mask to ring buffer boundary. */
 924        entry = lp->cur_tx & TX_RING_MOD_MASK;
 925        head  = &(MEM->tx_head[entry]);
 926
 927        /* Caution: the write order is important here, set the "ownership" bits
 928         * last.
 929         */
 930
 931        head->length = -len;
 932        head->misc = 0;
 933        lp->memcpy_f( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
 934#ifdef NORMAL_MEM_ACCESS
 935        head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
 936#else
 937    SET_FLAG(head,(TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP));
 938#endif
 939        lp->stats.tx_bytes += skb->len;
 940        dev_kfree_skb( skb );
 941        lp->cur_tx++;
 942        while( lp->cur_tx >= TX_RING_SIZE && lp->dirty_tx >= TX_RING_SIZE ) {
 943                lp->cur_tx -= TX_RING_SIZE;
 944                lp->dirty_tx -= TX_RING_SIZE;
 945        }
 946
 947        /* Trigger an immediate send poll. */
 948        DREG = CSR0_INEA | CSR0_TDMD;
 949        dev->trans_start = jiffies;
 950
 951        lp->lock = 0;
 952#ifdef NORMAL_MEM_ACCESS
 953        if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
 954#else
 955        if ((GET_FLAG(&MEM->tx_head[(entry+1) & TX_RING_MOD_MASK]) & TMD1_OWN) ==
 956#endif
 957                TMD1_OWN_HOST)
 958                dev->tbusy = 0;
 959        else
 960                lp->tx_full = 1;
 961        restore_flags(flags);
 962
 963        return 0;
 964}
 965
 966/* The LANCE interrupt handler. */
 967
 968static irqreturn_t lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
 969{
 970        struct net_device *dev = dev_id;
 971        struct lance_private *lp;
 972        struct lance_ioreg       *IO;
 973        int csr0, boguscnt = 10;
 974        int handled = 0;
 975
 976        if (dev == NULL) {
 977                DPRINTK( 1, ( "lance_interrupt(): interrupt for unknown device.\n" ));
 978                return IRQ_NONE;
 979        }
 980
 981        lp = (struct lance_private *)dev->priv;
 982        IO = lp->iobase;
 983        AREG = CSR0;
 984
 985        if (dev->interrupt) {
 986                        DPRINTK( 1, ( "Re-entering CAUSE=%08x STATUS=%08x\n",  
 987                                                  read_32bit_cp0_register(CP0_CAUSE),  
 988                                                  read_32bit_cp0_register(CP0_STATUS) ));
 989                        panic("lance: interrupt handler reentered !");
 990        }
 991
 992        dev->interrupt = 1;
 993
 994        while( ((csr0 = DREG) & (CSR0_ERR | CSR0_TINT | CSR0_RINT)) &&
 995                   --boguscnt >= 0) {
 996                handled = 1;
 997                /* Acknowledge all of the current interrupt sources ASAP. */
 998                DREG = csr0 & ~(CSR0_INIT | CSR0_STRT | CSR0_STOP |
 999                                                                        CSR0_TDMD | CSR0_INEA);
1000
1001                DPRINTK( 2, ( "%s: interrupt  csr0=%04x new csr=%04x.\n",
1002                                          dev->name, csr0, DREG ));
1003
1004                if (csr0 & CSR0_RINT)                   /* Rx interrupt */
1005                        lance_rx( dev );
1006
1007                if (csr0 & CSR0_TINT) {                 /* Tx-done interrupt */
1008                        int dirty_tx = lp->dirty_tx;
1009
1010                        while( dirty_tx < lp->cur_tx) {
1011                                int entry = dirty_tx & TX_RING_MOD_MASK;
1012#ifdef NORMAL_MEM_ACCESS
1013                                int status = MEM->tx_head[entry].flag;
1014#else
1015                                int status = GET_FLAG(&MEM->tx_head[entry]);
1016#endif
1017                                if (status & TMD1_OWN_CHIP)
1018                                        break;                  /* It still hasn't been Txed */
1019
1020#ifdef NORMAL_MEM_ACCESS
1021                                MEM->tx_head[entry].flag = 0;
1022#else
1023                                SET_FLAG(&MEM->tx_head[entry],0);
1024#endif
1025
1026                                if (status & TMD1_ERR) {
1027                                        /* There was an major error, log it. */
1028                                        int err_status = MEM->tx_head[entry].misc;
1029                                        lp->stats.tx_errors++;
1030                                        if (err_status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
1031                                        if (err_status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
1032                                        if (err_status & TMD3_LCOL) lp->stats.tx_window_errors++;
1033                                        if (err_status & TMD3_UFLO) {
1034                                                /* Ackk!  On FIFO errors the Tx unit is turned off! */
1035                                                lp->stats.tx_fifo_errors++;
1036                                                /* Remove this verbosity later! */
1037                                                DPRINTK( 1, ( "%s: Tx FIFO error! Status %04x\n",
1038                                                                          dev->name, csr0 ));
1039                                                /* Restart the chip. */
1040                                                DREG = CSR0_STRT;
1041                                        }
1042                                } else {
1043                                        if (status & (TMD1_MORE | TMD1_ONE | TMD1_DEF))
1044                                                lp->stats.collisions++;
1045                                        lp->stats.tx_packets++;
1046                                }
1047                                dirty_tx++;
1048                        }
1049
1050#ifndef final_version
1051                        if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1052                                DPRINTK( 0, ( "out-of-sync dirty pointer,"
1053                                                          " %d vs. %d, full=%d.\n",
1054                                                          dirty_tx, lp->cur_tx, lp->tx_full ));
1055                                dirty_tx += TX_RING_SIZE;
1056                        }
1057#endif
1058
1059                        if (lp->tx_full && dev->tbusy
1060                                && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1061                                /* The ring is no longer full, clear tbusy. */
1062                                lp->tx_full = 0;
1063                                dev->tbusy = 0;
1064                                mark_bh( NET_BH );
1065                        }
1066
1067                        lp->dirty_tx = dirty_tx;
1068                }
1069
1070                /* Log misc errors. */
1071                if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
1072                if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
1073                if (csr0 & CSR0_MERR) {
1074                        DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
1075                                                  "status %04x.\n", dev->name, csr0 ));
1076                        /* Restart the chip. */
1077                        DREG = CSR0_STRT;
1078                }
1079        }
1080
1081    /* Clear any other interrupt, and set interrupt enable. */
1082        DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
1083                   CSR0_IDON | CSR0_INEA;
1084
1085        DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
1086                                  dev->name, DREG ));
1087        dev->interrupt = 0;
1088        return IRQ_RETVAL(handled);
1089}
1090
1091
1092static int lance_rx( struct net_device *dev )
1093
1094{       struct lance_private *lp = (struct lance_private *)dev->priv;
1095        int entry = lp->cur_rx & RX_RING_MOD_MASK;
1096        int i;
1097
1098#ifdef NORMAL_MEM_ACCESS
1099        DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
1100                                  MEM->rx_head[entry].flag ));
1101#else
1102        DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
1103                                  GET_FLAG(&MEM->rx_head[entry]) ));
1104#endif
1105
1106        /* If we own the next entry, it's a new packet. Send it up. */
1107#ifdef NORMAL_MEM_ACCESS
1108        while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
1109#else
1110        while( (GET_FLAG(&MEM->rx_head[entry]) & RMD1_OWN) == RMD1_OWN_HOST ) {
1111#endif
1112                struct lance_rx_head *head = &(MEM->rx_head[entry]);
1113#ifdef NORMAL_MEM_ACCESS
1114                int status = head->flag;
1115#else
1116                int status = GET_FLAG(head);
1117#endif
1118
1119                if (status != (RMD1_ENP|RMD1_STP)) {            /* There was an error. */
1120                        /* There is a tricky error noted by John Murphy,
1121                           <murf@perftech.com> to Russ Nelson: Even with full-sized
1122                           buffers it's possible for a jabber packet to use two
1123                           buffers, with only the last correctly noting the error. */
1124                        if (status & RMD1_ENP)  /* Only count a general error at the */
1125                                lp->stats.rx_errors++; /* end of a packet.*/
1126                        if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
1127                        if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
1128                        if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
1129                        if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
1130#ifdef NORMAL_MEM_ACCESS
1131                        head->flag &= (RMD1_ENP|RMD1_STP);
1132#else
1133                        SET_FLAG(head,GET_FLAG(head) & (RMD1_ENP|RMD1_STP));
1134#endif
1135                } else {
1136                        /* Malloc up new buffer, compatible with net-3. */
1137                        short pkt_len = head->msg_length & 0xfff;
1138                        struct sk_buff *skb;
1139
1140                        if (pkt_len < 60) {
1141                                printk( "%s: Runt packet!\n", dev->name );
1142                                lp->stats.rx_errors++;
1143                        }
1144                        else {
1145                                skb = dev_alloc_skb( pkt_len+2 );
1146                                if (skb == NULL) {
1147                                        DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
1148                                                                  dev->name ));
1149                          for( i = 0; i < RX_RING_SIZE; i++ )
1150#ifdef NORMAL_MEM_ACCESS
1151                        if (MEM->rx_head[(entry+i) & RX_RING_MOD_MASK].flag &
1152#else
1153                                                if (GET_FLAG(&MEM->rx_head[(entry+i) & \
1154                                                                                                  RX_RING_MOD_MASK]) &
1155#endif
1156                                                        RMD1_OWN_CHIP)
1157                                                        break;
1158
1159                                        if (i > RX_RING_SIZE - 2) {
1160                                                lp->stats.rx_dropped++;
1161#ifdef NORMAL_MEM_ACCESS
1162                        head->flag |= RMD1_OWN_CHIP;
1163#else
1164                        SET_FLAG(head,GET_FLAG(head) | RMD1_OWN_CHIP);
1165#endif
1166                                                lp->cur_rx++;
1167                                        }
1168                                        break;
1169                                }
1170
1171                                if (lance_debug >= 3) {
1172                                        u_char *data = PKTBUF_ADDR(head), *p;
1173                                        printk( "%s: RX pkt type 0x%04x from ", dev->name,
1174                                                        ((u_short *)data)[6]);
1175                                        for( p = &data[6], i = 0; i < 6; i++ )
1176                                                printk("%02x%s", *p++, i != 5 ? ":" : "" );
1177                                        printk(" to ");
1178                                        for( p = data, i = 0; i < 6; i++ )
1179                                                printk("%02x%s", *p++, i != 5 ? ":" : "" );
1180                                        printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
1181                                                   "len %d\n",
1182                                                   data[15], data[16], data[17], data[18],
1183                                                   data[19], data[20], data[21], data[22],
1184                                                   pkt_len );
1185                                }
1186
1187                                skb->dev = dev;
1188                                skb_reserve( skb, 2 );  /* 16 byte align */
1189                                skb_put( skb, pkt_len );        /* Make room */
1190                                lp->memcpy_f( skb->data, PKTBUF_ADDR(head), pkt_len );
1191                                skb->protocol = eth_type_trans( skb, dev );
1192                                netif_rx( skb );
1193                                dev->last_rx = jiffies;
1194                                lp->stats.rx_packets++;
1195                                lp->stats.rx_bytes += pkt_len;
1196                        }
1197                }
1198
1199#ifdef NORMAL_MEM_ACCESS
1200                head->flag |= RMD1_OWN_CHIP;
1201#else
1202                SET_FLAG(head,GET_FLAG(head) | RMD1_OWN_CHIP);
1203#endif
1204                entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1205        }
1206        lp->cur_rx &= RX_RING_MOD_MASK;
1207
1208        /* From lance.c (Donald Becker): */
1209        /* We should check that at least two ring entries are free.      If not,
1210           we should free one and mark stats->rx_dropped++. */
1211
1212        return 0;
1213}
1214
1215
1216static int lance_close( struct net_device *dev )
1217
1218{       struct lance_private *lp = (struct lance_private *)dev->priv;
1219        struct lance_ioreg       *IO = lp->iobase;
1220
1221        dev->start = 0;
1222        dev->tbusy = 1;
1223
1224        AREG = CSR0;
1225
1226        DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
1227                                  dev->name, DREG ));
1228
1229        /* We stop the LANCE here -- it occasionally polls
1230           memory if we don't. */
1231        DREG = CSR0_STOP;
1232
1233        return 0;
1234}
1235
1236
1237static struct net_device_stats *lance_get_stats( struct net_device *dev )
1238
1239{       
1240        struct lance_private *lp = (struct lance_private *)dev->priv;
1241        return &lp->stats;
1242}
1243
1244
1245/* Set or clear the multicast filter for this adaptor.
1246   num_addrs == -1              Promiscuous mode, receive all packets
1247   num_addrs == 0               Normal mode, clear multicast list
1248   num_addrs > 0                Multicast mode, receive normal and MC packets, and do
1249                                                best-effort filtering.
1250 */
1251
1252static void set_multicast_list( struct net_device *dev )
1253
1254{       struct lance_private *lp = (struct lance_private *)dev->priv;
1255        struct lance_ioreg       *IO = lp->iobase;
1256
1257        if (!dev->start)
1258                /* Only possible if board is already started */
1259                return;
1260
1261        /* We take the simple way out and always enable promiscuous mode. */
1262        DREG = CSR0_STOP; /* Temporarily stop the lance. */
1263
1264        if (dev->flags & IFF_PROMISC) {
1265                /* Log any net taps. */
1266                DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
1267                REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
1268        } else {
1269                short multicast_table[4];
1270                int num_addrs = dev->mc_count;
1271                int i;
1272                /* We don't use the multicast table, but rely on upper-layer
1273                 * filtering. */
1274                memset( multicast_table, (num_addrs == 0) ? 0 : -1,
1275                                sizeof(multicast_table) );
1276                for( i = 0; i < 4; i++ )
1277                        REGA( CSR8+i ) = multicast_table[i];
1278                REGA( CSR15 ) = 0; /* Unset promiscuous mode */
1279        }
1280
1281        /*
1282         * Always set BSWP after a STOP as STOP puts it back into
1283         * little endian mode.
1284         */
1285        REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
1286
1287        /* Resume normal operation and reset AREG to CSR0 */
1288        REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
1289}
1290
1291
1292/* This is needed for old RieblCards and possible for new RieblCards */
1293
1294static int lance_set_mac_address( struct net_device *dev, void *addr )
1295
1296{       struct lance_private *lp = (struct lance_private *)dev->priv;
1297        struct sockaddr *saddr = addr;
1298        int i;
1299
1300        if (lp->cardtype != OLD_RIEBL && lp->cardtype != NEW_RIEBL)
1301                return( -EOPNOTSUPP );
1302
1303        if (dev->start) {
1304                /* Only possible while card isn't started */
1305                DPRINTK( 1, ( "%s: hwaddr can be set only while card isn't open.\n",
1306                                          dev->name ));
1307                return( -EIO );
1308        }
1309
1310        slow_memcpy( dev->dev_addr, saddr->sa_data, dev->addr_len );
1311
1312        {
1313                        unsigned char hwaddr[6];
1314                        for( i = 0; i < 6; i++ ) 
1315                                        hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
1316                        slow_memcpy(MEM->init.hwaddr, hwaddr, sizeof(hwaddr));
1317        }
1318
1319        lp->memcpy_f( RIEBL_HWADDR_ADDR, dev->dev_addr, 6 );
1320        /* set also the magic for future sessions */
1321#ifdef NORMAL_MEM_ACCESS
1322        *RIEBL_MAGIC_ADDR = RIEBL_MAGIC;
1323#else
1324        {
1325                        unsigned long magic = RIEBL_MAGIC;
1326                        slow_memcpy(RIEBL_MAGIC_ADDR, &magic, sizeof(*RIEBL_MAGIC_ADDR));
1327        }
1328#endif
1329        return( 0 );
1330}
1331
1332
1333#ifdef MODULE
1334static struct net_device bagetlance_dev;
1335
1336int init_module(void)
1337
1338{       int err;
1339
1340        bagetlance_dev.init = bagetlance_probe;
1341        if ((err = register_netdev( &bagetlance_dev ))) {
1342                if (err == -EIO)  {
1343                        printk( "No Vme Lance board found. Module not loaded.\n");
1344                }
1345                return( err );
1346        }
1347        return( 0 );
1348}
1349
1350void cleanup_module(void)
1351
1352{
1353        unregister_netdev( &bagetlance_dev );
1354}
1355
1356#endif /* MODULE */
1357
1358/*
1359 * Local variables:
1360 *  c-indent-level: 4
1361 *  tab-width: 4
1362 * End:
1363 */
1364
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.