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