/* Intel Professional Workstation/panther ethernet driver */
/* elp486.c: A panther 82596 ethernet driver for linux. */
/*
	IMPORTANT:
	When I was forced to switch from 2.0 to 2.2, I suddenly was smashed
	against the fact that the virtual addresses ARE different from the
	physical addresses!
	The impact for a driver this old was severe.
	Therefore I started using va_ to indicate a pointer that we can use,
	and pa_ to indicate a physical address "pointer". Conversion between
	the two is done using va_to_pa and pa_to_va.
	This means: THERE IS NO pointer pointing to a device without either
	the va_ or the pa_ prefix.
	Next thing: The i82596 uses 0xFFFFFFFF as a NULL pointer. The
	pa_to_va converts 0xFFFFFFFF to 0, and va_to_pa converts 0 to
	0xFFFFFFFF. So:
	va_ 0 <-> pa_ 0xffffffff

	ORIGIN:
	This is largely a copy of the apricot driver.

	There are currently two motherboards that I know of in the
	professional workstation. The only one that I know is the
	intel panther motherboard.

	Maintained By:
	Ard van Breemen
	<ard@murphy.nl>|<ard@cstmel.hobby.nl>|<ard@cstmel.nl.eu.org>

	Credits:
	Thanks to Murphy Software BV for letting me write this in their time.
	Well, actually, I get payed doing this...
	(Also: see http://www.murphy.nl for murphy, and my homepage ~ard for
	more information on the Professional Workstation)

Changes:
$Log: elp486.c,v $
Revision 1.1  1999/01/28 10:39:07  ard
For first distribution

Revision 1.2  1998/11/14 01:27:04  ard
Started cvs logging


Tue Aug 25 23:22:32 MET DST 1998
Submitted to the kernel tree (accepted?)

Wed Feb 18 11:37:55 MET 1998
Finalized first alpha release.
Major problem was that the original apricot driver is pre-2.0. Due to certain
expectations for skb it was outdated, and did not work anymore.

Todo:
- Speed up driver by preallocating skb's before receiving.
- Keep statistics for packet size.
- Use flexible memory model with skb's < max_packet_size.
  (This will allow for far more receiver buffers in the same space if
	the nominal packet size is < 1/2 max_packet_size...)
- Determine mac address from bios
*/
/*
	This driver is derived from the apricot.c as follows:

	Apricot
	Written 1994 by Mark Evans.
	This driver is for the Apricot 82596 bus-master interface

	Modularised 12/94 Mark Evans
    
	Driver skeleton 
	Written 1993 by Donald Becker.
	Copyright 1993 United States Government as represented by the Director,
	National Security Agency.  This software may only be used and distributed
	according to the terms of the GNU Public License as modified by SRC,
	incorporated herein by reference.

	The author may be reached as becker@super.org or
	C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
*/

/*static const char *version = "elp486.c:v0.000005 19980825\n";*/
static const char *version = "$Id: elp486.c,v 1.1 1999/01/28 10:39:07 ard Exp $\n";

#define REPORT_IN_INTERRUPT
#define SLOW_DOWN_IO udelay(5);

// #define BGP	printk("ELP486@%d\n",__LINE__);mdelay(1000); //BUG
#define BGP	

#include <linux/module.h>

#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/malloc.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>

#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/dma.h>

#ifdef REALY_OLD
#ifndef HAVE_PORTRESERVE
#define check_region(addr, size)	0
#define request_region(addr, size,name)	do ; while(0)
#endif

#ifndef HAVE_ALLOC_SKB
#define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
#define kfree_skbmem(buff, size) kfree_s(buff,size)
#endif
#endif

#define LOG_SRCDST    0x80000000
#define LOG_STATINT   0x40000000
#define LOG_STARTINT  0x20000000
#define ELP486_DEBUG 0 /*(LOG_STATINT)*/
#define i596_debug debug

#ifdef ELP486_DEBUG
static int i596_debug = ELP486_DEBUG;
#else
static int i596_debug =0xffff;
#endif

static const char * const medianame[] = {
	"10baseT", "AUI",
	"10baseT-FD", "AUI-FD",
};

#define ELP486_TOTAL_SIZE 16

#define I596_NULL (0xffffffff)
#define PA_NULL (0L)

#define CMD_EOL		0x8000	/* The last command of the list, stop. */
#define CMD_SUSP	0x4000	/* Suspend after doing cmd. */
#define CMD_INTR	0x2000	/* Interrupt after doing cmd. */

#define CMD_FLEX	0x0008	/* Enable flexible memory model */

enum commands {
	CmdNOp = 0,
	CmdSASetup = 1,
	CmdConfigure = 2,
	CmdMulticastList = 3,
	CmdTx = 4,
	CmdTDR = 5,
	CmdDump = 6,
	CmdDiagnose = 7
};

#define STAT_C		0x8000	/* Set to 0 after execution */
#define STAT_B		0x4000	/* Command being executed */
#define STAT_OK		0x2000	/* Command executed ok */
#define STAT_A		0x1000	/* Command aborted */

#define	 CUC_START	0x0100
#define	 CUC_RESUME	0x0200
#define	 CUC_SUSPEND    0x0300
#define	 CUC_ABORT	0x0400
#define	 RX_START	0x0010
#define	 RX_RESUME	0x0020
#define	 RX_SUSPEND	0x0030
#define	 RX_ABORT	0x0040
#define ACK_CX 0x8000
#define ACK_FR 0x4000
#define ACK_CNA 0x2000
#define ACK_RNR 0x1000

typedef unsigned long phys_addr;
static inline phys_addr va_to_pa(volatile void *x) {
	if(x) {
		return virt_to_bus(x);
	}	else {
		return I596_NULL;
	}
}

static inline void *pa_to_va(phys_addr x) {
	if(x==I596_NULL) {
		return NULL;
	}	else {
		return bus_to_virt(x);
	}
}

struct i596_cmd {
	unsigned short status;
	unsigned short command;
//	struct i596_cmd *next;
	phys_addr pa_next;
};

#define EOF		0x8000
#define SIZE_MASK	0x3fff

struct i596_tbd {
	unsigned short size;
	unsigned short pad;
	//	struct i596_tbd *next;
	phys_addr pa_next;
	// char *data;
	phys_addr pa_data;
	struct sk_buff *va_skb;
};

struct tx_cmd {
	struct i596_cmd cmd;
	//	struct i596_tbd *tbd;
	phys_addr pa_tbd;
	unsigned short size;
	unsigned short pad;
};

struct i596_rfd {
	unsigned short stat;
	unsigned short cmd;
	//	struct i596_rfd *next;
	phys_addr pa_next;
	//	struct i596_rbd *rbd; 
	phys_addr pa_rbd;
	unsigned short count;
	unsigned short size;
	char data[1532];
};

#define RBD_EL		0x8000
#define RBD_P		0x4000
#define RBD_SIZEMASK 0x3fff
#define RBD_EOF 0x8000
#define RBD_F 0x4000
struct i596_rbd {
	unsigned short size;
	unsigned short pad;
	//	struct i596_tbd *next;
	phys_addr pa_next;
	// char *data;
	phys_addr pa_data;
	// struct i596_tbd *prev;
	phys_addr pa_prev;
	
	/* Driver private part */
	struct sk_buff *va_skb;
};

#define RX_RING_SIZE 64
#define RX_SKBSIZE (ETH_FRAME_LEN+10)
#define RX_RBD_SIZE 32

struct i596_scb {
	unsigned short status;
	unsigned short command;
	// struct i596_cmd *cmd;
	phys_addr pa_cmd;
	// struct i596_rfd *rfd;
	phys_addr pa_rfd;
	unsigned long crc_err;
	unsigned long align_err;
	unsigned long resource_err;
	unsigned long over_err;
	unsigned long rcvdt_err;
	unsigned long short_err;
	unsigned short t_on;
	unsigned short t_off;
};

struct i596_iscp {
	unsigned long stat;
	// struct i596_scb *scb;
	phys_addr pa_scb;
};

struct i596_scp {
	unsigned long sysbus;
	unsigned long pad;
	// struct i596_iscp *iscp;
	phys_addr pa_iscp;
};

struct i596_private {
	struct i596_scp scp;
	struct i596_iscp iscp;
	struct i596_scb scb;
	struct i596_cmd set_add;
	char eth_addr[8];
	struct i596_cmd set_conf;
	char i596_config[16];
	struct i596_cmd tdr;
	unsigned long stat;
	int last_restart;
	volatile struct i596_rbd *va_rbd_list;
	volatile struct i596_rbd *va_rbd_tail;
	volatile struct i596_rfd *va_rx_tail;
	volatile struct i596_cmd *va_cmd_tail;
	volatile struct i596_cmd *va_cmd_head;
	int cmd_backlog;
	unsigned long last_cmd;
	struct enet_statistics stats;
};

char init_setup[] = {
	0x8E,	/* length, prefetch on */
	0xC8,	/* fifo to 8, monitor off */
	// 0x80,	/* don't save bad frames */
	0x40,	/* don't save bad frames */
	0x2E,	/* No source address insertion, 8 byte preamble */
	0x00,	/* priority and backoff defaults */
	0x60,	/* interframe spacing */
	0x00,	/* slot time LSB */
	0xf2,	/* slot time and retries */
	0x00,	/* promiscuous mode */
	0x00,	/* collision detect */
	0x40,	/* minimum frame length */
	/* 0xff,	 */
	0xfb, /* Enable crc append in memory,  */
	0x00,
	0x7f	/*  *multi IA */
};

static int i596_open(struct device *dev);
static int i596_start_xmit(struct sk_buff *skb, struct device *dev);
static void i596_interrupt(int irq, void *dev_id, struct pt_regs *regs);
static int i596_close(struct device *dev);
static struct enet_statistics *i596_get_stats(struct device *dev);
static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd);
static void print_eth(char *);
static void set_multicast_list(struct device *dev);

