Import changeset
[linux-flexiantxendom0-3.2.10.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 Public License, incorporated herein by reference.
10
11         The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
12         Center of Excellence in Space Data and Information Sciences
13            Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
14
15         Thanks go to jennings@Montrouge.SMR.slb.com ( Patrick Jennings)
16         and jrs@world.std.com (Rick Sladkey) for testing and bugfixes.
17         Mark Salazar <leslie@access.digex.net> made the changes for cards with
18         only 16K packet buffers.
19
20         Things remaining to do:
21         Verify that the tx and rx buffers don't have fencepost errors.
22         Move the theory of operation and memory map documentation.
23         The statistics need to be updated correctly.
24 */
25
26 static const char *version =
27         "3c507.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
28
29
30 #include <linux/module.h>
31
32 /*
33   Sources:
34         This driver wouldn't have been written with the availability of the
35         Crynwr driver source code.      It provided a known-working implementation
36         that filled in the gaping holes of the Intel documentation.  Three cheers
37         for Russ Nelson.
38
39         Intel Microcommunications Databook, Vol. 1, 1990.  It provides just enough
40         info that the casual reader might think that it documents the i82586 :-<.
41 */
42
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/types.h>
46 #include <linux/fcntl.h>
47 #include <linux/interrupt.h>
48 #include <linux/ptrace.h>
49 #include <linux/ioport.h>
50 #include <linux/in.h>
51 #include <linux/string.h>
52 #include <linux/spinlock.h>
53 #include <asm/system.h>
54 #include <asm/bitops.h>
55 #include <asm/io.h>
56 #include <asm/dma.h>
57 #include <linux/errno.h>
58
59 #include <linux/netdevice.h>
60 #include <linux/etherdevice.h>
61 #include <linux/skbuff.h>
62 #include <linux/malloc.h>
63 #include <linux/init.h>
64
65
66 /* use 0 for production, 1 for verification, 2..7 for debug */
67 #ifndef NET_DEBUG
68 #define NET_DEBUG 1
69 #endif
70 static unsigned int net_debug = NET_DEBUG;
71
72 /* A zero-terminated list of common I/O addresses to be probed. */
73 static unsigned int netcard_portlist[] __initdata =
74         { 0x300, 0x320, 0x340, 0x280, 0};
75
76 /*
77                         Details of the i82586.
78
79    You'll really need the databook to understand the details of this part,
80    but the outline is that the i82586 has two separate processing units.
81    Both are started from a list of three configuration tables, of which only
82    the last, the System Control Block (SCB), is used after reset-time.  The SCB
83    has the following fields:
84                 Status word
85                 Command word
86                 Tx/Command block addr.
87                 Rx block addr.
88    The command word accepts the following controls for the Tx and Rx units:
89   */
90
91 #define  CUC_START       0x0100
92 #define  CUC_RESUME      0x0200
93 #define  CUC_SUSPEND 0x0300
94 #define  RX_START        0x0010
95 #define  RX_RESUME       0x0020
96 #define  RX_SUSPEND      0x0030
97
98 /* The Rx unit uses a list of frame descriptors and a list of data buffer
99    descriptors.  We use full-sized (1518 byte) data buffers, so there is
100    a one-to-one pairing of frame descriptors to buffer descriptors.
101
102    The Tx ("command") unit executes a list of commands that look like:
103                 Status word             Written by the 82586 when the command is done.
104                 Command word    Command in lower 3 bits, post-command action in upper 3
105                 Link word               The address of the next command.
106                 Parameters              (as needed).
107
108         Some definitions related to the Command Word are:
109  */
110 #define CMD_EOL         0x8000                  /* The last command of the list, stop. */
111 #define CMD_SUSP        0x4000                  /* Suspend after doing cmd. */
112 #define CMD_INTR        0x2000                  /* Interrupt after doing cmd. */
113
114 enum commands {
115         CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
116         CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
117
118 /* Information that need to be kept for each board. */
119 struct net_local {
120         struct net_device_stats stats;
121         int last_restart;
122         ushort rx_head;
123         ushort rx_tail;
124         ushort tx_head;
125         ushort tx_cmd_link;
126         ushort tx_reap;
127         spinlock_t lock;
128 };
129
130 /*
131                 Details of the EtherLink16 Implementation
132   The 3c507 is a generic shared-memory i82586 implementation.
133   The host can map 16K, 32K, 48K, or 64K of the 64K memory into
134   0x0[CD][08]0000, or all 64K into 0xF[02468]0000.
135   */
136
137 /* Offsets from the base I/O address. */
138 #define SA_DATA         0       /* Station address data, or 3Com signature. */
139 #define MISC_CTRL       6       /* Switch the SA_DATA banks, and bus config bits. */
140 #define RESET_IRQ       10      /* Reset the latched IRQ line. */
141 #define SIGNAL_CA       11      /* Frob the 82586 Channel Attention line. */
142 #define ROM_CONFIG      13
143 #define MEM_CONFIG      14
144 #define IRQ_CONFIG      15
145 #define EL16_IO_EXTENT 16
146
147 /* The ID port is used at boot-time to locate the ethercard. */
148 #define ID_PORT         0x100
149
150 /* Offsets to registers in the mailbox (SCB). */
151 #define iSCB_STATUS     0x8
152 #define iSCB_CMD                0xA
153 #define iSCB_CBL                0xC     /* Command BLock offset. */
154 #define iSCB_RFA                0xE     /* Rx Frame Area offset. */
155
156 /*  Since the 3c507 maps the shared memory window so that the last byte is
157         at 82586 address FFFF, the first byte is at 82586 address 0, 16K, 32K, or
158         48K corresponding to window sizes of 64K, 48K, 32K and 16K respectively.
159         We can account for this be setting the 'SBC Base' entry in the ISCP table
160         below for all the 16 bit offset addresses, and also adding the 'SCB Base'
161         value to all 24 bit physical addresses (in the SCP table and the TX and RX
162         Buffer Descriptors).
163                                         -Mark
164         */
165 #define SCB_BASE                ((unsigned)64*1024 - (dev->mem_end - dev->mem_start))
166
167 /*
168   What follows in 'init_words[]' is the "program" that is downloaded to the
169   82586 memory.  It's mostly tables and command blocks, and starts at the
170   reset address 0xfffff6.  This is designed to be similar to the EtherExpress,
171   thus the unusual location of the SCB at 0x0008.
172
173   Even with the additional "don't care" values, doing it this way takes less
174   program space than initializing the individual tables, and I feel it's much
175   cleaner.
176
177   The databook is particularly useless for the first two structures, I had
178   to use the Crynwr driver as an example.
179
180    The memory setup is as follows:
181    */
182
183 #define CONFIG_CMD      0x0018
184 #define SET_SA_CMD      0x0024
185 #define SA_OFFSET       0x002A
186 #define IDLELOOP        0x30
187 #define TDR_CMD         0x38
188 #define TDR_TIME        0x3C
189 #define DUMP_CMD        0x40
190 #define DIAG_CMD        0x48
191 #define SET_MC_CMD      0x4E
192 #define DUMP_DATA       0x56    /* A 170 byte buffer for dump and Set-MC into. */
193
194 #define TX_BUF_START    0x0100
195 #define NUM_TX_BUFS     4
196 #define TX_BUF_SIZE     (1518+14+20+16) /* packet+header+TBD */
197
198 #define RX_BUF_START    0x2000
199 #define RX_BUF_SIZE     (1518+14+18)    /* packet+header+RBD */
200 #define RX_BUF_END              (dev->mem_end - dev->mem_start)
201
202 #define TX_TIMEOUT 5
203
204 /*
205   That's it: only 86 bytes to set up the beast, including every extra
206   command available.  The 170 byte buffer at DUMP_DATA is shared between the
207   Dump command (called only by the diagnostic program) and the SetMulticastList
208   command.
209
210   To complete the memory setup you only have to write the station address at
211   SA_OFFSET and create the Tx & Rx buffer lists.
212
213   The Tx command chain and buffer list is setup as follows:
214   A Tx command table, with the data buffer pointing to...
215   A Tx data buffer descriptor.  The packet is in a single buffer, rather than
216         chaining together several smaller buffers.
217   A NoOp command, which initially points to itself,
218   And the packet data.
219
220   A transmit is done by filling in the Tx command table and data buffer,
221   re-writing the NoOp command, and finally changing the offset of the last
222   command to point to the current Tx command.  When the Tx command is finished,
223   it jumps to the NoOp, when it loops until the next Tx command changes the
224   "link offset" in the NoOp.  This way the 82586 never has to go through the
225   slow restart sequence.
226
227   The Rx buffer list is set up in the obvious ring structure.  We have enough
228   memory (and low enough interrupt latency) that we can avoid the complicated
229   Rx buffer linked lists by alway associating a full-size Rx data buffer with
230   each Rx data frame.
231
232   I current use four transmit buffers starting at TX_BUF_START (0x0100), and
233   use the rest of memory, from RX_BUF_START to RX_BUF_END, for Rx buffers.
234
235   */
236
237 static unsigned short init_words[] = {
238         /*      System Configuration Pointer (SCP). */
239         0x0000,                                 /* Set bus size to 16 bits. */
240         0,0,                                    /* pad words. */
241         0x0000,0x0000,                  /* ISCP phys addr, set in init_82586_mem(). */
242
243         /*      Intermediate System Configuration Pointer (ISCP). */
244         0x0001,                                 /* Status word that's cleared when init is done. */
245         0x0008,0,0,                             /* SCB offset, (skip, skip) */
246
247         /* System Control Block (SCB). */
248         0,0xf000|RX_START|CUC_START,    /* SCB status and cmd. */
249         CONFIG_CMD,                             /* Command list pointer, points to Configure. */
250         RX_BUF_START,                           /* Rx block list. */
251         0,0,0,0,                                /* Error count: CRC, align, buffer, overrun. */
252
253         /* 0x0018: Configure command.  Change to put MAC data with packet. */
254         0, CmdConfigure,                /* Status, command.             */
255         SET_SA_CMD,                             /* Next command is Set Station Addr. */
256         0x0804,                                 /* "4" bytes of config data, 8 byte FIFO. */
257         0x2e40,                                 /* Magic values, including MAC data location. */
258         0,                                              /* Unused pad word. */
259
260         /* 0x0024: Setup station address command. */
261         0, CmdSASetup,
262         SET_MC_CMD,                             /* Next command. */
263         0xaa00,0xb000,0x0bad,   /* Station address (to be filled in) */
264
265         /* 0x0030: NOP, looping back to itself.  Point to first Tx buffer to Tx. */
266         0, CmdNOp, IDLELOOP, 0 /* pad */,
267
268         /* 0x0038: A unused Time-Domain Reflectometer command. */
269         0, CmdTDR, IDLELOOP, 0,
270
271         /* 0x0040: An unused Dump State command. */
272         0, CmdDump, IDLELOOP, DUMP_DATA,
273
274         /* 0x0048: An unused Diagnose command. */
275         0, CmdDiagnose, IDLELOOP,
276
277         /* 0x004E: An empty set-multicast-list command. */
278         0, CmdMulticastList, IDLELOOP, 0,
279 };
280
281 /* Index to functions, as function prototypes. */
282
283 extern int el16_probe(struct net_device *dev);  /* Called from Space.c */
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 void     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);
295 static void init_82586_mem(struct net_device *dev);
296
297 \f
298 /* Check for a network adaptor of this type, and return '0' iff one exists.
299         If dev->base_addr == 0, probe all likely locations.
300         If dev->base_addr == 1, always return failure.
301         If dev->base_addr == 2, (detachable devices only) allocate space for the
302         device and return success.
303         */
304
305 int __init el16_probe(struct net_device *dev)
306 {
307         int base_addr = dev->base_addr;
308         int i;
309
310         SET_MODULE_OWNER(dev);
311
312         if (base_addr > 0x1ff)  /* Check a single specified location. */
313                 return el16_probe1(dev, base_addr);
314         else if (base_addr != 0)
315                 return -ENXIO;          /* Don't probe at all. */
316
317         for (i = 0; netcard_portlist[i]; i++)
318                 if (el16_probe1(dev, netcard_portlist[i]) == 0)
319                         return 0;
320
321         return -ENODEV;
322 }
323
324 static int __init el16_probe1(struct net_device *dev, int ioaddr)
325 {
326         static unsigned char init_ID_done = 0, version_printed = 0;
327         int i, irq, irqval, retval;
328         struct net_local *lp;
329
330         if (init_ID_done == 0) {
331                 ushort lrs_state = 0xff;
332                 /* Send the ID sequence to the ID_PORT to enable the board(s). */
333                 outb(0x00, ID_PORT);
334                 for(i = 0; i < 255; i++) {
335                         outb(lrs_state, ID_PORT);
336                         lrs_state <<= 1;
337                         if (lrs_state & 0x100)
338                                 lrs_state ^= 0xe7;
339                 }
340                 outb(0x00, ID_PORT);
341                 init_ID_done = 1;
342         }
343
344         if (!request_region(ioaddr, EL16_IO_EXTENT, dev->name))
345                 return -ENODEV;
346
347         if ((inb(ioaddr) != '*') || (inb(ioaddr + 1) != '3') || 
348             (inb(ioaddr + 2) != 'C') || (inb(ioaddr + 3) != 'O')) {
349                 retval = -ENODEV;
350                 goto out;
351         }
352
353         if (net_debug  &&  version_printed++ == 0)
354                 printk(version);
355
356         printk("%s: 3c507 at %#x,", dev->name, ioaddr);
357
358         /* We should make a few more checks here, like the first three octets of
359            the S.A. for the manufacturer's code. */
360
361         irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
362
363         irqval = request_irq(irq, &el16_interrupt, 0, dev->name, dev);
364         if (irqval) {
365                 printk ("unable to get IRQ %d (irqval=%d).\n", irq, irqval);
366                 retval = -EAGAIN;
367                 goto out;
368         }
369
370         /* We've committed to using the board, and can start filling in *dev. */
371         dev->base_addr = ioaddr;
372
373         outb(0x01, ioaddr + MISC_CTRL);
374         for (i = 0; i < 6; i++) {
375                 dev->dev_addr[i] = inb(ioaddr + i);
376                 printk(" %02x", dev->dev_addr[i]);
377         }
378
379         if ((dev->mem_start & 0xf) > 0)
380                 net_debug = dev->mem_start & 7;
381
382 #ifdef MEM_BASE
383         dev->mem_start = MEM_BASE;
384         dev->mem_end = dev->mem_start + 0x10000;
385 #else
386         {
387                 int base;
388                 int size;
389                 char mem_config = inb(ioaddr + MEM_CONFIG);
390                 if (mem_config & 0x20) {
391                         size = 64*1024;
392                         base = 0xf00000 + (mem_config & 0x08 ? 0x080000
393                                                            : ((mem_config & 3) << 17));
394                 } else {
395                         size = ((mem_config & 3) + 1) << 14;
396                         base = 0x0c0000 + ( (mem_config & 0x18) << 12);
397                 }
398                 dev->mem_start = base;
399                 dev->mem_end = base + size;
400         }
401 #endif
402
403         dev->if_port = (inb(ioaddr + ROM_CONFIG) & 0x80) ? 1 : 0;
404         dev->irq = inb(ioaddr + IRQ_CONFIG) & 0x0f;
405
406         printk(", IRQ %d, %sternal xcvr, memory %#lx-%#lx.\n", dev->irq,
407                    dev->if_port ? "ex" : "in", dev->mem_start, dev->mem_end-1);
408
409         if (net_debug)
410                 printk(version);
411
412         /* Initialize the device structure. */
413         lp = dev->priv = kmalloc(sizeof(struct net_local), GFP_KERNEL);
414         if (dev->priv == NULL) {
415                 retval = -ENOMEM;
416                 goto out;
417         }
418         memset(dev->priv, 0, sizeof(struct net_local));
419         spin_lock_init(&lp->lock);
420
421         dev->open               = el16_open;
422         dev->stop               = el16_close;
423         dev->hard_start_xmit = el16_send_packet;
424         dev->get_stats  = el16_get_stats;
425         dev->tx_timeout = el16_tx_timeout;
426         dev->watchdog_timeo = TX_TIMEOUT;
427
428         ether_setup(dev);       /* Generic ethernet behaviour */
429
430         dev->flags&=~IFF_MULTICAST;     /* Multicast doesn't work */
431
432         return 0;
433 out:
434         release_region(ioaddr, EL16_IO_EXTENT);
435         return retval;
436 }
437
438 static int el16_open(struct net_device *dev)
439 {
440         /* Initialize the 82586 memory and start it. */
441         init_82586_mem(dev);
442
443         netif_start_queue(dev);
444         return 0;
445 }
446
447
448 static void el16_tx_timeout (struct net_device *dev)
449 {
450         struct net_local *lp = (struct net_local *) dev->priv;
451         int ioaddr = dev->base_addr;
452         unsigned long shmem = dev->mem_start;
453
454         if (net_debug > 1)
455                 printk ("%s: transmit timed out, %s?  ", dev->name,
456                         isa_readw (shmem + iSCB_STATUS) & 0x8000 ? "IRQ conflict" :
457                         "network cable problem");
458         /* Try to restart the adaptor. */
459         if (lp->last_restart == lp->stats.tx_packets) {
460                 if (net_debug > 1)
461                         printk ("Resetting board.\n");
462                 /* Completely reset the adaptor. */
463                 init_82586_mem (dev);
464         } else {
465                 /* Issue the channel attention signal and hope it "gets better". */
466                 if (net_debug > 1)
467                         printk ("Kicking board.\n");
468                 isa_writew (0xf000 | CUC_START | RX_START, shmem + iSCB_CMD);
469                 outb (0, ioaddr + SIGNAL_CA);   /* Issue channel-attn. */
470                 lp->last_restart = lp->stats.tx_packets;
471         }
472         dev->trans_start = jiffies;
473         netif_wake_queue (dev);
474 }
475
476
477 static int el16_send_packet (struct sk_buff *skb, struct net_device *dev)
478 {
479         struct net_local *lp = (struct net_local *) dev->priv;
480         int ioaddr = dev->base_addr;
481         unsigned long flags;
482         short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
483         unsigned char *buf = skb->data;
484
485         netif_stop_queue (dev);
486
487         spin_lock_irqsave (&lp->lock, flags);
488
489         lp->stats.tx_bytes += length;
490         /* Disable the 82586's input to the interrupt line. */
491         outb (0x80, ioaddr + MISC_CTRL);
492
493         hardware_send_packet (dev, buf, length);
494
495         dev->trans_start = jiffies;
496         /* Enable the 82586 interrupt input. */
497         outb (0x84, ioaddr + MISC_CTRL);
498
499         spin_unlock_irqrestore (&lp->lock, flags);
500
501         dev_kfree_skb (skb);
502
503         /* You might need to clean up and record Tx statistics here. */
504
505         return 0;
506 }
507
508 /*      The typical workload of the driver:
509         Handle the network interface interrupts. */
510 static void el16_interrupt(int irq, void *dev_id, struct pt_regs *regs)
511 {
512         struct net_device *dev = dev_id;
513         struct net_local *lp;
514         int ioaddr, status, boguscount = 0;
515         ushort ack_cmd = 0;
516         unsigned long shmem;
517
518         if (dev == NULL) {
519                 printk ("net_interrupt(): irq %d for unknown device.\n", irq);
520                 return;
521         }
522
523         ioaddr = dev->base_addr;
524         lp = (struct net_local *)dev->priv;
525         shmem = dev->mem_start;
526
527         spin_lock(&lp->lock);
528
529         status = isa_readw(shmem+iSCB_STATUS);
530
531         if (net_debug > 4) {
532                 printk("%s: 3c507 interrupt, status %4.4x.\n", dev->name, status);
533         }
534
535         /* Disable the 82586's input to the interrupt line. */
536         outb(0x80, ioaddr + MISC_CTRL);
537
538         /* Reap the Tx packet buffers. */
539         while (lp->tx_reap != lp->tx_head) {
540           unsigned short tx_status = isa_readw(shmem+lp->tx_reap);
541
542           if (tx_status == 0) {
543                 if (net_debug > 5)  printk("Couldn't reap %#x.\n", lp->tx_reap);
544                 break;
545           }
546           if (tx_status & 0x2000) {
547                 lp->stats.tx_packets++;
548                 lp->stats.collisions += tx_status & 0xf;
549                 netif_wake_queue(dev);
550           } else {
551                 lp->stats.tx_errors++;
552                 if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
553                 if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
554                 if (!(tx_status & 0x0040))  lp->stats.tx_heartbeat_errors++;
555                 if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
556           }
557           if (net_debug > 5)
558                   printk("Reaped %x, Tx status %04x.\n" , lp->tx_reap, tx_status);
559           lp->tx_reap += TX_BUF_SIZE;
560           if (lp->tx_reap > RX_BUF_START - TX_BUF_SIZE)
561                 lp->tx_reap = TX_BUF_START;
562           if (++boguscount > 4)
563                 break;
564         }
565
566         if (status & 0x4000) { /* Packet received. */
567                 if (net_debug > 5)
568                         printk("Received packet, rx_head %04x.\n", lp->rx_head);
569                 el16_rx(dev);
570         }
571
572         /* Acknowledge the interrupt sources. */
573         ack_cmd = status & 0xf000;
574
575         if ((status & 0x0700) != 0x0200 && netif_running(dev)) {
576                 if (net_debug)
577                         printk("%s: Command unit stopped, status %04x, restarting.\n",
578                                    dev->name, status);
579                 /* If this ever occurs we should really re-write the idle loop, reset
580                    the Tx list, and do a complete restart of the command unit.
581                    For now we rely on the Tx timeout if the resume doesn't work. */
582                 ack_cmd |= CUC_RESUME;
583         }
584
585         if ((status & 0x0070) != 0x0040 && netif_running(dev)) {
586                 static void init_rx_bufs(struct net_device *);
587                 /* The Rx unit is not ready, it must be hung.  Restart the receiver by
588                    initializing the rx buffers, and issuing an Rx start command. */
589                 if (net_debug)
590                         printk("%s: Rx unit stopped, status %04x, restarting.\n",
591                                    dev->name, status);
592                 init_rx_bufs(dev);
593                 isa_writew(RX_BUF_START,shmem+iSCB_RFA);
594                 ack_cmd |= RX_START;
595         }
596
597         isa_writew(ack_cmd,shmem+iSCB_CMD);
598         outb(0, ioaddr + SIGNAL_CA);                    /* Issue channel-attn. */
599
600         /* Clear the latched interrupt. */
601         outb(0, ioaddr + RESET_IRQ);
602
603         /* Enable the 82586's interrupt input. */
604         outb(0x84, ioaddr + MISC_CTRL);
605         spin_unlock(&lp->lock);
606 }
607
608 static int el16_close(struct net_device *dev)
609 {
610         int ioaddr = dev->base_addr;
611         unsigned long shmem = dev->mem_start;
612
613         netif_stop_queue(dev);
614
615         /* Flush the Tx and disable Rx. */
616         isa_writew(RX_SUSPEND | CUC_SUSPEND,shmem+iSCB_CMD);
617         outb(0, ioaddr + SIGNAL_CA);
618
619         /* Disable the 82586's input to the interrupt line. */
620         outb(0x80, ioaddr + MISC_CTRL);
621
622         /* We always physically use the IRQ line, so we don't do free_irq(). */
623
624         /* Update the statistics here. */
625
626         return 0;
627 }
628
629 /* Get the current statistics.  This may be called with the card open or
630    closed. */
631 static struct net_device_stats *el16_get_stats(struct net_device *dev)
632 {
633         struct net_local *lp = (struct net_local *)dev->priv;
634
635         /* ToDo: decide if there are any useful statistics from the SCB. */
636
637         return &lp->stats;
638 }
639
640 /* Initialize the Rx-block list. */
641 static void init_rx_bufs(struct net_device *dev)
642 {
643         struct net_local *lp = (struct net_local *)dev->priv;
644         unsigned long write_ptr;
645         unsigned short SCB_base = SCB_BASE;
646
647         int cur_rxbuf = lp->rx_head = RX_BUF_START;
648
649         /* Initialize each Rx frame + data buffer. */
650         do {    /* While there is room for one more. */
651
652           write_ptr = dev->mem_start + cur_rxbuf;
653
654                 isa_writew(0x0000,write_ptr);                   /* Status */
655                 isa_writew(0x0000,write_ptr+=2);                        /* Command */
656                 isa_writew(cur_rxbuf + RX_BUF_SIZE,write_ptr+=2);       /* Link */
657                 isa_writew(cur_rxbuf + 22,write_ptr+=2);                /* Buffer offset */
658                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for dest addr. */
659                 isa_writew(0x0000,write_ptr+=2);
660                 isa_writew(0x0000,write_ptr+=2);
661                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for source addr. */
662                 isa_writew(0x0000,write_ptr+=2);
663                 isa_writew(0x0000,write_ptr+=2);
664                 isa_writew(0x0000,write_ptr+=2);                        /* Pad for protocol. */
665
666                 isa_writew(0x0000,write_ptr+=2);                        /* Buffer: Actual count */
667                 isa_writew(-1,write_ptr+=2);                    /* Buffer: Next (none). */
668                 isa_writew(cur_rxbuf + 0x20 + SCB_base,write_ptr+=2);/* Buffer: Address low */
669                 isa_writew(0x0000,write_ptr+=2);
670                 /* Finally, the number of bytes in the buffer. */
671                 isa_writew(0x8000 + RX_BUF_SIZE-0x20,write_ptr+=2);
672
673                 lp->rx_tail = cur_rxbuf;
674                 cur_rxbuf += RX_BUF_SIZE;
675         } while (cur_rxbuf <= RX_BUF_END - RX_BUF_SIZE);
676
677         /* Terminate the list by setting the EOL bit, and wrap the pointer to make
678            the list a ring. */
679         write_ptr = dev->mem_start + lp->rx_tail + 2;
680         isa_writew(0xC000,write_ptr);                           /* Command, mark as last. */
681         isa_writew(lp->rx_head,write_ptr+2);                    /* Link */
682 }
683
684 static void init_82586_mem(struct net_device *dev)
685 {
686         struct net_local *lp = (struct net_local *)dev->priv;
687         short ioaddr = dev->base_addr;
688         unsigned long shmem = dev->mem_start;
689
690         /* Enable loopback to protect the wire while starting up,
691            and hold the 586 in reset during the memory initialization. */
692         outb(0x20, ioaddr + MISC_CTRL);
693
694         /* Fix the ISCP address and base. */
695         init_words[3] = SCB_BASE;
696         init_words[7] = SCB_BASE;
697
698         /* Write the words at 0xfff6 (address-aliased to 0xfffff6). */
699         isa_memcpy_toio(dev->mem_end-10, init_words, 10);
700
701         /* Write the words at 0x0000. */
702         isa_memcpy_toio(dev->mem_start, init_words + 5, sizeof(init_words) - 10);
703
704         /* Fill in the station address. */
705         isa_memcpy_toio(dev->mem_start+SA_OFFSET, dev->dev_addr,
706                    sizeof(dev->dev_addr));
707
708         /* The Tx-block list is written as needed.  We just set up the values. */
709         lp->tx_cmd_link = IDLELOOP + 4;
710         lp->tx_head = lp->tx_reap = TX_BUF_START;
711
712         init_rx_bufs(dev);
713
714         /* Start the 586 by releasing the reset line, but leave loopback. */
715         outb(0xA0, ioaddr + MISC_CTRL);
716
717         /* This was time consuming to track down: you need to give two channel
718            attention signals to reliably start up the i82586. */
719         outb(0, ioaddr + SIGNAL_CA);
720
721         {
722                 int boguscnt = 50;
723                 while (isa_readw(shmem+iSCB_STATUS) == 0)
724                         if (--boguscnt == 0) {
725                                 printk("%s: i82586 initialization timed out with status %04x,"
726                                            "cmd %04x.\n", dev->name,
727                                            isa_readw(shmem+iSCB_STATUS), isa_readw(shmem+iSCB_CMD));
728                                 break;
729                         }
730                 /* Issue channel-attn -- the 82586 won't start. */
731                 outb(0, ioaddr + SIGNAL_CA);
732         }
733
734         /* Disable loopback and enable interrupts. */
735         outb(0x84, ioaddr + MISC_CTRL);
736         if (net_debug > 4)
737                 printk("%s: Initialized 82586, status %04x.\n", dev->name,
738                            isa_readw(shmem+iSCB_STATUS));
739         return;
740 }
741
742 static void hardware_send_packet(struct net_device *dev, void *buf, short length)
743 {
744         struct net_local *lp = (struct net_local *)dev->priv;
745         short ioaddr = dev->base_addr;
746         ushort tx_block = lp->tx_head;
747         unsigned long write_ptr = dev->mem_start + tx_block;
748
749         /* Set the write pointer to the Tx block, and put out the header. */
750         isa_writew(0x0000,write_ptr);                   /* Tx status */
751         isa_writew(CMD_INTR|CmdTx,write_ptr+=2);                /* Tx command */
752         isa_writew(tx_block+16,write_ptr+=2);           /* Next command is a NoOp. */
753         isa_writew(tx_block+8,write_ptr+=2);                    /* Data Buffer offset. */
754
755         /* Output the data buffer descriptor. */
756         isa_writew(length | 0x8000,write_ptr+=2);               /* Byte count parameter. */
757         isa_writew(-1,write_ptr+=2);                    /* No next data buffer. */
758         isa_writew(tx_block+22+SCB_BASE,write_ptr+=2);  /* Buffer follows the NoOp command. */
759         isa_writew(0x0000,write_ptr+=2);                        /* Buffer address high bits (always zero). */
760
761         /* Output the Loop-back NoOp command. */
762         isa_writew(0x0000,write_ptr+=2);                        /* Tx status */
763         isa_writew(CmdNOp,write_ptr+=2);                        /* Tx command */
764         isa_writew(tx_block+16,write_ptr+=2);           /* Next is myself. */
765
766         /* Output the packet at the write pointer. */
767         isa_memcpy_toio(write_ptr+2, buf, length);
768
769         /* Set the old command link pointing to this send packet. */
770         isa_writew(tx_block,dev->mem_start + lp->tx_cmd_link);
771         lp->tx_cmd_link = tx_block + 20;
772
773         /* Set the next free tx region. */
774         lp->tx_head = tx_block + TX_BUF_SIZE;
775         if (lp->tx_head > RX_BUF_START - TX_BUF_SIZE)
776                 lp->tx_head = TX_BUF_START;
777
778         if (net_debug > 4) {
779                 printk("%s: 3c507 @%x send length = %d, tx_block %3x, next %3x.\n",
780                            dev->name, ioaddr, length, tx_block, lp->tx_head);
781         }
782
783         if (lp->tx_head != lp->tx_reap)
784                 netif_wake_queue(dev);
785 }
786
787 static void el16_rx(struct net_device *dev)
788 {
789         struct net_local *lp = (struct net_local *)dev->priv;
790         unsigned long shmem = dev->mem_start;
791         ushort rx_head = lp->rx_head;
792         ushort rx_tail = lp->rx_tail;
793         ushort boguscount = 10;
794         short frame_status;
795
796         while ((frame_status = isa_readw(shmem+rx_head)) < 0) {   /* Command complete */
797                 unsigned long read_frame = dev->mem_start + rx_head;
798                 ushort rfd_cmd = isa_readw(read_frame+2);
799                 ushort next_rx_frame = isa_readw(read_frame+4);
800                 ushort data_buffer_addr = isa_readw(read_frame+6);
801                 unsigned long data_frame = dev->mem_start + data_buffer_addr;
802                 ushort pkt_len = isa_readw(data_frame);
803
804                 if (rfd_cmd != 0 || data_buffer_addr != rx_head + 22
805                         || (pkt_len & 0xC000) != 0xC000) {
806                         printk("%s: Rx frame at %#x corrupted, status %04x cmd %04x"
807                                    "next %04x data-buf @%04x %04x.\n", dev->name, rx_head,
808                                    frame_status, rfd_cmd, next_rx_frame, data_buffer_addr,
809                                    pkt_len);
810                 } else if ((frame_status & 0x2000) == 0) {
811                         /* Frame Rxed, but with error. */
812                         lp->stats.rx_errors++;
813                         if (frame_status & 0x0800) lp->stats.rx_crc_errors++;
814                         if (frame_status & 0x0400) lp->stats.rx_frame_errors++;
815                         if (frame_status & 0x0200) lp->stats.rx_fifo_errors++;
816                         if (frame_status & 0x0100) lp->stats.rx_over_errors++;
817                         if (frame_status & 0x0080) lp->stats.rx_length_errors++;
818                 } else {
819                         /* Malloc up new buffer. */
820                         struct sk_buff *skb;
821
822                         pkt_len &= 0x3fff;
823                         skb = dev_alloc_skb(pkt_len+2);
824                         if (skb == NULL) {
825                                 printk("%s: Memory squeeze, dropping packet.\n", dev->name);
826                                 lp->stats.rx_dropped++;
827                                 break;
828                         }
829
830                         skb_reserve(skb,2);
831                         skb->dev = dev;
832
833                         /* 'skb->data' points to the start of sk_buff data area. */
834                         isa_memcpy_fromio(skb_put(skb,pkt_len), data_frame + 10, pkt_len);
835
836                         skb->protocol=eth_type_trans(skb,dev);
837                         netif_rx(skb);
838                         lp->stats.rx_packets++;
839                 }
840
841                 /* Clear the status word and set End-of-List on the rx frame. */
842                 isa_writew(0,read_frame);
843                 isa_writew(0xC000,read_frame+2);
844                 /* Clear the end-of-list on the prev. RFD. */
845                 isa_writew(0x0000,dev->mem_start + rx_tail + 2);
846
847                 rx_tail = rx_head;
848                 rx_head = next_rx_frame;
849                 if (--boguscount == 0)
850                         break;
851         }
852
853         lp->rx_head = rx_head;
854         lp->rx_tail = rx_tail;
855 }
856 #ifdef MODULE
857 static struct net_device dev_3c507;
858 static int io = 0x300;
859 static int irq = 0;
860 MODULE_PARM(io, "i");
861 MODULE_PARM(irq, "i");
862
863 int init_module(void)
864 {
865         if (io == 0)
866                 printk("3c507: You should not use auto-probing with insmod!\n");
867         dev_3c507.base_addr = io;
868         dev_3c507.irq       = irq;
869         dev_3c507.init      = el16_probe;
870         if (register_netdev(&dev_3c507) != 0) {
871                 printk("3c507: register_netdev() returned non-zero.\n");
872                 return -EIO;
873         }
874         return 0;
875 }
876
877 void
878 cleanup_module(void)
879 {
880         unregister_netdev(&dev_3c507);
881         kfree(dev_3c507.priv);
882         dev_3c507.priv = NULL;
883
884         /* If we don't do this, we can't re-insmod it later. */
885         free_irq(dev_3c507.irq, &dev_3c507);
886         release_region(dev_3c507.base_addr, EL16_IO_EXTENT);
887 }
888 #endif /* MODULE */
889 \f
890 /*
891  * Local variables:
892  *  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"
893  *  version-control: t
894  *  kept-new-versions: 5
895  *  tab-width: 4
896  *  c-indent-level: 4
897  * End:
898  */