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