summaryrefslogtreecommitdiff
path: root/sys/dev/ofw/ofnet.c
blob: 8efc624d6654724af3afe5caf87c8ccf829d1ae0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*	$OpenBSD: ofnet.c,v 1.3 1999/10/28 04:25:25 rahnds Exp $	*/
/*	$NetBSD: ofnet.c,v 1.4 1996/10/16 19:33:21 ws Exp $	*/

/*
 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
 * Copyright (C) 1995, 1996 TooLs GmbH.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by TooLs GmbH.
 * 4. The name of TooLs GmbH may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "ofnet.h"
#include "bpfilter.h"

#include <sys/param.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/syslog.h>

#include <net/if.h>

#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif

#include <dev/ofw/openfirm.h>

#if NIPKDB_OFN > 0
#include <ipkdb/ipkdb.h>
#include <machine/ipkdb.h>

struct cfattach ipkdb_ofn_ca = {
	0, ipkdb_probe, ipkdb_attach
};

static struct ipkdb_if *kifp;
static struct ofn_softc *ipkdb_of;

static int ipkdbprobe __P((void *, void *));
#endif

struct ofn_softc {
	struct device sc_dev;
	int sc_phandle;
	int sc_ihandle;
	struct arpcom sc_arpcom;
	void *dmabuf;
};

static int ofnprobe __P((struct device *, void *, void *));
static void ofnattach __P((struct device *, struct device *, void *));

struct cfattach ofnet_ca = {
	sizeof(struct ofn_softc), ofnprobe, ofnattach
};

struct cfdriver ofnet_cd = {
	NULL, "ofnet", DV_IFNET
};

static void ofnread __P((struct ofn_softc *));
static void ofntimer __P((struct ofn_softc *));
static void ofninit __P((struct ofn_softc *));
static void ofnstop __P((struct ofn_softc *));

static void ofnstart __P((struct ifnet *));
static int ofnioctl __P((struct ifnet *, u_long, caddr_t));
static void ofnwatchdog __P((struct ifnet *));

static int
ofnprobe(parent, match, aux)
	struct device *parent;
	void *match, *aux;
{
	struct ofprobe *ofp = aux;
	char type[32];
	int l;
	
#if NIPKDB_OFN > 0
	if (!parent)
		return ipkdbprobe(match, aux);
#endif
	if ((l = OF_getprop(ofp->phandle, "device_type", type, sizeof type - 1)) < 0)
		return 0;
	if (l >= sizeof type)
		return 0;
	type[l] = 0;
	if (strcmp(type, "network"))
		return 0;
	return 1;
}

static void
ofnattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct ofn_softc *of = (void *)self;
	struct ifnet *ifp = &of->sc_arpcom.ac_if;
	struct ofprobe *ofp = aux;
	char path[256];
	int l;
	
	of->sc_phandle = ofp->phandle;
#if NIPKDB_OFN > 0
	if (kifp
	    && kifp->unit - 1 == of->sc_dev.dv_unit
	    && OF_instance_to_package(kifp->port) == ofp->phandle)  {
		ipkdb_of = of;
		of->sc_ihandle = kifp->port;
	} else
#endif
	if ((l = OF_package_to_path(ofp->phandle, path, sizeof path - 1)) < 0
	    || l >= sizeof path
	    || (path[l] = 0, !(of->sc_ihandle = OF_open(path))))
		panic("ofnattach: unable to open");
printf("\nethernet dev: path %s\n", path);
	OF_call_method("dma-alloc", of->sc_ihandle, 1, 1, MAXPHYS,
		&(of->dmabuf));
	if (OF_getprop(ofp->phandle, "mac-address",
		       of->sc_arpcom.ac_enaddr, sizeof
			       (of->sc_arpcom.ac_enaddr)) < 0)
	{
		if (OF_getprop(ofp->phandle, "local-mac-address",
			       of->sc_arpcom.ac_enaddr, sizeof
				       (of->sc_arpcom.ac_enaddr)) < 0)
		{
			panic("ofnattach: no mac-address");
		}
	}
	printf(": address %s\n", ether_sprintf(of->sc_arpcom.ac_enaddr));
	
	bcopy(of->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
	ifp->if_softc = of;
	ifp->if_start = ofnstart;
	ifp->if_ioctl = ofnioctl;
	ifp->if_watchdog = ofnwatchdog;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;

	if_attach(ifp);
	ether_ifattach(ifp);

#if NBPFILTER > 0
	bpfattach(&of->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB,
		  sizeof(struct ether_header));
#endif

	dk_establish(0, self);					/* XXX */
}

