IRQ: Maintain regs pointer globally rather than passing to IRQ handlers
[powerpc.git] / drivers / net / atp.c
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 versionA[] =
35 "atp.c:v1.09=ac 2002/10/01 Donald Becker <becker@scyld.com>\n";
36 static 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
42 static 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. */
46 static int max_interrupt_work = 15;
47
48 #define NUM_UNITS 2
49 /* The standard set of ISA module parameters. */
50 static int io[NUM_UNITS];
51 static int irq[NUM_UNITS];
52 static 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
152 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
153 MODULE_DESCRIPTION("RealTek RTL8002/8012 parallel port Ethernet driver");
154 MODULE_LICENSE("GPL");
155
156 module_param(max_interrupt_work, int, 0);
157 module_param(debug, int, 0);
158 module_param_array(io, int, NULL, 0);
159 module_param_array(irq, int, NULL, 0);
160 module_param_array(xcvr, int, NULL, 0);
161 MODULE_PARM_DESC(max_interrupt_work, "ATP maximum events handled per interrupt");
162 MODULE_PARM_DESC(debug, "ATP debug level (0-7)");
163 MODULE_PARM_DESC(io, "ATP I/O base address(es)");
164 MODULE_PARM_DESC(irq, "ATP IRQ number(s)");
165 MODULE_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. */
171 static char mux_8012[] = { 0xff, 0xf7, 0xff, 0xfb, 0xf3, 0xfb, 0xff, 0xf7,};
172
173 struct 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>
193 static void atp_timed_checker(unsigned long ignored);
194 #endif
195
196 /* Index to functions, as function prototypes. */
197
198 static int atp_probe1(long ioaddr);
199 static void get_node_ID(struct net_device *dev);
200 static unsigned short eeprom_op(long ioaddr, unsigned int cmd);
201 static int net_open(struct net_device *dev);
202 static void hardware_init(struct net_device *dev);
203 static void write_packet(long ioaddr, int length, unsigned char *packet, int pad, int mode);
204 static void trigger_send(long ioaddr, int length);
205 static int      atp_send_packet(struct sk_buff *skb, struct net_device *dev);
206 static irqreturn_t atp_interrupt(int irq, void *dev_id);
207 static void net_rx(struct net_device *dev);
208 static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
209 static int net_close(struct net_device *dev);
210 static struct net_device_stats *net_get_stats(struct net_device *dev);
211 static void set_rx_mode_8002(struct net_device *dev);
212 static void set_rx_mode_8012(struct net_device *dev);
213 static void tx_timeout(struct net_device *dev);
214
215
216 /* A list of all installed ATP devices, for removing the driver module. */
217 static 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).
224
225    FIXME: we should use the parport layer for this
226    */
227 static 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
249 static 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. */
373 static 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
405 static 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    */
433 static 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. */
459 static 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
495 static 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
502 static 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
542 static 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
558 static 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. */
599 static irqreturn_t atp_interrupt(int irq, void *dev_instance)
600 {
601         struct net_device *dev = (struct net_device *)dev_instance;
602         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
608         if (dev == NULL) {
609                 printk(KERN_ERR "ATP_interrupt(): irq %d for unknown device.\n", irq);
610                 return IRQ_NONE;
611         }
612         ioaddr = dev->base_addr;
613         lp = netdev_priv(dev);
614
615         spin_lock(&lp->lock);
616
617         /* Disable additional spurious interrupts. */
618         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
619
620         /* The adapter's output is currently the IRQ line, switch it to data. */
621         write_reg(ioaddr, CMR2, CMR2_NULL);
622         write_reg(ioaddr, IMR, 0);
623
624         if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
625     while (--boguscount > 0) {
626                 int status = read_nibble(ioaddr, ISR);
627                 if (net_debug > 5) printk("loop status %02x..", status);
628
629                 if (status & (ISR_RxOK<<3)) {
630                         handled = 1;
631                         write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
632                         do {
633                                 int read_status = read_nibble(ioaddr, CMR1);
634                                 if (net_debug > 6)
635                                         printk("handling Rx packet %02x..", read_status);
636                                 /* We acknowledged the normal Rx interrupt, so if the interrupt
637                                    is still outstanding we must have a Rx error. */
638                                 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
639                                         lp->stats.rx_over_errors++;
640                                         /* Set to no-accept mode long enough to remove a packet. */
641                                         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
642                                         net_rx(dev);
643                                         /* Clear the interrupt and return to normal Rx mode. */
644                                         write_reg_high(ioaddr, ISR, ISRh_RxErr);
645                                         write_reg_high(ioaddr, CMR2, lp->addr_mode);
646                                 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
647                                         net_rx(dev);
648                                         num_tx_since_rx = 0;
649                                 } else
650                                         break;
651                         } while (--boguscount > 0);
652                 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
653                         handled = 1;
654                         if (net_debug > 6)  printk("handling Tx done..");
655                         /* Clear the Tx interrupt.  We should check for too many failures
656                            and reinitialize the adapter. */
657                         write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
658                         if (status & (ISR_TxErr<<3)) {
659                                 lp->stats.collisions++;
660                                 if (++lp->re_tx > 15) {
661                                         lp->stats.tx_aborted_errors++;
662                                         hardware_init(dev);
663                                         break;
664                                 }
665                                 /* Attempt to retransmit. */
666                                 if (net_debug > 6)  printk("attempting to ReTx");
667                                 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
668                         } else {
669                                 /* Finish up the transmit. */
670                                 lp->stats.tx_packets++;
671                                 lp->pac_cnt_in_tx_buf--;
672                                 if ( lp->saved_tx_size) {
673                                         trigger_send(ioaddr, lp->saved_tx_size);
674                                         lp->saved_tx_size = 0;
675                                         lp->re_tx = 0;
676                                 } else
677                                         lp->tx_unit_busy = 0;
678                                 netif_wake_queue(dev);  /* Inform upper layers. */
679                         }
680                         num_tx_since_rx++;
681                 } else if (num_tx_since_rx > 8
682                                    && time_after(jiffies, dev->last_rx + HZ)) {
683                         if (net_debug > 2)
684                                 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
685                                            "%ld jiffies status %02x  CMR1 %02x.\n", dev->name,
686                                            num_tx_since_rx, jiffies - dev->last_rx, status,
687                                            (read_nibble(ioaddr, CMR1) >> 3) & 15);
688                         lp->stats.rx_missed_errors++;
689                         hardware_init(dev);
690                         num_tx_since_rx = 0;
691                         break;
692                 } else
693                         break;
694     }
695
696         /* This following code fixes a rare (and very difficult to track down)
697            problem where the adapter forgets its ethernet address. */
698         {
699                 int i;
700                 for (i = 0; i < 6; i++)
701                         write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
702 #if 0 && defined(TIMED_CHECKER)
703                 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
704 #endif
705         }
706
707         /* Tell the adapter that it can go back to using the output line as IRQ. */
708     write_reg(ioaddr, CMR2, CMR2_IRQOUT);
709         /* Enable the physical interrupt line, which is sure to be low until.. */
710         outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
711         /* .. we enable the interrupt sources. */
712         write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
713         write_reg_high(ioaddr, IMR, ISRh_RxErr);                        /* Hmmm, really needed? */
714
715         spin_unlock(&lp->lock);
716
717         if (net_debug > 5) printk("exiting interrupt.\n");
718         return IRQ_RETVAL(handled);
719 }
720
721 #ifdef TIMED_CHECKER
722 /* This following code fixes a rare (and very difficult to track down)
723    problem where the adapter forgets its ethernet address. */
724 static void atp_timed_checker(unsigned long data)
725 {
726         struct net_device *dev = (struct net_device *)data;
727         long ioaddr = dev->base_addr;
728         struct net_local *lp = netdev_priv(dev);
729         int tickssofar = jiffies - lp->last_rx_time;
730         int i;
731
732         spin_lock(&lp->lock);
733         if (tickssofar > 2*HZ) {
734 #if 1
735                 for (i = 0; i < 6; i++)
736                         write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
737                 lp->last_rx_time = jiffies;
738 #else
739                 for (i = 0; i < 6; i++)
740                         if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
741                                 {
742                         struct net_local *lp = netdev_priv(atp_timed_dev);
743                         write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
744                         if (i == 2)
745                           lp->stats.tx_errors++;
746                         else if (i == 3)
747                           lp->stats.tx_dropped++;
748                         else if (i == 4)
749                           lp->stats.collisions++;
750                         else
751                           lp->stats.rx_errors++;
752                   }
753 #endif
754         }
755         spin_unlock(&lp->lock);
756         lp->timer.expires = jiffies + TIMED_CHECKER;
757         add_timer(&lp->timer);
758 }
759 #endif
760
761 /* We have a good packet(s), get it/them out of the buffers. */
762 static void net_rx(struct net_device *dev)
763 {
764         struct net_local *lp = netdev_priv(dev);
765         long ioaddr = dev->base_addr;
766         struct rx_header rx_head;
767
768         /* Process the received packet. */
769         outb(EOC+MAR, ioaddr + PAR_DATA);
770         read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
771         if (net_debug > 5)
772                 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
773                            rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
774         if ((rx_head.rx_status & 0x77) != 0x01) {
775                 lp->stats.rx_errors++;
776                 if (rx_head.rx_status & 0x0004) lp->stats.rx_frame_errors++;
777                 else if (rx_head.rx_status & 0x0002) lp->stats.rx_crc_errors++;
778                 if (net_debug > 3)
779                         printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
780                                    dev->name, rx_head.rx_status);
781                 if  (rx_head.rx_status & 0x0020) {
782                         lp->stats.rx_fifo_errors++;
783                         write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
784                         write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
785                 } else if (rx_head.rx_status & 0x0050)
786                         hardware_init(dev);
787                 return;
788         } else {
789                 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
790                 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
791                 struct sk_buff *skb;
792
793                 skb = dev_alloc_skb(pkt_len + 2);
794                 if (skb == NULL) {
795                         printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
796                                    dev->name);
797                         lp->stats.rx_dropped++;
798                         goto done;
799                 }
800                 skb->dev = dev;
801
802                 skb_reserve(skb, 2);    /* Align IP on 16 byte boundaries */
803                 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
804                 skb->protocol = eth_type_trans(skb, dev);
805                 netif_rx(skb);
806                 dev->last_rx = jiffies;
807                 lp->stats.rx_packets++;
808                 lp->stats.rx_bytes += pkt_len;
809         }
810  done:
811         write_reg(ioaddr, CMR1, CMR1_NextPkt);
812         lp->last_rx_time = jiffies;
813         return;
814 }
815
816 static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
817 {
818
819         if (data_mode <= 3) { /* Mode 0 or 1 */
820                 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
821                 outb(length == 8  ?  RdAddr | HNib | MAR  :  RdAddr | MAR,
822                          ioaddr + PAR_DATA);
823                 if (data_mode <= 1) { /* Mode 0 or 1 */
824                         do  *p++ = read_byte_mode0(ioaddr);  while (--length > 0);
825                 } else  /* Mode 2 or 3 */
826                         do  *p++ = read_byte_mode2(ioaddr);  while (--length > 0);
827         } else if (data_mode <= 5)
828                 do      *p++ = read_byte_mode4(ioaddr);  while (--length > 0);
829         else
830                 do      *p++ = read_byte_mode6(ioaddr);  while (--length > 0);
831
832     outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
833         outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
834 }
835
836 /* The inverse routine to net_open(). */
837 static int
838 net_close(struct net_device *dev)
839 {
840         struct net_local *lp = netdev_priv(dev);
841         long ioaddr = dev->base_addr;
842
843         netif_stop_queue(dev);
844
845         del_timer_sync(&lp->timer);
846
847         /* Flush the Tx and disable Rx here. */
848         lp->addr_mode = CMR2h_OFF;
849         write_reg_high(ioaddr, CMR2, CMR2h_OFF);
850
851         /* Free the IRQ line. */
852         outb(0x00, ioaddr + PAR_CONTROL);
853         free_irq(dev->irq, dev);
854
855         /* Reset the ethernet hardware and activate the printer pass-through. */
856         write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
857         return 0;
858 }
859
860 /* Get the current statistics.  This may be called with the card open or
861    closed. */
862 static struct net_device_stats *
863 net_get_stats(struct net_device *dev)
864 {
865         struct net_local *lp = netdev_priv(dev);
866         return &lp->stats;
867 }
868
869 /*
870  *      Set or clear the multicast filter for this adapter.
871  */
872
873 static void set_rx_mode_8002(struct net_device *dev)
874 {
875         struct net_local *lp = netdev_priv(dev);
876         long ioaddr = dev->base_addr;
877
878         if ( dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC))) {
879                 /* We must make the kernel realise we had to move
880                  *      into promisc mode or we start all out war on
881                  *      the cable. - AC
882                  */
883                 dev->flags|=IFF_PROMISC;
884                 lp->addr_mode = CMR2h_PROMISC;
885         } else
886                 lp->addr_mode = CMR2h_Normal;
887         write_reg_high(ioaddr, CMR2, lp->addr_mode);
888 }
889
890 static void set_rx_mode_8012(struct net_device *dev)
891 {
892         struct net_local *lp = netdev_priv(dev);
893         long ioaddr = dev->base_addr;
894         unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
895         int i;
896
897         if (dev->flags & IFF_PROMISC) {                 /* Set promiscuous. */
898                 new_mode = CMR2h_PROMISC;
899         } else if ((dev->mc_count > 1000)  ||  (dev->flags & IFF_ALLMULTI)) {
900                 /* Too many to filter perfectly -- accept all multicasts. */
901                 memset(mc_filter, 0xff, sizeof(mc_filter));
902                 new_mode = CMR2h_Normal;
903         } else {
904                 struct dev_mc_list *mclist;
905
906                 memset(mc_filter, 0, sizeof(mc_filter));
907                 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
908                          i++, mclist = mclist->next)
909                 {
910                         int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
911                         mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
912                 }
913                 new_mode = CMR2h_Normal;
914         }
915         lp->addr_mode = new_mode;
916     write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
917     for (i = 0; i < 8; i++)
918                 write_reg_byte(ioaddr, i, mc_filter[i]);
919         if (net_debug > 2 || 1) {
920                 lp->addr_mode = 1;
921                 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
922                            dev->name, lp->addr_mode);
923                 for (i = 0; i < 8; i++)
924                         printk(" %2.2x", mc_filter[i]);
925                 printk(".\n");
926         }
927
928         write_reg_high(ioaddr, CMR2, lp->addr_mode);
929     write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
930 }
931
932 static int __init atp_init_module(void) {
933         if (debug)                                      /* Emit version even if no cards detected. */
934                 printk(KERN_INFO "%s" KERN_INFO "%s", versionA, versionB);
935         return atp_init();
936 }
937
938 static void __exit atp_cleanup_module(void) {
939         struct net_device *next_dev;
940
941         while (root_atp_dev) {
942                 next_dev = ((struct net_local *)root_atp_dev->priv)->next_module;
943                 unregister_netdev(root_atp_dev);
944                 /* No need to release_region(), since we never snarf it. */
945                 free_netdev(root_atp_dev);
946                 root_atp_dev = next_dev;
947         }
948 }
949
950 module_init(atp_init_module);
951 module_exit(atp_cleanup_module);