More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / 3c507.c
blob3a7f8187c9647bed05d9690282a1393666b03fbe
1 /* 3c507.c: An EtherLink16 device driver for Linux. */
2 /*
3 Written 1993,1994 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
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
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.
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.
28 #define DRV_NAME "3c507"
29 #define DRV_VERSION "1.10a"
30 #define DRV_RELDATE "11/17/2001"
32 static const char version[] =
33 DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " Donald Becker (becker@scyld.com)\n";
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.
42 Intel Microcommunications Databook, Vol. 1, 1990. It provides just enough
43 info that the casual reader might think that it documents the i82586 :-<.
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>
59 #include <linux/skbuff.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
63 #include <asm/bitops.h>
64 #include <asm/dma.h>
65 #include <asm/io.h>
66 #include <asm/system.h>
67 #include <asm/uaccess.h>
69 /* use 0 for production, 1 for verification, 2..7 for debug */
70 #ifndef NET_DEBUG
71 #define NET_DEBUG 1
72 #endif
73 static unsigned int net_debug = NET_DEBUG;
74 #define debug net_debug
77 /* A zero-terminated list of common I/O addresses to be probed. */
78 static unsigned int netcard_portlist[] __initdata =
79 { 0x300, 0x320, 0x340, 0x280, 0};
82 Details of the i82586.
84 You'll really need the databook to understand the details of this part,
85 but the outline is that the i82586 has two separate processing units.
86 Both are started from a list of three configuration tables, of which only
87 the last, the System Control Block (SCB), is used after reset-time. The SCB
88 has the following fields:
89 Status word
90 Command word
91 Tx/Command block addr.
92 Rx block addr.
93 The command word accepts the following controls for the Tx and Rx units:
96 #define CUC_START 0x0100
97 #define CUC_RESUME 0x0200
98 #define CUC_SUSPEND 0x0300
99 #define RX_START 0x0010
100 #define RX_RESUME 0x0020
101 #define RX_SUSPEND 0x0030
103 /* The Rx unit uses a list of frame descriptors and a list of data buffer
104 descriptors. We use full-sized (1518 byte) data buffers, so there is
105 a one-to-one pairing of frame descriptors to buffer descriptors.
107 The Tx ("command") unit executes a list of commands that look like:
108 Status word Written by the 82586 when the command is done.
109 Command word Command in lower 3 bits, post-command action in upper 3
110 Link word The address of the next command.
111 Parameters (as needed).
113 Some definitions related to the Command Word are:
115 #define CMD_EOL 0x8000 /* The last command of the list, stop. */
116 #define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
117 #define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
119 enum commands {
120 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
121 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
123 /* Information that need to be kept for each board. */
124 struct net_local {
125 struct net_device_stats stats;
126 int last_restart;
127 ushort rx_head;
128 ushort rx_tail;
129 ushort tx_head;
130 ushort tx_cmd_link;
131 ushort tx_reap;
132 ushort tx_pkts_in_ring;
133 spinlock_t lock;
137 Details of the EtherLink16 Implementation
138 The 3c507 is a generic shared-memory i82586 implementation.
139 The host can map 16K, 32K, 48K, or 64K of the 64K memory into
140 0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
143 /* Offsets from the base I/O address. */
144 #define SA_DATA 0 /* Station address data, or 3Com signature. */
145 #define MISC_CTRL 6 /* Switch the SA_DATA banks, and bus config bits. */
146 #define RESET_IRQ 10 /* Reset the latched IRQ line. */
147 #define SIGNAL_CA 11 /* Frob the 82586 Channel Attention line. */
148 #define ROM_CONFIG 13
149 #define MEM_CONFIG 14
150 #define IRQ_CONFIG 15
151 #define EL16_IO_EXTENT 16
153 /* The ID port is used at boot-time to locate the ethercard. */
154 #define ID_PORT 0x100
156 /* Offsets to registers in the mailbox (SCB). */
157 #define iSCB_STATUS 0x8
158 #define iSCB_CMD 0xA
159 #define iSCB_CBL 0xC /* Command BLock offset. */
160 #define iSCB_RFA 0xE /* Rx Frame Area offset. */
162 /* Since the 3c507 maps the shared memory window so that the last byte is
163 at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
164 48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
165 We can account for this be setting the 'SBC Base' entry in the ISCP table
166 below for all the 16 bit offset addresses, and also adding the 'SCB Base'
167 value to all 24 bit physical addresses (in the SCP table and the TX and RX
168 Buffer Descriptors).
169 -Mark
171 #define SCB_BASE ((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
174 What follows in 'init_words[]' is the "program" that is downloaded to the
175 82586 memory. It's mostly tables and command blocks, and starts at the
176 reset address 0xfffff6. This is designed to be similar to the EtherExpress,
177 thus the unusual location of the SCB at 0x0008.
179 Even with the additional "don't care" values, doing it this way takes less
180 program space than initializing the individual tables, and I feel it's much
181 cleaner.
183 The databook is particularly useless for the first two structures, I had
184 to use the Crynwr driver as an example.
186 The memory setup is as follows:
189 #define CONFIG_CMD 0x0018
190 #define SET_SA_CMD 0x0024
191 #define SA_OFFSET 0x002A
192 #define IDLELOOP 0x30
193 #define TDR_CMD 0x38
194 #define TDR_TIME 0x3C
195 #define DUMP_CMD 0x40
196 #define DIAG_CMD 0x48
197 #define SET_MC_CMD 0x4E
198 #define DUMP_DATA 0x56 /* A 170 byte buffer for dump and Set-MC into. */
200 #define TX_BUF_START 0x0100
201 #define NUM_TX_BUFS 5
202 #define TX_BUF_SIZE (1518+14+20+16) /* packet+header+TBD */
204 #define RX_BUF_START 0x2000
205 #define RX_BUF_SIZE (1518+14+18) /* packet+header+RBD */
206 #define RX_BUF_END (dev->mem_end - dev->mem_start)
208 #define TX_TIMEOUT 5
211 That's it: only 86 bytes to set up the beast, including every extra
212 command available. The 170 byte buffer at DUMP_DATA is shared between the
213 Dump command (called only by the diagnostic program) and the SetMulticastList
214 command.
216 To complete the memory setup you only have to write the station address at
217 SA_OFFSET and create the Tx & Rx buffer lists.
219 The Tx command chain and buffer list is setup as follows:
220 A Tx command table, with the data buffer pointing to...
221 A Tx data buffer descriptor. The packet is in a single buffer, rather than
222 chaining together several smaller buffers.
223 A NoOp command, which initially points to itself,
224 And the packet data.
226 A transmit is done by filling in the Tx command table and data buffer,
227 re-writing the NoOp command, and finally changing the offset of the last
228 command to point to the current Tx command. When the Tx command is finished,
229 it jumps to the NoOp, when it loops until the next Tx command changes the
230 "link offset" in the NoOp. This way the 82586 never has to go through the
231 slow restart sequence.
233 The Rx buffer list is set up in the obvious ring structure. We have enough
234 memory (and low enough interrupt latency) that we can avoid the complicated
235 Rx buffer linked lists by alway associating a full-size Rx data buffer with
236 each Rx data frame.
238 I current use four transmit buffers starting at TX_BUF_START (0x0100), and
239 use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
243 static unsigned short init_words[] = {
244 /* System Configuration Pointer (SCP). */
245 0x0000, /* Set bus size to 16 bits. */
246 0,0, /* pad words. */
247 0x0000,0x0000, /* ISCP phys addr, set in init_82586_mem(). */
249 /* Intermediate System Configuration Pointer (ISCP). */
250 0x0001, /* Status word that's cleared when init is done. */
251 0x0008,0,0, /* SCB offset, (skip, skip) */
253 /* System Control Block (SCB). */
254 0,0xf000|RX_START|CUC_START, /* SCB status and cmd. */
255 CONFIG_CMD, /* Command list pointer, points to Configure. */
256 RX_BUF_START, /* Rx block list. */
257 0,0,0,0, /* Error count: CRC, align, buffer, overrun. */
259 /* 0x0018: Configure command. Change to put MAC data with packet. */
260 0, CmdConfigure, /* Status, command. */
261 SET_SA_CMD, /* Next command is Set Station Addr. */
262 0x0804, /* "4" bytes of config data, 8 byte FIFO. */
263 0x2e40, /* Magic values, including MAC data location. */
264 0, /* Unused pad word. */
266 /* 0x0024: Setup station address command. */
267 0, CmdSASetup,
268 SET_MC_CMD, /* Next command. */
269 0xaa00,0xb000,0x0bad, /* Station address (to be filled in) */
271 /* 0x0030: NOP, looping back to itself. Point to first Tx buffer to Tx. */
272 0, CmdNOp, IDLELOOP, 0 /* pad */,
274 /* 0x0038: A unused Time-Domain Reflectometer command. */
275 0, CmdTDR, IDLELOOP, 0,
277 /* 0x0040: An unused Dump State command. */
278 0, CmdDump, IDLELOOP, DUMP_DATA,
280 /* 0x0048: An unused Diagnose command. */
281 0, CmdDiagnose, IDLELOOP,
283 /* 0x004E: An empty set-multicast-list command. */
284 0, CmdMulticastList, IDLELOOP, 0,
287 /* Index to functions, as function prototypes. */
289 extern int el16_probe(struct net_device *dev); /* Called from Space.c */
291 static int el16_probe1(struct net_device *dev, int ioaddr);
292 static int el16_open(struct net_device *dev);
293 static int el16_send_packet(struct sk_buff *skb, struct net_device *dev);
294 static irqreturn_t el16_interrupt(int irq, void *dev_id, struct pt_regs *regs);
295 static void el16_rx(struct net_device *dev);
296 static int el16_close(struct net_device *dev);
297 static struct net_device_stats *el16_get_stats(struct net_device *dev);
298 static void el16_tx_timeout (struct net_device *dev);
300 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad);
301 static void init_82586_mem(struct net_device *dev);
302 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
305 /* Check for a network adaptor of this type, and return '0' iff one exists.
306 If dev->base_addr == 0, probe all likely locations.
307 If dev->base_addr == 1, always return failure.
308 If dev->base_addr == 2, (detachable devices only) allocate space for the
309 device and return success.
312 int __init el16_probe(struct net_device *dev)
314 int base_addr = dev->base_addr;
315 int i;
317 SET_MODULE_OWNER(dev);
319 if (base_addr > 0x1ff) /* Check a single specified location. */
320 return el16_probe1(dev, base_addr);
321 else if (base_addr != 0)
322 return -ENXIO; /* Don't probe at all. */
324 for (i = 0; netcard_portlist[i]; i++)
325 if (el16_probe1(dev, netcard_portlist[i]) == 0)
326 return 0;
328 return -ENODEV;
331 static int __init el16_probe1(struct net_device *dev, int ioaddr)
333 static unsigned char init_ID_done, version_printed;
334 int i, irq, irqval, retval;
335 struct net_local *lp;
337 if (init_ID_done == 0) {
338 ushort lrs_state = 0xff;
339 /* Send the ID sequence to the ID_PORT to enable the board(s). */
340 outb(0x00, ID_PORT);
341 for(i = 0; i < 255; i++) {
342 outb(lrs_state, ID_PORT);
343 lrs_state <<= 1;
344 if (lrs_state & 0x100)
345 lrs_state ^= 0xe7;
347 outb(0x00, ID_PORT);
348 init_ID_done = 1;
351 if (!request_region(ioaddr, EL16_IO_EXTENT, dev->name))
352 return -ENODEV;
354 if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') ||
355 (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
356 retval = -ENODEV;
357 goto out;
360 if (net_debug && version_printed++ == 0)
361 printk(version);
363 printk("%s: 3c507 at %#x,", dev->name, ioaddr);
365 /* We should make a few more checks here, like the first three octets of
366 the S.A. for the manufacturer's code. */
368 irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
370 irqval = request_irq(irq, &el16_interrupt, 0, dev->name, dev);
371 if (irqval) {
372 printk ("unable to get IRQ %d (irqval=%d).\n", irq, irqval);
373 retval = -EAGAIN;
374 goto out;
377 /* We've committed to using the board, and can start filling in *dev. */
378 dev->base_addr = ioaddr;
380 outb(0x01, ioaddr + MISC_CTRL);
381 for (i = 0; i < 6; i++) {
382 dev->dev_addr[i] = inb(ioaddr + i);
383 printk(" %02x", dev->dev_addr[i]);
386 if ((dev->mem_start & 0xf) > 0)
387 net_debug = dev->mem_start & 7;
389 #ifdef MEM_BASE
390 dev->mem_start = MEM_BASE;
391 dev->mem_end = dev->mem_start + 0x10000;
392 #else
394 int base;
395 int size;
396 char mem_config = inb(ioaddr + MEM_CONFIG);
397 if (mem_config & 0x20) {
398 size = 64*1024;
399 base = 0xf00000 + (mem_config & 0x08 ? 0x080000
400 : ((mem_config & 3) << 17));
401 } else {
402 size = ((mem_config & 3) + 1) << 14;
403 base = 0x0c0000 + ( (mem_config & 0x18) << 12);
405 dev->mem_start = base;
406 dev->mem_end = base + size;
408 #endif
410 dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
411 dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
413 printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
414 dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
416 if (net_debug)
417 printk(version);
419 /* Initialize the device structure. */
420 lp = dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
421 if (dev->priv == NULL) {
422 retval = -ENOMEM;
423 goto out;
425 memset(dev->priv, 0, sizeof(struct net_local));
426 spin_lock_init(&lp->lock);
428 dev->open = el16_open;
429 dev->stop = el16_close;
430 dev->hard_start_xmit = el16_send_packet;
431 dev->get_stats = el16_get_stats;
432 dev->tx_timeout = el16_tx_timeout;
433 dev->watchdog_timeo = TX_TIMEOUT;
434 dev->do_ioctl = netdev_ioctl;
436 ether_setup(dev); /* Generic ethernet behaviour */
438 dev->flags&=~IFF_MULTICAST; /* Multicast doesn't work */
440 return 0;
441 out:
442 release_region(ioaddr, EL16_IO_EXTENT);
443 return retval;
446 static int el16_open(struct net_device *dev)
448 /* Initialize the 82586 memory and start it. */
449 init_82586_mem(dev);
451 netif_start_queue(dev);
452 return 0;
456 static void el16_tx_timeout (struct net_device *dev)
458 struct net_local *lp = (struct net_local *) dev->priv;
459 int ioaddr = dev->base_addr;
460 unsigned long shmem = dev->mem_start;
462 if (net_debug > 1)
463 printk ("%s: transmit timed out, %s? ", dev->name,
464 isa_readw (shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
465 "network cable problem");
466 /* Try to restart the adaptor. */
467 if (lp->last_restart == lp->stats.tx_packets) {
468 if (net_debug > 1)
469 printk ("Resetting board.\n");
470 /* Completely reset the adaptor. */
471 init_82586_mem (dev);
472 lp->tx_pkts_in_ring = 0;
473 } else {
474 /* Issue the channel attention signal and hope it "gets better". */
475 if (net_debug > 1)
476 printk ("Kicking board.\n");
477 isa_writew (0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
478 outb (0, ioaddr + SIGNAL_CA); /* Issue channel-attn. */
479 lp->last_restart = lp->stats.tx_packets;
481 dev->trans_start = jiffies;
482 netif_wake_queue (dev);
486 static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
488 struct net_local *lp = (struct net_local *) dev->priv;
489 int ioaddr = dev->base_addr;
490 unsigned long flags;
491 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
492 unsigned char *buf = skb->data;
494 netif_stop_queue (dev);
496 spin_lock_irqsave (&lp->lock, flags);
498 lp->stats.tx_bytes += length;
499 /* Disable the 82586's input to the interrupt line. */
500 outb (0x80, ioaddr + MISC_CTRL);
502 hardware_send_packet (dev, buf, skb->len, length - skb->len);
504 dev->trans_start = jiffies;
505 /* Enable the 82586 interrupt input. */
506 outb (0x84, ioaddr + MISC_CTRL);
508 spin_unlock_irqrestore (&lp->lock, flags);
510 dev_kfree_skb (skb);
512 /* You might need to clean up and record Tx statistics here. */
514 return 0;
517 /* The typical workload of the driver:
518 Handle the network interface interrupts. */
519 static irqreturn_t el16_interrupt(int irq, void *dev_id, struct pt_regs *regs)
521 struct net_device *dev = dev_id;
522 struct net_local *lp;
523 int ioaddr, status, boguscount = 0;
524 ushort ack_cmd = 0;
525 unsigned long shmem;
527 if (dev == NULL) {
528 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
529 return IRQ_NONE;
532 ioaddr = dev->base_addr;
533 lp = (struct net_local *)dev->priv;
534 shmem = dev->mem_start;
536 spin_lock(&lp->lock);
538 status = isa_readw(shmem+iSCB_STATUS);
540 if (net_debug > 4) {
541 printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
544 /* Disable the 82586's input to the interrupt line. */
545 outb(0x80, ioaddr + MISC_CTRL);
547 /* Reap the Tx packet buffers. */
548 while (lp->tx_pkts_in_ring) {
549 unsigned short tx_status = isa_readw(shmem+lp->tx_reap);
550 if (!(tx_status & 0x8000)) {
551 if (net_debug > 5)
552 printk("Tx command incomplete (%#x).\n", lp->tx_reap);
553 break;
555 /* Tx unsuccessful or some interesting status bit set. */
556 if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
557 lp->stats.tx_errors++;
558 if (tx_status & 0x0600) lp->stats.tx_carrier_errors++;
559 if (tx_status & 0x0100) lp->stats.tx_fifo_errors++;
560 if (!(tx_status & 0x0040)) lp->stats.tx_heartbeat_errors++;
561 if (tx_status & 0x0020) lp->stats.tx_aborted_errors++;
562 lp->stats.collisions += tx_status & 0xf;
564 lp->stats.tx_packets++;
565 if (net_debug > 5)
566 printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
567 lp->tx_reap += TX_BUF_SIZE;
568 if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
569 lp->tx_reap = TX_BUF_START;
571 lp->tx_pkts_in_ring--;
572 /* There is always more space in the Tx ring buffer now. */
573 netif_wake_queue(dev);
575 if (++boguscount > 10)
576 break;
579 if (status & 0x4000) { /* Packet received. */
580 if (net_debug > 5)
581 printk("Received packet, rx_head %04x.\n", lp->rx_head);
582 el16_rx(dev);
585 /* Acknowledge the interrupt sources. */
586 ack_cmd = status & 0xf000;
588 if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
589 if (net_debug)
590 printk("%s: Command unit stopped, status %04x, restarting.\n",
591 dev->name, status);
592 /* If this ever occurs we should really re-write the idle loop, reset
593 the Tx list, and do a complete restart of the command unit.
594 For now we rely on the Tx timeout if the resume doesn't work. */
595 ack_cmd |= CUC_RESUME;
598 if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
599 static void init_rx_bufs(struct net_device *);
600 /* The Rx unit is not ready, it must be hung. Restart the receiver by
601 initializing the rx buffers, and issuing an Rx start command. */
602 if (net_debug)
603 printk("%s: Rx unit stopped, status %04x, restarting.\n",
604 dev->name, status);
605 init_rx_bufs(dev);
606 isa_writew(RX_BUF_START,shmem+iSCB_RFA);
607 ack_cmd |= RX_START;
610 isa_writew(ack_cmd,shmem+iSCB_CMD);
611 outb(0, ioaddr + SIGNAL_CA); /* Issue channel-attn. */
613 /* Clear the latched interrupt. */
614 outb(0, ioaddr + RESET_IRQ);
616 /* Enable the 82586's interrupt input. */
617 outb(0x84, ioaddr + MISC_CTRL);
618 spin_unlock(&lp->lock);
619 return IRQ_HANDLED;
622 static int el16_close(struct net_device *dev)
624 int ioaddr = dev->base_addr;
625 unsigned long shmem = dev->mem_start;
627 netif_stop_queue(dev);
629 /* Flush the Tx and disable Rx. */
630 isa_writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
631 outb(0, ioaddr + SIGNAL_CA);
633 /* Disable the 82586's input to the interrupt line. */
634 outb(0x80, ioaddr + MISC_CTRL);
636 /* We always physically use the IRQ line, so we don't do free_irq(). */
638 /* Update the statistics here. */
640 return 0;
643 /* Get the current statistics. This may be called with the card open or
644 closed. */
645 static struct net_device_stats *el16_get_stats(struct net_device *dev)
647 struct net_local *lp = (struct net_local *)dev->priv;
649 /* ToDo: decide if there are any useful statistics from the SCB. */
651 return &lp->stats;
654 /* Initialize the Rx-block list. */
655 static void init_rx_bufs(struct net_device *dev)
657 struct net_local *lp = (struct net_local *)dev->priv;
658 unsigned long write_ptr;
659 unsigned short SCB_base = SCB_BASE;
661 int cur_rxbuf = lp->rx_head = RX_BUF_START;
663 /* Initialize each Rx frame + data buffer. */
664 do { /* While there is room for one more. */
666 write_ptr = dev->mem_start + cur_rxbuf;
668 isa_writew(0x0000,write_ptr); /* Status */
669 isa_writew(0x0000,write_ptr+=2); /* Command */
670 isa_writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2); /* Link */
671 isa_writew(cur_rxbuf + 22,write_ptr+=2); /* Buffer offset */
672 isa_writew(0x0000,write_ptr+=2); /* Pad for dest addr. */
673 isa_writew(0x0000,write_ptr+=2);
674 isa_writew(0x0000,write_ptr+=2);
675 isa_writew(0x0000,write_ptr+=2); /* Pad for source addr. */
676 isa_writew(0x0000,write_ptr+=2);
677 isa_writew(0x0000,write_ptr+=2);
678 isa_writew(0x0000,write_ptr+=2); /* Pad for protocol. */
680 isa_writew(0x0000,write_ptr+=2); /* Buffer: Actual count */
681 isa_writew(-1,write_ptr+=2); /* Buffer: Next (none). */
682 isa_writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
683 isa_writew(0x0000,write_ptr+=2);
684 /* Finally, the number of bytes in the buffer. */
685 isa_writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
687 lp->rx_tail = cur_rxbuf;
688 cur_rxbuf += RX_BUF_SIZE;
689 } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
691 /* Terminate the list by setting the EOL bit, and wrap the pointer to make
692 the list a ring. */
693 write_ptr = dev->mem_start + lp->rx_tail + 2;
694 isa_writew(0xC000,write_ptr); /* Command, mark as last. */
695 isa_writew(lp->rx_head,write_ptr+2); /* Link */
698 static void init_82586_mem(struct net_device *dev)
700 struct net_local *lp = (struct net_local *)dev->priv;
701 short ioaddr = dev->base_addr;
702 unsigned long shmem = dev->mem_start;
704 /* Enable loopback to protect the wire while starting up,
705 and hold the 586 in reset during the memory initialization. */
706 outb(0x20, ioaddr + MISC_CTRL);
708 /* Fix the ISCP address and base. */
709 init_words[3] = SCB_BASE;
710 init_words[7] = SCB_BASE;
712 /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
713 isa_memcpy_toio(dev->mem_end-10, init_words, 10);
715 /* Write the words at 0x0000. */
716 isa_memcpy_toio(dev->mem_start, init_words + 5, sizeof(init_words) - 10);
718 /* Fill in the station address. */
719 isa_memcpy_toio(dev->mem_start+SA_OFFSET, dev->dev_addr,
720 sizeof(dev->dev_addr));
722 /* The Tx-block list is written as needed. We just set up the values. */
723 lp->tx_cmd_link = IDLELOOP + 4;
724 lp->tx_head = lp->tx_reap = TX_BUF_START;
726 init_rx_bufs(dev);
728 /* Start the 586 by releasing the reset line, but leave loopback. */
729 outb(0xA0, ioaddr + MISC_CTRL);
731 /* This was time consuming to track down: you need to give two channel
732 attention signals to reliably start up the i82586. */
733 outb(0, ioaddr + SIGNAL_CA);
736 int boguscnt = 50;
737 while (isa_readw(shmem+iSCB_STATUS) == 0)
738 if (--boguscnt == 0) {
739 printk("%s: i82586 initialization timed out with status %04x,"
740 "cmd %04x.\n", dev->name,
741 isa_readw(shmem+iSCB_STATUS), isa_readw(shmem+iSCB_CMD));
742 break;
744 /* Issue channel-attn -- the 82586 won't start. */
745 outb(0, ioaddr + SIGNAL_CA);
748 /* Disable loopback and enable interrupts. */
749 outb(0x84, ioaddr + MISC_CTRL);
750 if (net_debug > 4)
751 printk("%s: Initialized 82586, status %04x.\n", dev->name,
752 isa_readw(shmem+iSCB_STATUS));
753 return;
756 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad)
758 struct net_local *lp = (struct net_local *)dev->priv;
759 short ioaddr = dev->base_addr;
760 ushort tx_block = lp->tx_head;
761 unsigned long write_ptr = dev->mem_start + tx_block;
762 static char padding[ETH_ZLEN];
764 /* Set the write pointer to the Tx block, and put out the header. */
765 isa_writew(0x0000,write_ptr); /* Tx status */
766 isa_writew(CMD_INTR|CmdTx,write_ptr+=2); /* Tx command */
767 isa_writew(tx_block+16,write_ptr+=2); /* Next command is a NoOp. */
768 isa_writew(tx_block+8,write_ptr+=2); /* Data Buffer offset. */
770 /* Output the data buffer descriptor. */
771 isa_writew((pad + length) | 0x8000,write_ptr+=2); /* Byte count parameter. */
772 isa_writew(-1,write_ptr+=2); /* No next data buffer. */
773 isa_writew(tx_block+22+SCB_BASE,write_ptr+=2); /* Buffer follows the NoOp command. */
774 isa_writew(0x0000,write_ptr+=2); /* Buffer address high bits (always zero). */
776 /* Output the Loop-back NoOp command. */
777 isa_writew(0x0000,write_ptr+=2); /* Tx status */
778 isa_writew(CmdNOp,write_ptr+=2); /* Tx command */
779 isa_writew(tx_block+16,write_ptr+=2); /* Next is myself. */
781 /* Output the packet at the write pointer. */
782 isa_memcpy_toio(write_ptr+2, buf, length);
783 if (pad)
784 isa_memcpy_toio(write_ptr+length+2, padding, pad);
786 /* Set the old command link pointing to this send packet. */
787 isa_writew(tx_block,dev->mem_start + lp->tx_cmd_link);
788 lp->tx_cmd_link = tx_block + 20;
790 /* Set the next free tx region. */
791 lp->tx_head = tx_block + TX_BUF_SIZE;
792 if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
793 lp->tx_head = TX_BUF_START;
795 if (net_debug > 4) {
796 printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
797 dev->name, ioaddr, length, tx_block, lp->tx_head);
800 /* Grimly block further packets if there has been insufficient reaping. */
801 if (++lp->tx_pkts_in_ring < NUM_TX_BUFS)
802 netif_wake_queue(dev);
805 static void el16_rx(struct net_device *dev)
807 struct net_local *lp = (struct net_local *)dev->priv;
808 unsigned long shmem = dev->mem_start;
809 ushort rx_head = lp->rx_head;
810 ushort rx_tail = lp->rx_tail;
811 ushort boguscount = 10;
812 short frame_status;
814 while ((frame_status = isa_readw(shmem+rx_head)) < 0) { /* Command complete */
815 unsigned long read_frame = dev->mem_start + rx_head;
816 ushort rfd_cmd = isa_readw(read_frame+2);
817 ushort next_rx_frame = isa_readw(read_frame+4);
818 ushort data_buffer_addr = isa_readw(read_frame+6);
819 unsigned long data_frame = dev->mem_start + data_buffer_addr;
820 ushort pkt_len = isa_readw(data_frame);
822 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
823 || (pkt_len & 0xC000) != 0xC000) {
824 printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
825 "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
826 frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
827 pkt_len);
828 } else if ((frame_status & 0x2000) == 0) {
829 /* Frame Rxed, but with error. */
830 lp->stats.rx_errors++;
831 if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
832 if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
833 if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
834 if (frame_status & 0x0100) lp->stats.rx_over_errors++;
835 if (frame_status & 0x0080) lp->stats.rx_length_errors++;
836 } else {
837 /* Malloc up new buffer. */
838 struct sk_buff *skb;
840 pkt_len &= 0x3fff;
841 skb = dev_alloc_skb(pkt_len+2);
842 if (skb == NULL) {
843 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
844 lp->stats.rx_dropped++;
845 break;
848 skb_reserve(skb,2);
849 skb->dev = dev;
851 /* 'skb->data' points to the start of sk_buff data area. */
852 isa_memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
854 skb->protocol=eth_type_trans(skb,dev);
855 netif_rx(skb);
856 dev->last_rx = jiffies;
857 lp->stats.rx_packets++;
858 lp->stats.rx_bytes += pkt_len;
861 /* Clear the status word and set End-of-List on the rx frame. */
862 isa_writew(0,read_frame);
863 isa_writew(0xC000,read_frame+2);
864 /* Clear the end-of-list on the prev. RFD. */
865 isa_writew(0x0000,dev->mem_start + rx_tail + 2);
867 rx_tail = rx_head;
868 rx_head = next_rx_frame;
869 if (--boguscount == 0)
870 break;
873 lp->rx_head = rx_head;
874 lp->rx_tail = rx_tail;
878 * netdev_ethtool_ioctl: Handle network interface SIOCETHTOOL ioctls
879 * @dev: network interface on which out-of-band action is to be performed
880 * @useraddr: userspace address to which data is to be read and returned
882 * Process the various commands of the SIOCETHTOOL interface.
885 static int netdev_ethtool_ioctl (struct net_device *dev, void *useraddr)
887 u32 ethcmd;
889 /* dev_ioctl() in ../../net/core/dev.c has already checked
890 capable(CAP_NET_ADMIN), so don't bother with that here. */
892 if (get_user(ethcmd, (u32 *)useraddr))
893 return -EFAULT;
895 switch (ethcmd) {
897 case ETHTOOL_GDRVINFO: {
898 struct ethtool_drvinfo info = { ETHTOOL_GDRVINFO };
899 strcpy (info.driver, DRV_NAME);
900 strcpy (info.version, DRV_VERSION);
901 sprintf(info.bus_info, "ISA 0x%lx", dev->base_addr);
902 if (copy_to_user (useraddr, &info, sizeof (info)))
903 return -EFAULT;
904 return 0;
907 /* get message-level */
908 case ETHTOOL_GMSGLVL: {
909 struct ethtool_value edata = {ETHTOOL_GMSGLVL};
910 edata.data = debug;
911 if (copy_to_user(useraddr, &edata, sizeof(edata)))
912 return -EFAULT;
913 return 0;
915 /* set message-level */
916 case ETHTOOL_SMSGLVL: {
917 struct ethtool_value edata;
918 if (copy_from_user(&edata, useraddr, sizeof(edata)))
919 return -EFAULT;
920 debug = edata.data;
921 return 0;
924 default:
925 break;
928 return -EOPNOTSUPP;
932 * netdev_ioctl: Handle network interface ioctls
933 * @dev: network interface on which out-of-band action is to be performed
934 * @rq: user request data
935 * @cmd: command issued by user
937 * Process the various out-of-band ioctls passed to this driver.
940 static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
942 int rc = 0;
944 switch (cmd) {
945 case SIOCETHTOOL:
946 rc = netdev_ethtool_ioctl(dev, (void *) rq->ifr_data);
947 break;
949 default:
950 rc = -EOPNOTSUPP;
951 break;
954 return rc;
958 #ifdef MODULE
959 static struct net_device dev_3c507;
960 static int io = 0x300;
961 static int irq;
962 MODULE_PARM(io, "i");
963 MODULE_PARM(irq, "i");
964 MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
965 MODULE_PARM_DESC(irq, "(ignored)");
967 int init_module(void)
969 if (io == 0)
970 printk("3c507: You should not use auto-probing with insmod!\n");
971 dev_3c507.base_addr = io;
972 dev_3c507.irq = irq;
973 dev_3c507.init = el16_probe;
974 if (register_netdev(&dev_3c507) != 0) {
975 printk("3c507: register_netdev() returned non-zero.\n");
976 return -EIO;
978 return 0;
981 void
982 cleanup_module(void)
984 unregister_netdev(&dev_3c507);
985 kfree(dev_3c507.priv);
986 dev_3c507.priv = NULL;
988 /* If we don't do this, we can't re-insmod it later. */
989 free_irq(dev_3c507.irq, &dev_3c507);
990 release_region(dev_3c507.base_addr, EL16_IO_EXTENT);
992 #endif /* MODULE */
993 MODULE_LICENSE("GPL");
997 * Local variables:
998 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -I/usr/src/linux/drivers/net -Wall -Wstrict-prototypes -O6 -m486 -c 3c507.c"
999 * version-control: t
1000 * kept-new-versions: 5
1001 * tab-width: 4
1002 * c-indent-level: 4
1003 * End: