/* 8390.c: A general NS8390 ethernet driver core for linux. */
/*
  Written 1992,1993 by Donald Becker.
  
  Copyright 1993 United States Government as represented by the
  Director, National Security Agency.	 This software may be used and
  distributed according to the terms of the GNU Public License,
  incorporated herein by reference.
  
  This is the chip-specific code for many 8390-based ethernet adaptors.
  
  The Author may be reached as becker@super.org or
  C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  */

static char *version =
    "8390.c:v0.99-13f 10/18/93 Donald Becker (becker@super.org)\n";
#include <linux/config.h>

/*
  Braindamage remaining:
  
  Ethernet devices should use a chr_drv device interface, with ioctl()s to
  configure the card, bring the interface up or down, allow access to
  statistics, and maybe read() and write() access to raw packets.
  This won't be done until after Linux 1.00.
  
  Sources:
  The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
  The NE* programming info came from the Crynwr packet driver, and figuring
  out that the those boards are similar to the NatSemi evaluation board
  described in AN-729.	Thanks NS, no thanks to Novell/Eagle.
  */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/tty.h>
#include <linux/types.h>
#include <linux/ptrace.h>
#include <linux/string.h>
#include <asm/system.h>
#include <asm/segment.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <errno.h>
#include <linux/fcntl.h>
#include <linux/in.h>
#include <linux/interrupt.h>

#include "dev.h"
#include "eth.h"
#include "ip.h"
#include "protocol.h"
#include "tcp.h"
#include "skbuff.h"
#include "sock.h"
#include "arp.h"

#include "8390.h"

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

#define ei_reset_8390 (ei_local->reset_8390)
#define ei_block_output (ei_local->block_output)
#define ei_block_input (ei_local->block_input)

/* use 0 for production, 1 for verification, >2 for debug */
#ifdef EI_DEBUG
int ei_debug = EI_DEBUG;
#else
int ei_debug = 1;
#endif

/* Max number of packets received at one Intr. */
/*static int high_water_mark = 0;*/

/* Index to functions. */
/* Put in the device structure. */
int ei_open(struct device *dev);
/* Dispatch from interrupts. */
void ei_interrupt(int reg_ptr);
static void ei_tx_intr(struct device *dev);
static void ei_receive(struct device *dev);
static void ei_rx_overrun(struct device *dev);

/* Routines generic to NS8390-based boards. */
void NS8390_init(struct device *dev, int startp);
static void NS8390_trigger_send(struct device *dev, unsigned int length,
								int start_page);
#ifdef HAVE_MULTICAST
static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
#endif

struct sigaction ei_sigaction = { ei_interrupt, 0, 0, NULL, };

/* Open/initialize the board.  This routine goes all-out, setting everything
   up anew at each open, even though many of these registers should only
   need to be set once at boot.
   */
int ei_open(struct device *dev)
{
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
    
    if ( ! ei_local) {
		printk("%s: Opening a non-existent physical device\n", dev->name);
		return 1;		/* ENXIO would be more accurate. */
    }
    
    irq2dev_map[dev->irq] = dev;
    NS8390_init(dev, 1);
    ei_local->tx1 = ei_local->tx2 = 0;
    /* The old local flags... */
    ei_local->txing = 0;
    /* ... are now global. */
    dev->tbusy = 0;
    dev->interrupt = 0;
    dev->start = 1;
    ei_local->irqlock = 0;
    return 0;
}

