Merge branch 'acpi-cleanup' into fixes
[cascardo/linux.git] / drivers / net / ethernet / i825xx / 82596.c
1 /* 82596.c: A generic 82596 ethernet driver for linux. */
2 /*
3    Based on Apricot.c
4    Written 1994 by Mark Evans.
5    This driver is for the Apricot 82596 bus-master interface
6
7    Modularised 12/94 Mark Evans
8
9
10    Modified to support the 82596 ethernet chips on 680x0 VME boards.
11    by Richard Hirst <richard@sleepie.demon.co.uk>
12    Renamed to be 82596.c
13
14    980825:  Changed to receive directly in to sk_buffs which are
15    allocated at open() time.  Eliminates copy on incoming frames
16    (small ones are still copied).  Shared data now held in a
17    non-cached page, so we can run on 68060 in copyback mode.
18
19    TBD:
20    * look at deferring rx frames rather than discarding (as per tulip)
21    * handle tx ring full as per tulip
22    * performance test to tune rx_copybreak
23
24    Most of my modifications relate to the braindead big-endian
25    implementation by Intel.  When the i596 is operating in
26    'big-endian' mode, it thinks a 32 bit value of 0x12345678
27    should be stored as 0x56781234.  This is a real pain, when
28    you have linked lists which are shared by the 680x0 and the
29    i596.
30
31    Driver skeleton
32    Written 1993 by Donald Becker.
33    Copyright 1993 United States Government as represented by the Director,
34    National Security Agency. This software may only be used and distributed
35    according to the terms of the GNU General Public License as modified by SRC,
36    incorporated herein by reference.
37
38    The author may be reached as becker@scyld.com, or C/O
39    Scyld Computing Corporation, 410 Severn Ave., Suite 210, Annapolis MD 21403
40
41  */
42
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/string.h>
46 #include <linux/errno.h>
47 #include <linux/ioport.h>
48 #include <linux/interrupt.h>
49 #include <linux/delay.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/skbuff.h>
53 #include <linux/init.h>
54 #include <linux/bitops.h>
55 #include <linux/gfp.h>
56
57 #include <asm/io.h>
58 #include <asm/dma.h>
59 #include <asm/pgtable.h>
60 #include <asm/cacheflush.h>
61
62 static char version[] __initdata =
63         "82596.c $Revision: 1.5 $\n";
64
65 #define DRV_NAME        "82596"
66
67 /* DEBUG flags
68  */
69
70 #define DEB_INIT        0x0001
71 #define DEB_PROBE       0x0002
72 #define DEB_SERIOUS     0x0004
73 #define DEB_ERRORS      0x0008
74 #define DEB_MULTI       0x0010
75 #define DEB_TDR         0x0020
76 #define DEB_OPEN        0x0040
77 #define DEB_RESET       0x0080
78 #define DEB_ADDCMD      0x0100
79 #define DEB_STATUS      0x0200
80 #define DEB_STARTTX     0x0400
81 #define DEB_RXADDR      0x0800
82 #define DEB_TXADDR      0x1000
83 #define DEB_RXFRAME     0x2000
84 #define DEB_INTS        0x4000
85 #define DEB_STRUCT      0x8000
86 #define DEB_ANY         0xffff
87
88
89 #define DEB(x,y)        if (i596_debug & (x)) y
90
91
92 #if defined(CONFIG_MVME16x_NET) || defined(CONFIG_MVME16x_NET_MODULE)
93 #define ENABLE_MVME16x_NET
94 #endif
95 #if defined(CONFIG_BVME6000_NET) || defined(CONFIG_BVME6000_NET_MODULE)
96 #define ENABLE_BVME6000_NET
97 #endif
98
99 #ifdef ENABLE_MVME16x_NET
100 #include <asm/mvme16xhw.h>
101 #endif
102 #ifdef ENABLE_BVME6000_NET
103 #include <asm/bvme6000hw.h>
104 #endif
105
106 /*
107  * Define various macros for Channel Attention, word swapping etc., dependent
108  * on architecture.  MVME and BVME are 680x0 based, otherwise it is Intel.
109  */
110
111 #ifdef __mc68000__
112 #define WSWAPrfd(x)  ((struct i596_rfd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
113 #define WSWAPrbd(x)  ((struct i596_rbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
114 #define WSWAPiscp(x) ((struct i596_iscp *)(((u32)(x)<<16) | ((((u32)(x)))>>16)))
115 #define WSWAPscb(x)  ((struct i596_scb *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
116 #define WSWAPcmd(x)  ((struct i596_cmd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
117 #define WSWAPtbd(x)  ((struct i596_tbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
118 #define WSWAPchar(x) ((char *)            (((u32)(x)<<16) | ((((u32)(x)))>>16)))
119 #define ISCP_BUSY       0x00010000
120 #else
121 #error 82596.c: unknown architecture
122 #endif
123
124 /*
125  * These were the intel versions, left here for reference. There
126  * are currently no x86 users of this legacy i82596 chip.
127  */
128 #if 0
129 #define WSWAPrfd(x)     ((struct i596_rfd *)((long)x))
130 #define WSWAPrbd(x)     ((struct i596_rbd *)((long)x))
131 #define WSWAPiscp(x)    ((struct i596_iscp *)((long)x))
132 #define WSWAPscb(x)     ((struct i596_scb *)((long)x))
133 #define WSWAPcmd(x)     ((struct i596_cmd *)((long)x))
134 #define WSWAPtbd(x)     ((struct i596_tbd *)((long)x))
135 #define WSWAPchar(x)    ((char *)((long)x))
136 #define ISCP_BUSY       0x0001
137 #endif
138
139 /*
140  * The MPU_PORT command allows direct access to the 82596. With PORT access
141  * the following commands are available (p5-18). The 32-bit port command
142  * must be word-swapped with the most significant word written first.
143  * This only applies to VME boards.
144  */
145 #define PORT_RESET              0x00    /* reset 82596 */
146 #define PORT_SELFTEST           0x01    /* selftest */
147 #define PORT_ALTSCP             0x02    /* alternate SCB address */
148 #define PORT_ALTDUMP            0x03    /* Alternate DUMP address */
149
150 static int i596_debug = (DEB_SERIOUS|DEB_PROBE);
151
152 MODULE_AUTHOR("Richard Hirst");
153 MODULE_DESCRIPTION("i82596 driver");
154 MODULE_LICENSE("GPL");
155
156 module_param(i596_debug, int, 0);
157 MODULE_PARM_DESC(i596_debug, "i82596 debug mask");
158
159
160 /* Copy frames shorter than rx_copybreak, otherwise pass on up in
161  * a full sized sk_buff.  Value of 100 stolen from tulip.c (!alpha).
162  */
163 static int rx_copybreak = 100;
164
165 #define PKT_BUF_SZ      1536
166 #define MAX_MC_CNT      64
167
168 #define I596_TOTAL_SIZE 17
169
170 #define I596_NULL ((void *)0xffffffff)
171
172 #define CMD_EOL         0x8000  /* The last command of the list, stop. */
173 #define CMD_SUSP        0x4000  /* Suspend after doing cmd. */
174 #define CMD_INTR        0x2000  /* Interrupt after doing cmd. */
175
176 #define CMD_FLEX        0x0008  /* Enable flexible memory model */
177
178 enum commands {
179         CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
180         CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
181 };
182
183 #define STAT_C          0x8000  /* Set to 0 after execution */
184 #define STAT_B          0x4000  /* Command being executed */
185 #define STAT_OK         0x2000  /* Command executed ok */
186 #define STAT_A          0x1000  /* Command aborted */
187
188 #define  CUC_START      0x0100
189 #define  CUC_RESUME     0x0200
190 #define  CUC_SUSPEND    0x0300
191 #define  CUC_ABORT      0x0400
192 #define  RX_START       0x0010
193 #define  RX_RESUME      0x0020
194 #define  RX_SUSPEND     0x0030
195 #define  RX_ABORT       0x0040
196
197 #define TX_TIMEOUT      (HZ/20)
198
199
200 struct i596_reg {
201         unsigned short porthi;
202         unsigned short portlo;
203         unsigned long ca;
204 };
205
206 #define EOF             0x8000
207 #define SIZE_MASK       0x3fff
208
209 struct i596_tbd {
210         unsigned short size;
211         unsigned short pad;
212         struct i596_tbd *next;
213         char *data;
214 };
215
216 /* The command structure has two 'next' pointers; v_next is the address of
217  * the next command as seen by the CPU, b_next is the address of the next
218  * command as seen by the 82596.  The b_next pointer, as used by the 82596
219  * always references the status field of the next command, rather than the
220  * v_next field, because the 82596 is unaware of v_next.  It may seem more
221  * logical to put v_next at the end of the structure, but we cannot do that
222  * because the 82596 expects other fields to be there, depending on command
223  * type.
224  */
225
226 struct i596_cmd {
227         struct i596_cmd *v_next;        /* Address from CPUs viewpoint */
228         unsigned short status;
229         unsigned short command;
230         struct i596_cmd *b_next;        /* Address from i596 viewpoint */
231 };
232
233 struct tx_cmd {
234         struct i596_cmd cmd;
235         struct i596_tbd *tbd;
236         unsigned short size;
237         unsigned short pad;
238         struct sk_buff *skb;    /* So we can free it after tx */
239 };
240
241 struct tdr_cmd {
242         struct i596_cmd cmd;
243         unsigned short status;
244         unsigned short pad;
245 };
246
247 struct mc_cmd {
248         struct i596_cmd cmd;
249         short mc_cnt;
250         char mc_addrs[MAX_MC_CNT*6];
251 };
252
253 struct sa_cmd {
254         struct i596_cmd cmd;
255         char eth_addr[8];
256 };
257
258 struct cf_cmd {
259         struct i596_cmd cmd;
260         char i596_config[16];
261 };
262
263 struct i596_rfd {
264         unsigned short stat;
265         unsigned short cmd;
266         struct i596_rfd *b_next;        /* Address from i596 viewpoint */
267         struct i596_rbd *rbd;
268         unsigned short count;
269         unsigned short size;
270         struct i596_rfd *v_next;        /* Address from CPUs viewpoint */
271         struct i596_rfd *v_prev;
272 };
273
274 struct i596_rbd {
275     unsigned short count;
276     unsigned short zero1;
277     struct i596_rbd *b_next;
278     unsigned char *b_data;              /* Address from i596 viewpoint */
279     unsigned short size;
280     unsigned short zero2;
281     struct sk_buff *skb;
282     struct i596_rbd *v_next;
283     struct i596_rbd *b_addr;            /* This rbd addr from i596 view */
284     unsigned char *v_data;              /* Address from CPUs viewpoint */
285 };
286
287 #define TX_RING_SIZE 64
288 #define RX_RING_SIZE 16
289
290 struct i596_scb {
291         unsigned short status;
292         unsigned short command;
293         struct i596_cmd *cmd;
294         struct i596_rfd *rfd;
295         unsigned long crc_err;
296         unsigned long align_err;
297         unsigned long resource_err;
298         unsigned long over_err;
299         unsigned long rcvdt_err;
300         unsigned long short_err;
301         unsigned short t_on;
302         unsigned short t_off;
303 };
304
305 struct i596_iscp {
306         unsigned long stat;
307         struct i596_scb *scb;
308 };
309
310 struct i596_scp {
311         unsigned long sysbus;
312         unsigned long pad;
313         struct i596_iscp *iscp;
314 };
315
316 struct i596_private {
317         volatile struct i596_scp scp;
318         volatile struct i596_iscp iscp;
319         volatile struct i596_scb scb;
320         struct sa_cmd sa_cmd;
321         struct cf_cmd cf_cmd;
322         struct tdr_cmd tdr_cmd;
323         struct mc_cmd mc_cmd;
324         unsigned long stat;
325         int last_restart __attribute__((aligned(4)));
326         struct i596_rfd *rfd_head;
327         struct i596_rbd *rbd_head;
328         struct i596_cmd *cmd_tail;
329         struct i596_cmd *cmd_head;
330         int cmd_backlog;
331         unsigned long last_cmd;
332         struct i596_rfd rfds[RX_RING_SIZE];
333         struct i596_rbd rbds[RX_RING_SIZE];
334         struct tx_cmd tx_cmds[TX_RING_SIZE];
335         struct i596_tbd tbds[TX_RING_SIZE];
336         int next_tx_cmd;
337         spinlock_t lock;
338 };
339
340 static char init_setup[] =
341 {
342         0x8E,                   /* length, prefetch on */
343         0xC8,                   /* fifo to 8, monitor off */
344 #ifdef CONFIG_VME
345         0xc0,                   /* don't save bad frames */
346 #else
347         0x80,                   /* don't save bad frames */
348 #endif
349         0x2E,                   /* No source address insertion, 8 byte preamble */
350         0x00,                   /* priority and backoff defaults */
351         0x60,                   /* interframe spacing */
352         0x00,                   /* slot time LSB */
353         0xf2,                   /* slot time and retries */
354         0x00,                   /* promiscuous mode */
355         0x00,                   /* collision detect */
356         0x40,                   /* minimum frame length */
357         0xff,
358         0x00,
359         0x7f /*  *multi IA */ };
360
361 static int i596_open(struct net_device *dev);
362 static netdev_tx_t i596_start_xmit(struct sk_buff *skb, struct net_device *dev);
363 static irqreturn_t i596_interrupt(int irq, void *dev_id);
364 static int i596_close(struct net_device *dev);
365 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd);
366 static void i596_tx_timeout (struct net_device *dev);
367 static void print_eth(unsigned char *buf, char *str);
368 static void set_multicast_list(struct net_device *dev);
369
370 static int rx_ring_size = RX_RING_SIZE;
371 static int ticks_limit = 25;
372 static int max_cmd_backlog = TX_RING_SIZE-1;
373
374
375 static inline void CA(struct net_device *dev)
376 {
377 #ifdef ENABLE_MVME16x_NET
378         if (MACH_IS_MVME16x) {
379                 ((struct i596_reg *) dev->base_addr)->ca = 1;
380         }
381 #endif
382 #ifdef ENABLE_BVME6000_NET
383         if (MACH_IS_BVME6000) {
384                 volatile u32 i;
385
386                 i = *(volatile u32 *) (dev->base_addr);
387         }
388 #endif
389 }
390
391
392 static inline void MPU_PORT(struct net_device *dev, int c, volatile void *x)
393 {
394 #ifdef ENABLE_MVME16x_NET
395         if (MACH_IS_MVME16x) {
396                 struct i596_reg *p = (struct i596_reg *) (dev->base_addr);
397                 p->porthi = ((c) | (u32) (x)) & 0xffff;
398                 p->portlo = ((c) | (u32) (x)) >> 16;
399         }
400 #endif
401 #ifdef ENABLE_BVME6000_NET
402         if (MACH_IS_BVME6000) {
403                 u32 v = (u32) (c) | (u32) (x);
404                 v = ((u32) (v) << 16) | ((u32) (v) >> 16);
405                 *(volatile u32 *) dev->base_addr = v;
406                 udelay(1);
407                 *(volatile u32 *) dev->base_addr = v;
408         }
409 #endif
410 }
411
412
413 static inline int wait_istat(struct net_device *dev, struct i596_private *lp, int delcnt, char *str)
414 {
415         while (--delcnt && lp->iscp.stat)
416                 udelay(10);
417         if (!delcnt) {
418                 printk(KERN_ERR "%s: %s, status %4.4x, cmd %4.4x.\n",
419                      dev->name, str, lp->scb.status, lp->scb.command);
420                 return -1;
421         }
422         else
423                 return 0;
424 }
425
426
427 static inline int wait_cmd(struct net_device *dev, struct i596_private *lp, int delcnt, char *str)
428 {
429         while (--delcnt && lp->scb.command)
430                 udelay(10);
431         if (!delcnt) {
432                 printk(KERN_ERR "%s: %s, status %4.4x, cmd %4.4x.\n",
433                      dev->name, str, lp->scb.status, lp->scb.command);
434                 return -1;
435         }
436         else
437                 return 0;
438 }
439
440
441 static inline int wait_cfg(struct net_device *dev, struct i596_cmd *cmd, int delcnt, char *str)
442 {
443         volatile struct i596_cmd *c = cmd;
444
445         while (--delcnt && c->command)
446                 udelay(10);
447         if (!delcnt) {
448                 printk(KERN_ERR "%s: %s.\n", dev->name, str);
449                 return -1;
450         }
451         else
452                 return 0;
453 }
454
455
456 static void i596_display_data(struct net_device *dev)
457 {
458         struct i596_private *lp = dev->ml_priv;
459         struct i596_cmd *cmd;
460         struct i596_rfd *rfd;
461         struct i596_rbd *rbd;
462
463         printk(KERN_ERR "lp and scp at %p, .sysbus = %08lx, .iscp = %p\n",
464                &lp->scp, lp->scp.sysbus, lp->scp.iscp);
465         printk(KERN_ERR "iscp at %p, iscp.stat = %08lx, .scb = %p\n",
466                &lp->iscp, lp->iscp.stat, lp->iscp.scb);
467         printk(KERN_ERR "scb at %p, scb.status = %04x, .command = %04x,"
468                 " .cmd = %p, .rfd = %p\n",
469                &lp->scb, lp->scb.status, lp->scb.command,
470                 lp->scb.cmd, lp->scb.rfd);
471         printk(KERN_ERR "   errors: crc %lx, align %lx, resource %lx,"
472                " over %lx, rcvdt %lx, short %lx\n",
473                 lp->scb.crc_err, lp->scb.align_err, lp->scb.resource_err,
474                 lp->scb.over_err, lp->scb.rcvdt_err, lp->scb.short_err);
475         cmd = lp->cmd_head;
476         while (cmd != I596_NULL) {
477                 printk(KERN_ERR "cmd at %p, .status = %04x, .command = %04x, .b_next = %p\n",
478                   cmd, cmd->status, cmd->command, cmd->b_next);
479                 cmd = cmd->v_next;
480         }
481         rfd = lp->rfd_head;
482         printk(KERN_ERR "rfd_head = %p\n", rfd);
483         do {
484                 printk(KERN_ERR "   %p .stat %04x, .cmd %04x, b_next %p, rbd %p,"
485                         " count %04x\n",
486                         rfd, rfd->stat, rfd->cmd, rfd->b_next, rfd->rbd,
487                         rfd->count);
488                 rfd = rfd->v_next;
489         } while (rfd != lp->rfd_head);
490         rbd = lp->rbd_head;
491         printk(KERN_ERR "rbd_head = %p\n", rbd);
492         do {
493                 printk(KERN_ERR "   %p .count %04x, b_next %p, b_data %p, size %04x\n",
494                         rbd, rbd->count, rbd->b_next, rbd->b_data, rbd->size);
495                 rbd = rbd->v_next;
496         } while (rbd != lp->rbd_head);
497 }
498
499
500 #if defined(ENABLE_MVME16x_NET) || defined(ENABLE_BVME6000_NET)
501 static irqreturn_t i596_error(int irq, void *dev_id)
502 {
503         struct net_device *dev = dev_id;
504 #ifdef ENABLE_MVME16x_NET
505         if (MACH_IS_MVME16x) {
506                 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
507
508                 pcc2[0x28] = 1;
509                 pcc2[0x2b] = 0x1d;
510         }
511 #endif
512 #ifdef ENABLE_BVME6000_NET
513         if (MACH_IS_BVME6000) {
514                 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
515
516                 *ethirq = 1;
517                 *ethirq = 3;
518         }
519 #endif
520         printk(KERN_ERR "%s: Error interrupt\n", dev->name);
521         i596_display_data(dev);
522         return IRQ_HANDLED;
523 }
524 #endif
525
526 static inline void remove_rx_bufs(struct net_device *dev)
527 {
528         struct i596_private *lp = dev->ml_priv;
529         struct i596_rbd *rbd;
530         int i;
531
532         for (i = 0, rbd = lp->rbds; i < rx_ring_size; i++, rbd++) {
533                 if (rbd->skb == NULL)
534                         break;
535                 dev_kfree_skb(rbd->skb);
536                 rbd->skb = NULL;
537         }
538 }
539
540 static inline int init_rx_bufs(struct net_device *dev)
541 {
542         struct i596_private *lp = dev->ml_priv;
543         int i;
544         struct i596_rfd *rfd;
545         struct i596_rbd *rbd;
546
547         /* First build the Receive Buffer Descriptor List */
548
549         for (i = 0, rbd = lp->rbds; i < rx_ring_size; i++, rbd++) {
550                 struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
551
552                 if (skb == NULL) {
553                         remove_rx_bufs(dev);
554                         return -ENOMEM;
555                 }
556
557                 rbd->v_next = rbd+1;
558                 rbd->b_next = WSWAPrbd(virt_to_bus(rbd+1));
559                 rbd->b_addr = WSWAPrbd(virt_to_bus(rbd));
560                 rbd->skb = skb;
561                 rbd->v_data = skb->data;
562                 rbd->b_data = WSWAPchar(virt_to_bus(skb->data));
563                 rbd->size = PKT_BUF_SZ;
564 #ifdef __mc68000__
565                 cache_clear(virt_to_phys(skb->data), PKT_BUF_SZ);
566 #endif
567         }
568         lp->rbd_head = lp->rbds;
569         rbd = lp->rbds + rx_ring_size - 1;
570         rbd->v_next = lp->rbds;
571         rbd->b_next = WSWAPrbd(virt_to_bus(lp->rbds));
572
573         /* Now build the Receive Frame Descriptor List */
574
575         for (i = 0, rfd = lp->rfds; i < rx_ring_size; i++, rfd++) {
576                 rfd->rbd = I596_NULL;
577                 rfd->v_next = rfd+1;
578                 rfd->v_prev = rfd-1;
579                 rfd->b_next = WSWAPrfd(virt_to_bus(rfd+1));
580                 rfd->cmd = CMD_FLEX;
581         }
582         lp->rfd_head = lp->rfds;
583         lp->scb.rfd = WSWAPrfd(virt_to_bus(lp->rfds));
584         rfd = lp->rfds;
585         rfd->rbd = lp->rbd_head;
586         rfd->v_prev = lp->rfds + rx_ring_size - 1;
587         rfd = lp->rfds + rx_ring_size - 1;
588         rfd->v_next = lp->rfds;
589         rfd->b_next = WSWAPrfd(virt_to_bus(lp->rfds));
590         rfd->cmd = CMD_EOL|CMD_FLEX;
591
592         return 0;
593 }
594
595
596 static void rebuild_rx_bufs(struct net_device *dev)
597 {
598         struct i596_private *lp = dev->ml_priv;
599         int i;
600
601         /* Ensure rx frame/buffer descriptors are tidy */
602
603         for (i = 0; i < rx_ring_size; i++) {
604                 lp->rfds[i].rbd = I596_NULL;
605                 lp->rfds[i].cmd = CMD_FLEX;
606         }
607         lp->rfds[rx_ring_size-1].cmd = CMD_EOL|CMD_FLEX;
608         lp->rfd_head = lp->rfds;
609         lp->scb.rfd = WSWAPrfd(virt_to_bus(lp->rfds));
610         lp->rbd_head = lp->rbds;
611         lp->rfds[0].rbd = WSWAPrbd(virt_to_bus(lp->rbds));
612 }
613
614
615 static int init_i596_mem(struct net_device *dev)
616 {
617         struct i596_private *lp = dev->ml_priv;
618         unsigned long flags;
619
620         MPU_PORT(dev, PORT_RESET, NULL);
621
622         udelay(100);            /* Wait 100us - seems to help */
623
624 #if defined(ENABLE_MVME16x_NET) || defined(ENABLE_BVME6000_NET)
625 #ifdef ENABLE_MVME16x_NET
626         if (MACH_IS_MVME16x) {
627                 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
628
629                 /* Disable all ints for now */
630                 pcc2[0x28] = 1;
631                 pcc2[0x2a] = 0x48;
632                 /* Following disables snooping.  Snooping is not required
633                  * as we make appropriate use of non-cached pages for
634                  * shared data, and cache_push/cache_clear.
635                  */
636                 pcc2[0x2b] = 0x08;
637         }
638 #endif
639 #ifdef ENABLE_BVME6000_NET
640         if (MACH_IS_BVME6000) {
641                 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
642
643                 *ethirq = 1;
644         }
645 #endif
646
647         /* change the scp address */
648
649         MPU_PORT(dev, PORT_ALTSCP, (void *)virt_to_bus((void *)&lp->scp));
650
651 #endif
652
653         lp->last_cmd = jiffies;
654
655 #ifdef ENABLE_MVME16x_NET
656         if (MACH_IS_MVME16x)
657                 lp->scp.sysbus = 0x00000054;
658 #endif
659 #ifdef ENABLE_BVME6000_NET
660         if (MACH_IS_BVME6000)
661                 lp->scp.sysbus = 0x0000004c;
662 #endif
663
664         lp->scp.iscp = WSWAPiscp(virt_to_bus((void *)&lp->iscp));
665         lp->iscp.scb = WSWAPscb(virt_to_bus((void *)&lp->scb));
666         lp->iscp.stat = ISCP_BUSY;
667         lp->cmd_backlog = 0;
668
669         lp->cmd_head = lp->scb.cmd = I596_NULL;
670
671 #ifdef ENABLE_BVME6000_NET
672         if (MACH_IS_BVME6000) {
673                 lp->scb.t_on  = 7 * 25;
674                 lp->scb.t_off = 1 * 25;
675         }
676 #endif
677
678         DEB(DEB_INIT,printk(KERN_DEBUG "%s: starting i82596.\n", dev->name));
679
680         CA(dev);
681
682         if (wait_istat(dev,lp,1000,"initialization timed out"))
683                 goto failed;
684         DEB(DEB_INIT,printk(KERN_DEBUG "%s: i82596 initialization successful\n", dev->name));
685
686         /* Ensure rx frame/buffer descriptors are tidy */
687         rebuild_rx_bufs(dev);
688         lp->scb.command = 0;
689
690 #ifdef ENABLE_MVME16x_NET
691         if (MACH_IS_MVME16x) {
692                 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
693
694                 /* Enable ints, etc. now */
695                 pcc2[0x2a] = 0x55;      /* Edge sensitive */
696                 pcc2[0x2b] = 0x15;
697         }
698 #endif
699 #ifdef ENABLE_BVME6000_NET
700         if (MACH_IS_BVME6000) {
701                 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
702
703                 *ethirq = 3;
704         }
705 #endif
706
707
708         DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdConfigure\n", dev->name));
709         memcpy(lp->cf_cmd.i596_config, init_setup, 14);
710         lp->cf_cmd.cmd.command = CmdConfigure;
711         i596_add_cmd(dev, &lp->cf_cmd.cmd);
712
713         DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdSASetup\n", dev->name));
714         memcpy(lp->sa_cmd.eth_addr, dev->dev_addr, 6);
715         lp->sa_cmd.cmd.command = CmdSASetup;
716         i596_add_cmd(dev, &lp->sa_cmd.cmd);
717
718         DEB(DEB_INIT,printk(KERN_DEBUG "%s: queuing CmdTDR\n", dev->name));
719         lp->tdr_cmd.cmd.command = CmdTDR;
720         i596_add_cmd(dev, &lp->tdr_cmd.cmd);
721
722         spin_lock_irqsave (&lp->lock, flags);
723
724         if (wait_cmd(dev,lp,1000,"timed out waiting to issue RX_START")) {
725                 spin_unlock_irqrestore (&lp->lock, flags);
726                 goto failed;
727         }
728         DEB(DEB_INIT,printk(KERN_DEBUG "%s: Issuing RX_START\n", dev->name));
729         lp->scb.command = RX_START;
730         CA(dev);
731
732         spin_unlock_irqrestore (&lp->lock, flags);
733
734         if (wait_cmd(dev,lp,1000,"RX_START not processed"))
735                 goto failed;
736         DEB(DEB_INIT,printk(KERN_DEBUG "%s: Receive unit started OK\n", dev->name));
737         return 0;
738
739 failed:
740         printk(KERN_CRIT "%s: Failed to initialise 82596\n", dev->name);
741         MPU_PORT(dev, PORT_RESET, NULL);
742         return -1;
743 }
744
745 static inline int i596_rx(struct net_device *dev)
746 {
747         struct i596_private *lp = dev->ml_priv;
748         struct i596_rfd *rfd;
749         struct i596_rbd *rbd;
750         int frames = 0;
751
752         DEB(DEB_RXFRAME,printk(KERN_DEBUG "i596_rx(), rfd_head %p, rbd_head %p\n",
753                         lp->rfd_head, lp->rbd_head));
754
755         rfd = lp->rfd_head;             /* Ref next frame to check */
756
757         while ((rfd->stat) & STAT_C) {  /* Loop while complete frames */
758                 if (rfd->rbd == I596_NULL)
759                         rbd = I596_NULL;
760                 else if (rfd->rbd == lp->rbd_head->b_addr)
761                         rbd = lp->rbd_head;
762                 else {
763                         printk(KERN_CRIT "%s: rbd chain broken!\n", dev->name);
764                         /* XXX Now what? */
765                         rbd = I596_NULL;
766                 }
767                 DEB(DEB_RXFRAME, printk(KERN_DEBUG "  rfd %p, rfd.rbd %p, rfd.stat %04x\n",
768                         rfd, rfd->rbd, rfd->stat));
769
770                 if (rbd != I596_NULL && ((rfd->stat) & STAT_OK)) {
771                         /* a good frame */
772                         int pkt_len = rbd->count & 0x3fff;
773                         struct sk_buff *skb = rbd->skb;
774                         int rx_in_place = 0;
775
776                         DEB(DEB_RXADDR,print_eth(rbd->v_data, "received"));
777                         frames++;
778
779                         /* Check if the packet is long enough to just accept
780                          * without copying to a properly sized skbuff.
781                          */
782
783                         if (pkt_len > rx_copybreak) {
784                                 struct sk_buff *newskb;
785
786                                 /* Get fresh skbuff to replace filled one. */
787                                 newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
788                                 if (newskb == NULL) {
789                                         skb = NULL;     /* drop pkt */
790                                         goto memory_squeeze;
791                                 }
792                                 /* Pass up the skb already on the Rx ring. */
793                                 skb_put(skb, pkt_len);
794                                 rx_in_place = 1;
795                                 rbd->skb = newskb;
796                                 rbd->v_data = newskb->data;
797                                 rbd->b_data = WSWAPchar(virt_to_bus(newskb->data));
798 #ifdef __mc68000__
799                                 cache_clear(virt_to_phys(newskb->data), PKT_BUF_SZ);
800 #endif
801                         }
802                         else
803                                 skb = netdev_alloc_skb(dev, pkt_len + 2);
804 memory_squeeze:
805                         if (skb == NULL) {
806                                 /* XXX tulip.c can defer packets here!! */
807                                 printk(KERN_WARNING "%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
808                                 dev->stats.rx_dropped++;
809                         }
810                         else {
811                                 if (!rx_in_place) {
812                                         /* 16 byte align the data fields */
813                                         skb_reserve(skb, 2);
814                                         memcpy(skb_put(skb,pkt_len), rbd->v_data, pkt_len);
815                                 }
816                                 skb->protocol=eth_type_trans(skb,dev);
817                                 skb->len = pkt_len;
818 #ifdef __mc68000__
819                                 cache_clear(virt_to_phys(rbd->skb->data),
820                                                 pkt_len);
821 #endif
822                                 netif_rx(skb);
823                                 dev->stats.rx_packets++;
824                                 dev->stats.rx_bytes+=pkt_len;
825                         }
826                 }
827                 else {
828                         DEB(DEB_ERRORS, printk(KERN_DEBUG "%s: Error, rfd.stat = 0x%04x\n",
829                                         dev->name, rfd->stat));
830                         dev->stats.rx_errors++;
831                         if ((rfd->stat) & 0x0001)
832                                 dev->stats.collisions++;
833                         if ((rfd->stat) & 0x0080)
834                                 dev->stats.rx_length_errors++;
835                         if ((rfd->stat) & 0x0100)
836                                 dev->stats.rx_over_errors++;
837                         if ((rfd->stat) & 0x0200)
838                                 dev->stats.rx_fifo_errors++;
839                         if ((rfd->stat) & 0x0400)
840                                 dev->stats.rx_frame_errors++;
841                         if ((rfd->stat) & 0x0800)
842                                 dev->stats.rx_crc_errors++;
843                         if ((rfd->stat) & 0x1000)
844                                 dev->stats.rx_length_errors++;
845                 }
846
847                 /* Clear the buffer descriptor count and EOF + F flags */
848
849                 if (rbd != I596_NULL && (rbd->count & 0x4000)) {
850                         rbd->count = 0;
851                         lp->rbd_head = rbd->v_next;
852                 }
853
854                 /* Tidy the frame descriptor, marking it as end of list */
855
856                 rfd->rbd = I596_NULL;
857                 rfd->stat = 0;
858                 rfd->cmd = CMD_EOL|CMD_FLEX;
859                 rfd->count = 0;
860
861                 /* Remove end-of-list from old end descriptor */
862
863                 rfd->v_prev->cmd = CMD_FLEX;
864
865                 /* Update record of next frame descriptor to process */
866
867                 lp->scb.rfd = rfd->b_next;
868                 lp->rfd_head = rfd->v_next;
869                 rfd = lp->rfd_head;
870         }
871
872         DEB(DEB_RXFRAME,printk(KERN_DEBUG "frames %d\n", frames));
873
874         return 0;
875 }
876
877
878 static void i596_cleanup_cmd(struct net_device *dev, struct i596_private *lp)
879 {
880         struct i596_cmd *ptr;
881
882         while (lp->cmd_head != I596_NULL) {
883                 ptr = lp->cmd_head;
884                 lp->cmd_head = ptr->v_next;
885                 lp->cmd_backlog--;
886
887                 switch ((ptr->command) & 0x7) {
888                 case CmdTx:
889                         {
890                                 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
891                                 struct sk_buff *skb = tx_cmd->skb;
892
893                                 dev_kfree_skb(skb);
894
895                                 dev->stats.tx_errors++;
896                                 dev->stats.tx_aborted_errors++;
897
898                                 ptr->v_next = ptr->b_next = I596_NULL;
899                                 tx_cmd->cmd.command = 0;  /* Mark as free */
900                                 break;
901                         }
902                 default:
903                         ptr->v_next = ptr->b_next = I596_NULL;
904                 }
905         }
906
907         wait_cmd(dev,lp,100,"i596_cleanup_cmd timed out");
908         lp->scb.cmd = I596_NULL;
909 }
910
911 static void i596_reset(struct net_device *dev, struct i596_private *lp,
912                         int ioaddr)
913 {
914         unsigned long flags;
915
916         DEB(DEB_RESET,printk(KERN_DEBUG "i596_reset\n"));
917
918         spin_lock_irqsave (&lp->lock, flags);
919
920         wait_cmd(dev,lp,100,"i596_reset timed out");
921
922         netif_stop_queue(dev);
923
924         lp->scb.command = CUC_ABORT | RX_ABORT;
925         CA(dev);
926
927         /* wait for shutdown */
928         wait_cmd(dev,lp,1000,"i596_reset 2 timed out");
929         spin_unlock_irqrestore (&lp->lock, flags);
930
931         i596_cleanup_cmd(dev,lp);
932         i596_rx(dev);
933
934         netif_start_queue(dev);
935         init_i596_mem(dev);
936 }
937
938 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd)
939 {
940         struct i596_private *lp = dev->ml_priv;
941         int ioaddr = dev->base_addr;
942         unsigned long flags;
943
944         DEB(DEB_ADDCMD,printk(KERN_DEBUG "i596_add_cmd\n"));
945
946         cmd->status = 0;
947         cmd->command |= (CMD_EOL | CMD_INTR);
948         cmd->v_next = cmd->b_next = I596_NULL;
949
950         spin_lock_irqsave (&lp->lock, flags);
951
952         if (lp->cmd_head != I596_NULL) {
953                 lp->cmd_tail->v_next = cmd;
954                 lp->cmd_tail->b_next = WSWAPcmd(virt_to_bus(&cmd->status));
955         } else {
956                 lp->cmd_head = cmd;
957                 wait_cmd(dev,lp,100,"i596_add_cmd timed out");
958                 lp->scb.cmd = WSWAPcmd(virt_to_bus(&cmd->status));
959                 lp->scb.command = CUC_START;
960                 CA(dev);
961         }
962         lp->cmd_tail = cmd;
963         lp->cmd_backlog++;
964
965         spin_unlock_irqrestore (&lp->lock, flags);
966
967         if (lp->cmd_backlog > max_cmd_backlog) {
968                 unsigned long tickssofar = jiffies - lp->last_cmd;
969
970                 if (tickssofar < ticks_limit)
971                         return;
972
973                 printk(KERN_NOTICE "%s: command unit timed out, status resetting.\n", dev->name);
974
975                 i596_reset(dev, lp, ioaddr);
976         }
977 }
978
979 static int i596_open(struct net_device *dev)
980 {
981         int res = 0;
982
983         DEB(DEB_OPEN,printk(KERN_DEBUG "%s: i596_open() irq %d.\n", dev->name, dev->irq));
984
985         if (request_irq(dev->irq, i596_interrupt, 0, "i82596", dev)) {
986                 printk(KERN_ERR "%s: IRQ %d not free\n", dev->name, dev->irq);
987                 return -EAGAIN;
988         }
989 #ifdef ENABLE_MVME16x_NET
990         if (MACH_IS_MVME16x) {
991                 if (request_irq(0x56, i596_error, 0, "i82596_error", dev)) {
992                         res = -EAGAIN;
993                         goto err_irq_dev;
994                 }
995         }
996 #endif
997         res = init_rx_bufs(dev);
998         if (res)
999                 goto err_irq_56;
1000
1001         netif_start_queue(dev);
1002
1003         if (init_i596_mem(dev)) {
1004                 res = -EAGAIN;
1005                 goto err_queue;
1006         }
1007
1008         return 0;
1009
1010 err_queue:
1011         netif_stop_queue(dev);
1012         remove_rx_bufs(dev);
1013 err_irq_56:
1014 #ifdef ENABLE_MVME16x_NET
1015         free_irq(0x56, dev);
1016 err_irq_dev:
1017 #endif
1018         free_irq(dev->irq, dev);
1019
1020         return res;
1021 }
1022
1023 static void i596_tx_timeout (struct net_device *dev)
1024 {
1025         struct i596_private *lp = dev->ml_priv;
1026         int ioaddr = dev->base_addr;
1027
1028         /* Transmitter timeout, serious problems. */
1029         DEB(DEB_ERRORS,printk(KERN_ERR "%s: transmit timed out, status resetting.\n",
1030                         dev->name));
1031
1032         dev->stats.tx_errors++;
1033
1034         /* Try to restart the adaptor */
1035         if (lp->last_restart == dev->stats.tx_packets) {
1036                 DEB(DEB_ERRORS,printk(KERN_ERR "Resetting board.\n"));
1037                 /* Shutdown and restart */
1038                 i596_reset (dev, lp, ioaddr);
1039         } else {
1040                 /* Issue a channel attention signal */
1041                 DEB(DEB_ERRORS,printk(KERN_ERR "Kicking board.\n"));
1042                 lp->scb.command = CUC_START | RX_START;
1043                 CA (dev);
1044                 lp->last_restart = dev->stats.tx_packets;
1045         }
1046
1047         dev->trans_start = jiffies; /* prevent tx timeout */
1048         netif_wake_queue (dev);
1049 }
1050
1051 static netdev_tx_t i596_start_xmit(struct sk_buff *skb, struct net_device *dev)
1052 {
1053         struct i596_private *lp = dev->ml_priv;
1054         struct tx_cmd *tx_cmd;
1055         struct i596_tbd *tbd;
1056         short length = skb->len;
1057
1058         DEB(DEB_STARTTX,printk(KERN_DEBUG "%s: i596_start_xmit(%x,%p) called\n",
1059                                 dev->name, skb->len, skb->data));
1060
1061         if (skb->len < ETH_ZLEN) {
1062                 if (skb_padto(skb, ETH_ZLEN))
1063                         return NETDEV_TX_OK;
1064                 length = ETH_ZLEN;
1065         }
1066         netif_stop_queue(dev);
1067
1068         tx_cmd = lp->tx_cmds + lp->next_tx_cmd;
1069         tbd = lp->tbds + lp->next_tx_cmd;
1070
1071         if (tx_cmd->cmd.command) {
1072                 printk(KERN_NOTICE "%s: xmit ring full, dropping packet.\n",
1073                                 dev->name);
1074                 dev->stats.tx_dropped++;
1075
1076                 dev_kfree_skb(skb);
1077         } else {
1078                 if (++lp->next_tx_cmd == TX_RING_SIZE)
1079                         lp->next_tx_cmd = 0;
1080                 tx_cmd->tbd = WSWAPtbd(virt_to_bus(tbd));
1081                 tbd->next = I596_NULL;
1082
1083                 tx_cmd->cmd.command = CMD_FLEX | CmdTx;
1084                 tx_cmd->skb = skb;
1085
1086                 tx_cmd->pad = 0;
1087                 tx_cmd->size = 0;
1088                 tbd->pad = 0;
1089                 tbd->size = EOF | length;
1090
1091                 tbd->data = WSWAPchar(virt_to_bus(skb->data));
1092
1093 #ifdef __mc68000__
1094                 cache_push(virt_to_phys(skb->data), length);
1095 #endif
1096                 DEB(DEB_TXADDR,print_eth(skb->data, "tx-queued"));
1097                 i596_add_cmd(dev, &tx_cmd->cmd);
1098
1099                 dev->stats.tx_packets++;
1100                 dev->stats.tx_bytes += length;
1101         }
1102
1103         netif_start_queue(dev);
1104
1105         return NETDEV_TX_OK;
1106 }
1107
1108 static void print_eth(unsigned char *add, char *str)
1109 {
1110         printk(KERN_DEBUG "i596 0x%p, %pM --> %pM %02X%02X, %s\n",
1111                add, add + 6, add, add[12], add[13], str);
1112 }
1113
1114 static int io = 0x300;
1115 static int irq = 10;
1116
1117 static const struct net_device_ops i596_netdev_ops = {
1118         .ndo_open               = i596_open,
1119         .ndo_stop               = i596_close,
1120         .ndo_start_xmit         = i596_start_xmit,
1121         .ndo_set_rx_mode        = set_multicast_list,
1122         .ndo_tx_timeout         = i596_tx_timeout,
1123         .ndo_change_mtu         = eth_change_mtu,
1124         .ndo_set_mac_address    = eth_mac_addr,
1125         .ndo_validate_addr      = eth_validate_addr,
1126 };
1127
1128 struct net_device * __init i82596_probe(int unit)
1129 {
1130         struct net_device *dev;
1131         int i;
1132         struct i596_private *lp;
1133         char eth_addr[8];
1134         static int probed;
1135         int err;
1136
1137         if (probed)
1138                 return ERR_PTR(-ENODEV);
1139         probed++;
1140
1141         dev = alloc_etherdev(0);
1142         if (!dev)
1143                 return ERR_PTR(-ENOMEM);
1144
1145         if (unit >= 0) {
1146                 sprintf(dev->name, "eth%d", unit);
1147                 netdev_boot_setup_check(dev);
1148         } else {
1149                 dev->base_addr = io;
1150                 dev->irq = irq;
1151         }
1152
1153 #ifdef ENABLE_MVME16x_NET
1154         if (MACH_IS_MVME16x) {
1155                 if (mvme16x_config & MVME16x_CONFIG_NO_ETHERNET) {
1156                         printk(KERN_NOTICE "Ethernet probe disabled - chip not present\n");
1157                         err = -ENODEV;
1158                         goto out;
1159                 }
1160                 memcpy(eth_addr, (void *) 0xfffc1f2c, 6);       /* YUCK! Get addr from NOVRAM */
1161                 dev->base_addr = MVME_I596_BASE;
1162                 dev->irq = (unsigned) MVME16x_IRQ_I596;
1163                 goto found;
1164         }
1165 #endif
1166 #ifdef ENABLE_BVME6000_NET
1167         if (MACH_IS_BVME6000) {
1168                 volatile unsigned char *rtc = (unsigned char *) BVME_RTC_BASE;
1169                 unsigned char msr = rtc[3];
1170                 int i;
1171
1172                 rtc[3] |= 0x80;
1173                 for (i = 0; i < 6; i++)
1174                         eth_addr[i] = rtc[i * 4 + 7];   /* Stored in RTC RAM at offset 1 */
1175                 rtc[3] = msr;
1176                 dev->base_addr = BVME_I596_BASE;
1177                 dev->irq = (unsigned) BVME_IRQ_I596;
1178                 goto found;
1179         }
1180 #endif
1181         err = -ENODEV;
1182         goto out;
1183
1184 found:
1185         dev->mem_start = (int)__get_free_pages(GFP_ATOMIC, 0);
1186         if (!dev->mem_start) {
1187                 err = -ENOMEM;
1188                 goto out1;
1189         }
1190
1191         DEB(DEB_PROBE,printk(KERN_INFO "%s: 82596 at %#3lx,", dev->name, dev->base_addr));
1192
1193         for (i = 0; i < 6; i++)
1194                 DEB(DEB_PROBE,printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]));
1195
1196         DEB(DEB_PROBE,printk(" IRQ %d.\n", dev->irq));
1197
1198         DEB(DEB_PROBE,printk(KERN_INFO "%s", version));
1199
1200         /* The 82596-specific entries in the device structure. */
1201         dev->netdev_ops = &i596_netdev_ops;
1202         dev->watchdog_timeo = TX_TIMEOUT;
1203
1204         dev->ml_priv = (void *)(dev->mem_start);
1205
1206         lp = dev->ml_priv;
1207         DEB(DEB_INIT,printk(KERN_DEBUG "%s: lp at 0x%08lx (%zd bytes), "
1208                         "lp->scb at 0x%08lx\n",
1209                         dev->name, (unsigned long)lp,
1210                         sizeof(struct i596_private), (unsigned long)&lp->scb));
1211         memset((void *) lp, 0, sizeof(struct i596_private));
1212
1213 #ifdef __mc68000__
1214         cache_push(virt_to_phys((void *)(dev->mem_start)), 4096);
1215         cache_clear(virt_to_phys((void *)(dev->mem_start)), 4096);
1216         kernel_set_cachemode((void *)(dev->mem_start), 4096, IOMAP_NOCACHE_SER);
1217 #endif
1218         lp->scb.command = 0;
1219         lp->scb.cmd = I596_NULL;
1220         lp->scb.rfd = I596_NULL;
1221         spin_lock_init(&lp->lock);
1222
1223         err = register_netdev(dev);
1224         if (err)
1225                 goto out2;
1226         return dev;
1227 out2:
1228 #ifdef __mc68000__
1229         /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING,
1230          * XXX which may be invalid (CONFIG_060_WRITETHROUGH)
1231          */
1232         kernel_set_cachemode((void *)(dev->mem_start), 4096,
1233                         IOMAP_FULL_CACHING);
1234 #endif
1235         free_page ((u32)(dev->mem_start));
1236 out1:
1237 out:
1238         free_netdev(dev);
1239         return ERR_PTR(err);
1240 }
1241
1242 static irqreturn_t i596_interrupt(int irq, void *dev_id)
1243 {
1244         struct net_device *dev = dev_id;
1245         struct i596_private *lp;
1246         short ioaddr;
1247         unsigned short status, ack_cmd = 0;
1248         int handled = 0;
1249
1250 #ifdef ENABLE_BVME6000_NET
1251         if (MACH_IS_BVME6000) {
1252                 if (*(char *) BVME_LOCAL_IRQ_STAT & BVME_ETHERR) {
1253                         i596_error(irq, dev_id);
1254                         return IRQ_HANDLED;
1255                 }
1256         }
1257 #endif
1258         if (dev == NULL) {
1259                 printk(KERN_ERR "i596_interrupt(): irq %d for unknown device.\n", irq);
1260                 return IRQ_NONE;
1261         }
1262
1263         ioaddr = dev->base_addr;
1264         lp = dev->ml_priv;
1265
1266         spin_lock (&lp->lock);
1267
1268         wait_cmd(dev,lp,100,"i596 interrupt, timeout");
1269         status = lp->scb.status;
1270
1271         DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt, IRQ %d, status %4.4x.\n",
1272                         dev->name, irq, status));
1273
1274         ack_cmd = status & 0xf000;
1275
1276         if ((status & 0x8000) || (status & 0x2000)) {
1277                 struct i596_cmd *ptr;
1278
1279                 handled = 1;
1280                 if ((status & 0x8000))
1281                         DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt completed command.\n", dev->name));
1282                 if ((status & 0x2000))
1283                         DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700));
1284
1285                 while ((lp->cmd_head != I596_NULL) && (lp->cmd_head->status & STAT_C)) {
1286                         ptr = lp->cmd_head;
1287
1288                         DEB(DEB_STATUS,printk(KERN_DEBUG "cmd_head->status = %04x, ->command = %04x\n",
1289                                        lp->cmd_head->status, lp->cmd_head->command));
1290                         lp->cmd_head = ptr->v_next;
1291                         lp->cmd_backlog--;
1292
1293                         switch ((ptr->command) & 0x7) {
1294                         case CmdTx:
1295                             {
1296                                 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
1297                                 struct sk_buff *skb = tx_cmd->skb;
1298
1299                                 if ((ptr->status) & STAT_OK) {
1300                                         DEB(DEB_TXADDR,print_eth(skb->data, "tx-done"));
1301                                 } else {
1302                                         dev->stats.tx_errors++;
1303                                         if ((ptr->status) & 0x0020)
1304                                                 dev->stats.collisions++;
1305                                         if (!((ptr->status) & 0x0040))
1306                                                 dev->stats.tx_heartbeat_errors++;
1307                                         if ((ptr->status) & 0x0400)
1308                                                 dev->stats.tx_carrier_errors++;
1309                                         if ((ptr->status) & 0x0800)
1310                                                 dev->stats.collisions++;
1311                                         if ((ptr->status) & 0x1000)
1312                                                 dev->stats.tx_aborted_errors++;
1313                                 }
1314
1315                                 dev_kfree_skb_irq(skb);
1316
1317                                 tx_cmd->cmd.command = 0; /* Mark free */
1318                                 break;
1319                             }
1320                         case CmdTDR:
1321                             {
1322                                 unsigned short status = ((struct tdr_cmd *)ptr)->status;
1323
1324                                 if (status & 0x8000) {
1325                                         DEB(DEB_TDR,printk(KERN_INFO "%s: link ok.\n", dev->name));
1326                                 } else {
1327                                         if (status & 0x4000)
1328                                                 printk(KERN_ERR "%s: Transceiver problem.\n", dev->name);
1329                                         if (status & 0x2000)
1330                                                 printk(KERN_ERR "%s: Termination problem.\n", dev->name);
1331                                         if (status & 0x1000)
1332                                                 printk(KERN_ERR "%s: Short circuit.\n", dev->name);
1333
1334                                         DEB(DEB_TDR,printk(KERN_INFO "%s: Time %d.\n", dev->name, status & 0x07ff));
1335                                 }
1336                                 break;
1337                             }
1338                         case CmdConfigure:
1339                         case CmdMulticastList:
1340                                 /* Zap command so set_multicast_list() knows it is free */
1341                                 ptr->command = 0;
1342                                 break;
1343                         }
1344                         ptr->v_next = ptr->b_next = I596_NULL;
1345                         lp->last_cmd = jiffies;
1346                 }
1347
1348                 ptr = lp->cmd_head;
1349                 while ((ptr != I596_NULL) && (ptr != lp->cmd_tail)) {
1350                         ptr->command &= 0x1fff;
1351                         ptr = ptr->v_next;
1352                 }
1353
1354                 if ((lp->cmd_head != I596_NULL))
1355                         ack_cmd |= CUC_START;
1356                 lp->scb.cmd = WSWAPcmd(virt_to_bus(&lp->cmd_head->status));
1357         }
1358         if ((status & 0x1000) || (status & 0x4000)) {
1359                 if ((status & 0x4000))
1360                         DEB(DEB_INTS,printk(KERN_DEBUG "%s: i596 interrupt received a frame.\n", dev->name));
1361                 i596_rx(dev);
1362                 /* Only RX_START if stopped - RGH 07-07-96 */
1363                 if (status & 0x1000) {
1364                         if (netif_running(dev)) {
1365                                 DEB(DEB_ERRORS,printk(KERN_ERR "%s: i596 interrupt receive unit inactive, status 0x%x\n", dev->name, status));
1366                                 ack_cmd |= RX_START;
1367                                 dev->stats.rx_errors++;
1368                                 dev->stats.rx_fifo_errors++;
1369                                 rebuild_rx_bufs(dev);
1370                         }
1371                 }
1372         }
1373         wait_cmd(dev,lp,100,"i596 interrupt, timeout");
1374         lp->scb.command = ack_cmd;
1375
1376 #ifdef ENABLE_MVME16x_NET
1377         if (MACH_IS_MVME16x) {
1378                 /* Ack the interrupt */
1379
1380                 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1381
1382                 pcc2[0x2a] |= 0x08;
1383         }
1384 #endif
1385 #ifdef ENABLE_BVME6000_NET
1386         if (MACH_IS_BVME6000) {
1387                 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1388
1389                 *ethirq = 1;
1390                 *ethirq = 3;
1391         }
1392 #endif
1393         CA(dev);
1394
1395         DEB(DEB_INTS,printk(KERN_DEBUG "%s: exiting interrupt.\n", dev->name));
1396
1397         spin_unlock (&lp->lock);
1398         return IRQ_RETVAL(handled);
1399 }
1400
1401 static int i596_close(struct net_device *dev)
1402 {
1403         struct i596_private *lp = dev->ml_priv;
1404         unsigned long flags;
1405
1406         netif_stop_queue(dev);
1407
1408         DEB(DEB_INIT,printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x.\n",
1409                        dev->name, lp->scb.status));
1410
1411         spin_lock_irqsave(&lp->lock, flags);
1412
1413         wait_cmd(dev,lp,100,"close1 timed out");
1414         lp->scb.command = CUC_ABORT | RX_ABORT;
1415         CA(dev);
1416
1417         wait_cmd(dev,lp,100,"close2 timed out");
1418
1419         spin_unlock_irqrestore(&lp->lock, flags);
1420         DEB(DEB_STRUCT,i596_display_data(dev));
1421         i596_cleanup_cmd(dev,lp);
1422
1423 #ifdef ENABLE_MVME16x_NET
1424         if (MACH_IS_MVME16x) {
1425                 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1426
1427                 /* Disable all ints */
1428                 pcc2[0x28] = 1;
1429                 pcc2[0x2a] = 0x40;
1430                 pcc2[0x2b] = 0x40;      /* Set snooping bits now! */
1431         }
1432 #endif
1433 #ifdef ENABLE_BVME6000_NET
1434         if (MACH_IS_BVME6000) {
1435                 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1436
1437                 *ethirq = 1;
1438         }
1439 #endif
1440
1441 #ifdef ENABLE_MVME16x_NET
1442         free_irq(0x56, dev);
1443 #endif
1444         free_irq(dev->irq, dev);
1445         remove_rx_bufs(dev);
1446
1447         return 0;
1448 }
1449
1450 /*
1451  *    Set or clear the multicast filter for this adaptor.
1452  */
1453
1454 static void set_multicast_list(struct net_device *dev)
1455 {
1456         struct i596_private *lp = dev->ml_priv;
1457         int config = 0, cnt;
1458
1459         DEB(DEB_MULTI,printk(KERN_DEBUG "%s: set multicast list, %d entries, promisc %s, allmulti %s\n",
1460                 dev->name, netdev_mc_count(dev),
1461                 dev->flags & IFF_PROMISC  ? "ON" : "OFF",
1462                 dev->flags & IFF_ALLMULTI ? "ON" : "OFF"));
1463
1464         if (wait_cfg(dev, &lp->cf_cmd.cmd, 1000, "config change request timed out"))
1465                 return;
1466
1467         if ((dev->flags & IFF_PROMISC) && !(lp->cf_cmd.i596_config[8] & 0x01)) {
1468                 lp->cf_cmd.i596_config[8] |= 0x01;
1469                 config = 1;
1470         }
1471         if (!(dev->flags & IFF_PROMISC) && (lp->cf_cmd.i596_config[8] & 0x01)) {
1472                 lp->cf_cmd.i596_config[8] &= ~0x01;
1473                 config = 1;
1474         }
1475         if ((dev->flags & IFF_ALLMULTI) && (lp->cf_cmd.i596_config[11] & 0x20)) {
1476                 lp->cf_cmd.i596_config[11] &= ~0x20;
1477                 config = 1;
1478         }
1479         if (!(dev->flags & IFF_ALLMULTI) && !(lp->cf_cmd.i596_config[11] & 0x20)) {
1480                 lp->cf_cmd.i596_config[11] |= 0x20;
1481                 config = 1;
1482         }
1483         if (config) {
1484                 lp->cf_cmd.cmd.command = CmdConfigure;
1485                 i596_add_cmd(dev, &lp->cf_cmd.cmd);
1486         }
1487
1488         cnt = netdev_mc_count(dev);
1489         if (cnt > MAX_MC_CNT)
1490         {
1491                 cnt = MAX_MC_CNT;
1492                 printk(KERN_ERR "%s: Only %d multicast addresses supported",
1493                         dev->name, cnt);
1494         }
1495
1496         if (!netdev_mc_empty(dev)) {
1497                 struct netdev_hw_addr *ha;
1498                 unsigned char *cp;
1499                 struct mc_cmd *cmd;
1500
1501                 if (wait_cfg(dev, &lp->mc_cmd.cmd, 1000, "multicast list change request timed out"))
1502                         return;
1503                 cmd = &lp->mc_cmd;
1504                 cmd->cmd.command = CmdMulticastList;
1505                 cmd->mc_cnt = cnt * ETH_ALEN;
1506                 cp = cmd->mc_addrs;
1507                 netdev_for_each_mc_addr(ha, dev) {
1508                         if (!cnt--)
1509                                 break;
1510                         memcpy(cp, ha->addr, ETH_ALEN);
1511                         if (i596_debug > 1)
1512                                 DEB(DEB_MULTI,printk(KERN_INFO "%s: Adding address %pM\n",
1513                                                 dev->name, cp));
1514                         cp += ETH_ALEN;
1515                 }
1516                 i596_add_cmd(dev, &cmd->cmd);
1517         }
1518 }
1519
1520 #ifdef MODULE
1521 static struct net_device *dev_82596;
1522
1523 static int debug = -1;
1524 module_param(debug, int, 0);
1525 MODULE_PARM_DESC(debug, "i82596 debug mask");
1526
1527 int __init init_module(void)
1528 {
1529         if (debug >= 0)
1530                 i596_debug = debug;
1531         dev_82596 = i82596_probe(-1);
1532         if (IS_ERR(dev_82596))
1533                 return PTR_ERR(dev_82596);
1534         return 0;
1535 }
1536
1537 void __exit cleanup_module(void)
1538 {
1539         unregister_netdev(dev_82596);
1540 #ifdef __mc68000__
1541         /* XXX This assumes default cache mode to be IOMAP_FULL_CACHING,
1542          * XXX which may be invalid (CONFIG_060_WRITETHROUGH)
1543          */
1544
1545         kernel_set_cachemode((void *)(dev_82596->mem_start), 4096,
1546                         IOMAP_FULL_CACHING);
1547 #endif
1548         free_page ((u32)(dev_82596->mem_start));
1549         free_netdev(dev_82596);
1550 }
1551
1552 #endif                          /* MODULE */