blob: 001791775be3d0c871071ab1a85998cf38edd455 [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);
Stephen Hemminger613573252009-08-31 19:50:58 +0000202static netdev_tx_t atp_send_packet(struct sk_buff *skb,
203 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100204static irqreturn_t atp_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void net_rx(struct net_device *dev);
206static void read_block(long ioaddr, int length, unsigned char *buffer, int data_mode);
207static int net_close(struct net_device *dev);
Stephen Hemminger3a1a27f2009-01-07 18:10:24 -0800208static void set_rx_mode(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static 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
Stephen Hemminger3a1a27f2009-01-07 18:10:24 -0800245static const struct net_device_ops atp_netdev_ops = {
246 .ndo_open = net_open,
247 .ndo_stop = net_close,
248 .ndo_start_xmit = atp_send_packet,
249 .ndo_set_multicast_list = set_rx_mode,
250 .ndo_tx_timeout = tx_timeout,
251 .ndo_change_mtu = eth_change_mtu,
252 .ndo_set_mac_address = eth_mac_addr,
253 .ndo_validate_addr = eth_validate_addr,
254};
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int __init atp_probe1(long ioaddr)
257{
258 struct net_device *dev = NULL;
259 struct net_local *lp;
260 int saved_ctrl_reg, status, i;
261 int res;
262
263 outb(0xff, ioaddr + PAR_DATA);
264 /* Save the original value of the Control register, in case we guessed
265 wrong. */
266 saved_ctrl_reg = inb(ioaddr + PAR_CONTROL);
267 if (net_debug > 3)
268 printk("atp: Control register was %#2.2x.\n", saved_ctrl_reg);
269 /* IRQEN=0, SLCTB=high INITB=high, AUTOFDB=high, STBB=high. */
270 outb(0x04, ioaddr + PAR_CONTROL);
271#ifndef final_version
272 if (net_debug > 3) {
273 /* Turn off the printer multiplexer on the 8012. */
274 for (i = 0; i < 8; i++)
275 outb(mux_8012[i], ioaddr + PAR_DATA);
276 write_reg(ioaddr, MODSEL, 0x00);
277 printk("atp: Registers are ");
278 for (i = 0; i < 32; i++)
279 printk(" %2.2x", read_nibble(ioaddr, i));
280 printk(".\n");
281 }
282#endif
283 /* Turn off the printer multiplexer on the 8012. */
284 for (i = 0; i < 8; i++)
285 outb(mux_8012[i], ioaddr + PAR_DATA);
286 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
287 /* udelay() here? */
288 status = read_nibble(ioaddr, CMR1);
289
290 if (net_debug > 3) {
291 printk(KERN_DEBUG "atp: Status nibble was %#2.2x..", status);
292 for (i = 0; i < 32; i++)
293 printk(" %2.2x", read_nibble(ioaddr, i));
294 printk("\n");
295 }
296
297 if ((status & 0x78) != 0x08) {
298 /* The pocket adapter probe failed, restore the control register. */
299 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
300 return -ENODEV;
301 }
302 status = read_nibble(ioaddr, CMR2_h);
303 if ((status & 0x78) != 0x10) {
304 outb(saved_ctrl_reg, ioaddr + PAR_CONTROL);
305 return -ENODEV;
306 }
307
308 dev = alloc_etherdev(sizeof(struct net_local));
309 if (!dev)
310 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Find the IRQ used by triggering an interrupt. */
313 write_reg_byte(ioaddr, CMR2, 0x01); /* No accept mode, IRQ out. */
314 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE); /* Enable Tx and Rx. */
315
316 /* Omit autoIRQ routine for now. Use "table lookup" instead. Uhgggh. */
317 if (irq[0])
318 dev->irq = irq[0];
319 else if (ioaddr == 0x378)
320 dev->irq = 7;
321 else
322 dev->irq = 5;
323 write_reg_high(ioaddr, CMR1, CMR1h_TxRxOFF); /* Disable Tx and Rx units. */
324 write_reg(ioaddr, CMR2, CMR2_NULL);
325
326 dev->base_addr = ioaddr;
327
328 /* Read the station address PROM. */
329 get_node_ID(dev);
330
331#ifndef MODULE
332 if (net_debug)
Markus Dahms2c2a8c52007-05-09 07:58:10 +0200333 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#endif
335
Joe Perches0795af52007-10-03 17:59:30 -0700336 printk(KERN_NOTICE "%s: Pocket adapter found at %#3lx, IRQ %d, "
Johannes Berge1749612008-10-27 15:59:26 -0700337 "SAPROM %pM.\n",
338 dev->name, dev->base_addr, dev->irq, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Reset the ethernet hardware and activate the printer pass-through. */
341 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
342
343 lp = netdev_priv(dev);
344 lp->chip_type = RTL8002;
345 lp->addr_mode = CMR2h_Normal;
346 spin_lock_init(&lp->lock);
347
348 /* For the ATP adapter the "if_port" is really the data transfer mode. */
349 if (xcvr[0])
350 dev->if_port = xcvr[0];
351 else
352 dev->if_port = (dev->mem_start & 0xf) ? (dev->mem_start & 0x7) : 4;
353 if (dev->mem_end & 0xf)
354 net_debug = dev->mem_end & 7;
355
Stephen Hemminger3a1a27f2009-01-07 18:10:24 -0800356 dev->netdev_ops = &atp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dev->watchdog_timeo = TX_TIMEOUT;
358
359 res = register_netdev(dev);
360 if (res) {
361 free_netdev(dev);
362 return res;
363 }
364
365 lp->next_module = root_atp_dev;
366 root_atp_dev = dev;
367
368 return 0;
369}
370
371/* Read the station address PROM, usually a word-wide EEPROM. */
372static void __init get_node_ID(struct net_device *dev)
373{
374 long ioaddr = dev->base_addr;
375 int sa_offset = 0;
376 int i;
377
378 write_reg(ioaddr, CMR2, CMR2_EEPROM); /* Point to the EEPROM control registers. */
379
380 /* Some adapters have the station address at offset 15 instead of offset
381 zero. Check for it, and fix it if needed. */
382 if (eeprom_op(ioaddr, EE_READ(0)) == 0xffff)
383 sa_offset = 15;
384
385 for (i = 0; i < 3; i++)
Al Viroeca1ad82008-03-16 22:21:54 +0000386 ((__be16 *)dev->dev_addr)[i] =
387 cpu_to_be16(eeprom_op(ioaddr, EE_READ(sa_offset + i)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 write_reg(ioaddr, CMR2, CMR2_NULL);
390}
391
392/*
393 An EEPROM read command starts by shifting out 0x60+address, and then
394 shifting in the serial data. See the NatSemi databook for details.
395 * ________________
396 * CS : __|
397 * ___ ___
398 * CLK: ______| |___| |
399 * __ _______ _______
400 * DI : __X_______X_______X
401 * DO : _________X_______X
402 */
403
404static unsigned short __init eeprom_op(long ioaddr, u32 cmd)
405{
406 unsigned eedata_out = 0;
407 int num_bits = EE_CMD_SIZE;
408
409 while (--num_bits >= 0) {
410 char outval = (cmd & (1<<num_bits)) ? EE_DATA_WRITE : 0;
411 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_LOW);
412 write_reg_high(ioaddr, PROM_CMD, outval | EE_CLK_HIGH);
413 eedata_out <<= 1;
414 if (read_nibble(ioaddr, PROM_DATA) & EE_DATA_READ)
415 eedata_out++;
416 }
417 write_reg_high(ioaddr, PROM_CMD, EE_CLK_LOW & ~EE_CS);
418 return eedata_out;
419}
420
421
422/* Open/initialize the board. This is called (in the current kernel)
423 sometime after booting when the 'ifconfig' program is run.
424
425 This routine sets everything up anew at each open, even
426 registers that "should" only need to be set once at boot, so that
427 there is non-reboot way to recover if something goes wrong.
428
Wang Chenb74ca3a2008-12-08 01:14:16 -0800429 This is an attachable device: if there is no private entry then it wasn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 probed for at boot-time, and we need to probe for it again.
431 */
432static int net_open(struct net_device *dev)
433{
434 struct net_local *lp = netdev_priv(dev);
435 int ret;
436
437 /* The interrupt line is turned off (tri-stated) when the device isn't in
438 use. That's especially important for "attached" interfaces where the
439 port or interrupt may be shared. */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800440 ret = request_irq(dev->irq, atp_interrupt, 0, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (ret)
442 return ret;
443
444 hardware_init(dev);
445
446 init_timer(&lp->timer);
447 lp->timer.expires = jiffies + TIMED_CHECKER;
448 lp->timer.data = (unsigned long)dev;
449 lp->timer.function = &atp_timed_checker; /* timer handler */
450 add_timer(&lp->timer);
451
452 netif_start_queue(dev);
453 return 0;
454}
455
456/* This routine resets the hardware. We initialize everything, assuming that
457 the hardware may have been temporarily detached. */
458static void hardware_init(struct net_device *dev)
459{
460 struct net_local *lp = netdev_priv(dev);
461 long ioaddr = dev->base_addr;
462 int i;
463
464 /* Turn off the printer multiplexer on the 8012. */
465 for (i = 0; i < 8; i++)
466 outb(mux_8012[i], ioaddr + PAR_DATA);
467 write_reg_high(ioaddr, CMR1, CMR1h_RESET);
468
469 for (i = 0; i < 6; i++)
470 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
471
472 write_reg_high(ioaddr, CMR2, lp->addr_mode);
473
474 if (net_debug > 2) {
475 printk(KERN_DEBUG "%s: Reset: current Rx mode %d.\n", dev->name,
476 (read_nibble(ioaddr, CMR2_h) >> 3) & 0x0f);
477 }
478
479 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
480 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
481
482 /* Enable the interrupt line from the serial port. */
483 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
484
485 /* Unmask the interesting interrupts. */
486 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
487 write_reg_high(ioaddr, IMR, ISRh_RxErr);
488
489 lp->tx_unit_busy = 0;
490 lp->pac_cnt_in_tx_buf = 0;
491 lp->saved_tx_size = 0;
492}
493
494static void trigger_send(long ioaddr, int length)
495{
496 write_reg_byte(ioaddr, TxCNT0, length & 0xff);
497 write_reg(ioaddr, TxCNT1, length >> 8);
498 write_reg(ioaddr, CMR1, CMR1_Xmit);
499}
500
501static void write_packet(long ioaddr, int length, unsigned char *packet, int pad_len, int data_mode)
502{
503 if (length & 1)
504 {
505 length++;
506 pad_len++;
507 }
508
509 outb(EOC+MAR, ioaddr + PAR_DATA);
510 if ((data_mode & 1) == 0) {
511 /* Write the packet out, starting with the write addr. */
512 outb(WrAddr+MAR, ioaddr + PAR_DATA);
513 do {
514 write_byte_mode0(ioaddr, *packet++);
515 } while (--length > pad_len) ;
516 do {
517 write_byte_mode0(ioaddr, 0);
518 } while (--length > 0) ;
519 } else {
520 /* Write the packet out in slow mode. */
521 unsigned char outbyte = *packet++;
522
523 outb(Ctrl_LNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
524 outb(WrAddr+MAR, ioaddr + PAR_DATA);
525
526 outb((outbyte & 0x0f)|0x40, ioaddr + PAR_DATA);
527 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
528 outbyte >>= 4;
529 outb(outbyte & 0x0f, ioaddr + PAR_DATA);
530 outb(Ctrl_HNibWrite + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
531 while (--length > pad_len)
532 write_byte_mode1(ioaddr, *packet++);
533 while (--length > 0)
534 write_byte_mode1(ioaddr, 0);
535 }
536 /* Terminate the Tx frame. End of write: ECB. */
537 outb(0xff, ioaddr + PAR_DATA);
538 outb(Ctrl_HNibWrite | Ctrl_SelData | Ctrl_IRQEN, ioaddr + PAR_CONTROL);
539}
540
541static void tx_timeout(struct net_device *dev)
542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 long ioaddr = dev->base_addr;
544
545 printk(KERN_WARNING "%s: Transmit timed out, %s?\n", dev->name,
546 inb(ioaddr + PAR_CONTROL) & 0x10 ? "network cable problem"
547 : "IRQ conflict");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700548 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* Try to restart the adapter. */
550 hardware_init(dev);
551 dev->trans_start = jiffies;
552 netif_wake_queue(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700553 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Stephen Hemminger613573252009-08-31 19:50:58 +0000556static netdev_tx_t atp_send_packet(struct sk_buff *skb,
557 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct net_local *lp = netdev_priv(dev);
560 long ioaddr = dev->base_addr;
561 int length;
562 unsigned long flags;
563
564 length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
565
566 netif_stop_queue(dev);
567
568 /* Disable interrupts by writing 0x00 to the Interrupt Mask Register.
569 This sequence must not be interrupted by an incoming packet. */
570
571 spin_lock_irqsave(&lp->lock, flags);
572 write_reg(ioaddr, IMR, 0);
573 write_reg_high(ioaddr, IMR, 0);
574 spin_unlock_irqrestore(&lp->lock, flags);
575
576 write_packet(ioaddr, length, skb->data, length-skb->len, dev->if_port);
577
578 lp->pac_cnt_in_tx_buf++;
579 if (lp->tx_unit_busy == 0) {
580 trigger_send(ioaddr, length);
581 lp->saved_tx_size = 0; /* Redundant */
582 lp->re_tx = 0;
583 lp->tx_unit_busy = 1;
584 } else
585 lp->saved_tx_size = length;
586 /* Re-enable the LPT interrupts. */
587 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
588 write_reg_high(ioaddr, IMR, ISRh_RxErr);
589
590 dev->trans_start = jiffies;
591 dev_kfree_skb (skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000592 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595
596/* The typical workload of the driver:
597 Handle the network interface interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100598static irqreturn_t atp_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400600 struct net_device *dev = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct net_local *lp;
602 long ioaddr;
603 static int num_tx_since_rx;
604 int boguscount = max_interrupt_work;
605 int handled = 0;
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ioaddr = dev->base_addr;
608 lp = netdev_priv(dev);
609
610 spin_lock(&lp->lock);
611
612 /* Disable additional spurious interrupts. */
613 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
614
615 /* The adapter's output is currently the IRQ line, switch it to data. */
616 write_reg(ioaddr, CMR2, CMR2_NULL);
617 write_reg(ioaddr, IMR, 0);
618
619 if (net_debug > 5) printk(KERN_DEBUG "%s: In interrupt ", dev->name);
620 while (--boguscount > 0) {
621 int status = read_nibble(ioaddr, ISR);
622 if (net_debug > 5) printk("loop status %02x..", status);
623
624 if (status & (ISR_RxOK<<3)) {
625 handled = 1;
626 write_reg(ioaddr, ISR, ISR_RxOK); /* Clear the Rx interrupt. */
627 do {
628 int read_status = read_nibble(ioaddr, CMR1);
629 if (net_debug > 6)
630 printk("handling Rx packet %02x..", read_status);
631 /* We acknowledged the normal Rx interrupt, so if the interrupt
632 is still outstanding we must have a Rx error. */
633 if (read_status & (CMR1_IRQ << 3)) { /* Overrun. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700634 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Set to no-accept mode long enough to remove a packet. */
636 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
637 net_rx(dev);
638 /* Clear the interrupt and return to normal Rx mode. */
639 write_reg_high(ioaddr, ISR, ISRh_RxErr);
640 write_reg_high(ioaddr, CMR2, lp->addr_mode);
641 } else if ((read_status & (CMR1_BufEnb << 3)) == 0) {
642 net_rx(dev);
643 num_tx_since_rx = 0;
644 } else
645 break;
646 } while (--boguscount > 0);
647 } else if (status & ((ISR_TxErr + ISR_TxOK)<<3)) {
648 handled = 1;
649 if (net_debug > 6) printk("handling Tx done..");
650 /* Clear the Tx interrupt. We should check for too many failures
651 and reinitialize the adapter. */
652 write_reg(ioaddr, ISR, ISR_TxErr + ISR_TxOK);
653 if (status & (ISR_TxErr<<3)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700654 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (++lp->re_tx > 15) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700656 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 hardware_init(dev);
658 break;
659 }
660 /* Attempt to retransmit. */
661 if (net_debug > 6) printk("attempting to ReTx");
662 write_reg(ioaddr, CMR1, CMR1_ReXmit + CMR1_Xmit);
663 } else {
664 /* Finish up the transmit. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700665 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 lp->pac_cnt_in_tx_buf--;
667 if ( lp->saved_tx_size) {
668 trigger_send(ioaddr, lp->saved_tx_size);
669 lp->saved_tx_size = 0;
670 lp->re_tx = 0;
671 } else
672 lp->tx_unit_busy = 0;
673 netif_wake_queue(dev); /* Inform upper layers. */
674 }
675 num_tx_since_rx++;
676 } else if (num_tx_since_rx > 8
677 && time_after(jiffies, dev->last_rx + HZ)) {
678 if (net_debug > 2)
679 printk(KERN_DEBUG "%s: Missed packet? No Rx after %d Tx and "
680 "%ld jiffies status %02x CMR1 %02x.\n", dev->name,
681 num_tx_since_rx, jiffies - dev->last_rx, status,
682 (read_nibble(ioaddr, CMR1) >> 3) & 15);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700683 dev->stats.rx_missed_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 hardware_init(dev);
685 num_tx_since_rx = 0;
686 break;
687 } else
688 break;
689 }
690
691 /* This following code fixes a rare (and very difficult to track down)
692 problem where the adapter forgets its ethernet address. */
693 {
694 int i;
695 for (i = 0; i < 6; i++)
696 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
697#if 0 && defined(TIMED_CHECKER)
698 mod_timer(&lp->timer, jiffies + TIMED_CHECKER);
699#endif
700 }
701
702 /* Tell the adapter that it can go back to using the output line as IRQ. */
703 write_reg(ioaddr, CMR2, CMR2_IRQOUT);
704 /* Enable the physical interrupt line, which is sure to be low until.. */
705 outb(Ctrl_SelData + Ctrl_IRQEN, ioaddr + PAR_CONTROL);
706 /* .. we enable the interrupt sources. */
707 write_reg(ioaddr, IMR, ISR_RxOK | ISR_TxErr | ISR_TxOK);
708 write_reg_high(ioaddr, IMR, ISRh_RxErr); /* Hmmm, really needed? */
709
710 spin_unlock(&lp->lock);
711
712 if (net_debug > 5) printk("exiting interrupt.\n");
713 return IRQ_RETVAL(handled);
714}
715
716#ifdef TIMED_CHECKER
717/* This following code fixes a rare (and very difficult to track down)
718 problem where the adapter forgets its ethernet address. */
719static void atp_timed_checker(unsigned long data)
720{
721 struct net_device *dev = (struct net_device *)data;
722 long ioaddr = dev->base_addr;
723 struct net_local *lp = netdev_priv(dev);
724 int tickssofar = jiffies - lp->last_rx_time;
725 int i;
726
727 spin_lock(&lp->lock);
728 if (tickssofar > 2*HZ) {
729#if 1
730 for (i = 0; i < 6; i++)
731 write_reg_byte(ioaddr, PAR0 + i, dev->dev_addr[i]);
732 lp->last_rx_time = jiffies;
733#else
734 for (i = 0; i < 6; i++)
735 if (read_cmd_byte(ioaddr, PAR0 + i) != atp_timed_dev->dev_addr[i])
736 {
737 struct net_local *lp = netdev_priv(atp_timed_dev);
738 write_reg_byte(ioaddr, PAR0 + i, atp_timed_dev->dev_addr[i]);
739 if (i == 2)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700740 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 else if (i == 3)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700742 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 else if (i == 4)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700744 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 else
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700746 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748#endif
749 }
750 spin_unlock(&lp->lock);
751 lp->timer.expires = jiffies + TIMED_CHECKER;
752 add_timer(&lp->timer);
753}
754#endif
755
756/* We have a good packet(s), get it/them out of the buffers. */
757static void net_rx(struct net_device *dev)
758{
759 struct net_local *lp = netdev_priv(dev);
760 long ioaddr = dev->base_addr;
761 struct rx_header rx_head;
762
763 /* Process the received packet. */
764 outb(EOC+MAR, ioaddr + PAR_DATA);
765 read_block(ioaddr, 8, (unsigned char*)&rx_head, dev->if_port);
766 if (net_debug > 5)
767 printk(KERN_DEBUG " rx_count %04x %04x %04x %04x..", rx_head.pad,
768 rx_head.rx_count, rx_head.rx_status, rx_head.cur_addr);
769 if ((rx_head.rx_status & 0x77) != 0x01) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700770 dev->stats.rx_errors++;
771 if (rx_head.rx_status & 0x0004) dev->stats.rx_frame_errors++;
772 else if (rx_head.rx_status & 0x0002) dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (net_debug > 3)
774 printk(KERN_DEBUG "%s: Unknown ATP Rx error %04x.\n",
775 dev->name, rx_head.rx_status);
776 if (rx_head.rx_status & 0x0020) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700777 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 write_reg_high(ioaddr, CMR1, CMR1h_TxENABLE);
779 write_reg_high(ioaddr, CMR1, CMR1h_RxENABLE | CMR1h_TxENABLE);
780 } else if (rx_head.rx_status & 0x0050)
781 hardware_init(dev);
782 return;
783 } else {
784 /* Malloc up new buffer. The "-4" omits the FCS (CRC). */
785 int pkt_len = (rx_head.rx_count & 0x7ff) - 4;
786 struct sk_buff *skb;
787
788 skb = dev_alloc_skb(pkt_len + 2);
789 if (skb == NULL) {
790 printk(KERN_ERR "%s: Memory squeeze, dropping packet.\n",
791 dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700792 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto done;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
797 read_block(ioaddr, pkt_len, skb_put(skb,pkt_len), dev->if_port);
798 skb->protocol = eth_type_trans(skb, dev);
799 netif_rx(skb);
800 dev->last_rx = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700801 dev->stats.rx_packets++;
802 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 done:
805 write_reg(ioaddr, CMR1, CMR1_NextPkt);
806 lp->last_rx_time = jiffies;
807 return;
808}
809
810static void read_block(long ioaddr, int length, unsigned char *p, int data_mode)
811{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (data_mode <= 3) { /* Mode 0 or 1 */
813 outb(Ctrl_LNibRead, ioaddr + PAR_CONTROL);
814 outb(length == 8 ? RdAddr | HNib | MAR : RdAddr | MAR,
815 ioaddr + PAR_DATA);
816 if (data_mode <= 1) { /* Mode 0 or 1 */
Hannes Edere4c3c132008-12-25 23:55:35 -0800817 do { *p++ = read_byte_mode0(ioaddr); } while (--length > 0);
818 } else { /* Mode 2 or 3 */
819 do { *p++ = read_byte_mode2(ioaddr); } while (--length > 0);
820 }
821 } else if (data_mode <= 5) {
822 do { *p++ = read_byte_mode4(ioaddr); } while (--length > 0);
823 } else {
824 do { *p++ = read_byte_mode6(ioaddr); } while (--length > 0);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Hannes Edere4c3c132008-12-25 23:55:35 -0800827 outb(EOC+HNib+MAR, ioaddr + PAR_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 outb(Ctrl_SelData, ioaddr + PAR_CONTROL);
829}
830
831/* The inverse routine to net_open(). */
832static int
833net_close(struct net_device *dev)
834{
835 struct net_local *lp = netdev_priv(dev);
836 long ioaddr = dev->base_addr;
837
838 netif_stop_queue(dev);
839
840 del_timer_sync(&lp->timer);
841
842 /* Flush the Tx and disable Rx here. */
843 lp->addr_mode = CMR2h_OFF;
844 write_reg_high(ioaddr, CMR2, CMR2h_OFF);
845
846 /* Free the IRQ line. */
847 outb(0x00, ioaddr + PAR_CONTROL);
848 free_irq(dev->irq, dev);
849
850 /* Reset the ethernet hardware and activate the printer pass-through. */
851 write_reg_high(ioaddr, CMR1, CMR1h_RESET | CMR1h_MUX);
852 return 0;
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855/*
856 * Set or clear the multicast filter for this adapter.
857 */
858
859static void set_rx_mode_8002(struct net_device *dev)
860{
861 struct net_local *lp = netdev_priv(dev);
862 long ioaddr = dev->base_addr;
863
Wang Chenc16d1182008-07-22 13:13:12 +0800864 if (dev->mc_count > 0 || (dev->flags & (IFF_ALLMULTI|IFF_PROMISC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 lp->addr_mode = CMR2h_PROMISC;
Wang Chenc16d1182008-07-22 13:13:12 +0800866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 lp->addr_mode = CMR2h_Normal;
868 write_reg_high(ioaddr, CMR2, lp->addr_mode);
869}
870
871static void set_rx_mode_8012(struct net_device *dev)
872{
873 struct net_local *lp = netdev_priv(dev);
874 long ioaddr = dev->base_addr;
875 unsigned char new_mode, mc_filter[8]; /* Multicast hash filter */
876 int i;
877
878 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
879 new_mode = CMR2h_PROMISC;
880 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
881 /* Too many to filter perfectly -- accept all multicasts. */
882 memset(mc_filter, 0xff, sizeof(mc_filter));
883 new_mode = CMR2h_Normal;
884 } else {
885 struct dev_mc_list *mclist;
886
887 memset(mc_filter, 0, sizeof(mc_filter));
888 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
889 i++, mclist = mclist->next)
890 {
891 int filterbit = ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x3f;
892 mc_filter[filterbit >> 5] |= 1 << (filterbit & 31);
893 }
894 new_mode = CMR2h_Normal;
895 }
896 lp->addr_mode = new_mode;
897 write_reg(ioaddr, CMR2, CMR2_IRQOUT | 0x04); /* Switch to page 1. */
898 for (i = 0; i < 8; i++)
899 write_reg_byte(ioaddr, i, mc_filter[i]);
900 if (net_debug > 2 || 1) {
901 lp->addr_mode = 1;
902 printk(KERN_DEBUG "%s: Mode %d, setting multicast filter to",
903 dev->name, lp->addr_mode);
904 for (i = 0; i < 8; i++)
905 printk(" %2.2x", mc_filter[i]);
906 printk(".\n");
907 }
908
909 write_reg_high(ioaddr, CMR2, lp->addr_mode);
910 write_reg(ioaddr, CMR2, CMR2_IRQOUT); /* Switch back to page 0 */
911}
912
Stephen Hemminger3a1a27f2009-01-07 18:10:24 -0800913static void set_rx_mode(struct net_device *dev)
914{
915 struct net_local *lp = netdev_priv(dev);
916
917 if (lp->chip_type == RTL8002)
918 return set_rx_mode_8002(dev);
919 else
920 return set_rx_mode_8012(dev);
921}
922
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924static int __init atp_init_module(void) {
925 if (debug) /* Emit version even if no cards detected. */
Markus Dahms2c2a8c52007-05-09 07:58:10 +0200926 printk(KERN_INFO "%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return atp_init();
928}
929
930static void __exit atp_cleanup_module(void) {
931 struct net_device *next_dev;
932
933 while (root_atp_dev) {
Wang Chen454d7c92008-11-12 23:37:49 -0800934 struct net_local *atp_local = netdev_priv(root_atp_dev);
935 next_dev = atp_local->next_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 unregister_netdev(root_atp_dev);
937 /* No need to release_region(), since we never snarf it. */
938 free_netdev(root_atp_dev);
939 root_atp_dev = next_dev;
940 }
941}
942
943module_init(atp_init_module);
944module_exit(atp_cleanup_module);