Home | History | Annotate | Line # | Download | only in ic
mb86950.c revision 1.14
      1  1.14       dsl /*	$NetBSD: mb86950.c,v 1.14 2009/03/14 15:36:17 dsl Exp $	*/
      2   1.1  jdolecek 
      3   1.1  jdolecek /*
      4   1.1  jdolecek  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
      5   1.1  jdolecek  *
      6   1.1  jdolecek  * This software may be used, modified, copied, distributed, and sold, in
      7   1.1  jdolecek  * both source and binary form provided that the above copyright, these
      8   1.1  jdolecek  * terms and the following disclaimer are retained.  The name of the author
      9   1.1  jdolecek  * and/or the contributor may not be used to endorse or promote products
     10   1.1  jdolecek  * derived from this software without specific prior written permission.
     11   1.1  jdolecek  *
     12   1.1  jdolecek  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
     13   1.1  jdolecek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     14   1.1  jdolecek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     15   1.1  jdolecek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
     16   1.1  jdolecek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     17   1.1  jdolecek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     18   1.1  jdolecek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
     19   1.1  jdolecek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     20   1.1  jdolecek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     21   1.1  jdolecek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     22   1.1  jdolecek  * SUCH DAMAGE.
     23   1.1  jdolecek  */
     24   1.1  jdolecek 
     25   1.1  jdolecek /*
     26   1.1  jdolecek  * Portions copyright (C) 1993, David Greenman.  This software may be used,
     27   1.1  jdolecek  * modified, copied, distributed, and sold, in both source and binary form
     28   1.1  jdolecek  * provided that the above copyright and these terms are retained.  Under no
     29   1.1  jdolecek  * circumstances is the author responsible for the proper functioning of this
     30   1.1  jdolecek  * software, nor does the author assume any responsibility for damages
     31   1.1  jdolecek  * incurred with its use.
     32   1.1  jdolecek  */
     33   1.1  jdolecek 
     34   1.1  jdolecek  /*
     35   1.1  jdolecek   * Portions copyright (c) 1995 Mika Kortelainen
     36   1.1  jdolecek   * All rights reserved.
     37   1.1  jdolecek   *
     38   1.1  jdolecek   * Redistribution and use in source and binary forms, with or without
     39   1.1  jdolecek   * modification, are permitted provided that the following conditions
     40   1.1  jdolecek   * are met:
     41   1.1  jdolecek   * 1. Redistributions of source code must retain the above copyright
     42   1.1  jdolecek   *    notice, this list of conditions and the following disclaimer.
     43   1.1  jdolecek   * 2. Redistributions in binary form must reproduce the above copyright
     44   1.1  jdolecek   *    notice, this list of conditions and the following disclaimer in the
     45   1.1  jdolecek   *    documentation and/or other materials provided with the distribution.
     46   1.1  jdolecek   * 3. All advertising materials mentioning features or use of this software
     47   1.1  jdolecek   *    must display the following acknowledgement:
     48   1.1  jdolecek   *      This product includes software developed by  Mika Kortelainen
     49   1.1  jdolecek   * 4. The name of the author may not be used to endorse or promote products
     50   1.1  jdolecek   *    derived from this software without specific prior written permission
     51   1.1  jdolecek   *
     52   1.1  jdolecek   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53   1.1  jdolecek   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54   1.1  jdolecek   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55   1.1  jdolecek   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56   1.1  jdolecek   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57   1.1  jdolecek   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58   1.1  jdolecek   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59   1.1  jdolecek   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60   1.1  jdolecek   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61   1.1  jdolecek   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.1  jdolecek   */
     63   1.1  jdolecek 
     64   1.1  jdolecek  /*
     65   1.1  jdolecek   * Device driver for Fujitsu MB86960A/MB86965A based Ethernet cards.
     66   1.1  jdolecek   * Contributed by M.S. <seki (at) sysrap.cs.fujitsu.co.jp>
     67   1.1  jdolecek   */
     68   1.1  jdolecek 
     69   1.1  jdolecek #include <sys/cdefs.h>
     70  1.14       dsl __KERNEL_RCSID(0, "$NetBSD: mb86950.c,v 1.14 2009/03/14 15:36:17 dsl Exp $");
     71   1.1  jdolecek 
     72   1.1  jdolecek /*
     73   1.1  jdolecek  * Device driver for Fujitsu mb86950 based Ethernet cards.
     74   1.1  jdolecek  * Adapted by Dave J. Barnes from various Internet sources including
     75   1.1  jdolecek  * mb86960.c (NetBSD), if_qn.c (NetBSD/Amiga), DOS Packet Driver (Brian Fisher,
     76   1.1  jdolecek  * Queens University), EtherBoot Driver (Ken Yap).
     77   1.1  jdolecek  */
     78   1.1  jdolecek 
     79   1.1  jdolecek /* XXX There are still rough edges......
     80   1.2  jdolecek  *
     81   1.1  jdolecek  * (1) There is no watchdog timer for the transmitter. It's doubtful that
     82   1.2  jdolecek  *     transmit from the chip could be restarted without a hardware reset
     83   1.2  jdolecek  *     though. (Fixed - not fully tested)
     84   1.2  jdolecek  *
     85   1.1  jdolecek  * (2) The media interface callback goo is broke.  No big deal since to change
     86   1.2  jdolecek  *     from aui to bnc on the old Tiara LANCard requires moving 8 board jumpers.
     87   1.2  jdolecek  *     Other cards (SMC ?) using the EtherStar chip may support media change
     88   1.2  jdolecek  *     via software. (Fixed - tested)
     89   1.2  jdolecek  *
     90   1.2  jdolecek  * (3) The maximum outstanding transmit packets is set to 4.  What
     91   1.2  jdolecek  *     is a good limit of outstanding transmit packets for the EtherStar?
     92   1.2  jdolecek  *     Is there a way to tell how many bytes are remaining to be
     93   1.2  jdolecek  *     transmitted? [no]
     94   1.1  jdolecek ---
     95   1.2  jdolecek 	When the EtherStar was designed, CPU power was a fraction
     96   1.2  jdolecek 	of what it is now.  The single EtherStar transmit buffer
     97   1.2  jdolecek 	was fine.  It was unlikely that the CPU could outrun the
     98   1.2  jdolecek 	EtherStar. However, things in 2004 are quite different.
     99   1.2  jdolecek 	sc->txb_size is used to keep the CPU from overrunning the
    100   1.2  jdolecek 	EtherStar.  At most allow one packet transmitting and one
    101   1.2  jdolecek 	going into the fifo.
    102   1.2  jdolecek 
    103   1.1  jdolecek ---
    104   1.1  jdolecek     No, that isn't right either :(
    105   1.1  jdolecek 
    106   1.2  jdolecek  * (4) Multicast isn't supported.  Feel free to add multicast code
    107   1.2  jdolecek  *     if you know how to make the EtherStar do multicast.  Otherwise
    108   1.2  jdolecek  *     you'd have to use promiscuous mode and do multicast in software. OUCH!
    109   1.2  jdolecek  *
    110   1.1  jdolecek  * (5) There are no bus_space_barrier calls used. Are they needed? Maybe not.
    111   1.2  jdolecek  *
    112   1.2  jdolecek  * (6) Access to the fifo assumes word (16 bit) mode.  Cards configured for
    113   1.2  jdolecek  *     byte wide fifo access will require driver code changes.
    114   1.2  jdolecek  *
    115   1.2  jdolecek  * Only the minimum code necessary to make the Tiara LANCard work
    116   1.2  jdolecek  * has been tested. Other cards may require more work, especially
    117   1.2  jdolecek  * byte mode fifo and if DMA is used.
    118   1.2  jdolecek  *
    119   1.1  jdolecek  * djb / 2004
    120   1.1  jdolecek  */
    121   1.1  jdolecek 
    122   1.1  jdolecek #include "opt_inet.h"
    123   1.1  jdolecek #include "bpfilter.h"
    124   1.1  jdolecek #include "rnd.h"
    125   1.1  jdolecek 
    126   1.1  jdolecek #include <sys/param.h>
    127   1.1  jdolecek #include <sys/systm.h>
    128   1.1  jdolecek #include <sys/errno.h>
    129   1.1  jdolecek #include <sys/ioctl.h>
    130   1.1  jdolecek #include <sys/mbuf.h>
    131   1.1  jdolecek #include <sys/socket.h>
    132   1.1  jdolecek #include <sys/syslog.h>
    133   1.1  jdolecek #include <sys/device.h>
    134   1.1  jdolecek #if NRND > 0
    135   1.1  jdolecek #include <sys/rnd.h>
    136   1.1  jdolecek #endif
    137   1.1  jdolecek 
    138   1.1  jdolecek #include <net/if.h>
    139   1.1  jdolecek #include <net/if_dl.h>
    140   1.1  jdolecek #include <net/if_types.h>
    141   1.1  jdolecek #include <net/if_media.h>
    142   1.1  jdolecek #include <net/if_ether.h>
    143   1.1  jdolecek 
    144   1.1  jdolecek #ifdef INET
    145   1.1  jdolecek #include <netinet/in.h>
    146   1.1  jdolecek #include <netinet/in_systm.h>
    147   1.1  jdolecek #include <netinet/in_var.h>
    148   1.1  jdolecek #include <netinet/ip.h>
    149   1.1  jdolecek #include <netinet/if_inarp.h>
    150   1.1  jdolecek #endif
    151   1.1  jdolecek 
    152   1.1  jdolecek 
    153   1.1  jdolecek #if NBPFILTER > 0
    154   1.1  jdolecek #include <net/bpf.h>
    155   1.1  jdolecek #include <net/bpfdesc.h>
    156   1.1  jdolecek #endif
    157   1.1  jdolecek 
    158  1.10        ad #include <sys/bus.h>
    159   1.1  jdolecek 
    160   1.1  jdolecek #include <dev/ic/mb86950reg.h>
    161   1.1  jdolecek #include <dev/ic/mb86950var.h>
    162   1.1  jdolecek 
    163   1.1  jdolecek #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    164   1.1  jdolecek #define bus_space_write_stream_2	bus_space_write_2
    165   1.1  jdolecek #define bus_space_write_multi_stream_2	bus_space_write_multi_2
    166   1.1  jdolecek #define bus_space_read_multi_stream_2	bus_space_read_multi_2
    167   1.1  jdolecek #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
    168   1.1  jdolecek 
    169   1.1  jdolecek /* Standard driver entry points.  These can be static. */
    170  1.13       dsl int		mb86950_ioctl(struct ifnet *, u_long, void *);
    171  1.13       dsl void	mb86950_init(struct mb86950_softc *);
    172  1.13       dsl void	mb86950_start(struct ifnet *);
    173  1.13       dsl void	mb86950_watchdog(struct ifnet *);
    174  1.13       dsl void	mb86950_reset(struct mb86950_softc *);
    175   1.1  jdolecek 
    176   1.1  jdolecek /* Local functions. */
    177  1.13       dsl void	mb86950_stop(struct mb86950_softc *);
    178  1.13       dsl void	mb86950_tint(struct mb86950_softc *, u_int8_t);
    179  1.13       dsl void	mb86950_rint(struct mb86950_softc *, u_int8_t);
    180  1.13       dsl int		mb86950_get_fifo(struct mb86950_softc *, u_int);
    181  1.13       dsl ushort	mb86950_put_fifo(struct mb86950_softc *, struct mbuf *);
    182  1.13       dsl void	mb86950_drain_fifo(struct mb86950_softc *);
    183   1.1  jdolecek 
    184  1.13       dsl int		mb86950_mediachange(struct ifnet *);
    185  1.13       dsl void	mb86950_mediastatus(struct ifnet *, struct ifmediareq *);
    186   1.1  jdolecek 
    187   1.1  jdolecek 
    188   1.1  jdolecek #if ESTAR_DEBUG >= 1
    189  1.13       dsl void	mb86950_dump(int, struct mb86950_softc *);
    190   1.1  jdolecek #endif
    191   1.1  jdolecek 
    192   1.1  jdolecek /********************************************************************/
    193   1.1  jdolecek 
    194   1.1  jdolecek void
    195  1.14       dsl mb86950_attach(struct mb86950_softc *sc, u_int8_t *myea)
    196   1.1  jdolecek {
    197   1.1  jdolecek 
    198   1.1  jdolecek #ifdef DIAGNOSTIC
    199   1.1  jdolecek 	if (myea == NULL) {
    200   1.1  jdolecek 		printf("%s: ethernet address shouldn't be NULL\n",
    201  1.11    cegger 		    device_xname(&sc->sc_dev));
    202   1.1  jdolecek 		panic("NULL ethernet address");
    203   1.1  jdolecek 	}
    204   1.1  jdolecek #endif
    205   1.1  jdolecek 
    206   1.1  jdolecek 	/* Initialize 86950. */
    207   1.1  jdolecek 	mb86950_stop(sc);
    208   1.1  jdolecek 
    209   1.1  jdolecek 	memcpy(sc->sc_enaddr, myea, sizeof(sc->sc_enaddr));
    210   1.1  jdolecek 
    211   1.1  jdolecek 	sc->sc_stat |= ESTAR_STAT_ENABLED;
    212   1.1  jdolecek }
    213   1.1  jdolecek 
    214   1.1  jdolecek /*
    215   1.1  jdolecek  * Stop everything on the interface.
    216   1.1  jdolecek  *
    217   1.1  jdolecek  * All buffered packets, both transmitting and receiving,
    218   1.1  jdolecek  * if any, will be lost by stopping the interface.
    219   1.1  jdolecek  */
    220   1.1  jdolecek void
    221  1.14       dsl mb86950_stop(struct mb86950_softc *sc)
    222   1.1  jdolecek {
    223   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    224   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    225   1.1  jdolecek 
    226   1.1  jdolecek 	/* Stop interface hardware. */
    227   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_CONFIG, DISABLE_DLC);
    228   1.1  jdolecek 	delay(200);
    229   1.1  jdolecek 
    230   1.1  jdolecek 	/* Disable interrupts. */
    231   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_INT_EN, 0);
    232   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_INT_EN, 0);
    233   1.1  jdolecek 
    234   1.1  jdolecek 	/* Ack / Clear all interrupt status. */
    235   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_STAT, 0xff);
    236   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_STAT, 0xff);
    237   1.1  jdolecek 
    238   1.1  jdolecek 	/* Clear DMA Bit */
    239   1.1  jdolecek     bus_space_write_2(bst, bsh, BMPR_DMA, 0);
    240   1.1  jdolecek 
    241   1.1  jdolecek     /* accept no packets */
    242   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_MODE, 0);
    243   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_MODE, 0);
    244   1.1  jdolecek 
    245   1.1  jdolecek     mb86950_drain_fifo(sc);
    246   1.1  jdolecek 
    247   1.1  jdolecek }
    248   1.1  jdolecek 
    249   1.1  jdolecek void
    250  1.14       dsl mb86950_drain_fifo(struct mb86950_softc *sc)
    251   1.1  jdolecek {
    252   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    253   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    254   1.1  jdolecek 
    255   1.1  jdolecek 	/* Read data until bus read error (i.e. buffer empty). */
    256   1.2  jdolecek 	/* XXX There ought to be a better way, eats CPU and bothers the chip */
    257   1.1  jdolecek 	while (!(bus_space_read_1(bst, bsh, DLCR_RX_STAT) & RX_BUS_RD_ERR))
    258   1.1  jdolecek 		bus_space_read_2(bst, bsh, BMPR_FIFO);
    259   1.2  jdolecek 	/* XXX */
    260   1.1  jdolecek 
    261   1.1  jdolecek 	/* Clear Bus Rd Error */
    262   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_STAT, RX_BUS_RD_ERR);
    263   1.1  jdolecek }
    264   1.1  jdolecek 
    265   1.1  jdolecek /*
    266   1.1  jdolecek  * Install interface into kernel networking data structures
    267   1.1  jdolecek  */
    268   1.1  jdolecek void
    269   1.8  christos mb86950_config(struct mb86950_softc *sc, int *media,
    270   1.8  christos     int nmedia, int defmedia)
    271   1.1  jdolecek {
    272   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    273   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    274   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    275   1.1  jdolecek 
    276   1.1  jdolecek 	/* Initialize ifnet structure. */
    277  1.11    cegger 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    278   1.1  jdolecek 	ifp->if_softc = sc;
    279   1.1  jdolecek 	ifp->if_start = mb86950_start;
    280   1.1  jdolecek 	ifp->if_ioctl = mb86950_ioctl;
    281   1.1  jdolecek 	ifp->if_watchdog = mb86950_watchdog;
    282   1.1  jdolecek 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS;
    283   1.1  jdolecek 
    284   1.1  jdolecek 	IFQ_SET_READY(&ifp->if_snd);
    285   1.1  jdolecek 
    286   1.1  jdolecek 	/* Initialize media goo. */
    287   1.1  jdolecek 	/* XXX The Tiara LANCard uses board jumpers to change media.
    288   1.1  jdolecek 	 *       This code may have to be changed for other cards.
    289   1.1  jdolecek 	 */
    290   1.1  jdolecek 	ifmedia_init(&sc->sc_media, 0, mb86950_mediachange, mb86950_mediastatus);
    291   1.1  jdolecek 	ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
    292   1.1  jdolecek 	ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
    293   1.1  jdolecek 
    294   1.1  jdolecek 	/* Attach the interface. */
    295   1.1  jdolecek 	if_attach(ifp);
    296   1.1  jdolecek 
    297   1.1  jdolecek 	/* Feed the chip the station address. */
    298   1.1  jdolecek 	bus_space_write_region_1(bst, bsh, DLCR_NODE_ID, sc->sc_enaddr, ETHER_ADDR_LEN);
    299   1.1  jdolecek 
    300   1.1  jdolecek 	ether_ifattach(ifp, sc->sc_enaddr);
    301   1.1  jdolecek 
    302   1.1  jdolecek #if NRND > 0
    303  1.11    cegger 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
    304   1.1  jdolecek 	    RND_TYPE_NET, 0);
    305   1.1  jdolecek #endif
    306   1.1  jdolecek 
    307   1.1  jdolecek /* XXX No! This doesn't work - DLCR6 of the mb86950 is different
    308   1.1  jdolecek 
    309   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_CONFIG, 0x0f);
    310   1.1  jdolecek 	buf_config = bus_space_read_1(bst, bsh, DLCR_CONFIG);
    311   1.1  jdolecek 
    312   1.1  jdolecek 	sc->txb_count = ((buf_config & 0x0c) ? 2 : 1);
    313   1.1  jdolecek 	sc->txb_size = 1024 * (2 << ((buf_config & 0x0c) ? (((buf_config & 0x0c) >> 2) - 1) : 0));
    314   1.1  jdolecek 	sc->txb_free = (sc->txb_size * sc->txb_count) / 1500;
    315   1.1  jdolecek 
    316   1.1  jdolecek   	sc->rxb_size = ((8 << (buf_config & 3)) * 1024) - (sc->txb_size * sc->txb_count);
    317   1.1  jdolecek 	sc->rxb_max = sc->rxb_size / 64;
    318   1.1  jdolecek 
    319   1.1  jdolecek 	printf("mb86950: Buffer Size %dKB with %d transmit buffer(s) %dKB each.\n",
    320   1.1  jdolecek 		(8 << (buf_config & 3)), sc->txb_count,	(sc->txb_size / 1024));
    321   1.1  jdolecek 	printf("         Transmit Buffer Space for %d maximum sized packet(s).\n",sc->txb_free);
    322   1.1  jdolecek 	printf("         System Bus Width %d bits, Buffer Memory %d bits.\n",
    323   1.1  jdolecek 		((buf_config & 0x20) ? 8 : 16),
    324   1.1  jdolecek 		((buf_config & 0x10) ? 8 : 16));
    325   1.1  jdolecek 
    326   1.1  jdolecek */
    327   1.1  jdolecek 
    328   1.2  jdolecek 	/* Set reasonable values for number of packet flow control if not
    329   1.2  jdolecek 	 * set elsewhere */
    330   1.1  jdolecek 	if (sc->txb_num_pkt == 0) sc->txb_num_pkt = 1;
    331   1.1  jdolecek 	if (sc->rxb_num_pkt == 0) sc->rxb_num_pkt = 100;
    332   1.1  jdolecek 
    333   1.1  jdolecek 	/* Print additional info when attached. */
    334  1.11    cegger 	printf("%s: Ethernet address %s\n", device_xname(&sc->sc_dev),
    335   1.2  jdolecek 	    ether_sprintf(sc->sc_enaddr));
    336   1.1  jdolecek 
    337   1.1  jdolecek 	/* The attach is successful. */
    338   1.1  jdolecek 	sc->sc_stat |= ESTAR_STAT_ATTACHED;
    339   1.1  jdolecek }
    340   1.1  jdolecek 
    341   1.1  jdolecek /*
    342   1.1  jdolecek  * Media change callback.
    343   1.1  jdolecek  */
    344   1.1  jdolecek int
    345  1.14       dsl mb86950_mediachange(struct ifnet *ifp)
    346   1.1  jdolecek {
    347   1.1  jdolecek 
    348   1.1  jdolecek 	struct mb86950_softc *sc = ifp->if_softc;
    349   1.1  jdolecek 
    350   1.1  jdolecek 	if (sc->sc_mediachange)
    351   1.1  jdolecek 		return ((*sc->sc_mediachange)(sc));
    352   1.1  jdolecek 
    353   1.1  jdolecek 	return (0);
    354   1.1  jdolecek }
    355   1.1  jdolecek 
    356   1.1  jdolecek /*
    357   1.1  jdolecek  * Media status callback.
    358   1.1  jdolecek  */
    359   1.1  jdolecek void
    360  1.14       dsl mb86950_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    361   1.1  jdolecek {
    362   1.1  jdolecek 	struct mb86950_softc *sc = ifp->if_softc;
    363   1.1  jdolecek 
    364   1.1  jdolecek 	if ((sc->sc_stat & ESTAR_STAT_ENABLED) == 0) {
    365   1.1  jdolecek 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
    366   1.1  jdolecek 		ifmr->ifm_status = 0;
    367   1.1  jdolecek 		return;
    368   1.1  jdolecek 	}
    369   1.1  jdolecek 
    370   1.1  jdolecek 	if (sc->sc_mediastatus)
    371   1.1  jdolecek 		(*sc->sc_mediastatus)(sc, ifmr);
    372   1.1  jdolecek 
    373   1.1  jdolecek }
    374   1.1  jdolecek 
    375   1.1  jdolecek /*
    376   1.1  jdolecek  * Reset interface.
    377   1.1  jdolecek  */
    378   1.1  jdolecek void
    379  1.14       dsl mb86950_reset(struct mb86950_softc *sc)
    380   1.1  jdolecek {
    381   1.1  jdolecek 	int s;
    382   1.1  jdolecek 
    383   1.1  jdolecek 	s = splnet();
    384  1.11    cegger 	log(LOG_ERR, "%s: device reset\n", device_xname(&sc->sc_dev));
    385   1.1  jdolecek 	mb86950_stop(sc);
    386   1.1  jdolecek 	mb86950_init(sc);
    387   1.1  jdolecek 	splx(s);
    388   1.1  jdolecek }
    389   1.1  jdolecek 
    390   1.1  jdolecek /*
    391   1.1  jdolecek  * Device timeout/watchdog routine. Entered if the device neglects to
    392   1.1  jdolecek  * generate an interrupt after a transmit has been started on it.
    393   1.1  jdolecek  */
    394   1.1  jdolecek void
    395  1.14       dsl mb86950_watchdog(struct ifnet *ifp)
    396   1.1  jdolecek {
    397   1.1  jdolecek 	struct mb86950_softc *sc = ifp->if_softc;
    398   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    399   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    400   1.1  jdolecek 	u_int8_t tstat;
    401   1.1  jdolecek 
    402   1.1  jdolecek 	/* verbose watchdog messages for debugging timeouts */
    403   1.1  jdolecek     if ((tstat = bus_space_read_1(bst, bsh, DLCR_TX_STAT)) != 0) {
    404   1.1  jdolecek 		if (tstat & TX_CR_LOST) {
    405   1.2  jdolecek 			if ((tstat & (TX_COL | TX_16COL)) == 0) {
    406   1.2  jdolecek 				 log(LOG_ERR, "%s: carrier lost\n",
    407  1.11    cegger 				    device_xname(&sc->sc_dev));
    408   1.2  jdolecek 			} else {
    409   1.2  jdolecek 				log(LOG_ERR, "%s: excessive collisions\n",
    410  1.11    cegger 				    device_xname(&sc->sc_dev));
    411   1.2  jdolecek 			}
    412   1.2  jdolecek 		}
    413   1.2  jdolecek 		else if ((tstat & (TX_UNDERFLO | TX_BUS_WR_ERR)) != 0) {
    414   1.2  jdolecek 			log(LOG_ERR, "%s: tx fifo underflow/overflow\n",
    415  1.11    cegger 			    device_xname(&sc->sc_dev));
    416   1.2  jdolecek 		} else {
    417   1.2  jdolecek 			log(LOG_ERR, "%s: transmit error\n",
    418  1.11    cegger 			    device_xname(&sc->sc_dev));
    419   1.1  jdolecek 		}
    420   1.2  jdolecek 	} else {
    421  1.11    cegger 		log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
    422   1.1  jdolecek 	}
    423   1.1  jdolecek 
    424   1.1  jdolecek 	/* Don't know how many packets are lost by this accident.
    425   1.1  jdolecek 	 *  ... So just errors = errors + 1
    426   1.1  jdolecek 	 */
    427   1.1  jdolecek 	ifp->if_oerrors++;
    428   1.1  jdolecek 
    429   1.1  jdolecek 	mb86950_reset(sc);
    430   1.1  jdolecek 
    431   1.1  jdolecek }
    432   1.1  jdolecek 
    433   1.1  jdolecek /*
    434   1.1  jdolecek  ******************** IOCTL
    435   1.1  jdolecek  * Process an ioctl request.
    436   1.1  jdolecek  */
    437   1.1  jdolecek int
    438  1.12    dyoung mb86950_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
    439   1.1  jdolecek {
    440   1.1  jdolecek 	struct mb86950_softc *sc = ifp->if_softc;
    441   1.1  jdolecek 	struct ifaddr *ifa = (struct ifaddr *)data;
    442   1.1  jdolecek 	struct ifreq *ifr = (struct ifreq *)data;
    443   1.1  jdolecek 
    444   1.1  jdolecek 	int s, error = 0;
    445   1.1  jdolecek 
    446   1.1  jdolecek 	s = splnet();
    447   1.1  jdolecek 
    448   1.1  jdolecek 	switch (cmd) {
    449  1.12    dyoung 	case SIOCINITIFADDR:
    450  1.12    dyoung 		/* XXX deprecated ? What should I use instead? */
    451   1.1  jdolecek 		if ((error = mb86950_enable(sc)) != 0)
    452   1.1  jdolecek 			break;
    453   1.1  jdolecek 
    454   1.1  jdolecek 		ifp->if_flags |= IFF_UP;
    455   1.1  jdolecek 
    456  1.12    dyoung 		mb86950_init(sc);
    457   1.1  jdolecek 		switch (ifa->ifa_addr->sa_family) {
    458   1.1  jdolecek 
    459   1.1  jdolecek #ifdef INET
    460   1.1  jdolecek 		case AF_INET:
    461   1.1  jdolecek 			arp_ifinit(ifp, ifa);
    462   1.1  jdolecek 			break;
    463   1.1  jdolecek #endif
    464   1.1  jdolecek 
    465   1.1  jdolecek 
    466   1.1  jdolecek 		default:
    467   1.1  jdolecek 			break;
    468   1.1  jdolecek 		}
    469   1.1  jdolecek 		break;
    470   1.1  jdolecek 
    471   1.1  jdolecek 	case SIOCSIFFLAGS:
    472  1.12    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
    473  1.12    dyoung 			break;
    474  1.12    dyoung 		/* XXX re-use ether_ioctl() */
    475  1.12    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    476  1.12    dyoung 		case IFF_RUNNING:
    477   1.1  jdolecek 			/*
    478   1.1  jdolecek 			 * If interface is marked down and it is running, then
    479   1.1  jdolecek 			 * stop it.
    480   1.1  jdolecek 			 */
    481   1.1  jdolecek 			mb86950_stop(sc);
    482   1.1  jdolecek 			ifp->if_flags &= ~IFF_RUNNING;
    483   1.1  jdolecek 			mb86950_disable(sc);
    484  1.12    dyoung 			break;
    485  1.12    dyoung 		case IFF_UP:
    486   1.1  jdolecek 			/*
    487   1.1  jdolecek 			 * If interface is marked up and it is stopped, then
    488   1.1  jdolecek 			 * start it.
    489   1.1  jdolecek 			 */
    490   1.1  jdolecek 			if ((error = mb86950_enable(sc)) != 0)
    491   1.1  jdolecek 				break;
    492   1.1  jdolecek 			mb86950_init(sc);
    493  1.12    dyoung 			break;
    494  1.12    dyoung 		case IFF_UP|IFF_RUNNING:
    495   1.1  jdolecek 			/*
    496   1.1  jdolecek 			 * Reset the interface to pick up changes in any other
    497   1.1  jdolecek 			 * flags that affect hardware registers.
    498   1.1  jdolecek 			 */
    499  1.12    dyoung #if 0
    500  1.12    dyoung 			/* Setmode not supported */
    501   1.1  jdolecek 			mb86950_setmode(sc);
    502  1.12    dyoung #endif
    503  1.12    dyoung 			break;
    504  1.12    dyoung 		case 0:
    505  1.12    dyoung 			break;
    506   1.1  jdolecek 		}
    507   1.1  jdolecek 
    508   1.1  jdolecek #if ESTAR_DEBUG >= 1
    509   1.1  jdolecek 		/* "ifconfig fe0 debug" to print register dump. */
    510   1.1  jdolecek 		if (ifp->if_flags & IFF_DEBUG) {
    511   1.1  jdolecek 			log(LOG_INFO, "%s: SIOCSIFFLAGS(DEBUG)\n",
    512  1.11    cegger 			    device_xname(&sc->sc_dev));
    513   1.1  jdolecek 			mb86950_dump(LOG_DEBUG, sc);
    514   1.1  jdolecek 		}
    515   1.1  jdolecek #endif
    516   1.1  jdolecek 		break;
    517   1.1  jdolecek 
    518   1.1  jdolecek 	case SIOCGIFMEDIA:
    519   1.1  jdolecek 	case SIOCSIFMEDIA:
    520   1.1  jdolecek 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
    521   1.1  jdolecek 		break;
    522   1.1  jdolecek 
    523   1.1  jdolecek 	default:
    524  1.12    dyoung 		error = ether_ioctl(ifp, cmd, data);
    525   1.1  jdolecek 		break;
    526   1.1  jdolecek 	}
    527   1.1  jdolecek 
    528   1.1  jdolecek 	splx(s);
    529   1.1  jdolecek 	return (error);
    530   1.1  jdolecek }
    531   1.1  jdolecek 
    532   1.1  jdolecek /*
    533   1.1  jdolecek  * Initialize device.
    534   1.1  jdolecek  */
    535   1.1  jdolecek void
    536  1.14       dsl mb86950_init(struct mb86950_softc *sc)
    537   1.1  jdolecek {
    538   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    539   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    540   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    541   1.1  jdolecek 
    542   1.1  jdolecek 	/* Reset transmitter flags. */
    543   1.1  jdolecek 	ifp->if_flags &= ~IFF_OACTIVE;
    544   1.1  jdolecek 	ifp->if_timer = 0;
    545   1.1  jdolecek 	sc->txb_sched = 0;
    546   1.1  jdolecek 
    547   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_MODE, LBC);
    548   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_MODE, NORMAL_MODE);
    549   1.1  jdolecek 
    550   1.1  jdolecek 	/* Enable interrupts. */
    551   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_INT_EN, TX_MASK);
    552   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_INT_EN, RX_MASK);
    553   1.1  jdolecek 
    554   1.1  jdolecek 	/* Enable transmitter and receiver. */
    555   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_CONFIG, ENABLE_DLC);
    556   1.1  jdolecek 	delay(200);
    557   1.1  jdolecek 
    558   1.1  jdolecek 	/* Set 'running' flag. */
    559   1.1  jdolecek 	ifp->if_flags |= IFF_RUNNING;
    560   1.1  jdolecek 
    561   1.1  jdolecek 	/* ...and attempt to start output. */
    562   1.1  jdolecek 	mb86950_start(ifp);
    563   1.1  jdolecek 
    564   1.1  jdolecek }
    565   1.1  jdolecek 
    566   1.1  jdolecek void
    567  1.14       dsl mb86950_start(struct ifnet *ifp)
    568   1.1  jdolecek {
    569   1.1  jdolecek 	struct mb86950_softc *sc = ifp->if_softc;
    570   1.1  jdolecek     bus_space_tag_t bst = sc->sc_bst;
    571   1.1  jdolecek     bus_space_handle_t bsh = sc->sc_bsh;
    572   1.1  jdolecek 	struct mbuf *m;
    573   1.1  jdolecek 	int len;
    574   1.1  jdolecek 
    575   1.1  jdolecek 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    576   1.1  jdolecek 		return;
    577   1.1  jdolecek 
    578   1.1  jdolecek 	IF_DEQUEUE(&ifp->if_snd, m);
    579   1.1  jdolecek 	if (m == 0)
    580   1.1  jdolecek 		return;
    581   1.1  jdolecek 
    582   1.1  jdolecek #if NBPFILTER > 0
    583   1.1  jdolecek 	/* Tap off here if there is a BPF listener. */
    584   1.1  jdolecek 	if (ifp->if_bpf)
    585   1.1  jdolecek 		bpf_mtap(ifp->if_bpf, m);
    586   1.1  jdolecek #endif
    587   1.1  jdolecek 
    588   1.2  jdolecek 	/* Send the packet to the mb86950 */
    589   1.1  jdolecek 	len = mb86950_put_fifo(sc,m);
    590   1.1  jdolecek 	m_freem(m);
    591   1.1  jdolecek 
    592   1.2  jdolecek 	/* XXX bus_space_barrier here ? */
    593   1.1  jdolecek 	if (bus_space_read_1(bst, bsh, DLCR_TX_STAT) & (TX_UNDERFLO | TX_BUS_WR_ERR)) {
    594  1.11    cegger 		log(LOG_ERR, "%s: tx fifo underflow/overflow\n", device_xname(&sc->sc_dev));
    595   1.1  jdolecek 	}
    596   1.1  jdolecek 
    597   1.1  jdolecek 	bus_space_write_2(bst, bsh, BMPR_TX_LENGTH, len | TRANSMIT_START);
    598   1.1  jdolecek 
    599   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_INT_EN, TX_MASK);
    600   1.2  jdolecek 	/* XXX                          */
    601   1.1  jdolecek 	sc->txb_sched++;
    602   1.1  jdolecek 
    603   1.1  jdolecek 	/* We have space for 'n' transmit packets of size 'mtu. */
    604   1.1  jdolecek 	if (sc->txb_sched > sc->txb_num_pkt) {
    605   1.1  jdolecek 		ifp->if_flags |= IFF_OACTIVE;
    606   1.1  jdolecek 		ifp->if_timer = 2;
    607   1.1  jdolecek 	}
    608   1.1  jdolecek }
    609   1.1  jdolecek 
    610   1.1  jdolecek /*
    611   1.2  jdolecek  * Send packet - copy packet from mbuf to the fifo
    612   1.1  jdolecek  */
    613   1.1  jdolecek u_short
    614  1.14       dsl mb86950_put_fifo(struct mb86950_softc *sc, struct mbuf *m)
    615   1.1  jdolecek {
    616   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    617   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    618   1.1  jdolecek 	u_short *data;
    619   1.1  jdolecek 	u_char savebyte[2];
    620   1.1  jdolecek 	int len, len1, wantbyte;
    621   1.1  jdolecek 	u_short totlen;
    622   1.1  jdolecek 
    623   1.5       mrg 	memset(savebyte, 0, sizeof(savebyte));	/* XXX gcc */
    624   1.5       mrg 
    625   1.1  jdolecek 	totlen = wantbyte = 0;
    626   1.1  jdolecek 
    627   1.1  jdolecek 	for (; m != NULL; m = m->m_next) {
    628   1.1  jdolecek 		data = mtod(m, u_short *);
    629   1.1  jdolecek 		len = m->m_len;
    630   1.1  jdolecek 		if (len > 0) {
    631   1.1  jdolecek 			totlen += len;
    632   1.1  jdolecek 
    633   1.1  jdolecek 			/* Finish the last word. */
    634   1.1  jdolecek 			if (wantbyte) {
    635   1.1  jdolecek 				savebyte[1] = *((u_char *)data);
    636   1.1  jdolecek 				bus_space_write_2(bst, bsh, BMPR_FIFO, *savebyte);
    637   1.4     skrll 				data = (u_short *)((u_char *)data + 1);
    638   1.1  jdolecek 				len--;
    639   1.1  jdolecek 				wantbyte = 0;
    640   1.1  jdolecek 			}
    641   1.1  jdolecek 			/* Output contiguous words. */
    642   1.1  jdolecek 			if (len > 1) {
    643   1.1  jdolecek 				len1 = len/2;
    644   1.1  jdolecek 				bus_space_write_multi_stream_2(bst, bsh, BMPR_FIFO, data, len1);
    645   1.1  jdolecek 				data += len1;
    646   1.1  jdolecek 				len &= 1;
    647   1.1  jdolecek 			}
    648   1.1  jdolecek 			/* Save last byte, if necessary. */
    649   1.1  jdolecek 			if (len == 1) {
    650   1.1  jdolecek 				savebyte[0] = *((u_char *)data);
    651   1.1  jdolecek 				wantbyte = 1;
    652   1.1  jdolecek 			}
    653   1.1  jdolecek 		}
    654   1.1  jdolecek 	}
    655   1.1  jdolecek 
    656   1.1  jdolecek 	if (wantbyte) {
    657   1.1  jdolecek 		savebyte[1] = 0;
    658   1.1  jdolecek 		bus_space_write_2(bst, bsh, BMPR_FIFO, *savebyte);
    659   1.1  jdolecek 	}
    660   1.1  jdolecek 
    661   1.1  jdolecek 	if (totlen < (ETHER_MIN_LEN - ETHER_CRC_LEN)) {
    662   1.1  jdolecek 
    663   1.1  jdolecek 		/* Fill the rest of the packet with zeros. */
    664   1.2  jdolecek 		/* XXX Replace this mess with something else, eats CPU */
    665   1.2  jdolecek 		/* The zero fill and last byte ought to be combined somehow */
    666   1.1  jdolecek 		for(len = totlen + 1; len < (ETHER_MIN_LEN - ETHER_CRC_LEN); len += 2)
    667   1.1  jdolecek 	  		bus_space_write_2(bst, bsh, BMPR_FIFO, 0);
    668   1.2  jdolecek 		/* XXX                                       */
    669   1.1  jdolecek 
    670   1.1  jdolecek 		totlen = (ETHER_MIN_LEN - ETHER_CRC_LEN);
    671   1.1  jdolecek 	}
    672   1.1  jdolecek 
    673   1.1  jdolecek 	return (totlen);
    674   1.1  jdolecek }
    675   1.1  jdolecek 
    676   1.1  jdolecek /*
    677   1.2  jdolecek  * Handle interrupts.
    678   1.1  jdolecek  * Ethernet interface interrupt processor
    679   1.1  jdolecek  */
    680   1.1  jdolecek int
    681  1.14       dsl mb86950_intr(void *arg)
    682   1.1  jdolecek {
    683   1.1  jdolecek 	struct mb86950_softc *sc = arg;
    684   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    685   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    686   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    687   1.1  jdolecek 	u_int8_t tstat, rstat;
    688   1.1  jdolecek 
    689   1.1  jdolecek 	/* Get interrupt status. */
    690   1.1  jdolecek 	tstat = bus_space_read_1(bst, bsh, DLCR_TX_STAT);
    691   1.1  jdolecek 	rstat = bus_space_read_1(bst, bsh, DLCR_RX_STAT);
    692   1.1  jdolecek 
    693   1.1  jdolecek 	if (tstat == 0 && rstat == 0) return (0);
    694   1.1  jdolecek 
    695   1.1  jdolecek 	/* Disable etherstar interrupts so that we won't miss anything. */
    696   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_TX_INT_EN, 0);
    697   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_INT_EN, 0);
    698   1.1  jdolecek 
    699   1.1  jdolecek 	/*
    700   1.1  jdolecek 	 * Handle transmitter interrupts. Handle these first because
    701   1.1  jdolecek 	 * the receiver will reset the board under some conditions.
    702   1.1  jdolecek 	 */
    703   1.1  jdolecek 	if (tstat != 0) {
    704   1.1  jdolecek 
    705   1.1  jdolecek 		mb86950_tint(sc, tstat);
    706   1.1  jdolecek 
    707   1.1  jdolecek 		/* acknowledge transmit interrupt status. */
    708   1.1  jdolecek 		bus_space_write_1(bst, bsh, DLCR_TX_STAT, tstat);
    709   1.1  jdolecek 
    710   1.1  jdolecek 	}
    711   1.1  jdolecek 
    712   1.1  jdolecek 	/* Handle receiver interrupts. */
    713   1.1  jdolecek 	if (rstat != 0) {
    714   1.1  jdolecek 
    715   1.1  jdolecek 		mb86950_rint(sc, rstat);
    716   1.1  jdolecek 
    717   1.1  jdolecek 		/* acknowledge receive interrupt status. */
    718   1.1  jdolecek 		bus_space_write_1(bst, bsh, DLCR_RX_STAT, rstat);
    719   1.1  jdolecek 
    720   1.1  jdolecek 	}
    721   1.1  jdolecek 
    722   1.1  jdolecek 	/* If tx still pending reset tx interrupt mask */
    723   1.2  jdolecek 	if (sc->txb_sched > 0)
    724   1.2  jdolecek 		bus_space_write_1(bst, bsh, DLCR_TX_INT_EN, TX_MASK);
    725   1.1  jdolecek 
    726   1.1  jdolecek 	/*
    727   1.1  jdolecek 	 * If it looks like the transmitter can take more data,
    728   1.1  jdolecek 	 * attempt to start output on the interface. This is done
    729   1.1  jdolecek 	 * after handling the receiver interrupt to give the
    730   1.1  jdolecek 	 * receive operation priority.
    731   1.1  jdolecek 	 */
    732   1.1  jdolecek 
    733   1.1  jdolecek 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    734   1.1  jdolecek 		mb86950_start(ifp);
    735   1.1  jdolecek 
    736   1.1  jdolecek 	/* Set receive interrupts back */
    737   1.1  jdolecek 	bus_space_write_1(bst, bsh, DLCR_RX_INT_EN, RX_MASK);
    738   1.1  jdolecek 
    739   1.1  jdolecek 	return(1);
    740   1.1  jdolecek }
    741   1.1  jdolecek 
    742   1.1  jdolecek /* Transmission interrupt handler */
    743   1.1  jdolecek void
    744  1.14       dsl mb86950_tint(struct mb86950_softc *sc, u_int8_t tstat)
    745   1.1  jdolecek {
    746   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    747   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    748   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    749   1.1  jdolecek 	int col;
    750   1.1  jdolecek 
    751   1.1  jdolecek 	if (tstat & (TX_UNDERFLO | TX_BUS_WR_ERR)) {
    752   1.1  jdolecek 		/* XXX What do we need to do here? reset ? */
    753   1.1  jdolecek 		ifp->if_oerrors++;
    754   1.1  jdolecek 	}
    755   1.1  jdolecek 
    756   1.1  jdolecek 	/* excessive collision */
    757   1.1  jdolecek 	if (tstat & TX_16COL) {
    758   1.1  jdolecek 		ifp->if_collisions += 16;
    759   1.1  jdolecek 		/* 16 collisions means that the packet has been thrown away. */
    760   1.2  jdolecek 		if (sc->txb_sched > 0)
    761   1.2  jdolecek 			sc->txb_sched--;
    762   1.1  jdolecek 	}
    763   1.1  jdolecek 
    764   1.1  jdolecek 	/* transmission complete. */
    765   1.1  jdolecek 	if (tstat & TX_DONE) {
    766   1.1  jdolecek 		/* successfully transmitted packets ++. */
    767   1.1  jdolecek 		ifp->if_opackets++;
    768   1.2  jdolecek 		if (sc->txb_sched > 0)
    769   1.2  jdolecek 			sc->txb_sched--;
    770   1.1  jdolecek 
    771   1.1  jdolecek 		/* Collision count valid only when TX_DONE is set */
    772   1.1  jdolecek 		if (tstat & TX_COL) {
    773   1.1  jdolecek 			col = (bus_space_read_1(bst, bsh, DLCR_TX_MODE) & COL_MASK) >> 4;
    774   1.1  jdolecek 			ifp->if_collisions = ifp->if_collisions + col;
    775   1.1  jdolecek 		}
    776   1.1  jdolecek 	}
    777   1.1  jdolecek 
    778   1.1  jdolecek 	if (sc->txb_sched == 0) {
    779   1.1  jdolecek 		 /* Reset output active flag and stop timer. */
    780   1.1  jdolecek 		 ifp->if_flags &= ~IFF_OACTIVE;
    781   1.1  jdolecek 		 ifp->if_timer = 0;
    782   1.1  jdolecek 	}
    783   1.1  jdolecek }
    784   1.1  jdolecek 
    785   1.1  jdolecek /* receiver interrupt. */
    786   1.1  jdolecek void
    787  1.14       dsl mb86950_rint(struct mb86950_softc *sc, u_int8_t rstat)
    788   1.1  jdolecek {
    789   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    790   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    791   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    792   1.1  jdolecek 	u_int status, len;
    793   1.1  jdolecek 	int i;
    794   1.1  jdolecek 
    795   1.1  jdolecek 	 /* Update statistics if this interrupt is caused by an error. */
    796   1.1  jdolecek 	 if (rstat & RX_ERR_MASK) {
    797   1.1  jdolecek 
    798   1.1  jdolecek 		/* tried to read past end of fifo, should be harmless
    799   1.1  jdolecek 		 * count everything else
    800   1.1  jdolecek 		 */
    801   1.1  jdolecek 		if ((rstat & RX_BUS_RD_ERR) == 0) {
    802   1.1  jdolecek 			ifp->if_ierrors++;
    803   1.1  jdolecek 		}
    804   1.1  jdolecek 	}
    805   1.1  jdolecek 
    806   1.1  jdolecek 	/*
    807   1.1  jdolecek 	 * mb86950 has a flag indicating "receive buffer empty."
    808   1.1  jdolecek 	 * We just loop checking the flag to pull out all received
    809   1.1  jdolecek 	 * packets.
    810   1.1  jdolecek 	 *
    811   1.1  jdolecek 	 * We limit the number of iterrations to avoid infinite loop.
    812   1.1  jdolecek 	 * It can be caused by a very slow CPU (some broken
    813   1.1  jdolecek 	 * peripheral may insert incredible number of wait cycles)
    814   1.1  jdolecek 	 * or, worse, by a broken mb86950 chip.
    815   1.1  jdolecek 	 */
    816   1.1  jdolecek 	for (i = 0; i < sc->rxb_num_pkt; i++) {
    817   1.1  jdolecek 		/* Stop the iterration if 86950 indicates no packets. */
    818   1.1  jdolecek 		if (bus_space_read_1(bst, bsh, DLCR_RX_MODE) & RX_BUF_EMTY)
    819   1.1  jdolecek 			break;
    820   1.1  jdolecek 
    821   1.1  jdolecek 		/* receive packet status */
    822   1.1  jdolecek 		status = bus_space_read_2(bst, bsh, BMPR_FIFO);
    823   1.1  jdolecek 
    824   1.1  jdolecek 		/* bad packet? */
    825   1.1  jdolecek 		if ((status & GOOD_PKT) == 0) {
    826   1.1  jdolecek 			ifp->if_ierrors++;
    827   1.1  jdolecek 			mb86950_drain_fifo(sc);
    828   1.1  jdolecek 			continue;
    829   1.1  jdolecek 		}
    830   1.1  jdolecek 
    831   1.1  jdolecek 		/* Length valid ? */
    832   1.1  jdolecek 		len = bus_space_read_2(bst, bsh, BMPR_FIFO);
    833   1.1  jdolecek 
    834   1.1  jdolecek 		if (len > (ETHER_MAX_LEN - ETHER_CRC_LEN) || len < ETHER_HDR_LEN) {
    835   1.1  jdolecek 			ifp->if_ierrors++;
    836   1.1  jdolecek 			mb86950_drain_fifo(sc);
    837   1.1  jdolecek 			continue;
    838   1.1  jdolecek 		}
    839   1.1  jdolecek 
    840   1.1  jdolecek 		if (mb86950_get_fifo(sc, len) != 0) {
    841   1.1  jdolecek 			/* No mbufs? Drop packet. */
    842   1.1  jdolecek 			ifp->if_ierrors++;
    843   1.1  jdolecek 			mb86950_drain_fifo(sc);
    844   1.1  jdolecek 			return;
    845   1.1  jdolecek 		}
    846   1.1  jdolecek 
    847   1.1  jdolecek 		/* Successfully received a packet.  Update stat. */
    848   1.1  jdolecek 		ifp->if_ipackets++;
    849   1.1  jdolecek 	}
    850   1.1  jdolecek }
    851   1.1  jdolecek 
    852   1.1  jdolecek /*
    853   1.2  jdolecek  * Receive packet.
    854   1.1  jdolecek  * Retrieve packet from receive buffer and send to the next level up via
    855   1.1  jdolecek  * ether_input(). If there is a BPF listener, give a copy to BPF, too.
    856   1.1  jdolecek  * Returns 0 if success, -1 if error (i.e., mbuf allocation failure).
    857   1.1  jdolecek  */
    858   1.1  jdolecek int
    859  1.14       dsl mb86950_get_fifo(struct mb86950_softc *sc, u_int len)
    860   1.1  jdolecek {
    861   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
    862   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
    863   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    864   1.1  jdolecek 	struct mbuf *m;
    865   1.1  jdolecek 
    866   1.1  jdolecek 	/* Allocate a header mbuf. */
    867   1.1  jdolecek 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    868   1.1  jdolecek 	if (m == 0)
    869   1.1  jdolecek 		return (-1);
    870   1.1  jdolecek 
    871   1.1  jdolecek 	/*
    872   1.1  jdolecek 	 * Round len to even value.
    873   1.1  jdolecek 	 */
    874   1.1  jdolecek 	if (len & 1)
    875   1.1  jdolecek 		len++;
    876   1.1  jdolecek 
    877   1.1  jdolecek 	m->m_pkthdr.rcvif = ifp;
    878   1.1  jdolecek 	m->m_pkthdr.len = len;
    879   1.1  jdolecek 
    880   1.1  jdolecek 	/* The following silliness is to make NFS happy. */
    881   1.1  jdolecek #define	EROUND	((sizeof(struct ether_header) + 3) & ~3)
    882   1.1  jdolecek #define	EOFF	(EROUND - sizeof(struct ether_header))
    883   1.1  jdolecek 
    884   1.1  jdolecek 	/*
    885   1.1  jdolecek 	 * Our strategy has one more problem.  There is a policy on
    886   1.1  jdolecek 	 * mbuf cluster allocation.  It says that we must have at
    887   1.1  jdolecek 	 * least MINCLSIZE (208 bytes) to allocate a cluster.  For a
    888   1.1  jdolecek 	 * packet of a size between (MHLEN - 2) to (MINCLSIZE - 2),
    889   1.1  jdolecek 	 * our code violates the rule...
    890   1.1  jdolecek 	 * On the other hand, the current code is short, simple,
    891   1.1  jdolecek 	 * and fast, however.  It does no harmful thing, just wastes
    892   1.1  jdolecek 	 * some memory.  Any comments?  FIXME.
    893   1.1  jdolecek 	 */
    894   1.1  jdolecek 
    895   1.1  jdolecek 	/* Attach a cluster if this packet doesn't fit in a normal mbuf. */
    896   1.1  jdolecek 	if (len > MHLEN - EOFF) {
    897   1.1  jdolecek 		MCLGET(m, M_DONTWAIT);
    898   1.1  jdolecek 		if ((m->m_flags & M_EXT) == 0) {
    899   1.1  jdolecek 			m_freem(m);
    900   1.1  jdolecek 			return (-1);
    901   1.1  jdolecek 		}
    902   1.1  jdolecek 	}
    903   1.1  jdolecek 
    904   1.1  jdolecek 	/*
    905   1.1  jdolecek 	 * The following assumes there is room for the ether header in the
    906   1.1  jdolecek 	 * header mbuf.
    907   1.1  jdolecek 	 */
    908   1.1  jdolecek 	m->m_data += EOFF;
    909   1.1  jdolecek 
    910   1.1  jdolecek 	/* Set the length of this packet. */
    911   1.1  jdolecek 	m->m_len = len;
    912   1.1  jdolecek 
    913   1.1  jdolecek 	/* Get a packet. */
    914   1.1  jdolecek 	bus_space_read_multi_stream_2(bst, bsh, BMPR_FIFO, mtod(m, u_int16_t *), (len + 1) >> 1);
    915   1.1  jdolecek 
    916   1.1  jdolecek #if NBPFILTER > 0
    917   1.1  jdolecek 	/*
    918   1.1  jdolecek 	 * Check if there's a BPF listener on this interface.  If so, hand off
    919   1.1  jdolecek 	 * the raw packet to bpf.
    920   1.1  jdolecek 	 */
    921   1.1  jdolecek 	if (ifp->if_bpf)
    922   1.1  jdolecek 		bpf_mtap(ifp->if_bpf, m);
    923   1.1  jdolecek #endif
    924   1.1  jdolecek 
    925   1.1  jdolecek 	(*ifp->if_input)(ifp, m);
    926   1.1  jdolecek 	return (0);
    927   1.1  jdolecek }
    928   1.1  jdolecek 
    929   1.1  jdolecek /*
    930   1.1  jdolecek  * Enable power on the interface.
    931   1.1  jdolecek  */
    932   1.1  jdolecek int
    933  1.14       dsl mb86950_enable(struct mb86950_softc *sc)
    934   1.1  jdolecek {
    935   1.1  jdolecek 
    936   1.1  jdolecek 	if ((sc->sc_stat & ESTAR_STAT_ENABLED) == 0 && sc->sc_enable != NULL) {
    937   1.1  jdolecek 		if ((*sc->sc_enable)(sc) != 0) {
    938  1.11    cegger 			aprint_error_dev(&sc->sc_dev, "device enable failed\n");
    939   1.1  jdolecek 			return (EIO);
    940   1.1  jdolecek 		}
    941   1.1  jdolecek 	}
    942   1.1  jdolecek 
    943   1.1  jdolecek 	sc->sc_stat |= ESTAR_STAT_ENABLED;
    944   1.1  jdolecek 	return (0);
    945   1.1  jdolecek }
    946   1.1  jdolecek 
    947   1.1  jdolecek /*
    948   1.1  jdolecek  * Disable power on the interface.
    949   1.1  jdolecek  */
    950   1.1  jdolecek void
    951  1.14       dsl mb86950_disable(struct mb86950_softc *sc)
    952   1.1  jdolecek {
    953   1.1  jdolecek 
    954   1.1  jdolecek 	if ((sc->sc_stat & ESTAR_STAT_ENABLED) != 0 && sc->sc_disable != NULL) {
    955   1.1  jdolecek 		(*sc->sc_disable)(sc);
    956   1.1  jdolecek 		sc->sc_stat &= ~ESTAR_STAT_ENABLED;
    957   1.1  jdolecek 	}
    958   1.1  jdolecek }
    959   1.1  jdolecek 
    960   1.1  jdolecek /*
    961   1.1  jdolecek  * mbe_activate:
    962   1.1  jdolecek  *
    963   1.1  jdolecek  *	Handle device activation/deactivation requests.
    964   1.1  jdolecek  */
    965   1.1  jdolecek int
    966  1.14       dsl mb86950_activate(struct device *self, enum devact act)
    967   1.1  jdolecek {
    968   1.1  jdolecek 	struct mb86950_softc *sc = (struct mb86950_softc *)self;
    969   1.1  jdolecek 	int rv, s;
    970   1.1  jdolecek 
    971   1.1  jdolecek 	rv = 0;
    972   1.1  jdolecek 	s = splnet();
    973   1.1  jdolecek 	switch (act) {
    974   1.1  jdolecek 	case DVACT_ACTIVATE:
    975   1.1  jdolecek 		rv = EOPNOTSUPP;
    976   1.1  jdolecek 		break;
    977   1.1  jdolecek 
    978   1.1  jdolecek 	case DVACT_DEACTIVATE:
    979   1.1  jdolecek 		if_deactivate(&sc->sc_ec.ec_if);
    980   1.1  jdolecek 		break;
    981   1.1  jdolecek 	}
    982   1.1  jdolecek 	splx(s);
    983   1.1  jdolecek 	return (rv);
    984   1.1  jdolecek }
    985   1.1  jdolecek 
    986   1.1  jdolecek /*
    987   1.1  jdolecek  * mb86950_detach:
    988   1.1  jdolecek  *
    989   1.1  jdolecek  *	Detach a mb86950 interface.
    990   1.1  jdolecek  */
    991   1.1  jdolecek int
    992  1.14       dsl mb86950_detach(struct mb86950_softc *sc)
    993   1.1  jdolecek {
    994   1.1  jdolecek 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    995   1.1  jdolecek 
    996   1.1  jdolecek 	/* Succeed now if there's no work to do. */
    997   1.1  jdolecek 	if ((sc->sc_stat & ESTAR_STAT_ATTACHED) == 0)
    998   1.1  jdolecek 		return (0);
    999   1.1  jdolecek 
   1000   1.1  jdolecek 	/* Delete all media. */
   1001   1.1  jdolecek 	ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
   1002   1.1  jdolecek 
   1003   1.1  jdolecek #if NRND > 0
   1004   1.1  jdolecek 	/* Unhook the entropy source. */
   1005   1.1  jdolecek 	rnd_detach_source(&sc->rnd_source);
   1006   1.1  jdolecek #endif
   1007   1.1  jdolecek 	ether_ifdetach(ifp);
   1008   1.1  jdolecek 	if_detach(ifp);
   1009   1.1  jdolecek 
   1010   1.1  jdolecek 	return (0);
   1011   1.1  jdolecek }
   1012   1.1  jdolecek 
   1013   1.1  jdolecek #if ESTAR_DEBUG >= 1
   1014   1.1  jdolecek void
   1015  1.14       dsl mb86950_dump(int level, struct mb86950_softc *sc)
   1016   1.1  jdolecek {
   1017   1.1  jdolecek 	bus_space_tag_t bst = sc->sc_bst;
   1018   1.1  jdolecek 	bus_space_handle_t bsh = sc->sc_bsh;
   1019   1.1  jdolecek 
   1020   1.1  jdolecek 	log(level, "\tDLCR = %02x %02x %02x %02x %02x %02x %02x\n",
   1021   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_TX_STAT),
   1022   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_TX_INT_EN),
   1023   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_RX_STAT),
   1024   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_RX_INT_EN),
   1025   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_TX_MODE),
   1026   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_RX_MODE),
   1027   1.1  jdolecek 	    bus_space_read_1(bst, bsh, DLCR_CONFIG));
   1028   1.1  jdolecek 
   1029   1.2  jdolecek 	/* XXX BMPR2, 4 write only ?
   1030   1.1  jdolecek 	log(level, "\tBMPR = xxxx %04x %04x\n",
   1031   1.1  jdolecek 		bus_space_read_2(bst, bsh, BMPR_TX_LENGTH),
   1032   1.1  jdolecek 		bus_space_read_2(bst, bsh, BMPR_DMA));
   1033   1.2  jdolecek 	*/
   1034   1.1  jdolecek }
   1035   1.1  jdolecek #endif
   1036