ofnet.c revision 1.11 1 /* $NetBSD: ofnet.c,v 1.11 1998/01/22 01:47:43 cgd 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
62 struct cfattach ipkdb_ofn_ca = {
63 0, ipkdb_probe, ipkdb_attach
64 };
65
66 static struct ipkdb_if *kifp;
67 static struct ofn_softc *ipkdb_of;
68
69 static int ipkdbprobe __P((struct cfdata *, void *));
70 #endif
71
72 struct ofn_softc {
73 struct device sc_dev;
74 int sc_phandle;
75 int sc_ihandle;
76 struct ethercom sc_ethercom;
77 };
78
79 static int ofnprobe __P((struct device *, struct cfdata *, void *));
80 static void ofnattach __P((struct device *, struct device *, void *));
81
82 struct cfattach ofnet_ca = {
83 sizeof(struct ofn_softc), ofnprobe, ofnattach
84 };
85
86 static void ofnread __P((struct ofn_softc *));
87 static void ofntimer __P((struct ofn_softc *));
88 static void ofninit __P((struct ofn_softc *));
89 static void ofnstop __P((struct ofn_softc *));
90
91 static void ofnstart __P((struct ifnet *));
92 static int ofnioctl __P((struct ifnet *, u_long, caddr_t));
93 static void ofnwatchdog __P((struct ifnet *));
94
95 static int
96 ofnprobe(parent, match, aux)
97 struct device *parent;
98 struct cfdata *match;
99 void *aux;
100 {
101 struct ofprobe *ofp = aux;
102 char type[32];
103 int l;
104
105 #if NIPKDB_OFN > 0
106 if (!parent)
107 return ipkdbprobe(match, aux);
108 #endif
109 if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
110 return 0;
111 if (l >= sizeof type)
112 return 0;
113 type[l] = 0;
114 if (strcmp(type, "network"))
115 return 0;
116 return 1;
117 }
118
119 static void
120 ofnattach(parent, self, aux)
121 struct device *parent, *self;
122 void *aux;
123 {
124 struct ofn_softc *of = (void *)self;
125 struct ifnet *ifp = &of->sc_ethercom.ec_if;
126 struct ofprobe *ofp = aux;
127 char path[256];
128 int l;
129 u_int8_t myaddr[ETHER_ADDR_LEN];
130
131 of->sc_phandle = ofp->phandle;
132 #if NIPKDB_OFN > 0
133 if (kifp
134 && kifp->unit - 1 == of->sc_dev.dv_unit
135 && OF_instance_to_package(kifp->port) == ofp->phandle) {
136 ipkdb_of = of;
137 of->sc_ihandle = kifp->port;
138 } else
139 #endif
140 if ((l = OF_package_to_path(ofp->phandle, path, sizeof path - 1)) < 0
141 || l >= sizeof path
142 || (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
143 panic("ofnattach: unable to open");
144 if (OF_getprop(ofp->phandle, "mac-address",
145 myaddr, sizeof myaddr)
146 < 0)
147 panic("ofnattach: no mac-address");
148 printf(": address %s\n", ether_sprintf(myaddr));
149
150 bcopy(of->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
151 ifp->if_softc = of;
152 ifp->if_start = ofnstart;
153 ifp->if_ioctl = ofnioctl;
154 ifp->if_watchdog = ofnwatchdog;
155 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
156
157 if_attach(ifp);
158 ether_ifattach(ifp, myaddr);
159
160 #if NBPFILTER > 0
161 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
162 #endif
163
164 dk_establish(0, self); /* XXX */
165 }
166
167 static char buf[ETHERMTU + sizeof(struct ether_header)];
168
169 static void
170 ofnread(of)
171 struct ofn_softc *of;
172 {
173 struct ifnet *ifp = &of->sc_ethercom.ec_if;
174 struct ether_header *eh;
175 struct mbuf *m, **mp, *head;
176 int l, len;
177 char *bufp;
178
179 #if NIPKDB_OFN > 0
180 ipkdbrint(kifp, ifp);
181 #endif
182 while (1) {
183 if ((len = OF_read(of->sc_ihandle, buf, sizeof buf)) < 0) {
184 if (len == -2 || len == 0)
185 return;
186 ifp->if_ierrors++;
187 continue;
188 }
189 if (len < sizeof(struct ether_header)) {
190 ifp->if_ierrors++;
191 continue;
192 }
193 bufp = buf;
194
195 /* Allocate a header mbuf */
196 MGETHDR(m, M_DONTWAIT, MT_DATA);
197 if (m == 0) {
198 ifp->if_ierrors++;
199 continue;
200 }
201 m->m_pkthdr.rcvif = ifp;
202 m->m_pkthdr.len = len;
203 l = MHLEN;
204 head = 0;
205 mp = &head;
206
207 while (len > 0) {
208 if (head) {
209 MGET(m, M_DONTWAIT, MT_DATA);
210 if (m == 0) {
211 ifp->if_ierrors++;
212 m_freem(head);
213 head = 0;
214 break;
215 }
216 l = MLEN;
217 }
218 if (len >= MINCLSIZE) {
219 MCLGET(m, M_DONTWAIT);
220 if ((m->m_flags & M_EXT) == 0) {
221 ifp->if_ierrors++;
222 m_free(m);
223 m_freem(head);
224 head = 0;
225 break;
226 }
227 l = MCLBYTES;
228 }
229 m->m_len = l = min(len, l);
230 bcopy(bufp, mtod(m, char *), l);
231 bufp += l;
232 len -= l;
233 *mp = m;
234 mp = &m->m_next;
235 }
236 if (head == 0)
237 continue;
238 eh = mtod(head, struct ether_header *);
239
240 #if NBPFILTER > 0
241 if (ifp->if_bpf)
242 bpf_mtap(ifp->if_bpf, m);
243 #endif
244 m_adj(head, sizeof(struct ether_header));
245 ifp->if_ipackets++;
246 ether_input(ifp, eh, head);
247 }
248 }
249
250 static void
251 ofntimer(of)
252 struct ofn_softc *of;
253 {
254 ofnread(of);
255 timeout(ofntimer, of, 1);
256 }
257
258 static void
259 ofninit(of)
260 struct ofn_softc *of;
261 {
262 struct ifnet *ifp = &of->sc_ethercom.ec_if;
263
264 if (ifp->if_flags & IFF_RUNNING)
265 return;
266
267 ifp->if_flags |= IFF_RUNNING;
268 /* Start reading from interface */
269 ofntimer(of);
270 /* Attempt to start output */
271 ofnstart(ifp);
272 }
273
274 static void
275 ofnstop(of)
276 struct ofn_softc *of;
277 {
278 untimeout(ofntimer, of);
279 of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
280 }
281
282 static void
283 ofnstart(ifp)
284 struct ifnet *ifp;
285 {
286 struct ofn_softc *of = ifp->if_softc;
287 struct mbuf *m, *m0;
288 char *bufp;
289 int len;
290
291 if (!(ifp->if_flags & IFF_RUNNING))
292 return;
293
294 for (;;) {
295 /* First try reading any packets */
296 ofnread(of);
297
298 /* Now get the first packet on the queue */
299 IF_DEQUEUE(&ifp->if_snd, m0);
300 if (!m0)
301 return;
302
303 if (!(m0->m_flags & M_PKTHDR))
304 panic("ofnstart: no header mbuf");
305 len = m0->m_pkthdr.len;
306
307 #if NBPFILTER > 0
308 if (ifp->if_bpf)
309 bpf_mtap(ifp->if_bpf, m0);
310 #endif
311
312 if (len > ETHERMTU + sizeof(struct ether_header)) {
313 /* packet too large, toss it */
314 ifp->if_oerrors++;
315 m_freem(m0);
316 continue;
317 }
318
319 for (bufp = buf; m = m0;) {
320 bcopy(mtod(m, char *), bufp, m->m_len);
321 bufp += m->m_len;
322 MFREE(m, m0);
323 }
324 if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf)
325 ifp->if_oerrors++;
326 else
327 ifp->if_opackets++;
328 }
329 }
330
331 static int
332 ofnioctl(ifp, cmd, data)
333 struct ifnet *ifp;
334 u_long cmd;
335 caddr_t data;
336 {
337 struct ofn_softc *of = ifp->if_softc;
338 struct ifaddr *ifa = (struct ifaddr *)data;
339 struct ifreq *ifr = (struct ifreq *)data;
340 int error = 0;
341
342 switch (cmd) {
343 case SIOCSIFADDR:
344 ifp->if_flags |= IFF_UP;
345
346 switch (ifa->ifa_addr->sa_family) {
347 #ifdef INET
348 case AF_INET:
349 arp_ifinit(ifp, ifa);
350 break;
351 #endif
352 default:
353 break;
354 }
355 ofninit(of);
356 break;
357 case SIOCSIFFLAGS:
358 if (!(ifp->if_flags & IFF_UP)
359 && (ifp->if_flags & IFF_RUNNING)) {
360 /* If interface is down, but running, stop it. */
361 ofnstop(of);
362 } else if ((ifp->if_flags & IFF_UP)
363 && !(ifp->if_flags & IFF_RUNNING)) {
364 /* If interface is up, but not running, start it. */
365 ofninit(of);
366 } else {
367 /* Other flags are ignored. */
368 }
369 break;
370 default:
371 error = EINVAL;
372 break;
373 }
374 return error;
375 }
376
377 static void
378 ofnwatchdog(ifp)
379 struct ifnet *ifp;
380 {
381 struct ofn_softc *of = ifp->if_softc;
382
383 log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname);
384 ifp->if_oerrors++;
385 ofnstop(of);
386 ofninit(of);
387 }
388
389 #if NIPKDB_OFN > 0
390 static void
391 ipkdbofstart(kip)
392 struct ipkdb_if *kip;
393 {
394 int unit = kip->unit - 1;
395
396 if (ipkdb_of)
397 ipkdbattach(kip, &ipkdb_of->sc_ethercom);
398 }
399
400 static void
401 ipkdbofleave(kip)
402 struct ipkdb_if *kip;
403 {
404 }
405
406 static int
407 ipkdbofrcv(kip, buf, poll)
408 struct ipkdb_if *kip;
409 u_char *buf;
410 int poll;
411 {
412 int l;
413
414 do {
415 l = OF_read(kip->port, buf, ETHERMTU);
416 if (l < 0)
417 l = 0;
418 } while (!poll && !l);
419 return l;
420 }
421
422 static void
423 ipkdbofsend(kip, buf, l)
424 struct ipkdb_if *kip;
425 u_char *buf;
426 int l;
427 {
428 OF_write(kip->port, buf, l);
429 }
430
431 static int
432 ipkdbprobe(match, aux)
433 struct cfdata *match;
434 void *aux;
435 {
436 struct ipkdb_if *kip = aux;
437 static char name[256];
438 int len;
439 int phandle;
440
441 kip->unit = match->cf_unit + 1;
442
443 if (!(kip->port = OF_open("net")))
444 return -1;
445 if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0
446 || len >= sizeof name)
447 return -1;
448 name[len] = 0;
449 if ((phandle = OF_instance_to_package(kip->port)) == -1)
450 return -1;
451 if (OF_getprop(phandle, "mac-address", kip->myenetaddr, sizeof kip->myenetaddr)
452 < 0)
453 return -1;
454
455 kip->flags |= IPKDB_MYHW;
456 kip->name = name;
457 kip->start = ipkdbofstart;
458 kip->leave = ipkdbofleave;
459 kip->receive = ipkdbofrcv;
460 kip->send = ipkdbofsend;
461
462 kifp = kip;
463
464 return 0;
465 }
466 #endif
467