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