blob: 1e945551c144e6b720c833febc903ec2860296da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 3c507.c: An EtherLink16 device driver for Linux. */
2/*
3 Written 1993,1994 by Donald Becker.
4
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
7
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
10
11 The author may be reached as becker@scyld.com, or C/O
12 Scyld Computing Corporation
13 410 Severn Ave., Suite 210
14 Annapolis MD 21403
15
16
17 Thanks go to jennings@Montrouge.SMR.slb.com ( Patrick Jennings)
18 and jrs@world.std.com (Rick Sladkey) for testing and bugfixes.
19 Mark Salazar <leslie@access.digex.net> made the changes for cards with
20 only 16K packet buffers.
21
22 Things remaining to do:
23 Verify that the tx and rx buffers don't have fencepost errors.
24 Move the theory of operation and memory map documentation.
25 The statistics need to be updated correctly.
26*/
27
28#define DRV_NAME "3c507"
29#define DRV_VERSION "1.10a"
30#define DRV_RELDATE "11/17/2001"
31
32static const char version[] =
33 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Donald Becker (becker@scyld.com)\n";
34
35/*
36 Sources:
37 This driver wouldn't have been written with the availability of the
38 Crynwr driver source code. It provided a known-working implementation
39 that filled in the gaping holes of the Intel documentation. Three cheers
40 for Russ Nelson.
41
42 Intel Microcommunications Databook, Vol. 1, 1990. It provides just enough
43 info that the casual reader might think that it documents the i82586 :-<.
44*/
45
46#include <linux/module.h>
47#include <linux/kernel.h>
48#include <linux/types.h>
49#include <linux/fcntl.h>
50#include <linux/interrupt.h>
51#include <linux/ioport.h>
52#include <linux/in.h>
53#include <linux/string.h>
54#include <linux/spinlock.h>
55#include <linux/ethtool.h>
56#include <linux/errno.h>
57#include <linux/netdevice.h>
58#include <linux/etherdevice.h>
Daniel Drake1f044932009-12-24 08:11:24 +000059#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/init.h>
62#include <linux/bitops.h>
63
64#include <asm/dma.h>
65#include <asm/io.h>
66#include <asm/system.h>
67#include <asm/uaccess.h>
68
69/* use 0 for production, 1 for verification, 2..7 for debug */
70#ifndef NET_DEBUG
71#define NET_DEBUG 1
72#endif
73static unsigned int net_debug = NET_DEBUG;
74#define debug net_debug
75
76
77/*
78 Details of the i82586.
79
80 You'll really need the databook to understand the details of this part,
81 but the outline is that the i82586 has two separate processing units.
82 Both are started from a list of three configuration tables, of which only
83 the last, the System Control Block (SCB), is used after reset-time. The SCB
84 has the following fields:
85 Status word
86 Command word
87 Tx/Command block addr.
88 Rx block addr.
89 The command word accepts the following controls for the Tx and Rx units:
90 */
91
92#define CUC_START 0x0100
93#define CUC_RESUME 0x0200
94#define CUC_SUSPEND 0x0300
95#define RX_START 0x0010
96#define RX_RESUME 0x0020
97#define RX_SUSPEND 0x0030
98
99/* The Rx unit uses a list of frame descriptors and a list of data buffer
100 descriptors. We use full-sized (1518 byte) data buffers, so there is
101 a one-to-one pairing of frame descriptors to buffer descriptors.
102
103 The Tx ("command") unit executes a list of commands that look like:
104 Status word Written by the 82586 when the command is done.
105 Command word Command in lower 3 bits, post-command action in upper 3
106 Link word The address of the next command.
107 Parameters (as needed).
108
109 Some definitions related to the Command Word are:
110 */
111#define CMD_EOL 0x8000 /* The last command of the list, stop. */
112#define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
113#define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
114
115enum commands {
116 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
117 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
118
119/* Information that need to be kept for each board. */
120struct net_local {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int last_restart;
122 ushort rx_head;
123 ushort rx_tail;
124 ushort tx_head;
125 ushort tx_cmd_link;
126 ushort tx_reap;
127 ushort tx_pkts_in_ring;
128 spinlock_t lock;
129 void __iomem *base;
130};
131
132/*
133 Details of the EtherLink16 Implementation
134 The 3c507 is a generic shared-memory i82586 implementation.
135 The host can map 16K, 32K, 48K, or 64K of the 64K memory into
136 0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
137 */
138
139/* Offsets from the base I/O address. */
140#define SA_DATA 0 /* Station address data, or 3Com signature. */
141#define MISC_CTRL 6 /* Switch the SA_DATA banks, and bus config bits. */
142#define RESET_IRQ 10 /* Reset the latched IRQ line. */
143#define SIGNAL_CA 11 /* Frob the 82586 Channel Attention line. */
144#define ROM_CONFIG 13
145#define MEM_CONFIG 14
146#define IRQ_CONFIG 15
147#define EL16_IO_EXTENT 16
148
149/* The ID port is used at boot-time to locate the ethercard. */
150#define ID_PORT 0x100
151
152/* Offsets to registers in the mailbox (SCB). */
153#define iSCB_STATUS 0x8
154#define iSCB_CMD 0xA
155#define iSCB_CBL 0xC /* Command BLock offset. */
156#define iSCB_RFA 0xE /* Rx Frame Area offset. */
157
158/* Since the 3c507 maps the shared memory window so that the last byte is
159 at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
160 48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
161 We can account for this be setting the 'SBC Base' entry in the ISCP table
162 below for all the 16 bit offset addresses, and also adding the 'SCB Base'
163 value to all 24 bit physical addresses (in the SCP table and the TX and RX
164 Buffer Descriptors).
165 -Mark
166 */
167#define SCB_BASE ((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
168
169/*
170 What follows in 'init_words[]' is the "program" that is downloaded to the
171 82586 memory. It's mostly tables and command blocks, and starts at the
172 reset address 0xfffff6. This is designed to be similar to the EtherExpress,
173 thus the unusual location of the SCB at 0x0008.
174
175 Even with the additional "don't care" values, doing it this way takes less
176 program space than initializing the individual tables, and I feel it's much
177 cleaner.
178
179 The databook is particularly useless for the first two structures, I had
180 to use the Crynwr driver as an example.
181
182 The memory setup is as follows:
183 */
184
185#define CONFIG_CMD 0x0018
186#define SET_SA_CMD 0x0024
187#define SA_OFFSET 0x002A
188#define IDLELOOP 0x30
189#define TDR_CMD 0x38
190#define TDR_TIME 0x3C
191#define DUMP_CMD 0x40
192#define DIAG_CMD 0x48
193#define SET_MC_CMD 0x4E
194#define DUMP_DATA 0x56 /* A 170 byte buffer for dump and Set-MC into. */
195
196#define TX_BUF_START 0x0100
197#define NUM_TX_BUFS 5
198#define TX_BUF_SIZE (1518+14+20+16) /* packet+header+TBD */
199
200#define RX_BUF_START 0x2000
201#define RX_BUF_SIZE (1518+14+18) /* packet+header+RBD */
202#define RX_BUF_END (dev->mem_end - dev->mem_start)
203
Eric Dumazetc63fdf42010-11-03 22:49:35 +0000204#define TX_TIMEOUT (HZ/20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/*
207 That's it: only 86 bytes to set up the beast, including every extra
208 command available. The 170 byte buffer at DUMP_DATA is shared between the
209 Dump command (called only by the diagnostic program) and the SetMulticastList
210 command.
211
212 To complete the memory setup you only have to write the station address at
213 SA_OFFSET and create the Tx & Rx buffer lists.
214
215 The Tx command chain and buffer list is setup as follows:
216 A Tx command table, with the data buffer pointing to...
217 A Tx data buffer descriptor. The packet is in a single buffer, rather than
218 chaining together several smaller buffers.
219 A NoOp command, which initially points to itself,
220 And the packet data.
221
222 A transmit is done by filling in the Tx command table and data buffer,
223 re-writing the NoOp command, and finally changing the offset of the last
224 command to point to the current Tx command. When the Tx command is finished,
225 it jumps to the NoOp, when it loops until the next Tx command changes the
226 "link offset" in the NoOp. This way the 82586 never has to go through the
227 slow restart sequence.
228
229 The Rx buffer list is set up in the obvious ring structure. We have enough
230 memory (and low enough interrupt latency) that we can avoid the complicated
231 Rx buffer linked lists by alway associating a full-size Rx data buffer with
232 each Rx data frame.
233
234 I current use four transmit buffers starting at TX_BUF_START (0x0100), and
235 use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
236
237 */
238
239static unsigned short init_words[] = {
240 /* System Configuration Pointer (SCP). */
241 0x0000, /* Set bus size to 16 bits. */
242 0,0, /* pad words. */
243 0x0000,0x0000, /* ISCP phys addr, set in init_82586_mem(). */
244
245 /* Intermediate System Configuration Pointer (ISCP). */
246 0x0001, /* Status word that's cleared when init is done. */
247 0x0008,0,0, /* SCB offset, (skip, skip) */
248
249 /* System Control Block (SCB). */
250 0,0xf000|RX_START|CUC_START, /* SCB status and cmd. */
251 CONFIG_CMD, /* Command list pointer, points to Configure. */
252 RX_BUF_START, /* Rx block list. */
253 0,0,0,0, /* Error count: CRC, align, buffer, overrun. */
254
255 /* 0x0018: Configure command. Change to put MAC data with packet. */
256 0, CmdConfigure, /* Status, command. */
257 SET_SA_CMD, /* Next command is Set Station Addr. */
258 0x0804, /* "4" bytes of config data, 8 byte FIFO. */
259 0x2e40, /* Magic values, including MAC data location. */
260 0, /* Unused pad word. */
261
262 /* 0x0024: Setup station address command. */
263 0, CmdSASetup,
264 SET_MC_CMD, /* Next command. */
265 0xaa00,0xb000,0x0bad, /* Station address (to be filled in) */
266
267 /* 0x0030: NOP, looping back to itself. Point to first Tx buffer to Tx. */
268 0, CmdNOp, IDLELOOP, 0 /* pad */,
269
270 /* 0x0038: A unused Time-Domain Reflectometer command. */
271 0, CmdTDR, IDLELOOP, 0,
272
273 /* 0x0040: An unused Dump State command. */
274 0, CmdDump, IDLELOOP, DUMP_DATA,
275
276 /* 0x0048: An unused Diagnose command. */
277 0, CmdDiagnose, IDLELOOP,
278
279 /* 0x004E: An empty set-multicast-list command. */
280 0, CmdMulticastList, IDLELOOP, 0,
281};
282
283/* Index to functions, as function prototypes. */
284
285static int el16_probe1(struct net_device *dev, int ioaddr);
286static int el16_open(struct net_device *dev);
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000287static netdev_tx_t el16_send_packet(struct sk_buff *skb,
288 struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +0100289static irqreturn_t el16_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static void el16_rx(struct net_device *dev);
291static int el16_close(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static void el16_tx_timeout (struct net_device *dev);
293
294static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad);
295static void init_82586_mem(struct net_device *dev);
Jeff Garzik7282d492006-09-13 14:30:00 -0400296static const struct ethtool_ops netdev_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static void init_rx_bufs(struct net_device *);
298
299static int io = 0x300;
300static int irq;
301static int mem_start;
302
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/* Check for a network adaptor of this type, and return '0' iff one exists.
305 If dev->base_addr == 0, probe all likely locations.
306 If dev->base_addr == 1, always return failure.
307 If dev->base_addr == 2, (detachable devices only) allocate space for the
308 device and return success.
309 */
310
311struct net_device * __init el16_probe(int unit)
312{
313 struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
Joe Perchesb6bc7652010-12-21 02:16:08 -0800314 static const unsigned ports[] = { 0x300, 0x320, 0x340, 0x280, 0};
315 const unsigned *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int err = -ENODEV;
317
318 if (!dev)
319 return ERR_PTR(-ENODEV);
320
321 if (unit >= 0) {
322 sprintf(dev->name, "eth%d", unit);
323 netdev_boot_setup_check(dev);
324 io = dev->base_addr;
325 irq = dev->irq;
326 mem_start = dev->mem_start & 15;
327 }
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (io > 0x1ff) /* Check a single specified location. */
330 err = el16_probe1(dev, io);
331 else if (io != 0)
332 err = -ENXIO; /* Don't probe at all. */
333 else {
334 for (port = ports; *port; port++) {
335 err = el16_probe1(dev, *port);
336 if (!err)
337 break;
338 }
339 }
340
341 if (err)
342 goto out;
343 err = register_netdev(dev);
344 if (err)
345 goto out1;
346 return dev;
347out1:
348 free_irq(dev->irq, dev);
349 iounmap(((struct net_local *)netdev_priv(dev))->base);
350 release_region(dev->base_addr, EL16_IO_EXTENT);
351out:
352 free_netdev(dev);
353 return ERR_PTR(err);
354}
355
Stephen Hemminger1722de52009-01-09 13:01:13 +0000356static const struct net_device_ops netdev_ops = {
357 .ndo_open = el16_open,
358 .ndo_stop = el16_close,
359 .ndo_start_xmit = el16_send_packet,
360 .ndo_tx_timeout = el16_tx_timeout,
361 .ndo_change_mtu = eth_change_mtu,
362 .ndo_set_mac_address = eth_mac_addr,
363 .ndo_validate_addr = eth_validate_addr,
364};
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static int __init el16_probe1(struct net_device *dev, int ioaddr)
367{
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000368 static unsigned char init_ID_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int i, irq, irqval, retval;
370 struct net_local *lp;
371
372 if (init_ID_done == 0) {
373 ushort lrs_state = 0xff;
374 /* Send the ID sequence to the ID_PORT to enable the board(s). */
375 outb(0x00, ID_PORT);
376 for(i = 0; i < 255; i++) {
377 outb(lrs_state, ID_PORT);
378 lrs_state <<= 1;
379 if (lrs_state & 0x100)
380 lrs_state ^= 0xe7;
381 }
382 outb(0x00, ID_PORT);
383 init_ID_done = 1;
384 }
385
386 if (!request_region(ioaddr, EL16_IO_EXTENT, DRV_NAME))
387 return -ENODEV;
388
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400389 if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
391 retval = -ENODEV;
392 goto out;
393 }
394
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000395 pr_info("%s: 3c507 at %#x,", dev->name, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* We should make a few more checks here, like the first three octets of
398 the S.A. for the manufacturer's code. */
399
400 irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
401
Joe Perchesa0607fd2009-11-18 23:29:17 -0800402 irqval = request_irq(irq, el16_interrupt, 0, DRV_NAME, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (irqval) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000404 pr_cont("\n");
405 pr_err("3c507: unable to get IRQ %d (irqval=%d).\n", irq, irqval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 retval = -EAGAIN;
407 goto out;
408 }
409
410 /* We've committed to using the board, and can start filling in *dev. */
411 dev->base_addr = ioaddr;
412
413 outb(0x01, ioaddr + MISC_CTRL);
Joe Perches0795af52007-10-03 17:59:30 -0700414 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 dev->dev_addr[i] = inb(ioaddr + i);
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000416 pr_cont(" %pM", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 if (mem_start)
419 net_debug = mem_start & 7;
420
421#ifdef MEM_BASE
422 dev->mem_start = MEM_BASE;
423 dev->mem_end = dev->mem_start + 0x10000;
424#else
425 {
426 int base;
427 int size;
428 char mem_config = inb(ioaddr + MEM_CONFIG);
429 if (mem_config & 0x20) {
430 size = 64*1024;
431 base = 0xf00000 + (mem_config & 0x08 ? 0x080000
432 : ((mem_config & 3) << 17));
433 } else {
434 size = ((mem_config & 3) + 1) << 14;
435 base = 0x0c0000 + ( (mem_config & 0x18) << 12);
436 }
437 dev->mem_start = base;
438 dev->mem_end = base + size;
439 }
440#endif
441
442 dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
443 dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
444
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000445 pr_cont(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
447
448 if (net_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000449 pr_debug("%s", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 spin_lock_init(&lp->lock);
453 lp->base = ioremap(dev->mem_start, RX_BUF_END);
454 if (!lp->base) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000455 pr_err("3c507: unable to remap memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 retval = -EAGAIN;
457 goto out1;
458 }
459
Stephen Hemminger1722de52009-01-09 13:01:13 +0000460 dev->netdev_ops = &netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 dev->watchdog_timeo = TX_TIMEOUT;
462 dev->ethtool_ops = &netdev_ethtool_ops;
463 dev->flags &= ~IFF_MULTICAST; /* Multicast doesn't work */
464 return 0;
465out1:
466 free_irq(dev->irq, dev);
467out:
468 release_region(ioaddr, EL16_IO_EXTENT);
469 return retval;
470}
471
472static int el16_open(struct net_device *dev)
473{
474 /* Initialize the 82586 memory and start it. */
475 init_82586_mem(dev);
476
477 netif_start_queue(dev);
478 return 0;
479}
480
481
482static void el16_tx_timeout (struct net_device *dev)
483{
484 struct net_local *lp = netdev_priv(dev);
485 int ioaddr = dev->base_addr;
486 void __iomem *shmem = lp->base;
487
488 if (net_debug > 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000489 pr_debug("%s: transmit timed out, %s? ", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 readw(shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
491 "network cable problem");
492 /* Try to restart the adaptor. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700493 if (lp->last_restart == dev->stats.tx_packets) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (net_debug > 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000495 pr_cont("Resetting board.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Completely reset the adaptor. */
497 init_82586_mem (dev);
498 lp->tx_pkts_in_ring = 0;
499 } else {
500 /* Issue the channel attention signal and hope it "gets better". */
501 if (net_debug > 1)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000502 pr_cont("Kicking board.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 writew(0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
504 outb (0, ioaddr + SIGNAL_CA); /* Issue channel-attn. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700505 lp->last_restart = dev->stats.tx_packets;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700507 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 netif_wake_queue (dev);
509}
510
511
Stephen Hemminger27a1de92009-08-31 19:50:54 +0000512static netdev_tx_t el16_send_packet (struct sk_buff *skb,
513 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct net_local *lp = netdev_priv(dev);
516 int ioaddr = dev->base_addr;
517 unsigned long flags;
518 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
519 unsigned char *buf = skb->data;
520
521 netif_stop_queue (dev);
522
523 spin_lock_irqsave (&lp->lock, flags);
524
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700525 dev->stats.tx_bytes += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Disable the 82586's input to the interrupt line. */
527 outb (0x80, ioaddr + MISC_CTRL);
528
529 hardware_send_packet (dev, buf, skb->len, length - skb->len);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Enable the 82586 interrupt input. */
532 outb (0x84, ioaddr + MISC_CTRL);
533
534 spin_unlock_irqrestore (&lp->lock, flags);
535
536 dev_kfree_skb (skb);
537
538 /* You might need to clean up and record Tx statistics here. */
539
Patrick McHardy6ed10652009-06-23 06:03:08 +0000540 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543/* The typical workload of the driver:
544 Handle the network interface interrupts. */
David Howells7d12e782006-10-05 14:55:46 +0100545static irqreturn_t el16_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct net_device *dev = dev_id;
548 struct net_local *lp;
549 int ioaddr, status, boguscount = 0;
550 ushort ack_cmd = 0;
551 void __iomem *shmem;
552
553 if (dev == NULL) {
Julia Lawall17d95642010-05-27 16:30:03 -0700554 pr_err("net_interrupt(): irq %d for unknown device.\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return IRQ_NONE;
556 }
557
558 ioaddr = dev->base_addr;
559 lp = netdev_priv(dev);
560 shmem = lp->base;
561
562 spin_lock(&lp->lock);
563
564 status = readw(shmem+iSCB_STATUS);
565
566 if (net_debug > 4) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000567 pr_debug("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 /* Disable the 82586's input to the interrupt line. */
571 outb(0x80, ioaddr + MISC_CTRL);
572
573 /* Reap the Tx packet buffers. */
574 while (lp->tx_pkts_in_ring) {
575 unsigned short tx_status = readw(shmem+lp->tx_reap);
576 if (!(tx_status & 0x8000)) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400577 if (net_debug > 5)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000578 pr_debug("Tx command incomplete (%#x).\n", lp->tx_reap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 }
581 /* Tx unsuccessful or some interesting status bit set. */
582 if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700583 dev->stats.tx_errors++;
584 if (tx_status & 0x0600) dev->stats.tx_carrier_errors++;
585 if (tx_status & 0x0100) dev->stats.tx_fifo_errors++;
586 if (!(tx_status & 0x0040)) dev->stats.tx_heartbeat_errors++;
587 if (tx_status & 0x0020) dev->stats.tx_aborted_errors++;
588 dev->stats.collisions += tx_status & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700590 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (net_debug > 5)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000592 pr_debug("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 lp->tx_reap += TX_BUF_SIZE;
594 if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
595 lp->tx_reap = TX_BUF_START;
596
597 lp->tx_pkts_in_ring--;
598 /* There is always more space in the Tx ring buffer now. */
599 netif_wake_queue(dev);
600
601 if (++boguscount > 10)
602 break;
603 }
604
605 if (status & 0x4000) { /* Packet received. */
606 if (net_debug > 5)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000607 pr_debug("Received packet, rx_head %04x.\n", lp->rx_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 el16_rx(dev);
609 }
610
611 /* Acknowledge the interrupt sources. */
612 ack_cmd = status & 0xf000;
613
614 if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
615 if (net_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000616 pr_debug("%s: Command unit stopped, status %04x, restarting.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 dev->name, status);
618 /* If this ever occurs we should really re-write the idle loop, reset
619 the Tx list, and do a complete restart of the command unit.
620 For now we rely on the Tx timeout if the resume doesn't work. */
621 ack_cmd |= CUC_RESUME;
622 }
623
624 if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
625 /* The Rx unit is not ready, it must be hung. Restart the receiver by
626 initializing the rx buffers, and issuing an Rx start command. */
627 if (net_debug)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000628 pr_debug("%s: Rx unit stopped, status %04x, restarting.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 dev->name, status);
630 init_rx_bufs(dev);
631 writew(RX_BUF_START,shmem+iSCB_RFA);
632 ack_cmd |= RX_START;
633 }
634
635 writew(ack_cmd,shmem+iSCB_CMD);
636 outb(0, ioaddr + SIGNAL_CA); /* Issue channel-attn. */
637
638 /* Clear the latched interrupt. */
639 outb(0, ioaddr + RESET_IRQ);
640
641 /* Enable the 82586's interrupt input. */
642 outb(0x84, ioaddr + MISC_CTRL);
643 spin_unlock(&lp->lock);
644 return IRQ_HANDLED;
645}
646
647static int el16_close(struct net_device *dev)
648{
649 struct net_local *lp = netdev_priv(dev);
650 int ioaddr = dev->base_addr;
651 void __iomem *shmem = lp->base;
652
653 netif_stop_queue(dev);
654
655 /* Flush the Tx and disable Rx. */
656 writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
657 outb(0, ioaddr + SIGNAL_CA);
658
659 /* Disable the 82586's input to the interrupt line. */
660 outb(0x80, ioaddr + MISC_CTRL);
661
662 /* We always physically use the IRQ line, so we don't do free_irq(). */
663
664 /* Update the statistics here. */
665
666 return 0;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/* Initialize the Rx-block list. */
670static void init_rx_bufs(struct net_device *dev)
671{
672 struct net_local *lp = netdev_priv(dev);
673 void __iomem *write_ptr;
674 unsigned short SCB_base = SCB_BASE;
675
676 int cur_rxbuf = lp->rx_head = RX_BUF_START;
677
678 /* Initialize each Rx frame + data buffer. */
679 do { /* While there is room for one more. */
680
681 write_ptr = lp->base + cur_rxbuf;
682
683 writew(0x0000,write_ptr); /* Status */
684 writew(0x0000,write_ptr+=2); /* Command */
685 writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2); /* Link */
686 writew(cur_rxbuf + 22,write_ptr+=2); /* Buffer offset */
687 writew(0x0000,write_ptr+=2); /* Pad for dest addr. */
688 writew(0x0000,write_ptr+=2);
689 writew(0x0000,write_ptr+=2);
690 writew(0x0000,write_ptr+=2); /* Pad for source addr. */
691 writew(0x0000,write_ptr+=2);
692 writew(0x0000,write_ptr+=2);
693 writew(0x0000,write_ptr+=2); /* Pad for protocol. */
694
695 writew(0x0000,write_ptr+=2); /* Buffer: Actual count */
696 writew(-1,write_ptr+=2); /* Buffer: Next (none). */
697 writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
698 writew(0x0000,write_ptr+=2);
699 /* Finally, the number of bytes in the buffer. */
700 writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
701
702 lp->rx_tail = cur_rxbuf;
703 cur_rxbuf += RX_BUF_SIZE;
704 } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
705
706 /* Terminate the list by setting the EOL bit, and wrap the pointer to make
707 the list a ring. */
708 write_ptr = lp->base + lp->rx_tail + 2;
709 writew(0xC000,write_ptr); /* Command, mark as last. */
710 writew(lp->rx_head,write_ptr+2); /* Link */
711}
712
713static void init_82586_mem(struct net_device *dev)
714{
715 struct net_local *lp = netdev_priv(dev);
716 short ioaddr = dev->base_addr;
717 void __iomem *shmem = lp->base;
718
719 /* Enable loopback to protect the wire while starting up,
720 and hold the 586 in reset during the memory initialization. */
721 outb(0x20, ioaddr + MISC_CTRL);
722
723 /* Fix the ISCP address and base. */
724 init_words[3] = SCB_BASE;
725 init_words[7] = SCB_BASE;
726
727 /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
728 memcpy_toio(lp->base + RX_BUF_END - 10, init_words, 10);
729
730 /* Write the words at 0x0000. */
731 memcpy_toio(lp->base, init_words + 5, sizeof(init_words) - 10);
732
733 /* Fill in the station address. */
Daniel Drake1f044932009-12-24 08:11:24 +0000734 memcpy_toio(lp->base+SA_OFFSET, dev->dev_addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* The Tx-block list is written as needed. We just set up the values. */
737 lp->tx_cmd_link = IDLELOOP + 4;
738 lp->tx_head = lp->tx_reap = TX_BUF_START;
739
740 init_rx_bufs(dev);
741
742 /* Start the 586 by releasing the reset line, but leave loopback. */
743 outb(0xA0, ioaddr + MISC_CTRL);
744
745 /* This was time consuming to track down: you need to give two channel
746 attention signals to reliably start up the i82586. */
747 outb(0, ioaddr + SIGNAL_CA);
748
749 {
750 int boguscnt = 50;
751 while (readw(shmem+iSCB_STATUS) == 0)
752 if (--boguscnt == 0) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000753 pr_warning("%s: i82586 initialization timed out with status %04x, cmd %04x.\n",
754 dev->name, readw(shmem+iSCB_STATUS), readw(shmem+iSCB_CMD));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
756 }
757 /* Issue channel-attn -- the 82586 won't start. */
758 outb(0, ioaddr + SIGNAL_CA);
759 }
760
761 /* Disable loopback and enable interrupts. */
762 outb(0x84, ioaddr + MISC_CTRL);
763 if (net_debug > 4)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000764 pr_debug("%s: Initialized 82586, status %04x.\n", dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 readw(shmem+iSCB_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad)
769{
770 struct net_local *lp = netdev_priv(dev);
771 short ioaddr = dev->base_addr;
772 ushort tx_block = lp->tx_head;
773 void __iomem *write_ptr = lp->base + tx_block;
774 static char padding[ETH_ZLEN];
775
776 /* Set the write pointer to the Tx block, and put out the header. */
777 writew(0x0000,write_ptr); /* Tx status */
778 writew(CMD_INTR|CmdTx,write_ptr+=2); /* Tx command */
779 writew(tx_block+16,write_ptr+=2); /* Next command is a NoOp. */
780 writew(tx_block+8,write_ptr+=2); /* Data Buffer offset. */
781
782 /* Output the data buffer descriptor. */
783 writew((pad + length) | 0x8000,write_ptr+=2); /* Byte count parameter. */
784 writew(-1,write_ptr+=2); /* No next data buffer. */
785 writew(tx_block+22+SCB_BASE,write_ptr+=2); /* Buffer follows the NoOp command. */
786 writew(0x0000,write_ptr+=2); /* Buffer address high bits (always zero). */
787
788 /* Output the Loop-back NoOp command. */
789 writew(0x0000,write_ptr+=2); /* Tx status */
790 writew(CmdNOp,write_ptr+=2); /* Tx command */
791 writew(tx_block+16,write_ptr+=2); /* Next is myself. */
792
793 /* Output the packet at the write pointer. */
794 memcpy_toio(write_ptr+2, buf, length);
795 if (pad)
796 memcpy_toio(write_ptr+length+2, padding, pad);
797
798 /* Set the old command link pointing to this send packet. */
799 writew(tx_block,lp->base + lp->tx_cmd_link);
800 lp->tx_cmd_link = tx_block + 20;
801
802 /* Set the next free tx region. */
803 lp->tx_head = tx_block + TX_BUF_SIZE;
804 if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
805 lp->tx_head = TX_BUF_START;
806
807 if (net_debug > 4) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000808 pr_debug("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dev->name, ioaddr, length, tx_block, lp->tx_head);
810 }
811
812 /* Grimly block further packets if there has been insufficient reaping. */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400813 if (++lp->tx_pkts_in_ring < NUM_TX_BUFS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 netif_wake_queue(dev);
815}
816
817static void el16_rx(struct net_device *dev)
818{
819 struct net_local *lp = netdev_priv(dev);
820 void __iomem *shmem = lp->base;
821 ushort rx_head = lp->rx_head;
822 ushort rx_tail = lp->rx_tail;
823 ushort boguscount = 10;
824 short frame_status;
825
826 while ((frame_status = readw(shmem+rx_head)) < 0) { /* Command complete */
827 void __iomem *read_frame = lp->base + rx_head;
828 ushort rfd_cmd = readw(read_frame+2);
829 ushort next_rx_frame = readw(read_frame+4);
830 ushort data_buffer_addr = readw(read_frame+6);
831 void __iomem *data_frame = lp->base + data_buffer_addr;
832 ushort pkt_len = readw(data_frame);
833
Joe Perches8e95a202009-12-03 07:58:21 +0000834 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22 ||
835 (pkt_len & 0xC000) != 0xC000) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000836 pr_err("%s: Rx frame at %#x corrupted, "
Jeff Garzikcba05162007-11-23 21:50:34 -0500837 "status %04x cmd %04x next %04x "
838 "data-buf @%04x %04x.\n",
839 dev->name, rx_head, frame_status, rfd_cmd,
840 next_rx_frame, data_buffer_addr, pkt_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 } else if ((frame_status & 0x2000) == 0) {
842 /* Frame Rxed, but with error. */
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700843 dev->stats.rx_errors++;
844 if (frame_status & 0x0800) dev->stats.rx_crc_errors++;
845 if (frame_status & 0x0400) dev->stats.rx_frame_errors++;
846 if (frame_status & 0x0200) dev->stats.rx_fifo_errors++;
847 if (frame_status & 0x0100) dev->stats.rx_over_errors++;
848 if (frame_status & 0x0080) dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 } else {
850 /* Malloc up new buffer. */
851 struct sk_buff *skb;
852
853 pkt_len &= 0x3fff;
854 skb = dev_alloc_skb(pkt_len+2);
855 if (skb == NULL) {
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000856 pr_err("%s: Memory squeeze, dropping packet.\n",
Jeff Garzikcba05162007-11-23 21:50:34 -0500857 dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700858 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 }
861
862 skb_reserve(skb,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* 'skb->data' points to the start of sk_buff data area. */
865 memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
866
867 skb->protocol=eth_type_trans(skb,dev);
868 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700869 dev->stats.rx_packets++;
870 dev->stats.rx_bytes += pkt_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872
873 /* Clear the status word and set End-of-List on the rx frame. */
874 writew(0,read_frame);
875 writew(0xC000,read_frame+2);
876 /* Clear the end-of-list on the prev. RFD. */
877 writew(0x0000,lp->base + rx_tail + 2);
878
879 rx_tail = rx_head;
880 rx_head = next_rx_frame;
881 if (--boguscount == 0)
882 break;
883 }
884
885 lp->rx_head = rx_head;
886 lp->rx_tail = rx_tail;
887}
888
889static void netdev_get_drvinfo(struct net_device *dev,
890 struct ethtool_drvinfo *info)
891{
892 strcpy(info->driver, DRV_NAME);
893 strcpy(info->version, DRV_VERSION);
894 sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
895}
896
897static u32 netdev_get_msglevel(struct net_device *dev)
898{
899 return debug;
900}
901
902static void netdev_set_msglevel(struct net_device *dev, u32 level)
903{
904 debug = level;
905}
906
Jeff Garzik7282d492006-09-13 14:30:00 -0400907static const struct ethtool_ops netdev_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .get_drvinfo = netdev_get_drvinfo,
909 .get_msglevel = netdev_get_msglevel,
910 .set_msglevel = netdev_set_msglevel,
911};
912
913#ifdef MODULE
914static struct net_device *dev_3c507;
915module_param(io, int, 0);
916module_param(irq, int, 0);
917MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
918MODULE_PARM_DESC(irq, "(ignored)");
919
Randy Dunlap96e672c2006-06-10 13:33:48 -0700920int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 if (io == 0)
Alexander Beregalov646cdb32009-05-26 12:35:25 +0000923 pr_notice("3c507: You should not use auto-probing with insmod!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 dev_3c507 = el16_probe(-1);
925 return IS_ERR(dev_3c507) ? PTR_ERR(dev_3c507) : 0;
926}
927
Al Viroafc8eb42006-06-14 18:50:53 -0400928void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929cleanup_module(void)
930{
931 struct net_device *dev = dev_3c507;
932 unregister_netdev(dev);
933 free_irq(dev->irq, dev);
934 iounmap(((struct net_local *)netdev_priv(dev))->base);
935 release_region(dev->base_addr, EL16_IO_EXTENT);
936 free_netdev(dev);
937}
938#endif /* MODULE */
939MODULE_LICENSE("GPL");