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