static inline int init_rx_bufs(struct device *dev, int num) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	int i;
	struct i596_rfd *va_rfd;
	// struct i596_rbd *rbd;

	lp->scb.pa_rfd = I596_NULL;

	if (i596_debug > 1) printk ("%s: init_rx_bufs %d.\n", dev->name, num);

	for (i = 0; i < num; i++) {
		if (
			!(va_rfd = (struct i596_rfd *)kmalloc(sizeof(struct i596_rfd), GFP_KERNEL))
		) {
			break;
		}
		va_rfd->stat = 0x0000;
		va_rfd->pa_rbd = I596_NULL;
		va_rfd->count = 0;
		va_rfd->size = 1532;
		if (i == 0) {
			/* Remember that we are naughty... */
			/*rfd->cmd = CMD_EOL;*/
			lp->va_rx_tail = va_rfd;
		} else {
			va_rfd->cmd = 0x0000;
		}
		va_rfd->pa_next = lp->scb.pa_rfd;
		lp->scb.pa_rfd = va_to_pa(va_rfd);
	}
	if (i != 0) {
		lp->va_rx_tail->pa_next = lp->scb.pa_rfd;
	}
	#if 0
	for(i=0;i<RX_RBD_SIZE;i++) {
		rbd=(struct i596_rbd *)kmalloc(sizeof(struct i596_rbd),GFP_KERNEL);
		if(rbd) {
			rbd->pad=0;
			rbd->count=0;
			rbd->skb=dev_alloc_skb(RX_SKB_SIZE);
			if(!rbd->skb) {
				printk("dev_alloc_skb failed");
			}
			rbd->next=rfd->rbd;
			if(i) {
				rfd->rbd->prev=rbd;
				rbd->size=RX_SKB_SIZE;
			} else {
				rbd->size=RX_SKB_SIZE|RBD_EL;
				lp->rbd_tail=rbd;
			}

			rfd->rbd=rbd;
		} else {
			printk("Could not kmalloc rbd\n");
		}
	}
	lp->rbd_tail->next=rfd->rbd;
	#endif
	return (i);
}

static inline void remove_rx_bufs(struct device *dev) {
	struct i596_private *lp = (struct i596_private *)dev->priv;
	struct i596_rfd *va_rfd = pa_to_va(lp->scb.pa_rfd);
	lp->va_rx_tail->pa_next = I596_NULL;
	do {
		lp->scb.pa_rfd = va_rfd->pa_next;
		kfree(va_rfd);
		va_rfd = pa_to_va(lp->scb.pa_rfd);
	} while (va_rfd != lp->va_rx_tail);
	#if 0
	for(lp->rbd_list) {
	}
	#endif
}

#if 0
#define BUG(x) printk("%s@%d:%s\n",__FILE__,__LINE__,x)
#else
#define BUG(x)
#endif
static inline void CA(void) {
	outb(0,0xcb4);
	udelay(8);
}

static inline void CLEAR_INT(void) {
	/*
	According to documentation this port does not exist.
	But it does!
	I don't know what it does, but try living without it.
	It certainly does some good so once in a while.
	*/
	outb(0,0xcb8);
}

#define PORT_RESET 0
static inline void PORT(phys_addr address, unsigned int command) {
	outw(((((unsigned int)address) & 0xffff) | command), 0xcb0);
	outw((((unsigned int)address)>>16) & 0xffff, 0xcb2);
	/*
	This does not work. Probably the I/O bus is configured 16 bit :(
	outl(address|command,0xcb0);
	*/
}

