home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / EC.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  9KB  |  347 lines

  1. /* Driver for 3COM Ethernet card (PC specific code)
  2.  *
  3.  * This driver is deprecated - use the loadable packet driver from the
  4.  * Clarkson collection instead. Better yet, junk your 3C501 card and buy
  5.  * something more reasonable.
  6.  *
  7.  * Copyright 1991 Phil Karn, KA9Q
  8.  */
  9.  
  10. /****************************************************************************
  11. *    $Id: ec.c 1.2 93/07/16 11:44:26 ROOT_DOS Exp $
  12. *    15 Jul 93    1.2        GT    Fix warnings.                                    *
  13. ****************************************************************************/
  14.  
  15. #define    TIMER    20000    /* Timeout on transmissions */
  16.  
  17. #include <stdio.h>
  18. #include <dos.h>
  19. #include "global.h"
  20. #include "mbuf.h"
  21. #include "enet.h"
  22. #include "iface.h"
  23. #include "pktdrvr.h"
  24. #include "netuser.h"
  25. #include "ec.h"
  26. #include "arp.h"
  27. #include "trace.h"
  28. #include "pc.h"
  29. #include "ip.h"
  30.  
  31. static int ec_init __ARGS((struct iface *iface,unsigned bufsize));
  32. static int ec_raw __ARGS((struct iface *iface,struct mbuf *bp));
  33. static int ec_stop __ARGS((struct iface *iface));
  34. static void getecaddr __ARGS((unsigned base,char *cp));
  35. static void rcv_fixup __ARGS((unsigned base));
  36. static void setecaddr __ARGS((unsigned base,char *cp));
  37.  
  38. static INTERRUPT (*Ecvecsave[EC_MAX])();
  39. static INTERRUPT (*Ecvec[])() = {ec0vec,ec1vec,ec2vec};
  40.  
  41. struct ec Ec[EC_MAX];        /* Per-controller info */
  42. int Nec = 0;
  43.  
  44. /* Initialize interface */
  45. static int
  46. ec_init(iface,bufsize)
  47. struct iface *iface;
  48. unsigned bufsize;    /* Maximum size of receive queue in PACKETS */
  49. {
  50.     register struct ec *ecp;
  51.     register unsigned base;
  52.     int dev;
  53.  
  54.     dev = iface->dev;
  55.     ecp = &Ec[dev];
  56.     base = ecp->base;
  57.     ecp->iface = iface;
  58.  
  59.     /* Pulse IE_RESET */
  60.      outportb(IE_CSR(base),IE_RESET);
  61.  
  62.     /* Save old int vector */
  63.     Ecvecsave[dev] = getirq(ecp->vec);
  64.  
  65.     /* Set interrupt vector */
  66.     if(setirq(ecp->vec,Ecvec[dev]) == -1){
  67.         tprintf("IRQ %u out of range\n",ecp->vec);
  68.         return -1;
  69.     }
  70.     maskon(ecp->vec);    /* Enable interrupt */
  71.     if(iface->hwaddr == NULLCHAR)
  72.         iface->hwaddr = mallocw(EADDR_LEN);
  73.     getecaddr(base,iface->hwaddr);
  74.     setecaddr(base,iface->hwaddr);
  75.     if(memcmp(iface->hwaddr,Ether_bdcst,EADDR_LEN) == 0){
  76.         tprintf("EC address PROM contains broadcast address!!\n");
  77.         return -1;
  78.     }
  79.     /* Enable DMA/interrupt request, gain control of buffer */
  80.     outportb(IE_CSR(base),IE_RIDE|IE_SYSBFR);
  81.  
  82.     /* Enable transmit interrupts */
  83.     outportb(EDLC_XMT(base),EDLC_16 | EDLC_JAM);
  84.  
  85.     /* Set up the receiver interrupts and flush status */
  86.     outportb(EDLC_RCV(base),EDLC_MULTI|EDLC_GOOD|EDLC_ANY|EDLC_SHORT
  87.      |EDLC_DRIBBLE|EDLC_FCS|EDLC_OVER);
  88.     inportb(EDLC_RCV(base));
  89.  
  90.     /* Start receiver */
  91.     outportw(IE_RP(base),0);    /* Reset read pointer */
  92.     outportb(IE_CSR(base),IE_RIDE|IE_RCVEDLC);
  93.     return 0;
  94. }
  95. /* Send raw packet (caller provides header) */
  96. static int
  97. ec_raw(iface,bp)
  98. struct iface *iface;    /* Pointer to interface control block */
  99. struct mbuf *bp;        /* Data field */
  100. {
  101.     register struct ec *ecp;
  102.     register unsigned base;
  103.     register int i;
  104.     short size;
  105.  
  106.     dump(iface,IF_TRACE_OUT,CL_ETHERNET,bp);
  107.     iface->rawsndcnt++;
  108.     iface->lastsent = secclock();
  109.  
  110.     ecp = &Ec[iface->dev];
  111.     base = ecp->base;
  112.  
  113.     ecp->estats.xmit++;
  114.  
  115.     size = len_p(bp);
  116.     /* Pad the size out to the minimum, if necessary,
  117.      * with junk from the last packet (nice security hole here)
  118.      */
  119.     if(size < RUNT)
  120.         size = RUNT;
  121.     size = (size+1) & ~1;    /* round size up to next even number */
  122.  
  123.     /* Wait for transmitter ready, if necessary. IE_XMTBSY is valid
  124.      * only in the transmit mode, hence the initial check.
  125.      */
  126.     if((inportb(IE_CSR(base)) & IE_BUFCTL) == IE_XMTEDLC){
  127.         for(i=TIMER;(inportb(IE_CSR(base)) & IE_XMTBSY) && i != 0;i--)
  128.             ;
  129.         if(i == 0){
  130.             ecp->estats.timeout++;
  131.             free_p(bp);
  132.             return -1;
  133.         }
  134.     }
  135.     ecp->size = size;
  136.     /* Get control of the board buffer and disable receiver */
  137.     outportb(IE_CSR(base),IE_RIDE|IE_SYSBFR);
  138.     /* Point GP at beginning of packet */
  139.     outportw(IE_GP(base),BFRSIZ-size);
  140.     /* Actually load each piece with a fast assembler routine */
  141.     while(bp != NULLBUF){
  142.         outbuf(IE_BFR(base),bp->data,bp->cnt);
  143.         bp = free_mbuf(bp);
  144.     }
  145.     /* Start transmitter */
  146.     outportw(IE_GP(base),BFRSIZ-size);
  147.     outportb(IE_CSR(base),IE_RIDE|IE_XMTEDLC);
  148.     return 0;
  149. }
  150. /* Ethernet interrupt handler */
  151. void
  152. ecint(dev)
  153. int dev;
  154. {
  155.     register struct ec *ecp;
  156.     register unsigned base;
  157.     struct mbuf *bp;
  158.     int16 size;
  159.     char stat;
  160.     struct phdr phdr;
  161.  
  162.     ecp = &Ec[dev];
  163.     base = Ec[dev].base;
  164.     ecp->estats.intrpt++;
  165.  
  166.     /* Check for transmit jam */
  167.     if(!(inportb(IE_CSR(base)) & IE_XMTBSY)){
  168.         stat = inportb(EDLC_XMT(base));
  169.         if(stat & EDLC_16){
  170.             ecp->estats.jam16++;
  171.             rcv_fixup(base);
  172.         } else if(stat & EDLC_JAM){
  173.             /* Crank counter back to beginning and restart transmit */
  174.             ecp->estats.jam++;
  175.             outportb(IE_CSR(base),IE_RIDE|IE_SYSBFR);
  176.             outportw(IE_GP(base),BFRSIZ - ecp->size);
  177.             outportb(IE_CSR(base),IE_RIDE|IE_XMTEDLC);
  178.         }
  179.     }
  180.     for(;;){
  181.         stat = inportb(EDLC_RCV(base));
  182.         if(stat & EDLC_STALE)
  183.             break;
  184.  
  185.         if(stat & EDLC_OVER){
  186.             ecp->estats.over++;
  187.             rcv_fixup(base);
  188.             continue;
  189.         }
  190.         if(stat & (EDLC_SHORT | EDLC_FCS | EDLC_DRIBBLE)){
  191.             ecp->estats.bad++;
  192.             rcv_fixup(base);
  193.             continue;
  194.         }
  195.         if(stat & EDLC_ANY){
  196.             /* Get control of the buffer */
  197.             outportw(IE_GP(base),0);
  198.             outportb(IE_CSR(base),IE_RIDE|IE_SYSBFR);
  199.         
  200.             /* Allocate mbuf and copy the packet into it */
  201.             size = inportw(IE_RP(base));
  202.             if(size < RUNT || size > GIANT)
  203.                 ecp->estats.bad++;
  204.             else if((bp = alloc_mbuf(size+sizeof(phdr))) == NULLBUF)
  205.                 ecp->estats.nomem++;
  206.             else {
  207.                 ecp->estats.recv++;
  208.                 /* Generate descriptor header */
  209.                 phdr.iface = ecp->iface;
  210.                 phdr.type = CL_ETHERNET;
  211.                 memcpy(&bp->data[0],(char *)&phdr,sizeof(phdr));
  212.                 inbuf(IE_BFR(base),bp->data+sizeof(phdr),size);
  213.                 bp->cnt = size + sizeof(phdr);
  214.                 enqueue(&Hopper,bp);
  215.             }
  216.             outportb(IE_CSR(base),IE_RIDE|IE_RCVEDLC);
  217.             outportb(IE_RP(base),0);
  218.         }
  219.     }
  220.     /* Clear any spurious interrupts */
  221.     (void)inportb(EDLC_RCV(base));
  222.     (void)inportb(EDLC_XMT(base));
  223. }
  224. static void
  225. rcv_fixup(base)
  226. register unsigned base;
  227. {
  228.     outportb(IE_CSR(base),IE_RIDE|IE_SYSBFR);
  229.     outportb(IE_CSR(base),IE_RIDE|IE_RCVEDLC);
  230.     outportb(IE_RP(base),0);
  231. }
  232. /* Read Ethernet address from controller PROM */
  233. static void
  234. getecaddr(base,cp)
  235. register unsigned base;
  236. register char *cp;
  237. {
  238.     register int i;
  239.  
  240.     for(i=0;i<EADDR_LEN;i++){
  241.         outportw(IE_GP(base),i);
  242.         *cp++ = inportb(IE_SAPROM(base));
  243.     }
  244. }
  245. /* Set Ethernet address on controller */
  246. static void
  247. setecaddr(base,cp)
  248. register unsigned base;
  249. register char *cp;
  250. {
  251.     register int i;
  252.  
  253.     for(i=0;i<EADDR_LEN;i++)
  254.         outportb(EDLC_ADDR(base)+i,*cp++);
  255. }
  256. /* Shut down the Ethernet controller */
  257. static int
  258. ec_stop(iface)
  259. struct iface *iface;
  260. {
  261.     register unsigned base;
  262.     int dev;
  263.     register struct ec *ecp;
  264.  
  265.     dev = iface->dev;
  266.     ecp = &Ec[dev];
  267.     base = ecp->base;
  268.  
  269.     /* Disable interrupt */
  270.     maskoff(Ec[dev].vec);
  271.  
  272.     /* Restore original interrupt vector */
  273.     setirq(ecp->vec,Ecvecsave[dev]);
  274.  
  275.     /* Pulse IE_RESET */
  276.     outportb(IE_CSR(base),IE_RESET);
  277.     outportb(IE_CSR(base),0);
  278.     return 0;
  279. }
  280. /* Attach a 3-Com model 3C500 Ethernet controller to the system
  281.  * argv[0]: hardware type, must be "3c500"
  282.  * argv[1]: I/O address, e.g., "0x300"
  283.  * argv[2]: vector, e.g., "3"
  284.  * argv[3]: mode, must be "arpa"
  285.  * argv[4]: interface label, e.g., "ec0"
  286.  * argv[5]: maximum number of packets allowed on receive queue, e.g., "5"
  287.  * argv[6]: maximum transmission unit, bytes, e.g., "1500"
  288.  * argv[7]: IP address, optional (defaults to Ip_addr)
  289.  */
  290. int
  291. ec_attach(argc,argv,p)
  292. int argc;
  293. char *argv[];
  294. void *p;
  295. {
  296.     register struct iface *if_ec;
  297.     int dev;
  298.  
  299.     if(Nec >= EC_MAX){
  300.         tprintf("Too many Ethernet controllers\n");
  301.         return -1;
  302.     }
  303.     if(if_lookup(argv[4]) != NULLIF){
  304.         tprintf("Interface %s already exists\n",argv[4]);
  305.         return -1;
  306.     }
  307.     dev = Nec++;
  308.     if_ec = (struct iface *)callocw(1,sizeof(struct iface));
  309.     if_ec->addr = Ip_addr;
  310.     if(argc > 7)
  311.         if_ec->addr = resolve(argv[7]);
  312.     if(if_ec->addr == 0){
  313.         tprintf(Noipaddr);
  314.         free((char *)if_ec);
  315.         return -1;
  316.     }
  317.     if_ec->name = strdup(argv[4]);
  318.     if_ec->mtu = atoi(argv[6]);
  319.     if_ec->type = CL_ETHERNET;
  320.     if_ec->send = enet_send;
  321.     if_ec->output = enet_output;
  322.     if_ec->raw = ec_raw;
  323.     if_ec->stop = ec_stop;
  324.     if_ec->dev = dev;
  325.  
  326.     Ec[dev].base = htoi(argv[1]);
  327.     Ec[dev].vec = htoi(argv[2]);
  328.  
  329.     if(strcmp(argv[3],"arpa") != 0){
  330.         tprintf("Mode %s unknown for interface %s\n",
  331.             argv[3],argv[4]);
  332.         free(if_ec->name);
  333.         free((char *)if_ec);
  334.         return -1;
  335.     }
  336.     /* Initialize device */
  337.     if(ec_init(if_ec,(unsigned)atoi(argv[5])) != 0){
  338.         free(if_ec->name);
  339.         free((char *)if_ec);
  340.         return -1;
  341.     }
  342.     if_ec->next = Ifaces;
  343.     Ifaces = if_ec;
  344.  
  345.     return 0;
  346. }
  347.