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