Home | History | Annotate | Line # | Download | only in ofw
ofnet.c revision 1.16
      1 /*	$NetBSD: ofnet.c,v 1.16 1999/05/04 23:56:54 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 "opt_inet.h"
     35 #include "bpfilter.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/socket.h>
     43 #include <sys/syslog.h>
     44 
     45 #include <net/if.h>
     46 #include <net/if_ether.h>
     47 
     48 #ifdef INET
     49 #include <netinet/in.h>
     50 #include <netinet/if_inarp.h>
     51 #endif
     52 
     53 #if NBPFILTER > 0
     54 #include <net/bpf.h>
     55 #include <net/bpfdesc.h>
     56 #endif
     57 
     58 #include <dev/ofw/openfirm.h>
     59 
     60 #if NIPKDB_OFN > 0
     61 #include <ipkdb/ipkdb.h>
     62 #include <machine/ipkdb.h>
     63 
     64 struct cfattach ipkdb_ofn_ca = {
     65 	0, ipkdb_probe, ipkdb_attach
     66 };
     67 
     68 static struct ipkdb_if *kifp;
     69 static struct ofnet_softc *ipkdb_of;
     70 
     71 static int ipkdbprobe __P((struct cfdata *, void *));
     72 #endif
     73 
     74 struct ofnet_softc {
     75 	struct device sc_dev;
     76 	int sc_phandle;
     77 	int sc_ihandle;
     78 	struct ethercom sc_ethercom;
     79 };
     80 
     81 static int ofnet_match __P((struct device *, struct cfdata *, void *));
     82 static void ofnet_attach __P((struct device *, struct device *, void *));
     83 
     84 struct cfattach ofnet_ca = {
     85 	sizeof(struct ofnet_softc), ofnet_match, ofnet_attach
     86 };
     87 
     88 static void ofnet_read __P((struct ofnet_softc *));
     89 static void ofnet_timer __P((void *));
     90 static void ofnet_init __P((struct ofnet_softc *));
     91 static void ofnet_stop __P((struct ofnet_softc *));
     92 
     93 static void ofnet_start __P((struct ifnet *));
     94 static int ofnet_ioctl __P((struct ifnet *, u_long, caddr_t));
     95 static void ofnet_watchdog __P((struct ifnet *));
     96 
     97 static int
     98 ofnet_match(parent, match, aux)
     99 	struct device *parent;
    100 	struct cfdata *match;
    101 	void *aux;
    102 {
    103 	struct ofbus_attach_args *oba = aux;
    104 	char type[32];
    105 	int l;
    106 
    107 #if NIPKDB_OFN > 0
    108 	if (!parent)
    109 		return ipkdbprobe(match, aux);
    110 #endif
    111 	if (strcmp(oba->oba_busname, "ofw"))
    112 		return (0);
    113 	if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
    114 	    sizeof type - 1)) < 0)
    115 		return 0;
    116 	if (l >= sizeof type)
    117 		return 0;
    118 	type[l] = 0;
    119 	if (strcmp(type, "network"))
    120 		return 0;
    121 	return 1;
    122 }
    123 
    124 static void
    125 ofnet_attach(parent, self, aux)
    126 	struct device *parent, *self;
    127 	void *aux;
    128 {
    129 	struct ofnet_softc *of = (void *)self;
    130 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    131 	struct ofbus_attach_args *oba = aux;
    132 	char path[256];
    133 	int l;
    134 	u_int8_t myaddr[ETHER_ADDR_LEN];
    135 
    136 	of->sc_phandle = oba->oba_phandle;
    137 #if NIPKDB_OFN > 0
    138 	if (kifp &&
    139 	    kifp->unit - 1 == of->sc_dev.dv_unit &&
    140 	    OF_instance_to_package(kifp->port) == oba->oba_phandle)  {
    141 		ipkdb_of = of;
    142 		of->sc_ihandle = kifp->port;
    143 	} else
    144 #endif
    145 	if ((l = OF_package_to_path(oba->oba_phandle, path,
    146 	    sizeof path - 1)) < 0 ||
    147 	    l >= sizeof path ||
    148 	    (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
    149 		panic("ofnet_attach: unable to open");
    150 	if (OF_getprop(oba->oba_phandle, "mac-address", myaddr,
    151 	    sizeof myaddr) < 0)
    152 		panic("ofnet_attach: no mac-address");
    153 	printf(": address %s\n", ether_sprintf(myaddr));
    154 
    155 	bcopy(of->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    156 	ifp->if_softc = of;
    157 	ifp->if_start = ofnet_start;
    158 	ifp->if_ioctl = ofnet_ioctl;
    159 	ifp->if_watchdog = ofnet_watchdog;
    160 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    161 
    162 	if_attach(ifp);
    163 	ether_ifattach(ifp, myaddr);
    164 
    165 #if NBPFILTER > 0
    166 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    167 #endif
    168 
    169 	dk_establish(0, self);					/* XXX */
    170 }
    171 
    172 static char buf[ETHERMTU + sizeof(struct ether_header)];
    173 
    174 static void
    175 ofnet_read(of)
    176 	struct ofnet_softc *of;
    177 {
    178 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    179 	struct ether_header *eh;
    180 	struct mbuf *m, **mp, *head;
    181 	int l, len;
    182 	char *bufp;
    183 
    184 #if NIPKDB_OFN > 0
    185 	ipkdbrint(kifp, ifp);
    186 #endif
    187 	while (1) {
    188 		if ((len = OF_read(of->sc_ihandle, buf, sizeof buf)) < 0) {
    189 			if (len == -2 || len == 0)
    190 				return;
    191 			ifp->if_ierrors++;
    192 			continue;
    193 		}
    194 		if (len < sizeof(struct ether_header)) {
    195 			ifp->if_ierrors++;
    196 			continue;
    197 		}
    198 		bufp = buf;
    199 
    200 		/* Allocate a header mbuf */
    201 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    202 		if (m == 0) {
    203 			ifp->if_ierrors++;
    204 			continue;
    205 		}
    206 		m->m_pkthdr.rcvif = ifp;
    207 		m->m_pkthdr.len = len;
    208 		l = MHLEN;
    209 		head = 0;
    210 		mp = &head;
    211 
    212 		while (len > 0) {
    213 			if (head) {
    214 				MGET(m, M_DONTWAIT, MT_DATA);
    215 				if (m == 0) {
    216 					ifp->if_ierrors++;
    217 					m_freem(head);
    218 					head = 0;
    219 					break;
    220 				}
    221 				l = MLEN;
    222 			}
    223 			if (len >= MINCLSIZE) {
    224 				MCLGET(m, M_DONTWAIT);
    225 				if ((m->m_flags & M_EXT) == 0) {
    226 					ifp->if_ierrors++;
    227 					m_free(m);
    228 					m_freem(head);
    229 					head = 0;
    230 					break;
    231 				}
    232 				l = MCLBYTES;
    233 			}
    234 
    235 			/*
    236 			 * Make sure the data after the Ethernet header
    237 			 * is aligned.
    238 			 *
    239 			 * XXX Assumes the device is an ethernet, but
    240 			 * XXX then so does other code in this driver.
    241 			 */
    242 			if (head == NULL) {
    243 				caddr_t newdata = (caddr_t)ALIGN(m->m_data +
    244 				      sizeof(struct ether_header)) -
    245 				    sizeof(struct ether_header);
    246 				l -= newdata - m->m_data;
    247 				m->m_data = newdata;
    248 			}
    249 
    250 			m->m_len = l = min(len, l);
    251 			bcopy(bufp, mtod(m, char *), l);
    252 			bufp += l;
    253 			len -= l;
    254 			*mp = m;
    255 			mp = &m->m_next;
    256 		}
    257 		if (head == 0)
    258 			continue;
    259 		eh = mtod(head, struct ether_header *);
    260 
    261 #if NBPFILTER > 0
    262 		if (ifp->if_bpf)
    263 			bpf_mtap(ifp->if_bpf, m);
    264 #endif
    265 		m_adj(head, sizeof(struct ether_header));
    266 		ifp->if_ipackets++;
    267 		ether_input(ifp, eh, head);
    268 	}
    269 }
    270 
    271 static void
    272 ofnet_timer(arg)
    273 	void *arg;
    274 {
    275 	struct ofnet_softc *of = arg;
    276 
    277 	ofnet_read(of);
    278 	timeout(ofnet_timer, of, 1);
    279 }
    280 
    281 static void
    282 ofnet_init(of)
    283 	struct ofnet_softc *of;
    284 {
    285 	struct ifnet *ifp = &of->sc_ethercom.ec_if;
    286 
    287 	if (ifp->if_flags & IFF_RUNNING)
    288 		return;
    289 
    290 	ifp->if_flags |= IFF_RUNNING;
    291 	/* Start reading from interface */
    292 	ofnet_timer(of);
    293 	/* Attempt to start output */
    294 	ofnet_start(ifp);
    295 }
    296 
    297 static void
    298 ofnet_stop(of)
    299 	struct ofnet_softc *of;
    300 {
    301 	untimeout(ofnet_timer, of);
    302 	of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
    303 }
    304 
    305 static void
    306 ofnet_start(ifp)
    307 	struct ifnet *ifp;
    308 {
    309 	struct ofnet_softc *of = ifp->if_softc;
    310 	struct mbuf *m, *m0;
    311 	char *bufp;
    312 	int len;
    313 
    314 	if (!(ifp->if_flags & IFF_RUNNING))
    315 		return;
    316 
    317 	for (;;) {
    318 		/* First try reading any packets */
    319 		ofnet_read(of);
    320 
    321 		/* Now get the first packet on the queue */
    322 		IF_DEQUEUE(&ifp->if_snd, m0);
    323 		if (!m0)
    324 			return;
    325 
    326 		if (!(m0->m_flags & M_PKTHDR))
    327 			panic("ofnet_start: no header mbuf");
    328 		len = m0->m_pkthdr.len;
    329 
    330 #if NBPFILTER > 0
    331 		if (ifp->if_bpf)
    332 			bpf_mtap(ifp->if_bpf, m0);
    333 #endif
    334 
    335 		if (len > ETHERMTU + sizeof(struct ether_header)) {
    336 			/* packet too large, toss it */
    337 			ifp->if_oerrors++;
    338 			m_freem(m0);
    339 			continue;
    340 		}
    341 
    342 		for (bufp = buf; m = m0;) {
    343 			bcopy(mtod(m, char *), bufp, m->m_len);
    344 			bufp += m->m_len;
    345 			MFREE(m, m0);
    346 		}
    347 		if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf)
    348 			ifp->if_oerrors++;
    349 		else
    350 			ifp->if_opackets++;
    351 	}
    352 }
    353 
    354 static int
    355 ofnet_ioctl(ifp, cmd, data)
    356 	struct ifnet *ifp;
    357 	u_long cmd;
    358 	caddr_t data;
    359 {
    360 	struct ofnet_softc *of = ifp->if_softc;
    361 	struct ifaddr *ifa = (struct ifaddr *)data;
    362 	struct ifreq *ifr = (struct ifreq *)data;
    363 	int error = 0;
    364 
    365 	switch (cmd) {
    366 	case SIOCSIFADDR:
    367 		ifp->if_flags |= IFF_UP;
    368 
    369 		switch (ifa->ifa_addr->sa_family) {
    370 #ifdef	INET
    371 		case AF_INET:
    372 			arp_ifinit(ifp, ifa);
    373 			break;
    374 #endif
    375 		default:
    376 			break;
    377 		}
    378 		ofnet_init(of);
    379 		break;
    380 	case SIOCSIFFLAGS:
    381 		if ((ifp->if_flags & IFF_UP) == 0 &&
    382 		    (ifp->if_flags & IFF_RUNNING) != 0) {
    383 			/* If interface is down, but running, stop it. */
    384 			ofnet_stop(of);
    385 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
    386 			   (ifp->if_flags & IFF_RUNNING) == 0) {
    387 			/* If interface is up, but not running, start it. */
    388 			ofnet_init(of);
    389 		} else {
    390 			/* Other flags are ignored. */
    391 		}
    392 		break;
    393 	default:
    394 		error = EINVAL;
    395 		break;
    396 	}
    397 	return error;
    398 }
    399 
    400 static void
    401 ofnet_watchdog(ifp)
    402 	struct ifnet *ifp;
    403 {
    404 	struct ofnet_softc *of = ifp->if_softc;
    405 
    406 	log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname);
    407 	ifp->if_oerrors++;
    408 	ofnet_stop(of);
    409 	ofnet_init(of);
    410 }
    411 
    412 #if NIPKDB_OFN > 0
    413 static void
    414 ipkdbofstart(kip)
    415 	struct ipkdb_if *kip;
    416 {
    417 	int unit = kip->unit - 1;
    418 
    419 	if (ipkdb_of)
    420 		ipkdbattach(kip, &ipkdb_of->sc_ethercom);
    421 }
    422 
    423 static void
    424 ipkdbofleave(kip)
    425 	struct ipkdb_if *kip;
    426 {
    427 }
    428 
    429 static int
    430 ipkdbofrcv(kip, buf, poll)
    431 	struct ipkdb_if *kip;
    432 	u_char *buf;
    433 	int poll;
    434 {
    435 	int l;
    436 
    437 	do {
    438 		l = OF_read(kip->port, buf, ETHERMTU);
    439 		if (l < 0)
    440 			l = 0;
    441 	} while (!poll && !l);
    442 	return l;
    443 }
    444 
    445 static void
    446 ipkdbofsend(kip, buf, l)
    447 	struct ipkdb_if *kip;
    448 	u_char *buf;
    449 	int l;
    450 {
    451 	OF_write(kip->port, buf, l);
    452 }
    453 
    454 static int
    455 ipkdbprobe(match, aux)
    456 	struct cfdata *match;
    457 	void *aux;
    458 {
    459 	struct ipkdb_if *kip = aux;
    460 	static char name[256];
    461 	int len;
    462 	int phandle;
    463 
    464 	kip->unit = match->cf_unit + 1;
    465 
    466 	if (!(kip->port = OF_open("net")))
    467 		return -1;
    468 	if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0 ||
    469 	    len >= sizeof name)
    470 		return -1;
    471 	name[len] = 0;
    472 	if ((phandle = OF_instance_to_package(kip->port)) == -1)
    473 		return -1;
    474 	if (OF_getprop(phandle, "mac-address", kip->myenetaddr,
    475 	    sizeof kip->myenetaddr) < 0)
    476 		return -1;
    477 
    478 	kip->flags |= IPKDB_MYHW;
    479 	kip->name = name;
    480 	kip->start = ipkdbofstart;
    481 	kip->leave = ipkdbofleave;
    482 	kip->receive = ipkdbofrcv;
    483 	kip->send = ipkdbofsend;
    484 
    485 	kifp = kip;
    486 
    487 	return 0;
    488 }
    489 #endif
    490