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