📄 apricot.c
字号:
/* apricot.c: An Apricot 82596 ethernet driver for linux. *//* 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 = "apricot.c:v0.2 05/12/94\n";#include <linux/module.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>#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#define APRICOT_DEBUG 1#ifdef APRICOT_DEBUGint i596_debug = APRICOT_DEBUG;#elseint i596_debug = 1;#endif#define APRICOT_TOTAL_SIZE 17#define I596_NULL -1#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 0x0040struct i596_cmd { unsigned short status; unsigned short command; struct i596_cmd *next;};#define EOF 0x8000#define SIZE_MASK 0x3fffstruct i596_tbd { unsigned short size; unsigned short pad; struct i596_tbd *next; char *data;};struct tx_cmd { struct i596_cmd cmd; struct i596_tbd *tbd; unsigned short size; unsigned short pad;};struct i596_rfd { unsigned short stat; unsigned short cmd; struct i596_rfd *next; long rbd; unsigned short count; unsigned short size; char data[1532];};#define RX_RING_SIZE 8struct i596_scb { unsigned short status; unsigned short command; struct i596_cmd *cmd; struct i596_rfd *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;};struct i596_scp { unsigned long sysbus; unsigned long pad; struct i596_iscp *iscp;};struct i596_private { volatile struct i596_scp scp; volatile struct i596_iscp iscp; volatile struct i596_scb scb; volatile struct i596_cmd set_add; char eth_addr[8]; volatile struct i596_cmd set_conf; char i596_config[16]; volatile struct i596_cmd tdr; unsigned long stat; int last_restart; struct i596_rfd *rx_tail; struct i596_cmd *cmd_tail; struct i596_cmd *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 */ 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, 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 intinit_rx_bufs(struct device *dev, int num){ struct i596_private *lp = (struct i596_private *)dev->priv; int i; struct i596_rfd *rfd; lp->scb.rfd = (struct i596_rfd *)I596_NULL; if (i596_debug > 1) printk ("%s: init_rx_bufs %d.\n", dev->name, num); for (i = 0; i < num; i++) { if (!(rfd = (struct i596_rfd *)kmalloc(sizeof(struct i596_rfd), GFP_KERNEL))) break; rfd->stat = 0x0000; rfd->rbd = I596_NULL; rfd->count = 0; rfd->size = 1532; if (i == 0) { rfd->cmd = CMD_EOL; lp->rx_tail = rfd; } else rfd->cmd = 0x0000; rfd->next = lp->scb.rfd; lp->scb.rfd = rfd; } if (i != 0) lp->rx_tail->next = lp->scb.rfd; return (i);}static inline voidremove_rx_bufs(struct device *dev){ struct i596_private *lp = (struct i596_private *)dev->priv; struct i596_rfd *rfd = lp->scb.rfd; lp->rx_tail->next = (struct i596_rfd *)I596_NULL; do { lp->scb.rfd = rfd->next; kfree_s(rfd, sizeof(struct i596_rfd)); rfd = lp->scb.rfd; } while (rfd != lp->rx_tail);}static inline voidinit_i596_mem(struct device *dev){ struct i596_private *lp = (struct i596_private *)dev->priv; short ioaddr = dev->base_addr; int boguscnt = 100; /* change the scp address */ outw(0, ioaddr); outw(0, ioaddr); outb(4, ioaddr+0xf); outw(((((int)&lp->scp) & 0xffff) | 2), ioaddr); outw((((int)&lp->scp)>>16) & 0xffff, ioaddr); lp->last_cmd = jiffies; lp->scp.sysbus = 0x00440000; lp->scp.iscp = &(lp->iscp); lp->iscp.scb = &(lp->scb); lp->iscp.stat = 0x0001; lp->cmd_backlog = 0; lp->cmd_head = lp->scb.cmd = (struct i596_cmd *) I596_NULL; if (i596_debug > 2) printk("%s: starting i82596.\n", dev->name); (void) inb (ioaddr+0x10); outb(4, ioaddr+0xf); outw(0, ioaddr+4); 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; } lp->scb.command = 0; memcpy (lp->i596_config, init_setup, 14); lp->set_conf.command = CmdConfigure; i596_add_cmd(dev, &lp->set_conf); memcpy (lp->eth_addr, dev->dev_addr, 6); lp->set_add.command = CmdSASetup; i596_add_cmd(dev, &lp->set_add); lp->tdr.command = CmdTDR; i596_add_cmd(dev, &lp->tdr); 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; } lp->scb.command = RX_START; outw(0, ioaddr+4); boguscnt = 200; while (lp->scb.status, lp->scb.command) if (--boguscnt == 0) { printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n", lp->scb.status, lp->scb.command); break; } return;}static inline inti596_rx(struct device *dev){ struct i596_private *lp = (struct i596_private *)dev->priv; int frames = 0; if (i596_debug > 3) printk ("i596_rx()\n"); while ((lp->scb.rfd->stat) & STAT_C) { if (i596_debug >2) print_eth(lp->scb.rfd->data); if ((lp->scb.rfd->stat) & STAT_OK) { /* a good frame */ int pkt_len = lp->scb.rfd->count & 0x3fff; struct sk_buff *skb = dev_alloc_skb(pkt_len); frames++; 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), lp->scb.rfd->data, pkt_len); skb->protocol=eth_type_trans(skb,dev); netif_rx(skb); lp->stats.rx_packets++; if (i596_debug > 4) print_eth(skb->data); } else { lp->stats.rx_errors++; if ((lp->scb.rfd->stat) & 0x0001) lp->stats.collisions++; if ((lp->scb.rfd->stat) & 0x0080) lp->stats.rx_length_errors++; if ((lp->scb.rfd->stat) & 0x0100) lp->stats.rx_over_errors++; if ((lp->scb.rfd->stat) & 0x0200) lp->stats.rx_fifo_errors++; if ((lp->scb.rfd->stat) & 0x0400) lp->stats.rx_frame_errors++; if ((lp->scb.rfd->stat) & 0x0800) lp->stats.rx_crc_errors++; if ((lp->scb.rfd->stat) & 0x1000) lp->stats.rx_length_errors++; } lp->scb.rfd->stat = 0; lp->rx_tail->cmd = 0; lp->rx_tail = lp->scb.rfd; lp->scb.rfd = lp->scb.rfd->next; lp->rx_tail->count = 0; lp->rx_tail->cmd = CMD_EOL; } if (i596_debug > 3) printk ("frames %d\n", frames); return 0;}static inline voidi596_cleanup_cmd(struct i596_private *lp){ struct i596_cmd *ptr; int boguscnt = 100; if (i596_debug > 4) printk ("i596_cleanup_cmd\n"); while (lp->cmd_head != (struct i596_cmd *) I596_NULL) { ptr = lp->cmd_head; lp->cmd_head = lp->cmd_head->next; lp->cmd_backlog--; switch ((ptr->command) & 0x7) { case CmdTx: { struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr; struct sk_buff *skb = ((struct sk_buff *)(tx_cmd->tbd->data)) -1; dev_kfree_skb(skb, FREE_WRITE); lp->stats.tx_errors++; lp->stats.tx_aborted_errors++; ptr->next = (struct i596_cmd * ) I596_NULL; kfree_s((unsigned char *)tx_cmd, (sizeof (struct tx_cmd) + sizeof (struct i596_tbd))); break; } case CmdMulticastList: { unsigned short count = *((unsigned short *) (ptr + 1)); ptr->next = (struct i596_cmd * ) I596_NULL; kfree_s((unsigned char *)ptr, (sizeof (struct i596_cmd) + count + 2)); break; } default: ptr->next = (struct i596_cmd * ) I596_NULL; } } 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; } lp->scb.cmd = lp->cmd_head;}static inline voidi596_reset(struct device *dev, 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; } dev->start = 0; dev->tbusy = 1; lp->scb.command = CUC_ABORT|RX_ABORT; outw(0, ioaddr+4); /* 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; } i596_cleanup_cmd(lp); i596_rx(dev); dev->start = 1; dev->tbusy = 0; dev->interrupt = 0; init_i596_mem(dev);}static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd){ 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"); cmd->status = 0; cmd->command |= (CMD_EOL|CMD_INTR); cmd->next = (struct i596_cmd *) I596_NULL; save_flags(flags); cli(); if (lp->cmd_head != (struct i596_cmd *) I596_NULL) lp->cmd_tail->next = cmd; else { lp->cmd_head = 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; } lp->scb.cmd = cmd; lp->scb.command = CUC_START; outw (0, ioaddr+4); } lp->cmd_tail = cmd; lp->cmd_backlog++; lp->cmd_head = lp->scb.cmd; restore_flags(flags); if (lp->cmd_backlog > 16) { int tickssofar = jiffies - lp->last_cmd; if (tickssofar < 25) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -