plan 9 kernel history: overview | file list | diff list

2000/0713/carrera/devether.c (diff list | history)

carrera/devether.c on 1997/1210
1997/1210    
#include "u.h" 
#include "../port/lib.h" 
#include "mem.h" 
#include "dat.h" 
#include "fns.h" 
#include "io.h" 
#include "ureg.h" 
#include "../port/error.h" 
#include "../port/netif.h" 
1993/0903    
 
1997/1210    
#include "etherif.h" 
1993/0903    
 
1997/1210    
static Ether *etherxx[MaxEther]; 
1993/0903    
 
1997/1210    
Chan* 
etherattach(char* spec) 
1993/0906    
{ 
1997/1210    
	ulong ctlrno; 
	char *p; 
	Chan *chan; 
1993/0906    
 
1997/1210    
	ctlrno = 0; 
	if(spec && *spec){ 
		ctlrno = strtoul(spec, &p, 0); 
		if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther)) 
			error(Ebadarg); 
	} 
	if(etherxx[ctlrno] == 0) 
		error(Enodev); 
1993/0903    
 
1997/1210    
	chan = devattach('l', spec); 
	chan->dev = ctlrno; 
	if(etherxx[ctlrno]->dev && etherxx[ctlrno]->dev->attach) 
		etherxx[ctlrno]->dev->attach(etherxx[ctlrno]); 
	return chan; 
} 
1993/0903    
 
1997/1210    
static int 
etherwalk(Chan* chan, char* name) 
1993/0904    
{ 
1997/1210    
	return netifwalk(etherxx[chan->dev], chan, name); 
} 
1993/0903    
 
1997/1210    
static void 
etherstat(Chan* chan, char* dp) 
1993/0904    
{ 
1997/1210    
	netifstat(etherxx[chan->dev], chan, dp); 
} 
1993/0903    
 
1997/1210    
static Chan* 
etheropen(Chan* chan, int omode) 
1993/0903    
{ 
1997/1210    
	return netifopen(etherxx[chan->dev], chan, omode); 
} 
1993/0903    
 
1997/1210    
static void 
ethercreate(Chan*, char*, int, ulong) 
1993/0904    
{ 
1997/1210    
} 
1993/0904    
 
1997/1210    
static void 
etherclose(Chan* chan) 
1993/0905    
{ 
1997/1210    
	netifclose(etherxx[chan->dev], chan); 
} 
1993/0904    
 
1997/1210    
static long 
1998/0319    
etherread(Chan* chan, void* buf, long n, vlong off) 
1993/0903    
{ 
1997/1210    
	Ether *ether; 
1998/0319    
	ulong offset = off; 
1993/0903    
 
1997/1210    
	ether = etherxx[chan->dev]; 
	if((chan->qid.path & CHDIR) == 0 && ether->dev && ether->dev->ifstat){ 
		/* 
		 * With some controllers it is necessary to reach 
		 * into the chip to extract statistics. 
		 */ 
		if(NETTYPE(chan->qid.path) == Nifstatqid) 
			return ether->dev->ifstat(ether, buf, n, offset); 
		else if(NETTYPE(chan->qid.path) == Nstatqid) 
			ether->dev->ifstat(ether, buf, 0, offset); 
	} 
1993/0903    
 
1997/1210    
	return netifread(ether, chan, buf, n, offset); 
} 
1993/0904    
 
1997/1210    
static Block* 
etherbread(Chan* chan, long n, ulong offset) 
1993/0906    
{ 
1997/1210    
	return netifbread(etherxx[chan->dev], chan, n, offset); 
1993/0906    
} 
 
static void 
1997/1210    
etherremove(Chan*) 
1993/0903    
{ 
} 
 
1997/0327    
static void 
1997/1210    
etherwstat(Chan* chan, char* dp) 
1993/0905    
{ 
1997/1210    
	netifwstat(etherxx[chan->dev], chan, dp); 
1993/0905    
} 
 
