Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / net / lp486e.c
1 /* Intel Professional Workstation/panther ethernet driver */
2 /* lp486e.c: A panther 82596 ethernet driver for linux. */
3 /*
4     History and copyrights:
5
6     Driver skeleton
7         Written 1993 by Donald Becker.
8         Copyright 1993 United States Government as represented by the Director,
9         National Security Agency.  This software may only be used and
10         distributed according to the terms of the GNU General Public License
11         as modified by SRC, incorporated herein by reference.
12
13         The author may be reached as becker@scyld.com, or C/O
14         Scyld Computing Corporation
15         410 Severn Ave., Suite 210
16         Annapolis MD 21403
17
18     Apricot
19         Written 1994 by Mark Evans.
20         This driver is for the Apricot 82596 bus-master interface
21
22         Modularised 12/94 Mark Evans
23
24     Professional Workstation
25         Derived from apricot.c by Ard van Breemen
26         <ard@murphy.nl>|<ard@cstmel.hobby.nl>|<ard@cstmel.nl.eu.org>
27
28         Credits:
29         Thanks to Murphy Software BV for letting me write this in their time.
30         Well, actually, I get payed doing this...
31         (Also: see http://www.murphy.nl for murphy, and my homepage ~ard for
32         more information on the Professional Workstation)
33
34     Present version
35         aeb@cwi.nl
36 */
37 /*
38     There are currently two motherboards that I know of in the
39     professional workstation. The only one that I know is the
40     intel panther motherboard. -- ard
41 */
42 /*
43 The pws is equipped with an intel 82596. This is a very intelligent controller
44 which runs its own micro-code. Communication with the hostprocessor is done
45 through linked lists of commands and buffers in the hostprocessors memory.
46 A complete description of the 82596 is available from intel. Search for
47 a file called "29021806.pdf". It is a complete description of the chip itself.
48 To use it for the pws some additions are needed regarding generation of
49 the PORT and CA signal, and the interrupt glue needed for a pc.
50 I/O map:
51 PORT  SIZE ACTION MEANING
52 0xCB0    2 WRITE  Lower 16 bits for PORT command
53 0xCB2    2 WRITE  Upper 16 bits for PORT command, and issue of PORT command
54 0xCB4    1 WRITE  Generation of CA signal
55 0xCB8    1 WRITE  Clear interrupt glue
56 All other communication is through memory!
57 */
58
59 #define SLOW_DOWN_IO udelay(5)
60
61 #include <linux/module.h>
62 #include <linux/init.h>
63 #include <linux/delay.h>
64 #include <linux/kernel.h>
65 #include <linux/string.h>
66 #include <linux/errno.h>
67 #include <linux/ioport.h>
68 #include <linux/slab.h>
69 #include <linux/interrupt.h>
70 #include <linux/netdevice.h>
71 #include <linux/etherdevice.h>
72 #include <linux/skbuff.h>
73 #include <linux/bitops.h>
74
75 #include <asm/io.h>
76 #include <asm/dma.h>
77
78 #define DRV_NAME "lp486e"
79
80 /* debug print flags */
81 #define LOG_SRCDST    0x80000000
82 #define LOG_STATINT   0x40000000
83 #define LOG_STARTINT  0x20000000
84
85 #define i596_debug debug
86
87 static int i596_debug = 0;
88
89 static const char * const medianame[] = {
90         "10baseT", "AUI",
91         "10baseT-FD", "AUI-FD",
92 };
93
94 #define LP486E_TOTAL_SIZE 16
95
96 #define I596_NULL (0xffffffff)
97
98 #define CMD_EOL         0x8000  /* The last command of the list, stop. */
99 #define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
100 #define CMD_INTR        0x2000  /* Interrupt after doing cmd. */
101
102 #define CMD_FLEX        0x0008  /* Enable flexible memory model */
103
104 enum commands {
105         CmdNOP = 0,
106         CmdIASetup = 1,
107         CmdConfigure = 2,
108         CmdMulticastList = 3,
109         CmdTx = 4,
110         CmdTDR = 5,
111         CmdDump = 6,
112         CmdDiagnose = 7
113 };
114
115 #if 0
116 static const char *CUcmdnames[8] = { "NOP", "IASetup", "Configure", "MulticastList",
117                                      "Tx", "TDR", "Dump", "Diagnose" };
118 #endif
119
120 /* Status word bits */
121 #define STAT_CX         0x8000  /* The CU finished executing a command
122                                    with the Interrupt bit set */
123 #define STAT_FR         0x4000  /* The RU finished receiving a frame */
124 #define STAT_CNA        0x2000  /* The CU left the active state */
125 #define STAT_RNR        0x1000  /* The RU left the active state */
126 #define STAT_ACK        (STAT_CX | STAT_FR | STAT_CNA | STAT_RNR)
127 #define STAT_CUS        0x0700  /* Status of CU: 0: idle, 1: suspended,
128                                    2: active, 3-7: unused */
129 #define STAT_RUS        0x00f0  /* Status of RU: 0: idle, 1: suspended,
130                                    2: no resources, 4: ready,
131                                    10: no resources due to no more RBDs,
132                                    12: no more RBDs, other: unused */
133 #define STAT_T          0x0008  /* Bus throttle timers loaded */
134 #define STAT_ZERO       0x0807  /* Always zero */
135
136 #if 0
137 static char *CUstates[8] = {
138         "idle", "suspended", "active", 0, 0, 0, 0, 0
139 };
140 static char *RUstates[16] = {
141         "idle", "suspended", "no resources", 0, "ready", 0, 0, 0,
142         0, 0, "no RBDs", 0, "out of RBDs", 0, 0, 0
143 };
144
145 static void
146 i596_out_status(int status) {
147         int bad = 0;
148         char *s;
149
150         printk("status %4.4x:", status);
151         if (status == 0xffff)
152                 printk(" strange..\n");
153         else {
154                 if (status & STAT_CX)
155                         printk("  CU done");
156                 if (status & STAT_CNA)
157                         printk("  CU stopped");
158                 if (status & STAT_FR)
159                         printk("  got a frame");
160                 if (status & STAT_RNR)
161                         printk("  RU stopped");
162                 if (status & STAT_T)
163                         printk("  throttled");
164                 if (status & STAT_ZERO)
165                         bad = 1;
166                 s = CUstates[(status & STAT_CUS) >> 8];
167                 if (!s)
168                         bad = 1;
169                 else
170                         printk("  CU(%s)", s);
171                 s = RUstates[(status & STAT_RUS) >> 4];
172                 if (!s)
173                         bad = 1;
174                 else
175                         printk("  RU(%s)", s);
176                 if (bad)
177                         printk("  bad status");
178                 printk("\n");
179         }
180 }
181 #endif
182
183 /* Command word bits */
184 #define ACK_CX          0x8000
185 #define ACK_FR          0x4000
186 #define ACK_CNA         0x2000
187 #define ACK_RNR         0x1000
188
189 #define CUC_START       0x0100
190 #define CUC_RESUME      0x0200
191 #define CUC_SUSPEND     0x0300
192 #define CUC_ABORT       0x0400
193
194 #define RX_START        0x0010
195 #define RX_RESUME       0x0020
196 #define RX_SUSPEND      0x0030
197 #define RX_ABORT        0x0040
198
199 typedef u32 phys_addr;
200
201 static inline phys_addr
202 va_to_pa(void *x) {
203         return x ? virt_to_bus(x) : I596_NULL;
204 }
205
206 static inline void *
207 pa_to_va(phys_addr x) {
208         return (x == I596_NULL) ? NULL : bus_to_virt(x);
209 }
210
211 /* status bits for cmd */
212 #define CMD_STAT_C      0x8000  /* CU command complete */
213 #define CMD_STAT_B      0x4000  /* CU command in progress */
214 #define CMD_STAT_OK     0x2000  /* CU command completed without errors */
215 #define CMD_STAT_A      0x1000  /* CU command abnormally terminated */
216
217 struct i596_cmd {               /* 8 bytes */
218         unsigned short status;
219         unsigned short command;
220         phys_addr pa_next;      /* va_to_pa(struct i596_cmd *next) */
221 };
222
223 #define EOF             0x8000
224 #define SIZE_MASK       0x3fff
225
226 struct i596_tbd {
227         unsigned short size;
228         unsigned short pad;
229         phys_addr pa_next;      /* va_to_pa(struct i596_tbd *next) */
230         phys_addr pa_data;      /* va_to_pa(char *data) */
231         struct sk_buff *skb;
232 };
233
234 struct tx_cmd {
235         struct i596_cmd cmd;
236         phys_addr pa_tbd;       /* va_to_pa(struct i596_tbd *tbd) */
237         unsigned short size;
238         unsigned short pad;
239 };
240
241 /* status bits for rfd */
242 #define RFD_STAT_C      0x8000  /* Frame reception complete */
243 #define RFD_STAT_B      0x4000  /* Frame reception in progress */
244 #define RFD_STAT_OK     0x2000  /* Frame received without errors */
245 #define RFD_STATUS      0x1fff
246 #define RFD_LENGTH_ERR  0x1000
247 #define RFD_CRC_ERR     0x0800
248 #define RFD_ALIGN_ERR   0x0400
249 #define RFD_NOBUFS_ERR  0x0200
250 #define RFD_DMA_ERR     0x0100  /* DMA overrun failure to acquire system bus */
251 #define RFD_SHORT_FRAME_ERR     0x0080
252 #define RFD_NOEOP_ERR   0x0040
253 #define RFD_TRUNC_ERR   0x0020
254 #define RFD_MULTICAST  0x0002   /* 0: destination had our address
255                                    1: destination was broadcast/multicast */
256 #define RFD_COLLISION  0x0001
257
258 /* receive frame descriptor */
259 struct i596_rfd {
260         unsigned short stat;
261         unsigned short cmd;
262         phys_addr pa_next;      /* va_to_pa(struct i596_rfd *next) */
263         phys_addr pa_rbd;       /* va_to_pa(struct i596_rbd *rbd) */
264         unsigned short count;
265         unsigned short size;
266         char data[1532];
267 };
268
269 #define RBD_EL          0x8000
270 #define RBD_P           0x4000
271 #define RBD_SIZEMASK    0x3fff
272 #define RBD_EOF         0x8000
273 #define RBD_F           0x4000
274
275 /* receive buffer descriptor */
276 struct i596_rbd {
277         unsigned short size;
278         unsigned short pad;
279         phys_addr pa_next;      /* va_to_pa(struct i596_tbd *next) */
280         phys_addr pa_data;      /* va_to_pa(char *data) */
281         phys_addr pa_prev;      /* va_to_pa(struct i596_tbd *prev) */
282         
283         /* Driver private part */
284         struct sk_buff *skb;
285 };
286
287 #define RX_RING_SIZE 64
288 #define RX_SKBSIZE (ETH_FRAME_LEN+10)
289 #define RX_RBD_SIZE 32
290
291 /* System Control Block - 40 bytes */
292 struct i596_scb {
293         u16 status;             /* 0 */
294         u16 command;            /* 2 */
295         phys_addr pa_cmd;       /* 4 - va_to_pa(struct i596_cmd *cmd) */
296         phys_addr pa_rfd;       /* 8 - va_to_pa(struct i596_rfd *rfd) */
297         u32 crc_err;            /* 12 */
298         u32 align_err;          /* 16 */
299         u32 resource_err;       /* 20 */
300         u32 over_err;           /* 24 */
301         u32 rcvdt_err;          /* 28 */
302         u32 short_err;          /* 32 */
303         u16 t_on;               /* 36 */
304         u16 t_off;              /* 38 */
305 };
306
307 /* Intermediate System Configuration Pointer - 8 bytes */
308 struct i596_iscp {
309         u32 busy;               /* 0 */
310         phys_addr pa_scb;       /* 4 - va_to_pa(struct i596_scb *scb) */
311 };
312
313 /* System Configuration Pointer - 12 bytes */
314 struct i596_scp {
315         u32 sysbus;             /* 0 */
316         u32 pad;                /* 4 */
317         phys_addr pa_iscp;      /* 8 - va_to_pa(struct i596_iscp *iscp) */
318 };
319
320 /* Selftest and dump results - needs 16-byte alignment */
321 /*
322  * The size of the dump area is 304 bytes. When the dump is executed
323  * by the Port command an extra word will be appended to the dump area.
324  * The extra word is a copy of the Dump status word (containing the
325  * C, B, OK bits). [I find 0xa006, with a0 for C+OK and 6 for dump]
326  */
327 struct i596_dump {
328         u16 dump[153];          /* (304 = 130h) + 2 bytes */
329 };
330
331 struct i596_private {           /* aligned to a 16-byte boundary */
332         struct i596_scp scp;    /* 0 - needs 16-byte alignment */
333         struct i596_iscp iscp;  /* 12 */
334         struct i596_scb scb;    /* 20 */
335         u32 dummy;              /* 60 */
336         struct i596_dump dump;  /* 64 - needs 16-byte alignment */
337
338         struct i596_cmd set_add;
339         char eth_addr[8];       /* directly follows set_add */
340
341         struct i596_cmd set_conf;
342         char i596_config[16];   /* directly follows set_conf */
343
344         struct i596_cmd tdr;
345         unsigned long tdr_stat; /* directly follows tdr */
346
347         int last_restart;
348         struct i596_rbd *rbd_list;
349         struct i596_rbd *rbd_tail;
350         struct i596_rfd *rx_tail;
351         struct i596_cmd *cmd_tail;
352         struct i596_cmd *cmd_head;
353         int cmd_backlog;
354         unsigned long last_cmd;
355         struct net_device_stats stats;
356         spinlock_t cmd_lock;
357 };
358
359 static char init_setup[14] = {
360         0x8E,   /* length 14 bytes, prefetch on */
361         0xC8,   /* default: fifo to 8, monitor off */
362         0x40,   /* default: don't save bad frames (apricot.c had 0x80) */
363         0x2E,   /* (default is 0x26)
364                    No source address insertion, 8 byte preamble */
365         0x00,   /* default priority and backoff */
366         0x60,   /* default interframe spacing */
367         0x00,   /* default slot time LSB */
368         0xf2,   /* default slot time and nr of retries */
369         0x00,   /* default various bits
370                    (0: promiscuous mode, 1: broadcast disable,
371                     2: encoding mode, 3: transmit on no CRS,
372                     4: no CRC insertion, 5: CRC type,
373                     6: bit stuffing, 7: padding) */
374         0x00,   /* default carrier sense and collision detect */
375         0x40,   /* default minimum frame length */
376         0xff,   /* (default is 0xff, and that is what apricot.c has;
377                    elp486.c has 0xfb: Enable crc append in memory.) */
378         0x00,   /* default: not full duplex */
379         0x7f    /* (default is 0x3f) multi IA */
380 };
381
382 static int i596_open(struct net_device *dev);
383 static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev);
384 static irqreturn_t i596_interrupt(int irq, void *dev_id, struct pt_regs *regs);
385 static int i596_close(struct net_device *dev);
386 static struct net_device_stats *i596_get_stats(struct net_device *dev);
387 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd);
388 static void print_eth(char *);
389 static void set_multicast_list(struct net_device *dev);
390 static void i596_tx_timeout(struct net_device *dev);
391
392 static int
393 i596_timeout(struct net_device *dev, char *msg, int ct) {
394         struct i596_private *lp;
395         int boguscnt = ct;
396
397         lp = (struct i596_private *) dev->priv;
398         while (lp->scb.command) {
399                 if (--boguscnt == 0) {
400                         printk("%s: %s timed out - stat %4.4x, cmd %4.4x\n",
401                                dev->name, msg,
402                                lp->scb.status, lp->scb.command);
403                         return 1;
404                 }
405                 udelay(5);
406                 barrier();
407         }
408         return 0;
409 }
410
411 static inline int
412 init_rx_bufs(struct net_device *dev, int num) {
413         struct i596_private *lp;
414         struct i596_rfd *rfd;
415         int i;
416         // struct i596_rbd *rbd;
417
418         lp = (struct i596_private *) dev->priv;
419         lp->scb.pa_rfd = I596_NULL;
420
421         for (i = 0; i < num; i++) {
422                 rfd = kmalloc(sizeof(struct i596_rfd), GFP_KERNEL);
423                 if (rfd == NULL)
424                         break;
425
426                 rfd->stat = 0;
427                 rfd->pa_rbd = I596_NULL;
428                 rfd->count = 0;
429                 rfd->size = 1532;
430                 if (i == 0) {
431                         rfd->cmd = CMD_EOL;
432                         lp->rx_tail = rfd;
433                 } else {
434                         rfd->cmd = 0;
435                 }
436                 rfd->pa_next = lp->scb.pa_rfd;
437                 lp->scb.pa_rfd = va_to_pa(rfd);
438                 lp->rx_tail->pa_next = lp->scb.pa_rfd;
439         }
440
441 #if 0
442         for (i = 0; i<RX_RBD_SIZE; i++) {
443                 rbd = kmalloc(sizeof(struct i596_rbd), GFP_KERNEL);
444                 if (rbd) {
445                         rbd->pad = 0;
446                         rbd->count = 0;
447                         rbd->skb = dev_alloc_skb(RX_SKB_SIZE);
448                         if (!rbd->skb) {
449                                 printk("dev_alloc_skb failed");
450                         }
451                         rbd->next = rfd->rbd;
452                         if (i) {
453                                 rfd->rbd->prev = rbd;
454                                 rbd->size = RX_SKB_SIZE;
455                         } else {
456                                 rbd->size = (RX_SKB_SIZE | RBD_EL);
457                                 lp->rbd_tail = rbd;
458                         }
459
460                         rfd->rbd = rbd;
461                 } else {
462                         printk("Could not kmalloc rbd\n");
463                 }
464         }
465         lp->rbd_tail->next = rfd->rbd;
466 #endif
467         return (i);
468 }
469
470 static inline void
471 remove_rx_bufs(struct net_device *dev) {
472         struct i596_private *lp;
473         struct i596_rfd *rfd;
474
475         lp = (struct i596_private *) dev->priv;
476         lp->rx_tail->pa_next = I596_NULL;
477
478         do {
479                 rfd = pa_to_va(lp->scb.pa_rfd);
480                 lp->scb.pa_rfd = rfd->pa_next;
481                 kfree(rfd);
482         } while (rfd != lp->rx_tail);
483
484         lp->rx_tail = NULL;
485
486 #if 0
487         for (lp->rbd_list) {
488         }
489 #endif
490 }
491
492 #define PORT_RESET              0x00    /* reset 82596 */
493 #define PORT_SELFTEST           0x01    /* selftest */
494 #define PORT_ALTSCP             0x02    /* alternate SCB address */
495 #define PORT_DUMP               0x03    /* dump */
496
497 #define IOADDR  0xcb0           /* real constant */
498 #define IRQ     10              /* default IRQ - can be changed by ECU */
499
500 /* The 82596 requires two 16-bit write cycles for a port command */
501 static inline void
502 PORT(phys_addr a, unsigned int cmd) {
503         if (a & 0xf)
504                 printk("lp486e.c: PORT: address not aligned\n");
505         outw(((a & 0xffff) | cmd), IOADDR);
506         outw(((a>>16) & 0xffff), IOADDR+2);
507 }
508
509 static inline void
510 CA(void) {
511         outb(0, IOADDR+4);
512         udelay(8);
513 }
514
515 static inline void
516 CLEAR_INT(void) {
517         outb(0, IOADDR+8);
518 }
519
520 #define SIZE(x) (sizeof(x)/sizeof((x)[0]))
521
522 #if 0
523 /* selftest or dump */
524 static void
525 i596_port_do(struct net_device *dev, int portcmd, char *cmdname) {
526         struct i596_private *lp = dev->priv;
527         u16 *outp;
528         int i, m;
529
530         memset((void *)&(lp->dump), 0, sizeof(struct i596_dump));
531         outp = &(lp->dump.dump[0]);
532
533         PORT(va_to_pa(outp), portcmd);
534         mdelay(30);             /* random, unmotivated */
535
536         printk("lp486e i82596 %s result:\n", cmdname);
537         for (m = SIZE(lp->dump.dump); m && lp->dump.dump[m-1] == 0; m--)
538                 ;
539         for (i = 0; i < m; i++) {
540                 printk(" %04x", lp->dump.dump[i]);
541                 if (i%8 == 7)
542                         printk("\n");
543         }
544         printk("\n");
545 }
546 #endif
547
548 static int
549 i596_scp_setup(struct net_device *dev) {
550         struct i596_private *lp = dev->priv;
551         int boguscnt;
552
553         /* Setup SCP, ISCP, SCB */
554         /*
555          * sysbus bits:
556          *  only a single byte is significant - here 0x44
557          *  0x80: big endian mode (details depend on stepping)
558          *  0x40: 1
559          *  0x20: interrupt pin is active low
560          *  0x10: lock function disabled
561          *  0x08: external triggering of bus throttle timers
562          *  0x06: 00: 82586 compat mode, 01: segmented mode, 10: linear mode
563          *  0x01: unused
564          */
565         lp->scp.sysbus = 0x00440000;            /* linear mode */
566         lp->scp.pad = 0;                        /* must be zero */
567         lp->scp.pa_iscp = va_to_pa(&(lp->iscp));
568
569         /*
570          * The CPU sets the ISCP to 1 before it gives the first CA()
571          */
572         lp->iscp.busy = 0x0001;
573         lp->iscp.pa_scb = va_to_pa(&(lp->scb));
574
575         lp->scb.command = 0;
576         lp->scb.status = 0;
577         lp->scb.pa_cmd = I596_NULL;
578         /* lp->scb.pa_rfd has been initialised already */
579
580         lp->last_cmd = jiffies;
581         lp->cmd_backlog = 0;
582         lp->cmd_head = NULL;
583
584         /*
585          * Reset the 82596.
586          * We need to wait 10 systemclock cycles, and
587          * 5 serial clock cycles.
588          */
589         PORT(0, PORT_RESET);    /* address part ignored */
590         udelay(100);
591
592         /*
593          * Before the CA signal is asserted, the default SCP address
594          * (0x00fffff4) can be changed to a 16-byte aligned value
595          */
596         PORT(va_to_pa(&lp->scp), PORT_ALTSCP);  /* change the scp address */
597
598         /*
599          * The initialization procedure begins when a
600          * Channel Attention signal is asserted after a reset.
601          */
602
603         CA();
604
605         /*
606          * The ISCP busy is cleared by the 82596 after the SCB address is read.
607          */
608         boguscnt = 100;
609         while (lp->iscp.busy) {
610                 if (--boguscnt == 0) {
611                         /* No i82596 present? */
612                         printk("%s: i82596 initialization timed out\n",
613                                dev->name);
614                         return 1;
615                 }
616                 udelay(5);
617                 barrier();
618         }
619         /* I find here boguscnt==100, so no delay was required. */
620
621         return 0;
622 }
623
624 static int
625 init_i596(struct net_device *dev) {
626         struct i596_private *lp;
627
628         if (i596_scp_setup(dev))
629                 return 1;
630
631         lp = (struct i596_private *) dev->priv;
632         lp->scb.command = 0;
633
634         memcpy ((void *)lp->i596_config, init_setup, 14);
635         lp->set_conf.command = CmdConfigure;
636         i596_add_cmd(dev, (void *)&lp->set_conf);
637
638         memcpy ((void *)lp->eth_addr, dev->dev_addr, 6);
639         lp->set_add.command = CmdIASetup;
640         i596_add_cmd(dev, (struct i596_cmd *)&lp->set_add);
641
642         lp->tdr.command = CmdTDR;
643         i596_add_cmd(dev, (struct i596_cmd *)&lp->tdr);
644
645         if (lp->scb.command && i596_timeout(dev, "i82596 init", 200))
646                 return 1;
647
648         lp->scb.command = RX_START;
649         CA();
650
651         barrier();
652         
653         if (lp->scb.command && i596_timeout(dev, "Receive Unit start", 100))
654                 return 1;
655
656         return 0;
657 }
658
659 /* Receive a single frame */
660 static inline int
661 i596_rx_one(struct net_device *dev, struct i596_private *lp,
662             struct i596_rfd *rfd, int *frames) {
663
664         if (rfd->stat & RFD_STAT_OK) {
665                 /* a good frame */
666                 int pkt_len = (rfd->count & 0x3fff);
667                 struct sk_buff *skb = dev_alloc_skb(pkt_len);
668
669                 (*frames)++;
670
671                 if (rfd->cmd & CMD_EOL)
672                         printk("Received on EOL\n");
673
674                 if (skb == NULL) {
675                         printk ("%s: i596_rx Memory squeeze, "
676                                 "dropping packet.\n", dev->name);
677                         lp->stats.rx_dropped++;
678                         return 1;
679                 }
680
681                 skb->dev = dev;         
682                 memcpy(skb_put(skb,pkt_len), rfd->data, pkt_len);
683
684                 skb->protocol = eth_type_trans(skb,dev);
685                 netif_rx(skb);
686                 dev->last_rx = jiffies;
687                 lp->stats.rx_packets++;
688         } else {
689 #if 0
690                 printk("Frame reception error status %04x\n",
691                        rfd->stat);
692 #endif
693                 lp->stats.rx_errors++;
694                 if (rfd->stat & RFD_COLLISION)
695                         lp->stats.collisions++;
696                 if (rfd->stat & RFD_SHORT_FRAME_ERR)
697                         lp->stats.rx_length_errors++;
698                 if (rfd->stat & RFD_DMA_ERR)
699                         lp->stats.rx_over_errors++;
700                 if (rfd->stat & RFD_NOBUFS_ERR)
701                         lp->stats.rx_fifo_errors++;
702                 if (rfd->stat & RFD_ALIGN_ERR)
703                         lp->stats.rx_frame_errors++;
704                 if (rfd->stat & RFD_CRC_ERR)
705                         lp->stats.rx_crc_errors++;
706                 if (rfd->stat & RFD_LENGTH_ERR)
707                         lp->stats.rx_length_errors++;
708         }
709         rfd->stat = rfd->count = 0;
710         return 0;
711 }
712
713 static int
714 i596_rx(struct net_device *dev) {
715         struct i596_private *lp = (struct i596_private *) dev->priv;
716         struct i596_rfd *rfd;
717         int frames = 0;
718
719         while (1) {
720                 rfd = pa_to_va(lp->scb.pa_rfd);
721                 if (!rfd) {
722                         printk(KERN_ERR "i596_rx: NULL rfd?\n");
723                         return 0;
724                 }
725 #if 1
726                 if (rfd->stat && !(rfd->stat & (RFD_STAT_C | RFD_STAT_B)))
727                         printk("SF:%p-%04x\n", rfd, rfd->stat);
728 #endif
729                 if (!(rfd->stat & RFD_STAT_C))
730                         break;          /* next one not ready */
731                 if (i596_rx_one(dev, lp, rfd, &frames))
732                         break;          /* out of memory */
733                 rfd->cmd = CMD_EOL;
734                 lp->rx_tail->cmd = 0;
735                 lp->rx_tail = rfd;
736                 lp->scb.pa_rfd = rfd->pa_next;
737                 barrier();
738         }
739
740         return frames;
741 }
742
743 static void
744 i596_cleanup_cmd(struct net_device *dev) {
745         struct i596_private *lp;
746         struct i596_cmd *cmd;
747
748         lp = (struct i596_private *) dev->priv;
749         while (lp->cmd_head) {
750                 cmd = (struct i596_cmd *)lp->cmd_head;
751
752                 lp->cmd_head = pa_to_va(lp->cmd_head->pa_next);
753                 lp->cmd_backlog--;
754
755                 switch ((cmd->command) & 0x7) {
756                         case CmdTx: {
757                                 struct tx_cmd *tx_cmd = (struct tx_cmd *) cmd;
758                                 struct i596_tbd * tx_cmd_tbd;
759                                 tx_cmd_tbd = pa_to_va(tx_cmd->pa_tbd);
760
761                                 dev_kfree_skb_any(tx_cmd_tbd->skb);
762
763                                 lp->stats.tx_errors++;
764                                 lp->stats.tx_aborted_errors++;
765
766                                 cmd->pa_next = I596_NULL;
767                                 kfree((unsigned char *)tx_cmd);
768                                 netif_wake_queue(dev);
769                                 break;
770                         }
771                         case CmdMulticastList: {
772                                 // unsigned short count = *((unsigned short *) (ptr + 1));
773
774                                 cmd->pa_next = I596_NULL;
775                                 kfree((unsigned char *)cmd);
776                                 break;
777                         }
778                         default: {
779                                 cmd->pa_next = I596_NULL;
780                                 break;
781                         }
782                 }
783                 barrier();
784         }
785
786         if (lp->scb.command && i596_timeout(dev, "i596_cleanup_cmd", 100))
787                 ;
788
789         lp->scb.pa_cmd = va_to_pa(lp->cmd_head);
790 }
791
792 static void i596_reset(struct net_device *dev, struct i596_private *lp, int ioaddr) {
793
794         if (lp->scb.command && i596_timeout(dev, "i596_reset", 100))
795                 ;
796
797         netif_stop_queue(dev);
798
799         lp->scb.command = CUC_ABORT | RX_ABORT;
800         CA();
801         barrier();
802         
803         /* wait for shutdown */
804         if (lp->scb.command && i596_timeout(dev, "i596_reset(2)", 400))
805                 ;
806
807         i596_cleanup_cmd(dev);
808         i596_rx(dev);
809
810         netif_start_queue(dev);
811         /*dev_kfree_skb(skb, FREE_WRITE);*/
812         init_i596(dev);
813 }
814
815 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd) {
816         struct i596_private *lp = dev->priv;
817         int ioaddr = dev->base_addr;
818         unsigned long flags;
819
820         cmd->status = 0;
821         cmd->command |= (CMD_EOL | CMD_INTR);
822         cmd->pa_next = I596_NULL;
823
824         spin_lock_irqsave(&lp->cmd_lock, flags);
825         
826         if (lp->cmd_head) {
827                 lp->cmd_tail->pa_next = va_to_pa(cmd);
828         } else {
829                 lp->cmd_head = cmd;
830                 if (lp->scb.command && i596_timeout(dev, "i596_add_cmd", 100))
831                         ;
832                 lp->scb.pa_cmd = va_to_pa(cmd);
833                 lp->scb.command = CUC_START;
834                 CA();
835         }
836         lp->cmd_tail = cmd;
837         lp->cmd_backlog++;
838
839         lp->cmd_head = pa_to_va(lp->scb.pa_cmd);
840         spin_unlock_irqrestore(&lp->cmd_lock, flags);
841
842         if (lp->cmd_backlog > 16) {
843                 int tickssofar = jiffies - lp->last_cmd;
844                 if (tickssofar < HZ/4)
845                         return;
846
847                 printk(KERN_WARNING "%s: command unit timed out, status resetting.\n", dev->name);
848                 i596_reset(dev, lp, ioaddr);
849         }
850 }
851
852 static int i596_open(struct net_device *dev) 
853 {
854         int i;
855
856         i = request_irq(dev->irq, &i596_interrupt, SA_SHIRQ, dev->name, dev);
857         if (i) {
858                 printk(KERN_ERR "%s: IRQ %d not free\n", dev->name, dev->irq);
859                 return i;
860         }
861
862         if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE)
863                 printk(KERN_ERR "%s: only able to allocate %d receive buffers\n", dev->name, i);
864
865         if (i < 4) {
866                 free_irq(dev->irq, dev);
867                 return -EAGAIN;
868         }
869         netif_start_queue(dev);
870         init_i596(dev);
871         return 0;                       /* Always succeed */
872 }
873
874 static int i596_start_xmit (struct sk_buff *skb, struct net_device *dev) {
875         struct i596_private *lp = dev->priv;
876         struct tx_cmd *tx_cmd;
877         short length;
878
879         length = skb->len;
880         
881         if (length < ETH_ZLEN) {
882                 skb = skb_padto(skb, ETH_ZLEN);
883                 if (skb == NULL)
884                         return 0;
885                 length = ETH_ZLEN;
886         }
887         
888         dev->trans_start = jiffies;
889
890         tx_cmd = (struct tx_cmd *) kmalloc ((sizeof (struct tx_cmd) + sizeof (struct i596_tbd)), GFP_ATOMIC);
891         if (tx_cmd == NULL) {
892                 printk(KERN_WARNING "%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
893                 lp->stats.tx_dropped++;
894                 dev_kfree_skb (skb);
895         } else {
896                 struct i596_tbd *tx_cmd_tbd;
897                 tx_cmd_tbd = (struct i596_tbd *) (tx_cmd + 1);
898                 tx_cmd->pa_tbd = va_to_pa (tx_cmd_tbd);
899                 tx_cmd_tbd->pa_next = I596_NULL;
900
901                 tx_cmd->cmd.command = (CMD_FLEX | CmdTx);
902
903                 tx_cmd->pad = 0;
904                 tx_cmd->size = 0;
905                 tx_cmd_tbd->pad = 0;
906                 tx_cmd_tbd->size = (EOF | length);
907
908                 tx_cmd_tbd->pa_data = va_to_pa (skb->data);
909                 tx_cmd_tbd->skb = skb;
910
911                 if (i596_debug & LOG_SRCDST)
912                         print_eth (skb->data);
913
914                 i596_add_cmd (dev, (struct i596_cmd *) tx_cmd);
915
916                 lp->stats.tx_packets++;
917         }
918
919         return 0;
920 }
921
922 static void
923 i596_tx_timeout (struct net_device *dev) {
924         struct i596_private *lp = dev->priv;
925         int ioaddr = dev->base_addr;
926
927         /* Transmitter timeout, serious problems. */
928         printk(KERN_WARNING "%s: transmit timed out, status resetting.\n", dev->name);
929         lp->stats.tx_errors++;
930
931         /* Try to restart the adaptor */
932         if (lp->last_restart == lp->stats.tx_packets) {
933                 printk ("Resetting board.\n");
934
935                 /* Shutdown and restart */
936                 i596_reset (dev, lp, ioaddr);
937         } else {
938                 /* Issue a channel attention signal */
939                 printk ("Kicking board.\n");
940                 lp->scb.command = (CUC_START | RX_START);
941                 CA();
942                 lp->last_restart = lp->stats.tx_packets;
943         }
944         netif_wake_queue(dev);
945 }
946
947 static void print_eth(char *add) 
948 {
949         int i;
950
951         printk ("Dest  ");
952         for (i = 0; i < 6; i++)
953                 printk(" %2.2X", (unsigned char) add[i]);
954         printk ("\n");
955
956         printk ("Source");
957         for (i = 0; i < 6; i++)
958                 printk(" %2.2X", (unsigned char) add[i+6]);
959         printk ("\n");
960
961         printk ("type %2.2X%2.2X\n",
962                 (unsigned char) add[12], (unsigned char) add[13]);
963 }
964
965 static int __init lp486e_probe(struct net_device *dev) {
966         struct i596_private *lp;
967         unsigned char eth_addr[6] = { 0, 0xaa, 0, 0, 0, 0 };
968         unsigned char *bios;
969         int i, j;
970         int ret = -ENOMEM;
971         static int probed;
972
973         if (probed)
974                 return -ENODEV;
975         probed++;
976
977         if (!request_region(IOADDR, LP486E_TOTAL_SIZE, DRV_NAME)) {
978                 printk(KERN_ERR "lp486e: IO address 0x%x in use\n", IOADDR);
979                 return -EBUSY;
980         }
981
982         lp = (struct i596_private *) dev->priv;
983         spin_lock_init(&lp->cmd_lock);
984         
985         /*
986          * Do we really have this thing?
987          */
988         if (i596_scp_setup(dev)) {
989                 ret = -ENODEV;
990                 goto err_out_kfree;
991         }
992
993         dev->base_addr = IOADDR;
994         dev->irq = IRQ;
995
996
997         /*
998          * How do we find the ethernet address? I don't know.
999          * One possibility is to look at the EISA configuration area
1000          * [0xe8000-0xe9fff]. This contains the ethernet address
1001          * but not at a fixed address - things depend on setup options.
1002          *
1003          * If we find no address, or the wrong address, use
1004          *   ifconfig eth0 hw ether a1:a2:a3:a4:a5:a6
1005          * with the value found in the BIOS setup.
1006          */
1007         bios = bus_to_virt(0xe8000);
1008         for (j = 0; j < 0x2000; j++) {
1009                 if (bios[j] == 0 && bios[j+1] == 0xaa && bios[j+2] == 0) {
1010                         printk("%s: maybe address at BIOS 0x%x:",
1011                                dev->name, 0xe8000+j);
1012                         for (i = 0; i < 6; i++) {
1013                                 eth_addr[i] = bios[i+j];
1014                                 printk(" %2.2X", eth_addr[i]);
1015                         }
1016                         printk("\n");
1017                 }
1018         }
1019
1020         printk("%s: lp486e 82596 at %#3lx, IRQ %d,",
1021                dev->name, dev->base_addr, dev->irq);
1022         for (i = 0; i < 6; i++)
1023                 printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);
1024         printk("\n");
1025
1026         /* The LP486E-specific entries in the device structure. */
1027         dev->open = &i596_open;
1028         dev->stop = &i596_close;
1029         dev->hard_start_xmit = &i596_start_xmit;
1030         dev->get_stats = &i596_get_stats;
1031         dev->set_multicast_list = &set_multicast_list;
1032         dev->watchdog_timeo = 5*HZ;
1033         dev->tx_timeout = i596_tx_timeout;
1034
1035 #if 0
1036         /* selftest reports 0x320925ae - don't know what that means */
1037         i596_port_do(dev, PORT_SELFTEST, "selftest");
1038         i596_port_do(dev, PORT_DUMP, "dump");
1039 #endif
1040         return 0;
1041
1042 err_out_kfree:
1043         release_region(IOADDR, LP486E_TOTAL_SIZE);
1044         return ret;
1045 }
1046
1047 static inline void
1048 i596_handle_CU_completion(struct net_device *dev,
1049                           struct i596_private *lp,
1050                           unsigned short status,
1051                           unsigned short *ack_cmdp) {
1052         struct i596_cmd *cmd;
1053         int frames_out = 0;
1054         int commands_done = 0;
1055         int cmd_val;
1056         unsigned long flags;
1057
1058         spin_lock_irqsave(&lp->cmd_lock, flags);
1059         cmd = lp->cmd_head;
1060
1061         while (lp->cmd_head && (lp->cmd_head->status & CMD_STAT_C)) {
1062                 cmd = lp->cmd_head;
1063
1064                 lp->cmd_head = pa_to_va(lp->cmd_head->pa_next);
1065                 lp->cmd_backlog--;
1066
1067                 commands_done++;
1068                 cmd_val = cmd->command & 0x7;
1069 #if 0
1070                 printk("finished CU %s command (%d)\n",
1071                        CUcmdnames[cmd_val], cmd_val);
1072 #endif
1073                 switch (cmd_val) {
1074                 case CmdTx:
1075                 {
1076                         struct tx_cmd *tx_cmd;
1077                         struct i596_tbd *tx_cmd_tbd;
1078
1079                         tx_cmd = (struct tx_cmd *) cmd;
1080                         tx_cmd_tbd = pa_to_va(tx_cmd->pa_tbd);
1081
1082                         frames_out++;
1083                         if (cmd->status & CMD_STAT_OK) {
1084                                 if (i596_debug)
1085                                         print_eth(pa_to_va(tx_cmd_tbd->pa_data));
1086                         } else {
1087                                 lp->stats.tx_errors++;
1088                                 if (i596_debug)
1089                                         printk("transmission failure:%04x\n",
1090                                                cmd->status);
1091                                 if (cmd->status & 0x0020)
1092                                         lp->stats.collisions++;
1093                                 if (!(cmd->status & 0x0040))
1094                                         lp->stats.tx_heartbeat_errors++;
1095                                 if (cmd->status & 0x0400)
1096                                         lp->stats.tx_carrier_errors++;
1097                                 if (cmd->status & 0x0800)
1098                                         lp->stats.collisions++;
1099                                 if (cmd->status & 0x1000)
1100                                         lp->stats.tx_aborted_errors++;
1101                         }
1102                         dev_kfree_skb_irq(tx_cmd_tbd->skb);
1103
1104                         cmd->pa_next = I596_NULL;
1105                         kfree((unsigned char *)tx_cmd);
1106                         netif_wake_queue(dev);
1107                         break;
1108                 }
1109
1110                 case CmdMulticastList:
1111                         cmd->pa_next = I596_NULL;
1112                         kfree((unsigned char *)cmd);
1113                         break;
1114
1115                 case CmdTDR:
1116                 {
1117                         unsigned long status = *((unsigned long *) (cmd + 1));
1118                         if (status & 0x8000) {
1119                                 if (i596_debug)
1120                                         printk("%s: link ok.\n", dev->name);
1121                         } else {
1122                                 if (status & 0x4000)
1123                                         printk("%s: Transceiver problem.\n",
1124                                                dev->name);
1125                                 if (status & 0x2000)
1126                                         printk("%s: Termination problem.\n",
1127                                                dev->name);
1128                                 if (status & 0x1000)
1129                                         printk("%s: Short circuit.\n",
1130                                                dev->name);
1131                                 printk("%s: Time %ld.\n",
1132                                        dev->name, status & 0x07ff);
1133                         }
1134                 }
1135                 default:
1136                         cmd->pa_next = I596_NULL;
1137                         lp->last_cmd = jiffies;
1138                         
1139                 }
1140                 barrier();
1141         }
1142
1143         cmd = lp->cmd_head;
1144         while (cmd && (cmd != lp->cmd_tail)) {
1145                 cmd->command &= 0x1fff;
1146                 cmd = pa_to_va(cmd->pa_next);
1147                 barrier();
1148         }
1149
1150         if (lp->cmd_head)
1151                 *ack_cmdp |= CUC_START;
1152         lp->scb.pa_cmd = va_to_pa(lp->cmd_head);
1153         spin_unlock_irqrestore(&lp->cmd_lock, flags);
1154 }
1155
1156 static irqreturn_t
1157 i596_interrupt (int irq, void *dev_instance, struct pt_regs *regs) {
1158         struct net_device *dev = (struct net_device *) dev_instance;
1159         struct i596_private *lp;
1160         unsigned short status, ack_cmd = 0;
1161         int frames_in = 0;
1162
1163         lp = (struct i596_private *) dev->priv;
1164
1165         /*
1166          * The 82596 examines the command, performs the required action,
1167          * and then clears the SCB command word.
1168          */
1169         if (lp->scb.command && i596_timeout(dev, "interrupt", 40))
1170                 ;
1171
1172         /*
1173          * The status word indicates the status of the 82596.
1174          * It is modified only by the 82596.
1175          *
1176          * [So, we must not clear it. I find often status 0xffff,
1177          *  which is not one of the values allowed by the docs.]
1178          */
1179         status = lp->scb.status;
1180 #if 0
1181         if (i596_debug) {
1182                 printk("%s: i596 interrupt, ", dev->name);
1183                 i596_out_status(status);
1184         }
1185 #endif
1186         /* Impossible, but it happens - perhaps when we get
1187            a receive interrupt but scb.pa_rfd is I596_NULL. */
1188         if (status == 0xffff) {
1189                 printk("%s: i596_interrupt: got status 0xffff\n", dev->name);
1190                 goto out;
1191         }
1192
1193         ack_cmd = (status & STAT_ACK);
1194
1195         if (status & (STAT_CX | STAT_CNA))
1196                 i596_handle_CU_completion(dev, lp, status, &ack_cmd);
1197
1198         if (status & (STAT_FR | STAT_RNR)) {
1199                 /* Restart the receive unit when it got inactive somehow */
1200                 if ((status & STAT_RNR) && netif_running(dev))
1201                         ack_cmd |= RX_START;
1202
1203                 if (status & STAT_FR) {
1204                         frames_in = i596_rx(dev);
1205                         if (!frames_in)
1206                                 printk("receive frame reported, but no frames\n");
1207                 }
1208         }
1209
1210         /* acknowledge the interrupt */
1211         /*
1212         if ((lp->scb.pa_cmd != I596_NULL) && netif_running(dev))
1213                 ack_cmd |= CUC_START;
1214         */
1215
1216         if (lp->scb.command && i596_timeout(dev, "i596 interrupt", 100))
1217                 ;
1218
1219         lp->scb.command = ack_cmd;
1220
1221         CLEAR_INT();
1222         CA();
1223
1224  out:
1225         return IRQ_HANDLED;
1226 }
1227
1228 static int i596_close(struct net_device *dev) {
1229         struct i596_private *lp = dev->priv;
1230
1231         netif_stop_queue(dev);
1232
1233         if (i596_debug)
1234                 printk("%s: Shutting down ethercard, status was %4.4x.\n",
1235                        dev->name, lp->scb.status);
1236
1237         lp->scb.command = (CUC_ABORT | RX_ABORT);
1238         CA();
1239
1240         i596_cleanup_cmd(dev);
1241
1242         if (lp->scb.command && i596_timeout(dev, "i596_close", 200))
1243                 ;
1244
1245         free_irq(dev->irq, dev);
1246         remove_rx_bufs(dev);
1247
1248         return 0;
1249 }
1250
1251 static struct net_device_stats * i596_get_stats(struct net_device *dev) {
1252         struct i596_private *lp = dev->priv;
1253
1254         return &lp->stats;
1255 }
1256
1257 /*
1258 *       Set or clear the multicast filter for this adaptor.
1259 */
1260
1261 static void set_multicast_list(struct net_device *dev) {
1262         struct i596_private *lp = dev->priv;
1263         struct i596_cmd *cmd;
1264
1265         if (i596_debug > 1)
1266                 printk ("%s: set multicast list %d\n",
1267                         dev->name, dev->mc_count);
1268
1269         if (dev->mc_count > 0) {
1270                 struct dev_mc_list *dmi;
1271                 char *cp;
1272                 cmd = (struct i596_cmd *)kmalloc(sizeof(struct i596_cmd)+2+dev->mc_count*6, GFP_ATOMIC);
1273                 if (cmd == NULL) {
1274                         printk (KERN_ERR "%s: set_multicast Memory squeeze.\n", dev->name);
1275                         return;
1276                 }
1277                 cmd->command = CmdMulticastList;
1278                 *((unsigned short *) (cmd + 1)) = dev->mc_count * 6;
1279                 cp = ((char *)(cmd + 1))+2;
1280                 for (dmi = dev->mc_list; dmi != NULL; dmi = dmi->next) {
1281                         memcpy(cp, dmi,6);
1282                         cp += 6;
1283                 }
1284                 if (i596_debug & LOG_SRCDST)
1285                         print_eth (((char *)(cmd + 1)) + 2);
1286                 i596_add_cmd(dev, cmd);
1287         } else {
1288                 if (lp->set_conf.pa_next != I596_NULL) {
1289                         return;
1290                 }
1291                 if (dev->mc_count == 0 && !(dev->flags & (IFF_PROMISC | IFF_ALLMULTI))) {
1292                         if (dev->flags & IFF_ALLMULTI)
1293                                 dev->flags |= IFF_PROMISC;
1294                         lp->i596_config[8] &= ~0x01;
1295                 } else {
1296                         lp->i596_config[8] |= 0x01;
1297                 }
1298
1299                 i596_add_cmd(dev, (struct i596_cmd *) &lp->set_conf);
1300         }
1301 }
1302
1303 MODULE_AUTHOR("Ard van Breemen <ard@cstmel.nl.eu.org>");
1304 MODULE_DESCRIPTION("Intel Panther onboard i82596 driver");
1305 MODULE_LICENSE("GPL");
1306
1307 static struct net_device *dev_lp486e;
1308 static int full_duplex;
1309 static int options;
1310 static int io = IOADDR;
1311 static int irq = IRQ;
1312
1313 module_param(debug, int, 0);
1314 //module_param(max_interrupt_work, int, 0);
1315 //module_param(reverse_probe, int, 0);
1316 //module_param(rx_copybreak, int, 0);
1317 module_param(options, int, 0);
1318 module_param(full_duplex, int, 0);
1319
1320 static int __init lp486e_init_module(void) {
1321         int err;
1322         struct net_device *dev = alloc_etherdev(sizeof(struct i596_private));
1323         if (!dev)
1324                 return -ENOMEM;
1325
1326         dev->irq = irq;
1327         dev->base_addr = io;
1328         err = lp486e_probe(dev);
1329         if (err) {
1330                 free_netdev(dev);
1331                 return err;
1332         }
1333         err = register_netdev(dev);
1334         if (err) {
1335                 release_region(dev->base_addr, LP486E_TOTAL_SIZE);
1336                 free_netdev(dev);
1337                 return err;
1338         }
1339         dev_lp486e = dev;
1340         full_duplex = 0;
1341         options = 0;
1342         return 0;
1343 }
1344
1345 static void __exit lp486e_cleanup_module(void) {
1346         unregister_netdev(dev_lp486e);
1347         release_region(dev_lp486e->base_addr, LP486E_TOTAL_SIZE);
1348         free_netdev(dev_lp486e);
1349 }
1350
1351 module_init(lp486e_init_module);
1352 module_exit(lp486e_cleanup_module);