]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/if_fe.c
This commit was generated by cvs2svn to compensate for changes in r56893,
[FreeBSD/FreeBSD.git] / sys / i386 / isa / if_fe.c
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. <seki@sysrap.cs.fujitsu.co.jp>
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 "opt_fe.h"
74 #include "opt_inet.h"
75 #include "opt_ipx.h"
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/sockio.h>
80 #include <sys/mbuf.h>
81 #include <sys/socket.h>
82
83 #include <net/ethernet.h>
84 #include <net/if.h>
85 #include <net/if_dl.h>
86 #include <net/if_mib.h>
87 #include <net/if_media.h>
88 #include <net/if_types.h>
89
90 #include <netinet/in.h>
91 #include <netinet/if_ether.h>
92
93 #include <net/bpf.h>
94
95 #ifdef BRIDGE
96 #include <net/bridge.h>
97 #endif
98
99 #include <machine/clock.h>
100
101 #include <i386/isa/isa_device.h>
102 #include <i386/isa/icu.h>
103
104 /* PCCARD suport */
105 /* XXX FIXME! doesn't work with new pccard code, must be converted! */
106 #ifdef notdef
107 #include "card.h"
108 #endif
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 <masui@cs.titech.ac.jp>
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 BPF is in the kernel, call the attach for it.  */
2777         bpfattach(&sc->sc_if, DLT_EN10MB, sizeof(struct ether_header));
2778         return 1;
2779 }
2780
2781 /*
2782  * Reset interface, after some (hardware) trouble is deteced.
2783  */
2784 static void
2785 fe_reset (struct fe_softc *sc)
2786 {
2787         /* Record how many packets are lost by this accident.  */
2788         sc->sc_if.if_oerrors += sc->txb_sched + sc->txb_count;
2789         sc->mibdata.dot3StatsInternalMacTransmitErrors++;
2790
2791         /* Put the interface into known initial state.  */
2792         fe_stop(sc);
2793         if (sc->sc_if.if_flags & IFF_UP) fe_init(sc);
2794 }
2795
2796 /*
2797  * Stop everything on the interface.
2798  *
2799  * All buffered packets, both transmitting and receiving,
2800  * if any, will be lost by stopping the interface.
2801  */
2802 static void
2803 fe_stop (struct fe_softc *sc)
2804 {
2805         int s;
2806
2807         s = splimp();
2808
2809         /* Disable interrupts.  */
2810         outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
2811         outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
2812
2813         /* Stop interface hardware.  */
2814         DELAY( 200 );
2815         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2816         DELAY( 200 );
2817
2818         /* Clear all interrupt status.  */
2819         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );
2820         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );
2821
2822         /* Put the chip in stand-by mode.  */
2823         DELAY( 200 );
2824         outb( sc->ioaddr[ FE_DLCR7 ], sc->proto_dlcr7 | FE_D7_POWER_DOWN );
2825         DELAY( 200 );
2826
2827         /* Reset transmitter variables and interface flags.  */
2828         sc->sc_if.if_flags &= ~( IFF_OACTIVE | IFF_RUNNING );
2829         sc->sc_if.if_timer = 0;
2830         sc->txb_free = sc->txb_size;
2831         sc->txb_count = 0;
2832         sc->txb_sched = 0;
2833
2834         /* MAR loading can be delayed.  */
2835         sc->filter_change = 0;
2836
2837         /* Call a device-specific hook.  */
2838         if ( sc->stop ) sc->stop( sc );
2839
2840         (void) splx(s);
2841 }
2842
2843 /*
2844  * Device timeout/watchdog routine. Entered if the device neglects to
2845  * generate an interrupt after a transmit has been started on it.
2846  */
2847 static void
2848 fe_watchdog ( struct ifnet *ifp )
2849 {
2850         struct fe_softc *sc = (struct fe_softc *)ifp;
2851
2852         /* A "debug" message.  */
2853         printf("fe%d: transmission timeout (%d+%d)%s\n",
2854                ifp->if_unit, sc->txb_sched, sc->txb_count,
2855                (ifp->if_flags & IFF_UP) ? "" : " when down");
2856         if ( sc->sc_if.if_opackets == 0 && sc->sc_if.if_ipackets == 0 ) {
2857                 printf("fe%d: wrong IRQ setting in config?\n", ifp->if_unit);
2858         }
2859         fe_reset( sc );
2860 }
2861
2862 /*
2863  * Initialize device.
2864  */
2865 static void
2866 fe_init (void * xsc)
2867 {
2868         struct fe_softc *sc = xsc;
2869         int s;
2870
2871         /* We need an address. */
2872         if (TAILQ_EMPTY(&sc->sc_if.if_addrhead)) { /* XXX unlikely */
2873 #ifdef DIAGNOSTIC
2874                 printf("fe%d: init() without any address\n", sc->sc_unit);
2875 #endif
2876                 return;
2877         }
2878
2879         /* Start initializing 86960.  */
2880         s = splimp();
2881
2882         /* Call a hook before we start initializing the chip.  */
2883         if ( sc->init ) sc->init( sc );
2884
2885         /*
2886          * Make sure to disable the chip, also.
2887          * This may also help re-programming the chip after
2888          * hot insertion of PCMCIAs.
2889          */
2890         DELAY( 200 );
2891         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
2892         DELAY( 200 );
2893
2894         /* Power up the chip and select register bank for DLCRs.  */
2895         DELAY( 200 );
2896         outb( sc->ioaddr[ FE_DLCR7 ],
2897                 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP );
2898         DELAY( 200 );
2899
2900         /* Feed the station address.  */
2901         outblk( sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN );
2902
2903         /* Clear multicast address filter to receive nothing.  */
2904         outb( sc->ioaddr[ FE_DLCR7 ],
2905                 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
2906         outblk( sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN );
2907
2908         /* Select the BMPR bank for runtime register access.  */
2909         outb( sc->ioaddr[ FE_DLCR7 ],
2910                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
2911
2912         /* Initialize registers.  */
2913         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );   /* Clear all bits.  */
2914         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );   /* ditto.  */
2915         outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
2916         outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
2917         outb( sc->ioaddr[ FE_DLCR4 ], sc->proto_dlcr4 );
2918         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
2919         outb( sc->ioaddr[ FE_BMPR10 ], 0x00 );
2920         outb( sc->ioaddr[ FE_BMPR11 ], FE_B11_CTRL_SKIP | FE_B11_MODE1 );
2921         outb( sc->ioaddr[ FE_BMPR12 ], 0x00 );
2922         outb( sc->ioaddr[ FE_BMPR13 ], sc->proto_bmpr13 );
2923         outb( sc->ioaddr[ FE_BMPR14 ], 0x00 );
2924         outb( sc->ioaddr[ FE_BMPR15 ], 0x00 );
2925
2926         /* Enable interrupts.  */
2927         outb( sc->ioaddr[ FE_DLCR2 ], FE_TMASK );
2928         outb( sc->ioaddr[ FE_DLCR3 ], FE_RMASK );
2929
2930         /* Select requested media, just before enabling DLC.  */
2931         if (sc->msel) sc->msel(sc);
2932
2933         /* Enable transmitter and receiver.  */
2934         DELAY( 200 );
2935         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
2936         DELAY( 200 );
2937
2938 #ifdef DIAGNOSTIC
2939         /*
2940          * Make sure to empty the receive buffer.
2941          *
2942          * This may be redundant, but *if* the receive buffer were full
2943          * at this point, then the driver would hang.  I have experienced
2944          * some strange hang-up just after UP.  I hope the following
2945          * code solve the problem.
2946          *
2947          * I have changed the order of hardware initialization.
2948          * I think the receive buffer cannot have any packets at this
2949          * point in this version.  The following code *must* be
2950          * redundant now.  FIXME.
2951          *
2952          * I've heard a rumore that on some PC card implementation of
2953          * 8696x, the receive buffer can have some data at this point.
2954          * The following message helps discovering the fact.  FIXME.
2955          */
2956         if ( !( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) ) {
2957                 printf("fe%d: receive buffer has some data after reset\n",
2958                        sc->sc_unit);
2959                 fe_emptybuffer( sc );
2960         }
2961
2962         /* Do we need this here?  Actually, no.  I must be paranoia.  */
2963         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );   /* Clear all bits.  */
2964         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );   /* ditto.  */
2965 #endif
2966
2967         /* Set 'running' flag, because we are now running.   */
2968         sc->sc_if.if_flags |= IFF_RUNNING;
2969
2970         /*
2971          * At this point, the interface is running properly,
2972          * except that it receives *no* packets.  we then call
2973          * fe_setmode() to tell the chip what packets to be
2974          * received, based on the if_flags and multicast group
2975          * list.  It completes the initialization process.
2976          */
2977         fe_setmode( sc );
2978
2979 #if 0
2980         /* ...and attempt to start output queued packets.  */
2981         /* TURNED OFF, because the semi-auto media prober wants to UP
2982            the interface keeping it idle.  The upper layer will soon
2983            start the interface anyway, and there are no significant
2984            delay.  */
2985         fe_start( &sc->sc_if );
2986 #endif
2987
2988         (void) splx(s);
2989 }
2990
2991 /*
2992  * This routine actually starts the transmission on the interface
2993  */
2994 static void
2995 fe_xmit ( struct fe_softc * sc )
2996 {
2997         /*
2998          * Set a timer just in case we never hear from the board again.
2999          * We use longer timeout for multiple packet transmission.
3000          * I'm not sure this timer value is appropriate.  FIXME.
3001          */
3002         sc->sc_if.if_timer = 1 + sc->txb_count;
3003
3004         /* Update txb variables.  */
3005         sc->txb_sched = sc->txb_count;
3006         sc->txb_count = 0;
3007         sc->txb_free = sc->txb_size;
3008         sc->tx_excolls = 0;
3009
3010         /* Start transmitter, passing packets in TX buffer.  */
3011         outb( sc->ioaddr[ FE_BMPR10 ], sc->txb_sched | FE_B10_START );
3012 }
3013
3014 /*
3015  * Start output on interface.
3016  * We make two assumptions here:
3017  *  1) that the current priority is set to splimp _before_ this code
3018  *     is called *and* is returned to the appropriate priority after
3019  *     return
3020  *  2) that the IFF_OACTIVE flag is checked before this code is called
3021  *     (i.e. that the output part of the interface is idle)
3022  */
3023 void
3024 fe_start ( struct ifnet *ifp )
3025 {
3026         struct fe_softc *sc = ifp->if_softc;
3027         struct mbuf *m;
3028
3029 #ifdef DIAGNOSTIC
3030         /* Just a sanity check.  */
3031         if ( ( sc->txb_count == 0 ) != ( sc->txb_free == sc->txb_size ) ) {
3032                 /*
3033                  * Txb_count and txb_free co-works to manage the
3034                  * transmission buffer.  Txb_count keeps track of the
3035                  * used potion of the buffer, while txb_free does unused
3036                  * potion.  So, as long as the driver runs properly,
3037                  * txb_count is zero if and only if txb_free is same
3038                  * as txb_size (which represents whole buffer.)
3039                  */
3040                 printf("fe%d: inconsistent txb variables (%d, %d)\n",
3041                         sc->sc_unit, sc->txb_count, sc->txb_free);
3042                 /*
3043                  * So, what should I do, then?
3044                  *
3045                  * We now know txb_count and txb_free contradicts.  We
3046                  * cannot, however, tell which is wrong.  More
3047                  * over, we cannot peek 86960 transmission buffer or
3048                  * reset the transmission buffer.  (In fact, we can
3049                  * reset the entire interface.  I don't want to do it.)
3050                  *
3051                  * If txb_count is incorrect, leaving it as-is will cause
3052                  * sending of garbage after next interrupt.  We have to
3053                  * avoid it.  Hence, we reset the txb_count here.  If
3054                  * txb_free was incorrect, resetting txb_count just loose
3055                  * some packets.  We can live with it.
3056                  */
3057                 sc->txb_count = 0;
3058         }
3059 #endif
3060
3061         /*
3062          * First, see if there are buffered packets and an idle
3063          * transmitter - should never happen at this point.
3064          */
3065         if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
3066                 printf("fe%d: transmitter idle with %d buffered packets\n",
3067                        sc->sc_unit, sc->txb_count);
3068                 fe_xmit( sc );
3069         }
3070
3071         /*
3072          * Stop accepting more transmission packets temporarily, when
3073          * a filter change request is delayed.  Updating the MARs on
3074          * 86960 flushes the transmission buffer, so it is delayed
3075          * until all buffered transmission packets have been sent
3076          * out.
3077          */
3078         if ( sc->filter_change ) {
3079                 /*
3080                  * Filter change request is delayed only when the DLC is
3081                  * working.  DLC soon raise an interrupt after finishing
3082                  * the work.
3083                  */
3084                 goto indicate_active;
3085         }
3086
3087         for (;;) {
3088
3089                 /*
3090                  * See if there is room to put another packet in the buffer.
3091                  * We *could* do better job by peeking the send queue to
3092                  * know the length of the next packet.  Current version just
3093                  * tests against the worst case (i.e., longest packet).  FIXME.
3094                  *
3095                  * When adding the packet-peek feature, don't forget adding a
3096                  * test on txb_count against QUEUEING_MAX.
3097                  * There is a little chance the packet count exceeds
3098                  * the limit.  Assume transmission buffer is 8KB (2x8KB
3099                  * configuration) and an application sends a bunch of small
3100                  * (i.e., minimum packet sized) packets rapidly.  An 8KB
3101                  * buffer can hold 130 blocks of 62 bytes long...
3102                  */
3103                 if ( sc->txb_free
3104                     < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN ) {
3105                         /* No room.  */
3106                         goto indicate_active;
3107                 }
3108
3109 #if FE_SINGLE_TRANSMISSION
3110                 if ( sc->txb_count > 0 ) {
3111                         /* Just one packet per a transmission buffer.  */
3112                         goto indicate_active;
3113                 }
3114 #endif
3115
3116                 /*
3117                  * Get the next mbuf chain for a packet to send.
3118                  */
3119                 IF_DEQUEUE( &sc->sc_if.if_snd, m );
3120                 if ( m == NULL ) {
3121                         /* No more packets to send.  */
3122                         goto indicate_inactive;
3123                 }
3124
3125                 /*
3126                  * Copy the mbuf chain into the transmission buffer.
3127                  * txb_* variables are updated as necessary.
3128                  */
3129                 fe_write_mbufs( sc, m );
3130
3131                 /* Start transmitter if it's idle.  */
3132                 if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
3133                         fe_xmit( sc );
3134                 }
3135
3136                 /*
3137                  * Tap off here if there is a bpf listener,
3138                  * and the device is *not* in promiscuous mode.
3139                  * (86960 receives self-generated packets if 
3140                  * and only if it is in "receive everything"
3141                  * mode.)
3142                  */
3143                 if ( sc->sc_if.if_bpf
3144                   && !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
3145                         bpf_mtap( &sc->sc_if, m );
3146                 }
3147
3148                 m_freem( m );
3149         }
3150
3151   indicate_inactive:
3152         /*
3153          * We are using the !OACTIVE flag to indicate to
3154          * the outside world that we can accept an
3155          * additional packet rather than that the
3156          * transmitter is _actually_ active.  Indeed, the
3157          * transmitter may be active, but if we haven't
3158          * filled all the buffers with data then we still
3159          * want to accept more.
3160          */
3161         sc->sc_if.if_flags &= ~IFF_OACTIVE;
3162         return;
3163
3164   indicate_active:
3165         /*
3166          * The transmitter is active, and there are no room for
3167          * more outgoing packets in the transmission buffer.
3168          */
3169         sc->sc_if.if_flags |= IFF_OACTIVE;
3170         return;
3171 }
3172
3173 /*
3174  * Drop (skip) a packet from receive buffer in 86960 memory.
3175  */
3176 static void
3177 fe_droppacket ( struct fe_softc * sc, int len )
3178 {
3179         int i;
3180
3181         /*
3182          * 86960 manual says that we have to read 8 bytes from the buffer
3183          * before skip the packets and that there must be more than 8 bytes
3184          * remaining in the buffer when issue a skip command.
3185          * Remember, we have already read 4 bytes before come here.
3186          */
3187         if ( len > 12 ) {
3188                 /* Read 4 more bytes, and skip the rest of the packet.  */
3189 #ifdef FE_8BIT_SUPPORT
3190                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3191                 {
3192                         ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3193                         ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3194                         ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3195                         ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3196                 }
3197                 else
3198 #endif
3199                 {
3200                         ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3201                         ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3202                 }
3203                 outb( sc->ioaddr[ FE_BMPR14 ], FE_B14_SKIP );
3204         } else {
3205                 /* We should not come here unless receiving RUNTs.  */
3206 #ifdef FE_8BIT_SUPPORT
3207                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3208                 {
3209                         for ( i = 0; i < len; i++ ) {
3210                                 ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3211                         }
3212                 }
3213                 else
3214 #endif
3215                 {
3216                         for ( i = 0; i < len; i += 2 ) {
3217                                 ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3218                         }
3219                 }
3220         }
3221 }
3222
3223 #ifdef DIAGNOSTIC
3224 /*
3225  * Empty receiving buffer.
3226  */
3227 static void
3228 fe_emptybuffer ( struct fe_softc * sc )
3229 {
3230         int i;
3231         u_char saved_dlcr5;
3232
3233 #ifdef FE_DEBUG
3234         printf("fe%d: emptying receive buffer\n", sc->sc_unit);
3235 #endif
3236
3237         /*
3238          * Stop receiving packets, temporarily.
3239          */
3240         saved_dlcr5 = inb( sc->ioaddr[ FE_DLCR5 ] );
3241         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
3242         DELAY(1300);
3243
3244         /*
3245          * When we come here, the receive buffer management may
3246          * have been broken.  So, we cannot use skip operation.
3247          * Just discard everything in the buffer.
3248          */
3249 #ifdef FE_8BIT_SUPPORT
3250         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3251         {
3252                 for ( i = 0; i < 65536; i++ ) {
3253                         if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
3254                         ( void )inb( sc->ioaddr[ FE_BMPR8 ] );
3255                 }
3256         }
3257         else
3258 #endif
3259         {
3260                 for ( i = 0; i < 65536; i += 2 ) {
3261                         if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
3262                         ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
3263                 }
3264         }
3265
3266         /*
3267          * Double check.
3268          */
3269         if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) {
3270                 printf("fe%d: could not empty receive buffer\n", sc->sc_unit);
3271                 /* Hmm.  What should I do if this happens?  FIXME.  */
3272         }
3273
3274         /*
3275          * Restart receiving packets.
3276          */
3277         outb( sc->ioaddr[ FE_DLCR5 ], saved_dlcr5 );
3278 }
3279 #endif
3280
3281 /*
3282  * Transmission interrupt handler
3283  * The control flow of this function looks silly.  FIXME.
3284  */
3285 static void
3286 fe_tint ( struct fe_softc * sc, u_char tstat )
3287 {
3288         int left;
3289         int col;
3290
3291         /*
3292          * Handle "excessive collision" interrupt.
3293          */
3294         if ( tstat & FE_D0_COLL16 ) {
3295
3296                 /*
3297                  * Find how many packets (including this collided one)
3298                  * are left unsent in transmission buffer.
3299                  */
3300                 left = inb( sc->ioaddr[ FE_BMPR10 ] );
3301                 printf("fe%d: excessive collision (%d/%d)\n",
3302                        sc->sc_unit, left, sc->txb_sched);
3303
3304                 /*
3305                  * Clear the collision flag (in 86960) here
3306                  * to avoid confusing statistics.
3307                  */
3308                 outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
3309
3310                 /*
3311                  * Restart transmitter, skipping the
3312                  * collided packet.
3313                  *
3314                  * We *must* skip the packet to keep network running
3315                  * properly.  Excessive collision error is an
3316                  * indication of the network overload.  If we
3317                  * tried sending the same packet after excessive
3318                  * collision, the network would be filled with
3319                  * out-of-time packets.  Packets belonging
3320                  * to reliable transport (such as TCP) are resent
3321                  * by some upper layer.
3322                  */
3323                 outb( sc->ioaddr[ FE_BMPR11 ],
3324                         FE_B11_CTRL_SKIP | FE_B11_MODE1 );
3325
3326                 /* Update statistics.  */
3327                 sc->tx_excolls++;
3328         }
3329
3330         /*
3331          * Handle "transmission complete" interrupt.
3332          */
3333         if ( tstat & FE_D0_TXDONE ) {
3334
3335                 /*
3336                  * Add in total number of collisions on last
3337                  * transmission.  We also clear "collision occurred" flag
3338                  * here.
3339                  *
3340                  * 86960 has a design flaw on collision count on multiple
3341                  * packet transmission.  When we send two or more packets
3342                  * with one start command (that's what we do when the
3343                  * transmission queue is crowded), 86960 informs us number
3344                  * of collisions occurred on the last packet on the
3345                  * transmission only.  Number of collisions on previous
3346                  * packets are lost.  I have told that the fact is clearly
3347                  * stated in the Fujitsu document.
3348                  *
3349                  * I considered not to mind it seriously.  Collision
3350                  * count is not so important, anyway.  Any comments?  FIXME.
3351                  */
3352
3353                 if ( inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_D0_COLLID ) {
3354
3355                         /* Clear collision flag.  */
3356                         outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
3357
3358                         /* Extract collision count from 86960.  */
3359                         col = inb( sc->ioaddr[ FE_DLCR4 ] );
3360                         col = ( col & FE_D4_COL ) >> FE_D4_COL_SHIFT;
3361                         if ( col == 0 ) {
3362                                 /*
3363                                  * Status register indicates collisions,
3364                                  * while the collision count is zero.
3365                                  * This can happen after multiple packet
3366                                  * transmission, indicating that one or more
3367                                  * previous packet(s) had been collided.
3368                                  *
3369                                  * Since the accurate number of collisions
3370                                  * has been lost, we just guess it as 1;
3371                                  * Am I too optimistic?  FIXME.
3372                                  */
3373                                 col = 1;
3374                         }
3375                         sc->sc_if.if_collisions += col;
3376                         if ( col == 1 ) {
3377                                 sc->mibdata.dot3StatsSingleCollisionFrames++;
3378                         } else {
3379                                 sc->mibdata.dot3StatsMultipleCollisionFrames++;
3380                         }
3381                         sc->mibdata.dot3StatsCollFrequencies[col-1]++;
3382                 }
3383
3384                 /*
3385                  * Update transmission statistics.
3386                  * Be sure to reflect number of excessive collisions.
3387                  */
3388                 col = sc->tx_excolls;
3389                 sc->sc_if.if_opackets += sc->txb_sched - col;
3390                 sc->sc_if.if_oerrors += col;
3391                 sc->sc_if.if_collisions += col * 16;
3392                 sc->mibdata.dot3StatsExcessiveCollisions += col;
3393                 sc->mibdata.dot3StatsCollFrequencies[15] += col;
3394                 sc->txb_sched = 0;
3395
3396                 /*
3397                  * The transmitter is no more active.
3398                  * Reset output active flag and watchdog timer.
3399                  */
3400                 sc->sc_if.if_flags &= ~IFF_OACTIVE;
3401                 sc->sc_if.if_timer = 0;
3402
3403                 /*
3404                  * If more data is ready to transmit in the buffer, start
3405                  * transmitting them.  Otherwise keep transmitter idle,
3406                  * even if more data is queued.  This gives receive
3407                  * process a slight priority.
3408                  */
3409                 if ( sc->txb_count > 0 ) fe_xmit( sc );
3410         }
3411 }
3412
3413 /*
3414  * Ethernet interface receiver interrupt.
3415  */
3416 static void
3417 fe_rint ( struct fe_softc * sc, u_char rstat )
3418 {
3419         u_short len;
3420         u_char status;
3421         int i;
3422
3423         /*
3424          * Update statistics if this interrupt is caused by an error.
3425          * Note that, when the system was not sufficiently fast, the
3426          * receive interrupt might not be acknowledged immediately.  If
3427          * one or more errornous frames were received before this routine
3428          * was scheduled, they are ignored, and the following error stats
3429          * give less than real values.
3430          */
3431         if ( rstat & ( FE_D1_OVRFLO | FE_D1_CRCERR
3432                      | FE_D1_ALGERR | FE_D1_SRTPKT ) ) {
3433                 if ( rstat & FE_D1_OVRFLO )
3434                         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3435                 if ( rstat & FE_D1_CRCERR )
3436                         sc->mibdata.dot3StatsFCSErrors++;
3437                 if ( rstat & FE_D1_ALGERR )
3438                         sc->mibdata.dot3StatsAlignmentErrors++;
3439 #if 0
3440                 /* The reference MAC receiver defined in 802.3
3441                    silently ignores short frames (RUNTs) without
3442                    notifying upper layer.  RFC 1650 (dot3 MIB) is
3443                    based on the 802.3, and it has no stats entry for
3444                    RUNTs...  */
3445                 if ( rstat & FE_D1_SRTPKT )
3446                         sc->mibdata.dot3StatsFrameTooShorts++; /* :-) */
3447 #endif
3448                 sc->sc_if.if_ierrors++;
3449         }
3450
3451         /*
3452          * MB86960 has a flag indicating "receive queue empty."
3453          * We just loop, checking the flag, to pull out all received
3454          * packets.
3455          *
3456          * We limit the number of iterations to avoid infinite-loop.
3457          * The upper bound is set to unrealistic high value.
3458          */
3459         for ( i = 0; i < FE_MAX_RECV_COUNT * 2; i++ ) {
3460
3461                 /* Stop the iteration if 86960 indicates no packets.  */
3462                 if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) return;
3463
3464                 /*
3465                  * Extract a receive status byte.
3466                  * As our 86960 is in 16 bit bus access mode, we have to
3467                  * use inw() to get the status byte.  The significant
3468                  * value is returned in lower 8 bits.
3469                  */
3470 #ifdef FE_8BIT_SUPPORT
3471                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3472                 {
3473                         status = inb( sc->ioaddr[ FE_BMPR8 ] );
3474                         ( void ) inb( sc->ioaddr[ FE_BMPR8 ] );
3475                 }
3476                 else
3477 #endif
3478                 {
3479                         status = ( u_char )inw( sc->ioaddr[ FE_BMPR8 ] );
3480                 }       
3481
3482                 /*
3483                  * Extract the packet length.
3484                  * It is a sum of a header (14 bytes) and a payload.
3485                  * CRC has been stripped off by the 86960.
3486                  */
3487 #ifdef FE_8BIT_SUPPORT
3488                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3489                 {
3490                         len  =   inb( sc->ioaddr[ FE_BMPR8 ] );
3491                         len |= ( inb( sc->ioaddr[ FE_BMPR8 ] ) << 8 );
3492                 }
3493                 else
3494 #endif
3495                 {
3496                         len = inw( sc->ioaddr[ FE_BMPR8 ] );
3497                 }
3498
3499                 /*
3500                  * AS our 86960 is programed to ignore errored frame,
3501                  * we must not see any error indication in the
3502                  * receive buffer.  So, any error condition is a
3503                  * serious error, e.g., out-of-sync of the receive
3504                  * buffer pointers.
3505                  */
3506                 if ( ( status & 0xF0 ) != 0x20
3507                      || len > ETHER_MAX_LEN - ETHER_CRC_LEN
3508                      || len < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
3509                         printf("fe%d: RX buffer out-of-sync\n", sc->sc_unit);
3510                         sc->sc_if.if_ierrors++;
3511                         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3512                         fe_reset(sc);
3513                         return;
3514                 }
3515
3516                 /*
3517                  * Go get a packet.
3518                  */
3519                 if ( fe_get_packet( sc, len ) < 0 ) {
3520                         /*
3521                          * Negative return from fe_get_packet()
3522                          * indicates no available mbuf.  We stop
3523                          * receiving packets, even if there are more
3524                          * in the buffer.  We hope we can get more
3525                          * mbuf next time.
3526                          */
3527                         sc->sc_if.if_ierrors++;
3528                         sc->mibdata.dot3StatsMissedFrames++;
3529                         fe_droppacket( sc, len );
3530                         return;
3531                 }
3532
3533                 /* Successfully received a packet.  Update stat.  */
3534                 sc->sc_if.if_ipackets++;
3535         }
3536
3537         /* Maximum number of frames has been received.  Something
3538            strange is happening here... */
3539         printf("fe%d: unusual receive flood\n", sc->sc_unit);
3540         sc->mibdata.dot3StatsInternalMacReceiveErrors++;
3541         fe_reset(sc);
3542 }
3543
3544 /*
3545  * Ethernet interface interrupt processor
3546  */
3547 static void
3548 feintr ( int unit )
3549 {
3550         struct fe_softc *sc = &fe_softc[unit];
3551         u_char tstat, rstat;
3552         int loop_count = FE_MAX_LOOP;
3553
3554         /* Loop until there are no more new interrupt conditions.  */
3555         while (loop_count-- > 0) {
3556                 /*
3557                  * Get interrupt conditions, masking unneeded flags.
3558                  */
3559                 tstat = inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_TMASK;
3560                 rstat = inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_RMASK;
3561                 if ( tstat == 0 && rstat == 0 ) return;
3562
3563                 /*
3564                  * Reset the conditions we are acknowledging.
3565                  */
3566                 outb( sc->ioaddr[ FE_DLCR0 ], tstat );
3567                 outb( sc->ioaddr[ FE_DLCR1 ], rstat );
3568
3569                 /*
3570                  * Handle transmitter interrupts.
3571                  */
3572                 if ( tstat ) {
3573                         fe_tint( sc, tstat );
3574                 }
3575
3576                 /*
3577                  * Handle receiver interrupts
3578                  */
3579                 if ( rstat ) {
3580                         fe_rint( sc, rstat );
3581                 }
3582
3583                 /*
3584                  * Update the multicast address filter if it is
3585                  * needed and possible.  We do it now, because
3586                  * we can make sure the transmission buffer is empty,
3587                  * and there is a good chance that the receive queue
3588                  * is empty.  It will minimize the possibility of
3589                  * packet loss.
3590                  */
3591                 if ( sc->filter_change
3592                   && sc->txb_count == 0 && sc->txb_sched == 0 ) {
3593                         fe_loadmar(sc);
3594                         sc->sc_if.if_flags &= ~IFF_OACTIVE;
3595                 }
3596
3597                 /*
3598                  * If it looks like the transmitter can take more data,
3599                  * attempt to start output on the interface. This is done
3600                  * after handling the receiver interrupt to give the
3601                  * receive operation priority.
3602                  *
3603                  * BTW, I'm not sure in what case the OACTIVE is on at
3604                  * this point.  Is the following test redundant?
3605                  *
3606                  * No.  This routine polls for both transmitter and
3607                  * receiver interrupts.  86960 can raise a receiver
3608                  * interrupt when the transmission buffer is full.
3609                  */
3610                 if ( ( sc->sc_if.if_flags & IFF_OACTIVE ) == 0 ) {
3611                         fe_start( &sc->sc_if );
3612                 }
3613
3614         }
3615
3616         printf("fe%d: too many loops\n", sc->sc_unit);
3617         return;
3618 }
3619
3620 /*
3621  * Process an ioctl request. This code needs some work - it looks
3622  * pretty ugly.
3623  */
3624 static int
3625 fe_ioctl ( struct ifnet * ifp, u_long command, caddr_t data )
3626 {
3627         struct fe_softc *sc = ifp->if_softc;
3628         struct ifreq *ifr = (struct ifreq *)data;
3629         int s, error = 0;
3630
3631         s = splimp();
3632
3633         switch (command) {
3634
3635           case SIOCSIFADDR:
3636           case SIOCGIFADDR:
3637           case SIOCSIFMTU:
3638                 /* Just an ordinary action.  */
3639                 error = ether_ioctl(ifp, command, data);
3640                 break;
3641
3642           case SIOCSIFFLAGS:
3643                 /*
3644                  * Switch interface state between "running" and
3645                  * "stopped", reflecting the UP flag.
3646                  */
3647                 if ( sc->sc_if.if_flags & IFF_UP ) {
3648                         if ( ( sc->sc_if.if_flags & IFF_RUNNING ) == 0 ) {
3649                                 fe_init(sc);
3650                         }
3651                 } else {
3652                         if ( ( sc->sc_if.if_flags & IFF_RUNNING ) != 0 ) {
3653                                 fe_stop(sc);
3654                         }
3655                 }
3656
3657                 /*
3658                  * Promiscuous and/or multicast flags may have changed,
3659                  * so reprogram the multicast filter and/or receive mode.
3660                  */
3661                 fe_setmode( sc );
3662
3663                 /* Done.  */
3664                 break;
3665
3666           case SIOCADDMULTI:
3667           case SIOCDELMULTI:
3668                 /*
3669                  * Multicast list has changed; set the hardware filter
3670                  * accordingly.
3671                  */
3672                 fe_setmode( sc );
3673                 break;
3674
3675           case SIOCSIFMEDIA:
3676           case SIOCGIFMEDIA:
3677                 /* Let if_media to handle these commands and to call
3678                    us back.  */
3679                 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
3680                 break;
3681
3682           default:
3683                 error = EINVAL;
3684                 break;
3685         }
3686
3687         (void) splx(s);
3688         return (error);
3689 }
3690
3691 /*
3692  * Retrieve packet from receive buffer and send to the next level up via
3693  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
3694  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
3695  */
3696 static int
3697 fe_get_packet ( struct fe_softc * sc, u_short len )
3698 {
3699         struct ether_header *eh;
3700         struct mbuf *m;
3701
3702         /*
3703          * NFS wants the data be aligned to the word (4 byte)
3704          * boundary.  Ethernet header has 14 bytes.  There is a
3705          * 2-byte gap.
3706          */
3707 #define NFS_MAGIC_OFFSET 2
3708
3709         /*
3710          * This function assumes that an Ethernet packet fits in an
3711          * mbuf (with a cluster attached when necessary.)  On FreeBSD
3712          * 2.0 for x86, which is the primary target of this driver, an
3713          * mbuf cluster has 4096 bytes, and we are happy.  On ancient
3714          * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
3715          * however.  If the following #error message were printed upon
3716          * compile, you need to rewrite this function.
3717          */
3718 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
3719 #error "Too small MCLBYTES to use fe driver."
3720 #endif
3721
3722         /*
3723          * Our strategy has one more problem.  There is a policy on
3724          * mbuf cluster allocation.  It says that we must have at
3725          * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
3726          * allocate a cluster.  For a packet of a size between
3727          * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
3728          * On the other hand, the current code is short, simple,
3729          * and fast, however.  It does no harmful thing, just waists
3730          * some memory.  Any comments?  FIXME.
3731          */
3732
3733         /* Allocate an mbuf with packet header info.  */
3734         MGETHDR(m, M_DONTWAIT, MT_DATA);
3735         if ( m == NULL ) return -1;
3736
3737         /* Attach a cluster if this packet doesn't fit in a normal mbuf.  */
3738         if ( len > MHLEN - NFS_MAGIC_OFFSET ) {
3739                 MCLGET( m, M_DONTWAIT );
3740                 if ( !( m->m_flags & M_EXT ) ) {
3741                         m_freem( m );
3742                         return -1;
3743                 }
3744         }
3745
3746         /* Initialize packet header info.  */
3747         m->m_pkthdr.rcvif = &sc->sc_if;
3748         m->m_pkthdr.len = len;
3749
3750         /* Set the length of this packet.  */
3751         m->m_len = len;
3752
3753         /* The following silliness is to make NFS happy */
3754         m->m_data += NFS_MAGIC_OFFSET;
3755
3756         /* Get (actually just point to) the header part.  */
3757         eh = mtod(m, struct ether_header *);
3758
3759         /* Get a packet.  */
3760 #ifdef FE_8BIT_SUPPORT
3761         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3762         {
3763                 insb( sc->ioaddr[ FE_BMPR8 ], eh,   len );
3764         }
3765         else
3766 #endif
3767         {
3768                 insw( sc->ioaddr[ FE_BMPR8 ], eh, ( len + 1 ) >> 1 );
3769         }
3770
3771 #define ETHER_ADDR_IS_MULTICAST(A) (*(char *)(A) & 1)
3772
3773         /*
3774          * Check if there's a BPF listener on this interface.
3775          * If it is, hand off the raw packet to bpf.
3776          */
3777         if ( sc->sc_if.if_bpf ) {
3778                 bpf_mtap( &sc->sc_if, m );
3779         }
3780
3781 #ifdef BRIDGE
3782         if (do_bridge) {
3783                 struct ifnet *ifp;
3784
3785                 ifp = bridge_in(m);
3786                 if (ifp == BDG_DROP) {
3787                         m_freem(m);
3788                         return 0;
3789                 }
3790                 if (ifp != BDG_LOCAL)
3791                         bdg_forward(&m, ifp); /* not local, need forwarding */
3792                 if (ifp == BDG_LOCAL || ifp == BDG_BCAST || ifp == BDG_MCAST)
3793                         goto getit;
3794                 /* not local and not multicast, just drop it */
3795                 if (m)
3796                         m_freem(m);
3797                 return 0;
3798         }
3799 #endif
3800
3801         /*
3802          * Make sure this packet is (or may be) directed to us.
3803          * That is, the packet is either unicasted to our address,
3804          * or broad/multi-casted.  If any other packets are
3805          * received, it is an indication of an error -- probably
3806          * 86960 is in a wrong operation mode.
3807          * Promiscuous mode is an exception.  Under the mode, all
3808          * packets on the media must be received.  (We must have
3809          * programmed the 86960 so.)
3810          */
3811
3812         if ( ( sc->sc_if.if_flags & IFF_PROMISC )
3813           && !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
3814           && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
3815                 /*
3816                  * The packet was not for us.  This is normal since
3817                  * we are now in promiscuous mode.  Just drop the packet.
3818                  */
3819                 m_freem( m );
3820                 return 0;
3821         }
3822
3823 #ifdef BRIDGE
3824 getit:
3825 #endif
3826         /* Strip off the Ethernet header.  */
3827         m->m_pkthdr.len -= sizeof ( struct ether_header );
3828         m->m_len -= sizeof ( struct ether_header );
3829         m->m_data += sizeof ( struct ether_header );
3830
3831         /* Feed the packet to upper layer.  */
3832         ether_input( &sc->sc_if, eh, m );
3833         return 0;
3834 }
3835
3836 /*
3837  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
3838  * Returns number of bytes actually written, including length word.
3839  *
3840  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
3841  * Packets shorter than Ethernet minimum are legal, and we pad them
3842  * before sending out.  An exception is "partial" packets which are
3843  * shorter than mandatory Ethernet header.
3844  */
3845 static void
3846 fe_write_mbufs ( struct fe_softc *sc, struct mbuf *m )
3847 {
3848         u_short addr_bmpr8 = sc->ioaddr[ FE_BMPR8 ];
3849         u_short length, len;
3850         struct mbuf *mp;
3851         u_char *data;
3852         u_short savebyte;       /* WARNING: Architecture dependent!  */
3853 #define NO_PENDING_BYTE 0xFFFF
3854
3855         static u_char padding [ ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN ];
3856
3857 #ifdef DIAGNOSTIC
3858         /* First, count up the total number of bytes to copy */
3859         length = 0;
3860         for ( mp = m; mp != NULL; mp = mp->m_next ) {
3861                 length += mp->m_len;
3862         }
3863         /* Check if this matches the one in the packet header.  */
3864         if ( length != m->m_pkthdr.len ) {
3865                 printf("fe%d: packet length mismatch? (%d/%d)\n", sc->sc_unit,
3866                        length, m->m_pkthdr.len);
3867         }
3868 #else
3869         /* Just use the length value in the packet header.  */
3870         length = m->m_pkthdr.len;
3871 #endif
3872
3873 #ifdef DIAGNOSTIC
3874         /*
3875          * Should never send big packets.  If such a packet is passed,
3876          * it should be a bug of upper layer.  We just ignore it.
3877          * ... Partial (too short) packets, neither.
3878          */
3879         if ( length < ETHER_HDR_LEN
3880           || length > ETHER_MAX_LEN - ETHER_CRC_LEN ) {
3881                 printf("fe%d: got an out-of-spec packet (%u bytes) to send\n",
3882                         sc->sc_unit, length);
3883                 sc->sc_if.if_oerrors++;
3884                 sc->mibdata.dot3StatsInternalMacTransmitErrors++;
3885                 return;
3886         }
3887 #endif
3888
3889         /*
3890          * Put the length word for this frame.
3891          * Does 86960 accept odd length?  -- Yes.
3892          * Do we need to pad the length to minimum size by ourselves?
3893          * -- Generally yes.  But for (or will be) the last
3894          * packet in the transmission buffer, we can skip the
3895          * padding process.  It may gain performance slightly.  FIXME.
3896          */
3897 #ifdef FE_8BIT_SUPPORT
3898         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3899         {
3900                 len = max( length, ETHER_MIN_LEN - ETHER_CRC_LEN );
3901                 outb( addr_bmpr8,   len & 0x00ff );
3902                 outb( addr_bmpr8, ( len & 0xff00 ) >> 8 );
3903         }
3904         else
3905 #endif
3906         {
3907                 outw( addr_bmpr8, max( length, ETHER_MIN_LEN - ETHER_CRC_LEN ) );
3908         }
3909
3910         /*
3911          * Update buffer status now.
3912          * Truncate the length up to an even number, since we use outw().
3913          */
3914 #ifdef FE_8BIT_SUPPORT
3915         if ((sc->proto_dlcr6 & FE_D6_SBW) != FE_D6_SBW_BYTE)
3916 #endif
3917         {
3918                 length = ( length + 1 ) & ~1;
3919         }
3920         sc->txb_free -= FE_DATA_LEN_LEN + max( length, ETHER_MIN_LEN - ETHER_CRC_LEN);
3921         sc->txb_count++;
3922
3923         /*
3924          * Transfer the data from mbuf chain to the transmission buffer.
3925          * MB86960 seems to require that data be transferred as words, and
3926          * only words.  So that we require some extra code to patch
3927          * over odd-length mbufs.
3928          */
3929 #ifdef FE_8BIT_SUPPORT
3930         if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3931         {
3932                 /* 8-bit cards are easy.  */
3933                 for ( mp = m; mp != 0; mp = mp->m_next ) {
3934                         if ( mp->m_len ) {
3935                                 outsb( addr_bmpr8, mtod(mp, caddr_t), mp->m_len );
3936                         }
3937                 }
3938         }
3939         else
3940 #endif
3941         {
3942                 /* 16-bit cards are a pain.  */
3943                 savebyte = NO_PENDING_BYTE;
3944                 for ( mp = m; mp != 0; mp = mp->m_next ) {
3945
3946                         /* Ignore empty mbuf.  */
3947                         len = mp->m_len;
3948                         if ( len == 0 ) continue;
3949
3950                         /* Find the actual data to send.  */
3951                         data = mtod(mp, caddr_t);
3952
3953                         /* Finish the last byte.  */
3954                         if ( savebyte != NO_PENDING_BYTE ) {
3955                                 outw( addr_bmpr8, savebyte | ( *data << 8 ) );
3956                                 data++;
3957                                 len--;
3958                                 savebyte = NO_PENDING_BYTE;
3959                         }
3960
3961                         /* output contiguous words */
3962                         if (len > 1) {
3963                                 outsw( addr_bmpr8, data, len >> 1);
3964                                 data += len & ~1;
3965                                 len &= 1;
3966                         }
3967
3968                         /* Save a remaining byte, if there is one.  */
3969                         if ( len > 0 ) {
3970                                 savebyte = *data;
3971                         }
3972                 }
3973
3974                 /* Spit the last byte, if the length is odd.  */
3975                 if ( savebyte != NO_PENDING_BYTE ) {
3976                         outw( addr_bmpr8, savebyte );
3977                 }
3978         }
3979
3980         /* Pad to the Ethernet minimum length, if the packet is too short.  */
3981         if ( length < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
3982 #ifdef FE_8BIT_SUPPORT
3983                 if ((sc->proto_dlcr6 & FE_D6_SBW) == FE_D6_SBW_BYTE)
3984                 {
3985                         outsb( addr_bmpr8, padding,   ETHER_MIN_LEN - ETHER_CRC_LEN - length );
3986                 }
3987                 else
3988 #endif
3989                 {
3990                         outsw( addr_bmpr8, padding, ( ETHER_MIN_LEN - ETHER_CRC_LEN - length ) >> 1);
3991                 }
3992         }
3993 }
3994
3995 /*
3996  * Compute hash value for an Ethernet address
3997  */
3998 static int
3999 fe_hash ( u_char * ep )
4000 {
4001 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
4002
4003         u_long hash = 0xFFFFFFFFL;
4004         int i, j;
4005         u_char b;
4006         u_long m;
4007
4008         for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
4009                 b = *ep++;
4010                 for ( j = 8; --j >= 0; ) {
4011                         m = hash;
4012                         hash >>= 1;
4013                         if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
4014                         b >>= 1;
4015                 }
4016         }
4017         return ( ( int )( hash >> 26 ) );
4018 }
4019
4020 /*
4021  * Compute the multicast address filter from the
4022  * list of multicast addresses we need to listen to.
4023  */
4024 static struct fe_filter
4025 fe_mcaf ( struct fe_softc *sc )
4026 {
4027         int index;
4028         struct fe_filter filter;
4029         struct ifmultiaddr *ifma;
4030
4031         filter = fe_filter_nothing;
4032         for (ifma = sc->arpcom.ac_if.if_multiaddrs.lh_first; ifma;
4033              ifma = ifma->ifma_link.le_next) {
4034                 if (ifma->ifma_addr->sa_family != AF_LINK)
4035                         continue;
4036                 index = fe_hash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
4037 #ifdef FE_DEBUG
4038                 printf("fe%d: hash(%6D) == %d\n",
4039                         sc->sc_unit, enm->enm_addrlo , ":", index);
4040 #endif
4041
4042                 filter.data[index >> 3] |= 1 << (index & 7);
4043         }
4044         return ( filter );
4045 }
4046
4047 /*
4048  * Calculate a new "multicast packet filter" and put the 86960
4049  * receiver in appropriate mode.
4050  */
4051 static void
4052 fe_setmode ( struct fe_softc *sc )
4053 {
4054         int flags = sc->sc_if.if_flags;
4055
4056         /*
4057          * If the interface is not running, we postpone the update
4058          * process for receive modes and multicast address filter
4059          * until the interface is restarted.  It reduces some
4060          * complicated job on maintaining chip states.  (Earlier versions
4061          * of this driver had a bug on that point...)
4062          *
4063          * To complete the trick, fe_init() calls fe_setmode() after
4064          * restarting the interface.
4065          */
4066         if ( !( flags & IFF_RUNNING ) ) return;
4067
4068         /*
4069          * Promiscuous mode is handled separately.
4070          */
4071         if ( flags & IFF_PROMISC ) {
4072                 /*
4073                  * Program 86960 to receive all packets on the segment
4074                  * including those directed to other stations.
4075                  * Multicast filter stored in MARs are ignored
4076                  * under this setting, so we don't need to update it.
4077                  *
4078                  * Promiscuous mode in FreeBSD 2 is used solely by
4079                  * BPF, and BPF only listens to valid (no error) packets.
4080                  * So, we ignore erroneous ones even in this mode.
4081                  * (Older versions of fe driver mistook the point.)
4082                  */
4083                 outb( sc->ioaddr[ FE_DLCR5 ],
4084                         sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1 );
4085                 sc->filter_change = 0;
4086                 return;
4087         }
4088
4089         /*
4090          * Turn the chip to the normal (non-promiscuous) mode.
4091          */
4092         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 | FE_D5_AFM1 );
4093
4094         /*
4095          * Find the new multicast filter value.
4096          */
4097         if ( flags & IFF_ALLMULTI ) {
4098                 sc->filter = fe_filter_all;
4099         } else {
4100                 sc->filter = fe_mcaf( sc );
4101         }
4102         sc->filter_change = 1;
4103
4104         /*
4105          * We have to update the multicast filter in the 86960, A.S.A.P.
4106          *
4107          * Note that the DLC (Data Link Control unit, i.e. transmitter
4108          * and receiver) must be stopped when feeding the filter, and
4109          * DLC trashes all packets in both transmission and receive
4110          * buffers when stopped.
4111          *
4112          * To reduce the packet loss, we delay the filter update
4113          * process until buffers are empty.
4114          */
4115         if ( sc->txb_sched == 0 && sc->txb_count == 0
4116           && !( inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_D1_PKTRDY ) ) {
4117                 /*
4118                  * Buffers are (apparently) empty.  Load
4119                  * the new filter value into MARs now.
4120                  */
4121                 fe_loadmar(sc);
4122         } else {
4123                 /*
4124                  * Buffers are not empty.  Mark that we have to update
4125                  * the MARs.  The new filter will be loaded by feintr()
4126                  * later.
4127                  */
4128         }
4129 }
4130
4131 /*
4132  * Load a new multicast address filter into MARs.
4133  *
4134  * The caller must have splimp'ed before fe_loadmar.
4135  * This function starts the DLC upon return.  So it can be called only
4136  * when the chip is working, i.e., from the driver's point of view, when
4137  * a device is RUNNING.  (I mistook the point in previous versions.)
4138  */
4139 static void
4140 fe_loadmar ( struct fe_softc * sc )
4141 {
4142         /* Stop the DLC (transmitter and receiver).  */
4143         DELAY( 200 );
4144         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
4145         DELAY( 200 );
4146
4147         /* Select register bank 1 for MARs.  */
4148         outb( sc->ioaddr[ FE_DLCR7 ],
4149                 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
4150
4151         /* Copy filter value into the registers.  */
4152         outblk( sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN );
4153
4154         /* Restore the bank selection for BMPRs (i.e., runtime registers).  */
4155         outb( sc->ioaddr[ FE_DLCR7 ],
4156                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
4157
4158         /* Restart the DLC.  */
4159         DELAY( 200 );
4160         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
4161         DELAY( 200 );
4162
4163         /* We have just updated the filter.  */
4164         sc->filter_change = 0;
4165 }
4166
4167 /* Change the media selection.  */
4168 static int
4169 fe_medchange (struct ifnet *ifp)
4170 {
4171         struct fe_softc *sc = (struct fe_softc *)ifp->if_softc;
4172
4173 #ifdef DIAGNOSTIC
4174         /* If_media should not pass any request for a media which this
4175            interface doesn't support.  */
4176         int b;
4177
4178         for (b = 0; bit2media[b] != 0; b++) {
4179                 if (bit2media[b] == sc->media.ifm_media) break;
4180         }
4181         if (((1 << b) & sc->mbitmap) == 0) {
4182                 printf("fe%d: got an unsupported media request (0x%x)\n",
4183                        sc->sc_unit, sc->media.ifm_media);
4184                 return EINVAL;
4185         }
4186 #endif
4187
4188         /* We don't actually change media when the interface is down.
4189            fe_init() will do the job, instead.  Should we also wait
4190            until the transmission buffer being empty?  Changing the
4191            media when we are sending a frame will cause two garbages
4192            on wires, one on old media and another on new.  FIXME */
4193         if (sc->sc_if.if_flags & IFF_UP) {
4194                 if (sc->msel) sc->msel(sc);
4195         }
4196
4197         return 0;
4198 }
4199
4200 /* I don't know how I can support media status callback... FIXME.  */
4201 static void
4202 fe_medstat (struct ifnet *ifp, struct ifmediareq *ifmr)
4203 {
4204         (void)ifp;
4205         (void)ifmr;
4206 }