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