static void
ofnread(of)
	struct ofn_softc *of;
{
	struct ifnet *ifp = &of->sc_arpcom.ac_if;
	struct ether_header *eh;
	struct mbuf *m, **mp, *head;
	int l, len;
	char *bufp;
	char *buf = of->dmabuf;

#if NIPKDB_OFN > 0
	ipkdbrint(kifp, ifp);
#endif	
	while (1) {
		if ((len = OF_read(of->sc_ihandle, buf, sizeof buf)) < 0) {
			if (len == -2)
				return;
			ifp->if_ierrors++;
			continue;
		}
		if (len < sizeof(struct ether_header)) {
			ifp->if_ierrors++;
			continue;
		}
		bufp = buf;
		
		/* Allocate a header mbuf */
		MGETHDR(m, M_DONTWAIT, MT_DATA);
		if (m == 0) {
			ifp->if_ierrors++;
			continue;
		}
		m->m_pkthdr.rcvif = ifp;
		m->m_pkthdr.len = len;
		l = MHLEN;
		head = 0;
		mp = &head;
		
		while (len > 0) {
			if (head) {
				MGET(m, M_DONTWAIT, MT_DATA);
				if (m == 0) {
					ifp->if_ierrors++;
					m_freem(head);
					head = 0;
					break;
				}
				l = MLEN;
			}
			if (len >= MINCLSIZE) {
				MCLGET(m, M_DONTWAIT);
				if (m->m_flags & M_EXT)
					l = MCLBYTES;
			}
			m->m_len = l = min(len, l);
			bcopy(bufp, mtod(m, char *), l);
			bufp += l;
			len -= l;
			*mp = m;
			mp = &m->m_next;
		}
		if (head == 0)
			continue;
		eh = mtod(head, struct ether_header *);

#if	NBPFILTER > 0
		if (ifp->if_bpf) {
			bpf->mtap(ifp->if_bpf, m);
#endif
		m_adj(head, sizeof(struct ether_header));
		ifp->if_ipackets++;
		ether_input(ifp, eh, head);
	}
}

static void
ofntimer(of)
	struct ofn_softc *of;
{
	ofnread(of);
	timeout(ofntimer, of, 1);
}

static void
ofninit(of)
	struct ofn_softc *of;
{
	struct ifnet *ifp = &of->sc_arpcom.ac_if;

	if (ifp->if_flags & IFF_RUNNING)
		return;

	ifp->if_flags |= IFF_RUNNING;
	/* Start reading from interface */
	ofntimer(of);
	/* Attempt to start output */
	ofnstart(ifp);
}

static void
ofnstop(of)
	struct ofn_softc *of;
{
	untimeout(ofntimer, of);
	of->sc_arpcom.ac_if.if_flags &= ~IFF_RUNNING;
}

static void
ofnstart(ifp)
	struct ifnet *ifp;
{
	struct ofn_softc *of = ifp->if_softc;
	struct mbuf *m, *m0;
	char *bufp;
	char *buf;
	int len;
	buf = of->dmabuf;
	
	if (!(ifp->if_flags & IFF_RUNNING))
		return;

