ofnet.c revision 1.13 1 1.13 mycroft /* $NetBSD: ofnet.c,v 1.13 1998/02/24 05:44:39 mycroft Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 ws * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 ws * All rights reserved.
7 1.1 ws *
8 1.1 ws * Redistribution and use in source and binary forms, with or without
9 1.1 ws * modification, are permitted provided that the following conditions
10 1.1 ws * are met:
11 1.1 ws * 1. Redistributions of source code must retain the above copyright
12 1.1 ws * notice, this list of conditions and the following disclaimer.
13 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ws * notice, this list of conditions and the following disclaimer in the
15 1.1 ws * documentation and/or other materials provided with the distribution.
16 1.1 ws * 3. All advertising materials mentioning features or use of this software
17 1.1 ws * must display the following acknowledgement:
18 1.1 ws * This product includes software developed by TooLs GmbH.
19 1.1 ws * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 ws * derived from this software without specific prior written permission.
21 1.1 ws *
22 1.1 ws * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 ws * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 ws * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 ws * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 ws * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 ws * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 ws * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 ws * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 ws */
33 1.1 ws #include "ofnet.h"
34 1.1 ws #include "bpfilter.h"
35 1.1 ws
36 1.1 ws #include <sys/param.h>
37 1.1 ws #include <sys/device.h>
38 1.1 ws #include <sys/ioctl.h>
39 1.1 ws #include <sys/mbuf.h>
40 1.1 ws #include <sys/socket.h>
41 1.1 ws #include <sys/syslog.h>
42 1.1 ws
43 1.1 ws #include <net/if.h>
44 1.5 is #include <net/if_ether.h>
45 1.1 ws
46 1.1 ws #ifdef INET
47 1.1 ws #include <netinet/in.h>
48 1.5 is #include <netinet/if_inarp.h>
49 1.1 ws #endif
50 1.1 ws
51 1.6 thorpej #if NBPFILTER > 0
52 1.6 thorpej #include <net/bpf.h>
53 1.6 thorpej #include <net/bpfdesc.h>
54 1.6 thorpej #endif
55 1.6 thorpej
56 1.1 ws #include <dev/ofw/openfirm.h>
57 1.1 ws
58 1.4 ws #if NIPKDB_OFN > 0
59 1.4 ws #include <ipkdb/ipkdb.h>
60 1.4 ws #include <machine/ipkdb.h>
61 1.1 ws
62 1.4 ws struct cfattach ipkdb_ofn_ca = {
63 1.4 ws 0, ipkdb_probe, ipkdb_attach
64 1.1 ws };
65 1.1 ws
66 1.4 ws static struct ipkdb_if *kifp;
67 1.13 mycroft static struct ofnet_softc *ipkdb_of;
68 1.1 ws
69 1.6 thorpej static int ipkdbprobe __P((struct cfdata *, void *));
70 1.1 ws #endif
71 1.1 ws
72 1.13 mycroft struct ofnet_softc {
73 1.1 ws struct device sc_dev;
74 1.1 ws int sc_phandle;
75 1.1 ws int sc_ihandle;
76 1.5 is struct ethercom sc_ethercom;
77 1.1 ws };
78 1.1 ws
79 1.13 mycroft static int ofnet_match __P((struct device *, struct cfdata *, void *));
80 1.13 mycroft static void ofnet_attach __P((struct device *, struct device *, void *));
81 1.1 ws
82 1.1 ws struct cfattach ofnet_ca = {
83 1.13 mycroft sizeof(struct ofnet_softc), ofnet_match, ofnet_attach
84 1.1 ws };
85 1.1 ws
86 1.13 mycroft static void ofnet_read __P((struct ofnet_softc *));
87 1.13 mycroft static void ofnet_timer __P((struct ofnet_softc *));
88 1.13 mycroft static void ofnet_init __P((struct ofnet_softc *));
89 1.13 mycroft static void ofnet_stop __P((struct ofnet_softc *));
90 1.13 mycroft
91 1.13 mycroft static void ofnet_start __P((struct ifnet *));
92 1.13 mycroft static int ofnet_ioctl __P((struct ifnet *, u_long, caddr_t));
93 1.13 mycroft static void ofnet_watchdog __P((struct ifnet *));
94 1.1 ws
95 1.1 ws static int
96 1.13 mycroft ofnet_match(parent, match, aux)
97 1.1 ws struct device *parent;
98 1.6 thorpej struct cfdata *match;
99 1.6 thorpej void *aux;
100 1.1 ws {
101 1.13 mycroft struct ofbus_attach_args *oba = aux;
102 1.1 ws char type[32];
103 1.1 ws int l;
104 1.1 ws
105 1.4 ws #if NIPKDB_OFN > 0
106 1.1 ws if (!parent)
107 1.4 ws return ipkdbprobe(match, aux);
108 1.1 ws #endif
109 1.13 mycroft if (strcmp(oba->oba_busname, "ofw"))
110 1.13 mycroft return (0);
111 1.13 mycroft if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
112 1.13 mycroft sizeof type - 1)) < 0)
113 1.1 ws return 0;
114 1.1 ws if (l >= sizeof type)
115 1.1 ws return 0;
116 1.1 ws type[l] = 0;
117 1.1 ws if (strcmp(type, "network"))
118 1.1 ws return 0;
119 1.1 ws return 1;
120 1.1 ws }
121 1.1 ws
122 1.1 ws static void
123 1.13 mycroft ofnet_attach(parent, self, aux)
124 1.1 ws struct device *parent, *self;
125 1.1 ws void *aux;
126 1.1 ws {
127 1.13 mycroft struct ofnet_softc *of = (void *)self;
128 1.5 is struct ifnet *ifp = &of->sc_ethercom.ec_if;
129 1.13 mycroft struct ofbus_attach_args *oba = aux;
130 1.1 ws char path[256];
131 1.1 ws int l;
132 1.5 is u_int8_t myaddr[ETHER_ADDR_LEN];
133 1.1 ws
134 1.13 mycroft of->sc_phandle = oba->oba_phandle;
135 1.4 ws #if NIPKDB_OFN > 0
136 1.13 mycroft if (kifp &&
137 1.13 mycroft kifp->unit - 1 == of->sc_dev.dv_unit &&
138 1.13 mycroft OF_instance_to_package(kifp->port) == oba->oba_phandle) {
139 1.4 ws ipkdb_of = of;
140 1.1 ws of->sc_ihandle = kifp->port;
141 1.1 ws } else
142 1.1 ws #endif
143 1.13 mycroft if ((l = OF_package_to_path(oba->oba_phandle, path,
144 1.13 mycroft sizeof path - 1)) < 0 ||
145 1.13 mycroft l >= sizeof path ||
146 1.13 mycroft (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
147 1.13 mycroft panic("ofnet_attach: unable to open");
148 1.13 mycroft if (OF_getprop(oba->oba_phandle, "mac-address", myaddr,
149 1.13 mycroft sizeof myaddr) < 0)
150 1.13 mycroft panic("ofnet_attach: no mac-address");
151 1.5 is printf(": address %s\n", ether_sprintf(myaddr));
152 1.1 ws
153 1.1 ws bcopy(of->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
154 1.1 ws ifp->if_softc = of;
155 1.13 mycroft ifp->if_start = ofnet_start;
156 1.13 mycroft ifp->if_ioctl = ofnet_ioctl;
157 1.13 mycroft ifp->if_watchdog = ofnet_watchdog;
158 1.1 ws ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
159 1.1 ws
160 1.1 ws if_attach(ifp);
161 1.5 is ether_ifattach(ifp, myaddr);
162 1.1 ws
163 1.1 ws #if NBPFILTER > 0
164 1.5 is bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
165 1.1 ws #endif
166 1.1 ws
167 1.1 ws dk_establish(0, self); /* XXX */
168 1.1 ws }
169 1.1 ws
170 1.1 ws static char buf[ETHERMTU + sizeof(struct ether_header)];
171 1.1 ws
172 1.1 ws static void
173 1.13 mycroft ofnet_read(of)
174 1.13 mycroft struct ofnet_softc *of;
175 1.1 ws {
176 1.5 is struct ifnet *ifp = &of->sc_ethercom.ec_if;
177 1.1 ws struct ether_header *eh;
178 1.1 ws struct mbuf *m, **mp, *head;
179 1.1 ws int l, len;
180 1.1 ws char *bufp;
181 1.1 ws
182 1.4 ws #if NIPKDB_OFN > 0
183 1.4 ws ipkdbrint(kifp, ifp);
184 1.1 ws #endif
185 1.1 ws while (1) {
186 1.1 ws if ((len = OF_read(of->sc_ihandle, buf, sizeof buf)) < 0) {
187 1.9 mycroft if (len == -2 || len == 0)
188 1.1 ws return;
189 1.1 ws ifp->if_ierrors++;
190 1.1 ws continue;
191 1.1 ws }
192 1.1 ws if (len < sizeof(struct ether_header)) {
193 1.1 ws ifp->if_ierrors++;
194 1.1 ws continue;
195 1.1 ws }
196 1.1 ws bufp = buf;
197 1.1 ws
198 1.1 ws /* Allocate a header mbuf */
199 1.1 ws MGETHDR(m, M_DONTWAIT, MT_DATA);
200 1.1 ws if (m == 0) {
201 1.1 ws ifp->if_ierrors++;
202 1.1 ws continue;
203 1.1 ws }
204 1.1 ws m->m_pkthdr.rcvif = ifp;
205 1.1 ws m->m_pkthdr.len = len;
206 1.1 ws l = MHLEN;
207 1.1 ws head = 0;
208 1.1 ws mp = &head;
209 1.1 ws
210 1.1 ws while (len > 0) {
211 1.1 ws if (head) {
212 1.1 ws MGET(m, M_DONTWAIT, MT_DATA);
213 1.1 ws if (m == 0) {
214 1.1 ws ifp->if_ierrors++;
215 1.1 ws m_freem(head);
216 1.1 ws head = 0;
217 1.1 ws break;
218 1.1 ws }
219 1.1 ws l = MLEN;
220 1.1 ws }
221 1.1 ws if (len >= MINCLSIZE) {
222 1.1 ws MCLGET(m, M_DONTWAIT);
223 1.8 mycroft if ((m->m_flags & M_EXT) == 0) {
224 1.8 mycroft ifp->if_ierrors++;
225 1.9 mycroft m_free(m);
226 1.7 mycroft m_freem(head);
227 1.7 mycroft head = 0;
228 1.7 mycroft break;
229 1.7 mycroft }
230 1.7 mycroft l = MCLBYTES;
231 1.1 ws }
232 1.12 cgd
233 1.12 cgd /*
234 1.12 cgd * Make sure the data after the Ethernet header
235 1.12 cgd * is aligned.
236 1.12 cgd *
237 1.12 cgd * XXX Assumes the device is an ethernet, but
238 1.12 cgd * XXX then so does other code in this driver.
239 1.12 cgd */
240 1.12 cgd if (head == NULL) {
241 1.12 cgd caddr_t newdata = (caddr_t)ALIGN(m->m_data +
242 1.12 cgd sizeof(struct ether_header)) -
243 1.12 cgd sizeof(struct ether_header);
244 1.12 cgd l -= newdata - m->m_data;
245 1.12 cgd m->m_data = newdata;
246 1.12 cgd }
247 1.12 cgd
248 1.1 ws m->m_len = l = min(len, l);
249 1.1 ws bcopy(bufp, mtod(m, char *), l);
250 1.1 ws bufp += l;
251 1.1 ws len -= l;
252 1.1 ws *mp = m;
253 1.1 ws mp = &m->m_next;
254 1.1 ws }
255 1.1 ws if (head == 0)
256 1.1 ws continue;
257 1.1 ws eh = mtod(head, struct ether_header *);
258 1.1 ws
259 1.6 thorpej #if NBPFILTER > 0
260 1.6 thorpej if (ifp->if_bpf)
261 1.6 thorpej bpf_mtap(ifp->if_bpf, m);
262 1.1 ws #endif
263 1.1 ws m_adj(head, sizeof(struct ether_header));
264 1.1 ws ifp->if_ipackets++;
265 1.1 ws ether_input(ifp, eh, head);
266 1.1 ws }
267 1.1 ws }
268 1.1 ws
269 1.1 ws static void
270 1.13 mycroft ofnet_timer(of)
271 1.13 mycroft struct ofnet_softc *of;
272 1.1 ws {
273 1.13 mycroft ofnet_read(of);
274 1.13 mycroft timeout(ofnet_timer, of, 1);
275 1.1 ws }
276 1.1 ws
277 1.1 ws static void
278 1.13 mycroft ofnet_init(of)
279 1.13 mycroft struct ofnet_softc *of;
280 1.1 ws {
281 1.5 is struct ifnet *ifp = &of->sc_ethercom.ec_if;
282 1.1 ws
283 1.1 ws if (ifp->if_flags & IFF_RUNNING)
284 1.1 ws return;
285 1.1 ws
286 1.1 ws ifp->if_flags |= IFF_RUNNING;
287 1.1 ws /* Start reading from interface */
288 1.13 mycroft ofnet_timer(of);
289 1.1 ws /* Attempt to start output */
290 1.13 mycroft ofnet_start(ifp);
291 1.1 ws }
292 1.1 ws
293 1.1 ws static void
294 1.13 mycroft ofnet_stop(of)
295 1.13 mycroft struct ofnet_softc *of;
296 1.1 ws {
297 1.13 mycroft untimeout(ofnet_timer, of);
298 1.5 is of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
299 1.1 ws }
300 1.1 ws
301 1.1 ws static void
302 1.13 mycroft ofnet_start(ifp)
303 1.1 ws struct ifnet *ifp;
304 1.1 ws {
305 1.13 mycroft struct ofnet_softc *of = ifp->if_softc;
306 1.1 ws struct mbuf *m, *m0;
307 1.1 ws char *bufp;
308 1.1 ws int len;
309 1.1 ws
310 1.1 ws if (!(ifp->if_flags & IFF_RUNNING))
311 1.1 ws return;
312 1.1 ws
313 1.1 ws for (;;) {
314 1.1 ws /* First try reading any packets */
315 1.13 mycroft ofnet_read(of);
316 1.1 ws
317 1.1 ws /* Now get the first packet on the queue */
318 1.1 ws IF_DEQUEUE(&ifp->if_snd, m0);
319 1.1 ws if (!m0)
320 1.1 ws return;
321 1.1 ws
322 1.1 ws if (!(m0->m_flags & M_PKTHDR))
323 1.13 mycroft panic("ofnet_start: no header mbuf");
324 1.1 ws len = m0->m_pkthdr.len;
325 1.6 thorpej
326 1.6 thorpej #if NBPFILTER > 0
327 1.6 thorpej if (ifp->if_bpf)
328 1.6 thorpej bpf_mtap(ifp->if_bpf, m0);
329 1.6 thorpej #endif
330 1.6 thorpej
331 1.1 ws if (len > ETHERMTU + sizeof(struct ether_header)) {
332 1.1 ws /* packet too large, toss it */
333 1.1 ws ifp->if_oerrors++;
334 1.1 ws m_freem(m0);
335 1.1 ws continue;
336 1.1 ws }
337 1.1 ws
338 1.1 ws for (bufp = buf; m = m0;) {
339 1.1 ws bcopy(mtod(m, char *), bufp, m->m_len);
340 1.1 ws bufp += m->m_len;
341 1.1 ws MFREE(m, m0);
342 1.1 ws }
343 1.1 ws if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf)
344 1.1 ws ifp->if_oerrors++;
345 1.1 ws else
346 1.1 ws ifp->if_opackets++;
347 1.1 ws }
348 1.1 ws }
349 1.1 ws
350 1.1 ws static int
351 1.13 mycroft ofnet_ioctl(ifp, cmd, data)
352 1.1 ws struct ifnet *ifp;
353 1.1 ws u_long cmd;
354 1.1 ws caddr_t data;
355 1.1 ws {
356 1.13 mycroft struct ofnet_softc *of = ifp->if_softc;
357 1.1 ws struct ifaddr *ifa = (struct ifaddr *)data;
358 1.1 ws struct ifreq *ifr = (struct ifreq *)data;
359 1.1 ws int error = 0;
360 1.1 ws
361 1.1 ws switch (cmd) {
362 1.1 ws case SIOCSIFADDR:
363 1.1 ws ifp->if_flags |= IFF_UP;
364 1.1 ws
365 1.1 ws switch (ifa->ifa_addr->sa_family) {
366 1.1 ws #ifdef INET
367 1.1 ws case AF_INET:
368 1.5 is arp_ifinit(ifp, ifa);
369 1.1 ws break;
370 1.1 ws #endif
371 1.1 ws default:
372 1.1 ws break;
373 1.1 ws }
374 1.13 mycroft ofnet_init(of);
375 1.1 ws break;
376 1.1 ws case SIOCSIFFLAGS:
377 1.13 mycroft if ((ifp->if_flags & IFF_UP) == 0 &&
378 1.13 mycroft (ifp->if_flags & IFF_RUNNING) != 0) {
379 1.1 ws /* If interface is down, but running, stop it. */
380 1.13 mycroft ofnet_stop(of);
381 1.13 mycroft } else if ((ifp->if_flags & IFF_UP) != 0 &&
382 1.13 mycroft (ifp->if_flags & IFF_RUNNING) == 0) {
383 1.1 ws /* If interface is up, but not running, start it. */
384 1.13 mycroft ofnet_init(of);
385 1.1 ws } else {
386 1.1 ws /* Other flags are ignored. */
387 1.1 ws }
388 1.1 ws break;
389 1.1 ws default:
390 1.1 ws error = EINVAL;
391 1.1 ws break;
392 1.1 ws }
393 1.1 ws return error;
394 1.1 ws }
395 1.1 ws
396 1.1 ws static void
397 1.13 mycroft ofnet_watchdog(ifp)
398 1.1 ws struct ifnet *ifp;
399 1.1 ws {
400 1.13 mycroft struct ofnet_softc *of = ifp->if_softc;
401 1.1 ws
402 1.1 ws log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname);
403 1.5 is ifp->if_oerrors++;
404 1.13 mycroft ofnet_stop(of);
405 1.13 mycroft ofnet_init(of);
406 1.1 ws }
407 1.1 ws
408 1.4 ws #if NIPKDB_OFN > 0
409 1.1 ws static void
410 1.4 ws ipkdbofstart(kip)
411 1.4 ws struct ipkdb_if *kip;
412 1.1 ws {
413 1.1 ws int unit = kip->unit - 1;
414 1.1 ws
415 1.4 ws if (ipkdb_of)
416 1.5 is ipkdbattach(kip, &ipkdb_of->sc_ethercom);
417 1.1 ws }
418 1.1 ws
419 1.1 ws static void
420 1.4 ws ipkdbofleave(kip)
421 1.4 ws struct ipkdb_if *kip;
422 1.1 ws {
423 1.1 ws }
424 1.1 ws
425 1.1 ws static int
426 1.4 ws ipkdbofrcv(kip, buf, poll)
427 1.4 ws struct ipkdb_if *kip;
428 1.1 ws u_char *buf;
429 1.1 ws int poll;
430 1.1 ws {
431 1.1 ws int l;
432 1.1 ws
433 1.1 ws do {
434 1.1 ws l = OF_read(kip->port, buf, ETHERMTU);
435 1.1 ws if (l < 0)
436 1.1 ws l = 0;
437 1.1 ws } while (!poll && !l);
438 1.1 ws return l;
439 1.1 ws }
440 1.1 ws
441 1.1 ws static void
442 1.4 ws ipkdbofsend(kip, buf, l)
443 1.4 ws struct ipkdb_if *kip;
444 1.1 ws u_char *buf;
445 1.1 ws int l;
446 1.1 ws {
447 1.1 ws OF_write(kip->port, buf, l);
448 1.1 ws }
449 1.1 ws
450 1.1 ws static int
451 1.4 ws ipkdbprobe(match, aux)
452 1.6 thorpej struct cfdata *match;
453 1.6 thorpej void *aux;
454 1.1 ws {
455 1.4 ws struct ipkdb_if *kip = aux;
456 1.1 ws static char name[256];
457 1.1 ws int len;
458 1.1 ws int phandle;
459 1.1 ws
460 1.6 thorpej kip->unit = match->cf_unit + 1;
461 1.1 ws
462 1.1 ws if (!(kip->port = OF_open("net")))
463 1.1 ws return -1;
464 1.13 mycroft if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0 ||
465 1.13 mycroft len >= sizeof name)
466 1.1 ws return -1;
467 1.1 ws name[len] = 0;
468 1.1 ws if ((phandle = OF_instance_to_package(kip->port)) == -1)
469 1.1 ws return -1;
470 1.13 mycroft if (OF_getprop(phandle, "mac-address", kip->myenetaddr,
471 1.13 mycroft sizeof kip->myenetaddr) < 0)
472 1.1 ws return -1;
473 1.1 ws
474 1.4 ws kip->flags |= IPKDB_MYHW;
475 1.1 ws kip->name = name;
476 1.4 ws kip->start = ipkdbofstart;
477 1.4 ws kip->leave = ipkdbofleave;
478 1.4 ws kip->receive = ipkdbofrcv;
479 1.4 ws kip->send = ipkdbofsend;
480 1.1 ws
481 1.1 ws kifp = kip;
482 1.1 ws
483 1.1 ws return 0;
484 1.1 ws }
485 1.1 ws #endif
486