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