static int ei_start_xmit(struct sk_buff *skb, struct device *dev)
{
    int e8390_base = dev->base_addr;
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
    int length, send_length;
    int tmp_tbusy;	  /* we must lock dev_tint in dev.c with dev->t_busy =1 */
    /* because on a slow pc a quasi endless loop can appear */
    
    if (dev->tbusy) {	/* Do timeouts, just like the 8003 driver. */
		int txsr = inb(e8390_base+EN0_TSR), isr;
		int tickssofar = jiffies - dev->trans_start;
		if (tickssofar < 5	||	(tickssofar < 15 && ! (txsr & ENTSR_PTX))) {
			return 1;
		}
		isr = inb(e8390_base+EN0_ISR);
		printk("%s: transmit timed out, TX status %#2x, ISR %#2x.\n",
			   dev->name, txsr, isr);
		/* It's possible to check for an IRQ conflict here.
		   I may have to do that someday. */
		if (isr)
			printk("%s: Possible IRQ conflict on IRQ%d?", dev->name, dev->irq);
		else
			printk("%s: Possible network cable problem?\n", dev->name);
		/* It futile, but try to restart it anyway. */
		ei_reset_8390(dev);
		NS8390_init(dev, 1);
		printk("\n");
    }
    
    /* This is new: it means some higher layer thinks we've missed an
       tx-done interrupt. Caution: dev_tint() handles the cli()/sti()
       itself. */
    if (skb == NULL) {
		dev_tint(dev);
		return 0;
    }
    /* Fill in the ethernet header. */
    if (!skb->arp  &&  dev->rebuild_header(skb+1, dev)) {
		skb->dev = dev;
		arp_queue (skb);
		return 0;
    }
    skb->arp=1;
    
    if (skb->len <= 0)
		return 0;
    length = skb->len;
    send_length = ETH_ZLEN < length ? length : ETH_ZLEN;
    /* Turn off interrupts so that we can put the packet out safely. */
    cli();
    if (dev->interrupt || ei_local->irqlock) {
		/* We should never get here during an interrupt after 0.99.4. */
		sti();
		if (ei_debug > 2)
			printk("%s: Attempt to reenter critical zone%s.\n",
				   dev->name, ei_local->irqlock ? " during interrupt" : "");
		return 1;
    }
    /* Mask interrupts from the ethercard. */
    outb(0x00,	e8390_base + EN0_IMR);
	
    /* Atomically lock out dev.c:dev_tint(). */
    tmp_tbusy = set_bit(0, (void*)&dev->tbusy);
	
    ei_local->irqlock = 1;
    sti();
    if (ei_local->pingpong) {
		int output_page;
		if (ei_local->tx1 == 0) {
			output_page = ei_local->tx_start_page;
			ei_local->tx1 = send_length;
			if (ei_debug  &&  ei_local->tx2 > 0)
				printk("%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
					   dev->name, ei_local->tx2, ei_local->lasttx,
					   ei_local->txing);
		} else if (ei_local->tx2 == 0) {
			output_page = ei_local->tx_start_page + 6;
			ei_local->tx2 = send_length;
			if (ei_debug  &&  ei_local->tx1 > 0)
				printk("%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
					   dev->name, ei_local->tx1, ei_local->lasttx,
					   ei_local->txing);
		} else {
			/* We can get to here if we get an rx interrupt and queued
			   a tx packet just before masking 8390 irqs above. */
			if (ei_debug > 2)
				printk("%s: No packet buffer space for ping-pong use.\n",
					   dev->name);
			cli();
			ei_local->irqlock = 0;
			dev->tbusy = tmp_tbusy;
			outb_p(ENISR_ALL,  e8390_base + EN0_IMR);
			sti();
			return 1;
		}
		dev->trans_start = jiffies;
		ei_block_output(dev, length, (unsigned char *)(skb+1), output_page);
		if (! ei_local->txing) {
			NS8390_trigger_send(dev, send_length, output_page);
			if (output_page == ei_local->tx_start_page)
				ei_local->tx1 = -1, ei_local->lasttx = -1;
			else
				ei_local->tx2 = -1, ei_local->lasttx = -2;
			ei_local->txing = 1;
		} else
			ei_local->txqueue++;
		if (ei_local->tx1  &&  ei_local->tx2)
			tmp_tbusy = 1;
    } else {
		dev->trans_start = jiffies;
		ei_block_output(dev, length, (unsigned char *)(skb+1),
						ei_local->tx_start_page);
		NS8390_trigger_send(dev, send_length, ei_local->tx_start_page);
		tmp_tbusy = 1;
    }  /* PINGPONG */
    
    if (skb->free)
		kfree_skb (skb, FREE_WRITE);
    
    /* Turn 8390 interrupts back on. */
    cli();
    outb_p(ENISR_ALL,  e8390_base + EN0_IMR);
    ei_local->irqlock = 0;
    dev->tbusy=tmp_tbusy;
    sti();
    return 0;
}

/* The typical workload of the driver:
   Handle the ether interface interrupts. */