1997/1210    
static void 
etherrtrace(Netfile* f, Etherpkt* pkt, int len) 
1993/1212    
{ 
1997/1210    
	int i, n; 
	Block *bp; 
1993/1212    
 
1997/1210    
	if(qwindow(f->in) <= 0) 
		return; 
2000/0713    
	if(len > 58) 
		n = 58; 
1997/1210    
	else 
		n = len; 
2000/0713    
	bp = iallocb(64); 
	if(bp == nil) 
1997/1210    
		return; 
	memmove(bp->wp, pkt->d, n); 
	i = TK2MS(MACHP(0)->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); 
1993/1212    
} 
 
1997/1210    
Block* 
etheriq(Ether* ether, Block* bp, int freebp) 
1993/0903    
{ 
1997/1210    
	Etherpkt *pkt; 
	ushort type; 
	int len; 
	Netfile **ep, *f, **fp, *fx; 
	Block *xbp; 
1993/0903    
 
1997/1210    
	ether->inpackets++; 
1993/0903    
 
1997/1210    
	pkt = (Etherpkt*)bp->rp; 
	len = BLEN(bp); 
	type = (pkt->type[0]<<8)|pkt->type[1]; 
	fx = 0; 
	ep = ðer->f[Ntypes]; 
1993/0904    
 
1997/1210    
	/* check for valid multcast addresses */ 
	if((pkt->d[0] & 1) && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) && ether->prom == 0){ 
		if(!activemulti(ether, pkt->d, sizeof(pkt->d))){ 
			if(freebp){ 
				freeb(bp); 
				bp = 0; 
1993/0904    
			} 
1997/1210    
			return bp; 
1993/0903    
		} 
1997/1210    
	} 
1993/0904    
 
1997/1210    
	/* 
	 * 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++){ 
		if((f = *fp) && (f->type == type || f->type < 0)){ 
			if(f->type > -2){ 
				if(freebp && fx == 0) 
					fx = f; 
				else if(xbp = iallocb(len)){ 
					memmove(xbp->wp, pkt, len); 
					xbp->wp += len; 
					qpass(f->in, xbp); 
				} 
				else 
					ether->soverflows++; 
			} 
1993/0904    
			else 
1997/1210    
				etherrtrace(f, pkt, len); 
1993/0903    
		} 
1997/1210    
	} 
1993/0903    
 
1997/1210    
	if(fx){ 
		qpass(fx->in, bp); 
		return 0; 
1993/0903    
	} 
1997/1210    
	if(freebp){ 
		freeb(bp); 
		return 0; 
	} 
1993/0903    
 
1997/1210    
	return bp; 
1993/0903    
} 
 
1997/1210    
static int 
etheroq(Ether* ether, Block* bp) 
1993/0906    
{ 
1997/1210    
	int len, loopback, s; 
	Etherpkt *pkt; 
1993/0906    
 
1997/1210    
	ether->outpackets++; 
1993/0906    
 
	/* 
1997/1210    
	 * 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. 
1993/0906    
	 */ 
1997/1210    
	pkt = (Etherpkt*)bp->rp; 
	len = BLEN(bp); 
	loopback = (memcmp(pkt->d, ether->addr, sizeof(pkt->d)) == 0); 
	if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom){ 
		s = splhi(); 
		etheriq(ether, bp, loopback); 
		splx(s); 
1993/0906    
	} 
 
1997/1210    
	if(!loopback){ 
		if(ether->dev && ether->dev->transmit){ 
			qbwrite(ether->oq, bp); 
			ether->dev->transmit(ether); 
		} 
		else{ 
			freeb(bp); 
			return 0; 
		} 
1993/0903    
	} 
 
1997/1210    
	return len; 
1993/0903    
} 
 
1997/1210    
static long 
1998/0319    
etherwrite(Chan* chan, void* buf, long n, vlong) 
1993/0903    
{ 
1997/1210    
	Ether *ether; 
	Block *bp; 
1993/0905    
 
1997/1210    
	ether = etherxx[chan->dev]; 
	if(NETTYPE(chan->qid.path) != Ndataqid) 
		return netifwrite(ether, chan, buf, n); 
1993/0903    
 
1997/1210    
	if(n > ETHERMAXTU) 
		error(Etoobig); 
	if(n < ETHERMINTU) 
		error(Etoosmall); 
1993/0903    
 
1997/1210    
	bp = allocb(n); 
	if(waserror()){ 
		freeb(bp); 
		nexterror(); 
	} 
	memmove(bp->rp, buf, n); 
	memmove(bp->rp+Eaddrlen, ether->addr, Eaddrlen); 
	poperror(); 
	bp->wp += n; 
1993/0903    
 
1997/1210    
	return etheroq(ether, bp); 
1993/0903    
} 
 
1997/0327    
static long 
1997/1210    
etherbwrite(Chan* chan, Block* bp, ulong) 
1993/0903    
{ 
1997/1210    
	Ether *ether; 
	long n; 
1993/0903    
 
1997/1210    
	n = BLEN(bp); 
	ether = etherxx[chan->dev]; 
	if(NETTYPE(chan->qid.path) != Ndataqid){ 
		n = netifwrite(ether, chan, bp->rp, n); 
		freeb(bp); 
1993/1202    
		return n; 
1993/0918    
	} 
 
1997/1210    
	if(n > ETHERMAXTU){ 
		freeb(bp); 
		error(Ebadarg); 
1993/0903    
	} 
1997/1210    
	if(n < ETHERMINTU){ 
		freeb(bp); 
		error(Etoosmall); 
	} 
1993/0905    
 
1997/1210    
	return etheroq(ether, bp); 
1995/0108    
} 
 
1997/1210    
void 
etherreset(void) 
1993/0903    
{ 
1997/1210    
	Ether *ether; 
	int i, n, ctlrno; 
	char name[NAMELEN], buf[128]; 
1993/0903    
 
1997/1210    
	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) 
			continue; 
		for(n = 0; endev[n]; n++){ 
			if(cistrcmp(endev[n]->name, ether->type)) 
				continue; 
			ether->dev = endev[n]; 
			for(i = 0; i < ether->nopt; i++){ 
				if(strncmp(ether->opt[i], "ea=", 3)) 
					continue; 
				if(parseether(ether->addr, ðer->opt[i][3]) == -1) 
					memset(ether->addr, 0, Eaddrlen); 
			}	 
			if(endev[n]->reset(ether)) 
				break; 
1993/0903    
 
1997/1210    
			/* 
			 * 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. 
			 */ 
			if(ether->irq == 2) 
				ether->irq = 9; 
			intrenable(VectorPIC+ether->irq, ether->interrupt, ether, ether->tbdf); 
1993/0906    
 
1997/1210    
			i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX", 
				ctlrno, ether->type, ether->mbps, ether->port); 
			if(ether->irq) 
1999/0629    
				i += sprint(buf+i, " irq %ld", ether->irq); 
1997/1210    
			if(ether->mem) 
				i += sprint(buf+i, " addr 0x%luX", PADDR(ether->mem)); 
			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->addr[0], ether->addr[1], ether->addr[2], 
				ether->addr[3], ether->addr[4], ether->addr[5]); 
			sprint(buf+i, "\n"); 
			print(buf); 
1993/0906    
 
1997/1210    
			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); 
			} 
			if(ether->oq == 0) 
				panic("etherreset %s", name); 
1993/0906    
 
1997/1210    
			ether->alen = Eaddrlen; 
			memset(ether->bcast, 0xFF, Eaddrlen); 
			ether->arg = ether; 
			ether->promiscuous = ether->dev->promiscuous; 
			ether->multicast = ether->dev->multicast; 
 
			etherxx[ctlrno] = ether; 
			ether = 0; 
			break; 
		} 
1993/0906    
	} 
1997/1210    
	if(ether) 
		free(ether); 
1993/0903    
} 
1997/0327    
 
int 
1997/1210    
parseether(uchar* to, char* from) 
1997/0327    
{ 
	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; 
} 
 
1997/1210    
#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) ^ (((crc^b) & 1) ? POLY : 0); 
			b >>= 1; 
		} 
	} 
	return crc; 
} 
 
1997/0327    
Dev etherdevtab = { 
1997/0408    
	'l', 
	"ether", 
 
1997/0327    
	etherreset, 
	devinit, 
	etherattach, 
	devclone, 
	etherwalk, 
	etherstat, 
	etheropen, 
	ethercreate, 
	etherclose, 
	etherread, 
1997/1210    
	etherbread, 
1997/0327    
	etherwrite, 
1997/1210    
	etherbwrite, 
1997/0327    
	etherremove, 
	etherwstat, 
}; 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)