Home | History | Annotate | Line # | Download | only in pci
if_tl.c revision 1.77
      1 /*	$NetBSD: if_tl.c,v 1.77 2006/11/16 01:33:09 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *  This product includes software developed by Manuel Bouyer.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Texas Instruments ThunderLAN ethernet controller
     34  * ThunderLAN Programmer's Guide (TI Literature Number SPWU013A)
     35  * available from www.ti.com
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: if_tl.c,v 1.77 2006/11/16 01:33:09 christos Exp $");
     40 
     41 #undef TLDEBUG
     42 #define TL_PRIV_STATS
     43 #undef TLDEBUG_RX
     44 #undef TLDEBUG_TX
     45 #undef TLDEBUG_ADDR
     46 
     47 #include "opt_inet.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/protosw.h>
     53 #include <sys/socket.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/errno.h>
     56 #include <sys/malloc.h>
     57 #include <sys/kernel.h>
     58 #include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
     59 #include <sys/device.h>
     60 
     61 #include <net/if.h>
     62 #if defined(SIOCSIFMEDIA)
     63 #include <net/if_media.h>
     64 #endif
     65 #include <net/if_types.h>
     66 #include <net/if_dl.h>
     67 #include <net/route.h>
     68 #include <net/netisr.h>
     69 
     70 #include "bpfilter.h"
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #include <net/bpfdesc.h>
     74 #endif
     75 
     76 #include "rnd.h"
     77 #if NRND > 0
     78 #include <sys/rnd.h>
     79 #endif
     80 
     81 #ifdef INET
     82 #include <netinet/in.h>
     83 #include <netinet/in_systm.h>
     84 #include <netinet/in_var.h>
     85 #include <netinet/ip.h>
     86 #endif
     87 
     88 
     89 #if defined(__NetBSD__)
     90 #include <net/if_ether.h>
     91 #include <uvm/uvm_extern.h>
     92 #if defined(INET)
     93 #include <netinet/if_inarp.h>
     94 #endif
     95 
     96 #include <machine/bus.h>
     97 #include <machine/intr.h>
     98 
     99 #include <dev/pci/pcireg.h>
    100 #include <dev/pci/pcivar.h>
    101 #include <dev/pci/pcidevs.h>
    102 
    103 #include <dev/i2c/i2cvar.h>
    104 #include <dev/i2c/i2c_bitbang.h>
    105 #include <dev/i2c/at24cxxvar.h>
    106 
    107 #include <dev/mii/mii.h>
    108 #include <dev/mii/miivar.h>
    109 
    110 #include <dev/mii/tlphyvar.h>
    111 
    112 #include <dev/pci/if_tlregs.h>
    113 #include <dev/pci/if_tlvar.h>
    114 #endif /* __NetBSD__ */
    115 
    116 /* number of transmit/receive buffers */
    117 #ifndef TL_NBUF
    118 #define TL_NBUF 32
    119 #endif
    120 
    121 static int tl_pci_match(struct device *, struct cfdata *, void *);
    122 static void tl_pci_attach(struct device *, struct device *, void *);
    123 static int tl_intr(void *);
    124 
    125 static int tl_ifioctl(struct ifnet *, ioctl_cmd_t, caddr_t);
    126 static int tl_mediachange(struct ifnet *);
    127 static void tl_mediastatus(struct ifnet *, struct ifmediareq *);
    128 static void tl_ifwatchdog(struct ifnet *);
    129 static void tl_shutdown(void*);
    130 
    131 static void tl_ifstart(struct ifnet *);
    132 static void tl_reset(tl_softc_t*);
    133 static int  tl_init(struct ifnet *);
    134 static void tl_stop(struct ifnet *, int);
    135 static void tl_restart(void  *);
    136 static int  tl_add_RxBuff(tl_softc_t*, struct Rx_list*, struct mbuf*);
    137 static void tl_read_stats(tl_softc_t*);
    138 static void tl_ticks(void*);
    139 static int tl_multicast_hash(u_int8_t*);
    140 static void tl_addr_filter(tl_softc_t*);
    141 
    142 static u_int32_t tl_intreg_read(tl_softc_t*, u_int32_t);
    143 static void tl_intreg_write(tl_softc_t*, u_int32_t, u_int32_t);
    144 static u_int8_t tl_intreg_read_byte(tl_softc_t*, u_int32_t);
    145 static void tl_intreg_write_byte(tl_softc_t*, u_int32_t, u_int8_t);
    146 
    147 void	tl_mii_sync(struct tl_softc *);
    148 void	tl_mii_sendbits(struct tl_softc *, u_int32_t, int);
    149 
    150 
    151 #if defined(TLDEBUG_RX)
    152 static void ether_printheader(struct ether_header*);
    153 #endif
    154 
    155 int tl_mii_read(struct device *, int, int);
    156 void tl_mii_write(struct device *, int, int, int);
    157 
    158 void tl_statchg(struct device *);
    159 
    160 	/* I2C glue */
    161 static int tl_i2c_acquire_bus(void *, int);
    162 static void tl_i2c_release_bus(void *, int);
    163 static int tl_i2c_send_start(void *, int);
    164 static int tl_i2c_send_stop(void *, int);
    165 static int tl_i2c_initiate_xfer(void *, i2c_addr_t, int);
    166 static int tl_i2c_read_byte(void *, uint8_t *, int);
    167 static int tl_i2c_write_byte(void *, uint8_t, int);
    168 
    169 	/* I2C bit-bang glue */
    170 static void tl_i2cbb_set_bits(void *, uint32_t);
    171 static void tl_i2cbb_set_dir(void *, uint32_t);
    172 static uint32_t tl_i2cbb_read(void *);
    173 static const struct i2c_bitbang_ops tl_i2cbb_ops = {
    174 	tl_i2cbb_set_bits,
    175 	tl_i2cbb_set_dir,
    176 	tl_i2cbb_read,
    177 	{
    178 		TL_NETSIO_EDATA,	/* SDA */
    179 		TL_NETSIO_ECLOCK,	/* SCL */
    180 		TL_NETSIO_ETXEN,	/* SDA is output */
    181 		0,			/* SDA is input */
    182 	}
    183 };
    184 
    185 static inline void netsio_clr(tl_softc_t*, u_int8_t);
    186 static inline void netsio_set(tl_softc_t*, u_int8_t);
    187 static inline u_int8_t netsio_read(tl_softc_t*, u_int8_t);
    188 static inline void netsio_clr(sc, bits)
    189 	tl_softc_t* sc;
    190 	u_int8_t bits;
    191 {
    192 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
    193 	    tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & (~bits));
    194 }
    195 static inline void netsio_set(sc, bits)
    196 	tl_softc_t* sc;
    197 	u_int8_t bits;
    198 {
    199 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio,
    200 	    tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) | bits);
    201 }
    202 static inline u_int8_t netsio_read(sc, bits)
    203 	tl_softc_t* sc;
    204 	u_int8_t bits;
    205 {
    206 	return (tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio) & bits);
    207 }
    208 
    209 CFATTACH_DECL(tl, sizeof(tl_softc_t),
    210     tl_pci_match, tl_pci_attach, NULL, NULL);
    211 
    212 const struct tl_product_desc tl_compaq_products[] = {
    213 	{ PCI_PRODUCT_COMPAQ_N100TX, TLPHY_MEDIA_NO_10_T,
    214 	  "Compaq Netelligent 10/100 TX" },
    215 	{ PCI_PRODUCT_COMPAQ_INT100TX, TLPHY_MEDIA_NO_10_T,
    216 	  "Integrated Compaq Netelligent 10/100 TX" },
    217 	{ PCI_PRODUCT_COMPAQ_N10T, TLPHY_MEDIA_10_5,
    218 	  "Compaq Netelligent 10 T" },
    219 	{ PCI_PRODUCT_COMPAQ_N10T2, TLPHY_MEDIA_10_2,
    220 	  "Compaq Netelligent 10 T/2 UTP/Coax" },
    221 	{ PCI_PRODUCT_COMPAQ_IntNF3P, TLPHY_MEDIA_10_2,
    222 	  "Compaq Integrated NetFlex 3/P" },
    223 	{ PCI_PRODUCT_COMPAQ_IntPL100TX, TLPHY_MEDIA_10_2|TLPHY_MEDIA_NO_10_T,
    224 	  "Compaq ProLiant Integrated Netelligent 10/100 TX" },
    225 	{ PCI_PRODUCT_COMPAQ_DPNet100TX, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
    226 	  "Compaq Dual Port Netelligent 10/100 TX" },
    227 	{ PCI_PRODUCT_COMPAQ_DP4000, TLPHY_MEDIA_10_5|TLPHY_MEDIA_NO_10_T,
    228 	  "Compaq Deskpro 4000 5233MMX" },
    229 	{ PCI_PRODUCT_COMPAQ_NF3P_BNC, TLPHY_MEDIA_10_2,
    230 	  "Compaq NetFlex 3/P w/ BNC" },
    231 	{ PCI_PRODUCT_COMPAQ_NF3P, TLPHY_MEDIA_10_5,
    232 	  "Compaq NetFlex 3/P" },
    233 	{ 0, 0, NULL },
    234 };
    235 
    236 const struct tl_product_desc tl_ti_products[] = {
    237 	/*
    238 	 * Built-in Ethernet on the TI TravelMate 5000
    239 	 * docking station; better product description?
    240 	 */
    241 	{ PCI_PRODUCT_TI_TLAN, 0,
    242 	  "Texas Instruments ThunderLAN" },
    243 	{ 0, 0, NULL },
    244 };
    245 
    246 struct tl_vendor_desc {
    247 	u_int32_t tv_vendor;
    248 	const struct tl_product_desc *tv_products;
    249 };
    250 
    251 const struct tl_vendor_desc tl_vendors[] = {
    252 	{ PCI_VENDOR_COMPAQ, tl_compaq_products },
    253 	{ PCI_VENDOR_TI, tl_ti_products },
    254 	{ 0, NULL },
    255 };
    256 
    257 const struct tl_product_desc *tl_lookup_product(u_int32_t);
    258 
    259 const struct tl_product_desc *
    260 tl_lookup_product(id)
    261 	u_int32_t id;
    262 {
    263 	const struct tl_product_desc *tp;
    264 	const struct tl_vendor_desc *tv;
    265 
    266 	for (tv = tl_vendors; tv->tv_products != NULL; tv++)
    267 		if (PCI_VENDOR(id) == tv->tv_vendor)
    268 			break;
    269 
    270 	if ((tp = tv->tv_products) == NULL)
    271 		return (NULL);
    272 
    273 	for (; tp->tp_desc != NULL; tp++)
    274 		if (PCI_PRODUCT(id) == tp->tp_product)
    275 			break;
    276 
    277 	if (tp->tp_desc == NULL)
    278 		return (NULL);
    279 
    280 	return (tp);
    281 }
    282 
    283 static int
    284 tl_pci_match(struct device *parent, struct cfdata *match,
    285     void *aux)
    286 {
    287 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
    288 
    289 	if (tl_lookup_product(pa->pa_id) != NULL)
    290 		return (1);
    291 
    292 	return (0);
    293 }
    294 
    295 static void
    296 tl_pci_attach(struct device *parent, struct device *self, void *aux)
    297 {
    298 	tl_softc_t *sc = (tl_softc_t *)self;
    299 	struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
    300 	const struct tl_product_desc *tp;
    301 	struct ifnet * const ifp = &sc->tl_if;
    302 	bus_space_tag_t iot, memt;
    303 	bus_space_handle_t ioh, memh;
    304 	pci_intr_handle_t intrhandle;
    305 	const char *intrstr;
    306 	int ioh_valid, memh_valid;
    307 	int reg_io, reg_mem;
    308 	pcireg_t reg10, reg14;
    309 	pcireg_t csr;
    310 
    311 	printf("\n");
    312 
    313 	callout_init(&sc->tl_tick_ch);
    314 	callout_init(&sc->tl_restart_ch);
    315 
    316 	tp = tl_lookup_product(pa->pa_id);
    317 	if (tp == NULL)
    318 		panic("tl_pci_attach: impossible");
    319 	sc->tl_product = tp;
    320 
    321 	/*
    322 	 * Map the card space. First we have to find the I/O and MEM
    323 	 * registers. I/O is supposed to be at 0x10, MEM at 0x14,
    324 	 * but some boards (Compaq Netflex 3/P PCI) seem to have it reversed.
    325 	 * The ThunderLAN manual is not consistent about this either (there
    326 	 * are both cases in code examples).
    327 	 */
    328 	reg10 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x10);
    329 	reg14 = pci_conf_read(pa->pa_pc, pa->pa_tag, 0x14);
    330 	if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_IO)
    331 		reg_io = 0x10;
    332 	else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_IO)
    333 		reg_io = 0x14;
    334 	else
    335 		reg_io = 0;
    336 	if (PCI_MAPREG_TYPE(reg10) == PCI_MAPREG_TYPE_MEM)
    337 		reg_mem = 0x10;
    338 	else if (PCI_MAPREG_TYPE(reg14) == PCI_MAPREG_TYPE_MEM)
    339 		reg_mem = 0x14;
    340 	else
    341 		reg_mem = 0;
    342 
    343 	if (reg_io != 0)
    344 		ioh_valid = (pci_mapreg_map(pa, reg_io, PCI_MAPREG_TYPE_IO,
    345 		    0, &iot, &ioh, NULL, NULL) == 0);
    346 	else
    347 		ioh_valid = 0;
    348 	if (reg_mem != 0)
    349 		memh_valid = (pci_mapreg_map(pa, PCI_CBMA,
    350 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    351 		    0, &memt, &memh, NULL, NULL) == 0);
    352 	else
    353 		memh_valid = 0;
    354 
    355 	if (ioh_valid) {
    356 		sc->tl_bustag = iot;
    357 		sc->tl_bushandle = ioh;
    358 	} else if (memh_valid) {
    359 		sc->tl_bustag = memt;
    360 		sc->tl_bushandle = memh;
    361 	} else {
    362 		printf("%s: unable to map device registers\n",
    363 		    sc->sc_dev.dv_xname);
    364 		return;
    365 	}
    366 	sc->tl_dmatag = pa->pa_dmat;
    367 
    368 	/* Enable the device. */
    369 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    370 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    371 	    csr | PCI_COMMAND_MASTER_ENABLE);
    372 
    373 	printf("%s: %s\n", sc->sc_dev.dv_xname, tp->tp_desc);
    374 
    375 	tl_reset(sc);
    376 
    377 	/* fill in the i2c tag */
    378 	sc->sc_i2c.ic_cookie = sc;
    379 	sc->sc_i2c.ic_acquire_bus = tl_i2c_acquire_bus;
    380 	sc->sc_i2c.ic_release_bus = tl_i2c_release_bus;
    381 	sc->sc_i2c.ic_send_start = tl_i2c_send_start;
    382 	sc->sc_i2c.ic_send_stop = tl_i2c_send_stop;
    383 	sc->sc_i2c.ic_initiate_xfer = tl_i2c_initiate_xfer;
    384 	sc->sc_i2c.ic_read_byte = tl_i2c_read_byte;
    385 	sc->sc_i2c.ic_write_byte = tl_i2c_write_byte;
    386 
    387 #ifdef TLDEBUG
    388 	printf("default values of INTreg: 0x%x\n",
    389 	    tl_intreg_read(sc, TL_INT_Defaults));
    390 #endif
    391 
    392 	/* read mac addr */
    393 	if (seeprom_bootstrap_read(&sc->sc_i2c, 0x50, 0x83, 512/*?*/,
    394 				   sc->tl_enaddr, ETHER_ADDR_LEN)) {
    395 		printf("%s: error reading Ethernet address\n",
    396 		    sc->sc_dev.dv_xname);
    397 			return;
    398 	}
    399 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    400 	    ether_sprintf(sc->tl_enaddr));
    401 
    402 	/* Map and establish interrupts */
    403 	if (pci_intr_map(pa, &intrhandle)) {
    404 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    405 		return;
    406 	}
    407 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
    408 	sc->tl_if.if_softc = sc;
    409 	sc->tl_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
    410 	    tl_intr, sc);
    411 	if (sc->tl_ih == NULL) {
    412 		printf("%s: couldn't establish interrupt",
    413 		    sc->sc_dev.dv_xname);
    414 		if (intrstr != NULL)
    415 			printf(" at %s", intrstr);
    416 		printf("\n");
    417 		return;
    418 	}
    419 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    420 
    421 	/* init these pointers, so that tl_shutdown won't try to read them */
    422 	sc->Rx_list = NULL;
    423 	sc->Tx_list = NULL;
    424 
    425 	/* allocate DMA-safe memory for control structs */
    426 	if (bus_dmamem_alloc(sc->tl_dmatag,
    427 	        PAGE_SIZE, 0, PAGE_SIZE,
    428 	        &sc->ctrl_segs, 1, &sc->ctrl_nsegs, BUS_DMA_NOWAIT) != 0 ||
    429 	    bus_dmamem_map(sc->tl_dmatag, &sc->ctrl_segs,
    430 		sc->ctrl_nsegs, PAGE_SIZE, (caddr_t*)&sc->ctrl,
    431 		BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0) {
    432 			printf("%s: can't allocate DMA memory for lists\n",
    433 			    sc->sc_dev.dv_xname);
    434 			return;
    435 	}
    436 	/*
    437 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
    438 	 * doing
    439 	 * reboot before the driver initializes.
    440 	 */
    441 	(void) shutdownhook_establish(tl_shutdown, ifp);
    442 
    443 	/*
    444 	 * Initialize our media structures and probe the MII.
    445 	 *
    446 	 * Note that we don't care about the media instance.  We
    447 	 * are expecting to have multiple PHYs on the 10/100 cards,
    448 	 * and on those cards we exclude the internal PHY from providing
    449 	 * 10baseT.  By ignoring the instance, it allows us to not have
    450 	 * to specify it on the command line when switching media.
    451 	 */
    452 	sc->tl_mii.mii_ifp = ifp;
    453 	sc->tl_mii.mii_readreg = tl_mii_read;
    454 	sc->tl_mii.mii_writereg = tl_mii_write;
    455 	sc->tl_mii.mii_statchg = tl_statchg;
    456 	ifmedia_init(&sc->tl_mii.mii_media, IFM_IMASK, tl_mediachange,
    457 	    tl_mediastatus);
    458 	mii_attach(self, &sc->tl_mii, 0xffffffff, MII_PHY_ANY,
    459 	    MII_OFFSET_ANY, 0);
    460 	if (LIST_FIRST(&sc->tl_mii.mii_phys) == NULL) {
    461 		ifmedia_add(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    462 		ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_NONE);
    463 	} else
    464 		ifmedia_set(&sc->tl_mii.mii_media, IFM_ETHER|IFM_AUTO);
    465 
    466 	/*
    467 	 * We can support 802.1Q VLAN-sized frames.
    468 	 */
    469 	sc->tl_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    470 
    471 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    472 	ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
    473 	ifp->if_ioctl = tl_ifioctl;
    474 	ifp->if_start = tl_ifstart;
    475 	ifp->if_watchdog = tl_ifwatchdog;
    476 	ifp->if_init = tl_init;
    477 	ifp->if_stop = tl_stop;
    478 	ifp->if_timer = 0;
    479 	IFQ_SET_READY(&ifp->if_snd);
    480 	if_attach(ifp);
    481 	ether_ifattach(&(sc)->tl_if, (sc)->tl_enaddr);
    482 
    483 #if NRND > 0
    484 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    485 	    RND_TYPE_NET, 0);
    486 #endif
    487 }
    488 
    489 static void
    490 tl_reset(sc)
    491 	tl_softc_t *sc;
    492 {
    493 	int i;
    494 
    495 	/* read stats */
    496 	if (sc->tl_if.if_flags & IFF_RUNNING) {
    497 		callout_stop(&sc->tl_tick_ch);
    498 		tl_read_stats(sc);
    499 	}
    500 	/* Reset adapter */
    501 	TL_HR_WRITE(sc, TL_HOST_CMD,
    502 	    TL_HR_READ(sc, TL_HOST_CMD) | HOST_CMD_Ad_Rst);
    503 	DELAY(100000);
    504 	/* Disable interrupts */
    505 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
    506 	/* setup aregs & hash */
    507 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
    508 		tl_intreg_write(sc, i, 0);
    509 #ifdef TLDEBUG_ADDR
    510 	printf("Areg & hash registers: \n");
    511 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
    512 		printf("    reg %x: %x\n", i, tl_intreg_read(sc, i));
    513 #endif
    514 	/* Setup NetConfig */
    515 	tl_intreg_write(sc, TL_INT_NetConfig,
    516 	    TL_NETCONFIG_1F | TL_NETCONFIG_1chn | TL_NETCONFIG_PHY_EN);
    517 	/* Bsize: accept default */
    518 	/* TX commit in Acommit: accept default */
    519 	/* Load Ld_tmr and Ld_thr */
    520 	/* Ld_tmr = 3 */
    521 	TL_HR_WRITE(sc, TL_HOST_CMD, 0x3 | HOST_CMD_LdTmr);
    522 	/* Ld_thr = 0 */
    523 	TL_HR_WRITE(sc, TL_HOST_CMD, 0x0 | HOST_CMD_LdThr);
    524 	/* Unreset MII */
    525 	netsio_set(sc, TL_NETSIO_NMRST);
    526 	DELAY(100000);
    527 	sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
    528 }
    529 
    530 static void tl_shutdown(v)
    531 	void *v;
    532 {
    533 	tl_stop(v, 1);
    534 }
    535 
    536 static void tl_stop(struct ifnet *ifp, int disable)
    537 {
    538 	tl_softc_t *sc = ifp->if_softc;
    539 	struct Tx_list *Tx;
    540 	int i;
    541 
    542 	if ((ifp->if_flags & IFF_RUNNING) == 0)
    543 		return;
    544 	/* disable interrupts */
    545 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
    546 	/* stop TX and RX channels */
    547 	TL_HR_WRITE(sc, TL_HOST_CMD,
    548 	    HOST_CMD_STOP | HOST_CMD_RT | HOST_CMD_Nes);
    549 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_STOP);
    550 	DELAY(100000);
    551 
    552 	/* stop statistics reading loop, read stats */
    553 	callout_stop(&sc->tl_tick_ch);
    554 	tl_read_stats(sc);
    555 
    556 	/* Down the MII. */
    557 	mii_down(&sc->tl_mii);
    558 
    559 	/* deallocate memory allocations */
    560 	if (sc->Rx_list) {
    561 		for (i=0; i< TL_NBUF; i++) {
    562 			if (sc->Rx_list[i].m) {
    563 				bus_dmamap_unload(sc->tl_dmatag,
    564 				    sc->Rx_list[i].m_dmamap);
    565 				m_freem(sc->Rx_list[i].m);
    566 			}
    567 			bus_dmamap_destroy(sc->tl_dmatag,
    568 			    sc->Rx_list[i].m_dmamap);
    569 			sc->Rx_list[i].m = NULL;
    570 		}
    571 		free(sc->Rx_list, M_DEVBUF);
    572 		sc->Rx_list = NULL;
    573 		bus_dmamap_unload(sc->tl_dmatag, sc->Rx_dmamap);
    574 		bus_dmamap_destroy(sc->tl_dmatag, sc->Rx_dmamap);
    575 		sc->hw_Rx_list = NULL;
    576 		while ((Tx = sc->active_Tx) != NULL) {
    577 			Tx->hw_list->stat = 0;
    578 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
    579 			bus_dmamap_destroy(sc->tl_dmatag, Tx->m_dmamap);
    580 			m_freem(Tx->m);
    581 			sc->active_Tx = Tx->next;
    582 			Tx->next = sc->Free_Tx;
    583 			sc->Free_Tx = Tx;
    584 		}
    585 		sc->last_Tx = NULL;
    586 		free(sc->Tx_list, M_DEVBUF);
    587 		sc->Tx_list = NULL;
    588 		bus_dmamap_unload(sc->tl_dmatag, sc->Tx_dmamap);
    589 		bus_dmamap_destroy(sc->tl_dmatag, sc->Tx_dmamap);
    590 		sc->hw_Tx_list = NULL;
    591 	}
    592 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
    593 	ifp->if_timer = 0;
    594 	sc->tl_mii.mii_media_status &= ~IFM_ACTIVE;
    595 }
    596 
    597 static void tl_restart(v)
    598 	void *v;
    599 {
    600 	tl_init(v);
    601 }
    602 
    603 static int tl_init(ifp)
    604 	struct ifnet *ifp;
    605 {
    606 	tl_softc_t *sc = ifp->if_softc;
    607 	int i, s, error;
    608 	const char *errstring;
    609 	char *nullbuf;
    610 
    611 	s = splnet();
    612 	/* cancel any pending IO */
    613 	tl_stop(ifp, 1);
    614 	tl_reset(sc);
    615 	if ((sc->tl_if.if_flags & IFF_UP) == 0) {
    616 		splx(s);
    617 		return 0;
    618 	}
    619 	/* Set various register to reasonable value */
    620 	/* setup NetCmd in promisc mode if needed */
    621 	i = (ifp->if_flags & IFF_PROMISC) ? TL_NETCOMMAND_CAF : 0;
    622 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd,
    623 	    TL_NETCOMMAND_NRESET | TL_NETCOMMAND_NWRAP | i);
    624 	/* Max receive size : MCLBYTES */
    625 	tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxL, MCLBYTES & 0xff);
    626 	tl_intreg_write_byte(sc, TL_INT_MISC + TL_MISC_MaxRxH,
    627 	    (MCLBYTES >> 8) & 0xff);
    628 
    629 	/* init MAC addr */
    630 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    631 		tl_intreg_write_byte(sc, TL_INT_Areg0 + i , sc->tl_enaddr[i]);
    632 	/* add multicast filters */
    633 	tl_addr_filter(sc);
    634 #ifdef TLDEBUG_ADDR
    635 	printf("Wrote Mac addr, Areg & hash registers are now: \n");
    636 	for (i = TL_INT_Areg0; i <= TL_INT_HASH2; i = i + 4)
    637 		printf("    reg %x: %x\n", i, tl_intreg_read(sc, i));
    638 #endif
    639 
    640 	/* Pre-allocate receivers mbuf, make the lists */
    641 	sc->Rx_list = malloc(sizeof(struct Rx_list) * TL_NBUF, M_DEVBUF,
    642 	    M_NOWAIT|M_ZERO);
    643 	sc->Tx_list = malloc(sizeof(struct Tx_list) * TL_NBUF, M_DEVBUF,
    644 	    M_NOWAIT|M_ZERO);
    645 	if (sc->Rx_list == NULL || sc->Tx_list == NULL) {
    646 		errstring = "out of memory for lists";
    647 		error = ENOMEM;
    648 		goto bad;
    649 	}
    650 	error = bus_dmamap_create(sc->tl_dmatag,
    651 	    sizeof(struct tl_Rx_list) * TL_NBUF, 1,
    652 	    sizeof(struct tl_Rx_list) * TL_NBUF, 0, BUS_DMA_WAITOK,
    653 	    &sc->Rx_dmamap);
    654 	if (error == 0)
    655 		error = bus_dmamap_create(sc->tl_dmatag,
    656 		    sizeof(struct tl_Tx_list) * TL_NBUF, 1,
    657 		    sizeof(struct tl_Tx_list) * TL_NBUF, 0, BUS_DMA_WAITOK,
    658 		    &sc->Tx_dmamap);
    659 	if (error == 0)
    660 		error = bus_dmamap_create(sc->tl_dmatag, ETHER_MIN_TX, 1,
    661 		    ETHER_MIN_TX, 0, BUS_DMA_WAITOK,
    662 		    &sc->null_dmamap);
    663 	if (error) {
    664 		errstring = "can't allocate DMA maps for lists";
    665 		goto bad;
    666 	}
    667 	memset(sc->ctrl, 0, PAGE_SIZE);
    668 	sc->hw_Rx_list = (void *)sc->ctrl;
    669 	sc->hw_Tx_list =
    670 	    (void *)(sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF);
    671 	nullbuf = sc->ctrl + sizeof(struct tl_Rx_list) * TL_NBUF +
    672 	    sizeof(struct tl_Tx_list) * TL_NBUF;
    673 	error = bus_dmamap_load(sc->tl_dmatag, sc->Rx_dmamap,
    674 	    sc->hw_Rx_list, sizeof(struct tl_Rx_list) * TL_NBUF, NULL,
    675 	    BUS_DMA_WAITOK);
    676 	if (error == 0)
    677 		error = bus_dmamap_load(sc->tl_dmatag, sc->Tx_dmamap,
    678 		    sc->hw_Tx_list, sizeof(struct tl_Tx_list) * TL_NBUF, NULL,
    679 		    BUS_DMA_WAITOK);
    680 	if (error == 0)
    681 		error = bus_dmamap_load(sc->tl_dmatag, sc->null_dmamap,
    682 		    nullbuf, ETHER_MIN_TX, NULL, BUS_DMA_WAITOK);
    683 	if (error) {
    684 		errstring = "can't DMA map DMA memory for lists";
    685 		goto bad;
    686 	}
    687 	for (i=0; i< TL_NBUF; i++) {
    688 		error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES,
    689 		    1, MCLBYTES, 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
    690 		    &sc->Rx_list[i].m_dmamap);
    691 		if (error == 0) {
    692 			error = bus_dmamap_create(sc->tl_dmatag, MCLBYTES,
    693 			    TL_NSEG, MCLBYTES, 0,
    694 			    BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
    695 			    &sc->Tx_list[i].m_dmamap);
    696 		}
    697 		if (error) {
    698 			errstring = "can't allocate DMA maps for mbufs";
    699 			goto bad;
    700 		}
    701 		sc->Rx_list[i].hw_list = &sc->hw_Rx_list[i];
    702 		sc->Rx_list[i].hw_listaddr = sc->Rx_dmamap->dm_segs[0].ds_addr
    703 		    + sizeof(struct tl_Rx_list) * i;
    704 		sc->Tx_list[i].hw_list = &sc->hw_Tx_list[i];
    705 		sc->Tx_list[i].hw_listaddr = sc->Tx_dmamap->dm_segs[0].ds_addr
    706 		    + sizeof(struct tl_Tx_list) * i;
    707 		if (tl_add_RxBuff(sc, &sc->Rx_list[i], NULL) == 0) {
    708 			errstring = "out of mbuf for receive list";
    709 			error = ENOMEM;
    710 			goto bad;
    711 		}
    712 		if (i > 0) { /* chain the list */
    713 			sc->Rx_list[i - 1].next = &sc->Rx_list[i];
    714 			sc->hw_Rx_list[i - 1].fwd =
    715 			    htole32(sc->Rx_list[i].hw_listaddr);
    716 			sc->Tx_list[i - 1].next = &sc->Tx_list[i];
    717 		}
    718 	}
    719 	sc->hw_Rx_list[TL_NBUF - 1].fwd = 0;
    720 	sc->Rx_list[TL_NBUF - 1].next = NULL;
    721 	sc->hw_Tx_list[TL_NBUF - 1].fwd = 0;
    722 	sc->Tx_list[TL_NBUF - 1].next = NULL;
    723 
    724 	sc->active_Rx = &sc->Rx_list[0];
    725 	sc->last_Rx   = &sc->Rx_list[TL_NBUF - 1];
    726 	sc->active_Tx = sc->last_Tx = NULL;
    727 	sc->Free_Tx   = &sc->Tx_list[0];
    728 	bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
    729 	    sizeof(struct tl_Rx_list) * TL_NBUF,
    730 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    731 	bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
    732 	    sizeof(struct tl_Tx_list) * TL_NBUF,
    733 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    734 	bus_dmamap_sync(sc->tl_dmatag, sc->null_dmamap, 0, ETHER_MIN_TX,
    735 	    BUS_DMASYNC_PREWRITE);
    736 
    737 	/* set media */
    738 	mii_mediachg(&sc->tl_mii);
    739 
    740 	/* start ticks calls */
    741 	callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc);
    742 	/* write address of Rx list and enable interrupts */
    743 	TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->Rx_list[0].hw_listaddr);
    744 	TL_HR_WRITE(sc, TL_HOST_CMD,
    745 	    HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | HOST_CMD_IntOn);
    746 	sc->tl_if.if_flags |= IFF_RUNNING;
    747 	sc->tl_if.if_flags &= ~IFF_OACTIVE;
    748 	return 0;
    749 bad:
    750 	printf("%s: %s\n", sc->sc_dev.dv_xname, errstring);
    751 	splx(s);
    752 	return error;
    753 }
    754 
    755 
    756 static u_int32_t
    757 tl_intreg_read(sc, reg)
    758 	tl_softc_t *sc;
    759 	u_int32_t reg;
    760 {
    761 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
    762 	return TL_HR_READ(sc, TL_HOST_DIO_DATA);
    763 }
    764 
    765 static u_int8_t
    766 tl_intreg_read_byte(sc, reg)
    767 	tl_softc_t *sc;
    768 	u_int32_t reg;
    769 {
    770 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
    771 	    (reg & (~0x07)) & TL_HOST_DIOADR_MASK);
    772 	return TL_HR_READ_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x07));
    773 }
    774 
    775 static void
    776 tl_intreg_write(sc, reg, val)
    777 	tl_softc_t *sc;
    778 	u_int32_t reg;
    779 	u_int32_t val;
    780 {
    781 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR, reg & TL_HOST_DIOADR_MASK);
    782 	TL_HR_WRITE(sc, TL_HOST_DIO_DATA, val);
    783 }
    784 
    785 static void
    786 tl_intreg_write_byte(sc, reg, val)
    787 	tl_softc_t *sc;
    788 	u_int32_t reg;
    789 	u_int8_t val;
    790 {
    791 	TL_HR_WRITE(sc, TL_HOST_INTR_DIOADR,
    792 	    (reg & (~0x03)) & TL_HOST_DIOADR_MASK);
    793 	TL_HR_WRITE_BYTE(sc, TL_HOST_DIO_DATA + (reg & 0x03), val);
    794 }
    795 
    796 void
    797 tl_mii_sync(sc)
    798 	struct tl_softc *sc;
    799 {
    800 	int i;
    801 
    802 	netsio_clr(sc, TL_NETSIO_MTXEN);
    803 	for (i = 0; i < 32; i++) {
    804 		netsio_clr(sc, TL_NETSIO_MCLK);
    805 		netsio_set(sc, TL_NETSIO_MCLK);
    806 	}
    807 }
    808 
    809 void
    810 tl_mii_sendbits(sc, data, nbits)
    811 	struct tl_softc *sc;
    812 	u_int32_t data;
    813 	int nbits;
    814 {
    815 	int i;
    816 
    817 	netsio_set(sc, TL_NETSIO_MTXEN);
    818 	for (i = 1 << (nbits - 1); i; i = i >>  1) {
    819 		netsio_clr(sc, TL_NETSIO_MCLK);
    820 		netsio_read(sc, TL_NETSIO_MCLK);
    821 		if (data & i)
    822 			netsio_set(sc, TL_NETSIO_MDATA);
    823 		else
    824 			netsio_clr(sc, TL_NETSIO_MDATA);
    825 		netsio_set(sc, TL_NETSIO_MCLK);
    826 		netsio_read(sc, TL_NETSIO_MCLK);
    827 	}
    828 }
    829 
    830 int
    831 tl_mii_read(self, phy, reg)
    832 	struct device *self;
    833 	int phy, reg;
    834 {
    835 	struct tl_softc *sc = (struct tl_softc *)self;
    836 	int val = 0, i, err;
    837 
    838 	/*
    839 	 * Read the PHY register by manually driving the MII control lines.
    840 	 */
    841 
    842 	tl_mii_sync(sc);
    843 	tl_mii_sendbits(sc, MII_COMMAND_START, 2);
    844 	tl_mii_sendbits(sc, MII_COMMAND_READ, 2);
    845 	tl_mii_sendbits(sc, phy, 5);
    846 	tl_mii_sendbits(sc, reg, 5);
    847 
    848 	netsio_clr(sc, TL_NETSIO_MTXEN);
    849 	netsio_clr(sc, TL_NETSIO_MCLK);
    850 	netsio_set(sc, TL_NETSIO_MCLK);
    851 	netsio_clr(sc, TL_NETSIO_MCLK);
    852 
    853 	err = netsio_read(sc, TL_NETSIO_MDATA);
    854 	netsio_set(sc, TL_NETSIO_MCLK);
    855 
    856 	/* Even if an error occurs, must still clock out the cycle. */
    857 	for (i = 0; i < 16; i++) {
    858 		val <<= 1;
    859 		netsio_clr(sc, TL_NETSIO_MCLK);
    860 		if (err == 0 && netsio_read(sc, TL_NETSIO_MDATA))
    861 			val |= 1;
    862 		netsio_set(sc, TL_NETSIO_MCLK);
    863 	}
    864 	netsio_clr(sc, TL_NETSIO_MCLK);
    865 	netsio_set(sc, TL_NETSIO_MCLK);
    866 
    867 	return (err ? 0 : val);
    868 }
    869 
    870 void
    871 tl_mii_write(self, phy, reg, val)
    872 	struct device *self;
    873 	int phy, reg, val;
    874 {
    875 	struct tl_softc *sc = (struct tl_softc *)self;
    876 
    877 	/*
    878 	 * Write the PHY register by manually driving the MII control lines.
    879 	 */
    880 
    881 	tl_mii_sync(sc);
    882 	tl_mii_sendbits(sc, MII_COMMAND_START, 2);
    883 	tl_mii_sendbits(sc, MII_COMMAND_WRITE, 2);
    884 	tl_mii_sendbits(sc, phy, 5);
    885 	tl_mii_sendbits(sc, reg, 5);
    886 	tl_mii_sendbits(sc, MII_COMMAND_ACK, 2);
    887 	tl_mii_sendbits(sc, val, 16);
    888 
    889 	netsio_clr(sc, TL_NETSIO_MCLK);
    890 	netsio_set(sc, TL_NETSIO_MCLK);
    891 }
    892 
    893 void
    894 tl_statchg(self)
    895 	struct device *self;
    896 {
    897 	tl_softc_t *sc = (struct tl_softc *)self;
    898 	u_int32_t reg;
    899 
    900 #ifdef TLDEBUG
    901 	printf("tl_statchg, media %x\n", sc->tl_mii.mii_media.ifm_media);
    902 #endif
    903 
    904 	/*
    905 	 * We must keep the ThunderLAN and the PHY in sync as
    906 	 * to the status of full-duplex!
    907 	 */
    908 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetCmd);
    909 	if (sc->tl_mii.mii_media_active & IFM_FDX)
    910 		reg |= TL_NETCOMMAND_DUPLEX;
    911 	else
    912 		reg &= ~TL_NETCOMMAND_DUPLEX;
    913 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetCmd, reg);
    914 }
    915 
    916 /********** I2C glue **********/
    917 
    918 static int
    919 tl_i2c_acquire_bus(void *cookie, int flags)
    920 {
    921 
    922 	/* private bus */
    923 	return (0);
    924 }
    925 
    926 static void
    927 tl_i2c_release_bus(void *cookie, int flags)
    928 {
    929 
    930 	/* private bus */
    931 }
    932 
    933 static int
    934 tl_i2c_send_start(void *cookie, int flags)
    935 {
    936 
    937 	return (i2c_bitbang_send_start(cookie, flags, &tl_i2cbb_ops));
    938 }
    939 
    940 static int
    941 tl_i2c_send_stop(void *cookie, int flags)
    942 {
    943 
    944 	return (i2c_bitbang_send_stop(cookie, flags, &tl_i2cbb_ops));
    945 }
    946 
    947 static int
    948 tl_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    949 {
    950 
    951 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags, &tl_i2cbb_ops));
    952 }
    953 
    954 static int
    955 tl_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    956 {
    957 
    958 	return (i2c_bitbang_read_byte(cookie, valp, flags, &tl_i2cbb_ops));
    959 }
    960 
    961 static int
    962 tl_i2c_write_byte(void *cookie, uint8_t val, int flags)
    963 {
    964 
    965 	return (i2c_bitbang_write_byte(cookie, val, flags, &tl_i2cbb_ops));
    966 }
    967 
    968 /********** I2C bit-bang glue **********/
    969 
    970 static void
    971 tl_i2cbb_set_bits(void *cookie, uint32_t bits)
    972 {
    973 	struct tl_softc *sc = cookie;
    974 	uint8_t reg;
    975 
    976 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio);
    977 	reg = (reg & ~(TL_NETSIO_EDATA|TL_NETSIO_ECLOCK)) | bits;
    978 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, reg);
    979 }
    980 
    981 static void
    982 tl_i2cbb_set_dir(void *cookie, uint32_t bits)
    983 {
    984 	struct tl_softc *sc = cookie;
    985 	uint8_t reg;
    986 
    987 	reg = tl_intreg_read_byte(sc, TL_INT_NET + TL_INT_NetSio);
    988 	reg = (reg & ~TL_NETSIO_ETXEN) | bits;
    989 	tl_intreg_write_byte(sc, TL_INT_NET + TL_INT_NetSio, reg);
    990 }
    991 
    992 static uint32_t
    993 tl_i2cbb_read(void *cookie)
    994 {
    995 
    996 	return (tl_intreg_read_byte(cookie, TL_INT_NET + TL_INT_NetSio));
    997 }
    998 
    999 /********** End of I2C stuff **********/
   1000 
   1001 static int
   1002 tl_intr(v)
   1003 	void *v;
   1004 {
   1005 	tl_softc_t *sc = v;
   1006 	struct ifnet *ifp = &sc->tl_if;
   1007 	struct Rx_list *Rx;
   1008 	struct Tx_list *Tx;
   1009 	struct mbuf *m;
   1010 	u_int32_t int_type, int_reg;
   1011 	int ack = 0;
   1012 	int size;
   1013 
   1014 	int_reg = TL_HR_READ(sc, TL_HOST_INTR_DIOADR);
   1015 	int_type = int_reg  & TL_INTR_MASK;
   1016 	if (int_type == 0)
   1017 		return 0;
   1018 #if defined(TLDEBUG_RX) || defined(TLDEBUG_TX)
   1019 	printf("%s: interrupt type %x, intr_reg %x\n", sc->sc_dev.dv_xname,
   1020 	    int_type, int_reg);
   1021 #endif
   1022 	/* disable interrupts */
   1023 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOff);
   1024 	switch(int_type & TL_INTR_MASK) {
   1025 	case TL_INTR_RxEOF:
   1026 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
   1027 		    sizeof(struct tl_Rx_list) * TL_NBUF,
   1028 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1029 		while(le32toh(sc->active_Rx->hw_list->stat) &
   1030 		    TL_RX_CSTAT_CPLT) {
   1031 			/* dequeue and requeue at end of list */
   1032 			ack++;
   1033 			Rx = sc->active_Rx;
   1034 			sc->active_Rx = Rx->next;
   1035 			bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0,
   1036 			    Rx->m_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1037 			bus_dmamap_unload(sc->tl_dmatag, Rx->m_dmamap);
   1038 			m = Rx->m;
   1039 			size = le32toh(Rx->hw_list->stat) >> 16;
   1040 #ifdef TLDEBUG_RX
   1041 			printf("tl_intr: RX list complete, Rx %p, size=%d\n",
   1042 			    Rx, size);
   1043 #endif
   1044 			if (tl_add_RxBuff(sc, Rx, m ) == 0) {
   1045 				/*
   1046 				 * No new mbuf, reuse the same. This means
   1047 				 * that this packet
   1048 				 * is lost
   1049 				 */
   1050 				m = NULL;
   1051 #ifdef TL_PRIV_STATS
   1052 				sc->ierr_nomem++;
   1053 #endif
   1054 #ifdef TLDEBUG
   1055 				printf("%s: out of mbuf, lost input packet\n",
   1056 				    sc->sc_dev.dv_xname);
   1057 #endif
   1058 			}
   1059 			Rx->next = NULL;
   1060 			Rx->hw_list->fwd = 0;
   1061 			sc->last_Rx->hw_list->fwd = htole32(Rx->hw_listaddr);
   1062 			sc->last_Rx->next = Rx;
   1063 			sc->last_Rx = Rx;
   1064 
   1065 			/* deliver packet */
   1066 			if (m) {
   1067 				if (size < sizeof(struct ether_header)) {
   1068 					m_freem(m);
   1069 					continue;
   1070 				}
   1071 				m->m_pkthdr.rcvif = ifp;
   1072 				m->m_pkthdr.len = m->m_len = size;
   1073 #ifdef TLDEBUG_RX
   1074 				{ struct ether_header *eh =
   1075 				    mtod(m, struct ether_header *);
   1076 				printf("tl_intr: Rx packet:\n");
   1077 				ether_printheader(eh); }
   1078 #endif
   1079 #if NBPFILTER > 0
   1080 				if (ifp->if_bpf)
   1081 					bpf_mtap(ifp->if_bpf, m);
   1082 #endif /* NBPFILTER > 0 */
   1083 				(*ifp->if_input)(ifp, m);
   1084 			}
   1085 		}
   1086 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
   1087 		    sizeof(struct tl_Rx_list) * TL_NBUF,
   1088 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1089 #ifdef TLDEBUG_RX
   1090 		printf("TL_INTR_RxEOF: ack %d\n", ack);
   1091 #else
   1092 		if (ack == 0) {
   1093 			printf("%s: EOF intr without anything to read !\n",
   1094 			    sc->sc_dev.dv_xname);
   1095 			tl_reset(sc);
   1096 			/* shedule reinit of the board */
   1097 			callout_reset(&sc->tl_restart_ch, 1, tl_restart, ifp);
   1098 			return(1);
   1099 		}
   1100 #endif
   1101 		break;
   1102 	case TL_INTR_RxEOC:
   1103 		ack++;
   1104 		bus_dmamap_sync(sc->tl_dmatag, sc->Rx_dmamap, 0,
   1105 		    sizeof(struct tl_Rx_list) * TL_NBUF,
   1106 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1107 #ifdef TLDEBUG_RX
   1108 		printf("TL_INTR_RxEOC: ack %d\n", ack);
   1109 #endif
   1110 #ifdef DIAGNOSTIC
   1111 		if (le32toh(sc->active_Rx->hw_list->stat) & TL_RX_CSTAT_CPLT) {
   1112 			printf("%s: Rx EOC interrupt and active Tx list not "
   1113 			    "cleared\n", sc->sc_dev.dv_xname);
   1114 			return 0;
   1115 		} else
   1116 #endif
   1117 		{
   1118 		/*
   1119 		 * write address of Rx list and send Rx GO command, ack
   1120 		 * interrupt and enable interrupts in one command
   1121 		 */
   1122 		TL_HR_WRITE(sc, TL_HOST_CH_PARM, sc->active_Rx->hw_listaddr);
   1123 		TL_HR_WRITE(sc, TL_HOST_CMD,
   1124 		    HOST_CMD_GO | HOST_CMD_RT | HOST_CMD_Nes | ack | int_type |
   1125 		    HOST_CMD_ACK | HOST_CMD_IntOn);
   1126 		return 1;
   1127 		}
   1128 	case TL_INTR_TxEOF:
   1129 	case TL_INTR_TxEOC:
   1130 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
   1131 		    sizeof(struct tl_Tx_list) * TL_NBUF,
   1132 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1133 		while ((Tx = sc->active_Tx) != NULL) {
   1134 			if((le32toh(Tx->hw_list->stat) & TL_TX_CSTAT_CPLT) == 0)
   1135 				break;
   1136 			ack++;
   1137 #ifdef TLDEBUG_TX
   1138 			printf("TL_INTR_TxEOC: list 0x%x done\n",
   1139 			    (int)Tx->hw_listaddr);
   1140 #endif
   1141 			Tx->hw_list->stat = 0;
   1142 			bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0,
   1143 			    Tx->m_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1144 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
   1145 			m_freem(Tx->m);
   1146 			Tx->m = NULL;
   1147 			sc->active_Tx = Tx->next;
   1148 			if (sc->active_Tx == NULL)
   1149 				sc->last_Tx = NULL;
   1150 			Tx->next = sc->Free_Tx;
   1151 			sc->Free_Tx = Tx;
   1152 		}
   1153 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
   1154 		    sizeof(struct tl_Tx_list) * TL_NBUF,
   1155 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1156 		/* if this was an EOC, ACK immediatly */
   1157 		if (ack)
   1158 			sc->tl_if.if_flags &= ~IFF_OACTIVE;
   1159 		if (int_type == TL_INTR_TxEOC) {
   1160 #ifdef TLDEBUG_TX
   1161 			printf("TL_INTR_TxEOC: ack %d (will be set to 1)\n",
   1162 			    ack);
   1163 #endif
   1164 			TL_HR_WRITE(sc, TL_HOST_CMD, 1 | int_type |
   1165 			    HOST_CMD_ACK | HOST_CMD_IntOn);
   1166 			if ( sc->active_Tx != NULL) {
   1167 				/* needs a Tx go command */
   1168 				TL_HR_WRITE(sc, TL_HOST_CH_PARM,
   1169 				    sc->active_Tx->hw_listaddr);
   1170 				TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
   1171 			}
   1172 			sc->tl_if.if_timer = 0;
   1173 			if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0)
   1174 				tl_ifstart(&sc->tl_if);
   1175 			return 1;
   1176 		}
   1177 #ifdef TLDEBUG
   1178 		else {
   1179 			printf("TL_INTR_TxEOF: ack %d\n", ack);
   1180 		}
   1181 #endif
   1182 		sc->tl_if.if_timer = 0;
   1183 		if (IFQ_IS_EMPTY(&sc->tl_if.if_snd) == 0)
   1184 			tl_ifstart(&sc->tl_if);
   1185 		break;
   1186 	case TL_INTR_Stat:
   1187 		ack++;
   1188 #ifdef TLDEBUG
   1189 		printf("TL_INTR_Stat: ack %d\n", ack);
   1190 #endif
   1191 		tl_read_stats(sc);
   1192 		break;
   1193 	case TL_INTR_Adc:
   1194 		if (int_reg & TL_INTVec_MASK) {
   1195 			/* adapter check conditions */
   1196 			printf("%s: check condition, intvect=0x%x, "
   1197 			    "ch_param=0x%x\n", sc->sc_dev.dv_xname,
   1198 			    int_reg & TL_INTVec_MASK,
   1199 			    TL_HR_READ(sc, TL_HOST_CH_PARM));
   1200 			tl_reset(sc);
   1201 			/* shedule reinit of the board */
   1202 			callout_reset(&sc->tl_restart_ch, 1, tl_restart, ifp);
   1203 			return(1);
   1204 		} else {
   1205 			u_int8_t netstat;
   1206 			/* Network status */
   1207 			netstat =
   1208 			    tl_intreg_read_byte(sc, TL_INT_NET+TL_INT_NetSts);
   1209 			printf("%s: network status, NetSts=%x\n",
   1210 			    sc->sc_dev.dv_xname, netstat);
   1211 			/* Ack interrupts */
   1212 			tl_intreg_write_byte(sc, TL_INT_NET+TL_INT_NetSts,
   1213 			    netstat);
   1214 			ack++;
   1215 		}
   1216 		break;
   1217 	default:
   1218 		printf("%s: unhandled interrupt code %x!\n",
   1219 		    sc->sc_dev.dv_xname, int_type);
   1220 		ack++;
   1221 	}
   1222 
   1223 	if (ack) {
   1224 		/* Ack the interrupt and enable interrupts */
   1225 		TL_HR_WRITE(sc, TL_HOST_CMD, ack | int_type | HOST_CMD_ACK |
   1226 		    HOST_CMD_IntOn);
   1227 #if NRND > 0
   1228 		if (RND_ENABLED(&sc->rnd_source))
   1229 			rnd_add_uint32(&sc->rnd_source, int_reg);
   1230 #endif
   1231 		return 1;
   1232 	}
   1233 	/* ack = 0 ; interrupt was perhaps not our. Just enable interrupts */
   1234 	TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_IntOn);
   1235 	return 0;
   1236 }
   1237 
   1238 static int
   1239 tl_ifioctl(ifp, cmd, data)
   1240 	struct ifnet *ifp;
   1241 	ioctl_cmd_t cmd;
   1242 	caddr_t data;
   1243 {
   1244 	struct tl_softc *sc = ifp->if_softc;
   1245 	struct ifreq *ifr = (struct ifreq *)data;
   1246 	int s, error;
   1247 
   1248 	s = splnet();
   1249 	switch(cmd) {
   1250 	case SIOCSIFMEDIA:
   1251 	case SIOCGIFMEDIA:
   1252 		error = ifmedia_ioctl(ifp, ifr, &sc->tl_mii.mii_media, cmd);
   1253 		break;
   1254 	default:
   1255 		error = ether_ioctl(ifp, cmd, data);
   1256 		if (error == ENETRESET) {
   1257 			if (ifp->if_flags & IFF_RUNNING)
   1258 				tl_addr_filter(sc);
   1259 			error = 0;
   1260 		}
   1261 	}
   1262 	splx(s);
   1263 	return error;
   1264 }
   1265 
   1266 static void
   1267 tl_ifstart(ifp)
   1268 	struct ifnet *ifp;
   1269 {
   1270 	tl_softc_t *sc = ifp->if_softc;
   1271 	struct mbuf *mb_head;
   1272 	struct Tx_list *Tx;
   1273 	int segment, size;
   1274 	int again, error;
   1275 
   1276 	if ((sc->tl_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
   1277 		return;
   1278 txloop:
   1279 	/* If we don't have more space ... */
   1280 	if (sc->Free_Tx == NULL) {
   1281 #ifdef TLDEBUG
   1282 		printf("tl_ifstart: No free TX list\n");
   1283 #endif
   1284 		sc->tl_if.if_flags |= IFF_OACTIVE;
   1285 		return;
   1286 	}
   1287 	/* Grab a paquet for output */
   1288 	IFQ_DEQUEUE(&ifp->if_snd, mb_head);
   1289 	if (mb_head == NULL) {
   1290 #ifdef TLDEBUG_TX
   1291 		printf("tl_ifstart: nothing to send\n");
   1292 #endif
   1293 		return;
   1294 	}
   1295 	Tx = sc->Free_Tx;
   1296 	sc->Free_Tx = Tx->next;
   1297 	Tx->next = NULL;
   1298 	again = 0;
   1299 	/*
   1300 	 * Go through each of the mbufs in the chain and initialize
   1301 	 * the transmit list descriptors with the physical address
   1302 	 * and size of the mbuf.
   1303 	 */
   1304 tbdinit:
   1305 	memset(Tx->hw_list, 0, sizeof(struct tl_Tx_list));
   1306 	Tx->m = mb_head;
   1307 	size = mb_head->m_pkthdr.len;
   1308 	if ((error = bus_dmamap_load_mbuf(sc->tl_dmatag, Tx->m_dmamap, mb_head,
   1309 	    BUS_DMA_NOWAIT)) || (size < ETHER_MIN_TX &&
   1310 	    Tx->m_dmamap->dm_nsegs == TL_NSEG)) {
   1311 		struct mbuf *mn;
   1312 		/*
   1313 		 * We ran out of segments, or we will. We have to recopy this
   1314 		 * mbuf chain first.
   1315 		 */
   1316 		 if (error == 0)
   1317 			bus_dmamap_unload(sc->tl_dmatag, Tx->m_dmamap);
   1318 		 if (again) {
   1319 			/* already copyed, can't do much more */
   1320 			m_freem(mb_head);
   1321 			goto bad;
   1322 		}
   1323 		again = 1;
   1324 #ifdef TLDEBUG_TX
   1325 		printf("tl_ifstart: need to copy mbuf\n");
   1326 #endif
   1327 #ifdef TL_PRIV_STATS
   1328 		sc->oerr_mcopy++;
   1329 #endif
   1330 		MGETHDR(mn, M_DONTWAIT, MT_DATA);
   1331 		if (mn == NULL) {
   1332 			m_freem(mb_head);
   1333 			goto bad;
   1334 		}
   1335 		if (mb_head->m_pkthdr.len > MHLEN) {
   1336 			MCLGET(mn, M_DONTWAIT);
   1337 			if ((mn->m_flags & M_EXT) == 0) {
   1338 				m_freem(mn);
   1339 				m_freem(mb_head);
   1340 				goto bad;
   1341 			}
   1342 		}
   1343 		m_copydata(mb_head, 0, mb_head->m_pkthdr.len,
   1344 		    mtod(mn, caddr_t));
   1345 		mn->m_pkthdr.len = mn->m_len = mb_head->m_pkthdr.len;
   1346 		m_freem(mb_head);
   1347 		mb_head = mn;
   1348 		goto tbdinit;
   1349 	}
   1350 	for (segment = 0; segment < Tx->m_dmamap->dm_nsegs; segment++) {
   1351 		Tx->hw_list->seg[segment].data_addr =
   1352 		    htole32(Tx->m_dmamap->dm_segs[segment].ds_addr);
   1353 		Tx->hw_list->seg[segment].data_count =
   1354 		    htole32(Tx->m_dmamap->dm_segs[segment].ds_len);
   1355 	}
   1356 	bus_dmamap_sync(sc->tl_dmatag, Tx->m_dmamap, 0,
   1357 	    Tx->m_dmamap->dm_mapsize,
   1358 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1359 	/* We are at end of mbuf chain. check the size and
   1360 	 * see if it needs to be extended
   1361 	 */
   1362 	if (size < ETHER_MIN_TX) {
   1363 #ifdef DIAGNOSTIC
   1364 		if (segment >= TL_NSEG) {
   1365 			panic("tl_ifstart: to much segmets (%d)", segment);
   1366 		}
   1367 #endif
   1368 		/*
   1369 	 	 * add the nullbuf in the seg
   1370 	 	 */
   1371 		Tx->hw_list->seg[segment].data_count =
   1372 		    htole32(ETHER_MIN_TX - size);
   1373 		Tx->hw_list->seg[segment].data_addr =
   1374 		    htole32(sc->null_dmamap->dm_segs[0].ds_addr);
   1375 		size = ETHER_MIN_TX;
   1376 		segment++;
   1377 	}
   1378 	/* The list is done, finish the list init */
   1379 	Tx->hw_list->seg[segment - 1].data_count |=
   1380 	    htole32(TL_LAST_SEG);
   1381 	Tx->hw_list->stat = htole32((size << 16) | 0x3000);
   1382 #ifdef TLDEBUG_TX
   1383 	printf("%s: sending, Tx : stat = 0x%x\n", sc->sc_dev.dv_xname,
   1384 	    le32toh(Tx->hw_list->stat));
   1385 #if 0
   1386 	for(segment = 0; segment < TL_NSEG; segment++) {
   1387 		printf("    seg %d addr 0x%x len 0x%x\n",
   1388 		    segment,
   1389 		    le32toh(Tx->hw_list->seg[segment].data_addr),
   1390 		    le32toh(Tx->hw_list->seg[segment].data_count));
   1391 	}
   1392 #endif
   1393 #endif
   1394 	if (sc->active_Tx == NULL) {
   1395 		sc->active_Tx = sc->last_Tx = Tx;
   1396 #ifdef TLDEBUG_TX
   1397 		printf("%s: Tx GO, addr=0x%ux\n", sc->sc_dev.dv_xname,
   1398 		    (int)Tx->hw_listaddr);
   1399 #endif
   1400 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
   1401 		    sizeof(struct tl_Tx_list) * TL_NBUF,
   1402 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1403 		TL_HR_WRITE(sc, TL_HOST_CH_PARM, Tx->hw_listaddr);
   1404 		TL_HR_WRITE(sc, TL_HOST_CMD, HOST_CMD_GO);
   1405 	} else {
   1406 #ifdef TLDEBUG_TX
   1407 		printf("%s: Tx addr=0x%ux queued\n", sc->sc_dev.dv_xname,
   1408 		    (int)Tx->hw_listaddr);
   1409 #endif
   1410 		sc->last_Tx->hw_list->fwd = htole32(Tx->hw_listaddr);
   1411 		bus_dmamap_sync(sc->tl_dmatag, sc->Tx_dmamap, 0,
   1412 		    sizeof(struct tl_Tx_list) * TL_NBUF,
   1413 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1414 		sc->last_Tx->next = Tx;
   1415 		sc->last_Tx = Tx;
   1416 #ifdef DIAGNOSTIC
   1417 		if (sc->last_Tx->hw_list->fwd & 0x7)
   1418 			printf("%s: physical addr 0x%x of list not properly "
   1419 			   "aligned\n",
   1420 			   sc->sc_dev.dv_xname, sc->last_Rx->hw_list->fwd);
   1421 #endif
   1422 	}
   1423 #if NBPFILTER > 0
   1424 	/* Pass packet to bpf if there is a listener */
   1425 	if (ifp->if_bpf)
   1426 		bpf_mtap(ifp->if_bpf, mb_head);
   1427 #endif
   1428 	/*
   1429 	 * Set a 5 second timer just in case we don't hear from the card again.
   1430 	 */
   1431 	ifp->if_timer = 5;
   1432 	goto txloop;
   1433 bad:
   1434 #ifdef TLDEBUG
   1435 	printf("tl_ifstart: Out of mbuf, Tx pkt lost\n");
   1436 #endif
   1437 	Tx->next = sc->Free_Tx;
   1438 	sc->Free_Tx = Tx;
   1439 	return;
   1440 }
   1441 
   1442 static void
   1443 tl_ifwatchdog(ifp)
   1444 	struct ifnet *ifp;
   1445 {
   1446 	tl_softc_t *sc = ifp->if_softc;
   1447 
   1448 	if ((ifp->if_flags & IFF_RUNNING) == 0)
   1449 		return;
   1450 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1451 	ifp->if_oerrors++;
   1452 	tl_init(ifp);
   1453 }
   1454 
   1455 static int
   1456 tl_mediachange(ifp)
   1457 	struct ifnet *ifp;
   1458 {
   1459 
   1460 	if (ifp->if_flags & IFF_UP)
   1461 		tl_init(ifp);
   1462 	return (0);
   1463 }
   1464 
   1465 static void
   1466 tl_mediastatus(ifp, ifmr)
   1467 	struct ifnet *ifp;
   1468 	struct ifmediareq *ifmr;
   1469 {
   1470 	tl_softc_t *sc = ifp->if_softc;
   1471 
   1472 	mii_pollstat(&sc->tl_mii);
   1473 	ifmr->ifm_active = sc->tl_mii.mii_media_active;
   1474 	ifmr->ifm_status = sc->tl_mii.mii_media_status;
   1475 }
   1476 
   1477 static int tl_add_RxBuff(sc, Rx, oldm)
   1478 	tl_softc_t *sc;
   1479 	struct Rx_list *Rx;
   1480 	struct mbuf *oldm;
   1481 {
   1482 	struct mbuf *m;
   1483 	int error;
   1484 
   1485 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1486 	if (m != NULL) {
   1487 		MCLGET(m, M_DONTWAIT);
   1488 		if ((m->m_flags & M_EXT) == 0) {
   1489 			m_freem(m);
   1490 			if (oldm == NULL)
   1491 				return 0;
   1492 			m = oldm;
   1493 			m->m_data = m->m_ext.ext_buf;
   1494 		}
   1495 	} else {
   1496 		if (oldm == NULL)
   1497 			return 0;
   1498 		m = oldm;
   1499 		m->m_data = m->m_ext.ext_buf;
   1500 	}
   1501 
   1502 	/* (re)init the Rx_list struct */
   1503 
   1504 	Rx->m = m;
   1505 	if ((error = bus_dmamap_load(sc->tl_dmatag, Rx->m_dmamap,
   1506 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL, BUS_DMA_NOWAIT)) != 0) {
   1507 		printf("%s: bus_dmamap_load() failed (error %d) for "
   1508 		    "tl_add_RxBuff\n", sc->sc_dev.dv_xname, error);
   1509 		printf("size %d (%d)\n", m->m_pkthdr.len, MCLBYTES);
   1510 		m_freem(m);
   1511 		Rx->m = NULL;
   1512 		return 0;
   1513 	}
   1514 	bus_dmamap_sync(sc->tl_dmatag, Rx->m_dmamap, 0,
   1515 	    Rx->m_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1516 	/*
   1517 	 * Move the data pointer up so that the incoming data packet
   1518 	 * will be 32-bit aligned.
   1519 	 */
   1520 	m->m_data += 2;
   1521 
   1522 	Rx->hw_list->stat =
   1523 	    htole32(((Rx->m_dmamap->dm_segs[0].ds_len - 2) << 16) | 0x3000);
   1524 	Rx->hw_list->seg.data_count =
   1525 	    htole32(Rx->m_dmamap->dm_segs[0].ds_len - 2);
   1526 	Rx->hw_list->seg.data_addr =
   1527 	    htole32(Rx->m_dmamap->dm_segs[0].ds_addr + 2);
   1528 	return (m != oldm);
   1529 }
   1530 
   1531 static void tl_ticks(v)
   1532 	void *v;
   1533 {
   1534 	tl_softc_t *sc = v;
   1535 
   1536 	tl_read_stats(sc);
   1537 
   1538 	/* Tick the MII. */
   1539 	mii_tick(&sc->tl_mii);
   1540 
   1541 	/* read statistics every seconds */
   1542 	callout_reset(&sc->tl_tick_ch, hz, tl_ticks, sc);
   1543 }
   1544 
   1545 static void
   1546 tl_read_stats(sc)
   1547 	tl_softc_t *sc;
   1548 {
   1549 	u_int32_t reg;
   1550 	int ierr_overr;
   1551 	int ierr_code;
   1552 	int ierr_crc;
   1553 	int oerr_underr;
   1554 	int oerr_deferred;
   1555 	int oerr_coll;
   1556 	int oerr_multicoll;
   1557 	int oerr_exesscoll;
   1558 	int oerr_latecoll;
   1559 	int oerr_carrloss;
   1560 	struct ifnet *ifp = &sc->tl_if;
   1561 
   1562 	reg =  tl_intreg_read(sc, TL_INT_STATS_TX);
   1563 	ifp->if_opackets += reg & 0x00ffffff;
   1564 	oerr_underr = reg >> 24;
   1565 
   1566 	reg =  tl_intreg_read(sc, TL_INT_STATS_RX);
   1567 	ifp->if_ipackets += reg & 0x00ffffff;
   1568 	ierr_overr = reg >> 24;
   1569 
   1570 	reg =  tl_intreg_read(sc, TL_INT_STATS_FERR);
   1571 	ierr_crc = (reg & TL_FERR_CRC) >> 16;
   1572 	ierr_code = (reg & TL_FERR_CODE) >> 24;
   1573 	oerr_deferred = (reg & TL_FERR_DEF);
   1574 
   1575 	reg =  tl_intreg_read(sc, TL_INT_STATS_COLL);
   1576 	oerr_multicoll = (reg & TL_COL_MULTI);
   1577 	oerr_coll = (reg & TL_COL_SINGLE) >> 16;
   1578 
   1579 	reg =  tl_intreg_read(sc, TL_INT_LERR);
   1580 	oerr_exesscoll = (reg & TL_LERR_ECOLL);
   1581 	oerr_latecoll = (reg & TL_LERR_LCOLL) >> 8;
   1582 	oerr_carrloss = (reg & TL_LERR_CL) >> 16;
   1583 
   1584 
   1585 	ifp->if_oerrors += oerr_underr + oerr_exesscoll + oerr_latecoll +
   1586 	   oerr_carrloss;
   1587 	ifp->if_collisions += oerr_coll + oerr_multicoll;
   1588 	ifp->if_ierrors += ierr_overr + ierr_code + ierr_crc;
   1589 
   1590 	if (ierr_overr)
   1591 		printf("%s: receiver ring buffer overrun\n",
   1592 		    sc->sc_dev.dv_xname);
   1593 	if (oerr_underr)
   1594 		printf("%s: transmit buffer underrun\n",
   1595 		    sc->sc_dev.dv_xname);
   1596 #ifdef TL_PRIV_STATS
   1597 	sc->ierr_overr		+= ierr_overr;
   1598 	sc->ierr_code		+= ierr_code;
   1599 	sc->ierr_crc		+= ierr_crc;
   1600 	sc->oerr_underr		+= oerr_underr;
   1601 	sc->oerr_deferred	+= oerr_deferred;
   1602 	sc->oerr_coll		+= oerr_coll;
   1603 	sc->oerr_multicoll	+= oerr_multicoll;
   1604 	sc->oerr_exesscoll	+= oerr_exesscoll;
   1605 	sc->oerr_latecoll	+= oerr_latecoll;
   1606 	sc->oerr_carrloss	+= oerr_carrloss;
   1607 #endif
   1608 }
   1609 
   1610 static void tl_addr_filter(sc)
   1611 	tl_softc_t *sc;
   1612 {
   1613 	struct ether_multistep step;
   1614 	struct ether_multi *enm;
   1615 	u_int32_t hash[2] = {0, 0};
   1616 	int i;
   1617 
   1618 	sc->tl_if.if_flags &= ~IFF_ALLMULTI;
   1619 	ETHER_FIRST_MULTI(step, &sc->tl_ec, enm);
   1620 	while (enm != NULL) {
   1621 #ifdef TLDEBUG
   1622 		printf("tl_addr_filter: addrs %s %s\n",
   1623 		   ether_sprintf(enm->enm_addrlo),
   1624 		   ether_sprintf(enm->enm_addrhi));
   1625 #endif
   1626 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) {
   1627 			i = tl_multicast_hash(enm->enm_addrlo);
   1628 			hash[i/32] |= 1 << (i%32);
   1629 		} else {
   1630 			hash[0] = hash[1] = 0xffffffff;
   1631 			sc->tl_if.if_flags |= IFF_ALLMULTI;
   1632 			break;
   1633 		}
   1634 		ETHER_NEXT_MULTI(step, enm);
   1635 	}
   1636 #ifdef TLDEBUG
   1637 	printf("tl_addr_filer: hash1 %x has2 %x\n", hash[0], hash[1]);
   1638 #endif
   1639 	tl_intreg_write(sc, TL_INT_HASH1, hash[0]);
   1640 	tl_intreg_write(sc, TL_INT_HASH2, hash[1]);
   1641 }
   1642 
   1643 static int tl_multicast_hash(a)
   1644 	u_int8_t *a;
   1645 {
   1646 	int hash;
   1647 
   1648 #define DA(addr,bit) (addr[5 - (bit/8)] & (1 << bit%8))
   1649 #define xor8(a,b,c,d,e,f,g,h) (((a != 0) + (b != 0) + (c != 0) + (d != 0) + (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1)
   1650 
   1651 	hash  = xor8( DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30),
   1652 	    DA(a,36), DA(a,42));
   1653 	hash |= xor8( DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31),
   1654 	    DA(a,37), DA(a,43)) << 1;
   1655 	hash |= xor8( DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32),
   1656 	    DA(a,38), DA(a,44)) << 2;
   1657 	hash |= xor8( DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33),
   1658 	    DA(a,39), DA(a,45)) << 3;
   1659 	hash |= xor8( DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34),
   1660 	    DA(a,40), DA(a,46)) << 4;
   1661 	hash |= xor8( DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35),
   1662 	    DA(a,41), DA(a,47)) << 5;
   1663 
   1664 	return hash;
   1665 }
   1666 
   1667 #if defined(TLDEBUG_RX)
   1668 void
   1669 ether_printheader(eh)
   1670 	struct ether_header *eh;
   1671 {
   1672 	u_char *c = (char*)eh;
   1673 	int i;
   1674 	for (i=0; i<sizeof(struct ether_header); i++)
   1675 		printf("%x ", (u_int)c[i]);
   1676 		printf("\n");
   1677 }
   1678 #endif
   1679