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
|
/* $NetBSD: ofnet.c,v 1.63 2020/01/29 06:18:17 thorpej 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 <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ofnet.c,v 1.63 2020/01/29 06:18:17 thorpej Exp $");
#include "ofnet.h"
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/device.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/if_ether.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif
#include <dev/ofw/openfirm.h>
struct ofnet_softc {
device_t sc_dev;
int sc_phandle;
int sc_ihandle;
struct ethercom sc_ethercom;
struct callout sc_callout;
};
static int ofnet_match (device_t, cfdata_t, void *);
static void ofnet_attach (device_t, device_t, void *);
CFATTACH_DECL_NEW(ofnet, sizeof(struct ofnet_softc),
ofnet_match, ofnet_attach, NULL, NULL);
static void ofnet_read (struct ofnet_softc *);
static void ofnet_timer (void *);
static void ofnet_init (struct ofnet_softc *);
static void ofnet_stop (struct ofnet_softc *);
static void ofnet_start (struct ifnet *);
static int ofnet_ioctl (struct ifnet *, u_long, void *);
static void ofnet_watchdog (struct ifnet *);
static int
ofnet_match(device_t parent, cfdata_t match, void *aux)
{
struct ofbus_attach_args *oba = aux;
char type[32];
int l;
if (strcmp(oba->oba_busname, "ofw"))
return (0);
if ((l = OF_getprop(oba->oba_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
ofnet_attach(device_t parent, device_t self, void *aux)
{
struct ofnet_softc *of = device_private(self);
struct ifnet *ifp = &of->sc_ethercom.ec_if;
struct ofbus_attach_args *oba = aux;
char path[256];
int l;
u_int8_t myaddr[ETHER_ADDR_LEN];
of->sc_dev = self;
of->sc_phandle = oba->oba_phandle;
if ((l = OF_package_to_path(oba->oba_phandle, path,
sizeof path - 1)) < 0 ||
l >= sizeof path ||
(path[l] = 0, !(of->sc_ihandle = OF_open(path))))
panic("ofnet_attach: unable to open");
if (OF_getprop(oba->oba_phandle, "mac-address", myaddr,
sizeof myaddr) < 0)
panic("ofnet_attach: no mac-address");
printf(": address %s\n", ether_sprintf(myaddr));
callout_init(&of->sc_callout, 0);
strlcpy(ifp->if_xname, device_xname(of->sc_dev), IFNAMSIZ);
ifp->if_softc = of;
ifp->if_start = ofnet_start;
ifp->if_ioctl = ofnet_ioctl;
ifp->if_watchdog = ofnet_watchdog;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
IFQ_SET_READY(&ifp->if_snd);
if_attach(ifp);
ether_ifattach(ifp, myaddr);
}
static char buf[ETHER_MAX_LEN];
static void
ofnet_read(struct ofnet_softc *of)
{
struct ifnet *ifp = &of->sc_ethercom.ec_if;
struct mbuf *m, **mp, *head;
int s, l, len;
char *bufp;
s = splnet();
for (;;) {
len = OF_read(of->sc_ihandle, buf, sizeof buf);
if (len == -2 || len == 0)
break;
if (len < sizeof(struct ether_header)) {
if_statinc(ifp, if_ierrors);
continue;
}
bufp = buf;
/*
* We don't know if the interface included the FCS
* or not. For now, assume that it did if we got
* a packet length that looks like it could include
* the FCS.
*
* XXX Yuck.
*/
if (len > ETHER_MAX_LEN - ETHER_CRC_LEN)
len = ETHER_MAX_LEN - ETHER_CRC_LEN;
/* Allocate a header mbuf */
MGETHDR(m, M_DONTWAIT, MT_DATA);
if (m == 0) {
if_statinc(ifp, if_ierrors);
continue;
}
m_set_rcvif(m, 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) {
if_statinc(ifp, if_ierrors);
m_freem(head);
head = 0;
break;
}
l = MLEN;
}
if (len >= MINCLSIZE) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0) {
if_statinc(ifp, if_ierrors);
m_free(m);
m_freem(head);
head = 0;
break;
}
l = MCLBYTES;
}
/*
* Make sure the data after the Ethernet header
* is aligned.
*
* XXX Assumes the device is an ethernet, but
* XXX then so does other code in this driver.
*/
if (head == NULL) {
char *newdata = (char *)ALIGN(m->m_data +
sizeof(struct ether_header)) -
sizeof(struct ether_header);
l -= newdata - m->m_data;
m->m_data = newdata;
}
m->m_len = l = uimin(len, l);
memcpy(mtod(m, char *), bufp, l);
bufp += l;
len -= l;
*mp = m;
mp = &m->m_next;
}
if (head == 0)
continue;
if_percpuq_enqueue(ifp->if_percpuq, head);
}
splx(s);
}
static void
ofnet_timer(void *arg)
{
struct ofnet_softc *of = arg;
ofnet_read(of);
callout_reset(&of->sc_callout, 1, ofnet_timer, of);
}
static void
ofnet_init(struct ofnet_softc *of)
{
struct ifnet *ifp = &of->sc_ethercom.ec_if;
if (ifp->if_flags & IFF_RUNNING)
return;
ifp->if_flags |= IFF_RUNNING;
/* Start reading from interface */
ofnet_timer(of);
/* Attempt to start output */
ofnet_start(ifp);
}
static void
ofnet_stop(struct ofnet_softc *of)
{
callout_stop(&of->sc_callout);
of->sc_ethercom.ec_if.if_flags &= ~IFF_RUNNING;
}
static void
ofnet_start(struct ifnet *ifp)
{
struct ofnet_softc *of = ifp->if_softc;
struct mbuf *m, *m0;
char *bufp;
int len;
if (!(ifp->if_flags & IFF_RUNNING))
return;
for (;;) {
/* First try reading any packets */
ofnet_read(of);
/* Now get the first packet on the queue */
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (!m0)
return;
if (!(m0->m_flags & M_PKTHDR))
panic("ofnet_start: no header mbuf");
len = m0->m_pkthdr.len;
bpf_mtap(ifp, m0, BPF_D_OUT);
if (len > ETHERMTU + sizeof(struct ether_header)) {
/* packet too large, toss it */
if_statinc(ifp, if_oerrors);
m_freem(m0);
continue;
}
for (bufp = buf; (m = m0) != NULL;) {
memcpy(bufp, mtod(m, char *), m->m_len);
bufp += m->m_len;
m0 = m_free(m);
}
/*
* We don't know if the interface will auto-pad for
* us, so make sure it's at least as large as a
* minimum size Ethernet packet.
*/
if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
memset(bufp, 0, ETHER_MIN_LEN - ETHER_CRC_LEN - len);
bufp += ETHER_MIN_LEN - ETHER_CRC_LEN - len;
} else
len = bufp - buf;
if (OF_write(of->sc_ihandle, buf, len) != len)
if_statinc(ifp, if_oerrors);
else
if_statinc(ifp, if_opackets);
}
}
static int
ofnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct ofnet_softc *of = ifp->if_softc;
struct ifaddr *ifa = (struct ifaddr *)data;
/* struct ifreq *ifr = (struct ifreq *)data; */
int error = 0;
switch (cmd) {
case SIOCINITIFADDR:
ifp->if_flags |= IFF_UP;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
arp_ifinit(ifp, ifa);
break;
#endif
default:
break;
}
ofnet_init(of);
break;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
/* XXX re-use ether_ioctl() */
switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
case IFF_RUNNING:
/* If interface is down, but running, stop it. */
ofnet_stop(of);
break;
case IFF_UP:
/* If interface is up, but not running, start it. */
ofnet_init(of);
break;
default:
/* Other flags are ignored. */
break;
}
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return error;
}
static void
ofnet_watchdog(struct ifnet *ifp)
{
struct ofnet_softc *of = ifp->if_softc;
log(LOG_ERR, "%s: device timeout\n", device_xname(of->sc_dev));
if_statinc(ifp, if_oerrors);
ofnet_stop(of);
ofnet_init(of);
}
|