ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / 3c507.c
1 /* 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
32 static 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>
59 #include <linux/skbuff.h>
60 #include <linux/slab.h>
61 #include <linux/init.h>
62
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>
68
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
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
115 enum 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. */
120 struct net_local {
121         struct net_device_stats stats;
122         int last_restart;
123         ushort rx_head;
124         ushort rx_tail;
125         ushort tx_head;
126         ushort tx_cmd_link;
127         ushort tx_reap;
128         ushort tx_pkts_in_ring;
129         spinlock_t lock;
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
204 #define TX_TIMEOUT 5
205
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
239 static 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
285 static int      el16_probe1(struct net_device *dev, int ioaddr);
286 static int      el16_open(struct net_device *dev);
287 static int      el16_send_packet(struct sk_buff *skb, struct net_device *dev);
288 static irqreturn_t el16_interrupt(int irq, void *dev_id, struct pt_regs *regs);
289 static void el16_rx(struct net_device *dev);
290 static int      el16_close(struct net_device *dev);
291 static struct net_device_stats *el16_get_stats(struct net_device *dev);
292 static void el16_tx_timeout (struct net_device *dev);
293
294 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad);
295 static void init_82586_mem(struct net_device *dev);
296 static struct ethtool_ops netdev_ethtool_ops;
297
298 static int io = 0x300;
299 static int irq;
300 static int mem_start;
301
302 \f
303 /* Check for a network adaptor of this type, and return '0' iff one exists.
304         If dev->base_addr == 0, probe all likely locations.
305         If dev->base_addr == 1, always return failure.
306         If dev->base_addr == 2, (detachable devices only) allocate space for the
307         device and return success.
308         */
309
310 struct net_device * __init el16_probe(int unit)
311 {
312         struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
313         static unsigned ports[] = { 0x300, 0x320, 0x340, 0x280, 0};
314         unsigned *port;
315         int err = -ENODEV;
316
317         if (!dev)
318                 return ERR_PTR(-ENODEV);
319
320         if (unit >= 0) {
321                 sprintf(dev->name, "eth%d", unit);
322                 netdev_boot_setup_check(dev);
323                 io = dev->base_addr;
324                 irq = dev->irq;
325                 mem_start = dev->mem_start & 15;
326         }
327
328         SET_MODULE_OWNER(dev);
329
330         if (io > 0x1ff)         /* Check a single specified location. */
331                 err = el16_probe1(dev, io);
332         else if (io != 0)
333                 err = -ENXIO;           /* Don't probe at all. */
334         else {
335                 for (port = ports; *port; port++) {
336                         err = el16_probe1(dev, *port);
337                         if (!err)
338                                 break;
339                 }
340         }
341
342         if (err)
343                 goto out;
344         err = register_netdev(dev);
345         if (err)
346                 goto out1;
347         return dev;
348 out1:
349         free_irq(dev->irq, dev);
350         release_region(dev->base_addr, EL16_IO_EXTENT);
351 out:
352         free_netdev(dev);
353         return ERR_PTR(err);
354 }
355
356 static int __init el16_probe1(struct net_device *dev, int ioaddr)
357 {
358         static unsigned char init_ID_done, version_printed;
359         int i, irq, irqval, retval;
360         struct net_local *lp;
361
362         if (init_ID_done == 0) {
363                 ushort lrs_state = 0xff;
364                 /* Send the ID sequence to the ID_PORT to enable the board(s). */
365                 outb(0x00, ID_PORT);
366                 for(i = 0; i < 255; i++) {
367                         outb(lrs_state, ID_PORT);
368                         lrs_state <<= 1;
369                         if (lrs_state & 0x100)
370                                 lrs_state ^= 0xe7;
371                 }
372                 outb(0x00, ID_PORT);
373                 init_ID_done = 1;
374         }
375
376         if (!request_region(ioaddr, EL16_IO_EXTENT, dev->name))
377                 return -ENODEV;
378
379         if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') || 
380             (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
381                 retval = -ENODEV;
382                 goto out;
383         }
384
385         if (net_debug  &&  version_printed++ == 0)
386                 printk(version);
387
388         printk("%s: 3c507 at %#x,", dev->name, ioaddr);
389
390         /* We should make a few more checks here, like the first three octets of
391            the S.A. for the manufacturer's code. */
392
393         irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
394
395         irqval = request_irq(irq, &el16_interrupt, 0, dev->name, dev);
396         if (irqval) {
397                 printk ("unable to get IRQ %d (irqval=%d).\n", irq, irqval);
398                 retval = -EAGAIN;
399                 goto out;
400         }
401
402         /* We've committed to using the board, and can start filling in *dev. */
403         dev->base_addr = ioaddr;
404
405         outb(0x01, ioaddr + MISC_CTRL);
406         for (i = 0; i < 6; i++) {
407                 dev->dev_addr[i] = inb(ioaddr + i);
408                 printk(" %02x", dev->dev_addr[i]);
409         }
410
411         if (mem_start)
412                 net_debug = mem_start & 7;
413
414 #ifdef MEM_BASE
415         dev->mem_start = MEM_BASE;
416         dev->mem_end = dev->mem_start + 0x10000;
417 #else
418         {
419                 int base;
420                 int size;
421                 char mem_config = inb(ioaddr + MEM_CONFIG);
422                 if (mem_config & 0x20) {
423                         size = 64*1024;
424                         base = 0xf00000 + (mem_config & 0x08 ? 0x080000
425                                                            : ((mem_config & 3) << 17));
426                 } else {
427                         size = ((mem_config & 3) + 1) << 14;
428                         base = 0x0c0000 + ( (mem_config & 0x18) << 12);
429                 }
430                 dev->mem_start = base;
431                 dev->mem_end = base + size;
432         }
433 #endif
434
435         dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
436         dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
437
438         printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
439                    dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
440
441         if (net_debug)
442                 printk(version);
443
444         lp = netdev_priv(dev);
445         memset(lp, 0, sizeof(*lp));
446         spin_lock_init(&lp->lock);
447
448         dev->open = el16_open;
449         dev->stop = el16_close;
450         dev->hard_start_xmit = el16_send_packet;
451         dev->get_stats  = el16_get_stats;
452         dev->tx_timeout = el16_tx_timeout;
453         dev->watchdog_timeo = TX_TIMEOUT;
454         dev->ethtool_ops = &netdev_ethtool_ops;
455         dev->flags &= ~IFF_MULTICAST;   /* Multicast doesn't work */
456         return 0;
457 out:
458         release_region(ioaddr, EL16_IO_EXTENT);
459         return retval;
460 }
461
462 static int el16_open(struct net_device *dev)
463 {
464         /* Initialize the 82586 memory and start it. */
465         init_82586_mem(dev);
466
467         netif_start_queue(dev);
468         return 0;
469 }
470
471
472 static void el16_tx_timeout (struct net_device *dev)
473 {
474         struct net_local *lp = netdev_priv(dev);
475         int ioaddr = dev->base_addr;
476         unsigned long shmem = dev->mem_start;
477
478         if (net_debug > 1)
479                 printk ("%s: transmit timed out, %s?  ", dev->name,
480                         isa_readw (shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
481                         "network cable problem");
482         /* Try to restart the adaptor. */
483         if (lp->last_restart == lp->stats.tx_packets) {
484                 if (net_debug > 1)
485                         printk ("Resetting board.\n");
486                 /* Completely reset the adaptor. */
487                 init_82586_mem (dev);
488                 lp->tx_pkts_in_ring = 0;
489         } else {
490                 /* Issue the channel attention signal and hope it "gets better". */
491                 if (net_debug > 1)
492                         printk ("Kicking board.\n");
493                 isa_writew (0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
494                 outb (0, ioaddr + SIGNAL_CA);   /* Issue channel-attn. */
495                 lp->last_restart = lp->stats.tx_packets;
496         }
497         dev->trans_start = jiffies;
498         netif_wake_queue (dev);
499 }
500
501
502 static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
503 {
504         struct net_local *lp = netdev_priv(dev);
505         int ioaddr = dev->base_addr;
506         unsigned long flags;
507         short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
508         unsigned char *buf = skb->data;
509
510         netif_stop_queue (dev);
511
512         spin_lock_irqsave (&lp->lock, flags);
513
514         lp->stats.tx_bytes += length;
515         /* Disable the 82586's input to the interrupt line. */
516         outb (0x80, ioaddr + MISC_CTRL);
517
518         hardware_send_packet (dev, buf, skb->len, length - skb->len);
519
520         dev->trans_start = jiffies;
521         /* Enable the 82586 interrupt input. */
522         outb (0x84, ioaddr + MISC_CTRL);
523
524         spin_unlock_irqrestore (&lp->lock, flags);
525
526         dev_kfree_skb (skb);
527
528         /* You might need to clean up and record Tx statistics here. */
529
530         return 0;
531 }
532
533 /*      The typical workload of the driver:
534         Handle the network interface interrupts. */
535 static irqreturn_t el16_interrupt(int irq, void *dev_id, struct pt_regs *regs)
536 {
537         struct net_device *dev = dev_id;
538         struct net_local *lp;
539         int ioaddr, status, boguscount = 0;
540         ushort ack_cmd = 0;
541         unsigned long shmem;
542
543         if (dev == NULL) {
544                 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
545                 return IRQ_NONE;
546         }
547
548         ioaddr = dev->base_addr;
549         lp = netdev_priv(dev);
550         shmem = dev->mem_start;
551
552         spin_lock(&lp->lock);
553
554         status = isa_readw(shmem+iSCB_STATUS);
555
556         if (net_debug > 4) {
557                 printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
558         }
559
560         /* Disable the 82586's input to the interrupt line. */
561         outb(0x80, ioaddr + MISC_CTRL);
562
563         /* Reap the Tx packet buffers. */
564         while (lp->tx_pkts_in_ring) {
565           unsigned short tx_status = isa_readw(shmem+lp->tx_reap);
566           if (!(tx_status & 0x8000)) {
567                 if (net_debug > 5) 
568                         printk("Tx command incomplete (%#x).\n", lp->tx_reap);
569                 break;
570           }
571           /* Tx unsuccessful or some interesting status bit set. */
572           if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
573                 lp->stats.tx_errors++;
574                 if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
575                 if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
576                 if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
577                 if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
578                 lp->stats.collisions += tx_status & 0xf;
579           }
580           lp->stats.tx_packets++;
581           if (net_debug > 5)
582                   printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
583           lp->tx_reap += TX_BUF_SIZE;
584           if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
585                 lp->tx_reap = TX_BUF_START;
586
587           lp->tx_pkts_in_ring--;
588           /* There is always more space in the Tx ring buffer now. */
589           netif_wake_queue(dev);
590
591           if (++boguscount > 10)
592                 break;
593         }
594
595         if (status & 0x4000) { /* Packet received. */
596                 if (net_debug > 5)
597                         printk("Received packet, rx_head %04x.\n", lp->rx_head);
598                 el16_rx(dev);
599         }
600
601         /* Acknowledge the interrupt sources. */
602         ack_cmd = status & 0xf000;
603
604         if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
605                 if (net_debug)
606                         printk("%s: Command unit stopped, status %04x, restarting.\n",
607                                    dev->name, status);
608                 /* If this ever occurs we should really re-write the idle loop, reset
609                    the Tx list, and do a complete restart of the command unit.
610                    For now we rely on the Tx timeout if the resume doesn't work. */
611                 ack_cmd |= CUC_RESUME;
612         }
613
614         if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
615                 static void init_rx_bufs(struct net_device *);
616                 /* The Rx unit is not ready, it must be hung.  Restart the receiver by
617                    initializing the rx buffers, and issuing an Rx start command. */
618                 if (net_debug)
619                         printk("%s: Rx unit stopped, status %04x, restarting.\n",
620                                    dev->name, status);
621                 init_rx_bufs(dev);
622                 isa_writew(RX_BUF_START,shmem+iSCB_RFA);
623                 ack_cmd |= RX_START;
624         }
625
626         isa_writew(ack_cmd,shmem+iSCB_CMD);
627         outb(0, ioaddr + SIGNAL_CA);                    /* Issue channel-attn. */
628
629         /* Clear the latched interrupt. */
630         outb(0, ioaddr + RESET_IRQ);
631
632         /* Enable the 82586's interrupt input. */
633         outb(0x84, ioaddr + MISC_CTRL);
634         spin_unlock(&lp->lock);
635         return IRQ_HANDLED;
636 }
637
638 static int el16_close(struct net_device *dev)
639 {
640         int ioaddr = dev->base_addr;
641         unsigned long shmem = dev->mem_start;
642
643         netif_stop_queue(dev);
644
645         /* Flush the Tx and disable Rx. */
646         isa_writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
647         outb(0, ioaddr + SIGNAL_CA);
648
649         /* Disable the 82586's input to the interrupt line. */
650         outb(0x80, ioaddr + MISC_CTRL);
651
652         /* We always physically use the IRQ line, so we don't do free_irq(). */
653
654         /* Update the statistics here. */
655
656         return 0;
657 }
658
659 /* Get the current statistics.  This may be called with the card open or
660    closed. */
661 static struct net_device_stats *el16_get_stats(struct net_device *dev)
662 {
663         struct net_local *lp = netdev_priv(dev);
664
665         /* ToDo: decide if there are any useful statistics from the SCB. */
666
667         return &lp->stats;
668 }
669
670 /* Initialize the Rx-block list. */
671 static void init_rx_bufs(struct net_device *dev)
672 {
673         struct net_local *lp = netdev_priv(dev);
674         unsigned long write_ptr;
675         unsigned short SCB_base = SCB_BASE;
676
677         int cur_rxbuf = lp->rx_head = RX_BUF_START;
678
679         /* Initialize each Rx frame + data buffer. */
680         do {    /* While there is room for one more. */
681
682           write_ptr = dev->mem_start + cur_rxbuf;
683
684                 isa_writew(0x0000,write_ptr);                   /* Status */
685                 isa_writew(0x0000,write_ptr+=2);                        /* Command */
686                 isa_writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2);       /* Link */
687                 isa_writew(cur_rxbuf + 22,write_ptr+=2);                /* Buffer offset */
688                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for dest addr. */
689                 isa_writew(0x0000,write_ptr+=2);
690                 isa_writew(0x0000,write_ptr+=2);
691                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for source addr. */
692                 isa_writew(0x0000,write_ptr+=2);
693                 isa_writew(0x0000,write_ptr+=2);
694                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for protocol. */
695
696                 isa_writew(0x0000,write_ptr+=2);                        /* Buffer: Actual count */
697                 isa_writew(-1,write_ptr+=2);                    /* Buffer: Next (none). */
698                 isa_writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
699                 isa_writew(0x0000,write_ptr+=2);
700                 /* Finally, the number of bytes in the buffer. */
701                 isa_writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
702
703                 lp->rx_tail = cur_rxbuf;
704                 cur_rxbuf += RX_BUF_SIZE;
705         } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
706
707         /* Terminate the list by setting the EOL bit, and wrap the pointer to make
708            the list a ring. */
709         write_ptr = dev->mem_start + lp->rx_tail + 2;
710         isa_writew(0xC000,write_ptr);                           /* Command, mark as last. */
711         isa_writew(lp->rx_head,write_ptr+2);                    /* Link */
712 }
713
714 static void init_82586_mem(struct net_device *dev)
715 {
716         struct net_local *lp = netdev_priv(dev);
717         short ioaddr = dev->base_addr;
718         unsigned long shmem = dev->mem_start;
719
720         /* Enable loopback to protect the wire while starting up,
721            and hold the 586 in reset during the memory initialization. */
722         outb(0x20, ioaddr + MISC_CTRL);
723
724         /* Fix the ISCP address and base. */
725         init_words[3] = SCB_BASE;
726         init_words[7] = SCB_BASE;
727
728         /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
729         isa_memcpy_toio(dev->mem_end-10, init_words, 10);
730
731         /* Write the words at 0x0000. */
732         isa_memcpy_toio(dev->mem_start, init_words + 5, sizeof(init_words) - 10);
733
734         /* Fill in the station address. */
735         isa_memcpy_toio(dev->mem_start+SA_OFFSET, dev->dev_addr,
736                    sizeof(dev->dev_addr));
737
738         /* The Tx-block list is written as needed.  We just set up the values. */
739         lp->tx_cmd_link = IDLELOOP + 4;
740         lp->tx_head = lp->tx_reap = TX_BUF_START;
741
742         init_rx_bufs(dev);
743
744         /* Start the 586 by releasing the reset line, but leave loopback. */
745         outb(0xA0, ioaddr + MISC_CTRL);
746
747         /* This was time consuming to track down: you need to give two channel
748            attention signals to reliably start up the i82586. */
749         outb(0, ioaddr + SIGNAL_CA);
750
751         {
752                 int boguscnt = 50;
753                 while (isa_readw(shmem+iSCB_STATUS) == 0)
754                         if (--boguscnt == 0) {
755                                 printk("%s: i82586 initialization timed out with status %04x,"
756                                            "cmd %04x.\n", dev->name,
757                                            isa_readw(shmem+iSCB_STATUS), isa_readw(shmem+iSCB_CMD));
758                                 break;
759                         }
760                 /* Issue channel-attn -- the 82586 won't start. */
761                 outb(0, ioaddr + SIGNAL_CA);
762         }
763
764         /* Disable loopback and enable interrupts. */
765         outb(0x84, ioaddr + MISC_CTRL);
766         if (net_debug > 4)
767                 printk("%s: Initialized 82586, status %04x.\n", dev->name,
768                            isa_readw(shmem+iSCB_STATUS));
769         return;
770 }
771
772 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad)
773 {
774         struct net_local *lp = netdev_priv(dev);
775         short ioaddr = dev->base_addr;
776         ushort tx_block = lp->tx_head;
777         unsigned long write_ptr = dev->mem_start + tx_block;
778         static char padding[ETH_ZLEN];
779
780         /* Set the write pointer to the Tx block, and put out the header. */
781         isa_writew(0x0000,write_ptr);                   /* Tx status */
782         isa_writew(CMD_INTR|CmdTx,write_ptr+=2);                /* Tx command */
783         isa_writew(tx_block+16,write_ptr+=2);           /* Next command is a NoOp. */
784         isa_writew(tx_block+8,write_ptr+=2);                    /* Data Buffer offset. */
785
786         /* Output the data buffer descriptor. */
787         isa_writew((pad + length) | 0x8000,write_ptr+=2);               /* Byte count parameter. */
788         isa_writew(-1,write_ptr+=2);                    /* No next data buffer. */
789         isa_writew(tx_block+22+SCB_BASE,write_ptr+=2);  /* Buffer follows the NoOp command. */
790         isa_writew(0x0000,write_ptr+=2);                        /* Buffer address high bits (always zero). */
791
792         /* Output the Loop-back NoOp command. */
793         isa_writew(0x0000,write_ptr+=2);                        /* Tx status */
794         isa_writew(CmdNOp,write_ptr+=2);                        /* Tx command */
795         isa_writew(tx_block+16,write_ptr+=2);           /* Next is myself. */
796
797         /* Output the packet at the write pointer. */
798         isa_memcpy_toio(write_ptr+2, buf, length);
799         if (pad)
800                 isa_memcpy_toio(write_ptr+length+2, padding, pad);
801
802         /* Set the old command link pointing to this send packet. */
803         isa_writew(tx_block,dev->mem_start + lp->tx_cmd_link);
804         lp->tx_cmd_link = tx_block + 20;
805
806         /* Set the next free tx region. */
807         lp->tx_head = tx_block + TX_BUF_SIZE;
808         if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
809                 lp->tx_head = TX_BUF_START;
810
811         if (net_debug > 4) {
812                 printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
813                            dev->name, ioaddr, length, tx_block, lp->tx_head);
814         }
815
816         /* Grimly block further packets if there has been insufficient reaping. */
817         if (++lp->tx_pkts_in_ring < NUM_TX_BUFS) 
818                 netif_wake_queue(dev);
819 }
820
821 static void el16_rx(struct net_device *dev)
822 {
823         struct net_local *lp = netdev_priv(dev);
824         unsigned long shmem = dev->mem_start;
825         ushort rx_head = lp->rx_head;
826         ushort rx_tail = lp->rx_tail;
827         ushort boguscount = 10;
828         short frame_status;
829
830         while ((frame_status = isa_readw(shmem+rx_head)) < 0) {   /* Command complete */
831                 unsigned long read_frame = dev->mem_start + rx_head;
832                 ushort rfd_cmd = isa_readw(read_frame+2);
833                 ushort next_rx_frame = isa_readw(read_frame+4);
834                 ushort data_buffer_addr = isa_readw(read_frame+6);
835                 unsigned long data_frame = dev->mem_start + data_buffer_addr;
836                 ushort pkt_len = isa_readw(data_frame);
837
838                 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
839                         || (pkt_len & 0xC000) != 0xC000) {
840                         printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
841                                    "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
842                                    frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
843                                    pkt_len);
844                 } else if ((frame_status & 0x2000) == 0) {
845                         /* Frame Rxed, but with error. */
846                         lp->stats.rx_errors++;
847                         if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
848                         if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
849                         if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
850                         if (frame_status & 0x0100) lp->stats.rx_over_errors++;
851                         if (frame_status & 0x0080) lp->stats.rx_length_errors++;
852                 } else {
853                         /* Malloc up new buffer. */
854                         struct sk_buff *skb;
855
856                         pkt_len &= 0x3fff;
857                         skb = dev_alloc_skb(pkt_len+2);
858                         if (skb == NULL) {
859                                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
860                                 lp->stats.rx_dropped++;
861                                 break;
862                         }
863
864                         skb_reserve(skb,2);
865                         skb->dev = dev;
866
867                         /* 'skb->data' points to the start of sk_buff data area. */
868                         isa_memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
869
870                         skb->protocol=eth_type_trans(skb,dev);
871                         netif_rx(skb);
872                         dev->last_rx = jiffies;
873                         lp->stats.rx_packets++;
874                         lp->stats.rx_bytes += pkt_len;
875                 }
876
877                 /* Clear the status word and set End-of-List on the rx frame. */
878                 isa_writew(0,read_frame);
879                 isa_writew(0xC000,read_frame+2);
880                 /* Clear the end-of-list on the prev. RFD. */
881                 isa_writew(0x0000,dev->mem_start + rx_tail + 2);
882
883                 rx_tail = rx_head;
884                 rx_head = next_rx_frame;
885                 if (--boguscount == 0)
886                         break;
887         }
888
889         lp->rx_head = rx_head;
890         lp->rx_tail = rx_tail;
891 }
892
893 static void netdev_get_drvinfo(struct net_device *dev,
894                                struct ethtool_drvinfo *info)
895 {
896         strcpy(info->driver, DRV_NAME);
897         strcpy(info->version, DRV_VERSION);
898         sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
899 }
900
901 static u32 netdev_get_msglevel(struct net_device *dev)
902 {
903         return debug;
904 }
905
906 static void netdev_set_msglevel(struct net_device *dev, u32 level)
907 {
908         debug = level;
909 }
910
911 static struct ethtool_ops netdev_ethtool_ops = {
912         .get_drvinfo            = netdev_get_drvinfo,
913         .get_msglevel           = netdev_get_msglevel,
914         .set_msglevel           = netdev_set_msglevel,
915 };
916
917 #ifdef MODULE
918 static struct net_device *dev_3c507;
919 MODULE_PARM(io, "i");
920 MODULE_PARM(irq, "i");
921 MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
922 MODULE_PARM_DESC(irq, "(ignored)");
923
924 int init_module(void)
925 {
926         if (io == 0)
927                 printk("3c507: You should not use auto-probing with insmod!\n");
928         dev_3c507 = el16_probe(-1);
929         return IS_ERR(dev_3c507) ? PTR_ERR(dev_3c507) : 0;
930 }
931
932 void
933 cleanup_module(void)
934 {
935         struct net_device *dev = dev_3c507;
936         unregister_netdev(dev);
937         free_irq(dev->irq, dev);
938         release_region(dev->base_addr, EL16_IO_EXTENT);
939         free_netdev(dev);
940 }
941 #endif /* MODULE */
942 MODULE_LICENSE("GPL");
943
944 \f
945 /*
946  * Local variables:
947  *  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"
948  *  version-control: t
949  *  kept-new-versions: 5
950  *  tab-width: 4
951  *  c-indent-level: 4
952  * End:
953  */