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