if_eg.c revision 1.1 1 1.1 deraadt /*
2 1.1 deraadt * Copyright (c) 1993 Dean Huxley <dean (at) fsa.ca>
3 1.1 deraadt * All rights reserved.
4 1.1 deraadt *
5 1.1 deraadt * Redistribution and use in source and binary forms, with or without
6 1.1 deraadt * modification, are permitted provided that the following conditions
7 1.1 deraadt * are met:
8 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
9 1.1 deraadt * notice, this list of conditions and the following disclaimer.
10 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
12 1.1 deraadt * documentation and/or other materials provided with the distribution.
13 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
14 1.1 deraadt * must display the following acknowledgement:
15 1.1 deraadt * This product includes software developed by Dean Huxley.
16 1.1 deraadt * 4. The name of Dean Huxley may not be used to endorse or promote products
17 1.1 deraadt * derived from this software without specific prior written permission.
18 1.1 deraadt *
19 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 1.1 deraadt *
30 1.1 deraadt * $Id: if_eg.c,v 1.1 1994/08/23 18:00:08 deraadt Exp $
31 1.1 deraadt */
32 1.1 deraadt
33 1.1 deraadt /* To do:
34 1.1 deraadt * - multicast
35 1.1 deraadt * - promiscuous
36 1.1 deraadt */
37 1.1 deraadt #include "bpfilter.h"
38 1.1 deraadt
39 1.1 deraadt #include <sys/types.h>
40 1.1 deraadt #include <sys/param.h>
41 1.1 deraadt #include <sys/mbuf.h>
42 1.1 deraadt #include <sys/socket.h>
43 1.1 deraadt #include <sys/ioctl.h>
44 1.1 deraadt #include <sys/errno.h>
45 1.1 deraadt #include <sys/syslog.h>
46 1.1 deraadt #include <sys/select.h>
47 1.1 deraadt #include <sys/device.h>
48 1.1 deraadt
49 1.1 deraadt #include <net/if.h>
50 1.1 deraadt #include <net/netisr.h>
51 1.1 deraadt #include <net/if_dl.h>
52 1.1 deraadt #include <net/if_types.h>
53 1.1 deraadt #include <net/netisr.h>
54 1.1 deraadt
55 1.1 deraadt #ifdef INET
56 1.1 deraadt #include <netinet/in.h>
57 1.1 deraadt #include <netinet/in_systm.h>
58 1.1 deraadt #include <netinet/in_var.h>
59 1.1 deraadt #include <netinet/ip.h>
60 1.1 deraadt #include <netinet/if_ether.h>
61 1.1 deraadt #endif
62 1.1 deraadt
63 1.1 deraadt #ifdef NS
64 1.1 deraadt #include <netns/ns.h>
65 1.1 deraadt #include <netns/ns_if.h>
66 1.1 deraadt #endif
67 1.1 deraadt
68 1.1 deraadt #if NBPFILTER > 0
69 1.1 deraadt #include <net/bpf.h>
70 1.1 deraadt #include <net/bpfdesc.h>
71 1.1 deraadt #endif
72 1.1 deraadt
73 1.1 deraadt #include <machine/cpu.h>
74 1.1 deraadt #include <machine/pio.h>
75 1.1 deraadt
76 1.1 deraadt #include <i386/isa/isavar.h>
77 1.1 deraadt #include <i386/isa/if_egreg.h>
78 1.1 deraadt #include <i386/isa/elink.h>
79 1.1 deraadt
80 1.1 deraadt /* for debugging convenience */
81 1.1 deraadt #ifdef EGDEBUG
82 1.1 deraadt #define dlog(x) log x
83 1.1 deraadt #else
84 1.1 deraadt #define dlog(x)
85 1.1 deraadt #endif
86 1.1 deraadt
87 1.1 deraadt #define ETHER_MIN_LEN 64
88 1.1 deraadt #define ETHER_MAX_LEN 1518
89 1.1 deraadt #define ETHER_ADDR_LEN 6
90 1.1 deraadt
91 1.1 deraadt #define EG_INLEN 10
92 1.1 deraadt #define EG_BUFLEN 0x0670
93 1.1 deraadt
94 1.1 deraadt /*
95 1.1 deraadt * Ethernet software status per interface.
96 1.1 deraadt */
97 1.1 deraadt struct eg_softc {
98 1.1 deraadt struct device sc_dev;
99 1.1 deraadt struct intrhand sc_ih;
100 1.1 deraadt struct arpcom sc_arpcom; /* Ethernet common part */
101 1.1 deraadt short eg_cmd; /* Command register R/W */
102 1.1 deraadt short eg_ctl; /* Control register R/W (EG_CTL_*) */
103 1.1 deraadt short eg_stat; /* Status register R/O (EG_STAT_*) */
104 1.1 deraadt short eg_data; /* Data register R/W (16 bits) */
105 1.1 deraadt u_char eg_rom_major; /* Cards ROM version (major number) */
106 1.1 deraadt u_char eg_rom_minor; /* Cards ROM version (minor number) */
107 1.1 deraadt short eg_ram; /* Amount of RAM on the card */
108 1.1 deraadt u_char eg_pcb[64]; /* Primary Command Block buffer */
109 1.1 deraadt u_char eg_incount; /* Number of buffers currently used */
110 1.1 deraadt u_char *eg_inbuf; /* Incoming packet buffer */
111 1.1 deraadt u_char *eg_outbuf; /* Outgoing packet buffer */
112 1.1 deraadt };
113 1.1 deraadt
114 1.1 deraadt static int egprobe();
115 1.1 deraadt static void egattach();
116 1.1 deraadt
117 1.1 deraadt struct cfdriver egcd = {
118 1.1 deraadt NULL, "eg", egprobe, egattach, DV_IFNET, sizeof(struct eg_softc)
119 1.1 deraadt };
120 1.1 deraadt
121 1.1 deraadt int egintr __P((struct eg_softc *));
122 1.1 deraadt static void eginit __P((struct eg_softc *));
123 1.1 deraadt static int egioctl __P((struct ifnet *, int, caddr_t));
124 1.1 deraadt static int egrecv __P((struct eg_softc *));
125 1.1 deraadt static int egstart __P((struct ifnet *));
126 1.1 deraadt static int egwatchdog __P((int));
127 1.1 deraadt static void egreset __P((struct eg_softc *));
128 1.1 deraadt static inline void egread __P((struct eg_softc *, caddr_t, int));
129 1.1 deraadt static struct mbuf *egget __P((caddr_t, int, struct ifnet *));
130 1.1 deraadt static void egstop __P((struct eg_softc *));
131 1.1 deraadt
132 1.1 deraadt /*
133 1.1 deraadt * Support stuff
134 1.1 deraadt */
135 1.1 deraadt
136 1.1 deraadt static inline void
137 1.1 deraadt egprintpcb(sc)
138 1.1 deraadt struct eg_softc *sc;
139 1.1 deraadt {
140 1.1 deraadt int i;
141 1.1 deraadt
142 1.1 deraadt for (i = 0; i < sc->eg_pcb[1] + 2; i++)
143 1.1 deraadt dlog((LOG_DEBUG, "pcb[%2d] = %x\n", sc->eg_pcb[i]));
144 1.1 deraadt }
145 1.1 deraadt
146 1.1 deraadt
147 1.1 deraadt static inline void
148 1.1 deraadt egprintstat(b)
149 1.1 deraadt u_char b;
150 1.1 deraadt {
151 1.1 deraadt dlog((LOG_DEBUG, "%s %s %s %s %s %s %s\n",
152 1.1 deraadt (b & EG_STAT_HCRE)?"HCRE":"",
153 1.1 deraadt (b & EG_STAT_ACRF)?"ACRF":"",
154 1.1 deraadt (b & EG_STAT_DIR )?"DIR ":"",
155 1.1 deraadt (b & EG_STAT_DONE)?"DONE":"",
156 1.1 deraadt (b & EG_STAT_ASF3)?"ASF3":"",
157 1.1 deraadt (b & EG_STAT_ASF2)?"ASF2":"",
158 1.1 deraadt (b & EG_STAT_ASF1)?"ASF1":""));
159 1.1 deraadt }
160 1.1 deraadt
161 1.1 deraadt static int
162 1.1 deraadt egoutPCB(sc, b)
163 1.1 deraadt struct eg_softc *sc;
164 1.1 deraadt u_char b;
165 1.1 deraadt {
166 1.1 deraadt int i;
167 1.1 deraadt
168 1.1 deraadt for (i=0; i < 4000; i++) {
169 1.1 deraadt if (inb(sc->eg_stat) & EG_STAT_HCRE) {
170 1.1 deraadt outb(sc->eg_cmd, b);
171 1.1 deraadt return 0;
172 1.1 deraadt }
173 1.1 deraadt delay(10);
174 1.1 deraadt }
175 1.1 deraadt dlog((LOG_DEBUG, "egoutPCB failed\n"));
176 1.1 deraadt return 1;
177 1.1 deraadt }
178 1.1 deraadt
179 1.1 deraadt static int
180 1.1 deraadt egreadPCBstat(sc, statb)
181 1.1 deraadt struct eg_softc *sc;
182 1.1 deraadt u_char statb;
183 1.1 deraadt {
184 1.1 deraadt int i;
185 1.1 deraadt
186 1.1 deraadt for (i=0; i < 5000; i++) {
187 1.1 deraadt if (EG_PCB_STAT(inb(sc->eg_stat)))
188 1.1 deraadt break;
189 1.1 deraadt delay(10);
190 1.1 deraadt }
191 1.1 deraadt if (EG_PCB_STAT(inb(sc->eg_stat)) == statb)
192 1.1 deraadt return 0;
193 1.1 deraadt return 1;
194 1.1 deraadt }
195 1.1 deraadt
196 1.1 deraadt static int
197 1.1 deraadt egreadPCBready(sc)
198 1.1 deraadt struct eg_softc *sc;
199 1.1 deraadt {
200 1.1 deraadt int i;
201 1.1 deraadt
202 1.1 deraadt for (i=0; i < 10000; i++) {
203 1.1 deraadt if (inb(sc->eg_stat) & EG_STAT_ACRF)
204 1.1 deraadt return 0;
205 1.1 deraadt delay(5);
206 1.1 deraadt }
207 1.1 deraadt dlog((LOG_DEBUG, "PCB read not ready\n"));
208 1.1 deraadt return 1;
209 1.1 deraadt }
210 1.1 deraadt
211 1.1 deraadt static int
212 1.1 deraadt egwritePCB(sc)
213 1.1 deraadt struct eg_softc *sc;
214 1.1 deraadt {
215 1.1 deraadt int i;
216 1.1 deraadt u_char len;
217 1.1 deraadt
218 1.1 deraadt
219 1.1 deraadt outb(sc->eg_ctl, EG_PCB_MASK(inb(sc->eg_ctl)));
220 1.1 deraadt
221 1.1 deraadt len = sc->eg_pcb[1] + 2;
222 1.1 deraadt for (i = 0; i < len; i++)
223 1.1 deraadt egoutPCB(sc, sc->eg_pcb[i]);
224 1.1 deraadt
225 1.1 deraadt for (i=0; i < 4000; i++) {
226 1.1 deraadt if (inb(sc->eg_stat) & EG_STAT_HCRE)
227 1.1 deraadt break;
228 1.1 deraadt delay(10);
229 1.1 deraadt }
230 1.1 deraadt outb(sc->eg_ctl, EG_PCB_MASK(inb(sc->eg_ctl)) | EG_PCB_DONE);
231 1.1 deraadt egoutPCB(sc, len);
232 1.1 deraadt
233 1.1 deraadt if (egreadPCBstat(sc, EG_PCB_ACCEPT))
234 1.1 deraadt return 1;
235 1.1 deraadt return 0;
236 1.1 deraadt }
237 1.1 deraadt
238 1.1 deraadt static int
239 1.1 deraadt egreadPCB(sc)
240 1.1 deraadt struct eg_softc *sc;
241 1.1 deraadt {
242 1.1 deraadt int i;
243 1.1 deraadt u_char b;
244 1.1 deraadt
245 1.1 deraadt outb(sc->eg_ctl, EG_PCB_MASK(inb(sc->eg_ctl)));
246 1.1 deraadt
247 1.1 deraadt bzero(sc->eg_pcb, sizeof(sc->eg_pcb));
248 1.1 deraadt
249 1.1 deraadt if (egreadPCBready(sc))
250 1.1 deraadt return 1;
251 1.1 deraadt
252 1.1 deraadt sc->eg_pcb[0] = inb(sc->eg_cmd);
253 1.1 deraadt
254 1.1 deraadt if (egreadPCBready(sc))
255 1.1 deraadt return 1;
256 1.1 deraadt
257 1.1 deraadt sc->eg_pcb[1] = inb(sc->eg_cmd);
258 1.1 deraadt
259 1.1 deraadt if (sc->eg_pcb[1] > 62) {
260 1.1 deraadt dlog((LOG_DEBUG, "len %d too large\n", sc->eg_pcb[1]));
261 1.1 deraadt return 1;
262 1.1 deraadt }
263 1.1 deraadt
264 1.1 deraadt for (i = 0; i < sc->eg_pcb[1]; i++) {
265 1.1 deraadt if (egreadPCBready(sc))
266 1.1 deraadt return 1;
267 1.1 deraadt sc->eg_pcb[2+i] = inb(sc->eg_cmd);
268 1.1 deraadt }
269 1.1 deraadt if (egreadPCBready(sc))
270 1.1 deraadt return 1;
271 1.1 deraadt if (egreadPCBstat(sc, EG_PCB_DONE))
272 1.1 deraadt return 1;
273 1.1 deraadt if ((b = inb(sc->eg_cmd)) != sc->eg_pcb[1]+2) {
274 1.1 deraadt dlog((LOG_DEBUG, "%d != %d\n", b, sc->eg_pcb[1]+2));
275 1.1 deraadt return 1;
276 1.1 deraadt }
277 1.1 deraadt outb(sc->eg_ctl, EG_PCB_MASK(inb(sc->eg_ctl)) | EG_PCB_ACCEPT);
278 1.1 deraadt return 0;
279 1.1 deraadt }
280 1.1 deraadt
281 1.1 deraadt /*
282 1.1 deraadt * Real stuff
283 1.1 deraadt */
284 1.1 deraadt
285 1.1 deraadt int
286 1.1 deraadt egprobe(parent, self, aux)
287 1.1 deraadt struct device *parent, *self;
288 1.1 deraadt void *aux;
289 1.1 deraadt {
290 1.1 deraadt struct eg_softc *sc = (void *)self;
291 1.1 deraadt struct isa_attach_args *ia = aux;
292 1.1 deraadt int i;
293 1.1 deraadt
294 1.1 deraadt if (ia->ia_iobase & ~0x07f0 != 0) {
295 1.1 deraadt dlog((LOG_DEBUG, "Weird iobase %x\n", ia->ia_iobase));
296 1.1 deraadt return 0;
297 1.1 deraadt }
298 1.1 deraadt
299 1.1 deraadt sc->eg_cmd = ia->ia_iobase + EG_COMMAND;
300 1.1 deraadt sc->eg_ctl = ia->ia_iobase + EG_CONTROL;
301 1.1 deraadt sc->eg_stat = ia->ia_iobase + EG_STATUS;
302 1.1 deraadt sc->eg_data = ia->ia_iobase + EG_DATA;
303 1.1 deraadt
304 1.1 deraadt /* hard reset card */
305 1.1 deraadt outb(sc->eg_ctl, EG_CTL_RESET);
306 1.1 deraadt outb(sc->eg_ctl, 0);
307 1.1 deraadt for (i = 0; i < 5000; i++) {
308 1.1 deraadt delay(1000);
309 1.1 deraadt if (EG_PCB_STAT(inb(sc->eg_stat)) == 0)
310 1.1 deraadt break;
311 1.1 deraadt }
312 1.1 deraadt if (EG_PCB_STAT(inb(sc->eg_stat)) != 0) {
313 1.1 deraadt dlog((LOG_DEBUG, "eg: Reset failed\n"));
314 1.1 deraadt return 0;
315 1.1 deraadt }
316 1.1 deraadt sc->eg_pcb[0] = EG_CMD_GETINFO; /* Get Adapter Info */
317 1.1 deraadt sc->eg_pcb[1] = 0;
318 1.1 deraadt if (egwritePCB(sc) != 0)
319 1.1 deraadt return 0;
320 1.1 deraadt
321 1.1 deraadt if (egreadPCB(sc) != 0) {
322 1.1 deraadt egprintpcb(sc);
323 1.1 deraadt return 0;
324 1.1 deraadt }
325 1.1 deraadt
326 1.1 deraadt if (sc->eg_pcb[0] != EG_RSP_GETINFO || /* Get Adapter Info Response */
327 1.1 deraadt sc->eg_pcb[1] != 0x0a) {
328 1.1 deraadt egprintpcb(sc);
329 1.1 deraadt return 0;
330 1.1 deraadt }
331 1.1 deraadt sc->eg_rom_major = sc->eg_pcb[3];
332 1.1 deraadt sc->eg_rom_minor = sc->eg_pcb[2];
333 1.1 deraadt sc->eg_ram = sc->eg_pcb[6] | (sc->eg_pcb[7] << 8);
334 1.1 deraadt
335 1.1 deraadt ia->ia_iosize = 0x08;
336 1.1 deraadt ia->ia_msize = 0;
337 1.1 deraadt return 1;
338 1.1 deraadt }
339 1.1 deraadt
340 1.1 deraadt static void
341 1.1 deraadt egattach(parent, self, aux)
342 1.1 deraadt struct device *parent, *self;
343 1.1 deraadt void *aux;
344 1.1 deraadt {
345 1.1 deraadt struct eg_softc *sc = (void *)self;
346 1.1 deraadt struct isa_attach_args *ia = aux;
347 1.1 deraadt struct ifnet *ifp = &sc->sc_arpcom.ac_if;
348 1.1 deraadt int i;
349 1.1 deraadt
350 1.1 deraadt outb(sc->eg_ctl, 0); /* make sure board is in a nice state */
351 1.1 deraadt
352 1.1 deraadt sc->eg_pcb[0] = EG_CMD_GETEADDR; /* Get Station address */
353 1.1 deraadt sc->eg_pcb[1] = 0;
354 1.1 deraadt if (egwritePCB(sc) != 0) {
355 1.1 deraadt dlog((LOG_DEBUG, "write error\n"));
356 1.1 deraadt return;
357 1.1 deraadt }
358 1.1 deraadt if (egreadPCB(sc) != 0) {
359 1.1 deraadt dlog((LOG_DEBUG, "read error\n"));
360 1.1 deraadt egprintpcb(sc);
361 1.1 deraadt return;
362 1.1 deraadt }
363 1.1 deraadt
364 1.1 deraadt /* check Get station address response */
365 1.1 deraadt if (sc->eg_pcb[0] != EG_RSP_GETEADDR || sc->eg_pcb[1] != 0x06) {
366 1.1 deraadt dlog((LOG_DEBUG, "parse error\n"));
367 1.1 deraadt egprintpcb(sc);
368 1.1 deraadt return;
369 1.1 deraadt }
370 1.1 deraadt bcopy(&sc->eg_pcb[2], sc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN);
371 1.1 deraadt
372 1.1 deraadt printf(": ROM v%d.%02d %dk address %s\n",
373 1.1 deraadt sc->eg_rom_major,
374 1.1 deraadt sc->eg_rom_minor,
375 1.1 deraadt sc->eg_ram,
376 1.1 deraadt ether_sprintf(sc->sc_arpcom.ac_enaddr));
377 1.1 deraadt
378 1.1 deraadt sc->eg_pcb[0] = EG_CMD_SETEADDR; /* Set station address */
379 1.1 deraadt if (egwritePCB(sc) != 0) {
380 1.1 deraadt dlog((LOG_DEBUG, "write error2\n"));
381 1.1 deraadt return;
382 1.1 deraadt }
383 1.1 deraadt if (egreadPCB(sc) != 0) {
384 1.1 deraadt dlog((LOG_DEBUG, "read error2\n"));
385 1.1 deraadt egprintpcb(sc);
386 1.1 deraadt return;
387 1.1 deraadt }
388 1.1 deraadt if (sc->eg_pcb[0] != EG_RSP_SETEADDR || sc->eg_pcb[1] != 0x02 ||
389 1.1 deraadt sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0) {
390 1.1 deraadt dlog((LOG_DEBUG, "parse error2\n"));
391 1.1 deraadt egprintpcb(sc);
392 1.1 deraadt return;
393 1.1 deraadt }
394 1.1 deraadt
395 1.1 deraadt /* Initialize ifnet structure. */
396 1.1 deraadt ifp->if_unit = sc->sc_dev.dv_unit;
397 1.1 deraadt ifp->if_name = egcd.cd_name;
398 1.1 deraadt ifp->if_mtu = ETHERMTU;
399 1.1 deraadt ifp->if_output = ether_output;
400 1.1 deraadt ifp->if_start = egstart;
401 1.1 deraadt ifp->if_ioctl = egioctl;
402 1.1 deraadt ifp->if_watchdog = egwatchdog;
403 1.1 deraadt ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
404 1.1 deraadt
405 1.1 deraadt /* Now we can attach the interface. */
406 1.1 deraadt if_attach(ifp);
407 1.1 deraadt ether_ifattach(ifp);
408 1.1 deraadt
409 1.1 deraadt #if NBPFILTER > 0
410 1.1 deraadt bpfattach(&sc->sc_arpcom.ac_if.if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
411 1.1 deraadt #endif
412 1.1 deraadt
413 1.1 deraadt sc->sc_ih.ih_fun = egintr;
414 1.1 deraadt sc->sc_ih.ih_arg = sc;
415 1.1 deraadt sc->sc_ih.ih_level = IPL_NET;
416 1.1 deraadt intr_establish(ia->ia_irq, &sc->sc_ih);
417 1.1 deraadt }
418 1.1 deraadt
419 1.1 deraadt static void
420 1.1 deraadt eginit(sc)
421 1.1 deraadt register struct eg_softc *sc;
422 1.1 deraadt {
423 1.1 deraadt register struct ifnet *ifp = &sc->sc_arpcom.ac_if;
424 1.1 deraadt int s;
425 1.1 deraadt
426 1.1 deraadt if (ifp->if_addrlist == 0)
427 1.1 deraadt return;
428 1.1 deraadt
429 1.1 deraadt s = splimp();
430 1.1 deraadt
431 1.1 deraadt /* soft reset the board */
432 1.1 deraadt outb(sc->eg_ctl, EG_CTL_FLSH);
433 1.1 deraadt delay(100);
434 1.1 deraadt outb(sc->eg_ctl, EG_CTL_ATTN);
435 1.1 deraadt delay(100);
436 1.1 deraadt outb(sc->eg_ctl, 0);
437 1.1 deraadt delay(200);
438 1.1 deraadt
439 1.1 deraadt sc->eg_pcb[0] = EG_CMD_CONFIG82586; /* Configure 82586 */
440 1.1 deraadt sc->eg_pcb[1] = 2;
441 1.1 deraadt sc->eg_pcb[2] = 3; /* receive broadcast & multicast */
442 1.1 deraadt sc->eg_pcb[3] = 0;
443 1.1 deraadt if (egwritePCB(sc) != 0)
444 1.1 deraadt dlog((LOG_DEBUG, "write error3\n"));
445 1.1 deraadt
446 1.1 deraadt if (egreadPCB(sc) != 0) {
447 1.1 deraadt dlog((LOG_DEBUG, "read error\n"));
448 1.1 deraadt egprintpcb(sc);
449 1.1 deraadt } else if (sc->eg_pcb[2] != 0 || sc->eg_pcb[3] != 0)
450 1.1 deraadt printf("eg: configure card command failed.\n");
451 1.1 deraadt
452 1.1 deraadt if(sc->eg_inbuf == NULL)
453 1.1 deraadt sc->eg_inbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
454 1.1 deraadt sc->eg_incount = 0;
455 1.1 deraadt
456 1.1 deraadt if(sc->eg_outbuf == NULL)
457 1.1 deraadt sc->eg_outbuf = malloc(EG_BUFLEN, M_TEMP, M_NOWAIT);
458 1.1 deraadt
459 1.1 deraadt ifp->if_flags |= IFF_RUNNING;
460 1.1 deraadt ifp->if_flags &= ~IFF_OACTIVE;
461 1.1 deraadt
462 1.1 deraadt outb(sc->eg_ctl, EG_CTL_CMDE);
463 1.1 deraadt
464 1.1 deraadt egstart(ifp);
465 1.1 deraadt egrecv(sc);
466 1.1 deraadt
467 1.1 deraadt splx(s);
468 1.1 deraadt }
469 1.1 deraadt
470 1.1 deraadt static int
471 1.1 deraadt egrecv(sc)
472 1.1 deraadt struct eg_softc *sc;
473 1.1 deraadt {
474 1.1 deraadt while (sc->eg_incount < EG_INLEN) {
475 1.1 deraadt sc->eg_pcb[0] = EG_CMD_RECVPACKET;
476 1.1 deraadt sc->eg_pcb[1] = 0x08;
477 1.1 deraadt sc->eg_pcb[2] = 0; /* address not used.. we send zero */
478 1.1 deraadt sc->eg_pcb[3] = 0;
479 1.1 deraadt sc->eg_pcb[4] = 0;
480 1.1 deraadt sc->eg_pcb[5] = 0;
481 1.1 deraadt sc->eg_pcb[6] = EG_BUFLEN & 0xff; /* our buffer size */
482 1.1 deraadt sc->eg_pcb[7] = (EG_BUFLEN >> 8) & 0xff;
483 1.1 deraadt sc->eg_pcb[8] = 0; /* timeout, 0 == none */
484 1.1 deraadt sc->eg_pcb[9] = 0;
485 1.1 deraadt if (egwritePCB(sc) == 0) {
486 1.1 deraadt sc->eg_incount++;
487 1.1 deraadt } else
488 1.1 deraadt break;
489 1.1 deraadt }
490 1.1 deraadt }
491 1.1 deraadt
492 1.1 deraadt static int
493 1.1 deraadt egstart(ifp)
494 1.1 deraadt struct ifnet *ifp;
495 1.1 deraadt {
496 1.1 deraadt register struct eg_softc *sc = egcd.cd_devs[ifp->if_unit];
497 1.1 deraadt struct mbuf *m0, *m;
498 1.1 deraadt int len;
499 1.1 deraadt short *ptr;
500 1.1 deraadt
501 1.1 deraadt /* Don't transmit if interface is busy or not running */
502 1.1 deraadt if ((sc->sc_arpcom.ac_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
503 1.1 deraadt return 0;
504 1.1 deraadt
505 1.1 deraadt /* Dequeue the next datagram. */
506 1.1 deraadt IF_DEQUEUE(&sc->sc_arpcom.ac_if.if_snd, m0);
507 1.1 deraadt if (m0 == NULL)
508 1.1 deraadt return 0;
509 1.1 deraadt
510 1.1 deraadt sc->sc_arpcom.ac_if.if_flags |= IFF_OACTIVE;
511 1.1 deraadt
512 1.1 deraadt /* Copy the datagram to the buffer. */
513 1.1 deraadt len = 0;
514 1.1 deraadt for (m = m0; m; m = m->m_next) {
515 1.1 deraadt if (m->m_len == 0)
516 1.1 deraadt continue;
517 1.1 deraadt if (len + m->m_len > EG_BUFLEN) {
518 1.1 deraadt dlog((LOG_DEBUG, "Packet too large to send\n"));
519 1.1 deraadt m_freem(m0);
520 1.1 deraadt sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
521 1.1 deraadt sc->sc_arpcom.ac_if.if_oerrors++;
522 1.1 deraadt return 0;
523 1.1 deraadt }
524 1.1 deraadt bcopy(mtod(m, caddr_t), sc->eg_outbuf + len, m->m_len);
525 1.1 deraadt len += m->m_len;
526 1.1 deraadt }
527 1.1 deraadt #if NBPFILTER > 0
528 1.1 deraadt if (sc->sc_arpcom.ac_if.if_bpf)
529 1.1 deraadt bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m0);
530 1.1 deraadt #endif
531 1.1 deraadt m_freem(m0);
532 1.1 deraadt
533 1.1 deraadt /* length must be a minimum of ETHER_MIN_LEN bytes */
534 1.1 deraadt len = max(len, ETHER_MIN_LEN);
535 1.1 deraadt
536 1.1 deraadt /* set direction bit: host -> adapter */
537 1.1 deraadt outb(sc->eg_ctl, inb(sc->eg_ctl) & ~EG_CTL_DIR);
538 1.1 deraadt
539 1.1 deraadt sc->eg_pcb[0] = EG_CMD_SENDPACKET;
540 1.1 deraadt sc->eg_pcb[1] = 0x06;
541 1.1 deraadt sc->eg_pcb[2] = 0; /* address not used, we send zero */
542 1.1 deraadt sc->eg_pcb[3] = 0;
543 1.1 deraadt sc->eg_pcb[4] = 0;
544 1.1 deraadt sc->eg_pcb[5] = 0;
545 1.1 deraadt sc->eg_pcb[6] = len & 0xff; /* length of packet */
546 1.1 deraadt sc->eg_pcb[7] = (len >> 8) & 0xff;
547 1.1 deraadt if (egwritePCB(sc) == 0) {
548 1.1 deraadt for (ptr = (short *) sc->eg_outbuf; len > 0; len -= 2) {
549 1.1 deraadt outw(sc->eg_data, *ptr++);
550 1.1 deraadt while (!(inb(sc->eg_stat) & EG_STAT_HRDY))
551 1.1 deraadt ; /* XXX need timeout here */
552 1.1 deraadt }
553 1.1 deraadt } else {
554 1.1 deraadt dlog((LOG_DEBUG, "egwritePCB in egstart failed\n"));
555 1.1 deraadt sc->sc_arpcom.ac_if.if_oerrors++;
556 1.1 deraadt sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
557 1.1 deraadt }
558 1.1 deraadt
559 1.1 deraadt /* Set direction bit : Adapter -> host */
560 1.1 deraadt outb(sc->eg_ctl, inb(sc->eg_ctl) | EG_CTL_DIR);
561 1.1 deraadt
562 1.1 deraadt return 0;
563 1.1 deraadt }
564 1.1 deraadt
565 1.1 deraadt int
566 1.1 deraadt egintr(sc)
567 1.1 deraadt register struct eg_softc *sc;
568 1.1 deraadt {
569 1.1 deraadt int i, len;
570 1.1 deraadt short *ptr;
571 1.1 deraadt
572 1.1 deraadt while (inb(sc->eg_stat) & EG_STAT_ACRF) {
573 1.1 deraadt egreadPCB(sc);
574 1.1 deraadt switch(sc->eg_pcb[0]) {
575 1.1 deraadt case EG_RSP_RECVPACKET:
576 1.1 deraadt len = sc->eg_pcb[6]+(sc->eg_pcb[7]<<8);
577 1.1 deraadt for (ptr = (short *) sc->eg_inbuf; len > 0; len -= 2) {
578 1.1 deraadt while (!(inb(sc->eg_stat) & EG_STAT_HRDY))
579 1.1 deraadt ;
580 1.1 deraadt *ptr++ = inw(sc->eg_data);
581 1.1 deraadt }
582 1.1 deraadt len = sc->eg_pcb[8]+(sc->eg_pcb[9]<<8);
583 1.1 deraadt egrecv(sc);
584 1.1 deraadt sc->sc_arpcom.ac_if.if_ipackets++;
585 1.1 deraadt egread(sc, sc->eg_inbuf, len);
586 1.1 deraadt sc->eg_incount--;
587 1.1 deraadt break;
588 1.1 deraadt
589 1.1 deraadt case EG_RSP_SENDPACKET:
590 1.1 deraadt if (sc->eg_pcb[6] || sc->eg_pcb[7]) {
591 1.1 deraadt dlog((LOG_DEBUG, "packet dropped\n"));
592 1.1 deraadt sc->sc_arpcom.ac_if.if_oerrors++;
593 1.1 deraadt }
594 1.1 deraadt sc->sc_arpcom.ac_if.if_opackets++;
595 1.1 deraadt sc->sc_arpcom.ac_if.if_collisions += sc->eg_pcb[8] & 0xf;
596 1.1 deraadt sc->sc_arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
597 1.1 deraadt egstart(&sc->sc_arpcom.ac_if);
598 1.1 deraadt break;
599 1.1 deraadt
600 1.1 deraadt case EG_RSP_GETSTATS:
601 1.1 deraadt egprintpcb();
602 1.1 deraadt dlog((LOG_DEBUG, "Card Statistics\n"));
603 1.1 deraadt break;
604 1.1 deraadt
605 1.1 deraadt default:
606 1.1 deraadt dlog((LOG_DEBUG, "egintr: Unknown response %x??\n",
607 1.1 deraadt sc->eg_pcb[0]));
608 1.1 deraadt break;
609 1.1 deraadt }
610 1.1 deraadt }
611 1.1 deraadt
612 1.1 deraadt return 0;
613 1.1 deraadt }
614 1.1 deraadt
615 1.1 deraadt /*
616 1.1 deraadt * Pass a packet up to the higher levels.
617 1.1 deraadt */
618 1.1 deraadt static inline void
619 1.1 deraadt egread(sc, buf, len)
620 1.1 deraadt struct eg_softc *sc;
621 1.1 deraadt caddr_t buf;
622 1.1 deraadt int len;
623 1.1 deraadt {
624 1.1 deraadt register struct ether_header *eh;
625 1.1 deraadt struct mbuf *m;
626 1.1 deraadt int i;
627 1.1 deraadt
628 1.1 deraadt eh = (struct ether_header *)buf;
629 1.1 deraadt if (len <= sizeof(struct ether_header) ||
630 1.1 deraadt len > ETHER_MAX_LEN) {
631 1.1 deraadt dlog((LOG_DEBUG, "Unacceptable packet size %d\n", len));
632 1.1 deraadt sc->sc_arpcom.ac_if.if_ierrors++;
633 1.1 deraadt return;
634 1.1 deraadt }
635 1.1 deraadt len -= sizeof(struct ether_header);
636 1.1 deraadt
637 1.1 deraadt m = egget(buf, len, &sc->sc_arpcom.ac_if);
638 1.1 deraadt if (m == 0) {
639 1.1 deraadt dlog((LOG_DEBUG, "egget returned 0\n"));
640 1.1 deraadt sc->sc_arpcom.ac_if.if_ierrors++;
641 1.1 deraadt return;
642 1.1 deraadt }
643 1.1 deraadt
644 1.1 deraadt #if NBPFILTER > 0
645 1.1 deraadt /*
646 1.1 deraadt * Check if there's a BPF listener on this interface.
647 1.1 deraadt * If so, hand off the raw packet to bpf.
648 1.1 deraadt */
649 1.1 deraadt if (sc->sc_arpcom.ac_if.if_bpf) {
650 1.1 deraadt bpf_mtap(sc->sc_arpcom.ac_if.if_bpf, m);
651 1.1 deraadt
652 1.1 deraadt /*
653 1.1 deraadt * Note that the interface cannot be in promiscuous mode if
654 1.1 deraadt * there are no BPF listeners. And if we are in promiscuous
655 1.1 deraadt * mode, we have to check if this packet is really ours.
656 1.1 deraadt */
657 1.1 deraadt if ((sc->sc_arpcom.ac_if.if_flags & IFF_PROMISC) &&
658 1.1 deraadt (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
659 1.1 deraadt bcmp(eh->ether_dhost, sc->sc_arpcom.ac_enaddr,
660 1.1 deraadt sizeof(eh->ether_dhost)) != 0) {
661 1.1 deraadt m_freem(m);
662 1.1 deraadt return;
663 1.1 deraadt }
664 1.1 deraadt }
665 1.1 deraadt #endif
666 1.1 deraadt
667 1.1 deraadt ether_input(&sc->sc_arpcom.ac_if, eh, m);
668 1.1 deraadt }
669 1.1 deraadt
670 1.1 deraadt /*
671 1.1 deraadt * convert buf into mbufs
672 1.1 deraadt */
673 1.1 deraadt
674 1.1 deraadt static struct mbuf *
675 1.1 deraadt egget(buf, totlen, ifp)
676 1.1 deraadt caddr_t buf;
677 1.1 deraadt int totlen;
678 1.1 deraadt struct ifnet *ifp;
679 1.1 deraadt {
680 1.1 deraadt struct mbuf *top, **mp, *m, *p;
681 1.1 deraadt int len;
682 1.1 deraadt register caddr_t cp = buf;
683 1.1 deraadt char *epkt;
684 1.1 deraadt
685 1.1 deraadt buf += sizeof(struct ether_header);
686 1.1 deraadt cp = buf;
687 1.1 deraadt epkt = cp + totlen;
688 1.1 deraadt
689 1.1 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
690 1.1 deraadt if (m == 0) {
691 1.1 deraadt dlog((LOG_DEBUG, "MGETHDR returns 0\n"));
692 1.1 deraadt return 0;
693 1.1 deraadt }
694 1.1 deraadt m->m_pkthdr.rcvif = ifp;
695 1.1 deraadt m->m_pkthdr.len = totlen;
696 1.1 deraadt m->m_len = MHLEN;
697 1.1 deraadt top = 0;
698 1.1 deraadt mp = ⊤
699 1.1 deraadt
700 1.1 deraadt while (totlen > 0) {
701 1.1 deraadt if (top) {
702 1.1 deraadt MGET(m, M_DONTWAIT, MT_DATA);
703 1.1 deraadt if (m == 0) {
704 1.1 deraadt m_freem(top);
705 1.1 deraadt dlog((LOG_DEBUG, "MGET returns 0\n"));
706 1.1 deraadt return 0;
707 1.1 deraadt }
708 1.1 deraadt m->m_len = MLEN;
709 1.1 deraadt }
710 1.1 deraadt len = min(totlen, epkt - cp);
711 1.1 deraadt if (len >= MINCLSIZE) {
712 1.1 deraadt MCLGET(m, M_DONTWAIT);
713 1.1 deraadt if (m->m_flags & M_EXT)
714 1.1 deraadt m->m_len = len = min(len, MCLBYTES);
715 1.1 deraadt else
716 1.1 deraadt len = m->m_len;
717 1.1 deraadt } else {
718 1.1 deraadt /*
719 1.1 deraadt * Place initial small packet/header at end of mbuf.
720 1.1 deraadt */
721 1.1 deraadt if (len < m->m_len) {
722 1.1 deraadt if (top == 0 && len + max_linkhdr <= m->m_len)
723 1.1 deraadt m->m_data += max_linkhdr;
724 1.1 deraadt m->m_len = len;
725 1.1 deraadt } else
726 1.1 deraadt len = m->m_len;
727 1.1 deraadt }
728 1.1 deraadt bcopy(cp, mtod(m, caddr_t), (unsigned)len);
729 1.1 deraadt cp += len;
730 1.1 deraadt *mp = m;
731 1.1 deraadt mp = &m->m_next;
732 1.1 deraadt totlen -= len;
733 1.1 deraadt if (cp == epkt)
734 1.1 deraadt cp = buf;
735 1.1 deraadt }
736 1.1 deraadt
737 1.1 deraadt return top;
738 1.1 deraadt }
739 1.1 deraadt
740 1.1 deraadt static int
741 1.1 deraadt egioctl(ifp, cmd, data)
742 1.1 deraadt register struct ifnet *ifp;
743 1.1 deraadt int cmd;
744 1.1 deraadt caddr_t data;
745 1.1 deraadt {
746 1.1 deraadt register struct ifaddr *ifa = (struct ifaddr *) data;
747 1.1 deraadt struct eg_softc *sc = egcd.cd_devs[ifp->if_unit];
748 1.1 deraadt struct ifreq *ifr = (struct ifreq *) data;
749 1.1 deraadt int error = 0;
750 1.1 deraadt
751 1.1 deraadt switch (cmd) {
752 1.1 deraadt case SIOCSIFADDR:
753 1.1 deraadt ifp->if_flags |= IFF_UP;
754 1.1 deraadt switch (ifa->ifa_addr->sa_family) {
755 1.1 deraadt #ifdef INET
756 1.1 deraadt case AF_INET:
757 1.1 deraadt eginit(sc); /* before arpwhohas */
758 1.1 deraadt ((struct arpcom *) ifp)->ac_ipaddr = IA_SIN(ifa)->sin_addr;
759 1.1 deraadt arpwhohas((struct arpcom *) ifp, &IA_SIN(ifa)->sin_addr);
760 1.1 deraadt break;
761 1.1 deraadt #endif
762 1.1 deraadt #ifdef NS
763 1.1 deraadt case AF_NS:
764 1.1 deraadt {
765 1.1 deraadt register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
766 1.1 deraadt
767 1.1 deraadt if (ns_nullhost(*ina))
768 1.1 deraadt ina->x_host =
769 1.1 deraadt *(union ns_host *)(sc->sc_arpcom.ac_enaddr);
770 1.1 deraadt else {
771 1.1 deraadt ifp->if_flags &= ~IFF_RUNNING;
772 1.1 deraadt bcopy(ina->x_host.c_host,
773 1.1 deraadt sc->sc_arpcom.ac_enaddr,
774 1.1 deraadt sizeof(sc->sc_arpcom.ac_enaddr));
775 1.1 deraadt }
776 1.1 deraadt eginit(sc);
777 1.1 deraadt break;
778 1.1 deraadt }
779 1.1 deraadt #endif
780 1.1 deraadt default:
781 1.1 deraadt eginit(sc);
782 1.1 deraadt break;
783 1.1 deraadt }
784 1.1 deraadt break;
785 1.1 deraadt case SIOCSIFFLAGS:
786 1.1 deraadt if ((ifp->if_flags & IFF_UP) == 0 &&
787 1.1 deraadt (ifp->if_flags & IFF_RUNNING) != 0) {
788 1.1 deraadt ifp->if_flags &= ~IFF_RUNNING;
789 1.1 deraadt egstop(sc);
790 1.1 deraadt break;
791 1.1 deraadt } else if ((ifp->if_flags & IFF_UP) != 0 &&
792 1.1 deraadt (ifp->if_flags & IFF_RUNNING) == 0) {
793 1.1 deraadt eginit(sc);
794 1.1 deraadt } else {
795 1.1 deraadt /*
796 1.1 deraadt * XXX deal with flags changes:
797 1.1 deraadt * IFF_MULTICAST, IFF_PROMISC,
798 1.1 deraadt * IFF_LINK0, IFF_LINK1,
799 1.1 deraadt */
800 1.1 deraadt }
801 1.1 deraadt break;
802 1.1 deraadt default:
803 1.1 deraadt error = EINVAL;
804 1.1 deraadt }
805 1.1 deraadt return (error);
806 1.1 deraadt }
807 1.1 deraadt
808 1.1 deraadt static void
809 1.1 deraadt egreset(sc)
810 1.1 deraadt struct eg_softc *sc;
811 1.1 deraadt {
812 1.1 deraadt int s;
813 1.1 deraadt
814 1.1 deraadt dlog((LOG_DEBUG, "egreset()\n"));
815 1.1 deraadt s = splimp();
816 1.1 deraadt egstop(sc);
817 1.1 deraadt eginit(sc);
818 1.1 deraadt splx(s);
819 1.1 deraadt }
820 1.1 deraadt
821 1.1 deraadt static int
822 1.1 deraadt egwatchdog(unit)
823 1.1 deraadt int unit;
824 1.1 deraadt {
825 1.1 deraadt struct eg_softc *sc = egcd.cd_devs[unit];
826 1.1 deraadt
827 1.1 deraadt log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
828 1.1 deraadt sc->sc_arpcom.ac_if.if_oerrors++;
829 1.1 deraadt
830 1.1 deraadt egreset(sc);
831 1.1 deraadt return 0;
832 1.1 deraadt }
833 1.1 deraadt
834 1.1 deraadt static void
835 1.1 deraadt egstop(sc)
836 1.1 deraadt register struct eg_softc *sc;
837 1.1 deraadt {
838 1.1 deraadt
839 1.1 deraadt outb(sc->eg_ctl, 0);
840 1.1 deraadt }
841