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