xref: /netbsd-src/sys/dev/ic/elink3.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: elink3.c,v 1.32 1997/05/14 00:22:00 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 1996, 1997 Jonathan Stone <jonathan@NetBSD.org>
5  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Herb Peyerl.
19  * 4. The name of Herb Peyerl may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "bpfilter.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/errno.h>
42 #include <sys/syslog.h>
43 #include <sys/select.h>
44 #include <sys/device.h>
45 
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_ether.h>
49 #include <net/if_media.h>
50 
51 #ifdef INET
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/in_var.h>
55 #include <netinet/ip.h>
56 #include <netinet/if_inarp.h>
57 #endif
58 
59 #ifdef NS
60 #include <netns/ns.h>
61 #include <netns/ns_if.h>
62 #endif
63 
64 #if NBPFILTER > 0
65 #include <net/bpf.h>
66 #include <net/bpfdesc.h>
67 #endif
68 
69 #include <machine/cpu.h>
70 #include <machine/bus.h>
71 #include <machine/intr.h>
72 
73 #include <dev/ic/elink3var.h>
74 #include <dev/ic/elink3reg.h>
75 
76 #define ETHER_MIN_LEN	64
77 #define ETHER_MAX_LEN   1518
78 #define ETHER_ADDR_LEN  6
79 
80 /*
81  * Structure to map  media-present bits in boards to
82  * ifmedia codes and printable media names. Used for table-driven
83  * ifmedia initialization.
84  */
85 struct ep_media {
86 	int	epm_eeprom_data;	/* bitmask for eeprom config */
87 	int	epm_conn;		/* sc->ep_connectors code for medium */
88 	char*	epm_name;		/* name of medium */
89 	int	epm_ifmedia;		/* ifmedia word for medium */
90 	int	epm_ifdata;
91 };
92 
93 /*
94  * ep_media table for Vortex/Demon/Boomerang:
95  * map from media-present bits in register RESET_OPTIONS+2
96  * to  ifmedia "media words" and printable names.
97  *
98  * XXX indexed directly by INTERNAL_CONFIG default_media field,
99  * (i.e., EPMEDIA_ constants)  forcing order of entries.
100  *  Note that 3 is reserved.
101  */
102 struct ep_media ep_vortex_media[8] = {
103   { EP_PCI_UTP,        EPC_UTP, "utp",	    IFM_ETHER|IFM_10_T,
104        EPMEDIA_10BASE_T },
105   { EP_PCI_AUI,        EPC_AUI, "aui",	    IFM_ETHER|IFM_10_5,
106        EPMEDIA_AUI },
107   { 0,                 0,  	"reserved", IFM_NONE,  EPMEDIA_RESV1 },
108   { EP_PCI_BNC,        EPC_BNC, "bnc",	    IFM_ETHER|IFM_10_2,
109        EPMEDIA_10BASE_2 },
110   { EP_PCI_100BASE_TX, EPC_100TX, "100-TX", IFM_ETHER|IFM_100_TX,
111        EPMEDIA_100BASE_TX },
112   { EP_PCI_100BASE_FX, EPC_100FX, "100-FX", IFM_ETHER|IFM_100_FX,
113        EPMEDIA_100BASE_FX },
114   { EP_PCI_100BASE_MII,EPC_MII,   "mii",    IFM_ETHER|IFM_100_TX,
115        EPMEDIA_MII },
116   { EP_PCI_100BASE_T4, EPC_100T4, "100-T4", IFM_ETHER|IFM_100_T4,
117        EPMEDIA_100BASE_T4 }
118 };
119 
120 /*
121  * ep_media table for 3c509/3c509b/3c579/3c589:
122  * map from media-present bits in register CNFG_CNTRL
123  * (window 0, offset ?) to  ifmedia "media words" and printable names.
124  */
125 struct ep_media ep_isa_media[3] = {
126   { EP_W0_CC_UTP,  EPC_UTP, "utp",   IFM_ETHER|IFM_10_T, EPMEDIA_10BASE_T },
127   { EP_W0_CC_AUI,  EPC_AUI, "aui",   IFM_ETHER|IFM_10_5, EPMEDIA_AUI },
128   { EP_W0_CC_BNC,  EPC_BNC, "bnc",   IFM_ETHER|IFM_10_2, EPMEDIA_10BASE_2 },
129 };
130 
131 /* Map vortex reset_options bits to if_media codes. */
132 const u_int ep_default_to_media[8] = {
133 	IFM_ETHER | IFM_10_T,
134 	IFM_ETHER | IFM_10_5,
135 	0, 			/* reserved by 3Com */
136 	IFM_ETHER | IFM_10_2,
137 	IFM_ETHER | IFM_100_TX,
138 	IFM_ETHER | IFM_100_FX,
139 	IFM_ETHER | IFM_100_TX,	/* XXX really MII: need to talk to PHY */
140 	IFM_ETHER | IFM_100_T4,
141 };
142 
143 /* Autoconfig defintion of driver back-end */
144 struct cfdriver ep_cd = {
145 	NULL, "ep", DV_IFNET
146 };
147 
148 
149 void	ep_internalconfig __P((struct ep_softc *sc));
150 void	ep_vortex_probemedia __P((struct ep_softc *sc));
151 void	ep_isa_probemedia __P((struct ep_softc *sc));
152 
153 static void eptxstat __P((struct ep_softc *));
154 static int epstatus __P((struct ep_softc *));
155 void epinit __P((struct ep_softc *));
156 int epioctl __P((struct ifnet *, u_long, caddr_t));
157 void epstart __P((struct ifnet *));
158 void epwatchdog __P((struct ifnet *));
159 void epreset __P((struct ep_softc *));
160 static void epshutdown __P((void *));
161 void	epread __P((struct ep_softc *));
162 struct mbuf *epget __P((struct ep_softc *, int));
163 void	epmbuffill __P((void *));
164 void	epmbufempty __P((struct ep_softc *));
165 void	epsetfilter __P((struct ep_softc *));
166 int	epsetmedia __P((struct ep_softc *, int epmedium));
167 
168 /* ifmedia callbacks */
169 int	ep_media_change __P((struct ifnet *ifp));
170 void	ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
171 
172 static int epbusyeeprom __P((struct ep_softc *));
173 static inline void ep_complete_cmd __P((struct ep_softc *sc,
174 					u_int cmd, u_int arg));
175 
176 
177 /*
178  * Issue a (reset) command, and be sure it has completed.
179  * Used for commands that reset part or all of the  board.
180  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
181  * but older hardware doesn't implement it and we must delay.
182  * It's easiest to just delay always.
183  */
184 static inline void
185 ep_complete_cmd(sc, cmd, arg)
186 	struct ep_softc *sc;
187 	u_int cmd, arg;
188 {
189 	register bus_space_tag_t iot = sc->sc_iot;
190 	register bus_space_handle_t ioh = sc->sc_ioh;
191 
192 	bus_space_write_2(iot, ioh, cmd, arg);
193 
194 #ifdef notyet
195 	/* if this adapter family has S_COMMAND_IN_PROGRESS, use it */
196 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
197 		;
198 	else
199 #else
200 	DELAY(100000);	/* need at least 1 ms, but be generous. */
201 #endif
202 }
203 
204 
205 
206 /*
207  * Back-end attach and configure.
208  */
209 void
210 epconfig(sc, chipset)
211 	struct ep_softc *sc;
212 	u_short chipset;
213 {
214 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
215 	bus_space_tag_t iot = sc->sc_iot;
216 	bus_space_handle_t ioh = sc->sc_ioh;
217 	u_int16_t i;
218 	u_int8_t myla[6];
219 
220 	sc->ep_chipset = chipset;
221 
222 	/*
223 	 * We could have been groveling around in other register
224 	 * windows in the front-end; make sure we're in window 0
225 	 * to read the EEPROM.
226 	 */
227 	GO_WINDOW(0);
228 
229 	/*
230 	 * Read the station address from the eeprom
231 	 */
232 	for (i = 0; i < 3; i++) {
233 		u_int16_t x;
234 		if (epbusyeeprom(sc))
235 			return;		/* XXX why is eeprom busy? */
236 		bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
237 		    READ_EEPROM | i);
238 		if (epbusyeeprom(sc))
239 			return;		/* XXX why is eeprom busy? */
240 		x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
241 		myla[(i << 1)] = x >> 8;
242 		myla[(i << 1) + 1] = x;
243 	}
244 
245 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
246 	    ether_sprintf(myla));
247 
248 	/*
249 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900,3c515?) cards
250 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
251 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
252 	 * packet length.
253 	 * Commands to these cards implicitly upshift a packet size
254 	 * or threshold by 2 bits.
255 	 * To detect  cards with large-packet support, we probe by setting
256 	 * the transmit threshold register, then change windows and
257 	 * read back the threshold register directly, and see if the
258 	 * threshold value was shifted or not.
259 	 */
260 	bus_space_write_2(iot, ioh, EP_COMMAND,
261 			  SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
262 	GO_WINDOW(5);
263 	i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
264 	GO_WINDOW(1);
265 	switch (i)  {
266 	case EP_LARGEWIN_PROBE:
267 	case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
268 		sc->ep_pktlenshift = 0;
269 		break;
270 
271 	case (EP_LARGEWIN_PROBE << 2):
272 		sc->ep_pktlenshift = 2;
273 		/* XXX does the 3c515 support Vortex-style RESET_OPTIONS? */
274 		break;
275 
276 	default:
277 		printf("%s: wrote %d to TX_AVAIL_THRESH, read back %d. "
278 		    "Interface disabled\n",
279 		    sc->sc_dev.dv_xname, EP_THRESH_DISABLE, (int) i);
280 		return;
281 	}
282 
283 	/*
284 	 * Ensure Tx-available interrupts are enabled for
285 	 * start the interface.
286 	 * XXX should be in epinit()?
287 	 */
288 	bus_space_write_2(iot, ioh, EP_COMMAND,
289 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
290 
291 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
292 	ifp->if_softc = sc;
293 	ifp->if_start = epstart;
294 	ifp->if_ioctl = epioctl;
295 	ifp->if_watchdog = epwatchdog;
296 	ifp->if_flags =
297 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
298 
299 	if_attach(ifp);
300 	ether_ifattach(ifp, myla);
301 
302 	/*
303 	 * Finish configuration:
304 	 * determine chipset if the front-end couldn't do so,
305 	 * show board details, set media.
306 	 */
307 
308 	/* print RAM size */
309 	ep_internalconfig(sc);
310 	GO_WINDOW(0);
311 
312 	ifmedia_init(&sc->sc_media, 0, ep_media_change, ep_media_status);
313 
314 	/*
315 	 * If we've got an indirect (ISA, PCMCIA?) board, the chipset
316 	 * is unknown.  If the board has large-packet support, it's a
317 	 * Vortex/Boomerang, otherwise it's a 3c509.
318 	 * XXX use eeprom capability word instead?
319 	 */
320 	if (sc->ep_chipset == EP_CHIPSET_UNKNOWN && sc->ep_pktlenshift)  {
321 		printf("warning: unknown chipset, possibly 3c515?\n");
322 #ifdef notyet
323 		sc->sc_chipset = EP_CHIPSET_VORTEX;
324 #endif	/* notyet */
325 	}
326 
327 	/*
328 	 * Ascertain which media types are present and inform ifmedia.
329 	 */
330 	switch (sc->ep_chipset) {
331 	/* on a direct bus, the attach routine can tell, but check anyway. */
332 	case EP_CHIPSET_VORTEX:
333 	case EP_CHIPSET_BOOMERANG2:
334 		ep_vortex_probemedia(sc);
335 		break;
336 
337 	/* on ISA we can't yet tell 3c509 from 3c515. Assume the former. */
338 	case EP_CHIPSET_3C509:
339 	default:
340 		ep_isa_probemedia(sc);
341 		break;
342 	}
343 
344 	GO_WINDOW(1);		/* Window 1 is operating window */
345 
346 #if NBPFILTER > 0
347 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
348 		  sizeof(struct ether_header));
349 #endif
350 
351 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
352 
353 	/*  Establish callback to reset card when we reboot. */
354 	shutdownhook_establish(epshutdown, sc);
355 
356 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
357 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
358 }
359 
360 
361 /*
362  * Show interface-model-independent info from window 3
363  * internal-configuration register.
364  */
365 void
366 ep_internalconfig(sc)
367 	struct ep_softc *sc;
368 {
369 	bus_space_tag_t iot = sc->sc_iot;
370 	bus_space_handle_t ioh = sc->sc_ioh;
371 
372 	u_int config0;
373 	u_int config1;
374 
375 	int  ram_size, ram_width, ram_speed, rom_size, ram_split;
376 	/*
377 	 * NVRAM buffer Rx:Tx config names for busmastering cards
378 	 * (Demon, Vortex, and later).
379 	 */
380 	const char *onboard_ram_config[] = {
381 		"5:3", "3:1", "1:1", "(undefined)" };
382 
383 	GO_WINDOW(3);
384 	config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
385 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
386 	GO_WINDOW(0);
387 
388 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
389 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
390 	ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
391 	rom_size  = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
392 
393 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
394 
395 	printf("%s: %dKB %s-wide FIFO, %s Rx:Tx split, ",
396 	       sc->sc_dev.dv_xname,
397 	       8 << ram_size,
398 	       (ram_width) ? "word" : "byte",
399 	       onboard_ram_config[ram_split]);
400 }
401 
402 
403 /*
404  * Find supported media on 3c509-generation hardware that doesn't have
405  * a "reset_options" register in window 3.
406  * Use the config_cntrl register  in window 0 instead.
407  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
408  * that implement  CONFIG_CTRL.  We don't have a good way to set the
409  * default active mediuim; punt to ifconfig  instead.
410  *
411  * XXX what about 3c515, pcmcia 10/100?
412  */
413 void
414 ep_isa_probemedia(sc)
415 	struct ep_softc *sc;
416 {
417 	bus_space_tag_t iot = sc->sc_iot;
418 	bus_space_handle_t ioh = sc->sc_ioh;
419 	struct ifmedia *ifm = &sc->sc_media;
420 	int	conn, i;
421 	u_int16_t ep_w0_config, port;
422 
423 	conn = 0;
424 	GO_WINDOW(0);
425 	ep_w0_config = bus_space_read_2(iot, ioh, EP_W0_CONFIG_CTRL);
426 	for (i = 0; i < 3; i++) {
427 		struct ep_media * epm = ep_isa_media + i;
428 
429 		if ((ep_w0_config & epm->epm_eeprom_data) != 0) {
430 
431 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
432 			if (conn)
433 				printf("/");
434 			printf(epm->epm_name);
435 			conn |= epm->epm_conn;
436 		}
437 	}
438 	sc->ep_connectors = conn;
439 
440 	/* get default medium from EEPROM */
441 	if (epbusyeeprom(sc))
442 		return;		/* XXX why is eeprom busy? */
443 	bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
444 	    READ_EEPROM | EEPROM_ADDR_CFG);
445 	if (epbusyeeprom(sc))
446 		return;		/* XXX why is  eeprom busy? */
447 	port = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
448 	port = port >> 14;
449 
450 	printf(" (default %s)\n", ep_vortex_media[port].epm_name);
451 	/* tell ifconfig what currently-active media is. */
452 	ifmedia_set(ifm, ep_default_to_media[port]);
453 
454 	/* XXX autoselect not yet implemented */
455 }
456 
457 
458 /*
459  * Find media present on large-packet-capable elink3 devices.
460  * Show onboard configuration of large-packet-capable elink3 devices
461  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
462  * Use media and card-version info in window 3 instead.
463  *
464  * XXX how much of this works with 3c515, pcmcia 10/100?
465  */
466 void
467 ep_vortex_probemedia(sc)
468 	struct ep_softc *sc;
469 {
470 	bus_space_tag_t iot = sc->sc_iot;
471 	bus_space_handle_t ioh = sc->sc_ioh;
472 	struct ifmedia *ifm = &sc->sc_media;
473 	u_int config1, conn;
474 	int reset_options;
475 	int default_media;	/* 3-bit encoding of default (EEPROM) media */
476 	int autoselect;		/* boolean: should default to autoselect */
477 	const char *medium_name;
478 	register int i;
479 
480 	GO_WINDOW(3);
481 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
482 	reset_options  = (int)bus_space_read_1(iot, ioh, EP_W3_RESET_OPTIONS);
483 	GO_WINDOW(0);
484 
485 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
486         autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
487 
488 	/* set available media options */
489 	conn = 0;
490 	for (i = 0; i < 8; i++) {
491 		struct ep_media * epm = ep_vortex_media + i;
492 
493 		if ((reset_options & epm->epm_eeprom_data) != 0) {
494 			if (conn) printf("/");
495 			printf(epm->epm_name);
496 			conn |= epm->epm_conn;
497 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
498 		}
499 	}
500 
501 	sc->ep_connectors = conn;
502 
503 	/* Show  eeprom's idea of default media.  */
504 	medium_name = (default_media > 8)
505 		? "(unknown/impossible media)"
506 		: ep_vortex_media[default_media].epm_name;
507 	printf(" default %s%s\n",
508 	       medium_name,  (autoselect)? ", autoselect" : "" );
509 
510 #ifdef notyet
511 	/*
512 	 * Set default: either the active interface the card
513 	 * reads  from the EEPROM, or if autoselect is true,
514 	 * whatever we find is actually connected.
515 	 *
516 	 * XXX autoselect not yet implemented.
517 	 */
518 #endif	/* notyet */
519 
520 	/* tell ifconfig what currently-active media is. */
521 	ifmedia_set(ifm, ep_default_to_media[default_media]);
522 }
523 
524 
525 /*
526  * Bring device up.
527  *
528  * The order in here seems important. Otherwise we may not receive
529  * interrupts. ?!
530  */
531 void
532 epinit(sc)
533 	register struct ep_softc *sc;
534 {
535 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
536 	bus_space_tag_t iot = sc->sc_iot;
537 	bus_space_handle_t ioh = sc->sc_ioh;
538 	int i;
539 
540 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
541 		;
542 
543 	if (sc->bustype != EP_BUS_PCI) {
544 		GO_WINDOW(0);
545 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
546 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
547 	}
548 
549 	if (sc->bustype == EP_BUS_PCMCIA) {
550 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
551 	}
552 
553 	GO_WINDOW(2);
554 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
555 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
556 		    LLADDR(ifp->if_sadl)[i]);
557 
558 	/*
559 	 * Reset the station-address receive filter.
560 	 * A bug workaround for busmastering  (Vortex, Demon) cards.
561 	 */
562 	for (i = 0; i < 6; i++)
563 		bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
564 
565 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
566 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
567 
568 	GO_WINDOW(1);		/* Window 1 is operating window */
569 	for (i = 0; i < 31; i++)
570 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
571 
572 	/* Set threshhold for for Tx-space avaiable interrupt. */
573 	bus_space_write_2(iot, ioh, EP_COMMAND,
574 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
575 
576 	/* Enable interrupts. */
577 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
578 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
579 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
580 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
581 
582 	/*
583 	 * Attempt to get rid of any stray interrupts that occured during
584 	 * configuration.  On the i386 this isn't possible because one may
585 	 * already be queued.  However, a single stray interrupt is
586 	 * unimportant.
587 	 */
588 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
589 
590 	epsetfilter(sc);
591 	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
592 
593 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
594 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
595 
596 	epmbuffill(sc);
597 
598 	/* Interface is now `running', with no output active. */
599 	ifp->if_flags |= IFF_RUNNING;
600 	ifp->if_flags &= ~IFF_OACTIVE;
601 
602 	/* Attempt to start output, if any. */
603 	epstart(ifp);
604 }
605 
606 
607 /*
608  * Set multicast receive filter.
609  * elink3 hardware has no selective multicast filter in hardware.
610  * Enable reception of all multicasts and filter in software.
611  */
612 void
613 epsetfilter(sc)
614 	register struct ep_softc *sc;
615 {
616 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
617 
618 	GO_WINDOW(1);		/* Window 1 is operating window */
619 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
620 	    FIL_INDIVIDUAL | FIL_BRDCST |
621 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
622 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
623 }
624 
625 
626 int
627 ep_media_change(ifp)
628 	struct ifnet *ifp;
629 {
630 	register struct ep_softc *sc = ifp->if_softc;
631 
632 	return	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
633 }
634 
635 /*
636  * Set active media to a specific given EPMEDIA_<> value.
637  * For vortex/demon/boomerang cards, update media field in w3_internal_config,
638  *       and power on selected transceiver.
639  * For 3c509-generation cards (3c509/3c579/3c589/3c509B),
640  *	update media field in w0_address_config, and power on selected xcvr.
641  */
642 int
643 epsetmedia(sc, medium)
644 	register struct ep_softc *sc;
645 	int medium;
646 {
647 	bus_space_tag_t iot = sc->sc_iot;
648 	bus_space_handle_t ioh = sc->sc_ioh;
649 	int w4_media;
650 
651 	/*
652 	 * First, change the media-control bits in EP_W4_MEDIA_TYPE.
653 	 */
654 
655 	 /* Turn everything off.  First turn off linkbeat and UTP. */
656 	GO_WINDOW(4);
657 	w4_media = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
658 	w4_media =  w4_media & ~(ENABLE_UTP|SQE_ENABLE);
659 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, w4_media);
660 
661 	/* Turn off coax */
662 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
663 	delay(1000);
664 
665 	/*
666 	 * Now turn on the selected media/transceiver.
667 	 */
668 	GO_WINDOW(4);
669 	switch  (medium) {
670 	case EPMEDIA_10BASE_T:
671 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
672 		    w4_media | ENABLE_UTP);
673 		break;
674 
675 	case EPMEDIA_10BASE_2:
676 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
677 		DELAY(1000);	/* 50ms not enmough? */
678 		break;
679 
680 	/* XXX following only for new-generation cards */
681 	case EPMEDIA_100BASE_TX:
682 	case EPMEDIA_100BASE_FX:
683 	case EPMEDIA_100BASE_T4:	/* XXX check documentation */
684 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
685 		    w4_media | LINKBEAT_ENABLE);
686 		DELAY(1000);	/* not strictly necessary? */
687 		break;
688 
689 	case EPMEDIA_AUI:
690 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
691 		    w4_media | SQE_ENABLE);
692 		DELAY(1000);	/*  not strictly necessary? */
693 		break;
694 	case EPMEDIA_MII:
695 		break;
696 	default:
697 #if defined(DEBUG)
698 		printf("%s unknown media 0x%x\n", sc->sc_dev.dv_xname, medium);
699 #endif
700 		break;
701 
702 	}
703 
704 	/*
705 	 * Tell the chip which PHY [sic] to use.
706 	 */
707 	if  (sc->ep_chipset==EP_CHIPSET_VORTEX	||
708 	     sc->ep_chipset==EP_CHIPSET_BOOMERANG2) {
709 		int config0, config1;
710 
711 		GO_WINDOW(3);
712 		config0 = (u_int)bus_space_read_2(iot, ioh,
713 		    EP_W3_INTERNAL_CONFIG);
714 		config1 = (u_int)bus_space_read_2(iot, ioh,
715 		    EP_W3_INTERNAL_CONFIG + 2);
716 
717 #if defined(DEBUG)
718 		printf("%s:  read 0x%x, 0x%x from EP_W3_CONFIG register\n",
719 		       sc->sc_dev.dv_xname, config0, config1);
720 #endif
721 		config1 = config1 & ~CONFIG_MEDIAMASK;
722 		config1 |= (medium << CONFIG_MEDIAMASK_SHIFT);
723 
724 #if defined(DEBUG)
725 		printf("epsetmedia: %s: medium 0x%x, 0x%x to EP_W3_CONFIG\n",
726 		    sc->sc_dev.dv_xname, medium, config1);
727 #endif
728 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG, config0);
729 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2, config1);
730 	}
731 	else if (sc->ep_chipset == EP_CHIPSET_3C509) {
732 		register int w0_addr_cfg;
733 
734 		GO_WINDOW(0);
735 		w0_addr_cfg = bus_space_read_2(iot, ioh, EP_W0_ADDRESS_CFG);
736 		w0_addr_cfg &= 0x3fff;
737 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,
738 		    w0_addr_cfg | (medium << 14));
739 		DELAY(1000);
740 	}
741 
742 	GO_WINDOW(1);		/* Window 1 is operating window */
743 	return (0);
744 }
745 
746 /*
747  * Get currently-selected media from card.
748  * (if_media callback, may be called before interface is brought up).
749  */
750 void
751 ep_media_status(ifp, req)
752 	struct ifnet *ifp;
753 	struct ifmediareq *req;
754 {
755 	register struct ep_softc *sc = ifp->if_softc;
756 	bus_space_tag_t iot = sc->sc_iot;
757 	bus_space_handle_t ioh = sc->sc_ioh;
758 	u_int config1;
759 	u_int ep_mediastatus;
760 
761 	/* XXX read from softc when we start autosensing media */
762 	req->ifm_active = sc->sc_media.ifm_cur->ifm_media;
763 
764 	switch (sc->ep_chipset) {
765 	case EP_CHIPSET_VORTEX:
766 	case EP_CHIPSET_BOOMERANG:
767 		GO_WINDOW(3);
768 		delay(5000);
769 
770 		config1 = bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
771 		GO_WINDOW(1);
772 
773 		config1 =
774 		    (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
775 		req->ifm_active = ep_default_to_media[config1];
776 
777 		/* XXX check full-duplex bits? */
778 
779 		GO_WINDOW(4);
780 		req->ifm_status = IFM_AVALID;	/* XXX */
781 		ep_mediastatus = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
782 		if (ep_mediastatus & LINKBEAT_DETECT)
783 			req->ifm_status |= IFM_ACTIVE; 	/* XXX  automedia */
784 
785 		break;
786 
787 	case EP_CHIPSET_UNKNOWN:
788 	case EP_CHIPSET_3C509:
789 		req->ifm_status = 0;	/* XXX */
790 		break;
791 
792 	default:
793 		printf("%s: media_status on unknown chipset 0x%x\n",
794 		       ifp->if_xname, sc->ep_chipset);
795 		break;
796 	}
797 
798 	/* XXX look for softc heartbeat for other chips or media */
799 
800 	GO_WINDOW(1);
801 	return;
802 }
803 
804 
805 
806 /*
807  * Start outputting on the interface.
808  * Always called as splnet().
809  */
810 void
811 epstart(ifp)
812 	struct ifnet *ifp;
813 {
814 	register struct ep_softc *sc = ifp->if_softc;
815 	bus_space_tag_t iot = sc->sc_iot;
816 	bus_space_handle_t ioh = sc->sc_ioh;
817 	struct mbuf *m, *m0;
818 	int sh, len, pad;
819 
820 	/* Don't transmit if interface is busy or not running */
821 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
822 		return;
823 
824 startagain:
825 	/* Sneak a peek at the next packet */
826 	m0 = ifp->if_snd.ifq_head;
827 	if (m0 == 0)
828 		return;
829 
830 	/* We need to use m->m_pkthdr.len, so require the header */
831 	if ((m0->m_flags & M_PKTHDR) == 0)
832 		panic("epstart: no header mbuf");
833 	len = m0->m_pkthdr.len;
834 
835 	pad = (4 - len) & 3;
836 
837 	/*
838 	 * The 3c509 automatically pads short packets to minimum ethernet
839 	 * length, but we drop packets that are too large. Perhaps we should
840 	 * truncate them instead?
841 	 */
842 	if (len + pad > ETHER_MAX_LEN) {
843 		/* packet is obviously too large: toss it */
844 		++ifp->if_oerrors;
845 		IF_DEQUEUE(&ifp->if_snd, m0);
846 		m_freem(m0);
847 		goto readcheck;
848 	}
849 
850 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
851 		bus_space_write_2(iot, ioh, EP_COMMAND,
852 		    SET_TX_AVAIL_THRESH |
853 		    ((len + pad + 4) >> sc->ep_pktlenshift));
854 		/* not enough room in FIFO */
855 		ifp->if_flags |= IFF_OACTIVE;
856 		return;
857 	} else {
858 		bus_space_write_2(iot, ioh, EP_COMMAND,
859 		    SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
860 	}
861 
862 	IF_DEQUEUE(&ifp->if_snd, m0);
863 	if (m0 == 0)		/* not really needed */
864 		return;
865 
866 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
867 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
868 
869 #if NBPFILTER > 0
870 	if (ifp->if_bpf)
871 		bpf_mtap(ifp->if_bpf, m0);
872 #endif
873 
874 	/*
875 	 * Do the output at splhigh() so that an interrupt from another device
876 	 * won't cause a FIFO underrun.
877 	 */
878 	sh = splhigh();
879 
880 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
881 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
882 	    0xffff);	/* Second dword meaningless */
883 	if (EP_IS_BUS_32(sc->bustype)) {
884 		for (m = m0; m; ) {
885 			if (m->m_len > 3)  {
886 				/* align our reads from core */
887 				if (mtod(m, u_long) & 3)  {
888 					u_long count =
889 					    4 - (mtod(m, u_long) & 3);
890 					bus_space_write_multi_1(iot, ioh,
891 					    EP_W1_TX_PIO_WR_1,
892 					    mtod(m, u_int8_t *), count);
893 					m->m_data =
894 					    (void *)(mtod(m, u_long) + count);
895 					m->m_len -= count;
896 				}
897 				bus_space_write_multi_4(iot, ioh,
898 				    EP_W1_TX_PIO_WR_1,
899 				    mtod(m, u_int32_t *), m->m_len >> 2);
900 				m->m_data = (void *)(mtod(m, u_long) +
901 					(u_long)(m->m_len & ~3));
902 				m->m_len -= m->m_len & ~3;
903 			}
904 			if (m->m_len)  {
905 				bus_space_write_multi_1(iot, ioh,
906 				    EP_W1_TX_PIO_WR_1,
907 				    mtod(m, u_int8_t *), m->m_len);
908 			}
909 			MFREE(m, m0);
910 			m = m0;
911 		}
912 	} else {
913 		for (m = m0; m; ) {
914 			if (m->m_len > 1)  {
915 				if (mtod(m, u_long) & 1)  {
916 					bus_space_write_1(iot, ioh,
917 					    EP_W1_TX_PIO_WR_1,
918 					    *(mtod(m, u_int8_t *)));
919 					m->m_data =
920 					    (void *)(mtod(m, u_long) + 1);
921 					m->m_len -= 1;
922 				}
923 				bus_space_write_multi_2(iot, ioh,
924 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
925 				    m->m_len >> 1);
926 			}
927 			if (m->m_len & 1)  {
928 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
929 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
930 			}
931 			MFREE(m, m0);
932 			m = m0;
933 		}
934 	}
935 	while (pad--)
936 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
937 
938 	splx(sh);
939 
940 	++ifp->if_opackets;
941 
942 readcheck:
943 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
944 		/* We received a complete packet. */
945 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
946 
947 		if ((status & S_INTR_LATCH) == 0) {
948 			/*
949 			 * No interrupt, read the packet and continue
950 			 * Is  this supposed to happen? Is my motherboard
951 			 * completely busted?
952 			 */
953 			epread(sc);
954 		} else {
955 			/* Got an interrupt, return so that it gets serviced. */
956 			return;
957 		}
958 	} else {
959 		/* Check if we are stuck and reset [see XXX comment] */
960 		if (epstatus(sc)) {
961 			if (ifp->if_flags & IFF_DEBUG)
962 				printf("%s: adapter reset\n",
963 				    sc->sc_dev.dv_xname);
964 			epreset(sc);
965 		}
966 	}
967 
968 	goto startagain;
969 }
970 
971 
972 /*
973  * XXX: The 3c509 card can get in a mode where both the fifo status bit
974  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
975  *	We detect this situation and we reset the adapter.
976  *	It happens at times when there is a lot of broadcast traffic
977  *	on the cable (once in a blue moon).
978  */
979 static int
980 epstatus(sc)
981 	register struct ep_softc *sc;
982 {
983 	bus_space_tag_t iot = sc->sc_iot;
984 	bus_space_handle_t ioh = sc->sc_ioh;
985 	u_int16_t fifost;
986 
987 	/*
988 	 * Check the FIFO status and act accordingly
989 	 */
990 	GO_WINDOW(4);
991 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
992 	GO_WINDOW(1);
993 
994 	if (fifost & FIFOS_RX_UNDERRUN) {
995 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
996 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
997 		epreset(sc);
998 		return 0;
999 	}
1000 
1001 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
1002 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1003 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
1004 		return 1;
1005 	}
1006 
1007 	if (fifost & FIFOS_RX_OVERRUN) {
1008 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1009 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
1010 		return 1;
1011 	}
1012 
1013 	if (fifost & FIFOS_TX_OVERRUN) {
1014 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1015 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
1016 		epreset(sc);
1017 		return 0;
1018 	}
1019 
1020 	return 0;
1021 }
1022 
1023 
1024 static void
1025 eptxstat(sc)
1026 	register struct ep_softc *sc;
1027 {
1028 	bus_space_tag_t iot = sc->sc_iot;
1029 	bus_space_handle_t ioh = sc->sc_ioh;
1030 	int i;
1031 
1032 	/*
1033 	 * We need to read+write TX_STATUS until we get a 0 status
1034 	 * in order to turn off the interrupt flag.
1035 	 */
1036 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
1037 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
1038 
1039 		if (i & TXS_JABBER) {
1040 			++sc->sc_ethercom.ec_if.if_oerrors;
1041 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1042 				printf("%s: jabber (%x)\n",
1043 				       sc->sc_dev.dv_xname, i);
1044 			epreset(sc);
1045 		} else if (i & TXS_UNDERRUN) {
1046 			++sc->sc_ethercom.ec_if.if_oerrors;
1047 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1048 				printf("%s: fifo underrun (%x) @%d\n",
1049 				       sc->sc_dev.dv_xname, i,
1050 				       sc->tx_start_thresh);
1051 			if (sc->tx_succ_ok < 100)
1052 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
1053 					    sc->tx_start_thresh + 20);
1054 			sc->tx_succ_ok = 0;
1055 			epreset(sc);
1056 		} else if (i & TXS_MAX_COLLISION) {
1057 			++sc->sc_ethercom.ec_if.if_collisions;
1058 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
1059 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1060 		} else
1061 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
1062 	}
1063 }
1064 
1065 int
1066 epintr(arg)
1067 	void *arg;
1068 {
1069 	register struct ep_softc *sc = arg;
1070 	bus_space_tag_t iot = sc->sc_iot;
1071 	bus_space_handle_t ioh = sc->sc_ioh;
1072 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1073 	u_int16_t status;
1074 	int ret = 0;
1075 
1076 	for (;;) {
1077 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
1078 
1079 		status = bus_space_read_2(iot, ioh, EP_STATUS);
1080 
1081 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
1082 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
1083 			break;
1084 
1085 		ret = 1;
1086 
1087 		/*
1088 		 * Acknowledge any interrupts.  It's important that we do this
1089 		 * first, since there would otherwise be a race condition.
1090 		 * Due to the i386 interrupt queueing, we may get spurious
1091 		 * interrupts occasionally.
1092 		 */
1093 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | status);
1094 
1095 		if (status & S_RX_COMPLETE)
1096 			epread(sc);
1097 		if (status & S_TX_AVAIL) {
1098 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1099 			epstart(&sc->sc_ethercom.ec_if);
1100 		}
1101 		if (status & S_CARD_FAILURE) {
1102 			printf("%s: adapter failure (%x)\n",
1103 			    sc->sc_dev.dv_xname, status);
1104 			epreset(sc);
1105 			return (1);
1106 		}
1107 		if (status & S_TX_COMPLETE) {
1108 			eptxstat(sc);
1109 			epstart(ifp);
1110 		}
1111 	}
1112 
1113 	/* no more interrupts */
1114 	return (ret);
1115 }
1116 
1117 void
1118 epread(sc)
1119 	register struct ep_softc *sc;
1120 {
1121 	bus_space_tag_t iot = sc->sc_iot;
1122 	bus_space_handle_t ioh = sc->sc_ioh;
1123 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1124 	struct mbuf *m;
1125 	struct ether_header *eh;
1126 	int len;
1127 
1128 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
1129 
1130 again:
1131 	if (ifp->if_flags & IFF_DEBUG) {
1132 		int err = len & ERR_MASK;
1133 		char *s = NULL;
1134 
1135 		if (len & ERR_INCOMPLETE)
1136 			s = "incomplete packet";
1137 		else if (err == ERR_OVERRUN)
1138 			s = "packet overrun";
1139 		else if (err == ERR_RUNT)
1140 			s = "runt packet";
1141 		else if (err == ERR_ALIGNMENT)
1142 			s = "bad alignment";
1143 		else if (err == ERR_CRC)
1144 			s = "bad crc";
1145 		else if (err == ERR_OVERSIZE)
1146 			s = "oversized packet";
1147 		else if (err == ERR_DRIBBLE)
1148 			s = "dribble bits";
1149 
1150 		if (s)
1151 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
1152 	}
1153 
1154 	if (len & ERR_INCOMPLETE)
1155 		return;
1156 
1157 	if (len & ERR_RX) {
1158 		++ifp->if_ierrors;
1159 		goto abort;
1160 	}
1161 
1162 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
1163 
1164 	/* Pull packet off interface. */
1165 	m = epget(sc, len);
1166 	if (m == 0) {
1167 		ifp->if_ierrors++;
1168 		goto abort;
1169 	}
1170 
1171 	++ifp->if_ipackets;
1172 
1173 	/* We assume the header fit entirely in one mbuf. */
1174 	eh = mtod(m, struct ether_header *);
1175 
1176 #if NBPFILTER > 0
1177 	/*
1178 	 * Check if there's a BPF listener on this interface.
1179 	 * If so, hand off the raw packet to BPF.
1180 	 */
1181 	if (ifp->if_bpf) {
1182 		bpf_mtap(ifp->if_bpf, m);
1183 
1184 		/*
1185 		 * Note that the interface cannot be in promiscuous mode if
1186 		 * there are no BPF listeners.  And if we are in promiscuous
1187 		 * mode, we have to check if this packet is really ours.
1188 		 */
1189 		if ((ifp->if_flags & IFF_PROMISC) &&
1190 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
1191 		    bcmp(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl),
1192 			    sizeof(eh->ether_dhost)) != 0) {
1193 			m_freem(m);
1194 			return;
1195 		}
1196 	}
1197 #endif
1198 
1199 	/* We assume the header fit entirely in one mbuf. */
1200 	m_adj(m, sizeof(struct ether_header));
1201 	ether_input(ifp, eh, m);
1202 
1203 	/*
1204 	 * In periods of high traffic we can actually receive enough
1205 	 * packets so that the fifo overrun bit will be set at this point,
1206 	 * even though we just read a packet. In this case we
1207 	 * are not going to receive any more interrupts. We check for
1208 	 * this condition and read again until the fifo is not full.
1209 	 * We could simplify this test by not using epstatus(), but
1210 	 * rechecking the RX_STATUS register directly. This test could
1211 	 * result in unnecessary looping in cases where there is a new
1212 	 * packet but the fifo is not full, but it will not fix the
1213 	 * stuck behavior.
1214 	 *
1215 	 * Even with this improvement, we still get packet overrun errors
1216 	 * which are hurting performance. Maybe when I get some more time
1217 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
1218 	 */
1219 	if (epstatus(sc)) {
1220 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
1221 		/* Check if we are stuck and reset [see XXX comment] */
1222 		if (len & ERR_INCOMPLETE) {
1223 			if (ifp->if_flags & IFF_DEBUG)
1224 				printf("%s: adapter reset\n",
1225 				    sc->sc_dev.dv_xname);
1226 			epreset(sc);
1227 			return;
1228 		}
1229 		goto again;
1230 	}
1231 
1232 	return;
1233 
1234 abort:
1235 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1236 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1237 		;
1238 }
1239 
1240 struct mbuf *
1241 epget(sc, totlen)
1242 	struct ep_softc *sc;
1243 	int totlen;
1244 {
1245 	bus_space_tag_t iot = sc->sc_iot;
1246 	bus_space_handle_t ioh = sc->sc_ioh;
1247 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1248 	struct mbuf *top, **mp, *m;
1249 	int len, remaining;
1250 	int sh;
1251 
1252 	m = sc->mb[sc->next_mb];
1253 	sc->mb[sc->next_mb] = 0;
1254 	if (m == 0) {
1255 		MGETHDR(m, M_DONTWAIT, MT_DATA);
1256 		if (m == 0)
1257 			return 0;
1258 	} else {
1259 		/* If the queue is no longer full, refill. */
1260 		if (sc->last_mb == sc->next_mb)
1261 			timeout(epmbuffill, sc, 1);
1262 		/* Convert one of our saved mbuf's. */
1263 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1264 		m->m_data = m->m_pktdat;
1265 		m->m_flags = M_PKTHDR;
1266 	}
1267 	m->m_pkthdr.rcvif = ifp;
1268 	m->m_pkthdr.len = totlen;
1269 	len = MHLEN;
1270 	top = 0;
1271 	mp = &top;
1272 
1273 	/*
1274 	 * We read the packet at splhigh() so that an interrupt from another
1275 	 * device doesn't cause the card's buffer to overflow while we're
1276 	 * reading it.  We may still lose packets at other times.
1277 	 */
1278 	sh = splhigh();
1279 
1280 	while (totlen > 0) {
1281 		if (top) {
1282 			m = sc->mb[sc->next_mb];
1283 			sc->mb[sc->next_mb] = 0;
1284 			if (m == 0) {
1285 				MGET(m, M_DONTWAIT, MT_DATA);
1286 				if (m == 0) {
1287 					splx(sh);
1288 					m_freem(top);
1289 					return 0;
1290 				}
1291 			} else {
1292 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1293 			}
1294 			len = MLEN;
1295 		}
1296 		if (totlen >= MINCLSIZE) {
1297 			MCLGET(m, M_DONTWAIT);
1298 			if ((m->m_flags & M_EXT) == 0) {
1299 				splx(sh);
1300 				m_free(m);
1301 				m_freem(top);
1302 				return 0;
1303 			}
1304 			len = MCLBYTES;
1305 		}
1306 		if (top == 0)  {
1307 			/* align the struct ip header */
1308 			caddr_t newdata = (caddr_t)
1309 			    ALIGN(m->m_data + sizeof(struct ether_header))
1310 			    - sizeof(struct ether_header);
1311 			len -= newdata - m->m_data;
1312 			m->m_data = newdata;
1313 		}
1314 		remaining = len = min(totlen, len);
1315 		if (EP_IS_BUS_32(sc->bustype)) {
1316 			u_long offset = mtod(m, u_long);
1317 			/*
1318 			 * Read bytes up to the point where we are aligned.
1319 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
1320 			 * here because we're later reading 4-byte chunks.)
1321 			 */
1322 			if ((remaining > 3) && (offset & 3))  {
1323 				int count = (4 - (offset & 3));
1324 				bus_space_read_multi_1(iot, ioh,
1325 				    EP_W1_RX_PIO_RD_1,
1326 				    (u_int8_t *) offset, count);
1327 				offset += count;
1328 				remaining -= count;
1329 			}
1330 			if (remaining > 3) {
1331 				bus_space_read_multi_4(iot, ioh,
1332 				    EP_W1_RX_PIO_RD_1,
1333 				    (u_int32_t *) offset, remaining >> 2);
1334 				offset += remaining & ~3;
1335 				remaining &= 3;
1336 			}
1337 			if (remaining)  {
1338 				bus_space_read_multi_1(iot, ioh,
1339 				    EP_W1_RX_PIO_RD_1,
1340 				    (u_int8_t *) offset, remaining);
1341 			}
1342 		} else {
1343 			u_long offset = mtod(m, u_long);
1344 			if ((remaining > 1) && (offset & 1))  {
1345 				bus_space_read_multi_1(iot, ioh,
1346 				    EP_W1_RX_PIO_RD_1,
1347 				    (u_int8_t *) offset, 1);
1348 				remaining -= 1;
1349 				offset += 1;
1350 			}
1351 			if (remaining > 1) {
1352 				bus_space_read_multi_2(iot, ioh,
1353 				    EP_W1_RX_PIO_RD_1,
1354 				    (u_int16_t *) offset, remaining >> 1);
1355 				offset += remaining & ~1;
1356 			}
1357 			if (remaining & 1)  {
1358 				bus_space_read_multi_1(iot, ioh,
1359 				    EP_W1_RX_PIO_RD_1,
1360 				    (u_int8_t *) offset, remaining & 1);
1361 			}
1362 		}
1363 		m->m_len = len;
1364 		totlen -= len;
1365 		*mp = m;
1366 		mp = &m->m_next;
1367 	}
1368 
1369 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1370 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1371 		;
1372 
1373 	splx(sh);
1374 
1375 	return top;
1376 }
1377 
1378 int
1379 epioctl(ifp, cmd, data)
1380 	register struct ifnet *ifp;
1381 	u_long cmd;
1382 	caddr_t data;
1383 {
1384 	struct ep_softc *sc = ifp->if_softc;
1385 	struct ifaddr *ifa = (struct ifaddr *)data;
1386 	struct ifreq *ifr = (struct ifreq *)data;
1387 	int s, error = 0;
1388 
1389 	s = splnet();
1390 
1391 	switch (cmd) {
1392 
1393 	case SIOCSIFADDR:
1394 		ifp->if_flags |= IFF_UP;
1395 
1396 		switch (ifa->ifa_addr->sa_family) {
1397 #ifdef INET
1398 		case AF_INET:
1399 			epinit(sc);
1400 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
1401 			break;
1402 #endif
1403 #ifdef NS
1404 		case AF_NS:
1405 		    {
1406 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1407 
1408 			if (ns_nullhost(*ina))
1409 				ina->x_host = *(union ns_host *)
1410 				    LLADDR(ifp->if_sadl);
1411 			else
1412 				bcopy(ina->x_host.c_host,
1413 				    LLADDR(ifp->if_sadl),
1414 				    ifp->if_addrlen);
1415 			/* Set new address. */
1416 			epinit(sc);
1417 			break;
1418 		    }
1419 #endif
1420 		default:
1421 			epinit(sc);
1422 			break;
1423 		}
1424 		break;
1425 
1426 	case SIOCSIFMEDIA:
1427 	case SIOCGIFMEDIA:
1428 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1429 		break;
1430 
1431 	case SIOCSIFFLAGS:
1432 		if ((ifp->if_flags & IFF_UP) == 0 &&
1433 		    (ifp->if_flags & IFF_RUNNING) != 0) {
1434 			/*
1435 			 * If interface is marked down and it is running, then
1436 			 * stop it.
1437 			 */
1438 			epstop(sc);
1439 			ifp->if_flags &= ~IFF_RUNNING;
1440 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
1441 			   (ifp->if_flags & IFF_RUNNING) == 0) {
1442 			/*
1443 			 * If interface is marked up and it is stopped, then
1444 			 * start it.
1445 			 */
1446 			epinit(sc);
1447 		} else {
1448 			/*
1449 			 * deal with flags changes:
1450 			 * IFF_MULTICAST, IFF_PROMISC.
1451 			 */
1452 			epsetfilter(sc);
1453 		}
1454 		break;
1455 
1456 	case SIOCADDMULTI:
1457 	case SIOCDELMULTI:
1458 		error = (cmd == SIOCADDMULTI) ?
1459 		    ether_addmulti(ifr, &sc->sc_ethercom) :
1460 		    ether_delmulti(ifr, &sc->sc_ethercom);
1461 
1462 		if (error == ENETRESET) {
1463 			/*
1464 			 * Multicast list has changed; set the hardware filter
1465 			 * accordingly.
1466 			 */
1467 			epreset(sc);
1468 			error = 0;
1469 		}
1470 		break;
1471 
1472 	default:
1473 		error = EINVAL;
1474 		break;
1475 	}
1476 
1477 	splx(s);
1478 	return (error);
1479 }
1480 
1481 void
1482 epreset(sc)
1483 	struct ep_softc *sc;
1484 {
1485 	int s;
1486 
1487 	s = splnet();
1488 	epstop(sc);
1489 	epinit(sc);
1490 	splx(s);
1491 }
1492 
1493 void
1494 epwatchdog(ifp)
1495 	struct ifnet *ifp;
1496 {
1497 	struct ep_softc *sc = ifp->if_softc;
1498 
1499 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
1500 	++sc->sc_ethercom.ec_if.if_oerrors;
1501 
1502 	epreset(sc);
1503 }
1504 
1505 void
1506 epstop(sc)
1507 	register struct ep_softc *sc;
1508 {
1509 	bus_space_tag_t iot = sc->sc_iot;
1510 	bus_space_handle_t ioh = sc->sc_ioh;
1511 
1512 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
1513 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
1514 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
1515 		;
1516 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
1517 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
1518 
1519 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
1520 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
1521 
1522 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
1523 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
1524 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
1525 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
1526 
1527 	epmbufempty(sc);
1528 }
1529 
1530 
1531 /*
1532  * Before reboots, reset card completely.
1533  */
1534 static void
1535 epshutdown(arg)
1536 	void *arg;
1537 {
1538 	register struct ep_softc *sc = arg;
1539 
1540 	epstop(sc);
1541 	ep_complete_cmd(sc, EP_COMMAND, GLOBAL_RESET);
1542 }
1543 
1544 /*
1545  * We get eeprom data from the id_port given an offset into the
1546  * eeprom.  Basically; after the ID_sequence is sent to all of
1547  * the cards; they enter the ID_CMD state where they will accept
1548  * command requests. 0x80-0xbf loads the eeprom data.  We then
1549  * read the port 16 times and with every read; the cards check
1550  * for contention (ie: if one card writes a 0 bit and another
1551  * writes a 1 bit then the host sees a 0. At the end of the cycle;
1552  * each card compares the data on the bus; if there is a difference
1553  * then that card goes into ID_WAIT state again). In the meantime;
1554  * one bit of data is returned in the AX register which is conveniently
1555  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
1556  * bit of data with each read.
1557  *
1558  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
1559  */
1560 u_int16_t
1561 epreadeeprom(iot, ioh, offset)
1562 	bus_space_tag_t iot;
1563 	bus_space_handle_t ioh;
1564 	int offset;
1565 {
1566 	u_int16_t data = 0;
1567 	int i;
1568 
1569 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
1570 	delay(1000);
1571 	for (i = 0; i < 16; i++)
1572 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
1573 	return (data);
1574 }
1575 
1576 static int
1577 epbusyeeprom(sc)
1578 	struct ep_softc *sc;
1579 {
1580 	bus_space_tag_t iot = sc->sc_iot;
1581 	bus_space_handle_t ioh = sc->sc_ioh;
1582 	int i = 100, j;
1583 
1584 	if (sc->bustype == EP_BUS_PCMCIA) {
1585 		delay(1000);
1586 		return 0;
1587 	}
1588 
1589 	while (i--) {
1590 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
1591 		if (j & EEPROM_BUSY)
1592 			delay(100);
1593 		else
1594 			break;
1595 	}
1596 	if (!i) {
1597 		printf("\n%s: eeprom failed to come ready\n",
1598 		    sc->sc_dev.dv_xname);
1599 		return (1);
1600 	}
1601 	if (j & EEPROM_TST_MODE) {
1602 		/* XXX PnP mode? */
1603 		printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
1604 		return (1);
1605 	}
1606 	return (0);
1607 }
1608 
1609 void
1610 epmbuffill(v)
1611 	void *v;
1612 {
1613 	struct ep_softc *sc = v;
1614 	int s, i;
1615 
1616 	s = splnet();
1617 	i = sc->last_mb;
1618 	do {
1619 		if (sc->mb[i] == NULL)
1620 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
1621 		if (sc->mb[i] == NULL)
1622 			break;
1623 		i = (i + 1) % MAX_MBS;
1624 	} while (i != sc->next_mb);
1625 	sc->last_mb = i;
1626 	/* If the queue was not filled, try again. */
1627 	if (sc->last_mb != sc->next_mb)
1628 		timeout(epmbuffill, sc, 1);
1629 	splx(s);
1630 }
1631 
1632 void
1633 epmbufempty(sc)
1634 	struct ep_softc *sc;
1635 {
1636 	int s, i;
1637 
1638 	s = splnet();
1639 	for (i = 0; i<MAX_MBS; i++) {
1640 		if (sc->mb[i]) {
1641 			m_freem(sc->mb[i]);
1642 			sc->mb[i] = NULL;
1643 		}
1644 	}
1645 	sc->last_mb = sc->next_mb = 0;
1646 	untimeout(epmbuffill, sc);
1647 	splx(s);
1648 }
1649