Home | History | Annotate | Line # | Download | only in ofw
ofnet.c revision 1.6
      1 /*	$NetBSD: ofnet.c,v 1.6 1997/04/16 23:41:19 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
      5  * Copyright (C) 1995, 1996 TooLs GmbH.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by TooLs GmbH.
     19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #include "ofnet.h"
     34 #include "bpfilter.h"
     35 
     36 #include <sys/param.h>
     37 #include <sys/device.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/mbuf.h>
     40 #include <sys/socket.h>
     41 #include <sys/syslog.h>
     42 
     43 #include <net/if.h>
     44 #include <net/if_ether.h>
     45 
     46 #ifdef INET
     47 #include <netinet/in.h>
     48 #include <netinet/if_inarp.h>
     49 #endif
     50 
     51 #if NBPFILTER > 0
     52 #include <net/bpf.h>
     53 #include <net/bpfdesc.h>
     54 #endif
     55 
     56 #include <dev/ofw/openfirm.h>
     57 
     58 #if NIPKDB_OFN > 0
     59 #include <ipkdb/ipkdb.h>
     60 #include <machine/ipkdb.h>
     61 
     62 struct cfattach ipkdb_ofn_ca = {
     63 	0, ipkdb_probe, ipkdb_attach
     64 };
     65 
     66 static struct ipkdb_if *kifp;
     67 static struct ofn_softc *ipkdb_of;
     68 
     69 static int ipkdbprobe __P((struct cfdata *, void *));
     70 #endif
     71 
     72 struct ofn_softc {
     73 	struct device sc_dev;
     74 	int sc_phandle;
     75 	int sc_ihandle;
     76 	struct ethercom sc_ethercom;
     77 };
     78 
     79 static int ofnprobe __P((struct device *, struct cfdata *, void *));
     80 static void ofnattach __P((struct device *, struct device *, void *));
     81 
     82 struct cfattach ofnet_ca = {
     83 	sizeof(struct ofn_softc), ofnprobe, ofnattach
     84 };
     85 
     86 struct cfdriver ofnet_cd = {
     87 	NULL, "ofnet", DV_IFNET
     88 };
     89 
     90 static void ofnread __P((struct ofn_softc *));
     91 static void ofntimer __P((struct ofn_softc *));
     92 static void ofninit __P((struct ofn_softc *));
     93 static void ofnstop __P((struct ofn_softc *));
     94 
     95 static void ofnstart __P((struct ifnet *));
     96 static int ofnioctl __P((struct ifnet *, u_long, caddr_t));
     97 static void ofnwatchdog __P((struct ifnet *));
     98 
     99 static int
    100 ofnprobe(parent, match, aux)
    101 	struct device *parent;
    102 	struct cfdata *match;
    103 	void *aux;
    104 {
    105 	struct ofprobe *ofp = aux;
    106 	char type[32];
    107 	int l;
    108 
    109 #if NIPKDB_OFN > 0
    110 	if (!parent)
    111 		return ipkdbprobe(match, aux);
    112 #endif
    113 	if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
    114 		return 0;
    115 	if (l >= sizeof type)
    116 		return 0;
    117 	type[l] = 0;
    118 	if (strcmp(type, "network"))
    119 		return 0;
    120 	return 1;
    121 }
    122 
    123 static void
    124 ofnattach(parent, self, aux)
    125 	struct device *parent, *self;
    126 	void *aux;
    127 {
    128 	struct ofn_softc *of = (void *)self;
    129 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    130 	struct ofprobe *ofp = aux;
    131 	char path[256];
    132 	int l;
    133 	u_int8_t myaddr[ETHER_ADDR_LEN];
    134 
    135 	of->sc_phandle = ofp->phandle;
    136 #if NIPKDB_OFN > 0
    137 	if (kifp
    138 	    && kifp->unit - 1 == of->sc_dev.dv_unit
    139 	    && OF_instance_to_package(kifp->port) == ofp->phandle)  {
    140 		ipkdb_of = of;
    141 		of->sc_ihandle = kifp->port;
    142 	} else
    143 #endif
    144 	if ((l = OF_package_to_path(ofp->phandle, path, sizeof path - 1)) < 0
    145 	    || l >= sizeof path
    146 	    || (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
    147 		panic("ofnattach: unable to open");
    148 	if (OF_getprop(ofp->phandle, "mac-address",
    149 		       myaddr, sizeof myaddr)
    150 	    < 0)
    151 		panic("ofnattach: no max-address");
    152 	printf(": address %s\n", ether_sprintf(myaddr));
    153 
    154 	bcopy(of->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    155 	ifp->if_softc = of;
    156 	ifp->if_start = ofnstart;
    157 	ifp->if_ioctl = ofnioctl;
    158 	ifp->if_watchdog = ofnwatchdog;
    159 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    160 
    161 	if_attach(ifp);
    162 	ether_ifattach(ifp, myaddr);
    163 
    164 #if NBPFILTER > 0
    165 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    166 #endif
    167 
    168 	dk_establish(0, self);					/* XXX */
    169 }
    170 
    171 static char buf[ETHERMTU + sizeof(struct ether_header)];
    172 
    173 static void
    174 ofnread(of)
    175 	struct ofn_softc *of;
    176 {
    177 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    178 	struct ether_header *eh;
    179 	struct mbuf *m, **mp, *head;
    180 	int l, len;
    181 	char *bufp;
    182 
    183 #if NIPKDB_OFN > 0
    184 	ipkdbrint(kifp, ifp);
    185 #endif
    186 	while (1) {
    187 		if ((len = OF_read(of->sc_ihandle, buf, sizeof buf)) < 0) {
    188 			if (len == -2)
    189 				return;
    190 			ifp->if_ierrors++;
    191 			continue;
    192 		}
    193 		if (len < sizeof(struct ether_header)) {
    194 			ifp->if_ierrors++;
    195 			continue;
    196 		}
    197 		bufp = buf;
    198 
    199 		/* Allocate a header mbuf */
    200 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    201 		if (m == 0) {
    202 			ifp->if_ierrors++;
    203 			continue;
    204 		}
    205 		m->m_pkthdr.rcvif = ifp;
    206 		m->m_pkthdr.len = len;
    207 		l = MHLEN;
    208 		head = 0;
    209 		mp = &head;
    210 
    211 		while (len > 0) {
    212 			if (head) {
    213 				MGET(m, M_DONTWAIT, MT_DATA);
    214 				if (m == 0) {
    215 					ifp->if_ierrors++;
    216 					m_freem(head);
    217 					head = 0;
    218 					break;
    219 				}
    220 				l = MLEN;
    221 			}
    222 			if (len >= MINCLSIZE) {
    223 				MCLGET(m, M_DONTWAIT);
    224 				if (m->m_flags & M_EXT)
    225 					l = MCLBYTES;
    226 			}
    227 			m->m_len = l = min(len, l);
    228 			bcopy(bufp, mtod(m, char *), l);
    229 			bufp += l;
    230 			len -= l;
    231 			*mp = m;
    232 			mp = &m->m_next;
    233 		}
    234 		if (head == 0)
    235 			continue;
    236 		eh = mtod(head, struct ether_header *);
    237 
    238 #if NBPFILTER > 0
    239 		if (ifp->if_bpf)
    240 			bpf_mtap(ifp->if_bpf, m);
    241 #endif
    242 		m_adj(head, sizeof(struct ether_header));
    243 		ifp->if_ipackets++;
    244 		ether_input(ifp, eh, head);
    245 	}
    246 }
    247 
    248 static void
    249 ofntimer(of)
    250 	struct ofn_softc *of;
    251 {
    252 	ofnread(of);
    253 	timeout(ofntimer, of, 1);
    254 }
    255 
    256 static void
    257 ofninit(of)
    258 	struct ofn_softc *of;
    259 {
    260 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    261 
    262 	if (ifp->if_flags & IFF_RUNNING)
    263 		return;
    264 
    265 	ifp->if_flags |= IFF_RUNNING;
    266 	/* Start reading from interface */
    267 	ofntimer(of);
    268 	/* Attempt to start output */
    269 	ofnstart(ifp);
    270 }
    271 
    272 static void
    273 ofnstop(of)
    274 	struct ofn_softc *of;
    275 {
    276 	untimeout(ofntimer, of);
    277 	of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
    278 }
    279 
    280 static void
    281 ofnstart(ifp)
    282 	struct ifnet *ifp;
    283 {
    284 	struct ofn_softc *of = ifp->if_softc;
    285 	struct mbuf *m, *m0;
    286 	char *bufp;
    287 	int len;
    288 
    289 	if (!(ifp->if_flags & IFF_RUNNING))
    290 		return;
    291 
    292 	for (;;) {
    293 		/* First try reading any packets */
    294 		ofnread(of);
    295 
    296 		/* Now get the first packet on the queue */
    297 		IF_DEQUEUE(&ifp->if_snd, m0);
    298 		if (!m0)
    299 			return;
    300 
    301 		if (!(m0->m_flags & M_PKTHDR))
    302 			panic("ofnstart: no header mbuf");
    303 		len = m0->m_pkthdr.len;
    304 
    305 #if NBPFILTER > 0
    306 		if (ifp->if_bpf)
    307 			bpf_mtap(ifp->if_bpf, m0);
    308 #endif
    309 
    310 		if (len > ETHERMTU + sizeof(struct ether_header)) {
    311 			/* packet too large, toss it */
    312 			ifp->if_oerrors++;
    313 			m_freem(m0);
    314 			continue;
    315 		}
    316 
    317 		for (bufp = buf; m = m0;) {
    318 			bcopy(mtod(m, char *), bufp, m->m_len);
    319 			bufp += m->m_len;
    320 			MFREE(m, m0);
    321 		}
    322 		if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf)
    323 			ifp->if_oerrors++;
    324 		else
    325 			ifp->if_opackets++;
    326 	}
    327 }
    328 
    329 static int
    330 ofnioctl(ifp, cmd, data)
    331 	struct ifnet *ifp;
    332 	u_long cmd;
    333 	caddr_t data;
    334 {
    335 	struct ofn_softc *of = ifp->if_softc;
    336 	struct ifaddr *ifa = (struct ifaddr *)data;
    337 	struct ifreq *ifr = (struct ifreq *)data;
    338 	int error = 0;
    339 
    340 	switch (cmd) {
    341 	case SIOCSIFADDR:
    342 		ifp->if_flags |= IFF_UP;
    343 
    344 		switch (ifa->ifa_addr->sa_family) {
    345 #ifdef	INET
    346 		case AF_INET:
    347 			arp_ifinit(ifp, ifa);
    348 			break;
    349 #endif
    350 		default:
    351 			break;
    352 		}
    353 		ofninit(of);
    354 		break;
    355 	case SIOCSIFFLAGS:
    356 		if (!(ifp->if_flags & IFF_UP)
    357 		    && (ifp->if_flags & IFF_RUNNING)) {
    358 			/* If interface is down, but running, stop it. */
    359 			ofnstop(of);
    360 		} else if ((ifp->if_flags & IFF_UP)
    361 			   && !(ifp->if_flags & IFF_RUNNING)) {
    362 			/* If interface is up, but not running, start it. */
    363 			ofninit(of);
    364 		} else {
    365 			/* Other flags are ignored. */
    366 		}
    367 		break;
    368 	default:
    369 		error = EINVAL;
    370 		break;
    371 	}
    372 	return error;
    373 }
    374 
    375 static void
    376 ofnwatchdog(ifp)
    377 	struct ifnet *ifp;
    378 {
    379 	struct ofn_softc *of = ifp->if_softc;
    380 
    381 	log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname);
    382 	ifp->if_oerrors++;
    383 	ofnstop(of);
    384 	ofninit(of);
    385 }
    386 
    387 #if NIPKDB_OFN > 0
    388 static void
    389 ipkdbofstart(kip)
    390 	struct ipkdb_if *kip;
    391 {
    392 	int unit = kip->unit - 1;
    393 
    394 	if (ipkdb_of)
    395 		ipkdbattach(kip, &ipkdb_of->sc_ethercom);
    396 }
    397 
    398 static void
    399 ipkdbofleave(kip)
    400 	struct ipkdb_if *kip;
    401 {
    402 }
    403 
    404 static int
    405 ipkdbofrcv(kip, buf, poll)
    406 	struct ipkdb_if *kip;
    407 	u_char *buf;
    408 	int poll;
    409 {
    410 	int l;
    411 
    412 	do {
    413 		l = OF_read(kip->port, buf, ETHERMTU);
    414 		if (l < 0)
    415 			l = 0;
    416 	} while (!poll && !l);
    417 	return l;
    418 }
    419 
    420 static void
    421 ipkdbofsend(kip, buf, l)
    422 	struct ipkdb_if *kip;
    423 	u_char *buf;
    424 	int l;
    425 {
    426 	OF_write(kip->port, buf, l);
    427 }
    428 
    429 static int
    430 ipkdbprobe(match, aux)
    431 	struct cfdata *match;
    432 	void *aux;
    433 {
    434 	struct ipkdb_if *kip = aux;
    435 	static char name[256];
    436 	int len;
    437 	int phandle;
    438 
    439 	kip->unit = match->cf_unit + 1;
    440 
    441 	if (!(kip->port = OF_open("net")))
    442 		return -1;
    443 	if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0
    444 	    || len >= sizeof name)
    445 		return -1;
    446 	name[len] = 0;
    447 	if ((phandle = OF_instance_to_package(kip->port)) == -1)
    448 		return -1;
    449 	if (OF_getprop(phandle, "mac-address", kip->myenetaddr, sizeof kip->myenetaddr)
    450 	    < 0)
    451 		return -1;
    452 
    453 	kip->flags |= IPKDB_MYHW;
    454 	kip->name = name;
    455 	kip->start = ipkdbofstart;
    456 	kip->leave = ipkdbofleave;
    457 	kip->receive = ipkdbofrcv;
    458 	kip->send = ipkdbofsend;
    459 
    460 	kifp = kip;
    461 
    462 	return 0;
    463 }
    464 #endif
    465