| plan 9 kernel history: overview | file list | diff list |
1992/0410/pc/devether.c (diff list | history)
| pc/devether.c on 1992/0403 | ||
| 1992/0403 | /* * Western Digital ethernet adapter * BUGS: | |
| 1992/0404 | * no more than one controller | |
| 1992/0406 | * TODO: | |
| 1992/0403 | * fix for different controller types | |
| 1992/0406 | * output * deal with stalling and restarting output on input overflow * fix magic ring constants | |
| 1992/0407 | * rewrite per SMC doc | |
| 1992/0403 | */ #include "u.h" #include "../port/lib.h" #include "mem.h" #include "dat.h" #include "fns.h" #include "../port/error.h" #include "io.h" #include "devtab.h" | |
| 1992/0410 | static int debug; | |
| 1992/0403 | typedef struct Ctlr Ctlr; | |
| 1992/0410 | typedef struct Type Type; | |
| 1992/0404 | typedef struct Ring Ring; | |
| 1992/0403 | enum { IObase = 0x360, | |
| 1992/0410 | RAMbase = 0xD0000, | |
| 1992/0404 | RAMsize = 8*1024, BUFsize = 256, | |
| 1992/0403 | ||
| 1992/0410 | RINGbase = 6, /* gak */ RINGsize = 32, /* gak */ | |
| 1992/0404 | Nctlr = 1, | |
| 1992/0410 | NType = 9, /* types/interface */ | |
| 1992/0404 | }; | |
| 1992/0410 | #define NEXT(x, l) ((((x)+1)%(l)) == 0 ? RINGbase: (((x)+1)%(l))) #define PREV(x, l) (((x)-1) < RINGbase ? (l-1): ((x)-1)) | |
| 1992/0408 | ||
| 1992/0404 | /* * register offsets from IObase */ enum { | |
| 1992/0403 | EA = 0x08, /* Ethernet Address in ROM */ ID = 0x0E, /* interface type */ NIC = 0x10, /* National DP8390 Chip */ Cr = NIC+0x00, /* Page [01] */ | |
| 1992/0404 | Pstart = NIC+0x01, /* write */ Pstop = NIC+0x02, /* write */ Bnry = NIC+0x03, | |
| 1992/0403 | Tsr = NIC+0x04, /* read */ Tpsr = Tsr, /* write */ | |
| 1992/0404 | Tbcr0 = NIC+0x05, /* write */ Tbcr1 = NIC+0x06, /* write */ | |
| 1992/0403 | Isr = NIC+0x07, Rbcr0 = NIC+0x0A, Rbcr1 = NIC+0x0B, Rsr = NIC+0x0C, /* read */ Rcr = Rsr, /* write */ Cntr0 = NIC+0x0D, /* read */ Tcr = Cntr0, /* write */ Cntr1 = NIC+0x0E, /* read */ Dcr = Cntr1, /* write */ Cntr2 = NIC+0x0F, /* read */ Imr = Cntr2, /* write */ Par0 = NIC+0x01, /* Page 1 */ Curr = NIC+0x07, | |
| 1992/0404 | }; | |
| 1992/0403 | ||
| 1992/0404 | /* * some register bits */ enum { Prx = 0x01, /* Isr: packet received */ Ptx = 0x02, /* packet transmitted */ Rxe = 0x04, /* receive error */ Txe = 0x08, /* transmit error */ Ovw = 0x10, /* overwrite warning */ }; | |
| 1992/0403 | ||
| 1992/0404 | struct Ring { uchar status; uchar next; | |
| 1992/0407 | uchar len0; uchar len1; | |
| 1992/0404 | uchar data[BUFsize-4]; | |
| 1992/0403 | }; /* * one per ethernet packet type */ | |
| 1992/0410 | struct Type { | |
| 1992/0403 | QLock; int type; /* ethernet type */ int prom; /* promiscuous mode */ Queue *q; int inuse; Ctlr *ctlr; }; /* * per ethernet */ struct Ctlr { QLock; | |
| 1992/0407 | Ring *ring; | |
| 1992/0403 | Rendez rr; /* rendezvous for an input buffer */ | |
| 1992/0407 | Queue rq; | |
| 1992/0404 | uchar bnry; uchar curr; | |
| 1992/0403 | ||
| 1992/0407 | Etherpkt *xpkt; | |
| 1992/0404 | QLock xl; | |
| 1992/0403 | Rendez xr; | |
| 1992/0407 | uchar xbusy; | |
| 1992/0403 | int iobase; /* I/O base address */ | |
| 1992/0410 | Type type[NType]; | |
| 1992/0403 | uchar ea[6]; uchar ba[6]; uchar prom; /* true if promiscuous mode */ uchar kproc; /* true if kproc started */ char name[NAMELEN]; /* name of kproc */ Network net; | |
| 1992/0410 | Netprot prot[NType]; | |
| 1992/0403 | int inpackets; int outpackets; int crcs; /* input crc errors */ int oerrs; /* output errors */ int frames; /* framing errors */ int overflows; /* packet overflows */ int buffs; /* buffering errors */ }; static Ctlr ctlr[Nctlr]; | |
| 1992/0407 | static int isxfree(void *arg) { Ctlr *cp = arg; | |
| 1992/0409 | return cp->xbusy == 0; | |
| 1992/0407 | } | |
| 1992/0403 | static void etheroput(Queue *q, Block *bp) { | |
| 1992/0406 | Ctlr *cp; | |
| 1992/0403 | int len, n; | |
| 1992/0410 | Type *tp; | |
| 1992/0403 | Etherpkt *p; Block *nbp; | |
| 1992/0410 | cp = ((Type *)q->ptr)->ctlr; | |
| 1992/0403 | if(bp->type == M_CTL){ | |
| 1992/0410 | tp = q->ptr; | |
| 1992/0403 | if(streamparse("connect", bp)) | |
| 1992/0410 | tp->type = strtol((char *)bp->rptr, 0, 0); | |
| 1992/0403 | else if(streamparse("promiscuous", bp)) { | |
| 1992/0410 | tp->prom = 1; | |
| 1992/0406 | qlock(cp); cp->prom++; if(cp->prom == 1) outb(cp->iobase+Rcr, 0x14); /* PRO|AB */ qunlock(cp); | |
| 1992/0403 | } freeb(bp); return; } /* * give packet a local address, return upstream if destined for * this machine. */ | |
| 1992/0406 | if(BLEN(bp) < ETHERHDRSIZE && (bp = pullup(bp, ETHERHDRSIZE)) == 0) return; | |
| 1992/0403 | p = (Etherpkt *)bp->rptr; | |
| 1992/0406 | memmove(p->s, cp->ea, sizeof(cp->ea)); if(memcmp(cp->ea, p->d, sizeof(cp->ea)) == 0){ | |
| 1992/0403 | len = blen(bp); | |
| 1992/0406 | if (bp = expandb(bp, len >= ETHERMINTU ? len: ETHERMINTU)){ putq(&cp->rq, bp); wakeup(&cp->rr); | |
| 1992/0403 | } return; } | |
| 1992/0406 | if(memcmp(cp->ba, p->d, sizeof(cp->ba)) == 0){ | |
| 1992/0403 | len = blen(bp); nbp = copyb(bp, len); | |
| 1992/0406 | if(nbp = expandb(nbp, len >= ETHERMINTU ? len: ETHERMINTU)){ | |
| 1992/0403 | nbp->wptr = nbp->rptr+len; | |
| 1992/0406 | putq(&cp->rq, nbp); wakeup(&cp->rr); | |
| 1992/0403 | } } /* * only one transmitter at a time */ | |
| 1992/0407 | qlock(&cp->xl); | |
| 1992/0403 | if(waserror()){ | |
| 1992/0407 | qunlock(&cp->xl); | |
| 1992/0403 | nexterror(); } /* | |
| 1992/0407 | * Wait till we get an output buffer | |
| 1992/0403 | */ | |
| 1992/0407 | sleep(&cp->xr, isxfree, cp); p = cp->xpkt; | |
| 1992/0403 | /* * copy message into buffer */ len = 0; for(nbp = bp; nbp; nbp = nbp->next){ if(sizeof(Etherpkt) - len >= (n = BLEN(nbp))){ memmove(((uchar *)p)+len, nbp->rptr, n); len += n; } else print("no room damn it\n"); if(bp->flags & S_DELIM) break; } /* | |
| 1992/0407 | * pad the packet (zero the pad) | |
| 1992/0403 | */ if(len < ETHERMINTU){ memset(((char*)p)+len, 0, ETHERMINTU-len); len = ETHERMINTU; } /* | |
| 1992/0407 | * give packet a local address | |
| 1992/0403 | */ | |
| 1992/0407 | memmove(p->s, cp->ea, sizeof(cp->ea)); | |
| 1992/0403 | /* | |
| 1992/0407 | * start the transmission | |
| 1992/0403 | */ | |
| 1992/0407 | outb(cp->iobase+Tbcr0, len & 0xFF); outb(cp->iobase+Tbcr1, (len>>8) & 0xFF); outb(cp->iobase+Cr, 0x26); /* Page0|TXP|STA */ cp->xbusy = 1; | |
| 1992/0403 | freeb(bp); | |
| 1992/0407 | qunlock(&cp->xl); | |
| 1992/0403 | poperror(); } /* * open an ether line discipline * * the lock is to synchronize changing the ethertype with * sending packets up the stream on interrupts. */ static void etherstopen(Queue *q, Stream *s) { | |
| 1992/0404 | Ctlr *cp = &ctlr[0]; | |
| 1992/0410 | Type *tp; | |
| 1992/0403 | ||
| 1992/0410 | tp = &cp->type[s->id]; qlock(tp); RD(q)->ptr = WR(q)->ptr = tp; tp->type = 0; tp->q = RD(q); tp->inuse = 1; tp->ctlr = cp; qunlock(tp); | |
| 1992/0403 | } /* * close ether line discipline * * the lock is to synchronize changing the ethertype with * sending packets up the stream on interrupts. */ static void etherstclose(Queue *q) { | |
| 1992/0410 | Type *tp; | |
| 1992/0403 | ||
| 1992/0410 | tp = (Type *)(q->ptr); if(tp->prom){ qlock(tp->ctlr); tp->ctlr->prom--; if(tp->ctlr->prom == 0) outb(tp->ctlr->iobase+Rcr, 0x04);/* AB */ qunlock(tp->ctlr); | |
| 1992/0403 | } | |
| 1992/0410 | qlock(tp); tp->type = 0; tp->q = 0; tp->prom = 0; tp->inuse = 0; netdisown(&tp->ctlr->net, tp - tp->ctlr->type); qunlock(tp); | |
| 1992/0403 | } static Qinfo info = { nullput, etheroput, etherstopen, etherstclose, "ether" }; static int | |
| 1992/0404 | clonecon(Chan *c) | |
| 1992/0403 | { | |
| 1992/0404 | Ctlr *cp = &ctlr[0]; | |
| 1992/0410 | Type *tp; | |
| 1992/0403 | ||
| 1992/0410 | for(tp = cp->type; tp < &cp->type[NType]; tp++){ qlock(tp); if(tp->inuse || tp->q){ qunlock(tp); | |
| 1992/0403 | continue; } | |
| 1992/0410 | tp->inuse = 1; netown(&cp->net, tp - cp->type, u->p->user, 0); qunlock(tp); return tp - cp->type; | |
| 1992/0403 | } exhausted("ether channels"); } static void | |
| 1992/0404 | statsfill(Chan *c, char *p, int n) | |
| 1992/0403 | { | |
| 1992/0404 | Ctlr *cp = &ctlr[0]; | |
| 1992/0403 | char buf[256]; sprint(buf, "in: %d\nout: %d\ncrc errs %d\noverflows: %d\nframe errs %d\nbuff errs: %d\noerrs %d\naddr: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x\n", | |
| 1992/0404 | cp->inpackets, cp->outpackets, cp->crcs, cp->overflows, cp->frames, cp->buffs, cp->oerrs, cp->ea[0], cp->ea[1], cp->ea[2], cp->ea[3], cp->ea[4], cp->ea[5]); | |
| 1992/0403 | strncpy(p, buf, n); } static void typefill(Chan *c, char *p, int n) { char buf[16]; | |
| 1992/0410 | Type *tp; | |
| 1992/0403 | ||
| 1992/0410 | tp = &ctlr[0].type[STREAMID(c->qid.path)]; sprint(buf, "%d", tp->type); | |
| 1992/0403 | strncpy(p, buf, n); } static void intr(Ureg *ur) { | |
| 1992/0404 | Ctlr *cp = &ctlr[0]; | |
| 1992/0410 | uchar isr, bnry, curr; | |
| 1992/0404 | while(isr = inb(cp->iobase+Isr)){ outb(cp->iobase+Isr, isr); if(isr & Txe) cp->oerrs++; if(isr & Rxe){ cp->frames += inb(cp->iobase+Cntr0); cp->crcs += inb(cp->iobase+Cntr1); | |
| 1992/0410 | cp->overflows += inb(cp->iobase+Cntr2); | |
| 1992/0404 | } if(isr & Ptx) cp->outpackets++; if(isr & (Txe|Ptx)){ | |
| 1992/0407 | cp->xbusy = 0; wakeup(&cp->xr); | |
| 1992/0404 | } | |
| 1992/0410 | if(isr & Ovw){ bnry = inb(cp->iobase+Bnry); outb(cp->iobase+bnry, bnry); cp->buffs++; } | |
| 1992/0404 | /* * we have received packets. * this is the only place, other than the init code, * where we set the controller to Page1. | |
| 1992/0405 | * we must be sure to reset it back to Page0 in case | |
| 1992/0404 | * we interrupted some other part of this driver. */ | |
| 1992/0410 | if(isr & (Rxe|Prx)){ | |
| 1992/0404 | outb(cp->iobase+Cr, 0x62); /* Page1, RD2|STA */ cp->curr = inb(cp->iobase+Curr); outb(cp->iobase+Cr, 0x22); /* Page0, RD2|STA */ | |
| 1992/0410 | if(debug) print("I%d/%d/%d|", isr, cp->curr, cp->bnry); | |
| 1992/0404 | wakeup(&cp->rr); } } | |
| 1992/0403 | } /* * the following initialisation procedure | |
| 1992/0405 | * is mandatory. | |
| 1992/0404 | * we leave the chip idling on internal loopback | |
| 1992/0405 | * and pointing to Page0. | |
| 1992/0403 | */ static void | |
| 1992/0404 | init(Ctlr *cp) | |
| 1992/0403 | { int i; | |
| 1992/0404 | outb(cp->iobase+Cr, 0x21); /* Page0, RD2|STP */ outb(cp->iobase+Dcr, 0x48); /* FT1|LS */ outb(cp->iobase+Rbcr0, 0); outb(cp->iobase+Rbcr1, 0); outb(cp->iobase+Rcr, 0x04); /* AB */ outb(cp->iobase+Tcr, 0x20); /* LB0 */ | |
| 1992/0410 | cp->bnry = RINGbase; | |
| 1992/0408 | outb(cp->iobase+Bnry, cp->bnry); | |
| 1992/0407 | cp->ring = (Ring*)(KZERO|RAMbase); | |
| 1992/0410 | outb(cp->iobase+Pstart, RINGbase); outb(cp->iobase+Pstop, RINGsize); | |
| 1992/0404 | outb(cp->iobase+Isr, 0xFF); outb(cp->iobase+Imr, 0x1F); /* OVWE|TXEE|RXEE|PTXE|PRXE */ | |
| 1992/0403 | ||
| 1992/0404 | outb(cp->iobase+Cr, 0x61); /* Page1, RD2|STP */ for(i = 0; i < sizeof(cp->ea); i++) outb(cp->iobase+Par0+i, cp->ea[i]); | |
| 1992/0408 | cp->curr = cp->bnry+1; outb(cp->iobase+Curr, cp->curr); | |
| 1992/0404 | outb(cp->iobase+Cr, 0x22); /* Page0, RD2|STA */ outb(cp->iobase+Tpsr, 0); | |
| 1992/0407 | cp->xpkt = (Etherpkt*)(KZERO|RAMbase); | |
| 1992/0403 | } void etherreset(void) { | |
| 1992/0404 | Ctlr *cp = &ctlr[0]; | |
| 1992/0403 | int i; | |
| 1992/0409 | uchar msr; | |
| 1992/0403 | ||
| 1992/0404 | cp->iobase = IObase; | |
| 1992/0409 | msr = 0x40|inb(cp->iobase); outb(cp->iobase, msr); | |
| 1992/0410 | msr=0x40|inb(cp->iobase+0x05); outb(cp->iobase+0x05, msr); | |
| 1992/0409 | for(i = 0; i < 0x10; i++) print("#%2.2ux ", inb(cp->iobase+i)); print("\n"); | |
| 1992/0410 | msr=0x40|inb(cp->iobase+0x05); outb(cp->iobase+0x05, msr); | |
| 1992/0404 | for(i = 0; i < sizeof(cp->ea); i++) cp->ea[i] = inb(cp->iobase+EA+i); | |
| 1992/0408 | init(cp); setvec(Ethervec, intr); | |
| 1992/0404 | memset(cp->ba, 0xFF, sizeof(cp->ba)); | |
| 1992/0403 | ||
| 1992/0404 | cp->net.name = "ether"; | |
| 1992/0410 | cp->net.nconv = NType; | |
| 1992/0404 | cp->net.devp = &info; cp->net.protop = 0; cp->net.listen = 0; cp->net.clone = clonecon; cp->net.ninfo = 2; cp->net.prot = cp->prot; cp->net.info[0].name = "stats"; cp->net.info[0].fill = statsfill; cp->net.info[1].name = "type"; cp->net.info[1].fill = typefill; | |
| 1992/0403 | } | |
| 1992/0404 | static void | |
| 1992/0410 | etherup(Ctlr *cp, uchar *d0, int len0, uchar *d1, int len1) | |
| 1992/0404 | { | |
| 1992/0410 | Etherpkt *p; | |
| 1992/0404 | Block *bp; | |
| 1992/0410 | Type *tp; | |
| 1992/0404 | int t; | |
| 1992/0410 | p = (Etherpkt*)d0; t = (p->type[0]<<8)|p->type[1]; for(tp = &cp->type[0]; tp < &cp->type[NType]; tp++){ | |
| 1992/0404 | /* * check before locking just to save a lock */ | |
| 1992/0410 | if(tp->q == 0 || (t != tp->type && tp->type != -1)) | |
| 1992/0404 | continue; /* * only a trace channel gets packets destined for other machines */ | |
| 1992/0410 | if(tp->type != -1 && p->d[0] != 0xFF && memcmp(p->d, cp->ea, sizeof(p->d))) | |
| 1992/0404 | continue; /* * check after locking to make sure things didn't * change under foot */ | |
| 1992/0410 | if(canqlock(tp) == 0) | |
| 1992/0404 | continue; | |
| 1992/0410 | if(tp->q == 0 || tp->q->next->len > Streamhi || (t != tp->type && tp->type != -1)){ qunlock(tp); | |
| 1992/0404 | continue; } if(waserror() == 0){ | |
| 1992/0410 | bp = allocb(len0+len1); memmove(bp->rptr, d0, len0); if(len1) memmove(bp->rptr+len0, d1, len1); bp->wptr += len0+len1; | |
| 1992/0404 | bp->flags |= S_DELIM; | |
| 1992/0410 | PUTNEXT(tp->q, bp); | |
| 1992/0404 | } poperror(); | |
| 1992/0410 | qunlock(tp); | |
| 1992/0404 | } } | |
| 1992/0406 | static void printpkt(uchar bnry, ushort len, Etherpkt *p) { int i; print("%.2ux: %.4d d(%.2ux%.2ux%.2ux%.2ux%.2ux%.2ux)s(%.2ux%.2ux%.2ux%.2ux%.2ux%.2ux)t(%.2ux %.2ux)\n", bnry, len, p->d[0], p->d[1], p->d[2], p->d[3], p->d[4], p->d[5], p->s[0], p->s[1], p->s[2], p->s[3], p->s[4], p->s[5], p->type[0], p->type[1]); } | |
| 1992/0403 | static int isinput(void *arg) { | |
| 1992/0404 | Ctlr *cp = arg; | |
| 1992/0403 | ||
| 1992/0410 | return NEXT(cp->bnry, RINGsize) != cp->curr; | |
| 1992/0403 | } static void etherkproc(void *arg) { | |
| 1992/0404 | Ctlr *cp = arg; Block *bp; uchar bnry, curr; Ring *rp; | |
| 1992/0410 | int len0, len1; | |
| 1992/0403 | if(waserror()){ | |
| 1992/0404 | print("%s noted\n", cp->name); init(cp); cp->kproc = 0; | |
| 1992/0403 | nexterror(); } | |
| 1992/0404 | cp->kproc = 1; | |
| 1992/0403 | for(;;){ | |
| 1992/0404 | sleep(&cp->rr, isinput, cp); | |
| 1992/0406 | /* * process any internal loopback packets */ | |
| 1992/0404 | while(bp = getq(&cp->rq)){ cp->inpackets++; | |
| 1992/0410 | etherup(cp, bp->rptr, BLEN(bp), 0, 0); | |
| 1992/0404 | freeb(bp); } | |
| 1992/0408 | ||
| 1992/0406 | /* * process any received packets */ | |
| 1992/0410 | bnry = NEXT(cp->bnry, RINGsize); | |
| 1992/0408 | while(bnry != cp->curr){ rp = &cp->ring[bnry]; | |
| 1992/0406 | cp->inpackets++; | |
| 1992/0410 | len0 = ((rp->len1<<8)+rp->len0)-4; len1 = 0; if(rp->data+len0 >= (uchar*)&cp->ring[RINGsize]){ len1 = rp->data+len0 - (uchar*)&cp->ring[RINGsize]; len0 = (uchar*)&cp->ring[RINGsize] - rp->data; } etherup(cp, rp->data, len0, (uchar*)&cp->ring[RINGbase], len1); if(debug) print("K%d/%d/%d|", bnry, rp->next, PREV(rp->next, RINGsize)); | |
| 1992/0408 | bnry = rp->next; | |
| 1992/0410 | cp->bnry = PREV(bnry, RINGsize); | |
| 1992/0407 | outb(cp->iobase+Bnry, cp->bnry); | |
| 1992/0406 | } | |
| 1992/0403 | } } void etherinit(void) { int ctlrno = 0; /* * put the receiver online * and start the kproc */ | |
| 1992/0404 | outb(ctlr[ctlrno].iobase+Tcr, 0); | |
| 1992/0403 | if(ctlr[ctlrno].kproc == 0){ sprint(ctlr[ctlrno].name, "ether%dkproc", ctlrno); kproc(ctlr[ctlrno].name, etherkproc, &ctlr[ctlrno]); } } Chan * etherattach(char *spec) { return devattach('l', spec); } Chan * etherclone(Chan *c, Chan *nc) { return devclone(c, nc); } int etherwalk(Chan *c, char *name) { return netwalk(c, name, &ctlr[0].net); } void etherstat(Chan *c, char *dp) { netstat(c, dp, &ctlr[0].net); } Chan * etheropen(Chan *c, int omode) { return netopen(c, omode, &ctlr[0].net); } void ethercreate(Chan *c, char *name, int omode, ulong perm) { error(Eperm); } void etherremove(Chan *c) { error(Eperm); } void etherwstat(Chan *c, char *dp) { netwstat(c, dp, &ctlr[0].net); } void etherclose(Chan *c) { if(c->stream) streamclose(c); } long etherread(Chan *c, void *a, long n, ulong offset) { return netread(c, a, n, offset, &ctlr[0].net); } long etherwrite(Chan *c, char *a, long n, ulong offset) { return streamwrite(c, a, n, 0); | |
| 1992/0409 | } void etherdump(void) { Ctlr *cp = &ctlr[0]; uchar bnry, curr, isr; int s; s = splhi(); | |
| 1992/0410 | debug++; | |
| 1992/0409 | bnry = inb(cp->iobase+Bnry); outb(cp->iobase+Cr, 0x62); /* Page1, RD2|STA */ curr = inb(cp->iobase+Curr); outb(cp->iobase+Cr, 0x22); /* Page0, RD2|STA */ isr = inb(cp->iobase+Isr); print("b%d c%d x%d B%d C%d I%ux", cp->bnry, cp->curr, cp->xbusy, bnry, curr, isr); | |
| 1992/0410 | print("\t%d %d %d %d %d %d %d\n", cp->inpackets, cp->outpackets, | |
| 1992/0409 | cp->crcs, cp->oerrs, cp->frames, cp->overflows, cp->buffs); splx(s); | |
| 1992/0403 | } | |