Home | History | Annotate | Line # | Download | only in pci
if_ti.c revision 1.24
      1 /* $NetBSD: if_ti.c,v 1.24 2001/06/07 14:35:58 bouyer Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1997, 1998, 1999
      5  *	Bill Paul <wpaul (at) ctr.columbia.edu>.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Bill Paul.
     18  * 4. Neither the name of the author nor the names of any co-contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     32  * THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  *	FreeBSD Id: if_ti.c,v 1.15 1999/08/14 15:45:03 wpaul Exp
     35  */
     36 
     37 /*
     38  * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
     39  * Manuals, sample driver and firmware source kits are available
     40  * from http://www.alteon.com/support/openkits.
     41  *
     42  * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
     43  * Electrical Engineering Department
     44  * Columbia University, New York City
     45  */
     46 
     47 /*
     48  * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
     49  * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
     50  * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
     51  * Tigon supports hardware IP, TCP and UCP checksumming, multicast
     52  * filtering and jumbo (9014 byte) frames. The hardware is largely
     53  * controlled by firmware, which must be loaded into the NIC during
     54  * initialization.
     55  *
     56  * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
     57  * revision, which supports new features such as extended commands,
     58  * extended jumbo receive ring desciptors and a mini receive ring.
     59  *
     60  * Alteon Networks is to be commended for releasing such a vast amount
     61  * of development material for the Tigon NIC without requiring an NDA
     62  * (although they really should have done it a long time ago). With
     63  * any luck, the other vendors will finally wise up and follow Alteon's
     64  * stellar example.
     65  *
     66  * The firmware for the Tigon 1 and 2 NICs is compiled directly into
     67  * this driver by #including it as a C header file. This bloats the
     68  * driver somewhat, but it's the easiest method considering that the
     69  * driver code and firmware code need to be kept in sync. The source
     70  * for the firmware is not provided with the FreeBSD distribution since
     71  * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
     72  *
     73  * The following people deserve special thanks:
     74  * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
     75  *   for testing
     76  * - Raymond Lee of Netgear, for providing a pair of Netgear
     77  *   GA620 Tigon 2 boards for testing
     78  * - Ulf Zimmermann, for bringing the GA620 to my attention and
     79  *   convincing me to write this driver.
     80  * - Andrew Gallatin for providing FreeBSD/Alpha support.
     81  */
     82 
     83 #include "bpfilter.h"
     84 #include "opt_inet.h"
     85 #include "opt_ns.h"
     86 
     87 #include <sys/param.h>
     88 #include <sys/systm.h>
     89 #include <sys/sockio.h>
     90 #include <sys/mbuf.h>
     91 #include <sys/malloc.h>
     92 #include <sys/kernel.h>
     93 #include <sys/socket.h>
     94 #include <sys/queue.h>
     95 #include <sys/device.h>
     96 #include <sys/reboot.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 
    100 #include <net/if.h>
    101 #include <net/if_arp.h>
    102 #include <net/if_ether.h>
    103 #include <net/if_dl.h>
    104 #include <net/if_media.h>
    105 
    106 #if NBPFILTER > 0
    107 #include <net/bpf.h>
    108 #endif
    109 
    110 #ifdef INET
    111 #include <netinet/in.h>
    112 #include <netinet/if_inarp.h>
    113 #include <netinet/in_systm.h>
    114 #include <netinet/ip.h>
    115 #endif
    116 
    117 #ifdef NS
    118 #include <netns/ns.h>
    119 #include <netns/ns_if.h>
    120 #endif
    121 
    122 #include <machine/bus.h>
    123 
    124 #include <dev/pci/pcireg.h>
    125 #include <dev/pci/pcivar.h>
    126 #include <dev/pci/pcidevs.h>
    127 
    128 #include <dev/pci/if_tireg.h>
    129 #include <dev/pci/ti_fw.h>
    130 #include <dev/pci/ti_fw2.h>
    131 
    132 /*
    133  * Various supported device vendors/types and their names.
    134  */
    135 
    136 static const struct ti_type ti_devs[] = {
    137 	{ PCI_VENDOR_ALTEON,	PCI_PRODUCT_ALTEON_ACENIC,
    138 		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
    139 	{ PCI_VENDOR_ALTEON,	PCI_PRODUCT_ALTEON_ACENIC_COPPER,
    140 		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
    141 	{ PCI_VENDOR_3COM,	PCI_PRODUCT_3COM_3C985,
    142 		"3Com 3c985-SX Gigabit Ethernet" },
    143 	{ PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_GA620,
    144 		"Netgear GA620 1000baseSX Gigabit Ethernet" },
    145 	{ PCI_VENDOR_NETGEAR, PCI_PRODUCT_NETGEAR_GA620T,
    146 		"Netgear GA620 1000baseT Gigabit Ethernet" },
    147 	{ PCI_VENDOR_SGI, PCI_PRODUCT_SGI_TIGON,
    148 		"Silicon Graphics Gigabit Ethernet" },
    149 	{ 0, 0, NULL }
    150 };
    151 
    152 static const struct ti_type *ti_type_match __P((struct pci_attach_args *));
    153 static int ti_probe	__P((struct device *, struct cfdata *, void *));
    154 static void ti_attach	__P((struct device *, struct device *, void *));
    155 static void ti_shutdown __P((void *));
    156 static void ti_txeof		__P((struct ti_softc *));
    157 static void ti_rxeof		__P((struct ti_softc *));
    158 
    159 static void ti_stats_update	__P((struct ti_softc *));
    160 static int ti_encap		__P((struct ti_softc *, struct mbuf *,
    161 					u_int32_t *));
    162 
    163 static int ti_intr		__P((void *));
    164 static void ti_start		__P((struct ifnet *));
    165 static int ti_ioctl		__P((struct ifnet *, u_long, caddr_t));
    166 static void ti_init		__P((void *));
    167 static void ti_init2		__P((struct ti_softc *));
    168 static void ti_stop		__P((struct ti_softc *));
    169 static void ti_watchdog		__P((struct ifnet *));
    170 static int ti_ifmedia_upd	__P((struct ifnet *));
    171 static void ti_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
    172 
    173 static u_int32_t ti_eeprom_putbyte	__P((struct ti_softc *, int));
    174 static u_int8_t	ti_eeprom_getbyte	__P((struct ti_softc *,
    175 						int, u_int8_t *));
    176 static int ti_read_eeprom	__P((struct ti_softc *, caddr_t, int, int));
    177 
    178 static void ti_add_mcast	__P((struct ti_softc *, struct ether_addr *));
    179 static void ti_del_mcast	__P((struct ti_softc *, struct ether_addr *));
    180 static void ti_setmulti		__P((struct ti_softc *));
    181 
    182 static void ti_mem		__P((struct ti_softc *, u_int32_t,
    183 					u_int32_t, caddr_t));
    184 static void ti_loadfw		__P((struct ti_softc *));
    185 static void ti_cmd		__P((struct ti_softc *, struct ti_cmd_desc *));
    186 static void ti_cmd_ext		__P((struct ti_softc *, struct ti_cmd_desc *,
    187 					caddr_t, int));
    188 static void ti_handle_events	__P((struct ti_softc *));
    189 static int ti_alloc_jumbo_mem	__P((struct ti_softc *));
    190 static void *ti_jalloc		__P((struct ti_softc *));
    191 static void ti_jfree		__P((caddr_t, u_int, void *));
    192 static int ti_newbuf_std	__P((struct ti_softc *, int, struct mbuf *, bus_dmamap_t));
    193 static int ti_newbuf_mini	__P((struct ti_softc *, int, struct mbuf *, bus_dmamap_t));
    194 static int ti_newbuf_jumbo	__P((struct ti_softc *, int, struct mbuf *));
    195 static int ti_init_rx_ring_std	__P((struct ti_softc *));
    196 static void ti_free_rx_ring_std	__P((struct ti_softc *));
    197 static int ti_init_rx_ring_jumbo	__P((struct ti_softc *));
    198 static void ti_free_rx_ring_jumbo	__P((struct ti_softc *));
    199 static int ti_init_rx_ring_mini	__P((struct ti_softc *));
    200 static void ti_free_rx_ring_mini	__P((struct ti_softc *));
    201 static void ti_free_tx_ring	__P((struct ti_softc *));
    202 static int ti_init_tx_ring	__P((struct ti_softc *));
    203 
    204 static int ti_64bitslot_war	__P((struct ti_softc *));
    205 static int ti_chipinit		__P((struct ti_softc *));
    206 static int ti_gibinit		__P((struct ti_softc *));
    207 
    208 static int ti_ether_ioctl __P((struct ifnet *, u_long, caddr_t));
    209 
    210 struct cfattach ti_ca = {
    211 	sizeof(struct ti_softc), ti_probe, ti_attach
    212 };
    213 
    214 /*
    215  * Send an instruction or address to the EEPROM, check for ACK.
    216  */
    217 static u_int32_t ti_eeprom_putbyte(sc, byte)
    218 	struct ti_softc		*sc;
    219 	int			byte;
    220 {
    221 	int		i, ack = 0;
    222 
    223 	/*
    224 	 * Make sure we're in TX mode.
    225 	 */
    226 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    227 
    228 	/*
    229 	 * Feed in each bit and stobe the clock.
    230 	 */
    231 	for (i = 0x80; i; i >>= 1) {
    232 		if (byte & i) {
    233 			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
    234 		} else {
    235 			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
    236 		}
    237 		DELAY(1);
    238 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    239 		DELAY(1);
    240 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    241 	}
    242 
    243 	/*
    244 	 * Turn off TX mode.
    245 	 */
    246 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    247 
    248 	/*
    249 	 * Check for ack.
    250 	 */
    251 	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    252 	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
    253 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    254 
    255 	return(ack);
    256 }
    257 
    258 /*
    259  * Read a byte of data stored in the EEPROM at address 'addr.'
    260  * We have to send two address bytes since the EEPROM can hold
    261  * more than 256 bytes of data.
    262  */
    263 static u_int8_t ti_eeprom_getbyte(sc, addr, dest)
    264 	struct ti_softc		*sc;
    265 	int			addr;
    266 	u_int8_t		*dest;
    267 {
    268 	int		i;
    269 	u_int8_t		byte = 0;
    270 
    271 	EEPROM_START;
    272 
    273 	/*
    274 	 * Send write control code to EEPROM.
    275 	 */
    276 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
    277 		printf("%s: failed to send write command, status: %x\n",
    278 		    sc->sc_dev.dv_xname, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    279 		return(1);
    280 	}
    281 
    282 	/*
    283 	 * Send first byte of address of byte we want to read.
    284 	 */
    285 	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
    286 		printf("%s: failed to send address, status: %x\n",
    287 		    sc->sc_dev.dv_xname, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    288 		return(1);
    289 	}
    290 	/*
    291 	 * Send second byte address of byte we want to read.
    292 	 */
    293 	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
    294 		printf("%s: failed to send address, status: %x\n",
    295 		    sc->sc_dev.dv_xname, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    296 		return(1);
    297 	}
    298 
    299 	EEPROM_STOP;
    300 	EEPROM_START;
    301 	/*
    302 	 * Send read control code to EEPROM.
    303 	 */
    304 	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
    305 		printf("%s: failed to send read command, status: %x\n",
    306 		    sc->sc_dev.dv_xname, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
    307 		return(1);
    308 	}
    309 
    310 	/*
    311 	 * Start reading bits from EEPROM.
    312 	 */
    313 	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
    314 	for (i = 0x80; i; i >>= 1) {
    315 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    316 		DELAY(1);
    317 		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
    318 			byte |= i;
    319 		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
    320 		DELAY(1);
    321 	}
    322 
    323 	EEPROM_STOP;
    324 
    325 	/*
    326 	 * No ACK generated for read, so just return byte.
    327 	 */
    328 
    329 	*dest = byte;
    330 
    331 	return(0);
    332 }
    333 
    334 /*
    335  * Read a sequence of bytes from the EEPROM.
    336  */
    337 static int ti_read_eeprom(sc, dest, off, cnt)
    338 	struct ti_softc		*sc;
    339 	caddr_t			dest;
    340 	int			off;
    341 	int			cnt;
    342 {
    343 	int			err = 0, i;
    344 	u_int8_t		byte = 0;
    345 
    346 	for (i = 0; i < cnt; i++) {
    347 		err = ti_eeprom_getbyte(sc, off + i, &byte);
    348 		if (err)
    349 			break;
    350 		*(dest + i) = byte;
    351 	}
    352 
    353 	return(err ? 1 : 0);
    354 }
    355 
    356 /*
    357  * NIC memory access function. Can be used to either clear a section
    358  * of NIC local memory or (if buf is non-NULL) copy data into it.
    359  */
    360 static void ti_mem(sc, addr, len, buf)
    361 	struct ti_softc		*sc;
    362 	u_int32_t		addr, len;
    363 	caddr_t			buf;
    364 {
    365 	int			segptr, segsize, cnt;
    366 	caddr_t			ptr;
    367 
    368 	segptr = addr;
    369 	cnt = len;
    370 	ptr = buf;
    371 
    372 	while(cnt) {
    373 		if (cnt < TI_WINLEN)
    374 			segsize = cnt;
    375 		else
    376 			segsize = TI_WINLEN - (segptr % TI_WINLEN);
    377 		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
    378 		if (buf == NULL) {
    379 			bus_space_set_region_4(sc->ti_btag, sc->ti_bhandle,
    380 			    TI_WINDOW + (segptr & (TI_WINLEN - 1)), 0,
    381 			    segsize / 4);
    382 		} else {
    383 			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
    384 			    TI_WINDOW + (segptr & (TI_WINLEN - 1)),
    385 			    (u_int32_t *)ptr, segsize / 4);
    386 			ptr += segsize;
    387 		}
    388 		segptr += segsize;
    389 		cnt -= segsize;
    390 	}
    391 
    392 	return;
    393 }
    394 
    395 /*
    396  * Load firmware image into the NIC. Check that the firmware revision
    397  * is acceptable and see if we want the firmware for the Tigon 1 or
    398  * Tigon 2.
    399  */
    400 static void ti_loadfw(sc)
    401 	struct ti_softc		*sc;
    402 {
    403 	switch(sc->ti_hwrev) {
    404 	case TI_HWREV_TIGON:
    405 		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
    406 		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
    407 		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
    408 			printf("%s: firmware revision mismatch; want "
    409 			    "%d.%d.%d, got %d.%d.%d\n", sc->sc_dev.dv_xname,
    410 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
    411 			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
    412 			    tigonFwReleaseMinor, tigonFwReleaseFix);
    413 			return;
    414 		}
    415 		ti_mem(sc, tigonFwTextAddr, tigonFwTextLen,
    416 		    (caddr_t)tigonFwText);
    417 		ti_mem(sc, tigonFwDataAddr, tigonFwDataLen,
    418 		    (caddr_t)tigonFwData);
    419 		ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen,
    420 		    (caddr_t)tigonFwRodata);
    421 		ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
    422 		ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
    423 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
    424 		break;
    425 	case TI_HWREV_TIGON_II:
    426 		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
    427 		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
    428 		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
    429 			printf("%s: firmware revision mismatch; want "
    430 			    "%d.%d.%d, got %d.%d.%d\n", sc->sc_dev.dv_xname,
    431 			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
    432 			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
    433 			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
    434 			return;
    435 		}
    436 		ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen,
    437 		    (caddr_t)tigon2FwText);
    438 		ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen,
    439 		    (caddr_t)tigon2FwData);
    440 		ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
    441 		    (caddr_t)tigon2FwRodata);
    442 		ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
    443 		ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
    444 		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
    445 		break;
    446 	default:
    447 		printf("%s: can't load firmware: unknown hardware rev\n",
    448 		    sc->sc_dev.dv_xname);
    449 		break;
    450 	}
    451 
    452 	return;
    453 }
    454 
    455 /*
    456  * Send the NIC a command via the command ring.
    457  */
    458 static void ti_cmd(sc, cmd)
    459 	struct ti_softc		*sc;
    460 	struct ti_cmd_desc	*cmd;
    461 {
    462 	u_int32_t		index;
    463 
    464 	index = sc->ti_cmd_saved_prodidx;
    465 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
    466 	TI_INC(index, TI_CMD_RING_CNT);
    467 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
    468 	sc->ti_cmd_saved_prodidx = index;
    469 
    470 	return;
    471 }
    472 
    473 /*
    474  * Send the NIC an extended command. The 'len' parameter specifies the
    475  * number of command slots to include after the initial command.
    476  */
    477 static void ti_cmd_ext(sc, cmd, arg, len)
    478 	struct ti_softc		*sc;
    479 	struct ti_cmd_desc	*cmd;
    480 	caddr_t			arg;
    481 	int			len;
    482 {
    483 	u_int32_t		index;
    484 	int		i;
    485 
    486 	index = sc->ti_cmd_saved_prodidx;
    487 	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
    488 	TI_INC(index, TI_CMD_RING_CNT);
    489 	for (i = 0; i < len; i++) {
    490 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
    491 		    *(u_int32_t *)(&arg[i * 4]));
    492 		TI_INC(index, TI_CMD_RING_CNT);
    493 	}
    494 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
    495 	sc->ti_cmd_saved_prodidx = index;
    496 
    497 	return;
    498 }
    499 
    500 /*
    501  * Handle events that have triggered interrupts.
    502  */
    503 static void ti_handle_events(sc)
    504 	struct ti_softc		*sc;
    505 {
    506 	struct ti_event_desc	*e;
    507 
    508 	if (sc->ti_rdata->ti_event_ring == NULL)
    509 		return;
    510 
    511 	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
    512 		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
    513 		switch(e->ti_event) {
    514 		case TI_EV_LINKSTAT_CHANGED:
    515 			sc->ti_linkstat = e->ti_code;
    516 			if (e->ti_code == TI_EV_CODE_LINK_UP)
    517 				printf("%s: 10/100 link up\n",
    518 				       sc->sc_dev.dv_xname);
    519 			else if (e->ti_code == TI_EV_CODE_GIG_LINK_UP)
    520 				printf("%s: gigabit link up\n",
    521 				       sc->sc_dev.dv_xname);
    522 			else if (e->ti_code == TI_EV_CODE_LINK_DOWN)
    523 				printf("%s: link down\n",
    524 				       sc->sc_dev.dv_xname);
    525 			break;
    526 		case TI_EV_ERROR:
    527 			if (e->ti_code == TI_EV_CODE_ERR_INVAL_CMD)
    528 				printf("%s: invalid command\n",
    529 				       sc->sc_dev.dv_xname);
    530 			else if (e->ti_code == TI_EV_CODE_ERR_UNIMP_CMD)
    531 				printf("%s: unknown command\n",
    532 				       sc->sc_dev.dv_xname);
    533 			else if (e->ti_code == TI_EV_CODE_ERR_BADCFG)
    534 				printf("%s: bad config data\n",
    535 				       sc->sc_dev.dv_xname);
    536 			break;
    537 		case TI_EV_FIRMWARE_UP:
    538 			ti_init2(sc);
    539 			break;
    540 		case TI_EV_STATS_UPDATED:
    541 			ti_stats_update(sc);
    542 			break;
    543 		case TI_EV_RESET_JUMBO_RING:
    544 		case TI_EV_MCAST_UPDATED:
    545 			/* Who cares. */
    546 			break;
    547 		default:
    548 			printf("%s: unknown event: %d\n",
    549 			    sc->sc_dev.dv_xname, e->ti_event);
    550 			break;
    551 		}
    552 		/* Advance the consumer index. */
    553 		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
    554 		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
    555 	}
    556 
    557 	return;
    558 }
    559 
    560 /*
    561  * Memory management for the jumbo receive ring is a pain in the
    562  * butt. We need to allocate at least 9018 bytes of space per frame,
    563  * _and_ it has to be contiguous (unless you use the extended
    564  * jumbo descriptor format). Using malloc() all the time won't
    565  * work: malloc() allocates memory in powers of two, which means we
    566  * would end up wasting a considerable amount of space by allocating
    567  * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
    568  * to do our own memory management.
    569  *
    570  * The driver needs to allocate a contiguous chunk of memory at boot
    571  * time. We then chop this up ourselves into 9K pieces and use them
    572  * as external mbuf storage.
    573  *
    574  * One issue here is how much memory to allocate. The jumbo ring has
    575  * 256 slots in it, but at 9K per slot than can consume over 2MB of
    576  * RAM. This is a bit much, especially considering we also need
    577  * RAM for the standard ring and mini ring (on the Tigon 2). To
    578  * save space, we only actually allocate enough memory for 64 slots
    579  * by default, which works out to between 500 and 600K. This can
    580  * be tuned by changing a #define in if_tireg.h.
    581  */
    582 
    583 static int ti_alloc_jumbo_mem(sc)
    584 	struct ti_softc		*sc;
    585 {
    586 	caddr_t			ptr;
    587 	int		i;
    588 	struct ti_jpool_entry   *entry;
    589 	bus_dma_segment_t dmaseg;
    590 	int error, dmanseg;
    591 
    592 	/* Grab a big chunk o' storage. */
    593 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
    594 	    TI_JMEM, PAGE_SIZE, 0, &dmaseg, 1, &dmanseg,
    595 	    BUS_DMA_NOWAIT)) != 0) {
    596 		printf("%s: can't allocate jumbo buffer, error = %d\n",
    597 		       sc->sc_dev.dv_xname, error);
    598 		return (error);
    599 	}
    600 
    601 	if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
    602 	    TI_JMEM, (caddr_t *)&sc->ti_cdata.ti_jumbo_buf,
    603 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    604 		printf("%s: can't map jumbo buffer, error = %d\n",
    605 		       sc->sc_dev.dv_xname, error);
    606 		return (error);
    607 	}
    608 
    609 	if ((error = bus_dmamap_create(sc->sc_dmat,
    610 	    TI_JMEM, 1,
    611 	    TI_JMEM, 0, BUS_DMA_NOWAIT,
    612 	    &sc->jumbo_dmamap)) != 0) {
    613 		printf("%s: can't create jumbo buffer DMA map, error = %d\n",
    614 		       sc->sc_dev.dv_xname, error);
    615 		return (error);
    616 	}
    617 
    618 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->jumbo_dmamap,
    619 	    sc->ti_cdata.ti_jumbo_buf, TI_JMEM, NULL,
    620 	    BUS_DMA_NOWAIT)) != 0) {
    621 		printf("%s: can't load jumbo buffer DMA map, error = %d\n",
    622 		       sc->sc_dev.dv_xname, error);
    623 		return (error);
    624 	}
    625 	sc->jumbo_dmaaddr = sc->jumbo_dmamap->dm_segs[0].ds_addr;
    626 
    627 	SIMPLEQ_INIT(&sc->ti_jfree_listhead);
    628 	SIMPLEQ_INIT(&sc->ti_jinuse_listhead);
    629 
    630 	/*
    631 	 * Now divide it up into 9K pieces and save the addresses
    632 	 * in an array.
    633 	 */
    634 	ptr = sc->ti_cdata.ti_jumbo_buf;
    635 	for (i = 0; i < TI_JSLOTS; i++) {
    636 		sc->ti_cdata.ti_jslots[i] = ptr;
    637 		ptr += TI_JLEN;
    638 		entry = malloc(sizeof(struct ti_jpool_entry),
    639 			       M_DEVBUF, M_NOWAIT);
    640 		if (entry == NULL) {
    641 			free(sc->ti_cdata.ti_jumbo_buf, M_DEVBUF);
    642 			sc->ti_cdata.ti_jumbo_buf = NULL;
    643 			printf("%s: no memory for jumbo "
    644 			    "buffer queue!\n", sc->sc_dev.dv_xname);
    645 			return(ENOBUFS);
    646 		}
    647 		entry->slot = i;
    648 		SIMPLEQ_INSERT_HEAD(&sc->ti_jfree_listhead, entry,
    649 				    jpool_entries);
    650 	}
    651 
    652 	return(0);
    653 }
    654 
    655 /*
    656  * Allocate a jumbo buffer.
    657  */
    658 static void *ti_jalloc(sc)
    659 	struct ti_softc		*sc;
    660 {
    661 	struct ti_jpool_entry   *entry;
    662 
    663 	entry = SIMPLEQ_FIRST(&sc->ti_jfree_listhead);
    664 
    665 	if (entry == NULL) {
    666 		printf("%s: no free jumbo buffers\n", sc->sc_dev.dv_xname);
    667 		return(NULL);
    668 	}
    669 
    670 	SIMPLEQ_REMOVE_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
    671 	SIMPLEQ_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
    672 	return(sc->ti_cdata.ti_jslots[entry->slot]);
    673 }
    674 
    675 /*
    676  * Release a jumbo buffer.
    677  */
    678 static void ti_jfree(buf, size, arg)
    679 	caddr_t			buf;
    680 	u_int			size;
    681 	void *arg;
    682 {
    683 	struct ti_softc		*sc;
    684 	int		        i;
    685 	struct ti_jpool_entry   *entry;
    686 
    687 	/* Extract the softc struct pointer. */
    688 	sc = (struct ti_softc *)arg;
    689 
    690 	if (sc == NULL)
    691 		panic("ti_jfree: didn't get softc pointer!");
    692 
    693 	/* calculate the slot this buffer belongs to */
    694 
    695 	i = ((caddr_t)buf
    696 	     - (caddr_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
    697 
    698 	if ((i < 0) || (i >= TI_JSLOTS))
    699 		panic("ti_jfree: asked to free buffer that we don't manage!");
    700 	entry = SIMPLEQ_FIRST(&sc->ti_jinuse_listhead);
    701 	if (entry == NULL)
    702 		panic("ti_jfree: buffer not in use!");
    703 	entry->slot = i;
    704 	SIMPLEQ_REMOVE_HEAD(&sc->ti_jinuse_listhead,
    705 	    entry, jpool_entries);
    706 	SIMPLEQ_INSERT_HEAD(&sc->ti_jfree_listhead,
    707 	     entry, jpool_entries);
    708 
    709 	return;
    710 }
    711 
    712 
    713 /*
    714  * Intialize a standard receive ring descriptor.
    715  */
    716 static int ti_newbuf_std(sc, i, m, dmamap)
    717 	struct ti_softc		*sc;
    718 	int			i;
    719 	struct mbuf		*m;
    720 	bus_dmamap_t dmamap; /* required if (m != NULL) */
    721 {
    722 	struct mbuf		*m_new = NULL;
    723 	struct ti_rx_desc	*r;
    724 	int error;
    725 
    726 	if (dmamap == NULL) {
    727 		/* if (m) panic() */
    728 
    729 		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
    730 					       MCLBYTES, 0, BUS_DMA_NOWAIT,
    731 					       &dmamap)) != 0) {
    732 			printf("%s: can't create recv map, error = %d\n",
    733 			       sc->sc_dev.dv_xname, error);
    734 			return(ENOMEM);
    735 		}
    736 	}
    737 	sc->std_dmamap[i] = dmamap;
    738 
    739 	if (m == NULL) {
    740 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    741 		if (m_new == NULL) {
    742 			printf("%s: mbuf allocation failed "
    743 			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
    744 			return(ENOBUFS);
    745 		}
    746 
    747 		MCLGET(m_new, M_DONTWAIT);
    748 		if (!(m_new->m_flags & M_EXT)) {
    749 			printf("%s: cluster allocation failed "
    750 			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
    751 			m_freem(m_new);
    752 			return(ENOBUFS);
    753 		}
    754 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    755 		m_adj(m_new, ETHER_ALIGN);
    756 
    757 		if ((error = bus_dmamap_load(sc->sc_dmat, dmamap,
    758 				mtod(m_new, caddr_t), m_new->m_len, NULL,
    759 				BUS_DMA_NOWAIT)) != 0) {
    760 			printf("%s: can't load recv map, error = %d\n",
    761 			       sc->sc_dev.dv_xname, error);
    762 			return (ENOMEM);
    763 		}
    764 	} else {
    765 		m_new = m;
    766 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
    767 		m_new->m_data = m_new->m_ext.ext_buf;
    768 		m_adj(m_new, ETHER_ALIGN);
    769 
    770 		/* reuse the dmamap */
    771 	}
    772 
    773 	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
    774 	r = &sc->ti_rdata->ti_rx_std_ring[i];
    775 	TI_HOSTADDR(r->ti_addr) = dmamap->dm_segs[0].ds_addr;
    776 	r->ti_type = TI_BDTYPE_RECV_BD;
    777 	r->ti_flags = 0;
    778 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4)
    779 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    780 	if (sc->ethercom.ec_if.if_capenable &
    781 	    (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
    782 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    783 	r->ti_len = m_new->m_len; /* == ds_len */
    784 	r->ti_idx = i;
    785 
    786 	return(0);
    787 }
    788 
    789 /*
    790  * Intialize a mini receive ring descriptor. This only applies to
    791  * the Tigon 2.
    792  */
    793 static int ti_newbuf_mini(sc, i, m, dmamap)
    794 	struct ti_softc		*sc;
    795 	int			i;
    796 	struct mbuf		*m;
    797 	bus_dmamap_t dmamap; /* required if (m != NULL) */
    798 {
    799 	struct mbuf		*m_new = NULL;
    800 	struct ti_rx_desc	*r;
    801 	int error;
    802 
    803 	if (dmamap == NULL) {
    804 		/* if (m) panic() */
    805 
    806 		if ((error = bus_dmamap_create(sc->sc_dmat, MHLEN, 1,
    807 					       MHLEN, 0, BUS_DMA_NOWAIT,
    808 					       &dmamap)) != 0) {
    809 			printf("%s: can't create recv map, error = %d\n",
    810 			       sc->sc_dev.dv_xname, error);
    811 			return(ENOMEM);
    812 		}
    813 	}
    814 	sc->mini_dmamap[i] = dmamap;
    815 
    816 	if (m == NULL) {
    817 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    818 		if (m_new == NULL) {
    819 			printf("%s: mbuf allocation failed "
    820 			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
    821 			return(ENOBUFS);
    822 		}
    823 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
    824 		m_adj(m_new, ETHER_ALIGN);
    825 
    826 		if ((error = bus_dmamap_load(sc->sc_dmat, dmamap,
    827 				mtod(m_new, caddr_t), m_new->m_len, NULL,
    828 				BUS_DMA_NOWAIT)) != 0) {
    829 			printf("%s: can't load recv map, error = %d\n",
    830 			       sc->sc_dev.dv_xname, error);
    831 			return (ENOMEM);
    832 		}
    833 	} else {
    834 		m_new = m;
    835 		m_new->m_data = m_new->m_pktdat;
    836 		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
    837 		m_adj(m_new, ETHER_ALIGN);
    838 
    839 		/* reuse the dmamap */
    840 	}
    841 
    842 	r = &sc->ti_rdata->ti_rx_mini_ring[i];
    843 	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
    844 	TI_HOSTADDR(r->ti_addr) = dmamap->dm_segs[0].ds_addr;
    845 	r->ti_type = TI_BDTYPE_RECV_BD;
    846 	r->ti_flags = TI_BDFLAG_MINI_RING;
    847 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4)
    848 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    849 	if (sc->ethercom.ec_if.if_capenable &
    850 	    (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
    851 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    852 	r->ti_len = m_new->m_len; /* == ds_len */
    853 	r->ti_idx = i;
    854 
    855 	return(0);
    856 }
    857 
    858 /*
    859  * Initialize a jumbo receive ring descriptor. This allocates
    860  * a jumbo buffer from the pool managed internally by the driver.
    861  */
    862 static int ti_newbuf_jumbo(sc, i, m)
    863 	struct ti_softc		*sc;
    864 	int			i;
    865 	struct mbuf		*m;
    866 {
    867 	struct mbuf		*m_new = NULL;
    868 	struct ti_rx_desc	*r;
    869 
    870 	if (m == NULL) {
    871 		caddr_t			*buf = NULL;
    872 
    873 		/* Allocate the mbuf. */
    874 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
    875 		if (m_new == NULL) {
    876 			printf("%s: mbuf allocation failed "
    877 			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
    878 			return(ENOBUFS);
    879 		}
    880 
    881 		/* Allocate the jumbo buffer */
    882 		buf = ti_jalloc(sc);
    883 		if (buf == NULL) {
    884 			m_freem(m_new);
    885 			printf("%s: jumbo allocation failed "
    886 			    "-- packet dropped!\n", sc->sc_dev.dv_xname);
    887 			return(ENOBUFS);
    888 		}
    889 
    890 		/* Attach the buffer to the mbuf. */
    891 		m_new->m_data = m_new->m_ext.ext_buf = (void *)buf;
    892 		m_new->m_flags |= M_EXT;
    893 		m_new->m_len = m_new->m_pkthdr.len =
    894 		    m_new->m_ext.ext_size = ETHER_MAX_LEN_JUMBO;
    895 		m_new->m_ext.ext_free = ti_jfree;
    896 		m_new->m_ext.ext_arg = sc;
    897 		MCLINITREFERENCE(m_new);
    898 	} else {
    899 		m_new = m;
    900 		m_new->m_data = m_new->m_ext.ext_buf;
    901 		m_new->m_ext.ext_size = ETHER_MAX_LEN_JUMBO;
    902 	}
    903 
    904 	m_adj(m_new, ETHER_ALIGN);
    905 	/* Set up the descriptor. */
    906 	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
    907 	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
    908 	TI_HOSTADDR(r->ti_addr) = sc->jumbo_dmaaddr +
    909 		((caddr_t)mtod(m_new, caddr_t)
    910 		 - (caddr_t)sc->ti_cdata.ti_jumbo_buf);
    911 	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
    912 	r->ti_flags = TI_BDFLAG_JUMBO_RING;
    913 	if (sc->ethercom.ec_if.if_capenable & IFCAP_CSUM_IPv4)
    914 		r->ti_flags |= TI_BDFLAG_IP_CKSUM;
    915 	if (sc->ethercom.ec_if.if_capenable &
    916 	    (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
    917 		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
    918 	r->ti_len = m_new->m_len;
    919 	r->ti_idx = i;
    920 
    921 	return(0);
    922 }
    923 
    924 /*
    925  * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
    926  * that's 1MB or memory, which is a lot. For now, we fill only the first
    927  * 256 ring entries and hope that our CPU is fast enough to keep up with
    928  * the NIC.
    929  */
    930 static int ti_init_rx_ring_std(sc)
    931 	struct ti_softc		*sc;
    932 {
    933 	int		i;
    934 	struct ti_cmd_desc	cmd;
    935 
    936 	for (i = 0; i < TI_SSLOTS; i++) {
    937 		if (ti_newbuf_std(sc, i, NULL, 0) == ENOBUFS)
    938 			return(ENOBUFS);
    939 	};
    940 
    941 	TI_UPDATE_STDPROD(sc, i - 1);
    942 	sc->ti_std = i - 1;
    943 
    944 	return(0);
    945 }
    946 
    947 static void ti_free_rx_ring_std(sc)
    948 	struct ti_softc		*sc;
    949 {
    950 	int		i;
    951 
    952 	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
    953 		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
    954 			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
    955 			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
    956 
    957 			/* if (sc->std_dmamap[i] == 0) panic() */
    958 			bus_dmamap_destroy(sc->sc_dmat, sc->std_dmamap[i]);
    959 			sc->std_dmamap[i] = 0;
    960 		}
    961 		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
    962 		    sizeof(struct ti_rx_desc));
    963 	}
    964 
    965 	return;
    966 }
    967 
    968 static int ti_init_rx_ring_jumbo(sc)
    969 	struct ti_softc		*sc;
    970 {
    971 	int		i;
    972 	struct ti_cmd_desc	cmd;
    973 
    974 	for (i = 0; i < (TI_JSLOTS - 20); i++) {
    975 		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
    976 			return(ENOBUFS);
    977 	};
    978 
    979 	TI_UPDATE_JUMBOPROD(sc, i - 1);
    980 	sc->ti_jumbo = i - 1;
    981 
    982 	return(0);
    983 }
    984 
    985 static void ti_free_rx_ring_jumbo(sc)
    986 	struct ti_softc		*sc;
    987 {
    988 	int		i;
    989 
    990 	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
    991 		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
    992 			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
    993 			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
    994 		}
    995 		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
    996 		    sizeof(struct ti_rx_desc));
    997 	}
    998 
    999 	return;
   1000 }
   1001 
   1002 static int ti_init_rx_ring_mini(sc)
   1003 	struct ti_softc		*sc;
   1004 {
   1005 	int		i;
   1006 
   1007 	for (i = 0; i < TI_MSLOTS; i++) {
   1008 		if (ti_newbuf_mini(sc, i, NULL, 0) == ENOBUFS)
   1009 			return(ENOBUFS);
   1010 	};
   1011 
   1012 	TI_UPDATE_MINIPROD(sc, i - 1);
   1013 	sc->ti_mini = i - 1;
   1014 
   1015 	return(0);
   1016 }
   1017 
   1018 static void ti_free_rx_ring_mini(sc)
   1019 	struct ti_softc		*sc;
   1020 {
   1021 	int		i;
   1022 
   1023 	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
   1024 		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
   1025 			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
   1026 			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
   1027 
   1028 			/* if (sc->mini_dmamap[i] == 0) panic() */
   1029 			bus_dmamap_destroy(sc->sc_dmat, sc->mini_dmamap[i]);
   1030 			sc->mini_dmamap[i] = 0;
   1031 		}
   1032 		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
   1033 		    sizeof(struct ti_rx_desc));
   1034 	}
   1035 
   1036 	return;
   1037 }
   1038 
   1039 static void ti_free_tx_ring(sc)
   1040 	struct ti_softc		*sc;
   1041 {
   1042 	int		i;
   1043 	struct txdmamap_pool_entry *dma;
   1044 
   1045 	if (sc->ti_rdata->ti_tx_ring == NULL)
   1046 		return;
   1047 
   1048 	for (i = 0; i < TI_TX_RING_CNT; i++) {
   1049 		if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
   1050 			m_freem(sc->ti_cdata.ti_tx_chain[i]);
   1051 			sc->ti_cdata.ti_tx_chain[i] = NULL;
   1052 
   1053 			/* if (sc->txdma[i] == 0) panic() */
   1054 			SIMPLEQ_INSERT_HEAD(&sc->txdma_list, sc->txdma[i],
   1055 					    link);
   1056 			sc->txdma[i] = 0;
   1057 		}
   1058 		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
   1059 		    sizeof(struct ti_tx_desc));
   1060 	}
   1061 
   1062 	while ((dma = SIMPLEQ_FIRST(&sc->txdma_list))) {
   1063 		SIMPLEQ_REMOVE_HEAD(&sc->txdma_list, dma, link);
   1064 		bus_dmamap_destroy(sc->sc_dmat, dma->dmamap);
   1065 		free(dma, M_DEVBUF);
   1066 	}
   1067 
   1068 	return;
   1069 }
   1070 
   1071 static int ti_init_tx_ring(sc)
   1072 	struct ti_softc		*sc;
   1073 {
   1074 	int i, error;
   1075 	bus_dmamap_t dmamap;
   1076 	struct txdmamap_pool_entry *dma;
   1077 
   1078 	sc->ti_txcnt = 0;
   1079 	sc->ti_tx_saved_considx = 0;
   1080 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
   1081 
   1082 	SIMPLEQ_INIT(&sc->txdma_list);
   1083 	for (i = 0; i < TI_RSLOTS; i++) {
   1084 		/* I've seen mbufs with 30 fragments. */
   1085 		if ((error = bus_dmamap_create(sc->sc_dmat, ETHER_MAX_LEN_JUMBO,
   1086 					       40, ETHER_MAX_LEN_JUMBO, 0,
   1087 					       BUS_DMA_NOWAIT, &dmamap)) != 0) {
   1088 			printf("%s: can't create tx map, error = %d\n",
   1089 			       sc->sc_dev.dv_xname, error);
   1090 			return(ENOMEM);
   1091 		}
   1092 		dma = malloc(sizeof(*dma), M_DEVBUF, M_NOWAIT);
   1093 		if (!dma) {
   1094 			printf("%s: can't alloc txdmamap_pool_entry\n",
   1095 			       sc->sc_dev.dv_xname);
   1096 			bus_dmamap_destroy(sc->sc_dmat, dmamap);
   1097 			return (ENOMEM);
   1098 		}
   1099 		dma->dmamap = dmamap;
   1100 		SIMPLEQ_INSERT_HEAD(&sc->txdma_list, dma, link);
   1101 	}
   1102 
   1103 	return(0);
   1104 }
   1105 
   1106 /*
   1107  * The Tigon 2 firmware has a new way to add/delete multicast addresses,
   1108  * but we have to support the old way too so that Tigon 1 cards will
   1109  * work.
   1110  */
   1111 void ti_add_mcast(sc, addr)
   1112 	struct ti_softc		*sc;
   1113 	struct ether_addr	*addr;
   1114 {
   1115 	struct ti_cmd_desc	cmd;
   1116 	u_int16_t		*m;
   1117 	u_int32_t		ext[2] = {0, 0};
   1118 
   1119 	m = (u_int16_t *)&addr->ether_addr_octet[0]; /* XXX */
   1120 
   1121 	switch(sc->ti_hwrev) {
   1122 	case TI_HWREV_TIGON:
   1123 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
   1124 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
   1125 		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
   1126 		break;
   1127 	case TI_HWREV_TIGON_II:
   1128 		ext[0] = htons(m[0]);
   1129 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
   1130 		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
   1131 		break;
   1132 	default:
   1133 		printf("%s: unknown hwrev\n", sc->sc_dev.dv_xname);
   1134 		break;
   1135 	}
   1136 
   1137 	return;
   1138 }
   1139 
   1140 void ti_del_mcast(sc, addr)
   1141 	struct ti_softc		*sc;
   1142 	struct ether_addr	*addr;
   1143 {
   1144 	struct ti_cmd_desc	cmd;
   1145 	u_int16_t		*m;
   1146 	u_int32_t		ext[2] = {0, 0};
   1147 
   1148 	m = (u_int16_t *)&addr->ether_addr_octet[0]; /* XXX */
   1149 
   1150 	switch(sc->ti_hwrev) {
   1151 	case TI_HWREV_TIGON:
   1152 		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
   1153 		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
   1154 		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
   1155 		break;
   1156 	case TI_HWREV_TIGON_II:
   1157 		ext[0] = htons(m[0]);
   1158 		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
   1159 		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
   1160 		break;
   1161 	default:
   1162 		printf("%s: unknown hwrev\n", sc->sc_dev.dv_xname);
   1163 		break;
   1164 	}
   1165 
   1166 	return;
   1167 }
   1168 
   1169 /*
   1170  * Configure the Tigon's multicast address filter.
   1171  *
   1172  * The actual multicast table management is a bit of a pain, thanks to
   1173  * slight brain damage on the part of both Alteon and us. With our
   1174  * multicast code, we are only alerted when the multicast address table
   1175  * changes and at that point we only have the current list of addresses:
   1176  * we only know the current state, not the previous state, so we don't
   1177  * actually know what addresses were removed or added. The firmware has
   1178  * state, but we can't get our grubby mits on it, and there is no 'delete
   1179  * all multicast addresses' command. Hence, we have to maintain our own
   1180  * state so we know what addresses have been programmed into the NIC at
   1181  * any given time.
   1182  */
   1183 static void ti_setmulti(sc)
   1184 	struct ti_softc		*sc;
   1185 {
   1186 	struct ifnet		*ifp;
   1187 	struct ti_cmd_desc	cmd;
   1188 	struct ti_mc_entry	*mc;
   1189 	u_int32_t		intrs;
   1190 	struct ether_multi *enm;
   1191 	struct ether_multistep step;
   1192 
   1193 	ifp = &sc->ethercom.ec_if;
   1194 
   1195 	/* Disable interrupts. */
   1196 	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
   1197 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   1198 
   1199 	/* First, zot all the existing filters. */
   1200 	while ((mc = SIMPLEQ_FIRST(&sc->ti_mc_listhead)) != NULL) {
   1201 		ti_del_mcast(sc, &mc->mc_addr);
   1202 		SIMPLEQ_REMOVE_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
   1203 		free(mc, M_DEVBUF);
   1204 	}
   1205 
   1206 	/*
   1207 	 * Remember all multicast addresses so that we can delete them
   1208 	 * later.  Punt if there is a range of addresses or memory shortage.
   1209 	 */
   1210 	ETHER_FIRST_MULTI(step, &sc->ethercom, enm);
   1211 	while (enm != NULL) {
   1212 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1213 		    ETHER_ADDR_LEN) != 0)
   1214 			goto allmulti;
   1215 		if ((mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF,
   1216 		    M_NOWAIT)) == NULL)
   1217 			goto allmulti;
   1218 		memcpy(&mc->mc_addr, enm->enm_addrlo, ETHER_ADDR_LEN);
   1219 		SIMPLEQ_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
   1220 		ETHER_NEXT_MULTI(step, enm);
   1221 	}
   1222 
   1223 	/* Accept only programmed multicast addresses */
   1224 	ifp->if_flags &= ~IFF_ALLMULTI;
   1225 	TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
   1226 
   1227 	/* Now program new ones. */
   1228 	for (mc = SIMPLEQ_FIRST(&sc->ti_mc_listhead); mc != NULL;
   1229 	    mc = SIMPLEQ_NEXT(mc, mc_entries))
   1230 		ti_add_mcast(sc, &mc->mc_addr);
   1231 
   1232 	/* Re-enable interrupts. */
   1233 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
   1234 
   1235 	return;
   1236 
   1237 allmulti:
   1238 	/* No need to keep individual multicast addresses */
   1239 	while ((mc = SIMPLEQ_FIRST(&sc->ti_mc_listhead)) != NULL) {
   1240 		SIMPLEQ_REMOVE_HEAD(&sc->ti_mc_listhead, mc,
   1241 		    mc_entries);
   1242 		free(mc, M_DEVBUF);
   1243 	}
   1244 
   1245 	/* Accept all multicast addresses */
   1246 	ifp->if_flags |= IFF_ALLMULTI;
   1247 	TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
   1248 
   1249 	/* Re-enable interrupts. */
   1250 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
   1251 }
   1252 
   1253 /*
   1254  * Check to see if the BIOS has configured us for a 64 bit slot when
   1255  * we aren't actually in one. If we detect this condition, we can work
   1256  * around it on the Tigon 2 by setting a bit in the PCI state register,
   1257  * but for the Tigon 1 we must give up and abort the interface attach.
   1258  */
   1259 static int ti_64bitslot_war(sc)
   1260 	struct ti_softc		*sc;
   1261 {
   1262 	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
   1263 		CSR_WRITE_4(sc, 0x600, 0);
   1264 		CSR_WRITE_4(sc, 0x604, 0);
   1265 		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
   1266 		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
   1267 			if (sc->ti_hwrev == TI_HWREV_TIGON)
   1268 				return(EINVAL);
   1269 			else {
   1270 				TI_SETBIT(sc, TI_PCI_STATE,
   1271 				    TI_PCISTATE_32BIT_BUS);
   1272 				return(0);
   1273 			}
   1274 		}
   1275 	}
   1276 
   1277 	return(0);
   1278 }
   1279 
   1280 /*
   1281  * Do endian, PCI and DMA initialization. Also check the on-board ROM
   1282  * self-test results.
   1283  */
   1284 static int ti_chipinit(sc)
   1285 	struct ti_softc		*sc;
   1286 {
   1287 	u_int32_t		cacheline;
   1288 	u_int32_t		pci_writemax = 0;
   1289 
   1290 	/* Initialize link to down state. */
   1291 	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
   1292 
   1293 	/* Set endianness before we access any non-PCI registers. */
   1294 #if BYTE_ORDER == BIG_ENDIAN
   1295 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
   1296 	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
   1297 #else
   1298 	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
   1299 	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
   1300 #endif
   1301 
   1302 	/* Check the ROM failed bit to see if self-tests passed. */
   1303 	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
   1304 		printf("%s: board self-diagnostics failed!\n",
   1305 		       sc->sc_dev.dv_xname);
   1306 		return(ENODEV);
   1307 	}
   1308 
   1309 	/* Halt the CPU. */
   1310 	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
   1311 
   1312 	/* Figure out the hardware revision. */
   1313 	switch(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
   1314 	case TI_REV_TIGON_I:
   1315 		sc->ti_hwrev = TI_HWREV_TIGON;
   1316 		break;
   1317 	case TI_REV_TIGON_II:
   1318 		sc->ti_hwrev = TI_HWREV_TIGON_II;
   1319 		break;
   1320 	default:
   1321 		printf("%s: unsupported chip revision\n", sc->sc_dev.dv_xname);
   1322 		return(ENODEV);
   1323 	}
   1324 
   1325 	/* Do special setup for Tigon 2. */
   1326 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
   1327 		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
   1328 		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_256K);
   1329 		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
   1330 	}
   1331 
   1332 	/* Set up the PCI state register. */
   1333 	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
   1334 	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
   1335 		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
   1336 	}
   1337 
   1338 	/* Clear the read/write max DMA parameters. */
   1339 	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
   1340 	    TI_PCISTATE_READ_MAXDMA));
   1341 
   1342 	/* Get cache line size. */
   1343 	cacheline = PCI_CACHELINE(CSR_READ_4(sc, PCI_BHLC_REG));
   1344 
   1345 	/*
   1346 	 * If the system has set enabled the PCI memory write
   1347 	 * and invalidate command in the command register, set
   1348 	 * the write max parameter accordingly. This is necessary
   1349 	 * to use MWI with the Tigon 2.
   1350 	 */
   1351 	if (CSR_READ_4(sc, PCI_COMMAND_STATUS_REG)
   1352 	    & PCI_COMMAND_INVALIDATE_ENABLE) {
   1353 		switch(cacheline) {
   1354 		case 1:
   1355 		case 4:
   1356 		case 8:
   1357 		case 16:
   1358 		case 32:
   1359 		case 64:
   1360 			break;
   1361 		default:
   1362 		/* Disable PCI memory write and invalidate. */
   1363 			if (bootverbose)
   1364 				printf("%s: cache line size %d not "
   1365 				    "supported; disabling PCI MWI\n",
   1366 				    sc->sc_dev.dv_xname, cacheline);
   1367 			CSR_WRITE_4(sc, PCI_COMMAND_STATUS_REG,
   1368 				    CSR_READ_4(sc, PCI_COMMAND_STATUS_REG)
   1369 				    & ~PCI_COMMAND_INVALIDATE_ENABLE);
   1370 			break;
   1371 		}
   1372 	}
   1373 
   1374 #ifdef __brokenalpha__
   1375 	/*
   1376 	 * From the Alteon sample driver:
   1377 	 * Must insure that we do not cross an 8K (bytes) boundary
   1378 	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
   1379 	 * restriction on some ALPHA platforms with early revision
   1380 	 * 21174 PCI chipsets, such as the AlphaPC 164lx
   1381 	 */
   1382 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax|TI_PCI_READMAX_1024);
   1383 #else
   1384 	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
   1385 #endif
   1386 
   1387 	/* This sets the min dma param all the way up (0xff). */
   1388 	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
   1389 
   1390 	/* Configure DMA variables. */
   1391 #if BYTE_ORDER == BIG_ENDIAN
   1392 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
   1393 	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
   1394 	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
   1395 	    TI_OPMODE_DONT_FRAG_JUMBO);
   1396 #else
   1397 	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
   1398 	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
   1399 	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB);
   1400 #endif
   1401 
   1402 	/*
   1403 	 * Only allow 1 DMA channel to be active at a time.
   1404 	 * I don't think this is a good idea, but without it
   1405 	 * the firmware racks up lots of nicDmaReadRingFull
   1406 	 * errors.
   1407 	 * Incompatible with hardware assisted checksums.
   1408 	 */
   1409 	if ((sc->ethercom.ec_if.if_capenable &
   1410 	    (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4|IFCAP_CSUM_IPv4)) == 0)
   1411 		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
   1412 
   1413 	/* Recommended settings from Tigon manual. */
   1414 	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
   1415 	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
   1416 
   1417 	if (ti_64bitslot_war(sc)) {
   1418 		printf("%s: bios thinks we're in a 64 bit slot, "
   1419 		    "but we aren't", sc->sc_dev.dv_xname);
   1420 		return(EINVAL);
   1421 	}
   1422 
   1423 	return(0);
   1424 }
   1425 
   1426 /*
   1427  * Initialize the general information block and firmware, and
   1428  * start the CPU(s) running.
   1429  */
   1430 static int ti_gibinit(sc)
   1431 	struct ti_softc		*sc;
   1432 {
   1433 	struct ti_rcb		*rcb;
   1434 	int			i;
   1435 	struct ifnet		*ifp;
   1436 
   1437 	ifp = &sc->ethercom.ec_if;
   1438 
   1439 	/* Disable interrupts for now. */
   1440 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   1441 
   1442 	/* Tell the chip where to find the general information block. */
   1443 	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
   1444 	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, sc->info_dmaaddr +
   1445 		    ((caddr_t)&sc->ti_rdata->ti_info - (caddr_t)sc->ti_rdata));
   1446 
   1447 	/* Load the firmware into SRAM. */
   1448 	ti_loadfw(sc);
   1449 
   1450 	/* Set up the contents of the general info and ring control blocks. */
   1451 
   1452 	/* Set up the event ring and producer pointer. */
   1453 	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
   1454 
   1455 	TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1456 		((caddr_t)&sc->ti_rdata->ti_event_ring - (caddr_t)sc->ti_rdata);
   1457 	rcb->ti_flags = 0;
   1458 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
   1459 	    sc->info_dmaaddr + ((caddr_t)&sc->ti_rdata->ti_ev_prodidx_r
   1460 				- (caddr_t)sc->ti_rdata);
   1461 	sc->ti_ev_prodidx.ti_idx = 0;
   1462 	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
   1463 	sc->ti_ev_saved_considx = 0;
   1464 
   1465 	/* Set up the command ring and producer mailbox. */
   1466 	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
   1467 
   1468 	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
   1469 	rcb->ti_flags = 0;
   1470 	rcb->ti_max_len = 0;
   1471 	for (i = 0; i < TI_CMD_RING_CNT; i++) {
   1472 		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
   1473 	}
   1474 	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
   1475 	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
   1476 	sc->ti_cmd_saved_prodidx = 0;
   1477 
   1478 	/*
   1479 	 * Assign the address of the stats refresh buffer.
   1480 	 * We re-use the current stats buffer for this to
   1481 	 * conserve memory.
   1482 	 */
   1483 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
   1484 	    sc->info_dmaaddr + ((caddr_t)&sc->ti_rdata->ti_info.ti_stats
   1485 		    - (caddr_t)sc->ti_rdata);
   1486 
   1487 	/* Set up the standard receive ring. */
   1488 	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
   1489 	TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1490 		((caddr_t)&sc->ti_rdata->ti_rx_std_ring
   1491 		 - (caddr_t)sc->ti_rdata);
   1492 	rcb->ti_max_len = ETHER_MAX_LEN;
   1493 	rcb->ti_flags = 0;
   1494 	if (ifp->if_capenable & IFCAP_CSUM_IPv4)
   1495 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1496 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1497 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1498 	if (sc->ethercom.ec_nvlans != 0)
   1499 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1500 
   1501 	/* Set up the jumbo receive ring. */
   1502 	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
   1503 	TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1504 	    ((caddr_t)&sc->ti_rdata->ti_rx_jumbo_ring - (caddr_t)sc->ti_rdata);
   1505 	rcb->ti_max_len = ETHER_MAX_LEN_JUMBO;
   1506 	rcb->ti_flags = 0;
   1507 	if (ifp->if_capenable & IFCAP_CSUM_IPv4)
   1508 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1509 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1510 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1511 	if (sc->ethercom.ec_nvlans != 0)
   1512 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1513 
   1514 	/*
   1515 	 * Set up the mini ring. Only activated on the
   1516 	 * Tigon 2 but the slot in the config block is
   1517 	 * still there on the Tigon 1.
   1518 	 */
   1519 	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
   1520 	TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1521 	    ((caddr_t)&sc->ti_rdata->ti_rx_mini_ring - (caddr_t)sc->ti_rdata);
   1522 	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
   1523 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1524 		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
   1525 	else
   1526 		rcb->ti_flags = 0;
   1527 	if (ifp->if_capenable & IFCAP_CSUM_IPv4)
   1528 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1529 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1530 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM;
   1531 	if (sc->ethercom.ec_nvlans != 0)
   1532 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1533 
   1534 	/*
   1535 	 * Set up the receive return ring.
   1536 	 */
   1537 	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
   1538 	TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1539 	    ((caddr_t)&sc->ti_rdata->ti_rx_return_ring - (caddr_t)sc->ti_rdata);
   1540 	rcb->ti_flags = 0;
   1541 	rcb->ti_max_len = TI_RETURN_RING_CNT;
   1542 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
   1543 	    sc->info_dmaaddr + ((caddr_t)&sc->ti_rdata->ti_return_prodidx_r
   1544 		    - (caddr_t)sc->ti_rdata);
   1545 
   1546 	/*
   1547 	 * Set up the tx ring. Note: for the Tigon 2, we have the option
   1548 	 * of putting the transmit ring in the host's address space and
   1549 	 * letting the chip DMA it instead of leaving the ring in the NIC's
   1550 	 * memory and accessing it through the shared memory region. We
   1551 	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
   1552 	 * so we have to revert to the shared memory scheme if we detect
   1553 	 * a Tigon 1 chip.
   1554 	 */
   1555 	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
   1556 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
   1557 		sc->ti_rdata->ti_tx_ring_nic =
   1558 		    (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
   1559 	}
   1560 	bzero((char *)sc->ti_rdata->ti_tx_ring,
   1561 	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
   1562 	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
   1563 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1564 		rcb->ti_flags = 0;
   1565 	else
   1566 		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
   1567 	if (ifp->if_capenable & IFCAP_CSUM_IPv4)
   1568 		rcb->ti_flags |= TI_RCB_FLAG_IP_CKSUM;
   1569 	/*
   1570 	 * When we get the packet, there is a pseudo-header seed already
   1571 	 * in the th_sum or uh_sum field.  Make sure the firmware doesn't
   1572 	 * compute the pseudo-header checksum again!
   1573 	 */
   1574 	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4|IFCAP_CSUM_UDPv4))
   1575 		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM|
   1576 		    TI_RCB_FLAG_NO_PHDR_CKSUM;
   1577 	if (sc->ethercom.ec_nvlans != 0)
   1578 		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
   1579 	rcb->ti_max_len = TI_TX_RING_CNT;
   1580 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   1581 		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
   1582 	else
   1583 		TI_HOSTADDR(rcb->ti_hostaddr) = sc->info_dmaaddr +
   1584 		    ((caddr_t)&sc->ti_rdata->ti_tx_ring
   1585 		     - (caddr_t)sc->ti_rdata);
   1586 	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
   1587 	    sc->info_dmaaddr + ((caddr_t)&sc->ti_rdata->ti_tx_considx_r
   1588 		    - (caddr_t)sc->ti_rdata);
   1589 
   1590 	/* Set up tuneables */
   1591 	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN) ||
   1592 	    (sc->ethercom.ec_capenable & ETHERCAP_VLAN_MTU))
   1593 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
   1594 		    (sc->ti_rx_coal_ticks / 10));
   1595 	else
   1596 		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
   1597 	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
   1598 	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
   1599 	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
   1600 	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
   1601 	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
   1602 
   1603 	/* Turn interrupts on. */
   1604 	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
   1605 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   1606 
   1607 	/* Start CPU. */
   1608 	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
   1609 
   1610 	return(0);
   1611 }
   1612 
   1613 /*
   1614  * look for id in the device list, returning the first match
   1615  */
   1616 static const struct ti_type *
   1617 ti_type_match(pa)
   1618 	struct pci_attach_args *pa;
   1619 {
   1620 	const struct ti_type          *t;
   1621 
   1622 	t = ti_devs;
   1623 	while(t->ti_name != NULL) {
   1624 		if ((PCI_VENDOR(pa->pa_id) == t->ti_vid) &&
   1625 		    (PCI_PRODUCT(pa->pa_id) == t->ti_did)) {
   1626 			return (t);
   1627 		}
   1628 		t++;
   1629 	}
   1630 
   1631 	return(NULL);
   1632 }
   1633 
   1634 /*
   1635  * Probe for a Tigon chip. Check the PCI vendor and device IDs
   1636  * against our list and return its name if we find a match.
   1637  */
   1638 static int ti_probe(parent, match, aux)
   1639 	struct device *parent;
   1640 	struct cfdata *match;
   1641 	void *aux;
   1642 {
   1643 	struct pci_attach_args *pa = aux;
   1644 	const struct ti_type		*t;
   1645 
   1646 	t = ti_type_match(pa);
   1647 
   1648 	return((t == NULL) ? 0 : 1);
   1649 }
   1650 
   1651 static void ti_attach(parent, self, aux)
   1652 	struct device *parent, *self;
   1653 	void *aux;
   1654 {
   1655 	u_int32_t		command;
   1656 	struct ifnet		*ifp;
   1657 	struct ti_softc		*sc;
   1658 	u_char eaddr[ETHER_ADDR_LEN];
   1659 	struct pci_attach_args *pa = aux;
   1660 	pci_chipset_tag_t pc = pa->pa_pc;
   1661 	pci_intr_handle_t ih;
   1662 	const char *intrstr = NULL;
   1663 	bus_dma_segment_t dmaseg;
   1664 	int error, dmanseg, nolinear;
   1665 	const struct ti_type		*t;
   1666 
   1667 	t = ti_type_match(pa);
   1668 	if (t == NULL) {
   1669 		printf("ti_attach: were did the card go ?\n");
   1670 		return;
   1671 	}
   1672 
   1673 	printf(": %s (rev. 0x%02x)\n", t->ti_name, PCI_REVISION(pa->pa_class));
   1674 
   1675 	sc = (struct ti_softc *)self;
   1676 
   1677 	/*
   1678 	 * Map control/status registers.
   1679 	 */
   1680 	nolinear = 0;
   1681 	if (pci_mapreg_map(pa, 0x10,
   1682 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
   1683 	    BUS_SPACE_MAP_LINEAR , &sc->ti_btag, &sc->ti_bhandle,
   1684 	    NULL, NULL)) {
   1685 		nolinear = 1;
   1686 		if (pci_mapreg_map(pa, 0x10,
   1687 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
   1688 		    0 , &sc->ti_btag, &sc->ti_bhandle, NULL, NULL)) {
   1689 			printf(": can't map memory space\n");
   1690 			return;
   1691 		}
   1692 	}
   1693 	if (nolinear == 0)
   1694 		sc->ti_vhandle = (void *)(sc->ti_bhandle); /* XXX XXX XXX */
   1695 	else
   1696 		sc->ti_vhandle = NULL;
   1697 
   1698 	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
   1699 	command |= PCI_COMMAND_MASTER_ENABLE;
   1700 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
   1701 
   1702 	/* Allocate interrupt */
   1703 	if (pci_intr_map(pa, &ih)) {
   1704 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
   1705 		return;;
   1706 	}
   1707 	intrstr = pci_intr_string(pc, ih);
   1708 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, ti_intr, sc);
   1709 	if (sc->sc_ih == NULL) {
   1710 		printf("%s: couldn't establish interrupt",
   1711 		    sc->sc_dev.dv_xname);
   1712 		if (intrstr != NULL)
   1713 			printf(" at %s", intrstr);
   1714 		printf("\n");
   1715 		return;;
   1716 	}
   1717 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
   1718 	/*
   1719 	 * Add shutdown hook so that DMA is disabled prior to reboot. Not
   1720 	 * doing do could allow DMA to corrupt kernel memory during the
   1721 	 * reboot before the driver initializes.
   1722 	 */
   1723 	(void) shutdownhook_establish(ti_shutdown, sc);
   1724 
   1725 	if (ti_chipinit(sc)) {
   1726 		printf("%s: chip initialization failed\n", self->dv_xname);
   1727 		goto fail2;
   1728 	}
   1729 	if (sc->ti_hwrev == TI_HWREV_TIGON && nolinear == 1) {
   1730 		printf("%s: memory space not mapped linear\n", self->dv_xname);
   1731 	}
   1732 
   1733 	/* Zero out the NIC's on-board SRAM. */
   1734 	ti_mem(sc, 0x2000, 0x100000 - 0x2000,  NULL);
   1735 
   1736 	/* Init again -- zeroing memory may have clobbered some registers. */
   1737 	if (ti_chipinit(sc)) {
   1738 		printf("%s: chip initialization failed\n", self->dv_xname);
   1739 		goto fail2;
   1740 	}
   1741 
   1742 	/*
   1743 	 * Get station address from the EEPROM. Note: the manual states
   1744 	 * that the MAC address is at offset 0x8c, however the data is
   1745 	 * stored as two longwords (since that's how it's loaded into
   1746 	 * the NIC). This means the MAC address is actually preceeded
   1747 	 * by two zero bytes. We need to skip over those.
   1748 	 */
   1749 	if (ti_read_eeprom(sc, (caddr_t)&eaddr,
   1750 				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
   1751 		printf("%s: failed to read station address\n", self->dv_xname);
   1752 		goto fail2;
   1753 	}
   1754 
   1755 	/*
   1756 	 * A Tigon chip was detected. Inform the world.
   1757 	 */
   1758 	printf("%s: Ethernet address: %s\n", self->dv_xname,
   1759 				ether_sprintf(eaddr));
   1760 
   1761 	sc->sc_dmat = pa->pa_dmat;
   1762 
   1763 	/* Allocate the general information block and ring buffers. */
   1764 	if ((error = bus_dmamem_alloc(sc->sc_dmat,
   1765 	    sizeof(struct ti_ring_data), PAGE_SIZE, 0, &dmaseg, 1, &dmanseg,
   1766 	    BUS_DMA_NOWAIT)) != 0) {
   1767 		printf("%s: can't allocate ring buffer, error = %d\n",
   1768 		       sc->sc_dev.dv_xname, error);
   1769 		goto fail2;
   1770 	}
   1771 
   1772 	if ((error = bus_dmamem_map(sc->sc_dmat, &dmaseg, dmanseg,
   1773 	    sizeof(struct ti_ring_data), (caddr_t *)&sc->ti_rdata,
   1774 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1775 		printf("%s: can't map ring buffer, error = %d\n",
   1776 		       sc->sc_dev.dv_xname, error);
   1777 		goto fail2;
   1778 	}
   1779 
   1780 	if ((error = bus_dmamap_create(sc->sc_dmat,
   1781 	    sizeof(struct ti_ring_data), 1,
   1782 	    sizeof(struct ti_ring_data), 0, BUS_DMA_NOWAIT,
   1783 	    &sc->info_dmamap)) != 0) {
   1784 		printf("%s: can't create ring buffer DMA map, error = %d\n",
   1785 		       sc->sc_dev.dv_xname, error);
   1786 		goto fail2;
   1787 	}
   1788 
   1789 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->info_dmamap,
   1790 	    sc->ti_rdata, sizeof(struct ti_ring_data), NULL,
   1791 	    BUS_DMA_NOWAIT)) != 0) {
   1792 		printf("%s: can't load ring buffer DMA map, error = %d\n",
   1793 		       sc->sc_dev.dv_xname, error);
   1794 		goto fail2;
   1795 	}
   1796 
   1797 	sc->info_dmaaddr = sc->info_dmamap->dm_segs[0].ds_addr;
   1798 
   1799 	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
   1800 
   1801 	/* Try to allocate memory for jumbo buffers. */
   1802 	if (ti_alloc_jumbo_mem(sc)) {
   1803 		printf("%s: jumbo buffer allocation failed\n", self->dv_xname);
   1804 		goto fail2;
   1805 	}
   1806 
   1807 	SIMPLEQ_INIT(&sc->ti_mc_listhead);
   1808 
   1809 	/*
   1810 	 * We really need a better way to tell a 1000baseTX card
   1811 	 * from a 1000baseSX one, since in theory there could be
   1812 	 * OEMed 1000baseTX cards from lame vendors who aren't
   1813 	 * clever enough to change the PCI ID. For the moment
   1814 	 * though, the AceNIC is the only copper card available.
   1815 	 */
   1816 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALTEON &&
   1817 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALTEON_ACENIC_COPPER) ||
   1818 	    (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NETGEAR &&
   1819 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NETGEAR_GA620T))
   1820 		sc->ti_copper = 1;
   1821 	else
   1822 		sc->ti_copper = 0;
   1823 
   1824 	/* Set default tuneable values. */
   1825 	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
   1826 	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
   1827 	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
   1828 	sc->ti_rx_max_coal_bds = 64;
   1829 	sc->ti_tx_max_coal_bds = 128;
   1830 	sc->ti_tx_buf_ratio = 21;
   1831 
   1832 	/* Set up ifnet structure */
   1833 	ifp = &sc->ethercom.ec_if;
   1834 	ifp->if_softc = sc;
   1835 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
   1836 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
   1837 	ifp->if_ioctl = ti_ioctl;
   1838 	ifp->if_start = ti_start;
   1839 	ifp->if_watchdog = ti_watchdog;
   1840 	IFQ_SET_READY(&ifp->if_snd);
   1841 
   1842 #if 0
   1843 	/*
   1844 	 * XXX This is not really correct -- we don't necessarily
   1845 	 * XXX want to queue up as many as we can transmit at the
   1846 	 * XXX upper layer like that.  Someone with a board should
   1847 	 * XXX check to see how this affects performance.
   1848 	 */
   1849 	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
   1850 #endif
   1851 
   1852 	/*
   1853 	 * We can support 802.1Q VLAN-sized frames.
   1854 	 */
   1855 	sc->ethercom.ec_capabilities |=
   1856 	    ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING;
   1857 
   1858 	/*
   1859 	 * We can do IPv4, TCPv4, and UDPv4 checksums in hardware.
   1860 	 */
   1861 	ifp->if_capabilities |= IFCAP_CSUM_IPv4 | IFCAP_CSUM_TCPv4 |
   1862 	    IFCAP_CSUM_UDPv4;
   1863 
   1864 	/* Set up ifmedia support. */
   1865 	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
   1866 	if (sc->ti_copper) {
   1867                 /*
   1868                  * Copper cards allow manual 10/100 mode selection,
   1869                  * but not manual 1000baseTX mode selection. Why?
   1870                  * Becuase currently there's no way to specify the
   1871                  * master/slave setting through the firmware interface,
   1872                  * so Alteon decided to just bag it and handle it
   1873                  * via autonegotiation.
   1874                  */
   1875                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
   1876                 ifmedia_add(&sc->ifmedia,
   1877                     IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
   1878                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
   1879                 ifmedia_add(&sc->ifmedia,
   1880                     IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
   1881                 ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_TX, 0, NULL);
   1882                 ifmedia_add(&sc->ifmedia,
   1883                     IFM_ETHER|IFM_1000_TX|IFM_FDX, 0, NULL);
   1884 	} else {
   1885 		/* Fiber cards don't support 10/100 modes. */
   1886 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
   1887 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
   1888 	}
   1889 	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
   1890 	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
   1891 
   1892 	/*
   1893 	 * Call MI attach routines.
   1894 	 */
   1895 	if_attach(ifp);
   1896 	ether_ifattach(ifp, eaddr);
   1897 
   1898 	return;
   1899 fail2:
   1900 	pci_intr_disestablish(pc, sc->sc_ih);
   1901 	return;
   1902 }
   1903 
   1904 /*
   1905  * Frame reception handling. This is called if there's a frame
   1906  * on the receive return list.
   1907  *
   1908  * Note: we have to be able to handle three possibilities here:
   1909  * 1) the frame is from the mini receive ring (can only happen)
   1910  *    on Tigon 2 boards)
   1911  * 2) the frame is from the jumbo recieve ring
   1912  * 3) the frame is from the standard receive ring
   1913  */
   1914 
   1915 static void ti_rxeof(sc)
   1916 	struct ti_softc		*sc;
   1917 {
   1918 	struct ifnet		*ifp;
   1919 	struct ti_cmd_desc	cmd;
   1920 
   1921 	ifp = &sc->ethercom.ec_if;
   1922 
   1923 	while(sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
   1924 		struct ti_rx_desc	*cur_rx;
   1925 		u_int32_t		rxidx;
   1926 		struct mbuf		*m = NULL;
   1927 		u_int16_t		vlan_tag = 0;
   1928 		int			have_tag = 0;
   1929 		struct ether_header	*eh;
   1930 		bus_dmamap_t dmamap;
   1931 
   1932 		cur_rx =
   1933 		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
   1934 		rxidx = cur_rx->ti_idx;
   1935 		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
   1936 
   1937 		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
   1938 			have_tag = 1;
   1939 			vlan_tag = cur_rx->ti_vlan_tag;
   1940 		}
   1941 
   1942 		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
   1943 			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
   1944 			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
   1945 			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
   1946 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1947 				ifp->if_ierrors++;
   1948 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
   1949 				continue;
   1950 			}
   1951 			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL)
   1952 			    == ENOBUFS) {
   1953 				ifp->if_ierrors++;
   1954 				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
   1955 				continue;
   1956 			}
   1957 		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
   1958 			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
   1959 			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
   1960 			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
   1961 			dmamap = sc->mini_dmamap[rxidx];
   1962 			sc->mini_dmamap[rxidx] = 0;
   1963 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1964 				ifp->if_ierrors++;
   1965 				ti_newbuf_mini(sc, sc->ti_mini, m, dmamap);
   1966 				continue;
   1967 			}
   1968 			if (ti_newbuf_mini(sc, sc->ti_mini, NULL, dmamap)
   1969 			    == ENOBUFS) {
   1970 				ifp->if_ierrors++;
   1971 				ti_newbuf_mini(sc, sc->ti_mini, m, dmamap);
   1972 				continue;
   1973 			}
   1974 		} else {
   1975 			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
   1976 			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
   1977 			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
   1978 			dmamap = sc->std_dmamap[rxidx];
   1979 			sc->std_dmamap[rxidx] = 0;
   1980 			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
   1981 				ifp->if_ierrors++;
   1982 				ti_newbuf_std(sc, sc->ti_std, m, dmamap);
   1983 				continue;
   1984 			}
   1985 			if (ti_newbuf_std(sc, sc->ti_std, NULL, dmamap)
   1986 			    == ENOBUFS) {
   1987 				ifp->if_ierrors++;
   1988 				ti_newbuf_std(sc, sc->ti_std, m, dmamap);
   1989 				continue;
   1990 			}
   1991 		}
   1992 
   1993 		m->m_pkthdr.len = m->m_len = cur_rx->ti_len;
   1994 		ifp->if_ipackets++;
   1995 		m->m_pkthdr.rcvif = ifp;
   1996 
   1997 #if NBPFILTER > 0
   1998 		/*
   1999 	 	 * Handle BPF listeners. Let the BPF user see the packet, but
   2000 	 	 * don't pass it up to the ether_input() layer unless it's
   2001 	 	 * a broadcast packet, multicast packet, matches our ethernet
   2002 	 	 * address or the interface is in promiscuous mode.
   2003 	 	 */
   2004 		if (ifp->if_bpf)
   2005 			bpf_mtap(ifp->if_bpf, m);
   2006 #endif
   2007 
   2008 		eh = mtod(m, struct ether_header *);
   2009 		switch (ntohs(eh->ether_type)) {
   2010 		case ETHERTYPE_IP:
   2011 		    {
   2012 			struct ip *ip = (struct ip *) (eh + 1);
   2013 
   2014 			/*
   2015 			 * Note the Tigon firmware does not invert
   2016 			 * the checksum for us, hence the XOR.
   2017 			 */
   2018 			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
   2019 			if ((cur_rx->ti_ip_cksum ^ 0xffff) != 0)
   2020 				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
   2021 			/*
   2022 			 * ntohs() the constant so the compiler can
   2023 			 * optimize...
   2024 			 *
   2025 			 * XXX Figure out a sane way to deal with
   2026 			 * fragmented packets.
   2027 			 */
   2028 			if ((ip->ip_off & htons(IP_MF|IP_OFFMASK)) == 0) {
   2029 				switch (ip->ip_p) {
   2030 				case IPPROTO_TCP:
   2031 					m->m_pkthdr.csum_data =
   2032 					    cur_rx->ti_tcp_udp_cksum;
   2033 					m->m_pkthdr.csum_flags |=
   2034 					    M_CSUM_TCPv4|M_CSUM_DATA;
   2035 					break;
   2036 				case IPPROTO_UDP:
   2037 					m->m_pkthdr.csum_data =
   2038 					    cur_rx->ti_tcp_udp_cksum;
   2039 					m->m_pkthdr.csum_flags |=
   2040 					    M_CSUM_UDPv4|M_CSUM_DATA;
   2041 					break;
   2042 				default:
   2043 					/* Nothing */;
   2044 				}
   2045 			}
   2046 			break;
   2047 		    }
   2048 		default:
   2049 			/* Nothing. */
   2050 			break;
   2051 		}
   2052 
   2053 		if (have_tag) {
   2054 			struct mbuf *n;
   2055 			n = m_aux_add(m, AF_LINK, ETHERTYPE_VLAN);
   2056 			if (n) {
   2057 				*mtod(n, int *) = vlan_tag;
   2058 				n->m_len = sizeof(int);
   2059 			} else {
   2060 				printf("%s: no mbuf for tag\n", ifp->if_xname);
   2061 				m_freem(m);
   2062 				continue;
   2063 			}
   2064 			have_tag = vlan_tag = 0;
   2065 		}
   2066 		(*ifp->if_input)(ifp, m);
   2067 	}
   2068 
   2069 	/* Only necessary on the Tigon 1. */
   2070 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   2071 		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
   2072 		    sc->ti_rx_saved_considx);
   2073 
   2074 	TI_UPDATE_STDPROD(sc, sc->ti_std);
   2075 	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
   2076 	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
   2077 
   2078 	return;
   2079 }
   2080 
   2081 static void ti_txeof(sc)
   2082 	struct ti_softc		*sc;
   2083 {
   2084 	struct ti_tx_desc	*cur_tx = NULL;
   2085 	struct ifnet		*ifp;
   2086 
   2087 	ifp = &sc->ethercom.ec_if;
   2088 
   2089 	/*
   2090 	 * Go through our tx ring and free mbufs for those
   2091 	 * frames that have been sent.
   2092 	 */
   2093 	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
   2094 		u_int32_t		idx = 0;
   2095 
   2096 		idx = sc->ti_tx_saved_considx;
   2097 		if (sc->ti_hwrev == TI_HWREV_TIGON) {
   2098 			if (idx > 383)
   2099 				CSR_WRITE_4(sc, TI_WINBASE,
   2100 				    TI_TX_RING_BASE + 6144);
   2101 			else if (idx > 255)
   2102 				CSR_WRITE_4(sc, TI_WINBASE,
   2103 				    TI_TX_RING_BASE + 4096);
   2104 			else if (idx > 127)
   2105 				CSR_WRITE_4(sc, TI_WINBASE,
   2106 				    TI_TX_RING_BASE + 2048);
   2107 			else
   2108 				CSR_WRITE_4(sc, TI_WINBASE,
   2109 				    TI_TX_RING_BASE);
   2110 			cur_tx = &sc->ti_rdata->ti_tx_ring_nic[idx % 128];
   2111 		} else
   2112 			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
   2113 		if (cur_tx->ti_flags & TI_BDFLAG_END)
   2114 			ifp->if_opackets++;
   2115 		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
   2116 			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
   2117 			sc->ti_cdata.ti_tx_chain[idx] = NULL;
   2118 
   2119 			/* if (sc->txdma[idx] == 0) panic() */
   2120 			SIMPLEQ_INSERT_HEAD(&sc->txdma_list, sc->txdma[idx],
   2121 					    link);
   2122 			sc->txdma[idx] = 0;
   2123 		}
   2124 		sc->ti_txcnt--;
   2125 		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
   2126 		ifp->if_timer = 0;
   2127 	}
   2128 
   2129 	if (cur_tx != NULL)
   2130 		ifp->if_flags &= ~IFF_OACTIVE;
   2131 
   2132 	return;
   2133 }
   2134 
   2135 static int ti_intr(xsc)
   2136 	void			*xsc;
   2137 {
   2138 	struct ti_softc		*sc;
   2139 	struct ifnet		*ifp;
   2140 
   2141 	sc = xsc;
   2142 	ifp = &sc->ethercom.ec_if;
   2143 
   2144 #ifdef notdef
   2145 	/* Avoid this for now -- checking this register is expensive. */
   2146 	/* Make sure this is really our interrupt. */
   2147 	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE))
   2148 		return (0);
   2149 #endif
   2150 
   2151 	/* Ack interrupt and stop others from occuring. */
   2152 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   2153 
   2154 	if (ifp->if_flags & IFF_RUNNING) {
   2155 		/* Check RX return ring producer/consumer */
   2156 		ti_rxeof(sc);
   2157 
   2158 		/* Check TX ring producer/consumer */
   2159 		ti_txeof(sc);
   2160 	}
   2161 
   2162 	ti_handle_events(sc);
   2163 
   2164 	/* Re-enable interrupts. */
   2165 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   2166 
   2167 	if ((ifp->if_flags & IFF_RUNNING) != 0 &&
   2168 	    IFQ_IS_EMPTY(&ifp->if_snd) == 0)
   2169 		ti_start(ifp);
   2170 
   2171 	return (1);
   2172 }
   2173 
   2174 static void ti_stats_update(sc)
   2175 	struct ti_softc		*sc;
   2176 {
   2177 	struct ifnet		*ifp;
   2178 
   2179 	ifp = &sc->ethercom.ec_if;
   2180 
   2181 	ifp->if_collisions +=
   2182 	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
   2183 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
   2184 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
   2185 	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
   2186 	   ifp->if_collisions;
   2187 
   2188 	return;
   2189 }
   2190 
   2191 /*
   2192  * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
   2193  * pointers to descriptors.
   2194  */
   2195 static int ti_encap(sc, m_head, txidx)
   2196 	struct ti_softc		*sc;
   2197 	struct mbuf		*m_head;
   2198 	u_int32_t		*txidx;
   2199 {
   2200 	struct ti_tx_desc	*f = NULL;
   2201 	u_int32_t		frag, cur, cnt = 0;
   2202 	struct txdmamap_pool_entry *dma;
   2203 	bus_dmamap_t dmamap;
   2204 	int error, i;
   2205 	struct mbuf *n;
   2206 	u_int16_t csum_flags = 0;
   2207 
   2208 	dma = SIMPLEQ_FIRST(&sc->txdma_list);
   2209 	if (dma == NULL) {
   2210 		return ENOMEM;
   2211 	}
   2212 	dmamap = dma->dmamap;
   2213 
   2214 	error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m_head, 0);
   2215 	if (error) {
   2216 		struct mbuf *m;
   2217 		int i = 0;
   2218 		for (m = m_head; m; m = m->m_next)
   2219 			i++;
   2220 		printf("ti_encap: bus_dmamap_load_mbuf (len %d, %d frags) "
   2221 		       "error %d\n", m_head->m_pkthdr.len, i, error);
   2222 		return (ENOMEM);
   2223 	}
   2224 
   2225 	cur = frag = *txidx;
   2226 
   2227 	if (m_head->m_pkthdr.csum_flags & M_CSUM_IPv4) {
   2228 		/* IP header checksum field must be 0! */
   2229 		csum_flags |= TI_BDFLAG_IP_CKSUM;
   2230 	}
   2231 	if (m_head->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4))
   2232 		csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
   2233 
   2234 	/* XXX fragmented packet checksum capability? */
   2235 
   2236 	/*
   2237  	 * Start packing the mbufs in this chain into
   2238 	 * the fragment pointers. Stop when we run out
   2239  	 * of fragments or hit the end of the mbuf chain.
   2240 	 */
   2241 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2242 			if (sc->ti_hwrev == TI_HWREV_TIGON) {
   2243 				if (frag > 383)
   2244 					CSR_WRITE_4(sc, TI_WINBASE,
   2245 					    TI_TX_RING_BASE + 6144);
   2246 				else if (frag > 255)
   2247 					CSR_WRITE_4(sc, TI_WINBASE,
   2248 					    TI_TX_RING_BASE + 4096);
   2249 				else if (frag > 127)
   2250 					CSR_WRITE_4(sc, TI_WINBASE,
   2251 					    TI_TX_RING_BASE + 2048);
   2252 				else
   2253 					CSR_WRITE_4(sc, TI_WINBASE,
   2254 					    TI_TX_RING_BASE);
   2255 				f = &sc->ti_rdata->ti_tx_ring_nic[frag % 128];
   2256 			} else
   2257 				f = &sc->ti_rdata->ti_tx_ring[frag];
   2258 			if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
   2259 				break;
   2260 			TI_HOSTADDR(f->ti_addr) = dmamap->dm_segs[i].ds_addr;
   2261 			f->ti_len = dmamap->dm_segs[i].ds_len;
   2262 			f->ti_flags = csum_flags;
   2263 			n = m_aux_find(m_head, AF_LINK, ETHERTYPE_VLAN);
   2264 			if (n) {
   2265 				f->ti_flags |= TI_BDFLAG_VLAN_TAG;
   2266 				f->ti_vlan_tag = *mtod(n, int *);
   2267 			} else {
   2268 				f->ti_vlan_tag = 0;
   2269 			}
   2270 			/*
   2271 			 * Sanity check: avoid coming within 16 descriptors
   2272 			 * of the end of the ring.
   2273 			 */
   2274 			if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
   2275 				return(ENOBUFS);
   2276 			cur = frag;
   2277 			TI_INC(frag, TI_TX_RING_CNT);
   2278 			cnt++;
   2279 	}
   2280 
   2281 	if (i < dmamap->dm_nsegs)
   2282 		return(ENOBUFS);
   2283 
   2284 	if (frag == sc->ti_tx_saved_considx)
   2285 		return(ENOBUFS);
   2286 
   2287 	if (sc->ti_hwrev == TI_HWREV_TIGON)
   2288 		sc->ti_rdata->ti_tx_ring_nic[cur % 128].ti_flags |=
   2289 		    TI_BDFLAG_END;
   2290 	else
   2291 		sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
   2292 	sc->ti_cdata.ti_tx_chain[cur] = m_head;
   2293 	SIMPLEQ_REMOVE_HEAD(&sc->txdma_list, dma, link);
   2294 	sc->txdma[cur] = dma;
   2295 	sc->ti_txcnt += cnt;
   2296 
   2297 	*txidx = frag;
   2298 
   2299 	return(0);
   2300 }
   2301 
   2302 /*
   2303  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
   2304  * to the mbuf data regions directly in the transmit descriptors.
   2305  */
   2306 static void ti_start(ifp)
   2307 	struct ifnet		*ifp;
   2308 {
   2309 	struct ti_softc		*sc;
   2310 	struct mbuf		*m_head = NULL;
   2311 	u_int32_t		prodidx = 0;
   2312 
   2313 	sc = ifp->if_softc;
   2314 
   2315 	prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
   2316 
   2317 	while (sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
   2318 		IFQ_POLL(&ifp->if_snd, m_head);
   2319 		if (m_head == NULL)
   2320 			break;
   2321 
   2322 		/*
   2323 		 * Pack the data into the transmit ring. If we
   2324 		 * don't have room, set the OACTIVE flag and wait
   2325 		 * for the NIC to drain the ring.
   2326 		 */
   2327 		if (ti_encap(sc, m_head, &prodidx)) {
   2328 			ifp->if_flags |= IFF_OACTIVE;
   2329 			break;
   2330 		}
   2331 
   2332 		IFQ_DEQUEUE(&ifp->if_snd, m_head);
   2333 
   2334 		/*
   2335 		 * If there's a BPF listener, bounce a copy of this frame
   2336 		 * to him.
   2337 		 */
   2338 #if NBPFILTER > 0
   2339 		if (ifp->if_bpf)
   2340 			bpf_mtap(ifp->if_bpf, m_head);
   2341 #endif
   2342 	}
   2343 
   2344 	/* Transmit */
   2345 	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
   2346 
   2347 	/*
   2348 	 * Set a timeout in case the chip goes out to lunch.
   2349 	 */
   2350 	ifp->if_timer = 5;
   2351 
   2352 	return;
   2353 }
   2354 
   2355 static void ti_init(xsc)
   2356 	void			*xsc;
   2357 {
   2358 	struct ti_softc		*sc = xsc;
   2359         int			s;
   2360 
   2361 	s = splnet();
   2362 
   2363 	/* Cancel pending I/O and flush buffers. */
   2364 	ti_stop(sc);
   2365 
   2366 	/* Init the gen info block, ring control blocks and firmware. */
   2367 	if (ti_gibinit(sc)) {
   2368 		printf("%s: initialization failure\n", sc->sc_dev.dv_xname);
   2369 		splx(s);
   2370 		return;
   2371 	}
   2372 
   2373 	splx(s);
   2374 
   2375 	return;
   2376 }
   2377 
   2378 static void ti_init2(sc)
   2379 	struct ti_softc		*sc;
   2380 {
   2381 	struct ti_cmd_desc	cmd;
   2382 	struct ifnet		*ifp;
   2383 	u_int8_t		*m;
   2384 	struct ifmedia		*ifm;
   2385 	int			tmp;
   2386 
   2387 	ifp = &sc->ethercom.ec_if;
   2388 
   2389 	/* Specify MTU and interface index. */
   2390 	CSR_WRITE_4(sc, TI_GCR_IFINDEX, sc->sc_dev.dv_unit); /* ??? */
   2391 
   2392 	tmp = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
   2393 	if (sc->ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   2394 		tmp += ETHER_VLAN_ENCAP_LEN;
   2395 	CSR_WRITE_4(sc, TI_GCR_IFMTU, tmp);
   2396 
   2397 	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
   2398 
   2399 	/* Load our MAC address. */
   2400 	m = (u_int8_t *)LLADDR(ifp->if_sadl);
   2401 	CSR_WRITE_4(sc, TI_GCR_PAR0, (m[0] << 8) | m[1]);
   2402 	CSR_WRITE_4(sc, TI_GCR_PAR1, (m[2] << 24) | (m[3] << 16)
   2403 		    | (m[4] << 8) | m[5]);
   2404 	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
   2405 
   2406 	/* Enable or disable promiscuous mode as needed. */
   2407 	if (ifp->if_flags & IFF_PROMISC) {
   2408 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
   2409 	} else {
   2410 		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
   2411 	}
   2412 
   2413 	/* Program multicast filter. */
   2414 	ti_setmulti(sc);
   2415 
   2416 	/*
   2417 	 * If this is a Tigon 1, we should tell the
   2418 	 * firmware to use software packet filtering.
   2419 	 */
   2420 	if (sc->ti_hwrev == TI_HWREV_TIGON) {
   2421 		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
   2422 	}
   2423 
   2424 	/* Init RX ring. */
   2425 	ti_init_rx_ring_std(sc);
   2426 
   2427 	/* Init jumbo RX ring. */
   2428 	if (ifp->if_mtu > (MCLBYTES - ETHER_HDR_LEN - ETHER_CRC_LEN))
   2429 		ti_init_rx_ring_jumbo(sc);
   2430 
   2431 	/*
   2432 	 * If this is a Tigon 2, we can also configure the
   2433 	 * mini ring.
   2434 	 */
   2435 	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
   2436 		ti_init_rx_ring_mini(sc);
   2437 
   2438 	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
   2439 	sc->ti_rx_saved_considx = 0;
   2440 
   2441 	/* Init TX ring. */
   2442 	ti_init_tx_ring(sc);
   2443 
   2444 	/* Tell firmware we're alive. */
   2445 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
   2446 
   2447 	/* Enable host interrupts. */
   2448 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
   2449 
   2450 	ifp->if_flags |= IFF_RUNNING;
   2451 	ifp->if_flags &= ~IFF_OACTIVE;
   2452 
   2453 	/*
   2454 	 * Make sure to set media properly. We have to do this
   2455 	 * here since we have to issue commands in order to set
   2456 	 * the link negotiation and we can't issue commands until
   2457 	 * the firmware is running.
   2458 	 */
   2459 	ifm = &sc->ifmedia;
   2460 	tmp = ifm->ifm_media;
   2461 	ifm->ifm_media = ifm->ifm_cur->ifm_media;
   2462 	ti_ifmedia_upd(ifp);
   2463 	ifm->ifm_media = tmp;
   2464 
   2465 	return;
   2466 }
   2467 
   2468 /*
   2469  * Set media options.
   2470  */
   2471 static int ti_ifmedia_upd(ifp)
   2472 	struct ifnet		*ifp;
   2473 {
   2474 	struct ti_softc		*sc;
   2475 	struct ifmedia		*ifm;
   2476 	struct ti_cmd_desc	cmd;
   2477 
   2478 	sc = ifp->if_softc;
   2479 	ifm = &sc->ifmedia;
   2480 
   2481 	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
   2482 		return(EINVAL);
   2483 
   2484 	switch(IFM_SUBTYPE(ifm->ifm_media)) {
   2485 	case IFM_AUTO:
   2486 		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
   2487 		    TI_GLNK_FULL_DUPLEX|TI_GLNK_RX_FLOWCTL_Y|
   2488 		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
   2489 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
   2490 		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX|
   2491 		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
   2492 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2493 		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
   2494 		break;
   2495 	case IFM_1000_SX:
   2496 	case IFM_1000_TX:
   2497 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
   2498 			CSR_WRITE_4(sc, TI_GCR_GLINK,
   2499 			    TI_GLNK_PREF|TI_GLNK_1000MB|TI_GLNK_FULL_DUPLEX|
   2500 			    TI_GLNK_RX_FLOWCTL_Y|TI_GLNK_ENB);
   2501 		} else {
   2502 			CSR_WRITE_4(sc, TI_GCR_GLINK,
   2503 			    TI_GLNK_PREF|TI_GLNK_1000MB|
   2504 			    TI_GLNK_RX_FLOWCTL_Y|TI_GLNK_ENB);
   2505 		}
   2506 		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
   2507 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2508 		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
   2509 		break;
   2510 	case IFM_100_FX:
   2511 	case IFM_10_FL:
   2512 	case IFM_100_TX:
   2513 	case IFM_10_T:
   2514 		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
   2515 		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF);
   2516 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
   2517 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
   2518 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
   2519 		} else {
   2520 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
   2521 		}
   2522 		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
   2523 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
   2524 		} else {
   2525 			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
   2526 		}
   2527 		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
   2528 		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
   2529 		break;
   2530 	}
   2531 
   2532 	sc->ethercom.ec_if.if_baudrate =
   2533 	    ifmedia_baudrate(ifm->ifm_media);
   2534 
   2535 	return(0);
   2536 }
   2537 
   2538 /*
   2539  * Report current media status.
   2540  */
   2541 static void ti_ifmedia_sts(ifp, ifmr)
   2542 	struct ifnet		*ifp;
   2543 	struct ifmediareq	*ifmr;
   2544 {
   2545 	struct ti_softc		*sc;
   2546 	u_int32_t               media = 0;
   2547 
   2548 	sc = ifp->if_softc;
   2549 
   2550 	ifmr->ifm_status = IFM_AVALID;
   2551 	ifmr->ifm_active = IFM_ETHER;
   2552 
   2553 	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
   2554 		return;
   2555 
   2556 	ifmr->ifm_status |= IFM_ACTIVE;
   2557 
   2558 	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
   2559 		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
   2560 		if (sc->ti_copper)
   2561 			ifmr->ifm_active |= IFM_1000_TX;
   2562 		else
   2563 			ifmr->ifm_active |= IFM_1000_SX;
   2564 		if (media & TI_GLNK_FULL_DUPLEX)
   2565 			ifmr->ifm_active |= IFM_FDX;
   2566 		else
   2567 			ifmr->ifm_active |= IFM_HDX;
   2568 	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
   2569 		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
   2570 		if (sc->ti_copper) {
   2571 			if (media & TI_LNK_100MB)
   2572 				ifmr->ifm_active |= IFM_100_TX;
   2573 			if (media & TI_LNK_10MB)
   2574 				ifmr->ifm_active |= IFM_10_T;
   2575 		} else {
   2576 			if (media & TI_LNK_100MB)
   2577 				ifmr->ifm_active |= IFM_100_FX;
   2578 			if (media & TI_LNK_10MB)
   2579 				ifmr->ifm_active |= IFM_10_FL;
   2580 		}
   2581 		if (media & TI_LNK_FULL_DUPLEX)
   2582 			ifmr->ifm_active |= IFM_FDX;
   2583 		if (media & TI_LNK_HALF_DUPLEX)
   2584 			ifmr->ifm_active |= IFM_HDX;
   2585 	}
   2586 
   2587 	sc->ethercom.ec_if.if_baudrate =
   2588 	    ifmedia_baudrate(sc->ifmedia.ifm_media);
   2589 
   2590 	return;
   2591 }
   2592 
   2593 static int
   2594 ti_ether_ioctl(ifp, cmd, data)
   2595 	struct ifnet *ifp;
   2596 	u_long cmd;
   2597 	caddr_t data;
   2598 {
   2599 	struct ifaddr *ifa = (struct ifaddr *) data;
   2600 	struct ti_softc *sc = ifp->if_softc;
   2601 
   2602 	switch (cmd) {
   2603 	case SIOCSIFADDR:
   2604 		ifp->if_flags |= IFF_UP;
   2605 
   2606 		switch (ifa->ifa_addr->sa_family) {
   2607 #ifdef INET
   2608 		case AF_INET:
   2609 			ti_init(sc);
   2610 			arp_ifinit(ifp, ifa);
   2611 			break;
   2612 #endif
   2613 #ifdef NS
   2614 		case AF_NS:
   2615 		    {
   2616 			 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   2617 
   2618 			 if (ns_nullhost(*ina))
   2619 				ina->x_host = *(union ns_host *)
   2620 				    LLADDR(ifp->if_sadl);
   2621 			 else
   2622 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
   2623 				    ifp->if_addrlen);
   2624 			 /* Set new address. */
   2625 			 ti_init(sc);
   2626 			 break;
   2627 		    }
   2628 #endif
   2629 		default:
   2630 			ti_init(sc);
   2631 			break;
   2632 		}
   2633 		break;
   2634 
   2635 	default:
   2636 		return (EINVAL);
   2637 	}
   2638 
   2639 	return (0);
   2640 }
   2641 
   2642 static int ti_ioctl(ifp, command, data)
   2643 	struct ifnet		*ifp;
   2644 	u_long			command;
   2645 	caddr_t			data;
   2646 {
   2647 	struct ti_softc		*sc = ifp->if_softc;
   2648 	struct ifreq		*ifr = (struct ifreq *) data;
   2649 	int			s, error = 0;
   2650 	struct ti_cmd_desc	cmd;
   2651 
   2652 	s = splnet();
   2653 
   2654 	switch(command) {
   2655 	case SIOCSIFADDR:
   2656 	case SIOCGIFADDR:
   2657 		error = ti_ether_ioctl(ifp, command, data);
   2658 		break;
   2659 	case SIOCSIFMTU:
   2660 		if (ifr->ifr_mtu > ETHERMTU_JUMBO)
   2661 			error = EINVAL;
   2662 		else {
   2663 			ifp->if_mtu = ifr->ifr_mtu;
   2664 			ti_init(sc);
   2665 		}
   2666 		break;
   2667 	case SIOCSIFFLAGS:
   2668 		if (ifp->if_flags & IFF_UP) {
   2669 			/*
   2670 			 * If only the state of the PROMISC flag changed,
   2671 			 * then just use the 'set promisc mode' command
   2672 			 * instead of reinitializing the entire NIC. Doing
   2673 			 * a full re-init means reloading the firmware and
   2674 			 * waiting for it to start up, which may take a
   2675 			 * second or two.
   2676 			 */
   2677 			if (ifp->if_flags & IFF_RUNNING &&
   2678 			    ifp->if_flags & IFF_PROMISC &&
   2679 			    !(sc->ti_if_flags & IFF_PROMISC)) {
   2680 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
   2681 				    TI_CMD_CODE_PROMISC_ENB, 0);
   2682 			} else if (ifp->if_flags & IFF_RUNNING &&
   2683 			    !(ifp->if_flags & IFF_PROMISC) &&
   2684 			    sc->ti_if_flags & IFF_PROMISC) {
   2685 				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
   2686 				    TI_CMD_CODE_PROMISC_DIS, 0);
   2687 			} else
   2688 				ti_init(sc);
   2689 		} else {
   2690 			if (ifp->if_flags & IFF_RUNNING) {
   2691 				ti_stop(sc);
   2692 			}
   2693 		}
   2694 		sc->ti_if_flags = ifp->if_flags;
   2695 		error = 0;
   2696 		break;
   2697 	case SIOCADDMULTI:
   2698 	case SIOCDELMULTI:
   2699 		error = (command == SIOCADDMULTI) ?
   2700 		    ether_addmulti(ifr, &sc->ethercom) :
   2701 		    ether_delmulti(ifr, &sc->ethercom);
   2702 		if (error == ENETRESET) {
   2703 			if (ifp->if_flags & IFF_RUNNING)
   2704 				ti_setmulti(sc);
   2705 			error = 0;
   2706 		}
   2707 		break;
   2708 	case SIOCSIFMEDIA:
   2709 	case SIOCGIFMEDIA:
   2710 		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
   2711 		break;
   2712 	default:
   2713 		error = EINVAL;
   2714 		break;
   2715 	}
   2716 
   2717 	(void)splx(s);
   2718 
   2719 	return(error);
   2720 }
   2721 
   2722 static void ti_watchdog(ifp)
   2723 	struct ifnet		*ifp;
   2724 {
   2725 	struct ti_softc		*sc;
   2726 
   2727 	sc = ifp->if_softc;
   2728 
   2729 	printf("%s: watchdog timeout -- resetting\n", sc->sc_dev.dv_xname);
   2730 	ti_stop(sc);
   2731 	ti_init(sc);
   2732 
   2733 	ifp->if_oerrors++;
   2734 
   2735 	return;
   2736 }
   2737 
   2738 /*
   2739  * Stop the adapter and free any mbufs allocated to the
   2740  * RX and TX lists.
   2741  */
   2742 static void ti_stop(sc)
   2743 	struct ti_softc		*sc;
   2744 {
   2745 	struct ifnet		*ifp;
   2746 	struct ti_cmd_desc	cmd;
   2747 
   2748 	ifp = &sc->ethercom.ec_if;
   2749 
   2750 	/* Disable host interrupts. */
   2751 	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
   2752 	/*
   2753 	 * Tell firmware we're shutting down.
   2754 	 */
   2755 	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
   2756 
   2757 	/* Halt and reinitialize. */
   2758 	ti_chipinit(sc);
   2759 	ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
   2760 	ti_chipinit(sc);
   2761 
   2762 	/* Free the RX lists. */
   2763 	ti_free_rx_ring_std(sc);
   2764 
   2765 	/* Free jumbo RX list. */
   2766 	ti_free_rx_ring_jumbo(sc);
   2767 
   2768 	/* Free mini RX list. */
   2769 	ti_free_rx_ring_mini(sc);
   2770 
   2771 	/* Free TX buffers. */
   2772 	ti_free_tx_ring(sc);
   2773 
   2774 	sc->ti_ev_prodidx.ti_idx = 0;
   2775 	sc->ti_return_prodidx.ti_idx = 0;
   2776 	sc->ti_tx_considx.ti_idx = 0;
   2777 	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
   2778 
   2779 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   2780 
   2781 	return;
   2782 }
   2783 
   2784 /*
   2785  * Stop all chip I/O so that the kernel's probe routines don't
   2786  * get confused by errant DMAs when rebooting.
   2787  */
   2788 static void ti_shutdown(v)
   2789 	void *v;
   2790 {
   2791 	struct ti_softc		*sc = v;
   2792 
   2793 	ti_chipinit(sc);
   2794 
   2795 	return;
   2796 }
   2797