shithub: riscv

Download patch

ref: f0a314605f1a6d56da34ac07bb4effe2dcff8c37
parent: 5560efb3dbe76bfab78d0d7f97969d7589a3f171
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Feb 24 22:42:38 EST 2018

devether: remove (unimplemented) detach, allow device creation on attach

we allow devether to create ethernet cards on attach. this is useull
for virtual cards like the sink driver, so we can create a sink
by simply: bind -a '#l2:sink ea=112233445566' /net

the detach routine was never called, so remove it from the few drivers
that attempted to implement it.

--- a/sys/src/9/pc/etherm10g.c
+++ b/sys/src/9/pc/etherm10g.c
@@ -1614,7 +1614,6 @@
 	memmove(e->ea, c->ra, Eaddrlen);
 
 	e->attach = m10gattach;
-	e->detach = m10gshutdown;
 	e->transmit = m10gtransmit;
 	e->interrupt = m10ginterrupt;
 	e->ifstat = m10gifstat;
--- a/sys/src/9/pc/wavelan.c
+++ b/sys/src/9/pc/wavelan.c
@@ -1234,7 +1234,6 @@
 	ether->ctlr = ctlr;
 	ether->mbps = 10;
 	ether->attach = w_attach;
-	ether->detach = w_detach;
 	ether->transmit = w_transmit;
 	ether->ifstat = w_ifstat;
 	ether->ctl = w_ctl;
--- a/sys/src/9/port/devether.c
+++ b/sys/src/9/port/devether.c
@@ -14,23 +14,35 @@
 extern ushort ipcsum(uchar *);
 
 static Ether *etherxx[MaxEther];
