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