Merge branch 'master' into upstream-fixes
[sfrench/cifs-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 #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
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         void __iomem *base;
131 };
132
133 /*
134                 Details of the EtherLink16 Implementation
135   The 3c507 is a generic shared-memory i82586 implementation.
136   The host can map 16K, 32K, 48K, or 64K of the 64K memory into
137   0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
138   */
139
140 /* Offsets from the base I/O address. */
141 #define SA_DATA         0       /* Station address data, or 3Com signature. */
142 #define MISC_CTRL       6       /* Switch the SA_DATA banks, and bus config bits. */
143 #define RESET_IRQ       10      /* Reset the latched IRQ line. */
144 #define SIGNAL_CA       11      /* Frob the 82586 Channel Attention line. */
145 #define ROM_CONFIG      13
146 #define MEM_CONFIG      14
147 #define IRQ_CONFIG      15
148 #define EL16_IO_EXTENT 16
149
150 /* The ID port is used at boot-time to locate the ethercard. */
151 #define ID_PORT         0x100
152
153 /* Offsets to registers in the mailbox (SCB). */
154 #define iSCB_STATUS     0x8
155 #define iSCB_CMD                0xA
156 #define iSCB_CBL                0xC     /* Command BLock offset. */
157 #define iSCB_RFA                0xE     /* Rx Frame Area offset. */
158
159 /*  Since the 3c507 maps the shared memory window so that the last byte is
160         at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
161         48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
162         We can account for this be setting the 'SBC Base' entry in the ISCP table
163         below for all the 16 bit offset addresses, and also adding the 'SCB Base'
164         value to all 24 bit physical addresses (in the SCP table and the TX and RX
165         Buffer Descriptors).
166                                         -Mark
167         */
168 #define SCB_BASE                ((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
169
170 /*
171   What follows in 'init_words[]' is the "program" that is downloaded to the
172   82586 memory.  It's mostly tables and command blocks, and starts at the
173   reset address 0xfffff6.  This is designed to be similar to the EtherExpress,
174   thus the unusual location of the SCB at 0x0008.
175
176   Even with the additional "don't care" values, doing it this way takes less
177   program space than initializing the individual tables, and I feel it's much
178   cleaner.
179
180   The databook is particularly useless for the first two structures, I had
181   to use the Crynwr driver as an example.
182
183    The memory setup is as follows:
184    */
185
186 #define CONFIG_CMD      0x0018
187 #define SET_SA_CMD      0x0024
188 #define SA_OFFSET       0x002A
189 #define IDLELOOP        0x30
190 #define TDR_CMD         0x38
191 #define TDR_TIME        0x3C
192 #define DUMP_CMD        0x40
193 #define DIAG_CMD        0x48
194 #define SET_MC_CMD      0x4E
195 #define DUMP_DATA       0x56    /* A 170 byte buffer for dump and Set-MC into. */
196
197 #define TX_BUF_START    0x0100
198 #define NUM_TX_BUFS     5
199 #define TX_BUF_SIZE     (1518+14+20+16) /* packet+header+TBD */
200
201 #define RX_BUF_START    0x2000
202 #define RX_BUF_SIZE     (1518+14+18)    /* packet+header+RBD */
203 #define RX_BUF_END              (dev->mem_end - dev->mem_start)
204
205 #define TX_TIMEOUT 5
206
207 /*
208   That's it: only 86 bytes to set up the beast, including every extra
209   command available.  The 170 byte buffer at DUMP_DATA is shared between the
210   Dump command (called only by the diagnostic program) and the SetMulticastList
211   command.
212
213   To complete the memory setup you only have to write the station address at
214   SA_OFFSET and create the Tx & Rx buffer lists.
215
216   The Tx command chain and buffer list is setup as follows:
217   A Tx command table, with the data buffer pointing to...
218   A Tx data buffer descriptor.  The packet is in a single buffer, rather than
219         chaining together several smaller buffers.
220   A NoOp command, which initially points to itself,
221   And the packet data.
222
223   A transmit is done by filling in the Tx command table and data buffer,
224   re-writing the NoOp command, and finally changing the offset of the last
225   command to point to the current Tx command.  When the Tx command is finished,
226   it jumps to the NoOp, when it loops until the next Tx command changes the
227   "link offset" in the NoOp.  This way the 82586 never has to go through the
228   slow restart sequence.
229
230   The Rx buffer list is set up in the obvious ring structure.  We have enough
231   memory (and low enough interrupt latency) that we can avoid the complicated
232   Rx buffer linked lists by alway associating a full-size Rx data buffer with
233   each Rx data frame.
234
235   I current use four transmit buffers starting at TX_BUF_START (0x0100), and
236   use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
237
238   */
239
240 static unsigned short init_words[] = {
241         /*      System Configuration Pointer (SCP). */
242         0x0000,                                 /* Set bus size to 16 bits. */
243         0,0,                                    /* pad words. */
244         0x0000,0x0000,                  /* ISCP phys addr, set in init_82586_mem(). */
245
246         /*      Intermediate System Configuration Pointer (ISCP). */
247         0x0001,                                 /* Status word that's cleared when init is done. */
248         0x0008,0,0,                             /* SCB offset, (skip, skip) */
249
250         /* System Control Block (SCB). */
251         0,0xf000|RX_START|CUC_START,    /* SCB status and cmd. */
252         CONFIG_CMD,                             /* Command list pointer, points to Configure. */
253         RX_BUF_START,                           /* Rx block list. */
254         0,0,0,0,                                /* Error count: CRC, align, buffer, overrun. */
255
256         /* 0x0018: Configure command.  Change to put MAC data with packet. */
257         0, CmdConfigure,                /* Status, command.             */
258         SET_SA_CMD,                             /* Next command is Set Station Addr. */
259         0x0804,                                 /* "4" bytes of config data, 8 byte FIFO. */
260         0x2e40,                                 /* Magic values, including MAC data location. */
261         0,                                              /* Unused pad word. */
262
263         /* 0x0024: Setup station address command. */
264         0, CmdSASetup,
265         SET_MC_CMD,                             /* Next command. */
266         0xaa00,0xb000,0x0bad,   /* Station address (to be filled in) */
267
268         /* 0x0030: NOP, looping back to itself.  Point to first Tx buffer to Tx. */
269         0, CmdNOp, IDLELOOP, 0 /* pad */,
270
271         /* 0x0038: A unused Time-Domain Reflectometer command. */
272         0, CmdTDR, IDLELOOP, 0,
273
274         /* 0x0040: An unused Dump State command. */
275         0, CmdDump, IDLELOOP, DUMP_DATA,
276
277         /* 0x0048: An unused Diagnose command. */
278         0, CmdDiagnose, IDLELOOP,
279
280         /* 0x004E: An empty set-multicast-list command. */
281         0, CmdMulticastList, IDLELOOP, 0,
282 };
283
284 /* Index to functions, as function prototypes. */
285
286 static int      el16_probe1(struct net_device *dev, int ioaddr);
287 static int      el16_open(struct net_device *dev);
288 static int      el16_send_packet(struct sk_buff *skb, struct net_device *dev);
289 static irqreturn_t el16_interrupt(int irq, void *dev_id);
290 static void el16_rx(struct net_device *dev);
291 static int      el16_close(struct net_device *dev);
292 static struct net_device_stats *el16_get_stats(struct net_device *dev);
293 static void el16_tx_timeout (struct net_device *dev);
294
295 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad);
296 static void init_82586_mem(struct net_device *dev);
297 static const struct ethtool_ops netdev_ethtool_ops;
298 static void init_rx_bufs(struct net_device *);
299
300 static int io = 0x300;
301 static int irq;
302 static int mem_start;
303
304
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.
310         */
311
312 struct net_device * __init el16_probe(int unit)
313 {
314         struct net_device *dev = alloc_etherdev(sizeof(struct net_local));
315         static unsigned ports[] = { 0x300, 0x320, 0x340, 0x280, 0};
316         unsigned *port;
317         int err = -ENODEV;
318
319         if (!dev)
320                 return ERR_PTR(-ENODEV);
321
322         if (unit >= 0) {
323                 sprintf(dev->name, "eth%d", unit);
324                 netdev_boot_setup_check(dev);
325                 io = dev->base_addr;
326                 irq = dev->irq;
327                 mem_start = dev->mem_start & 15;
328         }
329
330         SET_MODULE_OWNER(dev);
331
332         if (io > 0x1ff)         /* Check a single specified location. */
333                 err = el16_probe1(dev, io);
334         else if (io != 0)
335                 err = -ENXIO;           /* Don't probe at all. */
336         else {
337                 for (port = ports; *port; port++) {
338                         err = el16_probe1(dev, *port);
339                         if (!err)
340                                 break;
341                 }
342         }
343
344         if (err)
345                 goto out;
346         err = register_netdev(dev);
347         if (err)
348                 goto out1;
349         return dev;
350 out1:
351         free_irq(dev->irq, dev);
352         iounmap(((struct net_local *)netdev_priv(dev))->base);
353         release_region(dev->base_addr, EL16_IO_EXTENT);
354 out:
355         free_netdev(dev);
356         return ERR_PTR(err);
357 }
358
359 static int __init el16_probe1(struct net_device *dev, int ioaddr)
360 {
361         static unsigned char init_ID_done, version_printed;
362         int i, irq, irqval, retval;
363         struct net_local *lp;
364
365         if (init_ID_done == 0) {
366                 ushort lrs_state = 0xff;
367                 /* Send the ID sequence to the ID_PORT to enable the board(s). */
368                 outb(0x00, ID_PORT);
369                 for(i = 0; i < 255; i++) {
370                         outb(lrs_state, ID_PORT);
371                         lrs_state <<= 1;
372                         if (lrs_state & 0x100)
373                                 lrs_state ^= 0xe7;
374                 }
375                 outb(0x00, ID_PORT);
376                 init_ID_done = 1;
377         }
378
379         if (!request_region(ioaddr, EL16_IO_EXTENT, DRV_NAME))
380                 return -ENODEV;
381
382         if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') ||
383             (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
384                 retval = -ENODEV;
385                 goto out;
386         }
387
388         if (net_debug  &&  version_printed++ == 0)
389                 printk(version);
390
391         printk("%s: 3c507 at %#x,", dev->name, ioaddr);
392
393         /* We should make a few more checks here, like the first three octets of
394            the S.A. for the manufacturer's code. */
395
396         irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
397
398         irqval = request_irq(irq, &el16_interrupt, 0, DRV_NAME, dev);
399         if (irqval) {
400                 printk(KERN_ERR "3c507: unable to get IRQ %d (irqval=%d).\n", irq, irqval);
401                 retval = -EAGAIN;
402                 goto out;
403         }
404
405         /* We've committed to using the board, and can start filling in *dev. */
406         dev->base_addr = ioaddr;
407
408         outb(0x01, ioaddr + MISC_CTRL);
409         for (i = 0; i < 6; i++) {
410                 dev->dev_addr[i] = inb(ioaddr + i);
411                 printk(" %02x", dev->dev_addr[i]);
412         }
413
414         if (mem_start)
415                 net_debug = mem_start & 7;
416
417 #ifdef MEM_BASE
418         dev->mem_start = MEM_BASE;
419         dev->mem_end = dev->mem_start + 0x10000;
420 #else
421         {
422                 int base;
423                 int size;
424                 char mem_config = inb(ioaddr + MEM_CONFIG);
425                 if (mem_config & 0x20) {
426                         size = 64*1024;
427                         base = 0xf00000 + (mem_config & 0x08 ? 0x080000
428                                                            : ((mem_config & 3) << 17));
429                 } else {
430                         size = ((mem_config & 3) + 1) << 14;
431                         base = 0x0c0000 + ( (mem_config & 0x18) << 12);
432                 }
433                 dev->mem_start = base;
434                 dev->mem_end = base + size;
435         }
436 #endif
437
438         dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
439         dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
440
441         printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
442                    dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
443
444         if (net_debug)
445                 printk(version);
446
447         lp = netdev_priv(dev);
448         memset(lp, 0, sizeof(*lp));
449         spin_lock_init(&lp->lock);
450         lp->base = ioremap(dev->mem_start, RX_BUF_END);
451         if (!lp->base) {
452                 printk(KERN_ERR "3c507: unable to remap memory\n");
453                 retval = -EAGAIN;
454                 goto out1;
455         }
456
457         dev->open = el16_open;
458         dev->stop = el16_close;
459         dev->hard_start_xmit = el16_send_packet;
460         dev->get_stats  = el16_get_stats;
461         dev->tx_timeout = el16_tx_timeout;
462         dev->watchdog_timeo = TX_TIMEOUT;
463         dev->ethtool_ops = &netdev_ethtool_ops;
464         dev->flags &= ~IFF_MULTICAST;   /* Multicast doesn't work */
465         return 0;
466 out1:
467         free_irq(dev->irq, dev);
468 out:
469         release_region(ioaddr, EL16_IO_EXTENT);
470         return retval;
471 }
472
473 static int el16_open(struct net_device *dev)
474 {
475         /* Initialize the 82586 memory and start it. */
476         init_82586_mem(dev);
477
478         netif_start_queue(dev);
479         return 0;
480 }
481
482
483 static void el16_tx_timeout (struct net_device *dev)
484 {
485         struct net_local *lp = netdev_priv(dev);
486         int ioaddr = dev->base_addr;
487         void __iomem *shmem = lp->base;
488
489         if (net_debug > 1)
490                 printk ("%s: transmit timed out, %s?  ", dev->name,
491                         readw(shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
492                         "network cable problem");
493         /* Try to restart the adaptor. */
494         if (lp->last_restart == lp->stats.tx_packets) {
495                 if (net_debug > 1)
496                         printk ("Resetting board.\n");
497                 /* Completely reset the adaptor. */
498                 init_82586_mem (dev);
499                 lp->tx_pkts_in_ring = 0;
500         } else {
501                 /* Issue the channel attention signal and hope it "gets better". */
502                 if (net_debug > 1)
503                         printk ("Kicking board.\n");
504                 writew(0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
505                 outb (0, ioaddr + SIGNAL_CA);   /* Issue channel-attn. */
506                 lp->last_restart = lp->stats.tx_packets;
507         }
508         dev->trans_start = jiffies;
509         netif_wake_queue (dev);
510 }
511
512
513 static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
514 {
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
525         lp->stats.tx_bytes += length;
526         /* 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
531         dev->trans_start = jiffies;
532         /* Enable the 82586 interrupt input. */
533         outb (0x84, ioaddr + MISC_CTRL);
534
535         spin_unlock_irqrestore (&lp->lock, flags);
536
537         dev_kfree_skb (skb);
538
539         /* You might need to clean up and record Tx statistics here. */
540
541         return 0;
542 }
543
544 /*      The typical workload of the driver:
545         Handle the network interface interrupts. */
546 static irqreturn_t el16_interrupt(int irq, void *dev_id)
547 {
548         struct net_device *dev = dev_id;
549         struct net_local *lp;
550         int ioaddr, status, boguscount = 0;
551         ushort ack_cmd = 0;
552         void __iomem *shmem;
553
554         if (dev == NULL) {
555                 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
556                 return IRQ_NONE;
557         }
558
559         ioaddr = dev->base_addr;
560         lp = netdev_priv(dev);
561         shmem = lp->base;
562
563         spin_lock(&lp->lock);
564
565         status = readw(shmem+iSCB_STATUS);
566
567         if (net_debug > 4) {
568                 printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
569         }
570
571         /* Disable the 82586's input to the interrupt line. */
572         outb(0x80, ioaddr + MISC_CTRL);
573
574         /* Reap the Tx packet buffers. */
575         while (lp->tx_pkts_in_ring) {
576           unsigned short tx_status = readw(shmem+lp->tx_reap);
577           if (!(tx_status & 0x8000)) {
578                 if (net_debug > 5)
579                         printk("Tx command incomplete (%#x).\n", lp->tx_reap);
580                 break;
581           }
582           /* Tx unsuccessful or some interesting status bit set. */
583           if (!(tx_status & 0x2000) || (tx_status & 0x0f3f)) {
584                 lp->stats.tx_errors++;
585                 if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
586                 if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
587                 if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
588                 if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
589                 lp->stats.collisions += tx_status & 0xf;
590           }
591           lp->stats.tx_packets++;
592           if (net_debug > 5)
593                   printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
594           lp->tx_reap += TX_BUF_SIZE;
595           if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
596                 lp->tx_reap = TX_BUF_START;
597
598           lp->tx_pkts_in_ring--;
599           /* There is always more space in the Tx ring buffer now. */
600           netif_wake_queue(dev);
601
602           if (++boguscount > 10)
603                 break;
604         }
605
606         if (status & 0x4000) { /* Packet received. */
607                 if (net_debug > 5)
608                         printk("Received packet, rx_head %04x.\n", lp->rx_head);
609                 el16_rx(dev);
610         }
611
612         /* Acknowledge the interrupt sources. */
613         ack_cmd = status & 0xf000;
614
615         if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
616                 if (net_debug)
617                         printk("%s: Command unit stopped, status %04x, restarting.\n",
618                                    dev->name, status);
619                 /* If this ever occurs we should really re-write the idle loop, reset
620                    the Tx list, and do a complete restart of the command unit.
621                    For now we rely on the Tx timeout if the resume doesn't work. */
622                 ack_cmd |= CUC_RESUME;
623         }
624
625         if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
626                 /* The Rx unit is not ready, it must be hung.  Restart the receiver by
627                    initializing the rx buffers, and issuing an Rx start command. */
628                 if (net_debug)
629                         printk("%s: Rx unit stopped, status %04x, restarting.\n",
630                                    dev->name, status);
631                 init_rx_bufs(dev);
632                 writew(RX_BUF_START,shmem+iSCB_RFA);
633                 ack_cmd |= RX_START;
634         }
635
636         writew(ack_cmd,shmem+iSCB_CMD);
637         outb(0, ioaddr + SIGNAL_CA);                    /* Issue channel-attn. */
638
639         /* Clear the latched interrupt. */
640         outb(0, ioaddr + RESET_IRQ);
641
642         /* Enable the 82586's interrupt input. */
643         outb(0x84, ioaddr + MISC_CTRL);
644         spin_unlock(&lp->lock);
645         return IRQ_HANDLED;
646 }
647
648 static int el16_close(struct net_device *dev)
649 {
650         struct net_local *lp = netdev_priv(dev);
651         int ioaddr = dev->base_addr;
652         void __iomem *shmem = lp->base;
653
654         netif_stop_queue(dev);
655
656         /* Flush the Tx and disable Rx. */
657         writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
658         outb(0, ioaddr + SIGNAL_CA);
659
660         /* Disable the 82586's input to the interrupt line. */
661         outb(0x80, ioaddr + MISC_CTRL);
662
663         /* We always physically use the IRQ line, so we don't do free_irq(). */
664
665         /* Update the statistics here. */
666
667         return 0;
668 }
669
670 /* Get the current statistics.  This may be called with the card open or
671    closed. */
672 static struct net_device_stats *el16_get_stats(struct net_device *dev)
673 {
674         struct net_local *lp = netdev_priv(dev);
675
676         /* ToDo: decide if there are any useful statistics from the SCB. */
677
678         return &lp->stats;
679 }
680
681 /* Initialize the Rx-block list. */
682 static void init_rx_bufs(struct net_device *dev)
683 {
684         struct net_local *lp = netdev_priv(dev);
685         void __iomem *write_ptr;
686         unsigned short SCB_base = SCB_BASE;
687
688         int cur_rxbuf = lp->rx_head = RX_BUF_START;
689
690         /* Initialize each Rx frame + data buffer. */
691         do {    /* While there is room for one more. */
692
693                 write_ptr = lp->base + cur_rxbuf;
694
695                 writew(0x0000,write_ptr);                       /* Status */
696                 writew(0x0000,write_ptr+=2);                    /* Command */
697                 writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2);   /* Link */
698                 writew(cur_rxbuf + 22,write_ptr+=2);            /* Buffer offset */
699                 writew(0x0000,write_ptr+=2);                    /* Pad for dest addr. */
700                 writew(0x0000,write_ptr+=2);
701                 writew(0x0000,write_ptr+=2);
702                 writew(0x0000,write_ptr+=2);                    /* Pad for source addr. */
703                 writew(0x0000,write_ptr+=2);
704                 writew(0x0000,write_ptr+=2);
705                 writew(0x0000,write_ptr+=2);                    /* Pad for protocol. */
706
707                 writew(0x0000,write_ptr+=2);                    /* Buffer: Actual count */
708                 writew(-1,write_ptr+=2);                        /* Buffer: Next (none). */
709                 writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
710                 writew(0x0000,write_ptr+=2);
711                 /* Finally, the number of bytes in the buffer. */
712                 writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
713
714                 lp->rx_tail = cur_rxbuf;
715                 cur_rxbuf += RX_BUF_SIZE;
716         } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
717
718         /* Terminate the list by setting the EOL bit, and wrap the pointer to make
719            the list a ring. */
720         write_ptr = lp->base + lp->rx_tail + 2;
721         writew(0xC000,write_ptr);                               /* Command, mark as last. */
722         writew(lp->rx_head,write_ptr+2);                        /* Link */
723 }
724
725 static void init_82586_mem(struct net_device *dev)
726 {
727         struct net_local *lp = netdev_priv(dev);
728         short ioaddr = dev->base_addr;
729         void __iomem *shmem = lp->base;
730
731         /* Enable loopback to protect the wire while starting up,
732            and hold the 586 in reset during the memory initialization. */
733         outb(0x20, ioaddr + MISC_CTRL);
734
735         /* Fix the ISCP address and base. */
736         init_words[3] = SCB_BASE;
737         init_words[7] = SCB_BASE;
738
739         /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
740         memcpy_toio(lp->base + RX_BUF_END - 10, init_words, 10);
741
742         /* Write the words at 0x0000. */
743         memcpy_toio(lp->base, init_words + 5, sizeof(init_words) - 10);
744
745         /* Fill in the station address. */
746         memcpy_toio(lp->base+SA_OFFSET, dev->dev_addr,
747                    sizeof(dev->dev_addr));
748
749         /* The Tx-block list is written as needed.  We just set up the values. */
750         lp->tx_cmd_link = IDLELOOP + 4;
751         lp->tx_head = lp->tx_reap = TX_BUF_START;
752
753         init_rx_bufs(dev);
754
755         /* Start the 586 by releasing the reset line, but leave loopback. */
756         outb(0xA0, ioaddr + MISC_CTRL);
757
758         /* This was time consuming to track down: you need to give two channel
759            attention signals to reliably start up the i82586. */
760         outb(0, ioaddr + SIGNAL_CA);
761
762         {
763                 int boguscnt = 50;
764                 while (readw(shmem+iSCB_STATUS) == 0)
765                         if (--boguscnt == 0) {
766                                 printk("%s: i82586 initialization timed out with status %04x,"
767                                            "cmd %04x.\n", dev->name,
768                                            readw(shmem+iSCB_STATUS), readw(shmem+iSCB_CMD));
769                                 break;
770                         }
771                 /* Issue channel-attn -- the 82586 won't start. */
772                 outb(0, ioaddr + SIGNAL_CA);
773         }
774
775         /* Disable loopback and enable interrupts. */
776         outb(0x84, ioaddr + MISC_CTRL);
777         if (net_debug > 4)
778                 printk("%s: Initialized 82586, status %04x.\n", dev->name,
779                            readw(shmem+iSCB_STATUS));
780         return;
781 }
782
783 static void hardware_send_packet(struct net_device *dev, void *buf, short length, short pad)
784 {
785         struct net_local *lp = netdev_priv(dev);
786         short ioaddr = dev->base_addr;
787         ushort tx_block = lp->tx_head;
788         void __iomem *write_ptr = lp->base + tx_block;
789         static char padding[ETH_ZLEN];
790
791         /* Set the write pointer to the Tx block, and put out the header. */
792         writew(0x0000,write_ptr);                       /* Tx status */
793         writew(CMD_INTR|CmdTx,write_ptr+=2);            /* Tx command */
794         writew(tx_block+16,write_ptr+=2);               /* Next command is a NoOp. */
795         writew(tx_block+8,write_ptr+=2);                        /* Data Buffer offset. */
796
797         /* Output the data buffer descriptor. */
798         writew((pad + length) | 0x8000,write_ptr+=2);           /* Byte count parameter. */
799         writew(-1,write_ptr+=2);                        /* No next data buffer. */
800         writew(tx_block+22+SCB_BASE,write_ptr+=2);      /* Buffer follows the NoOp command. */
801         writew(0x0000,write_ptr+=2);                    /* Buffer address high bits (always zero). */
802
803         /* Output the Loop-back NoOp command. */
804         writew(0x0000,write_ptr+=2);                    /* Tx status */
805         writew(CmdNOp,write_ptr+=2);                    /* Tx command */
806         writew(tx_block+16,write_ptr+=2);               /* Next is myself. */
807
808         /* Output the packet at the write pointer. */
809         memcpy_toio(write_ptr+2, buf, length);
810         if (pad)
811                 memcpy_toio(write_ptr+length+2, padding, pad);
812
813         /* Set the old command link pointing to this send packet. */
814         writew(tx_block,lp->base + lp->tx_cmd_link);
815         lp->tx_cmd_link = tx_block + 20;
816
817         /* Set the next free tx region. */
818         lp->tx_head = tx_block + TX_BUF_SIZE;
819         if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
820                 lp->tx_head = TX_BUF_START;
821
822         if (net_debug > 4) {
823                 printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
824                            dev->name, ioaddr, length, tx_block, lp->tx_head);
825         }
826
827         /* Grimly block further packets if there has been insufficient reaping. */
828         if (++lp->tx_pkts_in_ring < NUM_TX_BUFS)
829                 netif_wake_queue(dev);
830 }
831
832 static void el16_rx(struct net_device *dev)
833 {
834         struct net_local *lp = netdev_priv(dev);
835         void __iomem *shmem = lp->base;
836         ushort rx_head = lp->rx_head;
837         ushort rx_tail = lp->rx_tail;
838         ushort boguscount = 10;
839         short frame_status;
840
841         while ((frame_status = readw(shmem+rx_head)) < 0) {   /* Command complete */
842                 void __iomem *read_frame = lp->base + rx_head;
843                 ushort rfd_cmd = readw(read_frame+2);
844                 ushort next_rx_frame = readw(read_frame+4);
845                 ushort data_buffer_addr = readw(read_frame+6);
846                 void __iomem *data_frame = lp->base + data_buffer_addr;
847                 ushort pkt_len = readw(data_frame);
848
849                 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
850                         || (pkt_len & 0xC000) != 0xC000) {
851                         printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
852                                    "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
853                                    frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
854                                    pkt_len);
855                 } else if ((frame_status & 0x2000) == 0) {
856                         /* Frame Rxed, but with error. */
857                         lp->stats.rx_errors++;
858                         if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
859                         if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
860                         if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
861                         if (frame_status & 0x0100) lp->stats.rx_over_errors++;
862                         if (frame_status & 0x0080) lp->stats.rx_length_errors++;
863                 } else {
864                         /* Malloc up new buffer. */
865                         struct sk_buff *skb;
866
867                         pkt_len &= 0x3fff;
868                         skb = dev_alloc_skb(pkt_len+2);
869                         if (skb == NULL) {
870                                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
871                                 lp->stats.rx_dropped++;
872                                 break;
873                         }
874
875                         skb_reserve(skb,2);
876                         skb->dev = dev;
877
878                         /* 'skb->data' points to the start of sk_buff data area. */
879                         memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
880
881                         skb->protocol=eth_type_trans(skb,dev);
882                         netif_rx(skb);
883                         dev->last_rx = jiffies;
884                         lp->stats.rx_packets++;
885                         lp->stats.rx_bytes += pkt_len;
886                 }
887
888                 /* Clear the status word and set End-of-List on the rx frame. */
889                 writew(0,read_frame);
890                 writew(0xC000,read_frame+2);
891                 /* Clear the end-of-list on the prev. RFD. */
892                 writew(0x0000,lp->base + rx_tail + 2);
893
894                 rx_tail = rx_head;
895                 rx_head = next_rx_frame;
896                 if (--boguscount == 0)
897                         break;
898         }
899
900         lp->rx_head = rx_head;
901         lp->rx_tail = rx_tail;
902 }
903
904 static void netdev_get_drvinfo(struct net_device *dev,
905                                struct ethtool_drvinfo *info)
906 {
907         strcpy(info->driver, DRV_NAME);
908         strcpy(info->version, DRV_VERSION);
909         sprintf(info->bus_info, "ISA 0x%lx", dev->base_addr);
910 }
911
912 static u32 netdev_get_msglevel(struct net_device *dev)
913 {
914         return debug;
915 }
916
917 static void netdev_set_msglevel(struct net_device *dev, u32 level)
918 {
919         debug = level;
920 }
921
922 static const struct ethtool_ops netdev_ethtool_ops = {
923         .get_drvinfo            = netdev_get_drvinfo,
924         .get_msglevel           = netdev_get_msglevel,
925         .set_msglevel           = netdev_set_msglevel,
926 };
927
928 #ifdef MODULE
929 static struct net_device *dev_3c507;
930 module_param(io, int, 0);
931 module_param(irq, int, 0);
932 MODULE_PARM_DESC(io, "EtherLink16 I/O base address");
933 MODULE_PARM_DESC(irq, "(ignored)");
934
935 int __init init_module(void)
936 {
937         if (io == 0)
938                 printk("3c507: You should not use auto-probing with insmod!\n");
939         dev_3c507 = el16_probe(-1);
940         return IS_ERR(dev_3c507) ? PTR_ERR(dev_3c507) : 0;
941 }
942
943 void
944 cleanup_module(void)
945 {
946         struct net_device *dev = dev_3c507;
947         unregister_netdev(dev);
948         free_irq(dev->irq, dev);
949         iounmap(((struct net_local *)netdev_priv(dev))->base);
950         release_region(dev->base_addr, EL16_IO_EXTENT);
951         free_netdev(dev);
952 }
953 #endif /* MODULE */
954 MODULE_LICENSE("GPL");
955
956
957 /*
958  * Local variables:
959  *  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"
960  *  version-control: t
961  *  kept-new-versions: 5
962  *  tab-width: 4
963  *  c-indent-level: 4
964  * End:
965  */