root/drivers/net/atp.c

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

DEFINITIONS

This source file includes following definitions.
  1. atp_init
  2. atp_probe1
  3. get_node_ID
  4. eeprom_op
  5. net_open
  6. hardware_init
  7. trigger_send
  8. write_packet
  9. net_send_packet
  10. net_interrupt
  11. net_rx
  12. read_block
  13. net_close
  14. net_get_stats
  15. set_multicast_list

   1 /* atp.c: Attached (pocket) ethernet adaptor driver for linux. */
   2 /*
   3         Written 1993 by Donald Becker.
   4         Copyright 1993 United States Government as represented by the Director,
   5         National Security Agency.  This software may only be used and distributed
   6         according to the terms of the GNU Public License as modified by SRC,
   7         incorporated herein by reference.
   8 
   9         The author may be reached as becker@super.org or
  10         C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  11 
  12 */
  13 
  14 static char *version =
  15         "atp.c:v0.04 2/25/94 Donald Becker (becker@super.org)\n";
  16 
  17 /*
  18         This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket
  19         ethernet adaptor.  This is a common low-cost OEM pocket ethernet
  20         adaptor, sold under many names.
  21 
  22   Sources:
  23         This driver was written from the packet driver assembly code provided by
  24         Vincent Bono of AT-Lan-Tec.      Ever try to figure out how a complicated
  25         device works just from the assembly code?  It ain't pretty.  The following
  26         description is written based on guesses and writing lots of special-purpose
  27         code to test my theorized operation.
  28 
  29                                         Theory of Operation
  30         
  31         The RTL8002 adaptor seems to be built around a custom spin of the SEEQ
  32         controller core.  It probably has a 16K or 64K internal packet buffer, of
  33         which the first 4K is devoted to transmit and the rest to receive.
  34         The controller maintains the queue of received packet and the packet buffer
  35         access pointer internally, with only 'reset to beginning' and 'skip to next
  36         packet' commands visible.  The transmit packet queue holds two (or more?)
  37         packets: both 'retransmit this packet' (due to collision) and 'transmit next
  38         packet' commands must be started by hand.
  39 
  40         The station address is stored in a standard bit-serial EEPROM which must be
  41         read (ughh) by the device driver.  (Provisions have been made for
  42         substituting a 74S288 PROM, but I haven't gotten reports of any models
  43         using it.)  Unlike built-in devices, a pocket adaptor can temporarily lose
  44         power without indication to the device driver.  The major effect is that
  45         the station address, receive filter (promiscuous, etc.) and transceiver
  46         must be reset.
  47 
  48         The controller itself has 16 registers, some of which use only the lower
  49         bits.  The registers are read and written 4 bits at a time.  The four bit
  50         register address is presented on the data lines along with a few additional
  51         timing and control bits.  The data is then read from status port or written
  52         to the data port.
  53 
  54         Since the bulk data transfer of the actual packets through the slow
  55         parallel port dominates the driver's running time, four distinct data
  56         (non-register) transfer modes are provided by the adaptor, two in each
  57         direction.  In the first mode timing for the nibble transfers is
  58         provided through the data port.  In the second mode the same timing is
  59         provided through the control port.  In either case the data is read from
  60         the status port and written to the data port, just as it is accessing
  61         registers.
  62 
  63         In addition to the basic data transfer methods, several more are modes are
  64         created by adding some delay by doing multiple reads of the data to allow
  65         it to stabilize.  This delay seems to be needed on most machines.
  66 
  67         The data transfer mode is stored in the 'dev->if_port' field.  Its default
  68         value is '4'.  It may be overridden at boot-time using the third parameter
  69         to the "ether=..." initialization.
  70 
  71         The header file <atp.h> provides inline functions that encapsulate the
  72         register and data access methods.  These functions are hand-tuned to
  73         generate reasonable object code.  This header file also documents my
  74         interpretations of the device registers.
  75 */
  76 
  77 #include <linux/kernel.h>
  78 #include <linux/sched.h>
  79 #include <linux/types.h>
  80 #include <linux/fcntl.h>
  81 #include <linux/interrupt.h>
  82 #include <linux/ptrace.h>
  83 #include <linux/ioport.h>
  84 #include <linux/in.h>
  85 #include <linux/malloc.h>
  86 #include <linux/string.h>
  87 #include <asm/system.h>
  88 #include <asm/bitops.h>
  89 #include <asm/io.h>
  90 #include <asm/dma.h>
  91 #include <linux/errno.h>
  92 
  93 #include <linux/netdevice.h>
  94 #include <linux/etherdevice.h>
  95 #include <linux/skbuff.h>
  96 
  97 #include "atp.h"
  98 
  99 /* Compatibility definitions for earlier kernel versions. */
 100 #ifndef HAVE_AUTOIRQ
 101 /* From auto_irq.c, in ioport.h for later versions. */
 102 extern void autoirq_setup(int waittime);
 103 extern int autoirq_report(int waittime);
 104 /* The map from IRQ number (as passed to the interrupt handler) to
 105    'struct device'. */
 106 extern struct device *irq2dev_map[16];
 107 #endif
 108 
 109 #ifndef HAVE_ALLOC_SKB
 110 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
 111 #define kfree_skbmem(addr, size) kfree_s(addr,size);
 112 #endif
 113 
 114 #ifndef HAVE_PORTRESERVE
 115 #define check_region(ioaddr, size)              0
 116 #define register_iomem(ioaddr, size,name);              do ; while (0)
 117 #endif
 118 
 119 /* use 0 for production, 1 for verification, >2 for debug */
 120 #ifndef NET_DEBUG
 121 #define NET_DEBUG 4
 122 #endif
 123 static unsigned int net_debug = NET_DEBUG;
 124 
 125 /* The number of low I/O ports used by the ethercard. */
 126 #define ETHERCARD_TOTAL_SIZE    3
 127 
 128 /* Index to functions, as function prototypes. */
 129 
 130 extern int atp_probe(struct device *dev);
 131 
 132 static int atp_probe1(struct device *dev, short ioaddr);
 133 static void get_node_ID(struct device *dev);
 134 static unsigned short eeprom_op(short ioaddr, unsigned int cmd);
 135 static int net_open(struct device *dev);
 136 static void hardware_init(struct device *dev);
 137 static void write_packet(short ioaddr, int length, unsigned char *packet, int mode);
 138 static void trigger_send(short ioaddr, int length);
 139 static int      net_send_packet(struct sk_buff *skb, struct device *dev);
 140 static void net_interrupt(int reg_ptr);
 141 static void net_rx(struct device *dev);
 142 static void read_block(short ioaddr, int length, unsigned char *buffer, int data_mode);
 143 static int net_close(struct device *dev);
 144 static struct enet_statistics *net_get_stats(struct device *dev);
 145 static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
 146 
 147 
 148 /* Check for a network adaptor of this type, and return '0' iff one exists.
 149    If dev->base_addr == 0, probe all likely locations.
 150    If dev->base_addr == 1, always return failure.
 151    If dev->base_addr == 2, allocate space for the device and return success
 152    (detachable devices only).
 153    */
 154 int
 155 atp_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         int *port, ports[] = {0x378, 0x278, 0x3bc, 0};
 158         int base_addr = dev->base_addr;
 159 
 160         if (base_addr > 0x1ff)          /* Check a single specified location. */
 161                 return atp_probe1(dev, base_addr);
 162         else if (base_addr == 1)        /* Don't probe at all. */
 163                 return ENXIO;
 164 
 165         for (port = ports; *port; port++) {
 166                 int ioaddr = *port;
 167                 outb(0x57, ioaddr + PAR_DATA);
 168                 if (inb(ioaddr + PAR_DATA) != 0x57)
 169                         continue;
 170                 if (atp_probe1(dev, ioaddr) == 0)
 171                         return 0;
 172         }
 173 
 174         return ENODEV;
 175 }
 176 
 177 static int atp_probe1(struct device *dev, short ioaddr)
     /* [previous][next][first][last][top][bottom][index][help] */
 178 {
 179         int saved_ctrl_reg, status;
 180 
 181         outb(0xff, ioaddr + PAR_DATA);
 182         /* Save the original value of the Control register, in case we guessed
 183            wrong. */
 184         saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
 185         /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
 186         outb(0x04, ioaddr + PAR_CONTROL);
 187         write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 188         eeprom_delay(2048);
 189         status = read_nibble(ioaddr, CMR1);
 190 
 191         if ((status & 0x78) != 0x08) {
 192                 /* The pocket adaptor probe failed, restore the control register. */
 193                 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
 194                 return 1;
 195         }
 196         status = read_nibble(ioaddr, CMR2_h);
 197         if ((status & 0x78) != 0x10) {
 198                 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
 199                 return 1;
 200         }
 201         /* Find the IRQ used by triggering an interrupt. */
 202         write_reg_byte(ioaddr, CMR2, 0x01);                     /* No accept mode, IRQ out. */
 203         write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);  /* Enable Tx and Rx. */
 204 
 205         /* Omit autoIRQ routine for now. Use "table lookup" instead.  Uhgggh. */
 206         if (ioaddr == 0x378)
 207                 dev->irq = 7;
 208         else
 209                 dev->irq = 5;
 210         write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
 211         write_reg(ioaddr, CMR2, CMR2_NULL);
 212 
 213         dev->base_addr = ioaddr;
 214 
 215         /* Read the station address PROM.  */
 216         get_node_ID(dev);
 217 
 218         printk("%s: Pocket adaptor found at %#3x, IRQ %d, SAPROM "
 219                    "%02X:%02X:%02X:%02X:%02X:%02X.\n", dev->name, dev->base_addr,
 220                    dev->irq, dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
 221                    dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
 222 
 223         /* Leave the hardware in a reset state. */
 224     write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 225 
 226         if (net_debug)
 227                 printk(version);
 228 
 229         /* Initialize the device structure. */
 230         ether_setup(dev);
 231         dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
 232         memset(dev->priv, 0, sizeof(struct net_local));
 233 
 234 
 235         {
 236                 struct net_local *lp = (struct net_local *)dev->priv;
 237                 lp->addr_mode = CMR2h_Normal;
 238         }
 239 
 240         /* For the ATP adaptor the "if_port" is really the data transfer mode. */
 241         dev->if_port = (dev->mem_start & 0xf) ? dev->mem_start & 0x7 : 4;
 242         if (dev->mem_end & 0xf)
 243                 net_debug = dev->mem_end & 7;
 244 
 245         dev->open               = net_open;
 246         dev->stop               = net_close;
 247         dev->hard_start_xmit = net_send_packet;
 248         dev->get_stats  = net_get_stats;
 249         dev->set_multicast_list = &set_multicast_list;
 250 
 251         return 0;
 252 }
 253 
 254 /* Read the station address PROM, usually a word-wide EEPROM. */
 255 static void get_node_ID(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 256 {
 257         short ioaddr = dev->base_addr;
 258         int sa_offset = 0;
 259         int i;
 260         
 261         write_reg(ioaddr, CMR2, CMR2_EEPROM);     /* Point to the EEPROM control registers. */
 262         
 263         /* Some adaptors have the station address at offset 15 instead of offset
 264            zero.  Check for it, and fix it if needed. */
 265         if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
 266                 sa_offset = 15;
 267         
 268         for (i = 0; i < 3; i++)
 269                 ((unsigned short *)dev->dev_addr)[i] =
 270                         ntohs(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
 271         
 272         write_reg(ioaddr, CMR2, CMR2_NULL);
 273 }
 274 
 275 /*
 276   An EEPROM read command starts by shifting out 0x60+address, and then
 277   shifting in the serial data. See the NatSemi databook for details.
 278  *                 ________________
 279  * CS : __|
 280  *                         ___     ___
 281  * CLK: ______|   |___|   |
 282  *               __ _______ _______
 283  * DI :  __X_______X_______X
 284  * DO :  _________X_______X
 285  */
 286 
 287 static unsigned short eeprom_op(short ioaddr, unsigned int cmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 288 {
 289         unsigned eedata_out = 0;
 290         int num_bits = EE_CMD_SIZE;
 291         
 292         while (--num_bits >= 0) {
 293                 char outval = test_bit(num_bits, &cmd) ? EE_DATA_WRITE : 0;
 294                 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
 295                 eeprom_delay(5);
 296                 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
 297                 eedata_out <<= 1;
 298                 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
 299                         eedata_out++;
 300                 eeprom_delay(5);
 301         }
 302         write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
 303         return eedata_out;
 304 }
 305 
 306 
 307 /* Open/initialize the board.  This is called (in the current kernel)
 308    sometime after booting when the 'ifconfig' program is run.
 309 
 310    This routine sets everything up anew at each open, even
 311    registers that "should" only need to be set once at boot, so that
 312    there is non-reboot way to recover if something goes wrong.
 313 
 314    This is an attachable device: if there is no dev->priv entry then it wasn't
 315    probed for at boot-time, and we need to probe for it again.
 316    */
 317 static int net_open(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 318 {
 319 
 320         /* The interrupt line is turned off (tri-stated) when the device isn't in
 321            use.  That's especially important for "attached" interfaces where the
 322            port or interrupt may be shared. */
 323         if (irq2dev_map[dev->irq] != 0
 324                 || (irq2dev_map[dev->irq] = dev) == 0
 325                 || request_irq(dev->irq, &net_interrupt, 0, "atp")) {
 326                 return -EAGAIN;
 327         }
 328 
 329         hardware_init(dev);
 330         dev->start = 1;
 331         return 0;
 332 }
 333 
 334 /* This routine resets the hardware.  We initialize everything, assuming that
 335    the hardware may have been temporarily detached. */
 336 static void hardware_init(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         struct net_local *lp = (struct net_local *)dev->priv;
 339         int ioaddr = dev->base_addr;
 340     int i;
 341 
 342         write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 343         
 344     for (i = 0; i < 6; i++)
 345                 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
 346 
 347         write_reg_high(ioaddr, CMR2, lp->addr_mode);
 348 
 349         if (net_debug > 2) {
 350                 printk("%s: Reset: current Rx mode %d.\n", dev->name,
 351                            (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
 352         }
 353 
 354     write_reg(ioaddr, CMR2, CMR2_IRQOUT);
 355     write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
 356 
 357         /* Enable the interrupt line from the serial port. */
 358         outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 359 
 360         /* Unmask the interesting interrupts. */
 361     write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 362     write_reg_high(ioaddr, IMR, ISRh_RxErr);
 363 
 364         lp->tx_unit_busy = 0;
 365     lp->pac_cnt_in_tx_buf = 0;
 366         lp->saved_tx_size = 0;
 367 
 368         dev->tbusy = 0;
 369         dev->interrupt = 0;
 370 }
 371 
 372 static void trigger_send(short ioaddr, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
 373 {
 374         write_reg_byte(ioaddr, TxCNT0, length & 0xff);
 375         write_reg(ioaddr, TxCNT1, length >> 8);
 376         write_reg(ioaddr, CMR1, CMR1_Xmit);
 377 }
 378 
 379 static void write_packet(short ioaddr, int length, unsigned char *packet, int data_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 380 {
 381     length = (length + 1) & ~1;         /* Round up to word length. */
 382     outb(EOC+MAR, ioaddr + PAR_DATA);
 383     if ((data_mode & 1) == 0) {
 384                 /* Write the packet out, starting with the write addr. */
 385                 outb(WrAddr+MAR, ioaddr + PAR_DATA);
 386                 do {
 387                         write_byte_mode0(ioaddr, *packet++);
 388                 } while (--length > 0) ;
 389     } else {
 390                 /* Write the packet out in slow mode. */
 391                 unsigned char outbyte = *packet++;
 392 
 393                 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 394                 outb(WrAddr+MAR, ioaddr + PAR_DATA);
 395 
 396                 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
 397                 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
 398                 outbyte >>= 4;
 399                 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
 400                 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 401                 while (--length > 0)
 402                         write_byte_mode1(ioaddr, *packet++);
 403     }
 404     /* Terminate the Tx frame.  End of write: ECB. */
 405     outb(0xff, ioaddr + PAR_DATA);
 406     outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 407 }
 408 
 409 static int
 410 net_send_packet(struct sk_buff *skb, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 411 {
 412         struct net_local *lp = (struct net_local *)dev->priv;
 413         int ioaddr = dev->base_addr;
 414 
 415         if (dev->tbusy) {
 416                 /* If we get here, some higher level has decided we are broken.
 417                    There should really be a "kick me" function call instead. */
 418                 int tickssofar = jiffies - dev->trans_start;
 419                 if (tickssofar < 5)
 420                         return 1;
 421                 printk("%s: transmit timed out, %s?\n", dev->name,
 422                            inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
 423                            :  "IRQ conflict");
 424                 lp->stats.tx_errors++;
 425                 /* Try to restart the adaptor. */
 426                 hardware_init(dev);
 427                 dev->tbusy=0;
 428                 dev->trans_start = jiffies;
 429         }
 430 
 431         /* If some higher layer thinks we've missed an tx-done interrupt
 432            we are passed NULL. Caution: dev_tint() handles the cli()/sti()
 433            itself. */
 434         if (skb == NULL) {
 435                 dev_tint(dev);
 436                 return 0;
 437         }
 438 
 439         /* Block a timer-based transmit from overlapping.  This could better be
 440            done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
 441         if (set_bit(0, (void*)&dev->tbusy) != 0)
 442                 printk("%s: Transmitter access conflict.\n", dev->name);
 443         else {
 444                 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
 445                 unsigned char *buf = skb->data;
 446                 int flags;
 447 
 448                 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
 449                    This sequence must not be interrupted by an incoming packet. */
 450                 save_flags(flags);
 451                 cli();
 452                 write_reg(ioaddr, IMR, 0);
 453                 write_reg_high(ioaddr, IMR, 0);
 454                 restore_flags(flags);
 455 
 456                 write_packet(ioaddr, length, buf, dev->if_port);
 457 
 458                 lp->pac_cnt_in_tx_buf++;
 459                 if (lp->tx_unit_busy == 0) {
 460                         trigger_send(ioaddr, length);
 461                         lp->saved_tx_size = 0;                          /* Redundant */
 462                         lp->re_tx = 0;
 463                         lp->tx_unit_busy = 1;
 464                 } else
 465                         lp->saved_tx_size = length;
 466 
 467                 dev->trans_start = jiffies;
 468                 /* Re-enable the LPT interrupts. */
 469                 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 470                 write_reg_high(ioaddr, IMR, ISRh_RxErr);
 471         }
 472 
 473         dev_kfree_skb (skb, FREE_WRITE);
 474 
 475         return 0;
 476 }
 477 
 478 /* The typical workload of the driver:
 479    Handle the network interface interrupts. */
 480 static void
 481 net_interrupt(int reg_ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 482 {
 483         int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
 484         struct device *dev = (struct device *)(irq2dev_map[irq]);
 485         struct net_local *lp;
 486         int ioaddr, status, boguscount = 20;
 487         static int num_tx_since_rx = 0;
 488 
 489         if (dev == NULL) {
 490                 printk ("ATP_interrupt(): irq %d for unknown device.\n", irq);
 491                 return;
 492         }
 493         dev->interrupt = 1;
 494 
 495         ioaddr = dev->base_addr;
 496         lp = (struct net_local *)dev->priv;
 497 
 498         /* Disable additional spurious interrupts. */
 499         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
 500 
 501         /* The adaptor's output is currently the IRQ line, switch it to data. */
 502         write_reg(ioaddr, CMR2, CMR2_NULL);
 503         write_reg(ioaddr, IMR, 0);
 504 
 505         if (net_debug > 5) printk("%s: In interrupt ", dev->name);
 506     while (--boguscount > 0) {
 507                 status = read_nibble(ioaddr, ISR);
 508                 if (net_debug > 5) printk("loop status %02x..", status);
 509 
 510                 if (status & (ISR_RxOK<<3)) {
 511                         write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
 512                         do {
 513                                 int read_status = read_nibble(ioaddr, CMR1);
 514                                 if (net_debug > 6)
 515                                         printk("handling Rx packet %02x..", read_status);
 516                                 /* We acknowledged the normal Rx interrupt, so if the interrupt
 517                                    is still outstanding we must have a Rx error. */
 518                                 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
 519                                         lp->stats.rx_over_errors++;
 520                                         /* Set to no-accept mode long enough to remove a packet. */
 521                                         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
 522                                         net_rx(dev);
 523                                         /* Clear the interrupt and return to normal Rx mode. */
 524                                         write_reg_high(ioaddr, ISR, ISRh_RxErr);
 525                                         write_reg_high(ioaddr, CMR2, lp->addr_mode);
 526                                 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
 527                                         net_rx(dev);
 528                                         dev->last_rx = jiffies;
 529                                         num_tx_since_rx = 0;
 530                                 } else
 531                                         break;
 532                         } while (--boguscount > 0);
 533                 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
 534                         if (net_debug > 6)  printk("handling Tx done..");
 535                         /* Clear the Tx interrupt.  We should check for too many failures
 536                            and reinitialize the adaptor. */
 537                         write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
 538                         if (status & (ISR_TxErr<<3)) {
 539                                 lp->stats.collisions++;
 540                                 if (++lp->re_tx > 15) {
 541                                         lp->stats.tx_aborted_errors++;
 542                                         hardware_init(dev);
 543                                         break;
 544                                 }
 545                                 /* Attempt to retransmit. */
 546                                 if (net_debug > 6)  printk("attempting to ReTx");
 547                                 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
 548                         } else {
 549                                 /* Finish up the transmit. */
 550                                 lp->stats.tx_packets++;
 551                                 lp->pac_cnt_in_tx_buf--;
 552                                 if ( lp->saved_tx_size) {
 553                                         trigger_send(ioaddr, lp->saved_tx_size);
 554                                         lp->saved_tx_size = 0;
 555                                         lp->re_tx = 0;
 556                                 } else
 557                                         lp->tx_unit_busy = 0;
 558                                 dev->tbusy = 0;
 559                                 mark_bh(NET_BH);        /* Inform upper layers. */
 560                         }
 561                         num_tx_since_rx++;
 562                 } else if (num_tx_since_rx > 8
 563                                    && jiffies > dev->last_rx + 100) {
 564                         if (net_debug > 2)
 565                                 printk("%s: Missed packet? No Rx after %d Tx and %ld jiffies"
 566                                            " status %02x  CMR1 %02x.\n", dev->name,
 567                                            num_tx_since_rx, jiffies - dev->last_rx, status,
 568                                            (read_nibble(ioaddr, CMR1) >> 3) & 15);
 569                         lp->stats.rx_missed_errors++;
 570                         hardware_init(dev);
 571                         num_tx_since_rx = 0;
 572                         break;
 573                 } else
 574                         break;
 575     }
 576 
 577         /* This following code fixes a rare (and very difficult to track down)
 578            problem where the adaptor forgets its ethernet address. */
 579         {
 580                 int i;
 581                 for (i = 0; i < 6; i++)
 582                         write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
 583         }
 584 
 585         /* Tell the adaptor that it can go back to using the output line as IRQ. */
 586     write_reg(ioaddr, CMR2, CMR2_IRQOUT);
 587         /* Enable the physical interrupt line, which is sure to be low until.. */
 588         outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
 589         /* .. we enable the interrupt sources. */
 590         write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
 591         write_reg_high(ioaddr, IMR, ISRh_RxErr);                        /* Hmmm, really needed? */
 592 
 593         if (net_debug > 5) printk("exiting interrupt.\n");
 594 
 595         dev->interrupt = 0;
 596 
 597         return;
 598 }
 599 
 600 /* We have a good packet(s), get it/them out of the buffers. */
 601 static void net_rx(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 602 {
 603         struct net_local *lp = (struct net_local *)dev->priv;
 604         int ioaddr = dev->base_addr;
 605 #ifdef notdef
 606         ushort header[4];
 607 #else
 608         struct rx_header rx_head;
 609 #endif
 610 
 611         /* Process the received packet. */
 612         outb(EOC+MAR, ioaddr + PAR_DATA);
 613         read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
 614         if (net_debug > 5)
 615                 printk(" rx_count %04x %04x %04x %04x..", rx_head.pad,
 616                            rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
 617         if ((rx_head.rx_status & 0x77) != 0x01) {
 618                 lp->stats.rx_errors++;
 619                 /* Ackkk!  I don't have any documentation on what the error bits mean!
 620                    The best I can do is slap the device around a bit. */
 621                 if (net_debug > 3) printk("%s: Unknown ATP Rx error %04x.\n",
 622                                                                   dev->name, rx_head.rx_status);
 623                 hardware_init(dev);
 624                 return;
 625         } else {
 626                 /* Malloc up new buffer. */
 627                 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;           /* The "-4" is omits the FCS (CRC). */
 628                 struct sk_buff *skb;
 629                 
 630                 skb = alloc_skb(pkt_len, GFP_ATOMIC);
 631                 if (skb == NULL) {
 632                         printk("%s: Memory squeeze, dropping packet.\n", dev->name);
 633                         lp->stats.rx_dropped++;
 634                         goto done;
 635                 }
 636                 skb->len = pkt_len;
 637                 skb->dev = dev;
 638                 
 639                 read_block(ioaddr, pkt_len, skb->data, dev->if_port);
 640 
 641                 if (net_debug > 6) {
 642                         unsigned char *data = skb->data;
 643                         printk(" data %02x%02x%02x %02x%02x%02x %02x%02x%02x"
 644                                    "%02x%02x%02x %02x%02x..",
 645                                    data[0], data[1], data[2], data[3], data[4], data[5],
 646                                    data[6], data[7], data[8], data[9], data[10], data[11],
 647                                    data[12], data[13]);
 648                 }
 649                 
 650                 netif_rx(skb);
 651                 lp->stats.rx_packets++;
 652         }
 653  done:
 654         write_reg(ioaddr, CMR1, CMR1_NextPkt);
 655         return;
 656 }
 657 
 658 static void read_block(short ioaddr, int length, unsigned char *p, int data_mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 659 {
 660 
 661         if (data_mode <= 3) { /* Mode 0 or 1 */
 662                 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
 663                 outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
 664                          ioaddr + PAR_DATA);
 665                 if (data_mode <= 1) { /* Mode 0 or 1 */
 666                         do  *p++ = read_byte_mode0(ioaddr);  while (--length > 0);
 667                 } else  /* Mode 2 or 3 */
 668                         do  *p++ = read_byte_mode2(ioaddr);  while (--length > 0);
 669         } else if (data_mode <= 5)
 670                 do      *p++ = read_byte_mode4(ioaddr);  while (--length > 0);
 671         else
 672                 do      *p++ = read_byte_mode6(ioaddr);  while (--length > 0);
 673 
 674     outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
 675         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
 676 }
 677 
 678 /* The inverse routine to net_open(). */
 679 static int
 680 net_close(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 681 {
 682         struct net_local *lp = (struct net_local *)dev->priv;
 683         int ioaddr = dev->base_addr;
 684 
 685         dev->tbusy = 1;
 686         dev->start = 0;
 687 
 688         /* Flush the Tx and disable Rx here. */
 689         lp->addr_mode = CMR2h_OFF;
 690         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
 691 
 692         /* Free the IRQ line. */
 693         outb(0x00, ioaddr + PAR_CONTROL);
 694         free_irq(dev->irq);
 695         irq2dev_map[dev->irq] = 0;
 696 
 697         /* Leave the hardware in a reset state. */
 698     write_reg_high(ioaddr, CMR1, CMR1h_RESET);
 699 
 700         return 0;
 701 }
 702 
 703 /* Get the current statistics.  This may be called with the card open or
 704    closed. */
 705 static struct enet_statistics *
 706 net_get_stats(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 707 {
 708         struct net_local *lp = (struct net_local *)dev->priv;
 709         return &lp->stats;
 710 }
 711 
 712 /* Set or clear the multicast filter for this adaptor.
 713    num_addrs == -1      Promiscuous mode, receive all packets
 714    num_addrs == 0       Normal mode, clear multicast list
 715    num_addrs > 0        Multicast mode, receive normal and MC packets, and do
 716                         best-effort filtering.
 717  */
 718 static void
 719 set_multicast_list(struct device *dev, int num_addrs, void *addrs)
     /* [previous][next][first][last][top][bottom][index][help] */
 720 {
 721         struct net_local *lp = (struct net_local *)dev->priv;
 722         short ioaddr = dev->base_addr;
 723         lp->addr_mode = num_addrs ? CMR2h_PROMISC : CMR2h_Normal;
 724         write_reg_high(ioaddr, CMR2, lp->addr_mode);
 725 }
 726 
 727 /*
 728  * Local variables:
 729  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c atp.c"
 730  *  version-control: t
 731  *  kept-new-versions: 5
 732  *  tab-width: 4
 733  * End:
 734  */

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