	for (;;) {
		/* First try reading any packets */
		ofnread(of);
		
		/* Now get the first packet on the queue */
		IF_DEQUEUE(&ifp->if_snd, m0);
		if (!m0)
			return;
		
		if (!(m0->m_flags & M_PKTHDR))
			panic("ofnstart: no header mbuf");
		len = m0->m_pkthdr.len;
		
		if (len > ETHERMTU + sizeof(struct ether_header)) {
			/* packet too large, toss it */
			ifp->if_oerrors++;
			m_freem(m0);
			continue;
		}

#if NPBFILTER > 0
		if (ifp->if_bpf)
			bpf_mtab(ifp->if_bpf, m0);
#endif
		for (bufp = buf; m = m0;) {
			bcopy(mtod(m, char *), bufp, m->m_len);
			bufp += m->m_len;
			MFREE(m, m0);
		}
		if (OF_write(of->sc_ihandle, buf, bufp - buf) != bufp - buf)
			ifp->if_oerrors++;
		else
			ifp->if_opackets++;
	}
}

static int
ofnioctl(ifp, cmd, data)
	struct ifnet *ifp;
	u_long cmd;
	caddr_t data;
{
	struct ofn_softc *of = ifp->if_softc;
	struct ifaddr *ifa = (struct ifaddr *)data;
	struct ifreq *ifr = (struct ifreq *)data;
	int error = 0;
	
	switch (cmd) {
	case SIOCSIFADDR:
		ifp->if_flags |= IFF_UP;
		
		switch (ifa->ifa_addr->sa_family) {
#ifdef	INET
		case AF_INET:
			arp_ifinit(&of->sc_arpcom, ifa);
			break;
#endif
		default:
			break;
		}
		ofninit(of);
		break;
	case SIOCSIFFLAGS:
		if (!(ifp->if_flags & IFF_UP)
		    && (ifp->if_flags & IFF_RUNNING)) {
			/* If interface is down, but running, stop it. */
			ofnstop(of);
		} else if ((ifp->if_flags & IFF_UP)
			   && !(ifp->if_flags & IFF_RUNNING)) {
			/* If interface is up, but not running, start it. */
			ofninit(of);
		} else {
			/* Other flags are ignored. */
		}
		break;
	default:
		error = EINVAL;
		break;
	}
	return error;
}

static void
ofnwatchdog(ifp)
	struct ifnet *ifp;
{
	struct ofn_softc *of = ifp->if_softc;
	
	log(LOG_ERR, "%s: device timeout\n", of->sc_dev.dv_xname);
	of->sc_arpcom.ac_if.if_oerrors++;
	ofnstop(of);
	ofninit(of);
}

#if NIPKDB_OFN > 0
/* has not been updated to use dmabuf */
static void
ipkdbofstart(kip)
	struct ipkdb_if *kip;
{
	int unit = kip->unit - 1;
	
	if (ipkdb_of)
		ipkdbattach(kip, &ipkdb_of->sc_arpcom);
}

static void
ipkdbofleave(kip)
	struct ipkdb_if *kip;
{
}

static int
ipkdbofrcv(kip, buf, poll)
	struct ipkdb_if *kip;
	u_char *buf;
	int poll;
{
	int l;
	
	do {
		l = OF_read(kip->port, buf, ETHERMTU);
		if (l < 0)
			l = 0;
	} while (!poll && !l);
	return l;
}

static void
ipkdbofsend(kip, buf, l)
	struct ipkdb_if *kip;
	u_char *buf;
	int l;
{
	OF_write(kip->port, buf, l);
}

static int
ipkdbprobe(match, aux)
	void *match, *aux;
{
	struct cfdata *cf = match;
	struct ipkdb_if *kip = aux;
	static char name[256];
	int len;
	int phandle;
	
	kip->unit = cf->cf_unit + 1;

	if (!(kip->port = OF_open("net")))
		return -1;
	if ((len = OF_instance_to_path(kip->port, name, sizeof name - 1)) < 0
	    || len >= sizeof name)
		return -1;
	name[len] = 0;
	if ((phandle = OF_instance_to_package(kip->port)) == -1)
		return -1;
	if ( OF_getprop(phandle, "local-mac-address", kip->myenetaddr, sizeof kip->myenetaddr) &&
	OF_getprop(phandle, "mac-address", kip->myenetaddr, sizeof kip->myenetaddr)
	    < 0)
		return -1;
	
	kip->flags |= IPKDB_MYHW;
	kip->name = name;
	kip->start = ipkdbofstart;
	kip->leave = ipkdbofleave;
	kip->receive = ipkdbofrcv;
	kip->send = ipkdbofsend;

	kifp = kip;
	
	return 0;
}
#endif