static inline void init_i596_mem(struct device *dev) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	// short ioaddr = dev->base_addr;
	volatile long *va_selftest;
	int boguscnt = 10000;
	/* change the scp address */
	/*
	Reset the 82596.
	We need to wait 10 systemclock cycles, and 5 serial clock
	cycles. Just do 15 SLOW_DOWN_IOs, that must be more than
	enough
	*/
	PORT(PA_NULL,PORT_RESET);
	udelay(100);

	va_selftest=(long *)&lp->scp;
	*va_selftest=0x0L;
	PORT(va_to_pa(va_selftest),1);
	printk("Selftest *: %p\n",va_selftest);
	for(boguscnt=30000;boguscnt&&!(*va_selftest&STAT_C);boguscnt--){
		printk("selftest delay");
		udelay(1);
		printk(" done \n");
	}

	printk("elp486 i82596 selftest-timed(%d):%08lx\n",boguscnt,*va_selftest);

	lp->last_cmd = jiffies;

	lp->scp.sysbus = 0x00440000;
	lp->scp.pa_iscp = va_to_pa(&(lp->iscp));
	lp->iscp.pa_scb = va_to_pa(&(lp->scb));
	lp->iscp.stat = 0x0001;
	lp->cmd_backlog = 0;

	lp->va_cmd_head = NULL;
	lp->scb.pa_cmd = I596_NULL;

	if (i596_debug > 2) printk("%s: starting i82596.\n", dev->name);

	BGP
	PORT(va_to_pa(&lp->scp),2);
	CA();
	CLEAR_INT();
	BGP

	while (lp->iscp.stat) {
		if (--boguscnt == 0) {
			printk("%s: i82596 initialization timed out with status %4.4x, cmd %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
			break;
		}
		BGP
		udelay(5);
	}

	lp->scb.command = 0;

	BGP
	memcpy ((void *)lp->i596_config, init_setup, 14);
	lp->set_conf.command = CmdConfigure;
	BGP
	i596_add_cmd(dev, (void *)&lp->set_conf);
	BGP

	BGP
	memcpy ((void *)lp->eth_addr, dev->dev_addr, 6);
	BGP
	lp->set_add.command = CmdSASetup;
	BGP
	i596_add_cmd(dev, (struct i596_cmd *)&lp->set_add);
	BGP

	lp->tdr.command = CmdTDR;
	BGP
	i596_add_cmd(dev, (struct i596_cmd *)&lp->tdr);
	BGP

	boguscnt = 200;
	while (/*lp->scb.status,*/ lp->scb.command) {
		if (--boguscnt == 0) {
			printk("%s: receive unit start timed out with status %4.4x, cmd %4.4x.\n",
dev->name, lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	BGP
	}
	BGP
	lp->scb.command = RX_START;
	BGP
	CA();
	BGP
	boguscnt = 0xffffff;
	while (lp->scb.status, lp->scb.command) {
	BGP
		udelay(5);
		if (--boguscnt == 0) {
			printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command);
			break;
		}
	}
	BGP
	printk("stop\n");return; // BUG
	return;
}

static inline int i596_rx(struct device *dev) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	struct i596_rfd *va_rfd;
	int frames = 0;

	if (i596_debug > 3) printk ("i596_rx()\n");
	va_rfd=pa_to_va(lp->va_rx_tail->pa_next);
	do {
		if(va_rfd->stat && !(va_rfd->stat&(STAT_C|STAT_B))) {
			printk("SF:%p-%04x\n",va_rfd,va_rfd->stat);
		}
		if(va_rfd->stat & STAT_C) {
			if (i596_debug &LOG_SRCDST) print_eth(va_rfd->data);

			if (va_rfd->stat & STAT_OK) {
				/* a good frame */
				int pkt_len = va_rfd->count & 0x3fff;
				struct sk_buff *skb = dev_alloc_skb(pkt_len);

				frames++;

				if(va_rfd->cmd &CMD_EOL ) {
					printk("Received on EOL\n");
				}
				if (skb == NULL) {
					printk ("%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
					lp->stats.rx_dropped++;
					break;
				}

				skb->dev = dev;		
				memcpy(skb_put(skb,pkt_len), va_rfd->data, pkt_len);

				skb->protocol=eth_type_trans(skb,dev);
				netif_rx(skb);
				lp->stats.rx_packets++;

				if (i596_debug &LOG_SRCDST) print_eth(skb->data);
			} else {
				printk("Frame reception error status %04x\n",va_rfd->stat);
				lp->stats.rx_errors++;
				if ((va_rfd->stat)& 0x0001) lp->stats.collisions++;
				if ((va_rfd->stat)& 0x0080) lp->stats.rx_length_errors++;
				if ((va_rfd->stat)& 0x0100) lp->stats.rx_over_errors++;
				if ((va_rfd->stat)& 0x0200) lp->stats.rx_fifo_errors++;
				if ((va_rfd->stat)& 0x0400) lp->stats.rx_frame_errors++;
				if ((va_rfd->stat)& 0x0800) lp->stats.rx_crc_errors++;
				if ((va_rfd->stat)& 0x1000) lp->stats.rx_length_errors++;
			}
			va_rfd->count=va_rfd->cmd=va_rfd->stat=0;
		}
		va_rfd = pa_to_va(va_rfd->pa_next);
	} while(va_rfd!=pa_to_va(lp->va_rx_tail->pa_next));
	if(i596_debug) printk ("frames/irq %d\n", frames);
	/* lets be naughty: we do not CMD_EOL...  if(frames) { struct i596_rfd *va_nrfd; va_nrfd=NULL; va_rfd=lp->rx_tail->pa_next; while(!(rfd->cmd&CMD_EOL)&&!(rfd->stat)) { nrfd=rfd; rfd=rfd->next; } if(nrfd!=I596_NULL) { nrfd->cmd=CMD_EOL; } } lp->scb.rfd=rfd; */

	if (i596_debug > 3) printk ("frames %d\n", frames);

	return frames;
}

static inline void i596_cleanup_cmd(volatile struct i596_private *lp) {
	struct i596_cmd *va_cmd;
	int boguscnt = 100;

	if (i596_debug > 4) printk ("i596_cleanup_cmd\n");

	while (lp->va_cmd_head) {
		va_cmd = (struct i596_cmd *)lp->va_cmd_head;

		lp->va_cmd_head = pa_to_va(lp->va_cmd_head->pa_next);
		lp->cmd_backlog--;

		switch ((va_cmd->command) & 0x7) {
			case CmdTx: {
				struct tx_cmd *va_tx_cmd = (struct tx_cmd *) va_cmd;
				struct i596_tbd * va_tx_cmd_tbd;
				va_tx_cmd_tbd=pa_to_va(va_tx_cmd->pa_tbd);

				dev_kfree_skb(va_tx_cmd_tbd->va_skb);

				lp->stats.tx_errors++;
				lp->stats.tx_aborted_errors++;

				va_cmd->pa_next = I596_NULL;
				kfree((unsigned char *)va_tx_cmd);
				break;
			}
			case CmdMulticastList: {
				// unsigned short count = *((unsigned short *) (ptr + 1));

				va_cmd->pa_next = I596_NULL;
				kfree((unsigned char *)va_cmd);
				break;
			}
			default: {
				va_cmd->pa_next = I596_NULL;
				break;
			}
		}
	}
	while (lp->scb.status, lp->scb.command) {
		if (--boguscnt == 0) {
			printk("i596_cleanup_cmd timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	}
	lp->scb.pa_cmd = va_to_pa(lp->va_cmd_head);
}

static inline void i596_reset(struct device *dev, volatile struct i596_private *lp, int ioaddr) {
	int boguscnt = 100;
	if (i596_debug > 4) printk ("i596_reset\n");
	while (lp->scb.status, lp->scb.command) {
		if (--boguscnt == 0) {
			printk("i596_reset timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	}
	if(boguscnt) {
		printk(
			"WARNING:Using incredible hack for driver bug.\n"
			"Fortune teller says:Use this moment wisely, and shutdown now.\n"
		);
		free_irq(dev->irq, dev);
		request_irq(dev->irq, &i596_interrupt, SA_SHIRQ, dev->name, dev);
	}

	dev->start = 0;
	dev->tbusy = 1;

	lp->scb.command = CUC_ABORT|RX_ABORT;
	CA();

	/* wait for shutdown */
	boguscnt = 400;

	while ((lp->scb.status, lp->scb.command) || lp->scb.command) {
		if (--boguscnt == 0) {
			printk("i596_reset 2 timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	}

	i596_cleanup_cmd(lp);
	i596_rx(dev);

	dev->start = 1;
	dev->tbusy = 0;
	/*dev_kfree_skb(skb, FREE_WRITE);*/
	dev->interrupt = 0;
	init_i596_mem(dev);
}

static void i596_add_cmd(struct device *dev, struct i596_cmd *va_cmd) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	int ioaddr = dev->base_addr;
	unsigned long flags;
	int boguscnt = 100;

	if (i596_debug > 4) printk ("i596_add_cmd\n");

	va_cmd->status = 0;
	BGP
	va_cmd->command |= (CMD_EOL|CMD_INTR);
	va_cmd->pa_next = I596_NULL;
	BGP

	save_flags(flags);
	BGP
	cli();
	BGP
	if (lp->va_cmd_head) {
		lp->va_cmd_tail->pa_next = va_to_pa(va_cmd);
	BGP
	} else {
	BGP
		lp->va_cmd_head = va_cmd;
		while (lp->scb.status, lp->scb.command) {
			if (--boguscnt == 0) {
				printk("i596_add_cmd timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command);
				break;
			}
			udelay(5);
		}
	BGP
		lp->scb.pa_cmd = va_to_pa(va_cmd);
		lp->scb.command = CUC_START;
		CA();
	}
	lp->va_cmd_tail = va_cmd;
	lp->cmd_backlog++;

	lp->va_cmd_head = pa_to_va(lp->scb.pa_cmd);
	BGP
	restore_flags(flags);
	BGP

	if (lp->cmd_backlog > 16) {
		int tickssofar = jiffies - lp->last_cmd;
		if (tickssofar < 25) return;

		printk("%s: command unit timed out, status resetting.\n", dev->name);
	BGP
		i596_reset(dev, lp, ioaddr);
	BGP
	}
	BGP
}

static int i596_open(struct device *dev) {
	int i;
	if (i596_debug > 1) {
		printk("%s: i596_open() irq %d.\n", dev->name, dev->irq);
	}

//	#if (request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev)) {
//		#return -EAGAIN;
//	#}
	if (request_irq(dev->irq, &i596_interrupt, SA_SHIRQ, dev->name, dev)) {
		return -EAGAIN;
	}

//	#irq2dev_map[dev->irq] = dev;

	i = init_rx_bufs(dev, RX_RING_SIZE);

	if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE) {
		printk("%s: only able to allocate %d receive buffers\n", dev->name, i);
	}

	if (i < 4) {
		free_irq(dev->irq, dev);
//		irq2dev_map[dev->irq] = 0;
		return -EAGAIN;
	}

	dev->tbusy = 0;
	dev->interrupt = 0;
	dev->start = 1;
	MOD_INC_USE_COUNT;

	/* Initialize the 82596 memory */
	init_i596_mem(dev);

	return 0;			/* Always succeed */
}

static int i596_start_xmit(struct sk_buff *skb, struct device *dev) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	int ioaddr = dev->base_addr;
	struct tx_cmd *va_tx_cmd;

	if (i596_debug > 2) printk ("%s: elp486 start xmit\n", dev->name);

	/* Transmitter timeout, serious problems. */
	if (dev->tbusy) {
		int tickssofar = jiffies - dev->trans_start;
		if (tickssofar < 5) {
			return 1;
		}
		printk("%s: transmit timed out, status resetting.\n", dev->name);
		lp->stats.tx_errors++;
		/* Try to restart the adaptor */
		if (lp->last_restart == lp->stats.tx_packets) {
			if (i596_debug > 1) printk ("Resetting board.\n");

			/* Shutdown and restart */
			i596_reset(dev,lp, ioaddr);
		} else {
			/* Issue a channel attention signal */
			if (i596_debug > 1) printk ("Kicking board.\n");

			lp->scb.command = CUC_START|RX_START;
			CA(); /*outw(0, ioaddr+4);*/

			lp->last_restart = lp->stats.tx_packets;
		}
		dev->tbusy = 0;
		dev->trans_start = jiffies;
	}

	/* If some higher level thinks we've misses a tx-done interrupt
	we are passed NULL. n.b. dev_tint handles the cli()/sti()
	itself. */
	if (skb == NULL) {
		printk("What about dev_tint\n");
		/*dev_tint(dev);*/
		return 0;
	}

	/* shouldn't happen */
	if (skb->len <= 0) return 0;

	if (i596_debug > 3) printk("%s: i596_start_xmit() called\n", dev->name);

	/* Block a timer-based transmit from overlapping.  This could better be
	done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
	if (test_and_set_bit(0, (void*)&dev->tbusy) != 0) {
		printk("%s: Transmitter access conflict.\n", dev->name);
	} else {
		short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
		dev->trans_start = jiffies;

		va_tx_cmd = (struct tx_cmd *) kmalloc ((sizeof (struct tx_cmd) + sizeof (struct i596_tbd)), GFP_ATOMIC);
		if (va_tx_cmd == NULL) {
			printk ("%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
			lp->stats.tx_dropped++;

			dev_kfree_skb(skb);
		} else {
			struct i596_tbd * va_tx_cmd_tbd;
			va_tx_cmd_tbd=(struct i596_tbd *) (va_tx_cmd+1);
			va_tx_cmd->pa_tbd = va_to_pa(va_tx_cmd_tbd);
			va_tx_cmd_tbd->pa_next = I596_NULL;

			va_tx_cmd->cmd.command = CMD_FLEX|CmdTx;

			va_tx_cmd->pad = 0;
			va_tx_cmd->size = 0;
			va_tx_cmd_tbd->pad = 0;
			va_tx_cmd_tbd->size = EOF | length;

			va_tx_cmd_tbd->pa_data = va_to_pa(skb->data);
			va_tx_cmd_tbd->va_skb=skb;

			if (i596_debug & LOG_SRCDST) print_eth(skb->data);

			i596_add_cmd(dev, (struct i596_cmd *)va_tx_cmd);

			lp->stats.tx_packets++;
		}
	}

	dev->tbusy = 0;

	return 0;
}


static void print_eth(char *add) {
	int i;

	printk ("Dest  ");
	for (i = 0; i < 6; i++) {
		printk(" %2.2X", (unsigned char)add[i]);
	}
	printk ("\n");

	printk ("Source");
	for (i = 0; i < 6; i++) {
		printk(" %2.2X", (unsigned char)add[i+6]);
	}
	printk ("\n");
	printk ("type %2.2X%2.2X\n", (unsigned char)add[12], (unsigned char)add[13]);
}

int elp486_probe(struct device *dev) {
	int i;
	volatile struct i596_private *lp;
	// int checksum = 0;
	int ioaddr = 0xcb0;
	// char *bios;
	char eth_addr[6]={0x00,0xaa,0x00,0x06,0x36,0x9e};
	/*
	char eth_addr[6]={0x00,0x00,0x00,0x00,0x00,0x00};
	*/

	/* this is easy the ethernet interface can only be at 0x300 */
	/* first check nothing is already registered here */

	if (check_region(ioaddr, ELP486_TOTAL_SIZE)) {
		return ENODEV;
	}
	request_region(ioaddr, ELP486_TOTAL_SIZE, "elp486");

	dev->base_addr = ioaddr;
	ether_setup(dev);
	printk("%s: elp486 82596 at %#3x,", dev->name, ioaddr);

/*
	bios=0xe8337;
	for (i = 0; i < 6; i++) printk(" %2.2X", dev->dev_addr[i] = bios[i]);
*/
	for (i = 0; i < 6; i++) printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);

	dev->base_addr = ioaddr;
	dev->irq = 10;
	printk(" IRQ %d.\n", dev->irq);
	printk("%s: Please use ifconfig to change hardware address\n",dev->name);
	printk("%s: ifconfig eth0 hw ether a1:a2:a3:a4:a5:a6\n",dev->name);
	printk("%s: before using it.\n",dev->name);
	printk("%s: a[1-6] can be found in the\n",dev->name);
	printk("%s: setup menu->info of the bios\n",dev->name);

	if (i596_debug > 0) printk(version);

	/* The ELP486-specific entries in the device structure. */
	dev->open = &i596_open;
	dev->stop = &i596_close;
	dev->hard_start_xmit = &i596_start_xmit;
	dev->get_stats = &i596_get_stats;
	dev->set_multicast_list = &set_multicast_list;

	dev->mem_start = (int)kmalloc(sizeof(struct i596_private)+ 0x0f, GFP_KERNEL);
	/* align for scp */
	dev->priv = (void *)((dev->mem_start + 0xf) & 0xfffffff0);

	lp = (struct i596_private *)dev->priv;
	memset((void *)lp, 0, sizeof(struct i596_private));
	lp->scb.command = 0;
	lp->scb.pa_cmd = I596_NULL;
	lp->scb.pa_rfd = I596_NULL;

	return 0;
}

static void i596_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
{
	struct device *dev = (struct device *)dev_instance;
	volatile struct i596_private *lp;
	short ioaddr;
	int boguscnt = 200;
	unsigned short status, ack_cmd = 0;
	int frames_out=0,frames_in=0, commands_done=0;

	if (dev == NULL) {
		printk ("i596_interrupt(): irq %d for unknown device.\n", irq);
		return;
	}

	if (i596_debug &LOG_STARTINT) printk ("%s: i596_interrupt(): irq %d\n",dev->name, irq);

	if (dev->interrupt) printk("%s: Re-entering the interrupt handler.\n", dev->name);

	dev->interrupt = 1;

	ioaddr = dev->base_addr;

	// printk("elp486 IRQ\n");
	lp = (struct i596_private *)dev->priv;

	for(boguscnt=200;/*lp->scb.status,*/ boguscnt && lp->scb.command;boguscnt--) {
		udelay(1);
	}
	if (boguscnt) {
		if(boguscnt!=200) {
			printk("%s: Bleeh, system reacted in %d loops\n",dev->name,boguscnt);
		}
	} else {
		printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
	}
	status = lp->scb.status;

	if (i596_debug > 4) printk("%s: i596 interrupt, status %4.4x.\n", dev->name, status);

	/*ack_cmd = status & 0xf000;*/
	ack_cmd = status & 0x3000;

	if ((status & 0x8000) || (status & 0x2000)) {
		volatile struct i596_cmd *va_cmd;

		if ((i596_debug > 4) && (status & 0x8000)) printk("%s: i596 interrupt completed command.\n", dev->name);
		if ((i596_debug > 4) && (status & 0x2000)) printk("%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700);

		va_cmd=lp->va_cmd_head;
		ack_cmd|=ACK_CX;
		while (lp->va_cmd_head && (lp->va_cmd_head->status & STAT_C)) {
			va_cmd = lp->va_cmd_head;

			lp->va_cmd_head = pa_to_va(lp->va_cmd_head->pa_next);
			lp->cmd_backlog--;

			commands_done++;
			// printk("finished command %d\n",va_cmd->command&0x7);
			switch ((va_cmd->command) & 0x7) {
				case CmdTx: {
					struct tx_cmd *va_tx_cmd;
					struct i596_tbd * va_tx_cmd_tbd;

					va_tx_cmd = (struct tx_cmd *) va_cmd;
					va_tx_cmd_tbd=pa_to_va(va_tx_cmd->pa_tbd);


					frames_out++;
					if ((va_cmd->status) & STAT_OK) {
						if (i596_debug &LOG_SRCDST) print_eth(pa_to_va(va_tx_cmd_tbd->pa_data));
						BUG("Hiero");
						/*printk("%s@%d: skb=%p,data=%08x\n",__FILE__,__LINE__,skb,skb->data);*/
					} else {
						lp->stats.tx_errors++;
						/*
						printk("transmission failure:%04x\n",va_cmd->status);
						*/
						if ((va_cmd->status) & 0x0020) lp->stats.collisions++;
						if (!((va_cmd->status) & 0x0040)) lp->stats.tx_heartbeat_errors++;
						if ((va_cmd->status) & 0x0400) lp->stats.tx_carrier_errors++;
						if ((va_cmd->status) & 0x0800) lp->stats.collisions++;
						if ((va_cmd->status) & 0x1000) lp->stats.tx_aborted_errors++;
					}
					dev_kfree_skb(va_tx_cmd_tbd->va_skb);


					va_cmd->pa_next = I596_NULL;
					kfree((unsigned char *)va_tx_cmd);
					break;
				}
				case CmdMulticastList: {
					va_cmd->pa_next = I596_NULL;
					kfree((unsigned char *)va_cmd);
					break;
				}
				case CmdTDR: {
					unsigned long status = *((unsigned long *) (va_cmd + 1));

					if (status & 0x8000) {
						/*if (i596_debug > 3)*/ printk("%s: link ok.\n", dev->name);
					} else {
						if (status & 0x4000) printk("%s: Transceiver problem.\n", dev->name);
						if (status & 0x2000) printk("%s: Termination problem.\n", dev->name);
						if (status & 0x1000) printk("%s: Short circuit.\n", dev->name);
						printk("%s: Time %ld.\n", dev->name, status & 0x07ff);
					}
				}
				default: {
					va_cmd->pa_next = I596_NULL;
					lp->last_cmd = jiffies;
				}
			}
		}

		va_cmd = lp->va_cmd_head;
		while (va_cmd && (va_cmd != lp->va_cmd_tail)) {
			va_cmd->command &= 0x1fff;
			va_cmd = pa_to_va(va_cmd->pa_next);
		}

		if (lp->va_cmd_head && dev->start) ack_cmd |= CUC_START;
		lp->scb.pa_cmd = va_to_pa(lp->va_cmd_head);
	}

	if ((status & 0x1000) || (status & 0x4000)) {
		if ((i596_debug > 4) && (status & 0x4000)) printk("%s: i596 interrupt received a frame.\n", dev->name);
		if ((i596_debug > 4) && (status & 0x1000)) printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x0070);
		if ((status & 0x1000)) printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x0070);
		/*frames_in=i596_rx(dev);*/
		/* Restart the receive unit when it got inactive somehow */
		if ((status&0x1000)&& dev->start) ack_cmd |= RX_START;
	}
	// frames_in=0; //BUG
	frames_in=i596_rx(dev);
	if(frames_in)ack_cmd|=ACK_FR;
	if(status &0x4000 ) {
		if(!frames_in) {
			/*printk("receive frame reported, but no frames there\n");*/
		}
	} else {
		if(frames_in) {
			/*printk("frames received but not reported\n");*/
		}
	}
	

	/* acknowledge the interrupt */
	/*
	if ((lp->scb.pa_cmd != I596_NULL) && (dev->start)) ack_cmd | = CUC_START;
	*/
	boguscnt = 100;
	while (/*lp->scb.status,*/ lp->scb.command) {
		if (--boguscnt == 0) {
			printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	}
	lp->scb.command = ack_cmd;

	CLEAR_INT();
	CA();

	if (i596_debug > 4) printk("%s: exiting interrupt.\n", dev->name);
#ifdef REPORT_IN_INTERRUPT
	if(frames_in||frames_out||commands_done) {
		if(i596_debug) printk("reported: %d in, %d out, %d commands\n",frames_in,frames_out,commands_done);
	} else {
		if(i596_debug) printk("No completions reported\n");
	}
#endif
	dev->interrupt = 0;
	return;
}

static int i596_close(struct device *dev) {
	/*int ioaddr = dev->base_addr;*/
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	int boguscnt = 200;

	dev->start = 0;
	dev->tbusy = 1;

	if (i596_debug > 1) printk("%s: Shutting down ethercard, status was %4.4x.\n",
dev->name, lp->scb.status);

	// return 0;//BUG
	lp->scb.command = CUC_ABORT|RX_ABORT;
	CA(); /*outw(0, ioaddr+4);*/

	i596_cleanup_cmd(lp);

	while (/*lp->scb.status,*/ lp->scb.command) {
		if (--boguscnt == 0) {
			printk("%s: close timed timed out with status %4.4x, cmd %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
			break;
		}
		udelay(5);
	}
	free_irq(dev->irq, dev);
	remove_rx_bufs(dev);
	MOD_DEC_USE_COUNT;

	return 0;
}

static struct enet_statistics * i596_get_stats(struct device *dev) {
	struct i596_private *lp = (struct i596_private *)dev->priv;

	return &lp->stats;
}

/*
*	Set or clear the multicast filter for this adaptor.
*/

static void set_multicast_list(struct device *dev) {
	volatile struct i596_private *lp = (struct i596_private *)dev->priv;
	struct i596_cmd *va_cmd;

	if (i596_debug > 1) printk ("%s: set multicast list %d\n", dev->name, dev->mc_count);

	if (dev->mc_count > 0) {
		struct dev_mc_list *dmi;
		char *cp;
		va_cmd = (struct i596_cmd *) kmalloc(sizeof(struct i596_cmd)+2+dev->mc_count*6, GFP_ATOMIC);
		if (va_cmd == NULL) {
			printk ("%s: set_multicast Memory squeeze.\n", dev->name);
			return;
		}
		va_cmd->command = CmdMulticastList;
		*((unsigned short *) (va_cmd + 1)) = dev->mc_count * 6;
		cp=((char *)(va_cmd + 1))+2;
		for(dmi=dev->mc_list;dmi!=NULL;dmi=dmi->next) {
			memcpy(cp, dmi,6);
			cp+=6;
		}
		if(i596_debug & LOG_SRCDST) print_eth (((char *)(va_cmd + 1)) + 2);
		i596_add_cmd(dev, va_cmd);
	} else {
		if (lp->set_conf.pa_next != I596_NULL) {
			return;
		}
		if (dev->mc_count == 0 && !(dev->flags&(IFF_PROMISC|IFF_ALLMULTI))) {
			if(dev->flags&IFF_ALLMULTI) {
				dev->flags|=IFF_PROMISC;
			}
			lp->i596_config[8] &= ~0x01;
		} else {
			lp->i596_config[8] |= 0x01;
		}

		i596_add_cmd(dev, (struct i596_cmd *) &lp->set_conf);
	}
}

#ifdef HAVE_DEVLIST
static unsigned int elp486_portlist[] = {0xcb0, 0};
struct netdev_entry elp486_drv = {
	"elp486",
	elp486_probe,
	ELP486_TOTAL_SIZE,
	elp486_portlist
};
#endif

#ifdef MODULE
MODULE_AUTHOR("Ard van Breemen <ard@cstmel.nl.eu.org>");
MODULE_DESCRIPTION("Intel Panther onboard i82596 driver");
MODULE_PARM(debug, "i");
//MODULE_PARM(max_interrupt_work, "i");
//MODULE_PARM(reverse_probe, "i");
//MODULE_PARM(rx_copybreak, "i");
MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");

static char devicename[9] = { 0, };
static struct device dev_elp486 = {
	devicename, /* device name inserted by /linux/drivers/net/net_init.c */
	0, 0, 0, 0,
	0xcb0, 10,
	0, 0, 0, NULL, NULL
};

static int full_duplex;
static int options;
static int io = 0xcb0;
static int irq = 10;

int init_module(void) {
	struct device *dev = &dev_elp486;
	dev->name = devicename;
	dev->irq = irq;
	dev->base_addr = io;
	dev->init = elp486_probe;
	if (register_netdev(dev) != 0) {
		return -EIO;
	}
	full_duplex=0;
	options=0;
	return 0;
}

void cleanup_module(void) {
	unregister_netdev(&dev_elp486);
	kfree((void *)dev_elp486.mem_start);
	dev_elp486.priv = NULL;
	/* If we don't do this, we can't re-insmod it later. */
	release_region(dev_elp486.base_addr, ELP486_TOTAL_SIZE);
}
#endif /* MODULE */

/*
* Local variables:
*  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c elp486.c"
* End:
*/

