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 34 static 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 40 static 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. */ 44 static int max_interrupt_work = 15; 45 46 #define NUM_UNITS 2 47 /* The standard set of ISA module parameters. */ 48 static int io[NUM_UNITS]; 49 static int irq[NUM_UNITS]; 50 static 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/ 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/io.h> 144 #include <asm/dma.h> 145 146 #include "atp.h" 147 148 MODULE_AUTHOR("Donald Becker <becker@scyld.com>"); 149 MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver"); 150 MODULE_LICENSE("GPL"); 151 152 module_param(max_interrupt_work, int, 0); 153 module_param(debug, int, 0); 154 module_param_hw_array(io, int, ioport, NULL, 0); 155 module_param_hw_array(irq, int, irq, NULL, 0); 156 module_param_array(xcvr, int, NULL, 0); 157 MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt"); 158 MODULE_PARM_DESC(debug, "ATP debug level (0-7)"); 159 MODULE_PARM_DESC(io, "ATP I/O base address(es)"); 160 MODULE_PARM_DESC(irq, "ATP IRQ number(s)"); 161 MODULE_PARM_DESC(xcvr, "ATP transceiver(s) (0=internal, 1=external)"); 162 163 /* The number of low I/O ports used by the ethercard. */ 164 #define ETHERCARD_TOTAL_SIZE 3 165 166 /* Sequence to switch an 8012 from printer mux to ethernet mode. */ 167 static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,}; 168 169 struct net_local { 170 spinlock_t lock; 171 struct net_device *next_module; 172 struct timer_list timer; /* Media selection timer. */ 173 unsigned long last_rx_time; /* Last Rx, in jiffies, to handle Rx hang. */ 174 int saved_tx_size; 175 unsigned int tx_unit_busy:1; 176 unsigned char re_tx, /* Number of packet retransmissions. */ 177 addr_mode, /* Current Rx filter e.g. promiscuous, etc. */ 178 pac_cnt_in_tx_buf; 179 }; 180 181 /* This code, written by wwc@super.org, resets the adapter every 182 TIMED_CHECKER ticks. This recovers from an unknown error which 183 hangs the device. */ 184 #define TIMED_CHECKER (HZ/4) 185 #ifdef TIMED_CHECKER 186 #include <linux/timer.h> 187 static void atp_timed_checker(unsigned long ignored); 188 #endif 189 190 /* Index to functions, as function prototypes. */ 191 192 static int atp_probe1(long ioaddr); 193 static void get_node_ID(struct net_device *dev); 194 static unsigned short eeprom_op(long ioaddr, unsigned int cmd); 195 static int net_open(struct net_device *dev); 196 static void hardware_init(struct net_device *dev); 197 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode); 198 static void trigger_send(long ioaddr, int length); 199 static netdev_tx_t atp_send_packet(struct sk_buff *skb, 200 struct net_device *dev); 201 static irqreturn_t atp_interrupt(int irq, void *dev_id); 202 static void net_rx(struct net_device *dev); 203 static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode); 204 static int net_close(struct net_device *dev); 205 static void set_rx_mode(struct net_device *dev); 206 static void tx_timeout(struct net_device *dev); 207 208 209 /* A list of all installed ATP devices, for removing the driver module. */ 210 static struct net_device *root_atp_dev; 211 212 /* Check for a network adapter of this type, and return '0' iff one exists. 213 If dev->base_addr == 0, probe all likely locations. 214 If dev->base_addr == 1, always return failure. 215 If dev->base_addr == 2, allocate space for the device and return success 216 (detachable devices only). 217 218 FIXME: we should use the parport layer for this 219 */ 220 static int __init atp_init(void) 221 { 222 int *port, ports[] = {0x378, 0x278, 0x3bc, 0}; 223 int base_addr = io[0]; 224 225 if (base_addr > 0x1ff) /* Check a single specified location. */ 226 return atp_probe1(base_addr); 227 else if (base_addr == 1) /* Don't probe at all. */ 228 return -ENXIO; 229 230 for (port = ports; *port; port++) { 231 long ioaddr = *port; 232 outb(0x57, ioaddr + PAR_DATA); 233 if (inb(ioaddr + PAR_DATA) != 0x57) 234 continue; 235 if (atp_probe1(ioaddr) == 0) 236 return 0; 237 } 238 239 return -ENODEV; 240 } 241 242 static const struct net_device_ops atp_netdev_ops = { 243 .ndo_open = net_open, 244 .ndo_stop = net_close, 245 .ndo_start_xmit = atp_send_packet, 246 .ndo_set_rx_mode = set_rx_mode, 247 .ndo_tx_timeout = tx_timeout, 248 .ndo_set_mac_address = eth_mac_addr, 249 .ndo_validate_addr = eth_validate_addr, 250 }; 251 252 static int __init atp_probe1(long ioaddr) 253 { 254 struct net_device *dev = NULL; 255 struct net_local *lp; 256 int saved_ctrl_reg, status, i; 257 int res; 258 259 outb(0xff, ioaddr + PAR_DATA); 260 /* Save the original value of the Control register, in case we guessed 261 wrong. */ 262 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL); 263 if (net_debug > 3) 264 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg); 265 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */ 266 outb(0x04, ioaddr + PAR_CONTROL); 267 #ifndef final_version 268 if (net_debug > 3) { 269 /* Turn off the printer multiplexer on the 8012. */ 270 for (i = 0; i < 8; i++) 271 outb(mux_8012[i], ioaddr + PAR_DATA); 272 write_reg(ioaddr, MODSEL, 0x00); 273 printk("atp: Registers are "); 274 for (i = 0; i < 32; i++) 275 printk(" %2.2x", read_nibble(ioaddr, i)); 276 printk(".\n"); 277 } 278 #endif 279 /* Turn off the printer multiplexer on the 8012. */ 280 for (i = 0; i < 8; i++) 281 outb(mux_8012[i], ioaddr + PAR_DATA); 282 write_reg_high(ioaddr, CMR1, CMR1h_RESET); 283 /* udelay() here? */ 284 status = read_nibble(ioaddr, CMR1); 285 286 if (net_debug > 3) { 287 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status); 288 for (i = 0; i < 32; i++) 289 printk(" %2.2x", read_nibble(ioaddr, i)); 290 printk("\n"); 291 } 292 293 if ((status & 0x78) != 0x08) { 294 /* The pocket adapter probe failed, restore the control register. */ 295 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL); 296 return -ENODEV; 297 } 298 status = read_nibble(ioaddr, CMR2_h); 299 if ((status & 0x78) != 0x10) { 300 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL); 301 return -ENODEV; 302 } 303 304 dev = alloc_etherdev(sizeof(struct net_local)); 305 if (!dev) 306 return -ENOMEM; 307 308 /* Find the IRQ used by triggering an interrupt. */ 309 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */ 310 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */ 311 312 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */ 313 if (irq[0]) 314 dev->irq = irq[0]; 315 else if (ioaddr == 0x378) 316 dev->irq = 7; 317 else 318 dev->irq = 5; 319 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */ 320 write_reg(ioaddr, CMR2, CMR2_NULL); 321 322 dev->base_addr = ioaddr; 323 324 /* Read the station address PROM. */ 325 get_node_ID(dev); 326 327 #ifndef MODULE 328 if (net_debug) 329 printk(KERN_INFO "%s", version); 330 #endif 331 332 printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, " 333 "SAPROM %pM.\n", 334 dev->name, dev->base_addr, dev->irq, dev->dev_addr); 335 336 /* Reset the ethernet hardware and activate the printer pass-through. */ 337 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX); 338 339 lp = netdev_priv(dev); 340 lp->addr_mode = CMR2h_Normal; 341 spin_lock_init(&lp->lock); 342 343 /* For the ATP adapter the "if_port" is really the data transfer mode. */ 344 if (xcvr[0]) 345 dev->if_port = xcvr[0]; 346 else 347 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4; 348 if (dev->mem_end & 0xf) 349 net_debug = dev->mem_end & 7; 350 351 dev->netdev_ops = &atp_netdev_ops; 352 dev->watchdog_timeo = TX_TIMEOUT; 353 354 res = register_netdev(dev); 355 if (res) { 356 free_netdev(dev); 357 return res; 358 } 359 360 lp->next_module = root_atp_dev; 361 root_atp_dev = dev; 362 363 return 0; 364 } 365 366 /* Read the station address PROM, usually a word-wide EEPROM. */ 367 static void __init get_node_ID(struct net_device *dev) 368 { 369 long ioaddr = dev->base_addr; 370 int sa_offset = 0; 371 int i; 372 373 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */ 374 375 /* Some adapters have the station address at offset 15 instead of offset 376 zero. Check for it, and fix it if needed. */ 377 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff) 378 sa_offset = 15; 379 380 for (i = 0; i < 3; i++) 381 ((__be16 *)dev->dev_addr)[i] = 382 cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i))); 383 384 write_reg(ioaddr, CMR2, CMR2_NULL); 385 } 386 387 /* 388 An EEPROM read command starts by shifting out 0x60+address, and then 389 shifting in the serial data. See the NatSemi databook for details. 390 * ________________ 391 * CS : __| 392 * ___ ___ 393 * CLK: ______| |___| | 394 * __ _______ _______ 395 * DI : __X_______X_______X 396 * DO : _________X_______X 397 */ 398 399 static unsigned short __init eeprom_op(long ioaddr, u32 cmd) 400 { 401 unsigned eedata_out = 0; 402 int num_bits = EE_CMD_SIZE; 403 404 while (--num_bits >= 0) { 405 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0; 406 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW); 407 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH); 408 eedata_out <<= 1; 409 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ) 410 eedata_out++; 411 } 412 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS); 413 return eedata_out; 414 } 415 416 417 /* Open/initialize the board. This is called (in the current kernel) 418 sometime after booting when the 'ifconfig' program is run. 419 420 This routine sets everything up anew at each open, even 421 registers that "should" only need to be set once at boot, so that 422 there is non-reboot way to recover if something goes wrong. 423 424 This is an attachable device: if there is no private entry then it wasn't 425 probed for at boot-time, and we need to probe for it again. 426 */ 427 static int net_open(struct net_device *dev) 428 { 429 struct net_local *lp = netdev_priv(dev); 430 int ret; 431 432 /* The interrupt line is turned off (tri-stated) when the device isn't in 433 use. That's especially important for "attached" interfaces where the 434 port or interrupt may be shared. */ 435 ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev); 436 if (ret) 437 return ret; 438 439 hardware_init(dev); 440 441 setup_timer(&lp->timer, atp_timed_checker, (unsigned long)dev); 442 lp->timer.expires = jiffies + TIMED_CHECKER; 443 add_timer(&lp->timer); 444 445 netif_start_queue(dev); 446 return 0; 447 } 448 449 /* This routine resets the hardware. We initialize everything, assuming that 450 the hardware may have been temporarily detached. */ 451 static void hardware_init(struct net_device *dev) 452 { 453 struct net_local *lp = netdev_priv(dev); 454 long ioaddr = dev->base_addr; 455 int i; 456 457 /* Turn off the printer multiplexer on the 8012. */ 458 for (i = 0; i < 8; i++) 459 outb(mux_8012[i], ioaddr + PAR_DATA); 460 write_reg_high(ioaddr, CMR1, CMR1h_RESET); 461 462 for (i = 0; i < 6; i++) 463 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 464 465 write_reg_high(ioaddr, CMR2, lp->addr_mode); 466 467 if (net_debug > 2) { 468 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name, 469 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f); 470 } 471 472 write_reg(ioaddr, CMR2, CMR2_IRQOUT); 473 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); 474 475 /* Enable the interrupt line from the serial port. */ 476 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 477 478 /* Unmask the interesting interrupts. */ 479 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 480 write_reg_high(ioaddr, IMR, ISRh_RxErr); 481 482 lp->tx_unit_busy = 0; 483 lp->pac_cnt_in_tx_buf = 0; 484 lp->saved_tx_size = 0; 485 } 486 487 static void trigger_send(long ioaddr, int length) 488 { 489 write_reg_byte(ioaddr, TxCNT0, length & 0xff); 490 write_reg(ioaddr, TxCNT1, length >> 8); 491 write_reg(ioaddr, CMR1, CMR1_Xmit); 492 } 493 494 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode) 495 { 496 if (length & 1) 497 { 498 length++; 499 pad_len++; 500 } 501 502 outb(EOC+MAR, ioaddr + PAR_DATA); 503 if ((data_mode & 1) == 0) { 504 /* Write the packet out, starting with the write addr. */ 505 outb(WrAddr+MAR, ioaddr + PAR_DATA); 506 do { 507 write_byte_mode0(ioaddr, *packet++); 508 } while (--length > pad_len) ; 509 do { 510 write_byte_mode0(ioaddr, 0); 511 } while (--length > 0) ; 512 } else { 513 /* Write the packet out in slow mode. */ 514 unsigned char outbyte = *packet++; 515 516 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 517 outb(WrAddr+MAR, ioaddr + PAR_DATA); 518 519 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA); 520 outb(outbyte & 0x0f, ioaddr + PAR_DATA); 521 outbyte >>= 4; 522 outb(outbyte & 0x0f, ioaddr + PAR_DATA); 523 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 524 while (--length > pad_len) 525 write_byte_mode1(ioaddr, *packet++); 526 while (--length > 0) 527 write_byte_mode1(ioaddr, 0); 528 } 529 /* Terminate the Tx frame. End of write: ECB. */ 530 outb(0xff, ioaddr + PAR_DATA); 531 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL); 532 } 533 534 static void tx_timeout(struct net_device *dev) 535 { 536 long ioaddr = dev->base_addr; 537 538 printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name, 539 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem" 540 : "IRQ conflict"); 541 dev->stats.tx_errors++; 542 /* Try to restart the adapter. */ 543 hardware_init(dev); 544 netif_trans_update(dev); /* prevent tx timeout */ 545 netif_wake_queue(dev); 546 dev->stats.tx_errors++; 547 } 548 549 static netdev_tx_t atp_send_packet(struct sk_buff *skb, 550 struct net_device *dev) 551 { 552 struct net_local *lp = netdev_priv(dev); 553 long ioaddr = dev->base_addr; 554 int length; 555 unsigned long flags; 556 557 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN; 558 559 netif_stop_queue(dev); 560 561 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register. 562 This sequence must not be interrupted by an incoming packet. */ 563 564 spin_lock_irqsave(&lp->lock, flags); 565 write_reg(ioaddr, IMR, 0); 566 write_reg_high(ioaddr, IMR, 0); 567 spin_unlock_irqrestore(&lp->lock, flags); 568 569 write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port); 570 571 lp->pac_cnt_in_tx_buf++; 572 if (lp->tx_unit_busy == 0) { 573 trigger_send(ioaddr, length); 574 lp->saved_tx_size = 0; /* Redundant */ 575 lp->re_tx = 0; 576 lp->tx_unit_busy = 1; 577 } else 578 lp->saved_tx_size = length; 579 /* Re-enable the LPT interrupts. */ 580 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 581 write_reg_high(ioaddr, IMR, ISRh_RxErr); 582 583 dev_kfree_skb (skb); 584 return NETDEV_TX_OK; 585 } 586 587 588 /* The typical workload of the driver: 589 Handle the network interface interrupts. */ 590 static irqreturn_t atp_interrupt(int irq, void *dev_instance) 591 { 592 struct net_device *dev = dev_instance; 593 struct net_local *lp; 594 long ioaddr; 595 static int num_tx_since_rx; 596 int boguscount = max_interrupt_work; 597 int handled = 0; 598 599 ioaddr = dev->base_addr; 600 lp = netdev_priv(dev); 601 602 spin_lock(&lp->lock); 603 604 /* Disable additional spurious interrupts. */ 605 outb(Ctrl_SelData, ioaddr + PAR_CONTROL); 606 607 /* The adapter's output is currently the IRQ line, switch it to data. */ 608 write_reg(ioaddr, CMR2, CMR2_NULL); 609 write_reg(ioaddr, IMR, 0); 610 611 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name); 612 while (--boguscount > 0) { 613 int status = read_nibble(ioaddr, ISR); 614 if (net_debug > 5) printk("loop status %02x..", status); 615 616 if (status & (ISR_RxOK<<3)) { 617 handled = 1; 618 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */ 619 do { 620 int read_status = read_nibble(ioaddr, CMR1); 621 if (net_debug > 6) 622 printk("handling Rx packet %02x..", read_status); 623 /* We acknowledged the normal Rx interrupt, so if the interrupt 624 is still outstanding we must have a Rx error. */ 625 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */ 626 dev->stats.rx_over_errors++; 627 /* Set to no-accept mode long enough to remove a packet. */ 628 write_reg_high(ioaddr, CMR2, CMR2h_OFF); 629 net_rx(dev); 630 /* Clear the interrupt and return to normal Rx mode. */ 631 write_reg_high(ioaddr, ISR, ISRh_RxErr); 632 write_reg_high(ioaddr, CMR2, lp->addr_mode); 633 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) { 634 net_rx(dev); 635 num_tx_since_rx = 0; 636 } else 637 break; 638 } while (--boguscount > 0); 639 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) { 640 handled = 1; 641 if (net_debug > 6) printk("handling Tx done.."); 642 /* Clear the Tx interrupt. We should check for too many failures 643 and reinitialize the adapter. */ 644 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK); 645 if (status & (ISR_TxErr<<3)) { 646 dev->stats.collisions++; 647 if (++lp->re_tx > 15) { 648 dev->stats.tx_aborted_errors++; 649 hardware_init(dev); 650 break; 651 } 652 /* Attempt to retransmit. */ 653 if (net_debug > 6) printk("attempting to ReTx"); 654 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit); 655 } else { 656 /* Finish up the transmit. */ 657 dev->stats.tx_packets++; 658 lp->pac_cnt_in_tx_buf--; 659 if ( lp->saved_tx_size) { 660 trigger_send(ioaddr, lp->saved_tx_size); 661 lp->saved_tx_size = 0; 662 lp->re_tx = 0; 663 } else 664 lp->tx_unit_busy = 0; 665 netif_wake_queue(dev); /* Inform upper layers. */ 666 } 667 num_tx_since_rx++; 668 } else if (num_tx_since_rx > 8 && 669 time_after(jiffies, lp->last_rx_time + HZ)) { 670 if (net_debug > 2) 671 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and " 672 "%ld jiffies status %02x CMR1 %02x.\n", dev->name, 673 num_tx_since_rx, jiffies - lp->last_rx_time, status, 674 (read_nibble(ioaddr, CMR1) >> 3) & 15); 675 dev->stats.rx_missed_errors++; 676 hardware_init(dev); 677 num_tx_since_rx = 0; 678 break; 679 } else 680 break; 681 } 682 683 /* This following code fixes a rare (and very difficult to track down) 684 problem where the adapter forgets its ethernet address. */ 685 { 686 int i; 687 for (i = 0; i < 6; i++) 688 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 689 #if 0 && defined(TIMED_CHECKER) 690 mod_timer(&lp->timer, jiffies + TIMED_CHECKER); 691 #endif 692 } 693 694 /* Tell the adapter that it can go back to using the output line as IRQ. */ 695 write_reg(ioaddr, CMR2, CMR2_IRQOUT); 696 /* Enable the physical interrupt line, which is sure to be low until.. */ 697 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL); 698 /* .. we enable the interrupt sources. */ 699 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK); 700 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */ 701 702 spin_unlock(&lp->lock); 703 704 if (net_debug > 5) printk("exiting interrupt.\n"); 705 return IRQ_RETVAL(handled); 706 } 707 708 #ifdef TIMED_CHECKER 709 /* This following code fixes a rare (and very difficult to track down) 710 problem where the adapter forgets its ethernet address. */ 711 static void atp_timed_checker(unsigned long data) 712 { 713 struct net_device *dev = (struct net_device *)data; 714 long ioaddr = dev->base_addr; 715 struct net_local *lp = netdev_priv(dev); 716 int tickssofar = jiffies - lp->last_rx_time; 717 int i; 718 719 spin_lock(&lp->lock); 720 if (tickssofar > 2*HZ) { 721 #if 1 722 for (i = 0; i < 6; i++) 723 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]); 724 lp->last_rx_time = jiffies; 725 #else 726 for (i = 0; i < 6; i++) 727 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i]) 728 { 729 struct net_local *lp = netdev_priv(atp_timed_dev); 730 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]); 731 if (i == 2) 732 dev->stats.tx_errors++; 733 else if (i == 3) 734 dev->stats.tx_dropped++; 735 else if (i == 4) 736 dev->stats.collisions++; 737 else 738 dev->stats.rx_errors++; 739 } 740 #endif 741 } 742 spin_unlock(&lp->lock); 743 lp->timer.expires = jiffies + TIMED_CHECKER; 744 add_timer(&lp->timer); 745 } 746 #endif 747 748 /* We have a good packet(s), get it/them out of the buffers. */ 749 static void net_rx(struct net_device *dev) 750 { 751 struct net_local *lp = netdev_priv(dev); 752 long ioaddr = dev->base_addr; 753 struct rx_header rx_head; 754 755 /* Process the received packet. */ 756 outb(EOC+MAR, ioaddr + PAR_DATA); 757 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port); 758 if (net_debug > 5) 759 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad, 760 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr); 761 if ((rx_head.rx_status & 0x77) != 0x01) { 762 dev->stats.rx_errors++; 763 if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++; 764 else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++; 765 if (net_debug > 3) 766 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n", 767 dev->name, rx_head.rx_status); 768 if (rx_head.rx_status & 0x0020) { 769 dev->stats.rx_fifo_errors++; 770 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE); 771 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); 772 } else if (rx_head.rx_status & 0x0050) 773 hardware_init(dev); 774 return; 775 } else { 776 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */ 777 int pkt_len = (rx_head.rx_count & 0x7ff) - 4; 778 struct sk_buff *skb; 779 780 skb = netdev_alloc_skb(dev, pkt_len + 2); 781 if (skb == NULL) { 782 dev->stats.rx_dropped++; 783 goto done; 784 } 785 786 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */ 787 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port); 788 skb->protocol = eth_type_trans(skb, dev); 789 netif_rx(skb); 790 dev->stats.rx_packets++; 791 dev->stats.rx_bytes += pkt_len; 792 } 793 done: 794 write_reg(ioaddr, CMR1, CMR1_NextPkt); 795 lp->last_rx_time = jiffies; 796 } 797 798 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode) 799 { 800 if (data_mode <= 3) { /* Mode 0 or 1 */ 801 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL); 802 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR, 803 ioaddr + PAR_DATA); 804 if (data_mode <= 1) { /* Mode 0 or 1 */ 805 do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0); 806 } else { /* Mode 2 or 3 */ 807 do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0); 808 } 809 } else if (data_mode <= 5) { 810 do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0); 811 } else { 812 do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0); 813 } 814 815 outb(EOC+HNib+MAR, ioaddr + PAR_DATA); 816 outb(Ctrl_SelData, ioaddr + PAR_CONTROL); 817 } 818 819 /* The inverse routine to net_open(). */ 820 static int 821 net_close(struct net_device *dev) 822 { 823 struct net_local *lp = netdev_priv(dev); 824 long ioaddr = dev->base_addr; 825 826 netif_stop_queue(dev); 827 828 del_timer_sync(&lp->timer); 829 830 /* Flush the Tx and disable Rx here. */ 831 lp->addr_mode = CMR2h_OFF; 832 write_reg_high(ioaddr, CMR2, CMR2h_OFF); 833 834 /* Free the IRQ line. */ 835 outb(0x00, ioaddr + PAR_CONTROL); 836 free_irq(dev->irq, dev); 837 838 /* Reset the ethernet hardware and activate the printer pass-through. */ 839 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX); 840 return 0; 841 } 842 843 /* 844 * Set or clear the multicast filter for this adapter. 845 */ 846 847 static void set_rx_mode(struct net_device *dev) 848 { 849 struct net_local *lp = netdev_priv(dev); 850 long ioaddr = dev->base_addr; 851 852 if (!netdev_mc_empty(dev) || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) 853 lp->addr_mode = CMR2h_PROMISC; 854 else 855 lp->addr_mode = CMR2h_Normal; 856 write_reg_high(ioaddr, CMR2, lp->addr_mode); 857 } 858 859 static int __init atp_init_module(void) { 860 if (debug) /* Emit version even if no cards detected. */ 861 printk(KERN_INFO "%s", version); 862 return atp_init(); 863 } 864 865 static void __exit atp_cleanup_module(void) { 866 struct net_device *next_dev; 867 868 while (root_atp_dev) { 869 struct net_local *atp_local = netdev_priv(root_atp_dev); 870 next_dev = atp_local->next_module; 871 unregister_netdev(root_atp_dev); 872 /* No need to release_region(), since we never snarf it. */ 873 free_netdev(root_atp_dev); 874 root_atp_dev = next_dev; 875 } 876 } 877 878 module_init(atp_init_module); 879 module_exit(atp_cleanup_module); 880