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