| plan 9 kernel history: overview | file list | diff list |
1997/0408/pc/devether.c (diff list | history)
| 1997/0408/sys/src/9/pc/devether.c:1,407 – 1997/0415/sys/src/9/pc/devether.c:1,429 (short | long | prev | next) | ||
| 1992/0403 | #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "io.h" | |
| 1993/1113 | #include "ureg.h" #include "../port/error.h" #include "../port/netif.h" | |
| 1992/0403 | ||
| 1993/1113 | #include "etherif.h" | |
| 1992/1222 | ||
| 1997/0327 | static Ether *etherxx[MaxEther]; | |
| 1992/0403 | ||
| 1993/1113 | Chan* | |
| 1997/0327 | etherattach(char* spec) | |
| 1993/1113 | { ulong ctlrno; char *p; | |
| 1997/0327 | Chan *chan; | |
| 1992/0404 | ||
| 1993/1113 | ctlrno = 0; if(spec && *spec){ ctlrno = strtoul(spec, &p, 0); if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther)) error(Ebadarg); } | |
| 1997/0327 | if(etherxx[ctlrno] == 0) | |
| 1993/1113 | error(Enodev); | |
| 1992/0403 | ||
| 1997/0327 | chan = devattach('l', spec); chan->dev = ctlrno; if(etherxx[ctlrno]->attach) etherxx[ctlrno]->attach(etherxx[ctlrno]); return chan; | |
| 1993/1113 | } | |
| 1992/0922 | ||
| 1997/0327 | static int etherwalk(Chan* chan, char* name) | |
| 1992/0411 | { | |
| 1997/0327 | return netifwalk(etherxx[chan->dev], chan, name); | |
| 1992/0424 | } | |
| 1992/0411 | ||
| 1997/0327 | static void etherstat(Chan* chan, char* dp) | |
| 1992/0424 | { | |
| 1997/0327 | netifstat(etherxx[chan->dev], chan, dp); | |
| 1992/0424 | } | |
| 1997/0327 | static Chan* etheropen(Chan* chan, int omode) | |
| 1992/0424 | { | |
| 1997/0327 | return netifopen(etherxx[chan->dev], chan, omode); | |
| 1992/0424 | } | |
| 1997/0327 | static void | |
| 1995/0726 | ethercreate(Chan*, char*, int, ulong) | |
| 1992/0424 | { } | |
| 1997/0327 | static void etherclose(Chan* chan) | |
| 1992/0424 | { | |
| 1997/0327 | netifclose(etherxx[chan->dev], chan); | |
| 1992/0424 | } | |
| 1997/0327 | static long etherread(Chan* chan, void* buf, long n, ulong offset) | |
| 1992/0424 | { | |
| 1997/0327 | Ether *ether; | |
| 1996/0607 | ||
| 1997/0327 | ether = etherxx[chan->dev]; if((chan->qid.path & CHDIR) == 0 && ether->ifstat){ | |
| 1996/0607 | /* * With some controllers it is necessary to reach * into the chip to extract statistics. */ | |
| 1997/0327 | if(NETTYPE(chan->qid.path) == Nifstatqid) return ether->ifstat(ether, buf, n, offset); else if(NETTYPE(chan->qid.path) == Nstatqid) ether->ifstat(ether, buf, 0, offset); | |
| 1996/0607 | } | |
| 1997/0327 | return netifread(ether, chan, buf, n, offset); | |
| 1992/0411 | } | |
| 1997/0327 | static Block* etherbread(Chan* chan, long n, ulong offset) | |
| 1995/0108 | { | |
| 1997/0327 | return netifbread(etherxx[chan->dev], chan, n, offset); | |
| 1995/0108 | } | |
| 1997/0327 | static void | |
| 1995/0822 | etherremove(Chan*) | |
| 1992/0424 | { } | |
| 1997/0327 | static void etherwstat(Chan* chan, char* dp) | |
| 1992/0424 | { | |
| 1997/0327 | netifwstat(etherxx[chan->dev], chan, dp); | |
| 1992/0424 | } | |
| 1997/0327 | static void etherrtrace(Netfile* f, Etherpkt* pkt, int len) | |
| 1993/1120 | { | |
| 1997/0327 | int i, n; | |
| 1995/0713 | Block *bp; | |
| 1997/0327 | if(qwindow(f->in) <= 0) return; if(len > 64) n = 64; else n = len; bp = iallocb(n); if(bp == 0) return; memmove(bp->wp, pkt->d, n); i = TK2MS(m->ticks); bp->wp[58] = len>>8; bp->wp[59] = len; bp->wp[60] = i>>24; bp->wp[61] = i>>16; bp->wp[62] = i>>8; bp->wp[63] = i; bp->wp += 64; qpass(f->in, bp); } Block* etheriq(Ether* ether, Block* bp, int freebp) { Etherpkt *pkt; | |
| 1993/1120 | ushort type; | |
| 1997/0327 | int len; Netfile **ep, *f, **fp, *fx; Block *xbp; | |
| 1993/1120 | ||
| 1997/0327 | ether->inpackets++; pkt = (Etherpkt*)bp->rp; len = BLEN(bp); | |
| 1993/1120 | type = (pkt->type[0]<<8)|pkt->type[1]; | |
| 1997/0327 | fx = 0; ep = ðer->f[Ntypes]; | |
| 1997/0404 | /* check for valid multcast addresses */ if((pkt->d[0] & 1) && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) != 0 && ether->prom == 0){ if(!activemulti(ether, pkt->d, sizeof(pkt->d))){ if(freebp){ freeb(bp); bp = 0; } return bp; } } | |
| 1997/0327 | /* * Multiplex the packet to all the connections which want it. * If the packet is not to be used subsequently (freebp != 0), * attempt to simply pass it into one of the connections, thereby * saving a copy of the data (usual case hopefully). */ for(fp = ether->f; fp < ep; fp++){ | |
| 1995/0721 | if((f = *fp) && (f->type == type || f->type < 0)){ | |
| 1995/0713 | if(f->type > -2){ | |
| 1997/0327 | if(freebp && fx == 0) fx = f; else if(xbp = iallocb(len)){ memmove(xbp->wp, pkt, len); xbp->wp += len; qpass(f->in, xbp); } | |
| 1995/0801 | else | |
| 1997/0327 | ether->soverflows++; | |
| 1995/0713 | } | |
| 1997/0327 | else etherrtrace(f, pkt, len); | |
| 1995/0713 | } | |
| 1993/1120 | } | |
| 1997/0327 | if(fx){ qpass(fx->in, bp); return 0; } if(freebp){ freeb(bp); return 0; } return bp; | |
| 1993/1120 | } static int | |
| 1997/0327 | etheroq(Ether* ether, Block* bp) | |
| 1993/1120 | { | |
| 1997/0327 | int len, loopback, s; Etherpkt *pkt; | |
| 1993/1120 | ||
| 1997/0327 | ether->outpackets++; | |
| 1993/1120 | ||
| 1997/0327 | /* * Check if the packet has to be placed back onto the input queue, * i.e. if it's a loopback or broadcast packet or the interface is * in promiscuous mode. * If it's a loopback packet indicate to etheriq that the data isn't * needed and return, etheriq will pass-on or free the block. */ pkt = (Etherpkt*)bp->rp; len = BLEN(bp); loopback = !memcmp(pkt->d, ether->ea, sizeof(pkt->d)); if(loopback || !memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) || ether->prom){ s = splhi(); etheriq(ether, bp, loopback); splx(s); } | |
| 1994/0702 | ||
| 1997/0327 | if(!loopback){ qbwrite(ether->oq, bp); ether->transmit(ether); } return len; | |
| 1993/1120 | } | |
| 1997/0327 | static long etherwrite(Chan* chan, void* buf, long n, ulong) | |
| 1992/0407 | { | |
| 1997/0327 | Ether *ether; Block *bp; | |
| 1992/0407 | ||
| 1993/1113 | if(n > ETHERMAXTU) error(Ebadarg); | |
| 1992/0407 | ||
| 1997/0327 | ether = etherxx[chan->dev]; if(NETTYPE(chan->qid.path) != Ndataqid) return netifwrite(ether, chan, buf, n); | |
| 1992/0403 | ||
| 1997/0327 | bp = allocb(n); | |
| 1992/0403 | if(waserror()){ | |
| 1997/0327 | freeb(bp); | |
| 1992/0403 | nexterror(); } | |
| 1997/0327 | memmove(bp->rp, buf, n); memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen); | |
| 1995/0822 | poperror(); | |
| 1997/0327 | bp->wp += n; | |
| 1992/0403 | ||
| 1997/0327 | return etheroq(ether, bp); | |
| 1995/0108 | } | |
| 1997/0327 | static long etherbwrite(Chan* chan, Block* bp, ulong) | |
| 1995/0108 | { | |
| 1997/0327 | Ether *ether; long n; n = BLEN(bp); if(n > ETHERMAXTU){ freeb(bp); error(Ebadarg); } ether = etherxx[chan->dev]; if(NETTYPE(chan->qid.path) != Ndataqid){ n = netifwrite(ether, chan, bp->rp, n); freeb(bp); return n; } return etheroq(ether, bp); | |
| 1992/0403 | } | |
| 1993/1113 | static struct { | |
| 1997/0327 | char* type; | |
| 1993/1113 | int (*reset)(Ether*); | |
| 1993/1119 | } cards[MaxEther+1]; | |
| 1992/0403 | ||
| 1993/1113 | void | |
| 1997/0327 | addethercard(char* t, int (*r)(Ether*)) | |
| 1992/0403 | { | |
| 1993/1113 | static int ncard; | |
| 1992/0403 | ||
| 1993/1119 | if(ncard == MaxEther) | |
| 1993/1113 | panic("too many ether cards"); cards[ncard].type = t; cards[ncard].reset = r; ncard++; | |
| 1992/0403 | } | |
| 1997/0327 | int parseether(uchar *to, char *from) { char nip[4]; char *p; int i; p = from; for(i = 0; i < 6; i++){ if(*p == 0) return -1; nip[0] = *p++; if(*p == 0) return -1; nip[1] = *p++; nip[2] = 0; to[i] = strtoul(nip, 0, 16); if(*p == ':') p++; } return 0; } static void | |
| 1992/0424 | etherreset(void) { | |
| 1997/0327 | Ether *ether; | |
| 1993/1113 | int i, n, ctlrno; | |
| 1997/0327 | char name[NAMELEN], buf[128]; | |
| 1992/0410 | ||
| 1997/0327 | for(ether = 0, ctlrno = 0; ctlrno < MaxEther; ctlrno++){ if(ether == 0) ether = malloc(sizeof(Ether)); memset(ether, 0, sizeof(Ether)); ether->ctlrno = ctlrno; ether->tbdf = BUSUNKNOWN; ether->mbps = 10; if(isaconfig("ether", ctlrno, ether) == 0) | |
| 1993/1113 | continue; for(n = 0; cards[n].type; n++){ | |
| 1997/0327 | if(cistrcmp(cards[n].type, ether->type)) | |
| 1993/1113 | continue; | |
| 1997/0327 | for(i = 0; i < ether->nopt; i++){ if(strncmp(ether->opt[i], "ea=", 3)) continue; if(parseether(ether->ea, ðer->opt[i][3]) == -1) memset(ether->ea, 0, Eaddrlen); } if(cards[n].reset(ether)) | |
| 1993/1113 | break; | |
| 1992/1222 | /* * IRQ2 doesn't really exist, it's used to gang the interrupt * controllers together. A device set to IRQ2 will appear on * the second interrupt controller as IRQ9. */ | |
| 1997/0327 | if(ether->irq == 2) ether->irq = 9; intrenable(VectorPIC+ether->irq, ether->interrupt, ether, ether->tbdf); | |
| 1993/0212 | ||
| 1997/0327 | i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX irq %d", ctlrno, ether->type, ether->mbps, ether->port, ether->irq); if(ether->mem) i += sprint(buf+i, " addr 0x%luX", ether->mem & ~KZERO); if(ether->size) i += sprint(buf+i, " size 0x%luX", ether->size); i += sprint(buf+i, ": %2.2uX%2.2uX%2.2uX%2.2uX%2.2uX%2.2uX", ether->ea[0], ether->ea[1], ether->ea[2], ether->ea[3], ether->ea[4], ether->ea[5]); sprint(buf+i, "\n"); print(buf); | |
| 1993/0212 | ||
| 1997/0327 | snprint(name, sizeof(name), "ether%d", ctlrno); if(ether->mbps == 100){ netifinit(ether, name, Ntypes, 256*1024); if(ether->oq == 0) ether->oq = qopen(256*1024, 1, 0, 0); } else{ netifinit(ether, name, Ntypes, 32*1024); if(ether->oq == 0) ether->oq = qopen(64*1024, 1, 0, 0); } ether->alen = Eaddrlen; memmove(ether->addr, ether->ea, Eaddrlen); memset(ether->bcast, 0xFF, Eaddrlen); | |
| 1992/0410 | ||
| 1997/0327 | etherxx[ctlrno] = ether; ether = 0; | |
| 1993/1120 | break; | |
| 1993/1113 | } | |
| 1992/0403 | } | |
| 1997/0327 | if(ether) free(ether); | |
| 1992/0403 | } | |
| 1997/0327 | ||
| 1997/0415 | #define POLY 0xedb88320 /* really slow 32 bit crc for ethers */ ulong ethercrc(uchar *p, int len) { int i, j; ulong crc, b; crc = 0xffffffff; for(i = 0; i < len; i++){ b = *p++; for(j = 0; j < 8; j++){ crc = (crc>>1); if((crc^b) & 1) crc ^= POLY; b >>= 1; } } return crc; } | |
| 1997/0327 | Dev etherdevtab = { | |
| 1997/0408 | 'l', "ether", | |
| 1997/0327 | etherreset, devinit, etherattach, devclone, etherwalk, etherstat, etheropen, ethercreate, etherclose, etherread, etherbread, etherwrite, etherbwrite, etherremove, etherwstat, }; | |