The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/if_fe.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    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: src/sys/i386/isa/if_fe.c,v 1.20.2.6 1999/09/05 08:12:51 peter Exp $
   25  *
   26  * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
   27  * To be used with FreeBSD 2.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 for
   40  * RE1000/RE1000+/ME1500 support.  It is incomplete, however, since the
   41  * cards are not for AT-compatibles.  (They are for PC98 bus -- a
   42  * proprietary bus architecture available only in Japan.)  Further
   43  * work for PC98 version will be available as a part of FreeBSD(98)
   44  * project.
   45  *
   46  * This software is a derivative work of if_ed.c version 1.56 by David
   47  * Greenman available as a part of FreeBSD 2.0 RELEASE source distribution.
   48  *
   49  * The following lines are retained from the original if_ed.c:
   50  *
   51  * Copyright (C) 1993, David Greenman. This software may be used, modified,
   52  *   copied, distributed, and sold, in both source and binary form provided
   53  *   that the above copyright and these terms are retained. Under no
   54  *   circumstances is the author responsible for the proper functioning
   55  *   of this software, nor does the author assume any responsibility
   56  *   for damages incurred with its use.
   57  */
   58 
   59 /*
   60  * TODO:
   61  *  o   To support MBH10304 PC card.  It is another MB8696x based
   62  *      PCMCIA Ethernet card by Fujitsu, which is not compatible with
   63  *      MBH10302.
   64  *  o   To merge FreeBSD(98) efforts into a single source file.
   65  *  o   To support ISA PnP auto configuration for FMV-183/184.
   66  *  o   To reconsider mbuf usage.
   67  *  o   To reconsider transmission buffer usage, including
   68  *      transmission buffer size (currently 4KB x 2) and pros-and-
   69  *      cons of multiple frame transmission.
   70  *  o   To test IPX codes.
   71  */
   72 
   73 #include "isa.h"
   74 #include "fe.h"
   75 #include "bpfilter.h"
   76 
   77 #include <sys/param.h>
   78 #include <sys/kernel.h>
   79 #include <sys/systm.h>
   80 
   81 #include <sys/conf.h>
   82 
   83 #include <sys/errno.h>
   84 #include <sys/ioctl.h>
   85 #include <sys/mbuf.h>
   86 #include <sys/socket.h>
   87 #include <sys/syslog.h>
   88 
   89 #include <net/if.h>
   90 #include <net/if_dl.h>
   91 #include <net/if_types.h>
   92 
   93 #ifdef INET
   94 #include <netinet/in.h>
   95 #include <netinet/in_systm.h>
   96 #include <netinet/in_var.h>
   97 #include <netinet/ip.h>
   98 #include <netinet/if_ether.h>
   99 #endif
  100 
  101 /* IPX code is not tested.  FIXME.  */
  102 #ifdef IPX
  103 #include <netipx/ipx.h>
  104 #include <netipx/ipx_if.h>
  105 #endif
  106 
  107 /* To be used with IPv6 package of INRIA.  */
  108 #ifdef INET6
  109 /* IPv6 added by shin 96.2.6 */
  110 #include <netinet/if_ether6.h>
  111 #endif
  112 
  113 /* XNS code is not tested.  FIXME.  */
  114 #ifdef NS
  115 #include <netns/ns.h>
  116 #include <netns/ns_if.h>
  117 #endif
  118 
  119 #if NBPFILTER > 0
  120 #include <net/bpf.h>
  121 #include <net/bpfdesc.h>
  122 #endif
  123 
  124 #include <machine/clock.h>
  125 
  126 #include <i386/isa/isa.h>
  127 #include <i386/isa/isa_device.h>
  128 #include <i386/isa/icu.h>
  129 
  130 /* PCCARD suport */
  131 #include "card.h"
  132 #if NCARD > 0
  133 #include <sys/select.h>
  134 #include <pccard/cardinfo.h>
  135 #include <pccard/slot.h>
  136 #include <pccard/driver.h>
  137 #endif
  138 
  139 #include <i386/isa/ic/mb86960.h>
  140 #include <i386/isa/if_fereg.h>
  141 
  142 /*
  143  * This version of fe is an ISA device driver.
  144  * Override the following macro to adapt it to another bus.
  145  * (E.g., PC98.)
  146  */
  147 #define DEVICE  struct isa_device
  148 
  149 /*
  150  * Default settings for fe driver specific options.
  151  * They can be set in config file by "options" statements.
  152  */
  153 
  154 /*
  155  * Debug control.
  156  * 0: No debug at all.  All debug specific codes are stripped off.
  157  * 1: Silent.  No debug messages are logged except emergent ones.
  158  * 2: Brief.  Lair events and/or important information are logged.
  159  * 3: Detailed.  Logs all information which *may* be useful for debugging.
  160  * 4: Trace.  All actions in the driver is logged.  Super verbose.
  161  */
  162 #ifndef FE_DEBUG
  163 #define FE_DEBUG        1
  164 #endif
  165 
  166 /*
  167  * Transmit just one packet per a "send" command to 86960.
  168  * This option is intended for performance test.  An EXPERIMENTAL option.
  169  */
  170 #ifndef FE_SINGLE_TRANSMISSION
  171 #define FE_SINGLE_TRANSMISSION 0
  172 #endif
  173 
  174 /*
  175  * Device configuration flags.
  176  */
  177 
  178 /* DLCR6 settings.  */
  179 #define FE_FLAGS_DLCR6_VALUE    0x007F
  180 
  181 /* Force DLCR6 override.  */
  182 #define FE_FLAGS_OVERRIDE_DLCR6 0x0080
  183 
  184 /* Shouldn't these be defined somewhere else such as isa_device.h?  */
  185 #define NO_IOADDR       (-1)
  186 #define NO_IRQ          0
  187 
  188 /*
  189  * Data type for a multicast address filter on 8696x.
  190  */
  191 struct fe_filter { u_char data [ FE_FILTER_LEN ]; };
  192 
  193 /*
  194  * Special filter values.
  195  */
  196 static struct fe_filter const fe_filter_nothing = { FE_FILTER_NOTHING };
  197 static struct fe_filter const fe_filter_all     = { FE_FILTER_ALL };
  198 
  199 /* How many registers does an fe-supported adapter have at maximum?  */
  200 #define MAXREGISTERS 32
  201 
  202 /*
  203  * fe_softc: per line info and status
  204  */
  205 static struct fe_softc {
  206 
  207         /* Used by "common" codes.  */
  208         struct arpcom arpcom;   /* Ethernet common */
  209 
  210         /* Used by config codes.  */
  211 
  212         /* Set by probe() and not modified in later phases.  */
  213         char * typestr;         /* printable name of the interface.  */
  214         u_short iobase;         /* base I/O address of the adapter.  */
  215         u_short ioaddr [ MAXREGISTERS ]; /* I/O addresses of register.  */
  216         u_short txb_size;       /* size of TX buffer, in bytes  */
  217         u_char proto_dlcr4;     /* DLCR4 prototype.  */
  218         u_char proto_dlcr5;     /* DLCR5 prototype.  */
  219         u_char proto_dlcr6;     /* DLCR6 prototype.  */
  220         u_char proto_dlcr7;     /* DLCR7 prototype.  */
  221         u_char proto_bmpr13;    /* BMPR13 prototype.  */
  222 
  223         /* Vendor specific hooks.  */
  224         void ( * init )( struct fe_softc * ); /* Just before fe_init().  */
  225         void ( * stop )( struct fe_softc * ); /* Just after fe_stop().  */
  226 
  227         /* Transmission buffer management.  */
  228         u_short txb_free;       /* free bytes in TX buffer  */
  229         u_char txb_count;       /* number of packets in TX buffer  */
  230         u_char txb_sched;       /* number of scheduled packets  */
  231 
  232         /* Excessive collision counter (see fe_tint() for details.  */
  233         u_char tx_excolls;      /* # of excessive collisions.  */
  234 
  235         /* Multicast address filter management.  */
  236         u_char filter_change;   /* MARs must be changed ASAP. */
  237         struct fe_filter filter;/* new filter value.  */
  238 
  239 }       fe_softc[NFE];
  240 
  241 #define sc_if           arpcom.ac_if
  242 #define sc_unit         arpcom.ac_if.if_unit
  243 #define sc_enaddr       arpcom.ac_enaddr
  244 
  245 /* Standard driver entry points.  These can be static.  */
  246 static int              fe_probe        ( struct isa_device * );
  247 static int              fe_attach       ( struct isa_device * );
  248 static void             fe_init         ( int );
  249 static int              fe_ioctl        ( struct ifnet *, int, caddr_t );
  250 static void             fe_start        ( struct ifnet * );
  251 static void             fe_reset        ( int );
  252 static void             fe_watchdog     ( struct ifnet * );
  253 
  254 /* Local functions.  Order of declaration is confused.  FIXME.  */
  255 static int      fe_probe_fmv    ( DEVICE *, struct fe_softc * );
  256 static int      fe_probe_ati    ( DEVICE *, struct fe_softc * );
  257 static void     fe_init_ati     ( struct fe_softc * );
  258 static int      fe_probe_gwy    ( DEVICE *, struct fe_softc * );
  259 #if NCARD > 0
  260 static int      fe_probe_mbh    ( DEVICE *, struct fe_softc * );
  261 static void     fe_init_mbh     ( struct fe_softc * );
  262 static int      fe_probe_tdk    ( DEVICE *, struct fe_softc * );
  263 #endif
  264 static int      fe_get_packet   ( struct fe_softc *, u_short );
  265 static void     fe_stop         ( int );
  266 static void     fe_tint         ( struct fe_softc *, u_char );
  267 static void     fe_rint         ( struct fe_softc *, u_char );
  268 static void     fe_xmit         ( struct fe_softc * );
  269 static void     fe_emptybuffer  ( struct fe_softc * );
  270 static void     fe_write_mbufs  ( struct fe_softc *, struct mbuf * );
  271 static struct fe_filter
  272                 fe_mcaf         ( struct fe_softc * );
  273 static int      fe_hash         ( u_char * );
  274 static void     fe_setmode      ( struct fe_softc * );
  275 static void     fe_loadmar      ( struct fe_softc * );
  276 #if FE_DEBUG >= 1
  277 static void     fe_dump         ( int, struct fe_softc *, char * );
  278 #endif
  279 
  280 /* Driver struct used in the config code.  This must be public (external.)  */
  281 struct isa_driver fedriver =
  282 {
  283         fe_probe,
  284         fe_attach,
  285         "fe",
  286         1                       /* It's safe to mark as "sensitive"  */
  287 };
  288 
  289 /*
  290  * Fe driver specific constants which relate to 86960/86965.
  291  */
  292 
  293 /* Interrupt masks  */
  294 #define FE_TMASK ( FE_D2_COLL16 | FE_D2_TXDONE )
  295 #define FE_RMASK ( FE_D3_OVRFLO | FE_D3_CRCERR \
  296                  | FE_D3_ALGERR | FE_D3_SRTPKT | FE_D3_PKTRDY )
  297 
  298 /* Maximum number of iterations for a receive interrupt.  */
  299 #define FE_MAX_RECV_COUNT ( ( 65536 - 2048 * 2 ) / 64 )
  300         /*
  301          * Maximum size of SRAM is 65536,
  302          * minimum size of transmission buffer in fe is 2x2KB,
  303          * and minimum amount of received packet including headers
  304          * added by the chip is 64 bytes.
  305          * Hence FE_MAX_RECV_COUNT is the upper limit for number
  306          * of packets in the receive buffer.
  307          */
  308 
  309 /*
  310  * Routines to access contiguous I/O ports.
  311  */
  312 
  313 static void
  314 inblk ( struct fe_softc * sc, int offs, u_char * mem, int len )
  315 {
  316         while ( --len >= 0 ) {
  317                 *mem++ = inb( sc->ioaddr[ offs++ ] );
  318         }
  319 }
  320 
  321 static void
  322 outblk ( struct fe_softc * sc, int offs, u_char const * mem, int len )
  323 {
  324         while ( --len >= 0 ) {
  325                 outb( sc->ioaddr[ offs++ ], *mem++ );
  326         }
  327 }
  328 
  329 /* PCCARD Support */
  330 #if NCARD > 0
  331 /*
  332  *      PC-Card (PCMCIA) specific code.
  333  */
  334 static int feinit(struct pccard_devinfo *);             /* init device */
  335 static void feunload(struct pccard_devinfo *);          /* Disable driver */
  336 static int fe_card_intr(struct pccard_devinfo *);       /* Interrupt handler */
  337 
  338 static struct pccard_device fe_info = {
  339         "fe",
  340         feinit,
  341         feunload,
  342         fe_card_intr,
  343         0,                      /* Attributes - presently unused */
  344         &net_imask              /* Interrupt mask for device */
  345                                 /* XXX - Should this also include net_imask? */
  346 };
  347 
  348 DATA_SET(pccarddrv_set, fe_info);
  349   
  350 /*
  351  *      Initialize the device - called from Slot manager.
  352  */
  353 static int
  354 feinit(struct pccard_devinfo *devi)
  355 {
  356         struct fe_softc *sc;
  357 
  358         /* validate unit number. */
  359         if (devi->isahd.id_unit >= NFE)
  360                 return (ENODEV);
  361         /*
  362          * Probe the device. If a value is returned,
  363          * the device was found at the location.
  364          */
  365 #if FE_DEBUG >= 2
  366         printf("Start Probe\n");
  367 #endif
  368         sc = &fe_softc[devi->isahd.id_unit];
  369         memcpy(sc->sc_enaddr, devi->misc, ETHER_ADDR_LEN);
  370         if (fe_probe(&devi->isahd) == 0)
  371                 return (ENXIO);
  372 #if FE_DEBUG >= 2
  373         printf("Start attach\n");
  374 #endif
  375         if (fe_attach(&devi->isahd) == 0)
  376                 return (ENXIO);
  377 
  378         return (0);
  379 }
  380 
  381 /*
  382  *      feunload - unload the driver and clear the table.
  383  *      XXX TODO:
  384  *      This is usually called when the card is ejected, but
  385  *      can be caused by a modunload of a controller driver.
  386  *      The idea is to reset the driver's view of the device
  387  *      and ensure that any driver entry points such as
  388  *      read and write do not hang.
  389  */
  390 static void
  391 feunload(struct pccard_devinfo *devi)
  392 {
  393         struct fe_softc *sc = &fe_softc[devi->isahd.id_unit];
  394         printf("fe%d: unload\n", devi->isahd.id_unit);
  395         fe_stop(devi->isahd.id_unit);
  396 }
  397 
  398 /*
  399  *      fe_card_intr - Shared interrupt called from
  400  *       front end of PC-Card handler.
  401  */
  402 static int
  403 fe_card_intr(struct pccard_devinfo *devi)
  404 {
  405         feintr(devi->isahd.id_unit);
  406         return (1);
  407 }
  408 #endif /* NCARD > 0 */
  409 
  410 
  411 /*
  412  * Hardware probe routines.
  413  */
  414 
  415 /* How and where to probe; to support automatic I/O address detection.  */
  416 struct fe_probe_list
  417 {
  418         int ( * probe ) ( DEVICE *, struct fe_softc * );
  419         u_short const * addresses;
  420 };
  421 
  422 /* Lists of possible addresses.  */
  423 static u_short const fe_fmv_addr [] =
  424         { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340, 0 };
  425 static u_short const fe_ati_addr [] =
  426         { 0x240, 0x260, 0x280, 0x2A0, 0x300, 0x320, 0x340, 0x380, 0 };
  427 
  428 static struct fe_probe_list const fe_probe_list [] =
  429 {
  430         { fe_probe_fmv, fe_fmv_addr },
  431         { fe_probe_ati, fe_ati_addr },
  432 #if NCARD > 0
  433         { fe_probe_mbh, NULL },  /* PCMCIAs cannot be auto-detected.  */
  434         { fe_probe_tdk, NULL },
  435 #endif
  436         { NULL, NULL }
  437 };
  438 
  439 
  440 /*
  441  * Determine if the device is present
  442  *
  443  *   on entry:
  444  *      a pointer to an isa_device struct
  445  *   on exit:
  446  *      zero if device not found
  447  *      or number of i/o addresses used (if found)
  448  */
  449 
  450 static int
  451 fe_probe ( DEVICE * dev )
  452 {
  453         struct fe_softc * sc;
  454         int u;
  455         int nports;
  456         struct fe_probe_list const * list;
  457         u_short const * addr;
  458         u_short single [ 2 ];
  459 
  460         /* Initialize "minimum" parts of our softc.  */
  461         sc = &fe_softc[ dev->id_unit ];
  462         sc->sc_unit = dev->id_unit;
  463 
  464         /* Probe each possibility, one at a time.  */
  465         for ( list = fe_probe_list; list->probe != NULL; list++ ) {
  466 
  467                 if ( dev->id_iobase != NO_IOADDR ) {
  468                         /* Probe one specific address.  */
  469                         single[ 0 ] = dev->id_iobase;
  470                         single[ 1 ] = 0;
  471                         addr = single;
  472                 } else if ( list->addresses != NULL ) {
  473                         /* Auto detect.  */
  474                         addr = list->addresses;
  475                 } else {
  476                         /* We need a list of addresses to do auto detect.  */
  477                         continue;
  478                 }
  479 
  480                 /* Probe all possible addresses for the board.  */
  481                 while ( *addr != 0 ) {
  482 
  483                         /* See if the address is already in use.  */
  484                         for ( u = 0; u < NFE; u++ ) {
  485                                 if ( fe_softc[u].iobase == *addr ) break;
  486                         }
  487 
  488 #if FE_DEBUG >= 3
  489                         if ( u == NFE ) {
  490                             log( LOG_INFO, "fe%d: probing %d at 0x%x\n",
  491                                 sc->sc_unit, list - fe_probe_list, *addr );
  492                         } else if ( u == sc->sc_unit ) {
  493                             log( LOG_INFO, "fe%d: re-probing %d at 0x%x?\n",
  494                                 sc->sc_unit, list - fe_probe_list, *addr );
  495                         } else {
  496                             log( LOG_INFO, "fe%d: skipping %d at 0x%x\n",
  497                                 sc->sc_unit, list - fe_probe_list, *addr );
  498                         }
  499 #endif
  500 
  501                         /* Probe the address if it is free.  */
  502                         if ( u == NFE || u == sc->sc_unit ) {
  503 
  504                                 /* Probe an address.  */
  505                                 sc->iobase = *addr;
  506                                 nports = list->probe( dev, sc );
  507                                 if ( nports > 0 ) {
  508                                     /* Found.  */
  509                                     dev->id_iobase = *addr;
  510                                     return ( nports );
  511                                 }
  512                                 sc->iobase = 0;
  513                         }
  514 
  515                         /* Try next.  */
  516                         addr++;
  517                 }
  518         }
  519 
  520         /* Probe failed.  */
  521         return ( 0 );
  522 }
  523 
  524 /*
  525  * Check for specific bits in specific registers have specific values.
  526  */
  527 struct fe_simple_probe_struct
  528 {
  529         u_char port;    /* Offset from the base I/O address.  */
  530         u_char mask;    /* Bits to be checked.  */
  531         u_char bits;    /* Values to be compared against.  */
  532 };
  533 
  534 static int
  535 fe_simple_probe ( struct fe_softc const * sc,
  536                   struct fe_simple_probe_struct const * sp )
  537 {
  538         struct fe_simple_probe_struct const * p;
  539 
  540         for ( p = sp; p->mask != 0; p++ ) {
  541 #if FE_DEBUG >=2
  542                 printf("Probe Port:%x,Value:%x,Mask:%x.Bits:%x\n",
  543                         p->port,inb(sc->ioaddr[ p->port]),p->mask,p->bits);
  544 #endif
  545                 if ( ( inb( sc->ioaddr[ p->port ] ) & p->mask ) != p->bits ) 
  546                 {
  547                         return ( 0 );
  548                 }
  549         }
  550         return ( 1 );
  551 }
  552 
  553 /*
  554  * Routines to read all bytes from the config EEPROM through MB86965A.
  555  * I'm not sure what exactly I'm doing here...  I was told just to follow
  556  * the steps, and it worked.  Could someone tell me why the following
  557  * code works?  (Or, why all similar codes I tried previously doesn't
  558  * work.)  FIXME.
  559  */
  560 
  561 static void
  562 fe_strobe_eeprom ( u_short bmpr16 )
  563 {
  564         /*
  565          * We must guarantee 800ns (or more) interval to access slow
  566          * EEPROMs.  The following redundant code provides enough
  567          * delay with ISA timing.  (Even if the bus clock is "tuned.")
  568          * Some modification will be needed on faster busses.
  569          */
  570         outb( bmpr16, FE_B16_SELECT );
  571         outb( bmpr16, FE_B16_SELECT );
  572         outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
  573         outb( bmpr16, FE_B16_SELECT | FE_B16_CLOCK );
  574         outb( bmpr16, FE_B16_SELECT );
  575         outb( bmpr16, FE_B16_SELECT );
  576 }
  577 
  578 static void
  579 fe_read_eeprom ( struct fe_softc * sc, u_char * data )
  580 {
  581         u_short bmpr16 = sc->ioaddr[ FE_BMPR16 ];
  582         u_short bmpr17 = sc->ioaddr[ FE_BMPR17 ];
  583         u_char n, val, bit;
  584 
  585         /* Read bytes from EEPROM; two bytes per an iteration.  */
  586         for ( n = 0; n < FE_EEPROM_SIZE / 2; n++ ) {
  587 
  588                 /* Reset the EEPROM interface.  */
  589                 outb( bmpr16, 0x00 );
  590                 outb( bmpr17, 0x00 );
  591 
  592                 /* Start EEPROM access.  */
  593                 outb( bmpr16, FE_B16_SELECT );
  594                 outb( bmpr17, FE_B17_DATA );
  595                 fe_strobe_eeprom( bmpr16 );
  596 
  597                 /* Pass the iteration count to the chip.  */
  598                 val = 0x80 | n;
  599                 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
  600                         outb( bmpr17, ( val & bit ) ? FE_B17_DATA : 0 );
  601                         fe_strobe_eeprom( bmpr16 );
  602                 }
  603                 outb( bmpr17, 0x00 );
  604 
  605                 /* Read a byte.  */
  606                 val = 0;
  607                 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
  608                         fe_strobe_eeprom( bmpr16 );
  609                         if ( inb( bmpr17 ) & FE_B17_DATA ) {
  610                                 val |= bit;
  611                         }
  612                 }
  613                 *data++ = val;
  614 
  615                 /* Read one more byte.  */
  616                 val = 0;
  617                 for ( bit = 0x80; bit != 0x00; bit >>= 1 ) {
  618                         fe_strobe_eeprom( bmpr16 );
  619                         if ( inb( bmpr17 ) & FE_B17_DATA ) {
  620                                 val |= bit;
  621                         }
  622                 }
  623                 *data++ = val;
  624         }
  625 
  626         /* Reset the EEPROM interface, again.  */
  627         outb( bmpr16, 0x00 );
  628         outb( bmpr17, 0x00 );
  629 
  630 #if FE_DEBUG >= 3
  631         /* Report what we got.  */
  632         data -= FE_EEPROM_SIZE;
  633         log( LOG_INFO, "fe%d: EEPROM:"
  634                 " %02x%02x%02x%02x %02x%02x%02x%02x -"
  635                 " %02x%02x%02x%02x %02x%02x%02x%02x -"
  636                 " %02x%02x%02x%02x %02x%02x%02x%02x -"
  637                 " %02x%02x%02x%02x %02x%02x%02x%02x\n",
  638                 sc->sc_unit,
  639                 data[ 0], data[ 1], data[ 2], data[ 3],
  640                 data[ 4], data[ 5], data[ 6], data[ 7],
  641                 data[ 8], data[ 9], data[10], data[11],
  642                 data[12], data[13], data[14], data[15],
  643                 data[16], data[17], data[18], data[19],
  644                 data[20], data[21], data[22], data[23],
  645                 data[24], data[25], data[26], data[27],
  646                 data[28], data[29], data[30], data[31] );
  647 #endif
  648 }
  649 
  650 /*
  651  * Hardware (vendor) specific probe routines.
  652  */
  653 
  654 /*
  655  * Probe and initialization for Fujitsu FMV-180 series boards
  656  */
  657 static int
  658 fe_probe_fmv ( DEVICE * dev, struct fe_softc * sc )
  659 {
  660         int i, n;
  661 
  662         static u_short const baseaddr [ 8 ] =
  663                 { 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x300, 0x340 };
  664         static u_short const irqmap [ 4 ] =
  665                 { IRQ3,  IRQ7,  IRQ10, IRQ15 };
  666 
  667         static struct fe_simple_probe_struct const probe_table [] = {
  668                 { FE_DLCR2, 0x70, 0x00 },
  669                 { FE_DLCR4, 0x08, 0x00 },
  670             /*  { FE_DLCR5, 0x80, 0x00 },       Doesn't work.  */
  671 
  672                 { FE_FMV0,  0x78, 0x50 },       /* ERRDY+PRRDY */
  673                 { FE_FMV1,  0xB0, 0x00 },       /* FMV-183/184 has 0x48 bits. */
  674                 { FE_FMV3,  0x7F, 0x00 },
  675 #if 1
  676         /*
  677          * Test *vendor* part of the station address for Fujitsu.
  678          * The test will gain reliability of probe process, but
  679          * it rejects FMV-180 clone boards manufactured by other vendors.
  680          * We have to turn the test off when such cards are made available.
  681          */
  682                 { FE_FMV4, 0xFF, 0x00 },
  683                 { FE_FMV5, 0xFF, 0x00 },
  684                 { FE_FMV6, 0xFF, 0x0E },
  685 #else
  686         /*
  687          * We can always verify the *first* 2 bits (in Ethernet
  688          * bit order) are "no multicast" and "no local" even for
  689          * unknown vendors.
  690          */
  691                 { FE_FMV4, 0x03, 0x00 },
  692 #endif
  693                 { 0 }
  694         };
  695 
  696         /* "Hardware revision ID"  */
  697         int revision;
  698 
  699         /*
  700          * See if the specified address is possible for FMV-180 series.
  701          */
  702         for ( i = 0; i < 8; i++ ) {
  703                 if ( baseaddr[ i ] == sc->iobase ) break;
  704         }
  705         if ( i == 8 ) return 0;
  706 
  707         /* Setup an I/O address mapping table.  */
  708         for ( i = 0; i < MAXREGISTERS; i++ ) {
  709                 sc->ioaddr[ i ] = sc->iobase + i;
  710         }
  711 
  712         /* Simple probe.  */
  713         if ( !fe_simple_probe( sc, probe_table ) ) return 0;
  714 
  715         /* Check if our I/O address matches config info. on EEPROM.  */
  716         n = ( inb( sc->ioaddr[ FE_FMV2 ] ) & FE_FMV2_IOS )
  717             >> FE_FMV2_IOS_SHIFT;
  718         if ( baseaddr[ n ] != sc->iobase ) {
  719 #if 0
  720             /* May not work on some revisions of the cards... FIXME.  */
  721             return 0;
  722 #else
  723             /* Just log the fact and see what happens... FIXME.  */
  724             log( LOG_WARNING, "fe%d: strange I/O config?\n", sc->sc_unit );
  725 #endif
  726         }
  727 
  728         /* Find the "hardware revision."  */
  729         revision = inb( sc->ioaddr[ FE_FMV1 ] ) & FE_FMV1_REV;
  730 
  731         /* Determine the card type.  */
  732         sc->typestr = NULL;
  733         switch ( inb( sc->ioaddr[ FE_FMV0 ] ) & FE_FMV0_MEDIA ) {
  734           case 0:
  735                 /* No interface?  This doesn't seem to be an FMV-180...  */
  736                 return 0;
  737           case FE_FMV0_MEDIUM_T:
  738                 switch ( revision ) {
  739                   case 8:
  740                     sc->typestr = "FMV-183";
  741                     break;
  742                   case 12:
  743                     sc->typestr = "FMV-183 (on-board)";
  744                     break;
  745                 }
  746                 break;
  747           case FE_FMV0_MEDIUM_T | FE_FMV0_MEDIUM_5:
  748                 switch ( revision ) {
  749                   case 0:
  750                     sc->typestr = "FMV-181";
  751                     break;
  752                   case 1:
  753                     sc->typestr = "FMV-181A";
  754                     break;
  755                 }
  756                 break;
  757           case FE_FMV0_MEDIUM_2:
  758                 switch ( revision ) {
  759                   case 8:
  760                     sc->typestr = "FMV-184 (CSR = 2)";
  761                     break;
  762                 }
  763                 break;
  764           case FE_FMV0_MEDIUM_5:
  765                 switch ( revision ) {
  766                   case 8:
  767                     sc->typestr = "FMV-184 (CSR = 1)";
  768                     break;
  769                 }
  770                 break;
  771           case FE_FMV0_MEDIUM_2 | FE_FMV0_MEDIUM_5:
  772                 switch ( revision ) {
  773                   case 0:
  774                     sc->typestr = "FMV-182";
  775                     break;
  776                   case 1:
  777                     sc->typestr = "FMV-182A";
  778                     break;
  779                   case 8:
  780                     sc->typestr = "FMV-184 (CSR = 3)";
  781                     break;
  782                 }
  783                 break;
  784         }
  785         if ( sc->typestr == NULL ) {
  786                 /* Unknown card type...  Hope the driver works.  */
  787                 sc->typestr = "unknown FMV-180 version";
  788                 log( LOG_WARNING, "fe%d: %s: %x-%x-%x-%x\n",
  789                         sc->sc_unit, sc->typestr,
  790                         inb( sc->ioaddr[ FE_FMV0 ] ),
  791                         inb( sc->ioaddr[ FE_FMV1 ] ),
  792                         inb( sc->ioaddr[ FE_FMV2 ] ),
  793                         inb( sc->ioaddr[ FE_FMV3 ] ) );
  794         }
  795 
  796         /*
  797          * An FMV-180 has been proved.
  798          * Determine which IRQ to be used.
  799          *
  800          * In this version, we give a priority to the kernel config file.
  801          * If the EEPROM and config don't match, say it to the user for
  802          * an attention.
  803          */
  804         n = ( inb( sc->ioaddr[ FE_FMV2 ] ) & FE_FMV2_IRS )
  805                 >> FE_FMV2_IRS_SHIFT;
  806         if ( dev->id_irq == NO_IRQ ) {
  807                 /* Just use the probed value.  */
  808                 dev->id_irq = irqmap[ n ];
  809         } else if ( dev->id_irq != irqmap[ n ] ) {
  810                 /* Don't match.  */
  811                 log( LOG_WARNING,
  812                     "fe%d: check IRQ in config; it may be incorrect\n",
  813                     sc->sc_unit );
  814         }
  815 
  816         /*
  817          * Initialize constants in the per-line structure.
  818          */
  819 
  820         /* Get our station address from EEPROM.  */
  821         inblk( sc, FE_FMV4, sc->sc_enaddr, ETHER_ADDR_LEN );
  822 
  823         /* Make sure we got a valid station address.  */
  824         if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
  825           || ( sc->sc_enaddr[ 0 ] == 0x00
  826             && sc->sc_enaddr[ 1 ] == 0x00
  827             && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
  828 
  829         /*
  830          * Register values which (may) depend on board design.
  831          *
  832          * Program the 86960 as follows:
  833          *      SRAM: 32KB, 100ns, byte-wide access.
  834          *      Transmission buffer: 4KB x 2.
  835          *      System bus interface: 16 bits.
  836          */
  837         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
  838         sc->proto_dlcr5 = 0;
  839         sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
  840                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
  841         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
  842         sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
  843 
  844         /*
  845          * Minimum initialization of the hardware.
  846          * We write into registers; hope I/O ports have no
  847          * overlap with other boards.
  848          */
  849 
  850         /* Initialize ASIC.  */
  851         outb( sc->ioaddr[ FE_FMV3 ], 0 );
  852         outb( sc->ioaddr[ FE_FMV10 ], 0 );
  853 
  854         /* Initialize 86960.  */
  855         DELAY( 200 );
  856         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
  857         DELAY( 200 );
  858 
  859         /* Disable all interrupts.  */
  860         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
  861         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
  862 
  863         /* "Refresh" hardware configuration.  FIXME.  */
  864         outb( sc->ioaddr[ FE_FMV2 ], inb( sc->ioaddr[ FE_FMV2 ] ) );
  865 
  866         /* Turn the "master interrupt control" flag of ASIC on.  */
  867         outb( sc->ioaddr[ FE_FMV3 ], FE_FMV3_IRQENB );
  868 
  869         /*
  870          * That's all.  FMV-180 occupies 32 I/O addresses, by the way.
  871          */
  872         return 32;
  873 }
  874 
  875 /*
  876  * Probe and initialization for Allied-Telesis AT1700/RE2000 series.
  877  */
  878 static int
  879 fe_probe_ati ( DEVICE * dev, struct fe_softc * sc )
  880 {
  881         int i, n;
  882         u_char eeprom [ FE_EEPROM_SIZE ];
  883         u_char save16, save17;
  884 
  885         static u_short const baseaddr [ 8 ] =
  886                 { 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 };
  887         static u_short const irqmaps [ 4 ][ 4 ] =
  888         {
  889                 { IRQ3,  IRQ4,  IRQ5,  IRQ9  },
  890                 { IRQ10, IRQ11, IRQ12, IRQ15 },
  891                 { IRQ3,  IRQ11, IRQ5,  IRQ15 },
  892                 { IRQ10, IRQ11, IRQ14, IRQ15 },
  893         };
  894         static struct fe_simple_probe_struct const probe_table [] = {
  895                 { FE_DLCR2,  0x70, 0x00 },
  896                 { FE_DLCR4,  0x08, 0x00 },
  897                 { FE_DLCR5,  0x80, 0x00 },
  898 #if 0
  899                 { FE_BMPR16, 0x1B, 0x00 },
  900                 { FE_BMPR17, 0x7F, 0x00 },
  901 #endif
  902                 { 0 }
  903         };
  904 
  905         /* Assume we have 86965 and no need to restore these.  */
  906         save16 = 0;
  907         save17 = 0;
  908 
  909 #if FE_DEBUG >= 3
  910         log( LOG_INFO, "fe%d: probe (0x%x) for ATI\n",
  911              sc->sc_unit, sc->iobase );
  912         fe_dump( LOG_INFO, sc, NULL );
  913 #endif
  914 
  915         /*
  916          * See if the specified address is possible for MB86965A JLI mode.
  917          */
  918         for ( i = 0; i < 8; i++ ) {
  919                 if ( baseaddr[ i ] == sc->iobase ) break;
  920         }
  921         if ( i == 8 ) goto NOTFOUND;
  922 
  923         /* Setup an I/O address mapping table.  */
  924         for ( i = 0; i < MAXREGISTERS; i++ ) {
  925                 sc->ioaddr[ i ] = sc->iobase + i;
  926         }
  927 
  928         /*
  929          * We should test if MB86965A is on the base address now.
  930          * Unfortunately, it is very hard to probe it reliably, since
  931          * we have no way to reset the chip under software control.
  932          * On cold boot, we could check the "signature" bit patterns
  933          * described in the Fujitsu document.  On warm boot, however,
  934          * we can predict almost nothing about register values.
  935          */
  936         if ( !fe_simple_probe( sc, probe_table ) ) goto NOTFOUND;
  937 
  938         /* Check if our I/O address matches config info on 86965.  */
  939         n = ( inb( sc->ioaddr[ FE_BMPR19 ] ) & FE_B19_ADDR )
  940             >> FE_B19_ADDR_SHIFT;
  941         if ( baseaddr[ n ] != sc->iobase ) goto NOTFOUND;
  942 
  943         /*
  944          * We are now almost sure we have an AT1700 at the given
  945          * address.  So, read EEPROM through 86965.  We have to write
  946          * into LSI registers to read from EEPROM.  I want to avoid it
  947          * at this stage, but I cannot test the presence of the chip
  948          * any further without reading EEPROM.  FIXME.
  949          */
  950         save16 = inb( sc->ioaddr[ FE_BMPR16 ] );
  951         save17 = inb( sc->ioaddr[ FE_BMPR17 ] );
  952         fe_read_eeprom( sc, eeprom );
  953 
  954         /* Make sure the EEPROM is turned off.  */
  955         outb( sc->ioaddr[ FE_BMPR16 ], 0 );
  956         outb( sc->ioaddr[ FE_BMPR17 ], 0 );
  957 
  958         /* Make sure that config info in EEPROM and 86965 agree.  */
  959         if ( eeprom[ FE_EEPROM_CONF ] != inb( sc->ioaddr[ FE_BMPR19 ] ) ) {
  960                 goto NOTFOUND;
  961         }
  962 
  963         /*
  964          * The following model identification codes are stolen from
  965          * from the NetBSD port of the fe driver.  My reviewers
  966          * suggested minor revision.
  967          */
  968 
  969         /* Determine the card type.  */
  970         switch (eeprom[FE_ATI_EEP_MODEL]) {
  971           case FE_ATI_MODEL_AT1700T:
  972                 sc->typestr = "AT-1700T/RE2001";
  973                 break;
  974           case FE_ATI_MODEL_AT1700BT:
  975                 sc->typestr = "AT-1700BT/RE2003";
  976                 break;
  977           case FE_ATI_MODEL_AT1700FT:
  978                 sc->typestr = "AT-1700FT/RE2009";
  979                 break;
  980           case FE_ATI_MODEL_AT1700AT:
  981                 sc->typestr = "AT-1700AT/RE2005";
  982                 break;
  983           default:
  984                 sc->typestr = "unknown AT-1700/RE2000 ?";
  985                 break;
  986         }
  987 
  988         /*
  989          * Try to determine IRQ settings.
  990          * Different models use different ranges of IRQs.
  991          */
  992         if ( dev->id_irq == NO_IRQ ) {
  993                 n = ( inb( sc->ioaddr[ FE_BMPR19 ] ) & FE_B19_IRQ )
  994                     >> FE_B19_IRQ_SHIFT;
  995                 switch ( eeprom[ FE_ATI_EEP_REVISION ] & 0xf0 ) {
  996                   case 0x30:
  997                         dev->id_irq = irqmaps[ 3 ][ n ];
  998                         break;
  999                   case 0x10:
 1000                   case 0x50:
 1001                         dev->id_irq = irqmaps[ 2 ][ n ];
 1002                         break;
 1003                   case 0x40:
 1004                   case 0x60:
 1005                         if ( eeprom[ FE_ATI_EEP_MAGIC ] & 0x04 ) {
 1006                                 dev->id_irq = irqmaps[ 1 ][ n ];
 1007                         } else {
 1008                                 dev->id_irq = irqmaps[ 0 ][ n ];
 1009                         }
 1010                         break;
 1011                   default:
 1012                         dev->id_irq = irqmaps[ 0 ][ n ];
 1013                         break;
 1014                 }
 1015         }
 1016 
 1017 
 1018         /*
 1019          * Initialize constants in the per-line structure.
 1020          */
 1021 
 1022         /* Get our station address from EEPROM.  */
 1023         bcopy( eeprom + FE_ATI_EEP_ADDR, sc->sc_enaddr, ETHER_ADDR_LEN );
 1024 
 1025 #if 1
 1026         /*
 1027          * This test doesn't work well for AT1700 look-alike by
 1028          * other vendors.
 1029          */
 1030         /* Make sure the vendor part is for Allied-Telesis.  */
 1031         if ( sc->sc_enaddr[ 0 ] != 0x00
 1032           || sc->sc_enaddr[ 1 ] != 0x00
 1033           || sc->sc_enaddr[ 2 ] != 0xF4 ) return 0;
 1034 
 1035 #else
 1036         /* Make sure we got a valid station address.  */
 1037         if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
 1038           || ( sc->sc_enaddr[ 0 ] == 0x00
 1039             && sc->sc_enaddr[ 1 ] == 0x00
 1040             && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
 1041 #endif
 1042 
 1043         /*
 1044          * Program the 86960 as follows:
 1045          *      SRAM: 32KB, 100ns, byte-wide access.
 1046          *      Transmission buffer: 4KB x 2.
 1047          *      System bus interface: 16 bits.
 1048          */
 1049         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;  /* FIXME */
 1050         sc->proto_dlcr5 = 0;
 1051         sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
 1052                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
 1053         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_EC;
 1054 #if 0   /* XXXX Should we use this?  FIXME.  */
 1055         sc->proto_bmpr13 = eeprom[ FE_ATI_EEP_MEDIA ];
 1056 #else
 1057         sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
 1058 #endif
 1059 
 1060 #if FE_DEBUG >= 3
 1061         fe_dump( LOG_INFO, sc, "ATI found" );
 1062 #endif
 1063 
 1064         /* Setup hooks.  This may solves a nasty bug.  FIXME.  */
 1065         sc->init = fe_init_ati;
 1066 
 1067         /* Initialize 86965.  */
 1068         DELAY( 200 );
 1069         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1070         DELAY( 200 );
 1071 
 1072         /* Disable all interrupts.  */
 1073         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
 1074         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
 1075 
 1076 #if FE_DEBUG >= 3
 1077         fe_dump( LOG_INFO, sc, "end of fe_probe_ati()" );
 1078 #endif
 1079 
 1080         /*
 1081          * That's all.  AT1700 occupies 32 I/O addresses, by the way.
 1082          */
 1083         return 32;
 1084 
 1085       NOTFOUND:
 1086         /*
 1087          * We have no AT1700 at a given address.
 1088          * Restore BMPR16 and BMPR17 if we have destroyed them,
 1089          * hoping that the hardware on the address didn't get
 1090          * bad side effect.
 1091          */
 1092         if ( save16 != 0 | save17 != 0 ) {
 1093                 outb( sc->ioaddr[ FE_BMPR16 ], save16 );
 1094                 outb( sc->ioaddr[ FE_BMPR17 ], save17 );
 1095         }
 1096         return ( 0 );
 1097 }
 1098 
 1099 /* ATI specific initialization routine.  */
 1100 static void
 1101 fe_init_ati ( struct fe_softc * sc )
 1102 {
 1103 /*
 1104          * I've told that the following operation "Resets" the chip.
 1105          * Hope this solve a bug which hangs up the driver under
 1106          * heavy load...  FIXME.
 1107          */
 1108 
 1109         /* Minimal initialization of 86965.  */
 1110         DELAY( 200 );
 1111         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1112         DELAY( 200 );
 1113 
 1114         /* "Reset" by wrting into an undocument register location.  */
 1115         outb( sc->ioaddr[ 0x1F ], 0 );
 1116 
 1117         /* How long do we have to wait after the reset?  FIXME.  */
 1118         DELAY( 300 );
 1119 }
 1120 
 1121 /*
 1122  * Probe and initialization for Gateway Communications' old cards.
 1123  */
 1124 static int
 1125 fe_probe_gwy ( DEVICE * dev, struct fe_softc * sc )
 1126 {
 1127         int i,type;
 1128 
 1129         static struct fe_simple_probe_struct probe_table [] = {
 1130                 { FE_DLCR2, 0x70, 0x00 },
 1131                 { FE_DLCR4, 0x08, 0x00 },
 1132                 { FE_DLCR7, 0xC0, 0x00 },
 1133         /*
 1134                  * Test *vendor* part of the address for Gateway.
 1135                  * This test is essential to identify Gateway's cards.
 1136                  * We shuld define some symbolic names for the
 1137                  * following offsets.  FIXME.
 1138                  */
 1139                 { 0x18, 0xFF, 0x00 },
 1140                 { 0x19, 0xFF, 0x00 },
 1141                 { 0x1A, 0xFF, 0x61 },
 1142                 { 0 }
 1143         };
 1144 
 1145         /*
 1146          * We need explicit IRQ and supported address.
 1147          * I'm not sure which address and IRQ is possible for Gateway
 1148          * Ethernet family.  The following accepts everything.  FIXME.
 1149          */
 1150         if ( dev->id_irq == NO_IRQ || ( sc->iobase & ~0x3E0 ) != 0 ) {
 1151                 return ( 0 );
 1152         }
 1153 
 1154 #if FE_DEBUG >= 3
 1155         fe_dump( LOG_INFO, sc, "top of probe" );
 1156 #endif
 1157 
 1158         /* Setup an I/O address mapping table.  */
 1159         for ( i = 0; i < MAXREGISTERS; i++ ) {
 1160                 sc->ioaddr[ i ] = sc->iobase + i;
 1161         }
 1162 
 1163         /* See if the card is on its address.  */
 1164         if ( !fe_simple_probe( sc, probe_table ) ) {
 1165                 return 0;
 1166         }
 1167 
 1168         /* Determine the card type.  */
 1169         sc->typestr = "Gateway Ethernet w/ Fujitsu chipset";
 1170 
 1171         /* Get our station address from EEPROM. */
 1172         inblk( sc, 0x18, sc->sc_enaddr, ETHER_ADDR_LEN );
 1173 
 1174         /*
 1175          * Program the 86960 as follows:
 1176          *      SRAM: 16KB, 100ns, byte-wide access.
 1177          *      Transmission buffer: 2KB x 2.
 1178          *      System bus interface: 16 bits.
 1179          * Make sure to clear out ID bits in DLCR7
 1180          * (They actually are Encoder/Decoder control in NICE.)
 1181          */
 1182         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
 1183         sc->proto_dlcr5 = 0;
 1184         sc->proto_dlcr6 = FE_D6_BUFSIZ_16KB | FE_D6_TXBSIZ_2x2KB
 1185                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
 1186         sc->proto_dlcr7 = FE_D7_BYTSWP_LH;
 1187         sc->proto_bmpr13 = 0;
 1188 
 1189         /* Minimal initialization of 86960.  */
 1190         DELAY( 200 );
 1191         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1192         DELAY( 200 );
 1193 
 1194         /* Disable all interrupts.  */
 1195         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
 1196         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
 1197 
 1198         /* That's all.  The card occupies 32 I/O addresses, as always.  */
 1199         return 32;
 1200 }
 1201 
 1202 #if NCARD > 0
 1203         /*
 1204  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
 1205  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
 1206  */
 1207 static int
 1208 fe_probe_mbh ( DEVICE * dev, struct fe_softc * sc )
 1209 {
 1210         int i,type;
 1211 
 1212         static struct fe_simple_probe_struct probe_table [] = {
 1213                 { FE_DLCR0, 0x09, 0x00 },
 1214                 { FE_DLCR2, 0x79, 0x00 },
 1215                 { FE_DLCR4, 0x08, 0x00 },
 1216                 { FE_DLCR6, 0xFF, 0xB6 },
 1217         /*
 1218          * The following location has the first byte of the card's
 1219          * Ethernet (MAC) address.
 1220          * We can always verify the *first* 2 bits (in Ethernet
 1221          * bit order) are "global" and "unicast" for any vendors'.
 1222          */
 1223                 { FE_MBH10, 0x03, 0x00 },
 1224 
 1225         /* Just a gap?  Seems reliable, anyway.  */
 1226                 { 0x12, 0xFF, 0x00 },
 1227                 { 0x13, 0xFF, 0x00 },
 1228                 { 0x14, 0xFF, 0x00 },
 1229                 { 0x15, 0xFF, 0x00 },
 1230                 { 0x16, 0xFF, 0x00 },
 1231                 { 0x17, 0xFF, 0x00 },
 1232 #if 0
 1233                 { 0x18, 0xFF, 0xFF },
 1234                 { 0x19, 0xFF, 0xFF },
 1235 #endif
 1236 
 1237                 { 0 }
 1238         };
 1239 
 1240         /*
 1241          * We need explicit IRQ and supported address.
 1242          */
 1243         if ( dev->id_irq == NO_IRQ || ( sc->iobase & ~0x3E0 ) != 0 ) {
 1244                 return ( 0 );
 1245         }
 1246 
 1247 #if FE_DEBUG >= 3
 1248         fe_dump( LOG_INFO, sc, "top of probe" );
 1249 #endif
 1250 
 1251         /* Setup an I/O address mapping table.  */
 1252         for ( i = 0; i < MAXREGISTERS; i++ ) {
 1253                 sc->ioaddr[ i ] = sc->iobase + i;
 1254         }
 1255 
 1256         /*
 1257          * See if MBH10302 is on its address.
 1258          * I'm not sure the following probe code works.  FIXME.
 1259          */
 1260         if ( !fe_simple_probe( sc, probe_table ) ) return 0;
 1261 
 1262         /* Determine the card type.  */
 1263         sc->typestr = "MBH10302 (PCMCIA)";
 1264 
 1265         /*
 1266          * Initialize constants in the per-line structure.
 1267          */
 1268 
 1269         /* Get our station address from EEPROM.  */
 1270         inblk( sc, FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN );
 1271 
 1272         /* Make sure we got a valid station address.  */
 1273         if ( sc->sc_enaddr[ 0 ] == 0x00
 1274             && sc->sc_enaddr[ 1 ] == 0x00
 1275           && sc->sc_enaddr[ 2 ] == 0x00 ) return 0;
 1276 
 1277         /*
 1278          * Program the 86960 as follows:
 1279          *      SRAM: 32KB, 100ns, byte-wide access.
 1280          *      Transmission buffer: 4KB x 2.
 1281          *      System bus interface: 16 bits.
 1282          */
 1283         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
 1284         sc->proto_dlcr5 = 0;
 1285         sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
 1286                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
 1287         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
 1288         sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
 1289 
 1290         /* Setup hooks.  We need a special initialization procedure.  */
 1291         sc->init = fe_init_mbh;
 1292 
 1293         /*
 1294          * Minimum initialization.
 1295          */
 1296 
 1297         /* Minimal initialization of 86960.  */
 1298         DELAY( 200 );
 1299         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1300         DELAY( 200 );
 1301 
 1302         /* Disable all interrupts.  */
 1303         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
 1304         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
 1305 
 1306 #if 1   /* FIXME.  */
 1307         /* Initialize system bus interface and encoder/decoder operation.  */
 1308         outb( sc->ioaddr[ FE_MBH0 ], FE_MBH0_MAGIC | FE_MBH0_INTR_DISABLE );
 1309 #endif
 1310 
 1311         /*
 1312          * That's all.  MBH10302 occupies 32 I/O addresses, by the way.
 1313          */
 1314         return 32;
 1315 }
 1316 
 1317 /* MBH specific initialization routine.  */
 1318 static void
 1319 fe_init_mbh ( struct fe_softc * sc )
 1320 {
 1321         /* Minimal initialization of 86960.  */
 1322         DELAY( 200 );
 1323         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1324         DELAY( 200 );
 1325 
 1326         /* Disable all interrupts.  */
 1327         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
 1328         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
 1329 
 1330         /* Enable master interrupt flag.  */
 1331         outb( sc->ioaddr[ FE_MBH0 ], FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE );
 1332 }
 1333 
 1334 /*
 1335  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
 1336  * by MASUI Kenji <masui@cs.titech.ac.jp>
 1337  *
 1338  * (Contec uses TDK Ethenet chip -- hosokawa)
 1339  *
 1340  * This version of fe_probe_tdk has been rewrote to handle
 1341  * *generic* PC card implementation of Fujitsu MB8696x family.  The
 1342  * name _tdk is just for a historical reason. :-)
 1343  */
 1344 static int
 1345 fe_probe_tdk ( DEVICE * dev, struct fe_softc * sc )
 1346 {
 1347         int i;
 1348 
 1349         static struct fe_simple_probe_struct probe_table [] = {
 1350                 { FE_DLCR2, 0x70, 0x00 },
 1351                 { FE_DLCR4, 0x08, 0x00 },
 1352             /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
 1353                 { 0 }
 1354         };
 1355 
 1356         if ( dev->id_irq == NO_IRQ ) {
 1357                 return ( 0 );
 1358         }
 1359 
 1360         /* Setup an I/O address mapping table.  */
 1361         for ( i = 0; i < MAXREGISTERS; i++ ) {
 1362                 sc->ioaddr[ i ] = sc->iobase + i;
 1363         }
 1364 
 1365         /*
 1366          * See if C-NET(PC)C is on its address.
 1367          */
 1368 
 1369         if ( !fe_simple_probe( sc, probe_table ) ) return 0;
 1370 
 1371         /* Determine the card type.  */
 1372         sc->typestr = "Generic MB8696x Ethernet (PCMCIA)";
 1373 
 1374         /*
 1375          * Initialize constants in the per-line structure.
 1376          */
 1377 
 1378         /* The station address *must*be* already in sc_enaddr;
 1379            Make sure we got a valid station address.  */
 1380         if ( ( sc->sc_enaddr[ 0 ] & 0x03 ) != 0x00
 1381           || ( sc->sc_enaddr[ 0 ] == 0x00
 1382             && sc->sc_enaddr[ 1 ] == 0x00
 1383             && sc->sc_enaddr[ 2 ] == 0x00 ) ) return 0;
 1384 
 1385         /*
 1386          * Program the 86965 as follows:
 1387          *      SRAM: 32KB, 100ns, byte-wide access.
 1388          *      Transmission buffer: 4KB x 2.
 1389          *      System bus interface: 16 bits.
 1390          * XXX: Should we remove IDENT_NICE from DLCR7?  Or,
 1391          *      even add IDENT_EC instead?  FIXME.
 1392          */
 1393         sc->proto_dlcr4 = FE_D4_LBC_DISABLE | FE_D4_CNTRL;
 1394         sc->proto_dlcr5 = 0;
 1395         sc->proto_dlcr6 = FE_D6_BUFSIZ_32KB | FE_D6_TXBSIZ_2x4KB
 1396                 | FE_D6_BBW_BYTE | FE_D6_SBW_WORD | FE_D6_SRAM_100ns;
 1397         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
 1398         sc->proto_bmpr13 = FE_B13_TPTYPE_UTP | FE_B13_PORT_AUTO;
 1399 
 1400         /* Minimul initialization of 86960.  */
 1401         DELAY( 200 );
 1402         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1403         DELAY( 200 );
 1404 
 1405         /* Disable all interrupts.  */
 1406         outb( sc->ioaddr[ FE_DLCR2 ], 0 );
 1407         outb( sc->ioaddr[ FE_DLCR3 ], 0 );
 1408 
 1409         /*
 1410          * That's all.  C-NET(PC)C occupies 16 I/O addresses.
 1411          * XXX: Are there any card with 32 I/O addresses?  FIXME.
 1412          */
 1413         return 16;
 1414 }
 1415 #endif /* NCARD > 0 */
 1416 
 1417 /*
 1418  * Install interface into kernel networking data structures
 1419  */
 1420 static int
 1421 fe_attach ( DEVICE * dev )
 1422 {
 1423 #if NCARD > 0
 1424         static  int     already_ifattach[NFE];
 1425 #endif
 1426         struct fe_softc *sc = &fe_softc[dev->id_unit];
 1427 
 1428         /*
 1429          * Initialize ifnet structure
 1430          */
 1431         sc->sc_if.if_softc    = sc;
 1432         sc->sc_if.if_unit     = sc->sc_unit;
 1433         sc->sc_if.if_name     = "fe";
 1434         sc->sc_if.if_output   = ether_output;
 1435         sc->sc_if.if_start    = fe_start;
 1436         sc->sc_if.if_ioctl    = fe_ioctl;
 1437         sc->sc_if.if_watchdog = fe_watchdog;
 1438 
 1439         /*
 1440          * Set default interface flags.
 1441          */
 1442         sc->sc_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 1443 
 1444         /*
 1445          * Set maximum size of output queue, if it has not been set.
 1446          * It is done here as this driver may be started after the
 1447          * system initialization (i.e., the interface is PCMCIA.)
 1448          *
 1449          * I'm not sure this is really necessary, but, even if it is,
 1450          * it should be done somewhere else, e.g., in if_attach(),
 1451          * since it must be a common workaround for all network drivers.
 1452          * FIXME.
 1453          */
 1454         if ( sc->sc_if.if_snd.ifq_maxlen == 0 ) {
 1455                 sc->sc_if.if_snd.ifq_maxlen = ifqmaxlen;
 1456         }
 1457 
 1458 #if FE_DEBUG >= 3
 1459         fe_dump( LOG_INFO, sc, "attach()" );
 1460 #endif
 1461 
 1462 #if FE_SINGLE_TRANSMISSION
 1463         /* Override txb config to allocate minimum.  */
 1464         sc->proto_dlcr6 &= ~FE_D6_TXBSIZ
 1465         sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
 1466 #endif
 1467 
 1468         /* Modify hardware config if it is requested.  */
 1469         if ( dev->id_flags & FE_FLAGS_OVERRIDE_DLCR6 ) {
 1470                 sc->proto_dlcr6 = dev->id_flags & FE_FLAGS_DLCR6_VALUE;
 1471         }
 1472 
 1473         /* Find TX buffer size, based on the hardware dependent proto.  */
 1474         switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
 1475           case FE_D6_TXBSIZ_2x2KB: sc->txb_size = 2048; break;
 1476           case FE_D6_TXBSIZ_2x4KB: sc->txb_size = 4096; break;
 1477           case FE_D6_TXBSIZ_2x8KB: sc->txb_size = 8192; break;
 1478           default:
 1479                 /* Oops, we can't work with single buffer configuration.  */
 1480 #if FE_DEBUG >= 2
 1481                 log( LOG_WARNING, "fe%d: strange TXBSIZ config; fixing\n",
 1482                         sc->sc_unit );
 1483 #endif
 1484                 sc->proto_dlcr6 &= ~FE_D6_TXBSIZ;
 1485                 sc->proto_dlcr6 |=  FE_D6_TXBSIZ_2x2KB;
 1486                 sc->txb_size = 2048;
 1487                 break;
 1488         }
 1489 
 1490         /* Attach and stop the interface. */
 1491 #if NCARD > 0
 1492         if (already_ifattach[dev->id_unit] != 1) {
 1493                 if_attach(&sc->sc_if);
 1494                 already_ifattach[dev->id_unit] = 1;
 1495         }
 1496 #else
 1497         if_attach(&sc->sc_if);
 1498 #endif
 1499         fe_stop(sc->sc_unit);           /* This changes the state to IDLE.  */
 1500         ether_ifattach(&sc->sc_if);
 1501   
 1502         /* Print additional info when attached.  */
 1503         printf( "fe%d: address %6D, type %s\n", sc->sc_unit,
 1504                 sc->sc_enaddr, ":" , sc->typestr );
 1505 #if FE_DEBUG >= 3
 1506         {
 1507                 int buf, txb, bbw, sbw, ram;
 1508 
 1509                 buf = txb = bbw = sbw = ram = -1;
 1510                 switch ( sc->proto_dlcr6 & FE_D6_BUFSIZ ) {
 1511                   case FE_D6_BUFSIZ_8KB:  buf =  8; break;
 1512                   case FE_D6_BUFSIZ_16KB: buf = 16; break;
 1513                   case FE_D6_BUFSIZ_32KB: buf = 32; break;
 1514                   case FE_D6_BUFSIZ_64KB: buf = 64; break;
 1515                 }
 1516                 switch ( sc->proto_dlcr6 & FE_D6_TXBSIZ ) {
 1517                   case FE_D6_TXBSIZ_2x2KB: txb = 2; break;
 1518                   case FE_D6_TXBSIZ_2x4KB: txb = 4; break;
 1519                   case FE_D6_TXBSIZ_2x8KB: txb = 8; break;
 1520                 }
 1521                 switch ( sc->proto_dlcr6 & FE_D6_BBW ) {
 1522                   case FE_D6_BBW_BYTE: bbw =  8; break;
 1523                   case FE_D6_BBW_WORD: bbw = 16; break;
 1524                 }
 1525                 switch ( sc->proto_dlcr6 & FE_D6_SBW ) {
 1526                   case FE_D6_SBW_BYTE: sbw =  8; break;
 1527                   case FE_D6_SBW_WORD: sbw = 16; break;
 1528                 }
 1529                 switch ( sc->proto_dlcr6 & FE_D6_SRAM ) {
 1530                   case FE_D6_SRAM_100ns: ram = 100; break;
 1531                   case FE_D6_SRAM_150ns: ram = 150; break;
 1532                 }
 1533                 printf( "fe%d: SRAM %dKB %dbit %dns, TXB %dKBx2, %dbit I/O\n",
 1534                         sc->sc_unit, buf, bbw, ram, txb, sbw );
 1535         }
 1536 #endif
 1537 
 1538 #if NBPFILTER > 0
 1539         /* If BPF is in the kernel, call the attach for it.  */
 1540         bpfattach( &sc->sc_if, DLT_EN10MB, sizeof(struct ether_header));
 1541 #endif
 1542         return 1;
 1543 }
 1544 
 1545 /*
 1546  * Reset interface.
 1547  */
 1548 static void
 1549 fe_reset ( int unit )
 1550 {
 1551         /*
 1552          * Stop interface and re-initialize.
 1553          */
 1554         fe_stop(unit);
 1555         fe_init(unit);
 1556 }
 1557 
 1558 /*
 1559  * Stop everything on the interface.
 1560  *
 1561  * All buffered packets, both transmitting and receiving,
 1562  * if any, will be lost by stopping the interface.
 1563  */
 1564 static void
 1565 fe_stop ( int unit )
 1566 {
 1567         struct fe_softc *sc = &fe_softc[unit];
 1568         int s;
 1569 
 1570         s = splimp();
 1571 
 1572 #if FE_DEBUG >= 3
 1573         fe_dump( LOG_INFO, sc, "stop()" );
 1574 #endif
 1575 
 1576         /* Disable interrupts.  */
 1577         outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
 1578         outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
 1579 
 1580         /* Stop interface hardware.  */
 1581         DELAY( 200 );
 1582         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1583         DELAY( 200 );
 1584 
 1585         /* Clear all interrupt status.  */
 1586         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );
 1587         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );
 1588 
 1589         /* Put the chip in stand-by mode.  */
 1590         DELAY( 200 );
 1591         outb( sc->ioaddr[ FE_DLCR7 ], sc->proto_dlcr7 | FE_D7_POWER_DOWN );
 1592         DELAY( 200 );
 1593 
 1594         /* Reset transmitter variables and interface flags.  */
 1595         sc->sc_if.if_flags &= ~( IFF_OACTIVE | IFF_RUNNING );
 1596         sc->sc_if.if_timer = 0;
 1597         sc->txb_free = sc->txb_size;
 1598         sc->txb_count = 0;
 1599         sc->txb_sched = 0;
 1600 
 1601         /* MAR loading can be delayed.  */
 1602         sc->filter_change = 0;
 1603 
 1604         /* Update config status also.  */
 1605 
 1606         /* Call a hook.  */
 1607         if ( sc->stop ) sc->stop( sc );
 1608 
 1609 #if FE_DEBUG >= 3
 1610         fe_dump( LOG_INFO, sc, "end of stop()" );
 1611 #endif
 1612 
 1613         (void) splx(s);
 1614 }
 1615 
 1616 /*
 1617  * Device timeout/watchdog routine. Entered if the device neglects to
 1618  * generate an interrupt after a transmit has been started on it.
 1619  */
 1620 static void
 1621 fe_watchdog ( struct ifnet *ifp )
 1622 {
 1623         struct fe_softc *sc = (struct fe_softc *)ifp;
 1624 
 1625 #if FE_DEBUG >= 1
 1626         /* A "debug" message.  */
 1627         log( LOG_ERR, "fe%d: transmission timeout (%d+%d)%s\n",
 1628                 ifp->if_unit, sc->txb_sched, sc->txb_count,
 1629                 ( ifp->if_flags & IFF_UP ) ? "" : " when down" );
 1630         if ( sc->sc_if.if_opackets == 0 && sc->sc_if.if_ipackets == 0 ) {
 1631                 log( LOG_WARNING, "fe%d: wrong IRQ setting in config?\n",
 1632                     ifp->if_unit );
 1633         }
 1634 #endif
 1635 
 1636 #if FE_DEBUG >= 3
 1637         fe_dump( LOG_INFO, sc, NULL );
 1638 #endif
 1639 
 1640         /* Record how many packets are lost by this accident.  */
 1641         ifp->if_oerrors += sc->txb_sched + sc->txb_count;
 1642 
 1643         /* Put the interface into known initial state.  */
 1644         if ( ifp->if_flags & IFF_UP ) {
 1645                 fe_reset( ifp->if_unit );
 1646         } else {
 1647                 fe_stop( ifp->if_unit );
 1648         }
 1649 }
 1650 
 1651 /*
 1652  * Initialize device.
 1653  */
 1654 static void
 1655 fe_init ( int unit )
 1656 {
 1657         struct fe_softc *sc = &fe_softc[unit];
 1658         int i, s;
 1659 
 1660 #if FE_DEBUG >= 3
 1661         fe_dump( LOG_INFO, sc, "init()" );
 1662 #endif
 1663 
 1664         /* We need an address. */
 1665         if (sc->sc_if.if_addrlist == 0) {
 1666 #if FE_DEBUG >= 1
 1667                 log( LOG_ERR, "fe%d: init() without any address\n",
 1668                         sc->sc_unit );
 1669 #endif
 1670                 return;
 1671         }
 1672 
 1673 #if FE_DEBUG >= 1
 1674         /*
 1675          * Make sure we have a valid station address.
 1676          * The following test is applicable for any Ethernet interfaces.
 1677          * It can be done in somewhere common to all of them.  FIXME.
 1678          */
 1679         if ( ( sc->sc_enaddr[ 0 ] & 0x01 ) != 0
 1680           || ( sc->sc_enaddr[ 0 ] == 0x00
 1681             && sc->sc_enaddr[ 1 ] == 0x00
 1682             && sc->sc_enaddr[ 2 ] == 0x00 ) ) {
 1683                 log( LOG_ERR, "fe%d: invalid station address (%6D)\n",
 1684                         sc->sc_unit, sc->sc_enaddr, ":" );
 1685                 return;
 1686         }
 1687 #endif
 1688 
 1689         /* Start initializing 86960.  */
 1690         s = splimp();
 1691 
 1692         /* Call a hook.  */
 1693         if ( sc->init ) sc->init( sc );
 1694 
 1695 #if FE_DEBUG >= 3
 1696         fe_dump( LOG_INFO, sc, "after init hook" );
 1697 #endif
 1698 
 1699         /*
 1700          * Make sure to disable the chip, also.
 1701          * This may also help re-programming the chip after
 1702          * hot insertion of PCMCIAs.
 1703          */
 1704         DELAY( 200 );
 1705         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 1706         DELAY( 200 );
 1707 
 1708         /* Power up the chip and select register bank for DLCRs.  */
 1709         DELAY( 200 );
 1710         outb( sc->ioaddr[ FE_DLCR7 ],
 1711                 sc->proto_dlcr7 | FE_D7_RBS_DLCR | FE_D7_POWER_UP );
 1712         DELAY( 200 );
 1713 
 1714         /* Feed the station address.  */
 1715         outblk( sc, FE_DLCR8, sc->sc_enaddr, ETHER_ADDR_LEN );
 1716 
 1717         /* Clear multicast address filter to receive nothing.  */
 1718         outb( sc->ioaddr[ FE_DLCR7 ],
 1719                 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
 1720         outblk( sc, FE_MAR8, fe_filter_nothing.data, FE_FILTER_LEN );
 1721 
 1722         /* Select the BMPR bank for runtime register access.  */
 1723         outb( sc->ioaddr[ FE_DLCR7 ],
 1724                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
 1725 
 1726         /* Initialize registers.  */
 1727         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );   /* Clear all bits.  */
 1728         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );   /* ditto.  */
 1729         outb( sc->ioaddr[ FE_DLCR2 ], 0x00 );
 1730         outb( sc->ioaddr[ FE_DLCR3 ], 0x00 );
 1731         outb( sc->ioaddr[ FE_DLCR4 ], sc->proto_dlcr4 );
 1732         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
 1733         outb( sc->ioaddr[ FE_BMPR10 ], 0x00 );
 1734         outb( sc->ioaddr[ FE_BMPR11 ], FE_B11_CTRL_SKIP | FE_B11_MODE1 );
 1735         outb( sc->ioaddr[ FE_BMPR12 ], 0x00 );
 1736         outb( sc->ioaddr[ FE_BMPR13 ], sc->proto_bmpr13 );
 1737         outb( sc->ioaddr[ FE_BMPR14 ], 0x00 );
 1738         outb( sc->ioaddr[ FE_BMPR15 ], 0x00 );
 1739 
 1740 #if FE_DEBUG >= 3
 1741         fe_dump( LOG_INFO, sc, "just before enabling DLC" );
 1742 #endif
 1743 
 1744         /* Enable interrupts.  */
 1745         outb( sc->ioaddr[ FE_DLCR2 ], FE_TMASK );
 1746         outb( sc->ioaddr[ FE_DLCR3 ], FE_RMASK );
 1747 
 1748         /* Enable transmitter and receiver.  */
 1749         DELAY( 200 );
 1750         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
 1751         DELAY( 200 );
 1752 
 1753 #if FE_DEBUG >= 3
 1754         fe_dump( LOG_INFO, sc, "just after enabling DLC" );
 1755 #endif
 1756 
 1757         /*
 1758          * Make sure to empty the receive buffer.
 1759          *
 1760          * This may be redundant, but *if* the receive buffer were full
 1761          * at this point, then the driver would hang.  I have experienced
 1762          * some strange hang-up just after UP.  I hope the following
 1763          * code solve the problem.
 1764          *
 1765          * I have changed the order of hardware initialization.
 1766          * I think the receive buffer cannot have any packets at this
 1767          * point in this version.  The following code *must* be
 1768          * redundant now.  FIXME.
 1769          *
 1770          * I've heard a rumore that on some PC card implementation of
 1771          * 8696x, the receive buffer can have some data at this point.
 1772          * The following message helps discovering the fact.  FIXME.
 1773          */
 1774         if ( !( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) ) {
 1775                 log( LOG_WARNING,
 1776                         "fe%d: receive buffer has some data after reset\n",
 1777                         sc->sc_unit );
 1778                 
 1779                 fe_emptybuffer( sc );
 1780         }
 1781 
 1782 #if FE_DEBUG >= 3
 1783         fe_dump( LOG_INFO, sc, "after ERB loop" );
 1784 #endif
 1785 
 1786         /* Do we need this here?  Actually, no.  I must be paranoia.  */
 1787         outb( sc->ioaddr[ FE_DLCR0 ], 0xFF );   /* Clear all bits.  */
 1788         outb( sc->ioaddr[ FE_DLCR1 ], 0xFF );   /* ditto.  */
 1789 
 1790 #if FE_DEBUG >= 3
 1791         fe_dump( LOG_INFO, sc, "after FIXME" );
 1792 #endif
 1793         /* Set 'running' flag, because we are now running.   */
 1794         sc->sc_if.if_flags |= IFF_RUNNING;
 1795 
 1796         /*
 1797          * At this point, the interface is running properly,
 1798          * except that it receives *no* packets.  we then call
 1799          * fe_setmode() to tell the chip what packets to be
 1800          * received, based on the if_flags and multicast group
 1801          * list.  It completes the initialization process.
 1802          */
 1803         fe_setmode( sc );
 1804 
 1805 #if FE_DEBUG >= 3
 1806         fe_dump( LOG_INFO, sc, "after setmode" );
 1807 #endif
 1808 
 1809         /* ...and attempt to start output queued packets.  */
 1810         fe_start( &sc->sc_if );
 1811 
 1812 #if FE_DEBUG >= 3
 1813         fe_dump( LOG_INFO, sc, "init() done" );
 1814 #endif
 1815 
 1816         (void) splx(s);
 1817 }
 1818 
 1819 /*
 1820  * This routine actually starts the transmission on the interface
 1821  */
 1822 static void
 1823 fe_xmit ( struct fe_softc * sc )
 1824 {
 1825         /*
 1826          * Set a timer just in case we never hear from the board again.
 1827          * We use longer timeout for multiple packet transmission.
 1828          * I'm not sure this timer value is appropriate.  FIXME.
 1829          */
 1830         sc->sc_if.if_timer = 1 + sc->txb_count;
 1831 
 1832         /* Update txb variables.  */
 1833         sc->txb_sched = sc->txb_count;
 1834         sc->txb_count = 0;
 1835         sc->txb_free = sc->txb_size;
 1836         sc->tx_excolls = 0;
 1837 
 1838         /* Start transmitter, passing packets in TX buffer.  */
 1839         outb( sc->ioaddr[ FE_BMPR10 ], sc->txb_sched | FE_B10_START );
 1840 }
 1841 
 1842 /*
 1843  * Start output on interface.
 1844  * We make two assumptions here:
 1845  *  1) that the current priority is set to splimp _before_ this code
 1846  *     is called *and* is returned to the appropriate priority after
 1847  *     return
 1848  *  2) that the IFF_OACTIVE flag is checked before this code is called
 1849  *     (i.e. that the output part of the interface is idle)
 1850  */
 1851 void
 1852 fe_start ( struct ifnet *ifp )
 1853 {
 1854         struct fe_softc *sc = ifp->if_softc;
 1855         struct mbuf *m;
 1856 
 1857 #if FE_DEBUG >= 1
 1858         /* Just a sanity check.  */
 1859         if ( ( sc->txb_count == 0 ) != ( sc->txb_free == sc->txb_size ) ) {
 1860                 /*
 1861                  * Txb_count and txb_free co-works to manage the
 1862                  * transmission buffer.  Txb_count keeps track of the
 1863                  * used potion of the buffer, while txb_free does unused
 1864                  * potion.  So, as long as the driver runs properly,
 1865                  * txb_count is zero if and only if txb_free is same
 1866                  * as txb_size (which represents whole buffer.)
 1867                  */
 1868                 log( LOG_ERR, "fe%d: inconsistent txb variables (%d, %d)\n",
 1869                         sc->sc_unit, sc->txb_count, sc->txb_free );
 1870                 /*
 1871                  * So, what should I do, then?
 1872                  *
 1873                  * We now know txb_count and txb_free contradicts.  We
 1874                  * cannot, however, tell which is wrong.  More
 1875                  * over, we cannot peek 86960 transmission buffer or
 1876                  * reset the transmission buffer.  (In fact, we can
 1877                  * reset the entire interface.  I don't want to do it.)
 1878                  *
 1879                  * If txb_count is incorrect, leaving it as-is will cause
 1880                  * sending of garbage after next interrupt.  We have to
 1881                  * avoid it.  Hence, we reset the txb_count here.  If
 1882                  * txb_free was incorrect, resetting txb_count just loose
 1883                  * some packets.  We can live with it.
 1884                  */
 1885                 sc->txb_count = 0;
 1886         }
 1887 #endif
 1888 
 1889 #if FE_DEBUG >= 1
 1890         /*
 1891          * First, see if there are buffered packets and an idle
 1892          * transmitter - should never happen at this point.
 1893          */
 1894         if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
 1895                 log( LOG_ERR,
 1896                         "fe%d: transmitter idle with %d buffered packets\n",
 1897                         sc->sc_unit, sc->txb_count );
 1898                 fe_xmit( sc );
 1899         }
 1900 #endif
 1901 
 1902         /*
 1903          * Stop accepting more transmission packets temporarily, when
 1904          * a filter change request is delayed.  Updating the MARs on
 1905          * 86960 flushes the transmission buffer, so it is delayed
 1906          * until all buffered transmission packets have been sent
 1907          * out.
 1908          */
 1909         if ( sc->filter_change ) {
 1910                 /*
 1911                  * Filter change request is delayed only when the DLC is
 1912                  * working.  DLC soon raise an interrupt after finishing
 1913                  * the work.
 1914                  */
 1915                 goto indicate_active;
 1916         }
 1917 
 1918         for (;;) {
 1919 
 1920                 /*
 1921                  * See if there is room to put another packet in the buffer.
 1922                  * We *could* do better job by peeking the send queue to
 1923                  * know the length of the next packet.  Current version just
 1924                  * tests against the worst case (i.e., longest packet).  FIXME.
 1925                  *
 1926                  * When adding the packet-peek feature, don't forget adding a
 1927                  * test on txb_count against QUEUEING_MAX.
 1928                  * There is a little chance the packet count exceeds
 1929                  * the limit.  Assume transmission buffer is 8KB (2x8KB
 1930                  * configuration) and an application sends a bunch of small
 1931                  * (i.e., minimum packet sized) packets rapidly.  An 8KB
 1932                  * buffer can hold 130 blocks of 62 bytes long...
 1933                  */
 1934                 if ( sc->txb_free
 1935                     < ETHER_MAX_LEN - ETHER_CRC_LEN + FE_DATA_LEN_LEN ) {
 1936                         /* No room.  */
 1937                         goto indicate_active;
 1938                 }
 1939 
 1940 #if FE_SINGLE_TRANSMISSION
 1941                 if ( sc->txb_count > 0 ) {
 1942                         /* Just one packet per a transmission buffer.  */
 1943                         goto indicate_active;
 1944                 }
 1945 #endif
 1946 
 1947                 /*
 1948                  * Get the next mbuf chain for a packet to send.
 1949                  */
 1950                 IF_DEQUEUE( &sc->sc_if.if_snd, m );
 1951                 if ( m == NULL ) {
 1952                         /* No more packets to send.  */
 1953                         goto indicate_inactive;
 1954                 }
 1955 
 1956                 /*
 1957                  * Copy the mbuf chain into the transmission buffer.
 1958                  * txb_* variables are updated as necessary.
 1959                  */
 1960                 fe_write_mbufs( sc, m );
 1961 
 1962                 /* Start transmitter if it's idle.  */
 1963                 if ( ( sc->txb_count > 0 ) && ( sc->txb_sched == 0 ) ) {
 1964                         fe_xmit( sc );
 1965                 }
 1966 
 1967                 /*
 1968                  * Tap off here if there is a bpf listener,
 1969                  * and the device is *not* in promiscuous mode.
 1970                  * (86960 receives self-generated packets if 
 1971                  * and only if it is in "receive everything"
 1972                  * mode.)
 1973                  */
 1974 #if NBPFILTER > 0
 1975                 if ( sc->sc_if.if_bpf
 1976                   && !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
 1977                         bpf_mtap( &sc->sc_if, m );
 1978                 }
 1979 #endif
 1980 
 1981                 m_freem( m );
 1982         }
 1983 
 1984   indicate_inactive:
 1985         /*
 1986          * We are using the !OACTIVE flag to indicate to
 1987          * the outside world that we can accept an
 1988          * additional packet rather than that the
 1989          * transmitter is _actually_ active.  Indeed, the
 1990          * transmitter may be active, but if we haven't
 1991          * filled all the buffers with data then we still
 1992          * want to accept more.
 1993          */
 1994         sc->sc_if.if_flags &= ~IFF_OACTIVE;
 1995         return;
 1996 
 1997   indicate_active:
 1998         /*
 1999          * The transmitter is active, and there are no room for
 2000          * more outgoing packets in the transmission buffer.
 2001          */
 2002         sc->sc_if.if_flags |= IFF_OACTIVE;
 2003         return;
 2004 }
 2005 
 2006 /*
 2007  * Drop (skip) a packet from receive buffer in 86960 memory.
 2008  */
 2009 static void
 2010 fe_droppacket ( struct fe_softc * sc, int len )
 2011 {
 2012         int i;
 2013 
 2014         /*
 2015          * 86960 manual says that we have to read 8 bytes from the buffer
 2016          * before skip the packets and that there must be more than 8 bytes
 2017          * remaining in the buffer when issue a skip command.
 2018          * Remember, we have already read 4 bytes before come here.
 2019          */
 2020         if ( len > 12 ) {
 2021                 /* Read 4 more bytes, and skip the rest of the packet.  */
 2022                 ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
 2023                 ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
 2024         outb( sc->ioaddr[ FE_BMPR14 ], FE_B14_SKIP );
 2025         } else {
 2026                 /* We should not come here unless receiving RUNTs.  */
 2027                 for ( i = 0; i < len; i += 2 ) {
 2028                         ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
 2029                 }
 2030         }
 2031 }
 2032 
 2033 /*
 2034  * Empty receiving buffer.
 2035  */
 2036 static void
 2037 fe_emptybuffer ( struct fe_softc * sc )
 2038 {
 2039         int i;
 2040         u_char saved_dlcr5;
 2041 
 2042 #if FE_DEBUG >= 1
 2043         log( LOG_WARNING, "fe%d: emptying receive buffer\n", sc->sc_unit );
 2044 #endif
 2045         /*
 2046          * Stop receiving packets, temporarily.
 2047          */
 2048         saved_dlcr5 = inb( sc->ioaddr[ FE_DLCR5 ] );
 2049         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 );
 2050 
 2051         /*
 2052          * When we come here, the receive buffer management should
 2053          * have been broken.  So, we cannot use skip operation.
 2054          */
 2055         for ( i = 0; i < sc->txb_size; i += 2 ) {
 2056                 if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
 2057                 ( void )inw( sc->ioaddr[ FE_BMPR8 ] );
 2058         }
 2059 
 2060         /*
 2061          * Double check.
 2062          */
 2063         if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) {
 2064                 log( LOG_ERR, "fe%d: could not empty receive buffer\n",
 2065                         sc->sc_unit );
 2066                 /* Hmm.  What should I do if this happens?  FIXME.  */
 2067         }
 2068 
 2069         /*
 2070          * Restart receiving packets.
 2071          */
 2072         outb( sc->ioaddr[ FE_DLCR5 ], saved_dlcr5 );
 2073 }
 2074 
 2075 /*
 2076  * Transmission interrupt handler
 2077  * The control flow of this function looks silly.  FIXME.
 2078  */
 2079 static void
 2080 fe_tint ( struct fe_softc * sc, u_char tstat )
 2081 {
 2082         int left;
 2083         int col;
 2084 
 2085         /*
 2086          * Handle "excessive collision" interrupt.
 2087          */
 2088         if ( tstat & FE_D0_COLL16 ) {
 2089 
 2090                 /*
 2091                  * Find how many packets (including this collided one)
 2092                  * are left unsent in transmission buffer.
 2093                  */
 2094                 left = inb( sc->ioaddr[ FE_BMPR10 ] );
 2095 
 2096 #if FE_DEBUG >= 2
 2097                 log( LOG_WARNING, "fe%d: excessive collision (%d/%d)\n",
 2098                         sc->sc_unit, left, sc->txb_sched );
 2099 #endif
 2100 #if FE_DEBUG >= 3
 2101                 fe_dump( LOG_INFO, sc, NULL );
 2102 #endif
 2103 
 2104                 /*
 2105                  * Clear the collision flag (in 86960) here
 2106                  * to avoid confusing statistics.
 2107                  */
 2108                 outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
 2109 
 2110                 /*
 2111                  * Restart transmitter, skipping the
 2112                  * collided packet.
 2113                  *
 2114                  * We *must* skip the packet to keep network running
 2115                  * properly.  Excessive collision error is an
 2116                  * indication of the network overload.  If we
 2117                  * tried sending the same packet after excessive
 2118                  * collision, the network would be filled with
 2119                  * out-of-time packets.  Packets belonging
 2120                  * to reliable transport (such as TCP) are resent
 2121                  * by some upper layer.
 2122                  */
 2123                 outb( sc->ioaddr[ FE_BMPR11 ],
 2124                         FE_B11_CTRL_SKIP | FE_B11_MODE1 );
 2125 
 2126                 /* Update statistics.  */
 2127                 sc->tx_excolls++;
 2128         }
 2129 
 2130         /*
 2131          * Handle "transmission complete" interrupt.
 2132          */
 2133         if ( tstat & FE_D0_TXDONE ) {
 2134 
 2135                 /*
 2136                  * Add in total number of collisions on last
 2137                  * transmission.  We also clear "collision occurred" flag
 2138                  * here.
 2139                  *
 2140                  * 86960 has a design flaw on collision count on multiple
 2141                  * packet transmission.  When we send two or more packets
 2142                  * with one start command (that's what we do when the
 2143                  * transmission queue is crowded), 86960 informs us number
 2144                  * of collisions occurred on the last packet on the
 2145                  * transmission only.  Number of collisions on previous
 2146                  * packets are lost.  I have told that the fact is clearly
 2147                  * stated in the Fujitsu document.
 2148                  *
 2149                  * I considered not to mind it seriously.  Collision
 2150                  * count is not so important, anyway.  Any comments?  FIXME.
 2151                  */
 2152 
 2153                 if ( inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_D0_COLLID ) {
 2154 
 2155                         /* Clear collision flag.  */
 2156                         outb( sc->ioaddr[ FE_DLCR0 ], FE_D0_COLLID );
 2157 
 2158                         /* Extract collision count from 86960.  */
 2159                         col = inb( sc->ioaddr[ FE_DLCR4 ] );
 2160                         col = ( col & FE_D4_COL ) >> FE_D4_COL_SHIFT;
 2161                         if ( col == 0 ) {
 2162                                 /*
 2163                                  * Status register indicates collisions,
 2164                                  * while the collision count is zero.
 2165                                  * This can happen after multiple packet
 2166                                  * transmission, indicating that one or more
 2167                                  * previous packet(s) had been collided.
 2168                                  *
 2169                                  * Since the accurate number of collisions
 2170                                  * has been lost, we just guess it as 1;
 2171                                  * Am I too optimistic?  FIXME.
 2172                                  */
 2173                                 col = 1;
 2174                         }
 2175                         sc->sc_if.if_collisions += col;
 2176 #if FE_DEBUG >= 3
 2177                         log( LOG_WARNING, "fe%d: %d collision(s) (%d)\n",
 2178                                 sc->sc_unit, col, sc->txb_sched );
 2179 #endif
 2180                 }
 2181 
 2182                 /*
 2183                  * Update transmission statistics.
 2184                  * Be sure to reflect number of excessive collisions.
 2185                  */
 2186                 sc->sc_if.if_opackets += sc->txb_sched - sc->tx_excolls;
 2187                 sc->sc_if.if_oerrors += sc->tx_excolls;
 2188                 sc->sc_if.if_collisions += sc->tx_excolls * 16;
 2189                 sc->txb_sched = 0;
 2190 
 2191                 /*
 2192                  * The transmitter is no more active.
 2193                  * Reset output active flag and watchdog timer.
 2194                  */
 2195                 sc->sc_if.if_flags &= ~IFF_OACTIVE;
 2196                 sc->sc_if.if_timer = 0;
 2197 
 2198                 /*
 2199                  * If more data is ready to transmit in the buffer, start
 2200                  * transmitting them.  Otherwise keep transmitter idle,
 2201                  * even if more data is queued.  This gives receive
 2202                  * process a slight priority.
 2203                  */
 2204                 if ( sc->txb_count > 0 ) fe_xmit( sc );
 2205         }
 2206 }
 2207 
 2208 /*
 2209  * Ethernet interface receiver interrupt.
 2210  */
 2211 static void
 2212 fe_rint ( struct fe_softc * sc, u_char rstat )
 2213 {
 2214         u_short len;
 2215         u_char status;
 2216         int i;
 2217 
 2218         /*
 2219          * Update statistics if this interrupt is caused by an error.
 2220          */
 2221         if ( rstat & ( FE_D1_OVRFLO | FE_D1_CRCERR
 2222                      | FE_D1_ALGERR | FE_D1_SRTPKT ) ) {
 2223 #if FE_DEBUG >= 3
 2224                 log( LOG_WARNING,
 2225                         "fe%d: receive error: %s%s%s%s(%02x)\n",
 2226                         sc->sc_unit,
 2227                         rstat & FE_D1_OVRFLO ? "OVR " : "",
 2228                         rstat & FE_D1_CRCERR ? "CRC " : "",
 2229                         rstat & FE_D1_ALGERR ? "ALG " : "",
 2230                         rstat & FE_D1_SRTPKT ? "LEN " : "",
 2231                         rstat );
 2232 #endif
 2233                 sc->sc_if.if_ierrors++;
 2234         }
 2235 
 2236         /*
 2237          * MB86960 has a flag indicating "receive queue empty."
 2238          * We just loop, checking the flag, to pull out all received
 2239          * packets.
 2240          *
 2241          * We limit the number of iterations to avoid infinite-loop.
 2242          * It can be caused by a very slow CPU (some broken
 2243          * peripheral may insert incredible number of wait cycles)
 2244          * or, worse, by a broken MB86960 chip.
 2245          */
 2246         for ( i = 0; i < FE_MAX_RECV_COUNT; i++ ) {
 2247 
 2248                 /* Stop the iteration if 86960 indicates no packets.  */
 2249                 if ( inb( sc->ioaddr[ FE_DLCR5 ] ) & FE_D5_BUFEMP ) break;
 2250 
 2251                 /*
 2252                  * Extract A receive status byte.
 2253                  * As our 86960 is in 16 bit bus access mode, we have to
 2254                  * use inw() to get the status byte.  The significant
 2255                  * value is returned in lower 8 bits.
 2256                  */
 2257                 status = ( u_char )inw( sc->ioaddr[ FE_BMPR8 ] );
 2258 #if FE_DEBUG >= 4
 2259                 log( LOG_INFO, "fe%d: receive status = %04x\n",
 2260                         sc->sc_unit, status );
 2261 #endif
 2262 
 2263                 /*
 2264                  * Extract the packet length.
 2265                  * It is a sum of a header (14 bytes) and a payload.
 2266                  * CRC has been stripped off by the 86960.
 2267                  */
 2268                 len = inw( sc->ioaddr[ FE_BMPR8 ] );
 2269 
 2270                 /*
 2271                  * If there was an error, update statistics and drop
 2272                  * the packet, unless the interface is in promiscuous
 2273                  * mode.
 2274                  */
 2275                 if ( ( status & 0xF0 ) != 0x20 ) {
 2276                         if ( !( sc->sc_if.if_flags & IFF_PROMISC ) ) {
 2277                                 sc->sc_if.if_ierrors++;
 2278                                 fe_droppacket( sc, len );
 2279                                 continue;
 2280                         }
 2281                 }
 2282 
 2283                 /*
 2284                  * MB86960 checks the packet length and drop big packet
 2285                  * before passing it to us.  There are no chance we can
 2286                  * get big packets through it, even if they are actually
 2287                  * sent over a line.  Hence, if the length exceeds
 2288                  * the specified limit, it means some serious failure,
 2289                  * such as out-of-sync on receive buffer management.
 2290                  *
 2291                  * Same for short packets, since we have programmed
 2292                  * 86960 to drop short packets.
 2293                  */
 2294                 if ( len > ETHER_MAX_LEN - ETHER_CRC_LEN
 2295                   || len < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
 2296 #if FE_DEBUG >= 1
 2297                         log( LOG_WARNING,
 2298                                 "fe%d: received a %s packet? (%u bytes)\n",
 2299                                 sc->sc_unit,
 2300                                 len < ETHER_MIN_LEN - ETHER_CRC_LEN
 2301                                         ? "partial" : "big",
 2302                                 len );
 2303 #endif
 2304                         sc->sc_if.if_ierrors++;
 2305                         fe_emptybuffer( sc );
 2306                         continue;
 2307                 }
 2308 
 2309                 /*
 2310                  * Go get a packet.
 2311                  */
 2312                 if ( fe_get_packet( sc, len ) < 0 ) {
 2313                         /* Skip a packet, updating statistics.  */
 2314 #if FE_DEBUG >= 2
 2315                         log( LOG_WARNING, "%s%d: out of mbuf;"
 2316                             " dropping a packet (%u bytes)\n",
 2317                             sc->sc_unit, len );
 2318 #endif
 2319                         sc->sc_if.if_ierrors++;
 2320                         fe_droppacket( sc, len );
 2321 
 2322                         /*
 2323                          * We stop receiving packets, even if there are
 2324                          * more in the buffer.  We hope we can get more
 2325                          * mbuf next time.
 2326                          */
 2327                         return;
 2328                 }
 2329 
 2330                 /* Successfully received a packet.  Update stat.  */
 2331                 sc->sc_if.if_ipackets++;
 2332         }
 2333 }
 2334 
 2335 /*
 2336  * Ethernet interface interrupt processor
 2337  */
 2338 void
 2339 feintr ( int unit )
 2340 {
 2341         struct fe_softc *sc = &fe_softc[unit];
 2342         u_char tstat, rstat;
 2343 
 2344         /*
 2345          * Loop until there are no more new interrupt conditions.
 2346          */
 2347         for (;;) {
 2348 
 2349 #if FE_DEBUG >= 4
 2350                 fe_dump( LOG_INFO, sc, "intr()" );
 2351 #endif
 2352 
 2353                 /*
 2354                  * Get interrupt conditions, masking unneeded flags.
 2355                  */
 2356                 tstat = inb( sc->ioaddr[ FE_DLCR0 ] ) & FE_TMASK;
 2357                 rstat = inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_RMASK;
 2358                 if ( tstat == 0 && rstat == 0 ) break;
 2359 
 2360                 /*
 2361                  * Reset the conditions we are acknowledging.
 2362                  */
 2363                 outb( sc->ioaddr[ FE_DLCR0 ], tstat );
 2364                 outb( sc->ioaddr[ FE_DLCR1 ], rstat );
 2365 
 2366                 /*
 2367                  * Handle transmitter interrupts. Handle these first because
 2368                  * the receiver will reset the board under some conditions.
 2369                  */
 2370                 if ( tstat ) {
 2371                         fe_tint( sc, tstat );
 2372                 }
 2373 
 2374                 /*
 2375                  * Handle receiver interrupts
 2376                  */
 2377                 if ( rstat ) {
 2378                         fe_rint( sc, rstat );
 2379                 }
 2380 
 2381                 /*
 2382                  * Update the multicast address filter if it is
 2383                  * needed and possible.  We do it now, because
 2384                  * we can make sure the transmission buffer is empty,
 2385                  * and there is a good chance that the receive queue
 2386                  * is empty.  It will minimize the possibility of
 2387                  * packet loss.
 2388                  */
 2389                 if ( sc->filter_change
 2390                   && sc->txb_count == 0 && sc->txb_sched == 0 ) {
 2391                         fe_loadmar(sc);
 2392                         sc->sc_if.if_flags &= ~IFF_OACTIVE;
 2393                 }
 2394 
 2395                 /*
 2396                  * If it looks like the transmitter can take more data,
 2397                  * attempt to start output on the interface. This is done
 2398                  * after handling the receiver interrupt to give the
 2399                  * receive operation priority.
 2400                  *
 2401                  * BTW, I'm not sure in what case the OACTIVE is on at
 2402                  * this point.  Is the following test redundant?
 2403                  *
 2404                  * No.  This routine polls for both transmitter and
 2405                  * receiver interrupts.  86960 can raise a receiver
 2406                  * interrupt when the transmission buffer is full.
 2407                  */
 2408                 if ( ( sc->sc_if.if_flags & IFF_OACTIVE ) == 0 ) {
 2409                         fe_start( &sc->sc_if );
 2410                 }
 2411 
 2412         }
 2413 }
 2414 
 2415 /*
 2416  * Process an ioctl request. This code needs some work - it looks
 2417  * pretty ugly.
 2418  */
 2419 static int
 2420 fe_ioctl ( struct ifnet * ifp, int command, caddr_t data )
 2421 {
 2422         struct fe_softc *sc = ifp->if_softc;
 2423         int s, error = 0;
 2424 
 2425 #if FE_DEBUG >= 3
 2426         log( LOG_INFO, "fe%d: ioctl(%x)\n", sc->sc_unit, command );
 2427 #endif
 2428 
 2429         s = splimp();
 2430 
 2431         switch (command) {
 2432 
 2433           case SIOCSIFADDR:
 2434             {
 2435                 struct ifaddr * ifa = ( struct ifaddr * )data;
 2436 
 2437                 sc->sc_if.if_flags |= IFF_UP;
 2438 
 2439                 switch (ifa->ifa_addr->sa_family) {
 2440 #ifdef INET
 2441                   case AF_INET:
 2442                         fe_init( sc->sc_unit ); /* before arp_ifinit */
 2443                         arp_ifinit( &sc->arpcom, ifa );
 2444                         break;
 2445 #endif
 2446 #ifdef IPX
 2447                         /*
 2448                          * XXX - This code is probably wrong
 2449                          */
 2450                   case AF_IPX:
 2451                         {
 2452                                 register struct ipx_addr *ina
 2453                                     = &(IA_SIPX(ifa)->sipx_addr);
 2454 
 2455                                 if (ipx_nullhost(*ina))
 2456                                         ina->x_host =
 2457                                             *(union ipx_host *) (sc->sc_enaddr);                                else {
 2458                                         bcopy((caddr_t) ina->x_host.c_host,
 2459                                               (caddr_t) sc->sc_enaddr,
 2460                                               sizeof(sc->sc_enaddr));
 2461                                 }
 2462 
 2463                                 /*
 2464                                  * Set new address
 2465                                  */
 2466                                 fe_init(sc->sc_unit);
 2467                                 break;
 2468                         }
 2469 #endif
 2470 #ifdef INET6
 2471                   case AF_INET6:
 2472                         /* IPV6 added by shin 96.2.6 */
 2473                         fe_init(sc->sc_unit);
 2474                         ndp6_ifinit(&sc->arpcom, ifa);
 2475                         break;
 2476 #endif
 2477 #ifdef NS
 2478 
 2479                         /*
 2480                          * XXX - This code is probably wrong
 2481                          */
 2482                   case AF_NS:
 2483                         {
 2484                                 register struct ns_addr *ina
 2485                                     = &(IA_SNS(ifa)->sns_addr);
 2486 
 2487                                 if (ns_nullhost(*ina))
 2488                                         ina->x_host =
 2489                                             *(union ns_host *) (sc->sc_enaddr);
 2490                                 else {
 2491                                         bcopy((caddr_t) ina->x_host.c_host,
 2492                                               (caddr_t) sc->sc_enaddr,
 2493                                               sizeof(sc->sc_enaddr));
 2494                                 }
 2495 
 2496                                 /*
 2497                                  * Set new address
 2498                                  */
 2499                                 fe_init(sc->sc_unit);
 2500                                 break;
 2501                         }
 2502 #endif
 2503                   default:
 2504                         fe_init( sc->sc_unit );
 2505                         break;
 2506                 }
 2507                 break;
 2508             }
 2509 
 2510 #ifdef SIOCGIFADDR
 2511           case SIOCGIFADDR:
 2512             {
 2513                 struct ifreq * ifr = ( struct ifreq * )data;
 2514                 struct sockaddr * sa = ( struct sockaddr * )&ifr->ifr_data;
 2515 
 2516                 bcopy((caddr_t)sc->sc_enaddr,
 2517                       (caddr_t)sa->sa_data, ETHER_ADDR_LEN);
 2518                 break;
 2519             }
 2520 #endif
 2521 
 2522 #ifdef SIOCGIFPHYSADDR
 2523           case SIOCGIFPHYSADDR:
 2524             {
 2525                 struct ifreq * ifr = ( struct ifreq * )data;
 2526 
 2527                 bcopy((caddr_t)sc->sc_enaddr,
 2528                       (caddr_t)&ifr->ifr_data, ETHER_ADDR_LEN);
 2529                 break;
 2530             }
 2531 #endif
 2532 
 2533 #ifdef notdef
 2534 #ifdef SIOCSIFPHYSADDR
 2535           case SIOCSIFPHYSADDR:
 2536             {
 2537                 /*
 2538                  * Set the physical (Ethernet) address of the interface.
 2539                  * When and by whom is this command used?  FIXME.
 2540                  */
 2541                 struct ifreq * ifr = ( struct ifreq * )data;
 2542 
 2543                 bcopy((caddr_t)&ifr->ifr_data,
 2544                       (caddr_t)sc->sc_enaddr, ETHER_ADDR_LEN);
 2545                 fe_setlinkaddr( sc );
 2546                 break;
 2547             }
 2548 #endif
 2549 #endif /* notdef */
 2550 
 2551 #ifdef SIOCSIFFLAGS
 2552           case SIOCSIFFLAGS:
 2553             {
 2554                 /*
 2555                  * Switch interface state between "running" and
 2556                  * "stopped", reflecting the UP flag.
 2557                  */
 2558                 if ( sc->sc_if.if_flags & IFF_UP ) {
 2559                         if ( ( sc->sc_if.if_flags & IFF_RUNNING ) == 0 ) {
 2560                                 fe_init( sc->sc_unit );
 2561                         }
 2562                 } else {
 2563                         if ( ( sc->sc_if.if_flags & IFF_RUNNING ) != 0 ) {
 2564                                 fe_stop( sc->sc_unit );
 2565                         }
 2566                 }
 2567 
 2568                 /*
 2569                  * Promiscuous and/or multicast flags may have changed,
 2570                  * so reprogram the multicast filter and/or receive mode.
 2571                  */
 2572                 fe_setmode( sc );
 2573 
 2574 #if FE_DEBUG >= 1
 2575                 /* "ifconfig fe0 debug" to print register dump.  */
 2576                 if ( sc->sc_if.if_flags & IFF_DEBUG ) {
 2577                         fe_dump( LOG_DEBUG, sc, "SIOCSIFFLAGS(DEBUG)" );
 2578                 }
 2579 #endif
 2580                 break;
 2581             }
 2582 #endif
 2583 
 2584 #ifdef SIOCADDMULTI
 2585           case SIOCADDMULTI:
 2586           case SIOCDELMULTI:
 2587             {
 2588                 /*
 2589                  * Update out multicast list.
 2590                  */
 2591                 struct ifreq * ifr = ( struct ifreq * )data;
 2592 
 2593                 error = ( command == SIOCADDMULTI )
 2594                       ? ether_addmulti( ifr, &sc->arpcom )
 2595                       : ether_delmulti( ifr, &sc->arpcom );
 2596 
 2597                 if ( error == ENETRESET ) {
 2598                         /*
 2599                          * Multicast list has changed; set the hardware filter
 2600                          * accordingly.
 2601                          */
 2602                         fe_setmode( sc );
 2603                         error = 0;
 2604                 }
 2605 
 2606                 break;
 2607             }
 2608 #endif
 2609 
 2610 #ifdef SIOCSIFMTU
 2611           case SIOCSIFMTU:
 2612             {
 2613                 /*
 2614                  * Set the interface MTU.
 2615                  */
 2616                 struct ifreq * ifr = ( struct ifreq * )data;
 2617 
 2618                 if ( ifr->ifr_mtu > ETHERMTU ) {
 2619                         error = EINVAL;
 2620                 } else {
 2621                         sc->sc_if.if_mtu = ifr->ifr_mtu;
 2622                 }
 2623                 break;
 2624             }
 2625 #endif
 2626 
 2627           default:
 2628                 error = EINVAL;
 2629         }
 2630 
 2631         (void) splx(s);
 2632         return (error);
 2633 }
 2634 
 2635 /*
 2636  * Retrieve packet from receive buffer and send to the next level up via
 2637  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
 2638  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
 2639  */
 2640 static int
 2641 fe_get_packet ( struct fe_softc * sc, u_short len )
 2642 {
 2643         struct ether_header *eh;
 2644         struct mbuf *m;
 2645 
 2646         /*
 2647          * NFS wants the data be aligned to the word (4 byte)
 2648          * boundary.  Ethernet header has 14 bytes.  There is a
 2649          * 2-byte gap.
 2650          */
 2651 #define NFS_MAGIC_OFFSET 2
 2652 
 2653         /*
 2654          * This function assumes that an Ethernet packet fits in an
 2655          * mbuf (with a cluster attached when necessary.)  On FreeBSD
 2656          * 2.0 for x86, which is the primary target of this driver, an
 2657          * mbuf cluster has 4096 bytes, and we are happy.  On ancient
 2658          * BSDs, such as vanilla 4.3 for 386, a cluster size was 1024,
 2659          * however.  If the following #error message were printed upon
 2660          * compile, you need to rewrite this function.
 2661          */
 2662 #if ( MCLBYTES < ETHER_MAX_LEN - ETHER_CRC_LEN + NFS_MAGIC_OFFSET )
 2663 #error "Too small MCLBYTES to use fe driver."
 2664 #endif
 2665 
 2666         /*
 2667          * Our strategy has one more problem.  There is a policy on
 2668          * mbuf cluster allocation.  It says that we must have at
 2669          * least MINCLSIZE (208 bytes on FreeBSD 2.0 for x86) to
 2670          * allocate a cluster.  For a packet of a size between
 2671          * (MHLEN - 2) to (MINCLSIZE - 2), our code violates the rule...
 2672          * On the other hand, the current code is short, simple,
 2673          * and fast, however.  It does no harmful thing, just waists
 2674          * some memory.  Any comments?  FIXME.
 2675          */
 2676 
 2677         /* Allocate an mbuf with packet header info.  */
 2678         MGETHDR(m, M_DONTWAIT, MT_DATA);
 2679         if ( m == NULL ) return -1;
 2680 
 2681         /* Attach a cluster if this packet doesn't fit in a normal mbuf.  */
 2682         if ( len > MHLEN - NFS_MAGIC_OFFSET ) {
 2683                 MCLGET( m, M_DONTWAIT );
 2684                 if ( !( m->m_flags & M_EXT ) ) {
 2685                         m_freem( m );
 2686                         return -1;
 2687                 }
 2688         }
 2689 
 2690         /* Initialize packet header info.  */
 2691         m->m_pkthdr.rcvif = &sc->sc_if;
 2692         m->m_pkthdr.len = len;
 2693 
 2694         /* Set the length of this packet.  */
 2695         m->m_len = len;
 2696 
 2697         /* The following silliness is to make NFS happy */
 2698         m->m_data += NFS_MAGIC_OFFSET;
 2699 
 2700         /* Get a packet.  */
 2701         insw( sc->ioaddr[ FE_BMPR8 ], m->m_data, ( len + 1 ) >> 1 );
 2702 
 2703         /* Get (actually just point to) the header part.  */
 2704         eh = mtod( m, struct ether_header *);
 2705 
 2706 #define ETHER_ADDR_IS_MULTICAST(A) (*(char *)(A) & 1)
 2707 
 2708 #if NBPFILTER > 0
 2709         /*
 2710          * Check if there's a BPF listener on this interface.
 2711          * If it is, hand off the raw packet to bpf.
 2712          */
 2713         if ( sc->sc_if.if_bpf ) {
 2714                 bpf_mtap( &sc->sc_if, m );
 2715         }
 2716 #endif
 2717 
 2718         /*
 2719          * Make sure this packet is (or may be) directed to us.
 2720          * That is, the packet is either unicasted to our address,
 2721          * or broad/multi-casted.  If any other packets are
 2722          * received, it is an indication of an error -- probably
 2723          * 86960 is in a wrong operation mode.
 2724          * Promiscuous mode is an exception.  Under the mode, all
 2725          * packets on the media must be received.  (We must have
 2726          * programmed the 86960 so.)
 2727          */
 2728 
 2729         if ( ( sc->sc_if.if_flags & IFF_PROMISC )
 2730           && !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
 2731           && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
 2732                 /*
 2733                  * The packet was not for us.  This is normal since
 2734                  * we are now in promiscuous mode.  Just drop the packet.
 2735                  */
 2736                 m_freem( m );
 2737                 return 0;
 2738         }
 2739 
 2740 #if FE_DEBUG >= 3
 2741         if ( !ETHER_ADDR_IS_MULTICAST( eh->ether_dhost )
 2742           && bcmp( eh->ether_dhost, sc->sc_enaddr, ETHER_ADDR_LEN ) != 0 ) {
 2743                 /*
 2744                  * This packet was not for us.  We can't be in promiscuous
 2745                  * mode since the case was handled by above test.
 2746                  * We found an error (of this driver.)
 2747                  */
 2748                 log( LOG_WARNING,
 2749                         "fe%d: got an unwanted packet, dst = %6D\n",
 2750                         sc->sc_unit, eh->ether_dhost , ":" );
 2751                 m_freem( m );
 2752                 return 0;
 2753         }
 2754 #endif
 2755 
 2756         /* Strip off the Ethernet header.  */
 2757         m->m_pkthdr.len -= sizeof ( struct ether_header );
 2758         m->m_len -= sizeof ( struct ether_header );
 2759         m->m_data += sizeof ( struct ether_header );
 2760 
 2761         /* Feed the packet to upper layer.  */
 2762         ether_input( &sc->sc_if, eh, m );
 2763         return 0;
 2764 }
 2765 
 2766 /*
 2767  * Write an mbuf chain to the transmission buffer memory using 16 bit PIO.
 2768  * Returns number of bytes actually written, including length word.
 2769  *
 2770  * If an mbuf chain is too long for an Ethernet frame, it is not sent.
 2771  * Packets shorter than Ethernet minimum are legal, and we pad them
 2772  * before sending out.  An exception is "partial" packets which are
 2773  * shorter than mandatory Ethernet header.
 2774  */
 2775 static void
 2776 fe_write_mbufs ( struct fe_softc *sc, struct mbuf *m )
 2777 {
 2778         u_short addr_bmpr8 = sc->ioaddr[ FE_BMPR8 ];
 2779         u_short length, len;
 2780         struct mbuf *mp;
 2781         u_char *data;
 2782         u_short savebyte;       /* WARNING: Architecture dependent!  */
 2783 #define NO_PENDING_BYTE 0xFFFF
 2784 
 2785         static u_char padding [ ETHER_MIN_LEN - ETHER_CRC_LEN - ETHER_HDR_LEN ];
 2786 
 2787 #if FE_DEBUG >= 2
 2788         /* First, count up the total number of bytes to copy */
 2789         length = 0;
 2790         for ( mp = m; mp != NULL; mp = mp->m_next ) {
 2791                 length += mp->m_len;
 2792         }
 2793         /* Check if this matches the one in the packet header.  */
 2794         if ( length != m->m_pkthdr.len ) {
 2795                 log( LOG_WARNING, "fe%d: packet length mismatch? (%d/%d)\n",
 2796                         sc->sc_unit, length, m->m_pkthdr.len );
 2797         }
 2798 #else
 2799         /* Just use the length value in the packet header.  */
 2800         length = m->m_pkthdr.len;
 2801 #endif
 2802 
 2803 #if FE_DEBUG >= 1
 2804         /*
 2805          * Should never send big packets.  If such a packet is passed,
 2806          * it should be a bug of upper layer.  We just ignore it.
 2807          * ... Partial (too short) packets, neither.
 2808          */
 2809         if ( length < ETHER_HDR_LEN
 2810           || length > ETHER_MAX_LEN - ETHER_CRC_LEN ) {
 2811                 log( LOG_ERR,
 2812                         "fe%d: got an out-of-spec packet (%u bytes) to send\n",
 2813                         sc->sc_unit, length );
 2814                 sc->sc_if.if_oerrors++;
 2815                 return;
 2816         }
 2817 #endif
 2818 
 2819         /*
 2820          * Put the length word for this frame.
 2821          * Does 86960 accept odd length?  -- Yes.
 2822          * Do we need to pad the length to minimum size by ourselves?
 2823          * -- Generally yes.  But for (or will be) the last
 2824          * packet in the transmission buffer, we can skip the
 2825          * padding process.  It may gain performance slightly.  FIXME.
 2826          */
 2827         outw( addr_bmpr8, max( length, ETHER_MIN_LEN - ETHER_CRC_LEN ) );
 2828 
 2829         /*
 2830          * Update buffer status now.
 2831          * Truncate the length up to an even number, since we use outw().
 2832          */
 2833         length = ( length + 1 ) & ~1;
 2834         sc->txb_free -= FE_DATA_LEN_LEN + max( length, ETHER_MIN_LEN - ETHER_CRC_LEN);
 2835         sc->txb_count++;
 2836 
 2837         /*
 2838          * Transfer the data from mbuf chain to the transmission buffer.
 2839          * MB86960 seems to require that data be transferred as words, and
 2840          * only words.  So that we require some extra code to patch
 2841          * over odd-length mbufs.
 2842          */
 2843         savebyte = NO_PENDING_BYTE;
 2844         for ( mp = m; mp != 0; mp = mp->m_next ) {
 2845 
 2846                 /* Ignore empty mbuf.  */
 2847                 len = mp->m_len;
 2848                 if ( len == 0 ) continue;
 2849 
 2850                 /* Find the actual data to send.  */
 2851                 data = mtod(mp, caddr_t);
 2852 
 2853                 /* Finish the last byte.  */
 2854                 if ( savebyte != NO_PENDING_BYTE ) {
 2855                         outw( addr_bmpr8, savebyte | ( *data << 8 ) );
 2856                         data++;
 2857                         len--;
 2858                         savebyte = NO_PENDING_BYTE;
 2859                 }
 2860 
 2861                 /* output contiguous words */
 2862                 if (len > 1) {
 2863                         outsw( addr_bmpr8, data, len >> 1);
 2864                         data += len & ~1;
 2865                         len &= 1;
 2866                 }
 2867 
 2868                 /* Save a remaining byte, if there is one.  */
 2869                 if ( len > 0 ) {
 2870                         savebyte = *data;
 2871                 }
 2872         }
 2873 
 2874         /* Spit the last byte, if the length is odd.  */
 2875         if ( savebyte != NO_PENDING_BYTE ) {
 2876                 outw( addr_bmpr8, savebyte );
 2877         }
 2878 
 2879         /* Pad to the Ethernet minimum length, if the packet is too short.  */
 2880         if ( length < ETHER_MIN_LEN - ETHER_CRC_LEN ) {
 2881                 outsw( addr_bmpr8, padding, ( ETHER_MIN_LEN - ETHER_CRC_LEN - length ) >> 1);
 2882         }
 2883 }
 2884 
 2885 /*
 2886  * Compute hash value for an Ethernet address
 2887  */
 2888 static int
 2889 fe_hash ( u_char * ep )
 2890 {
 2891 #define FE_HASH_MAGIC_NUMBER 0xEDB88320L
 2892 
 2893         u_long hash = 0xFFFFFFFFL;
 2894         int i, j;
 2895         u_char b;
 2896         u_long m;
 2897 
 2898         for ( i = ETHER_ADDR_LEN; --i >= 0; ) {
 2899                 b = *ep++;
 2900                 for ( j = 8; --j >= 0; ) {
 2901                         m = hash;
 2902                         hash >>= 1;
 2903                         if ( ( m ^ b ) & 1 ) hash ^= FE_HASH_MAGIC_NUMBER;
 2904                         b >>= 1;
 2905                 }
 2906         }
 2907         return ( ( int )( hash >> 26 ) );
 2908 }
 2909 
 2910 /*
 2911  * Compute the multicast address filter from the
 2912  * list of multicast addresses we need to listen to.
 2913  */
 2914 static struct fe_filter
 2915 fe_mcaf ( struct fe_softc *sc )
 2916 {
 2917         int index;
 2918         struct fe_filter filter;
 2919         struct ether_multi *enm;
 2920         struct ether_multistep step;
 2921 
 2922         filter = fe_filter_nothing;
 2923         ETHER_FIRST_MULTI(step, &sc->arpcom, enm);
 2924         while ( enm != NULL) {
 2925                 if ( bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) {
 2926                         return ( fe_filter_all );
 2927                 }
 2928                 index = fe_hash( enm->enm_addrlo );
 2929 #if FE_DEBUG >= 4
 2930                 log( LOG_INFO, "fe%d: hash(%6D) == %d\n",
 2931                         sc->sc_unit, enm->enm_addrlo , ":", index );
 2932 #endif
 2933 
 2934                 filter.data[index >> 3] |= 1 << (index & 7);
 2935                 ETHER_NEXT_MULTI(step, enm);
 2936         }
 2937         return ( filter );
 2938 }
 2939 
 2940 /*
 2941  * Calculate a new "multicast packet filter" and put the 86960
 2942  * receiver in appropriate mode.
 2943  */
 2944 static void
 2945 fe_setmode ( struct fe_softc *sc )
 2946 {
 2947         int flags = sc->sc_if.if_flags;
 2948 
 2949         /*
 2950          * If the interface is not running, we postpone the update
 2951          * process for receive modes and multicast address filter
 2952          * until the interface is restarted.  It reduces some
 2953          * complicated job on maintaining chip states.  (Earlier versions
 2954          * of this driver had a bug on that point...)
 2955          *
 2956          * To complete the trick, fe_init() calls fe_setmode() after
 2957          * restarting the interface.
 2958          */
 2959         if ( !( flags & IFF_RUNNING ) ) return;
 2960 
 2961         /*
 2962          * Promiscuous mode is handled separately.
 2963          */
 2964         if ( flags & IFF_PROMISC ) {
 2965                 /*
 2966                  * Program 86960 to receive all packets on the segment
 2967                  * including those directed to other stations.
 2968                  * Multicast filter stored in MARs are ignored
 2969                  * under this setting, so we don't need to update it.
 2970                  *
 2971                  * Promiscuous mode in FreeBSD 2 is used solely by
 2972                  * BPF, and BPF only listens to valid (no error) packets.
 2973                  * So, we ignore erroneous ones even in this mode.
 2974                  * (Older versions of fe driver mistook the point.)
 2975                  */
 2976                 outb( sc->ioaddr[ FE_DLCR5 ],
 2977                         sc->proto_dlcr5 | FE_D5_AFM0 | FE_D5_AFM1 );
 2978                 sc->filter_change = 0;
 2979 
 2980 #if FE_DEBUG >= 3
 2981                 log( LOG_INFO, "fe%d: promiscuous mode\n", sc->sc_unit );
 2982 #endif
 2983                 return;
 2984         }
 2985 
 2986         /*
 2987          * Turn the chip to the normal (non-promiscuous) mode.
 2988          */
 2989         outb( sc->ioaddr[ FE_DLCR5 ], sc->proto_dlcr5 | FE_D5_AFM1 );
 2990 
 2991         /*
 2992          * Find the new multicast filter value.
 2993          * I'm not sure we have to handle modes other than MULTICAST.
 2994          * Who sets ALLMULTI?  Who turns MULTICAST off?  FIXME.
 2995          */
 2996         if ( flags & IFF_ALLMULTI ) {
 2997                 sc->filter = fe_filter_all;
 2998         } else if ( flags & IFF_MULTICAST ) {
 2999                 sc->filter = fe_mcaf( sc );
 3000         } else {
 3001                 sc->filter = fe_filter_nothing;
 3002         }
 3003         sc->filter_change = 1;
 3004 
 3005 #if FE_DEBUG >= 3
 3006         log( LOG_INFO, "fe%d: address filter: [%8D]\n",
 3007             sc->sc_unit, sc->filter.data, " " );
 3008 #endif
 3009 
 3010         /*
 3011          * We have to update the multicast filter in the 86960, A.S.A.P.
 3012          *
 3013          * Note that the DLC (Data Link Control unit, i.e. transmitter
 3014          * and receiver) must be stopped when feeding the filter, and
 3015          * DLC trashes all packets in both transmission and receive
 3016          * buffers when stopped.
 3017          *
 3018          * ... Are the above sentences correct?  I have to check the
 3019          *     manual of the MB86960A.  FIXME.
 3020          *
 3021          * To reduce the packet loss, we delay the filter update
 3022          * process until buffers are empty.
 3023          */
 3024         if ( sc->txb_sched == 0 && sc->txb_count == 0
 3025           && !( inb( sc->ioaddr[ FE_DLCR1 ] ) & FE_D1_PKTRDY ) ) {
 3026                 /*
 3027                  * Buffers are (apparently) empty.  Load
 3028                  * the new filter value into MARs now.
 3029                  */
 3030                 fe_loadmar(sc);
 3031         } else {
 3032                 /*
 3033                  * Buffers are not empty.  Mark that we have to update
 3034                  * the MARs.  The new filter will be loaded by feintr()
 3035                  * later.
 3036                  */
 3037 #if FE_DEBUG >= 4
 3038                 log( LOG_INFO, "fe%d: filter change delayed\n", sc->sc_unit );
 3039 #endif
 3040         }
 3041 }
 3042 
 3043 /*
 3044  * Load a new multicast address filter into MARs.
 3045  *
 3046  * The caller must have splimp'ed before fe_loadmar.
 3047  * This function starts the DLC upon return.  So it can be called only
 3048  * when the chip is working, i.e., from the driver's point of view, when
 3049  * a device is RUNNING.  (I mistook the point in previous versions.)
 3050  */
 3051 static void
 3052 fe_loadmar ( struct fe_softc * sc )
 3053 {
 3054         /* Stop the DLC (transmitter and receiver).  */
 3055         DELAY( 200 );
 3056         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_DISABLE );
 3057         DELAY( 200 );
 3058 
 3059         /* Select register bank 1 for MARs.  */
 3060         outb( sc->ioaddr[ FE_DLCR7 ],
 3061                 sc->proto_dlcr7 | FE_D7_RBS_MAR | FE_D7_POWER_UP );
 3062 
 3063         /* Copy filter value into the registers.  */
 3064         outblk( sc, FE_MAR8, sc->filter.data, FE_FILTER_LEN );
 3065 
 3066         /* Restore the bank selection for BMPRs (i.e., runtime registers).  */
 3067         outb( sc->ioaddr[ FE_DLCR7 ],
 3068                 sc->proto_dlcr7 | FE_D7_RBS_BMPR | FE_D7_POWER_UP );
 3069 
 3070         /* Restart the DLC.  */
 3071         DELAY( 200 );
 3072         outb( sc->ioaddr[ FE_DLCR6 ], sc->proto_dlcr6 | FE_D6_DLC_ENABLE );
 3073         DELAY( 200 );
 3074 
 3075         /* We have just updated the filter.  */
 3076         sc->filter_change = 0;
 3077 
 3078 #if FE_DEBUG >= 3
 3079         log( LOG_INFO, "fe%d: address filter changed\n", sc->sc_unit );
 3080 #endif
 3081 }
 3082 
 3083 #if FE_DEBUG >= 1
 3084 static void
 3085 fe_dump ( int level, struct fe_softc * sc, char * message )
 3086 {
 3087         log( level, "fe%d: %s,"
 3088             " DLCR = %02x %02x %02x %02x %02x %02x %02x %02x,"
 3089             " BMPR = xx xx %02x %02x %02x %02x %02x %02x,"
 3090             " asic = %02x %02x %02x %02x %02x %02x %02x %02x"
 3091             " + %02x %02x %02x %02x %02x %02x %02x %02x\n",
 3092             sc->sc_unit, message ? message : "registers",
 3093             inb( sc->ioaddr[ FE_DLCR0 ] ),  inb( sc->ioaddr[ FE_DLCR1 ] ),
 3094             inb( sc->ioaddr[ FE_DLCR2 ] ),  inb( sc->ioaddr[ FE_DLCR3 ] ),
 3095             inb( sc->ioaddr[ FE_DLCR4 ] ),  inb( sc->ioaddr[ FE_DLCR5 ] ),
 3096             inb( sc->ioaddr[ FE_DLCR6 ] ),  inb( sc->ioaddr[ FE_DLCR7 ] ),
 3097             inb( sc->ioaddr[ FE_BMPR10 ] ), inb( sc->ioaddr[ FE_BMPR11 ] ),
 3098             inb( sc->ioaddr[ FE_BMPR12 ] ), inb( sc->ioaddr[ FE_BMPR13 ] ),
 3099             inb( sc->ioaddr[ FE_BMPR14 ] ), inb( sc->ioaddr[ FE_BMPR15 ] ),
 3100             inb( sc->ioaddr[ 0x10 ] ),      inb( sc->ioaddr[ 0x11 ] ),
 3101             inb( sc->ioaddr[ 0x12 ] ),      inb( sc->ioaddr[ 0x13 ] ),
 3102             inb( sc->ioaddr[ 0x14 ] ),      inb( sc->ioaddr[ 0x15 ] ),
 3103             inb( sc->ioaddr[ 0x16 ] ),      inb( sc->ioaddr[ 0x17 ] ),
 3104             inb( sc->ioaddr[ 0x18 ] ),      inb( sc->ioaddr[ 0x19 ] ),
 3105             inb( sc->ioaddr[ 0x1A ] ),      inb( sc->ioaddr[ 0x1B ] ),
 3106             inb( sc->ioaddr[ 0x1C ] ),      inb( sc->ioaddr[ 0x1D ] ),
 3107             inb( sc->ioaddr[ 0x1E ] ),      inb( sc->ioaddr[ 0x1F ] ) );
 3108 }
 3109 #endif

Cache object: fb2165ebeacfe5a28cbc550c03276efe


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.