xref: /netbsd-src/sys/dev/ic/elink3.c (revision 7f21db1c0118155e0dd40b75182e30c589d9f63e)
1 /*	$NetBSD: elink3.c,v 1.130 2010/01/19 22:06:24 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1996, 1997 Jonathan Stone <jonathan@NetBSD.org>
35  * Copyright (c) 1994 Herb Peyerl <hpeyerl@beer.org>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by Herb Peyerl.
49  * 4. The name of Herb Peyerl may not be used to endorse or promote products
50  *    derived from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
55  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
56  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
57  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
61  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: elink3.c,v 1.130 2010/01/19 22:06:24 pooka Exp $");
66 
67 #include "opt_inet.h"
68 #include "rnd.h"
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/callout.h>
73 #include <sys/kernel.h>
74 #include <sys/mbuf.h>
75 #include <sys/socket.h>
76 #include <sys/ioctl.h>
77 #include <sys/errno.h>
78 #include <sys/syslog.h>
79 #include <sys/select.h>
80 #include <sys/device.h>
81 #if NRND > 0
82 #include <sys/rnd.h>
83 #endif
84 
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_ether.h>
88 #include <net/if_media.h>
89 
90 #include <net/bpf.h>
91 #include <net/bpfdesc.h>
92 
93 #include <sys/cpu.h>
94 #include <sys/bus.h>
95 #include <sys/intr.h>
96 
97 #include <dev/mii/mii.h>
98 #include <dev/mii/miivar.h>
99 #include <dev/mii/mii_bitbang.h>
100 
101 #include <dev/ic/elink3var.h>
102 #include <dev/ic/elink3reg.h>
103 
104 #ifdef DEBUG
105 int epdebug = 0;
106 #endif
107 
108 /*
109  * XXX endian workaround for big-endian CPUs  with pcmcia:
110  * if stream methods for bus_space_multi are not provided, define them
111  * using non-stream bus_space_{read,write}_multi_.
112  * Assumes host CPU is same endian-ness as bus.
113  */
114 #ifndef __BUS_SPACE_HAS_STREAM_METHODS
115 #define bus_space_read_multi_stream_2	bus_space_read_multi_2
116 #define bus_space_read_multi_stream_4	bus_space_read_multi_4
117 #define bus_space_write_multi_stream_2	bus_space_write_multi_2
118 #define bus_space_write_multi_stream_4	bus_space_write_multi_4
119 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
120 
121 /*
122  * Structure to map media-present bits in boards to ifmedia codes and
123  * printable media names. Used for table-driven ifmedia initialization.
124  */
125 struct ep_media {
126 	int	epm_mpbit;		/* media present bit */
127 	const char *epm_name;		/* name of medium */
128 	int	epm_ifmedia;		/* ifmedia word for medium */
129 	int	epm_epmedia;		/* ELINKMEDIA_* constant */
130 };
131 
132 /*
133  * Media table for the Demon/Vortex/Boomerang chipsets.
134  *
135  * Note that MII on the Demon and Vortex (3c59x) indicates an external
136  * MII connector (for connecting an external PHY) ... I think.  Treat
137  * it as `manual' on these chips.
138  *
139  * Any Boomerang (3c90x) chips with MII really do have an internal
140  * MII and real PHYs attached; no `native' media.
141  */
142 const struct ep_media ep_vortex_media[] = {
143 	{ ELINK_PCI_10BASE_T,	"10baseT",	IFM_ETHER|IFM_10_T,
144 	  ELINKMEDIA_10BASE_T },
145 	{ ELINK_PCI_10BASE_T,	"10baseT-FDX",	IFM_ETHER|IFM_10_T|IFM_FDX,
146 	  ELINKMEDIA_10BASE_T },
147 	{ ELINK_PCI_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
148 	  ELINKMEDIA_AUI },
149 	{ ELINK_PCI_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
150 	  ELINKMEDIA_10BASE_2 },
151 	{ ELINK_PCI_100BASE_TX,	"100baseTX",	IFM_ETHER|IFM_100_TX,
152 	  ELINKMEDIA_100BASE_TX },
153 	{ ELINK_PCI_100BASE_TX,	"100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
154 	  ELINKMEDIA_100BASE_TX },
155 	{ ELINK_PCI_100BASE_FX,	"100baseFX",	IFM_ETHER|IFM_100_FX,
156 	  ELINKMEDIA_100BASE_FX },
157 	{ ELINK_PCI_100BASE_MII,"manual",	IFM_ETHER|IFM_MANUAL,
158 	  ELINKMEDIA_MII },
159 	{ ELINK_PCI_100BASE_T4,	"100baseT4",	IFM_ETHER|IFM_100_T4,
160 	  ELINKMEDIA_100BASE_T4 },
161 	{ 0,			NULL,		0,
162 	  0 },
163 };
164 
165 /*
166  * Media table for the older 3Com Etherlink III chipset, used
167  * in the 3c509, 3c579, and 3c589.
168  */
169 const struct ep_media ep_509_media[] = {
170 	{ ELINK_W0_CC_UTP,	"10baseT",	IFM_ETHER|IFM_10_T,
171 	  ELINKMEDIA_10BASE_T },
172 	{ ELINK_W0_CC_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
173 	  ELINKMEDIA_AUI },
174 	{ ELINK_W0_CC_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
175 	  ELINKMEDIA_10BASE_2 },
176 	{ 0,			NULL,		0,
177 	  0 },
178 };
179 
180 void	ep_internalconfig(struct ep_softc *sc);
181 void	ep_vortex_probemedia(struct ep_softc *sc);
182 void	ep_509_probemedia(struct ep_softc *sc);
183 
184 static void eptxstat(struct ep_softc *);
185 static int epstatus(struct ep_softc *);
186 int	epinit(struct ifnet *);
187 void	epstop(struct ifnet *, int);
188 int	epioctl(struct ifnet *, u_long, void *);
189 void	epstart(struct ifnet *);
190 void	epwatchdog(struct ifnet *);
191 void	epreset(struct ep_softc *);
192 static bool epshutdown(device_t, int);
193 void	epread(struct ep_softc *);
194 struct mbuf *epget(struct ep_softc *, int);
195 void	epmbuffill(void *);
196 void	epmbufempty(struct ep_softc *);
197 void	epsetfilter(struct ep_softc *);
198 void	ep_roadrunner_mii_enable(struct ep_softc *);
199 void	epsetmedia(struct ep_softc *);
200 
201 /* ifmedia callbacks */
202 int	ep_media_change(struct ifnet *ifp);
203 void	ep_media_status(struct ifnet *ifp, struct ifmediareq *req);
204 
205 /* MII callbacks */
206 int	ep_mii_readreg(device_t, int, int);
207 void	ep_mii_writereg(device_t, int, int, int);
208 void	ep_statchg(device_t);
209 
210 void	ep_tick(void *);
211 
212 static int epbusyeeprom(struct ep_softc *);
213 u_int16_t ep_read_eeprom(struct ep_softc *, u_int16_t);
214 static inline void ep_reset_cmd(struct ep_softc *sc, u_int cmd, u_int arg);
215 static inline void ep_finish_reset(bus_space_tag_t, bus_space_handle_t);
216 static inline void ep_discard_rxtop(bus_space_tag_t, bus_space_handle_t);
217 static inline int ep_w1_reg(struct ep_softc *, int);
218 
219 /*
220  * MII bit-bang glue.
221  */
222 u_int32_t ep_mii_bitbang_read(device_t);
223 void ep_mii_bitbang_write(device_t, u_int32_t);
224 
225 const struct mii_bitbang_ops ep_mii_bitbang_ops = {
226 	ep_mii_bitbang_read,
227 	ep_mii_bitbang_write,
228 	{
229 		PHYSMGMT_DATA,		/* MII_BIT_MDO */
230 		PHYSMGMT_DATA,		/* MII_BIT_MDI */
231 		PHYSMGMT_CLK,		/* MII_BIT_MDC */
232 		PHYSMGMT_DIR,		/* MII_BIT_DIR_HOST_PHY */
233 		0,			/* MII_BIT_DIR_PHY_HOST */
234 	}
235 };
236 
237 /*
238  * Some chips (3c515 [Corkscrew] and 3c574 [RoadRunner]) have
239  * Window 1 registers offset!
240  */
241 static inline int
242 ep_w1_reg(struct ep_softc *sc, int reg)
243 {
244 
245 	switch (sc->ep_chipset) {
246 	case ELINK_CHIPSET_CORKSCREW:
247 		return (reg + 0x10);
248 
249 	case ELINK_CHIPSET_ROADRUNNER:
250 		switch (reg) {
251 		case ELINK_W1_FREE_TX:
252 		case ELINK_W1_RUNNER_RDCTL:
253 		case ELINK_W1_RUNNER_WRCTL:
254 			return (reg);
255 		}
256 		return (reg + 0x10);
257 	}
258 
259 	return (reg);
260 }
261 
262 /*
263  * Wait for any pending reset to complete.
264  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
265  * but older hardware doesn't implement it and we must delay.
266  */
267 static inline void
268 ep_finish_reset(bus_space_tag_t iot, bus_space_handle_t ioh)
269 {
270 	int i;
271 
272 	for (i = 0; i < 10000; i++) {
273 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
274 		    COMMAND_IN_PROGRESS) == 0)
275 			break;
276 		DELAY(10);
277 	}
278 }
279 
280 /*
281  * Issue a (reset) command, and be sure it has completed.
282  * Used for global reset, TX_RESET, RX_RESET.
283  */
284 static inline void
285 ep_reset_cmd(struct ep_softc *sc, u_int cmd, u_int arg)
286 {
287 	bus_space_tag_t iot = sc->sc_iot;
288 	bus_space_handle_t ioh = sc->sc_ioh;
289 
290 	bus_space_write_2(iot, ioh, cmd, arg);
291 	ep_finish_reset(iot, ioh);
292 }
293 
294 
295 static inline void
296 ep_discard_rxtop(bus_space_tag_t iot, bus_space_handle_t ioh)
297 {
298 	int i;
299 
300 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISCARD_TOP_PACK);
301 
302         /*
303 	 * Spin for about 1 msec, to avoid forcing a DELAY() between
304 	 * every received packet (adding latency and  limiting pkt-recv rate).
305 	 * On PCI, at 4 30-nsec PCI bus cycles for a read, 8000 iterations
306 	 * is about right.
307 	 */
308 	for (i = 0; i < 8000; i++) {
309 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
310 		    COMMAND_IN_PROGRESS) == 0)
311 		    return;
312 	}
313 
314 	/*  Didn't complete in a hurry. Do DELAY()s. */
315 	ep_finish_reset(iot, ioh);
316 }
317 
318 /*
319  * Back-end attach and configure.
320  */
321 int
322 epconfig(struct ep_softc *sc, u_short chipset, u_int8_t *enaddr)
323 {
324 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
325 	bus_space_tag_t iot = sc->sc_iot;
326 	bus_space_handle_t ioh = sc->sc_ioh;
327 	u_int16_t i;
328 	u_int8_t myla[ETHER_ADDR_LEN];
329 
330 	callout_init(&sc->sc_mii_callout, 0);
331 	callout_init(&sc->sc_mbuf_callout, 0);
332 
333 	sc->ep_chipset = chipset;
334 
335 	/*
336 	 * We could have been groveling around in other register
337 	 * windows in the front-end; make sure we're in window 0
338 	 * to read the EEPROM.
339 	 */
340 	GO_WINDOW(0);
341 
342 	if (enaddr == NULL) {
343 		/*
344 		 * Read the station address from the eeprom.
345 		 */
346 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
347 			u_int16_t x = ep_read_eeprom(sc, i);
348 			myla[(i << 1)] = x >> 8;
349 			myla[(i << 1) + 1] = x;
350 		}
351 		enaddr = myla;
352 	}
353 
354 	/*
355 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900) cards
356 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
357 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
358 	 * packet length.
359 	 * Commands to these cards implicitly upshift a packet size
360 	 * or threshold by 2 bits.
361 	 * To detect  cards with large-packet support, we probe by setting
362 	 * the transmit threshold register, then change windows and
363 	 * read back the threshold register directly, and see if the
364 	 * threshold value was shifted or not.
365 	 */
366 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
367 	    SET_TX_AVAIL_THRESH | ELINK_LARGEWIN_PROBE);
368 	GO_WINDOW(5);
369 	i = bus_space_read_2(iot, ioh, ELINK_W5_TX_AVAIL_THRESH);
370 	GO_WINDOW(1);
371 	switch (i)  {
372 	case ELINK_LARGEWIN_PROBE:
373 	case (ELINK_LARGEWIN_PROBE & ELINK_LARGEWIN_MASK):
374 		sc->ep_pktlenshift = 0;
375 		break;
376 
377 	case (ELINK_LARGEWIN_PROBE << 2):
378 		sc->ep_pktlenshift = 2;
379 		break;
380 
381 	default:
382 		aprint_error_dev(sc->sc_dev,
383 		    "wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
384 		    "Interface disabled\n",
385 		    ELINK_LARGEWIN_PROBE, (int) i);
386 		return (1);
387 	}
388 
389 	/*
390 	 * Ensure Tx-available interrupts are enabled for
391 	 * start the interface.
392 	 * XXX should be in epinit()?
393 	 */
394 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
395 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
396 
397 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
398 	ifp->if_softc = sc;
399 	ifp->if_start = epstart;
400 	ifp->if_ioctl = epioctl;
401 	ifp->if_watchdog = epwatchdog;
402 	ifp->if_init = epinit;
403 	ifp->if_stop = epstop;
404 	ifp->if_flags =
405 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
406 	IFQ_SET_READY(&ifp->if_snd);
407 
408 	if_attach(ifp);
409 	ether_ifattach(ifp, enaddr);
410 
411 	/*
412 	 * Finish configuration:
413 	 * determine chipset if the front-end couldn't do so,
414 	 * show board details, set media.
415 	 */
416 
417 	/*
418 	 * Print RAM size.  We also print the Ethernet address in here.
419 	 * It's extracted from the ifp, so we have to make sure it's
420 	 * been attached first.
421 	 */
422 	ep_internalconfig(sc);
423 	GO_WINDOW(0);
424 
425 	/*
426 	 * Display some additional information, if pertinent.
427 	 */
428 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
429 		aprint_normal_dev(sc->sc_dev, "RoadRunner FIFO buffer enabled\n");
430 
431 	/*
432 	 * Initialize our media structures and MII info.  We'll
433 	 * probe the MII if we discover that we have one.
434 	 */
435 	sc->sc_mii.mii_ifp = ifp;
436 	sc->sc_mii.mii_readreg = ep_mii_readreg;
437 	sc->sc_mii.mii_writereg = ep_mii_writereg;
438 	sc->sc_mii.mii_statchg = ep_statchg;
439 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ep_media_change,
440 	    ep_media_status);
441 
442 	/*
443 	 * All CORKSCREW chips have MII.
444 	 */
445 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW)
446 		sc->ep_flags |= ELINK_FLAGS_MII;
447 
448 	/*
449 	 * Now, determine which media we have.
450 	 */
451 	switch (sc->ep_chipset) {
452 	case ELINK_CHIPSET_ROADRUNNER:
453 		if (sc->ep_flags & ELINK_FLAGS_MII) {
454 			ep_roadrunner_mii_enable(sc);
455 			GO_WINDOW(0);
456 		}
457 		/* FALLTHROUGH */
458 
459 	case ELINK_CHIPSET_CORKSCREW:
460 	case ELINK_CHIPSET_BOOMERANG:
461 		/*
462 		 * If the device has MII, probe it.  We won't be using
463 		 * any `native' media in this case, only PHYs.  If
464 		 * we don't, just treat the Boomerang like the Vortex.
465 		 */
466 		if (sc->ep_flags & ELINK_FLAGS_MII) {
467 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
468 			    MII_PHY_ANY, MII_OFFSET_ANY, 0);
469 			if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
470 				ifmedia_add(&sc->sc_mii.mii_media,
471 				    IFM_ETHER|IFM_NONE, 0, NULL);
472 				ifmedia_set(&sc->sc_mii.mii_media,
473 				    IFM_ETHER|IFM_NONE);
474 			} else {
475 				ifmedia_set(&sc->sc_mii.mii_media,
476 				    IFM_ETHER|IFM_AUTO);
477 			}
478 			break;
479 		}
480 		/* FALLTHROUGH */
481 
482 	case ELINK_CHIPSET_VORTEX:
483 		ep_vortex_probemedia(sc);
484 		break;
485 
486 	default:
487 		ep_509_probemedia(sc);
488 		break;
489 	}
490 
491 	GO_WINDOW(1);		/* Window 1 is operating window */
492 
493 #if NRND > 0
494 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
495 	    RND_TYPE_NET, 0);
496 #endif
497 
498 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
499 
500 	/*  Establish callback to reset card when we reboot. */
501 	if (pmf_device_register1(sc->sc_dev, NULL, NULL, epshutdown))
502 		pmf_class_network_register(sc->sc_dev, ifp);
503 	else
504 		aprint_error_dev(sc->sc_dev,
505 		    "couldn't establish power handler\n");
506 
507 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
508 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
509 
510 	/* The attach is successful. */
511 	sc->sc_flags |= ELINK_FLAGS_ATTACHED;
512 	return (0);
513 }
514 
515 
516 /*
517  * Show interface-model-independent info from window 3
518  * internal-configuration register.
519  */
520 void
521 ep_internalconfig(struct ep_softc *sc)
522 {
523 	bus_space_tag_t iot = sc->sc_iot;
524 	bus_space_handle_t ioh = sc->sc_ioh;
525 
526 	u_int config0;
527 	u_int config1;
528 
529 	int  ram_size, ram_width, ram_split;
530 	/*
531 	 * NVRAM buffer Rx:Tx config names for busmastering cards
532 	 * (Demon, Vortex, and later).
533 	 */
534 	const char *const onboard_ram_config[] = {
535 		"5:3", "3:1", "1:1", "3:5" };
536 
537 	GO_WINDOW(3);
538 	config0 = (u_int)bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
539 	config1 = (u_int)bus_space_read_2(iot, ioh,
540 	    ELINK_W3_INTERNAL_CONFIG + 2);
541 	GO_WINDOW(0);
542 
543 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
544 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
545 
546 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
547 
548 	aprint_normal_dev(sc->sc_dev, "address %s, %dKB %s-wide FIFO, %s Rx:Tx split\n",
549 	       ether_sprintf(CLLADDR(sc->sc_ethercom.ec_if.if_sadl)),
550 	       8 << ram_size,
551 	       (ram_width) ? "word" : "byte",
552 	       onboard_ram_config[ram_split]);
553 }
554 
555 
556 /*
557  * Find supported media on 3c509-generation hardware that doesn't have
558  * a "reset_options" register in window 3.
559  * Use the config_cntrl register  in window 0 instead.
560  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
561  * that implement  CONFIG_CTRL.  We don't have a good way to set the
562  * default active medium; punt to ifconfig  instead.
563  */
564 void
565 ep_509_probemedia(struct ep_softc *sc)
566 {
567 	bus_space_tag_t iot = sc->sc_iot;
568 	bus_space_handle_t ioh = sc->sc_ioh;
569 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
570 	u_int16_t ep_w0_config, port;
571 	const struct ep_media *epm;
572 	const char *sep = "", *defmedianame = NULL;
573 	int defmedia = 0;
574 
575 	GO_WINDOW(0);
576 	ep_w0_config = bus_space_read_2(iot, ioh, ELINK_W0_CONFIG_CTRL);
577 
578 	aprint_normal_dev(sc->sc_dev, "");
579 
580 	/* Sanity check that there are any media! */
581 	if ((ep_w0_config & ELINK_W0_CC_MEDIAMASK) == 0) {
582 		aprint_error("no media present!\n");
583 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
584 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
585 		return;
586 	}
587 
588 	/*
589 	 * Get the default media from the EEPROM.
590 	 */
591 	port = ep_read_eeprom(sc, EEPROM_ADDR_CFG) >> 14;
592 
593 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
594 
595 	for (epm = ep_509_media; epm->epm_name != NULL; epm++) {
596 		if (ep_w0_config & epm->epm_mpbit) {
597 			/*
598 			 * This simple test works because 509 chipsets
599 			 * don't do full-duplex.
600 			 */
601 			if (epm->epm_epmedia == port || defmedia == 0) {
602 				defmedia = epm->epm_ifmedia;
603 				defmedianame = epm->epm_name;
604 			}
605 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
606 			    NULL);
607 			PRINT(epm->epm_name);
608 		}
609 	}
610 
611 #undef PRINT
612 
613 #ifdef DIAGNOSTIC
614 	if (defmedia == 0)
615 		panic("ep_509_probemedia: impossible");
616 #endif
617 
618 	aprint_normal(" (default %s)\n", defmedianame);
619 	ifmedia_set(ifm, defmedia);
620 }
621 
622 /*
623  * Find media present on large-packet-capable elink3 devices.
624  * Show onboard configuration of large-packet-capable elink3 devices
625  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
626  * Use media and card-version info in window 3 instead.
627  */
628 void
629 ep_vortex_probemedia(struct ep_softc *sc)
630 {
631 	bus_space_tag_t iot = sc->sc_iot;
632 	bus_space_handle_t ioh = sc->sc_ioh;
633 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
634 	const struct ep_media *epm;
635 	u_int config1;
636 	int reset_options;
637 	int default_media;	/* 3-bit encoding of default (EEPROM) media */
638 	int defmedia = 0;
639 	const char *sep = "", *defmedianame = NULL;
640 
641 	GO_WINDOW(3);
642 	config1 = (u_int)bus_space_read_2(iot, ioh,
643 	    ELINK_W3_INTERNAL_CONFIG + 2);
644 	reset_options = (int)bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
645 	GO_WINDOW(0);
646 
647 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
648 
649 	aprint_normal_dev(sc->sc_dev, "");
650 
651 	/* Sanity check that there are any media! */
652 	if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
653 		aprint_error("no media present!\n");
654 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
655 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
656 		return;
657 	}
658 
659 #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
660 
661 	for (epm = ep_vortex_media; epm->epm_name != NULL; epm++) {
662 		if (reset_options & epm->epm_mpbit) {
663 			/*
664 			 * Default media is a little more complicated
665 			 * on the Vortex.  We support full-duplex which
666 			 * uses the same reset options bit.
667 			 *
668 			 * XXX Check EEPROM for default to FDX?
669 			 */
670 			if (epm->epm_epmedia == default_media) {
671 				if ((epm->epm_ifmedia & IFM_FDX) == 0) {
672 					defmedia = epm->epm_ifmedia;
673 					defmedianame = epm->epm_name;
674 				}
675 			} else if (defmedia == 0) {
676 				defmedia = epm->epm_ifmedia;
677 				defmedianame = epm->epm_name;
678 			}
679 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
680 			    NULL);
681 			PRINT(epm->epm_name);
682 		}
683 	}
684 
685 #undef PRINT
686 
687 #ifdef DIAGNOSTIC
688 	if (defmedia == 0)
689 		panic("ep_vortex_probemedia: impossible");
690 #endif
691 
692 	aprint_normal(" (default %s)\n", defmedianame);
693 	ifmedia_set(ifm, defmedia);
694 }
695 
696 /*
697  * One second timer, used to tick the MII.
698  */
699 void
700 ep_tick(void *arg)
701 {
702 	struct ep_softc *sc = arg;
703 	int s;
704 
705 #ifdef DIAGNOSTIC
706 	if ((sc->ep_flags & ELINK_FLAGS_MII) == 0)
707 		panic("ep_tick");
708 #endif
709 
710 	if (!device_is_active(sc->sc_dev))
711 		return;
712 
713 	s = splnet();
714 	mii_tick(&sc->sc_mii);
715 	splx(s);
716 
717 	callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
718 }
719 
720 /*
721  * Bring device up.
722  *
723  * The order in here seems important. Otherwise we may not receive
724  * interrupts. ?!
725  */
726 int
727 epinit(struct ifnet *ifp)
728 {
729 	struct ep_softc *sc = ifp->if_softc;
730 	bus_space_tag_t iot = sc->sc_iot;
731 	bus_space_handle_t ioh = sc->sc_ioh;
732 	int i, error;
733 	const u_int8_t *addr;
734 
735 	if (!sc->enabled && (error = epenable(sc)) != 0)
736 		return (error);
737 
738 	/* Make sure any pending reset has completed before touching board */
739 	ep_finish_reset(iot, ioh);
740 
741 	/*
742 	 * Cancel any pending I/O.
743 	 */
744 	epstop(ifp, 0);
745 
746 	if (sc->bustype != ELINK_BUS_PCI && sc->bustype != ELINK_BUS_EISA
747 	    && sc->bustype != ELINK_BUS_MCA) {
748 		GO_WINDOW(0);
749 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL, 0);
750 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL,
751 		    ENABLE_DRQ_IRQ);
752 	}
753 
754 	if (sc->bustype == ELINK_BUS_PCMCIA) {
755 		bus_space_write_2(iot, ioh, ELINK_W0_RESOURCE_CFG, 0x3f00);
756 	}
757 
758 	GO_WINDOW(2);
759 	/* Reload the ether_addr. */
760 	addr = CLLADDR(ifp->if_sadl);
761 	for (i = 0; i < 6; i += 2)
762 		bus_space_write_2(iot, ioh, ELINK_W2_ADDR_0 + i,
763 		    (addr[i] << 0) | (addr[i + 1] << 8));
764 
765 	/*
766 	 * Reset the station-address receive filter.
767 	 * A bug workaround for busmastering (Vortex, Demon) cards.
768 	 */
769 	for (i = 0; i < 6; i += 2)
770 		bus_space_write_2(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
771 
772 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
773 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
774 
775 	GO_WINDOW(1);		/* Window 1 is operating window */
776 	for (i = 0; i < 31; i++)
777 		(void)bus_space_read_2(iot, ioh,
778 				       ep_w1_reg(sc, ELINK_W1_TX_STATUS));
779 
780 	/* Set threshold for Tx-space available interrupt. */
781 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
782 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
783 
784 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
785 		/*
786 		 * Enable options in the PCMCIA LAN COR register, via
787 		 * RoadRunner Window 1.
788 		 *
789 		 * XXX MAGIC CONSTANTS!
790 		 */
791 		u_int16_t cor;
792 
793 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, (1 << 11));
794 
795 		cor = bus_space_read_2(iot, ioh, 0) & ~0x30;
796 		if (sc->ep_flags & ELINK_FLAGS_USESHAREDMEM)
797 			cor |= 0x10;
798 		if (sc->ep_flags & ELINK_FLAGS_FORCENOWAIT)
799 			cor |= 0x20;
800 		bus_space_write_2(iot, ioh, 0, cor);
801 
802 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
803 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
804 
805 		if (sc->ep_flags & ELINK_FLAGS_MII) {
806 			ep_roadrunner_mii_enable(sc);
807 			GO_WINDOW(1);
808 		}
809 	}
810 
811 	/* Enable interrupts. */
812 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
813 	    SET_RD_0_MASK | WATCHED_INTERRUPTS);
814 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
815 	    SET_INTR_MASK | WATCHED_INTERRUPTS);
816 
817 	/*
818 	 * Attempt to get rid of any stray interrupts that occurred during
819 	 * configuration.  On the i386 this isn't possible because one may
820 	 * already be queued.  However, a single stray interrupt is
821 	 * unimportant.
822 	 */
823 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
824 
825 	epsetfilter(sc);
826 	epsetmedia(sc);
827 
828 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
829 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
830 
831 	epmbuffill(sc);
832 
833 	/* Interface is now `running', with no output active. */
834 	ifp->if_flags |= IFF_RUNNING;
835 	ifp->if_flags &= ~IFF_OACTIVE;
836 
837 	if (sc->ep_flags & ELINK_FLAGS_MII) {
838 		/* Start the one second clock. */
839 		callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
840 	}
841 
842 	/* Attempt to start output, if any. */
843 	epstart(ifp);
844 
845 	return (0);
846 }
847 
848 
849 /*
850  * Set multicast receive filter.
851  * elink3 hardware has no selective multicast filter in hardware.
852  * Enable reception of all multicasts and filter in software.
853  */
854 void
855 epsetfilter(struct ep_softc *sc)
856 {
857 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
858 
859 	GO_WINDOW(1);		/* Window 1 is operating window */
860 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
861 	    SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
862 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0) |
863 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0));
864 }
865 
866 int
867 ep_media_change(struct ifnet *ifp)
868 {
869 	struct ep_softc *sc = ifp->if_softc;
870 
871 	if (sc->enabled && (ifp->if_flags & IFF_UP) != 0)
872 		epreset(sc);
873 
874 	return (0);
875 }
876 
877 /*
878  * Reset and enable the MII on the RoadRunner.
879  */
880 void
881 ep_roadrunner_mii_enable(struct ep_softc *sc)
882 {
883 	bus_space_tag_t iot = sc->sc_iot;
884 	bus_space_handle_t ioh = sc->sc_ioh;
885 
886 	GO_WINDOW(3);
887 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
888 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
889 	delay(1000);
890 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
891 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_MII_RESET|
892 	    ELINK_RUNNER_ENABLE_MII);
893 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
894 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
895 	delay(1000);
896 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
897 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
898 }
899 
900 /*
901  * Set the card to use the specified media.
902  */
903 void
904 epsetmedia(struct ep_softc *sc)
905 {
906 	bus_space_tag_t iot = sc->sc_iot;
907 	bus_space_handle_t ioh = sc->sc_ioh;
908 
909 	/* Turn everything off.  First turn off linkbeat and UTP. */
910 	GO_WINDOW(4);
911 	bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0x0);
912 
913 	/* Turn off coax */
914 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
915 	delay(1000);
916 
917 	/*
918 	 * If the device has MII, select it, and then tell the
919 	 * PHY which media to use.
920 	 */
921 	if (sc->ep_flags & ELINK_FLAGS_MII) {
922 		int config0, config1;
923 
924 		GO_WINDOW(3);
925 
926 		if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
927 			int resopt;
928 
929 			resopt = bus_space_read_2(iot, ioh,
930 			    ELINK_W3_RESET_OPTIONS);
931 			bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
932 			    resopt | ELINK_RUNNER_ENABLE_MII);
933 		}
934 
935 		config0 = (u_int)bus_space_read_2(iot, ioh,
936 		    ELINK_W3_INTERNAL_CONFIG);
937 		config1 = (u_int)bus_space_read_2(iot, ioh,
938 		    ELINK_W3_INTERNAL_CONFIG + 2);
939 
940 		config1 = config1 & ~CONFIG_MEDIAMASK;
941 		config1 |= (ELINKMEDIA_MII << CONFIG_MEDIAMASK_SHIFT);
942 
943 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
944 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
945 		    config1);
946 		GO_WINDOW(1);	/* back to operating window */
947 
948 		mii_mediachg(&sc->sc_mii);
949 		return;
950 	}
951 
952 	/*
953 	 * Now turn on the selected media/transceiver.
954 	 */
955 	GO_WINDOW(4);
956 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_cur->ifm_media)) {
957 	case IFM_10_T:
958 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
959 		    JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
960 		break;
961 
962 	case IFM_10_2:
963 		bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
964 		DELAY(1000);	/* 50ms not enmough? */
965 		break;
966 
967 	case IFM_100_TX:
968 	case IFM_100_FX:
969 	case IFM_100_T4:		/* XXX check documentation */
970 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
971 		    LINKBEAT_ENABLE);
972 		DELAY(1000);	/* not strictly necessary? */
973 		break;
974 
975 	case IFM_10_5:
976 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
977 		    SQE_ENABLE);
978 		DELAY(1000);	/* not strictly necessary? */
979 		break;
980 
981 	case IFM_MANUAL:
982 		/*
983 		 * Nothing to do here; we are actually enabling the
984 		 * external PHY on the MII port.
985 		 */
986 		break;
987 
988 	case IFM_NONE:
989 		printf("%s: interface disabled\n", device_xname(sc->sc_dev));
990 		return;
991 
992 	default:
993 		panic("epsetmedia: impossible");
994 	}
995 
996 	/*
997 	 * Tell the chip which port to use.
998 	 */
999 	switch (sc->ep_chipset) {
1000 	case ELINK_CHIPSET_VORTEX:
1001 	case ELINK_CHIPSET_BOOMERANG:
1002 	    {
1003 		int mctl, config0, config1;
1004 
1005 		GO_WINDOW(3);
1006 		config0 = (u_int)bus_space_read_2(iot, ioh,
1007 		    ELINK_W3_INTERNAL_CONFIG);
1008 		config1 = (u_int)bus_space_read_2(iot, ioh,
1009 		    ELINK_W3_INTERNAL_CONFIG + 2);
1010 
1011 		config1 = config1 & ~CONFIG_MEDIAMASK;
1012 		config1 |= (sc->sc_mii.mii_media.ifm_cur->ifm_data <<
1013 		    CONFIG_MEDIAMASK_SHIFT);
1014 
1015 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
1016 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
1017 		    config1);
1018 
1019 		mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
1020 		if (sc->sc_mii.mii_media.ifm_cur->ifm_media & IFM_FDX)
1021 			mctl |= MAC_CONTROL_FDX;
1022 		else
1023 			mctl &= ~MAC_CONTROL_FDX;
1024 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
1025 		break;
1026 	    }
1027 	default:
1028 	    {
1029 		int w0_addr_cfg;
1030 
1031 		GO_WINDOW(0);
1032 		w0_addr_cfg = bus_space_read_2(iot, ioh, ELINK_W0_ADDRESS_CFG);
1033 		w0_addr_cfg &= 0x3fff;
1034 		bus_space_write_2(iot, ioh, ELINK_W0_ADDRESS_CFG, w0_addr_cfg |
1035 		    (sc->sc_mii.mii_media.ifm_cur->ifm_data << 14));
1036 		DELAY(1000);
1037 		break;
1038 	    }
1039 	}
1040 
1041 	GO_WINDOW(1);		/* Window 1 is operating window */
1042 }
1043 
1044 /*
1045  * Get currently-selected media from card.
1046  * (if_media callback, may be called before interface is brought up).
1047  */
1048 void
1049 ep_media_status(struct ifnet *ifp, struct ifmediareq *req)
1050 {
1051 	struct ep_softc *sc = ifp->if_softc;
1052 	bus_space_tag_t iot = sc->sc_iot;
1053 	bus_space_handle_t ioh = sc->sc_ioh;
1054 
1055 	if (sc->enabled == 0) {
1056 		req->ifm_active = IFM_ETHER|IFM_NONE;
1057 		req->ifm_status = 0;
1058 		return;
1059 	}
1060 
1061 	/*
1062 	 * If we have MII, go ask the PHY what's going on.
1063 	 */
1064 	if (sc->ep_flags & ELINK_FLAGS_MII) {
1065 		mii_pollstat(&sc->sc_mii);
1066 		req->ifm_active = sc->sc_mii.mii_media_active;
1067 		req->ifm_status = sc->sc_mii.mii_media_status;
1068 		return;
1069 	}
1070 
1071 	/*
1072 	 * Ok, at this point we claim that our active media is
1073 	 * the currently selected media.  We'll update our status
1074 	 * if our chipset allows us to detect link.
1075 	 */
1076 	req->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
1077 	req->ifm_status = 0;
1078 
1079 	switch (sc->ep_chipset) {
1080 	case ELINK_CHIPSET_VORTEX:
1081 	case ELINK_CHIPSET_BOOMERANG:
1082 		GO_WINDOW(4);
1083 		req->ifm_status = IFM_AVALID;
1084 		if (bus_space_read_2(iot, ioh, ELINK_W4_MEDIA_TYPE) &
1085 		    LINKBEAT_DETECT)
1086 			req->ifm_status |= IFM_ACTIVE;
1087 		GO_WINDOW(1);	/* back to operating window */
1088 		break;
1089 	}
1090 }
1091 
1092 
1093 
1094 /*
1095  * Start outputting on the interface.
1096  * Always called as splnet().
1097  */
1098 void
1099 epstart(struct ifnet *ifp)
1100 {
1101 	struct ep_softc *sc = ifp->if_softc;
1102 	bus_space_tag_t iot = sc->sc_iot;
1103 	bus_space_handle_t ioh = sc->sc_ioh;
1104 	struct mbuf *m, *m0;
1105 	int sh, len, pad;
1106 	bus_size_t txreg;
1107 
1108 	/* Don't transmit if interface is busy or not running */
1109 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
1110 		return;
1111 
1112 startagain:
1113 	/* Sneak a peek at the next packet */
1114 	IFQ_POLL(&ifp->if_snd, m0);
1115 	if (m0 == 0)
1116 		return;
1117 
1118 	/* We need to use m->m_pkthdr.len, so require the header */
1119 	if ((m0->m_flags & M_PKTHDR) == 0)
1120 		panic("epstart: no header mbuf");
1121 	len = m0->m_pkthdr.len;
1122 
1123 	pad = (4 - len) & 3;
1124 
1125 	/*
1126 	 * The 3c509 automatically pads short packets to minimum ethernet
1127 	 * length, but we drop packets that are too large. Perhaps we should
1128 	 * truncate them instead?
1129 	 */
1130 	if (len + pad > ETHER_MAX_LEN) {
1131 		/* packet is obviously too large: toss it */
1132 		++ifp->if_oerrors;
1133 		IFQ_DEQUEUE(&ifp->if_snd, m0);
1134 		m_freem(m0);
1135 		goto readcheck;
1136 	}
1137 
1138 	if (bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_FREE_TX)) <
1139 	    len + pad + 4) {
1140 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
1141 		    SET_TX_AVAIL_THRESH |
1142 		    ((len + pad + 4) >> sc->ep_pktlenshift));
1143 		/* not enough room in FIFO */
1144 		ifp->if_flags |= IFF_OACTIVE;
1145 		return;
1146 	} else {
1147 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
1148 		    SET_TX_AVAIL_THRESH | ELINK_THRESH_DISABLE);
1149 	}
1150 
1151 	IFQ_DEQUEUE(&ifp->if_snd, m0);
1152 	if (m0 == 0)		/* not really needed */
1153 		return;
1154 
1155 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_TX_START_THRESH |
1156 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/));
1157 
1158 	if (ifp->if_bpf)
1159 		bpf_ops->bpf_mtap(ifp->if_bpf, m0);
1160 
1161 	/*
1162 	 * Do the output at a high interrupt priority level so that an
1163 	 * interrupt from another device won't cause a FIFO underrun.
1164 	 * We choose splsched() since that blocks essentially everything
1165 	 * except for interrupts from serial devices (which typically
1166 	 * lose data if their interrupt isn't serviced fast enough).
1167 	 *
1168 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
1169 	 */
1170 	sh = splsched();
1171 
1172 	txreg = ep_w1_reg(sc, ELINK_W1_TX_PIO_WR_1);
1173 
1174 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
1175 		/*
1176 		 * Prime the FIFO buffer counter (number of 16-bit
1177 		 * words about to be written to the FIFO).
1178 		 *
1179 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
1180 		 * COUNTER IS NON-ZERO!
1181 		 */
1182 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL,
1183 		    (len + pad) >> 1);
1184 	}
1185 
1186 	bus_space_write_2(iot, ioh, txreg, len);
1187 	bus_space_write_2(iot, ioh, txreg, 0xffff); /* Second is meaningless */
1188 	if (ELINK_IS_BUS_32(sc->bustype)) {
1189 		for (m = m0; m;) {
1190 			if (m->m_len > 3)  {
1191 				/* align our reads from core */
1192 				if (mtod(m, u_long) & 3)  {
1193 					u_long count =
1194 					    4 - (mtod(m, u_long) & 3);
1195 					bus_space_write_multi_1(iot, ioh,
1196 					    txreg, mtod(m, u_int8_t *), count);
1197 					m->m_data =
1198 					    (void *)(mtod(m, u_long) + count);
1199 					m->m_len -= count;
1200 				}
1201 				bus_space_write_multi_stream_4(iot, ioh,
1202 				    txreg, mtod(m, u_int32_t *), m->m_len >> 2);
1203 				m->m_data = (void *)(mtod(m, u_long) +
1204 					(u_long)(m->m_len & ~3));
1205 				m->m_len -= m->m_len & ~3;
1206 			}
1207 			if (m->m_len)  {
1208 				bus_space_write_multi_1(iot, ioh,
1209 				    txreg, mtod(m, u_int8_t *), m->m_len);
1210 			}
1211 			MFREE(m, m0);
1212 			m = m0;
1213 		}
1214 	} else {
1215 		for (m = m0; m;) {
1216 			if (m->m_len > 1)  {
1217 				if (mtod(m, u_long) & 1)  {
1218 					bus_space_write_1(iot, ioh,
1219 					    txreg, *(mtod(m, u_int8_t *)));
1220 					m->m_data =
1221 					    (void *)(mtod(m, u_long) + 1);
1222 					m->m_len -= 1;
1223 				}
1224 				bus_space_write_multi_stream_2(iot, ioh,
1225 				    txreg, mtod(m, u_int16_t *),
1226 				    m->m_len >> 1);
1227 			}
1228 			if (m->m_len & 1)  {
1229 				bus_space_write_1(iot, ioh, txreg,
1230 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
1231 			}
1232 			MFREE(m, m0);
1233 			m = m0;
1234 		}
1235 	}
1236 	while (pad--)
1237 		bus_space_write_1(iot, ioh, txreg, 0);
1238 
1239 	splx(sh);
1240 
1241 	++ifp->if_opackets;
1242 
1243 readcheck:
1244 	if ((bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS)) &
1245 	    ERR_INCOMPLETE) == 0) {
1246 		/* We received a complete packet. */
1247 		u_int16_t status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1248 
1249 		if ((status & INTR_LATCH) == 0) {
1250 			/*
1251 			 * No interrupt, read the packet and continue
1252 			 * Is  this supposed to happen? Is my motherboard
1253 			 * completely busted?
1254 			 */
1255 			epread(sc);
1256 		} else {
1257 			/* Got an interrupt, return so that it gets serviced. */
1258 			return;
1259 		}
1260 	} else {
1261 		/* Check if we are stuck and reset [see XXX comment] */
1262 		if (epstatus(sc)) {
1263 			if (ifp->if_flags & IFF_DEBUG)
1264 				printf("%s: adapter reset\n",
1265 				    device_xname(sc->sc_dev));
1266 			epreset(sc);
1267 		}
1268 	}
1269 
1270 	goto startagain;
1271 }
1272 
1273 
1274 /*
1275  * XXX: The 3c509 card can get in a mode where both the fifo status bit
1276  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
1277  *	We detect this situation and we reset the adapter.
1278  *	It happens at times when there is a lot of broadcast traffic
1279  *	on the cable (once in a blue moon).
1280  */
1281 static int
1282 epstatus(struct ep_softc *sc)
1283 {
1284 	bus_space_tag_t iot = sc->sc_iot;
1285 	bus_space_handle_t ioh = sc->sc_ioh;
1286 	u_int16_t fifost;
1287 
1288 	/*
1289 	 * Check the FIFO status and act accordingly
1290 	 */
1291 	GO_WINDOW(4);
1292 	fifost = bus_space_read_2(iot, ioh, ELINK_W4_FIFO_DIAG);
1293 	GO_WINDOW(1);
1294 
1295 	if (fifost & FIFOS_RX_UNDERRUN) {
1296 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1297 			printf("%s: RX underrun\n", device_xname(sc->sc_dev));
1298 		epreset(sc);
1299 		return 0;
1300 	}
1301 
1302 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
1303 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1304 			printf("%s: RX Status overrun\n", device_xname(sc->sc_dev));
1305 		return 1;
1306 	}
1307 
1308 	if (fifost & FIFOS_RX_OVERRUN) {
1309 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1310 			printf("%s: RX overrun\n", device_xname(sc->sc_dev));
1311 		return 1;
1312 	}
1313 
1314 	if (fifost & FIFOS_TX_OVERRUN) {
1315 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1316 			printf("%s: TX overrun\n", device_xname(sc->sc_dev));
1317 		epreset(sc);
1318 		return 0;
1319 	}
1320 
1321 	return 0;
1322 }
1323 
1324 
1325 static void
1326 eptxstat(struct ep_softc *sc)
1327 {
1328 	bus_space_tag_t iot = sc->sc_iot;
1329 	bus_space_handle_t ioh = sc->sc_ioh;
1330 	int i;
1331 
1332 	/*
1333 	 * We need to read+write TX_STATUS until we get a 0 status
1334 	 * in order to turn off the interrupt flag.
1335 	 */
1336 	while ((i = bus_space_read_2(iot, ioh,
1337 	     ep_w1_reg(sc, ELINK_W1_TX_STATUS))) & TXS_COMPLETE) {
1338 		bus_space_write_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS),
1339 		    0x0);
1340 
1341 		if (i & TXS_JABBER) {
1342 			++sc->sc_ethercom.ec_if.if_oerrors;
1343 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1344 				printf("%s: jabber (%x)\n",
1345 				       device_xname(sc->sc_dev), i);
1346 			epreset(sc);
1347 		} else if (i & TXS_UNDERRUN) {
1348 			++sc->sc_ethercom.ec_if.if_oerrors;
1349 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
1350 				printf("%s: fifo underrun (%x) @%d\n",
1351 				       device_xname(sc->sc_dev), i,
1352 				       sc->tx_start_thresh);
1353 			if (sc->tx_succ_ok < 100)
1354 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
1355 					    sc->tx_start_thresh + 20);
1356 			sc->tx_succ_ok = 0;
1357 			epreset(sc);
1358 		} else if (i & TXS_MAX_COLLISION) {
1359 			++sc->sc_ethercom.ec_if.if_collisions;
1360 			bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
1361 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1362 		} else
1363 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
1364 	}
1365 }
1366 
1367 int
1368 epintr(void *arg)
1369 {
1370 	struct ep_softc *sc = arg;
1371 	bus_space_tag_t iot = sc->sc_iot;
1372 	bus_space_handle_t ioh = sc->sc_ioh;
1373 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1374 	u_int16_t status;
1375 	int ret = 0;
1376 
1377 	if (sc->enabled == 0 || !device_is_active(sc->sc_dev))
1378 		return (0);
1379 
1380 
1381 	for (;;) {
1382 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1383 
1384 		if ((status & WATCHED_INTERRUPTS) == 0) {
1385 			if ((status & INTR_LATCH) == 0) {
1386 #if 0
1387 				printf("%s: intr latch cleared\n",
1388 				       device_xname(&sc->sc_dev));
1389 #endif
1390 				break;
1391 			}
1392 		}
1393 
1394 		ret = 1;
1395 
1396 		/*
1397 		 * Acknowledge any interrupts.  It's important that we do this
1398 		 * first, since there would otherwise be a race condition.
1399 		 * Due to the i386 interrupt queueing, we may get spurious
1400 		 * interrupts occasionally.
1401 		 */
1402 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
1403 		    (status & (INTR_LATCH | ALL_INTERRUPTS)));
1404 
1405 #if 0
1406 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
1407 
1408 		printf("%s: intr%s%s%s%s\n", device_xname(&sc->sc_dev),
1409 		       (status & RX_COMPLETE)?" RX_COMPLETE":"",
1410 		       (status & TX_COMPLETE)?" TX_COMPLETE":"",
1411 		       (status & TX_AVAIL)?" TX_AVAIL":"",
1412 		       (status & CARD_FAILURE)?" CARD_FAILURE":"");
1413 #endif
1414 
1415 		if (status & RX_COMPLETE) {
1416 			epread(sc);
1417 		}
1418 		if (status & TX_AVAIL) {
1419 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
1420 			epstart(&sc->sc_ethercom.ec_if);
1421 		}
1422 		if (status & CARD_FAILURE) {
1423 			printf("%s: adapter failure (%x)\n",
1424 			    device_xname(sc->sc_dev), status);
1425 #if 1
1426 			epinit(ifp);
1427 #else
1428 			epreset(sc);
1429 #endif
1430 			return (1);
1431 		}
1432 		if (status & TX_COMPLETE) {
1433 			eptxstat(sc);
1434 			epstart(ifp);
1435 		}
1436 
1437 #if NRND > 0
1438 		if (status)
1439 			rnd_add_uint32(&sc->rnd_source, status);
1440 #endif
1441 	}
1442 
1443 	/* no more interrupts */
1444 	return (ret);
1445 }
1446 
1447 void
1448 epread(struct ep_softc *sc)
1449 {
1450 	bus_space_tag_t iot = sc->sc_iot;
1451 	bus_space_handle_t ioh = sc->sc_ioh;
1452 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1453 	struct mbuf *m;
1454 	int len;
1455 
1456 	len = bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS));
1457 
1458 again:
1459 	if (ifp->if_flags & IFF_DEBUG) {
1460 		int err = len & ERR_MASK;
1461 		const char *s = NULL;
1462 
1463 		if (len & ERR_INCOMPLETE)
1464 			s = "incomplete packet";
1465 		else if (err == ERR_OVERRUN)
1466 			s = "packet overrun";
1467 		else if (err == ERR_RUNT)
1468 			s = "runt packet";
1469 		else if (err == ERR_ALIGNMENT)
1470 			s = "bad alignment";
1471 		else if (err == ERR_CRC)
1472 			s = "bad crc";
1473 		else if (err == ERR_OVERSIZE)
1474 			s = "oversized packet";
1475 		else if (err == ERR_DRIBBLE)
1476 			s = "dribble bits";
1477 
1478 		if (s)
1479 			printf("%s: %s\n", device_xname(sc->sc_dev), s);
1480 	}
1481 
1482 	if (len & ERR_INCOMPLETE)
1483 		return;
1484 
1485 	if (len & ERR_RX) {
1486 		++ifp->if_ierrors;
1487 		goto abort;
1488 	}
1489 
1490 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
1491 
1492 	/* Pull packet off interface. */
1493 	m = epget(sc, len);
1494 	if (m == 0) {
1495 		ifp->if_ierrors++;
1496 		goto abort;
1497 	}
1498 
1499 	++ifp->if_ipackets;
1500 
1501 	/*
1502 	 * Check if there's a BPF listener on this interface.
1503 	 * If so, hand off the raw packet to BPF.
1504 	 */
1505 	if (ifp->if_bpf)
1506 		bpf_ops->bpf_mtap(ifp->if_bpf, m);
1507 
1508 	(*ifp->if_input)(ifp, m);
1509 
1510 	/*
1511 	 * In periods of high traffic we can actually receive enough
1512 	 * packets so that the fifo overrun bit will be set at this point,
1513 	 * even though we just read a packet. In this case we
1514 	 * are not going to receive any more interrupts. We check for
1515 	 * this condition and read again until the fifo is not full.
1516 	 * We could simplify this test by not using epstatus(), but
1517 	 * rechecking the RX_STATUS register directly. This test could
1518 	 * result in unnecessary looping in cases where there is a new
1519 	 * packet but the fifo is not full, but it will not fix the
1520 	 * stuck behavior.
1521 	 *
1522 	 * Even with this improvement, we still get packet overrun errors
1523 	 * which are hurting performance. Maybe when I get some more time
1524 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
1525 	 */
1526 	if (epstatus(sc)) {
1527 		len = bus_space_read_2(iot, ioh,
1528 		    ep_w1_reg(sc, ELINK_W1_RX_STATUS));
1529 		/* Check if we are stuck and reset [see XXX comment] */
1530 		if (len & ERR_INCOMPLETE) {
1531 			if (ifp->if_flags & IFF_DEBUG)
1532 				printf("%s: adapter reset\n",
1533 				    device_xname(sc->sc_dev));
1534 			epreset(sc);
1535 			return;
1536 		}
1537 		goto again;
1538 	}
1539 
1540 	return;
1541 
1542 abort:
1543 	ep_discard_rxtop(iot, ioh);
1544 
1545 }
1546 
1547 struct mbuf *
1548 epget(struct ep_softc *sc, int totlen)
1549 {
1550 	bus_space_tag_t iot = sc->sc_iot;
1551 	bus_space_handle_t ioh = sc->sc_ioh;
1552 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1553 	struct mbuf *m;
1554 	bus_size_t rxreg;
1555 	int len, remaining;
1556 	int s;
1557 	void *newdata;
1558 	u_long offset;
1559 
1560 	m = sc->mb[sc->next_mb];
1561 	sc->mb[sc->next_mb] = 0;
1562 	if (m == 0) {
1563 		MGETHDR(m, M_DONTWAIT, MT_DATA);
1564 		if (m == 0)
1565 			return 0;
1566 	} else {
1567 		/* If the queue is no longer full, refill. */
1568 		if (sc->last_mb == sc->next_mb)
1569 			callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
1570 
1571 		/* Convert one of our saved mbuf's. */
1572 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
1573 		m->m_data = m->m_pktdat;
1574 		m->m_flags = M_PKTHDR;
1575 		memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
1576 	}
1577 	m->m_pkthdr.rcvif = ifp;
1578 	m->m_pkthdr.len = totlen;
1579 	len = MHLEN;
1580 
1581 	/*
1582 	 * Allocate big enough space to hold whole packet, to avoid
1583 	 * allocating new mbufs on splsched().
1584 	 */
1585 	if (totlen + ALIGNBYTES > len) {
1586 		if (totlen + ALIGNBYTES > MCLBYTES) {
1587 			len = ALIGN(totlen + ALIGNBYTES);
1588 			MEXTMALLOC(m, len, M_DONTWAIT);
1589 		} else {
1590 			len = MCLBYTES;
1591 			MCLGET(m, M_DONTWAIT);
1592 		}
1593 		if ((m->m_flags & M_EXT) == 0) {
1594 			m_free(m);
1595 			return 0;
1596 		}
1597 	}
1598 
1599 	/* align the struct ip header */
1600 	newdata = (char *)ALIGN(m->m_data + sizeof(struct ether_header))
1601 	    - sizeof(struct ether_header);
1602 	m->m_data = newdata;
1603 	m->m_len = totlen;
1604 
1605 	rxreg = ep_w1_reg(sc, ELINK_W1_RX_PIO_RD_1);
1606 	remaining = totlen;
1607 	offset = mtod(m, u_long);
1608 
1609 	/*
1610 	 * We read the packet at a high interrupt priority level so that
1611 	 * an interrupt from another device won't cause the card's packet
1612 	 * buffer to overflow.  We choose splsched() since that blocks
1613 	 * essentially everything except for interrupts from serial
1614 	 * devices (which typically lose data if their interrupt isn't
1615 	 * serviced fast enough).
1616 	 *
1617 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
1618 	 */
1619 	s = splsched();
1620 
1621 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
1622 		/*
1623 		 * Prime the FIFO buffer counter (number of 16-bit
1624 		 * words about to be read from the FIFO).
1625 		 *
1626 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
1627 		 * COUNTER IS NON-ZERO!
1628 		 */
1629 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, totlen >> 1);
1630 	}
1631 
1632 	if (ELINK_IS_BUS_32(sc->bustype)) {
1633 		/*
1634 		 * Read bytes up to the point where we are aligned.
1635 		 * (We can align to 4 bytes, rather than ALIGNBYTES,
1636 		 * here because we're later reading 4-byte chunks.)
1637 		 */
1638 		if ((remaining > 3) && (offset & 3))  {
1639 			int count = (4 - (offset & 3));
1640 			bus_space_read_multi_1(iot, ioh,
1641 			    rxreg, (u_int8_t *) offset, count);
1642 			offset += count;
1643 			remaining -= count;
1644 		}
1645 		if (remaining > 3) {
1646 			bus_space_read_multi_stream_4(iot, ioh,
1647 			    rxreg, (u_int32_t *) offset,
1648 				    remaining >> 2);
1649 			offset += remaining & ~3;
1650 			remaining &= 3;
1651 		}
1652 		if (remaining)  {
1653 			bus_space_read_multi_1(iot, ioh,
1654 			    rxreg, (u_int8_t *) offset, remaining);
1655 		}
1656 	} else {
1657 		if ((remaining > 1) && (offset & 1))  {
1658 			bus_space_read_multi_1(iot, ioh,
1659 			    rxreg, (u_int8_t *) offset, 1);
1660 			remaining -= 1;
1661 			offset += 1;
1662 		}
1663 		if (remaining > 1) {
1664 			bus_space_read_multi_stream_2(iot, ioh,
1665 			    rxreg, (u_int16_t *) offset,
1666 			    remaining >> 1);
1667 			offset += remaining & ~1;
1668 		}
1669 		if (remaining & 1)  {
1670 				bus_space_read_multi_1(iot, ioh,
1671 			    rxreg, (u_int8_t *) offset, remaining & 1);
1672 		}
1673 	}
1674 
1675 	ep_discard_rxtop(iot, ioh);
1676 
1677 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
1678 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
1679 	splx(s);
1680 
1681 	return (m);
1682 }
1683 
1684 int
1685 epioctl(struct ifnet *ifp, u_long cmd, void *data)
1686 {
1687 	struct ep_softc *sc = ifp->if_softc;
1688 	struct ifreq *ifr = (struct ifreq *)data;
1689 	int s, error = 0;
1690 
1691 	s = splnet();
1692 
1693 	switch (cmd) {
1694 
1695 	case SIOCSIFMEDIA:
1696 	case SIOCGIFMEDIA:
1697 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
1698 		break;
1699 
1700 	case SIOCADDMULTI:
1701 	case SIOCDELMULTI:
1702 		if (sc->enabled == 0) {
1703 			error = EIO;
1704 			break;
1705 		}
1706 
1707 	default:
1708 		error = ether_ioctl(ifp, cmd, data);
1709 
1710 		if (error == ENETRESET) {
1711 			/*
1712 			 * Multicast list has changed; set the hardware filter
1713 			 * accordingly.
1714 			 */
1715 			if (ifp->if_flags & IFF_RUNNING)
1716 				epreset(sc);
1717 			error = 0;
1718 		}
1719 		break;
1720 	}
1721 
1722 	splx(s);
1723 	return (error);
1724 }
1725 
1726 void
1727 epreset(struct ep_softc *sc)
1728 {
1729 	int s;
1730 
1731 	s = splnet();
1732 	epinit(&sc->sc_ethercom.ec_if);
1733 	splx(s);
1734 }
1735 
1736 void
1737 epwatchdog(struct ifnet *ifp)
1738 {
1739 	struct ep_softc *sc = ifp->if_softc;
1740 
1741 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
1742 	++sc->sc_ethercom.ec_if.if_oerrors;
1743 
1744 	epreset(sc);
1745 }
1746 
1747 void
1748 epstop(struct ifnet *ifp, int disable)
1749 {
1750 	struct ep_softc *sc = ifp->if_softc;
1751 	bus_space_tag_t iot = sc->sc_iot;
1752 	bus_space_handle_t ioh = sc->sc_ioh;
1753 
1754 	if (sc->ep_flags & ELINK_FLAGS_MII) {
1755 		/* Stop the one second clock. */
1756 		callout_stop(&sc->sc_mbuf_callout);
1757 
1758 		/* Down the MII. */
1759 		mii_down(&sc->sc_mii);
1760 	}
1761 
1762 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
1763 		/*
1764 		 * Clear the FIFO buffer count, thus halting
1765 		 * any currently-running transactions.
1766 		 */
1767 		GO_WINDOW(1);		/* sanity */
1768 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
1769 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
1770 	}
1771 
1772 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
1773 	ep_discard_rxtop(iot, ioh);
1774 
1775 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
1776 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
1777 
1778 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
1779 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
1780 
1781 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | INTR_LATCH);
1782 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK);
1783 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK);
1784 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RX_FILTER);
1785 
1786 	epmbufempty(sc);
1787 
1788 	if (disable)
1789 		epdisable(sc);
1790 
1791 	ifp->if_flags &= ~IFF_RUNNING;
1792 }
1793 
1794 
1795 /*
1796  * Before reboots, reset card completely.
1797  */
1798 static bool
1799 epshutdown(device_t self, int howto)
1800 {
1801 	struct ep_softc *sc = device_private(self);
1802 	int s = splnet();
1803 
1804 	if (sc->enabled) {
1805 		epstop(&sc->sc_ethercom.ec_if, 0);
1806 		ep_reset_cmd(sc, ELINK_COMMAND, GLOBAL_RESET);
1807 		epdisable(sc);
1808 		sc->enabled = 0;
1809 	}
1810 	splx(s);
1811 
1812 	return true;
1813 }
1814 
1815 /*
1816  * We get eeprom data from the id_port given an offset into the
1817  * eeprom.  Basically; after the ID_sequence is sent to all of
1818  * the cards; they enter the ID_CMD state where they will accept
1819  * command requests. 0x80-0xbf loads the eeprom data.  We then
1820  * read the port 16 times and with every read; the cards check
1821  * for contention (ie: if one card writes a 0 bit and another
1822  * writes a 1 bit then the host sees a 0. At the end of the cycle;
1823  * each card compares the data on the bus; if there is a difference
1824  * then that card goes into ID_WAIT state again). In the meantime;
1825  * one bit of data is returned in the AX register which is conveniently
1826  * returned to us by bus_space_read_2().  Hence; we read 16 times getting one
1827  * bit of data with each read.
1828  *
1829  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
1830  */
1831 u_int16_t
1832 epreadeeprom(bus_space_tag_t iot, bus_space_handle_t ioh, int offset)
1833 {
1834 	u_int16_t data = 0;
1835 	int i;
1836 
1837 	bus_space_write_2(iot, ioh, 0, 0x80 + offset);
1838 	delay(1000);
1839 	for (i = 0; i < 16; i++)
1840 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
1841 	return (data);
1842 }
1843 
1844 static int
1845 epbusyeeprom(struct ep_softc *sc)
1846 {
1847 	bus_space_tag_t iot = sc->sc_iot;
1848 	bus_space_handle_t ioh = sc->sc_ioh;
1849 	bus_size_t eecmd;
1850 	int i = 100, j;
1851 	uint16_t busybit;
1852 
1853 	if (sc->bustype == ELINK_BUS_PCMCIA) {
1854 		delay(1000);
1855 		return 0;
1856 	}
1857 
1858 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW) {
1859 		eecmd = CORK_ASIC_EEPROM_COMMAND;
1860 		busybit = CORK_EEPROM_BUSY;
1861 	} else {
1862 		eecmd = ELINK_W0_EEPROM_COMMAND;
1863 		busybit = EEPROM_BUSY;
1864 	}
1865 
1866 	j = 0;		/* bad GCC flow analysis */
1867 	while (i--) {
1868 		j = bus_space_read_2(iot, ioh, eecmd);
1869 		if (j & busybit)
1870 			delay(100);
1871 		else
1872 			break;
1873 	}
1874 	if (i == 0) {
1875 		aprint_normal("\n");
1876 		aprint_error_dev(sc->sc_dev, "eeprom failed to come ready\n");
1877 		return (1);
1878 	}
1879 	if (sc->ep_chipset != ELINK_CHIPSET_CORKSCREW &&
1880 	    (j & EEPROM_TST_MODE) != 0) {
1881 		/* XXX PnP mode? */
1882 		printf("\n%s: erase pencil mark!\n", device_xname(sc->sc_dev));
1883 		return (1);
1884 	}
1885 	return (0);
1886 }
1887 
1888 u_int16_t
1889 ep_read_eeprom(struct ep_softc *sc, u_int16_t offset)
1890 {
1891 	bus_size_t eecmd, eedata;
1892 	u_int16_t readcmd;
1893 
1894 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW) {
1895 		eecmd = CORK_ASIC_EEPROM_COMMAND;
1896 		eedata = CORK_ASIC_EEPROM_DATA;
1897 	} else {
1898 		eecmd = ELINK_W0_EEPROM_COMMAND;
1899 		eedata = ELINK_W0_EEPROM_DATA;
1900 	}
1901 
1902 	/*
1903 	 * RoadRunner has a larger EEPROM, so a different read command
1904 	 * is required.
1905 	 */
1906 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER)
1907 		readcmd = READ_EEPROM_RR;
1908 	else
1909 		readcmd = READ_EEPROM;
1910 
1911 	if (epbusyeeprom(sc))
1912 		return (0);		/* XXX why is eeprom busy? */
1913 
1914 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, eecmd, readcmd | offset);
1915 
1916 	if (epbusyeeprom(sc))
1917 		return (0);		/* XXX why is eeprom busy? */
1918 
1919 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, eedata));
1920 }
1921 
1922 void
1923 epmbuffill(void *v)
1924 {
1925 	struct ep_softc *sc = v;
1926 	struct mbuf *m;
1927 	int s, i;
1928 
1929 	s = splnet();
1930 	i = sc->last_mb;
1931 	do {
1932 		if (sc->mb[i] == 0) {
1933 			MGET(m, M_DONTWAIT, MT_DATA);
1934 			if (m == 0)
1935 				break;
1936 			sc->mb[i] = m;
1937 		}
1938 		i = (i + 1) % MAX_MBS;
1939 	} while (i != sc->next_mb);
1940 	sc->last_mb = i;
1941 	/* If the queue was not filled, try again. */
1942 	if (sc->last_mb != sc->next_mb)
1943 		callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
1944 	splx(s);
1945 }
1946 
1947 void
1948 epmbufempty(struct ep_softc *sc)
1949 {
1950 	int s, i;
1951 
1952 	s = splnet();
1953 	for (i = 0; i < MAX_MBS; i++) {
1954 		if (sc->mb[i]) {
1955 			m_freem(sc->mb[i]);
1956 			sc->mb[i] = NULL;
1957 		}
1958 	}
1959 	sc->last_mb = sc->next_mb = 0;
1960 	callout_stop(&sc->sc_mbuf_callout);
1961 	splx(s);
1962 }
1963 
1964 int
1965 epenable(struct ep_softc *sc)
1966 {
1967 
1968 	if (sc->enabled == 0 && sc->enable != NULL) {
1969 		if ((*sc->enable)(sc) != 0) {
1970 			aprint_error_dev(sc->sc_dev, "device enable failed\n");
1971 			return (EIO);
1972 		}
1973 	}
1974 
1975 	sc->enabled = 1;
1976 	return (0);
1977 }
1978 
1979 void
1980 epdisable(struct ep_softc *sc)
1981 {
1982 
1983 	if (sc->enabled != 0 && sc->disable != NULL) {
1984 		(*sc->disable)(sc);
1985 		sc->enabled = 0;
1986 	}
1987 }
1988 
1989 /*
1990  * ep_activate:
1991  *
1992  *	Handle device activation/deactivation requests.
1993  */
1994 int
1995 ep_activate(device_t self, enum devact act)
1996 {
1997 	struct ep_softc *sc = device_private(self);
1998 
1999 	switch (act) {
2000 	case DVACT_DEACTIVATE:
2001 		if_deactivate(&sc->sc_ethercom.ec_if);
2002 		return 0;
2003 	default:
2004 		return EOPNOTSUPP;
2005 	}
2006 }
2007 
2008 /*
2009  * ep_detach:
2010  *
2011  *	Detach a elink3 interface.
2012  */
2013 int
2014 ep_detach(device_t self, int flags)
2015 {
2016 	struct ep_softc *sc = device_private(self);
2017 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2018 
2019 	/* Succeed now if there's no work to do. */
2020 	if ((sc->sc_flags & ELINK_FLAGS_ATTACHED) == 0)
2021 		return (0);
2022 
2023 	epdisable(sc);
2024 
2025 	callout_stop(&sc->sc_mii_callout);
2026 	callout_stop(&sc->sc_mbuf_callout);
2027 
2028 	if (sc->ep_flags & ELINK_FLAGS_MII) {
2029 		/* Detach all PHYs */
2030 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
2031 	}
2032 
2033 	/* Delete all remaining media. */
2034 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
2035 
2036 #if NRND > 0
2037 	rnd_detach_source(&sc->rnd_source);
2038 #endif
2039 	ether_ifdetach(ifp);
2040 	if_detach(ifp);
2041 
2042 	pmf_device_deregister(sc->sc_dev);
2043 
2044 	return (0);
2045 }
2046 
2047 u_int32_t
2048 ep_mii_bitbang_read(device_t self)
2049 {
2050 	struct ep_softc *sc = device_private(self);
2051 
2052 	/* We're already in Window 4. */
2053 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
2054 	    ELINK_W4_BOOM_PHYSMGMT));
2055 }
2056 
2057 void
2058 ep_mii_bitbang_write(device_t self, u_int32_t val)
2059 {
2060 	struct ep_softc *sc = device_private(self);
2061 
2062 	/* We're already in Window 4. */
2063 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
2064 	    ELINK_W4_BOOM_PHYSMGMT, val);
2065 }
2066 
2067 int
2068 ep_mii_readreg(device_t self, int phy, int reg)
2069 {
2070 	struct ep_softc *sc = device_private(self);
2071 	int val;
2072 
2073 	GO_WINDOW(4);
2074 
2075 	val = mii_bitbang_readreg(self, &ep_mii_bitbang_ops, phy, reg);
2076 
2077 	GO_WINDOW(1);
2078 
2079 	return (val);
2080 }
2081 
2082 void
2083 ep_mii_writereg(device_t self, int phy, int reg, int val)
2084 {
2085 	struct ep_softc *sc = device_private(self);
2086 
2087 	GO_WINDOW(4);
2088 
2089 	mii_bitbang_writereg(self, &ep_mii_bitbang_ops, phy, reg, val);
2090 
2091 	GO_WINDOW(1);
2092 }
2093 
2094 void
2095 ep_statchg(device_t self)
2096 {
2097 	struct ep_softc *sc = device_private(self);
2098 	bus_space_tag_t iot = sc->sc_iot;
2099 	bus_space_handle_t ioh = sc->sc_ioh;
2100 	int mctl;
2101 
2102 	GO_WINDOW(3);
2103 	mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
2104 	if (sc->sc_mii.mii_media_active & IFM_FDX)
2105 		mctl |= MAC_CONTROL_FDX;
2106 	else
2107 		mctl &= ~MAC_CONTROL_FDX;
2108 	bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
2109 	GO_WINDOW(1);	/* back to operating window */
2110 }
2111 
2112 void
2113 ep_power(int why, void *arg)
2114 {
2115 	struct ep_softc *sc = arg;
2116 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2117 	int s;
2118 
2119 	s = splnet();
2120 	switch (why) {
2121 	case PWR_SUSPEND:
2122 	case PWR_STANDBY:
2123 		epstop(ifp, 1);
2124 		break;
2125 	case PWR_RESUME:
2126 		if (ifp->if_flags & IFF_UP) {
2127 			(void)epinit(ifp);
2128 		}
2129 		break;
2130 	case PWR_SOFTSUSPEND:
2131 	case PWR_SOFTSTANDBY:
2132 	case PWR_SOFTRESUME:
2133 		break;
2134 	}
2135 	splx(s);
2136 }
2137