root/drivers/net/znote.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. znet_probe
  2. znet_open
  3. znet_send_packet
  4. znet_interrupt
  5. znet_rx
  6. znet_close
  7. net_get_stats
  8. set_multicast_list
  9. show_dma
  10. hardware_init
  11. foo
  12. do_command
  13. wait_for_done
  14. update_stop_hit

   1 /* znet.c: An Zenith Z-Note ethernet driver for linux. */
   2 
   3 static char *version = "znet.c:v0.04 5/10/94 becker@cesdis.gsfc.nasa.gov\n";
   4 
   5 /*
   6         Written by Donald Becker.
   7 
   8         The author may be reached as becker@cesdis.gsfc.nasa.gov.
   9         This driver is based on the Linux skeleton driver.  The copyright of the
  10         skeleton driver is held by the United States Government, as represented
  11         by DIRNSA, and it is released under the GPL.
  12 
  13         Thanks to Mike Hollick for alpha testing and suggestions.
  14 
  15   References:
  16            The Crynwr packet driver.
  17 
  18           "82593 CSMA/CD Core LAN Controller" Intel datasheet, 1992
  19           Intel Microcommunications Databook, Vol. 1, 1990.
  20     As usual with Intel, the documentation is incomplete and inaccurate.
  21         I had to read the Crynwr packet driver to figure out how to actually
  22         use the i82593, and guess at what register bits matched the loosely
  23         related i82586.
  24 
  25                                         Theory of Operation
  26 
  27         The i82593 used in the Zenith Z-Note series operates using two(!) slave
  28         DMA     channels, one interrupt, and one 8-bit I/O port.
  29 
  30         While there     several ways to configure '593 DMA system, I chose the one
  31         that seemed commesurate with the highest system performance in the face
  32         of moderate interrupt latency: Both DMA channels are configued as
  33         recirculating ring buffers, with one channel (#0) dedicated to Rx and
  34         the other channel (#1) to Tx and configuration.  (Note that this is
  35         different than the Crynwr driver, where the Tx DMA channel is initialized
  36         before each operation.  That approach simplifies operation and Tx error
  37         recovery, but requires additional I/O in normal operation and precludes
  38         transmit buffer chaining.)
  39 
  40         Both rings are set to 8192 bytes using {TX,RX}_RING_SIZE.  This provides
  41         a reasonable ring size for Rx, while simplifying DMA buffer allocation --
  42         DMA buffers must not cross a 128K boundary.  (In truth the size selection
  43         was influenced by my lack of '593 documentation.  I thus was constrained
  44         to use the Crynwr '593 initialization table, which sets the Rx ring size
  45         to 8K.)
  46 
  47         Despite my usual low opinion about Intel-designed parts, I must admit
  48         that the bulk data handling of the i82593 is a good design for
  49         an integrated system, like a laptop, where using two slave DMA channels
  50         doesn't pose a problem.  I still take issue with using only a single I/O
  51         port.  In the same controlled environment there are essentially no
  52         limitations on I/O space, and using multiple locations would eliminate
  53         the     need for multiple operations when looking at status registers,
  54         setting the Rx ring boundary, or switching to promiscuous mode.
  55 
  56         I also question Zenith's selection of the '593: one of the advertised
  57         advantages of earlier Intel parts was that if you figured out the magic
  58         initialization incantation you could use the same part on many different
  59         network types.  Zenith's use of the "FriendlyNet" (sic) connector rather
  60         than an on-board transceiver leads me to believe that they were planning
  61         to take advantage of this.  But, uhmmm, the '593 omits all but ethernet
  62         functionality from the serial subsystem.
  63  */
  64 
  65 #include <linux/config.h>
  66 #include <linux/kernel.h>
  67 #include <linux/sched.h>
  68 #include <linux/string.h>
  69 #include <linux/ptrace.h>
  70 #include <linux/errno.h>
  71 #include <linux/interrupt.h>
  72 #include <asm/system.h>
  73 #include <asm/bitops.h>
  74 #include <asm/io.h>
  75 #include <asm/dma.h>
  76 
  77 #include <linux/netdevice.h>
  78 #include <linux/etherdevice.h>
  79 #include <linux/skbuff.h>
  80 #include <linux/if_arp.h>
  81 
  82 #ifndef HAVE_AUTOIRQ
  83 /* From auto_irq.c, in ioport.h for later versions. */
  84 extern void autoirq_setup(int waittime);
  85 extern int autoirq_report(int waittime);
  86 /* The map from IRQ number (as passed to the interrupt handler) to
  87    'struct device'. */
  88 extern struct device *irq2dev_map[16];
  89 #endif
  90 
  91 #ifndef HAVE_ALLOC_SKB
  92 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
  93 #define kfree_skbmem(addr, size) kfree_s(addr,size);
  94 #endif
  95 
  96 #ifndef ZNET_DEBUG
  97 #define ZNET_DEBUG 3
  98 #endif
  99 static unsigned int znet_debug = ZNET_DEBUG;
 100 
 101 /* The DMA modes we need aren't in <dma.h>. */
 102 #define DMA_RX_MODE             0x14    /* Auto init, I/O to mem, ++, demand. */
 103 #define DMA_TX_MODE             0x18    /* Auto init, Mem to I/O, ++, demand. */
 104 #define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
 105 #define DMA_BUF_SIZE 8192
 106 #define RX_BUF_SIZE 8192
 107 #define TX_BUF_SIZE 8192
 108 
 109 /* Commands to the i82593 channel 0. */
 110 #define CMD0_CHNL_0                     0x00
 111 #define CMD0_CHNL_1                     0x10            /* Switch to channel 1. */
 112 #define CMD0_NOP (CMD0_CHNL_0)
 113 #define CMD0_PORT_1     CMD0_CHNL_1
 114 #define CMD1_PORT_0     1
 115 #define CMD0_IA_SETUP           1
 116 #define CMD0_CONFIGURE          2
 117 #define CMD0_MULTICAST_LIST 3
 118 #define CMD0_TRANSMIT           4
 119 #define CMD0_DUMP                       6
 120 #define CMD0_DIAGNOSE           7
 121 #define CMD0_Rx_ENABLE          8
 122 #define CMD0_Rx_DISABLE         10
 123 #define CMD0_Rx_STOP            11
 124 #define CMD0_RETRANSMIT         12
 125 #define CMD0_ABORT                      13
 126 #define CMD0_RESET                      14
 127 
 128 #define CMD0_ACK 0x80
 129 
 130 #define CMD0_STAT0 (0 << 5)
 131 #define CMD0_STAT1 (1 << 5)
 132 #define CMD0_STAT2 (2 << 5)
 133 #define CMD0_STAT3 (3 << 5)
 134 
 135 #define net_local znet_private
 136 struct znet_private {
 137         int rx_dma, tx_dma;
 138         struct enet_statistics stats;
 139         /* The starting, current, and end pointers for the packet buffers. */
 140         ushort *rx_start, *rx_cur, *rx_end;
 141         ushort *tx_start, *tx_cur, *tx_end;
 142         ushort tx_buf_len;                      /* Tx buffer lenght, in words. */
 143 };
 144 
 145 /* Only one can be built-in;-> */
 146 static struct znet_private zn;
 147 static ushort dma_buffer1[DMA_BUF_SIZE/2];
 148 static ushort dma_buffer2[DMA_BUF_SIZE/2];
 149 static ushort dma_buffer3[DMA_BUF_SIZE/2 + 8];
 150 
 151 /* The configuration block.  What an undocumented nightmare.  The first
 152    set of values are those suggested (without explaination) for ethernet
 153    in the Intel 82586 databook.  The rest appear to be completely undocumented,
 154    except for cryptic notes in the Crynwr packet driver.  This driver uses
 155    the Crynwr values verbatim. */
 156 
 157 static unsigned char i593_init[] = {
 158   0xAA,                                 /* 0: 16-byte input & 80-byte output FIFO. */
 159                                                 /*        threshhold, 96-byte FIFO, 82593 mode. */
 160   0x88,                                 /* 1: Continuous w/interrupts, 128-clock DMA.*/
 161   0x2E,                                 /* 2: 8-byte preamble, NO address insertion, */
 162                                                 /*        6-byte Ethernet address, loopback off.*/
 163   0x00,                                 /* 3: Default priorities & backoff methods. */
 164   0x60,                                 /* 4: 96-bit interframe spacing. */
 165   0x00,                                 /* 5: 512-bit slot time (low-order). */
 166   0xF2,                                 /* 6: Slot time (high-order), 15 COLL retries. */
 167   0x00,                                 /* 7: Promisc-off, broadcast-on, default CRC. */
 168   0x00,                                 /* 8: Default carrier-sense, collision-detect. */
 169   0x40,                                 /* 9: 64-byte minimum frame length. */
 170   0x5F,                                 /* A: Type/length checks OFF, no CRC input,
 171                                                    "jabber" termination, etc. */
 172   0x00,                                 /* B: Full-duplex disabled. */
 173   0x3F,                                 /* C: Default multicast addresses & backoff. */
 174   0x07,                                 /* D: Default IFS retriggering. */
 175   0x31,                                 /* E: Internal retransmit, drop "runt" packets,
 176                                                    synchr. DRQ deassertion, 6 status bytes. */
 177   0x22,                                 /* F: Receive ring-buffer size (8K), 
 178                                                    receive-stop register enable. */
 179 };
 180 
 181 struct netidblk {
 182         char magic[8];          /* The magic number (string) "NETIDBLK" */
 183         unsigned char netid[8]; /* The physical station address */
 184         char nettype, globalopt;
 185         char vendor[8];         /* The machine vendor and product name. */
 186         char product[8];
 187         char irq1, irq2;                /* Interrupts, only one is currently used.      */
 188         char dma1, dma2;
 189         short dma_mem_misc[8];          /* DMA buffer locations (unused in Linux). */
 190         short iobase1, iosize1;
 191         short iobase2, iosize2;         /* Second iobase unused. */
 192         char driver_options;                    /* Misc. bits */
 193         char pad;
 194 };
 195 
 196 int znet_probe(struct device *dev);
 197 static int      znet_open(struct device *dev);
 198 static int      znet_send_packet(struct sk_buff *skb, struct device *dev);
 199 static void     znet_interrupt(int reg_ptr);
 200 static void     znet_rx(struct device *dev);
 201 static int      znet_close(struct device *dev);
 202 static struct enet_statistics *net_get_stats(struct device *dev);
 203 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 204 static void hardware_init(struct device *dev);
 205 static int do_command(short ioaddr, int command, int length, ushort *buffer);
 206 static int      wait_for_done(short ioaddr);
 207 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
 208 
 209 #ifdef notdef
 210 static struct sigaction znet_sigaction = { &znet_interrupt, 0, 0, NULL, };
 211 #endif
 212 
 213 
 214 /* The Z-Note probe is pretty easy.  The NETIDBLK exists in the safe-to-probe
 215    BIOS area.  We just scan for the signature, and pull the vital parameters
 216    out of the structure. */
 217 
 218 int znet_probe(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 219 {
 220         int i;
 221         struct netidblk *netinfo;
 222         char *p;
 223 
 224         /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
 225         for(p = (char *)0xf0000; p < (char *)0x100000; p++)
 226                 if (*p == 'N'  &&  strncmp(p, "NETIDBLK", 8) == 0)
 227                         break;
 228 
 229         if (p >= (char *)0x100000) {
 230                 if (znet_debug > 1)
 231                         printk("No Z-Note ethernet adaptor found.\n");
 232                 return ENODEV;
 233         }
 234         netinfo = (struct netidblk *)p;
 235         dev->base_addr = netinfo->iobase1;
 236         dev->irq = netinfo->irq1;
 237 
 238         printk("%s: ZNET at %#3x,", dev->name, dev->base_addr);
 239 
 240         /* The station address is in the "netidblk" at 0x0f0000. */
 241         for (i = 0; i < 6; i++)
 242                 printk(" %2.2x", dev->dev_addr[i] = netinfo->netid[i]);
 243 
 244         printk(", using IRQ %d DMA %d and %d.\n", dev->irq, netinfo->dma1,
 245                 netinfo->dma2);
 246 
 247         if (znet_debug > 1) {
 248                 printk("%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
 249                            dev->name, netinfo->vendor,
 250                            netinfo->irq1, netinfo->irq2,
 251                            netinfo->dma1, netinfo->dma2);
 252                 printk("%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
 253                            dev->name, netinfo->iobase1, netinfo->iosize1,
 254                            netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
 255         }
 256 
 257         if (znet_debug > 0)
 258                 printk(version);
 259 
 260         dev->priv = (void *) &zn;
 261         zn.rx_dma = netinfo->dma1;
 262         zn.tx_dma = netinfo->dma2;
 263 
 264         /* These should never fail.  You can't add devices to a sealed box! */
 265         if (request_irq(dev->irq, &znet_interrupt)
 266                 || request_dma(zn.rx_dma)
 267                 || request_dma(zn.tx_dma)) {
 268                 printk("Not opened -- resource busy?!?\n");
 269                 return EBUSY;
 270         }
 271         irq2dev_map[dev->irq] = dev;
 272 
 273         /* Allocate buffer memory.      We can cross a 128K boundary, so we
 274            must be careful about the allocation.  It's easiest to waste 8K. */
 275         if (dma_page_eq(dma_buffer1, &dma_buffer1[RX_BUF_SIZE/2-1]))
 276           zn.rx_start = dma_buffer1;
 277         else 
 278           zn.rx_start = dma_buffer2;
 279 
 280         if (dma_page_eq(dma_buffer3, &dma_buffer3[RX_BUF_SIZE/2-1]))
 281           zn.tx_start = dma_buffer3;
 282         else
 283           zn.tx_start = dma_buffer2;
 284         zn.rx_end = zn.rx_start + RX_BUF_SIZE/2;
 285         zn.tx_buf_len = TX_BUF_SIZE/2;
 286         zn.tx_end = zn.tx_start + zn.tx_buf_len;
 287 
 288         /* The ZNET-specific entries in the device structure. */
 289         dev->open = &znet_open;
 290         dev->hard_start_xmit = &znet_send_packet;
 291         dev->stop = &znet_close;
 292         dev->get_stats  = net_get_stats;
 293 #ifdef HAVE_MULTICAST
 294         dev->set_multicast_list = &set_multicast_list;
 295 #endif
 296 
 297         /* Fill in the generic field of the device structure. */
 298         for (i = 0; i < DEV_NUMBUFFS; i++)
 299                 dev->buffs[i] = NULL;
 300 
 301         dev->hard_header        = eth_header;
 302         dev->add_arp            = eth_add_arp;
 303         dev->queue_xmit         = dev_queue_xmit;
 304         dev->rebuild_header     = eth_rebuild_header;
 305         dev->type_trans         = eth_type_trans;
 306 
 307         dev->type                       = ARPHRD_ETHER;
 308         dev->hard_header_len = ETH_HLEN;
 309         dev->mtu                        = 1500; /* eth_mtu */
 310         dev->addr_len           = ETH_ALEN;
 311         for (i = 0; i < ETH_ALEN; i++) {
 312                 dev->broadcast[i]=0xff;
 313         }
 314 
 315         /* New-style flags. */
 316         dev->flags                      = IFF_BROADCAST;
 317         dev->family                     = AF_INET;
 318         dev->pa_addr            = 0;
 319         dev->pa_brdaddr         = 0;
 320         dev->pa_mask            = 0;
 321         dev->pa_alen            = sizeof(unsigned long);
 322 
 323         return 0;
 324 }
 325 
 326 
 327 static int znet_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329         int ioaddr = dev->base_addr;
 330 
 331         if (znet_debug > 2)
 332                 printk("%s: znet_open() called.\n", dev->name);
 333 
 334         /* Turn on the 82501 SIA, using zenith-specific magic. */
 335         outb(0x10, 0xe6);                                       /* Select LAN control register */
 336         outb(inb(0xe7) | 0x84, 0xe7);           /* Turn on LAN power (bit 2). */
 337         /* According to the Crynwr driver we should wait 50 msec. for the
 338            LAN clock to stabilize.  My experiments indicates that the '593 can
 339            be initialized immediately.  The delay is probably needed for the
 340            DC-to-DC converter to come up to full voltage, and for the oscillator
 341            to be spot-on at 20Mhz before transmitting.
 342            Until this proves to be a problem we rely on the higher layers for the
 343            delay and save allocating a timer entry. */
 344 
 345         /* This follows the packet driver's lead, and checks for success. */
 346         if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
 347                 printk("%s: Problem turning on the transceiver power.\n", dev->name);
 348 
 349         dev->tbusy = 0;
 350         dev->interrupt = 0;
 351         hardware_init(dev);
 352         dev->start = 1;
 353 
 354         return 0;
 355 }
 356 
 357 static int znet_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359         int ioaddr = dev->base_addr;
 360 
 361         if (znet_debug > 4)
 362                 printk("%s: ZNet_send_packet(%d).\n", dev->name, dev->tbusy);
 363 
 364         /* Transmitter timeout, could be a serious problems. */
 365         if (dev->tbusy) {
 366                 ushort event, tx_status, rx_offset, state;
 367                 int tickssofar = jiffies - dev->trans_start;
 368                 if (tickssofar < 10)
 369                         return 1;
 370                 outb(CMD0_STAT0, ioaddr); event = inb(ioaddr);
 371                 outb(CMD0_STAT1, ioaddr); tx_status = inw(ioaddr);
 372                 outb(CMD0_STAT2, ioaddr); rx_offset = inw(ioaddr);
 373                 outb(CMD0_STAT3, ioaddr); state = inb(ioaddr);
 374                 printk("%s: transmit timed out, status %02x %04x %04x %02x,"
 375                            " resetting.\n", dev->name, event, tx_status, rx_offset, state);
 376                 if (tx_status == 0x0400)
 377                   printk("%s: Tx carrier error, check transceiver cable.\n",
 378                                  dev->name);
 379                 outb(CMD0_RESET, ioaddr);
 380                 hardware_init(dev);
 381         }
 382 
 383         if (skb == NULL) {
 384                 dev_tint(dev);
 385                 return 0;
 386         }
 387 
 388         /* Fill in the ethernet header. */
 389         if (!skb->arp  &&  dev->rebuild_header(skb+1, dev)) {
 390                 skb->dev = dev;
 391                 arp_queue (skb);
 392                 return 0;
 393         }
 394 
 395         /* Check that the part hasn't reset itself, probably from suspend. */
 396         outb(CMD0_STAT0, ioaddr);
 397         if (inw(ioaddr) == 0x0010
 398                 && inw(ioaddr) == 0x0000
 399                 && inw(ioaddr) == 0x0010)
 400           hardware_init(dev);
 401 
 402         /* Block a timer-based transmit from overlapping.  This could better be
 403            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 404         if (set_bit(0, (void*)&dev->tbusy) != 0)
 405                 printk("%s: Transmitter access conflict.\n", dev->name);
 406         else {
 407                 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 408                 unsigned char *buf = (void *)(skb+1);
 409                 ushort *tx_link = zn.tx_cur - 1;
 410                 ushort rnd_len = (length + 1)>>1;
 411 
 412                 {
 413                         short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
 414                         unsigned addr = inb(dma_port);
 415                         addr |= inb(dma_port) << 8;
 416                         addr <<= 1;
 417                         if (((int)zn.tx_cur & 0x1ffff) != addr)
 418                           printk("Address mismatch at Tx: %#x vs %#x.\n",
 419                                          (int)zn.tx_cur & 0xffff, addr);
 420                         zn.tx_cur = (ushort *)(((int)zn.tx_cur & 0xfe0000) | addr);
 421                 }
 422 
 423                 if (zn.tx_cur >= zn.tx_end)
 424                   zn.tx_cur = zn.tx_start;
 425                 *zn.tx_cur++ = length;
 426                 if (zn.tx_cur + rnd_len + 1 > zn.tx_end) {
 427                         int semi_cnt = (zn.tx_end - zn.tx_cur)<<1; /* Cvrt to byte cnt. */
 428                         memcpy(zn.tx_cur, buf, semi_cnt);
 429                         rnd_len -= semi_cnt>>1;
 430                         memcpy(zn.tx_start, buf + semi_cnt, length - semi_cnt);
 431                         zn.tx_cur = zn.tx_start + rnd_len;
 432                 } else {
 433                         memcpy(zn.tx_cur, buf, skb->len);
 434                         zn.tx_cur += rnd_len;
 435                 }
 436                 *zn.tx_cur++ = 0;
 437                 cli(); {
 438                         *tx_link = CMD0_TRANSMIT + CMD0_CHNL_1;
 439                         /* Is this always safe to do? */
 440                         outb(CMD0_TRANSMIT + CMD0_CHNL_1,ioaddr);
 441                 } sti();
 442 
 443                 dev->trans_start = jiffies;
 444                 if (znet_debug > 4)
 445                   printk("%s: Transmitter queued, length %d.\n", dev->name, length);
 446         }
 447         dev_kfree_skb(skb, FREE_WRITE); 
 448         return 0;
 449 }
 450 
 451 /* The ZNET interrupt handler. */
 452 static void     znet_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 453 {
 454         int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 455         struct device *dev = irq2dev_map[irq];
 456         int ioaddr;
 457         int boguscnt = 20;
 458 
 459         if (dev == NULL) {
 460                 printk ("znet_interrupt(): IRQ %d for unknown device.\n", irq);
 461                 return;
 462         }
 463 
 464         dev->interrupt = 1;
 465         ioaddr = dev->base_addr;
 466 
 467         outb(CMD0_STAT0, ioaddr);
 468         do {
 469                 ushort status = inb(ioaddr);
 470                 if (znet_debug > 5) {
 471                         ushort result, rx_ptr, running;
 472                         outb(CMD0_STAT1, ioaddr);
 473                         result = inw(ioaddr);
 474                         outb(CMD0_STAT2, ioaddr);
 475                         rx_ptr = inw(ioaddr);
 476                         outb(CMD0_STAT3, ioaddr);
 477                         running = inb(ioaddr);
 478                         printk("%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
 479                                  dev->name, status, result, rx_ptr, running, boguscnt);
 480                 }
 481                 if ((status & 0x80) == 0)
 482                         break;
 483 
 484                 if ((status & 0x0F) == 4) {     /* Transmit done. */
 485                         struct net_local *lp = (struct net_local *)dev->priv;
 486                         int tx_status;
 487                         outb(CMD0_STAT1, ioaddr);
 488                         tx_status = inw(ioaddr);
 489                         /* It's undocumented, but tx_status seems to match the i82586. */
 490                         if (tx_status & 0x2000) {
 491                                 lp->stats.tx_packets++;
 492                                 lp->stats.collisions += tx_status & 0xf;
 493                         } else {
 494                                 if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
 495                                 if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
 496                                 if (!(tx_status & 0x0040)) lp->stats.tx_heartbeat_errors++;
 497                                 if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
 498                                 /* ...and the catch-all. */
 499                                 if (tx_status | 0x0760 != 0x0760)
 500                                   lp->stats.tx_errors++;
 501                         }
 502                         dev->tbusy = 0;
 503                         mark_bh(INET_BH);       /* Inform upper layers. */
 504                 }
 505 
 506                 if ((status & 0x40)
 507                         || (status & 0x0f) == 11) {
 508                         znet_rx(dev);
 509                 }
 510                 /* Clear the interrupts we've handled. */
 511                 outb(CMD0_ACK,ioaddr);
 512         } while (boguscnt--);
 513 
 514         dev->interrupt = 0;
 515         return;
 516 }
 517 
 518 static void znet_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 519 {
 520         struct net_local *lp = (struct net_local *)dev->priv;
 521         int ioaddr = dev->base_addr;
 522         int boguscount = 1;
 523         short next_frame_end_offset = 0;                /* Offset of next frame start. */
 524         short *cur_frame_end;
 525         short cur_frame_end_offset;
 526 
 527         outb(CMD0_STAT2, ioaddr);
 528         cur_frame_end_offset = inw(ioaddr);
 529 
 530         if (cur_frame_end_offset == zn.rx_cur - zn.rx_start) {
 531                 printk("%s: Interrupted, but nothing to receive, offset %03x.\n",
 532                            dev->name, cur_frame_end_offset);
 533                 return;
 534         }
 535 
 536         /* Use same method as the Crynwr driver: construct a forward list in
 537            the same area of the backwards links we now have.  This allows us to
 538            pass packets to the upper layers in the order they were received --
 539            important for fast-path sequential operations. */
 540          while (zn.rx_start + cur_frame_end_offset != zn.rx_cur
 541                         && ++boguscount < 5) {
 542                 unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
 543                 int count, status;
 544 
 545                 if (cur_frame_end_offset < 4) {
 546                         /* Oh no, we have a special case: the frame trailer wraps around
 547                            the end of the ring buffer.  We've saved space at the end of
 548                            the ring buffer for just this problem. */
 549                         memcpy(zn.rx_end, zn.rx_start, 8);
 550                         cur_frame_end_offset += (RX_BUF_SIZE/2);
 551                 }
 552                 cur_frame_end = zn.rx_start + cur_frame_end_offset - 4;
 553 
 554                 lo_status = *cur_frame_end++;
 555                 hi_status = *cur_frame_end++;
 556                 status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
 557                 lo_cnt = *cur_frame_end++;
 558                 hi_cnt = *cur_frame_end++;
 559                 count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
 560 
 561                 if (znet_debug > 5)
 562                   printk("Constructing trailer at location %03x, %04x %04x %04x %04x"
 563                                  " count %#x status %04x.\n",
 564                                  cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
 565                                  count, status);
 566                 cur_frame_end[-4] = status;
 567                 cur_frame_end[-3] = next_frame_end_offset;
 568                 cur_frame_end[-2] = count;
 569                 next_frame_end_offset = cur_frame_end_offset;
 570                 cur_frame_end_offset -= ((count + 1)>>1) + 3;
 571                 if (cur_frame_end_offset < 0)
 572                   cur_frame_end_offset += RX_BUF_SIZE/2;
 573         };
 574 
 575         /* Now step  forward through the list. */
 576         do {
 577                 ushort *this_rfp_ptr = zn.rx_start + next_frame_end_offset;
 578                 int status = this_rfp_ptr[-4];
 579                 int pkt_len = this_rfp_ptr[-2];
 580           
 581                 if (znet_debug > 5)
 582                   printk("Looking at trailer ending at %04x status %04x length %03x"
 583                                  " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
 584                                  this_rfp_ptr[-3]<<1);
 585                 /* Once again we must assume that the i82586 docs apply. */
 586                 if ( ! (status & 0x2000)) {                             /* There was an error. */
 587                         lp->stats.rx_errors++;
 588                         if (status & 0x0800) lp->stats.rx_crc_errors++;
 589                         if (status & 0x0400) lp->stats.rx_frame_errors++;
 590                         if (status & 0x0200) lp->stats.rx_over_errors++; /* Wrong. */
 591                         if (status & 0x0100) lp->stats.rx_fifo_errors++;
 592                         if (status & 0x0080) lp->stats.rx_length_errors++;
 593                 } else if (pkt_len > 1536) {
 594                         lp->stats.rx_length_errors++;
 595                 } else {
 596                         /* Malloc up new buffer. */
 597                         int sksize = sizeof(struct sk_buff) + pkt_len;
 598                         struct sk_buff *skb;
 599 
 600                         skb = alloc_skb(sksize, GFP_ATOMIC);
 601                         if (skb == NULL) {
 602                                 if (znet_debug)
 603                                   printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 604                                 lp->stats.rx_dropped++;
 605                                 break;
 606                         }
 607                         skb->mem_len = sksize;
 608                         skb->mem_addr = skb;
 609                         skb->len = pkt_len;
 610                         skb->dev = dev;
 611 
 612                         if (&zn.rx_cur[(pkt_len+1)>>1] > zn.rx_end) {
 613                                 int semi_cnt = (zn.rx_end - zn.rx_cur)<<1;
 614                                 memcpy((unsigned char *) (skb + 1), zn.rx_cur, semi_cnt);
 615                                 memcpy((unsigned char *) (skb + 1) + semi_cnt, zn.rx_start,
 616                                            pkt_len - semi_cnt);
 617                         } else {
 618                                 memcpy((unsigned char *) (skb + 1), zn.rx_cur, pkt_len);
 619                                 if (znet_debug > 6) {
 620                                         unsigned int *packet = (unsigned int *) (skb + 1);
 621                                         printk("Packet data is %08x %08x %08x %08x.\n", packet[0],
 622                                                    packet[1], packet[2], packet[3]);
 623                                 }
 624                   }
 625 
 626 #ifdef HAVE_NETIF_RX
 627                         netif_rx(skb);
 628 #else
 629                         skb->lock = 0;
 630                         if (dev_rint((unsigned char*)skb, pkt_len, IN_SKBUFF, dev) != 0) {
 631                                 kfree_s(skb, sksize);
 632                                 lp->stats.rx_dropped++;
 633                                 break;
 634                         }
 635 #endif
 636                         lp->stats.rx_packets++;
 637                 }
 638                 zn.rx_cur = this_rfp_ptr;
 639                 if (zn.rx_cur >= zn.rx_end)
 640                         zn.rx_cur -= RX_BUF_SIZE/2;
 641                 update_stop_hit(ioaddr, (zn.rx_cur - zn.rx_start)<<1);
 642                 next_frame_end_offset = this_rfp_ptr[-3];
 643                 if (next_frame_end_offset == 0)         /* Read all the frames? */
 644                         break;                  /* Done for now */
 645                 this_rfp_ptr = zn.rx_start + next_frame_end_offset;
 646         } while (--boguscount);
 647 
 648         /* If any worth-while packets have been received, dev_rint()
 649            has done a mark_bh(INET_BH) for us and will work on them
 650            when we get to the bottom-half routine. */
 651         return;
 652 }
 653 
 654 /* The inverse routine to znet_open(). */
 655 static int znet_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 656 {
 657         int ioaddr = dev->base_addr;
 658 
 659         dev->tbusy = 1;
 660         dev->start = 0;
 661 
 662         outb(CMD0_RESET, ioaddr);                       /* CMD0_RESET */
 663 
 664         disable_dma(zn.rx_dma);
 665         disable_dma(zn.tx_dma);
 666 
 667         free_irq(dev->irq);
 668 
 669         if (znet_debug > 1)
 670                 printk("%s: Shutting down ethercard.\n", dev->name);
 671         /* Turn off transceiver power. */
 672         outb(0x10, 0xe6);                                       /* Select LAN control register */
 673         outb(inb(0xe7) & ~0x84, 0xe7);          /* Turn on LAN power (bit 2). */
 674 
 675         return 0;
 676 }
 677 
 678 /* Get the current statistics.  This may be called with the card open or
 679    closed. */
 680 static struct enet_statistics *net_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 681 {
 682                 struct net_local *lp = (struct net_local *)dev->priv;
 683 
 684                 return &lp->stats;
 685 }
 686 
 687 #ifdef HAVE_MULTICAST
 688 /* Set or clear the multicast filter for this adaptor.
 689    num_addrs == -1      Promiscuous mode, receive all packets
 690    num_addrs == 0       Normal mode, clear multicast list
 691    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 692                         best-effort filtering.
 693    As a side effect this routine must also initialize the device parameters.
 694    This is taken advantage of in open().
 695 
 696    N.B. that we change i593_init[] in place.  This (properly) makes the
 697    mode change persistent, but must be changed if this code is moved to
 698    a multiple adaptor environment.
 699  */
 700 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 701 {
 702         short ioaddr = dev->base_addr;
 703 
 704         if (num_addrs < 0) {
 705                 /* Enable promiscuous mode */
 706                 i593_init[7] &= ~3;             i593_init[7] |= 1;
 707                 i593_init[13] &= ~8;    i593_init[13] |= 8;
 708         } else if (num_addrs > 0) {
 709                 /* Enable accept-all-multicast mode */
 710                 i593_init[7] &= ~3;             i593_init[7] |= 0;
 711                 i593_init[13] &= ~8;    i593_init[13] |= 8;
 712         } else {                                        /* Enable normal mode. */
 713                 i593_init[7] &= ~3;             i593_init[7] |= 0;
 714                 i593_init[13] &= ~8;    i593_init[13] |= 0;
 715         }
 716         *zn.tx_cur++ = sizeof(i593_init);
 717         memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
 718         zn.tx_cur += sizeof(i593_init)/2;
 719         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
 720 #ifdef not_tested
 721         if (num_addrs > 0) {
 722                 int addrs_len = 6*num_addrs;
 723                 *zn.tx_cur++ = addrs_len;
 724                 memcpy(zn.tx_cur, addrs, addrs_len);
 725                 outb(CMD0_MULTICAST_LIST+CMD0_CHNL_1, ioaddr);
 726                 zn.tx_cur += addrs_len>>1;
 727         }
 728 #endif
 729 }
 730 #endif
 731 
 732 void show_dma(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734         short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
 735         unsigned addr = inb(dma_port);
 736         addr |= inb(dma_port) << 8;
 737         printk("Addr: %04x cnt:%3x...", addr<<1,
 738                    get_dma_residue(zn.tx_dma));
 739 }
 740 
 741 /* Initialize the hardware.  We have to do this when the board is open()ed
 742    or when we come out of suspend mode. */
 743 static void hardware_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 744 {
 745         short ioaddr = dev->base_addr;
 746 
 747         zn.rx_cur = zn.rx_start;
 748         zn.tx_cur = zn.tx_start;
 749 
 750         /* Reset the chip, and start it up. */
 751         outb(CMD0_RESET, ioaddr);
 752 
 753         cli(); {                                                        /* Protect against a DMA flip-flop */
 754                 disable_dma(zn.rx_dma);                 /* reset by an interrupting task. */
 755                 clear_dma_ff(zn.rx_dma);
 756                 set_dma_mode(zn.rx_dma, DMA_RX_MODE);
 757                 set_dma_addr(zn.rx_dma, (unsigned int) zn.rx_start);
 758                 set_dma_count(zn.rx_dma, RX_BUF_SIZE);
 759                 enable_dma(zn.rx_dma);
 760                 /* Now set up the Tx channel. */
 761                 disable_dma(zn.tx_dma);
 762                 clear_dma_ff(zn.tx_dma);
 763                 set_dma_mode(zn.tx_dma, DMA_TX_MODE);
 764                 set_dma_addr(zn.tx_dma, (unsigned int) zn.tx_start);
 765                 set_dma_count(zn.tx_dma, zn.tx_buf_len<<1);
 766                 enable_dma(zn.tx_dma);
 767         } sti();
 768 
 769         if (znet_debug > 1)
 770           printk("%s: Initializing the i82593, tx buf %p... ", dev->name,
 771                          zn.tx_start);
 772         /* Do an empty configure command, just like the Crynwr driver.  This
 773            resets to chip to its default values. */
 774         *zn.tx_cur++ = 0;
 775         *zn.tx_cur++ = 0;
 776         printk("stat:%02x ", inb(ioaddr)); show_dma();
 777         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
 778         *zn.tx_cur++ = sizeof(i593_init);
 779         memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
 780         zn.tx_cur += sizeof(i593_init)/2;
 781         printk("stat:%02x ", inb(ioaddr)); show_dma();
 782         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
 783         *zn.tx_cur++ = 6;
 784         memcpy(zn.tx_cur, dev->dev_addr, 6);
 785         zn.tx_cur += 3;
 786         printk("stat:%02x ", inb(ioaddr)); show_dma();
 787         outb(CMD0_IA_SETUP + CMD0_CHNL_1, ioaddr);
 788         printk("stat:%02x ", inb(ioaddr)); show_dma();
 789 
 790         update_stop_hit(ioaddr, 8192);
 791         if (znet_debug > 1)  printk("enabling Rx.\n");
 792         outb(CMD0_Rx_ENABLE+CMD0_CHNL_0, ioaddr);
 793         dev->tbusy = 0;
 794 }
 795 
 796 #ifdef notdef
 797 foo()
     /* [previous][next][first][last][top][bottom][index][help] */
 798 {
 799         /*do_command(ioaddr, CMD0_CONFIGURE+CMD0_CHNL_1, sizeof(i593_init) + 2,
 800                            zn.tx_buffer);*/
 801         /*do_command(ioaddr, CMD0_CONFIGURE+CMD0_CHNL_1, 32, zn.tx_buffer);*/
 802         /*outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);*/
 803 
 804         if (znet_debug > 1)  printk("Set Address... ");
 805         *zn.tx_cur++ = 6;
 806         memcpy(zn.tx_cur, dev->dev_addr, 6);
 807         zn.tx_cur += 3;
 808         outb(CMD0_IA_SETUP + CMD0_CHNL_1, ioaddr);
 809         {
 810                 unsigned stop_time = jiffies + 3; 
 811                 while (jiffies < stop_time);
 812         }
 813         if (znet_debug > 2) {
 814                 short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
 815                 unsigned addr = inb(dma_port);
 816                 addr |= inb(dma_port) << 8;
 817                 printk("Terminal addr is %04x, cnt. %03x...", addr<<1,
 818                            get_dma_residue(zn.tx_dma));
 819         }
 820         *zn.tx_cur++ = 6;
 821         memcpy(zn.tx_cur, dev->dev_addr, 6);
 822         zn.tx_cur += 3;
 823         outb(CMD0_IA_SETUP + CMD0_CHNL_1, ioaddr);
 824         {
 825                 unsigned stop_time = jiffies + 2; 
 826                 while (jiffies < stop_time);
 827         }
 828         if (znet_debug > 2) {
 829                 short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
 830                 unsigned addr = inb(dma_port);
 831                 addr |= inb(dma_port) << 8;
 832                 printk("Terminal addr is %04x, cnt. %03x...", addr<<1,
 833                            get_dma_residue(zn.tx_dma));
 834         }
 835         wait_for_done(ioaddr);
 836 
 837         if (znet_debug > 1)  printk("Set Mode... ");
 838         set_multicast_list(dev, 0, 0);
 839         {
 840                 unsigned stop_time = jiffies + 3; 
 841                 while (jiffies < stop_time);
 842         }
 843         if (znet_debug > 2) {
 844                 short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
 845                 unsigned addr = inb(dma_port);
 846                 addr |= inb(dma_port) << 8;
 847                 printk("Terminal addr is %04x, cnt. %03x...", addr<<1,
 848                            get_dma_residue(zn.tx_dma));
 849         }
 850         if (znet_debug > 2) {
 851                 int i;
 852                 outb(CMD0_DUMP+CMD0_CHNL_0, ioaddr);
 853                 printk("Dumping state:");
 854                 for (i = 0; i < 16; i++)
 855                         printk(" %04x", *zn.rx_cur++);
 856                 printk("\n             :");
 857                 for (;i < 32; i++)
 858                         printk(" %04x", *zn.rx_cur++);
 859                 printk("\n");
 860                 wait_for_done(ioaddr);
 861         }
 862 }
 863 
 864 static int do_command(short ioaddr, int command, int length, ushort *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 865 {
 866         /* This isn't needed, but is here for safety. */
 867         outb(CMD0_NOP+CMD0_STAT3,ioaddr);
 868         if (inb(ioaddr) & 3)
 869           printk("znet: do_command() while the i82593 is busy.\n");
 870 
 871         cli();
 872         disable_dma(zn.tx_dma);
 873         clear_dma_ff(zn.tx_dma);
 874         set_dma_mode(zn.tx_dma,DMA_MODE_WRITE);
 875         set_dma_addr(zn.tx_dma,(unsigned int) zn.tx_start);
 876         set_dma_count(zn.tx_dma,length);
 877         sti();
 878         enable_dma(zn.tx_dma);
 879         outb(command, ioaddr);
 880         return 0;
 881 }
 882 
 883 /* wait_for_done - this is a blatent rip-off of the wait_for_done routine
 884  ** from the Crynwr packet driver.      It does not work correctly - doesn't
 885  ** acknowledge the interrupts it gets or something.  It does determine
 886  ** when the command is done, or if there are none executing, though...
 887  **             -Mike
 888  */
 889 static int wait_for_done(short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 890 {
 891   unsigned int stat;
 892   unsigned stop_time = jiffies + 10;
 893   int ticks = 0;
 894 
 895   /* check to see if we are busy */
 896   outb(CMD0_NOP+CMD0_STAT3,ioaddr);
 897   stat = inb(ioaddr);
 898 
 899   /* check if busy */
 900   if ((stat&3)==0) {
 901         if (znet_debug > 5)
 902           printk("wait_for_done(): Not busy, status %02x.\n", stat);
 903         return 0;
 904   }
 905 
 906   while (jiffies < stop_time) {
 907           /* now check */
 908           outb(CMD0_NOP+CMD0_STAT3,ioaddr);
 909           stat = inb(ioaddr);
 910           if ((stat&3)==0) {
 911                   if (znet_debug > 5)
 912                         printk("Command completed after %d ticks status %02x.\n",
 913                                    ticks, stat);
 914                   outb((CMD0_NOP|CMD0_ACK),ioaddr);
 915                   return 0;
 916           }
 917           ticks++;
 918   }
 919   outb(CMD0_ABORT, ioaddr);
 920   if (znet_debug)
 921         printk("wait_for_done: command not ACK'd, status %02x after abort %02x.\n",
 922                    stat, inb(ioaddr));
 923 
 924   /* should re-initialize here... */
 925   return 1;
 926 }
 927 #endif /* notdef */
 928 
 929 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
     /* [previous][next][first][last][top][bottom][index][help] */
 930 {
 931         outb(CMD0_PORT_1, ioaddr);
 932         if (znet_debug > 5)
 933           printk("Updating stop hit with value %02x.\n",
 934                          (rx_stop_offset >> 6) | 0x80);
 935         outb((rx_stop_offset >> 6) | 0x80, ioaddr);
 936         outb(CMD1_PORT_0, ioaddr);
 937 }
 938 
 939 /*
 940  * Local variables:
 941  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c znet.c"
 942  *  version-control: t
 943  *  kept-new-versions: 5
 944  *  c-indent-level: 4
 945  *  tab-width: 4
 946  * End:
 947  */

/* [previous][next][first][last][top][bottom][index][help] */