Import 2.3.10pre5
[davej-history.git] / drivers / net / 82596.c
blob1dcbd9f40ba6a7924abd79266da9412faa42ad1d
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
7 Modularised 12/94 Mark Evans
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
14 *** Untested on Apricot hardware, and may require some hacking
15 *** to make it work. The old 82596.c reported hasn't worked
16 *** since 1.3.xx anyway. I have been unable to find any users
17 *** of Apricot hardware to test this on.
19 Most of my modifications relate to the braindead big-endian
20 implementation by Intel. When the i596 is operating in
21 'big-endian' mode, it thinks a 32 bit value of 0x12345678
22 should be stored as 0x56781234. This is a real pain, when
23 you have linked lists which are shared by the 680x0 and the
24 i596.
26 Driver skeleton
27 Written 1993 by Donald Becker.
28 Copyright 1993 United States Government as represented by the Director,
29 National Security Agency. This software may only be used and distributed
30 according to the terms of the GNU Public License as modified by SRC,
31 incorporated herein by reference.
33 The author may be reached as becker@super.org or
34 C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
38 static const char *version = "82596.c:v1.0 15/07/98\n";
40 #include <linux/config.h>
41 #include <linux/module.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/string.h>
46 #include <linux/ptrace.h>
47 #include <linux/errno.h>
48 #include <linux/ioport.h>
49 #include <linux/malloc.h>
50 #include <linux/interrupt.h>
51 #include <linux/delay.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/skbuff.h>
55 #include <linux/init.h>
57 #include <asm/bitops.h>
58 #include <asm/io.h>
59 #include <asm/dma.h>
60 #include <asm/pgtable.h> /*?? */
62 #ifdef CONFIG_MVME16x_NET
63 #include <asm/mvme16xhw.h>
64 #endif
65 #ifdef CONFIG_BVME6000_NET
66 #include <asm/bvme6000hw.h>
67 #endif
70 * Define various macros for Channel Attention, word swapping etc., dependent
71 * on architecture. MVME and BVME are 680x0 based, otherwise it is Intel.
74 #ifdef __mc68000__
75 #define WSWAPrfd(x) ((struct i596_rfd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
76 #define WSWAPrbd(x) ((struct i596_rbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
77 #define WSWAPiscp(x) ((struct i596_iscp *)(((u32)(x)<<16) | ((((u32)(x)))>>16)))
78 #define WSWAPscb(x) ((struct i596_scb *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
79 #define WSWAPcmd(x) ((struct i596_cmd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
80 #define WSWAPtbd(x) ((struct i596_tbd *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
81 #define WSWAPchar(x) ((char *) (((u32)(x)<<16) | ((((u32)(x)))>>16)))
82 #define ISCP_BUSY 0x00010000
83 #define MACH_IS_APRICOT 0
84 #else
85 #define WSWAPrfd(x) x
86 #define WSWAPiscp(x) ((struct i596_iscp *)(x))
87 #define WSWAPscb(x) ((struct i596_scb *)(x))
88 #define WSWAPcmd(x) x
89 #define WSWAPtbd(x) x
90 #define WSWAPchar(x) x
91 #define ISCP_BUSY 0x0001
92 #define MACH_IS_APRICOT 1
93 #endif
96 * The MPU_PORT command allows direct access to the 82596. With PORT access
97 * the following commands are available (p5-18). The 32-bit port command
98 * must be word-swapped with the most significant word written first.
99 * This only applies to VME boards.
101 #define PORT_RESET 0x00 /* reset 82596 */
102 #define PORT_SELFTEST 0x01 /* selftest */
103 #define PORT_ALTSCP 0x02 /* alternate SCB address */
104 #define PORT_ALTDUMP 0x03 /* Alternate DUMP address */
106 #ifndef HAVE_PORTRESERVE
107 #define check_region(addr, size) 0
108 #define request_region(addr, size,name) do ; while(0)
109 #endif
111 #ifndef HAVE_ALLOC_SKB
112 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
113 #define kfree_skbmem(buff, size) kfree_s(buff,size)
114 #endif
116 #define APRICOT_DEBUG 2
118 #ifdef APRICOT_DEBUG
119 int i596_debug = APRICOT_DEBUG;
120 #else
121 int i596_debug = 1;
122 #endif
124 #define I596_TOTAL_SIZE 17
126 #define I596_NULL -1
128 #define CMD_EOL 0x8000 /* The last command of the list, stop. */
129 #define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
130 #define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
132 #define CMD_FLEX 0x0008 /* Enable flexible memory model */
134 enum commands {
135 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
136 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
139 #define STAT_C 0x8000 /* Set to 0 after execution */
140 #define STAT_B 0x4000 /* Command being executed */
141 #define STAT_OK 0x2000 /* Command executed ok */
142 #define STAT_A 0x1000 /* Command aborted */
144 #define CUC_START 0x0100
145 #define CUC_RESUME 0x0200
146 #define CUC_SUSPEND 0x0300
147 #define CUC_ABORT 0x0400
148 #define RX_START 0x0010
149 #define RX_RESUME 0x0020
150 #define RX_SUSPEND 0x0030
151 #define RX_ABORT 0x0040
153 struct i596_reg {
154 unsigned short porthi;
155 unsigned short portlo;
156 unsigned long ca;
159 struct i596_cmd {
160 unsigned short status;
161 unsigned short command;
162 struct i596_cmd *next;
165 #define EOF 0x8000
166 #define SIZE_MASK 0x3fff
168 struct i596_tbd {
169 unsigned short size;
170 unsigned short pad;
171 struct i596_tbd *next;
172 char *data;
175 struct tx_cmd {
176 struct i596_cmd cmd;
177 struct i596_tbd *tbd;
178 unsigned short size;
179 unsigned short pad;
180 struct sk_buff *skb; /* So we can free it after tx */
183 struct i596_rfd {
184 unsigned short stat;
185 unsigned short cmd;
186 struct i596_rfd *next;
187 long rbd;
188 unsigned short count;
189 unsigned short size;
190 char data[1532];
193 #define RX_RING_SIZE 16
195 struct i596_scb {
196 unsigned short status;
197 unsigned short command;
198 struct i596_cmd *cmd;
199 struct i596_rfd *rfd;
200 unsigned long crc_err;
201 unsigned long align_err;
202 unsigned long resource_err;
203 unsigned long over_err;
204 unsigned long rcvdt_err;
205 unsigned long short_err;
206 unsigned short t_on;
207 unsigned short t_off;
210 struct i596_iscp {
211 unsigned long stat;
212 struct i596_scb *scb;
215 struct i596_scp {
216 unsigned long sysbus;
217 unsigned long pad;
218 struct i596_iscp *iscp;
221 struct i596_private {
222 volatile struct i596_scp scp;
223 volatile struct i596_iscp iscp;
224 volatile struct i596_scb scb;
225 struct i596_cmd set_add;
226 char eth_addr[8];
227 struct i596_cmd set_conf;
228 char i596_config[16];
229 struct i596_cmd tdr;
230 unsigned long stat;
231 int last_restart __attribute__((aligned(4)));
232 struct i596_rfd *rx_tail;
233 struct i596_cmd *cmd_tail;
234 struct i596_cmd *cmd_head;
235 int cmd_backlog;
236 unsigned long last_cmd;
237 struct net_device_stats stats;
240 char init_setup[] =
242 0x8E, /* length, prefetch on */
243 0xC8, /* fifo to 8, monitor off */
244 #ifdef CONFIG_VME
245 0xc0, /* don't save bad frames */
246 #else
247 0x80, /* don't save bad frames */
248 #endif
249 0x2E, /* No source address insertion, 8 byte preamble */
250 0x00, /* priority and backoff defaults */
251 0x60, /* interframe spacing */
252 0x00, /* slot time LSB */
253 0xf2, /* slot time and retries */
254 0x00, /* promiscuous mode */
255 0x00, /* collision detect */
256 0x40, /* minimum frame length */
257 0xff,
258 0x00,
259 0x7f /* *multi IA */ };
261 static int i596_open(struct device *dev);
262 static int i596_start_xmit(struct sk_buff *skb, struct device *dev);
263 static void i596_interrupt(int irq, void *dev_id, struct pt_regs *regs);
264 static int i596_close(struct device *dev);
265 static struct net_device_stats *i596_get_stats(struct device *dev);
266 static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd);
267 static void print_eth(char *);
268 static void set_multicast_list(struct device *dev);
270 static int ticks_limit = 25;
271 static int max_cmd_backlog = 16;
274 static inline void CA(struct device *dev)
276 #ifdef CONFIG_MVME16x_NET
277 if (MACH_IS_MVME16x) {
278 ((struct i596_reg *) dev->base_addr)->ca = 1;
280 #endif
281 #ifdef CONFIG_BVME6000_NET
282 if (MACH_IS_BVME6000) {
283 volatile u32 i = *(volatile u32 *) (dev->base_addr);
285 #endif
286 #ifdef CONFIG_APRICOT_i596
287 if (MACH_IS_APRICOT) {
288 outw(0, (short) (dev->base_addr) + 4);
290 #endif
294 static inline void MPU_PORT(struct device *dev, int c, volatile void *x)
296 #ifdef CONFIG_MVME16x_NET
297 if (MACH_IS_MVME16x) {
298 struct i596_reg *p = (struct i596_reg *) (dev->base_addr);
299 p->porthi = ((c) | (u32) (x)) & 0xffff;
300 p->portlo = ((c) | (u32) (x)) >> 16;
302 #endif
303 #ifdef CONFIG_BVME6000_NET
304 if (MACH_IS_BVME6000) {
305 u32 v = (u32) (c) | (u32) (x);
306 v = ((u32) (v) << 16) | ((u32) (v) >> 16);
307 *(volatile u32 *) dev->base_addr = v;
308 udelay(1);
309 *(volatile u32 *) dev->base_addr = v;
311 #endif
315 #if defined(CONFIG_MVME16x_NET) || defined(CONFIG_BVME6000_NET)
316 static void i596_error(int irq, void *dev_id, struct pt_regs *regs)
318 struct device *dev = dev_id;
319 struct i596_cmd *cmd;
321 struct i596_private *lp = (struct i596_private *) dev->priv;
322 printk("i596_error: lp = 0x%08x\n", (u32) lp);
323 printk("scp at %08x, .sysbus = %08x, .iscp = %08x\n",
324 (u32) & lp->scp, (u32) lp->scp.sysbus, (u32) lp->scp.iscp);
325 printk("iscp at %08x, .stat = %08x, .scb = %08x\n",
326 (u32) & lp->iscp, (u32) lp->iscp.stat, (u32) lp->iscp.scb);
327 printk("scb at %08x, .status = %04x, .command = %04x\n",
328 (u32) & lp->scb, lp->scb.status, lp->scb.command);
329 printk(" .cmd = %08x, .rfd = %08x\n", (u32) lp->scb.cmd,
330 (u32) lp->scb.rfd);
331 cmd = WSWAPcmd(lp->scb.cmd);
332 while (cmd && (u32) cmd < 0x1000000) {
333 printk("cmd at %08x, .status = %04x, .command = %04x, .next = %08x\n",
334 (u32) cmd, cmd->status, cmd->command, (u32) cmd->next);
335 cmd = WSWAPcmd(cmd->next);
337 while (1);
339 #endif
341 static inline int init_rx_bufs(struct device *dev, int num)
343 struct i596_private *lp = (struct i596_private *) dev->priv;
344 int i;
345 struct i596_rfd *rfd;
347 lp->scb.rfd = (struct i596_rfd *) I596_NULL;
349 if (i596_debug > 1)
350 printk("%s: init_rx_bufs %d.\n", dev->name, num);
352 for (i = 0; i < num; i++) {
353 if (!(rfd = (struct i596_rfd *) kmalloc(sizeof(struct i596_rfd), GFP_KERNEL)))
354 break;
356 rfd->stat = 0x0000;
357 rfd->rbd = I596_NULL;
358 rfd->count = 0;
359 rfd->size = 1532;
360 if (i == 0) {
361 rfd->cmd = CMD_EOL;
362 lp->rx_tail = rfd;
363 } else
364 rfd->cmd = 0x0000;
366 rfd->next = lp->scb.rfd;
367 lp->scb.rfd = WSWAPrfd(rfd);
370 if (i != 0)
371 lp->rx_tail->next = lp->scb.rfd;
373 return (i);
376 static inline void remove_rx_bufs(struct device *dev)
378 struct i596_private *lp = (struct i596_private *) dev->priv;
379 struct i596_rfd *rfd = WSWAPrfd(lp->scb.rfd);
381 lp->rx_tail->next = (struct i596_rfd *) I596_NULL;
383 do {
384 lp->scb.rfd = rfd->next;
385 kfree(rfd);
386 rfd = WSWAPrfd(lp->scb.rfd);
388 while (rfd != lp->rx_tail);
391 static inline void init_i596_mem(struct device *dev)
393 struct i596_private *lp = (struct i596_private *) dev->priv;
394 #if !defined(CONFIG_MVME16x_NET) && !defined(CONFIG_BVME6000_NET)
395 short ioaddr = dev->base_addr;
396 #endif
397 int boguscnt = 100000;
398 unsigned long flags;
400 #if defined(CONFIG_MVME16x_NET) || defined(CONFIG_BVME6000_NET)
401 #ifdef CONFIG_MVME16x_NET
402 if (MACH_IS_MVME16x) {
403 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
405 /* Disable all ints for now */
406 pcc2[0x28] = 1;
407 pcc2[0x2a] = 0x40;
408 pcc2[0x2b] = 0x40; /* Set snooping bits now! */
410 #endif
411 #ifdef CONFIG_BVME6000_NET
412 if (MACH_IS_BVME6000) {
413 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
415 *ethirq = 1;
417 #endif
419 MPU_PORT(dev, PORT_RESET, 0);
421 udelay(100); /* Wait 100us - seems to help */
423 /* change the scp address */
425 MPU_PORT(dev, PORT_ALTSCP, &lp->scp);
427 #else
429 /* change the scp address */
430 outw(0, ioaddr);
431 outw(0, ioaddr);
432 outb(4, ioaddr + 0xf);
433 outw(((((int) &lp->scp) & 0xffff) | 2), ioaddr);
434 outw((((int) &lp->scp) >> 16) & 0xffff, ioaddr);
435 #endif
437 lp->last_cmd = jiffies;
439 #ifdef CONFIG_MVME16x_NET
440 if (MACH_IS_MVME16x)
441 lp->scp.sysbus = 0x00000054;
442 #endif
443 #ifdef CONFIG_BVME6000_NET
444 if (MACH_IS_BVME6000)
445 lp->scp.sysbus = 0x0000004c;
446 #endif
447 #ifdef CONFIG_APRICOT_i596
448 if (MACH_IS_APRICOT)
449 lp->scp.sysbus = 0x00440000;
450 #endif
452 lp->scp.iscp = WSWAPiscp(&(lp->iscp));
453 lp->iscp.scb = WSWAPscb(&(lp->scb));
454 lp->iscp.stat = ISCP_BUSY;
455 lp->cmd_backlog = 0;
457 lp->cmd_head = lp->scb.cmd = (struct i596_cmd *) I596_NULL;
459 if (i596_debug > 1)
460 printk("%s: starting i82596.\n", dev->name);
462 #if !defined(CONFIG_MVME16x_NET) && !defined(CONFIG_BVME6000_NET)
463 (void) inb(ioaddr + 0x10);
464 outb(4, ioaddr + 0xf);
465 #endif
466 CA(dev);
468 while (lp->iscp.stat)
469 if (--boguscnt == 0) {
470 printk("%s: i82596 initialization timed out with status %4.4x, cmd %4.4x.\n",
471 dev->name, lp->scb.status, lp->scb.command);
472 break;
474 lp->scb.command = 0;
476 #ifdef CONFIG_MVME16x_NET
477 if (MACH_IS_MVME16x) {
478 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
480 /* Enable ints, etc. now */
481 pcc2[0x2a] = 0x08;
482 pcc2[0x2a] = 0x55; /* Edge sensitive */
483 pcc2[0x2b] = 0x55;
485 #endif
486 #ifdef CONFIG_BVME6000_NET
487 if (MACH_IS_BVME6000) {
488 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
490 *ethirq = 3;
492 #endif
494 memcpy(lp->i596_config, init_setup, 14);
495 lp->set_conf.command = CmdConfigure;
496 i596_add_cmd(dev, &lp->set_conf);
498 memcpy(lp->eth_addr, dev->dev_addr, 6);
499 lp->set_add.command = CmdSASetup;
500 i596_add_cmd(dev, &lp->set_add);
502 lp->tdr.command = CmdTDR;
503 i596_add_cmd(dev, &lp->tdr);
505 boguscnt = 200000;
507 save_flags(flags);
508 cli();
510 while (lp->scb.command)
511 if (--boguscnt == 0) {
512 printk("%s: receive unit start timed out with status %4.4x, cmd %4.4x.\n",
513 dev->name, lp->scb.status, lp->scb.command);
514 break;
516 lp->scb.command = RX_START;
517 CA(dev);
519 restore_flags(flags);
521 boguscnt = 2000;
522 while (lp->scb.command)
523 if (--boguscnt == 0) {
524 printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n",
525 lp->scb.status, lp->scb.command);
526 break;
528 return;
531 static inline int i596_rx(struct device *dev)
533 struct i596_private *lp = (struct i596_private *) dev->priv;
534 struct i596_rfd *rfd;
535 int frames = 0;
537 if (i596_debug > 3)
538 printk("i596_rx()\n");
540 rfd = WSWAPrfd(lp->scb.rfd); /* Reference next frame descriptor to check */
542 while ((rfd->stat) & STAT_C) { /* Loop while we have complete frames */
543 if (i596_debug > 2)
544 print_eth(rfd->data);
546 if ((rfd->stat) & STAT_OK) {
547 /* a good frame */
548 int pkt_len = rfd->count & 0x3fff;
549 struct sk_buff *skb = dev_alloc_skb(pkt_len);
551 frames++;
553 if (skb == NULL) {
554 printk("%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
555 lp->stats.rx_dropped++;
556 } else {
557 skb->dev = dev;
558 memcpy(skb_put(skb, pkt_len), rfd->data, pkt_len);
559 skb->protocol = eth_type_trans(skb, dev);
560 netif_rx(skb);
561 lp->stats.rx_packets++;
562 lp->stats.rx_bytes += pkt_len;
564 } else {
565 lp->stats.rx_errors++;
566 if ((rfd->stat) & 0x0001)
567 lp->stats.collisions++;
568 if ((rfd->stat) & 0x0080)
569 lp->stats.rx_length_errors++;
570 if ((rfd->stat) & 0x0100)
571 lp->stats.rx_over_errors++;
572 if ((rfd->stat) & 0x0200)
573 lp->stats.rx_fifo_errors++;
574 if ((rfd->stat) & 0x0400)
575 lp->stats.rx_frame_errors++;
576 if ((rfd->stat) & 0x0800)
577 lp->stats.rx_crc_errors++;
578 if ((rfd->stat) & 0x1000)
579 lp->stats.rx_length_errors++;
582 /* Clear the buffer descriptor count and EOF + F flags */
584 rfd->stat = 0;
585 rfd->count = 0;
586 rfd->cmd = CMD_EOL;
587 lp->rx_tail->cmd = 0;
588 lp->rx_tail = rfd;
589 lp->scb.rfd = rfd->next;
590 rfd = WSWAPrfd(lp->scb.rfd); /* Next frame descriptor to check */
593 if (i596_debug > 3)
594 printk("frames %d\n", frames);
596 return 0;
599 static inline void i596_cleanup_cmd(struct i596_private *lp)
601 struct i596_cmd *ptr;
602 int boguscnt = 1000;
604 if (i596_debug > 4)
605 printk("i596_cleanup_cmd\n");
607 while (lp->cmd_head != (struct i596_cmd *) I596_NULL) {
608 ptr = lp->cmd_head;
610 lp->cmd_head = WSWAPcmd(lp->cmd_head->next);
611 lp->cmd_backlog--;
613 switch ((ptr->command) & 0x7) {
614 case CmdTx:
616 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
617 struct sk_buff *skb = tx_cmd->skb;
619 dev_kfree_skb(skb);
621 lp->stats.tx_errors++;
622 lp->stats.tx_aborted_errors++;
624 ptr->next = (struct i596_cmd *) I596_NULL;
625 kfree(tx_cmd);
626 break;
628 case CmdMulticastList:
630 ptr->next = (struct i596_cmd *) I596_NULL;
631 kfree(ptr);
632 break;
634 default:
635 ptr->next = (struct i596_cmd *) I596_NULL;
639 while (lp->scb.command)
640 if (--boguscnt == 0) {
641 printk("i596_cleanup_cmd timed out with status %4.4x, cmd %4.4x.\n",
642 lp->scb.status, lp->scb.command);
643 break;
645 lp->scb.cmd = WSWAPcmd(lp->cmd_head);
648 static inline void i596_reset(struct device *dev, struct i596_private *lp, int ioaddr)
650 int boguscnt = 1000;
651 unsigned long flags;
653 if (i596_debug > 1)
654 printk("i596_reset\n");
656 save_flags(flags);
657 cli();
659 while (lp->scb.command)
660 if (--boguscnt == 0) {
661 printk("i596_reset timed out with status %4.4x, cmd %4.4x.\n",
662 lp->scb.status, lp->scb.command);
663 break;
665 dev->start = 0;
666 dev->tbusy = 1;
668 lp->scb.command = CUC_ABORT | RX_ABORT;
669 CA(dev);
671 /* wait for shutdown */
672 boguscnt = 4000;
674 while (lp->scb.command)
675 if (--boguscnt == 0) {
676 printk("i596_reset 2 timed out with status %4.4x, cmd %4.4x.\n",
677 lp->scb.status, lp->scb.command);
678 break;
680 restore_flags(flags);
682 i596_cleanup_cmd(lp);
683 i596_rx(dev);
685 dev->start = 1;
686 dev->tbusy = 0;
687 dev->interrupt = 0;
688 init_i596_mem(dev);
691 static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd)
693 struct i596_private *lp = (struct i596_private *) dev->priv;
694 int ioaddr = dev->base_addr;
695 unsigned long flags;
696 int boguscnt = 1000;
698 if (i596_debug > 4)
699 printk("i596_add_cmd\n");
701 cmd->status = 0;
702 cmd->command |= (CMD_EOL | CMD_INTR);
703 cmd->next = (struct i596_cmd *) I596_NULL;
704 save_flags(flags);
705 cli();
708 * RGH 300597: Looks to me like there could be a race condition
709 * here. Just because we havn't picked up all the command items
710 * yet, doesn't mean that the 82596 hasn't finished processing
711 * them. So, we may need to do a CUC_START anyway.
712 * Maybe not. If it interrupts saying the CU is idle when there
713 * is still something in the cmd queue, the int handler with restart
714 * the CU.
717 if (lp->cmd_head != (struct i596_cmd *) I596_NULL) {
718 lp->cmd_tail->next = WSWAPcmd(cmd);
719 } else {
720 lp->cmd_head = cmd;
721 while (lp->scb.command)
722 if (--boguscnt == 0) {
723 printk("i596_add_cmd timed out with status %4.4x, cmd %4.4x.\n",
724 lp->scb.status, lp->scb.command);
725 break;
727 lp->scb.cmd = WSWAPcmd(cmd);
728 lp->scb.command = CUC_START;
729 CA(dev);
731 lp->cmd_tail = cmd;
732 lp->cmd_backlog++;
734 lp->cmd_head = WSWAPcmd(lp->scb.cmd); /* Is this redundant? RGH 300597 */
735 restore_flags(flags);
737 if (lp->cmd_backlog > max_cmd_backlog) {
738 unsigned long tickssofar = jiffies - lp->last_cmd;
740 if (tickssofar < ticks_limit)
741 return;
743 printk("%s: command unit timed out, status resetting.\n", dev->name);
745 i596_reset(dev, lp, ioaddr);
749 static int i596_open(struct device *dev)
751 int i;
753 if (i596_debug > 1)
754 printk("%s: i596_open() irq %d.\n", dev->name, dev->irq);
756 if (request_irq(dev->irq, &i596_interrupt, 0, "apricot", dev))
757 return -EAGAIN;
758 #ifdef CONFIG_MVME16x_NET
759 if (MACH_IS_MVME16x) {
760 if (request_irq(0x56, &i596_error, 0, "apricot_error", dev))
761 return -EAGAIN;
763 #endif
764 if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE)
765 printk("%s: only able to allocate %d receive buffers\n", dev->name, i);
767 if (i < 4) {
768 free_irq(dev->irq, dev);
769 return -EAGAIN;
771 dev->tbusy = 0;
772 dev->interrupt = 0;
773 dev->start = 1;
774 MOD_INC_USE_COUNT;
776 /* Initialize the 82596 memory */
777 init_i596_mem(dev);
779 return 0; /* Always succeed */
782 static int i596_start_xmit(struct sk_buff *skb, struct device *dev)
784 struct i596_private *lp = (struct i596_private *) dev->priv;
785 int ioaddr = dev->base_addr;
786 struct tx_cmd *tx_cmd;
788 if (i596_debug > 2)
789 printk("%s: 82596 start xmit\n", dev->name);
791 /* Transmitter timeout, serious problems. */
792 if (dev->tbusy) {
793 int tickssofar = jiffies - dev->trans_start;
794 if (tickssofar < 5)
795 return 1;
796 printk("%s: transmit timed out, status resetting.\n",
797 dev->name);
798 lp->stats.tx_errors++;
799 /* Try to restart the adaptor */
800 if (lp->last_restart == lp->stats.tx_packets) {
801 if (i596_debug > 1)
802 printk("Resetting board.\n");
804 /* Shutdown and restart */
805 i596_reset(dev, lp, ioaddr);
806 } else {
807 /* Issue a channel attention signal */
808 if (i596_debug > 1)
809 printk("Kicking board.\n");
810 lp->scb.command = CUC_START | RX_START;
811 CA(dev);
812 lp->last_restart = lp->stats.tx_packets;
814 dev->tbusy = 0;
815 dev->trans_start = jiffies;
817 if (i596_debug > 3)
818 printk("%s: i596_start_xmit() called\n", dev->name);
820 /* Block a timer-based transmit from overlapping. This could better be
821 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
822 if (test_and_set_bit(0, (void *) &dev->tbusy) != 0)
823 printk("%s: Transmitter access conflict.\n", dev->name);
824 else {
825 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
826 dev->trans_start = jiffies;
828 tx_cmd = (struct tx_cmd *) kmalloc((sizeof(struct tx_cmd) + sizeof(struct i596_tbd)), GFP_ATOMIC);
829 if (tx_cmd == NULL) {
830 printk("%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
831 lp->stats.tx_dropped++;
833 dev_kfree_skb(skb);
834 } else {
835 struct i596_tbd *tbd = (struct i596_tbd *) (tx_cmd + 1);
836 tx_cmd->tbd = WSWAPtbd(tbd);
837 tbd->next = (struct i596_tbd *) I596_NULL;
839 tx_cmd->cmd.command = CMD_FLEX | CmdTx;
840 tx_cmd->skb = skb;
842 tx_cmd->pad = 0;
843 tx_cmd->size = 0;
844 tbd->pad = 0;
845 tbd->size = EOF | length;
847 tbd->data = WSWAPchar(skb->data);
849 if (i596_debug > 3)
850 print_eth(skb->data);
851 i596_add_cmd(dev, (struct i596_cmd *) tx_cmd);
853 lp->stats.tx_packets++;
854 lp->stats.tx_bytes += length;
858 dev->tbusy = 0;
860 return 0;
864 static void print_eth(char *add)
866 int i;
868 printk("print_eth(%08x)\n", (unsigned int) add);
869 printk("Dest ");
870 for (i = 0; i < 6; i++)
871 printk(" %2.2X", (unsigned char) add[i]);
872 printk("\n");
874 printk("Source");
875 for (i = 0; i < 6; i++)
876 printk(" %2.2X", (unsigned char) add[i + 6]);
877 printk("\n");
878 printk("type %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]);
881 int __init i82596_probe(struct device *dev)
883 int i;
884 struct i596_private *lp;
885 char eth_addr[6];
887 #ifdef CONFIG_MVME16x_NET
888 if (MACH_IS_MVME16x) {
889 static int probed = 0;
891 if (mvme16x_config & MVME16x_CONFIG_NO_ETHERNET) {
892 printk("Ethernet probe disabled - chip not present\n");
893 return ENODEV;
895 if (probed)
896 return ENODEV;
897 probed++;
898 memcpy(eth_addr, (void *) 0xfffc1f2c, 6); /* YUCK! Get addr from NOVRAM */
899 dev->base_addr = MVME_I596_BASE;
900 dev->irq = (unsigned) MVME16x_IRQ_I596;
902 #endif
903 #ifdef CONFIG_BVME6000_NET
904 if (MACH_IS_BVME6000) {
905 volatile unsigned char *rtc = (unsigned char *) BVME_RTC_BASE;
906 unsigned char msr = rtc[3];
907 int i;
909 rtc[3] |= 0x80;
910 for (i = 0; i < 6; i++)
911 eth_addr[i] = rtc[i * 4 + 7]; /* Stored in RTC RAM at offset 1 */
912 rtc[3] = msr;
913 dev->base_addr = BVME_I596_BASE;
914 dev->irq = (unsigned) BVME_IRQ_I596;
916 #endif
917 #ifdef CONFIG_APRICOT_INTEL
918 int checksum = 0;
919 int ioaddr = 0x300;
921 /* this is easy the ethernet interface can only be at 0x300 */
922 /* first check nothing is already registered here */
924 if (check_region(ioaddr, I596_TOTAL_SIZE))
925 return ENODEV;
927 for (i = 0; i < 8; i++) {
928 eth_addr[i] = inb(ioaddr + 8 + i);
929 checksum += eth_addr[i];
932 /* checksum is a multiple of 0x100, got this wrong first time
933 some machines have 0x100, some 0x200. The DOS driver doesn't
934 even bother with the checksum */
936 if (checksum % 0x100)
937 return ENODEV;
939 /* Some other boards trip the checksum.. but then appear as ether
940 address 0. Trap these - AC */
942 if (memcmp(eth_addr, "\x00\x00\x49", 3) != 0)
943 return ENODEV;
945 request_region(ioaddr, I596_TOTAL_SIZE, "i596");
947 dev->base_addr = ioaddr;
948 dev->irq = 10;
949 #endif
950 ether_setup(dev);
951 printk("%s: 82596 at %#3lx,", dev->name, dev->base_addr);
953 for (i = 0; i < 6; i++)
954 printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);
956 printk(" IRQ %d.\n", dev->irq);
958 if (i596_debug > 0)
959 printk(version);
961 /* The APRICOT-specific entries in the device structure. */
962 dev->open = &i596_open;
963 dev->stop = &i596_close;
964 dev->hard_start_xmit = &i596_start_xmit;
965 dev->get_stats = &i596_get_stats;
966 dev->set_multicast_list = &set_multicast_list;
968 dev->mem_start = (int) kmalloc(sizeof(struct i596_private) + 0x0f, GFP_KERNEL);
969 /* align for scp */
970 dev->priv = (void *) ((dev->mem_start + 0xf) & 0xfffffff0);
972 lp = (struct i596_private *) dev->priv;
973 if (i596_debug)
974 printk("%s: lp at 0x%08lx, lp->scb at 0x%08lx\n"
975 ,dev->name, (unsigned long) lp, (unsigned long) &lp->scb);
976 memset((void *) lp, 0, sizeof(struct i596_private));
977 lp->scb.command = 0;
978 lp->scb.cmd = (struct i596_cmd *) I596_NULL;
979 lp->scb.rfd = (struct i596_rfd *) I596_NULL;
981 return 0;
984 static void i596_interrupt(int irq, void *dev_id, struct pt_regs *regs)
986 struct device *dev = dev_id;
987 struct i596_private *lp;
988 short ioaddr;
989 int boguscnt = 2000;
990 unsigned short status, ack_cmd = 0;
992 #ifdef CONFIG_BVME6000_NET
993 if (MACH_IS_BVME6000) {
994 if (*(char *) BVME_LOCAL_IRQ_STAT & BVME_ETHERR) {
995 i596_error(BVME_IRQ_I596, NULL, NULL);
996 return;
999 #endif
1000 if (dev == NULL) {
1001 printk("i596_interrupt(): irq %d for unknown device.\n", irq);
1002 return;
1004 if (i596_debug > 3)
1005 printk("%s: i596_interrupt(): irq %d\n", dev->name, irq);
1007 if (dev->interrupt)
1008 printk("%s: Re-entering the interrupt handler.\n", dev->name);
1010 dev->interrupt = 1;
1012 ioaddr = dev->base_addr;
1014 lp = (struct i596_private *) dev->priv;
1016 while (lp->scb.command)
1017 if (--boguscnt == 0) {
1018 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
1019 break;
1021 status = lp->scb.status;
1023 if (i596_debug > 4)
1024 printk("%s: i596 interrupt, status %4.4x.\n", dev->name, status);
1026 ack_cmd = status & 0xf000;
1028 if ((status & 0x8000) || (status & 0x2000)) {
1029 struct i596_cmd *ptr;
1031 if ((i596_debug > 4) && (status & 0x8000))
1032 printk("%s: i596 interrupt completed command.\n", dev->name);
1033 if ((i596_debug > 4) && (status & 0x2000))
1034 printk("%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700);
1036 while ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (lp->cmd_head->status & STAT_C)) {
1037 ptr = lp->cmd_head;
1039 if (i596_debug > 2)
1040 printk("cmd_head->status = %04x, ->command = %04x\n",
1041 lp->cmd_head->status, lp->cmd_head->command);
1042 lp->cmd_head = WSWAPcmd(lp->cmd_head->next);
1043 lp->cmd_backlog--;
1045 switch ((ptr->command) & 0x7) {
1046 case CmdTx:
1048 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
1049 struct sk_buff *skb = tx_cmd->skb;
1051 if ((ptr->status) & STAT_OK) {
1052 if (i596_debug > 2)
1053 print_eth(skb->data);
1054 } else {
1055 lp->stats.tx_errors++;
1056 if ((ptr->status) & 0x0020)
1057 lp->stats.collisions++;
1058 if (!((ptr->status) & 0x0040))
1059 lp->stats.tx_heartbeat_errors++;
1060 if ((ptr->status) & 0x0400)
1061 lp->stats.tx_carrier_errors++;
1062 if ((ptr->status) & 0x0800)
1063 lp->stats.collisions++;
1064 if ((ptr->status) & 0x1000)
1065 lp->stats.tx_aborted_errors++;
1068 dev_kfree_skb(skb);
1070 ptr->next = (struct i596_cmd *) I596_NULL;
1071 kfree(tx_cmd);
1072 break;
1074 case CmdMulticastList:
1076 ptr->next = (struct i596_cmd *) I596_NULL;
1077 kfree(ptr);
1078 break;
1080 case CmdTDR:
1082 unsigned long status = *((unsigned long *) (ptr + 1));
1084 if (status & 0x8000) {
1085 if (i596_debug > 3)
1086 printk("%s: link ok.\n", dev->name);
1087 } else {
1088 if (status & 0x4000)
1089 printk("%s: Transceiver problem.\n", dev->name);
1090 if (status & 0x2000)
1091 printk("%s: Termination problem.\n", dev->name);
1092 if (status & 0x1000)
1093 printk("%s: Short circuit.\n", dev->name);
1095 if (i596_debug > 1)
1096 printk("%s: Time %ld.\n", dev->name, status & 0x07ff);
1098 break;
1100 case CmdConfigure:
1102 ptr->next = (struct i596_cmd *) I596_NULL;
1103 /* Zap command so set_multicast_list() knows it is free */
1104 ptr->command = 0;
1105 break;
1107 default:
1108 ptr->next = (struct i596_cmd *) I596_NULL;
1110 lp->last_cmd = jiffies;
1113 ptr = lp->cmd_head;
1114 while ((ptr != (struct i596_cmd *) I596_NULL) && (ptr != lp->cmd_tail)) {
1115 ptr->command &= 0x1fff;
1116 ptr = WSWAPcmd(ptr->next);
1119 if ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (dev->start))
1120 ack_cmd |= CUC_START;
1121 lp->scb.cmd = WSWAPcmd(lp->cmd_head);
1123 if ((status & 0x1000) || (status & 0x4000)) {
1124 if ((i596_debug > 4) && (status & 0x4000))
1125 printk("%s: i596 interrupt received a frame.\n", dev->name);
1126 /* Only RX_START if stopped - RGH 07-07-96 */
1127 if (status & 0x1000) {
1128 if (dev->start)
1129 ack_cmd |= RX_START;
1130 if (i596_debug > 1)
1131 printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x00f0);
1133 i596_rx(dev);
1135 /* acknowledge the interrupt */
1137 /* COMMENTED OUT <<<<<
1138 if ((lp->scb.cmd != (struct i596_cmd *) I596_NULL) && (dev->start))
1139 ack_cmd |= CUC_START;
1141 boguscnt = 1000;
1142 while (lp->scb.command)
1143 if (--boguscnt == 0) {
1144 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
1145 break;
1147 lp->scb.command = ack_cmd;
1149 #ifdef CONFIG_MVME16x_NET
1150 if (MACH_IS_MVME16x) {
1151 /* Ack the interrupt */
1153 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1155 pcc2[0x2a] |= 0x08;
1157 #endif
1158 #ifdef CONFIG_BVME6000_NET
1159 if (MACH_IS_BVME6000) {
1160 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1162 *ethirq = 1;
1163 *ethirq = 3;
1165 #endif
1166 #ifdef CONFIG_APRICOT_INTEL
1167 (void) inb(ioaddr + 0x10);
1168 outb(4, ioaddr + 0xf);
1169 #endif
1170 CA(dev);
1172 if (i596_debug > 4)
1173 printk("%s: exiting interrupt.\n", dev->name);
1175 dev->interrupt = 0;
1176 return;
1179 static int i596_close(struct device *dev)
1181 struct i596_private *lp = (struct i596_private *) dev->priv;
1182 int boguscnt = 2000;
1183 unsigned long flags;
1185 dev->start = 0;
1186 dev->tbusy = 1;
1188 if (i596_debug > 1)
1189 printk("%s: Shutting down ethercard, status was %4.4x.\n",
1190 dev->name, lp->scb.status);
1192 save_flags(flags);
1193 cli();
1195 while (lp->scb.command)
1196 if (--boguscnt == 0) {
1197 printk("%s: close1 timed out with status %4.4x, cmd %4.4x.\n",
1198 dev->name, lp->scb.status, lp->scb.command);
1199 break;
1201 lp->scb.command = CUC_ABORT | RX_ABORT;
1202 CA(dev);
1204 boguscnt = 2000;
1206 while (lp->scb.command)
1207 if (--boguscnt == 0) {
1208 printk("%s: close2 timed out with status %4.4x, cmd %4.4x.\n",
1209 dev->name, lp->scb.status, lp->scb.command);
1210 break;
1212 restore_flags(flags);
1214 i596_cleanup_cmd(lp);
1216 #ifdef CONFIG_MVME16x_NET
1217 if (MACH_IS_MVME16x) {
1218 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1220 /* Disable all ints */
1221 pcc2[0x28] = 1;
1222 pcc2[0x2a] = 0x40;
1223 pcc2[0x2b] = 0x40; /* Set snooping bits now! */
1225 #endif
1226 #ifdef CONFIG_BVME6000_NET
1227 if (MACH_IS_BVME6000) {
1228 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1230 *ethirq = 1;
1232 #endif
1234 free_irq(dev->irq, dev);
1235 remove_rx_bufs(dev);
1236 MOD_DEC_USE_COUNT;
1238 return 0;
1241 static struct net_device_stats *
1242 i596_get_stats(struct device *dev)
1244 struct i596_private *lp = (struct i596_private *) dev->priv;
1246 return &lp->stats;
1250 * Set or clear the multicast filter for this adaptor.
1253 static void set_multicast_list(struct device *dev)
1255 struct i596_private *lp = (struct i596_private *) dev->priv;
1256 struct i596_cmd *cmd;
1257 int config = 0;
1259 if (i596_debug > 1)
1260 printk("%s: set multicast list, %d entries, promisc %s, allmulti %s\n", dev->name, dev->mc_count, dev->flags & IFF_PROMISC ? "ON" : "OFF", dev->flags & IFF_ALLMULTI ? "ON" : "OFF");
1262 if ((dev->flags & IFF_PROMISC) && !(lp->i596_config[8] & 0x01)) {
1263 lp->i596_config[8] |= 0x01;
1264 config = 1;
1266 if (!(dev->flags & IFF_PROMISC) && (lp->i596_config[8] & 0x01)) {
1267 lp->i596_config[8] &= ~0x01;
1268 config = 1;
1270 if ((dev->flags & IFF_ALLMULTI) && (lp->i596_config[11] & 0x20)) {
1271 lp->i596_config[11] &= ~0x20;
1272 config = 1;
1274 if (!(dev->flags & IFF_ALLMULTI) && !(lp->i596_config[11] & 0x20)) {
1275 lp->i596_config[11] |= 0x20;
1276 config = 1;
1278 if (config) {
1279 if (lp->set_conf.command)
1280 printk("%s: config change request already queued\n",
1281 dev->name);
1282 else {
1283 lp->set_conf.command = CmdConfigure;
1284 i596_add_cmd(dev, &lp->set_conf);
1287 if (dev->mc_count > 0) {
1288 struct dev_mc_list *dmi;
1289 unsigned char *cp;
1290 cmd = (struct i596_cmd *) kmalloc(sizeof(struct i596_cmd) + 2 + dev->mc_count * 6, GFP_ATOMIC);
1291 if (cmd == NULL) {
1292 printk("%s: set_multicast Memory squeeze.\n", dev->name);
1293 return;
1295 cmd->command = CmdMulticastList;
1296 *((unsigned short *) (cmd + 1)) = dev->mc_count * 6;
1297 cp = ((unsigned char *) (cmd + 1)) + 2;
1298 for (dmi = dev->mc_list; dmi != NULL; dmi = dmi->next) {
1299 memcpy(cp, dmi->dmi_addr, 6);
1300 if (i596_debug > 1)
1301 printk("%s: Adding address %02x:%02x:%02x:%02x:%02x:%02x\n", dev->name, *(cp + 0), *(cp + 1), *(cp + 2), *(cp + 3), *(cp + 4), *(cp + 5));
1302 cp += 6;
1304 if (i596_debug > 2)
1305 print_eth(((char *) (cmd + 1)) + 2);
1306 i596_add_cmd(dev, cmd);
1310 #ifdef HAVE_DEVLIST
1311 static unsigned int i596_portlist[] __initdata =
1312 {0x300, 0};
1313 struct netdev_entry i596_drv =
1314 {"apricot", i82596_probe, I596_TOTAL_SIZE, apricot_portlist};
1315 #endif
1317 #ifdef MODULE
1318 static char devicename[9] =
1319 {0,};
1320 static struct device dev_apricot =
1322 devicename, /* device name inserted by /linux/drivers/net/net_init.c */
1323 0, 0, 0, 0,
1324 0x300, 10,
1325 0, 0, 0, NULL, i82596_probe};
1327 static int io = 0x300;
1328 static int irq = 10;
1329 MODULE_PARM(irq, "i");
1331 int init_module(void)
1333 dev_apricot.base_addr = io;
1334 dev_apricot.irq = irq;
1335 if (register_netdev(&dev_apricot) != 0)
1336 return -EIO;
1337 return 0;
1340 void cleanup_module(void)
1342 unregister_netdev(&dev_apricot);
1343 kfree((void *) dev_apricot.mem_start);
1344 dev_apricot.priv = NULL;
1346 /* If we don't do this, we can't re-insmod it later. */
1347 release_region(dev_apricot.base_addr, I596_TOTAL_SIZE);
1350 #endif /* MODULE */
1353 * Local variables:
1354 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 82596.c"
1355 * End: