1/* atp.c: Attached (pocket) ethernet adapter driver for linux. */ 2/* 3 This is a driver for commonly OEM pocket (parallel port) 4 ethernet adapters based on the Realtek RTL8002 and RTL8012 chips. 5 6 Written 1993-2000 by Donald Becker. 7 8 This software may be used and distributed according to the terms of 9 the GNU General Public License (GPL), incorporated herein by reference. 10 Drivers based on or derived from this code fall under the GPL and must 11 retain the authorship, copyright and license notice. This file is not 12 a complete program and may only be used when the entire operating 13 system is licensed under the GPL. 14 15 Copyright 1993 United States Government as represented by the Director, 16 National Security Agency. Copyright 1994-2000 retained by the original 17 author, Donald Becker. The timer-based reset code was supplied in 1995 18 by Bill Carlson, wwc@super.org. 19 20 The author may be reached as becker@scyld.com, or C/O 21 Scyld Computing Corporation 22 410 Severn Ave., Suite 210 23 Annapolis MD 21403 24 25 Support information and updates available at 26 http://www.scyld.com/network/atp.html 27 28 29 Modular support/softnet added by Alan Cox. 30 _bit abuse fixed up by Alan Cox 31 32*/ 33 34static const char version[] = 35"atp.c:v1.09=ac 2002/10/01 Donald Becker <becker@scyld.com>\n"; 36 37/* The user-configurable values. 38 These may be modified when a driver module is loaded.*/ 39 40static int debug = 1; /* 1 normal messages, 0 quiet .. 7 verbose. */ 41#define net_debug debug 42 43/* Maximum events (Rx packets, etc.) to handle at each interrupt. */ 44static int max_interrupt_work = 15; 45 46#define NUM_UNITS 2 47/* The standard set of ISA module parameters. */ 48static int io[NUM_UNITS]; 49static int irq[NUM_UNITS]; 50static int xcvr[NUM_UNITS]; /* The data transfer mode. */ 51 52/* Operational parameters that are set at compile time. */ 53 54/* Time in jiffies before concluding the transmitter is hung. */ 55#define TX_TIMEOUT (400*HZ/1000) 56 57/* 58 This file is a device driver for the RealTek (aka AT-Lan-Tec) pocket 59 ethernet adapter. This is a common low-cost OEM pocket ethernet 60 adapter, sold under many names. 61 62 Sources: 63 This driver was written from the packet driver assembly code provided by 64 Vincent Bono of AT-Lan-Tec. Ever try to figure out how a complicated 65 device works just from the assembly code? It ain't pretty. The following 66 description is written based on guesses and writing lots of special-purpose 67 code to test my theorized operation. 68 69 In 1997 Realtek made available the documentation for the second generation 70 RTL8012 chip, which has lead to several driver improvements. 71 http://www.realtek.com.tw/cn/cn.html 72 73 Theory of Operation 74 75 The RTL8002 adapter seems to be built around a custom spin of the SEEQ 76 controller core. It probably has a 16K or 64K internal packet buffer, of 77 which the first 4K is devoted to transmit and the rest to receive. 78 The controller maintains the queue of received packet and the packet buffer 79 access pointer internally, with only 'reset to beginning' and 'skip to next 80 packet' commands visible. The transmit packet queue holds two (or more?) 81 packets: both 'retransmit this packet' (due to collision) and 'transmit next 82 packet' commands must be started by hand. 83 84 The station address is stored in a standard bit-serial EEPROM which must be 85 read (ughh) by the device driver. (Provisions have been made for 86 substituting a 74S288 PROM, but I haven't gotten reports of any models 87 using it.) Unlike built-in devices, a pocket adapter can temporarily lose 88 power without indication to the device driver. The major effect is that 89 the station address, receive filter (promiscuous, etc.) and transceiver 90 must be reset. 91 92 The controller itself has 16 registers, some of which use only the lower 93 bits. The registers are read and written 4 bits at a time. The four bit 94 register address is presented on the data lines along with a few additional 95 timing and control bits. The data is then read from status port or written 96 to the data port. 97 98 Correction: the controller has two banks of 16 registers. The second 99 bank contains only the multicast filter table (now used) and the EEPROM 100 access registers. 101 102 Since the bulk data transfer of the actual packets through the slow 103 parallel port dominates the driver's running time, four distinct data 104 (non-register) transfer modes are provided by the adapter, two in each 105 direction. In the first mode timing for the nibble transfers is 106 provided through the data port. In the second mode the same timing is 107 provided through the control port. In either case the data is read from 108 the status port and written to the data port, just as it is accessing 109 registers. 110 111 In addition to the basic data transfer methods, several more are modes are 112 created by adding some delay by doing multiple reads of the data to allow 113 it to stabilize. This delay seems to be needed on most machines. 114 115 The data transfer mode is stored in the 'dev->if_port' field. Its default 116 value is '4'. It may be overridden at boot-time using the third parameter 117 to the "ether=..." initialization. 118 119 The header file <atp.h> provides inline functions that encapsulate the 120 register and data access methods. These functions are hand-tuned to 121 generate reasonable object code. This header file also documents my 122 interpretations of the device registers. 123*/ 124 125#include <linux/kernel.h> 126#include <linux/module.h> 127#include <linux/types.h> 128#include <linux/fcntl.h> 129#include <linux/interrupt.h> 130#include <linux/ioport.h> 131#include <linux/in.h> 132#include <linux/string.h> 133#include <linux/errno.h> 134#include <linux/init.h> 135#include <linux/crc32.h> 136#include <linux/netdevice.h> 137#include <linux/etherdevice.h> 138#include <linux/skbuff.h> 139#include <linux/spinlock.h> 140#include <linux/delay.h> 141#include <linux/bitops.h> 142 143#include <asm/system.h> 144#include <asm/io.h> 145#include <asm/dma.h> 146 147#include "atp.h" 148 149MODULE_AUTHOR("Donald Becker <becker@scyld.com>"); 150MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver"); 151MODULE_LICENSE("GPL"); 152 153module_param(max_interrupt_work, int, 0); 154module_param(debug, int, 0); 155module_param_array(io, int, NULL, 0); 156module_param_array(irq, int, NULL, 0); 157module_param_array(xcvr, int, NULL, 0); 158MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt"); 159MODULE_PARM_DESC(debug, "ATP debug level (0-7)"); 160MODULE_PARM_DESC(io, "ATP I/O base address(es)"); 161MODULE_PARM_DESC(irq, "ATP IRQ number(s)"); 162MODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)"); 163 164/* The number of low I/O ports used by the ethercard. */ 165#define ETHERCARD_TOTAL_SIZE 3 166 167/* Sequence to switch an 8012 from printer mux to ethernet mode. */ 168static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,}; 169 170struct net_local { 171 spinlock_t lock; 172 struct net_device *next_module; 173 struct timer_list timer; /* Media selection timer. */ 174 long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */ 175 int saved_tx_size; 176 unsigned int tx_unit_busy:1; 177 unsigned char re_tx, /* Number of packet retransmissions. */ 178 addr_mode, /* Current Rx filter e.g. promiscuous, etc. */ 179 pac_cnt_in_tx_buf, 180 chip_type; 181}; 182 183/* This code, written by wwc@super.org, resets the adapter every 184 TIMED_CHECKER ticks. This recovers from an unknown error which 185 hangs the device. */ 186#define TIMED_CHECKER (HZ/4) 187#ifdef TIMED_CHECKER 188#include <linux/timer.h> 189static void atp_timed_checker(unsigned long ignored); 190#endif 191 192/* Index to functions, as function prototypes. */ 193 194static int atp_probe1(long ioaddr); 195static void get_node_ID(struct net_device *dev); 196static unsigned short eeprom_op(long ioaddr, unsigned int cmd); 197static int net_open(struct net_device *dev); 198static void hardware_init(struct net_device *dev); 199static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode); 200static void trigger_send(long ioaddr, int length); 201static netdev_tx_t atp_send_packet(struct sk_buff *skb, 202 struct net_device *dev); 203static irqreturn_t atp_interrupt(int irq, void *dev_id); 204static void net_rx(struct net_device *dev); 205static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode); 206static int net_close(struct net_device *dev); 207static void set_rx_mode(struct net_device *dev); 208static void tx_timeout(struct net_device *dev); 209 210 211/* A list of all installed ATP devices, for removing the driver module. */ 212static struct net_device *root_atp_dev; 213 214static int __init atp_init(void) 215{ 216 int *port, ports[] = {0x378, 0x278, 0x3bc, 0}; 217 int base_addr = io[0]; 218 219 if (base_addr > 0x1ff) /* Check a single specified location. */ 220 return atp_probe1(base_addr); 221 else if (base_addr == 1) /* Don't probe at all. */ 222 return -ENXIO; 223 224 for (port = ports; *port; port++) { 225 long ioaddr = *port; 226 outb(0x57, ioaddr + PAR_DATA); 227 if (inb(ioaddr + PAR_DATA) != 0x57) 228 continue; 229 if (atp_probe1(ioaddr) == 0) 230 return 0; 231 } 232 233 return -ENODEV; 234} 235 236static const struct net_device_ops atp_netdev_ops = { 237 .ndo_open = net_open, 238 .ndo_stop = net_close, 239 .ndo_start_xmit = atp_send_packet, 240 .ndo_set_multicast_list = set_rx_mode, 241 .ndo_tx_timeout = tx_timeout, 242 .ndo_change_mtu = eth_change_mtu, 243 .ndo_set_mac_address = eth_mac_addr, 244 .ndo_validate_addr = eth_validate_addr, 245}; 246 247static int __init atp_probe1(long ioaddr) 248{ 249 struct net_device *dev = NULL; 250 struct net_local *lp; 251 int saved_ctrl_reg, status, i; 252 int res; 253 254 outb(0xff, ioaddr + PAR_DATA); 255 /* Save the original value of the Control register, in case we guessed 256 wrong. */ 257 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL); 258 if (net_debug > 3) 259 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg); 260 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */ 261 outb(0x04, ioaddr + PAR_CONTROL); 262#ifndef final_version 263 if (net_debug > 3) { 264 /* Turn off the printer multiplexer on the 8012. */ 265 for (i = 0; i < 8; i++) 266 outb(mux_8012[i], ioaddr + PAR_DATA); 267 write_reg(ioaddr, MODSEL, 0x00); 268 printk("atp: Registers are "); 269 for (i = 0; i < 32; i++) 270 printk(" %2.2x", read_nibble(ioaddr, i)); 271 printk(".\n"); 272 } 273#endif 274 /* Turn off the printer multiplexer on the 8012. */ 275 for (i = 0; i < 8; i++) 276 outb(mux_8012[i], ioaddr + PAR_DATA); 277 write_reg_high(ioaddr, CMR1, CMR1h_RESET); 278 /* udelay() here? */ 279 status = read_nibble(ioaddr, CMR1); 280 281 if (net_debug > 3) { 282 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status); 283 for (i = 0; i < 32; i++) 284 printk(" %2.2x", read_nibble(ioaddr, i)); 285 printk("\n"); 286 } 287 288 if ((status & 0x78) != 0x08) { 289 /* The pocket adapter probe failed, restore the control register. */ 290 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL); 291 return -ENODEV; 292 } 293 status = read_nibble(ioaddr, CMR2_h); 294 if ((status & 0x78) != 0x10) { 295 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL); 296 return -ENODEV; 297 } 298 299 dev = alloc_etherdev(sizeof(struct net_local)); 300 if (!dev) 301 return -ENOMEM; 302 303 /* Find the IRQ used by triggering an interrupt. */ 304 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */ 305 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */ 306 307 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */ 308 if (irq[0]) 309 dev->irq = irq[0]; 310 else if (ioaddr == 0x378) 311 dev->irq = 7; 312 else 313 dev->irq = 5; 314 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */ 315 write_reg(ioaddr, CMR2, CMR2_NULL); 316 317 dev->base_addr = ioaddr; 318 319 /* Read the station address PROM. */ 320 get_node_ID(dev); 321 322#ifndef MODULE 323 if (net_debug) 324 printk(KERN_INFO "%s", version); 325#endif 326 327 printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, " 328 "SAPROM %pM.\n", 329 dev->name, dev->base_addr, dev->irq, dev->dev_addr); 330 331 /* Reset the ethernet hardware and activate the printer pass-through. */ 332 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX); 333 334 lp = netdev_priv(dev); 335 lp->chip_type = RTL8002; 336 lp->addr_mode = CMR2h_Normal; 337 spin_lock_init(&lp->lock); 338 339 /* For the ATP adapter the "if_port" is really the data transfer mode. */ 340 if (xcvr[0]) 341 dev->if_port = xcvr[0]; 342 else 343 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4; 344 if (dev->mem_end & 0xf) 345 net_debug = dev->mem_end & 7; 346 347 dev->netdev_ops = &atp_netdev_ops; 348 dev->watchdog_timeo = TX_TIMEOUT; 349 350 res = register_netdev(dev); 351 if (res) { 352 free_netdev(dev); 353 return res; 354 } 355 356 lp->next_module = root_atp_dev; 357 root_atp_dev = dev; 358 359 return 0; 360} 361 362/* Read the station address PROM, usually a word-wide EEPROM. */ 363static void __init get_node_ID(struct net_device *dev) 364{ 365 long ioaddr = dev->base_addr; 366 int sa_offset = 0; 367 int i; 368 369 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */ 370 371 /* Some adapters have the station address at offset 15 instead of offset 372 zero. Check for it, and fix it if needed. */ 373 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff) 374 sa_offset = 15; 375 376 for (i = 0; i < 3; i++) 377 ((__be16 *)dev->dev_addr)[i] = 378 cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i))); 379 380 write_reg(ioaddr, CMR2, CMR2_NULL); 381} 382 383/* 384 An EEPROM read command starts by shifting out 0x60+address, and then 385 shifting in the serial data. See the NatSemi databook for details. 386 * ________________ 387 * CS : __| 388 * ___ ___ 389 * CLK: ______| |___| | 390 * __ _______ _______ 391 * DI : __X_______X_______X 392 * DO : _________X_______X 393 */ 394 395static unsigned short __init eeprom_op(long ioaddr, u32 cmd) 396{ 397 unsigned eedata_out = 0; 398 int num_bits = EE_CMD_SIZE; 399 400 while (--num_bits >= 0) { 401 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0; 402 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW); 403 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH); 404 eedata_out <<= 1; 405 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ) 406 eedata_out++; 407 } 408 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS); 409 return eedata_out; 410} 411 412 413/* Open/initialize the board. This is called (in the current kernel) 414 sometime after booting when the 'ifconfig' program is run. 415 416 This routine sets everything up anew at each open, even 417 registers that "should" only need to be set once at boot, so that 418 there is non-reboot way to recover if something goes wrong. 419 420 This is an attachable device: if there is no private entry then it wasn't 421 probed for at boot-time, and we need to probe for it again. 422 */ 423static int net_open(struct net_device *dev) 424{ 425 struct net_local *lp = netdev_priv(dev); 426 int ret; 427 428 /* The interrupt line is turned off (tri-stated) when the device isn't in 429 use. That's especially important for "attached" interfaces where the 430 port or interrupt may be shared. */ 431 ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev); 432 if (ret) 433 return ret; 434 435 hardware_init(dev); 436 437 init_timer(&lp->timer); 438 lp->timer.expires = jiffies + TIMED_CHECKER; 439 lp->timer.data = (unsigned long)dev; 440 lp->timer.function = &atp_timed_checker; /* timer handler */ 441 add_timer(&lp->timer); 442 443 netif_start_queue(dev); 444 return 0; 445} 446 447/* This routine resets the hardware. We initialize everything, assuming that 448 the hardware may have been temporarily detached. */ 449static void hardware_init(struct net_device *dev) 450{ 451 struct net_local *lp = netdev_priv(dev); 452 long ioaddr = dev->base_addr; 453 int i; 454 455 /* Turn off the printer multiplexer on the 8012. */ 456 for (i = 0; i < 8; i++) 457 outb(mux_8012[i], ioaddr + PAR_DATA); 458 write_reg_high(ioaddr, CMR1, CMR1h_RESET); 459 460 for (i = 0; i < 6; i++) 461 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 462 463 write_reg_high(ioaddr, CMR2, lp->addr_mode); 464 465 if (net_debug > 2) { 466 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name, 467 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f); 468 } 469 470 write_reg(ioaddr, CMR2, CMR2_IRQOUT); 471 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); 472 473 /* Enable the interrupt line from the serial port. */ 474 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 475 476 /* Unmask the interesting interrupts. */ 477 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 478 write_reg_high(ioaddr, IMR, ISRh_RxErr); 479 480 lp->tx_unit_busy = 0; 481 lp->pac_cnt_in_tx_buf = 0; 482 lp->saved_tx_size = 0; 483} 484 485static void trigger_send(long ioaddr, int length) 486{ 487 write_reg_byte(ioaddr, TxCNT0, length & 0xff); 488 write_reg(ioaddr, TxCNT1, length >> 8); 489 write_reg(ioaddr, CMR1, CMR1_Xmit); 490} 491 492static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode) 493{ 494 if (length & 1) 495 { 496 length++; 497 pad_len++; 498 } 499 500 outb(EOC+MAR, ioaddr + PAR_DATA); 501 if ((data_mode & 1) == 0) { 502 /* Write the packet out, starting with the write addr. */ 503 outb(WrAddr+MAR, ioaddr + PAR_DATA); 504 do { 505 write_byte_mode0(ioaddr, *packet++); 506 } while (--length > pad_len) ; 507 do { 508 write_byte_mode0(ioaddr, 0); 509 } while (--length > 0) ; 510 } else { 511 /* Write the packet out in slow mode. */ 512 unsigned char outbyte = *packet++; 513 514 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 515 outb(WrAddr+MAR, ioaddr + PAR_DATA); 516 517 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA); 518 outb(outbyte & 0x0f, ioaddr + PAR_DATA); 519 outbyte >>= 4; 520 outb(outbyte & 0x0f, ioaddr + PAR_DATA); 521 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 522 while (--length > pad_len) 523 write_byte_mode1(ioaddr, *packet++); 524 while (--length > 0) 525 write_byte_mode1(ioaddr, 0); 526 } 527 /* Terminate the Tx frame. End of write: ECB. */ 528 outb(0xff, ioaddr + PAR_DATA); 529 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL); 530} 531 532static void tx_timeout(struct net_device *dev) 533{ 534 long ioaddr = dev->base_addr; 535 536 printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name, 537 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem" 538 : "IRQ conflict"); 539 dev->stats.tx_errors++; 540 /* Try to restart the adapter. */ 541 hardware_init(dev); 542 dev->trans_start = jiffies; /* prevent tx timeout */ 543 netif_wake_queue(dev); 544 dev->stats.tx_errors++; 545} 546 547static netdev_tx_t atp_send_packet(struct sk_buff *skb, 548 struct net_device *dev) 549{ 550 struct net_local *lp = netdev_priv(dev); 551 long ioaddr = dev->base_addr; 552 int length; 553 unsigned long flags; 554 555 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN; 556 557 netif_stop_queue(dev); 558 559 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register. 560 This sequence must not be interrupted by an incoming packet. */ 561 562 spin_lock_irqsave(&lp->lock, flags); 563 write_reg(ioaddr, IMR, 0); 564 write_reg_high(ioaddr, IMR, 0); 565 spin_unlock_irqrestore(&lp->lock, flags); 566 567 write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port); 568 569 lp->pac_cnt_in_tx_buf++; 570 if (lp->tx_unit_busy == 0) { 571 trigger_send(ioaddr, length); 572 lp->saved_tx_size = 0; /* Redundant */ 573 lp->re_tx = 0; 574 lp->tx_unit_busy = 1; 575 } else 576 lp->saved_tx_size = length; 577 /* Re-enable the LPT interrupts. */ 578 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 579 write_reg_high(ioaddr, IMR, ISRh_RxErr); 580 581 dev_kfree_skb (skb); 582 return NETDEV_TX_OK; 583} 584 585 586/* The typical workload of the driver: 587 Handle the network interface interrupts. */ 588static irqreturn_t atp_interrupt(int irq, void *dev_instance) 589{ 590 struct net_device *dev = dev_instance; 591 struct net_local *lp; 592 long ioaddr; 593 static int num_tx_since_rx; 594 int boguscount = max_interrupt_work; 595 int handled = 0; 596 597 ioaddr = dev->base_addr; 598 lp = netdev_priv(dev); 599 600 spin_lock(&lp->lock); 601 602 /* Disable additional spurious interrupts. */ 603 outb(Ctrl_SelData, ioaddr + PAR_CONTROL); 604 605 /* The adapter's output is currently the IRQ line, switch it to data. */ 606 write_reg(ioaddr, CMR2, CMR2_NULL); 607 write_reg(ioaddr, IMR, 0); 608 609 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name); 610 while (--boguscount > 0) { 611 int status = read_nibble(ioaddr, ISR); 612 if (net_debug > 5) printk("loop status %02x..", status); 613 614 if (status & (ISR_RxOK<<3)) { 615 handled = 1; 616 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */ 617 do { 618 int read_status = read_nibble(ioaddr, CMR1); 619 if (net_debug > 6) 620 printk("handling Rx packet %02x..", read_status); 621 /* We acknowledged the normal Rx interrupt, so if the interrupt 622 is still outstanding we must have a Rx error. */ 623 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */ 624 dev->stats.rx_over_errors++; 625 /* Set to no-accept mode long enough to remove a packet. */ 626 write_reg_high(ioaddr, CMR2, CMR2h_OFF); 627 net_rx(dev); 628 /* Clear the interrupt and return to normal Rx mode. */ 629 write_reg_high(ioaddr, ISR, ISRh_RxErr); 630 write_reg_high(ioaddr, CMR2, lp->addr_mode); 631 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) { 632 net_rx(dev); 633 num_tx_since_rx = 0; 634 } else 635 break; 636 } while (--boguscount > 0); 637 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) { 638 handled = 1; 639 if (net_debug > 6) printk("handling Tx done.."); 640 /* Clear the Tx interrupt. We should check for too many failures 641 and reinitialize the adapter. */ 642 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK); 643 if (status & (ISR_TxErr<<3)) { 644 dev->stats.collisions++; 645 if (++lp->re_tx > 15) { 646 dev->stats.tx_aborted_errors++; 647 hardware_init(dev); 648 break; 649 } 650 /* Attempt to retransmit. */ 651 if (net_debug > 6) printk("attempting to ReTx"); 652 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit); 653 } else { 654 /* Finish up the transmit. */ 655 dev->stats.tx_packets++; 656 lp->pac_cnt_in_tx_buf--; 657 if ( lp->saved_tx_size) { 658 trigger_send(ioaddr, lp->saved_tx_size); 659 lp->saved_tx_size = 0; 660 lp->re_tx = 0; 661 } else 662 lp->tx_unit_busy = 0; 663 netif_wake_queue(dev); /* Inform upper layers. */ 664 } 665 num_tx_since_rx++; 666 } else if (num_tx_since_rx > 8 && 667 time_after(jiffies, dev->last_rx + HZ)) { 668 if (net_debug > 2) 669 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and " 670 "%ld jiffies status %02x CMR1 %02x.\n", dev->name, 671 num_tx_since_rx, jiffies - dev->last_rx, status, 672 (read_nibble(ioaddr, CMR1) >> 3) & 15); 673 dev->stats.rx_missed_errors++; 674 hardware_init(dev); 675 num_tx_since_rx = 0; 676 break; 677 } else 678 break; 679 } 680 681 /* This following code fixes a rare (and very difficult to track down) 682 problem where the adapter forgets its ethernet address. */ 683 { 684 int i; 685 for (i = 0; i < 6; i++) 686 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 687 } 688 689 /* Tell the adapter that it can go back to using the output line as IRQ. */ 690 write_reg(ioaddr, CMR2, CMR2_IRQOUT); 691 /* Enable the physical interrupt line, which is sure to be low until.. */ 692 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 693 /* .. we enable the interrupt sources. */ 694 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 695 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */ 696 697 spin_unlock(&lp->lock); 698 699 if (net_debug > 5) printk("exiting interrupt.\n"); 700 return IRQ_RETVAL(handled); 701} 702 703#ifdef TIMED_CHECKER 704/* This following code fixes a rare (and very difficult to track down) 705 problem where the adapter forgets its ethernet address. */ 706static void atp_timed_checker(unsigned long data) 707{ 708 struct net_device *dev = (struct net_device *)data; 709 long ioaddr = dev->base_addr; 710 struct net_local *lp = netdev_priv(dev); 711 int tickssofar = jiffies - lp->last_rx_time; 712 int i; 713 714 spin_lock(&lp->lock); 715 if (tickssofar > 2*HZ) { 716 for (i = 0; i < 6; i++) 717 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 718 lp->last_rx_time = jiffies; 719 } 720 spin_unlock(&lp->lock); 721 lp->timer.expires = jiffies + TIMED_CHECKER; 722 add_timer(&lp->timer); 723} 724#endif 725 726/* We have a good packet(s), get it/them out of the buffers. */ 727static void net_rx(struct net_device *dev) 728{ 729 struct net_local *lp = netdev_priv(dev); 730 long ioaddr = dev->base_addr; 731 struct rx_header rx_head; 732 733 /* Process the received packet. */ 734 outb(EOC+MAR, ioaddr + PAR_DATA); 735 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port); 736 if (net_debug > 5) 737 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad, 738 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr); 739 if ((rx_head.rx_status & 0x77) != 0x01) { 740 dev->stats.rx_errors++; 741 if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++; 742 else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++; 743 if (net_debug > 3) 744 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n", 745 dev->name, rx_head.rx_status); 746 if (rx_head.rx_status & 0x0020) { 747 dev->stats.rx_fifo_errors++; 748 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE); 749 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); 750 } else if (rx_head.rx_status & 0x0050) 751 hardware_init(dev); 752 return; 753 } else { 754 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */ 755 int pkt_len = (rx_head.rx_count & 0x7ff) - 4; 756 struct sk_buff *skb; 757 758 skb = dev_alloc_skb(pkt_len + 2); 759 if (skb == NULL) { 760 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n", 761 dev->name); 762 dev->stats.rx_dropped++; 763 goto done; 764 } 765 766 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */ 767 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port); 768 skb->protocol = eth_type_trans(skb, dev); 769 netif_rx(skb); 770 dev->last_rx = jiffies; 771 dev->stats.rx_packets++; 772 dev->stats.rx_bytes += pkt_len; 773 } 774 done: 775 write_reg(ioaddr, CMR1, CMR1_NextPkt); 776 lp->last_rx_time = jiffies; 777} 778 779static void read_block(long ioaddr, int length, unsigned char *p, int data_mode) 780{ 781 if (data_mode <= 3) { /* Mode 0 or 1 */ 782 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL); 783 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR, 784 ioaddr + PAR_DATA); 785 if (data_mode <= 1) { /* Mode 0 or 1 */ 786 do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0); 787 } else { /* Mode 2 or 3 */ 788 do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0); 789 } 790 } else if (data_mode <= 5) { 791 do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0); 792 } else { 793 do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0); 794 } 795 796 outb(EOC+HNib+MAR, ioaddr + PAR_DATA); 797 outb(Ctrl_SelData, ioaddr + PAR_CONTROL); 798} 799 800/* The inverse routine to net_open(). */ 801static int 802net_close(struct net_device *dev) 803{ 804 struct net_local *lp = netdev_priv(dev); 805 long ioaddr = dev->base_addr; 806 807 netif_stop_queue(dev); 808 809 del_timer_sync(&lp->timer); 810 811 /* Flush the Tx and disable Rx here. */ 812 lp->addr_mode = CMR2h_OFF; 813 write_reg_high(ioaddr, CMR2, CMR2h_OFF); 814 815 /* Free the IRQ line. */ 816 outb(0x00, ioaddr + PAR_CONTROL); 817 free_irq(dev->irq, dev); 818 819 /* Reset the ethernet hardware and activate the printer pass-through. */ 820 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX); 821 return 0; 822} 823 824/* 825 * Set or clear the multicast filter for this adapter. 826 */ 827 828static void set_rx_mode_8002(struct net_device *dev) 829{ 830 struct net_local *lp = netdev_priv(dev); 831 long ioaddr = dev->base_addr; 832 833 if (!netdev_mc_empty(dev) || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) 834 lp->addr_mode = CMR2h_PROMISC; 835 else 836 lp->addr_mode = CMR2h_Normal; 837 write_reg_high(ioaddr, CMR2, lp->addr_mode); 838} 839 840static void set_rx_mode_8012(struct net_device *dev) 841{ 842 struct net_local *lp = netdev_priv(dev); 843 long ioaddr = dev->base_addr; 844 unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */ 845 int i; 846 847 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */ 848 new_mode = CMR2h_PROMISC; 849 } else if ((netdev_mc_count(dev) > 1000) || 850 (dev->flags & IFF_ALLMULTI)) { 851 /* Too many to filter perfectly -- accept all multicasts. */ 852 memset(mc_filter, 0xff, sizeof(mc_filter)); 853 new_mode = CMR2h_Normal; 854 } else { 855 struct netdev_hw_addr *ha; 856 857 memset(mc_filter, 0, sizeof(mc_filter)); 858 netdev_for_each_mc_addr(ha, dev) { 859 int filterbit = ether_crc_le(ETH_ALEN, ha->addr) & 0x3f; 860 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31); 861 } 862 new_mode = CMR2h_Normal; 863 } 864 lp->addr_mode = new_mode; 865 write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */ 866 for (i = 0; i < 8; i++) 867 write_reg_byte(ioaddr, i, mc_filter[i]); 868 if (net_debug > 2 || 1) { 869 lp->addr_mode = 1; 870 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to", 871 dev->name, lp->addr_mode); 872 for (i = 0; i < 8; i++) 873 printk(" %2.2x", mc_filter[i]); 874 printk(".\n"); 875 } 876 877 write_reg_high(ioaddr, CMR2, lp->addr_mode); 878 write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */ 879} 880 881static void set_rx_mode(struct net_device *dev) 882{ 883 struct net_local *lp = netdev_priv(dev); 884 885 if (lp->chip_type == RTL8002) 886 return set_rx_mode_8002(dev); 887 else 888 return set_rx_mode_8012(dev); 889} 890 891 892static int __init atp_init_module(void) { 893 if (debug) /* Emit version even if no cards detected. */ 894 printk(KERN_INFO "%s", version); 895 return atp_init(); 896} 897 898static void __exit atp_cleanup_module(void) { 899 struct net_device *next_dev; 900 901 while (root_atp_dev) { 902 struct net_local *atp_local = netdev_priv(root_atp_dev); 903 next_dev = atp_local->next_module; 904 unregister_netdev(root_atp_dev); 905 /* No need to release_region(), since we never snarf it. */ 906 free_netdev(root_atp_dev); 907 root_atp_dev = next_dev; 908 } 909} 910 911module_init(atp_init_module); 912module_exit(atp_cleanup_module); 913