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