xref: /freebsd-12.1/sys/dev/fe/if_fe.c (revision 578d29dd)
1 /*
2  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3  *
4  * This software may be used, modified, copied, distributed, and sold, in
5  * both source and binary form provided that the above copyright, these
6  * terms and the following disclaimer are retained.  The name of the author
7  * and/or the contributor may not be used to endorse or promote products
8  * derived from this software without specific prior written permission.
9  *
10  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20  * SUCH DAMAGE.
21  */
22 
23 /*
24  * $FreeBSD$
25  *
26  * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
27  * To be used with FreeBSD 3.x
28  * Contributed by M. Sekiguchi. <[email protected]>
29  *
30  * This version is intended to be a generic template for various
31  * MB86960A/MB86965A based Ethernet cards.  It currently supports
32  * Fujitsu FMV-180 series for ISA and Allied-Telesis AT1700/RE2000
33  * series for ISA, as well as Fujitsu MBH10302 PC card.
34  * There are some currently-
35  * unused hooks embedded, which are primarily intended to support
36  * other types of Ethernet cards, but the author is not sure whether
37  * they are useful.
38  *
39  * This version also includes some alignments to support RE1000,
40  * C-NET(98)P2 and so on. These cards are not for AT-compatibles,
41  * but for NEC PC-98 bus -- a proprietary bus architecture available
42  * only in Japan. Confusingly, it is different from the Microsoft's
43  * PC98 architecture. :-{
44  * Further work for PC-98 version will be available as a part of
45  * FreeBSD(98) project.
46  *
47  * This software is a derivative work of if_ed.c version 1.56 by David
48  * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
49  *
50  * The following lines are retained from the original if_ed.c:
51  *
52  * Copyright (C) 1993, David Greenman. This software may be used, modified,
53  *   copied, distributed, and sold, in both source and binary form provided
54  *   that the above copyright and these terms are retained. Under no
55  *   circumstances is the author responsible for the proper functioning
56  *   of this software, nor does the author assume any responsibility
57  *   for damages incurred with its use.
58  */
59 
60 /*
61  * TODO:
62  *  o   To support ISA PnP auto configuration for FMV-183/184.
63  *  o   To reconsider mbuf usage.
64  *  o   To reconsider transmission buffer usage, including
65  *      transmission buffer size (currently 4KB x 2) and pros-and-
66  *      cons of multiple frame transmission.
67  *  o   To test IPX codes.
68  *  o   To test FreeBSD3.0-current.
69  *  o   To test BRIDGE codes.
70  */
71 
72 #include "fe.h"
73 #include "bpf.h"
74 #include "opt_fe.h"
75 #include "opt_inet.h"
76 #include "opt_ipx.h"
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/sockio.h>
81 #include <sys/mbuf.h>
82 #include <sys/socket.h>
83 
84 #include <net/ethernet.h>
85 #include <net/if.h>
86 #include <net/if_dl.h>
87 #include <net/if_mib.h>
88 #include <net/if_media.h>
89 #include <net/if_types.h>
90 
91 #include <netinet/in.h>
92 #include <netinet/if_ether.h>
93 
94 #if NBPF > 0
95 #include <net/bpf.h>
96 #endif
97 
98 #ifdef BRIDGE
99 #include <net/bridge.h>
100 #endif
101 
102 #include <machine/clock.h>
103 
104 #include <i386/isa/isa_device.h>
105 #include <i386/isa/icu.h>
106 
107 /* PCCARD suport */
108 #include "card.h"
109 #if NCARD > 0
110 #include <sys/kernel.h>
111 #include <sys/select.h>
112 #include <sys/module.h>
113 #include <pccard/cardinfo.h>
114 #include <pccard/slot.h>
115 #endif
116 
117 #include <i386/isa/ic/mb86960.h>
118 #include <i386/isa/if_fereg.h>
119 
120 /*
121  * Default settings for fe driver specific options.
122  * They can be set in config file by "options" statements.
123  */
124 
125 /*
126  * Transmit just one packet per a "send" command to 86960.
127  * This option is intended for performance test.  An EXPERIMENTAL option.
128  */
129 #ifndef FE_SINGLE_TRANSMISSION
130 #define FE_SINGLE_TRANSMISSION 0
131 #endif
132 
133 /*
134  * Maximum loops when interrupt.
135  * This option prevents an infinite loop due to hardware failure.
136  * (Some laptops make an infinite loop after PC-Card is ejected.)
137  */
138 #ifndef FE_MAX_LOOP
139 #define FE_MAX_LOOP 0x800
140 #endif
141 
142 /*
143  * If you define this option, 8-bit cards are also supported.
144  */
145 /*#define FE_8BIT_SUPPORT*/
146 
147 /*
148  * Device configuration flags.
149  */
150 
151 /* DLCR6 settings.  */
152 #define FE_FLAGS_DLCR6_VALUE	0x007F
153 
154 /* Force DLCR6 override.  */
155 #define FE_FLAGS_OVERRIDE_DLCR6	0x0080
156 
157 /* Shouldn't these be defined somewhere else such as isa_device.h?  */
158 #define NO_IOADDR	(-1)
159 #define NO_IRQ		0
160 
161 /*
162  * Data type for a multicast address filter on 8696x.
163  */
164 struct fe_filter { u_char data [ FE_FILTER_LEN ]; };
165 
166 /*
167  * Special filter values.
168  */
169 static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
170 static struct fe_filter const fe_filter_all     = { FE_FILTER_ALL };
171 
172 /* How many registers does an fe-supported adapter have at maximum?  */
173 #define MAXREGISTERS 32
174 
175 /*
176  * fe_softc: per line info and status
177  */
178 static struct fe_softc {
179 
180 	/* Used by "common" codes.  */
181 	struct arpcom arpcom;	/* Ethernet common */
182 
183 	/* Used by config codes.  */
184 
185 	/* Set by probe() and not modified in later phases.  */
186 	char const * typestr;	/* printable name of the interface.  */
187 	u_short iobase;		/* base I/O address of the adapter.  */
188 	u_short ioaddr [ MAXREGISTERS ]; /* I/O addresses of registers.  */
189 	u_short txb_size;	/* size of TX buffer, in bytes  */
190 	u_char proto_dlcr4;	/* DLCR4 prototype.  */
191 	u_char proto_dlcr5;	/* DLCR5 prototype.  */
192 	u_char proto_dlcr6;	/* DLCR6 prototype.  */
193 	u_char proto_dlcr7;	/* DLCR7 prototype.  */
194 	u_char proto_bmpr13;	/* BMPR13 prototype.  */
195 	u_char stability;	/* How stable is this?  */
196 	u_short priv_info;	/* info specific to a vendor/model.  */
197 
198 	/* Vendor/model specific hooks.  */
199 	void (*init)(struct fe_softc *); /* Just before fe_init().  */
200 	void (*stop)(struct fe_softc *); /* Just after fe_stop().  */
201 
202 	/* Transmission buffer management.  */
203 	u_short txb_free;	/* free bytes in TX buffer  */
204 	u_char txb_count;	/* number of packets in TX buffer  */
205 	u_char txb_sched;	/* number of scheduled packets  */
206 
207 	/* Excessive collision counter (see fe_tint() for details.)  */
208 	u_char tx_excolls;	/* # of excessive collisions.  */
209 
210 	/* Multicast address filter management.  */
211 	u_char filter_change;	/* MARs must be changed ASAP. */
212 	struct fe_filter filter;/* new filter value.  */
213 
214 	/* Network management.  */
215 	struct ifmib_iso_8802_3 mibdata;
216 
217 	/* Media information.  */
218 	struct ifmedia media;	/* used by if_media.  */
219 	u_short mbitmap;	/* bitmap for supported media; see bit2media */
220 	int defmedia;		/* default media  */
221 	void (* msel)(struct fe_softc *); /* media selector.  */
222 
223 }       fe_softc[NFE];
224 
225 #define sc_if		arpcom.ac_if
226 #define sc_unit		arpcom.ac_if.if_unit
227 #define sc_enaddr	arpcom.ac_enaddr
228 
229 /* Standard driver entry points.  These can be static.  */
230 static int		fe_probe	( struct isa_device * );
231 static int		fe_attach	( struct isa_device * );
232 static void		fe_init		( void * );
233 static ointhand2_t	feintr;
234 static int		fe_ioctl	( struct ifnet *, u_long, caddr_t );
235 static void		fe_start	( struct ifnet * );
236 static void		fe_watchdog	( struct ifnet * );
237 static int		fe_medchange	( struct ifnet * );
238 static void		fe_medstat	( struct ifnet *, struct ifmediareq * );
239 
240 /* Local functions.  Order of declaration is confused.  FIXME.  */
241 static int	fe_probe_ssi	( struct isa_device *, struct fe_softc * );
242 static int	fe_probe_jli	( struct isa_device *, struct fe_softc * );
243 static int	fe_probe_fmv	( struct isa_device *, struct fe_softc * );
244 static int	fe_probe_lnx	( struct isa_device *, struct fe_softc * );
245 static int	fe_probe_gwy	( struct isa_device *, struct fe_softc * );
246 static int	fe_probe_ubn	( struct isa_device *, struct fe_softc * );
247 #ifdef PC98
248 static int	fe_probe_re1000	( struct isa_device *, struct fe_softc * );
249 static int	fe_probe_cnet9ne( struct isa_device *, struct fe_softc * );
250 #endif
251 #if NCARD > 0
252 static int	fe_probe_mbh	( struct isa_device *, struct fe_softc * );
253 static int	fe_probe_tdk	( struct isa_device *, struct fe_softc * );
254 #endif
255 static int	fe_get_packet	( struct fe_softc *, u_short );
256 static void	fe_stop		( struct fe_softc * );
257 static void	fe_tint		( struct fe_softc *, u_char );
258 static void	fe_rint		( struct fe_softc *, u_char );
259 static void	fe_xmit		( struct fe_softc * );
260 static void	fe_write_mbufs	( struct fe_softc *, struct mbuf * );
261 static void	fe_setmode	( struct fe_softc * );
262 static void	fe_loadmar	( struct fe_softc * );
263 
264 #ifdef DIAGNOSTIC
265 static void	fe_emptybuffer	( struct fe_softc * );
266 #endif
267 
268 /* Driver struct used in the config code.  This must be public (external.)  */
269 struct isa_driver fedriver =
270 {
271 	fe_probe,
272 	fe_attach,
273 	"fe",
274 	1			/* It's safe to mark as "sensitive"  */
275 };
276 
277 /*
278  * Fe driver specific constants which relate to 86960/86965.
279  */
280 
281 /* Interrupt masks  */
282 #define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
283 #define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
284 		 | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
285 
286 /* Maximum number of iterations for a receive interrupt.  */
287 #define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
288 	/*
289 	 * Maximum size of SRAM is 65536,
290 	 * minimum size of transmission buffer in fe is 2x2KB,
291 	 * and minimum amount of received packet including headers
292 	 * added by the chip is 64 bytes.
293 	 * Hence FE_MAX_RECV_COUNT is the upper limit for number
294 	 * of packets in the receive buffer.
295 	 */
296 
297 /*
298  * Miscellaneous definitions not directly related to hardware.
299  */
300 
301 /* Flags for stability.  */
302 #define UNSTABLE_IRQ	0x01	/* IRQ setting may be incorrect.  */
303 #define UNSTABLE_MAC	0x02	/* Probed MAC address may be incorrect.  */
304 #define UNSTABLE_TYPE	0x04	/* Probed vendor/model may be incorrect.  */
305 
306 /* The following line must be delete when "net/if_media.h" support it.  */
307 #ifndef IFM_10_FL
308 #define IFM_10_FL	/* 13 */ IFM_10_5
309 #endif
310 
311 #if 0
312 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media.  */
313 static int const bit2media [] = {
314 #define MB_HA	0x0001
315 			IFM_HDX | IFM_ETHER | IFM_AUTO,
316 #define MB_HM	0x0002
317 			IFM_HDX | IFM_ETHER | IFM_MANUAL,
318 #define MB_HT	0x0004
319 			IFM_HDX | IFM_ETHER | IFM_10_T,
320 #define MB_H2	0x0008
321 			IFM_HDX | IFM_ETHER | IFM_10_2,
322 #define MB_H5	0x0010
323 			IFM_HDX | IFM_ETHER | IFM_10_5,
324 #define MB_HF	0x0020
325 			IFM_HDX | IFM_ETHER | IFM_10_FL,
326 #define MB_FT	0x0040
327 			IFM_FDX | IFM_ETHER | IFM_10_T,
328 	/* More can be come here... */
329 			0
330 };
331 #else
332 /* Mapping between media bitmap (in fe_softc.mbitmap) and ifm_media.  */
333 static int const bit2media [] = {
334 #define MB_HA	0x0001
335 			IFM_ETHER | IFM_AUTO,
336 #define MB_HM	0x0002
337 			IFM_ETHER | IFM_MANUAL,
338 #define MB_HT	0x0004
339 			IFM_ETHER | IFM_10_T,
340 #define MB_H2	0x0008
341 			IFM_ETHER | IFM_10_2,
342 #define MB_H5	0x0010
343 			IFM_ETHER | IFM_10_5,
344 #define MB_HF	0x0020
345 			IFM_ETHER | IFM_10_FL,
346 #define MB_FT	0x0040
347 			IFM_ETHER | IFM_10_T,
348 	/* More can be come here... */
349 			0
350 };
351 #endif
352 
353 /*
354  * Routines to access contiguous I/O ports.
355  */
356 
357 static void
358 inblk ( struct fe_softc * sc, int offs, u_char * mem, int len )
359 {
360 	while ( --len >= 0 ) {
361 		*mem++ = inb( sc->ioaddr[ offs++ ] );
362 	}
363 }
364 
365 static void
366 outblk ( struct fe_softc * sc, int offs, u_char const * mem, int len )
367 {
368 	while ( --len >= 0 ) {
369 		outb( sc->ioaddr[ offs++ ], *mem++ );
370 	}
371 }
372 
373 /* PCCARD Support */
374 #if NCARD > 0
375 /*
376  *      PC-Card (PCMCIA) specific code.
377  */
378 static int	feinit		(struct pccard_devinfo *);
379 static void	feunload	(struct pccard_devinfo *);
380 static int	fe_card_intr	(struct pccard_devinfo *);
381 
382 PCCARD_MODULE(fe, feinit, feunload, fe_card_intr, 0, net_imask);
383 
384 /*
385  *      Initialize the device - called from Slot manager.
386  */
387 static int
388 feinit(struct pccard_devinfo *devi)
389 {
390         struct fe_softc *sc;
391 
392 	/* validate unit number.  */
393 	if (devi->isahd.id_unit >= NFE) return ENODEV;
394 
395 	/* Prepare for the device probe process.  */
396 	sc = &fe_softc[devi->isahd.id_unit];
397 	sc->sc_unit = devi->isahd.id_unit;
398 	sc->iobase = devi->isahd.id_iobase;
399 
400 	/*
401 	 * When the feinit() is called, the devi->misc holds a
402 	 * six-byte value set by the pccard daemon.  If the
403 	 * corresponding entry in /etc/pccard.conf has an "ether"
404 	 * keyword, the value is the Ethernet MAC address extracted
405 	 * from CIS area of the card.  If the entry has no "ether"
406 	 * keyword, the daemon fills the field with binary zero,
407 	 * instead.  We passes the value (either MAC address or zero)
408 	 * to model-specific sub-probe routines through sc->sc_enaddr
409 	 * (it actually is sc->sc_arpcom.ar_enaddr, BTW) so that the
410 	 * sub-probe routies can use that info.
411 	 */
412 	bcopy(devi->misc, sc->sc_enaddr, ETHER_ADDR_LEN);
413 
414 	/* Probe for supported cards.  */
415 	if (fe_probe_mbh(&devi->isahd, sc) == 0
416 	 && fe_probe_tdk(&devi->isahd, sc) == 0) return ENXIO;
417 
418 	/* We've got a supported card.  Attach it, then.  */
419 	if (fe_attach(&devi->isahd) == 0) return ENXIO;
420 
421 	return 0;
422 }
423 
424 /*
425  *	feunload - unload the driver and clear the table.
426  *	XXX TODO:
427  *	This is usually called when the card is ejected, but
428  *	can be caused by a modunload of a controller driver.
429  *	The idea is to reset the driver's view of the device
430  *	and ensure that any driver entry points such as
431  *	read and write do not hang.
432  */
433 static void
434 feunload(struct pccard_devinfo *devi)
435 {
436 	struct fe_softc *sc = &fe_softc[devi->isahd.id_unit];
437 	printf("fe%d: unload\n", sc->sc_unit);
438 	fe_stop(sc);
439 	if_down(&sc->arpcom.ac_if);
440 }
441 
442 /*
443  *	fe_card_intr - Shared interrupt called from
444  *	 front end of PC-Card handler.
445  */
446 static int
447 fe_card_intr(struct pccard_devinfo *devi)
448 {
449 	feintr(devi->isahd.id_unit);
450 	return (1);
451 }
452 #endif /* NCARD > 0 */
453 
454 
455 /*
456  * Hardware probe routines.
457  *
458  * In older versions of this driver, we provided an automatic I/O
459  * address detection.  The features is, however, removed from this
460  * version, for simplicity.  Any comments?
461  */
462 
463 /*
464  * Determine if the device is present at a specified I/O address.  The
465  * main entry to the driver.
466  */
467 
468 static int
469 fe_probe (struct isa_device * dev)
470 {
471 	struct fe_softc * sc;
472 	int nports;
473 
474 #ifdef DIAGNOSTIC
475 	if (dev->id_unit >= NFE) {
476 		printf("fe%d: too large unit number for the current config\n",
477 		       dev->id_unit);
478 		return 0;
479 	}
480 #endif
481 
482 	/* Prepare for the softc struct.  */
483 	sc = &fe_softc[dev->id_unit];
484 	sc->sc_unit = dev->id_unit;
485 	sc->iobase = dev->id_iobase;
486 
487 	/* Probe for supported boards.  */
488 	nports = 0;
489 #ifdef PC98
490 	if (!nports) nports = fe_probe_re1000(dev, sc);
491 	if (!nports) nports = fe_probe_cnet9ne(dev, sc);
492 #endif
493 	if (!nports) nports = fe_probe_ssi(dev, sc);
494 	if (!nports) nports = fe_probe_jli(dev, sc);
495 	if (!nports) nports = fe_probe_fmv(dev, sc);
496 	if (!nports) nports = fe_probe_lnx(dev, sc);
497 	if (!nports) nports = fe_probe_ubn(dev, sc);
498 	if (!nports) nports = fe_probe_gwy(dev, sc);
499 
500 	/* We found supported board.  */
501 	return nports;
502 }
503 
504 /*
505  * Check for specific bits in specific registers have specific values.
506  * A common utility function called from various sub-probe routines.
507  */
508 
509 struct fe_simple_probe_struct
510 {
511 	u_char port;	/* Offset from the base I/O address.  */
512 	u_char mask;	/* Bits to be checked.  */
513 	u_char bits;	/* Values to be compared against.  */
514 };
515 
516 static int
517 fe_simple_probe ( struct fe_softc const * sc,
518 		  struct fe_simple_probe_struct const * sp )
519 {
520 	struct fe_simple_probe_struct const * p;
521 
522 	for ( p = sp; p->mask != 0; p++ ) {
523 #ifdef FE_DEBUG
524 	        unsigned a = sc->ioaddr[p->port];
525 		printf("fe%d: Probing %02x (%04x): %02x (%02x, %02x): %s\n",
526 		       sc->sc_unit, p->port, a, inb(a), p->mask, p->bits,
527 		       (inb(a) & p->mask) == p->bits ? "OK" : "NG");
528 #endif
529 		if ( ( inb( sc->ioaddr[ p->port ] ) & p->mask ) != p->bits )
530 		{
531 			return ( 0 );
532 		}
533 	}
534 	return ( 1 );
535 }
536 
537 /* Test if a given 6 byte value is a valid Ethernet station (MAC)
538    address.  "Vendor" is an expected vendor code (first three bytes,)
539    or a zero when nothing expected.  */
540 static int
541 valid_Ether_p (u_char const * addr, unsigned vendor)
542 {
543 #ifdef FE_DEBUG
544 	printf("fe?: validating %6D against %06x\n", addr, ":", vendor);
545 #endif
546 
547 	/* All zero is not allowed as a vendor code.  */
548 	if (addr[0] == 0 && addr[1] == 0 && addr[2] == 0) return 0;
549 
550 	switch (vendor) {
551 	    case 0x000000:
552 		/* Legal Ethernet address (stored in ROM) must have
553 		   its Group and Local bits cleared.  */
554 		if ((addr[0] & 0x03) != 0) return 0;
555 		break;
556 	    case 0x020000:
557 		/* Same as above, but a local address is allowed in
558                    this context.  */
559 		if ((addr[0] & 0x01) != 0) return 0;
560 		break;
561 	    default:
562 		/* Make sure the vendor part matches if one is given.  */
563 		if (   addr[0] != ((vendor >> 16) & 0xFF)
564 		    || addr[1] != ((vendor >>  8) & 0xFF)
565 		    || addr[2] != ((vendor      ) & 0xFF)) return 0;
566 		break;
567 	}
568 
569 	/* Host part must not be all-zeros nor all-ones.  */
570 	if (addr[3] == 0xFF && addr[4] == 0xFF && addr[5] == 0xFF) return 0;
571 	if (addr[3] == 0x00 && addr[4] == 0x00 && addr[5] == 0x00) return 0;
572 
573 	/* Given addr looks like an Ethernet address.  */
574 	return 1;
575 }
576 
577 /* Fill our softc struct with default value.  */
578 static void
579 fe_softc_defaults (struct fe_softc *sc)
580 {
581 	int i;
582 
583 	/* Initialize I/O address re-mapping table for the standard
584 	   (contiguous) register layout.  This routine doesn't use
585 	   ioaddr[], so the caller can safely override it after
586 	   calling fe_softc_defaults, if needed.  */
587 	for (i = 0; i < MAXREGISTERS; i++) sc->ioaddr[i] = sc->iobase + i;
588 
589 	/* Prepare for typical register prototypes.  We assume a
590            "typical" board has <32KB> of <fast> SRAM connected with a
591            <byte-wide> data lines.  */
592 	sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
593 	sc->proto_dlcr5 = 0;
594 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
595 		| FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
596 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
597 	sc->proto_bmpr13 = 0;
598 
599 	/* Assume the probe process (to be done later) is stable.  */
600 	sc->stability = 0;
601 
602 	/* A typical board needs no hooks.  */
603 	sc->init = NULL;
604 	sc->stop = NULL;
605 
606 	/* Assume the board has no software-controllable media selection.  */
607 	sc->mbitmap = MB_HM;
608 	sc->defmedia = MB_HM;
609 	sc->msel = NULL;
610 }
611 
612 /* Common error reporting routine used in probe routines for
613    "soft configured IRQ"-type boards.  */
614 static void
615 fe_irq_failure (char const *name, int unit, int irq, char const *list)
616 {
617 	printf("fe%d: %s board is detected, but %s IRQ was given\n",
618 	       unit, name, (irq == NO_IRQ ? "no" : "invalid"));
619 	if (list != NULL) {
620 		printf("fe%d: specify an IRQ from %s in kernel config\n",
621 		       unit, list);
622 	}
623 }
624 
625 /*
626  * Hardware (vendor) specific probe routines and hooks.
627  */
628 
629 /*
630  * Machine independent routines.
631  */
632 
633 /*
634  * Generic media selection scheme for MB86965 based boards.
635  */
636 static void
637 fe_msel_965 (struct fe_softc *sc)
638 {
639 	u_char b13;
640 
641 	/* Find the appropriate bits for BMPR13 tranceiver control.  */
642 	switch (IFM_SUBTYPE(sc->media.ifm_media)) {
643 	    case IFM_AUTO: b13 = FE_B13_PORT_AUTO | FE_B13_TPTYPE_UTP; break;
644 	    case IFM_10_T: b13 = FE_B13_PORT_TP   | FE_B13_TPTYPE_UTP; break;
645 	    default:       b13 = FE_B13_PORT_AUI;  break;
646 	}
647 
648 	/* Write it into the register.  It takes effect immediately.  */
649 	outb(sc->ioaddr[FE_BMPR13], sc->proto_bmpr13 | b13);
650 }
651 
652 /*
653  * Fujitsu MB86965 JLI mode support routines.
654  */
655 
656 /* Datasheet for 86965 explicitly states that it only supports serial
657  * EEPROM with 16 words (32 bytes) capacity.  (I.e., 93C06.)  However,
658  * ones with 64 words (128 bytes) are available in the marked, namely
659  * 93C46, and are also fully compatible with 86965.  It is known that
660  * some boards (e.g., ICL) actually have 93C46 on them and use extra
661  * storage to keep various config info.  */
662 #define JLI_EEPROM_SIZE	128
663 
664 /*
665  * Routines to read all bytes from the config EEPROM through MB86965A.
666  * It is a MicroWire (3-wire) serial EEPROM with 6-bit address.
667  * (93C06 or 93C46.)
668  */
669 static void
670 fe_strobe_eeprom_jli ( u_short bmpr16 )
671 {
672 	/*
673 	 * We must guarantee 1us (or more) interval to access slow
674 	 * EEPROMs.  The following redundant code provides enough
675 	 * delay with ISA timing.  (Even if the bus clock is "tuned.")
676 	 * Some modification will be needed on faster busses.
677 	 */
678 	outb( bmpr16, FE_B16_SELECT );
679 	outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
680 	outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
681 	outb( bmpr16, FE_B16_SELECT );
682 }
683 
684 static void
685 fe_read_eeprom_jli ( struct fe_softc * sc, u_char * data )
686 {
687 	u_short bmpr16 = sc->ioaddr[ FE_BMPR16 ];
688 	u_short bmpr17 = sc->ioaddr[ FE_BMPR17 ];
689 	u_char n, val, bit;
690 	u_char save16, save17;
691 
692 	/* Save the current value of the EEPROM interface registers.  */
693 	save16 = inb(bmpr16);
694 	save17 = inb(bmpr17);
695 
696 	/* Read bytes from EEPROM; two bytes per an iteration.  */
697 	for ( n = 0; n < JLI_EEPROM_SIZE / 2; n++ ) {
698 
699 		/* Reset the EEPROM interface.  */
700 		outb( bmpr16, 0x00 );
701 		outb( bmpr17, 0x00 );
702 
703 		/* Start EEPROM access.  */
704 		outb( bmpr16, FE_B16_SELECT );
705 		outb( bmpr17, FE_B17_DATA );
706 		fe_strobe_eeprom_jli( bmpr16 );
707 
708 		/* Pass the iteration count as well as a READ command.  */
709 		val = 0x80 | n;
710 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
711 			outb( bmpr17, ( val & bit ) ? FE_B17_DATA : 0 );
712 			fe_strobe_eeprom_jli( bmpr16 );
713 		}
714 		outb( bmpr17, 0x00 );
715 
716 		/* Read a byte.  */
717 		val = 0;
718 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
719 			fe_strobe_eeprom_jli( bmpr16 );
720 			if ( inb( bmpr17 ) & FE_B17_DATA ) {
721 				val |= bit;
722 			}
723 		}
724 		*data++ = val;
725 
726 		/* Read one more byte.  */
727 		val = 0;
728 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
729 			fe_strobe_eeprom_jli( bmpr16 );
730 			if ( inb( bmpr17 ) & FE_B17_DATA ) {
731 				val |= bit;
732 			}
733 		}
734 		*data++ = val;
735 	}
736 
737 #if 0
738 	/* Reset the EEPROM interface, again.  */
739 	outb( bmpr16, 0x00 );
740 	outb( bmpr17, 0x00 );
741 #else
742 	/* Make sure to restore the original value of EEPROM interface
743            registers, since we are not yet sure we have MB86965A on
744            the address.  */
745 	outb(bmpr17, save17);
746 	outb(bmpr16, save16);
747 #endif
748 
749 #if 1
750 	/* Report what we got.  */
751 	if (bootverbose) {
752 		int i;
753 		data -= JLI_EEPROM_SIZE;
754 		for (i = 0; i < JLI_EEPROM_SIZE; i += 16) {
755 			printf("fe%d: EEPROM(JLI):%3x: %16D\n",
756 			       sc->sc_unit, i, data + i, " ");
757 		}
758 	}
759 #endif
760 }
761 
762 static void
763 fe_init_jli (struct fe_softc * sc)
764 {
765 	/* "Reset" by writing into a magic location.  */
766 	DELAY(200);
767 	outb(sc->ioaddr[0x1E], inb(sc->ioaddr[0x1E]));
768 	DELAY(300);
769 }
770 
771 /*
772  * SSi 78Q8377A support routines.
773  */
774 
775 #define SSI_EEPROM_SIZE	512
776 #define SSI_DIN	0x01
777 #define SSI_DAT	0x01
778 #define SSI_CSL	0x02
779 #define SSI_CLK	0x04
780 #define SSI_EEP	0x10
781 
782 /*
783  * Routines to read all bytes from the config EEPROM through 78Q8377A.
784  * It is a MicroWire (3-wire) serial EEPROM with 8-bit address.  (I.e.,
785  * 93C56 or 93C66.)
786  *
787  * As I don't have SSi manuals, (hmm, an old song again!) I'm not exactly
788  * sure the following code is correct...  It is just stolen from the
789  * C-NET(98)P2 support routine in FreeBSD(98).
790  */
791 
792 static void
793 fe_read_eeprom_ssi (struct fe_softc *sc, u_char *data)
794 {
795 	u_short	bmpr12 = sc->ioaddr[FE_DLCR12];
796 	u_char val, bit;
797 	int n;
798 	u_char save6, save7, save12;
799 
800 	/* Save the current value for the DLCR registers we are about
801            to destroy.  */
802 	save6 = inb(sc->ioaddr[FE_DLCR6]);
803 	save7 = inb(sc->ioaddr[FE_DLCR7]);
804 
805 	/* Put the 78Q8377A into a state that we can access the EEPROM.  */
806 	outb(sc->ioaddr[FE_DLCR6],
807 	     FE_D6_BBW_WORD | FE_D6_SBW_WORD | FE_D6_DLC_DISABLE);
808 	outb(sc->ioaddr[FE_DLCR7],
809 	     FE_D7_BYTSWP_LH | FE_D7_RBS_BMPR | FE_D7_RDYPNS | FE_D7_POWER_UP);
810 
811 	/* Save the current value for the BMPR12 register, too.  */
812 	save12 = inb(bmpr12);
813 
814 	/* Read bytes from EEPROM; two bytes per an iteration.  */
815 	for ( n = 0; n < SSI_EEPROM_SIZE / 2; n++ ) {
816 
817 		/* Start EEPROM access  */
818 		outb(bmpr12, SSI_EEP);
819 		outb(bmpr12, SSI_EEP | SSI_CSL);
820 
821 		/* Send the following four bits to the EEPROM in the
822                    specified order: a dummy bit, a start bit, and
823                    command bits (10) for READ.  */
824 		outb(bmpr12, SSI_EEP | SSI_CSL                    );
825 		outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK          );	/* 0 */
826 		outb(bmpr12, SSI_EEP | SSI_CSL           | SSI_DAT);
827 		outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT);	/* 1 */
828 		outb(bmpr12, SSI_EEP | SSI_CSL           | SSI_DAT);
829 		outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK | SSI_DAT);	/* 1 */
830 		outb(bmpr12, SSI_EEP | SSI_CSL                    );
831 		outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK          );	/* 0 */
832 
833 		/* Pass the iteration count to the chip.  */
834 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
835 			val = ( n & bit ) ? SSI_DAT : 0;
836 			outb(bmpr12, SSI_EEP | SSI_CSL           | val);
837 			outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK | val);
838 		}
839 
840 		/* Read a byte.  */
841 		val = 0;
842 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
843 			outb(bmpr12, SSI_EEP | SSI_CSL);
844 			outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK);
845 			if (inb(bmpr12) & SSI_DIN) val |= bit;
846 		}
847 		*data++ = val;
848 
849 		/* Read one more byte.  */
850 		val = 0;
851 		for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
852 			outb(bmpr12, SSI_EEP | SSI_CSL);
853 			outb(bmpr12, SSI_EEP | SSI_CSL | SSI_CLK);
854 			if (inb(bmpr12) & SSI_DIN) val |= bit;
855 		}
856 		*data++ = val;
857 
858 		outb(bmpr12, SSI_EEP);
859 	}
860 
861 	/* Reset the EEPROM interface.  (For now.)  */
862 	outb( bmpr12, 0x00 );
863 
864 	/* Restore the saved register values, for the case that we
865            didn't have 78Q8377A at the given address.  */
866 	outb(bmpr12, save12);
867 	outb(sc->ioaddr[FE_DLCR7], save7);
868 	outb(sc->ioaddr[FE_DLCR6], save6);
869 
870 #if 1
871 	/* Report what we got.  */
872 	if (bootverbose) {
873 		int i;
874 		data -= SSI_EEPROM_SIZE;
875 		for (i = 0; i < SSI_EEPROM_SIZE; i += 16) {
876 			printf("fe%d: EEPROM(SSI):%3x: %16D\n",
877 			       sc->sc_unit, i, data + i, " ");
878 		}
879 	}
880 #endif
881 }
882 
883 #define	FE_SSI_EEP_IRQ		9	/* Irq ???		*/
884 #define	FE_SSI_EEP_ADDR		16	/* Station(MAC) address	*/
885 #define	FE_SSI_EEP_DUPLEX	25	/* Duplex mode ???	*/
886 
887 /*
888  * TDK/LANX boards support routines.
889  */
890 
891 /* AX012/AX013 equips an X24C01 chip, which has 128 bytes of memory cells.  */
892 #define LNX_EEPROM_SIZE	128
893 
894 /* Bit assignments and command definitions for the serial EEPROM
895    interface register in LANX ASIC.  */
896 #define LNX_SDA_HI	0x08	/* Drive SDA line high (logical 1.)	*/
897 #define LNX_SDA_LO	0x00	/* Drive SDA line low (logical 0.)	*/
898 #define LNX_SDA_FL	0x08	/* Float (don't drive) SDA line.	*/
899 #define LNX_SDA_IN	0x01	/* Mask for reading SDA line.		*/
900 #define LNX_CLK_HI	0x04	/* Drive clock line high (active.)	*/
901 #define LNX_CLK_LO	0x00	/* Drive clock line low (inactive.)	*/
902 
903 /* It is assumed that the CLK line is low and SDA is high (float) upon entry.  */
904 #define LNX_PH(D,K,N) \
905 	((LNX_SDA_##D | LNX_CLK_##K) << N)
906 #define LNX_CYCLE(D1,D2,D3,D4,K1,K2,K3,K4) \
907 	(LNX_PH(D1,K1,0)|LNX_PH(D2,K2,8)|LNX_PH(D3,K3,16)|LNX_PH(D4,K4,24))
908 
909 #define LNX_CYCLE_START	LNX_CYCLE(HI,LO,LO,HI, HI,HI,LO,LO)
910 #define LNX_CYCLE_STOP	LNX_CYCLE(LO,LO,HI,HI, LO,HI,HI,LO)
911 #define LNX_CYCLE_HI	LNX_CYCLE(HI,HI,HI,HI, LO,HI,LO,LO)
912 #define LNX_CYCLE_LO	LNX_CYCLE(LO,LO,LO,HI, LO,HI,LO,LO)
913 #define LNX_CYCLE_INIT	LNX_CYCLE(LO,HI,HI,HI, LO,LO,LO,LO)
914 
915 static void
916 fe_eeprom_cycle_lnx (u_short reg20, u_long cycle)
917 {
918 	outb(reg20, (cycle      ) & 0xFF);
919 	DELAY(15);
920 	outb(reg20, (cycle >>  8) & 0xFF);
921 	DELAY(15);
922 	outb(reg20, (cycle >> 16) & 0xFF);
923 	DELAY(15);
924 	outb(reg20, (cycle >> 24) & 0xFF);
925 	DELAY(15);
926 }
927 
928 static u_char
929 fe_eeprom_receive_lnx (u_short reg20)
930 {
931 	u_char dat;
932 
933 	outb(reg20, LNX_CLK_HI | LNX_SDA_FL);
934 	DELAY(15);
935 	dat = inb(reg20);
936 	outb(reg20, LNX_CLK_LO | LNX_SDA_FL);
937 	DELAY(15);
938 	return (dat & LNX_SDA_IN);
939 }
940 
941 static void
942 fe_read_eeprom_lnx (struct fe_softc *sc, u_char *data)
943 {
944 	int i;
945 	u_char n, bit, val;
946 	u_char save20;
947 	u_short reg20 = sc->ioaddr[0x14];
948 
949 	save20 = inb(reg20);
950 
951 	/* NOTE: DELAY() timing constants are approximately three
952            times longer (slower) than the required minimum.  This is
953            to guarantee a reliable operation under some tough
954            conditions...  Fortunately, this routine is only called
955            during the boot phase, so the speed is less important than
956            stability.  */
957 
958 #if 1
959 	/* Reset the X24C01's internal state machine and put it into
960 	   the IDLE state.  We usually don't need this, but *if*
961 	   someone (e.g., probe routine of other driver) write some
962 	   garbage into the register at 0x14, synchronization will be
963 	   lost, and the normal EEPROM access protocol won't work.
964 	   Moreover, as there are no easy way to reset, we need a
965 	   _manoeuvre_ here.  (It even lacks a reset pin, so pushing
966 	   the RESET button on the PC doesn't help!)  */
967 	fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_INIT);
968 	for (i = 0; i < 10; i++) {
969 		fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_START);
970 	}
971 	fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_STOP);
972 	DELAY(10000);
973 #endif
974 
975 	/* Issue a start condition.  */
976 	fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_START);
977 
978 	/* Send seven bits of the starting address (zero, in this
979 	   case) and a command bit for READ.  */
980 	val = 0x01;
981 	for (bit = 0x80; bit != 0x00; bit >>= 1) {
982 		if (val & bit) {
983 			fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_HI);
984 		} else {
985 			fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_LO);
986 		}
987 	}
988 
989 	/* Receive an ACK bit.  */
990 	if (fe_eeprom_receive_lnx(reg20)) {
991 		/* ACK was not received.  EEPROM is not present (i.e.,
992 		   this board was not a TDK/LANX) or not working
993 		   properly.  */
994 		if (bootverbose) {
995 			printf("fe%d: no ACK received from EEPROM(LNX)\n",
996 			       sc->sc_unit);
997 		}
998 		/* Clear the given buffer to indicate we could not get
999                    any info. and return.  */
1000 		bzero(data, LNX_EEPROM_SIZE);
1001 		goto RET;
1002 	}
1003 
1004 	/* Read bytes from EEPROM.  */
1005 	for (n = 0; n < LNX_EEPROM_SIZE; n++) {
1006 
1007 		/* Read a byte and store it into the buffer.  */
1008 		val = 0x00;
1009 		for (bit = 0x80; bit != 0x00; bit >>= 1) {
1010 			if (fe_eeprom_receive_lnx(reg20)) val |= bit;
1011 		}
1012 		*data++ = val;
1013 
1014 		/* Acknowledge if we have to read more.  */
1015 		if (n < LNX_EEPROM_SIZE - 1) {
1016 			fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_LO);
1017 		}
1018 	}
1019 
1020 	/* Issue a STOP condition, de-activating the clock line.
1021 	   It will be safer to keep the clock line low than to leave
1022 	   it high.  */
1023 	fe_eeprom_cycle_lnx(reg20, LNX_CYCLE_STOP);
1024 
1025     RET:
1026 	outb(reg20, save20);
1027 
1028 #if 1
1029 	/* Report what we got.  */
1030 	if (bootverbose) {
1031 		data -= LNX_EEPROM_SIZE;
1032 		for (i = 0; i < LNX_EEPROM_SIZE; i += 16) {
1033 			printf("fe%d: EEPROM(LNX):%3x: %16D\n",
1034 			       sc->sc_unit, i, data + i, " ");
1035 		}
1036 	}
1037 #endif
1038 }
1039 
1040 static void
1041 fe_init_lnx ( struct fe_softc * sc )
1042 {
1043 	/* Reset the 86960.  Do we need this?  FIXME.  */
1044 	outb(sc->ioaddr[0x12], 0x06);
1045 	DELAY(100);
1046 	outb(sc->ioaddr[0x12], 0x07);
1047 	DELAY(100);
1048 
1049 	/* Setup IRQ control register on the ASIC.  */
1050 	outb(sc->ioaddr[0x14], sc->priv_info);
1051 }
1052 
1053 /*
1054  * Ungermann-Bass boards support routine.
1055  */
1056 static void
1057 fe_init_ubn ( struct fe_softc * sc )
1058 {
1059  	/* Do we need this?  FIXME.  */
1060 	outb(sc->ioaddr[FE_DLCR7],
1061 		sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1062  	outb(sc->ioaddr[0x18], 0x00);
1063  	DELAY( 200 );
1064 
1065 	/* Setup IRQ control register on the ASIC.  */
1066 	outb(sc->ioaddr[0x14], sc->priv_info);
1067 }
1068 
1069 /*
1070  * Machine dependent probe routines.
1071  */
1072 
1073 #ifdef PC98
1074 static int
1075 fe_probe_fmv ( struct isa_device * dev, struct fe_softc * sc )
1076 {
1077 	/* PC-98 has no board of this architechture.  */
1078 	return 0;
1079 }
1080 
1081 /* ioaddr for RE1000/1000Plus - Very dirty!  */
1082 static u_short ioaddr_re1000[MAXREGISTERS] = {
1083 	0x0000, 0x0001, 0x0200, 0x0201, 0x0400, 0x0401, 0x0600, 0x0601,
1084 	0x0800, 0x0801, 0x0a00, 0x0a01, 0x0c00, 0x0c01, 0x0e00, 0x0e01,
1085 	0x1000, 0x1200, 0x1400, 0x1600, 0x1800, 0x1a00, 0x1c00, 0x1e00,
1086 	0x1001, 0x1201, 0x1401, 0x1601, 0x1801, 0x1a01, 0x1c01, 0x1e01,
1087 };
1088 
1089 /*
1090  * Probe and initialization for Allied-Telesis RE1000 series.
1091  */
1092 static void
1093 fe_init_re1000 ( struct fe_softc * sc )
1094 {
1095 	/* Setup IRQ control register on the ASIC.  */
1096 	outb(sc->ioaddr[FE_RE1000_IRQCONF], sc->priv_info);
1097 }
1098 
1099 static int
1100 fe_probe_re1000 ( struct isa_device * dev, struct fe_softc * sc )
1101 {
1102 	int i, n;
1103 	u_char sum;
1104 
1105 	static struct fe_simple_probe_struct probe_table [] = {
1106 		{ FE_DLCR2, 0x58, 0x00 },
1107 		{ FE_DLCR4, 0x08, 0x00 },
1108 		{ 0 }
1109 	};
1110 
1111 	/* See if the specified I/O address is possible for RE1000.  */
1112 	/* [01]D[02468ACE] are allowed.  */
1113 	if ((sc->iobase & ~0x10E) != 0xD0) return 0;
1114 
1115 	/* Setup an I/O address mapping table and some others.  */
1116 	fe_softc_defaults(sc);
1117 
1118 	/* Re-map ioaddr for RE1000.  */
1119 	for (i = 0; i < MAXREGISTERS; i++)
1120 		sc->ioaddr[i] = sc->iobase + ioaddr_re1000[i];
1121 
1122 	/* See if the card is on its address.  */
1123 	if (!fe_simple_probe(sc, probe_table)) return 0;
1124 
1125 	/* Get our station address from EEPROM.  */
1126 	inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
1127 
1128 	/* Make sure it is Allied-Telesis's.  */
1129 	if (!valid_Ether_p(sc->sc_enaddr, 0x0000F4)) return 0;
1130 #if 1
1131 	/* Calculate checksum.  */
1132 	sum = inb(sc->ioaddr[0x1e]);
1133 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1134 		sum ^= sc->sc_enaddr[i];
1135 	}
1136 	if (sum != 0) return 0;
1137 #endif
1138 	/* Setup the board type.  */
1139 	sc->typestr = "RE1000";
1140 
1141 	/* This looks like an RE1000 board.  It requires an
1142 	   explicit IRQ setting in config.  Make sure we have one,
1143 	   determining an appropriate value for the IRQ control
1144 	   register.  */
1145 	switch (dev->id_irq) {
1146 	  case IRQ3:  n = 0x10; break;
1147 	  case IRQ5:  n = 0x20; break;
1148 	  case IRQ6:  n = 0x40; break;
1149 	  case IRQ12: n = 0x80; break;
1150 	  default:
1151 		fe_irq_failure(sc->typestr,
1152 				sc->sc_unit, dev->id_irq, "3/5/6/12");
1153 		return 0;
1154 	}
1155 	sc->priv_info = inb(sc->ioaddr[FE_RE1000_IRQCONF]) & 0x0f | n;
1156 
1157 	/* Setup hooks.  We need a special initialization procedure.  */
1158 	sc->init = fe_init_re1000;
1159 
1160 	/* The I/O address range is fragmented in the RE1000.
1161 	   It occupies 2*16 I/O addresses, by the way.  */
1162 	return 2;
1163 }
1164 
1165 /* JLI sub-probe for Allied-Telesis RE1000Plus/ME1500 series.  */
1166 static u_short const *
1167 fe_probe_jli_re1000p (struct fe_softc * sc, u_char const * eeprom)
1168 {
1169 	int i;
1170 	static u_short const irqmaps_re1000p [4] = { IRQ3, IRQ5, IRQ6, IRQ12 };
1171 
1172 	/* Make sure the EEPROM contains Allied-Telesis bit pattern.  */
1173 	if (eeprom[1] != 0xFF) return NULL;
1174 	for (i =  2; i <  8; i++) if (eeprom[i] != 0xFF) return NULL;
1175 	for (i = 14; i < 24; i++) if (eeprom[i] != 0xFF) return NULL;
1176 
1177 	/* Get our station address from EEPROM, and make sure the
1178            EEPROM contains Allied-Telesis's address.  */
1179 	bcopy(eeprom+8, sc->sc_enaddr, ETHER_ADDR_LEN);
1180 	if (!valid_Ether_p(sc->sc_enaddr, 0x0000F4)) return NULL;
1181 
1182 	/* I don't know any sub-model identification.  */
1183 	sc->typestr = "RE1000Plus/ME1500";
1184 
1185 	/* Returns the IRQ table for the RE1000Plus.  */
1186 	return irqmaps_re1000p;
1187 }
1188 
1189 /*
1190  * Probe for Allied-Telesis RE1000Plus/ME1500 series.
1191  */
1192 static int
1193 fe_probe_jli (struct isa_device * dev, struct fe_softc * sc)
1194 {
1195 	int i, n;
1196 	int irq;
1197 	u_char eeprom [JLI_EEPROM_SIZE];
1198 	u_short const * irqmap;
1199 
1200 	static u_short const baseaddr [8] =
1201 		{ 0x1D6, 0x1D8, 0x1DA, 0x1D4, 0x0D4, 0x0D2, 0x0D8, 0x0D0 };
1202 	static struct fe_simple_probe_struct const probe_table [] = {
1203 	/*	{ FE_DLCR1,  0x20, 0x00 },	Doesn't work. */
1204 		{ FE_DLCR2,  0x50, 0x00 },
1205 		{ FE_DLCR4,  0x08, 0x00 },
1206 	/*	{ FE_DLCR5,  0x80, 0x00 },	Doesn't work. */
1207 #if 0
1208 		{ FE_BMPR16, 0x1B, 0x00 },
1209 		{ FE_BMPR17, 0x7F, 0x00 },
1210 #endif
1211 		{ 0 }
1212 	};
1213 
1214 	/*
1215 	 * See if the specified address is possible for MB86965A JLI mode.
1216 	 */
1217 	for (i = 0; i < 8; i++) {
1218 		if (baseaddr[i] == sc->iobase) break;
1219 	}
1220 	if (i == 8) return 0;
1221 
1222 	/* Fill the softc struct with reasonable default.  */
1223 	fe_softc_defaults(sc);
1224 
1225 	/* Re-map ioaddr for RE1000Plus.  */
1226 	for (i = 0; i < MAXREGISTERS; i++)
1227 		sc->ioaddr[i] = sc->iobase + ioaddr_re1000[i];
1228 
1229 	/*
1230 	 * We should test if MB86965A is on the base address now.
1231 	 * Unfortunately, it is very hard to probe it reliably, since
1232 	 * we have no way to reset the chip under software control.
1233 	 * On cold boot, we could check the "signature" bit patterns
1234 	 * described in the Fujitsu document.  On warm boot, however,
1235 	 * we can predict almost nothing about register values.
1236 	 */
1237 	if (!fe_simple_probe(sc, probe_table)) return 0;
1238 
1239 	/* Check if our I/O address matches config info on 86965.  */
1240 	n = (inb(sc->ioaddr[FE_BMPR19]) & FE_B19_ADDR) >> FE_B19_ADDR_SHIFT;
1241 	if (baseaddr[n] != sc->iobase) return 0;
1242 
1243 	/*
1244 	 * We are now almost sure we have an MB86965 at the given
1245 	 * address.  So, read EEPROM through it.  We have to write
1246 	 * into LSI registers to read from EEPROM.  I want to avoid it
1247 	 * at this stage, but I cannot test the presence of the chip
1248 	 * any further without reading EEPROM.  FIXME.
1249 	 */
1250 	fe_read_eeprom_jli(sc, eeprom);
1251 
1252 	/* Make sure that config info in EEPROM and 86965 agree.  */
1253 	if (eeprom[FE_EEPROM_CONF] != inb(sc->ioaddr[FE_BMPR19])) {
1254 		return 0;
1255 	}
1256 
1257 	/* Use 86965 media selection scheme, unless othewise
1258            specified.  It is "AUTO always" and "select with BMPR13".
1259            This behaviour covers most of the 86965 based board (as
1260            minimum requirements.)  It is backward compatible with
1261            previous versions, also.  */
1262 	sc->mbitmap = MB_HA;
1263 	sc->defmedia = MB_HA;
1264 	sc->msel = fe_msel_965;
1265 
1266 	/* Perform board-specific probe.  */
1267 	if ((irqmap = fe_probe_jli_re1000p(sc, eeprom)) == NULL) return 0;
1268 
1269 	/* Find the IRQ read from EEPROM.  */
1270 	n = (inb(sc->ioaddr[FE_BMPR19]) & FE_B19_IRQ) >> FE_B19_IRQ_SHIFT;
1271 	irq = irqmap[n];
1272 
1273 	/* Try to determine IRQ setting.  */
1274 	if (dev->id_irq == NO_IRQ && irq == NO_IRQ) {
1275 		/* The device must be configured with an explicit IRQ.  */
1276 		printf("fe%d: IRQ auto-detection does not work\n",
1277 		       sc->sc_unit);
1278 		return 0;
1279 	} else if (dev->id_irq == NO_IRQ && irq != NO_IRQ) {
1280 		/* Just use the probed IRQ value.  */
1281 		dev->id_irq = irq;
1282 	} else if (dev->id_irq != NO_IRQ && irq == NO_IRQ) {
1283 		/* No problem.  Go ahead.  */
1284 	} else if (dev->id_irq == irq) {
1285 		/* Good.  Go ahead.  */
1286 	} else {
1287 		/* User must be warned in this case.  */
1288 		sc->stability |= UNSTABLE_IRQ;
1289 	}
1290 
1291 	/* Setup a hook, which resets te 86965 when the driver is being
1292            initialized.  This may solve a nasty bug.  FIXME.  */
1293 	sc->init = fe_init_jli;
1294 
1295 	/* The I/O address range is fragmented in the RE1000Plus.
1296 	   It occupies 2*16 I/O addresses, by the way.  */
1297 	return 2;
1298 }
1299 
1300 /*
1301  * Probe and initialization for Contec C-NET(9N)E series.
1302  */
1303 
1304 /* TODO: Should be in "if_fereg.h" */
1305 #define FE_CNET9NE_INTR		0x10		/* Interrupt Mask? */
1306 
1307 static void
1308 fe_init_cnet9ne ( struct fe_softc * sc )
1309 {
1310 	/* Enable interrupt?  FIXME.  */
1311 	outb(sc->ioaddr[FE_CNET9NE_INTR], 0x10);
1312 }
1313 
1314 static int
1315 fe_probe_cnet9ne ( struct isa_device * dev, struct fe_softc * sc )
1316 {
1317 	int i;
1318 
1319 	static struct fe_simple_probe_struct probe_table [] = {
1320 		{ FE_DLCR2, 0x58, 0x00 },
1321 		{ FE_DLCR4, 0x08, 0x00 },
1322 		{ 0 }
1323 	};
1324 	static u_short ioaddr[MAXREGISTERS - 16] = {
1325 	/*	0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007,	*/
1326 	/*	0x008, 0x009, 0x00a, 0x00b, 0x00c, 0x00d, 0x00e, 0x00f,	*/
1327 		0x400, 0x402, 0x404, 0x406, 0x408, 0x40a, 0x40c, 0x40e,
1328 		0x401, 0x403, 0x405, 0x407, 0x409, 0x40b, 0x40d, 0x40f,
1329 	};
1330 
1331 	/* See if the specified I/O address is possible for C-NET(9N)E.  */
1332 	if (sc->iobase != 0x73D0) return 0;
1333 
1334 	/* Setup an I/O address mapping table and some others.  */
1335 	fe_softc_defaults(sc);
1336 
1337 	/* Re-map ioaddr for C-NET(9N)E.  */
1338 	for (i = 16; i < MAXREGISTERS; i++)
1339 		sc->ioaddr[i] = sc->iobase + ioaddr[i - 16];
1340 
1341 	/* See if the card is on its address.  */
1342 	if (!fe_simple_probe(sc, probe_table)) return 0;
1343 
1344 	/* Get our station address from EEPROM.  */
1345 	inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
1346 
1347 	/* Make sure it is Contec's.  */
1348 	if (!valid_Ether_p(sc->sc_enaddr, 0x00804C)) return 0;
1349 
1350 	/* Determine the card type.  */
1351 	if (sc->sc_enaddr[3] == 0x06) {
1352 		sc->typestr = "C-NET(9N)C";
1353 
1354 		/* We seems to need our own IDENT bits...  FIXME.  */
1355 		sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
1356 
1357 		/* C-NET(9N)C requires an explicit IRQ to work.  */
1358 		if (dev->id_irq == NO_IRQ) {
1359 			fe_irq_failure(sc->typestr, sc->sc_unit, NO_IRQ, NULL);
1360 			return 0;
1361 		}
1362 	} else {
1363 		sc->typestr = "C-NET(9N)E";
1364 
1365 		/* C-NET(9N)E works only IRQ5.  */
1366 		if (dev->id_irq != IRQ5) {
1367 			fe_irq_failure(sc->typestr,
1368 					sc->sc_unit, dev->id_irq, "5");
1369 			return 0;
1370 		}
1371 
1372 		/* We need an init hook to initialize ASIC before we start.  */
1373 		sc->init = fe_init_cnet9ne;
1374 	}
1375 
1376 	/* C-NET(9N)E has 64KB SRAM.  */
1377 	sc->proto_dlcr6 = FE_D6_BUFSIZ_64KB | FE_D6_TXBSIZ_2x4KB
1378 			| FE_D6_BBW_WORD | FE_D6_SBW_WORD | FE_D6_SRAM;
1379 
1380 	/* The I/O address range is fragmented in the C-NET(9N)E.
1381 	   This is the number of regs at iobase.  */
1382 	return 16;
1383 }
1384 
1385 /*
1386  * Probe for Contec C-NET(98)P2 series.
1387  * (Logitec LAN-98TP/LAN-98T25P - parhaps)
1388  */
1389 static int
1390 fe_probe_ssi (struct isa_device *dev, struct fe_softc *sc)
1391 {
1392 	u_char eeprom [SSI_EEPROM_SIZE];
1393 
1394 	static struct fe_simple_probe_struct probe_table [] = {
1395 		{ FE_DLCR2, 0x08, 0x00 },
1396 		{ FE_DLCR4, 0x08, 0x00 },
1397 		{ 0 }
1398 	};
1399 	static u_short const irqmap[] = {
1400 		/*                      INT0            INT1    INT2        */
1401 		NO_IRQ, NO_IRQ, NO_IRQ, IRQ3  , NO_IRQ, IRQ5  , IRQ6  , NO_IRQ,
1402 		NO_IRQ, IRQ9  , IRQ10 , NO_IRQ, IRQ12 , IRQ13 , NO_IRQ, NO_IRQ,
1403 		/*      INT3    INT41           INT5    INT6                */
1404 	};
1405 
1406 	/* See if the specified I/O address is possible for 78Q8377A.  */
1407 	/* [0-D]3D0 are allowed.  */
1408 	if ((sc->iobase & 0xFFF) != 0x3D0) return 0;	/* XXX */
1409 
1410 	/* Fill the softc struct with default values.  */
1411 	fe_softc_defaults(sc);
1412 
1413 	/* See if the card is on its address.  */
1414 	if (!fe_simple_probe(sc, probe_table)) return 0;
1415 
1416 	/* We now have to read the config EEPROM.  We should be very
1417            careful, since doing so destroys a register.  (Remember, we
1418            are not yet sure we have a C-NET(98)P2 board here.)  Don't
1419            remember to select BMPRs bofore reading EEPROM, since other
1420            register bank may be selected before the probe() is called.  */
1421 	fe_read_eeprom_ssi(sc, eeprom);
1422 
1423 	/* Make sure the Ethernet (MAC) station address is of Contec's.  */
1424 	if (!valid_Ether_p(eeprom+FE_SSI_EEP_ADDR, 0x00804C)) return 0;
1425 	bcopy(eeprom+FE_SSI_EEP_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN);
1426 
1427 	/* Setup the board type.  */
1428         sc->typestr = "C-NET(98)P2";
1429 
1430 	/* Get IRQ configuration from EEPROM.  */
1431 	dev->id_irq = irqmap[eeprom[FE_SSI_EEP_IRQ]];
1432 	if (dev->id_irq == NO_IRQ) {
1433 		fe_irq_failure(sc->typestr,
1434 				sc->sc_unit, dev->id_irq, "3/5/6/9/10/12/13");
1435 		return 0;
1436 	}
1437 
1438 	/* Get Duplex-mode configuration from EEPROM.  */
1439 	sc->proto_dlcr4 |= (eeprom[FE_SSI_EEP_DUPLEX] & FE_D4_DSC);
1440 
1441 	/* Fill softc struct accordingly.  */
1442 	sc->mbitmap = MB_HT;
1443 	sc->defmedia = MB_HT;
1444 
1445 	/* We have 16 registers.  */
1446 	return 16;
1447 }
1448 
1449 /*
1450  * Probe for TDK LAC-98012/013/025/9N011 - parhaps.
1451  */
1452 static int
1453 fe_probe_lnx (struct isa_device *dev, struct fe_softc *sc)
1454 {
1455 #ifndef FE_8BIT_SUPPORT
1456 	printf("fe%d: skip LAC-98012/013(only 16-bit cards are supported)\n",
1457 	       sc->sc_unit);
1458 	return 0;
1459 #else
1460 	int i;
1461 	u_char eeprom [LNX_EEPROM_SIZE];
1462 
1463 	static struct fe_simple_probe_struct probe_table [] = {
1464 		{ FE_DLCR2, 0x58, 0x00 },
1465 		{ FE_DLCR4, 0x08, 0x00 },
1466 		{ 0 }
1467 	};
1468 
1469 	/* See if the specified I/O address is possible for TDK/LANX boards.  */
1470 	/* 0D0, 4D0, 8D0, and CD0 are allowed.  */
1471 	if ((sc->iobase & ~0xC00) != 0xD0) return 0;
1472 
1473 	/* Fill the softc struct with default values.  */
1474 	fe_softc_defaults(sc);
1475 
1476 	/* Re-map ioaddr for LAC-98.
1477 	 *	0x000, 0x002, 0x004, 0x006, 0x008, 0x00a, 0x00c, 0x00e,
1478 	 *	0x100, 0x102, 0x104, 0x106, 0x108, 0x10a, 0x10c, 0x10e,
1479 	 *	0x200, 0x202, 0x204, 0x206, 0x208, 0x20a, 0x20c, 0x20e,
1480 	 *	0x300, 0x302, 0x304, 0x306, 0x308, 0x30a, 0x30c, 0x30e,
1481 	 */
1482 	for (i = 0; i < MAXREGISTERS; i++)
1483 		sc->ioaddr[i] = sc->iobase + ((i & 7) << 1) + ((i & 0x18) << 5);
1484 
1485 	/* See if the card is on its address.  */
1486 	if (!fe_simple_probe(sc, probe_table)) return 0;
1487 
1488 	/* We now have to read the config EEPROM.  We should be very
1489            careful, since doing so destroys a register.  (Remember, we
1490            are not yet sure we have a LAC-98012/98013 board here.)  */
1491 	fe_read_eeprom_lnx(sc, eeprom);
1492 
1493 	/* Make sure the Ethernet (MAC) station address is of TDK/LANX's.  */
1494 	if (!valid_Ether_p(eeprom, 0x008098)) return 0;
1495 	bcopy(eeprom, sc->sc_enaddr, ETHER_ADDR_LEN);
1496 
1497 	/* Setup the board type.  */
1498 	sc->typestr = "LAC-98012/98013";
1499 
1500 	/* This looks like a TDK/LANX board.  It requires an
1501 	   explicit IRQ setting in config.  Make sure we have one,
1502 	   determining an appropriate value for the IRQ control
1503 	   register.  */
1504 	switch (dev->id_irq) {
1505 	  case IRQ3 : sc->priv_info = 0x10 | LNX_CLK_LO | LNX_SDA_HI; break;
1506 	  case IRQ5 : sc->priv_info = 0x20 | LNX_CLK_LO | LNX_SDA_HI; break;
1507 	  case IRQ6 : sc->priv_info = 0x40 | LNX_CLK_LO | LNX_SDA_HI; break;
1508 	  case IRQ12: sc->priv_info = 0x80 | LNX_CLK_LO | LNX_SDA_HI; break;
1509 	  default:
1510 		fe_irq_failure(sc->typestr,
1511 				sc->sc_unit, dev->id_irq, "3/5/6/12");
1512 		return 0;
1513 	}
1514 
1515 	/* LAC-98's system bus width is 8-bit.  */
1516 	sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x2KB
1517 			| FE_D6_BBW_BYTE | FE_D6_SBW_BYTE | FE_D6_SRAM_150ns;
1518 
1519 	/* Setup hooks.  We need a special initialization procedure.  */
1520 	sc->init = fe_init_lnx;
1521 
1522 	/* The I/O address range is fragmented in the LAC-98.
1523 	   It occupies 16*4 I/O addresses, by the way.  */
1524 	return 16;
1525 #endif /* FE_8BIT_SUPPORT */
1526 }
1527 
1528 /*
1529  * Probe for Gateway Communications' old cards.
1530  * (both as Generic MB86960 probe routine)
1531  */
1532 static int
1533 fe_probe_gwy ( struct isa_device * dev, struct fe_softc * sc )
1534 {
1535 	static struct fe_simple_probe_struct probe_table [] = {
1536 	    /*	{ FE_DLCR2, 0x70, 0x00 }, */
1537 		{ FE_DLCR2, 0x58, 0x00 },
1538 		{ FE_DLCR4, 0x08, 0x00 },
1539 		{ 0 }
1540 	};
1541 
1542 	/* I'm not sure which address is possible, so accepts any.  FIXME.  */
1543 
1544 	/* Setup an I/O address mapping table and some others.  */
1545 	fe_softc_defaults(sc);
1546 
1547 	/* Does we need to re-map ioaddr?  FIXME.  */
1548 
1549 	/* See if the card is on its address.  */
1550 	if ( !fe_simple_probe( sc, probe_table ) ) return 0;
1551 
1552 	/* Get our station address from EEPROM. */
1553 	inblk( sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN );
1554 	if (!valid_Ether_p(sc->sc_enaddr, 0x000000)) return 0;
1555 
1556 	/* Determine the card type.  */
1557 	sc->typestr = "Generic MB86960 Ethernet";
1558 	if (valid_Ether_p(sc->sc_enaddr, 0x000061))
1559 		sc->typestr = "Gateway Ethernet (Fujitsu chipset)";
1560 
1561 	/* Gateway's board requires an explicit IRQ to work, since it
1562 	   is not possible to probe the setting of jumpers.  */
1563 	if (dev->id_irq == NO_IRQ) {
1564 		fe_irq_failure(sc->typestr, sc->sc_unit, NO_IRQ, NULL);
1565 		return 0;
1566 	}
1567 
1568 	/* We should change return value when re-mapping ioaddr.  FIXME. */
1569 	return 32;
1570 }
1571 
1572 /*
1573  * Probe for Ungermann-Bass Access/PC N98C+(Model 85152).
1574  */
1575 static int
1576 fe_probe_ubn (struct isa_device * dev, struct fe_softc * sc)
1577 {
1578 	u_char sum, save7;
1579 	int i;
1580 	static struct fe_simple_probe_struct const probe_table [] = {
1581 		{ FE_DLCR2, 0x58, 0x00 },
1582 		{ FE_DLCR4, 0x08, 0x00 },
1583 		{ 0 }
1584 	};
1585 
1586 	/* See if the specified I/O address is possible for Access/PC.  */
1587 	/* [01][048C]D0 are allowed.  */
1588 	if ((sc->iobase & ~0x1C00) != 0xD0) return 0;
1589 
1590 	/* Setup an I/O address mapping table and some others.  */
1591 	fe_softc_defaults(sc);
1592 
1593 	/* Re-map ioaddr for Access/PC N98C+.
1594 	 *	0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007,
1595 	 *	0x008, 0x009, 0x00a, 0x00b, 0x00c, 0x00d, 0x00e, 0x00f,
1596 	 *	0x200, 0x201, 0x202, 0x203, 0x204, 0x205, 0x206, 0x207,
1597 	 *	0x208, 0x209, 0x20a, 0x20b, 0x20c, 0x20d, 0x20e, 0x20f,
1598 	 */
1599 	for (i = 16; i < MAXREGISTERS; i++)
1600 		sc->ioaddr[i] = sc->iobase + 0x200 - 16 + i;
1601 
1602 	/* Simple probe.  */
1603 	if (!fe_simple_probe(sc, probe_table)) return 0;
1604 
1605 	/* NOTE: Access/NOTE N98 sometimes freeze when reading station
1606 	   address.  In case of using it togather with C-NET(9N)C,
1607 	   this problem usually happens.
1608 	   Writing DLCR7 prevents freezing, but I don't know why.  FIXME.  */
1609 
1610 	/* Save the current value for the DLCR7 register we are about
1611 	   to destroy.  */
1612 	save7 = inb(sc->ioaddr[FE_DLCR7]);
1613 	outb(sc->ioaddr[FE_DLCR7],
1614 		sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP);
1615 
1616 	/* Get our station address form ID ROM and make sure it is UBN's.  */
1617 	inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
1618 	if (!valid_Ether_p(sc->sc_enaddr, 0x00DD01)) goto fail_ubn;
1619 #if 1
1620 	/* Calculate checksum.  */
1621 	sum = inb(sc->ioaddr[0x1e]);
1622 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
1623 		sum ^= sc->sc_enaddr[i];
1624 	}
1625 	if (sum != 0) goto fail_ubn;
1626 #endif
1627 	/* Setup the board type.  */
1628 	sc->typestr = "Access/PC";
1629 
1630 	/* This looks like an AccessPC/N98C+ board.  It requires an
1631 	   explicit IRQ setting in config.  Make sure we have one,
1632 	   determining an appropriate value for the IRQ control
1633 	   register.  */
1634 	switch (dev->id_irq) {
1635 	  case IRQ3:  sc->priv_info = 0x01; break;
1636 	  case IRQ5:  sc->priv_info = 0x02; break;
1637 	  case IRQ6:  sc->priv_info = 0x04; break;
1638 	  case IRQ12: sc->priv_info = 0x08; break;
1639 	  default:
1640 		fe_irq_failure(sc->typestr,
1641 				sc->sc_unit, dev->id_irq, "3/5/6/12");
1642 		goto fail_ubn;
1643 	}
1644 
1645 	/* Setup hooks.  We need a special initialization procedure.  */
1646 	sc->init = fe_init_ubn;
1647 
1648 	/* The I/O address range is fragmented in the Access/PC N98C+.
1649 	   This is the number of regs at iobase.  */
1650 	return 16;
1651 
1652 fail_ubn:
1653 	outb(sc->ioaddr[FE_DLCR7], save7);
1654 	return 0;
1655 }
1656 
1657 #else	/* !PC98 */
1658 /*
1659  * Probe and initialization for Fujitsu FMV-180 series boards
1660  */
1661 
1662 static void
1663 fe_init_fmv (struct fe_softc *sc)
1664 {
1665 	/* Initialize ASIC.  */
1666 	outb( sc->ioaddr[ FE_FMV3 ], 0 );
1667 	outb( sc->ioaddr[ FE_FMV10 ], 0 );
1668 
1669 #if 0
1670 	/* "Refresh" hardware configuration.  FIXME.  */
1671 	outb( sc->ioaddr[ FE_FMV2 ], inb( sc->ioaddr[ FE_FMV2 ] ) );
1672 #endif
1673 
1674 	/* Turn the "master interrupt control" flag of ASIC on.  */
1675 	outb( sc->ioaddr[ FE_FMV3 ], FE_FMV3_IRQENB );
1676 }
1677 
1678 static void
1679 fe_msel_fmv184 (struct fe_softc *sc)
1680 {
1681 	u_char port;
1682 
1683 	/* FMV-184 has a special "register" to switch between AUI/BNC.
1684 	   Determine the value to write into the register, based on the
1685 	   user-specified media selection.  */
1686 	port = (IFM_SUBTYPE(sc->media.ifm_media) == IFM_10_2) ? 0x00 : 0x01;
1687 
1688 	/* The register is #5 on exntesion register bank...
1689 	   (Details of the register layout is not yet discovered.)  */
1690 	outb(sc->ioaddr[0x1B], 0x46);	/* ??? */
1691 	outb(sc->ioaddr[0x1E], 0x04);	/* select ex-reg #4.  */
1692 	outb(sc->ioaddr[0x1F], 0xC8);	/* ??? */
1693 	outb(sc->ioaddr[0x1E], 0x05);	/* select ex-reg #5.  */
1694 	outb(sc->ioaddr[0x1F], port);	/* Switch the media.  */
1695 	outb(sc->ioaddr[0x1E], 0x04);	/* select ex-reg #4.  */
1696 	outb(sc->ioaddr[0x1F], 0x00);	/* ??? */
1697 	outb(sc->ioaddr[0x1B], 0x00);	/* ??? */
1698 
1699 	/* Make sure to select "external tranceiver" on MB86964.  */
1700 	outb(sc->ioaddr[FE_BMPR13], sc->proto_bmpr13 | FE_B13_PORT_AUI);
1701 }
1702 
1703 static int
1704 fe_probe_fmv ( struct isa_device * dev, struct fe_softc * sc )
1705 {
1706 	int n;
1707 
1708 	static u_short const irqmap [ 4 ] =
1709 		{ IRQ3,  IRQ7,  IRQ10, IRQ15 };
1710 
1711 	static struct fe_simple_probe_struct const probe_table [] = {
1712 		{ FE_DLCR2, 0x71, 0x00 },
1713 		{ FE_DLCR4, 0x08, 0x00 },
1714 
1715 		{ FE_FMV0, 0x78, 0x50 },	/* ERRDY+PRRDY */
1716 		{ FE_FMV1, 0xB0, 0x00 },	/* FMV-183/4 has 0x48 bits. */
1717 		{ FE_FMV3, 0x7F, 0x00 },
1718 
1719 		{ 0 }
1720 	};
1721 
1722 	/* Board subtypes; it lists known FMV-180 variants.  */
1723 	struct subtype {
1724 		u_short mcode;
1725 		u_short mbitmap;
1726 		u_short defmedia;
1727 		char const * str;
1728 	};
1729 	static struct subtype const typelist [] = {
1730 	    { 0x0005, MB_HA|MB_HT|MB_H5, MB_HA, "FMV-181"		},
1731 	    { 0x0105, MB_HA|MB_HT|MB_H5, MB_HA, "FMV-181A"		},
1732 	    { 0x0003, MB_HM,             MB_HM, "FMV-182"		},
1733 	    { 0x0103, MB_HM,             MB_HM, "FMV-182A"		},
1734 	    { 0x0804, MB_HT,             MB_HT, "FMV-183"		},
1735 	    { 0x0C04, MB_HT,             MB_HT, "FMV-183 (on-board)"	},
1736 	    { 0x0803, MB_H2|MB_H5,       MB_H2, "FMV-184"		},
1737 	    { 0,      MB_HA,             MB_HA, "unknown FMV-180 (?)"	},
1738 	};
1739 	struct subtype const * type;
1740 
1741 	/* Media indicator and "Hardware revision ID"  */
1742 	u_short mcode;
1743 
1744 	/* See if the specified address is possible for FMV-180
1745            series.  220, 240, 260, 280, 2A0, 2C0, 300, and 340 are
1746            allowed for all boards, and 200, 2E0, 320, 360, 380, 3A0,
1747            3C0, and 3E0 for PnP boards.  */
1748 	if ((sc->iobase & ~0x1E0) != 0x200) return 0;
1749 
1750 	/* Setup an I/O address mapping table and some others.  */
1751 	fe_softc_defaults(sc);
1752 
1753 	/* Simple probe.  */
1754 	if (!fe_simple_probe(sc, probe_table)) return 0;
1755 
1756 	/* Get our station address from EEPROM, and make sure it is
1757            Fujitsu's.  */
1758 	inblk(sc, FE_FMV4, sc->sc_enaddr, ETHER_ADDR_LEN);
1759 	if (!valid_Ether_p(sc->sc_enaddr, 0x00000E)) return 0;
1760 
1761 	/* Find the supported media and "hardware revision" to know
1762            the model identification.  */
1763 	mcode = (inb(sc->ioaddr[FE_FMV0]) & FE_FMV0_MEDIA)
1764 	     | ((inb(sc->ioaddr[FE_FMV1]) & FE_FMV1_REV) << 8);
1765 
1766 	/* Determine the card type.  */
1767 	for (type = typelist; type->mcode != 0; type++) {
1768 		if (type->mcode == mcode) break;
1769 	}
1770 	if (type->mcode == 0) {
1771 	  	/* Unknown card type...  Hope the driver works.  */
1772 		sc->stability |= UNSTABLE_TYPE;
1773 		if (bootverbose) {
1774 			printf("fe%d: unknown config: %x-%x-%x-%x\n",
1775 			       sc->sc_unit,
1776 			       inb(sc->ioaddr[FE_FMV0]),
1777 			       inb(sc->ioaddr[FE_FMV1]),
1778 			       inb(sc->ioaddr[FE_FMV2]),
1779 			       inb(sc->ioaddr[FE_FMV3]));
1780 		}
1781 	}
1782 
1783 	/* Setup the board type and media information.  */
1784 	sc->typestr = type->str;
1785 	sc->mbitmap = type->mbitmap;
1786 	sc->defmedia = type->defmedia;
1787 	sc->msel = fe_msel_965;
1788 
1789 	if (type->mbitmap == (MB_H2 | MB_H5)) {
1790 		/* FMV184 requires a special media selection procedure.  */
1791 		sc->msel = fe_msel_fmv184;
1792 	}
1793 
1794 	/*
1795 	 * An FMV-180 has been probed.
1796 	 * Determine which IRQ to be used.
1797 	 *
1798 	 * In this version, we give a priority to the kernel config file.
1799 	 * If the EEPROM and config don't match, say it to the user for
1800 	 * an attention.
1801 	 */
1802 	n = ( inb( sc->ioaddr[ FE_FMV2 ] ) & FE_FMV2_IRS )
1803 		>> FE_FMV2_IRS_SHIFT;
1804 	if ( dev->id_irq == NO_IRQ ) {
1805 		/* Just use the probed value.  */
1806 		dev->id_irq = irqmap[ n ];
1807 	} else if ( dev->id_irq != irqmap[ n ] ) {
1808 		/* Don't match.  */
1809 		sc->stability |= UNSTABLE_IRQ;
1810 	}
1811 
1812 	/* We need an init hook to initialize ASIC before we start.  */
1813 	sc->init = fe_init_fmv;
1814 
1815 	/*
1816 	 * That's all.  FMV-180 occupies 32 I/O addresses, by the way.
1817 	 */
1818 	return 32;
1819 }
1820 
1821 /*
1822  * Fujitsu MB86965 JLI mode probe routines.
1823  *
1824  * 86965 has a special operating mode called JLI (mode 0), under which
1825  * the chip interfaces with ISA bus with a software-programmable
1826  * configuration.  (The Fujitsu document calls the feature "Plug and
1827  * play," but it is not compatible with the ISA-PnP spec. designed by
1828  * Intel and Microsoft.)  Ethernet cards designed to use JLI are
1829  * almost same, but there are two things which require board-specific
1830  * probe routines: EEPROM layout and IRQ pin connection.
1831  *
1832  * JLI provides a handy way to access EEPROM which should contains the
1833  * chip configuration information (such as I/O port address) as well
1834  * as Ethernet station (MAC) address.  The chip configuration info. is
1835  * stored on a fixed location.  However, the station address can be
1836  * located anywhere in the EEPROM; it is up to the board designer to
1837  * determine the location.  (The manual just says "somewhere in the
1838  * EEPROM.")  The fe driver must somehow find out the correct
1839  * location.
1840  *
1841  * Another problem resides in the IRQ pin connection.  JLI provides a
1842  * user to choose an IRQ from up to four predefined IRQs.  The 86965
1843  * chip has a register to select one out of the four possibilities.
1844  * However, the selection is against the four IRQ pins on the chip.
1845  * (So-called IRQ-A, -B, -C and -D.)  It is (again) up to the board
1846  * designer to determine which pin to connect which IRQ line on the
1847  * ISA bus.  We need a vendor (or model, for some vendor) specific IRQ
1848  * mapping table.
1849  *
1850  * The routine fe_probe_jli() provides all probe and initialization
1851  * processes which are common to all JLI implementation, and sub-probe
1852  * routines supply board-specific actions.
1853  *
1854  * JLI sub-probe routine has the following template:
1855  *
1856  *	u_short const * func (struct fe_softc * sc, u_char const * eeprom);
1857  *
1858  * where eeprom is a pointer to an array of 32 byte data read from the
1859  * config EEPROM on the board.  It retuns an IRQ mapping table for the
1860  * board, when the corresponding implementation is detected.  It
1861  * returns a NULL otherwise.
1862  *
1863  * Primary purpose of the functin is to analize the config EEPROM,
1864  * determine if it matches with the pattern of that of supported card,
1865  * and extract necessary information from it.  One of the information
1866  * expected to be extracted from EEPROM is the Ethernet station (MAC)
1867  * address, which must be set to the softc table of the interface by
1868  * the board-specific routine.
1869  */
1870 
1871 /* JLI sub-probe for Allied-Telesyn/Allied-Telesis AT1700/RE2000 series.  */
1872 static u_short const *
1873 fe_probe_jli_ati (struct fe_softc * sc, u_char const * eeprom)
1874 {
1875 	int i;
1876 	static u_short const irqmaps_ati [4][4] =
1877 	{
1878 		{ IRQ3,  IRQ4,  IRQ5,  IRQ9  },
1879 		{ IRQ10, IRQ11, IRQ12, IRQ15 },
1880 		{ IRQ3,  IRQ11, IRQ5,  IRQ15 },
1881 		{ IRQ10, IRQ11, IRQ14, IRQ15 },
1882 	};
1883 
1884 	/* Make sure the EEPROM contains Allied-Telesis/Allied-Telesyn
1885 	   bit pattern.  */
1886 	if (eeprom[1] != 0x00) return NULL;
1887 	for (i =  2; i <  8; i++) if (eeprom[i] != 0xFF) return NULL;
1888 	for (i = 14; i < 24; i++) if (eeprom[i] != 0xFF) return NULL;
1889 
1890 	/* Get our station address from EEPROM, and make sure the
1891            EEPROM contains ATI's address.  */
1892 	bcopy(eeprom+8, sc->sc_enaddr, ETHER_ADDR_LEN);
1893 	if (!valid_Ether_p(sc->sc_enaddr, 0x0000F4)) return NULL;
1894 
1895 	/*
1896 	 * The following model identification codes are stolen
1897 	 * from the NetBSD port of the fe driver.  My reviewers
1898 	 * suggested minor revision.
1899 	 */
1900 
1901 	/* Determine the card type.  */
1902 	switch (eeprom[FE_ATI_EEP_MODEL]) {
1903 	  case FE_ATI_MODEL_AT1700T:
1904 		sc->typestr = "AT-1700T/RE2001";
1905 		sc->mbitmap = MB_HT;
1906 		sc->defmedia = MB_HT;
1907 		break;
1908 	  case FE_ATI_MODEL_AT1700BT:
1909 		sc->typestr = "AT-1700BT/RE2003";
1910 		sc->mbitmap = MB_HA | MB_HT | MB_H2;
1911 		break;
1912 	  case FE_ATI_MODEL_AT1700FT:
1913 		sc->typestr = "AT-1700FT/RE2009";
1914 		sc->mbitmap = MB_HA | MB_HT | MB_HF;
1915 		break;
1916 	  case FE_ATI_MODEL_AT1700AT:
1917 		sc->typestr = "AT-1700AT/RE2005";
1918 		sc->mbitmap = MB_HA | MB_HT | MB_H5;
1919 		break;
1920 	  default:
1921 		sc->typestr = "unknown AT-1700/RE2000";
1922 		sc->stability |= UNSTABLE_TYPE | UNSTABLE_IRQ;
1923 		break;
1924 	}
1925 
1926 #if 0
1927 	/* Should we extract default media from eeprom?  Linux driver
1928 	   for AT1700 does it, although previous releases of FreeBSD
1929 	   don't.  FIXME.  */
1930 	/* Determine the default media selection from the config
1931            EEPROM.  The byte at offset EEP_MEDIA is believed to
1932            contain BMPR13 value to be set.  We just ignore STP bit or
1933            squelch bit, since we don't support those.  (It is
1934            intentional.)  */
1935 	switch (eeprom[FE_ATI_EEP_MEDIA] & FE_B13_PORT) {
1936 	    case FE_B13_AUTO:
1937 		sc->defmedia = MB_HA;
1938 		break;
1939 	    case FE_B13_TP:
1940 		sc->defmedia = MB_HT;
1941 		break;
1942 	    case FE_B13_AUI:
1943 		sc->defmedia = sc->mbitmap & (MB_H2|MB_H5|MB_H5); /*XXX*/
1944 		break;
1945 	    default:
1946 		sc->defmedia = MB_HA;
1947 		break;
1948 	}
1949 
1950 	/* Make sure the default media is compatible with the supported
1951 	   ones.  */
1952 	if ((sc->defmedia & sc->mbitmap) == 0) {
1953 		if (sc->defmedia == MB_HA) {
1954 			sc->defmedia = MB_HT;
1955 		} else {
1956 			sc->defmedia = MB_HA;
1957 		}
1958 	}
1959 #endif
1960 
1961 	/*
1962 	 * Try to determine IRQ settings.
1963 	 * Different models use different ranges of IRQs.
1964 	 */
1965 	switch ((eeprom[FE_ATI_EEP_REVISION] & 0xf0)
1966 	       |(eeprom[FE_ATI_EEP_MAGIC]    & 0x04)) {
1967 	    case 0x30: case 0x34: return irqmaps_ati[3];
1968 	    case 0x10: case 0x14:
1969 	    case 0x50: case 0x54: return irqmaps_ati[2];
1970 	    case 0x44: case 0x64: return irqmaps_ati[1];
1971 	    default:		  return irqmaps_ati[0];
1972 	}
1973 }
1974 
1975 /* JLI sub-probe and msel hook for ICL Ethernet.  */
1976 
1977 static void
1978 fe_msel_icl (struct fe_softc *sc)
1979 {
1980 	u_char d4;
1981 
1982 	/* Switch between UTP and "external tranceiver" as always.  */
1983 	fe_msel_965(sc);
1984 
1985 	/* The board needs one more bit (on DLCR4) be set appropriately.  */
1986 	if (IFM_SUBTYPE(sc->media.ifm_media) == IFM_10_5) {
1987 		d4 = sc->proto_dlcr4 | FE_D4_CNTRL;
1988 	} else {
1989 		d4 = sc->proto_dlcr4 & ~FE_D4_CNTRL;
1990 	}
1991 	outb(sc->ioaddr[FE_DLCR4], d4);
1992 }
1993 
1994 static u_short const *
1995 fe_probe_jli_icl (struct fe_softc * sc, u_char const * eeprom)
1996 {
1997 	int i;
1998 	u_short defmedia;
1999 	u_char d6;
2000 	static u_short const irqmap_icl [4] = { IRQ9, IRQ10, IRQ5, IRQ15 };
2001 
2002 	/* Make sure the EEPROM contains ICL bit pattern.  */
2003 	for (i = 24; i < 39; i++) {
2004 	    if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
2005 	}
2006 	for (i = 112; i < 122; i++) {
2007 	    if (eeprom[i] != 0x20 && (eeprom[i] & 0xF0) != 0x30) return NULL;
2008 	}
2009 
2010 	/* Make sure the EEPROM contains ICL's permanent station
2011            address.  If it isn't, probably this board is not an
2012            ICL's.  */
2013 	if (!valid_Ether_p(eeprom+122, 0x00004B)) return NULL;
2014 
2015 	/* Check if the "configured" Ethernet address in the EEPROM is
2016 	   valid.  Use it if it is, or use the "permanent" address instead.  */
2017 	if (valid_Ether_p(eeprom+4, 0x020000)) {
2018 		/* The configured address is valid.  Use it.  */
2019 		bcopy(eeprom+4, sc->sc_enaddr, ETHER_ADDR_LEN);
2020 	} else {
2021 		/* The configured address is invalid.  Use permanent.  */
2022 		bcopy(eeprom+122, sc->sc_enaddr, ETHER_ADDR_LEN);
2023 	}
2024 
2025 	/* Determine model and supported media.  */
2026 	switch (eeprom[0x5E]) {
2027 	    case 0:
2028 	        sc->typestr = "EtherTeam16i/COMBO";
2029 	        sc->mbitmap = MB_HA | MB_HT | MB_H5 | MB_H2;
2030 		break;
2031 	    case 1:
2032 		sc->typestr = "EtherTeam16i/TP";
2033 	        sc->mbitmap = MB_HT;
2034 		break;
2035 	    case 2:
2036 		sc->typestr = "EtherTeam16i/ErgoPro";
2037 		sc->mbitmap = MB_HA | MB_HT | MB_H5;
2038 		break;
2039 	    case 4:
2040 		sc->typestr = "EtherTeam16i/DUO";
2041 		sc->mbitmap = MB_HA | MB_HT | MB_H2;
2042 		break;
2043 	    default:
2044 		sc->typestr = "EtherTeam16i";
2045 		sc->stability |= UNSTABLE_TYPE;
2046 		if (bootverbose) {
2047 		    printf("fe%d: unknown model code %02x for EtherTeam16i\n",
2048 			   sc->sc_unit, eeprom[0x5E]);
2049 		}
2050 		break;
2051 	}
2052 
2053 	/* I'm not sure the following msel hook is required by all
2054            models or COMBO only...  FIXME.  */
2055 	sc->msel = fe_msel_icl;
2056 
2057 	/* Make the configured media selection the default media.  */
2058 	switch (eeprom[0x28]) {
2059 	    case 0: defmedia = MB_HA; break;
2060 	    case 1: defmedia = MB_H5; break;
2061 	    case 2: defmedia = MB_HT; break;
2062 	    case 3: defmedia = MB_H2; break;
2063 	    default:
2064 		if (bootverbose) {
2065 			printf("fe%d: unknown default media: %02x\n",
2066 			       sc->sc_unit, eeprom[0x28]);
2067 		}
2068 		defmedia = MB_HA;
2069 		break;
2070 	}
2071 
2072 	/* Make sure the default media is compatible with the
2073 	   supported media.  */
2074 	if ((defmedia & sc->mbitmap) == 0) {
2075 		if (bootverbose) {
2076 			printf("fe%d: default media adjusted\n", sc->sc_unit);
2077 		}
2078 		defmedia = sc->mbitmap;
2079 	}
2080 
2081 	/* Keep the determined default media.  */
2082 	sc->defmedia = defmedia;
2083 
2084 	/* ICL has "fat" models.  We have to program 86965 to properly
2085 	   reflect the hardware.  */
2086 	d6 = sc->proto_dlcr6 & ~(FE_D6_BUFSIZ | FE_D6_BBW);
2087 	switch ((eeprom[0x61] << 8) | eeprom[0x60]) {
2088 	    case 0x2008: d6 |= FE_D6_BUFSIZ_32KB | FE_D6_BBW_BYTE; break;
2089 	    case 0x4010: d6 |= FE_D6_BUFSIZ_64KB | FE_D6_BBW_WORD; break;
2090 	    default:
2091 		/* We can't support it, since we don't know which bits
2092 		   to set in DLCR6.  */
2093 		printf("fe%d: unknown SRAM config for ICL\n", sc->sc_unit);
2094 		return NULL;
2095 	}
2096 	sc->proto_dlcr6 = d6;
2097 
2098 	/* Returns the IRQ table for the ICL board.  */
2099 	return irqmap_icl;
2100 }
2101 
2102 /* JLI sub-probe for RATOC REX-5586/5587.  */
2103 static u_short const *
2104 fe_probe_jli_rex (struct fe_softc * sc, u_char const * eeprom)
2105 {
2106 	int i;
2107 	static u_short const irqmap_rex [4] = { IRQ3, IRQ4, IRQ5, NO_IRQ };
2108 
2109 	/* Make sure the EEPROM contains RATOC's config pattern.  */
2110 	if (eeprom[1] != eeprom[0]) return NULL;
2111 	for (i = 8; i < 32; i++) if (eeprom[i] != 0xFF) return NULL;
2112 
2113 	/* Get our station address from EEPROM.  Note that RATOC
2114 	   stores it "byte-swapped" in each word.  (I don't know why.)
2115 	   So, we just can't use bcopy().*/
2116 	sc->sc_enaddr[0] = eeprom[3];
2117 	sc->sc_enaddr[1] = eeprom[2];
2118 	sc->sc_enaddr[2] = eeprom[5];
2119 	sc->sc_enaddr[3] = eeprom[4];
2120 	sc->sc_enaddr[4] = eeprom[7];
2121 	sc->sc_enaddr[5] = eeprom[6];
2122 
2123 	/* Make sure the EEPROM contains RATOC's station address.  */
2124 	if (!valid_Ether_p(sc->sc_enaddr, 0x00C0D0)) return NULL;
2125 
2126 	/* I don't know any sub-model identification.  */
2127 	sc->typestr = "REX-5586/5587";
2128 
2129 	/* Returns the IRQ for the RATOC board.  */
2130 	return irqmap_rex;
2131 }
2132 
2133 /* JLI sub-probe for Unknown board.  */
2134 static u_short const *
2135 fe_probe_jli_unk (struct fe_softc * sc, u_char const * eeprom)
2136 {
2137 	int i, n, romsize;
2138 	static u_short const irqmap [4] = { NO_IRQ, NO_IRQ, NO_IRQ, NO_IRQ };
2139 
2140 	/* The generic JLI probe considered this board has an 86965
2141 	   in JLI mode, but any other board-specific routines could
2142 	   not find the matching implementation.  So, we "guess" the
2143 	   location by looking for a bit pattern which looks like a
2144 	   MAC address.  */
2145 
2146 	/* Determine how large the EEPROM is.  */
2147 	for (romsize = JLI_EEPROM_SIZE/2; romsize > 16; romsize >>= 1) {
2148 		for (i = 0; i < romsize; i++) {
2149 			if (eeprom[i] != eeprom[i+romsize]) break;
2150 		}
2151 		if (i < romsize) break;
2152 	}
2153 	romsize <<= 1;
2154 
2155 	/* Look for a bit pattern which looks like a MAC address.  */
2156 	for (n = 2; n <= romsize - ETHER_ADDR_LEN; n += 2) {
2157 		if (!valid_Ether_p(eeprom + n, 0x000000)) continue;
2158 	}
2159 
2160 	/* If no reasonable address was found, we can't go further.  */
2161 	if (n > romsize - ETHER_ADDR_LEN) return NULL;
2162 
2163 	/* Extract our (guessed) station address.  */
2164 	bcopy(eeprom+n, sc->sc_enaddr, ETHER_ADDR_LEN);
2165 
2166 	/* We are not sure what type of board it is... */
2167 	sc->typestr = "(unknown JLI)";
2168 	sc->stability |= UNSTABLE_TYPE | UNSTABLE_MAC;
2169 
2170 	/* Returns the totally unknown IRQ mapping table.  */
2171 	return irqmap;
2172 }
2173 
2174 /*
2175  * Probe and initialization for all JLI implementations.
2176  */
2177 
2178 static int
2179 fe_probe_jli (struct isa_device * dev, struct fe_softc * sc)
2180 {
2181 	int i, n;
2182 	int irq;
2183 	u_char eeprom [JLI_EEPROM_SIZE];
2184 	u_short const * irqmap;
2185 
2186 	static u_short const baseaddr [8] =
2187 		{ 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
2188 	static struct fe_simple_probe_struct const probe_table [] = {
2189 		{ FE_DLCR1,  0x20, 0x00 },
2190 		{ FE_DLCR2,  0x50, 0x00 },
2191 		{ FE_DLCR4,  0x08, 0x00 },
2192 		{ FE_DLCR5,  0x80, 0x00 },
2193 #if 0
2194 		{ FE_BMPR16, 0x1B, 0x00 },
2195 		{ FE_BMPR17, 0x7F, 0x00 },
2196 #endif
2197 		{ 0 }
2198 	};
2199 
2200 	/*
2201 	 * See if the specified address is possible for MB86965A JLI mode.
2202 	 */
2203 	for (i = 0; i < 8; i++) {
2204 		if (baseaddr[i] == sc->iobase) break;
2205 	}
2206 	if (i == 8) return 0;
2207 
2208 	/* Fill the softc struct with reasonable default.  */
2209 	fe_softc_defaults(sc);
2210 
2211 	/*
2212 	 * We should test if MB86965A is on the base address now.
2213 	 * Unfortunately, it is very hard to probe it reliably, since
2214 	 * we have no way to reset the chip under software control.
2215 	 * On cold boot, we could check the "signature" bit patterns
2216 	 * described in the Fujitsu document.  On warm boot, however,
2217 	 * we can predict almost nothing about register values.
2218 	 */
2219 	if (!fe_simple_probe(sc, probe_table)) return 0;
2220 
2221 	/* Check if our I/O address matches config info on 86965.  */
2222 	n = (inb(sc->ioaddr[FE_BMPR19]) & FE_B19_ADDR) >> FE_B19_ADDR_SHIFT;
2223 	if (baseaddr[n] != sc->iobase) return 0;
2224 
2225 	/*
2226 	 * We are now almost sure we have an MB86965 at the given
2227 	 * address.  So, read EEPROM through it.  We have to write
2228 	 * into LSI registers to read from EEPROM.  I want to avoid it
2229 	 * at this stage, but I cannot test the presence of the chip
2230 	 * any further without reading EEPROM.  FIXME.
2231 	 */
2232 	fe_read_eeprom_jli(sc, eeprom);
2233 
2234 	/* Make sure that config info in EEPROM and 86965 agree.  */
2235 	if (eeprom[FE_EEPROM_CONF] != inb(sc->ioaddr[FE_BMPR19])) {
2236 		return 0;
2237 	}
2238 
2239 	/* Use 86965 media selection scheme, unless othewise
2240            specified.  It is "AUTO always" and "select with BMPR13."
2241            This behaviour covers most of the 86965 based board (as
2242            minimum requirements.)  It is backward compatible with
2243            previous versions, also.  */
2244 	sc->mbitmap = MB_HA;
2245 	sc->defmedia = MB_HA;
2246 	sc->msel = fe_msel_965;
2247 
2248 	/* Perform board-specific probe, one by one.  Note that the
2249            order of probe is important and should not be changed
2250            arbitrarily.  */
2251 	if ((irqmap = fe_probe_jli_ati(sc, eeprom)) == NULL
2252 	 && (irqmap = fe_probe_jli_rex(sc, eeprom)) == NULL
2253 	 && (irqmap = fe_probe_jli_icl(sc, eeprom)) == NULL
2254 	 && (irqmap = fe_probe_jli_unk(sc, eeprom)) == NULL) return 0;
2255 
2256 	/* Find the IRQ read from EEPROM.  */
2257 	n = (inb(sc->ioaddr[FE_BMPR19]) & FE_B19_IRQ) >> FE_B19_IRQ_SHIFT;
2258 	irq = irqmap[n];
2259 
2260 	/* Try to determine IRQ setting.  */
2261 	if (dev->id_irq == NO_IRQ && irq == NO_IRQ) {
2262 		/* The device must be configured with an explicit IRQ.  */
2263 		printf("fe%d: IRQ auto-detection does not work\n",
2264 		       sc->sc_unit);
2265 		return 0;
2266 	} else if (dev->id_irq == NO_IRQ && irq != NO_IRQ) {
2267 		/* Just use the probed IRQ value.  */
2268 		dev->id_irq = irq;
2269 	} else if (dev->id_irq != NO_IRQ && irq == NO_IRQ) {
2270 		/* No problem.  Go ahead.  */
2271 	} else if (dev->id_irq == irq) {
2272 		/* Good.  Go ahead.  */
2273 	} else {
2274 		/* User must be warned in this case.  */
2275 		sc->stability |= UNSTABLE_IRQ;
2276 	}
2277 
2278 	/* Setup a hook, which resets te 86965 when the driver is being
2279            initialized.  This may solve a nasty bug.  FIXME.  */
2280 	sc->init = fe_init_jli;
2281 
2282 	/*
2283 	 * That's all.  86965 JLI occupies 32 I/O addresses, by the way.
2284 	 */
2285 	return 32;
2286 }
2287 
2288 /* Probe for TDK LAK-AX031, which is an SSi 78Q8377A based board.  */
2289 
2290 static int
2291 fe_probe_ssi (struct isa_device *dev, struct fe_softc *sc)
2292 {
2293 	u_char eeprom [SSI_EEPROM_SIZE];
2294 
2295 	static struct fe_simple_probe_struct probe_table [] = {
2296 		{ FE_DLCR2, 0x08, 0x00 },
2297 		{ FE_DLCR4, 0x08, 0x00 },
2298 		{ 0 }
2299 	};
2300 
2301 	/* See if the specified I/O address is possible for 78Q8377A.  */
2302 	if ((sc->iobase & ~0x3F0) != 0x000) return 0;
2303 
2304 	/* Fill the softc struct with default values.  */
2305 	fe_softc_defaults(sc);
2306 
2307 	/* See if the card is on its address.  */
2308 	if (!fe_simple_probe(sc, probe_table)) return 0;
2309 
2310 	/* We now have to read the config EEPROM.  We should be very
2311            careful, since doing so destroys a register.  (Remember, we
2312            are not yet sure we have a LAK-AX031 board here.)  Don't
2313            remember to select BMPRs bofore reading EEPROM, since other
2314            register bank may be selected before the probe() is called.  */
2315 	fe_read_eeprom_ssi(sc, eeprom);
2316 
2317 	/* Make sure the Ethernet (MAC) station address is of TDK's.  */
2318 	if (!valid_Ether_p(eeprom+FE_SSI_EEP_ADDR, 0x008098)) return 0;
2319 	bcopy(eeprom+FE_SSI_EEP_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN);
2320 
2321 	/* This looks like a TDK-AX031 board.  It requires an explicit
2322 	   IRQ setting in config, since we currently don't know how we
2323 	   can find the IRQ value assigned by ISA PnP manager.  */
2324 	if (dev->id_irq == NO_IRQ) {
2325 		fe_irq_failure("LAK-AX031", sc->sc_unit, dev->id_irq, NULL);
2326 		return 0;
2327 	}
2328 
2329 	/* Fill softc struct accordingly.  */
2330 	sc->typestr = "LAK-AX031";
2331 	sc->mbitmap = MB_HT;
2332 	sc->defmedia = MB_HT;
2333 
2334 	/* We have 16 registers.  */
2335 	return 16;
2336 }
2337 
2338 /*
2339  * Probe and initialization for TDK/LANX LAC-AX012/013 boards.
2340  */
2341 static int
2342 fe_probe_lnx (struct isa_device *dev, struct fe_softc *sc)
2343 {
2344 	u_char eeprom [LNX_EEPROM_SIZE];
2345 
2346 	static struct fe_simple_probe_struct probe_table [] = {
2347 		{ FE_DLCR2, 0x58, 0x00 },
2348 		{ FE_DLCR4, 0x08, 0x00 },
2349 		{ 0 }
2350 	};
2351 
2352 	/* See if the specified I/O address is possible for TDK/LANX boards.  */
2353 	/* 300, 320, 340, and 360 are allowed.  */
2354 	if ((sc->iobase & ~0x060) != 0x300) return 0;
2355 
2356 	/* Fill the softc struct with default values.  */
2357 	fe_softc_defaults(sc);
2358 
2359 	/* See if the card is on its address.  */
2360 	if (!fe_simple_probe(sc, probe_table)) return 0;
2361 
2362 	/* We now have to read the config EEPROM.  We should be very
2363            careful, since doing so destroys a register.  (Remember, we
2364            are not yet sure we have a LAC-AX012/AX013 board here.)  */
2365 	fe_read_eeprom_lnx(sc, eeprom);
2366 
2367 	/* Make sure the Ethernet (MAC) station address is of TDK/LANX's.  */
2368 	if (!valid_Ether_p(eeprom, 0x008098)) return 0;
2369 	bcopy(eeprom, sc->sc_enaddr, ETHER_ADDR_LEN);
2370 
2371 	/* This looks like a TDK/LANX board.  It requires an
2372 	   explicit IRQ setting in config.  Make sure we have one,
2373 	   determining an appropriate value for the IRQ control
2374 	   register.  */
2375 	switch (dev->id_irq) {
2376 	  case IRQ3: sc->priv_info = 0x40 | LNX_CLK_LO | LNX_SDA_HI; break;
2377 	  case IRQ4: sc->priv_info = 0x20 | LNX_CLK_LO | LNX_SDA_HI; break;
2378 	  case IRQ5: sc->priv_info = 0x10 | LNX_CLK_LO | LNX_SDA_HI; break;
2379 	  case IRQ9: sc->priv_info = 0x80 | LNX_CLK_LO | LNX_SDA_HI; break;
2380 	  default:
2381 		fe_irq_failure("LAC-AX012/AX013",
2382 			       sc->sc_unit, dev->id_irq, "3/4/5/9");
2383 		return 0;
2384 	}
2385 
2386 	/* Fill softc struct accordingly.  */
2387 	sc->typestr = "LAC-AX012/AX013";
2388 	sc->init = fe_init_lnx;
2389 
2390 	/* We have 32 registers.  */
2391 	return 32;
2392 }
2393 
2394 /*
2395  * Probe and initialization for Gateway Communications' old cards.
2396  */
2397 static int
2398 fe_probe_gwy ( struct isa_device * dev, struct fe_softc * sc )
2399 {
2400 	static struct fe_simple_probe_struct probe_table [] = {
2401 	    /*	{ FE_DLCR2, 0x70, 0x00 }, */
2402 		{ FE_DLCR2, 0x58, 0x00 },
2403 		{ FE_DLCR4, 0x08, 0x00 },
2404 		{ 0 }
2405 	};
2406 
2407 	/* See if the specified I/O address is possible for Gateway boards.  */
2408 	if ((sc->iobase & ~0x1E0) != 0x200) return 0;
2409 
2410 	/* Setup an I/O address mapping table and some others.  */
2411 	fe_softc_defaults(sc);
2412 
2413 	/* See if the card is on its address.  */
2414 	if ( !fe_simple_probe( sc, probe_table ) ) return 0;
2415 
2416 	/* Get our station address from EEPROM. */
2417 	inblk( sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN );
2418 
2419 	/* Make sure it is Gateway Communication's.  */
2420 	if (!valid_Ether_p(sc->sc_enaddr, 0x000061)) return 0;
2421 
2422 	/* Gateway's board requires an explicit IRQ to work, since it
2423 	   is not possible to probe the setting of jumpers.  */
2424 	if (dev->id_irq == NO_IRQ) {
2425 		fe_irq_failure("Gateway Ethernet", sc->sc_unit, NO_IRQ, NULL);
2426 		return 0;
2427 	}
2428 
2429 	/* Fill softc struct accordingly.  */
2430 	sc->typestr = "Gateway Ethernet (Fujitsu chipset)";
2431 
2432 	/* That's all.  The card occupies 32 I/O addresses, as always.  */
2433 	return 32;
2434 }
2435 
2436 /* Probe and initialization for Ungermann-Bass Network
2437    K.K. "Access/PC" boards.  */
2438 static int
2439 fe_probe_ubn (struct isa_device * dev, struct fe_softc * sc)
2440 {
2441 #if 0
2442 	u_char sum;
2443 #endif
2444 	static struct fe_simple_probe_struct const probe_table [] = {
2445 		{ FE_DLCR2, 0x58, 0x00 },
2446 		{ FE_DLCR4, 0x08, 0x00 },
2447 		{ 0 }
2448 	};
2449 
2450 	/* See if the specified I/O address is possible for AccessPC/ISA.  */
2451 	if ((sc->iobase & ~0x0E0) != 0x300) return 0;
2452 
2453 	/* Setup an I/O address mapping table and some others.  */
2454 	fe_softc_defaults(sc);
2455 
2456 	/* Simple probe.  */
2457 	if (!fe_simple_probe(sc, probe_table)) return 0;
2458 
2459 	/* Get our station address form ID ROM and make sure it is UBN's.  */
2460 	inblk(sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN);
2461 	if (!valid_Ether_p(sc->sc_enaddr, 0x00DD01)) return 0;
2462 #if 0
2463 	/* Calculate checksum.  */
2464 	sum = inb(sc->ioaddr[0x1e]);
2465 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
2466 		sum ^= sc->sc_enaddr[i];
2467 	}
2468 	if (sum != 0) return 0;
2469 #endif
2470 	/* This looks like an AccessPC/ISA board.  It requires an
2471 	   explicit IRQ setting in config.  Make sure we have one,
2472 	   determining an appropriate value for the IRQ control
2473 	   register.  */
2474 	switch (dev->id_irq) {
2475 	  case IRQ3:  sc->priv_info = 0x02; break;
2476 	  case IRQ4:  sc->priv_info = 0x04; break;
2477 	  case IRQ5:  sc->priv_info = 0x08; break;
2478 	  case IRQ10: sc->priv_info = 0x10; break;
2479 	  default:
2480 		fe_irq_failure("Access/PC",
2481 			       sc->sc_unit, dev->id_irq, "3/4/5/10");
2482 		return 0;
2483 	}
2484 
2485 	/* Fill softc struct accordingly.  */
2486 	sc->typestr = "Access/PC";
2487 	sc->init = fe_init_ubn;
2488 
2489 	/* We have 32 registers.  */
2490 	return 32;
2491 }
2492 #endif	/* PC98 */
2493 
2494 #if NCARD > 0
2495 /*
2496  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
2497  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
2498  */
2499 
2500 static void
2501 fe_init_mbh ( struct fe_softc * sc )
2502 {
2503 	/* Minimal initialization of 86960.  */
2504 	DELAY( 200 );
2505 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2506 	DELAY( 200 );
2507 
2508 	/* Disable all interrupts.  */
2509 	outb( sc->ioaddr[ FE_DLCR2 ], 0 );
2510 	outb( sc->ioaddr[ FE_DLCR3 ], 0 );
2511 
2512 	/* Enable master interrupt flag.  */
2513 	outb( sc->ioaddr[ FE_MBH0 ], FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE );
2514 }
2515 
2516 static int
2517 fe_probe_mbh ( struct isa_device * dev, struct fe_softc * sc )
2518 {
2519 	static struct fe_simple_probe_struct probe_table [] = {
2520 		{ FE_DLCR2, 0x58, 0x00 },
2521 		{ FE_DLCR4, 0x08, 0x00 },
2522 		{ FE_DLCR6, 0xFF, 0xB6 },
2523 		{ 0 }
2524 	};
2525 
2526 #ifdef DIAGNOSTIC
2527 	/* We need an explicit IRQ.  */
2528 	if (dev->id_irq == NO_IRQ) return 0;
2529 #endif
2530 
2531 	/* Ethernet MAC address should *NOT* have been given by pccardd,
2532 	   if this is a true MBH10302; i.e., Ethernet address must be
2533 	   "all-zero" upon entry.  */
2534 	if (sc->sc_enaddr[0] || sc->sc_enaddr[1] || sc->sc_enaddr[2] ||
2535 	    sc->sc_enaddr[3] || sc->sc_enaddr[4] || sc->sc_enaddr[5]) {
2536 		return 0;
2537 	}
2538 
2539 	/* Fill the softc struct with default values.  */
2540 	fe_softc_defaults(sc);
2541 
2542 	/*
2543 	 * See if MBH10302 is on its address.
2544 	 * I'm not sure the following probe code works.  FIXME.
2545 	 */
2546 	if ( !fe_simple_probe( sc, probe_table ) ) return 0;
2547 
2548 	/* Get our station address from EEPROM.  */
2549 	inblk( sc, FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN );
2550 
2551 	/* Make sure we got a valid station address.  */
2552 	if (!valid_Ether_p(sc->sc_enaddr, 0)) return 0;
2553 
2554 	/* Determine the card type.  */
2555 	sc->typestr = "MBH10302 (PCMCIA)";
2556 
2557 	/* We seems to need our own IDENT bits...  FIXME.  */
2558 	sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
2559 
2560 	/* Setup hooks.  We need a special initialization procedure.  */
2561 	sc->init = fe_init_mbh;
2562 
2563 	/*
2564 	 * That's all.  MBH10302 occupies 32 I/O addresses, by the way.
2565 	 */
2566 	return 32;
2567 }
2568 
2569 /*
2570  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
2571  * by MASUI Kenji <[email protected]>
2572  *
2573  * (Contec uses TDK Ethenet chip -- hosokawa)
2574  *
2575  * This version of fe_probe_tdk has been rewrote to handle
2576  * *generic* PC card implementation of Fujitsu MB8696x family.  The
2577  * name _tdk is just for a historical reason. :-)
2578  */
2579 static int
2580 fe_probe_tdk ( struct isa_device * dev, struct fe_softc * sc )
2581 {
2582         static struct fe_simple_probe_struct probe_table [] = {
2583                 { FE_DLCR2, 0x50, 0x00 },
2584                 { FE_DLCR4, 0x08, 0x00 },
2585             /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
2586                 { 0 }
2587         };
2588 
2589         if ( dev->id_irq == NO_IRQ ) {
2590                 return ( 0 );
2591         }
2592 
2593 	fe_softc_defaults(sc);
2594 
2595         /*
2596          * See if C-NET(PC)C is on its address.
2597          */
2598 
2599         if ( !fe_simple_probe( sc, probe_table ) ) return 0;
2600 
2601         /* Determine the card type.  */
2602         sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
2603 
2604         /*
2605          * Initialize constants in the per-line structure.
2606          */
2607 
2608         /* Make sure we got a valid station address.  */
2609         if (!valid_Ether_p(sc->sc_enaddr, 0)) return 0;
2610 
2611         /*
2612          * That's all.  C-NET(PC)C occupies 16 I/O addresses.
2613 	 * XXX: Are there any card with 32 I/O addresses?  FIXME.
2614          */
2615         return 16;
2616 }
2617 #endif /* NCARD > 0 */
2618 
2619 /*
2620  * Install interface into kernel networking data structures
2621  */
2622 static int
2623 fe_attach ( struct isa_device * dev )
2624 {
2625 #if NCARD > 0
2626 	static	int	already_ifattach[NFE];
2627 #endif
2628 	struct fe_softc *sc = &fe_softc[dev->id_unit];
2629 	int b;
2630 
2631 	dev->id_ointr = feintr;
2632 
2633 	/*
2634 	 * Initialize ifnet structure
2635 	 */
2636  	sc->sc_if.if_softc    = sc;
2637 	sc->sc_if.if_unit     = sc->sc_unit;
2638 	sc->sc_if.if_name     = "fe";
2639 	sc->sc_if.if_output   = ether_output;
2640 	sc->sc_if.if_start    = fe_start;
2641 	sc->sc_if.if_ioctl    = fe_ioctl;
2642 	sc->sc_if.if_watchdog = fe_watchdog;
2643 	sc->sc_if.if_init     = fe_init;
2644 	sc->sc_if.if_linkmib  = &sc->mibdata;
2645 	sc->sc_if.if_linkmiblen = sizeof (sc->mibdata);
2646 
2647 #if 0 /* I'm not sure... */
2648 	sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS;
2649 #endif
2650 
2651 	/*
2652 	 * Set fixed interface flags.
2653 	 */
2654  	sc->sc_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2655 
2656 #if 1
2657 	/*
2658 	 * Set maximum size of output queue, if it has not been set.
2659 	 * It is done here as this driver may be started after the
2660 	 * system initialization (i.e., the interface is PCMCIA.)
2661 	 *
2662 	 * I'm not sure this is really necessary, but, even if it is,
2663 	 * it should be done somewhere else, e.g., in if_attach(),
2664 	 * since it must be a common workaround for all network drivers.
2665 	 * FIXME.
2666 	 */
2667 	if ( sc->sc_if.if_snd.ifq_maxlen == 0 ) {
2668 		sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
2669 	}
2670 #endif
2671 
2672 #if FE_SINGLE_TRANSMISSION
2673 	/* Override txb config to allocate minimum.  */
2674 	sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
2675 	sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
2676 #endif
2677 
2678 	/* Modify hardware config if it is requested.  */
2679 	if ( dev->id_flags & FE_FLAGS_OVERRIDE_DLCR6 ) {
2680 		sc->proto_dlcr6 = dev->id_flags & FE_FLAGS_DLCR6_VALUE;
2681 	}
2682 
2683 	/* Find TX buffer size, based on the hardware dependent proto.  */
2684 	switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
2685 	  case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
2686 	  case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
2687 	  case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
2688 	  default:
2689 		/* Oops, we can't work with single buffer configuration.  */
2690 		if (bootverbose) {
2691 			printf("fe%d: strange TXBSIZ config; fixing\n",
2692 			       sc->sc_unit);
2693 		}
2694 		sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
2695 		sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
2696 		sc->txb_size = 2048;
2697 		break;
2698 	}
2699 
2700 	/* Initialize the if_media interface.  */
2701 	ifmedia_init(&sc->media, 0, fe_medchange, fe_medstat );
2702 	for (b = 0; bit2media[b] != 0; b++) {
2703 		if (sc->mbitmap & (1 << b)) {
2704 			ifmedia_add(&sc->media, bit2media[b], 0, NULL);
2705 		}
2706 	}
2707 	for (b = 0; bit2media[b] != 0; b++) {
2708 		if (sc->defmedia & (1 << b)) {
2709 			ifmedia_set(&sc->media, bit2media[b]);
2710 			break;
2711 		}
2712 	}
2713 #if 0	/* Turned off; this is called later, when the interface UPs.  */
2714 	fe_medchange(sc);
2715 #endif
2716 
2717 	/* Attach and stop the interface. */
2718 #if NCARD > 0
2719 	if (already_ifattach[dev->id_unit] != 1) {
2720 		if_attach(&sc->sc_if);
2721 		already_ifattach[dev->id_unit] = 1;
2722 	}
2723 #else
2724 	if_attach(&sc->sc_if);
2725 #endif
2726 	fe_stop(sc);
2727  	ether_ifattach(&sc->sc_if);
2728 
2729   	/* Print additional info when attached.  */
2730  	printf("fe%d: address %6D, type %s%s\n", sc->sc_unit,
2731 	       sc->sc_enaddr, ":" , sc->typestr,
2732 	       (sc->proto_dlcr4 & FE_D4_DSC) ? ", full duplex" : "");
2733 	if (bootverbose) {
2734 		int buf, txb, bbw, sbw, ram;
2735 
2736 		buf = txb = bbw = sbw = ram = -1;
2737 		switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
2738 		  case FE_D6_BUFSIZ_8KB:  buf =  8; break;
2739 		  case FE_D6_BUFSIZ_16KB: buf = 16; break;
2740 		  case FE_D6_BUFSIZ_32KB: buf = 32; break;
2741 		  case FE_D6_BUFSIZ_64KB: buf = 64; break;
2742 		}
2743 		switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
2744 		  case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
2745 		  case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
2746 		  case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
2747 		}
2748 		switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
2749 		  case FE_D6_BBW_BYTE: bbw =  8; break;
2750 		  case FE_D6_BBW_WORD: bbw = 16; break;
2751 		}
2752 		switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
2753 		  case FE_D6_SBW_BYTE: sbw =  8; break;
2754 		  case FE_D6_SBW_WORD: sbw = 16; break;
2755 		}
2756 		switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
2757 		  case FE_D6_SRAM_100ns: ram = 100; break;
2758 		  case FE_D6_SRAM_150ns: ram = 150; break;
2759 		}
2760 		printf("fe%d: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
2761 			sc->sc_unit, buf, bbw, ram, txb, sbw);
2762 	}
2763 	if (sc->stability & UNSTABLE_IRQ) {
2764 		printf("fe%d: warning: IRQ number may be incorrect\n",
2765 		       sc->sc_unit);
2766 	}
2767 	if (sc->stability & UNSTABLE_MAC) {
2768 		printf("fe%d: warning: above MAC address may be incorrect\n",
2769 		       sc->sc_unit);
2770 	}
2771 	if (sc->stability & UNSTABLE_TYPE) {
2772 		printf("fe%d: warning: hardware type was not validated\n",
2773 		       sc->sc_unit);
2774 	}
2775 
2776 #if NBPF > 0
2777 	/* If BPF is in the kernel, call the attach for it.  */
2778  	bpfattach(&sc->sc_if, DLT_EN10MB, sizeof(struct ether_header));
2779 #endif
2780 	return 1;
2781 }
2782 
2783 /*
2784  * Reset interface, after some (hardware) trouble is deteced.
2785  */
2786 static void
2787 fe_reset (struct fe_softc *sc)
2788 {
2789 	/* Record how many packets are lost by this accident.  */
2790 	sc->sc_if.if_oerrors += sc->txb_sched + sc->txb_count;
2791 	sc->mibdata.dot3StatsInternalMacTransmitErrors++;
2792 
2793 	/* Put the interface into known initial state.  */
2794 	fe_stop(sc);
2795 	if (sc->sc_if.if_flags & IFF_UP) fe_init(sc);
2796 }
2797 
2798 /*
2799  * Stop everything on the interface.
2800  *
2801  * All buffered packets, both transmitting and receiving,
2802  * if any, will be lost by stopping the interface.
2803  */
2804 static void
2805 fe_stop (struct fe_softc *sc)
2806 {
2807 	int s;
2808 
2809 	s = splimp();
2810 
2811 	/* Disable interrupts.  */
2812 	outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
2813 	outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
2814 
2815 	/* Stop interface hardware.  */
2816 	DELAY( 200 );
2817 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2818 	DELAY( 200 );
2819 
2820 	/* Clear all interrupt status.  */
2821 	outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );
2822 	outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );
2823 
2824 	/* Put the chip in stand-by mode.  */
2825 	DELAY( 200 );
2826 	outb( sc->ioaddr[ FE_DLCR7 ], sc->proto_dlcr7 | FE_D7_POWER_DOWN );
2827 	DELAY( 200 );
2828 
2829 	/* Reset transmitter variables and interface flags.  */
2830 	sc->sc_if.if_flags &= ~( IFF_OACTIVE | IFF_RUNNING );
2831 	sc->sc_if.if_timer = 0;
2832 	sc->txb_free = sc->txb_size;
2833 	sc->txb_count = 0;
2834 	sc->txb_sched = 0;
2835 
2836 	/* MAR loading can be delayed.  */
2837 	sc->filter_change = 0;
2838 
2839 	/* Call a device-specific hook.  */
2840 	if ( sc->stop ) sc->stop( sc );
2841 
2842 	(void) splx(s);
2843 }
2844 
2845 /*
2846  * Device timeout/watchdog routine. Entered if the device neglects to
2847  * generate an interrupt after a transmit has been started on it.
2848  */
2849 static void
2850 fe_watchdog ( struct ifnet *ifp )
2851 {
2852 	struct fe_softc *sc = (struct fe_softc *)ifp;
2853 
2854 	/* A "debug" message.  */
2855 	printf("fe%d: transmission timeout (%d+%d)%s\n",
2856 	       ifp->if_unit, sc->txb_sched, sc->txb_count,
2857 	       (ifp->if_flags & IFF_UP) ? "" : " when down");
2858 	if ( sc->sc_if.if_opackets == 0 && sc->sc_if.if_ipackets == 0 ) {
2859 		printf("fe%d: wrong IRQ setting in config?\n", ifp->if_unit);
2860 	}
2861 	fe_reset( sc );
2862 }
2863 
2864 /*
2865  * Initialize device.
2866  */
2867 static void
2868 fe_init (void * xsc)
2869 {
2870 	struct fe_softc *sc = xsc;
2871 	int s;
2872 
2873 	/* We need an address. */
2874 	if (TAILQ_EMPTY(&sc->sc_if.if_addrhead)) { /* XXX unlikely */
2875 #ifdef DIAGNOSTIC
2876 		printf("fe%d: init() without any address\n", sc->sc_unit);
2877 #endif
2878 		return;
2879 	}
2880 
2881 	/* Start initializing 86960.  */
2882 	s = splimp();
2883 
2884 	/* Call a hook before we start initializing the chip.  */
2885 	if ( sc->init ) sc->init( sc );
2886 
2887 	/*
2888 	 * Make sure to disable the chip, also.
2889 	 * This may also help re-programming the chip after
2890 	 * hot insertion of PCMCIAs.
2891 	 */
2892 	DELAY( 200 );
2893 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2894 	DELAY( 200 );
2895 
2896 	/* Power up the chip and select register bank for DLCRs.  */
2897 	DELAY( 200 );
2898 	outb( sc->ioaddr[ FE_DLCR7 ],
2899 		sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP );
2900 	DELAY( 200 );
2901 
2902 	/* Feed the station address.  */
2903 	outblk( sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN );
2904 
2905 	/* Clear multicast address filter to receive nothing.  */
2906 	outb( sc->ioaddr[ FE_DLCR7 ],
2907 		sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
2908 	outblk( sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN );
2909 
2910 	/* Select the BMPR bank for runtime register access.  */
2911 	outb( sc->ioaddr[ FE_DLCR7 ],
2912 		sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
2913 
2914 	/* Initialize registers.  */
2915 	outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );	/* Clear all bits.  */
2916 	outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );	/* ditto.  */
2917 	outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
2918 	outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
2919 	outb( sc->ioaddr[ FE_DLCR4 ], sc->proto_dlcr4 );
2920 	outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
2921 	outb( sc->ioaddr[ FE_BMPR10 ], 0x00 );
2922 	outb( sc->ioaddr[ FE_BMPR11 ], FE_B11_CTRL_SKIP | FE_B11_MODE1 );
2923 	outb( sc->ioaddr[ FE_BMPR12 ], 0x00 );
2924 	outb( sc->ioaddr[ FE_BMPR13 ], sc->proto_bmpr13 );
2925 	outb( sc->ioaddr[ FE_BMPR14 ], 0x00 );
2926 	outb( sc->ioaddr[ FE_BMPR15 ], 0x00 );
2927 
2928 	/* Enable interrupts.  */
2929 	outb( sc->ioaddr[ FE_DLCR2 ], FE_TMASK );
2930 	outb( sc->ioaddr[ FE_DLCR3 ], FE_RMASK );
2931 
2932 	/* Select requested media, just before enabling DLC.  */
2933 	if (sc->msel) sc->msel(sc);
2934 
2935 	/* Enable transmitter and receiver.  */
2936 	DELAY( 200 );
2937 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
2938 	DELAY( 200 );
2939 
2940 #ifdef DIAGNOSTIC
2941 	/*
2942 	 * Make sure to empty the receive buffer.
2943 	 *
2944 	 * This may be redundant, but *if* the receive buffer were full
2945 	 * at this point, then the driver would hang.  I have experienced
2946 	 * some strange hang-up just after UP.  I hope the following
2947 	 * code solve the problem.
2948 	 *
2949 	 * I have changed the order of hardware initialization.
2950 	 * I think the receive buffer cannot have any packets at this
2951 	 * point in this version.  The following code *must* be
2952 	 * redundant now.  FIXME.
2953 	 *
2954 	 * I've heard a rumore that on some PC card implementation of
2955 	 * 8696x, the receive buffer can have some data at this point.
2956 	 * The following message helps discovering the fact.  FIXME.
2957 	 */
2958 	if ( !( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) ) {
2959 		printf("fe%d: receive buffer has some data after reset\n",
2960 		       sc->sc_unit);
2961 		fe_emptybuffer( sc );
2962 	}
2963 
2964 	/* Do we need this here?  Actually, no.  I must be paranoia.  */
2965 	outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );	/* Clear all bits.  */
2966 	outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );	/* ditto.  */
2967 #endif
2968 
2969 	/* Set 'running' flag, because we are now running.   */
2970 	sc->sc_if.if_flags |= IFF_RUNNING;
2971 
2972 	/*
2973 	 * At this point, the interface is running properly,
2974 	 * except that it receives *no* packets.  we then call
2975 	 * fe_setmode() to tell the chip what packets to be
2976 	 * received, based on the if_flags and multicast group
2977 	 * list.  It completes the initialization process.
2978 	 */
2979 	fe_setmode( sc );
2980 
2981 #if 0
2982 	/* ...and attempt to start output queued packets.  */
2983 	/* TURNED OFF, because the semi-auto media prober wants to UP
2984            the interface keeping it idle.  The upper layer will soon
2985            start the interface anyway, and there are no significant
2986            delay.  */
2987 	fe_start( &sc->sc_if );
2988 #endif
2989 
2990 	(void) splx(s);
2991 }
2992 
2993 /*
2994  * This routine actually starts the transmission on the interface
2995  */
2996 static void
2997 fe_xmit ( struct fe_softc * sc )
2998 {
2999 	/*
3000 	 * Set a timer just in case we never hear from the board again.
3001 	 * We use longer timeout for multiple packet transmission.
3002 	 * I'm not sure this timer value is appropriate.  FIXME.
3003 	 */
3004 	sc->sc_if.if_timer = 1 + sc->txb_count;
3005 
3006 	/* Update txb variables.  */
3007 	sc->txb_sched = sc->txb_count;
3008 	sc->txb_count = 0;
3009 	sc->txb_free = sc->txb_size;
3010 	sc->tx_excolls = 0;
3011 
3012 	/* Start transmitter, passing packets in TX buffer.  */
3013 	outb( sc->ioaddr[ FE_BMPR10 ], sc->txb_sched | FE_B10_START );
3014 }
3015 
3016 /*
3017  * Start output on interface.
3018  * We make two assumptions here:
3019  *  1) that the current priority is set to splimp _before_ this code
3020  *     is called *and* is returned to the appropriate priority after
3021  *     return
3022  *  2) that the IFF_OACTIVE flag is checked before this code is called
3023  *     (i.e. that the output part of the interface is idle)
3024  */
3025 void
3026 fe_start ( struct ifnet *ifp )
3027 {
3028 	struct fe_softc *sc = ifp->if_softc;
3029 	struct mbuf *m;
3030 
3031 #ifdef DIAGNOSTIC
3032 	/* Just a sanity check.  */
3033 	if ( ( sc->txb_count == 0 ) != ( sc->txb_free == sc->txb_size ) ) {
3034 		/*
3035 		 * Txb_count and txb_free co-works to manage the
3036 		 * transmission buffer.  Txb_count keeps track of the
3037 		 * used potion of the buffer, while txb_free does unused
3038 		 * potion.  So, as long as the driver runs properly,
3039 		 * txb_count is zero if and only if txb_free is same
3040 		 * as txb_size (which represents whole buffer.)
3041 		 */
3042 		printf("fe%d: inconsistent txb variables (%d, %d)\n",
3043 			sc->sc_unit, sc->txb_count, sc->txb_free);
3044 		/*
3045 		 * So, what should I do, then?
3046 		 *
3047 		 * We now know txb_count and txb_free contradicts.  We
3048 		 * cannot, however, tell which is wrong.  More
3049 		 * over, we cannot peek 86960 transmission buffer or
3050 		 * reset the transmission buffer.  (In fact, we can
3051 		 * reset the entire interface.  I don't want to do it.)
3052 		 *
3053 		 * If txb_count is incorrect, leaving it as-is will cause
3054 		 * sending of garbage after next interrupt.  We have to
3055 		 * avoid it.  Hence, we reset the txb_count here.  If
3056 		 * txb_free was incorrect, resetting txb_count just loose
3057 		 * some packets.  We can live with it.
3058 		 */
3059 		sc->txb_count = 0;
3060 	}
3061 #endif
3062 
3063 	/*
3064 	 * First, see if there are buffered packets and an idle
3065 	 * transmitter - should never happen at this point.
3066 	 */
3067 	if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
3068 		printf("fe%d: transmitter idle with %d buffered packets\n",
3069 		       sc->sc_unit, sc->txb_count);
3070 		fe_xmit( sc );
3071 	}
3072 
3073 	/*
3074 	 * Stop accepting more transmission packets temporarily, when
3075 	 * a filter change request is delayed.  Updating the MARs on
3076 	 * 86960 flushes the transmission buffer, so it is delayed
3077 	 * until all buffered transmission packets have been sent
3078 	 * out.
3079 	 */
3080 	if ( sc->filter_change ) {
3081 		/*
3082 		 * Filter change request is delayed only when the DLC is
3083 		 * working.  DLC soon raise an interrupt after finishing
3084 		 * the work.
3085 		 */
3086 		goto indicate_active;
3087 	}
3088 
3089 	for (;;) {
3090 
3091 		/*
3092 		 * See if there is room to put another packet in the buffer.
3093 		 * We *could* do better job by peeking the send queue to
3094 		 * know the length of the next packet.  Current version just
3095 		 * tests against the worst case (i.e., longest packet).  FIXME.
3096 		 *
3097 		 * When adding the packet-peek feature, don't forget adding a
3098 		 * test on txb_count against QUEUEING_MAX.
3099 		 * There is a little chance the packet count exceeds
3100 		 * the limit.  Assume transmission buffer is 8KB (2x8KB
3101 		 * configuration) and an application sends a bunch of small
3102 		 * (i.e., minimum packet sized) packets rapidly.  An 8KB
3103 		 * buffer can hold 130 blocks of 62 bytes long...
3104 		 */
3105 		if ( sc->txb_free
3106 		    < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN ) {
3107 			/* No room.  */
3108 			goto indicate_active;
3109 		}
3110 
3111 #if FE_SINGLE_TRANSMISSION
3112 		if ( sc->txb_count > 0 ) {
3113 			/* Just one packet per a transmission buffer.  */
3114 			goto indicate_active;
3115 		}
3116 #endif
3117 
3118 		/*
3119 		 * Get the next mbuf chain for a packet to send.
3120 		 */
3121 		IF_DEQUEUE( &sc->sc_if.if_snd, m );
3122 		if ( m == NULL ) {
3123 			/* No more packets to send.  */
3124 			goto indicate_inactive;
3125 		}
3126 
3127 		/*
3128 		 * Copy the mbuf chain into the transmission buffer.
3129 		 * txb_* variables are updated as necessary.
3130 		 */
3131 		fe_write_mbufs( sc, m );
3132 
3133 		/* Start transmitter if it's idle.  */
3134 		if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
3135 			fe_xmit( sc );
3136 		}
3137 
3138 		/*
3139 		 * Tap off here if there is a bpf listener,
3140 		 * and the device is *not* in promiscuous mode.
3141 		 * (86960 receives self-generated packets if
3142 		 * and only if it is in "receive everything"
3143 		 * mode.)
3144 		 */
3145 #if NBPF > 0
3146 		if ( sc->sc_if.if_bpf
3147 		  && !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
3148 			bpf_mtap( &sc->sc_if, m );
3149 		}
3150 #endif
3151 
3152 		m_freem( m );
3153 	}
3154 
3155   indicate_inactive:
3156 	/*
3157 	 * We are using the !OACTIVE flag to indicate to
3158 	 * the outside world that we can accept an
3159 	 * additional packet rather than that the
3160 	 * transmitter is _actually_ active.  Indeed, the
3161 	 * transmitter may be active, but if we haven't
3162 	 * filled all the buffers with data then we still
3163 	 * want to accept more.
3164 	 */
3165 	sc->sc_if.if_flags &= ~IFF_OACTIVE;
3166 	return;
3167 
3168   indicate_active:
3169 	/*
3170 	 * The transmitter is active, and there are no room for
3171 	 * more outgoing packets in the transmission buffer.
3172 	 */
3173 	sc->sc_if.if_flags |= IFF_OACTIVE;
3174 	return;
3175 }
3176 
3177 /*
3178  * Drop (skip) a packet from receive buffer in 86960 memory.
3179  */
3180 static void
3181 fe_droppacket ( struct fe_softc * sc, int len )
3182 {
3183 	int i;
3184 
3185 	/*
3186 	 * 86960 manual says that we have to read 8 bytes from the buffer
3187 	 * before skip the packets and that there must be more than 8 bytes
3188 	 * remaining in the buffer when issue a skip command.
3189 	 * Remember, we have already read 4 bytes before come here.
3190 	 */
3191 	if ( len > 12 ) {
3192 		/* Read 4 more bytes, and skip the rest of the packet.  */
3193 #ifdef FE_8BIT_SUPPORT
3194 		if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3195 		{
3196 			( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3197 			( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3198 			( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3199 			( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3200 		}
3201 		else
3202 #endif
3203 		{
3204 			( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3205 			( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3206 		}
3207 		outb( sc->ioaddr[ FE_BMPR14 ], FE_B14_SKIP );
3208 	} else {
3209 		/* We should not come here unless receiving RUNTs.  */
3210 #ifdef FE_8BIT_SUPPORT
3211 		if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3212 		{
3213 			for ( i = 0; i < len; i++ ) {
3214 				( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3215 			}
3216 		}
3217 		else
3218 #endif
3219 		{
3220 			for ( i = 0; i < len; i += 2 ) {
3221 				( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3222 			}
3223 		}
3224 	}
3225 }
3226 
3227 #ifdef DIAGNOSTIC
3228 /*
3229  * Empty receiving buffer.
3230  */
3231 static void
3232 fe_emptybuffer ( struct fe_softc * sc )
3233 {
3234 	int i;
3235 	u_char saved_dlcr5;
3236 
3237 #ifdef FE_DEBUG
3238 	printf("fe%d: emptying receive buffer\n", sc->sc_unit);
3239 #endif
3240 
3241 	/*
3242 	 * Stop receiving packets, temporarily.
3243 	 */
3244 	saved_dlcr5 = inb( sc->ioaddr[ FE_DLCR5 ] );
3245 	outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
3246 	DELAY(1300);
3247 
3248 	/*
3249 	 * When we come here, the receive buffer management may
3250 	 * have been broken.  So, we cannot use skip operation.
3251 	 * Just discard everything in the buffer.
3252 	 */
3253 #ifdef FE_8BIT_SUPPORT
3254 	if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3255 	{
3256 		for ( i = 0; i < 65536; i++ ) {
3257 			if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
3258 			( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3259 		}
3260 	}
3261 	else
3262 #endif
3263 	{
3264 		for ( i = 0; i < 65536; i += 2 ) {
3265 			if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
3266 			( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3267 		}
3268 	}
3269 
3270 	/*
3271 	 * Double check.
3272 	 */
3273 	if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) {
3274 		printf("fe%d: could not empty receive buffer\n", sc->sc_unit);
3275 		/* Hmm.  What should I do if this happens?  FIXME.  */
3276 	}
3277 
3278 	/*
3279 	 * Restart receiving packets.
3280 	 */
3281 	outb( sc->ioaddr[ FE_DLCR5 ], saved_dlcr5 );
3282 }
3283 #endif
3284 
3285 /*
3286  * Transmission interrupt handler
3287  * The control flow of this function looks silly.  FIXME.
3288  */
3289 static void
3290 fe_tint ( struct fe_softc * sc, u_char tstat )
3291 {
3292 	int left;
3293 	int col;
3294 
3295 	/*
3296 	 * Handle "excessive collision" interrupt.
3297 	 */
3298 	if ( tstat & FE_D0_COLL16 ) {
3299 
3300 		/*
3301 		 * Find how many packets (including this collided one)
3302 		 * are left unsent in transmission buffer.
3303 		 */
3304 		left = inb( sc->ioaddr[ FE_BMPR10 ] );
3305 		printf("fe%d: excessive collision (%d/%d)\n",
3306 		       sc->sc_unit, left, sc->txb_sched);
3307 
3308 		/*
3309 		 * Clear the collision flag (in 86960) here
3310 		 * to avoid confusing statistics.
3311 		 */
3312 		outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
3313 
3314 		/*
3315 		 * Restart transmitter, skipping the
3316 		 * collided packet.
3317 		 *
3318 		 * We *must* skip the packet to keep network running
3319 		 * properly.  Excessive collision error is an
3320 		 * indication of the network overload.  If we
3321 		 * tried sending the same packet after excessive
3322 		 * collision, the network would be filled with
3323 		 * out-of-time packets.  Packets belonging
3324 		 * to reliable transport (such as TCP) are resent
3325 		 * by some upper layer.
3326 		 */
3327 		outb( sc->ioaddr[ FE_BMPR11 ],
3328 			FE_B11_CTRL_SKIP | FE_B11_MODE1 );
3329 
3330 		/* Update statistics.  */
3331 		sc->tx_excolls++;
3332 	}
3333 
3334 	/*
3335 	 * Handle "transmission complete" interrupt.
3336 	 */
3337 	if ( tstat & FE_D0_TXDONE ) {
3338 
3339 		/*
3340 		 * Add in total number of collisions on last
3341 		 * transmission.  We also clear "collision occurred" flag
3342 		 * here.
3343 		 *
3344 		 * 86960 has a design flaw on collision count on multiple
3345 		 * packet transmission.  When we send two or more packets
3346 		 * with one start command (that's what we do when the
3347 		 * transmission queue is crowded), 86960 informs us number
3348 		 * of collisions occurred on the last packet on the
3349 		 * transmission only.  Number of collisions on previous
3350 		 * packets are lost.  I have told that the fact is clearly
3351 		 * stated in the Fujitsu document.
3352 		 *
3353 		 * I considered not to mind it seriously.  Collision
3354 		 * count is not so important, anyway.  Any comments?  FIXME.
3355 		 */
3356 
3357 		if ( inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_D0_COLLID ) {
3358 
3359 			/* Clear collision flag.  */
3360 			outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
3361 
3362 			/* Extract collision count from 86960.  */
3363 			col = inb( sc->ioaddr[ FE_DLCR4 ] );
3364 			col = ( col & FE_D4_COL ) >> FE_D4_COL_SHIFT;
3365 			if ( col == 0 ) {
3366 				/*
3367 				 * Status register indicates collisions,
3368 				 * while the collision count is zero.
3369 				 * This can happen after multiple packet
3370 				 * transmission, indicating that one or more
3371 				 * previous packet(s) had been collided.
3372 				 *
3373 				 * Since the accurate number of collisions
3374 				 * has been lost, we just guess it as 1;
3375 				 * Am I too optimistic?  FIXME.
3376 				 */
3377 				col = 1;
3378 			}
3379 			sc->sc_if.if_collisions += col;
3380 			if ( col == 1 ) {
3381 				sc->mibdata.dot3StatsSingleCollisionFrames++;
3382 			} else {
3383 				sc->mibdata.dot3StatsMultipleCollisionFrames++;
3384 			}
3385 			sc->mibdata.dot3StatsCollFrequencies[col-1]++;
3386 		}
3387 
3388 		/*
3389 		 * Update transmission statistics.
3390 		 * Be sure to reflect number of excessive collisions.
3391 		 */
3392 		col = sc->tx_excolls;
3393 		sc->sc_if.if_opackets += sc->txb_sched - col;
3394 		sc->sc_if.if_oerrors += col;
3395 		sc->sc_if.if_collisions += col * 16;
3396 		sc->mibdata.dot3StatsExcessiveCollisions += col;
3397 		sc->mibdata.dot3StatsCollFrequencies[15] += col;
3398 		sc->txb_sched = 0;
3399 
3400 		/*
3401 		 * The transmitter is no more active.
3402 		 * Reset output active flag and watchdog timer.
3403 		 */
3404 		sc->sc_if.if_flags &= ~IFF_OACTIVE;
3405 		sc->sc_if.if_timer = 0;
3406 
3407 		/*
3408 		 * If more data is ready to transmit in the buffer, start
3409 		 * transmitting them.  Otherwise keep transmitter idle,
3410 		 * even if more data is queued.  This gives receive
3411 		 * process a slight priority.
3412 		 */
3413 		if ( sc->txb_count > 0 ) fe_xmit( sc );
3414 	}
3415 }
3416 
3417 /*
3418  * Ethernet interface receiver interrupt.
3419  */
3420 static void
3421 fe_rint ( struct fe_softc * sc, u_char rstat )
3422 {
3423 	u_short len;
3424 	u_char status;
3425 	int i;
3426 
3427 	/*
3428 	 * Update statistics if this interrupt is caused by an error.
3429 	 * Note that, when the system was not sufficiently fast, the
3430 	 * receive interrupt might not be acknowledged immediately.  If
3431 	 * one or more errornous frames were received before this routine
3432 	 * was scheduled, they are ignored, and the following error stats
3433 	 * give less than real values.
3434 	 */
3435 	if ( rstat & ( FE_D1_OVRFLO | FE_D1_CRCERR
3436 		     | FE_D1_ALGERR | FE_D1_SRTPKT ) ) {
3437 		if ( rstat & FE_D1_OVRFLO )
3438 			sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3439 		if ( rstat & FE_D1_CRCERR )
3440 			sc->mibdata.dot3StatsFCSErrors++;
3441 		if ( rstat & FE_D1_ALGERR )
3442 			sc->mibdata.dot3StatsAlignmentErrors++;
3443 #if 0
3444 		/* The reference MAC receiver defined in 802.3
3445 		   silently ignores short frames (RUNTs) without
3446 		   notifying upper layer.  RFC 1650 (dot3 MIB) is
3447 		   based on the 802.3, and it has no stats entry for
3448 		   RUNTs...  */
3449 		if ( rstat & FE_D1_SRTPKT )
3450 			sc->mibdata.dot3StatsFrameTooShorts++; /* :-) */
3451 #endif
3452 		sc->sc_if.if_ierrors++;
3453 	}
3454 
3455 	/*
3456 	 * MB86960 has a flag indicating "receive queue empty."
3457 	 * We just loop, checking the flag, to pull out all received
3458 	 * packets.
3459 	 *
3460 	 * We limit the number of iterations to avoid infinite-loop.
3461 	 * The upper bound is set to unrealistic high value.
3462 	 */
3463 	for ( i = 0; i < FE_MAX_RECV_COUNT * 2; i++ ) {
3464 
3465 		/* Stop the iteration if 86960 indicates no packets.  */
3466 		if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) return;
3467 
3468 		/*
3469 		 * Extract a receive status byte.
3470 		 * As our 86960 is in 16 bit bus access mode, we have to
3471 		 * use inw() to get the status byte.  The significant
3472 		 * value is returned in lower 8 bits.
3473 		 */
3474 #ifdef FE_8BIT_SUPPORT
3475 		if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3476 		{
3477 			status = inb( sc->ioaddr[ FE_BMPR8 ] );
3478 			( void ) inb( sc->ioaddr[ FE_BMPR8 ] );
3479 		}
3480 		else
3481 #endif
3482 		{
3483 			status = ( u_char )inw( sc->ioaddr[ FE_BMPR8 ] );
3484 		}
3485 
3486 		/*
3487 		 * Extract the packet length.
3488 		 * It is a sum of a header (14 bytes) and a payload.
3489 		 * CRC has been stripped off by the 86960.
3490 		 */
3491 #ifdef FE_8BIT_SUPPORT
3492 		if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3493 		{
3494 			len  =   inb( sc->ioaddr[ FE_BMPR8 ] );
3495 			len |= ( inb( sc->ioaddr[ FE_BMPR8 ] ) << 8 );
3496 		}
3497 		else
3498 #endif
3499 		{
3500 			len = inw( sc->ioaddr[ FE_BMPR8 ] );
3501 		}
3502 
3503 		/*
3504 		 * AS our 86960 is programed to ignore errored frame,
3505 		 * we must not see any error indication in the
3506 		 * receive buffer.  So, any error condition is a
3507 		 * serious error, e.g., out-of-sync of the receive
3508 		 * buffer pointers.
3509 		 */
3510 		if ( ( status & 0xF0 ) != 0x20
3511 		     || len > ETHER_MAX_LEN - ETHER_CRC_LEN
3512 		     || len < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
3513 			printf("fe%d: RX buffer out-of-sync\n", sc->sc_unit);
3514 			sc->sc_if.if_ierrors++;
3515 			sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3516 			fe_reset(sc);
3517 			return;
3518 		}
3519 
3520 		/*
3521 		 * Go get a packet.
3522 		 */
3523 		if ( fe_get_packet( sc, len ) < 0 ) {
3524 			/*
3525 			 * Negative return from fe_get_packet()
3526 			 * indicates no available mbuf.  We stop
3527 			 * receiving packets, even if there are more
3528 			 * in the buffer.  We hope we can get more
3529 			 * mbuf next time.
3530 			 */
3531 			sc->sc_if.if_ierrors++;
3532 			sc->mibdata.dot3StatsMissedFrames++;
3533 			fe_droppacket( sc, len );
3534 			return;
3535 		}
3536 
3537 		/* Successfully received a packet.  Update stat.  */
3538 		sc->sc_if.if_ipackets++;
3539 	}
3540 
3541 	/* Maximum number of frames has been received.  Something
3542            strange is happening here... */
3543 	printf("fe%d: unusual receive flood\n", sc->sc_unit);
3544 	sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3545 	fe_reset(sc);
3546 }
3547 
3548 /*
3549  * Ethernet interface interrupt processor
3550  */
3551 static void
3552 feintr ( int unit )
3553 {
3554 	struct fe_softc *sc = &fe_softc[unit];
3555 	u_char tstat, rstat;
3556 	int loop_count = FE_MAX_LOOP;
3557 
3558 	/* Loop until there are no more new interrupt conditions.  */
3559 	while (loop_count-- > 0) {
3560 		/*
3561 		 * Get interrupt conditions, masking unneeded flags.
3562 		 */
3563 		tstat = inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_TMASK;
3564 		rstat = inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_RMASK;
3565 		if ( tstat == 0 && rstat == 0 ) return;
3566 
3567 		/*
3568 		 * Reset the conditions we are acknowledging.
3569 		 */
3570 		outb( sc->ioaddr[ FE_DLCR0 ], tstat );
3571 		outb( sc->ioaddr[ FE_DLCR1 ], rstat );
3572 
3573 		/*
3574 		 * Handle transmitter interrupts.
3575 		 */
3576 		if ( tstat ) {
3577 			fe_tint( sc, tstat );
3578 		}
3579 
3580 		/*
3581 		 * Handle receiver interrupts
3582 		 */
3583 		if ( rstat ) {
3584 			fe_rint( sc, rstat );
3585 		}
3586 
3587 		/*
3588 		 * Update the multicast address filter if it is
3589 		 * needed and possible.  We do it now, because
3590 		 * we can make sure the transmission buffer is empty,
3591 		 * and there is a good chance that the receive queue
3592 		 * is empty.  It will minimize the possibility of
3593 		 * packet loss.
3594 		 */
3595 		if ( sc->filter_change
3596 		  && sc->txb_count == 0 && sc->txb_sched == 0 ) {
3597 			fe_loadmar(sc);
3598 			sc->sc_if.if_flags &= ~IFF_OACTIVE;
3599 		}
3600 
3601 		/*
3602 		 * If it looks like the transmitter can take more data,
3603 		 * attempt to start output on the interface. This is done
3604 		 * after handling the receiver interrupt to give the
3605 		 * receive operation priority.
3606 		 *
3607 		 * BTW, I'm not sure in what case the OACTIVE is on at
3608 		 * this point.  Is the following test redundant?
3609 		 *
3610 		 * No.  This routine polls for both transmitter and
3611 		 * receiver interrupts.  86960 can raise a receiver
3612 		 * interrupt when the transmission buffer is full.
3613 		 */
3614 		if ( ( sc->sc_if.if_flags & IFF_OACTIVE ) == 0 ) {
3615 			fe_start( &sc->sc_if );
3616 		}
3617 
3618 	}
3619 
3620 	printf("fe%d: too many loops\n", sc->sc_unit);
3621 	return;
3622 }
3623 
3624 /*
3625  * Process an ioctl request. This code needs some work - it looks
3626  * pretty ugly.
3627  */
3628 static int
3629 fe_ioctl ( struct ifnet * ifp, u_long command, caddr_t data )
3630 {
3631 	struct fe_softc *sc = ifp->if_softc;
3632 	struct ifreq *ifr = (struct ifreq *)data;
3633 	int s, error = 0;
3634 
3635 	s = splimp();
3636 
3637 	switch (command) {
3638 
3639 	  case SIOCSIFADDR:
3640 	  case SIOCGIFADDR:
3641 	  case SIOCSIFMTU:
3642 		/* Just an ordinary action.  */
3643 		error = ether_ioctl(ifp, command, data);
3644 		break;
3645 
3646 	  case SIOCSIFFLAGS:
3647 		/*
3648 		 * Switch interface state between "running" and
3649 		 * "stopped", reflecting the UP flag.
3650 		 */
3651 		if ( sc->sc_if.if_flags & IFF_UP ) {
3652 			if ( ( sc->sc_if.if_flags & IFF_RUNNING ) == 0 ) {
3653 				fe_init(sc);
3654 			}
3655 		} else {
3656 			if ( ( sc->sc_if.if_flags & IFF_RUNNING ) != 0 ) {
3657 				fe_stop(sc);
3658 			}
3659 		}
3660 
3661 		/*
3662 		 * Promiscuous and/or multicast flags may have changed,
3663 		 * so reprogram the multicast filter and/or receive mode.
3664 		 */
3665 		fe_setmode( sc );
3666 
3667 		/* Done.  */
3668 		break;
3669 
3670 	  case SIOCADDMULTI:
3671 	  case SIOCDELMULTI:
3672 		/*
3673 		 * Multicast list has changed; set the hardware filter
3674 		 * accordingly.
3675 		 */
3676 		fe_setmode( sc );
3677 		break;
3678 
3679 	  case SIOCSIFMEDIA:
3680 	  case SIOCGIFMEDIA:
3681 		/* Let if_media to handle these commands and to call
3682 		   us back.  */
3683 		error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
3684 		break;
3685 
3686 	  default:
3687 		error = EINVAL;
3688 		break;
3689 	}
3690 
3691 	(void) splx(s);
3692 	return (error);
3693 }
3694 
3695 /*
3696  * Retrieve packet from receive buffer and send to the next level up via
3697  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
3698  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
3699  */
3700 static int
3701 fe_get_packet ( struct fe_softc * sc, u_short len )
3702 {
3703 	struct ether_header *eh;
3704 	struct mbuf *m;
3705 
3706 	/*
3707 	 * NFS wants the data be aligned to the word (4 byte)
3708 	 * boundary.  Ethernet header has 14 bytes.  There is a
3709 	 * 2-byte gap.
3710 	 */
3711 #define NFS_MAGIC_OFFSET 2
3712 
3713 	/*
3714 	 * This function assumes that an Ethernet packet fits in an
3715 	 * mbuf (with a cluster attached when necessary.)  On FreeBSD
3716 	 * 2.0 for x86, which is the primary target of this driver, an
3717 	 * mbuf cluster has 4096 bytes, and we are happy.  On ancient
3718 	 * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
3719 	 * however.  If the following #error message were printed upon
3720 	 * compile, you need to rewrite this function.
3721 	 */
3722 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
3723 #error "Too small MCLBYTES to use fe driver."
3724 #endif
3725 
3726 	/*
3727 	 * Our strategy has one more problem.  There is a policy on
3728 	 * mbuf cluster allocation.  It says that we must have at
3729 	 * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
3730 	 * allocate a cluster.  For a packet of a size between
3731 	 * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
3732 	 * On the other hand, the current code is short, simple,
3733 	 * and fast, however.  It does no harmful thing, just waists
3734 	 * some memory.  Any comments?  FIXME.
3735 	 */
3736 
3737 	/* Allocate an mbuf with packet header info.  */
3738 	MGETHDR(m, M_DONTWAIT, MT_DATA);
3739 	if ( m == NULL ) return -1;
3740 
3741 	/* Attach a cluster if this packet doesn't fit in a normal mbuf.  */
3742 	if ( len > MHLEN - NFS_MAGIC_OFFSET ) {
3743 		MCLGET( m, M_DONTWAIT );
3744 		if ( !( m->m_flags & M_EXT ) ) {
3745 			m_freem( m );
3746 			return -1;
3747 		}
3748 	}
3749 
3750 	/* Initialize packet header info.  */
3751 	m->m_pkthdr.rcvif = &sc->sc_if;
3752 	m->m_pkthdr.len = len;
3753 
3754 	/* Set the length of this packet.  */
3755 	m->m_len = len;
3756 
3757 	/* The following silliness is to make NFS happy */
3758 	m->m_data += NFS_MAGIC_OFFSET;
3759 
3760 	/* Get (actually just point to) the header part.  */
3761 	eh = mtod(m, struct ether_header *);
3762 
3763 	/* Get a packet.  */
3764 #ifdef FE_8BIT_SUPPORT
3765 	if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3766 	{
3767 		insb( sc->ioaddr[ FE_BMPR8 ], eh,   len );
3768 	}
3769 	else
3770 #endif
3771 	{
3772 		insw( sc->ioaddr[ FE_BMPR8 ], eh, ( len + 1 ) >> 1 );
3773 	}
3774 
3775 #define ETHER_ADDR_IS_MULTICAST(A) (*(char *)(A) & 1)
3776 
3777 #if NBPF > 0
3778 	/*
3779 	 * Check if there's a BPF listener on this interface.
3780 	 * If it is, hand off the raw packet to bpf.
3781 	 */
3782 	if ( sc->sc_if.if_bpf ) {
3783 		bpf_mtap( &sc->sc_if, m );
3784 	}
3785 #endif
3786 
3787 #ifdef BRIDGE
3788 	if (do_bridge) {
3789 		struct ifnet *ifp;
3790 
3791 		ifp = bridge_in(m);
3792 		if (ifp == BDG_DROP) {
3793 			m_freem(m);
3794 			return 0;
3795 		}
3796 		if (ifp != BDG_LOCAL)
3797 			bdg_forward(&m, ifp); /* not local, need forwarding */
3798 		if (ifp == BDG_LOCAL || ifp == BDG_BCAST || ifp == BDG_MCAST)
3799 			goto getit;
3800 		/* not local and not multicast, just drop it */
3801 		if (m)
3802 			m_freem(m);
3803 		return 0;
3804 	}
3805 #endif
3806 
3807 	/*
3808 	 * Make sure this packet is (or may be) directed to us.
3809 	 * That is, the packet is either unicasted to our address,
3810 	 * or broad/multi-casted.  If any other packets are
3811 	 * received, it is an indication of an error -- probably
3812 	 * 86960 is in a wrong operation mode.
3813 	 * Promiscuous mode is an exception.  Under the mode, all
3814 	 * packets on the media must be received.  (We must have
3815 	 * programmed the 86960 so.)
3816 	 */
3817 
3818 	if ( ( sc->sc_if.if_flags & IFF_PROMISC )
3819 	  && !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
3820 	  && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
3821 		/*
3822 		 * The packet was not for us.  This is normal since
3823 		 * we are now in promiscuous mode.  Just drop the packet.
3824 		 */
3825 		m_freem( m );
3826 		return 0;
3827 	}
3828 
3829 #ifdef BRIDGE
3830 getit:
3831 #endif
3832 	/* Strip off the Ethernet header.  */
3833 	m->m_pkthdr.len -= sizeof ( struct ether_header );
3834 	m->m_len -= sizeof ( struct ether_header );
3835 	m->m_data += sizeof ( struct ether_header );
3836 
3837 	/* Feed the packet to upper layer.  */
3838 	ether_input( &sc->sc_if, eh, m );
3839 	return 0;
3840 }
3841 
3842 /*
3843  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
3844  * Returns number of bytes actually written, including length word.
3845  *
3846  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
3847  * Packets shorter than Ethernet minimum are legal, and we pad them
3848  * before sending out.  An exception is "partial" packets which are
3849  * shorter than mandatory Ethernet header.
3850  */
3851 static void
3852 fe_write_mbufs ( struct fe_softc *sc, struct mbuf *m )
3853 {
3854 	u_short addr_bmpr8 = sc->ioaddr[ FE_BMPR8 ];
3855 	u_short length, len;
3856 	struct mbuf *mp;
3857 	u_char *data;
3858 	u_short savebyte;	/* WARNING: Architecture dependent!  */
3859 #define NO_PENDING_BYTE 0xFFFF
3860 
3861 	static u_char padding [ ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN ];
3862 
3863 #ifdef DIAGNOSTIC
3864 	/* First, count up the total number of bytes to copy */
3865 	length = 0;
3866 	for ( mp = m; mp != NULL; mp = mp->m_next ) {
3867 		length += mp->m_len;
3868 	}
3869 	/* Check if this matches the one in the packet header.  */
3870 	if ( length != m->m_pkthdr.len ) {
3871 		printf("fe%d: packet length mismatch? (%d/%d)\n", sc->sc_unit,
3872 		       length, m->m_pkthdr.len);
3873 	}
3874 #else
3875 	/* Just use the length value in the packet header.  */
3876 	length = m->m_pkthdr.len;
3877 #endif
3878 
3879 #ifdef DIAGNOSTIC
3880 	/*
3881 	 * Should never send big packets.  If such a packet is passed,
3882 	 * it should be a bug of upper layer.  We just ignore it.
3883 	 * ... Partial (too short) packets, neither.
3884 	 */
3885 	if ( length < ETHER_HDR_LEN
3886 	  || length > ETHER_MAX_LEN - ETHER_CRC_LEN ) {
3887 		printf("fe%d: got an out-of-spec packet (%u bytes) to send\n",
3888 			sc->sc_unit, length);
3889 		sc->sc_if.if_oerrors++;
3890 		sc->mibdata.dot3StatsInternalMacTransmitErrors++;
3891 		return;
3892 	}
3893 #endif
3894 
3895 	/*
3896 	 * Put the length word for this frame.
3897 	 * Does 86960 accept odd length?  -- Yes.
3898 	 * Do we need to pad the length to minimum size by ourselves?
3899 	 * -- Generally yes.  But for (or will be) the last
3900 	 * packet in the transmission buffer, we can skip the
3901 	 * padding process.  It may gain performance slightly.  FIXME.
3902 	 */
3903 #ifdef FE_8BIT_SUPPORT
3904 	if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3905 	{
3906 		len = max( length, ETHER_MIN_LEN - ETHER_CRC_LEN );
3907 		outb( addr_bmpr8,   len & 0x00ff );
3908 		outb( addr_bmpr8, ( len & 0xff00 ) >> 8 );
3909 	}
3910 	else
3911 #endif
3912 	{
3913 		outw( addr_bmpr8, max( length, ETHER_MIN_LEN - ETHER_CRC_LEN ) );
3914 	}
3915 
3916 	/*
3917 	 * Update buffer status now.
3918 	 * Truncate the length up to an even number, since we use outw().
3919 	 */
3920 #ifdef FE_8BIT_SUPPORT
3921 	if ((sc->proto_dlcr6 & FE_D6_SBW) != FE_D6_SBW_BYTE)
3922 #endif
3923 	{
3924 		length = ( length + 1 ) & ~1;
3925 	}
3926 	sc->txb_free -= FE_DATA_LEN_LEN + max( length, ETHER_MIN_LEN - ETHER_CRC_LEN);
3927 	sc->txb_count++;
3928 
3929 	/*
3930 	 * Transfer the data from mbuf chain to the transmission buffer.
3931 	 * MB86960 seems to require that data be transferred as words, and
3932 	 * only words.  So that we require some extra code to patch
3933 	 * over odd-length mbufs.
3934 	 */
3935 #ifdef FE_8BIT_SUPPORT
3936 	if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3937 	{
3938 		/* 8-bit cards are easy.  */
3939 		for ( mp = m; mp != 0; mp = mp->m_next ) {
3940 			if ( mp->m_len ) {
3941 				outsb( addr_bmpr8, mtod(mp, caddr_t), mp->m_len );
3942 			}
3943 		}
3944 	}
3945 	else
3946 #endif
3947 	{
3948 		/* 16-bit cards are a pain.  */
3949 		savebyte = NO_PENDING_BYTE;
3950 		for ( mp = m; mp != 0; mp = mp->m_next ) {
3951 
3952 			/* Ignore empty mbuf.  */
3953 			len = mp->m_len;
3954 			if ( len == 0 ) continue;
3955 
3956 			/* Find the actual data to send.  */
3957 			data = mtod(mp, caddr_t);
3958 
3959 			/* Finish the last byte.  */
3960 			if ( savebyte != NO_PENDING_BYTE ) {
3961 				outw( addr_bmpr8, savebyte | ( *data << 8 ) );
3962 				data++;
3963 				len--;
3964 				savebyte = NO_PENDING_BYTE;
3965 			}
3966 
3967 			/* output contiguous words */
3968 			if (len > 1) {
3969 				outsw( addr_bmpr8, data, len >> 1);
3970 				data += len & ~1;
3971 				len &= 1;
3972 			}
3973 
3974 			/* Save a remaining byte, if there is one.  */
3975 			if ( len > 0 ) {
3976 				savebyte = *data;
3977 			}
3978 		}
3979 
3980 		/* Spit the last byte, if the length is odd.  */
3981 		if ( savebyte != NO_PENDING_BYTE ) {
3982 			outw( addr_bmpr8, savebyte );
3983 		}
3984 	}
3985 
3986 	/* Pad to the Ethernet minimum length, if the packet is too short.  */
3987 	if ( length < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
3988 #ifdef FE_8BIT_SUPPORT
3989 		if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3990 		{
3991 			outsb( addr_bmpr8, padding,   ETHER_MIN_LEN - ETHER_CRC_LEN - length );
3992 		}
3993 		else
3994 #endif
3995 		{
3996 			outsw( addr_bmpr8, padding, ( ETHER_MIN_LEN - ETHER_CRC_LEN - length ) >> 1);
3997 		}
3998 	}
3999 }
4000 
4001 /*
4002  * Compute hash value for an Ethernet address
4003  */
4004 static int
4005 fe_hash ( u_char * ep )
4006 {
4007 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
4008 
4009 	u_long hash = 0xFFFFFFFFL;
4010 	int i, j;
4011 	u_char b;
4012 	u_long m;
4013 
4014 	for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
4015 		b = *ep++;
4016 		for ( j = 8; --j >= 0; ) {
4017 			m = hash;
4018 			hash >>= 1;
4019 			if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
4020 			b >>= 1;
4021 		}
4022 	}
4023 	return ( ( int )( hash >> 26 ) );
4024 }
4025 
4026 /*
4027  * Compute the multicast address filter from the
4028  * list of multicast addresses we need to listen to.
4029  */
4030 static struct fe_filter
4031 fe_mcaf ( struct fe_softc *sc )
4032 {
4033 	int index;
4034 	struct fe_filter filter;
4035 	struct ifmultiaddr *ifma;
4036 
4037 	filter = fe_filter_nothing;
4038 	for (ifma = sc->arpcom.ac_if.if_multiaddrs.lh_first; ifma;
4039 	     ifma = ifma->ifma_link.le_next) {
4040 		if (ifma->ifma_addr->sa_family != AF_LINK)
4041 			continue;
4042 		index = fe_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
4043 #ifdef FE_DEBUG
4044 		printf("fe%d: hash(%6D) == %d\n",
4045 			sc->sc_unit, enm->enm_addrlo , ":", index);
4046 #endif
4047 
4048 		filter.data[index >> 3] |= 1 << (index & 7);
4049 	}
4050 	return ( filter );
4051 }
4052 
4053 /*
4054  * Calculate a new "multicast packet filter" and put the 86960
4055  * receiver in appropriate mode.
4056  */
4057 static void
4058 fe_setmode ( struct fe_softc *sc )
4059 {
4060 	int flags = sc->sc_if.if_flags;
4061 
4062 	/*
4063 	 * If the interface is not running, we postpone the update
4064 	 * process for receive modes and multicast address filter
4065 	 * until the interface is restarted.  It reduces some
4066 	 * complicated job on maintaining chip states.  (Earlier versions
4067 	 * of this driver had a bug on that point...)
4068 	 *
4069 	 * To complete the trick, fe_init() calls fe_setmode() after
4070 	 * restarting the interface.
4071 	 */
4072 	if ( !( flags & IFF_RUNNING ) ) return;
4073 
4074 	/*
4075 	 * Promiscuous mode is handled separately.
4076 	 */
4077 	if ( flags & IFF_PROMISC ) {
4078 		/*
4079 		 * Program 86960 to receive all packets on the segment
4080 		 * including those directed to other stations.
4081 		 * Multicast filter stored in MARs are ignored
4082 		 * under this setting, so we don't need to update it.
4083 		 *
4084 		 * Promiscuous mode in FreeBSD 2 is used solely by
4085 		 * BPF, and BPF only listens to valid (no error) packets.
4086 		 * So, we ignore erroneous ones even in this mode.
4087 		 * (Older versions of fe driver mistook the point.)
4088 		 */
4089 		outb( sc->ioaddr[ FE_DLCR5 ],
4090 			sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1 );
4091 		sc->filter_change = 0;
4092 		return;
4093 	}
4094 
4095 	/*
4096 	 * Turn the chip to the normal (non-promiscuous) mode.
4097 	 */
4098 	outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 | FE_D5_AFM1 );
4099 
4100 	/*
4101 	 * Find the new multicast filter value.
4102 	 */
4103 	if ( flags & IFF_ALLMULTI ) {
4104 		sc->filter = fe_filter_all;
4105 	} else {
4106 		sc->filter = fe_mcaf( sc );
4107 	}
4108 	sc->filter_change = 1;
4109 
4110 	/*
4111 	 * We have to update the multicast filter in the 86960, A.S.A.P.
4112 	 *
4113 	 * Note that the DLC (Data Link Control unit, i.e. transmitter
4114 	 * and receiver) must be stopped when feeding the filter, and
4115 	 * DLC trashes all packets in both transmission and receive
4116 	 * buffers when stopped.
4117 	 *
4118 	 * To reduce the packet loss, we delay the filter update
4119 	 * process until buffers are empty.
4120 	 */
4121 	if ( sc->txb_sched == 0 && sc->txb_count == 0
4122           && !( inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_D1_PKTRDY ) ) {
4123 		/*
4124 		 * Buffers are (apparently) empty.  Load
4125 		 * the new filter value into MARs now.
4126 		 */
4127 		fe_loadmar(sc);
4128 	} else {
4129 		/*
4130 		 * Buffers are not empty.  Mark that we have to update
4131 		 * the MARs.  The new filter will be loaded by feintr()
4132 		 * later.
4133 		 */
4134 	}
4135 }
4136 
4137 /*
4138  * Load a new multicast address filter into MARs.
4139  *
4140  * The caller must have splimp'ed before fe_loadmar.
4141  * This function starts the DLC upon return.  So it can be called only
4142  * when the chip is working, i.e., from the driver's point of view, when
4143  * a device is RUNNING.  (I mistook the point in previous versions.)
4144  */
4145 static void
4146 fe_loadmar ( struct fe_softc * sc )
4147 {
4148 	/* Stop the DLC (transmitter and receiver).  */
4149 	DELAY( 200 );
4150 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
4151 	DELAY( 200 );
4152 
4153 	/* Select register bank 1 for MARs.  */
4154 	outb( sc->ioaddr[ FE_DLCR7 ],
4155 		sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
4156 
4157 	/* Copy filter value into the registers.  */
4158 	outblk( sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN );
4159 
4160 	/* Restore the bank selection for BMPRs (i.e., runtime registers).  */
4161 	outb( sc->ioaddr[ FE_DLCR7 ],
4162 		sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
4163 
4164 	/* Restart the DLC.  */
4165 	DELAY( 200 );
4166 	outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
4167 	DELAY( 200 );
4168 
4169 	/* We have just updated the filter.  */
4170 	sc->filter_change = 0;
4171 }
4172 
4173 /* Change the media selection.  */
4174 static int
4175 fe_medchange (struct ifnet *ifp)
4176 {
4177 	struct fe_softc *sc = (struct fe_softc *)ifp->if_softc;
4178 
4179 #ifdef DIAGNOSTIC
4180 	/* If_media should not pass any request for a media which this
4181 	   interface doesn't support.  */
4182 	int b;
4183 
4184 	for (b = 0; bit2media[b] != 0; b++) {
4185 		if (bit2media[b] == sc->media.ifm_media) break;
4186 	}
4187 	if (((1 << b) & sc->mbitmap) == 0) {
4188 		printf("fe%d: got an unsupported media request (0x%x)\n",
4189 		       sc->sc_unit, sc->media.ifm_media);
4190 		return EINVAL;
4191 	}
4192 #endif
4193 
4194 	/* We don't actually change media when the interface is down.
4195 	   fe_init() will do the job, instead.  Should we also wait
4196 	   until the transmission buffer being empty?  Changing the
4197 	   media when we are sending a frame will cause two garbages
4198 	   on wires, one on old media and another on new.  FIXME */
4199 	if (sc->sc_if.if_flags & IFF_UP) {
4200 		if (sc->msel) sc->msel(sc);
4201 	}
4202 
4203 	return 0;
4204 }
4205 
4206 /* I don't know how I can support media status callback... FIXME.  */
4207 static void
4208 fe_medstat (struct ifnet *ifp, struct ifmediareq *ifmr)
4209 {
4210 	(void)ifp;
4211 	(void)ifmr;
4212 }
4213