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