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