if_ti.c revision 227098
1139825Simp/*-
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 */
3245386Swpaul
3345386Swpaul/*
3445386Swpaul * Alteon Networks Tigon PCI gigabit ethernet driver for FreeBSD.
3545386Swpaul * Manuals, sample driver and firmware source kits are available
3645386Swpaul * from http://www.alteon.com/support/openkits.
37131652Sbms *
3845386Swpaul * Written by Bill Paul <wpaul@ctr.columbia.edu>
3945386Swpaul * Electrical Engineering Department
4045386Swpaul * Columbia University, New York City
4145386Swpaul */
4245386Swpaul
4345386Swpaul/*
4445386Swpaul * The Alteon Networks Tigon chip contains an embedded R4000 CPU,
4545386Swpaul * gigabit MAC, dual DMA channels and a PCI interface unit. NICs
4645386Swpaul * using the Tigon may have anywhere from 512K to 2MB of SRAM. The
4745386Swpaul * Tigon supports hardware IP, TCP and UCP checksumming, multicast
4845386Swpaul * filtering and jumbo (9014 byte) frames. The hardware is largely
4945386Swpaul * controlled by firmware, which must be loaded into the NIC during
5045386Swpaul * initialization.
5145386Swpaul *
5245386Swpaul * The Tigon 2 contains 2 R4000 CPUs and requires a newer firmware
5345386Swpaul * revision, which supports new features such as extended commands,
5445386Swpaul * extended jumbo receive ring desciptors and a mini receive ring.
5545386Swpaul *
5645386Swpaul * Alteon Networks is to be commended for releasing such a vast amount
5745386Swpaul * of development material for the Tigon NIC without requiring an NDA
5845386Swpaul * (although they really should have done it a long time ago). With
5945386Swpaul * any luck, the other vendors will finally wise up and follow Alteon's
6045386Swpaul * stellar example.
6145386Swpaul *
6245386Swpaul * The firmware for the Tigon 1 and 2 NICs is compiled directly into
6345386Swpaul * this driver by #including it as a C header file. This bloats the
6445386Swpaul * driver somewhat, but it's the easiest method considering that the
6545386Swpaul * driver code and firmware code need to be kept in sync. The source
6645386Swpaul * for the firmware is not provided with the FreeBSD distribution since
6745386Swpaul * compiling it requires a GNU toolchain targeted for mips-sgi-irix5.3.
6845386Swpaul *
6945386Swpaul * The following people deserve special thanks:
7045386Swpaul * - Terry Murphy of 3Com, for providing a 3c985 Tigon 1 board
7145386Swpaul *   for testing
7245386Swpaul * - Raymond Lee of Netgear, for providing a pair of Netgear
7345386Swpaul *   GA620 Tigon 2 boards for testing
7445386Swpaul * - Ulf Zimmermann, for bringing the GA260 to my attention and
7545386Swpaul *   convincing me to write this driver.
7645386Swpaul * - Andrew Gallatin for providing FreeBSD/Alpha support.
7745386Swpaul */
7845386Swpaul
79113038Sobrien#include <sys/cdefs.h>
80113038Sobrien__FBSDID("$FreeBSD: head/sys/dev/ti/if_ti.c 227098 2011-11-04 23:09:57Z yongari $");
81113038Sobrien
8298849Sken#include "opt_ti.h"
8398849Sken
8445386Swpaul#include <sys/param.h>
8545386Swpaul#include <sys/systm.h>
8645386Swpaul#include <sys/sockio.h>
8745386Swpaul#include <sys/mbuf.h>
8845386Swpaul#include <sys/malloc.h>
8945386Swpaul#include <sys/kernel.h>
90129878Sphk#include <sys/module.h>
9145386Swpaul#include <sys/socket.h>
9245386Swpaul#include <sys/queue.h>
9398849Sken#include <sys/conf.h>
94153770Syongari#include <sys/sf_buf.h>
9545386Swpaul
9645386Swpaul#include <net/if.h>
9745386Swpaul#include <net/if_arp.h>
9845386Swpaul#include <net/ethernet.h>
9945386Swpaul#include <net/if_dl.h>
10045386Swpaul#include <net/if_media.h>
10183115Sbrooks#include <net/if_types.h>
10283115Sbrooks#include <net/if_vlan_var.h>
10345386Swpaul
10445386Swpaul#include <net/bpf.h>
10545386Swpaul
10645386Swpaul#include <netinet/in_systm.h>
10745386Swpaul#include <netinet/in.h>
10845386Swpaul#include <netinet/ip.h>
10945386Swpaul
11045386Swpaul#include <machine/bus.h>
11149011Swpaul#include <machine/resource.h>
11249011Swpaul#include <sys/bus.h>
11349011Swpaul#include <sys/rman.h>
11445386Swpaul
11598849Sken/* #define TI_PRIVATE_JUMBOS */
116153770Syongari#ifndef TI_PRIVATE_JUMBOS
117153770Syongari#include <vm/vm.h>
11898849Sken#include <vm/vm_page.h>
119153770Syongari#endif
12098849Sken
121119288Simp#include <dev/pci/pcireg.h>
122119288Simp#include <dev/pci/pcivar.h>
12345386Swpaul
12498849Sken#include <sys/tiio.h>
125153280Sscottl#include <dev/ti/if_tireg.h>
126153280Sscottl#include <dev/ti/ti_fw.h>
127153280Sscottl#include <dev/ti/ti_fw2.h>
12845386Swpaul
12958698Sjlemon#define TI_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_IP_FRAGS)
13098849Sken/*
13198849Sken * We can only turn on header splitting if we're using extended receive
13298849Sken * BDs.
13398849Sken */
13498849Sken#if defined(TI_JUMBO_HDRSPLIT) && defined(TI_PRIVATE_JUMBOS)
13598849Sken#error "options TI_JUMBO_HDRSPLIT and TI_PRIVATE_JUMBOS are mutually exclusive"
13698849Sken#endif /* TI_JUMBO_HDRSPLIT && TI_JUMBO_HDRSPLIT */
13745386Swpaul
13898849Skentypedef enum {
13998849Sken	TI_SWAP_HTON,
14098849Sken	TI_SWAP_NTOH
14198849Sken} ti_swap_type;
14298849Sken
14345386Swpaul/*
14445386Swpaul * Various supported device vendors/types and their names.
14545386Swpaul */
14645386Swpaul
147219547Smariusstatic const struct ti_type const ti_devs[] = {
14845386Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC,
14963702Swpaul		"Alteon AceNIC 1000baseSX Gigabit Ethernet" },
15063699Swpaul	{ ALT_VENDORID,	ALT_DEVICEID_ACENIC_COPPER,
15163702Swpaul		"Alteon AceNIC 1000baseT Gigabit Ethernet" },
15245386Swpaul	{ TC_VENDORID,	TC_DEVICEID_3C985,
15345386Swpaul		"3Com 3c985-SX Gigabit Ethernet" },
15445386Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620,
15564139Swpaul		"Netgear GA620 1000baseSX Gigabit Ethernet" },
15664139Swpaul	{ NG_VENDORID, NG_DEVICEID_GA620T,
15764139Swpaul		"Netgear GA620 1000baseT Gigabit Ethernet" },
15845386Swpaul	{ SGI_VENDORID, SGI_DEVICEID_TIGON,
15945386Swpaul		"Silicon Graphics Gigabit Ethernet" },
16056206Swpaul	{ DEC_VENDORID, DEC_DEVICEID_FARALLON_PN9000SX,
16156206Swpaul		"Farallon PN9000SX Gigabit Ethernet" },
16245386Swpaul	{ 0, 0, NULL }
16345386Swpaul};
16445386Swpaul
16598849Sken
16698849Skenstatic	d_open_t	ti_open;
16798849Skenstatic	d_close_t	ti_close;
16898849Skenstatic	d_ioctl_t	ti_ioctl2;
16998849Sken
17098849Skenstatic struct cdevsw ti_cdevsw = {
171126080Sphk	.d_version =	D_VERSION,
172153281Sscottl	.d_flags =	0,
173111815Sphk	.d_open =	ti_open,
174111815Sphk	.d_close =	ti_close,
175111815Sphk	.d_ioctl =	ti_ioctl2,
176111815Sphk	.d_name =	"ti",
17798849Sken};
17898849Sken
179142407Simpstatic int ti_probe(device_t);
180142407Simpstatic int ti_attach(device_t);
181142407Simpstatic int ti_detach(device_t);
182142407Simpstatic void ti_txeof(struct ti_softc *);
183142407Simpstatic void ti_rxeof(struct ti_softc *);
18445386Swpaul
185142407Simpstatic void ti_stats_update(struct ti_softc *);
186153982Syongaristatic int ti_encap(struct ti_softc *, struct mbuf **);
18745386Swpaul
188142407Simpstatic void ti_intr(void *);
189142407Simpstatic void ti_start(struct ifnet *);
190153770Syongaristatic void ti_start_locked(struct ifnet *);
191142407Simpstatic int ti_ioctl(struct ifnet *, u_long, caddr_t);
192142407Simpstatic void ti_init(void *);
193153770Syongaristatic void ti_init_locked(void *);
194142407Simpstatic void ti_init2(struct ti_softc *);
195142407Simpstatic void ti_stop(struct ti_softc *);
196199559Sjhbstatic void ti_watchdog(void *);
197173839Syongaristatic int ti_shutdown(device_t);
198142407Simpstatic int ti_ifmedia_upd(struct ifnet *);
199227093Syongaristatic int ti_ifmedia_upd_locked(struct ti_softc *);
200142407Simpstatic void ti_ifmedia_sts(struct ifnet *, struct ifmediareq *);
20145386Swpaul
202227089Syongaristatic uint32_t ti_eeprom_putbyte(struct ti_softc *, int);
203227089Syongaristatic uint8_t	ti_eeprom_getbyte(struct ti_softc *, int, uint8_t *);
204142407Simpstatic int ti_read_eeprom(struct ti_softc *, caddr_t, int, int);
20545386Swpaul
206142407Simpstatic void ti_add_mcast(struct ti_softc *, struct ether_addr *);
207142407Simpstatic void ti_del_mcast(struct ti_softc *, struct ether_addr *);
208142407Simpstatic void ti_setmulti(struct ti_softc *);
20945386Swpaul
210227089Syongaristatic void ti_mem_read(struct ti_softc *, uint32_t, uint32_t, void *);
211227089Syongaristatic void ti_mem_write(struct ti_softc *, uint32_t, uint32_t, void *);
212227089Syongaristatic void ti_mem_zero(struct ti_softc *, uint32_t, uint32_t);
213227089Syongaristatic int ti_copy_mem(struct ti_softc *, uint32_t, uint32_t, caddr_t, int,
214227087Syongari    int);
215227089Syongaristatic int ti_copy_scratch(struct ti_softc *, uint32_t, uint32_t, caddr_t,
216227087Syongari    int, int, int);
217142407Simpstatic int ti_bcopy_swap(const void *, void *, size_t, ti_swap_type);
218142407Simpstatic void ti_loadfw(struct ti_softc *);
219142407Simpstatic void ti_cmd(struct ti_softc *, struct ti_cmd_desc *);
220142407Simpstatic void ti_cmd_ext(struct ti_softc *, struct ti_cmd_desc *, caddr_t, int);
221142407Simpstatic void ti_handle_events(struct ti_softc *);
222153396Sscottlstatic int ti_alloc_dmamaps(struct ti_softc *);
223153396Sscottlstatic void ti_free_dmamaps(struct ti_softc *);
224153396Sscottlstatic int ti_alloc_jumbo_mem(struct ti_softc *);
22598849Sken#ifdef TI_PRIVATE_JUMBOS
226142407Simpstatic void *ti_jalloc(struct ti_softc *);
227142407Simpstatic void ti_jfree(void *, void *);
22898849Sken#endif /* TI_PRIVATE_JUMBOS */
229142407Simpstatic int ti_newbuf_std(struct ti_softc *, int, struct mbuf *);
230142407Simpstatic int ti_newbuf_mini(struct ti_softc *, int, struct mbuf *);
231142407Simpstatic int ti_newbuf_jumbo(struct ti_softc *, int, struct mbuf *);
232142407Simpstatic int ti_init_rx_ring_std(struct ti_softc *);
233142407Simpstatic void ti_free_rx_ring_std(struct ti_softc *);
234142407Simpstatic int ti_init_rx_ring_jumbo(struct ti_softc *);
235142407Simpstatic void ti_free_rx_ring_jumbo(struct ti_softc *);
236142407Simpstatic int ti_init_rx_ring_mini(struct ti_softc *);
237142407Simpstatic void ti_free_rx_ring_mini(struct ti_softc *);
238142407Simpstatic void ti_free_tx_ring(struct ti_softc *);
239142407Simpstatic int ti_init_tx_ring(struct ti_softc *);
24045386Swpaul
241142407Simpstatic int ti_64bitslot_war(struct ti_softc *);
242142407Simpstatic int ti_chipinit(struct ti_softc *);
243142407Simpstatic int ti_gibinit(struct ti_softc *);
24445386Swpaul
24598849Sken#ifdef TI_JUMBO_HDRSPLIT
246227087Syongaristatic __inline void ti_hdr_split(struct mbuf *top, int hdr_len, int pkt_len,
247227087Syongari    int idx);
24898849Sken#endif /* TI_JUMBO_HDRSPLIT */
24998849Sken
25049011Swpaulstatic device_method_t ti_methods[] = {
25149011Swpaul	/* Device interface */
25249011Swpaul	DEVMETHOD(device_probe,		ti_probe),
25349011Swpaul	DEVMETHOD(device_attach,	ti_attach),
25449011Swpaul	DEVMETHOD(device_detach,	ti_detach),
25549011Swpaul	DEVMETHOD(device_shutdown,	ti_shutdown),
25649011Swpaul	{ 0, 0 }
25749011Swpaul};
25849011Swpaul
25949011Swpaulstatic driver_t ti_driver = {
26051455Swpaul	"ti",
26149011Swpaul	ti_methods,
26249011Swpaul	sizeof(struct ti_softc)
26349011Swpaul};
26449011Swpaul
26549011Swpaulstatic devclass_t ti_devclass;
26649011Swpaul
267113506SmdoddDRIVER_MODULE(ti, pci, ti_driver, ti_devclass, 0, 0);
268113506SmdoddMODULE_DEPEND(ti, pci, 1, 1, 1);
269113506SmdoddMODULE_DEPEND(ti, ether, 1, 1, 1);
27049011Swpaul
27145386Swpaul/*
27245386Swpaul * Send an instruction or address to the EEPROM, check for ACK.
27345386Swpaul */
274227089Syongaristatic uint32_t
275227087Syongariti_eeprom_putbyte(struct ti_softc *sc, int byte)
27645386Swpaul{
277227087Syongari	int i, ack = 0;
27845386Swpaul
27945386Swpaul	/*
28045386Swpaul	 * Make sure we're in TX mode.
28145386Swpaul	 */
28245386Swpaul	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
28345386Swpaul
28445386Swpaul	/*
28545386Swpaul	 * Feed in each bit and stobe the clock.
28645386Swpaul	 */
28745386Swpaul	for (i = 0x80; i; i >>= 1) {
28845386Swpaul		if (byte & i) {
28945386Swpaul			TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
29045386Swpaul		} else {
29145386Swpaul			TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_DOUT);
29245386Swpaul		}
29345386Swpaul		DELAY(1);
29445386Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
29545386Swpaul		DELAY(1);
29645386Swpaul		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
29745386Swpaul	}
29845386Swpaul
29945386Swpaul	/*
30045386Swpaul	 * Turn off TX mode.
30145386Swpaul	 */
30245386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
30345386Swpaul
30445386Swpaul	/*
30545386Swpaul	 * Check for ack.
30645386Swpaul	 */
30745386Swpaul	TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
30845386Swpaul	ack = CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN;
30945386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
31045386Swpaul
311131654Sbms	return (ack);
31245386Swpaul}
31345386Swpaul
31445386Swpaul/*
31545386Swpaul * Read a byte of data stored in the EEPROM at address 'addr.'
31645386Swpaul * We have to send two address bytes since the EEPROM can hold
31745386Swpaul * more than 256 bytes of data.
31845386Swpaul */
319227089Syongaristatic uint8_t
320227089Syongariti_eeprom_getbyte(struct ti_softc *sc, int addr, uint8_t *dest)
32145386Swpaul{
322227087Syongari	int i;
323227089Syongari	uint8_t byte = 0;
32445386Swpaul
32545386Swpaul	EEPROM_START;
32645386Swpaul
32745386Swpaul	/*
32845386Swpaul	 * Send write control code to EEPROM.
32945386Swpaul	 */
33045386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
331162321Sglebius		device_printf(sc->ti_dev,
332150719Sjhb		    "failed to send write command, status: %x\n",
333150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
334131654Sbms		return (1);
33545386Swpaul	}
33645386Swpaul
33745386Swpaul	/*
33845386Swpaul	 * Send first byte of address of byte we want to read.
33945386Swpaul	 */
34045386Swpaul	if (ti_eeprom_putbyte(sc, (addr >> 8) & 0xFF)) {
341162321Sglebius		device_printf(sc->ti_dev, "failed to send address, status: %x\n",
342150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
343131654Sbms		return (1);
34445386Swpaul	}
34545386Swpaul	/*
34645386Swpaul	 * Send second byte address of byte we want to read.
34745386Swpaul	 */
34845386Swpaul	if (ti_eeprom_putbyte(sc, addr & 0xFF)) {
349162321Sglebius		device_printf(sc->ti_dev, "failed to send address, status: %x\n",
350150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
351131654Sbms		return (1);
35245386Swpaul	}
35345386Swpaul
35445386Swpaul	EEPROM_STOP;
35545386Swpaul	EEPROM_START;
35645386Swpaul	/*
35745386Swpaul	 * Send read control code to EEPROM.
35845386Swpaul	 */
35945386Swpaul	if (ti_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
360162321Sglebius		device_printf(sc->ti_dev,
361150719Sjhb		    "failed to send read command, status: %x\n",
362150719Sjhb		    CSR_READ_4(sc, TI_MISC_LOCAL_CTL));
363131654Sbms		return (1);
36445386Swpaul	}
36545386Swpaul
36645386Swpaul	/*
36745386Swpaul	 * Start reading bits from EEPROM.
36845386Swpaul	 */
36945386Swpaul	TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_TXEN);
37045386Swpaul	for (i = 0x80; i; i >>= 1) {
37145386Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
37245386Swpaul		DELAY(1);
37345386Swpaul		if (CSR_READ_4(sc, TI_MISC_LOCAL_CTL) & TI_MLC_EE_DIN)
37445386Swpaul			byte |= i;
37545386Swpaul		TI_CLRBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_EE_CLK);
37645386Swpaul		DELAY(1);
37745386Swpaul	}
37845386Swpaul
37945386Swpaul	EEPROM_STOP;
38045386Swpaul
38145386Swpaul	/*
38245386Swpaul	 * No ACK generated for read, so just return byte.
38345386Swpaul	 */
38445386Swpaul
38545386Swpaul	*dest = byte;
38645386Swpaul
387131654Sbms	return (0);
38845386Swpaul}
38945386Swpaul
39045386Swpaul/*
39145386Swpaul * Read a sequence of bytes from the EEPROM.
39245386Swpaul */
393102336Salfredstatic int
394227086Syongariti_read_eeprom(struct ti_softc *sc, caddr_t dest, int off, int cnt)
39545386Swpaul{
396227087Syongari	int err = 0, i;
397227089Syongari	uint8_t byte = 0;
39845386Swpaul
39945386Swpaul	for (i = 0; i < cnt; i++) {
40045386Swpaul		err = ti_eeprom_getbyte(sc, off + i, &byte);
40145386Swpaul		if (err)
40245386Swpaul			break;
40345386Swpaul		*(dest + i) = byte;
40445386Swpaul	}
40545386Swpaul
406131654Sbms	return (err ? 1 : 0);
40745386Swpaul}
40845386Swpaul
40945386Swpaul/*
410153770Syongari * NIC memory read function.
411153770Syongari * Can be used to copy data from NIC local memory.
41245386Swpaul */
413102336Salfredstatic void
414227089Syongariti_mem_read(struct ti_softc *sc, uint32_t addr, uint32_t len, void *buf)
41545386Swpaul{
416227087Syongari	int segptr, segsize, cnt;
417227087Syongari	char *ptr;
41845386Swpaul
41945386Swpaul	segptr = addr;
42045386Swpaul	cnt = len;
42145386Swpaul	ptr = buf;
42245386Swpaul
423131654Sbms	while (cnt) {
42445386Swpaul		if (cnt < TI_WINLEN)
42545386Swpaul			segsize = cnt;
42645386Swpaul		else
42745386Swpaul			segsize = TI_WINLEN - (segptr % TI_WINLEN);
42845386Swpaul		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
429153770Syongari		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
430227089Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), (uint32_t *)ptr,
431153770Syongari		    segsize / 4);
432153770Syongari		ptr += segsize;
43345386Swpaul		segptr += segsize;
43445386Swpaul		cnt -= segsize;
43545386Swpaul	}
43645386Swpaul}
43745386Swpaul
438153770Syongari
439153770Syongari/*
440153770Syongari * NIC memory write function.
441153770Syongari * Can be used to copy data into NIC local memory.
442153770Syongari */
443153770Syongaristatic void
444227089Syongariti_mem_write(struct ti_softc *sc, uint32_t addr, uint32_t len, void *buf)
445153770Syongari{
446227087Syongari	int segptr, segsize, cnt;
447227087Syongari	char *ptr;
448153770Syongari
449153770Syongari	segptr = addr;
450153770Syongari	cnt = len;
451153770Syongari	ptr = buf;
452153770Syongari
453153770Syongari	while (cnt) {
454153770Syongari		if (cnt < TI_WINLEN)
455153770Syongari			segsize = cnt;
456153770Syongari		else
457153770Syongari			segsize = TI_WINLEN - (segptr % TI_WINLEN);
458153770Syongari		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
459153770Syongari		bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
460227089Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), (uint32_t *)ptr,
461153770Syongari		    segsize / 4);
462153770Syongari		ptr += segsize;
463153770Syongari		segptr += segsize;
464153770Syongari		cnt -= segsize;
465153770Syongari	}
466153770Syongari}
467153770Syongari
468153770Syongari/*
469153770Syongari * NIC memory read function.
470153770Syongari * Can be used to clear a section of NIC local memory.
471153770Syongari */
472153770Syongaristatic void
473227089Syongariti_mem_zero(struct ti_softc *sc, uint32_t addr, uint32_t len)
474153770Syongari{
475227087Syongari	int segptr, segsize, cnt;
476153770Syongari
477153770Syongari	segptr = addr;
478153770Syongari	cnt = len;
479153770Syongari
480153770Syongari	while (cnt) {
481153770Syongari		if (cnt < TI_WINLEN)
482153770Syongari			segsize = cnt;
483153770Syongari		else
484153770Syongari			segsize = TI_WINLEN - (segptr % TI_WINLEN);
485153770Syongari		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
486153770Syongari		bus_space_set_region_4(sc->ti_btag, sc->ti_bhandle,
487153770Syongari		    TI_WINDOW + (segptr & (TI_WINLEN - 1)), 0, segsize / 4);
488153770Syongari		segptr += segsize;
489153770Syongari		cnt -= segsize;
490153770Syongari	}
491153770Syongari}
492153770Syongari
49398849Skenstatic int
494227089Syongariti_copy_mem(struct ti_softc *sc, uint32_t tigon_addr, uint32_t len,
495227086Syongari    caddr_t buf, int useraddr, int readdata)
49698849Sken{
497227087Syongari	int segptr, segsize, cnt;
498227087Syongari	caddr_t ptr;
499227089Syongari	uint32_t origwin;
500227089Syongari	uint8_t tmparray[TI_WINLEN], tmparray2[TI_WINLEN];
501227087Syongari	int resid, segresid;
502227087Syongari	int first_pass;
50398849Sken
504153770Syongari	TI_LOCK_ASSERT(sc);
505153770Syongari
50698849Sken	/*
50798849Sken	 * At the moment, we don't handle non-aligned cases, we just bail.
50898849Sken	 * If this proves to be a problem, it will be fixed.
50998849Sken	 */
51098849Sken	if ((readdata == 0)
51198849Sken	 && (tigon_addr & 0x3)) {
512162321Sglebius		device_printf(sc->ti_dev, "%s: tigon address %#x isn't "
513162321Sglebius		    "word-aligned\n", __func__, tigon_addr);
514162321Sglebius		device_printf(sc->ti_dev, "%s: unaligned writes aren't "
515162321Sglebius		    "yet supported\n", __func__);
516131654Sbms		return (EINVAL);
51798849Sken	}
51898849Sken
51998849Sken	segptr = tigon_addr & ~0x3;
52098849Sken	segresid = tigon_addr - segptr;
52198849Sken
52298849Sken	/*
52398849Sken	 * This is the non-aligned amount left over that we'll need to
52498849Sken	 * copy.
52598849Sken	 */
52698849Sken	resid = len & 0x3;
52798849Sken
52898849Sken	/* Add in the left over amount at the front of the buffer */
52998849Sken	resid += segresid;
53098849Sken
53198849Sken	cnt = len & ~0x3;
53298849Sken	/*
53398849Sken	 * If resid + segresid is >= 4, add multiples of 4 to the count and
53498849Sken	 * decrease the residual by that much.
53598849Sken	 */
53698849Sken	cnt += resid & ~0x3;
53798849Sken	resid -= resid & ~0x3;
53898849Sken
53998849Sken	ptr = buf;
54098849Sken
54198849Sken	first_pass = 1;
54298849Sken
54398849Sken	/*
54498849Sken	 * Save the old window base value.
54598849Sken	 */
54698849Sken	origwin = CSR_READ_4(sc, TI_WINBASE);
54798849Sken
548131654Sbms	while (cnt) {
54998849Sken		bus_size_t ti_offset;
55098849Sken
55198849Sken		if (cnt < TI_WINLEN)
55298849Sken			segsize = cnt;
55398849Sken		else
55498849Sken			segsize = TI_WINLEN - (segptr % TI_WINLEN);
55598849Sken		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
55698849Sken
55798849Sken		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN -1));
55898849Sken
55998849Sken		if (readdata) {
56098849Sken
56198849Sken			bus_space_read_region_4(sc->ti_btag,
56298849Sken						sc->ti_bhandle, ti_offset,
563227089Syongari						(uint32_t *)tmparray,
56498849Sken						segsize >> 2);
56598849Sken			if (useraddr) {
56698849Sken				/*
56798849Sken				 * Yeah, this is a little on the kludgy
56898849Sken				 * side, but at least this code is only
56998849Sken				 * used for debugging.
57098849Sken				 */
57198849Sken				ti_bcopy_swap(tmparray, tmparray2, segsize,
57298849Sken					      TI_SWAP_NTOH);
57398849Sken
574153281Sscottl				TI_UNLOCK(sc);
57598849Sken				if (first_pass) {
57698849Sken					copyout(&tmparray2[segresid], ptr,
57798849Sken						segsize - segresid);
57898849Sken					first_pass = 0;
57998849Sken				} else
58098849Sken					copyout(tmparray2, ptr, segsize);
581153281Sscottl				TI_LOCK(sc);
58298849Sken			} else {
58398849Sken				if (first_pass) {
58498849Sken
58598849Sken					ti_bcopy_swap(tmparray, tmparray2,
58698849Sken						      segsize, TI_SWAP_NTOH);
587153281Sscottl					TI_UNLOCK(sc);
58898849Sken					bcopy(&tmparray2[segresid], ptr,
58998849Sken					      segsize - segresid);
590153281Sscottl					TI_LOCK(sc);
59198849Sken					first_pass = 0;
59298849Sken				} else
59398849Sken					ti_bcopy_swap(tmparray, ptr, segsize,
59498849Sken						      TI_SWAP_NTOH);
59598849Sken			}
59698849Sken
59798849Sken		} else {
59898849Sken			if (useraddr) {
599153281Sscottl				TI_UNLOCK(sc);
60098849Sken				copyin(ptr, tmparray2, segsize);
601153281Sscottl				TI_LOCK(sc);
60298849Sken				ti_bcopy_swap(tmparray2, tmparray, segsize,
60398849Sken					      TI_SWAP_HTON);
60498849Sken			} else
60598849Sken				ti_bcopy_swap(ptr, tmparray, segsize,
60698849Sken					      TI_SWAP_HTON);
60798849Sken
60898849Sken			bus_space_write_region_4(sc->ti_btag,
60998849Sken						 sc->ti_bhandle, ti_offset,
610227089Syongari						 (uint32_t *)tmparray,
61198849Sken						 segsize >> 2);
61298849Sken		}
61398849Sken		segptr += segsize;
61498849Sken		ptr += segsize;
61598849Sken		cnt -= segsize;
61698849Sken	}
61798849Sken
61898849Sken	/*
61998849Sken	 * Handle leftover, non-word-aligned bytes.
62098849Sken	 */
62198849Sken	if (resid != 0) {
622227089Syongari		uint32_t	tmpval, tmpval2;
62398849Sken		bus_size_t	ti_offset;
62498849Sken
62598849Sken		/*
62698849Sken		 * Set the segment pointer.
62798849Sken		 */
62898849Sken		CSR_WRITE_4(sc, TI_WINBASE, (segptr & ~(TI_WINLEN - 1)));
62998849Sken
63098849Sken		ti_offset = TI_WINDOW + (segptr & (TI_WINLEN - 1));
63198849Sken
63298849Sken		/*
63398849Sken		 * First, grab whatever is in our source/destination.
63498849Sken		 * We'll obviously need this for reads, but also for
63598849Sken		 * writes, since we'll be doing read/modify/write.
63698849Sken		 */
63798849Sken		bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
63898849Sken					ti_offset, &tmpval, 1);
63998849Sken
64098849Sken		/*
64198849Sken		 * Next, translate this from little-endian to big-endian
64298849Sken		 * (at least on i386 boxes).
64398849Sken		 */
64498849Sken		tmpval2 = ntohl(tmpval);
64598849Sken
64698849Sken		if (readdata) {
64798849Sken			/*
64898849Sken			 * If we're reading, just copy the leftover number
64998849Sken			 * of bytes from the host byte order buffer to
65098849Sken			 * the user's buffer.
65198849Sken			 */
652153281Sscottl			if (useraddr) {
653153281Sscottl				TI_UNLOCK(sc);
65498849Sken				copyout(&tmpval2, ptr, resid);
655153281Sscottl				TI_LOCK(sc);
656153281Sscottl			} else
65798849Sken				bcopy(&tmpval2, ptr, resid);
65898849Sken		} else {
65998849Sken			/*
66098849Sken			 * If we're writing, first copy the bytes to be
66198849Sken			 * written into the network byte order buffer,
66298849Sken			 * leaving the rest of the buffer with whatever was
66398849Sken			 * originally in there.  Then, swap the bytes
66498849Sken			 * around into host order and write them out.
66598849Sken			 *
66698849Sken			 * XXX KDM the read side of this has been verified
66798849Sken			 * to work, but the write side of it has not been
66898849Sken			 * verified.  So user beware.
66998849Sken			 */
670153281Sscottl			if (useraddr) {
671153281Sscottl				TI_UNLOCK(sc);
67298849Sken				copyin(ptr, &tmpval2, resid);
673153281Sscottl				TI_LOCK(sc);
674153281Sscottl			} else
67598849Sken				bcopy(ptr, &tmpval2, resid);
67698849Sken
67798849Sken			tmpval = htonl(tmpval2);
67898849Sken
67998849Sken			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
68098849Sken						 ti_offset, &tmpval, 1);
68198849Sken		}
68298849Sken	}
68398849Sken
68498849Sken	CSR_WRITE_4(sc, TI_WINBASE, origwin);
68598849Sken
686131654Sbms	return (0);
68798849Sken}
68898849Sken
68998849Skenstatic int
690227089Syongariti_copy_scratch(struct ti_softc *sc, uint32_t tigon_addr, uint32_t len,
691227086Syongari    caddr_t buf, int useraddr, int readdata, int cpu)
69298849Sken{
693227089Syongari	uint32_t segptr;
694227087Syongari	int cnt;
695227089Syongari	uint32_t tmpval, tmpval2;
696227087Syongari	caddr_t ptr;
69798849Sken
698153770Syongari	TI_LOCK_ASSERT(sc);
699153770Syongari
70098849Sken	/*
70198849Sken	 * At the moment, we don't handle non-aligned cases, we just bail.
70298849Sken	 * If this proves to be a problem, it will be fixed.
70398849Sken	 */
70498849Sken	if (tigon_addr & 0x3) {
705162321Sglebius		device_printf(sc->ti_dev, "%s: tigon address %#x "
706162321Sglebius		    "isn't word-aligned\n", __func__, tigon_addr);
707131654Sbms		return (EINVAL);
70898849Sken	}
70998849Sken
71098849Sken	if (len & 0x3) {
711162321Sglebius		device_printf(sc->ti_dev, "%s: transfer length %d "
712162321Sglebius		    "isn't word-aligned\n", __func__, len);
713131654Sbms		return (EINVAL);
71498849Sken	}
71598849Sken
71698849Sken	segptr = tigon_addr;
71798849Sken	cnt = len;
71898849Sken	ptr = buf;
71998849Sken
72098849Sken	while (cnt) {
72198849Sken		CSR_WRITE_4(sc, CPU_REG(TI_SRAM_ADDR, cpu), segptr);
72298849Sken
72398849Sken		if (readdata) {
72498849Sken			tmpval2 = CSR_READ_4(sc, CPU_REG(TI_SRAM_DATA, cpu));
72598849Sken
72698849Sken			tmpval = ntohl(tmpval2);
72798849Sken
72898849Sken			/*
72998849Sken			 * Note:  I've used this debugging interface
73098849Sken			 * extensively with Alteon's 12.3.15 firmware,
73198849Sken			 * compiled with GCC 2.7.2.1 and binutils 2.9.1.
73298849Sken			 *
73398849Sken			 * When you compile the firmware without
73498849Sken			 * optimization, which is necessary sometimes in
73598849Sken			 * order to properly step through it, you sometimes
736131652Sbms			 * read out a bogus value of 0xc0017c instead of
73798849Sken			 * whatever was supposed to be in that scratchpad
73898849Sken			 * location.  That value is on the stack somewhere,
73998849Sken			 * but I've never been able to figure out what was
74098849Sken			 * causing the problem.
74198849Sken			 *
74298849Sken			 * The address seems to pop up in random places,
74398849Sken			 * often not in the same place on two subsequent
74498849Sken			 * reads.
74598849Sken			 *
74698849Sken			 * In any case, the underlying data doesn't seem
74798849Sken			 * to be affected, just the value read out.
74898849Sken			 *
74998849Sken			 * KDM, 3/7/2000
75098849Sken			 */
75198849Sken
75298849Sken			if (tmpval2 == 0xc0017c)
753162321Sglebius				device_printf(sc->ti_dev, "found 0xc0017c at "
754162321Sglebius				    "%#x (tmpval2)\n", segptr);
75598849Sken
75698849Sken			if (tmpval == 0xc0017c)
757162321Sglebius				device_printf(sc->ti_dev, "found 0xc0017c at "
758162321Sglebius				    "%#x (tmpval)\n", segptr);
75998849Sken
76098849Sken			if (useraddr)
76198849Sken				copyout(&tmpval, ptr, 4);
76298849Sken			else
76398849Sken				bcopy(&tmpval, ptr, 4);
76498849Sken		} else {
76598849Sken			if (useraddr)
76698849Sken				copyin(ptr, &tmpval2, 4);
76798849Sken			else
76898849Sken				bcopy(ptr, &tmpval2, 4);
76998849Sken
77098849Sken			tmpval = htonl(tmpval2);
77198849Sken
77298849Sken			CSR_WRITE_4(sc, CPU_REG(TI_SRAM_DATA, cpu), tmpval);
77398849Sken		}
77498849Sken
77598849Sken		cnt -= 4;
77698849Sken		segptr += 4;
77798849Sken		ptr += 4;
77898849Sken	}
77998849Sken
780131654Sbms	return (0);
78198849Sken}
78298849Sken
78398849Skenstatic int
784227086Syongariti_bcopy_swap(const void *src, void *dst, size_t len, ti_swap_type swap_type)
78598849Sken{
786227089Syongari	const uint8_t *tmpsrc;
787227089Syongari	uint8_t *tmpdst;
78898849Sken	size_t tmplen;
78998849Sken
79098849Sken	if (len & 0x3) {
791106627Sjhb		printf("ti_bcopy_swap: length %zd isn't 32-bit aligned\n",
79298849Sken		       len);
793131654Sbms		return (-1);
79498849Sken	}
79598849Sken
79698849Sken	tmpsrc = src;
79798849Sken	tmpdst = dst;
79898849Sken	tmplen = len;
79998849Sken
80098849Sken	while (tmplen) {
80198849Sken		if (swap_type == TI_SWAP_NTOH)
802227089Syongari			*(uint32_t *)tmpdst =
803227089Syongari				ntohl(*(const uint32_t *)tmpsrc);
80498849Sken		else
805227089Syongari			*(uint32_t *)tmpdst =
806227089Syongari				htonl(*(const uint32_t *)tmpsrc);
80798849Sken
80898849Sken		tmpsrc += 4;
80998849Sken		tmpdst += 4;
81098849Sken		tmplen -= 4;
81198849Sken	}
81298849Sken
813131654Sbms	return (0);
81498849Sken}
81598849Sken
81645386Swpaul/*
81745386Swpaul * Load firmware image into the NIC. Check that the firmware revision
81845386Swpaul * is acceptable and see if we want the firmware for the Tigon 1 or
81945386Swpaul * Tigon 2.
82045386Swpaul */
821102336Salfredstatic void
822227086Syongariti_loadfw(struct ti_softc *sc)
82345386Swpaul{
824153770Syongari
825153770Syongari	TI_LOCK_ASSERT(sc);
826153770Syongari
827131654Sbms	switch (sc->ti_hwrev) {
82845386Swpaul	case TI_HWREV_TIGON:
82945386Swpaul		if (tigonFwReleaseMajor != TI_FIRMWARE_MAJOR ||
83045386Swpaul		    tigonFwReleaseMinor != TI_FIRMWARE_MINOR ||
83145386Swpaul		    tigonFwReleaseFix != TI_FIRMWARE_FIX) {
832162321Sglebius			device_printf(sc->ti_dev, "firmware revision mismatch; "
833150719Sjhb			    "want %d.%d.%d, got %d.%d.%d\n",
83445386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
83545386Swpaul			    TI_FIRMWARE_FIX, tigonFwReleaseMajor,
83645386Swpaul			    tigonFwReleaseMinor, tigonFwReleaseFix);
83745386Swpaul			return;
83845386Swpaul		}
839153770Syongari		ti_mem_write(sc, tigonFwTextAddr, tigonFwTextLen, tigonFwText);
840153770Syongari		ti_mem_write(sc, tigonFwDataAddr, tigonFwDataLen, tigonFwData);
841153770Syongari		ti_mem_write(sc, tigonFwRodataAddr, tigonFwRodataLen,
842153770Syongari		    tigonFwRodata);
843153770Syongari		ti_mem_zero(sc, tigonFwBssAddr, tigonFwBssLen);
844153770Syongari		ti_mem_zero(sc, tigonFwSbssAddr, tigonFwSbssLen);
84545386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigonFwStartAddr);
84645386Swpaul		break;
84745386Swpaul	case TI_HWREV_TIGON_II:
84845386Swpaul		if (tigon2FwReleaseMajor != TI_FIRMWARE_MAJOR ||
84945386Swpaul		    tigon2FwReleaseMinor != TI_FIRMWARE_MINOR ||
85045386Swpaul		    tigon2FwReleaseFix != TI_FIRMWARE_FIX) {
851162321Sglebius			device_printf(sc->ti_dev, "firmware revision mismatch; "
852150719Sjhb			    "want %d.%d.%d, got %d.%d.%d\n",
85345386Swpaul			    TI_FIRMWARE_MAJOR, TI_FIRMWARE_MINOR,
85445386Swpaul			    TI_FIRMWARE_FIX, tigon2FwReleaseMajor,
85545386Swpaul			    tigon2FwReleaseMinor, tigon2FwReleaseFix);
85645386Swpaul			return;
85745386Swpaul		}
858153770Syongari		ti_mem_write(sc, tigon2FwTextAddr, tigon2FwTextLen,
859153770Syongari		    tigon2FwText);
860153770Syongari		ti_mem_write(sc, tigon2FwDataAddr, tigon2FwDataLen,
861153770Syongari		    tigon2FwData);
862153770Syongari		ti_mem_write(sc, tigon2FwRodataAddr, tigon2FwRodataLen,
863153770Syongari		    tigon2FwRodata);
864153770Syongari		ti_mem_zero(sc, tigon2FwBssAddr, tigon2FwBssLen);
865153770Syongari		ti_mem_zero(sc, tigon2FwSbssAddr, tigon2FwSbssLen);
86645386Swpaul		CSR_WRITE_4(sc, TI_CPU_PROGRAM_COUNTER, tigon2FwStartAddr);
86745386Swpaul		break;
86845386Swpaul	default:
869162321Sglebius		device_printf(sc->ti_dev,
870150719Sjhb		    "can't load firmware: unknown hardware rev\n");
87145386Swpaul		break;
87245386Swpaul	}
87345386Swpaul}
87445386Swpaul
87545386Swpaul/*
87645386Swpaul * Send the NIC a command via the command ring.
87745386Swpaul */
878102336Salfredstatic void
879227086Syongariti_cmd(struct ti_softc *sc, struct ti_cmd_desc *cmd)
88045386Swpaul{
881227087Syongari	int index;
88245386Swpaul
88345386Swpaul	index = sc->ti_cmd_saved_prodidx;
884227089Syongari	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(uint32_t *)(cmd));
88545386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
88645386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
88745386Swpaul	sc->ti_cmd_saved_prodidx = index;
88845386Swpaul}
88945386Swpaul
89045386Swpaul/*
89145386Swpaul * Send the NIC an extended command. The 'len' parameter specifies the
89245386Swpaul * number of command slots to include after the initial command.
89345386Swpaul */
894102336Salfredstatic void
895227086Syongariti_cmd_ext(struct ti_softc *sc, struct ti_cmd_desc *cmd, caddr_t arg, int len)
89645386Swpaul{
897227087Syongari	int index;
898227087Syongari	int i;
89945386Swpaul
90045386Swpaul	index = sc->ti_cmd_saved_prodidx;
901227089Syongari	CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4), *(uint32_t *)(cmd));
90245386Swpaul	TI_INC(index, TI_CMD_RING_CNT);
90345386Swpaul	for (i = 0; i < len; i++) {
90445386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (index * 4),
905227089Syongari		    *(uint32_t *)(&arg[i * 4]));
90645386Swpaul		TI_INC(index, TI_CMD_RING_CNT);
90745386Swpaul	}
90845386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, index);
90945386Swpaul	sc->ti_cmd_saved_prodidx = index;
91045386Swpaul}
91145386Swpaul
91245386Swpaul/*
91345386Swpaul * Handle events that have triggered interrupts.
91445386Swpaul */
915102336Salfredstatic void
916227086Syongariti_handle_events(struct ti_softc *sc)
91745386Swpaul{
918227087Syongari	struct ti_event_desc *e;
91945386Swpaul
92045386Swpaul	if (sc->ti_rdata->ti_event_ring == NULL)
92145386Swpaul		return;
92245386Swpaul
92345386Swpaul	while (sc->ti_ev_saved_considx != sc->ti_ev_prodidx.ti_idx) {
92445386Swpaul		e = &sc->ti_rdata->ti_event_ring[sc->ti_ev_saved_considx];
925153770Syongari		switch (TI_EVENT_EVENT(e)) {
92645386Swpaul		case TI_EV_LINKSTAT_CHANGED:
927153770Syongari			sc->ti_linkstat = TI_EVENT_CODE(e);
928227092Syongari			if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
929227092Syongari				if_link_state_change(sc->ti_ifp, LINK_STATE_UP);
930227092Syongari				sc->ti_ifp->if_baudrate = IF_Mbps(100);
931227092Syongari				if (bootverbose)
932227092Syongari					device_printf(sc->ti_dev,
933227092Syongari					    "10/100 link up\n");
934227092Syongari			} else if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
935227092Syongari				if_link_state_change(sc->ti_ifp, LINK_STATE_UP);
936227092Syongari				sc->ti_ifp->if_baudrate = IF_Gbps(1UL);
937227092Syongari				if (bootverbose)
938227092Syongari					device_printf(sc->ti_dev,
939227092Syongari					    "gigabit link up\n");
940227092Syongari			} else if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN) {
941227092Syongari				if_link_state_change(sc->ti_ifp,
942227092Syongari				    LINK_STATE_DOWN);
943227092Syongari				sc->ti_ifp->if_baudrate = 0;
944227092Syongari				if (bootverbose)
945227092Syongari					device_printf(sc->ti_dev,
946227092Syongari					    "link down\n");
947227092Syongari			}
94845386Swpaul			break;
94945386Swpaul		case TI_EV_ERROR:
950153770Syongari			if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_INVAL_CMD)
951162321Sglebius				device_printf(sc->ti_dev, "invalid command\n");
952153770Syongari			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_UNIMP_CMD)
953162321Sglebius				device_printf(sc->ti_dev, "unknown command\n");
954153770Syongari			else if (TI_EVENT_CODE(e) == TI_EV_CODE_ERR_BADCFG)
955162321Sglebius				device_printf(sc->ti_dev, "bad config data\n");
95645386Swpaul			break;
95745386Swpaul		case TI_EV_FIRMWARE_UP:
95845386Swpaul			ti_init2(sc);
95945386Swpaul			break;
96045386Swpaul		case TI_EV_STATS_UPDATED:
96145386Swpaul			ti_stats_update(sc);
96245386Swpaul			break;
96345386Swpaul		case TI_EV_RESET_JUMBO_RING:
96445386Swpaul		case TI_EV_MCAST_UPDATED:
96545386Swpaul			/* Who cares. */
96645386Swpaul			break;
96745386Swpaul		default:
968162321Sglebius			device_printf(sc->ti_dev, "unknown event: %d\n",
969153770Syongari			    TI_EVENT_EVENT(e));
97045386Swpaul			break;
97145386Swpaul		}
97245386Swpaul		/* Advance the consumer index. */
97345386Swpaul		TI_INC(sc->ti_ev_saved_considx, TI_EVENT_RING_CNT);
97445386Swpaul		CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, sc->ti_ev_saved_considx);
97545386Swpaul	}
97645386Swpaul}
97745386Swpaul
978153396Sscottlstatic int
979153396Sscottlti_alloc_dmamaps(struct ti_softc *sc)
980153396Sscottl{
981153396Sscottl	int i;
982153396Sscottl
983153396Sscottl	for (i = 0; i < TI_TX_RING_CNT; i++) {
984153982Syongari		sc->ti_cdata.ti_txdesc[i].tx_m = NULL;
985153982Syongari		sc->ti_cdata.ti_txdesc[i].tx_dmamap = 0;
986153396Sscottl		if (bus_dmamap_create(sc->ti_mbuftx_dmat, 0,
987153982Syongari				      &sc->ti_cdata.ti_txdesc[i].tx_dmamap))
988153396Sscottl			return (ENOBUFS);
989153396Sscottl	}
990153396Sscottl	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
991153396Sscottl		if (bus_dmamap_create(sc->ti_mbufrx_dmat, 0,
992153396Sscottl				      &sc->ti_cdata.ti_rx_std_maps[i]))
993153396Sscottl			return (ENOBUFS);
994153396Sscottl	}
995153396Sscottl
996153396Sscottl	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
997153396Sscottl		if (bus_dmamap_create(sc->ti_jumbo_dmat, 0,
998153396Sscottl				      &sc->ti_cdata.ti_rx_jumbo_maps[i]))
999153396Sscottl			return (ENOBUFS);
1000153396Sscottl	}
1001153396Sscottl	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
1002153396Sscottl		if (bus_dmamap_create(sc->ti_mbufrx_dmat, 0,
1003153396Sscottl				      &sc->ti_cdata.ti_rx_mini_maps[i]))
1004153396Sscottl			return (ENOBUFS);
1005153396Sscottl	}
1006153396Sscottl
1007153396Sscottl	return (0);
1008153396Sscottl}
1009153396Sscottl
1010153396Sscottlstatic void
1011153396Sscottlti_free_dmamaps(struct ti_softc *sc)
1012153396Sscottl{
1013153396Sscottl	int i;
1014153396Sscottl
1015153770Syongari	if (sc->ti_mbuftx_dmat)
1016153770Syongari		for (i = 0; i < TI_TX_RING_CNT; i++)
1017153982Syongari			if (sc->ti_cdata.ti_txdesc[i].tx_dmamap) {
1018153770Syongari				bus_dmamap_destroy(sc->ti_mbuftx_dmat,
1019153982Syongari				    sc->ti_cdata.ti_txdesc[i].tx_dmamap);
1020153982Syongari				sc->ti_cdata.ti_txdesc[i].tx_dmamap = 0;
1021153770Syongari			}
1022153396Sscottl
1023153770Syongari	if (sc->ti_mbufrx_dmat)
1024153770Syongari		for (i = 0; i < TI_STD_RX_RING_CNT; i++)
1025153770Syongari			if (sc->ti_cdata.ti_rx_std_maps[i]) {
1026153770Syongari				bus_dmamap_destroy(sc->ti_mbufrx_dmat,
1027153770Syongari				    sc->ti_cdata.ti_rx_std_maps[i]);
1028153770Syongari				sc->ti_cdata.ti_rx_std_maps[i] = 0;
1029153770Syongari			}
1030153396Sscottl
1031153770Syongari	if (sc->ti_jumbo_dmat)
1032153770Syongari		for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++)
1033153770Syongari			if (sc->ti_cdata.ti_rx_jumbo_maps[i]) {
1034153770Syongari				bus_dmamap_destroy(sc->ti_jumbo_dmat,
1035153770Syongari				    sc->ti_cdata.ti_rx_jumbo_maps[i]);
1036153770Syongari				sc->ti_cdata.ti_rx_jumbo_maps[i] = 0;
1037153770Syongari			}
1038153770Syongari	if (sc->ti_mbufrx_dmat)
1039153770Syongari		for (i = 0; i < TI_MINI_RX_RING_CNT; i++)
1040153770Syongari			if (sc->ti_cdata.ti_rx_mini_maps[i]) {
1041153770Syongari				bus_dmamap_destroy(sc->ti_mbufrx_dmat,
1042153770Syongari				    sc->ti_cdata.ti_rx_mini_maps[i]);
1043153770Syongari				sc->ti_cdata.ti_rx_mini_maps[i] = 0;
1044153770Syongari			}
1045153396Sscottl}
1046153396Sscottl
104798849Sken#ifdef TI_PRIVATE_JUMBOS
104898849Sken
104945386Swpaul/*
105045386Swpaul * Memory management for the jumbo receive ring is a pain in the
105145386Swpaul * butt. We need to allocate at least 9018 bytes of space per frame,
105245386Swpaul * _and_ it has to be contiguous (unless you use the extended
105345386Swpaul * jumbo descriptor format). Using malloc() all the time won't
105445386Swpaul * work: malloc() allocates memory in powers of two, which means we
105545386Swpaul * would end up wasting a considerable amount of space by allocating
105645386Swpaul * 9K chunks. We don't have a jumbo mbuf cluster pool. Thus, we have
105745386Swpaul * to do our own memory management.
105845386Swpaul *
105945386Swpaul * The driver needs to allocate a contiguous chunk of memory at boot
106045386Swpaul * time. We then chop this up ourselves into 9K pieces and use them
106145386Swpaul * as external mbuf storage.
106245386Swpaul *
106345386Swpaul * One issue here is how much memory to allocate. The jumbo ring has
106445386Swpaul * 256 slots in it, but at 9K per slot than can consume over 2MB of
106545386Swpaul * RAM. This is a bit much, especially considering we also need
106645386Swpaul * RAM for the standard ring and mini ring (on the Tigon 2). To
106745386Swpaul * save space, we only actually allocate enough memory for 64 slots
106845386Swpaul * by default, which works out to between 500 and 600K. This can
106945386Swpaul * be tuned by changing a #define in if_tireg.h.
107045386Swpaul */
107145386Swpaul
1072102336Salfredstatic int
1073227086Syongariti_alloc_jumbo_mem(struct ti_softc *sc)
107445386Swpaul{
1075227087Syongari	struct ti_jpool_entry *entry;
1076227087Syongari	caddr_t ptr;
1077227087Syongari	int i;
107845386Swpaul
1079153396Sscottl	/*
1080153396Sscottl	 * Grab a big chunk o' storage.  Since we are chopping this pool up
1081153396Sscottl	 * into ~9k chunks, there doesn't appear to be a need to use page
1082153396Sscottl	 * alignment.
1083153396Sscottl	 */
1084153288Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
1085153396Sscottl				1, 0,			/* algnmnt, boundary */
1086153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
1087153288Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
1088153288Sscottl				NULL, NULL,		/* filter, filterarg */
1089153288Sscottl				TI_JMEM,		/* maxsize */
1090153288Sscottl				1,			/* nsegments */
1091153288Sscottl				TI_JMEM,		/* maxsegsize */
1092153288Sscottl				0,			/* flags */
1093153288Sscottl				NULL, NULL,		/* lockfunc, lockarg */
1094153288Sscottl				&sc->ti_jumbo_dmat) != 0) {
1095153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo dmat\n");
1096153288Sscottl		return (ENOBUFS);
1097153288Sscottl	}
109845386Swpaul
1099153288Sscottl	if (bus_dmamem_alloc(sc->ti_jumbo_dmat,
1100153288Sscottl			     (void**)&sc->ti_cdata.ti_jumbo_buf,
1101219547Smarius			     BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
1102219547Smarius			     &sc->ti_jumbo_dmamap) != 0) {
1103153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo memory\n");
1104131654Sbms		return (ENOBUFS);
110545386Swpaul	}
110645386Swpaul
110745386Swpaul	SLIST_INIT(&sc->ti_jfree_listhead);
110845386Swpaul	SLIST_INIT(&sc->ti_jinuse_listhead);
110945386Swpaul
111045386Swpaul	/*
111145386Swpaul	 * Now divide it up into 9K pieces and save the addresses
111267405Sbmilekic	 * in an array.
111345386Swpaul	 */
111445386Swpaul	ptr = sc->ti_cdata.ti_jumbo_buf;
111545386Swpaul	for (i = 0; i < TI_JSLOTS; i++) {
111667405Sbmilekic		sc->ti_cdata.ti_jslots[i] = ptr;
111767405Sbmilekic		ptr += TI_JLEN;
1118131652Sbms		entry = malloc(sizeof(struct ti_jpool_entry),
111945386Swpaul			       M_DEVBUF, M_NOWAIT);
112045386Swpaul		if (entry == NULL) {
1121153396Sscottl			device_printf(sc->ti_dev, "no memory for jumbo "
1122150719Sjhb			    "buffer queue!\n");
1123131654Sbms			return (ENOBUFS);
112445386Swpaul		}
112545386Swpaul		entry->slot = i;
112645386Swpaul		SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
112745386Swpaul	}
112845386Swpaul
1129131654Sbms	return (0);
113045386Swpaul}
113145386Swpaul
113245386Swpaul/*
113345386Swpaul * Allocate a jumbo buffer.
113445386Swpaul */
1135227086Syongaristatic void *ti_jalloc(struct ti_softc *sc)
113645386Swpaul{
1137227087Syongari	struct ti_jpool_entry *entry;
1138131652Sbms
113945386Swpaul	entry = SLIST_FIRST(&sc->ti_jfree_listhead);
1140131652Sbms
114145386Swpaul	if (entry == NULL) {
1142162321Sglebius		device_printf(sc->ti_dev, "no free jumbo buffers\n");
1143131654Sbms		return (NULL);
114445386Swpaul	}
114545386Swpaul
114645386Swpaul	SLIST_REMOVE_HEAD(&sc->ti_jfree_listhead, jpool_entries);
114745386Swpaul	SLIST_INSERT_HEAD(&sc->ti_jinuse_listhead, entry, jpool_entries);
1148131654Sbms	return (sc->ti_cdata.ti_jslots[entry->slot]);
114945386Swpaul}
115045386Swpaul
115145386Swpaul/*
115245386Swpaul * Release a jumbo buffer.
115345386Swpaul */
1154102336Salfredstatic void
1155227086Syongariti_jfree(void *buf, void *args)
115645386Swpaul{
1157227087Syongari	struct ti_softc *sc;
1158227087Syongari	int i;
1159227087Syongari	struct ti_jpool_entry *entry;
116045386Swpaul
116145386Swpaul	/* Extract the softc struct pointer. */
116267405Sbmilekic	sc = (struct ti_softc *)args;
116345386Swpaul
116445386Swpaul	if (sc == NULL)
116567405Sbmilekic		panic("ti_jfree: didn't get softc pointer!");
116645386Swpaul
116745386Swpaul	/* calculate the slot this buffer belongs to */
116867405Sbmilekic	i = ((vm_offset_t)buf
116945386Swpaul	     - (vm_offset_t)sc->ti_cdata.ti_jumbo_buf) / TI_JLEN;
117045386Swpaul
117145386Swpaul	if ((i < 0) || (i >= TI_JSLOTS))
117245386Swpaul		panic("ti_jfree: asked to free buffer that we don't manage!");
117345386Swpaul
117464837Sdwmalone	entry = SLIST_FIRST(&sc->ti_jinuse_listhead);
117564837Sdwmalone	if (entry == NULL)
117664837Sdwmalone		panic("ti_jfree: buffer not in use!");
117764837Sdwmalone	entry->slot = i;
117864837Sdwmalone	SLIST_REMOVE_HEAD(&sc->ti_jinuse_listhead, jpool_entries);
117964837Sdwmalone	SLIST_INSERT_HEAD(&sc->ti_jfree_listhead, entry, jpool_entries);
118045386Swpaul}
118145386Swpaul
1182153396Sscottl#else
1183153396Sscottl
1184153396Sscottlstatic int
1185227086Syongariti_alloc_jumbo_mem(struct ti_softc *sc)
1186153396Sscottl{
1187153396Sscottl
1188153396Sscottl	/*
1189153396Sscottl	 * The VM system will take care of providing aligned pages.  Alignment
1190153396Sscottl	 * is set to 1 here so that busdma resources won't be wasted.
1191153396Sscottl	 */
1192153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
1193153396Sscottl				1, 0,			/* algnmnt, boundary */
1194153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
1195153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
1196153396Sscottl				NULL, NULL,		/* filter, filterarg */
1197153396Sscottl				PAGE_SIZE * 4 /*XXX*/,	/* maxsize */
1198153396Sscottl				4,			/* nsegments */
1199153396Sscottl				PAGE_SIZE,		/* maxsegsize */
1200153396Sscottl				0,			/* flags */
1201153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
1202153396Sscottl				&sc->ti_jumbo_dmat) != 0) {
1203153396Sscottl		device_printf(sc->ti_dev, "Failed to allocate jumbo dmat\n");
1204153396Sscottl		return (ENOBUFS);
1205153396Sscottl	}
1206153396Sscottl
1207153396Sscottl	return (0);
1208153396Sscottl}
1209153396Sscottl
121098849Sken#endif /* TI_PRIVATE_JUMBOS */
121145386Swpaul
121245386Swpaul/*
121345386Swpaul * Intialize a standard receive ring descriptor.
121445386Swpaul */
1215102336Salfredstatic int
1216227086Syongariti_newbuf_std(struct ti_softc *sc, int i, struct mbuf *m)
121745386Swpaul{
1218227087Syongari	bus_dmamap_t map;
1219227087Syongari	bus_dma_segment_t segs;
1220227087Syongari	struct mbuf *m_new = NULL;
1221227087Syongari	struct ti_rx_desc *r;
1222227087Syongari	int nsegs;
122345386Swpaul
1224153396Sscottl	nsegs = 0;
122549036Swpaul	if (m == NULL) {
1226111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
122787846Sluigi		if (m_new == NULL)
1228131654Sbms			return (ENOBUFS);
122945386Swpaul
1230111119Simp		MCLGET(m_new, M_DONTWAIT);
123145386Swpaul		if (!(m_new->m_flags & M_EXT)) {
123245386Swpaul			m_freem(m_new);
1233131654Sbms			return (ENOBUFS);
123445386Swpaul		}
123549036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
123649036Swpaul	} else {
123749036Swpaul		m_new = m;
123849036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
123949036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
124045386Swpaul	}
124145386Swpaul
124248597Swpaul	m_adj(m_new, ETHER_ALIGN);
124345386Swpaul	sc->ti_cdata.ti_rx_std_chain[i] = m_new;
124445386Swpaul	r = &sc->ti_rdata->ti_rx_std_ring[i];
1245153396Sscottl	map = sc->ti_cdata.ti_rx_std_maps[i];
1246153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_mbufrx_dmat, map, m_new, &segs,
1247153396Sscottl				    &nsegs, 0))
1248153396Sscottl		return (ENOBUFS);
1249153396Sscottl	if (nsegs != 1)
1250153396Sscottl		return (ENOBUFS);
1251153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1252153396Sscottl	r->ti_len = segs.ds_len;
125345386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
125445386Swpaul	r->ti_flags = 0;
1255227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
125658698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
125745386Swpaul	r->ti_idx = i;
125845386Swpaul
1259153396Sscottl	bus_dmamap_sync(sc->ti_mbufrx_dmat, map, BUS_DMASYNC_PREREAD);
1260131654Sbms	return (0);
126145386Swpaul}
126245386Swpaul
126345386Swpaul/*
126445386Swpaul * Intialize a mini receive ring descriptor. This only applies to
126545386Swpaul * the Tigon 2.
126645386Swpaul */
1267102336Salfredstatic int
1268227086Syongariti_newbuf_mini(struct ti_softc *sc, int i, struct mbuf *m)
126945386Swpaul{
1270227087Syongari	bus_dma_segment_t segs;
1271227087Syongari	bus_dmamap_t map;
1272227087Syongari	struct mbuf *m_new = NULL;
1273227087Syongari	struct ti_rx_desc *r;
1274227087Syongari	int nsegs;
127545386Swpaul
1276153396Sscottl	nsegs = 0;
127749036Swpaul	if (m == NULL) {
1278111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
127945386Swpaul		if (m_new == NULL) {
1280131654Sbms			return (ENOBUFS);
128145386Swpaul		}
128249036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
128349036Swpaul	} else {
128449036Swpaul		m_new = m;
128549036Swpaul		m_new->m_data = m_new->m_pktdat;
128649036Swpaul		m_new->m_len = m_new->m_pkthdr.len = MHLEN;
128745386Swpaul	}
128849036Swpaul
128948597Swpaul	m_adj(m_new, ETHER_ALIGN);
129045386Swpaul	r = &sc->ti_rdata->ti_rx_mini_ring[i];
129145386Swpaul	sc->ti_cdata.ti_rx_mini_chain[i] = m_new;
1292153396Sscottl	map = sc->ti_cdata.ti_rx_mini_maps[i];
1293153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_mbufrx_dmat, map, m_new, &segs,
1294153396Sscottl				    &nsegs, 0))
1295153396Sscottl		return (ENOBUFS);
1296153396Sscottl	if (nsegs != 1)
1297153396Sscottl		return (ENOBUFS);
1298153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1299153396Sscottl	r->ti_len = segs.ds_len;
130045386Swpaul	r->ti_type = TI_BDTYPE_RECV_BD;
130145386Swpaul	r->ti_flags = TI_BDFLAG_MINI_RING;
1302227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
130358698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
130445386Swpaul	r->ti_idx = i;
130545386Swpaul
1306153396Sscottl	bus_dmamap_sync(sc->ti_mbufrx_dmat, map, BUS_DMASYNC_PREREAD);
1307131654Sbms	return (0);
130845386Swpaul}
130945386Swpaul
131098849Sken#ifdef TI_PRIVATE_JUMBOS
131198849Sken
131245386Swpaul/*
131345386Swpaul * Initialize a jumbo receive ring descriptor. This allocates
131445386Swpaul * a jumbo buffer from the pool managed internally by the driver.
131545386Swpaul */
1316102336Salfredstatic int
1317227086Syongariti_newbuf_jumbo(struct ti_softc *sc, int i, struct mbuf *m)
131845386Swpaul{
1319227087Syongari	bus_dmamap_t map;
1320227087Syongari	struct mbuf *m_new = NULL;
1321227087Syongari	struct ti_rx_desc *r;
1322227087Syongari	int nsegs;
1323227087Syongari	bus_dma_segment_t segs;
132445386Swpaul
132549036Swpaul	if (m == NULL) {
1326227087Syongari		caddr_t *buf = NULL;
132745386Swpaul
132845386Swpaul		/* Allocate the mbuf. */
1329111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
133045386Swpaul		if (m_new == NULL) {
1331131654Sbms			return (ENOBUFS);
133245386Swpaul		}
133345386Swpaul
133445386Swpaul		/* Allocate the jumbo buffer */
133545386Swpaul		buf = ti_jalloc(sc);
133645386Swpaul		if (buf == NULL) {
133745386Swpaul			m_freem(m_new);
1338162321Sglebius			device_printf(sc->ti_dev, "jumbo allocation failed "
1339150719Sjhb			    "-- packet dropped!\n");
1340131654Sbms			return (ENOBUFS);
134145386Swpaul		}
134245386Swpaul
134345386Swpaul		/* Attach the buffer to the mbuf. */
134464837Sdwmalone		m_new->m_data = (void *) buf;
134564837Sdwmalone		m_new->m_len = m_new->m_pkthdr.len = TI_JUMBO_FRAMELEN;
1346227088Syongari		MEXTADD(m_new, buf, TI_JUMBO_FRAMELEN, ti_jfree, buf,
134768621Sbmilekic		    (struct ti_softc *)sc, 0, EXT_NET_DRV);
134849036Swpaul	} else {
134949036Swpaul		m_new = m;
135049036Swpaul		m_new->m_data = m_new->m_ext.ext_buf;
135149036Swpaul		m_new->m_ext.ext_size = TI_JUMBO_FRAMELEN;
135245386Swpaul	}
135345386Swpaul
135449780Swpaul	m_adj(m_new, ETHER_ALIGN);
135545386Swpaul	/* Set up the descriptor. */
135645386Swpaul	r = &sc->ti_rdata->ti_rx_jumbo_ring[i];
135745386Swpaul	sc->ti_cdata.ti_rx_jumbo_chain[i] = m_new;
1358153396Sscottl	map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1359153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_jumbo_dmat, map, m_new, &segs,
1360153396Sscottl				    &nsegs, 0))
1361153396Sscottl		return (ENOBUFS);
1362153396Sscottl	if (nsegs != 1)
1363153396Sscottl		return (ENOBUFS);
1364153396Sscottl	ti_hostaddr64(&r->ti_addr, segs.ds_addr);
1365153396Sscottl	r->ti_len = segs.ds_len;
136645386Swpaul	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
136745386Swpaul	r->ti_flags = TI_BDFLAG_JUMBO_RING;
1368227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
136958698Sjlemon		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM | TI_BDFLAG_IP_CKSUM;
137045386Swpaul	r->ti_idx = i;
137145386Swpaul
1372153396Sscottl	bus_dmamap_sync(sc->ti_jumbo_dmat, map, BUS_DMASYNC_PREREAD);
1373131654Sbms	return (0);
137445386Swpaul}
137545386Swpaul
137698849Sken#else
137798849Sken
137898849Sken#if (PAGE_SIZE == 4096)
137998849Sken#define NPAYLOAD 2
138098849Sken#else
138198849Sken#define NPAYLOAD 1
1382131652Sbms#endif
138398849Sken
138498849Sken#define TCP_HDR_LEN (52 + sizeof(struct ether_header))
138598849Sken#define UDP_HDR_LEN (28 + sizeof(struct ether_header))
138698849Sken#define NFS_HDR_LEN (UDP_HDR_LEN)
1387227087Syongaristatic int HDR_LEN = TCP_HDR_LEN;
138898849Sken
1389131655Sbms/*
1390131655Sbms * Initialize a jumbo receive ring descriptor. This allocates
1391131655Sbms * a jumbo buffer from the pool managed internally by the driver.
1392131655Sbms */
139398849Skenstatic int
1394227086Syongariti_newbuf_jumbo(struct ti_softc *sc, int idx, struct mbuf *m_old)
139598849Sken{
1396227087Syongari	bus_dmamap_t map;
1397227087Syongari	struct mbuf *cur, *m_new = NULL;
1398227087Syongari	struct mbuf *m[3] = {NULL, NULL, NULL};
1399227087Syongari	struct ti_rx_desc_ext *r;
1400227087Syongari	vm_page_t frame;
1401227087Syongari	static int color;
1402227087Syongari	/* 1 extra buf to make nobufs easy*/
1403227087Syongari	struct sf_buf *sf[3] = {NULL, NULL, NULL};
1404227087Syongari	int i;
1405227087Syongari	bus_dma_segment_t segs[4];
1406227087Syongari	int nsegs;
140798849Sken
140898849Sken	if (m_old != NULL) {
140998849Sken		m_new = m_old;
141098849Sken		cur = m_old->m_next;
141198849Sken		for (i = 0; i <= NPAYLOAD; i++){
141298849Sken			m[i] = cur;
141398849Sken			cur = cur->m_next;
141498849Sken		}
141598849Sken	} else {
141698849Sken		/* Allocate the mbufs. */
1417111119Simp		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
141898849Sken		if (m_new == NULL) {
1419162321Sglebius			device_printf(sc->ti_dev, "mbuf allocation failed "
1420150719Sjhb			    "-- packet dropped!\n");
142198849Sken			goto nobufs;
142298849Sken		}
1423111119Simp		MGET(m[NPAYLOAD], M_DONTWAIT, MT_DATA);
142498849Sken		if (m[NPAYLOAD] == NULL) {
1425162321Sglebius			device_printf(sc->ti_dev, "cluster mbuf allocation "
1426162321Sglebius			    "failed -- packet dropped!\n");
142798849Sken			goto nobufs;
142898849Sken		}
1429111119Simp		MCLGET(m[NPAYLOAD], M_DONTWAIT);
143098849Sken		if ((m[NPAYLOAD]->m_flags & M_EXT) == 0) {
1431162321Sglebius			device_printf(sc->ti_dev, "mbuf allocation failed "
1432150719Sjhb			    "-- packet dropped!\n");
143398849Sken			goto nobufs;
143498849Sken		}
143598849Sken		m[NPAYLOAD]->m_len = MCLBYTES;
143698849Sken
143798849Sken		for (i = 0; i < NPAYLOAD; i++){
1438111119Simp			MGET(m[i], M_DONTWAIT, MT_DATA);
143998849Sken			if (m[i] == NULL) {
1440162321Sglebius				device_printf(sc->ti_dev, "mbuf allocation "
1441162321Sglebius				    "failed -- packet dropped!\n");
144298849Sken				goto nobufs;
144398849Sken			}
1444138424Salc			frame = vm_page_alloc(NULL, color++,
1445138424Salc			    VM_ALLOC_INTERRUPT | VM_ALLOC_NOOBJ |
1446138424Salc			    VM_ALLOC_WIRED);
1447138424Salc			if (frame == NULL) {
1448162321Sglebius				device_printf(sc->ti_dev, "buffer allocation "
1449150719Sjhb				    "failed -- packet dropped!\n");
145098849Sken				printf("      index %d page %d\n", idx, i);
1451131655Sbms				goto nobufs;
145298849Sken			}
1453138424Salc			sf[i] = sf_buf_alloc(frame, SFB_NOWAIT);
1454138424Salc			if (sf[i] == NULL) {
1455138424Salc				vm_page_unwire(frame, 0);
1456138424Salc				vm_page_free(frame);
1457162321Sglebius				device_printf(sc->ti_dev, "buffer allocation "
1458150719Sjhb				    "failed -- packet dropped!\n");
1459138424Salc				printf("      index %d page %d\n", idx, i);
1460138424Salc				goto nobufs;
1461138424Salc			}
146298849Sken		}
146398849Sken		for (i = 0; i < NPAYLOAD; i++){
1464131655Sbms		/* Attach the buffer to the mbuf. */
1465138424Salc			m[i]->m_data = (void *)sf_buf_kva(sf[i]);
146698849Sken			m[i]->m_len = PAGE_SIZE;
1467138424Salc			MEXTADD(m[i], sf_buf_kva(sf[i]), PAGE_SIZE,
1468175872Sphk			    sf_buf_mext, (void*)sf_buf_kva(sf[i]), sf[i],
1469175872Sphk			    0, EXT_DISPOSABLE);
147098849Sken			m[i]->m_next = m[i+1];
147198849Sken		}
147298849Sken		/* link the buffers to the header */
147398849Sken		m_new->m_next = m[0];
147498849Sken		m_new->m_data += ETHER_ALIGN;
147598849Sken		if (sc->ti_hdrsplit)
147698849Sken			m_new->m_len = MHLEN - ETHER_ALIGN;
147798849Sken		else
1478131655Sbms			m_new->m_len = HDR_LEN;
147998849Sken		m_new->m_pkthdr.len = NPAYLOAD * PAGE_SIZE + m_new->m_len;
148098849Sken	}
148198849Sken
148298849Sken	/* Set up the descriptor. */
148398849Sken	r = &sc->ti_rdata->ti_rx_jumbo_ring[idx];
148498849Sken	sc->ti_cdata.ti_rx_jumbo_chain[idx] = m_new;
1485153396Sscottl	map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1486153396Sscottl	if (bus_dmamap_load_mbuf_sg(sc->ti_jumbo_dmat, map, m_new, segs,
1487153396Sscottl				    &nsegs, 0))
1488153396Sscottl		return (ENOBUFS);
1489153396Sscottl	if ((nsegs < 1) || (nsegs > 4))
1490153396Sscottl		return (ENOBUFS);
1491153396Sscottl	ti_hostaddr64(&r->ti_addr0, segs[0].ds_addr);
149298849Sken	r->ti_len0 = m_new->m_len;
149398849Sken
1494153396Sscottl	ti_hostaddr64(&r->ti_addr1, segs[1].ds_addr);
149598849Sken	r->ti_len1 = PAGE_SIZE;
149698849Sken
1497153396Sscottl	ti_hostaddr64(&r->ti_addr2, segs[2].ds_addr);
149898849Sken	r->ti_len2 = m[1]->m_ext.ext_size; /* could be PAGE_SIZE or MCLBYTES */
149998849Sken
150098849Sken	if (PAGE_SIZE == 4096) {
1501153396Sscottl		ti_hostaddr64(&r->ti_addr3, segs[3].ds_addr);
150298849Sken		r->ti_len3 = MCLBYTES;
150398849Sken	} else {
150498849Sken		r->ti_len3 = 0;
150598849Sken	}
1506131655Sbms	r->ti_type = TI_BDTYPE_RECV_JUMBO_BD;
150798849Sken
1508131655Sbms	r->ti_flags = TI_BDFLAG_JUMBO_RING|TI_RCB_FLAG_USE_EXT_RX_BD;
150998849Sken
1510227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
151198849Sken		r->ti_flags |= TI_BDFLAG_TCP_UDP_CKSUM|TI_BDFLAG_IP_CKSUM;
151298849Sken
1513131655Sbms	r->ti_idx = idx;
151498849Sken
1515153396Sscottl	bus_dmamap_sync(sc->ti_jumbo_dmat, map, BUS_DMASYNC_PREREAD);
1516131655Sbms	return (0);
151798849Sken
1518131655Sbmsnobufs:
151998849Sken
152098849Sken	/*
152198849Sken	 * Warning! :
152298849Sken	 * This can only be called before the mbufs are strung together.
1523131652Sbms	 * If the mbufs are strung together, m_freem() will free the chain,
152498849Sken	 * so that the later mbufs will be freed multiple times.
152598849Sken	 */
1526131655Sbms	if (m_new)
1527131655Sbms		m_freem(m_new);
152898849Sken
1529131655Sbms	for (i = 0; i < 3; i++) {
1530131655Sbms		if (m[i])
1531131655Sbms			m_freem(m[i]);
1532138424Salc		if (sf[i])
1533138424Salc			sf_buf_mext((void *)sf_buf_kva(sf[i]), sf[i]);
1534131655Sbms	}
1535131655Sbms	return (ENOBUFS);
153698849Sken}
153798849Sken#endif
153898849Sken
153945386Swpaul/*
154045386Swpaul * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
154145386Swpaul * that's 1MB or memory, which is a lot. For now, we fill only the first
154245386Swpaul * 256 ring entries and hope that our CPU is fast enough to keep up with
154345386Swpaul * the NIC.
154445386Swpaul */
1545102336Salfredstatic int
1546227086Syongariti_init_rx_ring_std(struct ti_softc *sc)
154745386Swpaul{
1548227087Syongari	int i;
1549227087Syongari	struct ti_cmd_desc cmd;
155045386Swpaul
155145386Swpaul	for (i = 0; i < TI_SSLOTS; i++) {
155245386Swpaul		if (ti_newbuf_std(sc, i, NULL) == ENOBUFS)
1553131654Sbms			return (ENOBUFS);
155445386Swpaul	};
155545386Swpaul
155645386Swpaul	TI_UPDATE_STDPROD(sc, i - 1);
155748597Swpaul	sc->ti_std = i - 1;
155845386Swpaul
1559131654Sbms	return (0);
156045386Swpaul}
156145386Swpaul
1562102336Salfredstatic void
1563227086Syongariti_free_rx_ring_std(struct ti_softc *sc)
156445386Swpaul{
1565227087Syongari	bus_dmamap_t map;
1566227087Syongari	int i;
156745386Swpaul
156845386Swpaul	for (i = 0; i < TI_STD_RX_RING_CNT; i++) {
156945386Swpaul		if (sc->ti_cdata.ti_rx_std_chain[i] != NULL) {
1570153770Syongari			map = sc->ti_cdata.ti_rx_std_maps[i];
1571153770Syongari			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
1572153770Syongari			    BUS_DMASYNC_POSTREAD);
1573153770Syongari			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
157445386Swpaul			m_freem(sc->ti_cdata.ti_rx_std_chain[i]);
157545386Swpaul			sc->ti_cdata.ti_rx_std_chain[i] = NULL;
157645386Swpaul		}
157745386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_std_ring[i],
157845386Swpaul		    sizeof(struct ti_rx_desc));
157945386Swpaul	}
158045386Swpaul}
158145386Swpaul
1582102336Salfredstatic int
1583227086Syongariti_init_rx_ring_jumbo(struct ti_softc *sc)
158445386Swpaul{
1585227087Syongari	struct ti_cmd_desc cmd;
1586227087Syongari	int i;
158745386Swpaul
158863699Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
158945386Swpaul		if (ti_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1590131654Sbms			return (ENOBUFS);
159145386Swpaul	};
159245386Swpaul
159345386Swpaul	TI_UPDATE_JUMBOPROD(sc, i - 1);
159448597Swpaul	sc->ti_jumbo = i - 1;
159545386Swpaul
1596131654Sbms	return (0);
159745386Swpaul}
159845386Swpaul
1599102336Salfredstatic void
1600227086Syongariti_free_rx_ring_jumbo(struct ti_softc *sc)
160145386Swpaul{
1602227087Syongari	bus_dmamap_t map;
1603227087Syongari	int i;
160445386Swpaul
160545386Swpaul	for (i = 0; i < TI_JUMBO_RX_RING_CNT; i++) {
160645386Swpaul		if (sc->ti_cdata.ti_rx_jumbo_chain[i] != NULL) {
1607153770Syongari			map = sc->ti_cdata.ti_rx_jumbo_maps[i];
1608153770Syongari			bus_dmamap_sync(sc->ti_jumbo_dmat, map,
1609153770Syongari			    BUS_DMASYNC_POSTREAD);
1610153770Syongari			bus_dmamap_unload(sc->ti_jumbo_dmat, map);
161145386Swpaul			m_freem(sc->ti_cdata.ti_rx_jumbo_chain[i]);
161245386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[i] = NULL;
161345386Swpaul		}
161445386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_jumbo_ring[i],
161545386Swpaul		    sizeof(struct ti_rx_desc));
161645386Swpaul	}
161745386Swpaul}
161845386Swpaul
1619102336Salfredstatic int
1620227086Syongariti_init_rx_ring_mini(struct ti_softc *sc)
162145386Swpaul{
1622227087Syongari	int i;
162345386Swpaul
162445386Swpaul	for (i = 0; i < TI_MSLOTS; i++) {
162545386Swpaul		if (ti_newbuf_mini(sc, i, NULL) == ENOBUFS)
1626131654Sbms			return (ENOBUFS);
162745386Swpaul	};
162845386Swpaul
162945386Swpaul	TI_UPDATE_MINIPROD(sc, i - 1);
163048597Swpaul	sc->ti_mini = i - 1;
163145386Swpaul
1632131654Sbms	return (0);
163345386Swpaul}
163445386Swpaul
1635102336Salfredstatic void
1636227086Syongariti_free_rx_ring_mini(struct ti_softc *sc)
163745386Swpaul{
1638227087Syongari	bus_dmamap_t map;
1639227087Syongari	int i;
164045386Swpaul
164145386Swpaul	for (i = 0; i < TI_MINI_RX_RING_CNT; i++) {
164245386Swpaul		if (sc->ti_cdata.ti_rx_mini_chain[i] != NULL) {
1643153770Syongari			map = sc->ti_cdata.ti_rx_mini_maps[i];
1644153770Syongari			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
1645153770Syongari			    BUS_DMASYNC_POSTREAD);
1646153770Syongari			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
164745386Swpaul			m_freem(sc->ti_cdata.ti_rx_mini_chain[i]);
164845386Swpaul			sc->ti_cdata.ti_rx_mini_chain[i] = NULL;
164945386Swpaul		}
165045386Swpaul		bzero((char *)&sc->ti_rdata->ti_rx_mini_ring[i],
165145386Swpaul		    sizeof(struct ti_rx_desc));
165245386Swpaul	}
165345386Swpaul}
165445386Swpaul
1655102336Salfredstatic void
1656227086Syongariti_free_tx_ring(struct ti_softc *sc)
165745386Swpaul{
1658227087Syongari	struct ti_txdesc *txd;
1659227087Syongari	int i;
166045386Swpaul
166145386Swpaul	if (sc->ti_rdata->ti_tx_ring == NULL)
166245386Swpaul		return;
166345386Swpaul
166445386Swpaul	for (i = 0; i < TI_TX_RING_CNT; i++) {
1665153982Syongari		txd = &sc->ti_cdata.ti_txdesc[i];
1666153982Syongari		if (txd->tx_m != NULL) {
1667153982Syongari			bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
1668153770Syongari			    BUS_DMASYNC_POSTWRITE);
1669153982Syongari			bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
1670153982Syongari			m_freem(txd->tx_m);
1671153982Syongari			txd->tx_m = NULL;
167245386Swpaul		}
167345386Swpaul		bzero((char *)&sc->ti_rdata->ti_tx_ring[i],
167445386Swpaul		    sizeof(struct ti_tx_desc));
167545386Swpaul	}
167645386Swpaul}
167745386Swpaul
1678102336Salfredstatic int
1679227086Syongariti_init_tx_ring(struct ti_softc *sc)
168045386Swpaul{
1681227087Syongari	struct ti_txdesc *txd;
1682227087Syongari	int i;
1683153982Syongari
1684153982Syongari	STAILQ_INIT(&sc->ti_cdata.ti_txfreeq);
1685153982Syongari	STAILQ_INIT(&sc->ti_cdata.ti_txbusyq);
1686153982Syongari	for (i = 0; i < TI_TX_RING_CNT; i++) {
1687153982Syongari		txd = &sc->ti_cdata.ti_txdesc[i];
1688153982Syongari		STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txfreeq, txd, tx_q);
1689153982Syongari	}
169048011Swpaul	sc->ti_txcnt = 0;
169145386Swpaul	sc->ti_tx_saved_considx = 0;
1692153778Sscottl	sc->ti_tx_saved_prodidx = 0;
169345386Swpaul	CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, 0);
1694131654Sbms	return (0);
169545386Swpaul}
169645386Swpaul
169745386Swpaul/*
169845386Swpaul * The Tigon 2 firmware has a new way to add/delete multicast addresses,
169945386Swpaul * but we have to support the old way too so that Tigon 1 cards will
170045386Swpaul * work.
170145386Swpaul */
1702105219Sphkstatic void
1703227086Syongariti_add_mcast(struct ti_softc *sc, struct ether_addr *addr)
170445386Swpaul{
1705227087Syongari	struct ti_cmd_desc cmd;
1706227089Syongari	uint16_t *m;
1707227089Syongari	uint32_t ext[2] = {0, 0};
170845386Swpaul
1709227089Syongari	m = (uint16_t *)&addr->octet[0];
171045386Swpaul
1711131654Sbms	switch (sc->ti_hwrev) {
171245386Swpaul	case TI_HWREV_TIGON:
171345386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
171445386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
171545386Swpaul		TI_DO_CMD(TI_CMD_ADD_MCAST_ADDR, 0, 0);
171645386Swpaul		break;
171745386Swpaul	case TI_HWREV_TIGON_II:
171845386Swpaul		ext[0] = htons(m[0]);
171945386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
172045386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_ADD_MCAST, 0, 0, (caddr_t)&ext, 2);
172145386Swpaul		break;
172245386Swpaul	default:
1723162321Sglebius		device_printf(sc->ti_dev, "unknown hwrev\n");
172445386Swpaul		break;
172545386Swpaul	}
172645386Swpaul}
172745386Swpaul
1728105219Sphkstatic void
1729227086Syongariti_del_mcast(struct ti_softc *sc, struct ether_addr *addr)
173045386Swpaul{
1731227087Syongari	struct ti_cmd_desc cmd;
1732227089Syongari	uint16_t *m;
1733227089Syongari	uint32_t ext[2] = {0, 0};
173445386Swpaul
1735227089Syongari	m = (uint16_t *)&addr->octet[0];
173645386Swpaul
1737131654Sbms	switch (sc->ti_hwrev) {
173845386Swpaul	case TI_HWREV_TIGON:
173945386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR0, htons(m[0]));
174045386Swpaul		CSR_WRITE_4(sc, TI_GCR_MAR1, (htons(m[1]) << 16) | htons(m[2]));
174145386Swpaul		TI_DO_CMD(TI_CMD_DEL_MCAST_ADDR, 0, 0);
174245386Swpaul		break;
174345386Swpaul	case TI_HWREV_TIGON_II:
174445386Swpaul		ext[0] = htons(m[0]);
174545386Swpaul		ext[1] = (htons(m[1]) << 16) | htons(m[2]);
174645386Swpaul		TI_DO_CMD_EXT(TI_CMD_EXT_DEL_MCAST, 0, 0, (caddr_t)&ext, 2);
174745386Swpaul		break;
174845386Swpaul	default:
1749162321Sglebius		device_printf(sc->ti_dev, "unknown hwrev\n");
175045386Swpaul		break;
175145386Swpaul	}
175245386Swpaul}
175345386Swpaul
175445386Swpaul/*
175545386Swpaul * Configure the Tigon's multicast address filter.
175645386Swpaul *
175745386Swpaul * The actual multicast table management is a bit of a pain, thanks to
175845386Swpaul * slight brain damage on the part of both Alteon and us. With our
175945386Swpaul * multicast code, we are only alerted when the multicast address table
176045386Swpaul * changes and at that point we only have the current list of addresses:
176145386Swpaul * we only know the current state, not the previous state, so we don't
176245386Swpaul * actually know what addresses were removed or added. The firmware has
176345386Swpaul * state, but we can't get our grubby mits on it, and there is no 'delete
176445386Swpaul * all multicast addresses' command. Hence, we have to maintain our own
176545386Swpaul * state so we know what addresses have been programmed into the NIC at
176645386Swpaul * any given time.
176745386Swpaul */
1768102336Salfredstatic void
1769227086Syongariti_setmulti(struct ti_softc *sc)
177045386Swpaul{
1771227087Syongari	struct ifnet *ifp;
1772227087Syongari	struct ifmultiaddr *ifma;
1773227087Syongari	struct ti_cmd_desc cmd;
1774227087Syongari	struct ti_mc_entry *mc;
1775227089Syongari	uint32_t intrs;
177645386Swpaul
1777153770Syongari	TI_LOCK_ASSERT(sc);
1778153770Syongari
1779147256Sbrooks	ifp = sc->ti_ifp;
178045386Swpaul
178145386Swpaul	if (ifp->if_flags & IFF_ALLMULTI) {
178245386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_ENB, 0);
178345386Swpaul		return;
178445386Swpaul	} else {
178545386Swpaul		TI_DO_CMD(TI_CMD_SET_ALLMULTI, TI_CMD_CODE_ALLMULTI_DIS, 0);
178645386Swpaul	}
178745386Swpaul
178845386Swpaul	/* Disable interrupts. */
178945386Swpaul	intrs = CSR_READ_4(sc, TI_MB_HOSTINTR);
179045386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
179145386Swpaul
179245386Swpaul	/* First, zot all the existing filters. */
179371999Sphk	while (SLIST_FIRST(&sc->ti_mc_listhead) != NULL) {
179471999Sphk		mc = SLIST_FIRST(&sc->ti_mc_listhead);
179545386Swpaul		ti_del_mcast(sc, &mc->mc_addr);
179645386Swpaul		SLIST_REMOVE_HEAD(&sc->ti_mc_listhead, mc_entries);
179745386Swpaul		free(mc, M_DEVBUF);
179845386Swpaul	}
179945386Swpaul
180045386Swpaul	/* Now program new ones. */
1801195049Srwatson	if_maddr_rlock(ifp);
180272084Sphk	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
180345386Swpaul		if (ifma->ifma_addr->sa_family != AF_LINK)
180445386Swpaul			continue;
180545386Swpaul		mc = malloc(sizeof(struct ti_mc_entry), M_DEVBUF, M_NOWAIT);
1806144165Ssam		if (mc == NULL) {
1807162321Sglebius			device_printf(sc->ti_dev,
1808162321Sglebius			    "no memory for mcast filter entry\n");
1809144165Ssam			continue;
1810144165Ssam		}
181145386Swpaul		bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
181245386Swpaul		    (char *)&mc->mc_addr, ETHER_ADDR_LEN);
181345386Swpaul		SLIST_INSERT_HEAD(&sc->ti_mc_listhead, mc, mc_entries);
181445386Swpaul		ti_add_mcast(sc, &mc->mc_addr);
181545386Swpaul	}
1816195049Srwatson	if_maddr_runlock(ifp);
181745386Swpaul
181845386Swpaul	/* Re-enable interrupts. */
181945386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, intrs);
182045386Swpaul}
182145386Swpaul
182245386Swpaul/*
182345386Swpaul * Check to see if the BIOS has configured us for a 64 bit slot when
182445386Swpaul * we aren't actually in one. If we detect this condition, we can work
182545386Swpaul * around it on the Tigon 2 by setting a bit in the PCI state register,
182645386Swpaul * but for the Tigon 1 we must give up and abort the interface attach.
182745386Swpaul */
1828227086Syongaristatic int ti_64bitslot_war(struct ti_softc *sc)
182945386Swpaul{
1830227087Syongari
183145386Swpaul	if (!(CSR_READ_4(sc, TI_PCI_STATE) & TI_PCISTATE_32BIT_BUS)) {
183245386Swpaul		CSR_WRITE_4(sc, 0x600, 0);
183345386Swpaul		CSR_WRITE_4(sc, 0x604, 0);
183445386Swpaul		CSR_WRITE_4(sc, 0x600, 0x5555AAAA);
183545386Swpaul		if (CSR_READ_4(sc, 0x604) == 0x5555AAAA) {
183645386Swpaul			if (sc->ti_hwrev == TI_HWREV_TIGON)
1837131654Sbms				return (EINVAL);
183845386Swpaul			else {
183945386Swpaul				TI_SETBIT(sc, TI_PCI_STATE,
184045386Swpaul				    TI_PCISTATE_32BIT_BUS);
1841131654Sbms				return (0);
184245386Swpaul			}
184345386Swpaul		}
184445386Swpaul	}
184545386Swpaul
1846131654Sbms	return (0);
184745386Swpaul}
184845386Swpaul
184945386Swpaul/*
185045386Swpaul * Do endian, PCI and DMA initialization. Also check the on-board ROM
185145386Swpaul * self-test results.
185245386Swpaul */
1853102336Salfredstatic int
1854227086Syongariti_chipinit(struct ti_softc *sc)
185545386Swpaul{
1856227089Syongari	uint32_t cacheline;
1857227089Syongari	uint32_t pci_writemax = 0;
1858227089Syongari	uint32_t hdrsplit;
185945386Swpaul
186045386Swpaul	/* Initialize link to down state. */
186145386Swpaul	sc->ti_linkstat = TI_EV_CODE_LINK_DOWN;
186245386Swpaul
186345386Swpaul	/* Set endianness before we access any non-PCI registers. */
1864153770Syongari#if 0 && BYTE_ORDER == BIG_ENDIAN
186545386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
186645386Swpaul	    TI_MHC_BIGENDIAN_INIT | (TI_MHC_BIGENDIAN_INIT << 24));
186745386Swpaul#else
186845386Swpaul	CSR_WRITE_4(sc, TI_MISC_HOST_CTL,
186945386Swpaul	    TI_MHC_LITTLEENDIAN_INIT | (TI_MHC_LITTLEENDIAN_INIT << 24));
187045386Swpaul#endif
187145386Swpaul
187245386Swpaul	/* Check the ROM failed bit to see if self-tests passed. */
187345386Swpaul	if (CSR_READ_4(sc, TI_CPU_STATE) & TI_CPUSTATE_ROMFAIL) {
1874162321Sglebius		device_printf(sc->ti_dev, "board self-diagnostics failed!\n");
1875131654Sbms		return (ENODEV);
187645386Swpaul	}
187745386Swpaul
187845386Swpaul	/* Halt the CPU. */
187945386Swpaul	TI_SETBIT(sc, TI_CPU_STATE, TI_CPUSTATE_HALT);
188045386Swpaul
188145386Swpaul	/* Figure out the hardware revision. */
1882131654Sbms	switch (CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_CHIP_REV_MASK) {
188345386Swpaul	case TI_REV_TIGON_I:
188445386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON;
188545386Swpaul		break;
188645386Swpaul	case TI_REV_TIGON_II:
188745386Swpaul		sc->ti_hwrev = TI_HWREV_TIGON_II;
188845386Swpaul		break;
188945386Swpaul	default:
1890162321Sglebius		device_printf(sc->ti_dev, "unsupported chip revision\n");
1891131654Sbms		return (ENODEV);
189245386Swpaul	}
189345386Swpaul
189445386Swpaul	/* Do special setup for Tigon 2. */
189545386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
189645386Swpaul		TI_SETBIT(sc, TI_CPU_CTL_B, TI_CPUSTATE_HALT);
189776033Swpaul		TI_SETBIT(sc, TI_MISC_LOCAL_CTL, TI_MLC_SRAM_BANK_512K);
189845386Swpaul		TI_SETBIT(sc, TI_MISC_CONF, TI_MCR_SRAM_SYNCHRONOUS);
189945386Swpaul	}
190045386Swpaul
190198849Sken	/*
190298849Sken	 * We don't have firmware source for the Tigon 1, so Tigon 1 boards
190398849Sken	 * can't do header splitting.
190498849Sken	 */
190598849Sken#ifdef TI_JUMBO_HDRSPLIT
190698849Sken	if (sc->ti_hwrev != TI_HWREV_TIGON)
190798849Sken		sc->ti_hdrsplit = 1;
190898849Sken	else
1909162321Sglebius		device_printf(sc->ti_dev,
1910150719Sjhb		    "can't do header splitting on a Tigon I board\n");
191198849Sken#endif /* TI_JUMBO_HDRSPLIT */
191298849Sken
191345386Swpaul	/* Set up the PCI state register. */
191445386Swpaul	CSR_WRITE_4(sc, TI_PCI_STATE, TI_PCI_READ_CMD|TI_PCI_WRITE_CMD);
191545386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II) {
191645386Swpaul		TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_USE_MEM_RD_MULT);
191745386Swpaul	}
191845386Swpaul
191945386Swpaul	/* Clear the read/write max DMA parameters. */
192045386Swpaul	TI_CLRBIT(sc, TI_PCI_STATE, (TI_PCISTATE_WRITE_MAXDMA|
192145386Swpaul	    TI_PCISTATE_READ_MAXDMA));
192245386Swpaul
192345386Swpaul	/* Get cache line size. */
192445386Swpaul	cacheline = CSR_READ_4(sc, TI_PCI_BIST) & 0xFF;
192545386Swpaul
192645386Swpaul	/*
192745386Swpaul	 * If the system has set enabled the PCI memory write
192845386Swpaul	 * and invalidate command in the command register, set
192945386Swpaul	 * the write max parameter accordingly. This is necessary
193045386Swpaul	 * to use MWI with the Tigon 2.
193145386Swpaul	 */
193245386Swpaul	if (CSR_READ_4(sc, TI_PCI_CMDSTAT) & PCIM_CMD_MWIEN) {
1933131654Sbms		switch (cacheline) {
193445386Swpaul		case 1:
193545386Swpaul		case 4:
193645386Swpaul		case 8:
193745386Swpaul		case 16:
193845386Swpaul		case 32:
193945386Swpaul		case 64:
194045386Swpaul			break;
194145386Swpaul		default:
194245386Swpaul		/* Disable PCI memory write and invalidate. */
194345386Swpaul			if (bootverbose)
1944162321Sglebius				device_printf(sc->ti_dev, "cache line size %d"
1945162321Sglebius				    " not supported; disabling PCI MWI\n",
1946150719Sjhb				    cacheline);
194745386Swpaul			CSR_WRITE_4(sc, TI_PCI_CMDSTAT, CSR_READ_4(sc,
194845386Swpaul			    TI_PCI_CMDSTAT) & ~PCIM_CMD_MWIEN);
194945386Swpaul			break;
195045386Swpaul		}
195145386Swpaul	}
195245386Swpaul
195345386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, pci_writemax);
195445386Swpaul
195545386Swpaul	/* This sets the min dma param all the way up (0xff). */
195645386Swpaul	TI_SETBIT(sc, TI_PCI_STATE, TI_PCISTATE_MINDMA);
195745386Swpaul
195898849Sken	if (sc->ti_hdrsplit)
195998849Sken		hdrsplit = TI_OPMODE_JUMBO_HDRSPLIT;
196098849Sken	else
196198849Sken		hdrsplit = 0;
196298849Sken
196345386Swpaul	/* Configure DMA variables. */
196445386Swpaul#if BYTE_ORDER == BIG_ENDIAN
196545386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_BD |
196645386Swpaul	    TI_OPMODE_BYTESWAP_DATA | TI_OPMODE_WORDSWAP_BD |
196745386Swpaul	    TI_OPMODE_WARN_ENB | TI_OPMODE_FATAL_ENB |
196898849Sken	    TI_OPMODE_DONT_FRAG_JUMBO | hdrsplit);
196998849Sken#else /* BYTE_ORDER */
197045386Swpaul	CSR_WRITE_4(sc, TI_GCR_OPMODE, TI_OPMODE_BYTESWAP_DATA|
197145386Swpaul	    TI_OPMODE_WORDSWAP_BD|TI_OPMODE_DONT_FRAG_JUMBO|
197298849Sken	    TI_OPMODE_WARN_ENB|TI_OPMODE_FATAL_ENB | hdrsplit);
197398849Sken#endif /* BYTE_ORDER */
197445386Swpaul
197545386Swpaul	/*
197645386Swpaul	 * Only allow 1 DMA channel to be active at a time.
197745386Swpaul	 * I don't think this is a good idea, but without it
197845386Swpaul	 * the firmware racks up lots of nicDmaReadRingFull
197958698Sjlemon	 * errors.  This is not compatible with hardware checksums.
198045386Swpaul	 */
1981227095Syongari	if ((sc->ti_ifp->if_capenable & (IFCAP_TXCSUM | IFCAP_RXCSUM)) == 0)
198258698Sjlemon		TI_SETBIT(sc, TI_GCR_OPMODE, TI_OPMODE_1_DMA_ACTIVE);
198345386Swpaul
198445386Swpaul	/* Recommended settings from Tigon manual. */
198545386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_WRITECFG, TI_DMA_STATE_THRESH_8W);
198645386Swpaul	CSR_WRITE_4(sc, TI_GCR_DMA_READCFG, TI_DMA_STATE_THRESH_8W);
198745386Swpaul
198845386Swpaul	if (ti_64bitslot_war(sc)) {
1989162321Sglebius		device_printf(sc->ti_dev, "bios thinks we're in a 64 bit slot, "
1990150719Sjhb		    "but we aren't");
1991131654Sbms		return (EINVAL);
199245386Swpaul	}
199345386Swpaul
1994131654Sbms	return (0);
199545386Swpaul}
199645386Swpaul
199745386Swpaul/*
199845386Swpaul * Initialize the general information block and firmware, and
199945386Swpaul * start the CPU(s) running.
200045386Swpaul */
2001102336Salfredstatic int
2002227086Syongariti_gibinit(struct ti_softc *sc)
200345386Swpaul{
2004227087Syongari	struct ifnet *ifp;
2005227087Syongari	struct ti_rcb *rcb;
2006227087Syongari	uint32_t rdphys;
2007227087Syongari	int i;
200845386Swpaul
2009153770Syongari	TI_LOCK_ASSERT(sc);
2010153770Syongari
2011147256Sbrooks	ifp = sc->ti_ifp;
2012143903Sscottl	rdphys = sc->ti_rdata_phys;
201345386Swpaul
201445386Swpaul	/* Disable interrupts for now. */
201545386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
201645386Swpaul
2017143903Sscottl	/*
2018143903Sscottl	 * Tell the chip where to find the general information block.
2019143903Sscottl	 * While this struct could go into >4GB memory, we allocate it in a
2020143903Sscottl	 * single slab with the other descriptors, and those don't seem to
2021143903Sscottl	 * support being located in a 64-bit region.
2022143903Sscottl	 */
202345386Swpaul	CSR_WRITE_4(sc, TI_GCR_GENINFO_HI, 0);
2024143903Sscottl	CSR_WRITE_4(sc, TI_GCR_GENINFO_LO, rdphys + TI_RD_OFF(ti_info));
202545386Swpaul
202645386Swpaul	/* Load the firmware into SRAM. */
202745386Swpaul	ti_loadfw(sc);
202845386Swpaul
202945386Swpaul	/* Set up the contents of the general info and ring control blocks. */
203045386Swpaul
203145386Swpaul	/* Set up the event ring and producer pointer. */
203245386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_ev_rcb;
203345386Swpaul
2034143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_event_ring);
203545386Swpaul	rcb->ti_flags = 0;
203645386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_ev_prodidx_ptr) =
2037143903Sscottl	    rdphys + TI_RD_OFF(ti_ev_prodidx_r);
203845386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
203945386Swpaul	CSR_WRITE_4(sc, TI_GCR_EVENTCONS_IDX, 0);
204045386Swpaul	sc->ti_ev_saved_considx = 0;
204145386Swpaul
204245386Swpaul	/* Set up the command ring and producer mailbox. */
204345386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_cmd_rcb;
204445386Swpaul
204545386Swpaul	TI_HOSTADDR(rcb->ti_hostaddr) = TI_GCR_NIC_ADDR(TI_GCR_CMDRING);
204645386Swpaul	rcb->ti_flags = 0;
204745386Swpaul	rcb->ti_max_len = 0;
204845386Swpaul	for (i = 0; i < TI_CMD_RING_CNT; i++) {
204945386Swpaul		CSR_WRITE_4(sc, TI_GCR_CMDRING + (i * 4), 0);
205045386Swpaul	}
205145386Swpaul	CSR_WRITE_4(sc, TI_GCR_CMDCONS_IDX, 0);
205245386Swpaul	CSR_WRITE_4(sc, TI_MB_CMDPROD_IDX, 0);
205345386Swpaul	sc->ti_cmd_saved_prodidx = 0;
205445386Swpaul
205545386Swpaul	/*
205645386Swpaul	 * Assign the address of the stats refresh buffer.
205745386Swpaul	 * We re-use the current stats buffer for this to
205845386Swpaul	 * conserve memory.
205945386Swpaul	 */
206045386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_refresh_stats_ptr) =
2061143903Sscottl	    rdphys + TI_RD_OFF(ti_info.ti_stats);
206245386Swpaul
206345386Swpaul	/* Set up the standard receive ring. */
206445386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_std_rx_rcb;
2065143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_std_ring);
206645386Swpaul	rcb->ti_max_len = TI_FRAMELEN;
206745386Swpaul	rcb->ti_flags = 0;
2068227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
206958698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
207058698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
2071227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2072227095Syongari		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
207345386Swpaul
207445386Swpaul	/* Set up the jumbo receive ring. */
207545386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_jumbo_rx_rcb;
2076143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_jumbo_ring);
207798849Sken
207898849Sken#ifdef TI_PRIVATE_JUMBOS
207949036Swpaul	rcb->ti_max_len = TI_JUMBO_FRAMELEN;
208045386Swpaul	rcb->ti_flags = 0;
208198849Sken#else
208298849Sken	rcb->ti_max_len = PAGE_SIZE;
208398849Sken	rcb->ti_flags = TI_RCB_FLAG_USE_EXT_RX_BD;
208498849Sken#endif
2085227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
208658698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
208758698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
2088227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2089227095Syongari		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
209045386Swpaul
209145386Swpaul	/*
209245386Swpaul	 * Set up the mini ring. Only activated on the
209345386Swpaul	 * Tigon 2 but the slot in the config block is
209445386Swpaul	 * still there on the Tigon 1.
209545386Swpaul	 */
209645386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_mini_rx_rcb;
2097143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_mini_ring);
209851352Swpaul	rcb->ti_max_len = MHLEN - ETHER_ALIGN;
209945386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
210045386Swpaul		rcb->ti_flags = TI_RCB_FLAG_RING_DISABLED;
210145386Swpaul	else
210245386Swpaul		rcb->ti_flags = 0;
2103227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_RXCSUM)
210458698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
210558698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
2106227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2107227095Syongari		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
210845386Swpaul
210945386Swpaul	/*
211045386Swpaul	 * Set up the receive return ring.
211145386Swpaul	 */
211245386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_return_rcb;
2113143903Sscottl	TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_rx_return_ring);
211445386Swpaul	rcb->ti_flags = 0;
211545386Swpaul	rcb->ti_max_len = TI_RETURN_RING_CNT;
211645386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_return_prodidx_ptr) =
2117143903Sscottl	    rdphys + TI_RD_OFF(ti_return_prodidx_r);
211845386Swpaul
211945386Swpaul	/*
212045386Swpaul	 * Set up the tx ring. Note: for the Tigon 2, we have the option
212145386Swpaul	 * of putting the transmit ring in the host's address space and
212245386Swpaul	 * letting the chip DMA it instead of leaving the ring in the NIC's
212345386Swpaul	 * memory and accessing it through the shared memory region. We
212445386Swpaul	 * do this for the Tigon 2, but it doesn't work on the Tigon 1,
212545386Swpaul	 * so we have to revert to the shared memory scheme if we detect
212645386Swpaul	 * a Tigon 1 chip.
212745386Swpaul	 */
212845386Swpaul	CSR_WRITE_4(sc, TI_WINBASE, TI_TX_RING_BASE);
212945386Swpaul	bzero((char *)sc->ti_rdata->ti_tx_ring,
213045386Swpaul	    TI_TX_RING_CNT * sizeof(struct ti_tx_desc));
213145386Swpaul	rcb = &sc->ti_rdata->ti_info.ti_tx_rcb;
213245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
213345386Swpaul		rcb->ti_flags = 0;
213445386Swpaul	else
213545386Swpaul		rcb->ti_flags = TI_RCB_FLAG_HOST_RING;
2136227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_VLAN_HWTAGGING)
2137227095Syongari		rcb->ti_flags |= TI_RCB_FLAG_VLAN_ASSIST;
2138227095Syongari	if (sc->ti_ifp->if_capenable & IFCAP_TXCSUM)
213958698Sjlemon		rcb->ti_flags |= TI_RCB_FLAG_TCP_UDP_CKSUM |
214058698Sjlemon		     TI_RCB_FLAG_IP_CKSUM | TI_RCB_FLAG_NO_PHDR_CKSUM;
214145386Swpaul	rcb->ti_max_len = TI_TX_RING_CNT;
214245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
214345386Swpaul		TI_HOSTADDR(rcb->ti_hostaddr) = TI_TX_RING_BASE;
214445386Swpaul	else
2145143903Sscottl		TI_HOSTADDR(rcb->ti_hostaddr) = rdphys + TI_RD_OFF(ti_tx_ring);
214645386Swpaul	TI_HOSTADDR(sc->ti_rdata->ti_info.ti_tx_considx_ptr) =
2147143903Sscottl	    rdphys + TI_RD_OFF(ti_tx_considx_r);
214845386Swpaul
2149153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2150153770Syongari	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
2151153770Syongari
215245386Swpaul	/* Set up tuneables */
215398849Sken#if 0
215445386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
215545386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
215645386Swpaul		    (sc->ti_rx_coal_ticks / 10));
215745386Swpaul	else
215898849Sken#endif
215945386Swpaul		CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS, sc->ti_rx_coal_ticks);
216045386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS, sc->ti_tx_coal_ticks);
216145386Swpaul	CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
216245386Swpaul	CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD, sc->ti_rx_max_coal_bds);
216345386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD, sc->ti_tx_max_coal_bds);
216445386Swpaul	CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO, sc->ti_tx_buf_ratio);
216545386Swpaul
216645386Swpaul	/* Turn interrupts on. */
216745386Swpaul	CSR_WRITE_4(sc, TI_GCR_MASK_INTRS, 0);
216845386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
216945386Swpaul
217045386Swpaul	/* Start CPU. */
217145386Swpaul	TI_CLRBIT(sc, TI_CPU_STATE, (TI_CPUSTATE_HALT|TI_CPUSTATE_STEP));
217245386Swpaul
2173131654Sbms	return (0);
217445386Swpaul}
217545386Swpaul
2176143903Sscottlstatic void
2177143903Sscottlti_rdata_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2178143903Sscottl{
2179143903Sscottl	struct ti_softc *sc;
2180143903Sscottl
2181143903Sscottl	sc = arg;
2182143903Sscottl	if (error || nseg != 1)
2183143903Sscottl		return;
2184143903Sscottl
2185143903Sscottl	/*
2186143903Sscottl	 * All of the Tigon data structures need to live at <4GB.  This
2187143903Sscottl	 * cast is fine since busdma was told about this constraint.
2188143903Sscottl	 */
2189153770Syongari	sc->ti_rdata_phys = segs[0].ds_addr;
2190143903Sscottl	return;
2191143903Sscottl}
2192227087Syongari
219345386Swpaul/*
219445386Swpaul * Probe for a Tigon chip. Check the PCI vendor and device IDs
219545386Swpaul * against our list and return its name if we find a match.
219645386Swpaul */
2197102336Salfredstatic int
2198227086Syongariti_probe(device_t dev)
219945386Swpaul{
2200227087Syongari	const struct ti_type *t;
220145386Swpaul
220245386Swpaul	t = ti_devs;
220345386Swpaul
2204131654Sbms	while (t->ti_name != NULL) {
220549011Swpaul		if ((pci_get_vendor(dev) == t->ti_vid) &&
220649011Swpaul		    (pci_get_device(dev) == t->ti_did)) {
220749011Swpaul			device_set_desc(dev, t->ti_name);
2208142398Simp			return (BUS_PROBE_DEFAULT);
220949011Swpaul		}
221045386Swpaul		t++;
221145386Swpaul	}
221245386Swpaul
2213131654Sbms	return (ENXIO);
221445386Swpaul}
221545386Swpaul
221698849Skenstatic int
2217227086Syongariti_attach(device_t dev)
221845386Swpaul{
2219227087Syongari	struct ifnet *ifp;
2220227087Syongari	struct ti_softc *sc;
2221227087Syongari	int error = 0, rid;
2222227087Syongari	u_char eaddr[6];
222345386Swpaul
222449011Swpaul	sc = device_get_softc(dev);
2225150719Sjhb	sc->ti_unit = device_get_unit(dev);
2226153396Sscottl	sc->ti_dev = dev;
222745386Swpaul
222893818Sjhb	mtx_init(&sc->ti_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2229153770Syongari	    MTX_DEF);
2230199559Sjhb	callout_init_mtx(&sc->ti_watchdog, &sc->ti_mtx, 0);
2231113609Snjl	ifmedia_init(&sc->ifmedia, IFM_IMASK, ti_ifmedia_upd, ti_ifmedia_sts);
2232147805Sscottl	ifp = sc->ti_ifp = if_alloc(IFT_ETHER);
2233147805Sscottl	if (ifp == NULL) {
2234150719Sjhb		device_printf(dev, "can not if_alloc()\n");
2235147805Sscottl		error = ENOSPC;
2236147805Sscottl		goto fail;
2237147805Sscottl	}
2238227095Syongari	sc->ti_ifp->if_hwassist = TI_CSUM_FEATURES;
2239227095Syongari	sc->ti_ifp->if_capabilities = IFCAP_TXCSUM | IFCAP_RXCSUM;
2240147256Sbrooks	sc->ti_ifp->if_capenable = sc->ti_ifp->if_capabilities;
224169583Swpaul
224245386Swpaul	/*
224345386Swpaul	 * Map control/status registers.
224445386Swpaul	 */
224572813Swpaul	pci_enable_busmaster(dev);
224645386Swpaul
224749011Swpaul	rid = TI_PCI_LOMEM;
2248127135Snjl	sc->ti_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
2249178588Smarius	    RF_ACTIVE);
225049011Swpaul
225149011Swpaul	if (sc->ti_res == NULL) {
2252150719Sjhb		device_printf(dev, "couldn't map memory\n");
225349011Swpaul		error = ENXIO;
225445386Swpaul		goto fail;
225545386Swpaul	}
225645386Swpaul
225749035Swpaul	sc->ti_btag = rman_get_bustag(sc->ti_res);
225849035Swpaul	sc->ti_bhandle = rman_get_bushandle(sc->ti_res);
225949035Swpaul
226049011Swpaul	/* Allocate interrupt */
226149011Swpaul	rid = 0;
2262131652Sbms
2263127135Snjl	sc->ti_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
226449011Swpaul	    RF_SHAREABLE | RF_ACTIVE);
226545386Swpaul
226649011Swpaul	if (sc->ti_irq == NULL) {
2267150719Sjhb		device_printf(dev, "couldn't map interrupt\n");
226849011Swpaul		error = ENXIO;
226945386Swpaul		goto fail;
227045386Swpaul	}
227145386Swpaul
227245386Swpaul	if (ti_chipinit(sc)) {
2273150719Sjhb		device_printf(dev, "chip initialization failed\n");
227449011Swpaul		error = ENXIO;
227545386Swpaul		goto fail;
227645386Swpaul	}
227745386Swpaul
227845386Swpaul	/* Zero out the NIC's on-board SRAM. */
2279153770Syongari	ti_mem_zero(sc, 0x2000, 0x100000 - 0x2000);
228045386Swpaul
228145386Swpaul	/* Init again -- zeroing memory may have clobbered some registers. */
228245386Swpaul	if (ti_chipinit(sc)) {
2283150719Sjhb		device_printf(dev, "chip initialization failed\n");
228449011Swpaul		error = ENXIO;
228545386Swpaul		goto fail;
228645386Swpaul	}
228745386Swpaul
228845386Swpaul	/*
228945386Swpaul	 * Get station address from the EEPROM. Note: the manual states
229045386Swpaul	 * that the MAC address is at offset 0x8c, however the data is
229145386Swpaul	 * stored as two longwords (since that's how it's loaded into
229272645Sasmodai	 * the NIC). This means the MAC address is actually preceded
229345386Swpaul	 * by two zero bytes. We need to skip over those.
229445386Swpaul	 */
2295147256Sbrooks	if (ti_read_eeprom(sc, eaddr,
229645386Swpaul				TI_EE_MAC_OFFSET + 2, ETHER_ADDR_LEN)) {
2297150719Sjhb		device_printf(dev, "failed to read station address\n");
229849011Swpaul		error = ENXIO;
229945386Swpaul		goto fail;
230045386Swpaul	}
230145386Swpaul
230245386Swpaul	/* Allocate the general information block and ring buffers. */
2303166165Smarius	if (bus_dma_tag_create(bus_get_dma_tag(dev),	/* parent */
2304143903Sscottl				1, 0,			/* algnmnt, boundary */
2305143903Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2306143903Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2307143903Sscottl				NULL, NULL,		/* filter, filterarg */
2308143903Sscottl				BUS_SPACE_MAXSIZE_32BIT,/* maxsize */
2309143903Sscottl				0,			/* nsegments */
2310143903Sscottl				BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
2311143903Sscottl				0,			/* flags */
2312143903Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2313143903Sscottl				&sc->ti_parent_dmat) != 0) {
2314150719Sjhb		device_printf(dev, "Failed to allocate parent dmat\n");
2315143903Sscottl		error = ENOMEM;
2316143903Sscottl		goto fail;
2317143903Sscottl	}
231845386Swpaul
2319143903Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2320143903Sscottl				PAGE_SIZE, 0,		/* algnmnt, boundary */
2321143903Sscottl				BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
2322143903Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2323143903Sscottl				NULL, NULL,		/* filter, filterarg */
2324143903Sscottl				sizeof(struct ti_ring_data),	/* maxsize */
2325143903Sscottl				1,			/* nsegments */
2326143903Sscottl				sizeof(struct ti_ring_data),	/* maxsegsize */
2327143903Sscottl				0,			/* flags */
2328143903Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2329143903Sscottl				&sc->ti_rdata_dmat) != 0) {
2330150719Sjhb		device_printf(dev, "Failed to allocate rdata dmat\n");
2331143903Sscottl		error = ENOMEM;
233245386Swpaul		goto fail;
233345386Swpaul	}
233445386Swpaul
2335143903Sscottl	if (bus_dmamem_alloc(sc->ti_rdata_dmat, (void**)&sc->ti_rdata,
2336219547Smarius			     BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
2337219547Smarius			     &sc->ti_rdata_dmamap) != 0) {
2338150719Sjhb		device_printf(dev, "Failed to allocate rdata memory\n");
2339143903Sscottl		error = ENOMEM;
2340143903Sscottl		goto fail;
2341143903Sscottl	}
2342143903Sscottl
2343143903Sscottl	if (bus_dmamap_load(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2344143903Sscottl			    sc->ti_rdata, sizeof(struct ti_ring_data),
2345143903Sscottl			    ti_rdata_cb, sc, BUS_DMA_NOWAIT) != 0) {
2346150719Sjhb		device_printf(dev, "Failed to load rdata segments\n");
2347143903Sscottl		error = ENOMEM;
2348143903Sscottl		goto fail;
2349143903Sscottl	}
2350143903Sscottl
235145386Swpaul	bzero(sc->ti_rdata, sizeof(struct ti_ring_data));
235245386Swpaul
235345386Swpaul	/* Try to allocate memory for jumbo buffers. */
235445386Swpaul	if (ti_alloc_jumbo_mem(sc)) {
2355150719Sjhb		device_printf(dev, "jumbo buffer allocation failed\n");
235649011Swpaul		error = ENXIO;
235745386Swpaul		goto fail;
235845386Swpaul	}
235945386Swpaul
2360153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2361153396Sscottl				1, 0,			/* algnmnt, boundary */
2362153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2363153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2364153396Sscottl				NULL, NULL,		/* filter, filterarg */
2365153396Sscottl				MCLBYTES * TI_MAXTXSEGS,/* maxsize */
2366153396Sscottl				TI_MAXTXSEGS,		/* nsegments */
2367153396Sscottl				MCLBYTES,		/* maxsegsize */
2368153396Sscottl				0,			/* flags */
2369153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2370153396Sscottl				&sc->ti_mbuftx_dmat) != 0) {
2371153396Sscottl		device_printf(dev, "Failed to allocate rdata dmat\n");
2372153396Sscottl		error = ENOMEM;
2373153396Sscottl		goto fail;
2374153396Sscottl	}
2375153396Sscottl
2376153396Sscottl	if (bus_dma_tag_create(sc->ti_parent_dmat,	/* parent */
2377153396Sscottl				1, 0,			/* algnmnt, boundary */
2378153396Sscottl				BUS_SPACE_MAXADDR,	/* lowaddr */
2379153396Sscottl				BUS_SPACE_MAXADDR,	/* highaddr */
2380153396Sscottl				NULL, NULL,		/* filter, filterarg */
2381153396Sscottl				MCLBYTES,		/* maxsize */
2382153396Sscottl				1,			/* nsegments */
2383153396Sscottl				MCLBYTES,		/* maxsegsize */
2384153396Sscottl				0,			/* flags */
2385153396Sscottl				NULL, NULL,		/* lockfunc, lockarg */
2386153396Sscottl				&sc->ti_mbufrx_dmat) != 0) {
2387153396Sscottl		device_printf(dev, "Failed to allocate rdata dmat\n");
2388153396Sscottl		error = ENOMEM;
2389153396Sscottl		goto fail;
2390153396Sscottl	}
2391153396Sscottl
2392153396Sscottl	if (ti_alloc_dmamaps(sc)) {
2393153396Sscottl		device_printf(dev, "dma map creation failed\n");
2394153396Sscottl		error = ENXIO;
2395153396Sscottl		goto fail;
2396153396Sscottl	}
2397153396Sscottl
239863699Swpaul	/*
239963699Swpaul	 * We really need a better way to tell a 1000baseTX card
240063699Swpaul	 * from a 1000baseSX one, since in theory there could be
240163699Swpaul	 * OEMed 1000baseTX cards from lame vendors who aren't
240263699Swpaul	 * clever enough to change the PCI ID. For the moment
240363699Swpaul	 * though, the AceNIC is the only copper card available.
240463699Swpaul	 */
240563699Swpaul	if (pci_get_vendor(dev) == ALT_VENDORID &&
240663699Swpaul	    pci_get_device(dev) == ALT_DEVICEID_ACENIC_COPPER)
240763699Swpaul		sc->ti_copper = 1;
240864139Swpaul	/* Ok, it's not the only copper card available. */
240964139Swpaul	if (pci_get_vendor(dev) == NG_VENDORID &&
241064139Swpaul	    pci_get_device(dev) == NG_DEVICEID_GA620T)
241164139Swpaul		sc->ti_copper = 1;
241263699Swpaul
241345386Swpaul	/* Set default tuneable values. */
241445386Swpaul	sc->ti_stat_ticks = 2 * TI_TICKS_PER_SEC;
241598849Sken#if 0
241645386Swpaul	sc->ti_rx_coal_ticks = TI_TICKS_PER_SEC / 5000;
241798849Sken#endif
241898849Sken	sc->ti_rx_coal_ticks = 170;
241945386Swpaul	sc->ti_tx_coal_ticks = TI_TICKS_PER_SEC / 500;
242045386Swpaul	sc->ti_rx_max_coal_bds = 64;
242198849Sken#if 0
242245386Swpaul	sc->ti_tx_max_coal_bds = 128;
242398849Sken#endif
242498849Sken	sc->ti_tx_max_coal_bds = 32;
242545386Swpaul	sc->ti_tx_buf_ratio = 21;
242645386Swpaul
242745386Swpaul	/* Set up ifnet structure */
242845386Swpaul	ifp->if_softc = sc;
2429121816Sbrooks	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2430153281Sscottl	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
243145386Swpaul	ifp->if_ioctl = ti_ioctl;
243245386Swpaul	ifp->if_start = ti_start;
243345386Swpaul	ifp->if_init = ti_init;
2434227092Syongari	ifp->if_baudrate = IF_Gbps(1UL);
243545386Swpaul	ifp->if_snd.ifq_maxlen = TI_TX_RING_CNT - 1;
243645386Swpaul
243745386Swpaul	/* Set up ifmedia support. */
243863699Swpaul	if (sc->ti_copper) {
243963699Swpaul		/*
244063699Swpaul		 * Copper cards allow manual 10/100 mode selection,
244163699Swpaul		 * but not manual 1000baseTX mode selection. Why?
244263699Swpaul		 * Becuase currently there's no way to specify the
244363699Swpaul		 * master/slave setting through the firmware interface,
244463699Swpaul		 * so Alteon decided to just bag it and handle it
244563699Swpaul		 * via autonegotiation.
244663699Swpaul		 */
244763699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
244863699Swpaul		ifmedia_add(&sc->ifmedia,
244963699Swpaul		    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
245063699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_TX, 0, NULL);
245163699Swpaul		ifmedia_add(&sc->ifmedia,
245263699Swpaul		    IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
245395673Sphk		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_T, 0, NULL);
245463699Swpaul		ifmedia_add(&sc->ifmedia,
245595673Sphk		    IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
245663699Swpaul	} else {
245763699Swpaul		/* Fiber cards don't support 10/100 modes. */
245863699Swpaul		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_1000_SX, 0, NULL);
245963699Swpaul		ifmedia_add(&sc->ifmedia,
246063699Swpaul		    IFM_ETHER|IFM_1000_SX|IFM_FDX, 0, NULL);
246163699Swpaul	}
246245386Swpaul	ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_AUTO, 0, NULL);
246345386Swpaul	ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_AUTO);
246445386Swpaul
246545386Swpaul	/*
246698849Sken	 * We're assuming here that card initialization is a sequential
246798849Sken	 * thing.  If it isn't, multiple cards probing at the same time
246898849Sken	 * could stomp on the list of softcs here.
246998849Sken	 */
247098849Sken
247198849Sken	/* Register the device */
247298849Sken	sc->dev = make_dev(&ti_cdevsw, sc->ti_unit, UID_ROOT, GID_OPERATOR,
247398849Sken			   0600, "ti%d", sc->ti_unit);
2474120980Sphk	sc->dev->si_drv1 = sc;
247598849Sken
247698849Sken	/*
247763090Sarchie	 * Call MI attach routine.
247845386Swpaul	 */
2479147256Sbrooks	ether_ifattach(ifp, eaddr);
248045386Swpaul
2481227095Syongari	/* VLAN capability setup. */
2482227095Syongari	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM |
2483227095Syongari	    IFCAP_VLAN_HWTAGGING;
2484227095Syongari	ifp->if_capenable = ifp->if_capabilities;
2485227095Syongari	/* Tell the upper layer we support VLAN over-sized frames. */
2486227095Syongari	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
2487227095Syongari
2488227092Syongari	/* Driver supports link state tracking. */
2489227092Syongari	ifp->if_capabilities |= IFCAP_LINKSTATE;
2490227092Syongari	ifp->if_capenable |= IFCAP_LINKSTATE;
2491227092Syongari
2492113609Snjl	/* Hook interrupt last to avoid having to lock softc */
2493153281Sscottl	error = bus_setup_intr(dev, sc->ti_irq, INTR_TYPE_NET|INTR_MPSAFE,
2494166901Spiso	   NULL, ti_intr, sc, &sc->ti_intrhand);
2495112872Snjl
2496112872Snjl	if (error) {
2497150719Sjhb		device_printf(dev, "couldn't set up irq\n");
2498112872Snjl		goto fail;
2499112872Snjl	}
2500112872Snjl
250145386Swpaulfail:
2502153770Syongari	if (error)
2503112872Snjl		ti_detach(dev);
2504112872Snjl
2505131654Sbms	return (error);
250645386Swpaul}
250745386Swpaul
250898849Sken/*
2509113609Snjl * Shutdown hardware and free up resources. This can be called any
2510113609Snjl * time after the mutex has been initialized. It is called in both
2511113609Snjl * the error case in attach and the normal detach case so it needs
2512113609Snjl * to be careful about only freeing resources that have actually been
2513113609Snjl * allocated.
2514113609Snjl */
2515102336Salfredstatic int
2516227086Syongariti_detach(device_t dev)
251749011Swpaul{
2518227087Syongari	struct ti_softc *sc;
2519227087Syongari	struct ifnet *ifp;
252049011Swpaul
252149011Swpaul	sc = device_get_softc(dev);
2522144407Sscottl	if (sc->dev)
2523144407Sscottl		destroy_dev(sc->dev);
2524112930Sphk	KASSERT(mtx_initialized(&sc->ti_mtx), ("ti mutex not initialized"));
2525147256Sbrooks	ifp = sc->ti_ifp;
2526199559Sjhb	if (device_is_attached(dev)) {
2527199559Sjhb		ether_ifdetach(ifp);
2528199559Sjhb		TI_LOCK(sc);
2529153770Syongari		ti_stop(sc);
2530199559Sjhb		TI_UNLOCK(sc);
2531199559Sjhb	}
253249011Swpaul
2533113609Snjl	/* These should only be active if attach succeeded */
2534199559Sjhb	callout_drain(&sc->ti_watchdog);
2535199559Sjhb	bus_generic_detach(dev);
2536153770Syongari	ti_free_dmamaps(sc);
2537113609Snjl	ifmedia_removeall(&sc->ifmedia);
253849011Swpaul
2539153288Sscottl#ifdef TI_PRIVATE_JUMBOS
2540153288Sscottl	if (sc->ti_cdata.ti_jumbo_buf)
2541153288Sscottl		bus_dmamem_free(sc->ti_jumbo_dmat, sc->ti_cdata.ti_jumbo_buf,
2542153288Sscottl		    sc->ti_jumbo_dmamap);
2543153396Sscottl#endif
2544153288Sscottl	if (sc->ti_jumbo_dmat)
2545153288Sscottl		bus_dma_tag_destroy(sc->ti_jumbo_dmat);
2546153396Sscottl	if (sc->ti_mbuftx_dmat)
2547153396Sscottl		bus_dma_tag_destroy(sc->ti_mbuftx_dmat);
2548153396Sscottl	if (sc->ti_mbufrx_dmat)
2549153396Sscottl		bus_dma_tag_destroy(sc->ti_mbufrx_dmat);
2550227091Syongari	if (sc->ti_rdata && sc->ti_rdata_dmamap)
2551227091Syongari		bus_dmamap_unload(sc->ti_rdata_dmat, sc->ti_rdata_dmamap);
2552143903Sscottl	if (sc->ti_rdata)
2553143903Sscottl		bus_dmamem_free(sc->ti_rdata_dmat, sc->ti_rdata,
2554143903Sscottl				sc->ti_rdata_dmamap);
2555143903Sscottl	if (sc->ti_rdata_dmat)
2556143903Sscottl		bus_dma_tag_destroy(sc->ti_rdata_dmat);
2557143903Sscottl	if (sc->ti_parent_dmat)
2558143903Sscottl		bus_dma_tag_destroy(sc->ti_parent_dmat);
2559112872Snjl	if (sc->ti_intrhand)
2560112872Snjl		bus_teardown_intr(dev, sc->ti_irq, sc->ti_intrhand);
2561112872Snjl	if (sc->ti_irq)
2562112872Snjl		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ti_irq);
2563112872Snjl	if (sc->ti_res) {
2564112872Snjl		bus_release_resource(dev, SYS_RES_MEMORY, TI_PCI_LOMEM,
2565112872Snjl		    sc->ti_res);
2566112872Snjl	}
2567151297Sru	if (ifp)
2568151297Sru		if_free(ifp);
256949011Swpaul
257067087Swpaul	mtx_destroy(&sc->ti_mtx);
257149011Swpaul
2572131654Sbms	return (0);
257349011Swpaul}
257449011Swpaul
257598849Sken#ifdef TI_JUMBO_HDRSPLIT
257645386Swpaul/*
257798849Sken * If hdr_len is 0, that means that header splitting wasn't done on
257898849Sken * this packet for some reason.  The two most likely reasons are that
257998849Sken * the protocol isn't a supported protocol for splitting, or this
258098849Sken * packet had a fragment offset that wasn't 0.
258198849Sken *
258298849Sken * The header length, if it is non-zero, will always be the length of
258398849Sken * the headers on the packet, but that length could be longer than the
258498849Sken * first mbuf.  So we take the minimum of the two as the actual
2585131652Sbms * length.
258698849Sken */
258798849Skenstatic __inline void
258898849Skenti_hdr_split(struct mbuf *top, int hdr_len, int pkt_len, int idx)
258998849Sken{
259098849Sken	int i = 0;
259198849Sken	int lengths[4] = {0, 0, 0, 0};
259298849Sken	struct mbuf *m, *mp;
259398849Sken
259498849Sken	if (hdr_len != 0)
259598849Sken		top->m_len = min(hdr_len, top->m_len);
259698849Sken	pkt_len -= top->m_len;
259798849Sken	lengths[i++] = top->m_len;
259898849Sken
259998849Sken	mp = top;
260098849Sken	for (m = top->m_next; m && pkt_len; m = m->m_next) {
260198849Sken		m->m_len = m->m_ext.ext_size = min(m->m_len, pkt_len);
260298849Sken		pkt_len -= m->m_len;
260398849Sken		lengths[i++] = m->m_len;
260498849Sken		mp = m;
260598849Sken	}
260698849Sken
260798849Sken#if 0
260898849Sken	if (hdr_len != 0)
260998849Sken		printf("got split packet: ");
261098849Sken	else
261198849Sken		printf("got non-split packet: ");
2612131652Sbms
261398849Sken	printf("%d,%d,%d,%d = %d\n", lengths[0],
261498849Sken	    lengths[1], lengths[2], lengths[3],
261598849Sken	    lengths[0] + lengths[1] + lengths[2] +
261698849Sken	    lengths[3]);
261798849Sken#endif
261898849Sken
261998849Sken	if (pkt_len)
262098849Sken		panic("header splitting didn't");
2621131652Sbms
262298849Sken	if (m) {
262398849Sken		m_freem(m);
262498849Sken		mp->m_next = NULL;
262598849Sken
262698849Sken	}
262798849Sken	if (mp->m_next != NULL)
262898849Sken		panic("ti_hdr_split: last mbuf in chain should be null");
262998849Sken}
263098849Sken#endif /* TI_JUMBO_HDRSPLIT */
263198849Sken
263298849Sken/*
263345386Swpaul * Frame reception handling. This is called if there's a frame
263445386Swpaul * on the receive return list.
263545386Swpaul *
263645386Swpaul * Note: we have to be able to handle three possibilities here:
263745386Swpaul * 1) the frame is from the mini receive ring (can only happen)
263845386Swpaul *    on Tigon 2 boards)
263945386Swpaul * 2) the frame is from the jumbo recieve ring
264045386Swpaul * 3) the frame is from the standard receive ring
264145386Swpaul */
264245386Swpaul
2643102336Salfredstatic void
2644227086Syongariti_rxeof(struct ti_softc *sc)
264545386Swpaul{
2646227087Syongari	struct ifnet *ifp;
2647227087Syongari	bus_dmamap_t map;
2648227087Syongari	struct ti_cmd_desc cmd;
264945386Swpaul
2650122689Ssam	TI_LOCK_ASSERT(sc);
2651122689Ssam
2652147256Sbrooks	ifp = sc->ti_ifp;
265345386Swpaul
2654131654Sbms	while (sc->ti_rx_saved_considx != sc->ti_return_prodidx.ti_idx) {
2655227087Syongari		struct ti_rx_desc *cur_rx;
2656227087Syongari		struct mbuf *m = NULL;
2657227089Syongari		uint32_t rxidx;
2658227089Syongari		uint16_t vlan_tag = 0;
2659227087Syongari		int have_tag = 0;
266045386Swpaul
266145386Swpaul		cur_rx =
266245386Swpaul		    &sc->ti_rdata->ti_rx_return_ring[sc->ti_rx_saved_considx];
266345386Swpaul		rxidx = cur_rx->ti_idx;
266445386Swpaul		TI_INC(sc->ti_rx_saved_considx, TI_RETURN_RING_CNT);
266545386Swpaul
266645386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_VLAN_TAG) {
266745386Swpaul			have_tag = 1;
2668227094Syongari			vlan_tag = cur_rx->ti_vlan_tag;
266945386Swpaul		}
267045386Swpaul
267145386Swpaul		if (cur_rx->ti_flags & TI_BDFLAG_JUMBO_RING) {
267298849Sken
267345386Swpaul			TI_INC(sc->ti_jumbo, TI_JUMBO_RX_RING_CNT);
267445386Swpaul			m = sc->ti_cdata.ti_rx_jumbo_chain[rxidx];
267545386Swpaul			sc->ti_cdata.ti_rx_jumbo_chain[rxidx] = NULL;
2676153396Sscottl			map = sc->ti_cdata.ti_rx_jumbo_maps[rxidx];
2677153396Sscottl			bus_dmamap_sync(sc->ti_jumbo_dmat, map,
2678153396Sscottl			    BUS_DMASYNC_POSTREAD);
2679153396Sscottl			bus_dmamap_unload(sc->ti_jumbo_dmat, map);
268045386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
268145386Swpaul				ifp->if_ierrors++;
268245386Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
268345386Swpaul				continue;
268445386Swpaul			}
268548597Swpaul			if (ti_newbuf_jumbo(sc, sc->ti_jumbo, NULL) == ENOBUFS) {
268648597Swpaul				ifp->if_ierrors++;
268748597Swpaul				ti_newbuf_jumbo(sc, sc->ti_jumbo, m);
268848597Swpaul				continue;
268948597Swpaul			}
269098849Sken#ifdef TI_PRIVATE_JUMBOS
2691131655Sbms			m->m_len = cur_rx->ti_len;
269298849Sken#else /* TI_PRIVATE_JUMBOS */
269398849Sken#ifdef TI_JUMBO_HDRSPLIT
269498849Sken			if (sc->ti_hdrsplit)
269598849Sken				ti_hdr_split(m, TI_HOSTADDR(cur_rx->ti_addr),
269698849Sken					     cur_rx->ti_len, rxidx);
269798849Sken			else
269898849Sken#endif /* TI_JUMBO_HDRSPLIT */
2699131655Sbms			m_adj(m, cur_rx->ti_len - m->m_pkthdr.len);
270098849Sken#endif /* TI_PRIVATE_JUMBOS */
270145386Swpaul		} else if (cur_rx->ti_flags & TI_BDFLAG_MINI_RING) {
270245386Swpaul			TI_INC(sc->ti_mini, TI_MINI_RX_RING_CNT);
270345386Swpaul			m = sc->ti_cdata.ti_rx_mini_chain[rxidx];
270445386Swpaul			sc->ti_cdata.ti_rx_mini_chain[rxidx] = NULL;
2705153396Sscottl			map = sc->ti_cdata.ti_rx_mini_maps[rxidx];
2706153396Sscottl			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
2707153396Sscottl			    BUS_DMASYNC_POSTREAD);
2708153396Sscottl			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
270945386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
271045386Swpaul				ifp->if_ierrors++;
271145386Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
271245386Swpaul				continue;
271345386Swpaul			}
271448597Swpaul			if (ti_newbuf_mini(sc, sc->ti_mini, NULL) == ENOBUFS) {
271548597Swpaul				ifp->if_ierrors++;
271648597Swpaul				ti_newbuf_mini(sc, sc->ti_mini, m);
271748597Swpaul				continue;
271848597Swpaul			}
271998849Sken			m->m_len = cur_rx->ti_len;
272045386Swpaul		} else {
272145386Swpaul			TI_INC(sc->ti_std, TI_STD_RX_RING_CNT);
272245386Swpaul			m = sc->ti_cdata.ti_rx_std_chain[rxidx];
272345386Swpaul			sc->ti_cdata.ti_rx_std_chain[rxidx] = NULL;
2724153396Sscottl			map = sc->ti_cdata.ti_rx_std_maps[rxidx];
2725153396Sscottl			bus_dmamap_sync(sc->ti_mbufrx_dmat, map,
2726153396Sscottl			    BUS_DMASYNC_POSTREAD);
2727153396Sscottl			bus_dmamap_unload(sc->ti_mbufrx_dmat, map);
272845386Swpaul			if (cur_rx->ti_flags & TI_BDFLAG_ERROR) {
272945386Swpaul				ifp->if_ierrors++;
273045386Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
273145386Swpaul				continue;
273245386Swpaul			}
273348597Swpaul			if (ti_newbuf_std(sc, sc->ti_std, NULL) == ENOBUFS) {
273448597Swpaul				ifp->if_ierrors++;
273548597Swpaul				ti_newbuf_std(sc, sc->ti_std, m);
273648597Swpaul				continue;
273748597Swpaul			}
273898849Sken			m->m_len = cur_rx->ti_len;
273945386Swpaul		}
274045386Swpaul
274198849Sken		m->m_pkthdr.len = cur_rx->ti_len;
274245386Swpaul		ifp->if_ipackets++;
274345386Swpaul		m->m_pkthdr.rcvif = ifp;
274445386Swpaul
2745227095Syongari		if (ifp->if_capenable & IFCAP_RXCSUM) {
2746227095Syongari			if (cur_rx->ti_flags & TI_BDFLAG_IP_CKSUM) {
2747227095Syongari				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2748227095Syongari				if ((cur_rx->ti_ip_cksum ^ 0xffff) == 0)
2749227095Syongari					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2750227095Syongari			}
2751227095Syongari			if (cur_rx->ti_flags & TI_BDFLAG_TCP_UDP_CKSUM) {
2752227095Syongari				m->m_pkthdr.csum_data =
2753227095Syongari				    cur_rx->ti_tcp_udp_cksum;
2754227095Syongari				m->m_pkthdr.csum_flags |= CSUM_DATA_VALID;
2755227095Syongari			}
275658698Sjlemon		}
275745386Swpaul
275845386Swpaul		/*
2759106936Ssam		 * If we received a packet with a vlan tag,
2760106936Ssam		 * tag it before passing the packet upward.
276145386Swpaul		 */
2762153512Sglebius		if (have_tag) {
2763162375Sandre			m->m_pkthdr.ether_vtag = vlan_tag;
2764162375Sandre			m->m_flags |= M_VLANTAG;
2765153512Sglebius		}
2766122689Ssam		TI_UNLOCK(sc);
2767106936Ssam		(*ifp->if_input)(ifp, m);
2768122689Ssam		TI_LOCK(sc);
276945386Swpaul	}
277045386Swpaul
277145386Swpaul	/* Only necessary on the Tigon 1. */
277245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON)
277345386Swpaul		CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX,
277445386Swpaul		    sc->ti_rx_saved_considx);
277545386Swpaul
277648597Swpaul	TI_UPDATE_STDPROD(sc, sc->ti_std);
277748597Swpaul	TI_UPDATE_MINIPROD(sc, sc->ti_mini);
277848597Swpaul	TI_UPDATE_JUMBOPROD(sc, sc->ti_jumbo);
277945386Swpaul}
278045386Swpaul
2781102336Salfredstatic void
2782227086Syongariti_txeof(struct ti_softc *sc)
278345386Swpaul{
2784227087Syongari	struct ti_txdesc *txd;
2785227087Syongari	struct ti_tx_desc txdesc;
2786227087Syongari	struct ti_tx_desc *cur_tx = NULL;
2787227087Syongari	struct ifnet *ifp;
2788227087Syongari	int idx;
278945386Swpaul
2790147256Sbrooks	ifp = sc->ti_ifp;
279145386Swpaul
2792153982Syongari	txd = STAILQ_FIRST(&sc->ti_cdata.ti_txbusyq);
2793153982Syongari	if (txd == NULL)
2794153982Syongari		return;
279545386Swpaul	/*
279645386Swpaul	 * Go through our tx ring and free mbufs for those
279745386Swpaul	 * frames that have been sent.
279845386Swpaul	 */
2799153982Syongari	for (idx = sc->ti_tx_saved_considx; idx != sc->ti_tx_considx.ti_idx;
2800153982Syongari	    TI_INC(idx, TI_TX_RING_CNT)) {
280145386Swpaul		if (sc->ti_hwrev == TI_HWREV_TIGON) {
2802153770Syongari			ti_mem_read(sc, TI_TX_RING_BASE + idx * sizeof(txdesc),
2803153770Syongari			    sizeof(txdesc), &txdesc);
2804153770Syongari			cur_tx = &txdesc;
280545386Swpaul		} else
280645386Swpaul			cur_tx = &sc->ti_rdata->ti_tx_ring[idx];
280748011Swpaul		sc->ti_txcnt--;
2808153982Syongari		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2809153982Syongari		if ((cur_tx->ti_flags & TI_BDFLAG_END) == 0)
2810153982Syongari			continue;
2811153982Syongari		bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2812153982Syongari		    BUS_DMASYNC_POSTWRITE);
2813153982Syongari		bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
2814153982Syongari
2815153982Syongari		ifp->if_opackets++;
2816153982Syongari		m_freem(txd->tx_m);
2817153982Syongari		txd->tx_m = NULL;
2818153982Syongari		STAILQ_REMOVE_HEAD(&sc->ti_cdata.ti_txbusyq, tx_q);
2819153982Syongari		STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txfreeq, txd, tx_q);
2820153982Syongari		txd = STAILQ_FIRST(&sc->ti_cdata.ti_txbusyq);
282145386Swpaul	}
2822153982Syongari	sc->ti_tx_saved_considx = idx;
282345386Swpaul
2824199559Sjhb	sc->ti_timer = sc->ti_txcnt > 0 ? 5 : 0;
282545386Swpaul}
282645386Swpaul
2827102336Salfredstatic void
2828227086Syongariti_intr(void *xsc)
282945386Swpaul{
2830227087Syongari	struct ti_softc *sc;
2831227087Syongari	struct ifnet *ifp;
283245386Swpaul
283345386Swpaul	sc = xsc;
283467087Swpaul	TI_LOCK(sc);
2835147256Sbrooks	ifp = sc->ti_ifp;
283645386Swpaul
283798849Sken/*#ifdef notdef*/
283845386Swpaul	/* Avoid this for now -- checking this register is expensive. */
283945386Swpaul	/* Make sure this is really our interrupt. */
284067087Swpaul	if (!(CSR_READ_4(sc, TI_MISC_HOST_CTL) & TI_MHC_INTSTATE)) {
284167087Swpaul		TI_UNLOCK(sc);
284245386Swpaul		return;
284367087Swpaul	}
284498849Sken/*#endif*/
284545386Swpaul
284645386Swpaul	/* Ack interrupt and stop others from occuring. */
284745386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
284845386Swpaul
2849148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
285045386Swpaul		/* Check RX return ring producer/consumer */
285145386Swpaul		ti_rxeof(sc);
285245386Swpaul
285345386Swpaul		/* Check TX ring producer/consumer */
285445386Swpaul		ti_txeof(sc);
285545386Swpaul	}
285645386Swpaul
285745386Swpaul	ti_handle_events(sc);
285845386Swpaul
2859148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
2860227098Syongari	    ifp->if_snd.ifq_head != NULL) {
2861227098Syongari		/* Re-enable interrupts. */
2862227098Syongari		CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
2863153770Syongari		ti_start_locked(ifp);
2864227098Syongari	}
286545386Swpaul
286667087Swpaul	TI_UNLOCK(sc);
286745386Swpaul}
286845386Swpaul
2869102336Salfredstatic void
2870227086Syongariti_stats_update(struct ti_softc *sc)
287145386Swpaul{
2872227087Syongari	struct ifnet *ifp;
287345386Swpaul
2874147256Sbrooks	ifp = sc->ti_ifp;
287545386Swpaul
2876153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2877153770Syongari	    BUS_DMASYNC_POSTREAD);
2878153770Syongari
287945386Swpaul	ifp->if_collisions +=
288045386Swpaul	   (sc->ti_rdata->ti_info.ti_stats.dot3StatsSingleCollisionFrames +
288145386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsMultipleCollisionFrames +
288245386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsExcessiveCollisions +
288345386Swpaul	   sc->ti_rdata->ti_info.ti_stats.dot3StatsLateCollisions) -
288445386Swpaul	   ifp->if_collisions;
2885153770Syongari
2886153770Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2887153770Syongari	    BUS_DMASYNC_PREREAD);
288845386Swpaul}
288945386Swpaul
2890153982Syongari/*
2891153982Syongari * Encapsulate an mbuf chain in the tx ring  by coupling the mbuf data
2892153982Syongari * pointers to descriptors.
2893153982Syongari */
2894153982Syongaristatic int
2895227086Syongariti_encap(struct ti_softc *sc, struct mbuf **m_head)
2896153396Sscottl{
2897227087Syongari	struct ti_txdesc *txd;
2898227087Syongari	struct ti_tx_desc *f;
2899227087Syongari	struct ti_tx_desc txdesc;
2900227087Syongari	struct mbuf *m;
2901227087Syongari	bus_dma_segment_t txsegs[TI_MAXTXSEGS];
2902227089Syongari	uint16_t csum_flags;
2903227087Syongari	int error, frag, i, nseg;
2904153396Sscottl
2905153982Syongari	if ((txd = STAILQ_FIRST(&sc->ti_cdata.ti_txfreeq)) == NULL)
2906153982Syongari		return (ENOBUFS);
2907153396Sscottl
2908153982Syongari	error = bus_dmamap_load_mbuf_sg(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2909161236Syongari	    *m_head, txsegs, &nseg, 0);
2910153982Syongari	if (error == EFBIG) {
2911161236Syongari		m = m_defrag(*m_head, M_DONTWAIT);
2912161236Syongari		if (m == NULL) {
2913161236Syongari			m_freem(*m_head);
2914161236Syongari			*m_head = NULL;
2915153982Syongari			return (ENOMEM);
2916153982Syongari		}
2917161236Syongari		*m_head = m;
2918153982Syongari		error = bus_dmamap_load_mbuf_sg(sc->ti_mbuftx_dmat,
2919161236Syongari		    txd->tx_dmamap, *m_head, txsegs, &nseg, 0);
2920153982Syongari		if (error) {
2921161236Syongari			m_freem(*m_head);
2922161236Syongari			*m_head = NULL;
2923153982Syongari			return (error);
2924153982Syongari		}
2925153982Syongari	} else if (error != 0)
2926153982Syongari		return (error);
2927153982Syongari	if (nseg == 0) {
2928161236Syongari		m_freem(*m_head);
2929161236Syongari		*m_head = NULL;
2930153982Syongari		return (EIO);
2931153776Sscottl	}
2932153776Sscottl
2933153982Syongari	if (sc->ti_txcnt + nseg >= TI_TX_RING_CNT) {
2934153982Syongari		bus_dmamap_unload(sc->ti_mbuftx_dmat, txd->tx_dmamap);
2935153982Syongari		return (ENOBUFS);
2936153982Syongari	}
2937153982Syongari
2938161236Syongari	m = *m_head;
2939161236Syongari	csum_flags = 0;
2940161236Syongari	if (m->m_pkthdr.csum_flags) {
2941161236Syongari		if (m->m_pkthdr.csum_flags & CSUM_IP)
2942161236Syongari			csum_flags |= TI_BDFLAG_IP_CKSUM;
2943161236Syongari		if (m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP))
2944161236Syongari			csum_flags |= TI_BDFLAG_TCP_UDP_CKSUM;
2945161236Syongari		if (m->m_flags & M_LASTFRAG)
2946161236Syongari			csum_flags |= TI_BDFLAG_IP_FRAG_END;
2947161236Syongari		else if (m->m_flags & M_FRAG)
2948161236Syongari			csum_flags |= TI_BDFLAG_IP_FRAG;
2949161236Syongari	}
2950161236Syongari
2951153982Syongari	bus_dmamap_sync(sc->ti_mbuftx_dmat, txd->tx_dmamap,
2952153982Syongari	    BUS_DMASYNC_PREWRITE);
2953153982Syongari	bus_dmamap_sync(sc->ti_rdata_dmat, sc->ti_rdata_dmamap,
2954153982Syongari	    BUS_DMASYNC_PREWRITE);
2955153982Syongari
2956153982Syongari	frag = sc->ti_tx_saved_prodidx;
2957153982Syongari	for (i = 0; i < nseg; i++) {
2958153396Sscottl		if (sc->ti_hwrev == TI_HWREV_TIGON) {
2959153770Syongari			bzero(&txdesc, sizeof(txdesc));
2960153770Syongari			f = &txdesc;
2961153396Sscottl		} else
2962153396Sscottl			f = &sc->ti_rdata->ti_tx_ring[frag];
2963153982Syongari		ti_hostaddr64(&f->ti_addr, txsegs[i].ds_addr);
2964153982Syongari		f->ti_len = txsegs[i].ds_len;
2965153396Sscottl		f->ti_flags = csum_flags;
2966162375Sandre		if (m->m_flags & M_VLANTAG) {
2967153396Sscottl			f->ti_flags |= TI_BDFLAG_VLAN_TAG;
2968227094Syongari			f->ti_vlan_tag = m->m_pkthdr.ether_vtag;
2969153396Sscottl		} else {
2970153396Sscottl			f->ti_vlan_tag = 0;
2971153396Sscottl		}
2972153396Sscottl
2973153770Syongari		if (sc->ti_hwrev == TI_HWREV_TIGON)
2974153770Syongari			ti_mem_write(sc, TI_TX_RING_BASE + frag *
2975153770Syongari			    sizeof(txdesc), sizeof(txdesc), &txdesc);
2976153396Sscottl		TI_INC(frag, TI_TX_RING_CNT);
2977153396Sscottl	}
2978153396Sscottl
2979153982Syongari	sc->ti_tx_saved_prodidx = frag;
2980153982Syongari	/* set TI_BDFLAG_END on the last descriptor */
2981153982Syongari	frag = (frag + TI_TX_RING_CNT - 1) % TI_TX_RING_CNT;
2982153770Syongari	if (sc->ti_hwrev == TI_HWREV_TIGON) {
2983153770Syongari		txdesc.ti_flags |= TI_BDFLAG_END;
2984153982Syongari		ti_mem_write(sc, TI_TX_RING_BASE + frag * sizeof(txdesc),
2985153770Syongari		    sizeof(txdesc), &txdesc);
2986153770Syongari	} else
2987153982Syongari		sc->ti_rdata->ti_tx_ring[frag].ti_flags |= TI_BDFLAG_END;
2988153396Sscottl
2989153982Syongari	STAILQ_REMOVE_HEAD(&sc->ti_cdata.ti_txfreeq, tx_q);
2990153982Syongari	STAILQ_INSERT_TAIL(&sc->ti_cdata.ti_txbusyq, txd, tx_q);
2991153982Syongari	txd->tx_m = m;
2992153982Syongari	sc->ti_txcnt += nseg;
299345386Swpaul
2994131654Sbms	return (0);
299545386Swpaul}
299645386Swpaul
2997153770Syongaristatic void
2998227086Syongariti_start(struct ifnet *ifp)
2999153770Syongari{
3000227087Syongari	struct ti_softc *sc;
3001153770Syongari
3002153770Syongari	sc = ifp->if_softc;
3003153770Syongari	TI_LOCK(sc);
3004153770Syongari	ti_start_locked(ifp);
3005153770Syongari	TI_UNLOCK(sc);
3006153770Syongari}
3007153770Syongari
300845386Swpaul/*
300945386Swpaul * Main transmit routine. To avoid having to do mbuf copies, we put pointers
301045386Swpaul * to the mbuf data regions directly in the transmit descriptors.
301145386Swpaul */
3012102336Salfredstatic void
3013227086Syongariti_start_locked(struct ifnet *ifp)
301445386Swpaul{
3015227087Syongari	struct ti_softc *sc;
3016227087Syongari	struct mbuf *m_head = NULL;
3017227087Syongari	int enq = 0;
301845386Swpaul
301945386Swpaul	sc = ifp->if_softc;
302045386Swpaul
3021153982Syongari	for (; ifp->if_snd.ifq_head != NULL &&
3022153982Syongari	    sc->ti_txcnt < (TI_TX_RING_CNT - 16);) {
302345386Swpaul		IF_DEQUEUE(&ifp->if_snd, m_head);
302445386Swpaul		if (m_head == NULL)
302545386Swpaul			break;
302645386Swpaul
302745386Swpaul		/*
302858698Sjlemon		 * XXX
302958698Sjlemon		 * safety overkill.  If this is a fragmented packet chain
303058698Sjlemon		 * with delayed TCP/UDP checksums, then only encapsulate
303158698Sjlemon		 * it if we have enough descriptors to handle the entire
303258698Sjlemon		 * chain at once.
303358698Sjlemon		 * (paranoia -- may not actually be needed)
303458698Sjlemon		 */
303558698Sjlemon		if (m_head->m_flags & M_FIRSTFRAG &&
303658698Sjlemon		    m_head->m_pkthdr.csum_flags & (CSUM_DELAY_DATA)) {
303758698Sjlemon			if ((TI_TX_RING_CNT - sc->ti_txcnt) <
303858698Sjlemon			    m_head->m_pkthdr.csum_data + 16) {
303958698Sjlemon				IF_PREPEND(&ifp->if_snd, m_head);
3040148887Srwatson				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
304158698Sjlemon				break;
304258698Sjlemon			}
304358698Sjlemon		}
304458698Sjlemon
304558698Sjlemon		/*
304645386Swpaul		 * Pack the data into the transmit ring. If we
304745386Swpaul		 * don't have room, set the OACTIVE flag and wait
304845386Swpaul		 * for the NIC to drain the ring.
304945386Swpaul		 */
3050153982Syongari		if (ti_encap(sc, &m_head)) {
3051153982Syongari			if (m_head == NULL)
3052153982Syongari				break;
305345386Swpaul			IF_PREPEND(&ifp->if_snd, m_head);
3054148887Srwatson			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
305545386Swpaul			break;
305645386Swpaul		}
305745386Swpaul
3058153982Syongari		enq++;
305945386Swpaul		/*
306045386Swpaul		 * If there's a BPF listener, bounce a copy of this frame
306145386Swpaul		 * to him.
306245386Swpaul		 */
3063167190Scsjp		ETHER_BPF_MTAP(ifp, m_head);
306445386Swpaul	}
306545386Swpaul
3066153982Syongari	if (enq > 0) {
3067153982Syongari		/* Transmit */
3068153982Syongari		CSR_WRITE_4(sc, TI_MB_SENDPROD_IDX, sc->ti_tx_saved_prodidx);
306945386Swpaul
3070153982Syongari		/*
3071153982Syongari		 * Set a timeout in case the chip goes out to lunch.
3072153982Syongari		 */
3073199559Sjhb		sc->ti_timer = 5;
3074153982Syongari	}
307545386Swpaul}
307645386Swpaul
3077102336Salfredstatic void
3078227086Syongariti_init(void *xsc)
307945386Swpaul{
3080227087Syongari	struct ti_softc *sc;
3081153770Syongari
3082153770Syongari	sc = xsc;
3083153770Syongari	TI_LOCK(sc);
3084153770Syongari	ti_init_locked(sc);
3085153770Syongari	TI_UNLOCK(sc);
3086153770Syongari}
3087153770Syongari
3088153770Syongaristatic void
3089227086Syongariti_init_locked(void *xsc)
3090153770Syongari{
3091227087Syongari	struct ti_softc *sc = xsc;
309245386Swpaul
309345386Swpaul	/* Cancel pending I/O and flush buffers. */
309445386Swpaul	ti_stop(sc);
309545386Swpaul
309645386Swpaul	/* Init the gen info block, ring control blocks and firmware. */
309745386Swpaul	if (ti_gibinit(sc)) {
3098162321Sglebius		device_printf(sc->ti_dev, "initialization failure\n");
309945386Swpaul		return;
310045386Swpaul	}
310145386Swpaul}
310245386Swpaul
3103227086Syongaristatic void ti_init2(struct ti_softc *sc)
310445386Swpaul{
3105227087Syongari	struct ti_cmd_desc cmd;
3106227087Syongari	struct ifnet *ifp;
3107227089Syongari	uint8_t *ea;
3108227087Syongari	struct ifmedia *ifm;
3109227087Syongari	int tmp;
311045386Swpaul
3111153770Syongari	TI_LOCK_ASSERT(sc);
3112153770Syongari
3113147256Sbrooks	ifp = sc->ti_ifp;
311445386Swpaul
311545386Swpaul	/* Specify MTU and interface index. */
3116121816Sbrooks	CSR_WRITE_4(sc, TI_GCR_IFINDEX, sc->ti_unit);
311745386Swpaul	CSR_WRITE_4(sc, TI_GCR_IFMTU, ifp->if_mtu +
3118118454Ssimokawa	    ETHER_HDR_LEN + ETHER_CRC_LEN + ETHER_VLAN_ENCAP_LEN);
311945386Swpaul	TI_DO_CMD(TI_CMD_UPDATE_GENCOM, 0, 0);
312045386Swpaul
312145386Swpaul	/* Load our MAC address. */
3122153770Syongari	ea = IF_LLADDR(sc->ti_ifp);
3123153770Syongari	CSR_WRITE_4(sc, TI_GCR_PAR0, (ea[0] << 8) | ea[1]);
3124153770Syongari	CSR_WRITE_4(sc, TI_GCR_PAR1,
3125153770Syongari	    (ea[2] << 24) | (ea[3] << 16) | (ea[4] << 8) | ea[5]);
312645386Swpaul	TI_DO_CMD(TI_CMD_SET_MAC_ADDR, 0, 0);
312745386Swpaul
312845386Swpaul	/* Enable or disable promiscuous mode as needed. */
312945386Swpaul	if (ifp->if_flags & IFF_PROMISC) {
313045386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_ENB, 0);
313145386Swpaul	} else {
313245386Swpaul		TI_DO_CMD(TI_CMD_SET_PROMISC_MODE, TI_CMD_CODE_PROMISC_DIS, 0);
313345386Swpaul	}
313445386Swpaul
313545386Swpaul	/* Program multicast filter. */
313645386Swpaul	ti_setmulti(sc);
313745386Swpaul
313845386Swpaul	/*
313945386Swpaul	 * If this is a Tigon 1, we should tell the
314045386Swpaul	 * firmware to use software packet filtering.
314145386Swpaul	 */
314245386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON) {
314345386Swpaul		TI_DO_CMD(TI_CMD_FDR_FILTERING, TI_CMD_CODE_FILT_ENB, 0);
314445386Swpaul	}
314545386Swpaul
314645386Swpaul	/* Init RX ring. */
314745386Swpaul	ti_init_rx_ring_std(sc);
314845386Swpaul
314945386Swpaul	/* Init jumbo RX ring. */
315045386Swpaul	if (ifp->if_mtu > (ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN))
315145386Swpaul		ti_init_rx_ring_jumbo(sc);
315245386Swpaul
315345386Swpaul	/*
315445386Swpaul	 * If this is a Tigon 2, we can also configure the
315545386Swpaul	 * mini ring.
315645386Swpaul	 */
315745386Swpaul	if (sc->ti_hwrev == TI_HWREV_TIGON_II)
315845386Swpaul		ti_init_rx_ring_mini(sc);
315945386Swpaul
316045386Swpaul	CSR_WRITE_4(sc, TI_GCR_RXRETURNCONS_IDX, 0);
316145386Swpaul	sc->ti_rx_saved_considx = 0;
316245386Swpaul
316345386Swpaul	/* Init TX ring. */
316445386Swpaul	ti_init_tx_ring(sc);
316545386Swpaul
316645386Swpaul	/* Tell firmware we're alive. */
316745386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_UP, 0);
316845386Swpaul
316945386Swpaul	/* Enable host interrupts. */
317045386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 0);
317145386Swpaul
3172148887Srwatson	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3173148887Srwatson	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3174199559Sjhb	callout_reset(&sc->ti_watchdog, hz, ti_watchdog, sc);
317545386Swpaul
317645386Swpaul	/*
317745386Swpaul	 * Make sure to set media properly. We have to do this
317845386Swpaul	 * here since we have to issue commands in order to set
317945386Swpaul	 * the link negotiation and we can't issue commands until
318045386Swpaul	 * the firmware is running.
318145386Swpaul	 */
318245386Swpaul	ifm = &sc->ifmedia;
318345386Swpaul	tmp = ifm->ifm_media;
318445386Swpaul	ifm->ifm_media = ifm->ifm_cur->ifm_media;
3185227093Syongari	ti_ifmedia_upd_locked(sc);
318645386Swpaul	ifm->ifm_media = tmp;
318745386Swpaul}
318845386Swpaul
318945386Swpaul/*
319045386Swpaul * Set media options.
319145386Swpaul */
3192102336Salfredstatic int
3193227086Syongariti_ifmedia_upd(struct ifnet *ifp)
319445386Swpaul{
3195227087Syongari	struct ti_softc *sc;
3196227093Syongari	int error;
3197227093Syongari
3198227093Syongari	sc = ifp->if_softc;
3199227093Syongari	TI_LOCK(sc);
3200227093Syongari	error = ti_ifmedia_upd(ifp);
3201227093Syongari	TI_UNLOCK(sc);
3202227093Syongari
3203227093Syongari	return (error);
3204227093Syongari}
3205227093Syongari
3206227093Syongaristatic int
3207227093Syongariti_ifmedia_upd_locked(struct ti_softc *sc)
3208227093Syongari{
3209227087Syongari	struct ifmedia *ifm;
3210227087Syongari	struct ti_cmd_desc cmd;
3211227089Syongari	uint32_t flowctl;
321245386Swpaul
321345386Swpaul	ifm = &sc->ifmedia;
321445386Swpaul
321545386Swpaul	if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
3216131654Sbms		return (EINVAL);
321745386Swpaul
321898849Sken	flowctl = 0;
321998849Sken
3220131654Sbms	switch (IFM_SUBTYPE(ifm->ifm_media)) {
322145386Swpaul	case IFM_AUTO:
322298849Sken		/*
322398849Sken		 * Transmit flow control doesn't work on the Tigon 1.
322498849Sken		 */
322598849Sken		flowctl = TI_GLNK_RX_FLOWCTL_Y;
322698849Sken
322798849Sken		/*
322898849Sken		 * Transmit flow control can also cause problems on the
322998849Sken		 * Tigon 2, apparantly with both the copper and fiber
323098849Sken		 * boards.  The symptom is that the interface will just
323198849Sken		 * hang.  This was reproduced with Alteon 180 switches.
323298849Sken		 */
323398849Sken#if 0
323498849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
3235131652Sbms			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
323698849Sken#endif
323798849Sken
323845386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
323998849Sken		    TI_GLNK_FULL_DUPLEX| flowctl |
324045386Swpaul		    TI_GLNK_AUTONEGENB|TI_GLNK_ENB);
324198849Sken
324298849Sken		flowctl = TI_LNK_RX_FLOWCTL_Y;
324398849Sken#if 0
324498849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
324598849Sken			flowctl |= TI_LNK_TX_FLOWCTL_Y;
324698849Sken#endif
324798849Sken
324845386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_100MB|TI_LNK_10MB|
324998849Sken		    TI_LNK_FULL_DUPLEX|TI_LNK_HALF_DUPLEX| flowctl |
325045386Swpaul		    TI_LNK_AUTONEGENB|TI_LNK_ENB);
325145386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
325245386Swpaul		    TI_CMD_CODE_NEGOTIATE_BOTH, 0);
325345386Swpaul		break;
325445386Swpaul	case IFM_1000_SX:
325595673Sphk	case IFM_1000_T:
325698849Sken		flowctl = TI_GLNK_RX_FLOWCTL_Y;
325798849Sken#if 0
325898849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
3259131652Sbms			flowctl |= TI_GLNK_TX_FLOWCTL_Y;
326098849Sken#endif
326198849Sken
326245386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, TI_GLNK_PREF|TI_GLNK_1000MB|
326398849Sken		    flowctl |TI_GLNK_ENB);
326445386Swpaul		CSR_WRITE_4(sc, TI_GCR_LINK, 0);
326563699Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
326663699Swpaul			TI_SETBIT(sc, TI_GCR_GLINK, TI_GLNK_FULL_DUPLEX);
326763699Swpaul		}
326845386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
326945386Swpaul		    TI_CMD_CODE_NEGOTIATE_GIGABIT, 0);
327045386Swpaul		break;
327145386Swpaul	case IFM_100_FX:
327245386Swpaul	case IFM_10_FL:
327363699Swpaul	case IFM_100_TX:
327463699Swpaul	case IFM_10_T:
327598849Sken		flowctl = TI_LNK_RX_FLOWCTL_Y;
327698849Sken#if 0
327798849Sken		if (sc->ti_hwrev != TI_HWREV_TIGON)
327898849Sken			flowctl |= TI_LNK_TX_FLOWCTL_Y;
327998849Sken#endif
328098849Sken
328145386Swpaul		CSR_WRITE_4(sc, TI_GCR_GLINK, 0);
328298849Sken		CSR_WRITE_4(sc, TI_GCR_LINK, TI_LNK_ENB|TI_LNK_PREF|flowctl);
328363699Swpaul		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_100_FX ||
328463699Swpaul		    IFM_SUBTYPE(ifm->ifm_media) == IFM_100_TX) {
328545386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_100MB);
328645386Swpaul		} else {
328745386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_10MB);
328845386Swpaul		}
328945386Swpaul		if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) {
329045386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_FULL_DUPLEX);
329145386Swpaul		} else {
329245386Swpaul			TI_SETBIT(sc, TI_GCR_LINK, TI_LNK_HALF_DUPLEX);
329345386Swpaul		}
329445386Swpaul		TI_DO_CMD(TI_CMD_LINK_NEGOTIATION,
329545386Swpaul		    TI_CMD_CODE_NEGOTIATE_10_100, 0);
329645386Swpaul		break;
329745386Swpaul	}
329845386Swpaul
3299131654Sbms	return (0);
330045386Swpaul}
330145386Swpaul
330245386Swpaul/*
330345386Swpaul * Report current media status.
330445386Swpaul */
3305102336Salfredstatic void
3306227086Syongariti_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
330745386Swpaul{
3308227087Syongari	struct ti_softc *sc;
3309227089Syongari	uint32_t media = 0;
331045386Swpaul
331145386Swpaul	sc = ifp->if_softc;
331245386Swpaul
3313227093Syongari	TI_LOCK(sc);
3314227093Syongari
331545386Swpaul	ifmr->ifm_status = IFM_AVALID;
331645386Swpaul	ifmr->ifm_active = IFM_ETHER;
331745386Swpaul
3318227093Syongari	if (sc->ti_linkstat == TI_EV_CODE_LINK_DOWN) {
3319227093Syongari		TI_UNLOCK(sc);
332045386Swpaul		return;
3321227093Syongari	}
332245386Swpaul
332345386Swpaul	ifmr->ifm_status |= IFM_ACTIVE;
332445386Swpaul
332563699Swpaul	if (sc->ti_linkstat == TI_EV_CODE_GIG_LINK_UP) {
332663699Swpaul		media = CSR_READ_4(sc, TI_GCR_GLINK_STAT);
332763699Swpaul		if (sc->ti_copper)
332895673Sphk			ifmr->ifm_active |= IFM_1000_T;
332963699Swpaul		else
333063699Swpaul			ifmr->ifm_active |= IFM_1000_SX;
333163699Swpaul		if (media & TI_GLNK_FULL_DUPLEX)
333263699Swpaul			ifmr->ifm_active |= IFM_FDX;
333363699Swpaul		else
333463699Swpaul			ifmr->ifm_active |= IFM_HDX;
333563699Swpaul	} else if (sc->ti_linkstat == TI_EV_CODE_LINK_UP) {
333645386Swpaul		media = CSR_READ_4(sc, TI_GCR_LINK_STAT);
333763699Swpaul		if (sc->ti_copper) {
333863699Swpaul			if (media & TI_LNK_100MB)
333963699Swpaul				ifmr->ifm_active |= IFM_100_TX;
334063699Swpaul			if (media & TI_LNK_10MB)
334163699Swpaul				ifmr->ifm_active |= IFM_10_T;
334263699Swpaul		} else {
334363699Swpaul			if (media & TI_LNK_100MB)
334463699Swpaul				ifmr->ifm_active |= IFM_100_FX;
334563699Swpaul			if (media & TI_LNK_10MB)
334663699Swpaul				ifmr->ifm_active |= IFM_10_FL;
334763699Swpaul		}
334845386Swpaul		if (media & TI_LNK_FULL_DUPLEX)
334945386Swpaul			ifmr->ifm_active |= IFM_FDX;
335045386Swpaul		if (media & TI_LNK_HALF_DUPLEX)
335145386Swpaul			ifmr->ifm_active |= IFM_HDX;
335245386Swpaul	}
3353227093Syongari	TI_UNLOCK(sc);
335445386Swpaul}
335545386Swpaul
3356102336Salfredstatic int
3357227086Syongariti_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
335845386Swpaul{
3359227087Syongari	struct ti_softc *sc = ifp->if_softc;
3360227087Syongari	struct ifreq *ifr = (struct ifreq *) data;
3361227087Syongari	struct ti_cmd_desc cmd;
3362227087Syongari	int mask, error = 0;
336345386Swpaul
3364131654Sbms	switch (command) {
336545386Swpaul	case SIOCSIFMTU:
3366153770Syongari		TI_LOCK(sc);
336745386Swpaul		if (ifr->ifr_mtu > TI_JUMBO_MTU)
336845386Swpaul			error = EINVAL;
336945386Swpaul		else {
337045386Swpaul			ifp->if_mtu = ifr->ifr_mtu;
3371153770Syongari			ti_init_locked(sc);
337245386Swpaul		}
3373153770Syongari		TI_UNLOCK(sc);
337445386Swpaul		break;
337545386Swpaul	case SIOCSIFFLAGS:
3376153770Syongari		TI_LOCK(sc);
337745386Swpaul		if (ifp->if_flags & IFF_UP) {
337845386Swpaul			/*
337945386Swpaul			 * If only the state of the PROMISC flag changed,
338045386Swpaul			 * then just use the 'set promisc mode' command
338145386Swpaul			 * instead of reinitializing the entire NIC. Doing
338245386Swpaul			 * a full re-init means reloading the firmware and
338345386Swpaul			 * waiting for it to start up, which may take a
338445386Swpaul			 * second or two.
338545386Swpaul			 */
3386148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
338745386Swpaul			    ifp->if_flags & IFF_PROMISC &&
338845386Swpaul			    !(sc->ti_if_flags & IFF_PROMISC)) {
338945386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
339045386Swpaul				    TI_CMD_CODE_PROMISC_ENB, 0);
3391148887Srwatson			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
339245386Swpaul			    !(ifp->if_flags & IFF_PROMISC) &&
339345386Swpaul			    sc->ti_if_flags & IFF_PROMISC) {
339445386Swpaul				TI_DO_CMD(TI_CMD_SET_PROMISC_MODE,
339545386Swpaul				    TI_CMD_CODE_PROMISC_DIS, 0);
339645386Swpaul			} else
3397153770Syongari				ti_init_locked(sc);
339845386Swpaul		} else {
3399148887Srwatson			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
340045386Swpaul				ti_stop(sc);
340145386Swpaul			}
340245386Swpaul		}
340345386Swpaul		sc->ti_if_flags = ifp->if_flags;
3404153770Syongari		TI_UNLOCK(sc);
340545386Swpaul		break;
340645386Swpaul	case SIOCADDMULTI:
340745386Swpaul	case SIOCDELMULTI:
3408153770Syongari		TI_LOCK(sc);
3409153770Syongari		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
341045386Swpaul			ti_setmulti(sc);
3411153770Syongari		TI_UNLOCK(sc);
341245386Swpaul		break;
341345386Swpaul	case SIOCSIFMEDIA:
341445386Swpaul	case SIOCGIFMEDIA:
341545386Swpaul		error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
341645386Swpaul		break;
341783630Sjlemon	case SIOCSIFCAP:
3418153770Syongari		TI_LOCK(sc);
341983630Sjlemon		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3420227095Syongari		if ((mask & IFCAP_TXCSUM) != 0 &&
3421227095Syongari		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3422227095Syongari			ifp->if_capenable ^= IFCAP_TXCSUM;
3423227095Syongari			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3424227095Syongari				ifp->if_hwassist |= TI_CSUM_FEATURES;
3425227095Syongari                        else
3426227095Syongari				ifp->if_hwassist &= ~TI_CSUM_FEATURES;
3427227095Syongari                }
3428227095Syongari		if ((mask & IFCAP_RXCSUM) != 0 &&
3429227095Syongari		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0)
3430227095Syongari			ifp->if_capenable ^= IFCAP_RXCSUM;
3431227095Syongari		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3432227095Syongari		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0)
3433227095Syongari                        ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3434227095Syongari		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
3435227095Syongari		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
3436227095Syongari			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
3437227095Syongari		if ((mask & (IFCAP_TXCSUM | IFCAP_RXCSUM |
3438227095Syongari		    IFCAP_VLAN_HWTAGGING)) != 0) {
3439227095Syongari			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3440227095Syongari				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3441153770Syongari				ti_init_locked(sc);
3442227095Syongari			}
3443131655Sbms		}
3444153770Syongari		TI_UNLOCK(sc);
3445227095Syongari		VLAN_CAPABILITIES(ifp);
344683630Sjlemon		break;
344745386Swpaul	default:
3448106936Ssam		error = ether_ioctl(ifp, command, data);
344945386Swpaul		break;
345045386Swpaul	}
345145386Swpaul
3452131654Sbms	return (error);
345345386Swpaul}
345445386Swpaul
345598849Skenstatic int
3456130585Sphkti_open(struct cdev *dev, int flags, int fmt, struct thread *td)
345798849Sken{
345898849Sken	struct ti_softc *sc;
345998849Sken
3460120980Sphk	sc = dev->si_drv1;
346198849Sken	if (sc == NULL)
3462131654Sbms		return (ENODEV);
346398849Sken
346498849Sken	TI_LOCK(sc);
346598849Sken	sc->ti_flags |= TI_FLAG_DEBUGING;
346698849Sken	TI_UNLOCK(sc);
346798849Sken
3468131654Sbms	return (0);
346998849Sken}
347098849Sken
347198849Skenstatic int
3472130585Sphkti_close(struct cdev *dev, int flag, int fmt, struct thread *td)
347398849Sken{
347498849Sken	struct ti_softc *sc;
347598849Sken
3476120980Sphk	sc = dev->si_drv1;
347798849Sken	if (sc == NULL)
3478131654Sbms		return (ENODEV);
347998849Sken
348098849Sken	TI_LOCK(sc);
348198849Sken	sc->ti_flags &= ~TI_FLAG_DEBUGING;
348298849Sken	TI_UNLOCK(sc);
348398849Sken
3484131654Sbms	return (0);
348598849Sken}
348698849Sken
348798849Sken/*
348898849Sken * This ioctl routine goes along with the Tigon character device.
348998849Sken */
3490131652Sbmsstatic int
3491150719Sjhbti_ioctl2(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
3492150719Sjhb    struct thread *td)
349398849Sken{
3494227087Syongari	struct ti_softc *sc;
3495120980Sphk	int error;
349698849Sken
3497120980Sphk	sc = dev->si_drv1;
349898849Sken	if (sc == NULL)
3499131654Sbms		return (ENODEV);
350098849Sken
350198849Sken	error = 0;
350298849Sken
3503131654Sbms	switch (cmd) {
350498849Sken	case TIIOCGETSTATS:
350598849Sken	{
350698849Sken		struct ti_stats *outstats;
350798849Sken
350898849Sken		outstats = (struct ti_stats *)addr;
350998849Sken
3510153281Sscottl		TI_LOCK(sc);
351198849Sken		bcopy(&sc->ti_rdata->ti_info.ti_stats, outstats,
351298849Sken		      sizeof(struct ti_stats));
3513153281Sscottl		TI_UNLOCK(sc);
351498849Sken		break;
351598849Sken	}
351698849Sken	case TIIOCGETPARAMS:
351798849Sken	{
3518227087Syongari		struct ti_params *params;
351998849Sken
352098849Sken		params = (struct ti_params *)addr;
352198849Sken
3522153281Sscottl		TI_LOCK(sc);
352398849Sken		params->ti_stat_ticks = sc->ti_stat_ticks;
352498849Sken		params->ti_rx_coal_ticks = sc->ti_rx_coal_ticks;
352598849Sken		params->ti_tx_coal_ticks = sc->ti_tx_coal_ticks;
352698849Sken		params->ti_rx_max_coal_bds = sc->ti_rx_max_coal_bds;
352798849Sken		params->ti_tx_max_coal_bds = sc->ti_tx_max_coal_bds;
352898849Sken		params->ti_tx_buf_ratio = sc->ti_tx_buf_ratio;
352998849Sken		params->param_mask = TI_PARAM_ALL;
3530153281Sscottl		TI_UNLOCK(sc);
353198849Sken
353298849Sken		error = 0;
353398849Sken
353498849Sken		break;
353598849Sken	}
353698849Sken	case TIIOCSETPARAMS:
353798849Sken	{
353898849Sken		struct ti_params *params;
353998849Sken
354098849Sken		params = (struct ti_params *)addr;
354198849Sken
3542153281Sscottl		TI_LOCK(sc);
354398849Sken		if (params->param_mask & TI_PARAM_STAT_TICKS) {
354498849Sken			sc->ti_stat_ticks = params->ti_stat_ticks;
354598849Sken			CSR_WRITE_4(sc, TI_GCR_STAT_TICKS, sc->ti_stat_ticks);
354698849Sken		}
354798849Sken
354898849Sken		if (params->param_mask & TI_PARAM_RX_COAL_TICKS) {
354998849Sken			sc->ti_rx_coal_ticks = params->ti_rx_coal_ticks;
355098849Sken			CSR_WRITE_4(sc, TI_GCR_RX_COAL_TICKS,
355198849Sken				    sc->ti_rx_coal_ticks);
355298849Sken		}
355398849Sken
355498849Sken		if (params->param_mask & TI_PARAM_TX_COAL_TICKS) {
355598849Sken			sc->ti_tx_coal_ticks = params->ti_tx_coal_ticks;
355698849Sken			CSR_WRITE_4(sc, TI_GCR_TX_COAL_TICKS,
355798849Sken				    sc->ti_tx_coal_ticks);
355898849Sken		}
355998849Sken
356098849Sken		if (params->param_mask & TI_PARAM_RX_COAL_BDS) {
356198849Sken			sc->ti_rx_max_coal_bds = params->ti_rx_max_coal_bds;
356298849Sken			CSR_WRITE_4(sc, TI_GCR_RX_MAX_COAL_BD,
356398849Sken				    sc->ti_rx_max_coal_bds);
356498849Sken		}
356598849Sken
356698849Sken		if (params->param_mask & TI_PARAM_TX_COAL_BDS) {
356798849Sken			sc->ti_tx_max_coal_bds = params->ti_tx_max_coal_bds;
356898849Sken			CSR_WRITE_4(sc, TI_GCR_TX_MAX_COAL_BD,
356998849Sken				    sc->ti_tx_max_coal_bds);
357098849Sken		}
357198849Sken
357298849Sken		if (params->param_mask & TI_PARAM_TX_BUF_RATIO) {
357398849Sken			sc->ti_tx_buf_ratio = params->ti_tx_buf_ratio;
357498849Sken			CSR_WRITE_4(sc, TI_GCR_TX_BUFFER_RATIO,
357598849Sken				    sc->ti_tx_buf_ratio);
357698849Sken		}
3577153281Sscottl		TI_UNLOCK(sc);
357898849Sken
357998849Sken		error = 0;
358098849Sken
358198849Sken		break;
358298849Sken	}
358398849Sken	case TIIOCSETTRACE: {
358498849Sken		ti_trace_type	trace_type;
358598849Sken
358698849Sken		trace_type = *(ti_trace_type *)addr;
358798849Sken
358898849Sken		/*
358998849Sken		 * Set tracing to whatever the user asked for.  Setting
359098849Sken		 * this register to 0 should have the effect of disabling
359198849Sken		 * tracing.
359298849Sken		 */
359398849Sken		CSR_WRITE_4(sc, TI_GCR_NIC_TRACING, trace_type);
359498849Sken
359598849Sken		error = 0;
359698849Sken
359798849Sken		break;
359898849Sken	}
359998849Sken	case TIIOCGETTRACE: {
3600227087Syongari		struct ti_trace_buf *trace_buf;
3601227089Syongari		uint32_t trace_start, cur_trace_ptr, trace_len;
360298849Sken
360398849Sken		trace_buf = (struct ti_trace_buf *)addr;
360498849Sken
3605153281Sscottl		TI_LOCK(sc);
360698849Sken		trace_start = CSR_READ_4(sc, TI_GCR_NICTRACE_START);
360798849Sken		cur_trace_ptr = CSR_READ_4(sc, TI_GCR_NICTRACE_PTR);
360898849Sken		trace_len = CSR_READ_4(sc, TI_GCR_NICTRACE_LEN);
360998849Sken
361098849Sken#if 0
3611150719Sjhb		if_printf(sc->ti_ifp, "trace_start = %#x, cur_trace_ptr = %#x, "
3612150719Sjhb		       "trace_len = %d\n", trace_start,
361398849Sken		       cur_trace_ptr, trace_len);
3614150719Sjhb		if_printf(sc->ti_ifp, "trace_buf->buf_len = %d\n",
361598849Sken		       trace_buf->buf_len);
361698849Sken#endif
361798849Sken
361898849Sken		error = ti_copy_mem(sc, trace_start, min(trace_len,
361998849Sken				    trace_buf->buf_len),
362098849Sken				    (caddr_t)trace_buf->buf, 1, 1);
362198849Sken
362298849Sken		if (error == 0) {
362398849Sken			trace_buf->fill_len = min(trace_len,
362498849Sken						  trace_buf->buf_len);
362598849Sken			if (cur_trace_ptr < trace_start)
362698849Sken				trace_buf->cur_trace_ptr =
362798849Sken					trace_start - cur_trace_ptr;
362898849Sken			else
362998849Sken				trace_buf->cur_trace_ptr =
363098849Sken					cur_trace_ptr - trace_start;
363198849Sken		} else
363298849Sken			trace_buf->fill_len = 0;
3633153281Sscottl		TI_UNLOCK(sc);
363498849Sken
363598849Sken		break;
363698849Sken	}
363798849Sken
363898849Sken	/*
363998849Sken	 * For debugging, five ioctls are needed:
364098849Sken	 * ALT_ATTACH
364198849Sken	 * ALT_READ_TG_REG
364298849Sken	 * ALT_WRITE_TG_REG
364398849Sken	 * ALT_READ_TG_MEM
364498849Sken	 * ALT_WRITE_TG_MEM
364598849Sken	 */
364698849Sken	case ALT_ATTACH:
364798849Sken		/*
3648131652Sbms		 * From what I can tell, Alteon's Solaris Tigon driver
364998849Sken		 * only has one character device, so you have to attach
365098849Sken		 * to the Tigon board you're interested in.  This seems
365198849Sken		 * like a not-so-good way to do things, since unless you
365298849Sken		 * subsequently specify the unit number of the device
3653177626Sbrueffer		 * you're interested in every ioctl, you'll only be
365498849Sken		 * able to debug one board at a time.
365598849Sken		 */
365698849Sken		error = 0;
365798849Sken		break;
365898849Sken	case ALT_READ_TG_MEM:
365998849Sken	case ALT_WRITE_TG_MEM:
366098849Sken	{
366198849Sken		struct tg_mem *mem_param;
3662227089Syongari		uint32_t sram_end, scratch_end;
366398849Sken
366498849Sken		mem_param = (struct tg_mem *)addr;
366598849Sken
366698849Sken		if (sc->ti_hwrev == TI_HWREV_TIGON) {
366798849Sken			sram_end = TI_END_SRAM_I;
366898849Sken			scratch_end = TI_END_SCRATCH_I;
366998849Sken		} else {
367098849Sken			sram_end = TI_END_SRAM_II;
367198849Sken			scratch_end = TI_END_SCRATCH_II;
367298849Sken		}
367398849Sken
367498849Sken		/*
367598849Sken		 * For now, we'll only handle accessing regular SRAM,
367698849Sken		 * nothing else.
367798849Sken		 */
3678153281Sscottl		TI_LOCK(sc);
367998849Sken		if ((mem_param->tgAddr >= TI_BEG_SRAM)
368098849Sken		 && ((mem_param->tgAddr + mem_param->len) <= sram_end)) {
368198849Sken			/*
368298849Sken			 * In this instance, we always copy to/from user
368398849Sken			 * space, so the user space argument is set to 1.
368498849Sken			 */
368598849Sken			error = ti_copy_mem(sc, mem_param->tgAddr,
368698849Sken					    mem_param->len,
368798849Sken					    mem_param->userAddr, 1,
368898849Sken					    (cmd == ALT_READ_TG_MEM) ? 1 : 0);
368998849Sken		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH)
369098849Sken			&& (mem_param->tgAddr <= scratch_end)) {
369198849Sken			error = ti_copy_scratch(sc, mem_param->tgAddr,
369298849Sken						mem_param->len,
369398849Sken						mem_param->userAddr, 1,
369498849Sken						(cmd == ALT_READ_TG_MEM) ?
369598849Sken						1 : 0, TI_PROCESSOR_A);
369698849Sken		} else if ((mem_param->tgAddr >= TI_BEG_SCRATCH_B_DEBUG)
369798849Sken			&& (mem_param->tgAddr <= TI_BEG_SCRATCH_B_DEBUG)) {
369898849Sken			if (sc->ti_hwrev == TI_HWREV_TIGON) {
3699150719Sjhb				if_printf(sc->ti_ifp,
3700150719Sjhb				    "invalid memory range for Tigon I\n");
370198849Sken				error = EINVAL;
370298849Sken				break;
370398849Sken			}
3704131652Sbms			error = ti_copy_scratch(sc, mem_param->tgAddr -
370598849Sken						TI_SCRATCH_DEBUG_OFF,
370698849Sken						mem_param->len,
370798849Sken						mem_param->userAddr, 1,
370898849Sken						(cmd == ALT_READ_TG_MEM) ?
370998849Sken						1 : 0, TI_PROCESSOR_B);
371098849Sken		} else {
3711150719Sjhb			if_printf(sc->ti_ifp, "memory address %#x len %d is "
3712150719Sjhb			        "out of supported range\n",
371398849Sken			        mem_param->tgAddr, mem_param->len);
371498849Sken			error = EINVAL;
371598849Sken		}
3716153281Sscottl		TI_UNLOCK(sc);
371798849Sken
371898849Sken		break;
371998849Sken	}
372098849Sken	case ALT_READ_TG_REG:
372198849Sken	case ALT_WRITE_TG_REG:
372298849Sken	{
372398849Sken		struct tg_reg	*regs;
3724227089Syongari		uint32_t	tmpval;
372598849Sken
372698849Sken		regs = (struct tg_reg *)addr;
372798849Sken
372898849Sken		/*
372998849Sken		 * Make sure the address in question isn't out of range.
373098849Sken		 */
373198849Sken		if (regs->addr > TI_REG_MAX) {
373298849Sken			error = EINVAL;
373398849Sken			break;
373498849Sken		}
3735153281Sscottl		TI_LOCK(sc);
373698849Sken		if (cmd == ALT_READ_TG_REG) {
373798849Sken			bus_space_read_region_4(sc->ti_btag, sc->ti_bhandle,
373898849Sken						regs->addr, &tmpval, 1);
373998849Sken			regs->data = ntohl(tmpval);
374098849Sken#if 0
374198849Sken			if ((regs->addr == TI_CPU_STATE)
374298849Sken			 || (regs->addr == TI_CPU_CTL_B)) {
3743150719Sjhb				if_printf(sc->ti_ifp, "register %#x = %#x\n",
3744150719Sjhb				       regs->addr, tmpval);
374598849Sken			}
374698849Sken#endif
374798849Sken		} else {
374898849Sken			tmpval = htonl(regs->data);
374998849Sken			bus_space_write_region_4(sc->ti_btag, sc->ti_bhandle,
375098849Sken						 regs->addr, &tmpval, 1);
375198849Sken		}
3752153281Sscottl		TI_UNLOCK(sc);
375398849Sken
375498849Sken		break;
375598849Sken	}
375698849Sken	default:
375798849Sken		error = ENOTTY;
375898849Sken		break;
375998849Sken	}
3760131654Sbms	return (error);
376198849Sken}
376298849Sken
3763102336Salfredstatic void
3764199559Sjhbti_watchdog(void *arg)
376545386Swpaul{
3766227087Syongari	struct ti_softc *sc;
3767227087Syongari	struct ifnet *ifp;
376845386Swpaul
3769199559Sjhb	sc = arg;
3770199559Sjhb	TI_LOCK_ASSERT(sc);
3771199559Sjhb	callout_reset(&sc->ti_watchdog, hz, ti_watchdog, sc);
3772199559Sjhb	if (sc->ti_timer == 0 || --sc->ti_timer > 0)
3773199559Sjhb		return;
377445386Swpaul
377598849Sken	/*
377698849Sken	 * When we're debugging, the chip is often stopped for long periods
377798849Sken	 * of time, and that would normally cause the watchdog timer to fire.
377898849Sken	 * Since that impedes debugging, we don't want to do that.
377998849Sken	 */
3780199559Sjhb	if (sc->ti_flags & TI_FLAG_DEBUGING)
378198849Sken		return;
378298849Sken
3783199559Sjhb	ifp = sc->ti_ifp;
3784150719Sjhb	if_printf(ifp, "watchdog timeout -- resetting\n");
378545386Swpaul	ti_stop(sc);
3786153770Syongari	ti_init_locked(sc);
378745386Swpaul
378845386Swpaul	ifp->if_oerrors++;
378945386Swpaul}
379045386Swpaul
379145386Swpaul/*
379245386Swpaul * Stop the adapter and free any mbufs allocated to the
379345386Swpaul * RX and TX lists.
379445386Swpaul */
3795102336Salfredstatic void
3796227086Syongariti_stop(struct ti_softc *sc)
379745386Swpaul{
3798227087Syongari	struct ifnet *ifp;
3799227087Syongari	struct ti_cmd_desc cmd;
380045386Swpaul
3801153770Syongari	TI_LOCK_ASSERT(sc);
380267087Swpaul
3803147256Sbrooks	ifp = sc->ti_ifp;
380445386Swpaul
380545386Swpaul	/* Disable host interrupts. */
380645386Swpaul	CSR_WRITE_4(sc, TI_MB_HOSTINTR, 1);
380745386Swpaul	/*
380845386Swpaul	 * Tell firmware we're shutting down.
380945386Swpaul	 */
381045386Swpaul	TI_DO_CMD(TI_CMD_HOST_STATE, TI_CMD_CODE_STACK_DOWN, 0);
381145386Swpaul
381245386Swpaul	/* Halt and reinitialize. */
3813153770Syongari	if (ti_chipinit(sc) != 0)
3814153770Syongari		return;
3815153770Syongari	ti_mem_zero(sc, 0x2000, 0x100000 - 0x2000);
3816153770Syongari	if (ti_chipinit(sc) != 0)
3817153770Syongari		return;
381845386Swpaul
381945386Swpaul	/* Free the RX lists. */
382045386Swpaul	ti_free_rx_ring_std(sc);
382145386Swpaul
382245386Swpaul	/* Free jumbo RX list. */
382345386Swpaul	ti_free_rx_ring_jumbo(sc);
382445386Swpaul
382545386Swpaul	/* Free mini RX list. */
382645386Swpaul	ti_free_rx_ring_mini(sc);
382745386Swpaul
382845386Swpaul	/* Free TX buffers. */
382945386Swpaul	ti_free_tx_ring(sc);
383045386Swpaul
383145386Swpaul	sc->ti_ev_prodidx.ti_idx = 0;
383245386Swpaul	sc->ti_return_prodidx.ti_idx = 0;
383345386Swpaul	sc->ti_tx_considx.ti_idx = 0;
383445386Swpaul	sc->ti_tx_saved_considx = TI_TXCONS_UNSET;
383545386Swpaul
3836148887Srwatson	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3837199559Sjhb	callout_stop(&sc->ti_watchdog);
383845386Swpaul}
383945386Swpaul
384045386Swpaul/*
384145386Swpaul * Stop all chip I/O so that the kernel's probe routines don't
384245386Swpaul * get confused by errant DMAs when rebooting.
384345386Swpaul */
3844173839Syongaristatic int
3845227086Syongariti_shutdown(device_t dev)
384645386Swpaul{
3847227087Syongari	struct ti_softc *sc;
384845386Swpaul
384949011Swpaul	sc = device_get_softc(dev);
385067087Swpaul	TI_LOCK(sc);
385145386Swpaul	ti_chipinit(sc);
385267087Swpaul	TI_UNLOCK(sc);
3853173839Syongari
3854173839Syongari	return (0);
385545386Swpaul}
3856