ofnet.c revision 1.23
1/* $NetBSD: ofnet.c,v 1.23 2001/11/13 07:26:28 lukem 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.23 2001/11/13 07:26:28 lukem 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 70struct cfattach ipkdb_ofn_ca = { 71 0, ipkdb_probe, ipkdb_attach 72}; 73 74static struct ipkdb_if *kifp; 75static struct ofnet_softc *ipkdb_of; 76 77static int ipkdbprobe (struct cfdata *, void *); 78#endif 79 80struct 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 88static int ofnet_match (struct device *, struct cfdata *, void *); 89static void ofnet_attach (struct device *, struct device *, void *); 90 91struct cfattach ofnet_ca = { 92 sizeof(struct ofnet_softc), ofnet_match, ofnet_attach 93}; 94 95static void ofnet_read (struct ofnet_softc *); 96static void ofnet_timer (void *); 97static void ofnet_init (struct ofnet_softc *); 98static void ofnet_stop (struct ofnet_softc *); 99 100static void ofnet_start (struct ifnet *); 101static int ofnet_ioctl (struct ifnet *, u_long, caddr_t); 102static void ofnet_watchdog (struct ifnet *); 103 104static int 105ofnet_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 128static void 129ofnet_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 166 if_attach(ifp); 167 ether_ifattach(ifp, myaddr); 168 169#ifdef __BROKEN_DK_ESTABLISH 170 dk_establish(0, self); /* XXX */ 171#endif 172} 173 174static char buf[ETHERMTU + sizeof(struct ether_header)]; 175 176static void 177ofnet_read(struct ofnet_softc *of) 178{ 179 struct ifnet *ifp = &of->sc_ethercom.ec_if; 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 209 /* 210 * We don't know if the interface included the FCS 211 * or not. For now, assume that it did if we got 212 * a packet length that looks like it could include 213 * the FCS. 214 * 215 * XXX Yuck. 216 */ 217 218 if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN)) 219 m->m_flags |= M_HASFCS; 220 221 l = MHLEN; 222 head = 0; 223 mp = &head; 224 225 while (len > 0) { 226 if (head) { 227 MGET(m, M_DONTWAIT, MT_DATA); 228 if (m == 0) { 229 ifp->if_ierrors++; 230 m_freem(head); 231 head = 0; 232 break; 233 } 234 l = MLEN; 235 } 236 if (len >= MINCLSIZE) { 237 MCLGET(m, M_DONTWAIT); 238 if ((m->m_flags & M_EXT) == 0) { 239 ifp->if_ierrors++; 240 m_free(m); 241 m_freem(head); 242 head = 0; 243 break; 244 } 245 l = MCLBYTES; 246 } 247 248 /* 249 * Make sure the data after the Ethernet header 250 * is aligned. 251 * 252 * XXX Assumes the device is an ethernet, but 253 * XXX then so does other code in this driver. 254 */ 255 if (head == NULL) { 256 caddr_t newdata = (caddr_t)ALIGN(m->m_data + 257 sizeof(struct ether_header)) - 258 sizeof(struct ether_header); 259 l -= newdata - m->m_data; 260 m->m_data = newdata; 261 } 262 263 m->m_len = l = min(len, l); 264 bcopy(bufp, mtod(m, char *), l); 265 bufp += l; 266 len -= l; 267 *mp = m; 268 mp = &m->m_next; 269 } 270 if (head == 0) 271 continue; 272 273#if NBPFILTER > 0 274 if (ifp->if_bpf) 275 bpf_mtap(ifp->if_bpf, m); 276#endif 277 ifp->if_ipackets++; 278 (*ifp->if_input)(ifp, head); 279 } 280} 281 282static void 283ofnet_timer(arg) 284 void *arg; 285{ 286 struct ofnet_softc *of = arg; 287 288 ofnet_read(of); 289 callout_reset(&of->sc_callout, 1, ofnet_timer, of); 290} 291 292static void 293ofnet_init(struct ofnet_softc *of) 294{ 295 struct ifnet *ifp = &of->sc_ethercom.ec_if; 296 297 if (ifp->if_flags & IFF_RUNNING) 298 return; 299 300 ifp->if_flags |= IFF_RUNNING; 301 /* Start reading from interface */ 302 ofnet_timer(of); 303 /* Attempt to start output */ 304 ofnet_start(ifp); 305} 306 307static void 308ofnet_stop(struct ofnet_softc *of) 309{ 310 callout_stop(&of->sc_callout); 311 of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING; 312} 313 314static void 315ofnet_start(struct ifnet *ifp) 316{ 317 struct ofnet_softc *of = ifp->if_softc; 318 struct mbuf *m, *m0; 319 char *bufp; 320 int len; 321 322 if (!(ifp->if_flags & IFF_RUNNING)) 323 return; 324 325 for (;;) { 326 /* First try reading any packets */ 327 ofnet_read(of); 328 329 /* Now get the first packet on the queue */ 330 IF_DEQUEUE(&ifp->if_snd, m0); 331 if (!m0) 332 return; 333 334 if (!(m0->m_flags & M_PKTHDR)) 335 panic("ofnet_start: no header mbuf"); 336 len = m0->m_pkthdr.len; 337 338#if NBPFILTER > 0 339 if (ifp->if_bpf) 340 bpf_mtap(ifp->if_bpf, m0); 341#endif 342 343 if (len > ETHERMTU + sizeof(struct ether_header)) { 344 /* packet too large, toss it */ 345 ifp->if_oerrors++; 346 m_freem(m0); 347 continue; 348 } 349 350 for (bufp = buf; (m = m0) != NULL;) { 351 bcopy(mtod(m, char *), bufp, m->m_len); 352 bufp += m->m_len; 353 MFREE(m, m0); 354 } 355 356 /* 357 * We don't know if the interface will auto-pad for 358 * us, so make sure it's at least as large as a 359 * minimum size Ethernet packet. 360 */ 361 362 if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) 363 len = ETHER_MIN_LEN - ETHER_CRC_LEN; 364 else 365 len = bufp - buf; 366 367 if (OF_write(of->sc_ihandle, buf, len) != len) 368 ifp->if_oerrors++; 369 else 370 ifp->if_opackets++; 371 } 372} 373 374static int 375ofnet_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 376{ 377 struct ofnet_softc *of = ifp->if_softc; 378 struct ifaddr *ifa = (struct ifaddr *)data; 379 /* struct ifreq *ifr = (struct ifreq *)data; */ 380 int error = 0; 381 382 switch (cmd) { 383 case SIOCSIFADDR: 384 ifp->if_flags |= IFF_UP; 385 386 switch (ifa->ifa_addr->sa_family) { 387#ifdef INET 388 case AF_INET: 389 arp_ifinit(ifp, ifa); 390 break; 391#endif 392 default: 393 break; 394 } 395 ofnet_init(of); 396 break; 397 case SIOCSIFFLAGS: 398 if ((ifp->if_flags & IFF_UP) == 0 && 399 (ifp->if_flags & IFF_RUNNING) != 0) { 400 /* If interface is down, but running, stop it. */ 401 ofnet_stop(of); 402 } else if ((ifp->if_flags & IFF_UP) != 0 && 403 (ifp->if_flags & IFF_RUNNING) == 0) { 404 /* If interface is up, but not running, start it. */ 405 ofnet_init(of); 406 } else { 407 /* Other flags are ignored. */ 408 } 409 break; 410 default: 411 error = EINVAL; 412 break; 413 } 414 return error; 415} 416 417static void 418ofnet_watchdog(struct ifnet *ifp) 419{ 420 struct ofnet_softc *of = ifp->if_softc; 421 422 log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname); 423 ifp->if_oerrors++; 424 ofnet_stop(of); 425 ofnet_init(of); 426} 427 428#if NIPKDB_OFN > 0 429static void 430ipkdbofstart(struct ipkdb_if *kip) 431{ 432 int unit = kip->unit - 1; 433 434 if (ipkdb_of) 435 ipkdbattach(kip, &ipkdb_of->sc_ethercom); 436} 437 438static void 439ipkdbofleave(struct ipkdb_if *kip) 440{ 441} 442 443static int 444ipkdbofrcv(struct ipkdb_if *kip, u_char *buf, int poll) 445{ 446 int l; 447 448 do { 449 l = OF_read(kip->port, buf, ETHERMTU); 450 if (l < 0) 451 l = 0; 452 } while (!poll && !l); 453 return l; 454} 455 456static void 457ipkdbofsend(struct ipkdb_if *kip, u_char *buf, int l) 458{ 459 OF_write(kip->port, buf, l); 460} 461 462static int 463ipkdbprobe(struct cfdata *match, void *aux) 464{ 465 struct ipkdb_if *kip = aux; 466 static char name[256]; 467 int len; 468 int phandle; 469 470 kip->unit = match->cf_unit + 1; 471 472 if (!(kip->port = OF_open("net"))) 473 return -1; 474 if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0 || 475 len >= sizeof name) 476 return -1; 477 name[len] = 0; 478 if ((phandle = OF_instance_to_package(kip->port)) == -1) 479 return -1; 480 if (OF_getprop(phandle, "mac-address", kip->myenetaddr, 481 sizeof kip->myenetaddr) < 0) 482 return -1; 483 484 kip->flags |= IPKDB_MYHW; 485 kip->name = name; 486 kip->start = ipkdbofstart; 487 kip->leave = ipkdbofleave; 488 kip->receive = ipkdbofrcv; 489 kip->send = ipkdbofsend; 490 491 kifp = kip; 492 493 return 0; 494} 495#endif 496