if_ti.c revision 79472
145386Swpaul/*
245386Swpaul * Copyright (c) 1997, 1998, 1999
345386Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
445386Swpaul *
545386Swpaul * Redistribution and use in source and binary forms, with or without
645386Swpaul * modification, are permitted provided that the following conditions
745386Swpaul * are met:
845386Swpaul * 1. Redistributions of source code must retain the above copyright
945386Swpaul *    notice, this list of conditions and the following disclaimer.
1045386Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1145386Swpaul *    notice, this list of conditions and the following disclaimer in the
1245386Swpaul *    documentation and/or other materials provided with the distribution.
1345386Swpaul * 3. All advertising materials mentioning features or use of this software
1445386Swpaul *    must display the following acknowledgement:
1545386Swpaul *	This product includes software developed by Bill Paul.
1645386Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1745386Swpaul *    may be used to endorse or promote products derived from this software
1845386Swpaul *    without specific prior written permission.
1945386Swpaul *
2045386Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2145386Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2245386Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2345386Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2445386Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2545386Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2645386Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2745386Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2845386Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2945386Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3045386Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3145386Swpaul *
3250477Speter * $FreeBSD: head/sys/dev/ti/if_ti.c 79472 2001-07-09 17:58:42Z wpaul $
3345386Swpaul */
3445386Swpaul
3545386Swpaul/*
3645386Swpaul * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
3745386Swpaul * Manuals, sample driver and firmware source kits are available
3845386Swpaul * from http://www.alteon.com/support/openkits.
3945386Swpaul *
4045386Swpaul * Written by Bill Paul <wpaul@ctr.columbia.edu>
4145386Swpaul * Electrical Engineering Department
4245386Swpaul * Columbia University, New York City
4345386Swpaul */
4445386Swpaul
4545386Swpaul/*
4645386Swpaul * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
4745386Swpaul * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
4845386Swpaul * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
4945386Swpaul * Tigon supports hardware IP, TCP and UCP checksumming, multicast
5045386Swpaul * filtering and jumbo (9014 byte) frames. The hardware is largely
5145386Swpaul * controlled by firmware, which must be loaded into the NIC during
5245386Swpaul * initialization.
5345386Swpaul *
5445386Swpaul * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
5545386Swpaul * revision, which supports new features such as extended commands,
5645386Swpaul * extended jumbo receive ring desciptors and a mini receive ring.
5745386Swpaul *
5845386Swpaul * Alteon Networks is to be commended for releasing such a vast amount
5945386Swpaul * of development material for the Tigon NIC without requiring an NDA
6045386Swpaul * (although they really should have done it a long time ago). With
6145386Swpaul * any luck, the other vendors will finally wise up and follow Alteon's
6245386Swpaul * stellar example.
6345386Swpaul *
6445386Swpaul * The firmware for the Tigon 1 and 2 NICs is compiled directly into
6545386Swpaul * this driver by #including it as a C header file. This bloats the
6645386Swpaul * driver somewhat, but it's the easiest method considering that the
6745386Swpaul * driver code and firmware code need to be kept in sync. The source
6845386Swpaul * for the firmware is not provided with the FreeBSD distribution since
6945386Swpaul * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
7045386Swpaul *
7145386Swpaul * The following people deserve special thanks:
7245386Swpaul * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
7345386Swpaul *   for testing
7445386Swpaul * - Raymond Lee of Netgear, for providing a pair of Netgear
7545386Swpaul *   GA620 Tigon 2 boards for testing
7645386Swpaul * - Ulf Zimmermann, for bringing the GA260 to my attention and
7745386Swpaul *   convincing me to write this driver.
7845386Swpaul * - Andrew Gallatin for providing FreeBSD/Alpha support.
7945386Swpaul */
8045386Swpaul
8145386Swpaul#include "vlan.h"
8245386Swpaul
8345386Swpaul#include <sys/param.h>
8445386Swpaul#include <sys/systm.h>
8545386Swpaul#include <sys/sockio.h>
8645386Swpaul#include <sys/mbuf.h>
8745386Swpaul#include <sys/malloc.h>
8845386Swpaul#include <sys/kernel.h>
8945386Swpaul#include <sys/socket.h>
9045386Swpaul#include <sys/queue.h>
9145386Swpaul
9245386Swpaul#include <net/if.h>
9345386Swpaul#include <net/if_arp.h>
9445386Swpaul#include <net/ethernet.h>
9545386Swpaul#include <net/if_dl.h>
9645386Swpaul#include <net/if_media.h>
9745386Swpaul
9845386Swpaul#include <net/bpf.h>
9945386Swpaul
10045386Swpaul#if NVLAN > 0
10145386Swpaul#include <net/if_types.h>
10245386Swpaul#include <net/if_vlan_var.h>
10345386Swpaul#endif
10445386Swpaul
10545386Swpaul#include <netinet/in_systm.h>
10645386Swpaul#include <netinet/in.h>
10745386Swpaul#include <netinet/ip.h>
10845386Swpaul
10945386Swpaul#include <vm/vm.h>              /* for vtophys */
11045386Swpaul#include <vm/pmap.h>            /* for vtophys */
11145386Swpaul#include <machine/bus_memio.h>
11245386Swpaul#include <machine/bus.h>
11349011Swpaul#include <machine/resource.h>
11449011Swpaul#include <sys/bus.h>
11549011Swpaul#include <sys/rman.h>
11645386Swpaul
11745386Swpaul#include <pci/pcireg.h>
11845386Swpaul#include <pci/pcivar.h>
11945386Swpaul
12045386Swpaul#include <pci/if_tireg.h>
12145386Swpaul#include <pci/ti_fw.h>
12245386Swpaul#include <pci/ti_fw2.h>
12345386Swpaul
12458698Sjlemon#define TI_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
12545386Swpaul
12645386Swpaul#if !defined(lint)
12745386Swpaulstatic const char rcsid[] =
12850477Speter  "$FreeBSD: head/sys/dev/ti/if_ti.c 79472 2001-07-09 17:58:42Z wpaul $";
12945386Swpaul#endif
13045386Swpaul
13145386Swpaul/*
13245386Swpaul * Various supported device vendors/types and their names.
13345386Swpaul */
13445386Swpaul
13545386Swpaulstatic struct ti_type ti_devs[] = {
13645386Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC,
13763702Swpaul		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
13863699Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC_COPPER,
13963702Swpaul		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
14045386Swpaul	{ TC_VENDORID,	TC_DEVICEID_3C985,
14145386Swpaul		"3Com 3c985-SX Gigabit Ethernet" },
14245386Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620,
14364139Swpaul		"Netgear GA620 1000baseSX Gigabit Ethernet" },
14464139Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620T,
14564139Swpaul		"Netgear GA620 1000baseT Gigabit Ethernet" },
14645386Swpaul	{ SGI_VENDORID, SGI_DEVICEID_TIGON,
14745386Swpaul		"Silicon Graphics Gigabit Ethernet" },
14856206Swpaul	{ DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
14956206Swpaul		"Farallon PN9000SX Gigabit Ethernet" },
15045386Swpaul	{ 0, 0, NULL }
15145386Swpaul};
15245386Swpaul
15349011Swpaulstatic int ti_probe		__P((device_t));
15449011Swpaulstatic int ti_attach		__P((device_t));
15549011Swpaulstatic int ti_detach		__P((device_t));
15645386Swpaulstatic void ti_txeof		__P((struct ti_softc *));
15745386Swpaulstatic void ti_rxeof		__P((struct ti_softc *));
15845386Swpaul
15945386Swpaulstatic void ti_stats_update	__P((struct ti_softc *));
16045386Swpaulstatic int ti_encap		__P((struct ti_softc *, struct mbuf *,
16145386Swpaul					u_int32_t *));
16245386Swpaul
16345386Swpaulstatic void ti_intr		__P((void *));
16445386Swpaulstatic void ti_start		__P((struct ifnet *));
16545386Swpaulstatic int ti_ioctl		__P((struct ifnet *, u_long, caddr_t));
16645386Swpaulstatic void ti_init		__P((void *));
16745386Swpaulstatic void ti_init2		__P((struct ti_softc *));
16845386Swpaulstatic void ti_stop		__P((struct ti_softc *));
16945386Swpaulstatic void ti_watchdog		__P((struct ifnet *));
17049011Swpaulstatic void ti_shutdown		__P((device_t));
17145386Swpaulstatic int ti_ifmedia_upd	__P((struct ifnet *));
17245386Swpaulstatic void ti_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
17345386Swpaul
17445386Swpaulstatic u_int32_t ti_eeprom_putbyte	__P((struct ti_softc *, int));
17545386Swpaulstatic u_int8_t	ti_eeprom_getbyte	__P((struct ti_softc *,
17645386Swpaul						int, u_int8_t *));
17745386Swpaulstatic int ti_read_eeprom	__P((struct ti_softc *, caddr_t, int, int));
17845386Swpaul
17945386Swpaulstatic void ti_add_mcast	__P((struct ti_softc *, struct ether_addr *));
18045386Swpaulstatic void ti_del_mcast	__P((struct ti_softc *, struct ether_addr *));
18145386Swpaulstatic void ti_setmulti		__P((struct ti_softc *));
18245386Swpaul
18345386Swpaulstatic void ti_mem		__P((struct ti_softc *, u_int32_t,
18445386Swpaul					u_int32_t, caddr_t));
18545386Swpaulstatic void ti_loadfw		__P((struct ti_softc *));
18645386Swpaulstatic void ti_cmd		__P((struct ti_softc *, struct ti_cmd_desc *));
18745386Swpaulstatic void ti_cmd_ext		__P((struct ti_softc *, struct ti_cmd_desc *,
18845386Swpaul					caddr_t, int));
18945386Swpaulstatic void ti_handle_events	__P((struct ti_softc *));
19045386Swpaulstatic int ti_alloc_jumbo_mem	__P((struct ti_softc *));
19145386Swpaulstatic void *ti_jalloc		__P((struct ti_softc *));
19264837Sdwmalonestatic void ti_jfree		__P((caddr_t, void *));
19345386Swpaulstatic int ti_newbuf_std	__P((struct ti_softc *, int, struct mbuf *));
19445386Swpaulstatic int ti_newbuf_mini	__P((struct ti_softc *, int, struct mbuf *));
19545386Swpaulstatic int ti_newbuf_jumbo	__P((struct ti_softc *, int, struct mbuf *));
19645386Swpaulstatic int ti_init_rx_ring_std	__P((struct ti_softc *));
19745386Swpaulstatic void ti_free_rx_ring_std	__P((struct ti_softc *));
19845386Swpaulstatic int ti_init_rx_ring_jumbo	__P((struct ti_softc *));
19945386Swpaulstatic void ti_free_rx_ring_jumbo	__P((struct ti_softc *));
20045386Swpaulstatic int ti_init_rx_ring_mini	__P((struct ti_softc *));
20145386Swpaulstatic void ti_free_rx_ring_mini	__P((struct ti_softc *));
20245386Swpaulstatic void ti_free_tx_ring	__P((struct ti_softc *));
20345386Swpaulstatic int ti_init_tx_ring	__P((struct ti_softc *));
20445386Swpaul
20545386Swpaulstatic int ti_64bitslot_war	__P((struct ti_softc *));
20645386Swpaulstatic int ti_chipinit		__P((struct ti_softc *));
20745386Swpaulstatic int ti_gibinit		__P((struct ti_softc *));
20845386Swpaul
20949011Swpaulstatic device_method_t ti_methods[] = {
21049011Swpaul	/* Device interface */
21149011Swpaul	DEVMETHOD(device_probe,		ti_probe),
21249011Swpaul	DEVMETHOD(device_attach,	ti_attach),
21349011Swpaul	DEVMETHOD(device_detach,	ti_detach),
21449011Swpaul	DEVMETHOD(device_shutdown,	ti_shutdown),
21549011Swpaul	{ 0, 0 }
21649011Swpaul};
21749011Swpaul
21849011Swpaulstatic driver_t ti_driver = {
21951455Swpaul	"ti",
22049011Swpaul	ti_methods,
22149011Swpaul	sizeof(struct ti_softc)
22249011Swpaul};
22349011Swpaul
22449011Swpaulstatic devclass_t ti_devclass;
22549011Swpaul
22651533SwpaulDRIVER_MODULE(if_ti, pci, ti_driver, ti_devclass, 0, 0);
22749011Swpaul
22845386Swpaul/*
22945386Swpaul * Send an instruction or address to the EEPROM, check for ACK.
23045386Swpaul */
23145386Swpaulstatic u_int32_t ti_eeprom_putbyte(sc, byte)
23245386Swpaul	struct ti_softc		*sc;
23345386Swpaul	int			byte;
23445386Swpaul{
23545386Swpaul	register int		i, ack = 0;
23645386Swpaul
23745386Swpaul	/*
23845386Swpaul	 * Make sure we're in TX mode.
23945386Swpaul	 */
24045386Swpaul	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
24145386Swpaul
24245386Swpaul	/*
24345386Swpaul	 * Feed in each bit and stobe the clock.
24445386Swpaul	 */
24545386Swpaul	for (i = 0x80; i; i >>= 1) {
24645386Swpaul		if (byte & i) {
24745386Swpaul			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
24845386Swpaul		} else {
24945386Swpaul			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
25045386Swpaul		}
25145386Swpaul		DELAY(1);
25245386Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
25345386Swpaul		DELAY(1);
25445386Swpaul		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
25545386Swpaul	}
25645386Swpaul
25745386Swpaul	/*
25845386Swpaul	 * Turn off TX mode.
25945386Swpaul	 */
26045386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
26145386Swpaul
26245386Swpaul	/*
26345386Swpaul	 * Check for ack.
26445386Swpaul	 */
26545386Swpaul	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
26645386Swpaul	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
26745386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
26845386Swpaul
26945386Swpaul	return(ack);
27045386Swpaul}
27145386Swpaul
27245386Swpaul/*
27345386Swpaul * Read a byte of data stored in the EEPROM at address 'addr.'
27445386Swpaul * We have to send two address bytes since the EEPROM can hold
27545386Swpaul * more than 256 bytes of data.
27645386Swpaul */
27745386Swpaulstatic u_int8_t ti_eeprom_getbyte(sc, addr, dest)
27845386Swpaul	struct ti_softc		*sc;
27945386Swpaul	int			addr;
28045386Swpaul	u_int8_t		*dest;
28145386Swpaul{
28245386Swpaul	register int		i;
28345386Swpaul	u_int8_t		byte = 0;
28445386Swpaul
28545386Swpaul	EEPROM_START;
28645386Swpaul
28745386Swpaul	/*
28845386Swpaul	 * Send write control code to EEPROM.
28945386Swpaul	 */
29045386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
29145386Swpaul		printf("ti%d: failed to send write command, status: %x\n",
29245386Swpaul		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
29345386Swpaul		return(1);
29445386Swpaul	}
29545386Swpaul
29645386Swpaul	/*
29745386Swpaul	 * Send first byte of address of byte we want to read.
29845386Swpaul	 */
29945386Swpaul	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
30045386Swpaul		printf("ti%d: failed to send address, status: %x\n",
30145386Swpaul		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
30245386Swpaul		return(1);
30345386Swpaul	}
30445386Swpaul	/*
30545386Swpaul	 * Send second byte address of byte we want to read.
30645386Swpaul	 */
30745386Swpaul	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
30845386Swpaul		printf("ti%d: failed to send address, status: %x\n",
30945386Swpaul		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
31045386Swpaul		return(1);
31145386Swpaul	}
31245386Swpaul
31345386Swpaul	EEPROM_STOP;
31445386Swpaul	EEPROM_START;
31545386Swpaul	/*
31645386Swpaul	 * Send read control code to EEPROM.
31745386Swpaul	 */
31845386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
31945386Swpaul		printf("ti%d: failed to send read command, status: %x\n",
32045386Swpaul		    sc->ti_unit, CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
32145386Swpaul		return(1);
32245386Swpaul	}
32345386Swpaul
32445386Swpaul	/*
32545386Swpaul	 * Start reading bits from EEPROM.
32645386Swpaul	 */
32745386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
32845386Swpaul	for (i = 0x80; i; i >>= 1) {
32945386Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
33045386Swpaul		DELAY(1);
33145386Swpaul		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
33245386Swpaul			byte |= i;
33345386Swpaul		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
33445386Swpaul		DELAY(1);
33545386Swpaul	}
33645386Swpaul
33745386Swpaul	EEPROM_STOP;
33845386Swpaul
33945386Swpaul	/*
34045386Swpaul	 * No ACK generated for read, so just return byte.
34145386Swpaul	 */
34245386Swpaul
34345386Swpaul	*dest = byte;
34445386Swpaul
34545386Swpaul	return(0);
34645386Swpaul}
34745386Swpaul
34845386Swpaul/*
34945386Swpaul * Read a sequence of bytes from the EEPROM.
35045386Swpaul */
35145386Swpaulstatic int ti_read_eeprom(sc, dest, off, cnt)
35245386Swpaul	struct ti_softc		*sc;
35345386Swpaul	caddr_t			dest;
35445386Swpaul	int			off;
35545386Swpaul	int			cnt;
35645386Swpaul{
35745386Swpaul	int			err = 0, i;
35845386Swpaul	u_int8_t		byte = 0;
35945386Swpaul
36045386Swpaul	for (i = 0; i < cnt; i++) {
36145386Swpaul		err = ti_eeprom_getbyte(sc, off + i, &byte);
36245386Swpaul		if (err)
36345386Swpaul			break;
36445386Swpaul		*(dest + i) = byte;
36545386Swpaul	}
36645386Swpaul
36745386Swpaul	return(err ? 1 : 0);
36845386Swpaul}
36945386Swpaul
37045386Swpaul/*
37145386Swpaul * NIC memory access function. Can be used to either clear a section
37245386Swpaul * of NIC local memory or (if buf is non-NULL) copy data into it.
37345386Swpaul */
37445386Swpaulstatic void ti_mem(sc, addr, len, buf)
37545386Swpaul	struct ti_softc		*sc;
37645386Swpaul	u_int32_t		addr, len;
37745386Swpaul	caddr_t			buf;
37845386Swpaul{
37945386Swpaul	int			segptr, segsize, cnt;
38045386Swpaul	caddr_t			ti_winbase, ptr;
38145386Swpaul
38245386Swpaul	segptr = addr;
38345386Swpaul	cnt = len;
38449133Swpaul	ti_winbase = (caddr_t)(sc->ti_vhandle + TI_WINDOW);
38545386Swpaul	ptr = buf;
38645386Swpaul
38745386Swpaul	while(cnt) {
38845386Swpaul		if (cnt < TI_WINLEN)
38945386Swpaul			segsize = cnt;
39045386Swpaul		else
39145386Swpaul			segsize = TI_WINLEN - (segptr % TI_WINLEN);
39245386Swpaul		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
39345386Swpaul		if (buf == NULL)
39445386Swpaul			bzero((char *)ti_winbase + (segptr &
39545386Swpaul			    (TI_WINLEN - 1)), segsize);
39645386Swpaul		else {
39745386Swpaul			bcopy((char *)ptr, (char *)ti_winbase +
39845386Swpaul			    (segptr & (TI_WINLEN - 1)), segsize);
39945386Swpaul			ptr += segsize;
40045386Swpaul		}
40145386Swpaul		segptr += segsize;
40245386Swpaul		cnt -= segsize;
40345386Swpaul	}
40445386Swpaul
40545386Swpaul	return;
40645386Swpaul}
40745386Swpaul
40845386Swpaul/*
40945386Swpaul * Load firmware image into the NIC. Check that the firmware revision
41045386Swpaul * is acceptable and see if we want the firmware for the Tigon 1 or
41145386Swpaul * Tigon 2.
41245386Swpaul */
41345386Swpaulstatic void ti_loadfw(sc)
41445386Swpaul	struct ti_softc		*sc;
41545386Swpaul{
41645386Swpaul	switch(sc->ti_hwrev) {
41745386Swpaul	case TI_HWREV_TIGON:
41845386Swpaul		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
41945386Swpaul		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
42045386Swpaul		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
42145386Swpaul			printf("ti%d: firmware revision mismatch; want "
42245386Swpaul			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
42345386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
42445386Swpaul			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
42545386Swpaul			    tigonFwReleaseMinor, tigonFwReleaseFix);
42645386Swpaul			return;
42745386Swpaul		}
42845386Swpaul		ti_mem(sc, tigonFwTextAddr, tigonFwTextLen,
42945386Swpaul		    (caddr_t)tigonFwText);
43045386Swpaul		ti_mem(sc, tigonFwDataAddr, tigonFwDataLen,
43145386Swpaul		    (caddr_t)tigonFwData);
43245386Swpaul		ti_mem(sc, tigonFwRodataAddr, tigonFwRodataLen,
43345386Swpaul		    (caddr_t)tigonFwRodata);
43445386Swpaul		ti_mem(sc, tigonFwBssAddr, tigonFwBssLen, NULL);
43545386Swpaul		ti_mem(sc, tigonFwSbssAddr, tigonFwSbssLen, NULL);
43645386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
43745386Swpaul		break;
43845386Swpaul	case TI_HWREV_TIGON_II:
43945386Swpaul		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
44045386Swpaul		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
44145386Swpaul		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
44245386Swpaul			printf("ti%d: firmware revision mismatch; want "
44345386Swpaul			    "%d.%d.%d, got %d.%d.%d\n", sc->ti_unit,
44445386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
44545386Swpaul			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
44645386Swpaul			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
44745386Swpaul			return;
44845386Swpaul		}
44945386Swpaul		ti_mem(sc, tigon2FwTextAddr, tigon2FwTextLen,
45045386Swpaul		    (caddr_t)tigon2FwText);
45145386Swpaul		ti_mem(sc, tigon2FwDataAddr, tigon2FwDataLen,
45245386Swpaul		    (caddr_t)tigon2FwData);
45345386Swpaul		ti_mem(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
45445386Swpaul		    (caddr_t)tigon2FwRodata);
45545386Swpaul		ti_mem(sc, tigon2FwBssAddr, tigon2FwBssLen, NULL);
45645386Swpaul		ti_mem(sc, tigon2FwSbssAddr, tigon2FwSbssLen, NULL);
45745386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
45845386Swpaul		break;
45945386Swpaul	default:
46045386Swpaul		printf("ti%d: can't load firmware: unknown hardware rev\n",
46145386Swpaul		    sc->ti_unit);
46245386Swpaul		break;
46345386Swpaul	}
46445386Swpaul
46545386Swpaul	return;
46645386Swpaul}
46745386Swpaul
46845386Swpaul/*
46945386Swpaul * Send the NIC a command via the command ring.
47045386Swpaul */
47145386Swpaulstatic void ti_cmd(sc, cmd)
47245386Swpaul	struct ti_softc		*sc;
47345386Swpaul	struct ti_cmd_desc	*cmd;
47445386Swpaul{
47545386Swpaul	u_int32_t		index;
47645386Swpaul
47745386Swpaul	if (sc->ti_rdata->ti_cmd_ring == NULL)
47845386Swpaul		return;
47945386Swpaul
48045386Swpaul	index = sc->ti_cmd_saved_prodidx;
48145386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
48245386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
48345386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
48445386Swpaul	sc->ti_cmd_saved_prodidx = index;
48545386Swpaul
48645386Swpaul	return;
48745386Swpaul}
48845386Swpaul
48945386Swpaul/*
49045386Swpaul * Send the NIC an extended command. The 'len' parameter specifies the
49145386Swpaul * number of command slots to include after the initial command.
49245386Swpaul */
49345386Swpaulstatic void ti_cmd_ext(sc, cmd, arg, len)
49445386Swpaul	struct ti_softc		*sc;
49545386Swpaul	struct ti_cmd_desc	*cmd;
49645386Swpaul	caddr_t			arg;
49745386Swpaul	int			len;
49845386Swpaul{
49945386Swpaul	u_int32_t		index;
50045386Swpaul	register int		i;
50145386Swpaul
50245386Swpaul	if (sc->ti_rdata->ti_cmd_ring == NULL)
50345386Swpaul		return;
50445386Swpaul
50545386Swpaul	index = sc->ti_cmd_saved_prodidx;
50645386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(u_int32_t *)(cmd));
50745386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
50845386Swpaul	for (i = 0; i < len; i++) {
50945386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
51045386Swpaul		    *(u_int32_t *)(&arg[i * 4]));
51145386Swpaul		TI_INC(index, TI_CMD_RING_CNT);
51245386Swpaul	}
51345386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
51445386Swpaul	sc->ti_cmd_saved_prodidx = index;
51545386Swpaul
51645386Swpaul	return;
51745386Swpaul}
51845386Swpaul
51945386Swpaul/*
52045386Swpaul * Handle events that have triggered interrupts.
52145386Swpaul */
52245386Swpaulstatic void ti_handle_events(sc)
52345386Swpaul	struct ti_softc		*sc;
52445386Swpaul{
52545386Swpaul	struct ti_event_desc	*e;
52645386Swpaul
52745386Swpaul	if (sc->ti_rdata->ti_event_ring == NULL)
52845386Swpaul		return;
52945386Swpaul
53045386Swpaul	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
53145386Swpaul		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
53245386Swpaul		switch(e->ti_event) {
53345386Swpaul		case TI_EV_LINKSTAT_CHANGED:
53445386Swpaul			sc->ti_linkstat = e->ti_code;
53545386Swpaul			if (e->ti_code == TI_EV_CODE_LINK_UP)
53645386Swpaul				printf("ti%d: 10/100 link up\n", sc->ti_unit);
53745386Swpaul			else if (e->ti_code == TI_EV_CODE_GIG_LINK_UP)
53845386Swpaul				printf("ti%d: gigabit link up\n", sc->ti_unit);
53945386Swpaul			else if (e->ti_code == TI_EV_CODE_LINK_DOWN)
54045386Swpaul				printf("ti%d: link down\n", sc->ti_unit);
54145386Swpaul			break;
54245386Swpaul		case TI_EV_ERROR:
54345386Swpaul			if (e->ti_code == TI_EV_CODE_ERR_INVAL_CMD)
54445386Swpaul				printf("ti%d: invalid command\n", sc->ti_unit);
54545386Swpaul			else if (e->ti_code == TI_EV_CODE_ERR_UNIMP_CMD)
54645386Swpaul				printf("ti%d: unknown command\n", sc->ti_unit);
54745386Swpaul			else if (e->ti_code == TI_EV_CODE_ERR_BADCFG)
54845386Swpaul				printf("ti%d: bad config data\n", sc->ti_unit);
54945386Swpaul			break;
55045386Swpaul		case TI_EV_FIRMWARE_UP:
55145386Swpaul			ti_init2(sc);
55245386Swpaul			break;
55345386Swpaul		case TI_EV_STATS_UPDATED:
55445386Swpaul			ti_stats_update(sc);
55545386Swpaul			break;
55645386Swpaul		case TI_EV_RESET_JUMBO_RING:
55745386Swpaul		case TI_EV_MCAST_UPDATED:
55845386Swpaul			/* Who cares. */
55945386Swpaul			break;
56045386Swpaul		default:
56145386Swpaul			printf("ti%d: unknown event: %d\n",
56245386Swpaul			    sc->ti_unit, e->ti_event);
56345386Swpaul			break;
56445386Swpaul		}
56545386Swpaul		/* Advance the consumer index. */
56645386Swpaul		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
56745386Swpaul		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
56845386Swpaul	}
56945386Swpaul
57045386Swpaul	return;
57145386Swpaul}
57245386Swpaul
57345386Swpaul/*
57445386Swpaul * Memory management for the jumbo receive ring is a pain in the
57545386Swpaul * butt. We need to allocate at least 9018 bytes of space per frame,
57645386Swpaul * _and_ it has to be contiguous (unless you use the extended
57745386Swpaul * jumbo descriptor format). Using malloc() all the time won't
57845386Swpaul * work: malloc() allocates memory in powers of two, which means we
57945386Swpaul * would end up wasting a considerable amount of space by allocating
58045386Swpaul * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
58145386Swpaul * to do our own memory management.
58245386Swpaul *
58345386Swpaul * The driver needs to allocate a contiguous chunk of memory at boot
58445386Swpaul * time. We then chop this up ourselves into 9K pieces and use them
58545386Swpaul * as external mbuf storage.
58645386Swpaul *
58745386Swpaul * One issue here is how much memory to allocate. The jumbo ring has
58845386Swpaul * 256 slots in it, but at 9K per slot than can consume over 2MB of
58945386Swpaul * RAM. This is a bit much, especially considering we also need
59045386Swpaul * RAM for the standard ring and mini ring (on the Tigon 2). To
59145386Swpaul * save space, we only actually allocate enough memory for 64 slots
59245386Swpaul * by default, which works out to between 500 and 600K. This can
59345386Swpaul * be tuned by changing a #define in if_tireg.h.
59445386Swpaul */
59545386Swpaul
59645386Swpaulstatic int ti_alloc_jumbo_mem(sc)
59745386Swpaul	struct ti_softc		*sc;
59845386Swpaul{
59945386Swpaul	caddr_t			ptr;
60045386Swpaul	register int		i;
60145386Swpaul	struct ti_jpool_entry   *entry;
60245386Swpaul
60345386Swpaul	/* Grab a big chunk o' storage. */
60445386Swpaul	sc->ti_cdata.ti_jumbo_buf = contigmalloc(TI_JMEM, M_DEVBUF,
60550548Sbde		M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
60645386Swpaul
60745386Swpaul	if (sc->ti_cdata.ti_jumbo_buf == NULL) {
60845386Swpaul		printf("ti%d: no memory for jumbo buffers!\n", sc->ti_unit);
60945386Swpaul		return(ENOBUFS);
61045386Swpaul	}
61145386Swpaul
61245386Swpaul	SLIST_INIT(&sc->ti_jfree_listhead);
61345386Swpaul	SLIST_INIT(&sc->ti_jinuse_listhead);
61445386Swpaul
61545386Swpaul	/*
61645386Swpaul	 * Now divide it up into 9K pieces and save the addresses
61767405Sbmilekic	 * in an array.
61845386Swpaul	 */
61945386Swpaul	ptr = sc->ti_cdata.ti_jumbo_buf;
62045386Swpaul	for (i = 0; i < TI_JSLOTS; i++) {
62167405Sbmilekic		sc->ti_cdata.ti_jslots[i] = ptr;
62267405Sbmilekic		ptr += TI_JLEN;
62345386Swpaul		entry = malloc(sizeof(struct ti_jpool_entry),
62445386Swpaul			       M_DEVBUF, M_NOWAIT);
62545386Swpaul		if (entry == NULL) {
62662793Sgallatin			contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM,
62762793Sgallatin			           M_DEVBUF);
62845386Swpaul			sc->ti_cdata.ti_jumbo_buf = NULL;
62945386Swpaul			printf("ti%d: no memory for jumbo "
63045386Swpaul			    "buffer queue!\n", sc->ti_unit);
63145386Swpaul			return(ENOBUFS);
63245386Swpaul		}
63345386Swpaul		entry->slot = i;
63445386Swpaul		SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
63545386Swpaul	}
63645386Swpaul
63745386Swpaul	return(0);
63845386Swpaul}
63945386Swpaul
64045386Swpaul/*
64145386Swpaul * Allocate a jumbo buffer.
64245386Swpaul */
64345386Swpaulstatic void *ti_jalloc(sc)
64445386Swpaul	struct ti_softc		*sc;
64545386Swpaul{
64645386Swpaul	struct ti_jpool_entry   *entry;
64745386Swpaul
64845386Swpaul	entry = SLIST_FIRST(&sc->ti_jfree_listhead);
64945386Swpaul
65045386Swpaul	if (entry == NULL) {
65145386Swpaul		printf("ti%d: no free jumbo buffers\n", sc->ti_unit);
65245386Swpaul		return(NULL);
65345386Swpaul	}
65445386Swpaul
65545386Swpaul	SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
65645386Swpaul	SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
65767405Sbmilekic	return(sc->ti_cdata.ti_jslots[entry->slot]);
65845386Swpaul}
65945386Swpaul
66045386Swpaul/*
66145386Swpaul * Release a jumbo buffer.
66245386Swpaul */
66364837Sdwmalonestatic void ti_jfree(buf, args)
66445386Swpaul	caddr_t			buf;
66564837Sdwmalone	void			*args;
66645386Swpaul{
66745386Swpaul	struct ti_softc		*sc;
66845386Swpaul	int		        i;
66945386Swpaul	struct ti_jpool_entry   *entry;
67045386Swpaul
67145386Swpaul	/* Extract the softc struct pointer. */
67267405Sbmilekic	sc = (struct ti_softc *)args;
67345386Swpaul
67445386Swpaul	if (sc == NULL)
67567405Sbmilekic		panic("ti_jfree: didn't get softc pointer!");
67645386Swpaul
67745386Swpaul	/* calculate the slot this buffer belongs to */
67867405Sbmilekic	i = ((vm_offset_t)buf
67945386Swpaul	     - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
68045386Swpaul
68145386Swpaul	if ((i < 0) || (i >= TI_JSLOTS))
68245386Swpaul		panic("ti_jfree: asked to free buffer that we don't manage!");
68345386Swpaul
68464837Sdwmalone	entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
68564837Sdwmalone	if (entry == NULL)
68664837Sdwmalone		panic("ti_jfree: buffer not in use!");
68764837Sdwmalone	entry->slot = i;
68864837Sdwmalone	SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
68964837Sdwmalone	SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
69064837Sdwmalone
69145386Swpaul	return;
69245386Swpaul}
69345386Swpaul
69445386Swpaul
69545386Swpaul/*
69645386Swpaul * Intialize a standard receive ring descriptor.
69745386Swpaul */
69845386Swpaulstatic int ti_newbuf_std(sc, i, m)
69945386Swpaul	struct ti_softc		*sc;
70045386Swpaul	int			i;
70145386Swpaul	struct mbuf		*m;
70245386Swpaul{
70345386Swpaul	struct mbuf		*m_new = NULL;
70445386Swpaul	struct ti_rx_desc	*r;
70545386Swpaul
70649036Swpaul	if (m == NULL) {
70745386Swpaul		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
70845386Swpaul		if (m_new == NULL) {
70945386Swpaul			printf("ti%d: mbuf allocation failed "
71045386Swpaul			    "-- packet dropped!\n", sc->ti_unit);
71145386Swpaul			return(ENOBUFS);
71245386Swpaul		}
71345386Swpaul
71445386Swpaul		MCLGET(m_new, M_DONTWAIT);
71545386Swpaul		if (!(m_new->m_flags & M_EXT)) {
71645386Swpaul			printf("ti%d: cluster allocation failed "
71745386Swpaul			    "-- packet dropped!\n", sc->ti_unit);
71845386Swpaul			m_freem(m_new);
71945386Swpaul			return(ENOBUFS);
72045386Swpaul		}
72149036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
72249036Swpaul	} else {
72349036Swpaul		m_new = m;
72449036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
72549036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
72645386Swpaul	}
72745386Swpaul
72848597Swpaul	m_adj(m_new, ETHER_ALIGN);
72945386Swpaul	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
73045386Swpaul	r = &sc->ti_rdata->ti_rx_std_ring[i];
73145386Swpaul	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
73245386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
73345386Swpaul	r->ti_flags = 0;
73458698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
73558698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
73649036Swpaul	r->ti_len = m_new->m_len;
73745386Swpaul	r->ti_idx = i;
73845386Swpaul
73945386Swpaul	return(0);
74045386Swpaul}
74145386Swpaul
74245386Swpaul/*
74345386Swpaul * Intialize a mini receive ring descriptor. This only applies to
74445386Swpaul * the Tigon 2.
74545386Swpaul */
74645386Swpaulstatic int ti_newbuf_mini(sc, i, m)
74745386Swpaul	struct ti_softc		*sc;
74845386Swpaul	int			i;
74945386Swpaul	struct mbuf		*m;
75045386Swpaul{
75145386Swpaul	struct mbuf		*m_new = NULL;
75245386Swpaul	struct ti_rx_desc	*r;
75345386Swpaul
75449036Swpaul	if (m == NULL) {
75545386Swpaul		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
75645386Swpaul		if (m_new == NULL) {
75745386Swpaul			printf("ti%d: mbuf allocation failed "
75845386Swpaul			    "-- packet dropped!\n", sc->ti_unit);
75945386Swpaul			return(ENOBUFS);
76045386Swpaul		}
76149036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
76249036Swpaul	} else {
76349036Swpaul		m_new = m;
76449036Swpaul		m_new->m_data = m_new->m_pktdat;
76549036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
76645386Swpaul	}
76749036Swpaul
76848597Swpaul	m_adj(m_new, ETHER_ALIGN);
76945386Swpaul	r = &sc->ti_rdata->ti_rx_mini_ring[i];
77045386Swpaul	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
77145386Swpaul	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
77245386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
77345386Swpaul	r->ti_flags = TI_BDFLAG_MINI_RING;
77458698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
77558698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
77649036Swpaul	r->ti_len = m_new->m_len;
77745386Swpaul	r->ti_idx = i;
77845386Swpaul
77945386Swpaul	return(0);
78045386Swpaul}
78145386Swpaul
78245386Swpaul/*
78345386Swpaul * Initialize a jumbo receive ring descriptor. This allocates
78445386Swpaul * a jumbo buffer from the pool managed internally by the driver.
78545386Swpaul */
78645386Swpaulstatic int ti_newbuf_jumbo(sc, i, m)
78745386Swpaul	struct ti_softc		*sc;
78845386Swpaul	int			i;
78945386Swpaul	struct mbuf		*m;
79045386Swpaul{
79145386Swpaul	struct mbuf		*m_new = NULL;
79245386Swpaul	struct ti_rx_desc	*r;
79345386Swpaul
79449036Swpaul	if (m == NULL) {
79545386Swpaul		caddr_t			*buf = NULL;
79645386Swpaul
79745386Swpaul		/* Allocate the mbuf. */
79845386Swpaul		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
79945386Swpaul		if (m_new == NULL) {
80045386Swpaul			printf("ti%d: mbuf allocation failed "
80145386Swpaul			    "-- packet dropped!\n", sc->ti_unit);
80245386Swpaul			return(ENOBUFS);
80345386Swpaul		}
80445386Swpaul
80545386Swpaul		/* Allocate the jumbo buffer */
80645386Swpaul		buf = ti_jalloc(sc);
80745386Swpaul		if (buf == NULL) {
80845386Swpaul			m_freem(m_new);
80945386Swpaul			printf("ti%d: jumbo allocation failed "
81045386Swpaul			    "-- packet dropped!\n", sc->ti_unit);
81145386Swpaul			return(ENOBUFS);
81245386Swpaul		}
81345386Swpaul
81445386Swpaul		/* Attach the buffer to the mbuf. */
81564837Sdwmalone		m_new->m_data = (void *) buf;
81664837Sdwmalone		m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
81767405Sbmilekic		MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree,
81868621Sbmilekic		    (struct ti_softc *)sc, 0, EXT_NET_DRV);
81949036Swpaul	} else {
82049036Swpaul		m_new = m;
82149036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
82249036Swpaul		m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
82345386Swpaul	}
82445386Swpaul
82549780Swpaul	m_adj(m_new, ETHER_ALIGN);
82645386Swpaul	/* Set up the descriptor. */
82745386Swpaul	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
82845386Swpaul	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
82945386Swpaul	TI_HOSTADDR(r->ti_addr) = vtophys(mtod(m_new, caddr_t));
83045386Swpaul	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
83145386Swpaul	r->ti_flags = TI_BDFLAG_JUMBO_RING;
83258698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
83358698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
83449036Swpaul	r->ti_len = m_new->m_len;
83545386Swpaul	r->ti_idx = i;
83645386Swpaul
83745386Swpaul	return(0);
83845386Swpaul}
83945386Swpaul
84045386Swpaul/*
84145386Swpaul * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
84245386Swpaul * that's 1MB or memory, which is a lot. For now, we fill only the first
84345386Swpaul * 256 ring entries and hope that our CPU is fast enough to keep up with
84445386Swpaul * the NIC.
84545386Swpaul */
84645386Swpaulstatic int ti_init_rx_ring_std(sc)
84745386Swpaul	struct ti_softc		*sc;
84845386Swpaul{
84945386Swpaul	register int		i;
85045386Swpaul	struct ti_cmd_desc	cmd;
85145386Swpaul
85245386Swpaul	for (i = 0; i < TI_SSLOTS; i++) {
85345386Swpaul		if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
85445386Swpaul			return(ENOBUFS);
85545386Swpaul	};
85645386Swpaul
85745386Swpaul	TI_UPDATE_STDPROD(sc, i - 1);
85848597Swpaul	sc->ti_std = i - 1;
85945386Swpaul
86045386Swpaul	return(0);
86145386Swpaul}
86245386Swpaul
86345386Swpaulstatic void ti_free_rx_ring_std(sc)
86445386Swpaul	struct ti_softc		*sc;
86545386Swpaul{
86645386Swpaul	register int		i;
86745386Swpaul
86845386Swpaul	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
86945386Swpaul		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
87045386Swpaul			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
87145386Swpaul			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
87245386Swpaul		}
87345386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
87445386Swpaul		    sizeof(struct ti_rx_desc));
87545386Swpaul	}
87645386Swpaul
87745386Swpaul	return;
87845386Swpaul}
87945386Swpaul
88045386Swpaulstatic int ti_init_rx_ring_jumbo(sc)
88145386Swpaul	struct ti_softc		*sc;
88245386Swpaul{
88345386Swpaul	register int		i;
88445386Swpaul	struct ti_cmd_desc	cmd;
88545386Swpaul
88663699Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
88745386Swpaul		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
88845386Swpaul			return(ENOBUFS);
88945386Swpaul	};
89045386Swpaul
89145386Swpaul	TI_UPDATE_JUMBOPROD(sc, i - 1);
89248597Swpaul	sc->ti_jumbo = i - 1;
89345386Swpaul
89445386Swpaul	return(0);
89545386Swpaul}
89645386Swpaul
89745386Swpaulstatic void ti_free_rx_ring_jumbo(sc)
89845386Swpaul	struct ti_softc		*sc;
89945386Swpaul{
90045386Swpaul	register int		i;
90145386Swpaul
90245386Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
90345386Swpaul		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
90445386Swpaul			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
90545386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
90645386Swpaul		}
90745386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
90845386Swpaul		    sizeof(struct ti_rx_desc));
90945386Swpaul	}
91045386Swpaul
91145386Swpaul	return;
91245386Swpaul}
91345386Swpaul
91445386Swpaulstatic int ti_init_rx_ring_mini(sc)
91545386Swpaul	struct ti_softc		*sc;
91645386Swpaul{
91745386Swpaul	register int		i;
91845386Swpaul
91945386Swpaul	for (i = 0; i < TI_MSLOTS; i++) {
92045386Swpaul		if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
92145386Swpaul			return(ENOBUFS);
92245386Swpaul	};
92345386Swpaul
92445386Swpaul	TI_UPDATE_MINIPROD(sc, i - 1);
92548597Swpaul	sc->ti_mini = i - 1;
92645386Swpaul
92745386Swpaul	return(0);
92845386Swpaul}
92945386Swpaul
93045386Swpaulstatic void ti_free_rx_ring_mini(sc)
93145386Swpaul	struct ti_softc		*sc;
93245386Swpaul{
93345386Swpaul	register int		i;
93445386Swpaul
93545386Swpaul	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
93645386Swpaul		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
93745386Swpaul			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
93845386Swpaul			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
93945386Swpaul		}
94045386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
94145386Swpaul		    sizeof(struct ti_rx_desc));
94245386Swpaul	}
94345386Swpaul
94445386Swpaul	return;
94545386Swpaul}
94645386Swpaul
94745386Swpaulstatic void ti_free_tx_ring(sc)
94845386Swpaul	struct ti_softc		*sc;
94945386Swpaul{
95045386Swpaul	register int		i;
95145386Swpaul
95245386Swpaul	if (sc->ti_rdata->ti_tx_ring == NULL)
95345386Swpaul		return;
95445386Swpaul
95545386Swpaul	for (i = 0; i < TI_TX_RING_CNT; i++) {
95645386Swpaul		if (sc->ti_cdata.ti_tx_chain[i] != NULL) {
95745386Swpaul			m_freem(sc->ti_cdata.ti_tx_chain[i]);
95845386Swpaul			sc->ti_cdata.ti_tx_chain[i] = NULL;
95945386Swpaul		}
96045386Swpaul		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
96145386Swpaul		    sizeof(struct ti_tx_desc));
96245386Swpaul	}
96345386Swpaul
96445386Swpaul	return;
96545386Swpaul}
96645386Swpaul
96745386Swpaulstatic int ti_init_tx_ring(sc)
96845386Swpaul	struct ti_softc		*sc;
96945386Swpaul{
97048011Swpaul	sc->ti_txcnt = 0;
97145386Swpaul	sc->ti_tx_saved_considx = 0;
97245386Swpaul	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
97345386Swpaul	return(0);
97445386Swpaul}
97545386Swpaul
97645386Swpaul/*
97745386Swpaul * The Tigon 2 firmware has a new way to add/delete multicast addresses,
97845386Swpaul * but we have to support the old way too so that Tigon 1 cards will
97945386Swpaul * work.
98045386Swpaul */
98145386Swpaulvoid ti_add_mcast(sc, addr)
98245386Swpaul	struct ti_softc		*sc;
98345386Swpaul	struct ether_addr	*addr;
98445386Swpaul{
98545386Swpaul	struct ti_cmd_desc	cmd;
98645386Swpaul	u_int16_t		*m;
98745386Swpaul	u_int32_t		ext[2] = {0, 0};
98845386Swpaul
98945386Swpaul	m = (u_int16_t *)&addr->octet[0];
99045386Swpaul
99145386Swpaul	switch(sc->ti_hwrev) {
99245386Swpaul	case TI_HWREV_TIGON:
99345386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
99445386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
99545386Swpaul		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
99645386Swpaul		break;
99745386Swpaul	case TI_HWREV_TIGON_II:
99845386Swpaul		ext[0] = htons(m[0]);
99945386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
100045386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
100145386Swpaul		break;
100245386Swpaul	default:
100345386Swpaul		printf("ti%d: unknown hwrev\n", sc->ti_unit);
100445386Swpaul		break;
100545386Swpaul	}
100645386Swpaul
100745386Swpaul	return;
100845386Swpaul}
100945386Swpaul
101045386Swpaulvoid ti_del_mcast(sc, addr)
101145386Swpaul	struct ti_softc		*sc;
101245386Swpaul	struct ether_addr	*addr;
101345386Swpaul{
101445386Swpaul	struct ti_cmd_desc	cmd;
101545386Swpaul	u_int16_t		*m;
101645386Swpaul	u_int32_t		ext[2] = {0, 0};
101745386Swpaul
101845386Swpaul	m = (u_int16_t *)&addr->octet[0];
101945386Swpaul
102045386Swpaul	switch(sc->ti_hwrev) {
102145386Swpaul	case TI_HWREV_TIGON:
102245386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
102345386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
102445386Swpaul		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
102545386Swpaul		break;
102645386Swpaul	case TI_HWREV_TIGON_II:
102745386Swpaul		ext[0] = htons(m[0]);
102845386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
102945386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
103045386Swpaul		break;
103145386Swpaul	default:
103245386Swpaul		printf("ti%d: unknown hwrev\n", sc->ti_unit);
103345386Swpaul		break;
103445386Swpaul	}
103545386Swpaul
103645386Swpaul	return;
103745386Swpaul}
103845386Swpaul
103945386Swpaul/*
104045386Swpaul * Configure the Tigon's multicast address filter.
104145386Swpaul *
104245386Swpaul * The actual multicast table management is a bit of a pain, thanks to
104345386Swpaul * slight brain damage on the part of both Alteon and us. With our
104445386Swpaul * multicast code, we are only alerted when the multicast address table
104545386Swpaul * changes and at that point we only have the current list of addresses:
104645386Swpaul * we only know the current state, not the previous state, so we don't
104745386Swpaul * actually know what addresses were removed or added. The firmware has
104845386Swpaul * state, but we can't get our grubby mits on it, and there is no 'delete
104945386Swpaul * all multicast addresses' command. Hence, we have to maintain our own
105045386Swpaul * state so we know what addresses have been programmed into the NIC at
105145386Swpaul * any given time.
105245386Swpaul */
105345386Swpaulstatic void ti_setmulti(sc)
105445386Swpaul	struct ti_softc		*sc;
105545386Swpaul{
105645386Swpaul	struct ifnet		*ifp;
105745386Swpaul	struct ifmultiaddr	*ifma;
105845386Swpaul	struct ti_cmd_desc	cmd;
105945386Swpaul	struct ti_mc_entry	*mc;
106045386Swpaul	u_int32_t		intrs;
106145386Swpaul
106245386Swpaul	ifp = &sc->arpcom.ac_if;
106345386Swpaul
106445386Swpaul	if (ifp->if_flags & IFF_ALLMULTI) {
106545386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
106645386Swpaul		return;
106745386Swpaul	} else {
106845386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
106945386Swpaul	}
107045386Swpaul
107145386Swpaul	/* Disable interrupts. */
107245386Swpaul	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
107345386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
107445386Swpaul
107545386Swpaul	/* First, zot all the existing filters. */
107671999Sphk	while (SLIST_FIRST(&sc->ti_mc_listhead) != NULL) {
107771999Sphk		mc = SLIST_FIRST(&sc->ti_mc_listhead);
107845386Swpaul		ti_del_mcast(sc, &mc->mc_addr);
107945386Swpaul		SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
108045386Swpaul		free(mc, M_DEVBUF);
108145386Swpaul	}
108245386Swpaul
108345386Swpaul	/* Now program new ones. */
108472084Sphk	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
108545386Swpaul		if (ifma->ifma_addr->sa_family != AF_LINK)
108645386Swpaul			continue;
108745386Swpaul		mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
108845386Swpaul		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
108945386Swpaul		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
109045386Swpaul		SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
109145386Swpaul		ti_add_mcast(sc, &mc->mc_addr);
109245386Swpaul	}
109345386Swpaul
109445386Swpaul	/* Re-enable interrupts. */
109545386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
109645386Swpaul
109745386Swpaul	return;
109845386Swpaul}
109945386Swpaul
110045386Swpaul/*
110145386Swpaul * Check to see if the BIOS has configured us for a 64 bit slot when
110245386Swpaul * we aren't actually in one. If we detect this condition, we can work
110345386Swpaul * around it on the Tigon 2 by setting a bit in the PCI state register,
110445386Swpaul * but for the Tigon 1 we must give up and abort the interface attach.
110545386Swpaul */
110645386Swpaulstatic int ti_64bitslot_war(sc)
110745386Swpaul	struct ti_softc		*sc;
110845386Swpaul{
110945386Swpaul	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
111045386Swpaul		CSR_WRITE_4(sc, 0x600, 0);
111145386Swpaul		CSR_WRITE_4(sc, 0x604, 0);
111245386Swpaul		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
111345386Swpaul		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
111445386Swpaul			if (sc->ti_hwrev == TI_HWREV_TIGON)
111545386Swpaul				return(EINVAL);
111645386Swpaul			else {
111745386Swpaul				TI_SETBIT(sc, TI_PCI_STATE,
111845386Swpaul				    TI_PCISTATE_32BIT_BUS);
111945386Swpaul				return(0);
112045386Swpaul			}
112145386Swpaul		}
112245386Swpaul	}
112345386Swpaul
112445386Swpaul	return(0);
112545386Swpaul}
112645386Swpaul
112745386Swpaul/*
112845386Swpaul * Do endian, PCI and DMA initialization. Also check the on-board ROM
112945386Swpaul * self-test results.
113045386Swpaul */
113145386Swpaulstatic int ti_chipinit(sc)
113245386Swpaul	struct ti_softc		*sc;
113345386Swpaul{
113445386Swpaul	u_int32_t		cacheline;
113545386Swpaul	u_int32_t		pci_writemax = 0;
113645386Swpaul
113745386Swpaul	/* Initialize link to down state. */
113845386Swpaul	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
113945386Swpaul
114058698Sjlemon	sc->arpcom.ac_if.if_hwassist = TI_CSUM_FEATURES;
114158698Sjlemon
114245386Swpaul	/* Set endianness before we access any non-PCI registers. */
114345386Swpaul#if BYTE_ORDER == BIG_ENDIAN
114445386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
114545386Swpaul	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
114645386Swpaul#else
114745386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
114845386Swpaul	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
114945386Swpaul#endif
115045386Swpaul
115145386Swpaul	/* Check the ROM failed bit to see if self-tests passed. */
115245386Swpaul	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
115345386Swpaul		printf("ti%d: board self-diagnostics failed!\n", sc->ti_unit);
115445386Swpaul		return(ENODEV);
115545386Swpaul	}
115645386Swpaul
115745386Swpaul	/* Halt the CPU. */
115845386Swpaul	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
115945386Swpaul
116045386Swpaul	/* Figure out the hardware revision. */
116145386Swpaul	switch(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
116245386Swpaul	case TI_REV_TIGON_I:
116345386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON;
116445386Swpaul		break;
116545386Swpaul	case TI_REV_TIGON_II:
116645386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON_II;
116745386Swpaul		break;
116845386Swpaul	default:
116945386Swpaul		printf("ti%d: unsupported chip revision\n", sc->ti_unit);
117045386Swpaul		return(ENODEV);
117145386Swpaul	}
117245386Swpaul
117345386Swpaul	/* Do special setup for Tigon 2. */
117445386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
117545386Swpaul		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
117676033Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_512K);
117745386Swpaul		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
117845386Swpaul	}
117945386Swpaul
118045386Swpaul	/* Set up the PCI state register. */
118145386Swpaul	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
118245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
118345386Swpaul		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
118445386Swpaul	}
118545386Swpaul
118645386Swpaul	/* Clear the read/write max DMA parameters. */
118745386Swpaul	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
118845386Swpaul	    TI_PCISTATE_READ_MAXDMA));
118945386Swpaul
119045386Swpaul	/* Get cache line size. */
119145386Swpaul	cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
119245386Swpaul
119345386Swpaul	/*
119445386Swpaul	 * If the system has set enabled the PCI memory write
119545386Swpaul	 * and invalidate command in the command register, set
119645386Swpaul	 * the write max parameter accordingly. This is necessary
119745386Swpaul	 * to use MWI with the Tigon 2.
119845386Swpaul	 */
119945386Swpaul	if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
120045386Swpaul		switch(cacheline) {
120145386Swpaul		case 1:
120245386Swpaul		case 4:
120345386Swpaul		case 8:
120445386Swpaul		case 16:
120545386Swpaul		case 32:
120645386Swpaul		case 64:
120745386Swpaul			break;
120845386Swpaul		default:
120945386Swpaul		/* Disable PCI memory write and invalidate. */
121045386Swpaul			if (bootverbose)
121145386Swpaul				printf("ti%d: cache line size %d not "
121245386Swpaul				    "supported; disabling PCI MWI\n",
121345386Swpaul				    sc->ti_unit, cacheline);
121445386Swpaul			CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
121545386Swpaul			    TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
121645386Swpaul			break;
121745386Swpaul		}
121845386Swpaul	}
121945386Swpaul
122045386Swpaul#ifdef __brokenalpha__
122145386Swpaul	/*
122245386Swpaul	 * From the Alteon sample driver:
122345386Swpaul	 * Must insure that we do not cross an 8K (bytes) boundary
122445386Swpaul	 * for DMA reads.  Our highest limit is 1K bytes.  This is a
122545386Swpaul	 * restriction on some ALPHA platforms with early revision
122645386Swpaul	 * 21174 PCI chipsets, such as the AlphaPC 164lx
122745386Swpaul	 */
122845386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax|TI_PCI_READMAX_1024);
122945386Swpaul#else
123045386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
123145386Swpaul#endif
123245386Swpaul
123345386Swpaul	/* This sets the min dma param all the way up (0xff). */
123445386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
123545386Swpaul
123645386Swpaul	/* Configure DMA variables. */
123745386Swpaul#if BYTE_ORDER == BIG_ENDIAN
123845386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
123945386Swpaul	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
124045386Swpaul	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
124145386Swpaul	    TI_OPMODE_DONT_FRAG_JUMBO);
124245386Swpaul#else
124345386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
124445386Swpaul	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
124545386Swpaul	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB);
124645386Swpaul#endif
124745386Swpaul
124845386Swpaul	/*
124945386Swpaul	 * Only allow 1 DMA channel to be active at a time.
125045386Swpaul	 * I don't think this is a good idea, but without it
125145386Swpaul	 * the firmware racks up lots of nicDmaReadRingFull
125258698Sjlemon	 * errors.  This is not compatible with hardware checksums.
125345386Swpaul	 */
125458698Sjlemon	if (sc->arpcom.ac_if.if_hwassist == 0)
125558698Sjlemon		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
125645386Swpaul
125745386Swpaul	/* Recommended settings from Tigon manual. */
125845386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
125945386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
126045386Swpaul
126145386Swpaul	if (ti_64bitslot_war(sc)) {
126245386Swpaul		printf("ti%d: bios thinks we're in a 64 bit slot, "
126345386Swpaul		    "but we aren't", sc->ti_unit);
126445386Swpaul		return(EINVAL);
126545386Swpaul	}
126645386Swpaul
126745386Swpaul	return(0);
126845386Swpaul}
126945386Swpaul
127045386Swpaul/*
127145386Swpaul * Initialize the general information block and firmware, and
127245386Swpaul * start the CPU(s) running.
127345386Swpaul */
127445386Swpaulstatic int ti_gibinit(sc)
127545386Swpaul	struct ti_softc		*sc;
127645386Swpaul{
127745386Swpaul	struct ti_rcb		*rcb;
127845386Swpaul	int			i;
127945386Swpaul	struct ifnet		*ifp;
128045386Swpaul
128145386Swpaul	ifp = &sc->arpcom.ac_if;
128245386Swpaul
128345386Swpaul	/* Disable interrupts for now. */
128445386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
128545386Swpaul
128645386Swpaul	/* Tell the chip where to find the general information block. */
128745386Swpaul	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
128845386Swpaul	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, vtophys(&sc->ti_rdata->ti_info));
128945386Swpaul
129045386Swpaul	/* Load the firmware into SRAM. */
129145386Swpaul	ti_loadfw(sc);
129245386Swpaul
129345386Swpaul	/* Set up the contents of the general info and ring control blocks. */
129445386Swpaul
129545386Swpaul	/* Set up the event ring and producer pointer. */
129645386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
129745386Swpaul
129845386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_event_ring);
129945386Swpaul	rcb->ti_flags = 0;
130045386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
130145386Swpaul	    vtophys(&sc->ti_ev_prodidx);
130245386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
130345386Swpaul	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
130445386Swpaul	sc->ti_ev_saved_considx = 0;
130545386Swpaul
130645386Swpaul	/* Set up the command ring and producer mailbox. */
130745386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
130845386Swpaul
130945386Swpaul	sc->ti_rdata->ti_cmd_ring =
131049133Swpaul	    (struct ti_cmd_desc *)(sc->ti_vhandle + TI_GCR_CMDRING);
131145386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
131245386Swpaul	rcb->ti_flags = 0;
131345386Swpaul	rcb->ti_max_len = 0;
131445386Swpaul	for (i = 0; i < TI_CMD_RING_CNT; i++) {
131545386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
131645386Swpaul	}
131745386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
131845386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
131945386Swpaul	sc->ti_cmd_saved_prodidx = 0;
132045386Swpaul
132145386Swpaul	/*
132245386Swpaul	 * Assign the address of the stats refresh buffer.
132345386Swpaul	 * We re-use the current stats buffer for this to
132445386Swpaul	 * conserve memory.
132545386Swpaul	 */
132645386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
132745386Swpaul	    vtophys(&sc->ti_rdata->ti_info.ti_stats);
132845386Swpaul
132945386Swpaul	/* Set up the standard receive ring. */
133045386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
133145386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) = vtophys(&sc->ti_rdata->ti_rx_std_ring);
133245386Swpaul	rcb->ti_max_len = TI_FRAMELEN;
133345386Swpaul	rcb->ti_flags = 0;
133458698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
133558698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
133658698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
133745386Swpaul#if NVLAN > 0
133845386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
133945386Swpaul#endif
134045386Swpaul
134145386Swpaul	/* Set up the jumbo receive ring. */
134245386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
134345386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) =
134445386Swpaul	    vtophys(&sc->ti_rdata->ti_rx_jumbo_ring);
134549036Swpaul	rcb->ti_max_len = TI_JUMBO_FRAMELEN;
134645386Swpaul	rcb->ti_flags = 0;
134758698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
134858698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
134958698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
135045386Swpaul#if NVLAN > 0
135145386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
135245386Swpaul#endif
135345386Swpaul
135445386Swpaul	/*
135545386Swpaul	 * Set up the mini ring. Only activated on the
135645386Swpaul	 * Tigon 2 but the slot in the config block is
135745386Swpaul	 * still there on the Tigon 1.
135845386Swpaul	 */
135945386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
136045386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) =
136145386Swpaul	    vtophys(&sc->ti_rdata->ti_rx_mini_ring);
136251352Swpaul	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
136345386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
136445386Swpaul		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
136545386Swpaul	else
136645386Swpaul		rcb->ti_flags = 0;
136758698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
136858698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
136958698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
137045386Swpaul#if NVLAN > 0
137145386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
137245386Swpaul#endif
137345386Swpaul
137445386Swpaul	/*
137545386Swpaul	 * Set up the receive return ring.
137645386Swpaul	 */
137745386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
137845386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) =
137945386Swpaul	    vtophys(&sc->ti_rdata->ti_rx_return_ring);
138045386Swpaul	rcb->ti_flags = 0;
138145386Swpaul	rcb->ti_max_len = TI_RETURN_RING_CNT;
138245386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
138345386Swpaul	    vtophys(&sc->ti_return_prodidx);
138445386Swpaul
138545386Swpaul	/*
138645386Swpaul	 * Set up the tx ring. Note: for the Tigon 2, we have the option
138745386Swpaul	 * of putting the transmit ring in the host's address space and
138845386Swpaul	 * letting the chip DMA it instead of leaving the ring in the NIC's
138945386Swpaul	 * memory and accessing it through the shared memory region. We
139045386Swpaul	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
139145386Swpaul	 * so we have to revert to the shared memory scheme if we detect
139245386Swpaul	 * a Tigon 1 chip.
139345386Swpaul	 */
139445386Swpaul	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
139545386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON) {
139645386Swpaul		sc->ti_rdata->ti_tx_ring_nic =
139749133Swpaul		    (struct ti_tx_desc *)(sc->ti_vhandle + TI_WINDOW);
139845386Swpaul	}
139945386Swpaul	bzero((char *)sc->ti_rdata->ti_tx_ring,
140045386Swpaul	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
140145386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
140245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
140345386Swpaul		rcb->ti_flags = 0;
140445386Swpaul	else
140545386Swpaul		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
140645386Swpaul#if NVLAN > 0
140745386Swpaul	rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
140845386Swpaul#endif
140958698Sjlemon	if (sc->arpcom.ac_if.if_hwassist)
141058698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
141158698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
141245386Swpaul	rcb->ti_max_len = TI_TX_RING_CNT;
141345386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
141445386Swpaul		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
141545386Swpaul	else
141645386Swpaul		TI_HOSTADDR(rcb->ti_hostaddr) =
141745386Swpaul		    vtophys(&sc->ti_rdata->ti_tx_ring);
141845386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
141945386Swpaul	    vtophys(&sc->ti_tx_considx);
142045386Swpaul
142145386Swpaul	/* Set up tuneables */
142245386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
142345386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
142445386Swpaul		    (sc->ti_rx_coal_ticks / 10));
142545386Swpaul	else
142645386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
142745386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
142845386Swpaul	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
142945386Swpaul	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
143045386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
143145386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
143245386Swpaul
143345386Swpaul	/* Turn interrupts on. */
143445386Swpaul	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
143545386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
143645386Swpaul
143745386Swpaul	/* Start CPU. */
143845386Swpaul	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
143945386Swpaul
144045386Swpaul	return(0);
144145386Swpaul}
144245386Swpaul
144345386Swpaul/*
144445386Swpaul * Probe for a Tigon chip. Check the PCI vendor and device IDs
144545386Swpaul * against our list and return its name if we find a match.
144645386Swpaul */
144749011Swpaulstatic int ti_probe(dev)
144849011Swpaul	device_t		dev;
144945386Swpaul{
145045386Swpaul	struct ti_type		*t;
145145386Swpaul
145245386Swpaul	t = ti_devs;
145345386Swpaul
145445386Swpaul	while(t->ti_name != NULL) {
145549011Swpaul		if ((pci_get_vendor(dev) == t->ti_vid) &&
145649011Swpaul		    (pci_get_device(dev) == t->ti_did)) {
145749011Swpaul			device_set_desc(dev, t->ti_name);
145849011Swpaul			return(0);
145949011Swpaul		}
146045386Swpaul		t++;
146145386Swpaul	}
146245386Swpaul
146349011Swpaul	return(ENXIO);
146445386Swpaul}
146545386Swpaul
146649011Swpaulstatic int ti_attach(dev)
146749011Swpaul	device_t		dev;
146845386Swpaul{
146945386Swpaul	u_int32_t		command;
147045386Swpaul	struct ifnet		*ifp;
147145386Swpaul	struct ti_softc		*sc;
147249011Swpaul	int			unit, error = 0, rid;
147345386Swpaul
147449011Swpaul	sc = device_get_softc(dev);
147549011Swpaul	unit = device_get_unit(dev);
147645386Swpaul	bzero(sc, sizeof(struct ti_softc));
147745386Swpaul
147871228Sbmilekic	mtx_init(&sc->ti_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
147969583Swpaul	TI_LOCK(sc);
148069583Swpaul
148145386Swpaul	/*
148245386Swpaul	 * Map control/status registers.
148345386Swpaul	 */
148472813Swpaul	pci_enable_busmaster(dev);
148579472Swpaul	pci_enable_io(dev, SYS_RES_MEMORY);
148661041Speter	command = pci_read_config(dev, PCIR_COMMAND, 4);
148745386Swpaul
148845386Swpaul	if (!(command & PCIM_CMD_MEMEN)) {
148945386Swpaul		printf("ti%d: failed to enable memory mapping!\n", unit);
149049011Swpaul		error = ENXIO;
149145386Swpaul		goto fail;
149245386Swpaul	}
149345386Swpaul
149449011Swpaul	rid = TI_PCI_LOMEM;
149549011Swpaul	sc->ti_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
149665176Sdfr	    0, ~0, 1, RF_ACTIVE|PCI_RF_DENSE);
149749011Swpaul
149849011Swpaul	if (sc->ti_res == NULL) {
149945386Swpaul		printf ("ti%d: couldn't map memory\n", unit);
150049011Swpaul		error = ENXIO;
150145386Swpaul		goto fail;
150245386Swpaul	}
150345386Swpaul
150449035Swpaul	sc->ti_btag = rman_get_bustag(sc->ti_res);
150549035Swpaul	sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
150649133Swpaul	sc->ti_vhandle = (vm_offset_t)rman_get_virtual(sc->ti_res);
150749035Swpaul
150849011Swpaul	/* Allocate interrupt */
150949011Swpaul	rid = 0;
151049133Swpaul
151149011Swpaul	sc->ti_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
151249011Swpaul	    RF_SHAREABLE | RF_ACTIVE);
151345386Swpaul
151449011Swpaul	if (sc->ti_irq == NULL) {
151549011Swpaul		printf("ti%d: couldn't map interrupt\n", unit);
151649011Swpaul		error = ENXIO;
151745386Swpaul		goto fail;
151845386Swpaul	}
151945386Swpaul
152049011Swpaul	error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET,
152149011Swpaul	   ti_intr, sc, &sc->ti_intrhand);
152249011Swpaul
152349011Swpaul	if (error) {
152449011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
152549011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
152649011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
152749011Swpaul		printf("ti%d: couldn't set up irq\n", unit);
152845386Swpaul		goto fail;
152945386Swpaul	}
153045386Swpaul
153145386Swpaul	sc->ti_unit = unit;
153245386Swpaul
153345386Swpaul	if (ti_chipinit(sc)) {
153445386Swpaul		printf("ti%d: chip initialization failed\n", sc->ti_unit);
153549011Swpaul		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
153649011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
153749011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
153849011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
153949011Swpaul		error = ENXIO;
154045386Swpaul		goto fail;
154145386Swpaul	}
154245386Swpaul
154345386Swpaul	/* Zero out the NIC's on-board SRAM. */
154445386Swpaul	ti_mem(sc, 0x2000, 0x100000 - 0x2000,  NULL);
154545386Swpaul
154645386Swpaul	/* Init again -- zeroing memory may have clobbered some registers. */
154745386Swpaul	if (ti_chipinit(sc)) {
154845386Swpaul		printf("ti%d: chip initialization failed\n", sc->ti_unit);
154949011Swpaul		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
155049011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
155149011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
155249011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
155349011Swpaul		error = ENXIO;
155445386Swpaul		goto fail;
155545386Swpaul	}
155645386Swpaul
155745386Swpaul	/*
155845386Swpaul	 * Get station address from the EEPROM. Note: the manual states
155945386Swpaul	 * that the MAC address is at offset 0x8c, however the data is
156045386Swpaul	 * stored as two longwords (since that's how it's loaded into
156172645Sasmodai	 * the NIC). This means the MAC address is actually preceded
156245386Swpaul	 * by two zero bytes. We need to skip over those.
156345386Swpaul	 */
156445386Swpaul	if (ti_read_eeprom(sc, (caddr_t)&sc->arpcom.ac_enaddr,
156545386Swpaul				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
156645386Swpaul		printf("ti%d: failed to read station address\n", unit);
156749011Swpaul		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
156849011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
156949011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
157049011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
157149011Swpaul		error = ENXIO;
157245386Swpaul		goto fail;
157345386Swpaul	}
157445386Swpaul
157545386Swpaul	/*
157645386Swpaul	 * A Tigon chip was detected. Inform the world.
157745386Swpaul	 */
157845386Swpaul	printf("ti%d: Ethernet address: %6D\n", unit,
157945386Swpaul				sc->arpcom.ac_enaddr, ":");
158045386Swpaul
158145386Swpaul	/* Allocate the general information block and ring buffers. */
158249011Swpaul	sc->ti_rdata = contigmalloc(sizeof(struct ti_ring_data), M_DEVBUF,
158350548Sbde	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
158445386Swpaul
158549011Swpaul	if (sc->ti_rdata == NULL) {
158649011Swpaul		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
158749011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
158849011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
158949011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
159049011Swpaul		error = ENXIO;
159145386Swpaul		printf("ti%d: no memory for list buffers!\n", sc->ti_unit);
159245386Swpaul		goto fail;
159345386Swpaul	}
159445386Swpaul
159545386Swpaul	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
159645386Swpaul
159745386Swpaul	/* Try to allocate memory for jumbo buffers. */
159845386Swpaul	if (ti_alloc_jumbo_mem(sc)) {
159945386Swpaul		printf("ti%d: jumbo buffer allocation failed\n", sc->ti_unit);
160049011Swpaul		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
160149011Swpaul		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
160249011Swpaul		bus_release_resource(dev, SYS_RES_MEMORY,
160349011Swpaul		    TI_PCI_LOMEM, sc->ti_res);
160462793Sgallatin		contigfree(sc->ti_rdata, sizeof(struct ti_ring_data),
160562793Sgallatin		    M_DEVBUF);
160649011Swpaul		error = ENXIO;
160745386Swpaul		goto fail;
160845386Swpaul	}
160945386Swpaul
161063699Swpaul	/*
161163699Swpaul	 * We really need a better way to tell a 1000baseTX card
161263699Swpaul	 * from a 1000baseSX one, since in theory there could be
161363699Swpaul	 * OEMed 1000baseTX cards from lame vendors who aren't
161463699Swpaul	 * clever enough to change the PCI ID. For the moment
161563699Swpaul	 * though, the AceNIC is the only copper card available.
161663699Swpaul	 */
161763699Swpaul	if (pci_get_vendor(dev) == ALT_VENDORID &&
161863699Swpaul	    pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
161963699Swpaul		sc->ti_copper = 1;
162064139Swpaul	/* Ok, it's not the only copper card available. */
162164139Swpaul	if (pci_get_vendor(dev) == NG_VENDORID &&
162264139Swpaul	    pci_get_device(dev) == NG_DEVICEID_GA620T)
162364139Swpaul		sc->ti_copper = 1;
162463699Swpaul
162545386Swpaul	/* Set default tuneable values. */
162645386Swpaul	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
162745386Swpaul	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
162845386Swpaul	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
162945386Swpaul	sc->ti_rx_max_coal_bds = 64;
163045386Swpaul	sc->ti_tx_max_coal_bds = 128;
163145386Swpaul	sc->ti_tx_buf_ratio = 21;
163245386Swpaul
163345386Swpaul	/* Set up ifnet structure */
163445386Swpaul	ifp = &sc->arpcom.ac_if;
163545386Swpaul	ifp->if_softc = sc;
163645386Swpaul	ifp->if_unit = sc->ti_unit;
163745386Swpaul	ifp->if_name = "ti";
163845386Swpaul	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
163945386Swpaul	ifp->if_ioctl = ti_ioctl;
164045386Swpaul	ifp->if_output = ether_output;
164145386Swpaul	ifp->if_start = ti_start;
164245386Swpaul	ifp->if_watchdog = ti_watchdog;
164345386Swpaul	ifp->if_init = ti_init;
164445386Swpaul	ifp->if_mtu = ETHERMTU;
164545386Swpaul	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
164645386Swpaul
164745386Swpaul	/* Set up ifmedia support. */
164845386Swpaul	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
164963699Swpaul	if (sc->ti_copper) {
165063699Swpaul		/*
165163699Swpaul		 * Copper cards allow manual 10/100 mode selection,
165263699Swpaul		 * but not manual 1000baseTX mode selection. Why?
165363699Swpaul		 * Becuase currently there's no way to specify the
165463699Swpaul		 * master/slave setting through the firmware interface,
165563699Swpaul		 * so Alteon decided to just bag it and handle it
165663699Swpaul		 * via autonegotiation.
165763699Swpaul		 */
165863699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
165963699Swpaul		ifmedia_add(&sc->ifmedia,
166063699Swpaul		    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
166163699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
166263699Swpaul		ifmedia_add(&sc->ifmedia,
166363699Swpaul		    IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
166463699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_TX, 0, NULL);
166563699Swpaul		ifmedia_add(&sc->ifmedia,
166663699Swpaul		    IFM_ETHER|IFM_1000_TX|IFM_FDX, 0, NULL);
166763699Swpaul	} else {
166863699Swpaul		/* Fiber cards don't support 10/100 modes. */
166963699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
167063699Swpaul		ifmedia_add(&sc->ifmedia,
167163699Swpaul		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
167263699Swpaul	}
167345386Swpaul	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
167445386Swpaul	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
167545386Swpaul
167645386Swpaul	/*
167763090Sarchie	 * Call MI attach routine.
167845386Swpaul	 */
167963090Sarchie	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
168067087Swpaul	TI_UNLOCK(sc);
168167087Swpaul	return(0);
168245386Swpaul
168345386Swpaulfail:
168467087Swpaul	TI_UNLOCK(sc);
168567087Swpaul	mtx_destroy(&sc->ti_mtx);
168649011Swpaul	return(error);
168745386Swpaul}
168845386Swpaul
168949011Swpaulstatic int ti_detach(dev)
169049011Swpaul	device_t		dev;
169149011Swpaul{
169249011Swpaul	struct ti_softc		*sc;
169349011Swpaul	struct ifnet		*ifp;
169449011Swpaul
169549011Swpaul
169649011Swpaul	sc = device_get_softc(dev);
169767087Swpaul	TI_LOCK(sc);
169849011Swpaul	ifp = &sc->arpcom.ac_if;
169949011Swpaul
170063090Sarchie	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
170149011Swpaul	ti_stop(sc);
170249011Swpaul
170349011Swpaul	bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
170449011Swpaul	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
170549011Swpaul	bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM, sc->ti_res);
170649011Swpaul
170762793Sgallatin	contigfree(sc->ti_cdata.ti_jumbo_buf, TI_JMEM, M_DEVBUF);
170862793Sgallatin	contigfree(sc->ti_rdata, sizeof(struct ti_ring_data), M_DEVBUF);
170949011Swpaul	ifmedia_removeall(&sc->ifmedia);
171049011Swpaul
171167087Swpaul	TI_UNLOCK(sc);
171267087Swpaul	mtx_destroy(&sc->ti_mtx);
171349011Swpaul
171449011Swpaul	return(0);
171549011Swpaul}
171649011Swpaul
171745386Swpaul/*
171845386Swpaul * Frame reception handling. This is called if there's a frame
171945386Swpaul * on the receive return list.
172045386Swpaul *
172145386Swpaul * Note: we have to be able to handle three possibilities here:
172245386Swpaul * 1) the frame is from the mini receive ring (can only happen)
172345386Swpaul *    on Tigon 2 boards)
172445386Swpaul * 2) the frame is from the jumbo recieve ring
172545386Swpaul * 3) the frame is from the standard receive ring
172645386Swpaul */
172745386Swpaul
172845386Swpaulstatic void ti_rxeof(sc)
172945386Swpaul	struct ti_softc		*sc;
173045386Swpaul{
173145386Swpaul	struct ifnet		*ifp;
173248597Swpaul	struct ti_cmd_desc	cmd;
173345386Swpaul
173445386Swpaul	ifp = &sc->arpcom.ac_if;
173545386Swpaul
173645386Swpaul	while(sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
173745386Swpaul		struct ti_rx_desc	*cur_rx;
173845386Swpaul		u_int32_t		rxidx;
173945386Swpaul		struct ether_header	*eh;
174045386Swpaul		struct mbuf		*m = NULL;
174145386Swpaul#if NVLAN > 0
174245386Swpaul		u_int16_t		vlan_tag = 0;
174345386Swpaul		int			have_tag = 0;
174445386Swpaul#endif
174545386Swpaul
174645386Swpaul		cur_rx =
174745386Swpaul		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
174845386Swpaul		rxidx = cur_rx->ti_idx;
174945386Swpaul		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
175045386Swpaul
175145386Swpaul#if NVLAN > 0
175245386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
175345386Swpaul			have_tag = 1;
175477058Sphk			vlan_tag = cur_rx->ti_vlan_tag & 0xfff;
175545386Swpaul		}
175645386Swpaul#endif
175745386Swpaul
175845386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
175945386Swpaul			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
176045386Swpaul			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
176145386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
176245386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
176345386Swpaul				ifp->if_ierrors++;
176445386Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
176545386Swpaul				continue;
176645386Swpaul			}
176748597Swpaul			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
176848597Swpaul				ifp->if_ierrors++;
176948597Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
177048597Swpaul				continue;
177148597Swpaul			}
177245386Swpaul		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
177345386Swpaul			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
177445386Swpaul			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
177545386Swpaul			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
177645386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
177745386Swpaul				ifp->if_ierrors++;
177845386Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
177945386Swpaul				continue;
178045386Swpaul			}
178148597Swpaul			if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
178248597Swpaul				ifp->if_ierrors++;
178348597Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
178448597Swpaul				continue;
178548597Swpaul			}
178645386Swpaul		} else {
178745386Swpaul			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
178845386Swpaul			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
178945386Swpaul			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
179045386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
179145386Swpaul				ifp->if_ierrors++;
179245386Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
179345386Swpaul				continue;
179445386Swpaul			}
179548597Swpaul			if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
179648597Swpaul				ifp->if_ierrors++;
179748597Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
179848597Swpaul				continue;
179948597Swpaul			}
180045386Swpaul		}
180145386Swpaul
180245386Swpaul		m->m_pkthdr.len = m->m_len = cur_rx->ti_len;
180345386Swpaul		ifp->if_ipackets++;
180445386Swpaul		eh = mtod(m, struct ether_header *);
180545386Swpaul		m->m_pkthdr.rcvif = ifp;
180645386Swpaul
180745386Swpaul		/* Remove header from mbuf and pass it on. */
180845386Swpaul		m_adj(m, sizeof(struct ether_header));
180945386Swpaul
181058698Sjlemon		if (ifp->if_hwassist) {
181158698Sjlemon			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED |
181258698Sjlemon			    CSUM_DATA_VALID;
181358698Sjlemon			if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
181458698Sjlemon				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
181558698Sjlemon			m->m_pkthdr.csum_data = cur_rx->ti_tcp_udp_cksum;
181658698Sjlemon		}
181745386Swpaul
181845386Swpaul#if NVLAN > 0
181945386Swpaul		/*
182045386Swpaul		 * If we received a packet with a vlan tag, pass it
182145386Swpaul		 * to vlan_input() instead of ether_input().
182245386Swpaul		 */
182345386Swpaul		if (have_tag) {
182445386Swpaul			vlan_input_tag(eh, m, vlan_tag);
182545386Swpaul			have_tag = vlan_tag = 0;
182645386Swpaul			continue;
182745386Swpaul		}
182845386Swpaul#endif
182945386Swpaul		ether_input(ifp, eh, m);
183045386Swpaul	}
183145386Swpaul
183245386Swpaul	/* Only necessary on the Tigon 1. */
183345386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
183445386Swpaul		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
183545386Swpaul		    sc->ti_rx_saved_considx);
183645386Swpaul
183748597Swpaul	TI_UPDATE_STDPROD(sc, sc->ti_std);
183848597Swpaul	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
183948597Swpaul	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
184045386Swpaul
184145386Swpaul	return;
184245386Swpaul}
184345386Swpaul
184445386Swpaulstatic void ti_txeof(sc)
184545386Swpaul	struct ti_softc		*sc;
184645386Swpaul{
184745386Swpaul	struct ti_tx_desc	*cur_tx = NULL;
184845386Swpaul	struct ifnet		*ifp;
184945386Swpaul
185045386Swpaul	ifp = &sc->arpcom.ac_if;
185145386Swpaul
185245386Swpaul	/*
185345386Swpaul	 * Go through our tx ring and free mbufs for those
185445386Swpaul	 * frames that have been sent.
185545386Swpaul	 */
185645386Swpaul	while (sc->ti_tx_saved_considx != sc->ti_tx_considx.ti_idx) {
185745386Swpaul		u_int32_t		idx = 0;
185845386Swpaul
185945386Swpaul		idx = sc->ti_tx_saved_considx;
186045386Swpaul		if (sc->ti_hwrev == TI_HWREV_TIGON) {
186145386Swpaul			if (idx > 383)
186245386Swpaul				CSR_WRITE_4(sc, TI_WINBASE,
186345386Swpaul				    TI_TX_RING_BASE + 6144);
186445386Swpaul			else if (idx > 255)
186545386Swpaul				CSR_WRITE_4(sc, TI_WINBASE,
186645386Swpaul				    TI_TX_RING_BASE + 4096);
186745386Swpaul			else if (idx > 127)
186845386Swpaul				CSR_WRITE_4(sc, TI_WINBASE,
186945386Swpaul				    TI_TX_RING_BASE + 2048);
187045386Swpaul			else
187145386Swpaul				CSR_WRITE_4(sc, TI_WINBASE,
187245386Swpaul				    TI_TX_RING_BASE);
187345386Swpaul			cur_tx = &sc->ti_rdata->ti_tx_ring_nic[idx % 128];
187445386Swpaul		} else
187545386Swpaul			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
187645386Swpaul		if (cur_tx->ti_flags & TI_BDFLAG_END)
187745386Swpaul			ifp->if_opackets++;
187845386Swpaul		if (sc->ti_cdata.ti_tx_chain[idx] != NULL) {
187945386Swpaul			m_freem(sc->ti_cdata.ti_tx_chain[idx]);
188045386Swpaul			sc->ti_cdata.ti_tx_chain[idx] = NULL;
188145386Swpaul		}
188248011Swpaul		sc->ti_txcnt--;
188345386Swpaul		TI_INC(sc->ti_tx_saved_considx, TI_TX_RING_CNT);
188445386Swpaul		ifp->if_timer = 0;
188545386Swpaul	}
188645386Swpaul
188745386Swpaul	if (cur_tx != NULL)
188845386Swpaul		ifp->if_flags &= ~IFF_OACTIVE;
188945386Swpaul
189045386Swpaul	return;
189145386Swpaul}
189245386Swpaul
189345386Swpaulstatic void ti_intr(xsc)
189445386Swpaul	void			*xsc;
189545386Swpaul{
189645386Swpaul	struct ti_softc		*sc;
189745386Swpaul	struct ifnet		*ifp;
189845386Swpaul
189945386Swpaul	sc = xsc;
190067087Swpaul	TI_LOCK(sc);
190145386Swpaul	ifp = &sc->arpcom.ac_if;
190245386Swpaul
190345386Swpaul#ifdef notdef
190445386Swpaul	/* Avoid this for now -- checking this register is expensive. */
190545386Swpaul	/* Make sure this is really our interrupt. */
190667087Swpaul	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE)) {
190767087Swpaul		TI_UNLOCK(sc);
190845386Swpaul		return;
190967087Swpaul	}
191045386Swpaul#endif
191145386Swpaul
191245386Swpaul	/* Ack interrupt and stop others from occuring. */
191345386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
191445386Swpaul
191545386Swpaul	if (ifp->if_flags & IFF_RUNNING) {
191645386Swpaul		/* Check RX return ring producer/consumer */
191745386Swpaul		ti_rxeof(sc);
191845386Swpaul
191945386Swpaul		/* Check TX ring producer/consumer */
192045386Swpaul		ti_txeof(sc);
192145386Swpaul	}
192245386Swpaul
192345386Swpaul	ti_handle_events(sc);
192445386Swpaul
192545386Swpaul	/* Re-enable interrupts. */
192645386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
192745386Swpaul
192845386Swpaul	if (ifp->if_flags & IFF_RUNNING && ifp->if_snd.ifq_head != NULL)
192945386Swpaul		ti_start(ifp);
193045386Swpaul
193167087Swpaul	TI_UNLOCK(sc);
193267087Swpaul
193345386Swpaul	return;
193445386Swpaul}
193545386Swpaul
193645386Swpaulstatic void ti_stats_update(sc)
193745386Swpaul	struct ti_softc		*sc;
193845386Swpaul{
193945386Swpaul	struct ifnet		*ifp;
194045386Swpaul
194145386Swpaul	ifp = &sc->arpcom.ac_if;
194245386Swpaul
194345386Swpaul	ifp->if_collisions +=
194445386Swpaul	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
194545386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
194645386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
194745386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
194845386Swpaul	   ifp->if_collisions;
194945386Swpaul
195045386Swpaul	return;
195145386Swpaul}
195245386Swpaul
195345386Swpaul/*
195445386Swpaul * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
195545386Swpaul * pointers to descriptors.
195645386Swpaul */
195745386Swpaulstatic int ti_encap(sc, m_head, txidx)
195845386Swpaul	struct ti_softc		*sc;
195945386Swpaul	struct mbuf		*m_head;
196045386Swpaul	u_int32_t		*txidx;
196145386Swpaul{
196245386Swpaul	struct ti_tx_desc	*f = NULL;
196345386Swpaul	struct mbuf		*m;
196448011Swpaul	u_int32_t		frag, cur, cnt = 0;
196558698Sjlemon	u_int16_t		csum_flags = 0;
196645386Swpaul#if NVLAN > 0
196745386Swpaul	struct ifvlan		*ifv = NULL;
196845386Swpaul
196945386Swpaul	if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
197045386Swpaul	    m_head->m_pkthdr.rcvif != NULL &&
197145386Swpaul	    m_head->m_pkthdr.rcvif->if_type == IFT_8021_VLAN)
197245386Swpaul		ifv = m_head->m_pkthdr.rcvif->if_softc;
197345386Swpaul#endif
197445386Swpaul
197545386Swpaul	m = m_head;
197645386Swpaul	cur = frag = *txidx;
197745386Swpaul
197858698Sjlemon	if (m_head->m_pkthdr.csum_flags) {
197958698Sjlemon		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
198058698Sjlemon			csum_flags |= TI_BDFLAG_IP_CKSUM;
198158698Sjlemon		if (m_head->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
198258698Sjlemon			csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
198358698Sjlemon		if (m_head->m_flags & M_LASTFRAG)
198458698Sjlemon			csum_flags |= TI_BDFLAG_IP_FRAG_END;
198558698Sjlemon		else if (m_head->m_flags & M_FRAG)
198658698Sjlemon			csum_flags |= TI_BDFLAG_IP_FRAG;
198758698Sjlemon	}
198845386Swpaul	/*
198945386Swpaul 	 * Start packing the mbufs in this chain into
199045386Swpaul	 * the fragment pointers. Stop when we run out
199145386Swpaul 	 * of fragments or hit the end of the mbuf chain.
199245386Swpaul	 */
199345386Swpaul	for (m = m_head; m != NULL; m = m->m_next) {
199445386Swpaul		if (m->m_len != 0) {
199545386Swpaul			if (sc->ti_hwrev == TI_HWREV_TIGON) {
199645386Swpaul				if (frag > 383)
199745386Swpaul					CSR_WRITE_4(sc, TI_WINBASE,
199845386Swpaul					    TI_TX_RING_BASE + 6144);
199945386Swpaul				else if (frag > 255)
200045386Swpaul					CSR_WRITE_4(sc, TI_WINBASE,
200145386Swpaul					    TI_TX_RING_BASE + 4096);
200245386Swpaul				else if (frag > 127)
200345386Swpaul					CSR_WRITE_4(sc, TI_WINBASE,
200445386Swpaul					    TI_TX_RING_BASE + 2048);
200545386Swpaul				else
200645386Swpaul					CSR_WRITE_4(sc, TI_WINBASE,
200745386Swpaul					    TI_TX_RING_BASE);
200845386Swpaul				f = &sc->ti_rdata->ti_tx_ring_nic[frag % 128];
200945386Swpaul			} else
201045386Swpaul				f = &sc->ti_rdata->ti_tx_ring[frag];
201145386Swpaul			if (sc->ti_cdata.ti_tx_chain[frag] != NULL)
201245386Swpaul				break;
201345386Swpaul			TI_HOSTADDR(f->ti_addr) = vtophys(mtod(m, vm_offset_t));
201445386Swpaul			f->ti_len = m->m_len;
201558698Sjlemon			f->ti_flags = csum_flags;
201645386Swpaul#if NVLAN > 0
201745386Swpaul			if (ifv != NULL) {
201845386Swpaul				f->ti_flags |= TI_BDFLAG_VLAN_TAG;
201977058Sphk				f->ti_vlan_tag = ifv->ifv_tag & 0xfff;
202045386Swpaul			} else {
202145386Swpaul				f->ti_vlan_tag = 0;
202245386Swpaul			}
202345386Swpaul#endif
202448011Swpaul			/*
202548011Swpaul			 * Sanity check: avoid coming within 16 descriptors
202648011Swpaul			 * of the end of the ring.
202748011Swpaul			 */
202848011Swpaul			if ((TI_TX_RING_CNT - (sc->ti_txcnt + cnt)) < 16)
202948011Swpaul				return(ENOBUFS);
203045386Swpaul			cur = frag;
203145386Swpaul			TI_INC(frag, TI_TX_RING_CNT);
203248011Swpaul			cnt++;
203345386Swpaul		}
203445386Swpaul	}
203545386Swpaul
203645386Swpaul	if (m != NULL)
203745386Swpaul		return(ENOBUFS);
203845386Swpaul
203946177Swpaul	if (frag == sc->ti_tx_saved_considx)
204046177Swpaul		return(ENOBUFS);
204146177Swpaul
204245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
204345386Swpaul		sc->ti_rdata->ti_tx_ring_nic[cur % 128].ti_flags |=
204445386Swpaul		    TI_BDFLAG_END;
204545386Swpaul	else
204645386Swpaul		sc->ti_rdata->ti_tx_ring[cur].ti_flags |= TI_BDFLAG_END;
204747458Swpaul	sc->ti_cdata.ti_tx_chain[cur] = m_head;
204848011Swpaul	sc->ti_txcnt += cnt;
204945386Swpaul
205045386Swpaul	*txidx = frag;
205145386Swpaul
205245386Swpaul	return(0);
205345386Swpaul}
205445386Swpaul
205545386Swpaul/*
205645386Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
205745386Swpaul * to the mbuf data regions directly in the transmit descriptors.
205845386Swpaul */
205945386Swpaulstatic void ti_start(ifp)
206045386Swpaul	struct ifnet		*ifp;
206145386Swpaul{
206245386Swpaul	struct ti_softc		*sc;
206345386Swpaul	struct mbuf		*m_head = NULL;
206445386Swpaul	u_int32_t		prodidx = 0;
206545386Swpaul
206645386Swpaul	sc = ifp->if_softc;
206767087Swpaul	TI_LOCK(sc);
206845386Swpaul
206945386Swpaul	prodidx = CSR_READ_4(sc, TI_MB_SENDPROD_IDX);
207045386Swpaul
207145386Swpaul	while(sc->ti_cdata.ti_tx_chain[prodidx] == NULL) {
207245386Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
207345386Swpaul		if (m_head == NULL)
207445386Swpaul			break;
207545386Swpaul
207645386Swpaul		/*
207758698Sjlemon		 * XXX
207858698Sjlemon		 * safety overkill.  If this is a fragmented packet chain
207958698Sjlemon		 * with delayed TCP/UDP checksums, then only encapsulate
208058698Sjlemon		 * it if we have enough descriptors to handle the entire
208158698Sjlemon		 * chain at once.
208258698Sjlemon		 * (paranoia -- may not actually be needed)
208358698Sjlemon		 */
208458698Sjlemon		if (m_head->m_flags & M_FIRSTFRAG &&
208558698Sjlemon		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
208658698Sjlemon			if ((TI_TX_RING_CNT - sc->ti_txcnt) <
208758698Sjlemon			    m_head->m_pkthdr.csum_data + 16) {
208858698Sjlemon				IF_PREPEND(&ifp->if_snd, m_head);
208958698Sjlemon				ifp->if_flags |= IFF_OACTIVE;
209058698Sjlemon				break;
209158698Sjlemon			}
209258698Sjlemon		}
209358698Sjlemon
209458698Sjlemon		/*
209545386Swpaul		 * Pack the data into the transmit ring. If we
209645386Swpaul		 * don't have room, set the OACTIVE flag and wait
209745386Swpaul		 * for the NIC to drain the ring.
209845386Swpaul		 */
209945386Swpaul		if (ti_encap(sc, m_head, &prodidx)) {
210045386Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
210145386Swpaul			ifp->if_flags |= IFF_OACTIVE;
210245386Swpaul			break;
210345386Swpaul		}
210445386Swpaul
210545386Swpaul		/*
210645386Swpaul		 * If there's a BPF listener, bounce a copy of this frame
210745386Swpaul		 * to him.
210845386Swpaul		 */
210945386Swpaul		if (ifp->if_bpf)
211045386Swpaul			bpf_mtap(ifp, m_head);
211145386Swpaul	}
211245386Swpaul
211345386Swpaul	/* Transmit */
211445386Swpaul	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, prodidx);
211545386Swpaul
211645386Swpaul	/*
211745386Swpaul	 * Set a timeout in case the chip goes out to lunch.
211845386Swpaul	 */
211945386Swpaul	ifp->if_timer = 5;
212067087Swpaul	TI_UNLOCK(sc);
212145386Swpaul
212245386Swpaul	return;
212345386Swpaul}
212445386Swpaul
212545386Swpaulstatic void ti_init(xsc)
212645386Swpaul	void			*xsc;
212745386Swpaul{
212845386Swpaul	struct ti_softc		*sc = xsc;
212945386Swpaul
213045386Swpaul	/* Cancel pending I/O and flush buffers. */
213145386Swpaul	ti_stop(sc);
213245386Swpaul
213367087Swpaul	TI_LOCK(sc);
213445386Swpaul	/* Init the gen info block, ring control blocks and firmware. */
213545386Swpaul	if (ti_gibinit(sc)) {
213645386Swpaul		printf("ti%d: initialization failure\n", sc->ti_unit);
213767087Swpaul		TI_UNLOCK(sc);
213845386Swpaul		return;
213945386Swpaul	}
214045386Swpaul
214167087Swpaul	TI_UNLOCK(sc);
214245386Swpaul
214345386Swpaul	return;
214445386Swpaul}
214545386Swpaul
214645386Swpaulstatic void ti_init2(sc)
214745386Swpaul	struct ti_softc		*sc;
214845386Swpaul{
214945386Swpaul	struct ti_cmd_desc	cmd;
215045386Swpaul	struct ifnet		*ifp;
215145386Swpaul	u_int16_t		*m;
215245386Swpaul	struct ifmedia		*ifm;
215345386Swpaul	int			tmp;
215445386Swpaul
215545386Swpaul	ifp = &sc->arpcom.ac_if;
215645386Swpaul
215745386Swpaul	/* Specify MTU and interface index. */
215845386Swpaul	CSR_WRITE_4(sc, TI_GCR_IFINDEX, ifp->if_unit);
215945386Swpaul	CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
216045386Swpaul	    ETHER_HDR_LEN + ETHER_CRC_LEN);
216145386Swpaul	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
216245386Swpaul
216345386Swpaul	/* Load our MAC address. */
216445386Swpaul	m = (u_int16_t *)&sc->arpcom.ac_enaddr[0];
216545386Swpaul	CSR_WRITE_4(sc, TI_GCR_PAR0, htons(m[0]));
216645386Swpaul	CSR_WRITE_4(sc, TI_GCR_PAR1, (htons(m[1]) << 16) | htons(m[2]));
216745386Swpaul	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
216845386Swpaul
216945386Swpaul	/* Enable or disable promiscuous mode as needed. */
217045386Swpaul	if (ifp->if_flags & IFF_PROMISC) {
217145386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
217245386Swpaul	} else {
217345386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
217445386Swpaul	}
217545386Swpaul
217645386Swpaul	/* Program multicast filter. */
217745386Swpaul	ti_setmulti(sc);
217845386Swpaul
217945386Swpaul	/*
218045386Swpaul	 * If this is a Tigon 1, we should tell the
218145386Swpaul	 * firmware to use software packet filtering.
218245386Swpaul	 */
218345386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON) {
218445386Swpaul		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
218545386Swpaul	}
218645386Swpaul
218745386Swpaul	/* Init RX ring. */
218845386Swpaul	ti_init_rx_ring_std(sc);
218945386Swpaul
219045386Swpaul	/* Init jumbo RX ring. */
219145386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
219245386Swpaul		ti_init_rx_ring_jumbo(sc);
219345386Swpaul
219445386Swpaul	/*
219545386Swpaul	 * If this is a Tigon 2, we can also configure the
219645386Swpaul	 * mini ring.
219745386Swpaul	 */
219845386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
219945386Swpaul		ti_init_rx_ring_mini(sc);
220045386Swpaul
220145386Swpaul	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
220245386Swpaul	sc->ti_rx_saved_considx = 0;
220345386Swpaul
220445386Swpaul	/* Init TX ring. */
220545386Swpaul	ti_init_tx_ring(sc);
220645386Swpaul
220745386Swpaul	/* Tell firmware we're alive. */
220845386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
220945386Swpaul
221045386Swpaul	/* Enable host interrupts. */
221145386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
221245386Swpaul
221345386Swpaul	ifp->if_flags |= IFF_RUNNING;
221445386Swpaul	ifp->if_flags &= ~IFF_OACTIVE;
221545386Swpaul
221645386Swpaul	/*
221745386Swpaul	 * Make sure to set media properly. We have to do this
221845386Swpaul	 * here since we have to issue commands in order to set
221945386Swpaul	 * the link negotiation and we can't issue commands until
222045386Swpaul	 * the firmware is running.
222145386Swpaul	 */
222245386Swpaul	ifm = &sc->ifmedia;
222345386Swpaul	tmp = ifm->ifm_media;
222445386Swpaul	ifm->ifm_media = ifm->ifm_cur->ifm_media;
222545386Swpaul	ti_ifmedia_upd(ifp);
222645386Swpaul	ifm->ifm_media = tmp;
222745386Swpaul
222845386Swpaul	return;
222945386Swpaul}
223045386Swpaul
223145386Swpaul/*
223245386Swpaul * Set media options.
223345386Swpaul */
223445386Swpaulstatic int ti_ifmedia_upd(ifp)
223545386Swpaul	struct ifnet		*ifp;
223645386Swpaul{
223745386Swpaul	struct ti_softc		*sc;
223845386Swpaul	struct ifmedia		*ifm;
223945386Swpaul	struct ti_cmd_desc	cmd;
224045386Swpaul
224145386Swpaul	sc = ifp->if_softc;
224245386Swpaul	ifm = &sc->ifmedia;
224345386Swpaul
224445386Swpaul	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
224545386Swpaul		return(EINVAL);
224645386Swpaul
224745386Swpaul	switch(IFM_SUBTYPE(ifm->ifm_media)) {
224845386Swpaul	case IFM_AUTO:
224945386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
225045386Swpaul		    TI_GLNK_FULL_DUPLEX|TI_GLNK_RX_FLOWCTL_Y|
225145386Swpaul		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
225245386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
225345386Swpaul		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX|
225445386Swpaul		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
225545386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
225645386Swpaul		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
225745386Swpaul		break;
225845386Swpaul	case IFM_1000_SX:
225963699Swpaul	case IFM_1000_TX:
226045386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
226163699Swpaul		    TI_GLNK_RX_FLOWCTL_Y|TI_GLNK_ENB);
226245386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
226363699Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
226463699Swpaul			TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
226563699Swpaul		}
226645386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
226745386Swpaul		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
226845386Swpaul		break;
226945386Swpaul	case IFM_100_FX:
227045386Swpaul	case IFM_10_FL:
227163699Swpaul	case IFM_100_TX:
227263699Swpaul	case IFM_10_T:
227345386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
227445386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF);
227563699Swpaul		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
227663699Swpaul		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
227745386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
227845386Swpaul		} else {
227945386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
228045386Swpaul		}
228145386Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
228245386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
228345386Swpaul		} else {
228445386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
228545386Swpaul		}
228645386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
228745386Swpaul		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
228845386Swpaul		break;
228945386Swpaul	}
229045386Swpaul
229145386Swpaul	return(0);
229245386Swpaul}
229345386Swpaul
229445386Swpaul/*
229545386Swpaul * Report current media status.
229645386Swpaul */
229745386Swpaulstatic void ti_ifmedia_sts(ifp, ifmr)
229845386Swpaul	struct ifnet		*ifp;
229945386Swpaul	struct ifmediareq	*ifmr;
230045386Swpaul{
230145386Swpaul	struct ti_softc		*sc;
230263699Swpaul	u_int32_t		media = 0;
230345386Swpaul
230445386Swpaul	sc = ifp->if_softc;
230545386Swpaul
230645386Swpaul	ifmr->ifm_status = IFM_AVALID;
230745386Swpaul	ifmr->ifm_active = IFM_ETHER;
230845386Swpaul
230945386Swpaul	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN)
231045386Swpaul		return;
231145386Swpaul
231245386Swpaul	ifmr->ifm_status |= IFM_ACTIVE;
231345386Swpaul
231463699Swpaul	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
231563699Swpaul		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
231663699Swpaul		if (sc->ti_copper)
231763699Swpaul			ifmr->ifm_active |= IFM_1000_TX;
231863699Swpaul		else
231963699Swpaul			ifmr->ifm_active |= IFM_1000_SX;
232063699Swpaul		if (media & TI_GLNK_FULL_DUPLEX)
232163699Swpaul			ifmr->ifm_active |= IFM_FDX;
232263699Swpaul		else
232363699Swpaul			ifmr->ifm_active |= IFM_HDX;
232463699Swpaul	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
232545386Swpaul		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
232663699Swpaul		if (sc->ti_copper) {
232763699Swpaul			if (media & TI_LNK_100MB)
232863699Swpaul				ifmr->ifm_active |= IFM_100_TX;
232963699Swpaul			if (media & TI_LNK_10MB)
233063699Swpaul				ifmr->ifm_active |= IFM_10_T;
233163699Swpaul		} else {
233263699Swpaul			if (media & TI_LNK_100MB)
233363699Swpaul				ifmr->ifm_active |= IFM_100_FX;
233463699Swpaul			if (media & TI_LNK_10MB)
233563699Swpaul				ifmr->ifm_active |= IFM_10_FL;
233663699Swpaul		}
233745386Swpaul		if (media & TI_LNK_FULL_DUPLEX)
233845386Swpaul			ifmr->ifm_active |= IFM_FDX;
233945386Swpaul		if (media & TI_LNK_HALF_DUPLEX)
234045386Swpaul			ifmr->ifm_active |= IFM_HDX;
234145386Swpaul	}
234245386Swpaul
234345386Swpaul	return;
234445386Swpaul}
234545386Swpaul
234645386Swpaulstatic int ti_ioctl(ifp, command, data)
234745386Swpaul	struct ifnet		*ifp;
234845386Swpaul	u_long			command;
234945386Swpaul	caddr_t			data;
235045386Swpaul{
235145386Swpaul	struct ti_softc		*sc = ifp->if_softc;
235245386Swpaul	struct ifreq		*ifr = (struct ifreq *) data;
235367087Swpaul	int			error = 0;
235445386Swpaul	struct ti_cmd_desc	cmd;
235545386Swpaul
235667087Swpaul	TI_LOCK(sc);
235745386Swpaul
235845386Swpaul	switch(command) {
235945386Swpaul	case SIOCSIFADDR:
236045386Swpaul	case SIOCGIFADDR:
236145386Swpaul		error = ether_ioctl(ifp, command, data);
236245386Swpaul		break;
236345386Swpaul	case SIOCSIFMTU:
236445386Swpaul		if (ifr->ifr_mtu > TI_JUMBO_MTU)
236545386Swpaul			error = EINVAL;
236645386Swpaul		else {
236745386Swpaul			ifp->if_mtu = ifr->ifr_mtu;
236845386Swpaul			ti_init(sc);
236945386Swpaul		}
237045386Swpaul		break;
237145386Swpaul	case SIOCSIFFLAGS:
237245386Swpaul		if (ifp->if_flags & IFF_UP) {
237345386Swpaul			/*
237445386Swpaul			 * If only the state of the PROMISC flag changed,
237545386Swpaul			 * then just use the 'set promisc mode' command
237645386Swpaul			 * instead of reinitializing the entire NIC. Doing
237745386Swpaul			 * a full re-init means reloading the firmware and
237845386Swpaul			 * waiting for it to start up, which may take a
237945386Swpaul			 * second or two.
238045386Swpaul			 */
238145386Swpaul			if (ifp->if_flags & IFF_RUNNING &&
238245386Swpaul			    ifp->if_flags & IFF_PROMISC &&
238345386Swpaul			    !(sc->ti_if_flags & IFF_PROMISC)) {
238445386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
238545386Swpaul				    TI_CMD_CODE_PROMISC_ENB, 0);
238645386Swpaul			} else if (ifp->if_flags & IFF_RUNNING &&
238745386Swpaul			    !(ifp->if_flags & IFF_PROMISC) &&
238845386Swpaul			    sc->ti_if_flags & IFF_PROMISC) {
238945386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
239045386Swpaul				    TI_CMD_CODE_PROMISC_DIS, 0);
239145386Swpaul			} else
239245386Swpaul				ti_init(sc);
239345386Swpaul		} else {
239445386Swpaul			if (ifp->if_flags & IFF_RUNNING) {
239545386Swpaul				ti_stop(sc);
239645386Swpaul			}
239745386Swpaul		}
239845386Swpaul		sc->ti_if_flags = ifp->if_flags;
239945386Swpaul		error = 0;
240045386Swpaul		break;
240145386Swpaul	case SIOCADDMULTI:
240245386Swpaul	case SIOCDELMULTI:
240345386Swpaul		if (ifp->if_flags & IFF_RUNNING) {
240445386Swpaul			ti_setmulti(sc);
240545386Swpaul			error = 0;
240645386Swpaul		}
240745386Swpaul		break;
240845386Swpaul	case SIOCSIFMEDIA:
240945386Swpaul	case SIOCGIFMEDIA:
241045386Swpaul		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
241145386Swpaul		break;
241245386Swpaul	default:
241345386Swpaul		error = EINVAL;
241445386Swpaul		break;
241545386Swpaul	}
241645386Swpaul
241767087Swpaul	TI_UNLOCK(sc);
241845386Swpaul
241945386Swpaul	return(error);
242045386Swpaul}
242145386Swpaul
242245386Swpaulstatic void ti_watchdog(ifp)
242345386Swpaul	struct ifnet		*ifp;
242445386Swpaul{
242545386Swpaul	struct ti_softc		*sc;
242645386Swpaul
242745386Swpaul	sc = ifp->if_softc;
242867087Swpaul	TI_LOCK(sc);
242945386Swpaul
243045386Swpaul	printf("ti%d: watchdog timeout -- resetting\n", sc->ti_unit);
243145386Swpaul	ti_stop(sc);
243245386Swpaul	ti_init(sc);
243345386Swpaul
243445386Swpaul	ifp->if_oerrors++;
243567087Swpaul	TI_UNLOCK(sc);
243645386Swpaul
243745386Swpaul	return;
243845386Swpaul}
243945386Swpaul
244045386Swpaul/*
244145386Swpaul * Stop the adapter and free any mbufs allocated to the
244245386Swpaul * RX and TX lists.
244345386Swpaul */
244445386Swpaulstatic void ti_stop(sc)
244545386Swpaul	struct ti_softc		*sc;
244645386Swpaul{
244745386Swpaul	struct ifnet		*ifp;
244845386Swpaul	struct ti_cmd_desc	cmd;
244945386Swpaul
245067087Swpaul	TI_LOCK(sc);
245167087Swpaul
245245386Swpaul	ifp = &sc->arpcom.ac_if;
245345386Swpaul
245445386Swpaul	/* Disable host interrupts. */
245545386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
245645386Swpaul	/*
245745386Swpaul	 * Tell firmware we're shutting down.
245845386Swpaul	 */
245945386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
246045386Swpaul
246145386Swpaul	/* Halt and reinitialize. */
246245386Swpaul	ti_chipinit(sc);
246345386Swpaul	ti_mem(sc, 0x2000, 0x100000 - 0x2000, NULL);
246445386Swpaul	ti_chipinit(sc);
246545386Swpaul
246645386Swpaul	/* Free the RX lists. */
246745386Swpaul	ti_free_rx_ring_std(sc);
246845386Swpaul
246945386Swpaul	/* Free jumbo RX list. */
247045386Swpaul	ti_free_rx_ring_jumbo(sc);
247145386Swpaul
247245386Swpaul	/* Free mini RX list. */
247345386Swpaul	ti_free_rx_ring_mini(sc);
247445386Swpaul
247545386Swpaul	/* Free TX buffers. */
247645386Swpaul	ti_free_tx_ring(sc);
247745386Swpaul
247845386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
247945386Swpaul	sc->ti_return_prodidx.ti_idx = 0;
248045386Swpaul	sc->ti_tx_considx.ti_idx = 0;
248145386Swpaul	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
248245386Swpaul
248345386Swpaul	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
248467087Swpaul	TI_UNLOCK(sc);
248545386Swpaul
248645386Swpaul	return;
248745386Swpaul}
248845386Swpaul
248945386Swpaul/*
249045386Swpaul * Stop all chip I/O so that the kernel's probe routines don't
249145386Swpaul * get confused by errant DMAs when rebooting.
249245386Swpaul */
249349011Swpaulstatic void ti_shutdown(dev)
249449011Swpaul	device_t		dev;
249545386Swpaul{
249645386Swpaul	struct ti_softc		*sc;
249745386Swpaul
249849011Swpaul	sc = device_get_softc(dev);
249967087Swpaul	TI_LOCK(sc);
250045386Swpaul	ti_chipinit(sc);
250167087Swpaul	TI_UNLOCK(sc);
250245386Swpaul
250345386Swpaul	return;
250445386Swpaul}
2505