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