linux-bk/drivers/net/declance.c
<<
>>
Prefs
   1/*     
   2 *    Lance ethernet driver for the MIPS processor based
   3 *      DECstation family
   4 *
   5 *
   6 *      adopted from sunlance.c by Richard van den Berg
   7 *
   8 *      additional sources:
   9 *      - PMAD-AA TURBOchannel Ethernet Module Functional Specification,
  10 *        Revision 1.2
  11 *
  12 *      History:
  13 *
  14 *      v0.001: The kernel accepts the code and it shows the hardware address.
  15 *
  16 *      v0.002: Removed most sparc stuff, left only some module and dma stuff.
  17 *
  18 *      v0.003: Enhanced base address calculation from proposals by
  19 *      Harald Koerfgen and Thomas Riemer.
  20 *
  21 *      v0.004: lance-regs is pointing at the right addresses, added prom
  22 *      check. First start of address mapping and DMA.
  23 *
  24 *      v0.005: started to play around with LANCE-DMA. This driver will not work
  25 *      for non IOASIC lances. HK
  26 *
  27 *      v0.006: added pointer arrays to lance_private and setup routine for them
  28 *      in dec_lance_init. HK
  29 *
  30 *      v0.007: Big shit. The LANCE seems to use a different DMA mechanism to access
  31 *      the init block. This looks like one (short) word at a time, but the smallest
  32 *      amount the IOASIC can transfer is a (long) word. So we have a 2-2 padding here.
  33 *      Changed lance_init_block accordingly. The 16-16 padding for the buffers
  34 *      seems to be correct. HK
  35 *
  36 *     v0.008 - mods to make PMAX_LANCE work. 01/09/1999 triemer
  37 */
  38
  39#undef DEBUG_DRIVER
  40
  41static char *version =
  42"declance.c: v0.008 by Linux Mips DECstation task force\n";
  43
  44static char *lancestr = "LANCE";
  45
  46/*
  47 * card types
  48 */
  49#define ASIC_LANCE 1
  50#define PMAD_LANCE 2
  51#define PMAX_LANCE 3
  52
  53#include <linux/init.h>
  54#include <linux/kernel.h>
  55#include <linux/netdevice.h>
  56
  57#include <asm/dec/interrupts.h>
  58#include <asm/dec/ioasic_ints.h>
  59#include <asm/dec/ioasic_addrs.h>
  60#include <asm/dec/machtype.h>
  61#include <asm/dec/tc.h>
  62#include <asm/dec/kn01.h>
  63#include <asm/wbflush.h>
  64#include <asm/addrspace.h>
  65
  66#include <linux/config.h>
  67#include <linux/errno.h>
  68#include <linux/hdreg.h>
  69#include <linux/ioport.h>
  70#include <linux/sched.h>
  71#include <linux/mm.h>
  72#include <linux/stddef.h>
  73#include <linux/string.h>
  74#include <linux/unistd.h>
  75#include <linux/slab.h>
  76#include <linux/user.h>
  77#include <linux/utsname.h>
  78#include <linux/a.out.h>
  79#include <linux/tty.h>
  80#include <linux/delay.h>
  81#include <linux/crc32.h>
  82#include <asm/io.h>
  83#include <linux/etherdevice.h>
  84
  85#ifndef CONFIG_TC
  86unsigned long system_base;
  87unsigned long dmaptr;
  88#endif
  89static int type;
  90
  91#define LE_CSR0 0
  92#define LE_CSR1 1
  93#define LE_CSR2 2
  94#define LE_CSR3 3
  95
  96#define LE_MO_PROM      0x8000  /* Enable promiscuous mode */
  97
  98#define LE_C0_ERR       0x8000  /* Error: set if BAB, SQE, MISS or ME is set */
  99#define LE_C0_BABL      0x4000  /* BAB:  Babble: tx timeout. */
 100#define LE_C0_CERR      0x2000  /* SQE:  Signal quality error */
 101#define LE_C0_MISS      0x1000  /* MISS: Missed a packet */
 102#define LE_C0_MERR      0x0800  /* ME:   Memory error */
 103#define LE_C0_RINT      0x0400  /* Received interrupt */
 104#define LE_C0_TINT      0x0200  /* Transmitter Interrupt */
 105#define LE_C0_IDON      0x0100  /* IFIN: Init finished. */
 106#define LE_C0_INTR      0x0080  /* Interrupt or error */
 107#define LE_C0_INEA      0x0040  /* Interrupt enable */
 108#define LE_C0_RXON      0x0020  /* Receiver on */
 109#define LE_C0_TXON      0x0010  /* Transmitter on */
 110#define LE_C0_TDMD      0x0008  /* Transmitter demand */
 111#define LE_C0_STOP      0x0004  /* Stop the card */
 112#define LE_C0_STRT      0x0002  /* Start the card */
 113#define LE_C0_INIT      0x0001  /* Init the card */
 114
 115#define LE_C3_BSWP      0x4     /* SWAP */
 116#define LE_C3_ACON      0x2     /* ALE Control */
 117#define LE_C3_BCON      0x1     /* Byte control */
 118
 119/* Receive message descriptor 1 */
 120#define LE_R1_OWN       0x80    /* Who owns the entry */
 121#define LE_R1_ERR       0x40    /* Error: if FRA, OFL, CRC or BUF is set */
 122#define LE_R1_FRA       0x20    /* FRA: Frame error */
 123#define LE_R1_OFL       0x10    /* OFL: Frame overflow */
 124#define LE_R1_CRC       0x08    /* CRC error */
 125#define LE_R1_BUF       0x04    /* BUF: Buffer error */
 126#define LE_R1_SOP       0x02    /* Start of packet */
 127#define LE_R1_EOP       0x01    /* End of packet */
 128#define LE_R1_POK       0x03    /* Packet is complete: SOP + EOP */
 129
 130#define LE_T1_OWN       0x80    /* Lance owns the packet */
 131#define LE_T1_ERR       0x40    /* Error summary */
 132#define LE_T1_EMORE     0x10    /* Error: more than one retry needed */
 133#define LE_T1_EONE      0x08    /* Error: one retry needed */
 134#define LE_T1_EDEF      0x04    /* Error: deferred */
 135#define LE_T1_SOP       0x02    /* Start of packet */
 136#define LE_T1_EOP       0x01    /* End of packet */
 137#define LE_T1_POK       0x03    /* Packet is complete: SOP + EOP */
 138
 139#define LE_T3_BUF       0x8000  /* Buffer error */
 140#define LE_T3_UFL       0x4000  /* Error underflow */
 141#define LE_T3_LCOL      0x1000  /* Error late collision */
 142#define LE_T3_CLOS      0x0800  /* Error carrier loss */
 143#define LE_T3_RTY       0x0400  /* Error retry */
 144#define LE_T3_TDR       0x03ff  /* Time Domain Reflectometry counter */
 145
 146/* Define: 2^4 Tx buffers and 2^4 Rx buffers */
 147
 148#ifndef LANCE_LOG_TX_BUFFERS
 149#define LANCE_LOG_TX_BUFFERS 4
 150#define LANCE_LOG_RX_BUFFERS 4
 151#endif
 152
 153#define TX_RING_SIZE                    (1 << (LANCE_LOG_TX_BUFFERS))
 154#define TX_RING_MOD_MASK                (TX_RING_SIZE - 1)
 155
 156#define RX_RING_SIZE                    (1 << (LANCE_LOG_RX_BUFFERS))
 157#define RX_RING_MOD_MASK                (RX_RING_SIZE - 1)
 158
 159#define PKT_BUF_SZ              1536
 160#define RX_BUFF_SIZE            PKT_BUF_SZ
 161#define TX_BUFF_SIZE            PKT_BUF_SZ
 162
 163#undef TEST_HITS
 164#define DEBUG_DRIVER 1
 165
 166#define ZERO 0
 167
 168/* The DS2000/3000 have a linear 64 KB buffer.
 169
 170 * The PMAD-AA has 128 kb buffer on-board. 
 171 *
 172 * The IOASIC LANCE devices use a shared memory region. This region as seen 
 173 * from the CPU is (max) 128 KB long and has to be on an 128 KB boundary.
 174 * The LANCE sees this as a 64 KB long continuous memory region.
 175 *
 176 * The LANCE's DMA address is used as an index in this buffer and DMA takes
 177 * place in bursts of eight 16-Bit words which are packed into four 32-Bit words
 178 * by the IOASIC. This leads to a strange padding: 16 bytes of valid data followed
 179 * by a 16 byte gap :-(.
 180 */
 181
 182struct lance_rx_desc {
 183        unsigned short rmd0;    /* low address of packet */
 184        short gap0;
 185        unsigned char rmd1_hadr;        /* high address of packet */
 186        unsigned char rmd1_bits;        /* descriptor bits */
 187        short gap1;
 188        short length;           /* This length is 2s complement (negative)!
 189                                   * Buffer length
 190                                 */
 191        short gap2;
 192        unsigned short mblength;        /* This is the actual number of bytes received */
 193        short gap3;
 194};
 195
 196struct lance_tx_desc {
 197        unsigned short tmd0;    /* low address of packet */
 198        short gap0;
 199        unsigned char tmd1_hadr;        /* high address of packet */
 200        unsigned char tmd1_bits;        /* descriptor bits */
 201        short gap1;
 202        short length;           /* Length is 2s complement (negative)! */
 203        short gap2;
 204        unsigned short misc;
 205        short gap3;
 206};
 207
 208
 209/* First part of the LANCE initialization block, described in databook. */
 210struct lance_init_block {
 211        unsigned short mode;    /* Pre-set mode (reg. 15) */
 212        short gap0;
 213
 214        unsigned char phys_addr[12];    /* Physical ethernet address
 215                                           * only 0, 1, 4, 5, 8, 9 are valid
 216                                           * 2, 3, 6, 7, 10, 11 are gaps
 217                                         */
 218        unsigned short filter[8];       /* Multicast filter.
 219                                           * only 0, 2, 4, 6 are valid
 220                                           * 1, 3, 5, 7 are gaps
 221                                         */
 222
 223        /* Receive and transmit ring base, along with extra bits. */
 224        unsigned short rx_ptr;  /* receive descriptor addr */
 225        short gap1;
 226        unsigned short rx_len;  /* receive len and high addr */
 227        short gap2;
 228        unsigned short tx_ptr;  /* transmit descriptor addr */
 229        short gap3;
 230        unsigned short tx_len;  /* transmit len and high addr */
 231        short gap4;
 232        char gap5[16];
 233
 234        /* The buffer descriptors */
 235        struct lance_rx_desc brx_ring[RX_RING_SIZE];
 236        struct lance_tx_desc btx_ring[TX_RING_SIZE];
 237};
 238
 239#define BUF_OFFSET_CPU sizeof(struct lance_init_block)
 240#define BUF_OFFSET_LNC (sizeof(struct lance_init_block)>>1)
 241
 242#define libdesc_offset(rt, elem) \
 243((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
 244
 245/*
 246 * This works *only* for the ring descriptors
 247 */
 248#define LANCE_ADDR(x) (PHYSADDR(x) >> 1)
 249
 250struct lance_private {
 251        char *name;
 252        volatile struct lance_regs *ll;
 253        volatile struct lance_init_block *init_block;
 254        volatile unsigned long *dma_ptr_reg;
 255
 256        spinlock_t      lock;
 257
 258        int rx_new, tx_new;
 259        int rx_old, tx_old;
 260
 261        struct net_device_stats stats;
 262
 263        unsigned short busmaster_regval;
 264
 265        struct net_device *dev; /* Backpointer        */
 266        struct lance_private *next_module;
 267        struct timer_list       multicast_timer;
 268
 269        /* Pointers to the ring buffers as seen from the CPU */
 270        char *rx_buf_ptr_cpu[RX_RING_SIZE];
 271        char *tx_buf_ptr_cpu[TX_RING_SIZE];
 272
 273        /* Pointers to the ring buffers as seen from the LANCE */
 274        char *rx_buf_ptr_lnc[RX_RING_SIZE];
 275        char *tx_buf_ptr_lnc[TX_RING_SIZE];
 276};
 277
 278#define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
 279                        lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
 280                        lp->tx_old - lp->tx_new-1)
 281
 282/* The lance control ports are at an absolute address, machine and tc-slot
 283 * dependant.
 284 * DECstations do only 32-bit access and the LANCE uses 16 bit addresses,
 285 * so we have to give the structure an extra member making rap pointing
 286 * at the right address
 287 */
 288struct lance_regs {
 289        volatile unsigned short rdp;    /* register data port */
 290        unsigned short pad;
 291        volatile unsigned short rap;    /* register address port */
 292};
 293
 294int dec_lance_debug = 2;
 295
 296/*
 297   #ifdef MODULE
 298   static struct lance_private *root_lance_dev = NULL;
 299   #endif
 300 */
 301
 302static inline void writereg(volatile unsigned short *regptr, short value)
 303{
 304        *regptr = value;
 305        wbflush();
 306}
 307
 308/* Load the CSR registers */
 309static void load_csrs(struct lance_private *lp)
 310{
 311        volatile struct lance_regs *ll = lp->ll;
 312        int leptr;
 313
 314        /* The address space as seen from the LANCE
 315         * begins at address 0. HK
 316         */
 317        leptr = 0;
 318
 319        writereg(&ll->rap, LE_CSR1);
 320        writereg(&ll->rdp, (leptr & 0xFFFF));
 321        writereg(&ll->rap, LE_CSR2);
 322        writereg(&ll->rdp, leptr >> 16);
 323        writereg(&ll->rap, LE_CSR3);
 324        writereg(&ll->rdp, lp->busmaster_regval);
 325
 326        /* Point back to csr0 */
 327        writereg(&ll->rap, LE_CSR0);
 328}
 329
 330/*
 331 * Our specialized copy routines
 332 *
 333 */
 334void cp_to_buf(void *to, const void *from, __kernel_size_t len)
 335{
 336        unsigned short *tp, *fp, clen;
 337        unsigned char *rtp, *rfp;
 338
 339        if (type == PMAX_LANCE) {
 340                clen = len >> 1;
 341                tp = (unsigned short *) to;
 342                fp = (unsigned short *) from;
 343
 344                while (clen--) {
 345                        *tp++ = *fp++;
 346                        tp++;
 347                }
 348
 349                clen = len & 1;
 350                rtp = (unsigned char *) tp;
 351                rfp = (unsigned char *) fp;
 352                while (clen--) {
 353                        *rtp++ = *rfp++;
 354                }
 355        } else {
 356                /*
 357                 * copy 16 Byte chunks
 358                 */
 359                clen = len >> 4;
 360                tp = (unsigned short *) to;
 361                fp = (unsigned short *) from;
 362                while (clen--) {
 363                        *tp++ = *fp++;
 364                        *tp++ = *fp++;
 365                        *tp++ = *fp++;
 366                        *tp++ = *fp++;
 367                        *tp++ = *fp++;
 368                        *tp++ = *fp++;
 369                        *tp++ = *fp++;
 370                        *tp++ = *fp++;
 371                        tp += 8;
 372                }
 373
 374                /*
 375                 * do the rest, if any.
 376                 */
 377                clen = len & 15;
 378                rtp = (unsigned char *) tp;
 379                rfp = (unsigned char *) fp;
 380                while (clen--) {
 381                        *rtp++ = *rfp++;
 382                }
 383        }
 384
 385        wbflush();
 386}
 387
 388void cp_from_buf(void *to, unsigned char *from, int len)
 389{
 390        unsigned short *tp, *fp, clen;
 391        unsigned char *rtp, *rfp;
 392
 393        if (type == PMAX_LANCE) {
 394                clen = len >> 1;
 395                tp = (unsigned short *) to;
 396                fp = (unsigned short *) from;
 397                while (clen--) {
 398                        *tp++ = *fp++;
 399                        fp++;
 400                }
 401
 402                clen = len & 1;
 403
 404                rtp = (unsigned char *) tp;
 405                rfp = (unsigned char *) fp;
 406
 407                while (clen--) {
 408                        *rtp++ = *rfp++;
 409                }
 410        } else {
 411
 412                /*
 413                 * copy 16 Byte chunks
 414                 */
 415                clen = len >> 4;
 416                tp = (unsigned short *) to;
 417                fp = (unsigned short *) from;
 418                while (clen--) {
 419                        *tp++ = *fp++;
 420                        *tp++ = *fp++;
 421                        *tp++ = *fp++;
 422                        *tp++ = *fp++;
 423                        *tp++ = *fp++;
 424                        *tp++ = *fp++;
 425                        *tp++ = *fp++;
 426                        *tp++ = *fp++;
 427                        fp += 8;
 428                }
 429
 430                /*
 431                 * do the rest, if any.
 432                 */
 433                clen = len & 15;
 434                rtp = (unsigned char *) tp;
 435                rfp = (unsigned char *) fp;
 436                while (clen--) {
 437                        *rtp++ = *rfp++;
 438                }
 439
 440
 441        }
 442
 443}
 444
 445/* Setup the Lance Rx and Tx rings */
 446static void lance_init_ring(struct net_device *dev)
 447{
 448        struct lance_private *lp = (struct lance_private *) dev->priv;
 449        volatile struct lance_init_block *ib;
 450        int leptr;
 451        int i;
 452
 453        ib = (struct lance_init_block *) (dev->mem_start);
 454
 455        /* Lock out other processes while setting up hardware */
 456        netif_stop_queue(dev);
 457        lp->rx_new = lp->tx_new = 0;
 458        lp->rx_old = lp->tx_old = 0;
 459
 460        /* Copy the ethernet address to the lance init block.
 461         * XXX bit 0 of the physical address registers has to be zero
 462         */
 463        ib->phys_addr[0] = dev->dev_addr[0];
 464        ib->phys_addr[1] = dev->dev_addr[1];
 465        ib->phys_addr[4] = dev->dev_addr[2];
 466        ib->phys_addr[5] = dev->dev_addr[3];
 467        ib->phys_addr[8] = dev->dev_addr[4];
 468        ib->phys_addr[9] = dev->dev_addr[5];
 469        /* Setup the initialization block */
 470
 471        /* Setup rx descriptor pointer */
 472        leptr = LANCE_ADDR(libdesc_offset(brx_ring, 0));
 473        ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
 474        ib->rx_ptr = leptr;
 475        if (ZERO)
 476                printk("RX ptr: %8.8x(%8.8x)\n", leptr, libdesc_offset(brx_ring, 0));
 477
 478        /* Setup tx descriptor pointer */
 479        leptr = LANCE_ADDR(libdesc_offset(btx_ring, 0));
 480        ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
 481        ib->tx_ptr = leptr;
 482        if (ZERO)
 483                printk("TX ptr: %8.8x(%8.8x)\n", leptr, libdesc_offset(btx_ring, 0));
 484
 485        if (ZERO)
 486                printk("TX rings:\n");
 487
 488        /* Setup the Tx ring entries */
 489        for (i = 0; i < TX_RING_SIZE; i++) {
 490                leptr = (int) lp->tx_buf_ptr_lnc[i];
 491                ib->btx_ring[i].tmd0 = leptr;
 492                ib->btx_ring[i].tmd1_hadr = leptr >> 16;
 493                ib->btx_ring[i].tmd1_bits = 0;
 494                ib->btx_ring[i].length = 0xf000;        /* The ones required by tmd2 */
 495                ib->btx_ring[i].misc = 0;
 496                if (i < 3 && ZERO)
 497                        printk("%d: 0x%8.8x(0x%8.8x)\n", i, leptr, (int) lp->tx_buf_ptr_cpu[i]);
 498        }
 499
 500        /* Setup the Rx ring entries */
 501        if (ZERO)
 502                printk("RX rings:\n");
 503        for (i = 0; i < RX_RING_SIZE; i++) {
 504                leptr = (int) lp->rx_buf_ptr_lnc[i];
 505                ib->brx_ring[i].rmd0 = leptr;
 506                ib->brx_ring[i].rmd1_hadr = leptr >> 16;
 507                ib->brx_ring[i].rmd1_bits = LE_R1_OWN;
 508                ib->brx_ring[i].length = -RX_BUFF_SIZE | 0xf000;
 509                ib->brx_ring[i].mblength = 0;
 510                if (i < 3 && ZERO)
 511                        printk("%d: 0x%8.8x(0x%8.8x)\n", i, leptr, (int) lp->rx_buf_ptr_cpu[i]);
 512        }
 513        wbflush();
 514}
 515
 516static int init_restart_lance(struct lance_private *lp)
 517{
 518        volatile struct lance_regs *ll = lp->ll;
 519        int i;
 520
 521        writereg(&ll->rap, LE_CSR0);
 522        writereg(&ll->rdp, LE_C0_INIT);
 523
 524        /* Wait for the lance to complete initialization */
 525        for (i = 0; (i < 100) && !(ll->rdp & LE_C0_IDON); i++) {
 526                udelay(10);
 527        }
 528        if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
 529                printk("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, ll->rdp);
 530                return -1;
 531        }
 532        if ((ll->rdp & LE_C0_ERR)) {
 533                printk("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, ll->rdp);
 534                return -1;
 535        }
 536        writereg(&ll->rdp, LE_C0_IDON);
 537        writereg(&ll->rdp, LE_C0_STRT);
 538        writereg(&ll->rdp, LE_C0_INEA);
 539
 540        return 0;
 541}
 542
 543static int lance_rx(struct net_device *dev)
 544{
 545        struct lance_private *lp = (struct lance_private *) dev->priv;
 546        volatile struct lance_init_block *ib;
 547        volatile struct lance_rx_desc *rd = 0;
 548        unsigned char bits;
 549        int len = 0;
 550        struct sk_buff *skb = 0;
 551        ib = (struct lance_init_block *) (dev->mem_start);
 552
 553#ifdef TEST_HITS
 554        {
 555        int i;
 556
 557        printk("[");
 558        for (i = 0; i < RX_RING_SIZE; i++) {
 559                if (i == lp->rx_new)
 560                        printk("%s",
 561                               ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "_" : "X");
 562                else
 563                        printk("%s",
 564                               ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "." : "1");
 565        }
 566        printk("]");
 567        }
 568#endif
 569
 570
 571        for (rd = &ib->brx_ring[lp->rx_new];
 572             !((bits = rd->rmd1_bits) & LE_R1_OWN);
 573             rd = &ib->brx_ring[lp->rx_new]) {
 574
 575                /* We got an incomplete frame? */
 576                if ((bits & LE_R1_POK) != LE_R1_POK) {
 577                        lp->stats.rx_over_errors++;
 578                        lp->stats.rx_errors++;
 579                } else if (bits & LE_R1_ERR) {
 580                        /* Count only the end frame as a rx error,
 581                         * not the beginning
 582                         */
 583                        if (bits & LE_R1_BUF)
 584                                lp->stats.rx_fifo_errors++;
 585                        if (bits & LE_R1_CRC)
 586                                lp->stats.rx_crc_errors++;
 587                        if (bits & LE_R1_OFL)
 588                                lp->stats.rx_over_errors++;
 589                        if (bits & LE_R1_FRA)
 590                                lp->stats.rx_frame_errors++;
 591                        if (bits & LE_R1_EOP)
 592                                lp->stats.rx_errors++;
 593                } else {
 594                        len = (rd->mblength & 0xfff) - 4;
 595                        skb = dev_alloc_skb(len + 2);
 596
 597                        if (skb == 0) {
 598                                printk("%s: Memory squeeze, deferring packet.\n",
 599                                       dev->name);
 600                                lp->stats.rx_dropped++;
 601                                rd->mblength = 0;
 602                                rd->rmd1_bits = LE_R1_OWN;
 603                                lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
 604                                return 0;
 605                        }
 606                        lp->stats.rx_bytes += len;
 607
 608                        skb->dev = dev;
 609                        skb_reserve(skb, 2);    /* 16 byte align */
 610                        skb_put(skb, len);      /* make room */
 611
 612                        cp_from_buf(skb->data,
 613                                 (char *) lp->rx_buf_ptr_cpu[lp->rx_new],
 614                                    len);
 615
 616                        skb->protocol = eth_type_trans(skb, dev);
 617                        netif_rx(skb);
 618                        dev->last_rx = jiffies;
 619                        lp->stats.rx_packets++;
 620                }
 621
 622                /* Return the packet to the pool */
 623                rd->mblength = 0;
 624                rd->length = -RX_BUFF_SIZE | 0xf000;
 625                rd->rmd1_bits = LE_R1_OWN;
 626                lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
 627        }
 628        return 0;
 629}
 630
 631static void lance_tx(struct net_device *dev)
 632{
 633        struct lance_private *lp = (struct lance_private *) dev->priv;
 634        volatile struct lance_init_block *ib;
 635        volatile struct lance_regs *ll = lp->ll;
 636        volatile struct lance_tx_desc *td;
 637        int i, j;
 638        int status;
 639        ib = (struct lance_init_block *) (dev->mem_start);
 640        j = lp->tx_old;
 641
 642        spin_lock(&lp->lock);
 643
 644        for (i = j; i != lp->tx_new; i = j) {
 645                td = &ib->btx_ring[i];
 646                /* If we hit a packet not owned by us, stop */
 647                if (td->tmd1_bits & LE_T1_OWN)
 648                        break;
 649
 650                if (td->tmd1_bits & LE_T1_ERR) {
 651                        status = td->misc;
 652
 653                        lp->stats.tx_errors++;
 654                        if (status & LE_T3_RTY)
 655                                lp->stats.tx_aborted_errors++;
 656                        if (status & LE_T3_LCOL)
 657                                lp->stats.tx_window_errors++;
 658
 659                        if (status & LE_T3_CLOS) {
 660                                lp->stats.tx_carrier_errors++;
 661                                printk("%s: Carrier Lost\n", dev->name);
 662                                /* Stop the lance */
 663                                writereg(&ll->rap, LE_CSR0);
 664                                writereg(&ll->rdp, LE_C0_STOP);
 665                                lance_init_ring(dev);
 666                                load_csrs(lp);
 667                                init_restart_lance(lp);
 668                                goto out;
 669                        }
 670                        /* Buffer errors and underflows turn off the
 671                         * transmitter, restart the adapter.
 672                         */
 673                        if (status & (LE_T3_BUF | LE_T3_UFL)) {
 674                                lp->stats.tx_fifo_errors++;
 675
 676                                printk("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
 677                                       dev->name);
 678                                /* Stop the lance */
 679                                writereg(&ll->rap, LE_CSR0);
 680                                writereg(&ll->rdp, LE_C0_STOP);
 681                                lance_init_ring(dev);
 682                                load_csrs(lp);
 683                                init_restart_lance(lp);
 684                                goto out;
 685                        }
 686                } else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
 687                        /*
 688                         * So we don't count the packet more than once.
 689                         */
 690                        td->tmd1_bits &= ~(LE_T1_POK);
 691
 692                        /* One collision before packet was sent. */
 693                        if (td->tmd1_bits & LE_T1_EONE)
 694                                lp->stats.collisions++;
 695
 696                        /* More than one collision, be optimistic. */
 697                        if (td->tmd1_bits & LE_T1_EMORE)
 698                                lp->stats.collisions += 2;
 699
 700                        lp->stats.tx_packets++;
 701                }
 702                j = (j + 1) & TX_RING_MOD_MASK;
 703        }
 704        lp->tx_old = j;
 705out:
 706        if (netif_queue_stopped(dev) &&
 707            TX_BUFFS_AVAIL > 0)
 708                netif_wake_queue(dev);
 709
 710        spin_unlock(&lp->lock);
 711}
 712
 713static void lance_interrupt(const int irq, void *dev_id, struct pt_regs *regs)
 714{
 715        struct net_device *dev = (struct net_device *) dev_id;
 716        struct lance_private *lp = (struct lance_private *) dev->priv;
 717        volatile struct lance_regs *ll = lp->ll;
 718        int csr0;
 719
 720        writereg(&ll->rap, LE_CSR0);
 721        csr0 = ll->rdp;
 722
 723        /* Acknowledge all the interrupt sources ASAP */
 724        writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
 725
 726        if ((csr0 & LE_C0_ERR)) {
 727                /* Clear the error condition */
 728                writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
 729                         LE_C0_CERR | LE_C0_MERR);
 730        }
 731        if (csr0 & LE_C0_RINT)
 732                lance_rx(dev);
 733
 734        if (csr0 & LE_C0_TINT)
 735                lance_tx(dev);
 736
 737        if (csr0 & LE_C0_BABL)
 738                lp->stats.tx_errors++;
 739
 740        if (csr0 & LE_C0_MISS)
 741                lp->stats.rx_errors++;
 742
 743        if (csr0 & LE_C0_MERR) {
 744                volatile unsigned long int_stat = *(unsigned long *) (system_base + IOCTL + SIR);
 745
 746                printk("%s: Memory error, status %04x\n", dev->name, csr0);
 747
 748                if (int_stat & LANCE_DMA_MEMRDERR) {
 749                        printk("%s: DMA error\n", dev->name);
 750                        int_stat |= LANCE_DMA_MEMRDERR;
 751                        /*
 752                         * re-enable LANCE DMA
 753                         */
 754                        *(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
 755                        wbflush();
 756                }
 757                writereg(&ll->rdp, LE_C0_STOP);
 758
 759                lance_init_ring(dev);
 760                load_csrs(lp);
 761                init_restart_lance(lp);
 762                netif_wake_queue(dev);
 763        }
 764
 765        writereg(&ll->rdp, LE_C0_INEA);
 766        writereg(&ll->rdp, LE_C0_INEA);
 767}
 768
 769struct net_device *last_dev = 0;
 770
 771static int lance_open(struct net_device *dev)
 772{
 773        volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
 774        struct lance_private *lp = (struct lance_private *) dev->priv;
 775        volatile struct lance_regs *ll = lp->ll;
 776        int status = 0;
 777
 778        last_dev = dev;
 779
 780        /* Stop the Lance */
 781        writereg(&ll->rap, LE_CSR0);
 782        writereg(&ll->rdp, LE_C0_STOP);
 783
 784        /* Set mode and clear multicast filter only at device open,
 785         * so that lance_init_ring() called at any error will not
 786         * forget multicast filters.
 787         *
 788         * BTW it is common bug in all lance drivers! --ANK
 789         */
 790        ib->mode = 0;
 791        ib->filter [0] = 0;
 792        ib->filter [2] = 0;
 793        ib->filter [4] = 0;
 794        ib->filter [6] = 0;
 795
 796        lance_init_ring(dev);
 797        load_csrs(lp);
 798
 799        netif_start_queue(dev);
 800
 801        /* Associate IRQ with lance_interrupt */
 802        if (request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
 803                printk("Lance: Can't get irq %d\n", dev->irq);
 804                return -EAGAIN;
 805        }
 806
 807        status = init_restart_lance(lp);
 808
 809        /*
 810         * if (!status)
 811         *      MOD_INC_USE_COUNT;
 812         */
 813
 814        return status;
 815}
 816
 817static int lance_close(struct net_device *dev)
 818{
 819        struct lance_private *lp = (struct lance_private *) dev->priv;
 820        volatile struct lance_regs *ll = lp->ll;
 821
 822        netif_stop_queue(dev);
 823        del_timer_sync(&lp->multicast_timer);
 824
 825        /* Stop the card */
 826        writereg(&ll->rap, LE_CSR0);
 827        writereg(&ll->rdp, LE_C0_STOP);
 828
 829        free_irq(dev->irq, (void *) dev);
 830        /*
 831           MOD_DEC_USE_COUNT;
 832         */
 833        return 0;
 834}
 835
 836static inline int lance_reset(struct net_device *dev)
 837{
 838        struct lance_private *lp = (struct lance_private *) dev->priv;
 839        volatile struct lance_regs *ll = lp->ll;
 840        int status;
 841
 842        /* Stop the lance */
 843        writereg(&ll->rap, LE_CSR0);
 844        writereg(&ll->rdp, LE_C0_STOP);
 845
 846        lance_init_ring(dev);
 847        load_csrs(lp);
 848        dev->trans_start = jiffies;
 849        status = init_restart_lance(lp);
 850        return status;
 851}
 852
 853static void lance_tx_timeout(struct net_device *dev)
 854{
 855        struct lance_private *lp = (struct lance_private *) dev->priv;
 856        volatile struct lance_regs *ll = lp->ll;
 857
 858        printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
 859                               dev->name, ll->rdp);
 860                        lance_reset(dev);
 861        netif_wake_queue(dev);
 862}
 863
 864static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
 865{
 866        struct lance_private *lp = (struct lance_private *) dev->priv;
 867        volatile struct lance_regs *ll = lp->ll;
 868        volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
 869        int entry, skblen, len;
 870
 871        skblen = skb->len;
 872
 873        len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
 874
 875        lp->stats.tx_bytes += len;
 876
 877        entry = lp->tx_new & TX_RING_MOD_MASK;
 878        ib->btx_ring[entry].length = (-len);
 879        ib->btx_ring[entry].misc = 0;
 880
 881        cp_to_buf((char *) lp->tx_buf_ptr_cpu[entry], skb->data, skblen);
 882
 883        /* Clear the slack of the packet, do I need this? */
 884        /* For a firewall its a good idea - AC */
 885/*
 886   if (len != skblen)
 887   memset ((char *) &ib->tx_buf [entry][skblen], 0, (len - skblen) << 1);
 888 */
 889
 890        /* Now, give the packet to the lance */
 891        ib->btx_ring[entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
 892        lp->tx_new = (lp->tx_new + 1) & TX_RING_MOD_MASK;
 893
 894        if (TX_BUFFS_AVAIL <= 0)
 895                netif_stop_queue(dev);
 896
 897        /* Kick the lance: transmit now */
 898        writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
 899
 900        spin_unlock_irq(&lp->lock);
 901
 902        dev->trans_start = jiffies;
 903        dev_kfree_skb(skb);
 904
 905        return 0;
 906}
 907
 908static struct net_device_stats *lance_get_stats(struct net_device *dev)
 909{
 910        struct lance_private *lp = (struct lance_private *) dev->priv;
 911
 912        return &lp->stats;
 913}
 914
 915static void lance_load_multicast(struct net_device *dev)
 916{
 917        volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
 918        volatile u16 *mcast_table = (u16 *) & ib->filter;
 919        struct dev_mc_list *dmi = dev->mc_list;
 920        char *addrs;
 921        int i, j, bit, byte;
 922        u32 crc;
 923
 924        /* set all multicast bits */
 925        if (dev->flags & IFF_ALLMULTI) {
 926                ib->filter[0] = 0xffff;
 927                ib->filter[2] = 0xffff;
 928                ib->filter[4] = 0xffff;
 929                ib->filter[6] = 0xffff;
 930                return;
 931        }
 932        /* clear the multicast filter */
 933        ib->filter[0] = 0;
 934        ib->filter[2] = 0;
 935        ib->filter[4] = 0;
 936        ib->filter[6] = 0;
 937
 938        /* Add addresses */
 939        for (i = 0; i < dev->mc_count; i++) {
 940                addrs = dmi->dmi_addr;
 941                dmi = dmi->next;
 942
 943                /* multicast address? */
 944                if (!(*addrs & 1))
 945                        continue;
 946
 947                crc = ether_crc_le(6, addrs);
 948                crc = crc >> 26;
 949                mcast_table[2 * (crc >> 4)] |= 1 << (crc & 0xf);
 950        }
 951        return;
 952}
 953
 954static void lance_set_multicast(struct net_device *dev)
 955{
 956        struct lance_private *lp = (struct lance_private *) dev->priv;
 957        volatile struct lance_init_block *ib;
 958        volatile struct lance_regs *ll = lp->ll;
 959
 960        ib = (struct lance_init_block *) (dev->mem_start);
 961
 962        if (!netif_running(dev))
 963                return;
 964
 965        if (lp->tx_old != lp->tx_new) {
 966                mod_timer(&lp->multicast_timer, jiffies + 4);
 967                netif_wake_queue(dev);
 968                return;
 969        }
 970
 971        netif_stop_queue(dev);
 972
 973        writereg(&ll->rap, LE_CSR0);
 974        writereg(&ll->rdp, LE_C0_STOP);
 975
 976        lance_init_ring(dev);
 977
 978        if (dev->flags & IFF_PROMISC) {
 979                ib->mode |= LE_MO_PROM;
 980        } else {
 981                ib->mode &= ~LE_MO_PROM;
 982                lance_load_multicast(dev);
 983        }
 984        load_csrs(lp);
 985        init_restart_lance(lp);
 986        netif_wake_queue(dev);
 987}
 988
 989static void lance_set_multicast_retry(unsigned long _opaque)
 990{
 991        struct net_device *dev = (struct net_device *) _opaque;
 992
 993        lance_set_multicast(dev);
 994}
 995
 996static int __init dec_lance_init(struct net_device *dev, const int type)
 997{
 998        static unsigned version_printed;
 999        struct net_device *dev;
1000        struct lance_private *lp;
1001        volatile struct lance_regs *ll;
1002        int i, ret;
1003        unsigned long esar_base;
1004        unsigned char *esar;
1005
1006#ifndef CONFIG_TC
1007        system_base = KN01_LANCE_BASE;
1008#else
1009        int slot;
1010#endif
1011
1012        if (dec_lance_debug && version_printed++ == 0)
1013                printk(version);
1014
1015        dev = init_etherdev(0, sizeof(struct lance_private));
1016        if (!dev)
1017                return -ENOMEM;
1018
1019        /* init_etherdev ensures the data structures used by the LANCE are aligned. */
1020        lp = (struct lance_private *) dev->priv;
1021        spin_lock_init(&lp->lock);
1022
1023        switch (type) {
1024#ifdef CONFIG_TC
1025        case ASIC_LANCE:
1026                dev->base_addr = system_base + LANCE;
1027
1028                /* buffer space for the on-board LANCE shared memory */
1029                /*
1030                 * FIXME: ugly hack!
1031                 */
1032                dev->mem_start = KSEG1ADDR(0x00020000);
1033                dev->mem_end = dev->mem_start + 0x00020000;
1034                dev->irq = ETHER;
1035                esar_base = system_base + ESAR;
1036        
1037                /* Workaround crash with booting KN04 2.1k from Disk */
1038                memset(dev->mem_start, 0, dev->mem_end - dev->mem_start);
1039
1040                /*
1041                 * setup the pointer arrays, this sucks [tm] :-(
1042                 */
1043                for (i = 0; i < RX_RING_SIZE; i++) {
1044                        lp->rx_buf_ptr_cpu[i] = (char *) (dev->mem_start + BUF_OFFSET_CPU
1045                                                 + 2 * i * RX_BUFF_SIZE);
1046                        lp->rx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1047                                                     + i * RX_BUFF_SIZE);
1048                }
1049                for (i = 0; i < TX_RING_SIZE; i++) {
1050                        lp->tx_buf_ptr_cpu[i] = (char *) (dev->mem_start + BUF_OFFSET_CPU
1051                                        + 2 * RX_RING_SIZE * RX_BUFF_SIZE
1052                                                 + 2 * i * TX_BUFF_SIZE);
1053                        lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1054                                            + RX_RING_SIZE * RX_BUFF_SIZE
1055                                                     + i * TX_BUFF_SIZE);
1056                }
1057
1058                /*
1059                 * setup and enable IOASIC LANCE DMA
1060                 */
1061                lp->dma_ptr_reg = (unsigned long *) (system_base + IOCTL + LANCE_DMA_P);
1062                *(lp->dma_ptr_reg) = PHYSADDR(dev->mem_start) << 3;
1063                *(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
1064                wbflush();
1065
1066                break;
1067        case PMAD_LANCE:
1068                slot = search_tc_card("PMAD-AA");
1069                claim_tc_card(slot);
1070
1071                dev->mem_start = get_tc_base_addr(slot);
1072                dev->base_addr = dev->mem_start + 0x100000;
1073                dev->irq = get_tc_irq_nr(slot);
1074                esar_base = dev->mem_start + 0x1c0002;
1075                break;
1076#endif
1077        case PMAX_LANCE:
1078                dev->irq = ETHER;
1079                dev->base_addr = KN01_LANCE_BASE;
1080                dev->mem_start = KN01_LANCE_BASE + 0x01000000;
1081                esar_base = KN01_RTC_BASE + 1;
1082                /*
1083                 * setup the pointer arrays, this sucks [tm] :-(
1084                 */
1085                for (i = 0; i < RX_RING_SIZE; i++) {
1086                        lp->rx_buf_ptr_cpu[i] =
1087                            (char *) (dev->mem_start + BUF_OFFSET_CPU
1088                                      + 2 * i * RX_BUFF_SIZE);
1089
1090                        lp->rx_buf_ptr_lnc[i] =
1091                            (char *) (BUF_OFFSET_LNC
1092                                      + i * RX_BUFF_SIZE);
1093
1094                }
1095                for (i = 0; i < TX_RING_SIZE; i++) {
1096                        lp->tx_buf_ptr_cpu[i] =
1097                            (char *) (dev->mem_start + BUF_OFFSET_CPU
1098                                      + 2 * RX_RING_SIZE * RX_BUFF_SIZE
1099                                      + 2 * i * TX_BUFF_SIZE);
1100                        lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1101                                            + RX_RING_SIZE * RX_BUFF_SIZE
1102                                                     + i * TX_BUFF_SIZE);
1103
1104                }
1105                break;
1106        default:
1107                printk("declance_init called with unknown type\n");
1108                ret = -ENODEV;
1109                goto err_out;
1110        }
1111
1112        ll = (struct lance_regs *) dev->base_addr;
1113        esar = (unsigned char *) esar_base;
1114
1115        /* prom checks */
1116        /* First, check for test pattern */
1117        if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
1118            esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
1119                printk("Ethernet station address prom not found!\n");
1120                ret = -ENODEV;
1121                goto err_out;
1122        }
1123        /* Check the prom contents */
1124        for (i = 0; i < 8; i++) {
1125                if (esar[i * 4] != esar[0x3c - i * 4] &&
1126                    esar[i * 4] != esar[0x40 + i * 4] &&
1127                    esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
1128                        printk("Something is wrong with the ethernet "
1129                               "station address prom!\n");
1130                        ret = -ENODEV;
1131                        goto err_out;
1132                }
1133        }
1134
1135        /* Copy the ethernet address to the device structure, later to the
1136         * lance initialization block so the lance gets it every time it's
1137         * (re)initialized.
1138         */
1139        switch (type) {
1140        case ASIC_LANCE:
1141                printk("%s: IOASIC onboard LANCE, addr = ", dev->name);
1142                break;
1143        case PMAD_LANCE:
1144                printk("%s: PMAD-AA, addr = ", dev->name);
1145                break;
1146        case PMAX_LANCE:
1147                printk("%s: PMAX onboard LANCE, addr = ", dev->name);
1148                break;
1149        }
1150        for (i = 0; i < 6; i++) {
1151                dev->dev_addr[i] = esar[i * 4];
1152                printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
1153        }
1154
1155        printk(" irq = %d\n", dev->irq);
1156
1157        lp->dev = dev;
1158        dev->open = &lance_open;
1159        dev->stop = &lance_close;
1160        dev->hard_start_xmit = &lance_start_xmit;
1161        dev->tx_timeout = &lance_tx_timeout;
1162        dev->watchdog_timeo = 5*HZ;
1163        dev->get_stats = &lance_get_stats;
1164        dev->set_multicast_list = &lance_set_multicast;
1165
1166        /* lp->ll is the location of the registers for lance card */
1167        lp->ll = ll;
1168
1169        lp->name = lancestr;
1170
1171        /* busmaster_regval (CSR3) should be zero according to the PMAD-AA
1172         * specification.
1173         */
1174        lp->busmaster_regval = 0;
1175
1176        dev->dma = 0;
1177
1178        ether_setup(dev);
1179
1180        /* We cannot sleep if the chip is busy during a
1181         * multicast list update event, because such events
1182         * can occur from interrupts (ex. IPv6).  So we
1183         * use a timer to try again later when necessary. -DaveM
1184         */
1185        init_timer(&lp->multicast_timer);
1186        lp->multicast_timer.data = (unsigned long) dev;
1187        lp->multicast_timer.function = &lance_set_multicast_retry;
1188
1189#ifdef MODULE
1190        dev->ifindex = dev_new_index();
1191        lp->next_module = root_lance_dev;
1192        root_lance_dev = lp;
1193#endif
1194        return 0;
1195
1196err_out:
1197        unregister_netdev(dev);
1198        kfree(dev);
1199        return ret;
1200}
1201
1202
1203/* Find all the lance cards on the system and initialize them */
1204static int __init dec_lance_probe(void)
1205{
1206        struct net_device *dev = NULL;
1207        static int called;
1208
1209#ifdef MODULE
1210        root_lance_dev = NULL;
1211#endif
1212
1213
1214#ifdef CONFIG_TC
1215        int slot = -1;
1216
1217        if (TURBOCHANNEL) {
1218                if (IOASIC && !called) {
1219                        called = 1;
1220                        type = ASIC_LANCE;
1221                } else {
1222                        if ((slot = search_tc_card("PMAD-AA")) >= 0) {
1223                                type = PMAD_LANCE;
1224                        } else {
1225                                return -ENODEV;
1226                        }
1227                }
1228        } else {
1229                if (!called) {
1230                        called = 1;
1231                        type = PMAX_LANCE;
1232                } else {
1233                        return -ENODEV;
1234                }
1235        }
1236#else
1237        if (!called && !TURBOCHANNEL) {
1238                called = 1;
1239                type = PMAX_LANCE;
1240        } else {
1241                return -ENODEV;
1242        }
1243#endif
1244
1245        return dec_lance_init(dev, type);
1246}
1247
1248static void __exit dec_lance_cleanup(void)
1249{
1250#ifdef MODULE
1251   struct lance_private *lp;
1252
1253   while (root_lance_dev) {
1254   lp = root_lance_dev->next_module;
1255
1256   unregister_netdev(root_lance_dev->dev);
1257   kfree(root_lance_dev->dev);
1258   root_lance_dev = lp;
1259   }
1260#endif /* MODULE */
1261}
1262
1263module_init(dec_lance_probe);
1264module_exit(dec_lance_cleanup);
1265
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.