* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / net / 82596.c
blobf3e1839041ebbf2471f49acc5f096f06a8873a42
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_ALLOC_SKB
107 #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
108 #define kfree_skbmem(buff, size) kfree_s(buff,size)
109 #endif
111 #define APRICOT_DEBUG 2
113 #ifdef APRICOT_DEBUG
114 int i596_debug = APRICOT_DEBUG;
115 #else
116 int i596_debug = 1;
117 #endif
119 #define I596_TOTAL_SIZE 17
121 #define I596_NULL -1
123 #define CMD_EOL 0x8000 /* The last command of the list, stop. */
124 #define CMD_SUSP 0x4000 /* Suspend after doing cmd. */
125 #define CMD_INTR 0x2000 /* Interrupt after doing cmd. */
127 #define CMD_FLEX 0x0008 /* Enable flexible memory model */
129 enum commands {
130 CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
131 CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7
134 #define STAT_C 0x8000 /* Set to 0 after execution */
135 #define STAT_B 0x4000 /* Command being executed */
136 #define STAT_OK 0x2000 /* Command executed ok */
137 #define STAT_A 0x1000 /* Command aborted */
139 #define CUC_START 0x0100
140 #define CUC_RESUME 0x0200
141 #define CUC_SUSPEND 0x0300
142 #define CUC_ABORT 0x0400
143 #define RX_START 0x0010
144 #define RX_RESUME 0x0020
145 #define RX_SUSPEND 0x0030
146 #define RX_ABORT 0x0040
148 struct i596_reg {
149 unsigned short porthi;
150 unsigned short portlo;
151 unsigned long ca;
154 struct i596_cmd {
155 unsigned short status;
156 unsigned short command;
157 struct i596_cmd *next;
160 #define EOF 0x8000
161 #define SIZE_MASK 0x3fff
163 struct i596_tbd {
164 unsigned short size;
165 unsigned short pad;
166 struct i596_tbd *next;
167 char *data;
170 struct tx_cmd {
171 struct i596_cmd cmd;
172 struct i596_tbd *tbd;
173 unsigned short size;
174 unsigned short pad;
175 struct sk_buff *skb; /* So we can free it after tx */
178 struct i596_rfd {
179 unsigned short stat;
180 unsigned short cmd;
181 struct i596_rfd *next;
182 long rbd;
183 unsigned short count;
184 unsigned short size;
185 char data[1532];
188 #define RX_RING_SIZE 16
190 struct i596_scb {
191 unsigned short status;
192 unsigned short command;
193 struct i596_cmd *cmd;
194 struct i596_rfd *rfd;
195 unsigned long crc_err;
196 unsigned long align_err;
197 unsigned long resource_err;
198 unsigned long over_err;
199 unsigned long rcvdt_err;
200 unsigned long short_err;
201 unsigned short t_on;
202 unsigned short t_off;
205 struct i596_iscp {
206 unsigned long stat;
207 struct i596_scb *scb;
210 struct i596_scp {
211 unsigned long sysbus;
212 unsigned long pad;
213 struct i596_iscp *iscp;
216 struct i596_private {
217 volatile struct i596_scp scp;
218 volatile struct i596_iscp iscp;
219 volatile struct i596_scb scb;
220 struct i596_cmd set_add;
221 char eth_addr[8];
222 struct i596_cmd set_conf;
223 char i596_config[16];
224 struct i596_cmd tdr;
225 unsigned long stat;
226 int last_restart __attribute__((aligned(4)));
227 struct i596_rfd *rx_tail;
228 struct i596_cmd *cmd_tail;
229 struct i596_cmd *cmd_head;
230 int cmd_backlog;
231 unsigned long last_cmd;
232 struct net_device_stats stats;
235 char init_setup[] =
237 0x8E, /* length, prefetch on */
238 0xC8, /* fifo to 8, monitor off */
239 #ifdef CONFIG_VME
240 0xc0, /* don't save bad frames */
241 #else
242 0x80, /* don't save bad frames */
243 #endif
244 0x2E, /* No source address insertion, 8 byte preamble */
245 0x00, /* priority and backoff defaults */
246 0x60, /* interframe spacing */
247 0x00, /* slot time LSB */
248 0xf2, /* slot time and retries */
249 0x00, /* promiscuous mode */
250 0x00, /* collision detect */
251 0x40, /* minimum frame length */
252 0xff,
253 0x00,
254 0x7f /* *multi IA */ };
256 static int i596_open(struct net_device *dev);
257 static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev);
258 static void i596_interrupt(int irq, void *dev_id, struct pt_regs *regs);
259 static int i596_close(struct net_device *dev);
260 static struct net_device_stats *i596_get_stats(struct net_device *dev);
261 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd);
262 static void print_eth(char *);
263 static void set_multicast_list(struct net_device *dev);
265 static int ticks_limit = 25;
266 static int max_cmd_backlog = 16;
269 static inline void CA(struct net_device *dev)
271 #ifdef CONFIG_MVME16x_NET
272 if (MACH_IS_MVME16x) {
273 ((struct i596_reg *) dev->base_addr)->ca = 1;
275 #endif
276 #ifdef CONFIG_BVME6000_NET
277 if (MACH_IS_BVME6000) {
278 volatile u32 i = *(volatile u32 *) (dev->base_addr);
280 #endif
281 #ifdef CONFIG_APRICOT_i596
282 if (MACH_IS_APRICOT) {
283 outw(0, (short) (dev->base_addr) + 4);
285 #endif
289 static inline void MPU_PORT(struct net_device *dev, int c, volatile void *x)
291 #ifdef CONFIG_MVME16x_NET
292 if (MACH_IS_MVME16x) {
293 struct i596_reg *p = (struct i596_reg *) (dev->base_addr);
294 p->porthi = ((c) | (u32) (x)) & 0xffff;
295 p->portlo = ((c) | (u32) (x)) >> 16;
297 #endif
298 #ifdef CONFIG_BVME6000_NET
299 if (MACH_IS_BVME6000) {
300 u32 v = (u32) (c) | (u32) (x);
301 v = ((u32) (v) << 16) | ((u32) (v) >> 16);
302 *(volatile u32 *) dev->base_addr = v;
303 udelay(1);
304 *(volatile u32 *) dev->base_addr = v;
306 #endif
310 #if defined(CONFIG_MVME16x_NET) || defined(CONFIG_BVME6000_NET)
311 static void i596_error(int irq, void *dev_id, struct pt_regs *regs)
313 struct net_device *dev = dev_id;
314 struct i596_cmd *cmd;
316 struct i596_private *lp = (struct i596_private *) dev->priv;
317 printk("i596_error: lp = 0x%08x\n", (u32) lp);
318 printk("scp at %08x, .sysbus = %08x, .iscp = %08x\n",
319 (u32) & lp->scp, (u32) lp->scp.sysbus, (u32) lp->scp.iscp);
320 printk("iscp at %08x, .stat = %08x, .scb = %08x\n",
321 (u32) & lp->iscp, (u32) lp->iscp.stat, (u32) lp->iscp.scb);
322 printk("scb at %08x, .status = %04x, .command = %04x\n",
323 (u32) & lp->scb, lp->scb.status, lp->scb.command);
324 printk(" .cmd = %08x, .rfd = %08x\n", (u32) lp->scb.cmd,
325 (u32) lp->scb.rfd);
326 cmd = WSWAPcmd(lp->scb.cmd);
327 while (cmd && (u32) cmd < 0x1000000) {
328 printk("cmd at %08x, .status = %04x, .command = %04x, .next = %08x\n",
329 (u32) cmd, cmd->status, cmd->command, (u32) cmd->next);
330 cmd = WSWAPcmd(cmd->next);
332 while (1);
334 #endif
336 static inline int init_rx_bufs(struct net_device *dev, int num)
338 struct i596_private *lp = (struct i596_private *) dev->priv;
339 int i;
340 struct i596_rfd *rfd;
342 lp->scb.rfd = (struct i596_rfd *) I596_NULL;
344 if (i596_debug > 1)
345 printk("%s: init_rx_bufs %d.\n", dev->name, num);
347 for (i = 0; i < num; i++) {
348 if (!(rfd = (struct i596_rfd *) kmalloc(sizeof(struct i596_rfd), GFP_KERNEL)))
349 break;
351 rfd->stat = 0x0000;
352 rfd->rbd = I596_NULL;
353 rfd->count = 0;
354 rfd->size = 1532;
355 if (i == 0) {
356 rfd->cmd = CMD_EOL;
357 lp->rx_tail = rfd;
358 } else
359 rfd->cmd = 0x0000;
361 rfd->next = lp->scb.rfd;
362 lp->scb.rfd = WSWAPrfd(rfd);
365 if (i != 0)
366 lp->rx_tail->next = lp->scb.rfd;
368 return (i);
371 static inline void remove_rx_bufs(struct net_device *dev)
373 struct i596_private *lp = (struct i596_private *) dev->priv;
374 struct i596_rfd *rfd = WSWAPrfd(lp->scb.rfd);
376 lp->rx_tail->next = (struct i596_rfd *) I596_NULL;
378 do {
379 lp->scb.rfd = rfd->next;
380 kfree(rfd);
381 rfd = WSWAPrfd(lp->scb.rfd);
383 while (rfd != lp->rx_tail);
386 static inline void init_i596_mem(struct net_device *dev)
388 struct i596_private *lp = (struct i596_private *) dev->priv;
389 #if !defined(CONFIG_MVME16x_NET) && !defined(CONFIG_BVME6000_NET)
390 short ioaddr = dev->base_addr;
391 #endif
392 int boguscnt = 100000;
393 unsigned long flags;
395 #if defined(CONFIG_MVME16x_NET) || defined(CONFIG_BVME6000_NET)
396 #ifdef CONFIG_MVME16x_NET
397 if (MACH_IS_MVME16x) {
398 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
400 /* Disable all ints for now */
401 pcc2[0x28] = 1;
402 pcc2[0x2a] = 0x40;
403 pcc2[0x2b] = 0x40; /* Set snooping bits now! */
405 #endif
406 #ifdef CONFIG_BVME6000_NET
407 if (MACH_IS_BVME6000) {
408 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
410 *ethirq = 1;
412 #endif
414 MPU_PORT(dev, PORT_RESET, 0);
416 udelay(100); /* Wait 100us - seems to help */
418 /* change the scp address */
420 MPU_PORT(dev, PORT_ALTSCP, &lp->scp);
422 #else
424 /* change the scp address */
425 outw(0, ioaddr);
426 outw(0, ioaddr);
427 outb(4, ioaddr + 0xf);
428 outw(((((int) &lp->scp) & 0xffff) | 2), ioaddr);
429 outw((((int) &lp->scp) >> 16) & 0xffff, ioaddr);
430 #endif
432 lp->last_cmd = jiffies;
434 #ifdef CONFIG_MVME16x_NET
435 if (MACH_IS_MVME16x)
436 lp->scp.sysbus = 0x00000054;
437 #endif
438 #ifdef CONFIG_BVME6000_NET
439 if (MACH_IS_BVME6000)
440 lp->scp.sysbus = 0x0000004c;
441 #endif
442 #ifdef CONFIG_APRICOT_i596
443 if (MACH_IS_APRICOT)
444 lp->scp.sysbus = 0x00440000;
445 #endif
447 lp->scp.iscp = WSWAPiscp(&(lp->iscp));
448 lp->iscp.scb = WSWAPscb(&(lp->scb));
449 lp->iscp.stat = ISCP_BUSY;
450 lp->cmd_backlog = 0;
452 lp->cmd_head = lp->scb.cmd = (struct i596_cmd *) I596_NULL;
454 if (i596_debug > 1)
455 printk("%s: starting i82596.\n", dev->name);
457 #if !defined(CONFIG_MVME16x_NET) && !defined(CONFIG_BVME6000_NET)
458 (void) inb(ioaddr + 0x10);
459 outb(4, ioaddr + 0xf);
460 #endif
461 CA(dev);
463 while (lp->iscp.stat)
464 if (--boguscnt == 0) {
465 printk("%s: i82596 initialization timed out with status %4.4x, cmd %4.4x.\n",
466 dev->name, lp->scb.status, lp->scb.command);
467 break;
469 lp->scb.command = 0;
471 #ifdef CONFIG_MVME16x_NET
472 if (MACH_IS_MVME16x) {
473 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
475 /* Enable ints, etc. now */
476 pcc2[0x2a] = 0x08;
477 pcc2[0x2a] = 0x55; /* Edge sensitive */
478 pcc2[0x2b] = 0x55;
480 #endif
481 #ifdef CONFIG_BVME6000_NET
482 if (MACH_IS_BVME6000) {
483 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
485 *ethirq = 3;
487 #endif
489 memcpy(lp->i596_config, init_setup, 14);
490 lp->set_conf.command = CmdConfigure;
491 i596_add_cmd(dev, &lp->set_conf);
493 memcpy(lp->eth_addr, dev->dev_addr, 6);
494 lp->set_add.command = CmdSASetup;
495 i596_add_cmd(dev, &lp->set_add);
497 lp->tdr.command = CmdTDR;
498 i596_add_cmd(dev, &lp->tdr);
500 boguscnt = 200000;
502 save_flags(flags);
503 cli();
505 while (lp->scb.command)
506 if (--boguscnt == 0) {
507 printk("%s: receive unit start timed out with status %4.4x, cmd %4.4x.\n",
508 dev->name, lp->scb.status, lp->scb.command);
509 break;
511 lp->scb.command = RX_START;
512 CA(dev);
514 restore_flags(flags);
516 boguscnt = 2000;
517 while (lp->scb.command)
518 if (--boguscnt == 0) {
519 printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n",
520 lp->scb.status, lp->scb.command);
521 break;
523 return;
526 static inline int i596_rx(struct net_device *dev)
528 struct i596_private *lp = (struct i596_private *) dev->priv;
529 struct i596_rfd *rfd;
530 int frames = 0;
532 if (i596_debug > 3)
533 printk("i596_rx()\n");
535 rfd = WSWAPrfd(lp->scb.rfd); /* Reference next frame descriptor to check */
537 while ((rfd->stat) & STAT_C) { /* Loop while we have complete frames */
538 if (i596_debug > 2)
539 print_eth(rfd->data);
541 if ((rfd->stat) & STAT_OK) {
542 /* a good frame */
543 int pkt_len = rfd->count & 0x3fff;
544 struct sk_buff *skb = dev_alloc_skb(pkt_len);
546 frames++;
548 if (skb == NULL) {
549 printk("%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
550 lp->stats.rx_dropped++;
551 } else {
552 skb->dev = dev;
553 memcpy(skb_put(skb, pkt_len), rfd->data, pkt_len);
554 skb->protocol = eth_type_trans(skb, dev);
555 netif_rx(skb);
556 lp->stats.rx_packets++;
557 lp->stats.rx_bytes += pkt_len;
559 } else {
560 lp->stats.rx_errors++;
561 if ((rfd->stat) & 0x0001)
562 lp->stats.collisions++;
563 if ((rfd->stat) & 0x0080)
564 lp->stats.rx_length_errors++;
565 if ((rfd->stat) & 0x0100)
566 lp->stats.rx_over_errors++;
567 if ((rfd->stat) & 0x0200)
568 lp->stats.rx_fifo_errors++;
569 if ((rfd->stat) & 0x0400)
570 lp->stats.rx_frame_errors++;
571 if ((rfd->stat) & 0x0800)
572 lp->stats.rx_crc_errors++;
573 if ((rfd->stat) & 0x1000)
574 lp->stats.rx_length_errors++;
577 /* Clear the buffer descriptor count and EOF + F flags */
579 rfd->stat = 0;
580 rfd->count = 0;
581 rfd->cmd = CMD_EOL;
582 lp->rx_tail->cmd = 0;
583 lp->rx_tail = rfd;
584 lp->scb.rfd = rfd->next;
585 rfd = WSWAPrfd(lp->scb.rfd); /* Next frame descriptor to check */
588 if (i596_debug > 3)
589 printk("frames %d\n", frames);
591 return 0;
594 static inline void i596_cleanup_cmd(struct i596_private *lp)
596 struct i596_cmd *ptr;
597 int boguscnt = 1000;
599 if (i596_debug > 4)
600 printk("i596_cleanup_cmd\n");
602 while (lp->cmd_head != (struct i596_cmd *) I596_NULL) {
603 ptr = lp->cmd_head;
605 lp->cmd_head = WSWAPcmd(lp->cmd_head->next);
606 lp->cmd_backlog--;
608 switch ((ptr->command) & 0x7) {
609 case CmdTx:
611 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
612 struct sk_buff *skb = tx_cmd->skb;
614 dev_kfree_skb(skb);
616 lp->stats.tx_errors++;
617 lp->stats.tx_aborted_errors++;
619 ptr->next = (struct i596_cmd *) I596_NULL;
620 kfree(tx_cmd);
621 break;
623 case CmdMulticastList:
625 ptr->next = (struct i596_cmd *) I596_NULL;
626 kfree(ptr);
627 break;
629 default:
630 ptr->next = (struct i596_cmd *) I596_NULL;
634 while (lp->scb.command)
635 if (--boguscnt == 0) {
636 printk("i596_cleanup_cmd timed out with status %4.4x, cmd %4.4x.\n",
637 lp->scb.status, lp->scb.command);
638 break;
640 lp->scb.cmd = WSWAPcmd(lp->cmd_head);
643 static inline void i596_reset(struct net_device *dev, struct i596_private *lp, int ioaddr)
645 int boguscnt = 1000;
646 unsigned long flags;
648 if (i596_debug > 1)
649 printk("i596_reset\n");
651 save_flags(flags);
652 cli();
654 while (lp->scb.command)
655 if (--boguscnt == 0) {
656 printk("i596_reset timed out with status %4.4x, cmd %4.4x.\n",
657 lp->scb.status, lp->scb.command);
658 break;
660 dev->start = 0;
661 dev->tbusy = 1;
663 lp->scb.command = CUC_ABORT | RX_ABORT;
664 CA(dev);
666 /* wait for shutdown */
667 boguscnt = 4000;
669 while (lp->scb.command)
670 if (--boguscnt == 0) {
671 printk("i596_reset 2 timed out with status %4.4x, cmd %4.4x.\n",
672 lp->scb.status, lp->scb.command);
673 break;
675 restore_flags(flags);
677 i596_cleanup_cmd(lp);
678 i596_rx(dev);
680 dev->start = 1;
681 dev->tbusy = 0;
682 dev->interrupt = 0;
683 init_i596_mem(dev);
686 static void i596_add_cmd(struct net_device *dev, struct i596_cmd *cmd)
688 struct i596_private *lp = (struct i596_private *) dev->priv;
689 int ioaddr = dev->base_addr;
690 unsigned long flags;
691 int boguscnt = 1000;
693 if (i596_debug > 4)
694 printk("i596_add_cmd\n");
696 cmd->status = 0;
697 cmd->command |= (CMD_EOL | CMD_INTR);
698 cmd->next = (struct i596_cmd *) I596_NULL;
699 save_flags(flags);
700 cli();
703 * RGH 300597: Looks to me like there could be a race condition
704 * here. Just because we havn't picked up all the command items
705 * yet, doesn't mean that the 82596 hasn't finished processing
706 * them. So, we may need to do a CUC_START anyway.
707 * Maybe not. If it interrupts saying the CU is idle when there
708 * is still something in the cmd queue, the int handler with restart
709 * the CU.
712 if (lp->cmd_head != (struct i596_cmd *) I596_NULL) {
713 lp->cmd_tail->next = WSWAPcmd(cmd);
714 } else {
715 lp->cmd_head = cmd;
716 while (lp->scb.command)
717 if (--boguscnt == 0) {
718 printk("i596_add_cmd timed out with status %4.4x, cmd %4.4x.\n",
719 lp->scb.status, lp->scb.command);
720 break;
722 lp->scb.cmd = WSWAPcmd(cmd);
723 lp->scb.command = CUC_START;
724 CA(dev);
726 lp->cmd_tail = cmd;
727 lp->cmd_backlog++;
729 lp->cmd_head = WSWAPcmd(lp->scb.cmd); /* Is this redundant? RGH 300597 */
730 restore_flags(flags);
732 if (lp->cmd_backlog > max_cmd_backlog) {
733 unsigned long tickssofar = jiffies - lp->last_cmd;
735 if (tickssofar < ticks_limit)
736 return;
738 printk("%s: command unit timed out, status resetting.\n", dev->name);
740 i596_reset(dev, lp, ioaddr);
744 static int i596_open(struct net_device *dev)
746 int i;
748 if (i596_debug > 1)
749 printk("%s: i596_open() irq %d.\n", dev->name, dev->irq);
751 if (request_irq(dev->irq, &i596_interrupt, 0, "apricot", dev))
752 return -EAGAIN;
753 #ifdef CONFIG_MVME16x_NET
754 if (MACH_IS_MVME16x) {
755 if (request_irq(0x56, &i596_error, 0, "apricot_error", dev))
756 return -EAGAIN;
758 #endif
759 if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE)
760 printk("%s: only able to allocate %d receive buffers\n", dev->name, i);
762 if (i < 4) {
763 free_irq(dev->irq, dev);
764 return -EAGAIN;
766 dev->tbusy = 0;
767 dev->interrupt = 0;
768 dev->start = 1;
769 MOD_INC_USE_COUNT;
771 /* Initialize the 82596 memory */
772 init_i596_mem(dev);
774 return 0; /* Always succeed */
777 static int i596_start_xmit(struct sk_buff *skb, struct net_device *dev)
779 struct i596_private *lp = (struct i596_private *) dev->priv;
780 int ioaddr = dev->base_addr;
781 struct tx_cmd *tx_cmd;
783 if (i596_debug > 2)
784 printk("%s: 82596 start xmit\n", dev->name);
786 /* Transmitter timeout, serious problems. */
787 if (dev->tbusy) {
788 int tickssofar = jiffies - dev->trans_start;
789 if (tickssofar < 5)
790 return 1;
791 printk("%s: transmit timed out, status resetting.\n",
792 dev->name);
793 lp->stats.tx_errors++;
794 /* Try to restart the adaptor */
795 if (lp->last_restart == lp->stats.tx_packets) {
796 if (i596_debug > 1)
797 printk("Resetting board.\n");
799 /* Shutdown and restart */
800 i596_reset(dev, lp, ioaddr);
801 } else {
802 /* Issue a channel attention signal */
803 if (i596_debug > 1)
804 printk("Kicking board.\n");
805 lp->scb.command = CUC_START | RX_START;
806 CA(dev);
807 lp->last_restart = lp->stats.tx_packets;
809 dev->tbusy = 0;
810 dev->trans_start = jiffies;
812 if (i596_debug > 3)
813 printk("%s: i596_start_xmit() called\n", dev->name);
815 /* Block a timer-based transmit from overlapping. This could better be
816 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
817 if (test_and_set_bit(0, (void *) &dev->tbusy) != 0)
818 printk("%s: Transmitter access conflict.\n", dev->name);
819 else {
820 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
821 dev->trans_start = jiffies;
823 tx_cmd = (struct tx_cmd *) kmalloc((sizeof(struct tx_cmd) + sizeof(struct i596_tbd)), GFP_ATOMIC);
824 if (tx_cmd == NULL) {
825 printk("%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
826 lp->stats.tx_dropped++;
828 dev_kfree_skb(skb);
829 } else {
830 struct i596_tbd *tbd = (struct i596_tbd *) (tx_cmd + 1);
831 tx_cmd->tbd = WSWAPtbd(tbd);
832 tbd->next = (struct i596_tbd *) I596_NULL;
834 tx_cmd->cmd.command = CMD_FLEX | CmdTx;
835 tx_cmd->skb = skb;
837 tx_cmd->pad = 0;
838 tx_cmd->size = 0;
839 tbd->pad = 0;
840 tbd->size = EOF | length;
842 tbd->data = WSWAPchar(skb->data);
844 if (i596_debug > 3)
845 print_eth(skb->data);
846 i596_add_cmd(dev, (struct i596_cmd *) tx_cmd);
848 lp->stats.tx_packets++;
849 lp->stats.tx_bytes += length;
853 dev->tbusy = 0;
855 return 0;
859 static void print_eth(char *add)
861 int i;
863 printk("print_eth(%08x)\n", (unsigned int) add);
864 printk("Dest ");
865 for (i = 0; i < 6; i++)
866 printk(" %2.2X", (unsigned char) add[i]);
867 printk("\n");
869 printk("Source");
870 for (i = 0; i < 6; i++)
871 printk(" %2.2X", (unsigned char) add[i + 6]);
872 printk("\n");
873 printk("type %2.2X%2.2X\n", (unsigned char) add[12], (unsigned char) add[13]);
876 int __init i82596_probe(struct net_device *dev)
878 int i;
879 struct i596_private *lp;
880 char eth_addr[6];
882 #ifdef CONFIG_MVME16x_NET
883 if (MACH_IS_MVME16x) {
884 static int probed = 0;
886 if (mvme16x_config & MVME16x_CONFIG_NO_ETHERNET) {
887 printk("Ethernet probe disabled - chip not present\n");
888 return ENODEV;
890 if (probed)
891 return ENODEV;
892 probed++;
893 memcpy(eth_addr, (void *) 0xfffc1f2c, 6); /* YUCK! Get addr from NOVRAM */
894 dev->base_addr = MVME_I596_BASE;
895 dev->irq = (unsigned) MVME16x_IRQ_I596;
897 #endif
898 #ifdef CONFIG_BVME6000_NET
899 if (MACH_IS_BVME6000) {
900 volatile unsigned char *rtc = (unsigned char *) BVME_RTC_BASE;
901 unsigned char msr = rtc[3];
902 int i;
904 rtc[3] |= 0x80;
905 for (i = 0; i < 6; i++)
906 eth_addr[i] = rtc[i * 4 + 7]; /* Stored in RTC RAM at offset 1 */
907 rtc[3] = msr;
908 dev->base_addr = BVME_I596_BASE;
909 dev->irq = (unsigned) BVME_IRQ_I596;
911 #endif
912 #ifdef CONFIG_APRICOT_INTEL
913 int checksum = 0;
914 int ioaddr = 0x300;
916 /* this is easy the ethernet interface can only be at 0x300 */
917 /* first check nothing is already registered here */
919 if (check_region(ioaddr, I596_TOTAL_SIZE))
920 return ENODEV;
922 for (i = 0; i < 8; i++) {
923 eth_addr[i] = inb(ioaddr + 8 + i);
924 checksum += eth_addr[i];
927 /* checksum is a multiple of 0x100, got this wrong first time
928 some machines have 0x100, some 0x200. The DOS driver doesn't
929 even bother with the checksum */
931 if (checksum % 0x100)
932 return ENODEV;
934 /* Some other boards trip the checksum.. but then appear as ether
935 address 0. Trap these - AC */
937 if (memcmp(eth_addr, "\x00\x00\x49", 3) != 0)
938 return ENODEV;
940 request_region(ioaddr, I596_TOTAL_SIZE, "i596");
942 dev->base_addr = ioaddr;
943 dev->irq = 10;
944 #endif
945 ether_setup(dev);
946 printk("%s: 82596 at %#3lx,", dev->name, dev->base_addr);
948 for (i = 0; i < 6; i++)
949 printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);
951 printk(" IRQ %d.\n", dev->irq);
953 if (i596_debug > 0)
954 printk(version);
956 /* The APRICOT-specific entries in the device structure. */
957 dev->open = &i596_open;
958 dev->stop = &i596_close;
959 dev->hard_start_xmit = &i596_start_xmit;
960 dev->get_stats = &i596_get_stats;
961 dev->set_multicast_list = &set_multicast_list;
963 dev->mem_start = (int) kmalloc(sizeof(struct i596_private) + 0x0f, GFP_KERNEL);
964 /* align for scp */
965 dev->priv = (void *) ((dev->mem_start + 0xf) & 0xfffffff0);
967 lp = (struct i596_private *) dev->priv;
968 if (i596_debug)
969 printk("%s: lp at 0x%08lx, lp->scb at 0x%08lx\n"
970 ,dev->name, (unsigned long) lp, (unsigned long) &lp->scb);
971 memset((void *) lp, 0, sizeof(struct i596_private));
972 lp->scb.command = 0;
973 lp->scb.cmd = (struct i596_cmd *) I596_NULL;
974 lp->scb.rfd = (struct i596_rfd *) I596_NULL;
976 return 0;
979 static void i596_interrupt(int irq, void *dev_id, struct pt_regs *regs)
981 struct net_device *dev = dev_id;
982 struct i596_private *lp;
983 short ioaddr;
984 int boguscnt = 2000;
985 unsigned short status, ack_cmd = 0;
987 #ifdef CONFIG_BVME6000_NET
988 if (MACH_IS_BVME6000) {
989 if (*(char *) BVME_LOCAL_IRQ_STAT & BVME_ETHERR) {
990 i596_error(BVME_IRQ_I596, NULL, NULL);
991 return;
994 #endif
995 if (dev == NULL) {
996 printk("i596_interrupt(): irq %d for unknown device.\n", irq);
997 return;
999 if (i596_debug > 3)
1000 printk("%s: i596_interrupt(): irq %d\n", dev->name, irq);
1002 if (dev->interrupt)
1003 printk("%s: Re-entering the interrupt handler.\n", dev->name);
1005 dev->interrupt = 1;
1007 ioaddr = dev->base_addr;
1009 lp = (struct i596_private *) dev->priv;
1011 while (lp->scb.command)
1012 if (--boguscnt == 0) {
1013 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
1014 break;
1016 status = lp->scb.status;
1018 if (i596_debug > 4)
1019 printk("%s: i596 interrupt, status %4.4x.\n", dev->name, status);
1021 ack_cmd = status & 0xf000;
1023 if ((status & 0x8000) || (status & 0x2000)) {
1024 struct i596_cmd *ptr;
1026 if ((i596_debug > 4) && (status & 0x8000))
1027 printk("%s: i596 interrupt completed command.\n", dev->name);
1028 if ((i596_debug > 4) && (status & 0x2000))
1029 printk("%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700);
1031 while ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (lp->cmd_head->status & STAT_C)) {
1032 ptr = lp->cmd_head;
1034 if (i596_debug > 2)
1035 printk("cmd_head->status = %04x, ->command = %04x\n",
1036 lp->cmd_head->status, lp->cmd_head->command);
1037 lp->cmd_head = WSWAPcmd(lp->cmd_head->next);
1038 lp->cmd_backlog--;
1040 switch ((ptr->command) & 0x7) {
1041 case CmdTx:
1043 struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
1044 struct sk_buff *skb = tx_cmd->skb;
1046 if ((ptr->status) & STAT_OK) {
1047 if (i596_debug > 2)
1048 print_eth(skb->data);
1049 } else {
1050 lp->stats.tx_errors++;
1051 if ((ptr->status) & 0x0020)
1052 lp->stats.collisions++;
1053 if (!((ptr->status) & 0x0040))
1054 lp->stats.tx_heartbeat_errors++;
1055 if ((ptr->status) & 0x0400)
1056 lp->stats.tx_carrier_errors++;
1057 if ((ptr->status) & 0x0800)
1058 lp->stats.collisions++;
1059 if ((ptr->status) & 0x1000)
1060 lp->stats.tx_aborted_errors++;
1063 dev_kfree_skb(skb);
1065 ptr->next = (struct i596_cmd *) I596_NULL;
1066 kfree(tx_cmd);
1067 break;
1069 case CmdMulticastList:
1071 ptr->next = (struct i596_cmd *) I596_NULL;
1072 kfree(ptr);
1073 break;
1075 case CmdTDR:
1077 unsigned long status = *((unsigned long *) (ptr + 1));
1079 if (status & 0x8000) {
1080 if (i596_debug > 3)
1081 printk("%s: link ok.\n", dev->name);
1082 } else {
1083 if (status & 0x4000)
1084 printk("%s: Transceiver problem.\n", dev->name);
1085 if (status & 0x2000)
1086 printk("%s: Termination problem.\n", dev->name);
1087 if (status & 0x1000)
1088 printk("%s: Short circuit.\n", dev->name);
1090 if (i596_debug > 1)
1091 printk("%s: Time %ld.\n", dev->name, status & 0x07ff);
1093 break;
1095 case CmdConfigure:
1097 ptr->next = (struct i596_cmd *) I596_NULL;
1098 /* Zap command so set_multicast_list() knows it is free */
1099 ptr->command = 0;
1100 break;
1102 default:
1103 ptr->next = (struct i596_cmd *) I596_NULL;
1105 lp->last_cmd = jiffies;
1108 ptr = lp->cmd_head;
1109 while ((ptr != (struct i596_cmd *) I596_NULL) && (ptr != lp->cmd_tail)) {
1110 ptr->command &= 0x1fff;
1111 ptr = WSWAPcmd(ptr->next);
1114 if ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (dev->start))
1115 ack_cmd |= CUC_START;
1116 lp->scb.cmd = WSWAPcmd(lp->cmd_head);
1118 if ((status & 0x1000) || (status & 0x4000)) {
1119 if ((i596_debug > 4) && (status & 0x4000))
1120 printk("%s: i596 interrupt received a frame.\n", dev->name);
1121 /* Only RX_START if stopped - RGH 07-07-96 */
1122 if (status & 0x1000) {
1123 if (dev->start)
1124 ack_cmd |= RX_START;
1125 if (i596_debug > 1)
1126 printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x00f0);
1128 i596_rx(dev);
1130 /* acknowledge the interrupt */
1132 /* COMMENTED OUT <<<<<
1133 if ((lp->scb.cmd != (struct i596_cmd *) I596_NULL) && (dev->start))
1134 ack_cmd |= CUC_START;
1136 boguscnt = 1000;
1137 while (lp->scb.command)
1138 if (--boguscnt == 0) {
1139 printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
1140 break;
1142 lp->scb.command = ack_cmd;
1144 #ifdef CONFIG_MVME16x_NET
1145 if (MACH_IS_MVME16x) {
1146 /* Ack the interrupt */
1148 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1150 pcc2[0x2a] |= 0x08;
1152 #endif
1153 #ifdef CONFIG_BVME6000_NET
1154 if (MACH_IS_BVME6000) {
1155 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1157 *ethirq = 1;
1158 *ethirq = 3;
1160 #endif
1161 #ifdef CONFIG_APRICOT_INTEL
1162 (void) inb(ioaddr + 0x10);
1163 outb(4, ioaddr + 0xf);
1164 #endif
1165 CA(dev);
1167 if (i596_debug > 4)
1168 printk("%s: exiting interrupt.\n", dev->name);
1170 dev->interrupt = 0;
1171 return;
1174 static int i596_close(struct net_device *dev)
1176 struct i596_private *lp = (struct i596_private *) dev->priv;
1177 int boguscnt = 2000;
1178 unsigned long flags;
1180 dev->start = 0;
1181 dev->tbusy = 1;
1183 if (i596_debug > 1)
1184 printk("%s: Shutting down ethercard, status was %4.4x.\n",
1185 dev->name, lp->scb.status);
1187 save_flags(flags);
1188 cli();
1190 while (lp->scb.command)
1191 if (--boguscnt == 0) {
1192 printk("%s: close1 timed out with status %4.4x, cmd %4.4x.\n",
1193 dev->name, lp->scb.status, lp->scb.command);
1194 break;
1196 lp->scb.command = CUC_ABORT | RX_ABORT;
1197 CA(dev);
1199 boguscnt = 2000;
1201 while (lp->scb.command)
1202 if (--boguscnt == 0) {
1203 printk("%s: close2 timed out with status %4.4x, cmd %4.4x.\n",
1204 dev->name, lp->scb.status, lp->scb.command);
1205 break;
1207 restore_flags(flags);
1209 i596_cleanup_cmd(lp);
1211 #ifdef CONFIG_MVME16x_NET
1212 if (MACH_IS_MVME16x) {
1213 volatile unsigned char *pcc2 = (unsigned char *) 0xfff42000;
1215 /* Disable all ints */
1216 pcc2[0x28] = 1;
1217 pcc2[0x2a] = 0x40;
1218 pcc2[0x2b] = 0x40; /* Set snooping bits now! */
1220 #endif
1221 #ifdef CONFIG_BVME6000_NET
1222 if (MACH_IS_BVME6000) {
1223 volatile unsigned char *ethirq = (unsigned char *) BVME_ETHIRQ_REG;
1225 *ethirq = 1;
1227 #endif
1229 free_irq(dev->irq, dev);
1230 remove_rx_bufs(dev);
1231 MOD_DEC_USE_COUNT;
1233 return 0;
1236 static struct net_device_stats *
1237 i596_get_stats(struct net_device *dev)
1239 struct i596_private *lp = (struct i596_private *) dev->priv;
1241 return &lp->stats;
1245 * Set or clear the multicast filter for this adaptor.
1248 static void set_multicast_list(struct net_device *dev)
1250 struct i596_private *lp = (struct i596_private *) dev->priv;
1251 struct i596_cmd *cmd;
1252 int config = 0;
1254 if (i596_debug > 1)
1255 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");
1257 if ((dev->flags & IFF_PROMISC) && !(lp->i596_config[8] & 0x01)) {
1258 lp->i596_config[8] |= 0x01;
1259 config = 1;
1261 if (!(dev->flags & IFF_PROMISC) && (lp->i596_config[8] & 0x01)) {
1262 lp->i596_config[8] &= ~0x01;
1263 config = 1;
1265 if ((dev->flags & IFF_ALLMULTI) && (lp->i596_config[11] & 0x20)) {
1266 lp->i596_config[11] &= ~0x20;
1267 config = 1;
1269 if (!(dev->flags & IFF_ALLMULTI) && !(lp->i596_config[11] & 0x20)) {
1270 lp->i596_config[11] |= 0x20;
1271 config = 1;
1273 if (config) {
1274 if (lp->set_conf.command)
1275 printk("%s: config change request already queued\n",
1276 dev->name);
1277 else {
1278 lp->set_conf.command = CmdConfigure;
1279 i596_add_cmd(dev, &lp->set_conf);
1282 if (dev->mc_count > 0) {
1283 struct dev_mc_list *dmi;
1284 unsigned char *cp;
1285 cmd = (struct i596_cmd *) kmalloc(sizeof(struct i596_cmd) + 2 + dev->mc_count * 6, GFP_ATOMIC);
1286 if (cmd == NULL) {
1287 printk("%s: set_multicast Memory squeeze.\n", dev->name);
1288 return;
1290 cmd->command = CmdMulticastList;
1291 *((unsigned short *) (cmd + 1)) = dev->mc_count * 6;
1292 cp = ((unsigned char *) (cmd + 1)) + 2;
1293 for (dmi = dev->mc_list; dmi != NULL; dmi = dmi->next) {
1294 memcpy(cp, dmi->dmi_addr, 6);
1295 if (i596_debug > 1)
1296 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));
1297 cp += 6;
1299 if (i596_debug > 2)
1300 print_eth(((char *) (cmd + 1)) + 2);
1301 i596_add_cmd(dev, cmd);
1305 #ifdef HAVE_DEVLIST
1306 static unsigned int i596_portlist[] __initdata =
1307 {0x300, 0};
1308 struct netdev_entry i596_drv =
1309 {"apricot", i82596_probe, I596_TOTAL_SIZE, apricot_portlist};
1310 #endif
1312 #ifdef MODULE
1313 static char devicename[9] =
1314 {0,};
1315 static struct net_device dev_apricot =
1317 devicename, /* device name inserted by /linux/drivers/net/net_init.c */
1318 0, 0, 0, 0,
1319 0x300, 10,
1320 0, 0, 0, NULL, i82596_probe};
1322 static int io = 0x300;
1323 static int irq = 10;
1324 MODULE_PARM(irq, "i");
1326 int init_module(void)
1328 dev_apricot.base_addr = io;
1329 dev_apricot.irq = irq;
1330 if (register_netdev(&dev_apricot) != 0)
1331 return -EIO;
1332 return 0;
1335 void cleanup_module(void)
1337 unregister_netdev(&dev_apricot);
1338 kfree((void *) dev_apricot.mem_start);
1339 dev_apricot.priv = NULL;
1341 /* If we don't do this, we can't re-insmod it later. */
1342 release_region(dev_apricot.base_addr, I596_TOTAL_SIZE);
1345 #endif /* MODULE */
1348 * Local variables:
1349 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c 82596.c"
1350 * End: