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. atp_timed_checker
  12. net_rx
  13. read_block
  14. net_close
  15. net_get_stats
  16. set_multicast_list

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

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