void ei_interrupt(int reg_ptr)
{
    int irq = -(((struct pt_regs *)reg_ptr)->orig_eax+2);
    struct device *dev = (struct device *)(irq2dev_map[irq]);
    int e8390_base;
    int interrupts, boguscount = 0;
    struct ei_device *ei_local;
    
    if (dev == NULL) {
		printk ("net_interrupt(): irq %d for unknown device.\n", irq);
		return;
    }
    e8390_base = dev->base_addr;
    ei_local = (struct ei_device *) dev->priv;
    if (dev->interrupt || ei_local->irqlock) {
		/* The "irqlock" check is only for testing. */
		sti();
		printk(ei_local->irqlock
			   ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
			   : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
			   dev->name, inb_p(e8390_base + EN0_ISR),
			   inb_p(e8390_base + EN0_IMR));
		return;
    }
    
    dev->interrupt = 1;
    sti(); /* Allow other interrupts. */
    
    /* Change to page 0 and read the intr status reg. */
    outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
    if (ei_debug > 3)
		printk("%s: interrupt(isr=%#2.2x).\n", dev->name,
			   inb_p(e8390_base + EN0_ISR));
    
    /* !!Assumption!! -- we stay in page 0.	 Don't break this. */
    while ((interrupts = inb_p(e8390_base + EN0_ISR)) != 0
		   && ++boguscount < 20) {
		if (interrupts & ENISR_RDC) {
			/* Ack meaningless DMA complete. */
			outb_p(ENISR_RDC, e8390_base + EN0_ISR);
		}
		if (interrupts & ENISR_OVER) {
			ei_rx_overrun(dev);
		} else if (interrupts & (ENISR_RX+ENISR_RX_ERR)) {
			/* Got a good (?) packet. */
			ei_receive(dev);
		}
		/* Push the next to-transmit packet through. */
		if (interrupts & ENISR_TX) {
			ei_tx_intr(dev);
		} else if (interrupts & ENISR_COUNTERS) {
			struct ei_device *ei_local = (struct ei_device *) dev->priv;
			ei_local->stat.rx_frame_errors += inb_p(e8390_base + EN0_COUNTER0);
			ei_local->stat.rx_crc_errors   += inb_p(e8390_base + EN0_COUNTER1);
			ei_local->stat.rx_missed_errors+= inb_p(e8390_base + EN0_COUNTER2);
			outb_p(ENISR_COUNTERS, e8390_base + EN0_ISR); /* Ack intr. */
		}
		
		/* Ignore the transmit errs and reset intr for now. */
		if (interrupts & ENISR_TX_ERR) {
			outb_p(ENISR_TX_ERR, e8390_base + EN0_ISR); /* Ack intr. */
		}
		outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
    }
    
    if (interrupts && ei_debug) {
		printk("%s: unknown interrupt %#2x\n", dev->name, interrupts);
		outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base + E8390_CMD);
		outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
    }
    dev->interrupt = 0;
    return;
}

/* We have finished a transmit: check for errors and then trigger the next
   packet to be sent. */
static void ei_tx_intr(struct device *dev)
{
    int e8390_base = dev->base_addr;
    int status = inb(e8390_base + EN0_TSR);
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
    
    outb_p(ENISR_TX, e8390_base + EN0_ISR); /* Ack intr. */
    
    if (ei_local->pingpong) {
		ei_local->txqueue--;
		if (ei_local->tx1 < 0) {
			if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
				printk("%s: bogus last_tx_buffer %d, tx1=%d.\n",
					   ei_local->name, ei_local->lasttx, ei_local->tx1);
			ei_local->tx1 = 0;
			dev->tbusy = 0;
			if (ei_local->tx2 > 0) {
				NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
				dev->trans_start = jiffies;
				ei_local->txing = 1;
				ei_local->tx2 = -1,
				ei_local->lasttx = 2;
			} else
				ei_local->lasttx = 20, ei_local->txing = 0;
		} else if (ei_local->tx2 < 0) {
			if (ei_local->lasttx != 2  &&  ei_local->lasttx != -2)
				printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
					   ei_local->name, ei_local->lasttx, ei_local->tx2);
			ei_local->tx2 = 0;
			dev->tbusy = 0;
			if (ei_local->tx1 > 0) {
				NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
				dev->trans_start = jiffies;
				ei_local->txing = 1;
				ei_local->tx1 = -1;
				ei_local->lasttx = 1;
			} else
				ei_local->lasttx = 10, ei_local->txing = 0;
		} else
			printk("%s: unexpected TX-done interrupt, lasttx=%d.\n",
				   dev->name, ei_local->lasttx);
    } else {
		ei_local->txing = 0;
		dev->tbusy = 0;
    }
    
    /* Do the statistics _after_ we start the next TX. */
    if (status & ENTSR_PTX)
		ei_local->stat.tx_packets++;
    else
		ei_local->stat.tx_errors++;
    if (status & ENTSR_COL)
		ei_local->stat.collisions++;
    if (status & ENTSR_ABT)
		ei_local->stat.tx_aborted_errors++;
    if (status & ENTSR_CRS)
		ei_local->stat.tx_carrier_errors++;
    if (status & ENTSR_FU)
		ei_local->stat.tx_fifo_errors++;
    if (status & ENTSR_CDH)
		ei_local->stat.tx_heartbeat_errors++;
    if (status & ENTSR_OWC)
		ei_local->stat.tx_window_errors++;
    
    mark_bh (INET_BH);
}

