ofnet.c revision 1.7
1/* $NetBSD: ofnet.c,v 1.7 1997/04/24 02:08:46 mycroft 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 62struct cfattach ipkdb_ofn_ca = { 63 0, ipkdb_probe, ipkdb_attach 64}; 65 66static struct ipkdb_if *kifp; 67static struct ofn_softc *ipkdb_of; 68 69static int ipkdbprobe __P((struct cfdata *, void *)); 70#endif 71 72struct ofn_softc { 73 struct device sc_dev; 74 int sc_phandle; 75 int sc_ihandle; 76 struct ethercom sc_ethercom; 77}; 78 79static int ofnprobe __P((struct device *, struct cfdata *, void *)); 80static void ofnattach __P((struct device *, struct device *, void *)); 81 82struct cfattach ofnet_ca = { 83 sizeof(struct ofn_softc), ofnprobe, ofnattach 84}; 85 86struct cfdriver ofnet_cd = { 87 NULL, "ofnet", DV_IFNET 88}; 89 90static void ofnread __P((struct ofn_softc *)); 91static void ofntimer __P((struct ofn_softc *)); 92static void ofninit __P((struct ofn_softc *)); 93static void ofnstop __P((struct ofn_softc *)); 94 95static void ofnstart __P((struct ifnet *)); 96static int ofnioctl __P((struct ifnet *, u_long, caddr_t)); 97static void ofnwatchdog __P((struct ifnet *)); 98 99static int 100ofnprobe(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 123static void 124ofnattach(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 171static char buf[ETHERMTU + sizeof(struct ether_header)]; 172 173static void 174ofnread(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) == 0) 225 m_freem(head); 226 head = 0; 227 break; 228 } 229 l = MCLBYTES; 230 } 231 m->m_len = l = min(len, l); 232 bcopy(bufp, mtod(m, char *), l); 233 bufp += l; 234 len -= l; 235 *mp = m; 236 mp = &m->m_next; 237 } 238 if (head == 0) 239 continue; 240 eh = mtod(head, struct ether_header *); 241 242#if NBPFILTER > 0 243 if (ifp->if_bpf) 244 bpf_mtap(ifp->if_bpf, m); 245#endif 246 m_adj(head, sizeof(struct ether_header)); 247 ifp->if_ipackets++; 248 ether_input(ifp, eh, head); 249 } 250} 251 252static void 253ofntimer(of) 254 struct ofn_softc *of; 255{ 256 ofnread(of); 257 timeout(ofntimer, of, 1); 258} 259 260static void 261ofninit(of) 262 struct ofn_softc *of; 263{ 264 struct ifnet *ifp = &of->sc_ethercom.ec_if; 265 266 if (ifp->if_flags & IFF_RUNNING) 267 return; 268 269 ifp->if_flags |= IFF_RUNNING; 270 /* Start reading from interface */ 271 ofntimer(of); 272 /* Attempt to start output */ 273 ofnstart(ifp); 274} 275 276static void 277ofnstop(of) 278 struct ofn_softc *of; 279{ 280 untimeout(ofntimer, of); 281 of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING; 282} 283 284static void 285ofnstart(ifp) 286 struct ifnet *ifp; 287{ 288 struct ofn_softc *of = ifp->if_softc; 289 struct mbuf *m, *m0; 290 char *bufp; 291 int len; 292 293 if (!(ifp->if_flags & IFF_RUNNING)) 294 return; 295 296 for (;;) { 297 /* First try reading any packets */ 298 ofnread(of); 299 300 /* Now get the first packet on the queue */ 301 IF_DEQUEUE(&ifp->if_snd, m0); 302 if (!m0) 303 return; 304 305 if (!(m0->m_flags & M_PKTHDR)) 306 panic("ofnstart: no header mbuf"); 307 len = m0->m_pkthdr.len; 308 309#if NBPFILTER > 0 310 if (ifp->if_bpf) 311 bpf_mtap(ifp->if_bpf, m0); 312#endif 313 314 if (len > ETHERMTU + sizeof(struct ether_header)) { 315 /* packet too large, toss it */ 316 ifp->if_oerrors++; 317 m_freem(m0); 318 continue; 319 } 320 321 for (bufp = buf; m = m0;) { 322 bcopy(mtod(m, char *), bufp, m->m_len); 323 bufp += m->m_len; 324 MFREE(m, m0); 325 } 326 if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf) 327 ifp->if_oerrors++; 328 else 329 ifp->if_opackets++; 330 } 331} 332 333static int 334ofnioctl(ifp, cmd, data) 335 struct ifnet *ifp; 336 u_long cmd; 337 caddr_t data; 338{ 339 struct ofn_softc *of = ifp->if_softc; 340 struct ifaddr *ifa = (struct ifaddr *)data; 341 struct ifreq *ifr = (struct ifreq *)data; 342 int error = 0; 343 344 switch (cmd) { 345 case SIOCSIFADDR: 346 ifp->if_flags |= IFF_UP; 347 348 switch (ifa->ifa_addr->sa_family) { 349#ifdef INET 350 case AF_INET: 351 arp_ifinit(ifp, ifa); 352 break; 353#endif 354 default: 355 break; 356 } 357 ofninit(of); 358 break; 359 case SIOCSIFFLAGS: 360 if (!(ifp->if_flags & IFF_UP) 361 && (ifp->if_flags & IFF_RUNNING)) { 362 /* If interface is down, but running, stop it. */ 363 ofnstop(of); 364 } else if ((ifp->if_flags & IFF_UP) 365 && !(ifp->if_flags & IFF_RUNNING)) { 366 /* If interface is up, but not running, start it. */ 367 ofninit(of); 368 } else { 369 /* Other flags are ignored. */ 370 } 371 break; 372 default: 373 error = EINVAL; 374 break; 375 } 376 return error; 377} 378 379static void 380ofnwatchdog(ifp) 381 struct ifnet *ifp; 382{ 383 struct ofn_softc *of = ifp->if_softc; 384 385 log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname); 386 ifp->if_oerrors++; 387 ofnstop(of); 388 ofninit(of); 389} 390 391#if NIPKDB_OFN > 0 392static void 393ipkdbofstart(kip) 394 struct ipkdb_if *kip; 395{ 396 int unit = kip->unit - 1; 397 398 if (ipkdb_of) 399 ipkdbattach(kip, &ipkdb_of->sc_ethercom); 400} 401 402static void 403ipkdbofleave(kip) 404 struct ipkdb_if *kip; 405{ 406} 407 408static int 409ipkdbofrcv(kip, buf, poll) 410 struct ipkdb_if *kip; 411 u_char *buf; 412 int poll; 413{ 414 int l; 415 416 do { 417 l = OF_read(kip->port, buf, ETHERMTU); 418 if (l < 0) 419 l = 0; 420 } while (!poll && !l); 421 return l; 422} 423 424static void 425ipkdbofsend(kip, buf, l) 426 struct ipkdb_if *kip; 427 u_char *buf; 428 int l; 429{ 430 OF_write(kip->port, buf, l); 431} 432 433static int 434ipkdbprobe(match, aux) 435 struct cfdata *match; 436 void *aux; 437{ 438 struct ipkdb_if *kip = aux; 439 static char name[256]; 440 int len; 441 int phandle; 442 443 kip->unit = match->cf_unit + 1; 444 445 if (!(kip->port = OF_open("net"))) 446 return -1; 447 if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0 448 || len >= sizeof name) 449 return -1; 450 name[len] = 0; 451 if ((phandle = OF_instance_to_package(kip->port)) == -1) 452 return -1; 453 if (OF_getprop(phandle, "mac-address", kip->myenetaddr, sizeof kip->myenetaddr) 454 < 0) 455 return -1; 456 457 kip->flags |= IPKDB_MYHW; 458 kip->name = name; 459 kip->start = ipkdbofstart; 460 kip->leave = ipkdbofleave; 461 kip->receive = ipkdbofrcv; 462 kip->send = ipkdbofsend; 463 464 kifp = kip; 465 466 return 0; 467} 468#endif 469