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