/* We have a good packet(s), get it/them out of the buffers. */

static void ei_receive(struct device *dev)
{
    int e8390_base = dev->base_addr;
    struct ei_device *ei_local = (struct ei_device *) dev->priv;
    int rxing_page, this_frame, next_frame, current_offset;
    int boguscount = 0;
    struct e8390_pkt_hdr rx_frame;
    int num_rx_pages = ei_local->stop_page-ei_local->rx_start_page;
    
    while (++boguscount < 10) {
		int pkt_len;
		
		/* Get the rx page (incoming packet pointer). */
		outb_p(E8390_NODMA+E8390_PAGE1, e8390_base + E8390_CMD);
		rxing_page = inb_p(e8390_base + EN1_CURPAG);
		outb_p(E8390_NODMA+E8390_PAGE0, e8390_base + E8390_CMD);
		
		/* Remove one frame from the ring.  Boundary is alway a page behind. */
		this_frame = inb_p(e8390_base + EN0_BOUNDARY) + 1;
		if (this_frame >= ei_local->stop_page)
			this_frame = ei_local->rx_start_page;
		
		/* Someday we'll omit the previous step, iff we never get this message.*/
		if (ei_debug > 0	&&	this_frame != ei_local->current_page)
			printk("%s: mismatched read page pointers %2x vs %2x.\n",
				   dev->name, this_frame, ei_local->current_page);
		
		if (this_frame == rxing_page)	/* Read all the frames? */
			break;				/* Done for now */
		
		current_offset = this_frame << 8;
		ei_block_input(dev, sizeof(rx_frame), (char *)&rx_frame,
					   current_offset);
		
		pkt_len = rx_frame.count - sizeof(rx_frame);
		
		next_frame = this_frame + 1 + ((pkt_len+4)>>8);
		
		/* Check for bogosity warned by 3c503 book: the status byte is never
		   written.  This happened a lot during testing! This code should be
		   cleaned up someday. */
		if (	 rx_frame.next != next_frame
			&& rx_frame.next != next_frame + 1
			&& rx_frame.next != next_frame - num_rx_pages
			&& rx_frame.next != next_frame + 1 - num_rx_pages) {
#ifndef EI_DEBUG
			ei_local->current_page = rxing_page;
			outb(ei_local->current_page-1, e8390_base+EN0_BOUNDARY);
			continue;
#else
			static int last_rx_bogosity = -1;
			printk("%s: bogus packet header, status=%#2x nxpg=%#2x sz=%#x (at %#4x)\n",
				   dev->name, rx_frame.status, rx_frame.next, rx_frame.count,
				   current_offset);
			
			if (ei_local->stat.rx_packets != last_rx_bogosity) {
				/* Maybe we can avoid resetting the chip... empty the packet ring. */
				ei_local->current_page = rxing_page;
				printk("%s:	setting next frame to %#2x (nxt=%#2x, rx_frm.nx=%#2x rx_frm.stat=%#2x).\n",
					   dev->name, ei_local->current_page, next_frame,
					   rx_frame.next, rx_frame.status);
				last_rx_bogosity = ei_local->stat.rx_packets;
				outb(ei_local->current_page-1, e8390_base+EN0_BOUNDARY);
				continue;
			} else {
				/* Oh no Mr Bill! Last ditch error recovery. */
				printk("%s: recovery failed, resetting at packet #%d..",
					   dev->