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