home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / net / apricot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  25.2 KB  |  1,050 lines

  1. /* apricot.c: An Apricot 82596 ethernet driver for linux. */
  2. /*
  3.     Apricot
  4.         Written 1994 by Mark Evans.
  5.     This driver is for the Apricot 82596 bus-master interface
  6.  
  7.         Modularised 12/94 Mark Evans
  8.     
  9.     Driver skeleton 
  10.     Written 1993 by Donald Becker.
  11.     Copyright 1993 United States Government as represented by the Director,
  12.     National Security Agency.  This software may only be used and distributed
  13.     according to the terms of the GNU Public License as modified by SRC,
  14.     incorporated herein by reference.
  15.  
  16.     The author may be reached as becker@super.org or
  17.     C/O Supercomputing Research Ctr., 17100 Science Dr., Bowie MD 20715
  18.     
  19.  
  20. */
  21.  
  22. static char *version = "apricot.c:v0.2 05/12/94\n";
  23.  
  24. #ifdef MODULE
  25. #include <linux/module.h>
  26. #include <linux/version.h>
  27. #endif
  28.  
  29. #include <linux/kernel.h>
  30. #include <linux/sched.h>
  31. #include <linux/string.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/errno.h>
  34. #include <linux/ioport.h>
  35. #include <linux/malloc.h>
  36. #include <linux/interrupt.h>
  37. #include <asm/bitops.h>
  38. #include <asm/io.h>
  39. #include <asm/dma.h>
  40.  
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/skbuff.h>
  44.  
  45. #ifndef HAVE_PORTRESERVE
  46. #define check_region(addr, size)    0
  47. #define request_region(addr, size,name)    do ; while(0)
  48. #endif
  49.  
  50. #ifndef HAVE_ALLOC_SKB
  51. #define alloc_skb(size, priority) (struct sk_buff *) kmalloc(size,priority)
  52. #define kfree_skbmem(buff, size) kfree_s(buff,size)
  53. #endif
  54.  
  55. #define APRICOT_DEBUG 1
  56.  
  57. #ifdef APRICOT_DEBUG
  58. int i596_debug = APRICOT_DEBUG;
  59. #else
  60. int i596_debug = 1;
  61. #endif
  62.  
  63. #define APRICOT_TOTAL_SIZE 17
  64.  
  65. #define I596_NULL -1
  66.  
  67. #define CMD_EOL        0x8000    /* The last command of the list, stop. */
  68. #define CMD_SUSP    0x4000    /* Suspend after doing cmd. */
  69. #define CMD_INTR    0x2000    /* Interrupt after doing cmd. */
  70.  
  71. #define CMD_FLEX    0x0008    /* Enable flexible memory model */
  72.  
  73. enum commands {
  74.     CmdNOp = 0, CmdSASetup = 1, CmdConfigure = 2, CmdMulticastList = 3,
  75.     CmdTx = 4, CmdTDR = 5, CmdDump = 6, CmdDiagnose = 7};
  76.  
  77. #define STAT_C        0x8000    /* Set to 0 after execution */
  78. #define STAT_B        0x4000    /* Command being executed */
  79. #define STAT_OK        0x2000    /* Command executed ok */
  80. #define STAT_A        0x1000    /* Command aborted */
  81.  
  82. #define     CUC_START    0x0100
  83. #define     CUC_RESUME    0x0200
  84. #define     CUC_SUSPEND    0x0300
  85. #define     CUC_ABORT    0x0400
  86. #define     RX_START    0x0010
  87. #define     RX_RESUME    0x0020
  88. #define     RX_SUSPEND    0x0030
  89. #define     RX_ABORT    0x0040
  90.  
  91. struct i596_cmd {
  92.     unsigned short status;
  93.     unsigned short command;
  94.     struct i596_cmd *next;
  95. };
  96.  
  97. #define EOF        0x8000
  98. #define SIZE_MASK    0x3fff
  99.  
  100. struct i596_tbd {
  101.     unsigned short size;
  102.     unsigned short pad;
  103.     struct i596_tbd *next;
  104.     char *data;
  105. };
  106.  
  107. struct tx_cmd {
  108.     struct i596_cmd cmd;
  109.     struct i596_tbd *tbd;
  110.     unsigned short size;
  111.     unsigned short pad;
  112. };
  113.  
  114. struct i596_rfd {
  115.     unsigned short stat;
  116.     unsigned short cmd;
  117.     struct i596_rfd *next;
  118.     long rbd; 
  119.     unsigned short count;
  120.     unsigned short size;
  121.     char data[1532];
  122. };
  123.  
  124. #define RX_RING_SIZE 8
  125.  
  126. struct i596_scb {
  127.     unsigned short status;
  128.     unsigned short command;
  129.     struct i596_cmd *cmd;
  130.     struct i596_rfd *rfd;
  131.     unsigned long crc_err;
  132.     unsigned long align_err;
  133.     unsigned long resource_err;
  134.     unsigned long over_err;
  135.     unsigned long rcvdt_err;
  136.     unsigned long short_err;
  137.     unsigned short t_on;
  138.     unsigned short t_off;
  139. };
  140.  
  141. struct i596_iscp {
  142.     unsigned long stat;
  143.     struct i596_scb *scb;
  144. };
  145.  
  146. struct i596_scp {
  147.     unsigned long sysbus;
  148.     unsigned long pad;
  149.     struct i596_iscp *iscp;
  150. };
  151.  
  152. struct i596_private {
  153.     struct i596_scp scp;
  154.     struct i596_iscp iscp;
  155.     struct i596_scb scb;
  156.     struct i596_cmd set_add;
  157.     char eth_addr[8];
  158.     struct i596_cmd set_conf;
  159.     char i596_config[16];
  160.     struct i596_cmd tdr;
  161.     unsigned long stat;
  162.     int last_restart;
  163.     struct i596_rfd *rx_tail;
  164.     struct i596_cmd *cmd_tail;
  165.     struct i596_cmd *cmd_head;
  166.     int cmd_backlog;
  167.     unsigned long last_cmd;
  168.     struct enet_statistics stats;
  169. };
  170.  
  171. char init_setup[] = {
  172.     0x8E,    /* length, prefetch on */
  173.     0xC8,    /* fifo to 8, monitor off */
  174.     0x80,    /* don't save bad frames */
  175.     0x2E,    /* No source address insertion, 8 byte preamble */
  176.     0x00,    /* priority and backoff defaults */
  177.     0x60,    /* interframe spacing */
  178.     0x00,    /* slot time LSB */
  179.     0xf2,    /* slot time and retries */
  180.     0x00,    /* promiscuous mode */
  181.     0x00,    /* collision detect */
  182.     0x40,    /* minimum frame length */
  183.     0xff,    
  184.     0x00,
  185.     0x7f    /*  *multi IA */ };
  186.  
  187. static int i596_open(struct device *dev);
  188. static int i596_start_xmit(struct sk_buff *skb, struct device *dev);
  189. static void i596_interrupt(int irq, struct pt_regs *regs);
  190. static int i596_close(struct device *dev);
  191. static struct enet_statistics *i596_get_stats(struct device *dev);
  192. static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd);
  193. static void print_eth(char *);
  194. #ifdef HAVE_MULTICAST
  195. static void set_multicast_list(struct device *dev, int num_addrs, void *addrs);
  196. #endif
  197.  
  198.  
  199. static inline int
  200. init_rx_bufs(struct device *dev, int num)
  201. {
  202.     struct i596_private *lp = (struct i596_private *)dev->priv;
  203.     int i;
  204.     struct i596_rfd *rfd;
  205.  
  206.     lp->scb.rfd = (struct i596_rfd *)I596_NULL;
  207.  
  208.     if (i596_debug > 1) printk ("%s: init_rx_bufs %d.\n", dev->name, num);
  209.  
  210.     for (i = 0; i < num; i++)
  211.     {
  212.     if (!(rfd = (struct i596_rfd *)kmalloc(sizeof(struct i596_rfd), GFP_KERNEL)))
  213.             break;
  214.  
  215.     rfd->stat = 0x0000;
  216.     rfd->rbd = I596_NULL;
  217.     rfd->count = 0;
  218.     rfd->size = 1532;
  219.         if (i == 0)
  220.         {
  221.         rfd->cmd = CMD_EOL;
  222.             lp->rx_tail = rfd;
  223.         }
  224.         else
  225.         rfd->cmd = 0x0000;
  226.  
  227.         rfd->next = lp->scb.rfd;
  228.         lp->scb.rfd = rfd;
  229.     }
  230.  
  231.     if (i != 0)
  232.       lp->rx_tail->next = lp->scb.rfd;
  233.  
  234.     return (i);
  235. }
  236.  
  237. static inline void
  238. remove_rx_bufs(struct device *dev)
  239. {
  240.     struct i596_private *lp = (struct i596_private *)dev->priv;
  241.     struct i596_rfd *rfd = lp->scb.rfd;
  242.  
  243.     lp->rx_tail->next = (struct i596_rfd *)I596_NULL;
  244.  
  245.     do
  246.     {
  247.         lp->scb.rfd = rfd->next;
  248.         kfree_s(rfd, sizeof(struct i596_rfd));
  249.         rfd = lp->scb.rfd;
  250.     }
  251.     while (rfd != lp->rx_tail);
  252. }
  253.  
  254. static inline void
  255. init_i596_mem(struct device *dev)
  256. {
  257.     struct i596_private *lp = (struct i596_private *)dev->priv;
  258.     short ioaddr = dev->base_addr;
  259.     int boguscnt = 100;
  260.  
  261.     /* change the scp address */
  262.     outw(0, ioaddr);
  263.     outw(0, ioaddr);
  264.     outb(4, ioaddr+0xf);
  265.     outw(((((int)&lp->scp) & 0xffff) | 2), ioaddr);
  266.     outw((((int)&lp->scp)>>16) & 0xffff, ioaddr);
  267.  
  268.     lp->last_cmd = jiffies;
  269.  
  270.     lp->scp.sysbus = 0x00440000;
  271.     lp->scp.iscp = &(lp->iscp);
  272.     lp->iscp.scb = &(lp->scb);
  273.     lp->iscp.stat = 0x0001;
  274.     lp->cmd_backlog = 0;
  275.  
  276.     lp->cmd_head = lp->scb.cmd = (struct i596_cmd *) I596_NULL;
  277.  
  278.     if (i596_debug > 2) printk("%s: starting i82596.\n", dev->name);
  279.  
  280.     (void) inb (ioaddr+0x10);
  281.     outb(4, ioaddr+0xf);
  282.     outw(0, ioaddr+4);
  283.  
  284.     while (lp->iscp.stat)
  285.     if (--boguscnt == 0)
  286.     {
  287.         printk("%s: i82596 initialization timed out with status %4.4x, cmd %4.4x.\n",
  288.            dev->name, lp->scb.status, lp->scb.command);
  289.         break;
  290.         }
  291.  
  292.     lp->scb.command = 0;
  293.  
  294.     memcpy (lp->i596_config, init_setup, 14);
  295.     lp->set_conf.command = CmdConfigure;
  296.     i596_add_cmd(dev, &lp->set_conf);
  297.  
  298.     memcpy (lp->eth_addr, dev->dev_addr, 6);
  299.     lp->set_add.command = CmdSASetup;
  300.     i596_add_cmd(dev, &lp->set_add);
  301.  
  302.     lp->tdr.command = CmdTDR;
  303.     i596_add_cmd(dev, &lp->tdr);
  304.  
  305.     boguscnt = 200;
  306.     while (lp->scb.status, lp->scb.command)
  307.     if (--boguscnt == 0)
  308.     {
  309.         printk("%s: receive unit start timed out with status %4.4x, cmd %4.4x.\n",
  310.            dev->name, lp->scb.status, lp->scb.command);
  311.         break;
  312.         }
  313.  
  314.     lp->scb.command = RX_START;
  315.     outw(0, ioaddr+4);
  316.  
  317.     boguscnt = 200;
  318.     while (lp->scb.status, lp->scb.command)
  319.         if (--boguscnt == 0)
  320.     {
  321.         printk("i82596 init timed out with status %4.4x, cmd %4.4x.\n",
  322.         lp->scb.status, lp->scb.command);
  323.         break;
  324.     }
  325.  
  326.     return;
  327. }
  328.  
  329. static inline int
  330. i596_rx(struct device *dev)
  331. {
  332.     struct i596_private *lp = (struct i596_private *)dev->priv;
  333.     int frames = 0;
  334.  
  335.     if (i596_debug > 3) printk ("i596_rx()\n");
  336.  
  337.     while ((lp->scb.rfd->stat) & STAT_C)
  338.     {
  339.         if (i596_debug >2) print_eth(lp->scb.rfd->data);
  340.  
  341.     if ((lp->scb.rfd->stat) & STAT_OK)
  342.     {
  343.         /* a good frame */
  344.         int pkt_len = lp->scb.rfd->count & 0x3fff;
  345.         struct sk_buff *skb = alloc_skb(pkt_len, GFP_ATOMIC);
  346.  
  347.         frames++;
  348.  
  349.         if (skb == NULL)
  350.         {
  351.         printk ("%s: i596_rx Memory squeeze, dropping packet.\n", dev->name);
  352.         lp->stats.rx_dropped++;
  353.         break;
  354.         }
  355.  
  356.         skb->len = pkt_len;
  357.           skb->dev = dev;        
  358.         memcpy(skb->data, lp->scb.rfd->data, pkt_len);
  359.  
  360.         netif_rx(skb);
  361.         lp->stats.rx_packets++;
  362.  
  363.         if (i596_debug > 4) print_eth(skb->data);
  364.     }
  365.     else
  366.     {
  367.         lp->stats.rx_errors++;
  368.         if ((lp->scb.rfd->stat) & 0x0001) lp->stats.collisions++;
  369.         if ((lp->scb.rfd->stat) & 0x0080) lp->stats.rx_length_errors++;
  370.         if ((lp->scb.rfd->stat) & 0x0100) lp->stats.rx_over_errors++;
  371.         if ((lp->scb.rfd->stat) & 0x0200) lp->stats.rx_fifo_errors++;
  372.         if ((lp->scb.rfd->stat) & 0x0400) lp->stats.rx_frame_errors++;
  373.         if ((lp->scb.rfd->stat) & 0x0800) lp->stats.rx_crc_errors++;
  374.         if ((lp->scb.rfd->stat) & 0x1000) lp->stats.rx_length_errors++;
  375.     }
  376.  
  377.     lp->scb.rfd->stat = 0;
  378.     lp->rx_tail->cmd = 0;
  379.     lp->rx_tail = lp->scb.rfd;
  380.     lp->scb.rfd = lp->scb.rfd->next;
  381.     lp->rx_tail->count = 0;
  382.     lp->rx_tail->cmd = CMD_EOL;
  383.  
  384.     }
  385.  
  386.     if (i596_debug > 3) printk ("frames %d\n", frames);
  387.  
  388.     return 0;
  389. }
  390.  
  391. static inline void
  392. i596_cleanup_cmd(struct i596_private *lp)
  393. {
  394.     struct i596_cmd *ptr;
  395.     int boguscnt = 100;
  396.  
  397.     if (i596_debug > 4) printk ("i596_cleanup_cmd\n");
  398.  
  399.     while (lp->cmd_head != (struct i596_cmd *) I596_NULL)
  400.     {
  401.     ptr = lp->cmd_head;
  402.  
  403.     lp->cmd_head = lp->cmd_head->next;
  404.     lp->cmd_backlog--;
  405.  
  406.     switch ((ptr->command) & 0x7)
  407.     {
  408.         case CmdTx:
  409.         {
  410.         struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
  411.         struct sk_buff *skb = ((struct sk_buff *)(tx_cmd->tbd->data)) -1;
  412.  
  413.         dev_kfree_skb(skb, FREE_WRITE);
  414.  
  415.         lp->stats.tx_errors++;
  416.         lp->stats.tx_aborted_errors++;
  417.  
  418.         ptr->next = (struct i596_cmd * ) I596_NULL;
  419.         kfree_s((unsigned char *)tx_cmd, (sizeof (struct tx_cmd) + sizeof (struct i596_tbd)));
  420.         break;
  421.         }
  422.         case CmdMulticastList:
  423.         {
  424.         unsigned short count = *((unsigned short *) (ptr + 1));
  425.  
  426.         ptr->next = (struct i596_cmd * ) I596_NULL;
  427.         kfree_s((unsigned char *)ptr, (sizeof (struct i596_cmd) + count + 2));
  428.         break;
  429.         }
  430.         default:
  431.         ptr->next = (struct i596_cmd * ) I596_NULL;
  432.     }
  433.     }
  434.  
  435.     while (lp->scb.status, lp->scb.command)
  436.     if (--boguscnt == 0)
  437.     {
  438.         printk("i596_cleanup_cmd timed out with status %4.4x, cmd %4.4x.\n",
  439.         lp->scb.status, lp->scb.command);
  440.         break;
  441.         }
  442.  
  443.     lp->scb.cmd = lp->cmd_head;
  444. }
  445.  
  446. static inline void
  447. i596_reset(struct device *dev, struct i596_private *lp, int ioaddr)
  448. {
  449.     int boguscnt = 100;
  450.  
  451.     if (i596_debug > 4) printk ("i596_reset\n");
  452.  
  453.     while (lp->scb.status, lp->scb.command)
  454.         if (--boguscnt == 0)
  455.     {
  456.         printk("i596_reset timed out with status %4.4x, cmd %4.4x.\n",
  457.         lp->scb.status, lp->scb.command);
  458.         break;
  459.     }
  460.  
  461.     dev->start = 0;
  462.     dev->tbusy = 1;
  463.  
  464.     lp->scb.command = CUC_ABORT|RX_ABORT;
  465.     outw(0, ioaddr+4);
  466.  
  467.     /* wait for shutdown */
  468.     boguscnt = 400;
  469.  
  470.     while ((lp->scb.status, lp->scb.command) || lp->scb.command)
  471.         if (--boguscnt == 0)
  472.     {
  473.         printk("i596_reset 2 timed out with status %4.4x, cmd %4.4x.\n",
  474.         lp->scb.status, lp->scb.command);
  475.         break;
  476.     }
  477.  
  478.     i596_cleanup_cmd(lp);
  479.     i596_rx(dev);
  480.  
  481.     dev->start = 1;
  482.     dev->tbusy = 0;
  483.     dev->interrupt = 0;
  484.     init_i596_mem(dev);
  485. }
  486.  
  487. static void i596_add_cmd(struct device *dev, struct i596_cmd *cmd)
  488. {
  489.     struct i596_private *lp = (struct i596_private *)dev->priv;
  490.     int ioaddr = dev->base_addr;
  491.     unsigned long flags;
  492.     int boguscnt = 100;
  493.  
  494.     if (i596_debug > 4) printk ("i596_add_cmd\n");
  495.  
  496.     cmd->status = 0;
  497.     cmd->command |= (CMD_EOL|CMD_INTR);
  498.     cmd->next = (struct i596_cmd *) I596_NULL;
  499.  
  500.     save_flags(flags);
  501.     cli();
  502.     if (lp->cmd_head != (struct i596_cmd *) I596_NULL)
  503.     lp->cmd_tail->next = cmd;
  504.     else 
  505.     {
  506.     lp->cmd_head = cmd;
  507.     while (lp->scb.status, lp->scb.command)
  508.         if (--boguscnt == 0)
  509.         {
  510.         printk("i596_add_cmd timed out with status %4.4x, cmd %4.4x.\n",
  511.            lp->scb.status, lp->scb.command);
  512.         break;
  513.             }
  514.  
  515.     lp->scb.cmd = cmd;
  516.     lp->scb.command = CUC_START;
  517.         outw (0, ioaddr+4);
  518.     }
  519.     lp->cmd_tail = cmd;
  520.     lp->cmd_backlog++;
  521.  
  522.     lp->cmd_head = lp->scb.cmd;
  523.     restore_flags(flags);
  524.  
  525.     if (lp->cmd_backlog > 16) 
  526.     {
  527.     int tickssofar = jiffies - lp->last_cmd;
  528.  
  529.     if (tickssofar < 25) return;
  530.  
  531.     printk("%s: command unit timed out, status resetting.\n", dev->name);
  532.  
  533.     i596_reset(dev, lp, ioaddr);
  534.     }
  535. }
  536.  
  537. static int
  538. i596_open(struct device *dev)
  539. {
  540.     int i;
  541.  
  542.     if (i596_debug > 1)
  543.     printk("%s: i596_open() irq %d.\n", dev->name, dev->irq);
  544.  
  545.     if (request_irq(dev->irq, &i596_interrupt, 0, "apricot"))
  546.     return -EAGAIN;
  547.  
  548.     irq2dev_map[dev->irq] = dev;
  549.  
  550.     i = init_rx_bufs(dev, RX_RING_SIZE);
  551.  
  552.     if ((i = init_rx_bufs(dev, RX_RING_SIZE)) < RX_RING_SIZE)
  553.         printk("%s: only able to allocate %d receive buffers\n", dev->name, i);
  554.  
  555.     if (i < 4)
  556.     {
  557.         free_irq(dev->irq);
  558.         irq2dev_map[dev->irq] = 0;
  559.         return -EAGAIN;
  560.     }
  561.  
  562.     dev->tbusy = 0;
  563.     dev->interrupt = 0;
  564.     dev->start = 1;
  565. #ifdef MODULE
  566.     MOD_INC_USE_COUNT;
  567. #endif
  568.  
  569.     /* Initialize the 82596 memory */
  570.     init_i596_mem(dev);
  571.  
  572.     return 0;            /* Always succeed */
  573. }
  574.  
  575. static int
  576. i596_start_xmit(struct sk_buff *skb, struct device *dev)
  577. {
  578.     struct i596_private *lp = (struct i596_private *)dev->priv;
  579.     int ioaddr = dev->base_addr;
  580.     struct tx_cmd *tx_cmd;
  581.  
  582.     if (i596_debug > 2) printk ("%s: Apricot start xmit\n", dev->name);
  583.  
  584.     /* Transmitter timeout, serious problems. */
  585.     if (dev->tbusy) {
  586.     int tickssofar = jiffies - dev->trans_start;
  587.     if (tickssofar < 5)
  588.         return 1;
  589.     printk("%s: transmit timed out, status resetting.\n",
  590.            dev->name);
  591.     lp->stats.tx_errors++;
  592.     /* Try to restart the adaptor */
  593.     if (lp->last_restart == lp->stats.tx_packets) {
  594.         if (i596_debug > 1) printk ("Resetting board.\n");
  595.  
  596.         /* Shutdown and restart */
  597.             i596_reset(dev,lp, ioaddr);
  598.     } else {
  599.         /* Issue a channel attention signal */
  600.         if (i596_debug > 1) printk ("Kicking board.\n");
  601.  
  602.         lp->scb.command = CUC_START|RX_START;
  603.         outw(0, ioaddr+4);
  604.  
  605.         lp->last_restart = lp->stats.tx_packets;
  606.     }
  607.     dev->tbusy = 0;
  608.     dev->trans_start = jiffies;
  609.     }
  610.  
  611.     /* If some higher level thinks we've misses a tx-done interrupt
  612.        we are passed NULL. n.b. dev_tint handles the cli()/sti()
  613.        itself. */
  614.     if (skb == NULL) {
  615.     dev_tint(dev);
  616.     return 0;
  617.     }
  618.  
  619.     /* shouldn't happen */
  620.     if (skb->len <= 0) return 0;
  621.  
  622.     if (i596_debug > 3) printk("%s: i596_start_xmit() called\n", dev->name);
  623.  
  624.     /* Block a timer-based transmit from overlapping.  This could better be
  625.        done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
  626.     if (set_bit(0, (void*)&dev->tbusy) != 0)
  627.     printk("%s: Transmitter access conflict.\n", dev->name);
  628.     else
  629.     {
  630.     short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
  631.     dev->trans_start = jiffies;
  632.  
  633.     tx_cmd = (struct tx_cmd *) kmalloc ((sizeof (struct tx_cmd) + sizeof (struct i596_tbd)), GFP_ATOMIC);
  634.     if (tx_cmd == NULL)
  635.     {
  636.         printk ("%s: i596_xmit Memory squeeze, dropping packet.\n", dev->name);
  637.         lp->stats.tx_dropped++;
  638.  
  639.         dev_kfree_skb(skb, FREE_WRITE);
  640.     }
  641.     else
  642.     {
  643.         tx_cmd->tbd = (struct i596_tbd *) (tx_cmd + 1);
  644.         tx_cmd->tbd->next = (struct i596_tbd *) I596_NULL;
  645.  
  646.         tx_cmd->cmd.command = CMD_FLEX|CmdTx;
  647.  
  648.         tx_cmd->pad = 0;
  649.         tx_cmd->size = 0;
  650.         tx_cmd->tbd->pad = 0;
  651.         tx_cmd->tbd->size = EOF | length;
  652.  
  653.         tx_cmd->tbd->data = skb->data;
  654.  
  655.         if (i596_debug > 3) print_eth(skb->data);
  656.  
  657.         i596_add_cmd(dev, (struct i596_cmd *)tx_cmd);
  658.  
  659.         lp->stats.tx_packets++;
  660.     }
  661.     }
  662.  
  663.     dev->tbusy = 0;
  664.  
  665.     return 0;
  666. }
  667.  
  668.  
  669. static void print_eth(char *add)
  670. {
  671.     int i;
  672.  
  673.     printk ("Dest  ");
  674.     for (i = 0; i < 6; i++)
  675.     printk(" %2.2X", (unsigned char)add[i]);
  676.     printk ("\n");
  677.  
  678.     printk ("Source");
  679.     for (i = 0; i < 6; i++)
  680.     printk(" %2.2X", (unsigned char)add[i+6]);
  681.     printk ("\n");
  682.     printk ("type %2.2X%2.2X\n", (unsigned char)add[12], (unsigned char)add[13]);
  683. }
  684.  
  685. int apricot_probe(struct device *dev)
  686. {
  687.     int i;
  688.     struct i596_private *lp;
  689.     int checksum = 0;
  690.     int ioaddr = 0x300;
  691.     char eth_addr[6];
  692.     
  693.     /* this is easy the ethernet interface can only be at 0x300 */
  694.     /* first check nothing is already registered here */
  695.  
  696.     if (check_region(ioaddr, APRICOT_TOTAL_SIZE))
  697.     return ENODEV;
  698.  
  699.     for (i = 0; i < 8; i++)
  700.     {
  701.         eth_addr[i] = inb(ioaddr+8+i);
  702.     checksum += eth_addr[i];
  703.      }
  704.  
  705.     /* checksum is a multiple of 0x100, got this wrong first time
  706.        some machines have 0x100, some 0x200. The DOS driver doesn't
  707.        even bother with the checksum */
  708.  
  709.     if (checksum % 0x100) return ENODEV;
  710.  
  711.     /* Some other boards trip the checksum.. but then appear as ether
  712.        address 0. Trap these - AC */
  713.        
  714.     if(memcmp(eth_addr,"\x00\x00\x49",3)!= 0)
  715.         return ENODEV;
  716.  
  717.     request_region(ioaddr, APRICOT_TOTAL_SIZE,"apricot");
  718.  
  719.     dev->base_addr = ioaddr;
  720.     ether_setup(dev);
  721.     printk("%s: Apricot 82596 at %#3x,", dev->name, ioaddr);
  722.  
  723.     for (i = 0; i < 6; i++)
  724.     printk(" %2.2X", dev->dev_addr[i] = eth_addr[i]);
  725.  
  726.     dev->base_addr = ioaddr;
  727.     dev->irq = 10;
  728.     printk(" IRQ %d.\n", dev->irq);
  729.  
  730.     if (i596_debug > 0) printk(version);
  731.  
  732.     /* The APRICOT-specific entries in the device structure. */
  733.     dev->open = &i596_open;
  734.     dev->stop = &i596_close;
  735.     dev->hard_start_xmit = &i596_start_xmit;
  736.     dev->get_stats = &i596_get_stats;
  737. #ifdef HAVE_MULTICAST
  738.     dev->set_multicast_list = &set_multicast_list;
  739. #endif
  740.  
  741.     dev->mem_start = (int)kmalloc(sizeof(struct i596_private)+ 0x0f, GFP_KERNEL);
  742.     /* align for scp */
  743.     dev->priv = (void *)((dev->mem_start + 0xf) & 0xfffffff0);
  744.  
  745.     lp = (struct i596_private *)dev->priv;
  746.     memset((void *)lp, 0, sizeof(struct i596_private));
  747.     lp->scb.command = 0;
  748.     lp->scb.cmd = (struct i596_cmd *) I596_NULL;
  749.     lp->scb.rfd = (struct i596_rfd *)I596_NULL;
  750.  
  751.     return 0;
  752. }
  753.  
  754. static void
  755. i596_interrupt(int irq, struct pt_regs *regs)
  756. {
  757.     struct device *dev = (struct device *)(irq2dev_map[irq]);
  758.     struct i596_private *lp;
  759.     short ioaddr;
  760.     int boguscnt = 200;
  761.     unsigned short status, ack_cmd = 0;
  762.  
  763.     if (dev == NULL) {
  764.     printk ("i596_interrupt(): irq %d for unknown device.\n", irq);
  765.     return;
  766.     }
  767.  
  768.     if (i596_debug > 3) printk ("%s: i596_interrupt(): irq %d\n",dev->name, irq);
  769.  
  770.     if (dev->interrupt)
  771.     printk("%s: Re-entering the interrupt handler.\n", dev->name);
  772.  
  773.     dev->interrupt = 1;
  774.  
  775.     ioaddr = dev->base_addr;
  776.  
  777.     lp = (struct i596_private *)dev->priv;
  778.  
  779.     while (lp->scb.status, lp->scb.command)
  780.     if (--boguscnt == 0)
  781.         {
  782.         printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
  783.         break;
  784.         }
  785.     status = lp->scb.status;
  786.  
  787.     if (i596_debug > 4)
  788.     printk("%s: i596 interrupt, status %4.4x.\n", dev->name, status);
  789.  
  790.     ack_cmd = status & 0xf000;
  791.  
  792.     if ((status & 0x8000) || (status & 0x2000))
  793.     {
  794.     struct i596_cmd *ptr;
  795.  
  796.     if ((i596_debug > 4) && (status & 0x8000))
  797.         printk("%s: i596 interrupt completed command.\n", dev->name);
  798.     if ((i596_debug > 4) && (status & 0x2000))
  799.         printk("%s: i596 interrupt command unit inactive %x.\n", dev->name, status & 0x0700);
  800.  
  801.     while ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (lp->cmd_head->status & STAT_C))
  802.     {
  803.         ptr = lp->cmd_head;
  804.  
  805.         lp->cmd_head = lp->cmd_head->next;
  806.         lp->cmd_backlog--;
  807.  
  808.         switch ((ptr->command) & 0x7)
  809.         {
  810.         case CmdTx:
  811.         {
  812.             struct tx_cmd *tx_cmd = (struct tx_cmd *) ptr;
  813.             struct sk_buff *skb = ((struct sk_buff *)(tx_cmd->tbd->data)) -1;
  814.  
  815.             dev_kfree_skb(skb, FREE_WRITE);
  816.  
  817.             if ((ptr->status) & STAT_OK)
  818.             {
  819.                 if (i596_debug >2) print_eth(skb->data);
  820.             }
  821.             else
  822.             {
  823.             lp->stats.tx_errors++;
  824.             if ((ptr->status) & 0x0020) lp->stats.collisions++;
  825.             if (!((ptr->status) & 0x0040)) lp->stats.tx_heartbeat_errors++;
  826.             if ((ptr->status) & 0x0400) lp->stats.tx_carrier_errors++;
  827.             if ((ptr->status) & 0x0800) lp->stats.collisions++;
  828.             if ((ptr->status) & 0x1000) lp->stats.tx_aborted_errors++;
  829.             }
  830.  
  831.  
  832.             ptr->next = (struct i596_cmd * ) I596_NULL;
  833.             kfree_s((unsigned char *)tx_cmd, (sizeof (struct tx_cmd) + sizeof (struct i596_tbd)));
  834.             break;
  835.         }
  836.         case CmdMulticastList:
  837.         {
  838.             unsigned short count = *((unsigned short *) (ptr + 1));
  839.  
  840.             ptr->next = (struct i596_cmd * ) I596_NULL;
  841.             kfree_s((unsigned char *)ptr, (sizeof (struct i596_cmd) + count + 2));
  842.             break;
  843.         }
  844.         case CmdTDR:
  845.         {
  846.             unsigned long status = *((unsigned long *) (ptr + 1));
  847.  
  848.             if (status & 0x8000)
  849.             {
  850.             if (i596_debug > 3)
  851.                     printk("%s: link ok.\n", dev->name);
  852.             }
  853.             else
  854.             {
  855.             if (status & 0x4000)
  856.                     printk("%s: Transceiver problem.\n", dev->name);
  857.             if (status & 0x2000)
  858.                     printk("%s: Termination problem.\n", dev->name);
  859.             if (status & 0x1000)
  860.                     printk("%s: Short circuit.\n", dev->name);
  861.  
  862.                 printk("%s: Time %ld.\n", dev->name, status & 0x07ff);
  863.             }
  864.         }
  865.         default:
  866.             ptr->next = (struct i596_cmd * ) I596_NULL;
  867.  
  868.         lp->last_cmd = jiffies;
  869.          }
  870.     }
  871.  
  872.     ptr = lp->cmd_head;
  873.     while ((ptr != (struct i596_cmd *) I596_NULL) && (ptr != lp->cmd_tail))
  874.     {
  875.         ptr->command &= 0x1fff;
  876.         ptr = ptr->next;
  877.     }
  878.  
  879.     if ((lp->cmd_head != (struct i596_cmd *) I596_NULL) && (dev->start)) ack_cmd |= CUC_START;
  880.     lp->scb.cmd = lp->cmd_head;
  881.     }
  882.  
  883.     if ((status & 0x1000) || (status & 0x4000))
  884.     {
  885.     if ((i596_debug > 4) && (status & 0x4000))
  886.         printk("%s: i596 interrupt received a frame.\n", dev->name);
  887.     if ((i596_debug > 4) && (status & 0x1000))
  888.         printk("%s: i596 interrupt receive unit inactive %x.\n", dev->name, status & 0x0070);
  889.  
  890.     i596_rx(dev);
  891.  
  892.     if (dev->start) ack_cmd |= RX_START;
  893.     }
  894.  
  895.     /* acknowledge the interrupt */
  896.  
  897. /*
  898.     if ((lp->scb.cmd != (struct i596_cmd *) I596_NULL) && (dev->start)) ack_cmd | = CUC_START;
  899. */
  900.     boguscnt = 100;
  901.     while (lp->scb.status, lp->scb.command)
  902.     if (--boguscnt == 0)
  903.         {
  904.         printk("%s: i596 interrupt, timeout status %4.4x command %4.4x.\n", dev->name, lp->scb.status, lp->scb.command);
  905.         break;
  906.         }
  907.     lp->scb.command = ack_cmd;
  908.  
  909.     (void) inb (ioaddr+0x10);
  910.     outb (4, ioaddr+0xf);
  911.     outw (0, ioaddr+4);
  912.  
  913.     if (i596_debug > 4)
  914.     printk("%s: exiting interrupt.\n", dev->name);
  915.  
  916.     dev->interrupt = 0;
  917.     return;
  918. }
  919.  
  920. static int
  921. i596_close(struct device *dev)
  922. {
  923.     int ioaddr = dev->base_addr;
  924.     struct i596_private *lp = (struct i596_private *)dev->priv;
  925.     int boguscnt = 200;
  926.  
  927.     dev->start = 0;
  928.     dev->tbusy = 1;
  929.  
  930.     if (i596_debug > 1)
  931.     printk("%s: Shutting down ethercard, status was %4.4x.\n",
  932.            dev->name, lp->scb.status);
  933.  
  934.     lp->scb.command = CUC_ABORT|RX_ABORT;
  935.     outw(0, ioaddr+4);
  936.  
  937.     i596_cleanup_cmd(lp);
  938.  
  939.     while (lp->scb.status, lp->scb.command)
  940.     if (--boguscnt == 0)
  941.     {
  942.         printk("%s: close timed timed out with status %4.4x, cmd %4.4x.\n",
  943.            dev->name, lp->scb.status, lp->scb.command);
  944.         break;
  945.         }
  946.     free_irq(dev->irq);
  947.     irq2dev_map[dev->irq] = 0;
  948.     remove_rx_bufs(dev);
  949. #ifdef MODULE
  950.     MOD_DEC_USE_COUNT;
  951. #endif
  952.  
  953.  
  954.     return 0;
  955. }
  956.  
  957. static struct enet_statistics *
  958. i596_get_stats(struct device *dev)
  959. {
  960.     struct i596_private *lp = (struct i596_private *)dev->priv;
  961.  
  962.     return &lp->stats;
  963. }
  964.  
  965. #ifdef HAVE_MULTICAST
  966. /* Set or clear the multicast filter for this adaptor.
  967.    num_addrs == -1    Promiscuous mode, receive all packets
  968.    num_addrs == 0    Normal mode, clear multicast list
  969.    num_addrs > 0    Multicast mode, receive normal and MC packets, and do
  970.                best-effort filtering.
  971.  */
  972. static void
  973. set_multicast_list(struct device *dev, int num_addrs, void *addrs)
  974. {
  975.     struct i596_private *lp = (struct i596_private *)dev->priv;
  976.     struct i596_cmd *cmd;
  977.  
  978.     if (i596_debug > 1)
  979.     printk ("%s: set multicast list %d\n", dev->name, num_addrs);
  980.  
  981.     if (num_addrs > 0) {
  982.     cmd = (struct i596_cmd *) kmalloc(sizeof(struct i596_cmd)+2+num_addrs*6, GFP_ATOMIC);
  983.     if (cmd == NULL)
  984.     {
  985.         printk ("%s: set_multicast Memory squeeze.\n", dev->name);
  986.         return;
  987.     }
  988.  
  989.         cmd->command = CmdMulticastList;
  990.         *((unsigned short *) (cmd + 1)) = num_addrs * 6;
  991.         memcpy (((char *)(cmd + 1))+2, addrs, num_addrs * 6);
  992.         print_eth (((char *)(cmd + 1)) + 2);
  993.         
  994.         i596_add_cmd(dev, cmd);
  995.     } else
  996.     {
  997.     if (lp->set_conf.next != (struct i596_cmd * ) I596_NULL) return;
  998.     if (num_addrs == 0)
  999.         lp->i596_config[8] &= ~0x01;
  1000.     else
  1001.         lp->i596_config[8] |= 0x01;
  1002.  
  1003.     i596_add_cmd(dev, &lp->set_conf);
  1004.     }
  1005.  
  1006. }
  1007. #endif
  1008.  
  1009. #ifdef HAVE_DEVLIST
  1010. static unsigned int apricot_portlist[] = {0x300, 0};
  1011. struct netdev_entry apricot_drv = 
  1012. {"apricot", apricot_probe, APRICOT_TOTAL_SIZE, apricot_portlist};
  1013. #endif
  1014.  
  1015. #ifdef MODULE
  1016. char kernel_version[] = UTS_RELEASE;
  1017. static struct device dev_apricot = {
  1018.   "        ", /* device name inserted by /linux/drivers/net/net_init.c */
  1019.   0, 0, 0, 0,
  1020.   0x300, 10,
  1021.   0, 0, 0, NULL, apricot_probe };
  1022.  
  1023. int
  1024. init_module(void)
  1025. {
  1026.   if (register_netdev(&dev_apricot) != 0)
  1027.     return -EIO;
  1028.   return 0;
  1029. }
  1030.  
  1031. void
  1032. cleanup_module(void)
  1033. {
  1034.   if (MOD_IN_USE)
  1035.     printk("%s: device busy, remove delayed\n", dev_apricot.name);
  1036.   else
  1037.   {
  1038.     unregister_netdev(&dev_apricot);
  1039.     kfree_s((void *)dev_apricot.mem_start, sizeof(struct i596_private) + 0xf);
  1040.     dev_apricot.priv = NULL;
  1041.   }
  1042. }
  1043. #endif /* MODULE */
  1044.  
  1045. /*
  1046.  * Local variables:
  1047.  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c apricot.c"
  1048.  * End:
  1049.  */
  1050.