+static Ether *etherprobe(int cardno, int ctlrno, char *conf);
 
 Chan*
 etherattach(char* spec)
 {
 	ulong ctlrno;
-	char *p;
+	char *conf;
 	Chan *chan;
 
 	ctlrno = 0;
-	if(spec && *spec){
-		ctlrno = strtoul(spec, &p, 0);
-		if((ctlrno == 0 && p == spec) || *p || (ctlrno >= MaxEther))
-			error(Ebadarg);
+	if(*spec){
+		ctlrno = strtoul(spec, &conf, 0);
+		if(ctlrno >= MaxEther)
+			error(Enodev);
+		if(conf == spec)
+			error(Ebadspec);
+		if(*conf){
+			if(*conf != ':')
+				error(Ebadspec);
+			*conf++ = 0;
+			if(!iseve())
+				error(Enoattach);
+			if(etherxx[ctlrno] != nil)
+				error(Einuse);
+			etherxx[ctlrno] = etherprobe(-1, ctlrno, conf);
+		}
 	}
-	if(etherxx[ctlrno] == 0)
+	if(etherxx[ctlrno] == nil)
 		error(Enodev);
-
 	chan = devattach('l', spec);
 	if(waserror()){
 		chanfree(chan);
@@ -350,7 +362,7 @@
 }
 
 static Ether*
-etherprobe(int cardno, int ctlrno)
+etherprobe(int cardno, int ctlrno, char *conf)
 {
 	int i, lg;
 	ulong mb, bsz;
@@ -370,36 +382,41 @@
 	ether->maxmtu = ETHERMAXTU;
 
 	if(cardno < 0){
-		if(isaconfig("ether", ctlrno, ether) == 0){
-			free(ether);
-			return nil;
-		}
-		for(cardno = 0; cards[cardno].type; cardno++){
-			if(cistrcmp(cards[cardno].type, ether->type))
-				continue;
-			for(i = 0; i < ether->nopt; i++){
-				if(strncmp(ether->opt[i], "ea=", 3))
-					continue;
+		if(conf != nil){
+			kstrdup(&ether->type, conf);
+			ether->nopt = tokenize(ether->type, ether->opt, nelem(ether->opt));
+			if(ether->nopt < 1)
+				goto Nope;
+			memmove(&ether->opt[0], &ether->opt[1], --ether->nopt*sizeof(ether->opt[0]));
+		} else if(isaconfig("ether", ctlrno, ether) == 0)
+			goto Nope;
+
+		for(cardno = 0; cards[cardno].type != nil; cardno++)
+			if(cistrcmp(cards[cardno].type, ether->type) == 0)
+				break;
+		if(cards[cardno].type == nil)
+			goto Nope;
+
+		for(i = 0; i < ether->nopt; i++){
+			if(strncmp(ether->opt[i], "ea=", 3) == 0){
 				if(parseether(ether->ea, &ether->opt[i][3]))
 					memset(ether->ea, 0, Eaddrlen);
 			}
-			break;
 		}
 	}
-
-	if(cardno >= MaxEther || cards[cardno].type == nil){
-		free(ether);
-		return nil;
-	}
+	if(cardno >= MaxEther || cards[cardno].type == nil)
+		goto Nope;
 	snprint(ether->name, sizeof(ether->name), "ether%d", ctlrno);
 	if(cards[cardno].reset(ether) < 0){
+Nope:
+		if(conf != nil) free(ether->type);	/* see kstrdup() above */
 		free(ether);
 		return nil;
 	}
+	ether->type = cards[cardno].type;
 
 	print("#l%d: %s: %dMbps port 0x%luX irq %d ea %E\n",
-		ctlrno, cards[cardno].type,
-		ether->mbps, ether->port, ether->irq, ether->ea);
+		ctlrno, ether->type, ether->mbps, ether->port, ether->irq, ether->ea);
 
 	/* compute log10(ether->mbps) into lg */
 	for(lg = 0, mb = ether->mbps; mb >= 10; lg++)
@@ -439,7 +456,7 @@
 	fmtinstall('E', eipfmt);
 
 	for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
-		if((ether = etherprobe(-1, ctlrno)) == nil)
+		if((ether = etherprobe(-1, ctlrno, nil)) == nil)
 			continue;
 		etherxx[ctlrno] = ether;
 	}
@@ -451,7 +468,7 @@
 				ctlrno++;
 				continue;
 			}
-			if((ether = etherprobe(cardno, ctlrno)) == nil){
+			if((ether = etherprobe(cardno, ctlrno, nil)) == nil){
 				cardno++;
 				continue;
 			}
--- a/sys/src/9/port/etherif.h
+++ b/sys/src/9/port/etherif.h
@@ -20,7 +20,6 @@
 	int 	maxmtu;
 
 	void	(*attach)(Ether*);	/* filled in by reset routine */
-	void	(*detach)(Ether*);
 	void	(*transmit)(Ether*);
 	long	(*ifstat)(Ether*, void*, long, ulong);
 	long 	(*ctl)(Ether*, void*, long); /* custom ctl messages */
--- a/sys/src/9/port/netif.c
+++ b/sys/src/9/port/netif.c
@@ -80,7 +80,7 @@
 			break;
 		case 1:
 			q.path = Naddrqid;
-			devdir(c, q, "addr", 0, eve, 0666, dp);
+			devdir(c, q, "addr", 0, eve, 0444, dp);
 			break;
 		case 2:
 			q.path = Nstatqid;
--- a/sys/src/9/xen/etherxen.c
+++ b/sys/src/9/xen/etherxen.c
@@ -474,7 +474,6 @@
 	memmove(ether->ea, ea, sizeof ether->ea);
 	ether->mbps = 100;	// XXX what speed?
 	ether->attach = etherxenattach;
-	ether->detach = nil;
 	ether->transmit = etherxentransmit;
 	ether->irq = -1;
 	ether->tbdf = BUSUNKNOWN;
--- a/sys/src/9/zynq/dat.h
+++ b/sys/src/9/zynq/dat.h
@@ -159,6 +159,7 @@
 	int	stack[1];
 };
 
+#define NISAOPT		8
 struct ISAConf
 {
 	char	*type;
@@ -165,7 +166,7 @@
 	ulong	port;
 	int	irq;
 	int	nopt;
-	char	*opt[1];
+	char	*opt[NISAOPT];
 };
 #define BUSUNKNOWN -1